-
Notifications
You must be signed in to change notification settings - Fork 3
/
inotify-revert.el
60 lines (47 loc) · 1.75 KB
/
inotify-revert.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
;;; 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: