当试图杀死包含Emacs中的更改的缓冲区时,会显示消息:"Buffer [buffer] modified; kill all any?(yes or no)".
而不是这个我想让Emacs问我是否要:1.查看更改的差异,2.保存缓冲区,3.杀死缓冲区.
怎么样?
答案在于使用建议,因为钩子通常在杀死缓冲区时运行,并且在您想要更改的"缓冲区修改"提示之后运行.
以下建议可以满足您的需求(我认为).几个笔记:
运行diff时,原始缓冲区被标记为未修改 - 但您确实需要保存它.
diff中的另一个缓冲区不会被删除
(defadvice kill-buffer (around my-kill-buffer-check activate) "Prompt when a buffer is about to be killed." (let* ((buffer-file-name (buffer-file-name)) backup-file) ;; see 'backup-buffer (if (and (buffer-modified-p) buffer-file-name (file-exists-p buffer-file-name) (setq backup-file (car (find-backup-file-name buffer-file-name)))) (let ((answer (completing-read (format "Buffer modified %s, (d)iff, (s)ave, (k)ill? " (buffer-name)) '("d" "s" "k") nil t))) (cond ((equal answer "d") (set-buffer-modified-p nil) (let ((orig-buffer (current-buffer)) (file-to-diff (if (file-newer-than-file-p buffer-file-name backup-file) buffer-file-name backup-file))) (set-buffer (get-buffer-create (format "%s last-revision" (file-name-nondirectory file-to-diff)))) (buffer-disable-undo) (insert-file-contents file-to-diff nil nil nil t) (set-buffer-modified-p nil) (setq buffer-read-only t) (ediff-buffers (current-buffer) orig-buffer))) ((equal answer "k") (set-buffer-modified-p nil) ad-do-it) (t (save-buffer) ad-do-it))) ad-do-it)))
您将要编写一些代码以放入kill-buffer-hooks和 write-file-functions列表.从概念上讲,你想要做的是
测试缓冲区是否已被修改
显示您的消息并获得响应,并按照要求执行操作
然后清除修改后的标志,这样正常的kill-buffer就不会再回来再问.