(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)))(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)))))))(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)))))(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)))