Emacs - 当文件被外部修改时发出通知

发布于 2024-11-29 09:47:37 字数 841 浏览 2 评论 0原文

如何让 emacs 通知我一个或多个访问的文件已从其他地方更改这一事实?以下是此功能在 Gedit 中的实现方式:

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 中实现的模式对话框(这很烦人,但毕竟它确实有效)。您有什么建议?

How do I make emacs notify me about the fact that one or more visited files are changed from elsewhere? Here's how this functionality is implemented in Gedit:

gedit - notify about external changes

I've taken a look at the approaches explained in the "Revert Buffer" document at EmacsWiki and found sort of a workaround (modified a bit for my personal preference):

(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

That's great, but what I would like is to have an automatic solution. Maybe, some kind of noisy message inlined in a buffer (like in the screenshot). Maybe, even a modal dialog as implemented in Visual Studio (that's annoying, but it does the trick, after all). What would be your suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话墙 2024-12-06 09:47:37

这就是我能想到的 - 非常感谢来自 EmacsWiki 的人提供的提示。我创建了一个每隔几秒触发一次的回调 - 它检查修改时间并在文件被修改时显示一条恼人的消息。

从好的方面来说,该解决方案大部分都有效,并通知我有关更改的信息,无论缓冲区是否被修改。不利的一面是,它涉及不断轮询当前文件的修改时间,但我的工作场景可以容忍这一点。

emacs 通知外部变化

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

Here's what I've been able to come up with - big thanks to the guys from EmacsWiki for hints. I created a callback that gets triggered every few seconds - it checks modtime and displays an annoying message if the file has been modified.

On the upside, the solution mostly works and notifies me about changes regardless of whether the buffer is modified or not. On the downside, it involves constant polling of current file for modtime, but my work scenarios can tolerate that.

emacs notify external changes

;(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")))))))
得不到的就毁灭 2024-12-06 09:47:37

如果您也想要脏缓冲区的警告,那么您可以建议切换到缓冲区,因此当您切换到缓冲区并且它被修改时,它会检查文件是否被修改,如果是,那么它可以像内置检查在缓冲区未修改的情况下进行。

If you want the warning for dirty buffers too then you could advise switch-to-buffer, so when you switch to a buffer and it's modified then it would check if the file is modified and if so then it could offer to revert it like the built-in check does in case of unmodified buffers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文