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.
;; Rename buffers along with files.
;; 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)
(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.
;;;--------------------------------------------------------------------------
;;; Improved compilation machinery.