原版的
这是我对问题代码段的改进。查看我的VC历史记录,我确认以下片段作为OP发布的片段开始。所以我确实为此付出了代价。
这是对我稳定的代码:
(defun modi/revert-all-file-buffers ()
"Refresh all open buffers from their respective files."
(interactive)
(let* ((list (buffer-list))
(buffer (car list)))
(while buffer
(let ((filename (buffer-file-name buffer)))
;; Revert only buffers containing files, which are not modified;
;; do not try to revert non-file buffers like *Messages*.
(when (and filename
(not (buffer-modified-p buffer)))
(if (file-exists-p filename)
;; If the file exists, revert the buffer.
(with-current-buffer buffer
(revert-buffer :ignore-auto :noconfirm :preserve-modes))
;; If the file doesn't exist, kill the buffer.
(let (kill-buffer-query-functions) ; No query done when killing buffer
(kill-buffer buffer)
(message "Killed non-existing file buffer: %s" filename)))))
(setq buffer (pop list)))
(message "Finished reverting buffers containing unmodified files.")))
更新资料
在查看@Drew的 解决方案之后,这是上述内容的改进版本和更好文档版本。
(defun modi/revert-all-file-buffers ()
"Refresh all open file buffers without confirmation.
Buffers in modified (not yet saved) state in emacs will not be reverted. They
will be reverted though if they were modified outside emacs.
Buffers visiting files which do not exist any more or are no longer readable
will be killed."
(interactive)
(dolist (buf (buffer-list))
(let ((filename (buffer-file-name buf)))
;; Revert only buffers containing files, which are not modified;
;; do not try to revert non-file buffers like *Messages*.
(when (and filename
(not (buffer-modified-p buf)))
(if (file-readable-p filename)
;; If the file exists and is readable, revert the buffer.
(with-current-buffer buf
(revert-buffer :ignore-auto :noconfirm :preserve-modes))
;; Otherwise, kill the buffer.
(let (kill-buffer-query-functions) ; No query done when killing buffer
(kill-buffer buf)
(message "Killed non-existing/unreadable file buffer: %s" filename))))))
(message "Finished reverting buffers containing unmodified files."))
参考
B2
您的示例还原缓冲区。我使用了非常相似的功能(很可能是从此代码片段派生的),并且运行良好。