Skip to content

Commit 739cce8

Browse files
committed
feat: add initial support for inotify-revert
1 parent 4ee7712 commit 739cce8

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

core/me-loaddefs.el

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ Mount eCryptfs' private directory." t)
1717
Unmount eCryptfs' private directory." t)
1818
(register-definition-prefixes "../elisp/ecryptfs" '("ecryptfs-"))
1919

20+
21+
;;; Generated autoloads from ../elisp/inotify-revert.el
22+
23+
(autoload 'inotify-revert-mode "../elisp/inotify-revert" "\
24+
Like `auto-revert-mode', but faster.
25+
26+
This is a minor mode. If called interactively, toggle the
27+
`Inotify-revert mode' mode. If the prefix argument is positive,
28+
enable the mode, and if it is zero or negative, disable the mode.
29+
30+
If called from Lisp, toggle the mode if ARG is `toggle'. Enable
31+
the mode if ARG is nil, omitted, or is a positive number.
32+
Disable the mode if ARG is a negative number.
33+
34+
To check whether the minor mode is enabled in the current buffer,
35+
evaluate `inotify-revert-mode'.
36+
37+
The mode's hook is called both when the mode is enabled and when
38+
it is disabled.
39+
40+
(fn &optional ARG)" t)
41+
(register-definition-prefixes "../elisp/inotify-revert" '("inotify-revert-"))
42+
2043

2144
;;; Generated autoloads from ../modules/extras/me-cocogitto.el
2245

elisp/inotify-revert.el

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
;;; inotify-revert.el --- Auto-revert using inotify -*- lexical-binding: t; -*-
2+
3+
;; Copyright (c) 2019 fmdkdd
4+
;;
5+
;; Author: fmdkdd
6+
7+
;; This file is not part of GNU Emacs.
8+
9+
;;; Commentary:
10+
11+
;; Like auto-revert-mode, but without the weird lag.
12+
;;
13+
;; auto-revert-mode supposedly uses inotify thourgh filenotify, and reverts to
14+
;; polling when inotify fails. On my machine, auto-revert can take up to 1sec
15+
;; to revert a file that has changed, while using this mode is instantaneous.
16+
17+
;; From: https://github.com/fmdkdd/dotfiles/blob/master/emacs/.emacs.d/elisp/inotify-revert.el
18+
19+
;;; Code:
20+
21+
(require 'filenotify)
22+
23+
(defvar-local inotify-revert--descriptor nil
24+
"The filenotify descriptor for the current buffer.")
25+
26+
(defun inotify-revert-activate ()
27+
"Start watching the current buffer's file for changes."
28+
(when buffer-file-name
29+
(let ((buffer (current-buffer)))
30+
(setq inotify-revert--descriptor
31+
(file-notify-add-watch
32+
buffer-file-name '(change)
33+
;; Using a lambda to capture the buffer, instead of a handler in a
34+
;; separate defun
35+
(lambda (event)
36+
(when (eq (nth 1 event) 'changed)
37+
(with-current-buffer buffer
38+
(revert-buffer 'ignore-auto 'noconfirm)))))))))
39+
40+
(defun inotify-revert-deactivate ()
41+
"Unsubscribe to the current buffer's file changes."
42+
(when inotify-revert--descriptor
43+
(file-notify-rm-watch inotify-revert--descriptor)
44+
(kill-local-variable inotify-revert--descriptor)))
45+
46+
;;;###autoload
47+
(define-minor-mode inotify-revert-mode
48+
"Like `auto-revert-mode', but faster."
49+
:lighter " revert"
50+
(cond
51+
(inotify-revert-mode
52+
(inotify-revert-activate))
53+
((not inotify-revert-mode)
54+
(inotify-revert-deactivate))))
55+
56+
(provide 'inotify-revert)
57+
58+
;;; inotify-revert.el ends here

0 commit comments

Comments
 (0)