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