当尝试杀死包含Emacs中的更改的缓冲区时,消息: “缓冲区[缓冲区]已修改;无论如何都要杀死?(是或否)”显示。
而不是这个我想让Emacs问我是否愿意: 1.查看变化的差异, 2.保存缓冲区, 3.杀死缓冲区。
如何?
答案 0 :(得分:21)
答案在于使用advice,因为钩子通常在杀死缓冲区时运行在“缓冲区修改”提示符后要运行
。以下建议可以满足您的需求(我认为)。几个笔记:
(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)))
答案 1 :(得分:5)
您需要编写一些代码以放入kill-buffer-hooks和write-file-functions列表。从概念上讲,你想要做的是