Emacs - 无法让缓冲提供保存工作

标签 emacs elisp emacs23

我想让 Emacs 询问我是否要保存修改后的缓冲区,当该缓冲区与文件无关时。要打开一个新缓冲区(不访问文件),我的 .emacs 文件中有以下功能:

;; Creates a new empty buffer
(defun new-empty-buffer ()
  "Opens a new empty buffer."
  (interactive)
  (let ((buf (generate-new-buffer "untitled")))
    (switch-to-buffer buf)
    (funcall (and default-major-mode))
    (setq buffer-offer-save t)))

我认为将“buffer-offer-save”设置为非零值会成功。但是每当我用“kill-this-buffer”杀死缓冲区时,它会立即被杀死而不问任何事情。

这发生在 GNU Emacs 23.1.1

有任何想法吗?

谢谢,

最佳答案

编辑添加使用 buffers-offer-save .注意:变量 buffer-offer-save is only used upon exiting Emacs .

您可以从以下代码开始并根据需要对其进行自定义:

(add-to-list 'kill-buffer-query-functions 'ask-me-first)
(defun ask-me-first ()
  "prompt when killing a buffer"
  (if (or buffer-offer-save 
          (eq this-command 'kill-this-buffer)
          (and (buffer-modified-p) (not (buffer-file-name))))
      (y-or-n-p (format "Do you want to kill %s without saving? " (buffer-name)))
    t))

经过进一步思考,这有点笨手笨脚,因为系统会提示您输入所有被杀死的缓冲区,而且 Emacs 经常使用大量临时缓冲区。如果您只想在尝试以交互方式终止缓冲区(与文件无关)时收到提示。

您可以使用此建议,它仅在您以交互方式尝试终止缓冲区时提示您:
(defadvice kill-buffer (around kill-buffer-ask-first activate)
  "if called interactively, prompt before killing"
  (if (and (or buffer-offer-save (interactive-p))
           (buffer-modified-p)
           (not (buffer-file-name)))
      (let ((answ (completing-read
                   (format "Buffer '%s' modified and not associated with a file, what do you want to do? (k)ill (s)ave (a)bort? " (buffer-name))
                   '("k" "s" "a")
                   nil
                   t)))
        (when (cond ((string-match answ "k")
                     ;; kill
                     t)
                    ((string-match answ "s")
                     ;; write then kill
                     (call-interactively 'write-file)
                     t)
                    (nil))
          ad-do-it)

        t)
    ;; not prompting, just do it
    ad-do-it))

关于Emacs - 无法让缓冲提供保存工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2357881/

相关文章:

emacs - 如何告诉 emacs 在 M-x 编译或 elisp 编译错误时不拆分窗口?

emacs - emacs 中的迷你缓冲区大小随着更改默认字体而增加

Win32 上的 Emacs 与 hunspell - 个人词典未保存

emacs - 如何优雅地退出 SLIME 和 Emacs?

emacs - 如何在isearch中绑定(bind)文本插入

emacs - Emacs无法加载其启动文件

whitespace - Emacs 23.3.1 : whitespace style

emacs - 使用utf8x而不是utf8的组织模式导出

emacs - 如何重新绑定(bind)通常绑定(bind)到 C-h 和 <f1> 的 Emacs 帮助键?

emacs - 将带有动态菜单的按钮添加到emacs的modeline吗?