3

我正在尝试创建一个从 emacs*Buffer List*缓冲区恢复缓冲区的函数。据我从文档中可以看出,没有办法快速做到这一点(以内置的保存/标记/访问功能的方式buff-menu.el)。所以我正在写一些省略号。这是我目前的尝试:

(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:

对于未来的读者:工作函数和一个单键绑定来调用它buffer-menu-mode

;; 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,直到您foo-mode-hook将损坏的元素从中删除或删除为止。问我怎么知道的!

4

1 回答 1

4

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

编辑:

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

  • 我不得不换(buf)buf两个地方。 buf是一个变量,而不是要调用的函数。
  • (let (()) ...)构造导致错误。要么消除它,要么将其更改为(let () ...)(尽管我不知道您为什么要这样做)。
于 2012-08-07T23:14:36.153 回答