gpt4 book ai didi

emacs - 如何恢复 emacs 缓冲区列表中的缓冲区?

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

我正在尝试创建一个函数来恢复来自 emacs 的 *Buffer List* 缓冲区的缓冲区。据我从文档中得知,没有办法快速完成此操作(以 buff-menu.el 中内置的保存/标记/访问功能的方式)。所以我正在写一些 elisp。这是我目前的尝试:

(defun frobnitz ()
"Call in buffer list to revert buffer at point to file."
(interactive)
(let ((buf (buffer-menu-buffer t)))
(if (y-or-n-p (concat "Revert " (buffer-name (buf)) " ?"))
(with-current-buffer buf
(let (())
(revert-buffer t t t)
(message
(concat "Reverted " (buffer-name (buf)) "to last saved state."))
)))))

不幸的是,上面的 defun 似乎不起作用,我很难弄清楚为什么。如果我评估上面的内容,切换到 *Buffer List* 缓冲区,并调用 M-: (frobnitz),然后会出现以下错误。

Debugger entered--Lisp error: (void-function buffer-menu-buffer)
(buffer-menu-buffer t)
(let ((buf (buffer-menu-buffer t))) (if (y-or-n-p (concat "Revert " (buffer-name (buf)) " ?")) (with-current-buffer buf (let (nil) (revert-buffer t t t) (message (concat "Reverted " (buffer-name (buf)) "to last saved state."))))))
frobnitz()
eval((frobnitz) nil)
eval-expression((frobnitz) nil)
call-interactively(eval-expression nil nil)

这似乎告诉我没有函数 buffer-menu-buffer - 但这似乎也不太可能,因为 buffer-menu-buffer 是一个非常重要的中心使缓冲区菜单起作用的功能!出于类似的原因,我对自己弄乱 buffer-menu-buffer 深表警惕 - 我不想破坏缓冲区菜单。

请记住,答案可能是“调用您忽略的这个函数”,我怎样才能让这个 defun 实现其直接从缓冲区菜单恢复缓冲区的既定目的?


更新:正如回答者 Sean 指出的那样,我遇到困难的函数的正确名称是 Buffer-menu-buffer 首字母大写的 B。解决了这个问题后,我遇到了另一个问题:

  (let (nil) (revert-buffer t t t) (message (concat "Reverted " buf-name "to last saved state.")))
(save-current-buffer (set-buffer buf) (let (nil) (revert-buffer t t t) (message (concat "Reverted " buf-name "to last saved state."))))
(with-current-buffer buf (let (nil) (revert-buffer t t t) (message (concat "Reverted " buf-name "to last saved state."))))
(if (y-or-n-p (concat "Revert " buf-name " ?")) (with-current-buffer buf (let (nil) (revert-buffer t t t) (message (concat "Reverted " buf-name "to last saved state.")))))
(let ((buf (Buffer-menu-buffer t)) (buf-name (concat "" (buffer-name (Buffer-menu-buffer t))))) (if (y-or-n-p (concat "Revert " buf-name " ?")) (with-current-buffer buf (let (nil) (revert-buffer t t t) (message (concat "Reverted " buf-name "to last saved state."))))))
frobnitz()
eval((frobnitz) nil)
eval-expression((frobnitz) nil)
call-interactively(eval-expression nil nil)

我的猜测是 with-current-buffer 会尝试保存当前缓冲区,这在 *Buffer List* 中是不允许的。所以现在我正在寻找替代方案 - 也许只是切换、还原并调用 (buffer-list) 切换回来。


更新 2:

对于 future 的读者:在 buffer-menu-mode 中调用它的工作函数和单键绑定(bind):

;; Enhance the buffer menu's capabilities.
(defun revert-buffer-from-buffer-list ()
"Call in buffer list to revert buffer at point to file.

Bind this to a key in `buffer-menu-mode' to use it there - not productive in
other modes because it depends on the `Buffer-menu-buffer' function. Undefined
behavior if you invoke it on a buffer not associated with a file: that's why it
has a confirmation gate. Buffers not associated with files get to play by their
own rules when it comes to `revert-buffer' (which see)."
(interactive)
(let (
(buf (Buffer-menu-buffer t))
(buf-name (concat "" (buffer-name(Buffer-menu-buffer t))))
)
(if (y-or-n-p (concat "Revert " buf-name " ?"))
(with-current-buffer buf
(let ()
(revert-buffer t t t)
(message (concat "Reverted " buf-name " to last saved state."))
)))))
(add-hook 'Buffer-menu-mode-hook
(lambda ()
(define-key Buffer-menu-mode-map (kbd "R") revert-buffer-from-buffer-list)
))

还有一个警告:add-hook 不是幂等的,所以如果你向 foo-mode-hook 添加了你不打算添加的东西,或者不工作,你冒着破坏 foo-mode 的风险,直到你 zorch foo-mode-hook 或从中删除损坏的元素。问我怎么知道!

最佳答案

我的 Emacs 有一个函数 Buffer-menu-buffer,但是没有 buffer-menu-buffer。我想这就是让你绊倒的原因。

编辑:

我发现您的代码还有两个问题,之后我可以使用它从缓冲区菜单中恢复缓冲区。

  • 我不得不在两个地方将 (buf) 更改为 bufbuf 是一个变量,不是要调用的函数。
  • (let (()) ...) 构造导致错误。要么删除它,要么将其更改为 (let () ...)(尽管我不知道您为什么要这样做)。

关于emacs - 如何恢复 emacs 缓冲区列表中的缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11855397/

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