Emacs - 无法获得缓冲区提供 - 保存工作 [英] Emacs - Can't get buffer-offer-save working

查看:110
本文介绍了Emacs - 无法获得缓冲区提供 - 保存工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to have Emacs ask me whether I want to save a modified buffer, when that buffer is not associated with a file. To open a new buffer (not visiting a file) I have the following function in my .emacs file:

;; 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)))

我以为设置缓冲区优惠 - 保存到一些不为零的东西将成为伎俩。但是每当我用kill-this-buffer杀死缓冲区时,它会立即死机而不问任何事情。

I thought setting "buffer-offer-save" to something not nil would made the trick. But whenever I kill the buffer with "kill-this-buffer", it gets instantly killed without asking anything.

这发生在GNU Emacs 23.1.1

This happens on GNU Emacs 23.1.1

任何想法?

感谢
W

推荐答案

编辑添加使用 buffers-offer-save 。注意:变量 buffer-offer-save 仅在退出 Emacs 时使用。

您可以开始使用此代码并将其自定义为您想要的:

You can start with this code and customize it to what you want:

(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使用。如果您只是尝试以交互方式杀死一个缓冲区(与文件无关)时,系统会提示您。

Upon further reflection, that is a bit heavy-handed because you get prompted for all buffers that get killed, and there are often lots of temporary buffers that Emacs uses. If you just want to be prompted when you try to interactively kill a buffer (that isn't associated with a file).

您可以使用仅提示您的建议当你以交互方式尝试杀死缓冲区时:

You can use this advice which only prompts you when you're interactively trying to kill a buffer:

(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 - 无法获得缓冲区提供 - 保存工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆