如何在emacs';缓冲区列表?

如何在emacs';缓冲区列表?,emacs,elisp,Emacs,Elisp,我正在尝试创建一个函数,该函数将从emacs的*缓冲区列表*缓冲区还原缓冲区。从文档中可以看出,没有办法快速完成这项工作(以buff menu.el内置的save/mark/visit函数的方式)。所以我在写一些elisp。以下是我目前的尝试: (defun frobnitz () "Call in buffer list to revert buffer at point to file." (interactive) (let ((buf (buffer-menu-buffer

我正在尝试创建一个函数,该函数将从emacs的
*缓冲区列表*
缓冲区还原缓冲区。从文档中可以看出,没有办法快速完成这项工作(以
buff menu.el
内置的save/mark/visit函数的方式)。所以我在写一些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*
Buffer,并调用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 menu buffer
非常谨慎——我不想破坏buffer menu

请记住,答案可能是“调用您忽略的函数”,我如何让此defun实现其声明的目的,即直接从缓冲区菜单还原缓冲区


更新:正如回答者肖恩所指出的,我一直很难使用的函数的正确名称是
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)
我的猜测是,使用当前缓冲区的
尝试保存当前缓冲区,而
*缓冲区列表*
中没有。因此,现在我正在寻找一种替代方法—可能只是切换、还原和调用
(缓冲区列表)
以切换回


更新2:

对于未来的读者:工作函数和在
缓冲区菜单模式下调用它的单键绑定

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

还有一个警告:
addhook
不是幂等的,所以如果你在
foo-mode-hook
中添加了你不想做或不起作用的东西,你就有可能破坏
foo-mode
,直到你把
foo-mode-hook
去掉或剪掉它。问我怎么知道的

我的Emacs有一个功能
Buffer menu Buffer
,但没有
Buffer menu Buffer
。我想这就是你被绊倒的原因

编辑:

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

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

这肯定是个问题,谢谢-我弄错了。现在有一个不同的问题-想在第二轮中试一试吗?我独立实现了变量与函数的部分,但是
let()
的问题根本不在我的关注范围之内。谢谢你澄清这一点。函数现在可以正常工作,接受:)