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

pyg*_*iel 5 emacs elisp

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

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

hua*_*uan 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.")
Run Code Online (Sandbox Code Playgroud)

取消,

(cancel-timer monitor-timer)
Run Code Online (Sandbox Code Playgroud)

编辑:

正如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.")
Run Code Online (Sandbox Code Playgroud)