chiark / gitweb /
el/dot-emacs.el: Add machinery for not renaming buffers along with files.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 7 May 2020 00:09:46 +0000 (01:09 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 7 May 2020 00:09:46 +0000 (01:09 +0100)
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.

el/dot-emacs.el

index b5f45b66e6fa9d2e4507d1e9b7f90baf17ee3461..34df1a0413086bfe77fbb5567199dfa3a8200c8b 100644 (file)
@@ -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.