From: Mark Wooding Date: Thu, 7 May 2020 00:09:46 +0000 (+0100) Subject: el/dot-emacs.el: Add machinery for not renaming buffers along with files. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/profile/commitdiff_plain/52ffecb1e30140d89c6cf8de707dbb300d5c82e4?ds=inline el/dot-emacs.el: Add machinery for not renaming buffers along with files. For example: if `backup-by-copying' is nil (which it is for me), then `backup-buffer' will rename the file to its backup name -- and then we'll be left visiting the backup file, which is obviously hopeless. Add a variable `mdw-inhibit-rename-buffer' which instructs the `rename-file' advice not to rename the buffer as well, and a new macro `mdw-advise-to-inhibit-rename-buffer' to advise various functions to set this variable while they're operating. --- diff --git a/el/dot-emacs.el b/el/dot-emacs.el index b5f45b6..34df1a0 100644 --- a/el/dot-emacs.el +++ b/el/dot-emacs.el @@ -590,13 +590,31 @@ (defadvice display-buffer (before mdw-inhibit-other-frames activate) ;; Rename buffers along with files. +(defvar mdw-inhibit-rename-buffer nil + "If non-nil, `rename-file' won't rename the buffer visiting the file.") + +(defmacro mdw-advise-to-inhibit-rename-buffer (function) + "Advise FUNCTION to set `mdw-inhibit-rename-buffer' while it runs. + +This will prevent `rename-file' from renaming the buffer." + `(defadvice ,function (around mdw-inhibit-rename-buffer compile activate) + "Don't rename the buffer when renaming the underlying file." + (let ((mdw-inhibit-rename-buffer t)) + ad-do-it))) +(mdw-advise-to-inhibit-rename-buffer recode-file-name) +(mdw-advise-to-inhibit-rename-buffer set-visited-file-name) +(mdw-advise-to-inhibit-rename-buffer backup-buffer) + (defadvice rename-file (after mdw-rename-buffers (from to &optional forcep) compile activate) - "If a buffer is visiting the file, rename it to match the new name." - (let ((buffer (get-file-buffer from))) - (when buffer - (with-current-buffer buffer - (set-visited-file-name to nil t))))) + "If a buffer is visiting the file, rename it to match the new name. + +Don't do this if `mdw-inhibit-rename-buffer' is non-nil." + (unless mdw-inhibit-rename-buffer + (let ((buffer (get-file-buffer from))) + (when buffer + (with-current-buffer buffer + (set-visited-file-name to nil t)))))) ;;;-------------------------------------------------------------------------- ;;; Improved compilation machinery.