Paste number 10147: A good way to avoid multiple ,@body ?

Index of paste annotations: 3 | 2 | 1

Paste number 10147: A good way to avoid multiple ,@body ?
Pasted by: bpalmer
When:6 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+7TV
Channel:#xemacs
Paste contents:
Raw Source | XML | Display As
(defmacro with-renamed-buffer (buffer &rest body)
  "Rename a buffer to a unique name"
  `(let* ((b (get-buffer buffer))
	  (name (buffer-name b)))
     (if b
	 (unwind-protect
	     (progn (set-buffer b)
		    (rename-uniquely)
		    ,@body)
	   (set-buffer b)
	   (rename-buffer b t))
       ,@body)))

Annotations for this paste:

Annotation number 3: with gensyms
Pasted by: bpalmer
When:6 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+7TV/3
Paste contents:
Raw Source | Display As
(defmacro with-renamed-buffer (buffer &rest body)
  "Rename a buffer to a unique name temporarily, while BODY executes.
This is useful so that new shell buffers can be created without
running into any issues about an existing *shell* buffer, for example"
  (let ((b (gensym))
	(bufname (gensym)))
  `(let* ((,b (get-buffer ,buffer))
	  (,bufname (buffer-name b)))
     (unwind-protect
	 (progn (when ,b (with-current-buffer ,b
			  (rename-uniquely)))
		,@body)
       (when (and ,b (buffer-live-p ,b))
	 (with-current-buffer ,b
	   (rename-buffer ,bufname t)))))))

Annotation number 2: seems to work, ignoring variable capture
Pasted by: bpalmer
When:6 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+7TV/2
Paste contents:
Raw Source | Display As
(defmacro with-renamed-buffer (buffer &rest body)
  "Rename a buffer to a unique name temporarily, while BODY executes.
This is useful so that new shell buffers can be created without
running into any issues about an existing *shell* buffer, for example"
  `(let* ((b (get-buffer ,buffer))
	  (name (buffer-name b)))
     (unwind-protect
	 (progn (when b (with-current-buffer b
			  (rename-uniquely)))
		,@body)
       (when (and b (buffer-live-p b))
	 (with-current-buffer b
	   (rename-buffer name t))))))
(put 'with-renamed-buffer 'lisp-indent-function 1)

(defun shell-current-directory ()
  "Create a shell pertaining to the current directory."  
  (interactive)
  (let ((current-shell-buffer (directory-shell-buffer)))
    (if current-shell-buffer
	(pop-to-buffer current-shell-buffer)      
      (with-renamed-buffer "*shell*"
	(shell)
	(rename-buffer (directory-shell-buffer-name) t)))))

Annotation number 1: slightly cleaned
Pasted by: bpalmer
When:6 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+7TV/1
Paste contents:
Raw Source | Display As
(defmacro with-renamed-buffer (buffer &rest body)
  "Rename a buffer to a unique name temporarily, while BODY executes.
This is useful so that new shell buffers can be created without
running into any issues about an existing *shell* buffer, for example"
  `(let* ((b (get-buffer buffer))
	  (name (buffer-name b)))
     (if b
	 (unwind-protect
	     (progn (with-current-buffer b
		      (rename-uniquely))
		    ,@body)
	   (with-current-buffer b
	     (rename-buffer name t)))
       ,@body)))

Colorize as:
Show Line Numbers
Index of paste annotations: 3 | 2 | 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.