如何让emacs通知我一个或多个访问过的文件是从其他地方更改的?以下是Gedit中如何实现此功能:
我已经看过EmacsWiki的"Revert Buffer"文档中解释的方法,并找到了一种解决方法(根据个人喜好进行了修改):
(global-set-key (kbd "<f5>") (lambda ()
(interactive)
(if (string= (buffer-name) ecb-directories-buffer-name)
(refresh-ecb)
(if (buffer-modified-p)
(revert-buffer) ; ask for confirmation
(revert-buffer t t))))) ; don't ask for confirmation - it's unnecessary, since the buffer hasn't been modified
这很好,但我想要的是拥有一个自动解决方案。也许,在缓冲区内嵌一些嘈杂的消息(如截图中所示)。也许,即使是在Visual Studio中实现的模态对话框(这很烦人,但它毕竟是诀窍)。你的建议是什么?
答案 0 :(得分:2)
如果你也想要脏缓冲区的警告那么你可以建议切换到缓冲区,所以当你切换到缓冲区并且它被修改然后它将检查文件是否被修改,如果是,那么它可以提供恢复就像未修改的缓冲区中的内置检查一样。
答案 1 :(得分:2)
这是我能够想到的 - 非常感谢来自EmacsWiki的人提示。我创建了一个每隔几秒触发一次的回调 - 它检查modtime并在文件被修改时显示恼人的消息。
从好的方面来说,无论缓冲区是否被修改,该解决方案大多数都可以工作并通知我有关更改的信息。在缺点方面,它涉及对modtime当前文件的持续轮询,但我的工作场景可以容忍。
;(global-auto-revert-mode 1)
(defun ask-user-about-supersession-threat (fn) "blatantly ignore files that changed on disk")
(run-with-timer 0 2 'my-check-external-modifications)
(add-hook 'after-save-hook 'my-check-external-modifications)
(add-hook 'after-revert-hook 'my-check-external-modifications)
(defun my-load-external-modifications ()
(interactive)
(if (string= (buffer-name) ecb-directories-buffer-name)
(refresh-ecb)
(if (buffer-modified-p)
(revert-buffer) ; ask for confirmation
(revert-buffer t t)) ; don't ask for confirmation - it's unnecessary, since the buffer hasn't been modified
(my-check-external-modifications)))
(defun my-overwrite-external-modifications ()
(interactive)
(clear-visited-file-modtime)
(set-buffer-modified-p (current-buffer))
(save-buffer)
(my-check-external-modifications))
(defun my-check-external-modifications ()
(if (verify-visited-file-modtime (current-buffer))
(progn
(global-set-key (kbd "<f5>") 'my-load-external-modifications)
(global-set-key (kbd "C-s") 'save-buffer)
(setq header-line-format tabbar-header-line-format))
(progn
(global-set-key (kbd "<f5>") 'my-load-external-modifications)
(global-set-key (kbd "C-s") 'my-overwrite-external-modifications)
(setq header-line-format (format "%s. Press F5 to load external changes, C-s to overwrite them"
(propertize "This file has been changed externally" 'face '(:foreground "#f00")))))))