Skip to content
Switch branches/tags
Go to file
Cannot retrieve contributors at this time
;;; inotify-revert.el --- Auto-revert using inotify -*- lexical-binding: t; -*-
;; Copyright (c) 2019 fmdkdd
;; Author: fmdkdd
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Like auto-revert-mode, but without the weird lag.
;; auto-revert-mode supposedly uses inotify thourgh filenotify, and reverts to
;; polling when inotify fails. On my machine, auto-revert can take up to 1sec
;; to revert a file that has changed, while using this mode is instantaneous.
;;; Code:
(require 'filenotify)
(defvar-local inotify-revert--descriptor nil
"The filenotify descriptor for the current buffer.")
(defun inotify-revert-activate ()
"Start watching the current buffer's file for changes."
(when buffer-file-name
(let ((buffer (current-buffer)))
(setq inotify-revert--descriptor
buffer-file-name '(change)
;; Using a lambda to capture the buffer, instead of a handler in a
;; separate defun
(lambda (event)
(when (eq (nth 1 event) 'changed)
(with-current-buffer buffer
(revert-buffer 'ignore-auto 'noconfirm)))))))))
(defun inotify-revert-deactivate ()
"Unsubscribe to the current buffer's file changes."
(when inotify-revert--descriptor
(file-notify-rm-watch inotify-revert--descriptor)
(kill-local-variable inotify-revert--descriptor)))
(define-minor-mode inotify-revert-mode
"Like `auto-revert-mode', but faster."
:lighter " revert"
((not inotify-revert-mode)
(provide 'inotify-revert)
;;; inotify-revert.el ends here
;; Local Variables:
;; eval: (fmdkdd/byte-compile-on-save)
;; End: