gpt4 book ai didi

emacs - "Diff, save or kill"在 Emacs 中杀死缓冲区时

转载 作者:行者123 更新时间:2023-12-03 12:44:05 25 4
gpt4 key购买 nike

当试图杀死包含 Emacs 更改的缓冲区时,消息:
显示“缓冲区 [缓冲区] 已修改;仍然杀死?(是或否)”。

而不是这个,我想让 Emacs 问我是否想:
1.查看变化的差异,
2.保存缓冲区,
3. 杀死缓冲区。

如何?

最佳答案

答案在于使用 advice , 因为在终止缓冲区运行时钩子(Hook)通常会运行 之后 您要更改的“缓冲区已修改”提示。

以下建议可以满足您的要求(我认为)。几点注意事项:

  • 运行差异时,原始缓冲区被标记为未修改 - 但您确实需要保存它。
  • 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)))

    关于emacs - "Diff, save or kill"在 Emacs 中杀死缓冲区时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/331569/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com