[英]How can I revert the buffer at point in emacs' buffer list?
I'm trying to create a function that will revert buffers from emacs' *Buffer List*
buffer. 我正在尝试创建一个将从emacs'
*Buffer List*
缓冲区恢复缓冲区的函数。 As far as I can tell from the documentation, there's no way to do this quickly (in the manner of the save/mark/visit functions built in to buff-menu.el
). 据我从文档中可以看出,没有办法快速执行此操作(以
buff-menu.el
内置的save / mark / visit函数buff-menu.el
)。 So I'm writing some elisp. 所以我正在写一些elisp。 Here's my current attempt:
这是我目前的尝试:
(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."))
)))))
Unfortunately, the above defun doesn't seem to work, and I'm having trouble figuring out why. 不幸的是,上述修改似乎不起作用,我无法弄清楚原因。 If I eval the above, switch to the
*Buffer List*
buffer, and invoke M-: (frobnitz) , then it errors out with the following. 如果我评估上述内容,请切换到
*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)
It seems like that's telling me that there's no function buffer-menu-buffer
- but that also seems gratuitously unlikely, since buffer-menu-buffer
is a pretty central function in getting the buffer menu to work! 似乎这告诉我没有函数
buffer-menu-buffer
- 但这似乎也不太可能,因为buffer-menu-buffer
是使缓冲区菜单工作的一个非常重要的功能! For similar reasons, I'm deeply wary of messing with buffer-menu-buffer
myself - I don't want to break the buffer menu. 出于类似的原因,我非常担心自己搞乱
buffer-menu-buffer
- 我不想打破缓冲区菜单。
Bearing in mind that the answer might be "invoke this function that you overlooked," how can I get this defun to accomplish its stated purpose of reverting a buffer directly from the buffer menu? 请记住,答案可能是“调用您忽略的这个功能”,我怎样才能实现其目的,即直接从缓冲区菜单中恢复缓冲区?
Update: as answerer Sean points out, the correct name of the function I was having a hard time with is Buffer-menu-buffer
with a capital initial B. Having fixed that problem, I came across another: 更新:正如回答者肖恩指出的那样,我正在努力工作的函数的正确名称是具有大写字母B的
Buffer-menu-buffer
。修复了这个问题后,我遇到了另一个问题:
(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)
My guess is that with-current-buffer
tries to save the current buffer and that's a no-no on *Buffer List*
. 我的猜测是,
with-current-buffer
尝试保存当前缓冲区,并且这是*Buffer List*
上的*Buffer List*
。 So now I'm looking for an alternative - maybe just switch, revert, and invoke (buffer-list)
to switch back. 所以现在我正在寻找一种替代方案 - 可能只需切换,恢复和调用
(buffer-list)
来切换回来。
Update 2: 更新2:
For future readers: The working function and a single-key binding to invoke it in buffer-menu-mode
: 对于未来的读者:工作函数和单键绑定在
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)
))
Also an exhortation to caution: add-hook
is not idempotent, so if you add things to foo-mode-hook
that you don't intend to or which don't work, you risk breaking foo-mode
until you zorch foo-mode-hook
or prune the broken elements out of it. 另外要谨慎一点:
add-hook
并不是幂等的,所以如果你向foo-mode-hook
添加你不打算或不能工作的东西,你就有可能破坏foo-mode
直到你zorch foo-mode-hook
或修剪破碎的元素。 Ask me how I know! 问我怎么知道!
My Emacs has a function Buffer-menu-buffer
, but no buffer-menu-buffer
. 我的Emacs有一个函数
Buffer-menu-buffer
,但没有buffer-menu-buffer
。 I imagine that's what's tripping you up. 我想这就是绊倒你的原因。
EDIT: 编辑:
I found two more problems with your code, after which I was able to revert buffers from the buffer menu with it. 我发现你的代码还有两个问题,之后我可以用它来恢复缓冲区菜单中的缓冲区。
(buf)
to buf
in two places. (buf)
到buf
。 buf
is a variable, not a function to call. buf
是一个变量,而不是一个调用的函数。 (let (()) ...)
construct causes an error. (let (()) ...)
构造会导致错误。 Either eliminate it, or change it to (let () ...)
(although I don't know why you'd want to). (let () ...)
(虽然我不知道你为什么要这样做)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.