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