Emacs Lisp,如何监视文件/目录的更改

时间:2010-08-11 08:54:10

标签: emacs elisp

我正在寻找一种方法来定期检查某个目录下的文件是否从上次检查更改(功能符号到FAM守护程序或gio.monitor_directory)。在emacs lisp。

  • 是否有提供此功能的库/代码段?
  • 如果没有,我该如何实现这样的功能?

3 个答案:

答案 0 :(得分:6)

(defun install-monitor (file secs)
  (run-with-timer
   0 secs
   (lambda (f p)
     (unless (< p (second (time-since (elt (file-attributes f) 5))))
       (message "File %s changed!" f)))
   file secs))

(defvar monitor-timer (install-monitor "/tmp" 5)
  "Check if /tmp is changed every 5s.")

要取消,

(cancel-timer monitor-timer)

编辑:

正如mankoff所述,上面的代码片段监视最近5秒内的文件修改,而不是自上次检查以来。为了实现后者,我们每次进行检查时都需要保存属性。希望这有效:

(defvar monitor-attributes nil
  "Cached file attributes to be monitored.")

(defun install-monitor (file secs)
  (run-with-timer
   0 secs
   (lambda (f p)
     (let ((att (file-attributes f)))
       (unless (or (null monitor-attributes) (equalp monitor-attributes att))
         (message "File %s changed!" f))
       (setq monitor-attributes att)))
   file secs))

(defvar monitor-timer (install-monitor "/tmp" 5)
  "Check if /tmp is changed every 5s.")

答案 1 :(得分:1)

我没有一个合适的解决方案,但也许有几个指针可以让你朝着正确的方向前进。

根据一些快速的谷歌搜索,似乎dbus有一个inotify接口内置插件。从最新版本的emacs开始,您可以通过Emacs lisp(至少在Linux下)访问dbus接口,也许您可​​以将所有这些连接在一起以使其正常工作。请参阅此处有关在Emacs中使用dbus的示例:

http://emacs-fu.blogspot.com/2009/01/using-d-bus-example.html

答案 2 :(得分:0)

Emacs与各种文件系统监视程序库链接,并在filenotify.el中提供一个统一的接口。

相关问题