Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
60 lines (47 sloc)
1.75 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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 | |
(file-notify-add-watch | |
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))) | |
;;;###autoload | |
(define-minor-mode inotify-revert-mode | |
"Like `auto-revert-mode', but faster." | |
:lighter " revert" | |
(cond | |
(inotify-revert-mode | |
(inotify-revert-activate)) | |
((not inotify-revert-mode) | |
(inotify-revert-deactivate)))) | |
(provide 'inotify-revert) | |
;;; inotify-revert.el ends here | |
;; Local Variables: | |
;; eval: (fmdkdd/byte-compile-on-save) | |
;; End: |