我正在寻找一种方法,定期检查某个目录下的文件是否与上次检查时有变化(类似于FAM守护程序或gio.monitor_directory的功能)。在emacs lisp中。
- 是否有任何库/代码片段提供此功能?
- 如果没有,如何实现这样的功能?
(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.")
我没有一个完美的解决方案,但也许可以给你一些指针来帮助你找到正确的方向。
根据一些快速的谷歌搜索,似乎dbus已经内置了inotify接口。自从最新版本的emacs以来,你可以通过Emacs lisp(至少在Linux下)访问dbus接口,也许你可以将所有这些组合起来使其工作。这里有一个使用dbus与Emacs的例子:
http://emacs-fu.blogspot.com/2009/01/using-d-bus-example.html