防止Emacs询问“存在已修改的缓冲区;仍要退出吗?”

15
每当我尝试退出Emacs时,它都会询问我是否要保存任何已修改的缓冲区。如果我回答“否”,它将会问我:

存在已修改的缓冲区; 确认退出吗?(y or n)

有没有办法阻止Emacs询问我最后一个问题呢?

这就是如何完成的:https://dev59.com/sHE85IYBdhLWcg3wl0jF#10780124。 - yPhil
3个回答

18

有多种方法可以实现这一点:

您可以建议使用save-buffers-kill-emacs函数:

(defadvice save-buffers-kill-emacs (around no-y-or-n activate)
  (flet ((yes-or-no-p (&rest args) t)
         (y-or-n-p (&rest args) t))
    ad-do-it))

这样做的缺点是它也会绕过Emacs中活动进程的检查(在文件缓冲区检查之后进行)。因此,最安全的做法可能是编写自己的save-buffers-kill-emacs版本。
(defun my-save-buffers-kill-emacs (&optional arg)
  "Offer to save each buffer(once only), then kill this Emacs process.
With prefix ARG, silently save all file-visiting buffers, then kill."
  (interactive "P")
  (save-some-buffers arg t)
  (and (or (not (fboundp 'process-list))
       ;; process-list is not defined on MSDOS.
       (let ((processes (process-list))
         active)
         (while processes
           (and (memq (process-status (car processes)) '(run stop open listen))
            (process-query-on-exit-flag (car processes))
            (setq active t))
           (setq processes (cdr processes)))
         (or (not active)
         (progn (list-processes t)
            (yes-or-no-p "Active processes exist; kill them and exit anyway? ")))))
       ;; Query the user for other things, perhaps.
       (run-hook-with-args-until-failure 'kill-emacs-query-functions)
       (or (null confirm-kill-emacs)
       (funcall confirm-kill-emacs "Really exit Emacs? "))
       (kill-emacs)))

并将其绑定到标准的 C-x C-c 键绑定中:

(global-set-key (kbd "C-x C-c") 'my-save-buffers-kill-emacs)

或将其设置为“save-buffers-kill-emacs”:
(fset 'save-buffers-kill-emacs 'my-save-buffers-kill-emacs)

如果您像您建议的那样建议使用yes-or-no-p,那么它不仅会影响到save-buffers-kill-emacs中的调用(这是OP想要更改的),而且还会影响到save-some-buffers中的调用(据我所知,OP并不想更改)。 - Gareth Rees
不,第一次保存缓冲区提示仍将出现。您将被提示一次是否应保存文件缓冲区,但不会出现第二个“修改的缓冲区存在;仍要退出吗?(y或n)”提示。 - zev
啊,狡猾:save-some-buffers 使用 map-y-or-n-p 而不是 y-or-n-p,所以你的建议对它没有影响。 - Gareth Rees
感谢您的详细回答。 - Tassos

7
您可以将以下内容添加到您的.emacs文件中,这将提示您保存未保存的文件更改,然后退出而无需进一步确认:
(defun my-kill-emacs ()
  "save some buffers, then exit unconditionally"
  (interactive)
  (save-some-buffers nil t)
  (kill-emacs))
(global-set-key (kbd "C-x C-c") 'my-kill-emacs)

6
如果你查看save-buffers-kill-emacs源代码,你会发现没有用户选项可以关闭这个问题。
所以我很抱歉,最简单的方法是编写自己版本的save-buffers-kill-emacs跳过确认(请参阅Trey Jackson的答案)。
然而,我认为更好的方法是改变你的工作习惯,这样你就不必经常退出Emacs了。如果你经常退出Emacs,那么这意味着你没有充分利用Emacs的客户端/服务器功能,或者它在交互式shell、编辑远程机器上的文件、连接多个终端等方面的能力。

我想你是对的,我应该改变习惯。感谢建议。+1 - Tassos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接