+(defun auto-revert-notify-rm-watch ()
+ "Disable file watch for current buffer's associated file."
+ (when auto-revert-notify-watch-descriptor
+ (funcall (if (fboundp 'inotify-rm-watch) 'inotify-rm-watch 'w32-rm-watch)
+ auto-revert-notify-watch-descriptor)
+ (remhash auto-revert-notify-watch-descriptor
+ auto-revert-notify-watch-descriptor-hash-list))
+ (setq auto-revert-notify-watch-descriptor nil
+ auto-revert-notify-modified-p nil))
+
+(defun auto-revert-notify-add-watch ()
+ "Enable file watch for current buffer's associated file."
+ (when (and buffer-file-name auto-revert-notify-enabled)
+ (auto-revert-notify-rm-watch)
+ (let ((func (if (fboundp 'inotify-add-watch)
+ 'inotify-add-watch 'w32-add-watch))
+ (aspect (if (fboundp 'inotify-add-watch)
+ '(close-write) '(last-write-time))))
+ (setq auto-revert-notify-watch-descriptor
+ (funcall func buffer-file-name aspect 'auto-revert-notify-handler))
+ (puthash auto-revert-notify-watch-descriptor
+ (current-buffer)
+ auto-revert-notify-watch-descriptor-hash-list))))
+
+(defun auto-revert-notify-handler (event)
+ "Handle an event returned from file watch."
+ (when (listp event)
+ (let ((buffer
+ (gethash (car event) auto-revert-notify-watch-descriptor-hash-list)))
+ (when (bufferp buffer)
+ (with-current-buffer buffer
+ (setq auto-revert-notify-modified-p t))))))
+