我跟着用例烦恼了很多。
foo.rb
。foo.rb
重命名为bar.rb
使用外部工具。(例如从终端重命名)foo.rb
缓冲区,编辑它,保存C-x s
时,此缓冲区
保存为旧文件名(foo.rb
),不是预期的bar.rb
。是否有任何emacs配置可以防止这种情况?
我期待的是:
foo.rb
。foo.rb
重命名为bar.rb
使用外部工具foo.rb
缓冲区,保存时告诉我:文件被删除或提示,
另存为新文件名。答案 0 :(得分:0)
这是解决方案的开始。魔鬼的细节;可能存在许多此代码无法处理的极端情况。
(defvar do-not-save-list ())
(defun do-not-save-add-to-list (event)
(when (eq 'renamed (cadr event))
(add-to-list 'do-not-save-list
(caddr event))))
(defun do-not-save-add-watcher ()
(setq do-not-save-list (remove (buffer-file-name) do-not-save-list))
(file-notify-add-watch (buffer-file-name)
'(change)
#'do-not-save-add-to-list))
(add-hook 'find-file-hook #'do-not-save-add-watcher)
(defun do-not-save-check ()
(let ((filename (buffer-file-name)))
(when (member filename do-not-save-list)
(message "not saving %s" filename)
t ;; returns non-nil to prevent write
)))
(add-hook 'write-file-functions #'do-not-save-check)
(defun do-not-save-kill-hook ()
(setq do-not-save-list (remove (buffer-file-name) do-not-save-list)))
(add-hook 'kill-buffer-hook #'do-not-save-kill-hook)