autorevert.el 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. ;;; autorevert.el --- revert buffers when files on disk change -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1997-1999, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: Anders Lindgren
  4. ;; Keywords: convenience
  5. ;; Created: 1997-06-01
  6. ;; Date: 1999-11-30
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Introduction:
  20. ;;
  21. ;; Whenever a file that Emacs is editing has been changed by another
  22. ;; program the user normally has to execute the command `revert-buffer'
  23. ;; to load the new content of the file into Emacs.
  24. ;;
  25. ;; This package contains two minor modes: Global Auto-Revert Mode and
  26. ;; Auto-Revert Mode. Both modes automatically revert buffers
  27. ;; whenever the corresponding files have been changed on disk and the
  28. ;; buffer contains no unsaved changes.
  29. ;;
  30. ;; Auto-Revert Mode can be activated for individual buffers. Global
  31. ;; Auto-Revert Mode applies to all file buffers. (If the user option
  32. ;; `global-auto-revert-non-file-buffers' is non-nil, it also applies
  33. ;; to some non-file buffers. This option is disabled by default.)
  34. ;;
  35. ;; Since checking a remote file is slow, these modes check or revert
  36. ;; remote files only if the user option `auto-revert-remote-files' is
  37. ;; non-nil. It is recommended to disable version control for remote
  38. ;; files.
  39. ;;
  40. ;; Both modes operate by checking the time stamp of all files at
  41. ;; intervals of `auto-revert-interval'. The default is every five
  42. ;; seconds. The check is aborted whenever the user actually uses
  43. ;; Emacs. You should never even notice that this package is active
  44. ;; (except that your buffers will be reverted, of course).
  45. ;;
  46. ;; If the file exists, Auto-Revert Mode updates the buffer based on
  47. ;; its (possibly empty) contents. If the file no longer exists, then
  48. ;; there is nothing to revert, so it does not modify the buffer. Once
  49. ;; a deleted file corresponding to a buffer in Auto-Revert Mode
  50. ;; reappears, Auto-Revert Mode continues to work.
  51. ;;
  52. ;; If Emacs is compiled with file notification support, notifications
  53. ;; are used instead of checking the time stamp of the files. You can
  54. ;; disable this by setting the user option `auto-revert-use-notify' to
  55. ;; nil. Alternatively, a regular expression of directories to be
  56. ;; excluded from file notifications can be specified by
  57. ;; `auto-revert-notify-exclude-dir-regexp'.
  58. ;;
  59. ;; After reverting a file buffer, Auto-Revert Mode normally puts point
  60. ;; at the same position that a regular manual revert would. However,
  61. ;; there is one exception to this rule. If point is at the end of the
  62. ;; buffer before reverting, it stays at the end. Similarly if point
  63. ;; is displayed at the end of a file buffer in any window, it will stay
  64. ;; at the end of the buffer in that window, even if the window is not
  65. ;; selected. This way, you can use Auto-Revert Mode to `tail' a file.
  66. ;; Just put point at the end of the buffer and it will stay there.
  67. ;; These rules apply to file buffers. For non-file buffers, the
  68. ;; behavior may be mode dependent.
  69. ;;
  70. ;; While you can use Auto-Revert Mode to tail a file, this package
  71. ;; contains a third minor mode, Auto-Revert Tail Mode, which does so
  72. ;; more efficiently, as long as you are sure that the file will only
  73. ;; change by growing at the end. It only appends the new output,
  74. ;; instead of reverting the entire buffer. It does so even if the
  75. ;; buffer contains unsaved changes. (Because they will not be lost.)
  76. ;; Usage:
  77. ;;
  78. ;; Go to the appropriate buffer and press either of:
  79. ;; M-x auto-revert-mode RET
  80. ;; M-x auto-revert-tail-mode RET
  81. ;;
  82. ;; To activate Global Auto-Revert Mode, press:
  83. ;; M-x global-auto-revert-mode RET
  84. ;;
  85. ;; To activate Global Auto-Revert Mode every time Emacs is started
  86. ;; customize the option `global-auto-revert-mode' or the following
  87. ;; line could be added to your ~/.emacs:
  88. ;; (global-auto-revert-mode 1)
  89. ;;
  90. ;; The function `turn-on-auto-revert-mode' could be added to any major
  91. ;; mode hook to activate Auto-Revert Mode for all buffers in that
  92. ;; mode. For example, the following line will activate Auto-Revert
  93. ;; Mode in all C mode buffers:
  94. ;;
  95. ;; (add-hook 'c-mode-hook #'turn-on-auto-revert-mode)
  96. ;;; Code:
  97. ;; Dependencies:
  98. (eval-when-compile (require 'cl-lib))
  99. (require 'timer)
  100. (require 'filenotify)
  101. ;; Custom Group:
  102. ;;
  103. ;; The two modes will be placed next to Auto Save Mode under the
  104. ;; Files group under Emacs.
  105. (defgroup auto-revert nil
  106. "Revert individual buffers when files on disk change.
  107. Auto-Revert Mode enables auto-revert in individual buffers.
  108. Global Auto-Revert Mode does so in all buffers."
  109. :group 'files
  110. :group 'convenience)
  111. ;; Variables:
  112. ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
  113. ;;; What's this?: ;;;###autoload
  114. (defvar auto-revert-mode nil
  115. "Non-nil when Auto-Revert Mode is active.
  116. Never set this variable directly, use the command `auto-revert-mode' instead.")
  117. (put 'auto-revert-mode 'permanent-local t)
  118. (defvar auto-revert-tail-mode nil
  119. "Non-nil when Auto-Revert Tail Mode is active.
  120. Never set this variable directly, use the command
  121. `auto-revert-tail-mode' instead.")
  122. (put 'auto-revert-tail-mode 'permanent-local t)
  123. (defvar auto-revert-timer nil
  124. "Timer used by Auto-Revert Mode.")
  125. (defcustom auto-revert-interval 5
  126. "Time, in seconds, between Auto-Revert Mode file checks.
  127. The value may be an integer or floating point number.
  128. If a timer is already active, there are two ways to make sure
  129. that the new value will take effect immediately. You can set
  130. this variable through Custom or you can call the command
  131. `auto-revert-set-timer' after setting the variable. Otherwise,
  132. the new value will take effect the first time Auto-Revert Mode
  133. calls `auto-revert-set-timer' for internal reasons or in your
  134. next editing session."
  135. :group 'auto-revert
  136. :type 'number
  137. :set (lambda (variable value)
  138. (set-default variable value)
  139. (and (boundp 'auto-revert-timer)
  140. auto-revert-timer
  141. (auto-revert-set-timer))))
  142. (defcustom auto-revert-stop-on-user-input t
  143. "When non-nil, user input temporarily interrupts Auto-Revert Mode.
  144. With this setting, Auto-Revert Mode checks for user input after
  145. handling each buffer and does not process any further buffers
  146. \(until the next run of the timer) if user input is available.
  147. When nil, Auto-Revert Mode checks files and reverts buffers,
  148. with quitting disabled, without paying attention to user input.
  149. Thus, with this setting, Emacs might be non-responsive at times."
  150. :group 'auto-revert
  151. :type 'boolean)
  152. (defcustom auto-revert-verbose t
  153. "When nil, Auto-Revert Mode does not generate any messages.
  154. When non-nil, a message is generated whenever a buffer is reverted."
  155. :group 'auto-revert
  156. :type 'boolean)
  157. (defcustom auto-revert-mode-text " ARev"
  158. "String to display in the mode line when Auto-Revert Mode is active.
  159. \(When the string is not empty, make sure that it has a leading space.)"
  160. :tag "Auto-Revert Mode Text" ; To separate it from `global-...'
  161. :group 'auto-revert
  162. :type 'string)
  163. (defcustom auto-revert-tail-mode-text " Tail"
  164. "String to display in the mode line when Auto-Revert Tail Mode is active.
  165. \(When the string is not empty, make sure that it has a leading space.)"
  166. :group 'auto-revert
  167. :type 'string
  168. :version "22.1")
  169. (defcustom auto-revert-mode-hook nil
  170. "Functions to run when Auto-Revert Mode is activated."
  171. :tag "Auto-Revert Mode Hook" ; To separate it from `global-...'
  172. :group 'auto-revert
  173. :type 'hook)
  174. (defcustom global-auto-revert-mode-text ""
  175. "String to display when Global Auto-Revert Mode is active.
  176. The default is nothing since when this mode is active this text doesn't
  177. vary over time, or between buffers. Hence mode line text
  178. would only waste precious space."
  179. :group 'auto-revert
  180. :type 'string)
  181. (defcustom global-auto-revert-mode-hook nil
  182. "Hook called when Global Auto-Revert Mode is activated."
  183. :group 'auto-revert
  184. :type 'hook)
  185. (defcustom global-auto-revert-non-file-buffers nil
  186. "When nil, Global Auto-Revert Mode operates only on file-visiting buffers.
  187. When non-nil, both file buffers and buffers with a custom
  188. `revert-buffer-function' and a `buffer-stale-function' are
  189. reverted by Global Auto-Revert Mode. These include the Buffer
  190. List buffer displayed by `buffer-menu', and Dired buffers showing
  191. complete local directories. The Buffer List buffer reverts every
  192. `auto-revert-interval' seconds; Dired buffers when the file list of
  193. the main directory changes. Dired buffers do not auto-revert as
  194. a result of changes in subdirectories, or in the contents, size,
  195. modes, etc., of files. You may still sometimes want to revert
  196. them manually.
  197. Use this option with care since it could lead to excessive auto-reverts.
  198. For more information, see Info node `(emacs)Autorevert'."
  199. :group 'auto-revert
  200. :type 'boolean
  201. :link '(info-link "(emacs)Autorevert"))
  202. (defcustom global-auto-revert-ignore-modes ()
  203. "List of major modes Global Auto-Revert Mode should not check."
  204. :group 'auto-revert
  205. :type '(repeat sexp))
  206. (defcustom auto-revert-load-hook nil
  207. "Functions to run when Auto-Revert Mode is first loaded."
  208. :tag "Load Hook"
  209. :group 'auto-revert
  210. :type 'hook)
  211. (defcustom auto-revert-check-vc-info nil
  212. "If non-nil Auto-Revert Mode reliably updates version control info.
  213. Auto-Revert Mode updates version control info whenever the buffer
  214. needs reverting, regardless of the value of this variable.
  215. However, the version control state can change without changes to
  216. the work file. If the change is made from the current Emacs
  217. session, all info is updated. But if, for instance, a new
  218. version is checked in from outside the current Emacs session, the
  219. version control number in the mode line, as well as other version
  220. control related information, may not be properly updated. If you
  221. are worried about this, set this variable to a non-nil value.
  222. This currently works by automatically updating the version
  223. control info every `auto-revert-interval' seconds. Nevertheless,
  224. it should not cause excessive CPU usage on a reasonably fast
  225. machine, if it does not apply to too many version controlled
  226. buffers. CPU usage depends on the version control system."
  227. :group 'auto-revert
  228. :type 'boolean
  229. :version "22.1")
  230. (defvar-local global-auto-revert-ignore-buffer nil
  231. "When non-nil, Global Auto-Revert Mode will not revert this buffer.
  232. This variable becomes buffer local when set in any fashion.")
  233. (defcustom auto-revert-remote-files nil
  234. "If non-nil remote files are also reverted."
  235. :group 'auto-revert
  236. :type 'boolean
  237. :version "24.4")
  238. (defcustom auto-revert-use-notify t
  239. "If non-nil Auto-Revert Mode uses file notification functions.
  240. You should set this variable through Custom."
  241. :group 'auto-revert
  242. :type 'boolean
  243. :set (lambda (variable value)
  244. (set-default variable value)
  245. (unless (symbol-value variable)
  246. (dolist (buf (buffer-list))
  247. (with-current-buffer buf
  248. (when (symbol-value 'auto-revert-notify-watch-descriptor)
  249. (auto-revert-notify-rm-watch))))))
  250. :initialize 'custom-initialize-default
  251. :version "24.4")
  252. (defcustom auto-revert-notify-exclude-dir-regexp
  253. (concat
  254. ;; No mounted file systems.
  255. "^" (regexp-opt '("/afs/" "/media/" "/mnt" "/net/" "/tmp_mnt/"))
  256. ;; No remote files.
  257. (unless auto-revert-remote-files "\\|^/[^/|:][^/|]+:"))
  258. "Regular expression of directories to be excluded from file notifications."
  259. :group 'auto-revert
  260. :type 'regexp
  261. :version "24.4")
  262. ;; Internal variables:
  263. (defvar auto-revert-buffer-list ()
  264. "List of buffers in Auto-Revert Mode.
  265. Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
  266. buffers to this list.
  267. The timer function `auto-revert-buffers' is responsible for purging
  268. the list of old buffers.")
  269. (defvar auto-revert-remaining-buffers ()
  270. "Buffers not checked when user input stopped execution.")
  271. (defvar auto-revert-tail-pos 0
  272. "Position of last known end of file.")
  273. (defun auto-revert-find-file-function ()
  274. (setq-local auto-revert-tail-pos
  275. (nth 7 (file-attributes buffer-file-name))))
  276. (add-hook 'find-file-hook
  277. #'auto-revert-find-file-function)
  278. (defvar auto-revert-notify-watch-descriptor-hash-list
  279. (make-hash-table :test 'equal)
  280. "A hash table collecting all file watch descriptors.
  281. Hash key is a watch descriptor, hash value is a list of buffers
  282. which are related to files being watched and carrying the same
  283. default directory.")
  284. (defvar-local auto-revert-notify-watch-descriptor nil
  285. "The file watch descriptor active for the current buffer.")
  286. (put 'auto-revert-notify-watch-descriptor 'permanent-local t)
  287. (defvar-local auto-revert-notify-modified-p nil
  288. "Non-nil when file has been modified on the file system.
  289. This has been reported by a file notification event.")
  290. ;; Functions:
  291. (defun auto-revert-remove-current-buffer ()
  292. "Remove dead buffer from `auto-revert-buffer-list'."
  293. (setq auto-revert-buffer-list
  294. (delq (current-buffer) auto-revert-buffer-list)))
  295. ;;;###autoload
  296. (define-minor-mode auto-revert-mode
  297. "Toggle reverting buffer when the file changes (Auto-Revert Mode).
  298. With a prefix argument ARG, enable Auto-Revert Mode if ARG is
  299. positive, and disable it otherwise. If called from Lisp, enable
  300. the mode if ARG is omitted or nil.
  301. Auto-Revert Mode is a minor mode that affects only the current
  302. buffer. When enabled, it reverts the buffer when the file on
  303. disk changes.
  304. When a buffer is reverted, a message is generated. This can be
  305. suppressed by setting `auto-revert-verbose' to nil.
  306. Use `global-auto-revert-mode' to automatically revert all buffers.
  307. Use `auto-revert-tail-mode' if you know that the file will only grow
  308. without being changed in the part that is already in the buffer."
  309. :group 'auto-revert :lighter auto-revert-mode-text
  310. (if auto-revert-mode
  311. (when (not (memq (current-buffer) auto-revert-buffer-list))
  312. (push (current-buffer) auto-revert-buffer-list)
  313. (add-hook
  314. 'kill-buffer-hook
  315. #'auto-revert-remove-current-buffer
  316. nil t))
  317. (when auto-revert-use-notify (auto-revert-notify-rm-watch))
  318. (auto-revert-remove-current-buffer))
  319. (auto-revert-set-timer)
  320. (when auto-revert-mode
  321. (auto-revert-buffers)
  322. (setq auto-revert-tail-mode nil)))
  323. ;;;###autoload
  324. (defun turn-on-auto-revert-mode ()
  325. "Turn on Auto-Revert Mode.
  326. This function is designed to be added to hooks, for example:
  327. (add-hook \\='c-mode-hook #\\='turn-on-auto-revert-mode)"
  328. (auto-revert-mode 1))
  329. ;;;###autoload
  330. (define-minor-mode auto-revert-tail-mode
  331. "Toggle reverting tail of buffer when the file grows.
  332. With a prefix argument ARG, enable Auto-Revert Tail Mode if ARG
  333. is positive, and disable it otherwise. If called from Lisp,
  334. enable the mode if ARG is omitted or nil.
  335. When Auto-Revert Tail Mode is enabled, the tail of the file is
  336. constantly followed, as with the shell command `tail -f'. This
  337. means that whenever the file grows on disk (presumably because
  338. some background process is appending to it from time to time),
  339. this is reflected in the current buffer.
  340. You can edit the buffer and turn this mode off and on again as
  341. you please. But make sure the background process has stopped
  342. writing before you save the file!
  343. When a buffer is reverted, a message is generated. This can be
  344. suppressed by setting `auto-revert-verbose' to nil.
  345. Use `auto-revert-mode' for changes other than appends!"
  346. :group 'find-file :lighter auto-revert-tail-mode-text
  347. (when auto-revert-tail-mode
  348. (unless buffer-file-name
  349. (auto-revert-tail-mode 0)
  350. (error "This buffer is not visiting a file"))
  351. (if (and (buffer-modified-p)
  352. (zerop auto-revert-tail-pos) ; library was loaded only after finding file
  353. (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
  354. (auto-revert-tail-mode 0)
  355. ;; a-r-tail-pos stores the size of the file at the time of the
  356. ;; last revert. After this package loads, it adds a
  357. ;; find-file-hook to set this variable every time a file is
  358. ;; loaded. If the package is loaded only _after_ visiting the
  359. ;; file to be reverted, then we have no idea what the value of
  360. ;; a-r-tail-pos should have been when the file was visited. If
  361. ;; the file has changed on disk in the meantime, all we can do
  362. ;; is offer to revert the whole thing. If you choose not to
  363. ;; revert, then you might miss some output then happened
  364. ;; between visiting the file and activating a-r-t-mode.
  365. (and (zerop auto-revert-tail-pos)
  366. (not (verify-visited-file-modtime (current-buffer)))
  367. (y-or-n-p "File changed on disk, content may be missing. \
  368. Perform a full revert? ")
  369. ;; Use this (not just revert-buffer) for point-preservation.
  370. (auto-revert-buffers))
  371. ;; else we might reappend our own end when we save
  372. (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
  373. (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
  374. (setq-local auto-revert-tail-pos
  375. (nth 7 (file-attributes buffer-file-name))))
  376. ;; let auto-revert-mode set up the mechanism for us if it isn't already
  377. (or auto-revert-mode
  378. (let ((auto-revert-tail-mode t))
  379. (auto-revert-mode 1)))
  380. (setq auto-revert-mode nil))))
  381. ;;;###autoload
  382. (defun turn-on-auto-revert-tail-mode ()
  383. "Turn on Auto-Revert Tail Mode.
  384. This function is designed to be added to hooks, for example:
  385. (add-hook \\='my-logfile-mode-hook #\\='turn-on-auto-revert-tail-mode)"
  386. (auto-revert-tail-mode 1))
  387. ;;;###autoload
  388. (define-minor-mode global-auto-revert-mode
  389. "Toggle Global Auto-Revert Mode.
  390. With a prefix argument ARG, enable Global Auto-Revert Mode if ARG
  391. is positive, and disable it otherwise. If called from Lisp,
  392. enable the mode if ARG is omitted or nil.
  393. Global Auto-Revert Mode is a global minor mode that reverts any
  394. buffer associated with a file when the file changes on disk. Use
  395. `auto-revert-mode' to revert a particular buffer.
  396. If `global-auto-revert-non-file-buffers' is non-nil, this mode
  397. may also revert some non-file buffers, as described in the
  398. documentation of that variable. It ignores buffers with modes
  399. matching `global-auto-revert-ignore-modes', and buffers with a
  400. non-nil vale of `global-auto-revert-ignore-buffer'.
  401. When a buffer is reverted, a message is generated. This can be
  402. suppressed by setting `auto-revert-verbose' to nil.
  403. This function calls the hook `global-auto-revert-mode-hook'.
  404. It displays the text that `global-auto-revert-mode-text'
  405. specifies in the mode line."
  406. :global t :group 'auto-revert :lighter global-auto-revert-mode-text
  407. (auto-revert-set-timer)
  408. (if global-auto-revert-mode
  409. (auto-revert-buffers)
  410. (dolist (buf (buffer-list))
  411. (with-current-buffer buf
  412. (when auto-revert-use-notify
  413. (auto-revert-notify-rm-watch))))))
  414. (defun auto-revert-set-timer ()
  415. "Restart or cancel the timer used by Auto-Revert Mode.
  416. If such a timer is active, cancel it. Start a new timer if
  417. Global Auto-Revert Mode is active or if Auto-Revert Mode is active
  418. in some buffer. Restarting the timer ensures that Auto-Revert Mode
  419. will use an up-to-date value of `auto-revert-interval'"
  420. (interactive)
  421. (if (timerp auto-revert-timer)
  422. (cancel-timer auto-revert-timer))
  423. (setq auto-revert-timer
  424. (if (or global-auto-revert-mode auto-revert-buffer-list)
  425. (run-with-timer auto-revert-interval
  426. auto-revert-interval
  427. 'auto-revert-buffers))))
  428. (defun auto-revert-notify-rm-watch ()
  429. "Disable file notification for current buffer's associated file."
  430. (when auto-revert-notify-watch-descriptor
  431. (maphash
  432. (lambda (key value)
  433. (when (equal key auto-revert-notify-watch-descriptor)
  434. (setq value (delete (current-buffer) value))
  435. (if value
  436. (puthash key value auto-revert-notify-watch-descriptor-hash-list)
  437. (remhash key auto-revert-notify-watch-descriptor-hash-list)
  438. (ignore-errors
  439. (file-notify-rm-watch auto-revert-notify-watch-descriptor)))))
  440. auto-revert-notify-watch-descriptor-hash-list)
  441. (remove-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch))
  442. (setq auto-revert-notify-watch-descriptor nil
  443. auto-revert-notify-modified-p nil))
  444. (defun auto-revert-notify-add-watch ()
  445. "Enable file notification for current buffer's associated file."
  446. ;; We can assume that `buffer-file-name' and
  447. ;; `auto-revert-use-notify' are non-nil.
  448. (if (or (string-match auto-revert-notify-exclude-dir-regexp
  449. (expand-file-name default-directory))
  450. (file-symlink-p (or buffer-file-name default-directory)))
  451. ;; Fallback to file checks.
  452. (setq-local auto-revert-use-notify nil)
  453. (when (not auto-revert-notify-watch-descriptor)
  454. (setq auto-revert-notify-watch-descriptor
  455. (ignore-errors
  456. (if buffer-file-name
  457. (file-notify-add-watch
  458. (expand-file-name buffer-file-name default-directory)
  459. '(change attribute-change)
  460. 'auto-revert-notify-handler)
  461. (file-notify-add-watch
  462. (expand-file-name default-directory)
  463. '(change)
  464. 'auto-revert-notify-handler))))
  465. (if auto-revert-notify-watch-descriptor
  466. (progn
  467. (puthash
  468. auto-revert-notify-watch-descriptor
  469. (cons (current-buffer)
  470. (gethash auto-revert-notify-watch-descriptor
  471. auto-revert-notify-watch-descriptor-hash-list))
  472. auto-revert-notify-watch-descriptor-hash-list)
  473. (add-hook 'kill-buffer-hook
  474. #'auto-revert-notify-rm-watch nil t))
  475. ;; Fallback to file checks.
  476. (setq-local auto-revert-use-notify nil)))))
  477. ;; If we have file notifications, we want to update the auto-revert buffers
  478. ;; immediately when a notification occurs. Since file updates can happen very
  479. ;; often, we want to skip some revert operations so that we don't spend all our
  480. ;; time reverting the buffer.
  481. ;;
  482. ;; We do this by reverting immediately in response to the first in a flurry of
  483. ;; notifications. We suppress subsequent notifications until the next time
  484. ;; `auto-revert-buffers' is called (this happens on a timer with a period set by
  485. ;; `auto-revert-interval').
  486. (defvar auto-revert-buffers-counter 1
  487. "Incremented each time `auto-revert-buffers' is called")
  488. (defvar-local auto-revert-buffers-counter-lockedout 0
  489. "Buffer-local value to indicate whether we should immediately
  490. update the buffer on a notification event or not. If
  491. (= auto-revert-buffers-counter-lockedout
  492. auto-revert-buffers-counter)
  493. then the updates are locked out, and we wait until the next call
  494. of `auto-revert-buffers' to revert the buffer. If no lockout is
  495. present, then we revert immediately and set the lockout, so that
  496. no more reverts are possible until the next call of
  497. `auto-revert-buffers'")
  498. (defun auto-revert-notify-handler (event)
  499. "Handle an EVENT returned from file notification."
  500. (with-demoted-errors
  501. (let* ((descriptor (car event))
  502. (action (nth 1 event))
  503. (file (nth 2 event))
  504. (file1 (nth 3 event)) ;; Target of `renamed'.
  505. (buffers (gethash descriptor
  506. auto-revert-notify-watch-descriptor-hash-list)))
  507. ;; Check, that event is meant for us.
  508. (cl-assert descriptor)
  509. ;; Since we watch a directory, a file name must be returned.
  510. (cl-assert (stringp file))
  511. (when (eq action 'renamed) (cl-assert (stringp file1)))
  512. (if (eq action 'stopped)
  513. ;; File notification has stopped. Continue with polling.
  514. (cl-dolist (buffer
  515. (if global-auto-revert-mode
  516. (buffer-list) auto-revert-buffer-list))
  517. (with-current-buffer buffer
  518. (when (and (equal descriptor auto-revert-notify-watch-descriptor)
  519. (or
  520. ;; A buffer associated with a file.
  521. (and (stringp buffer-file-name)
  522. (string-equal
  523. (file-name-nondirectory file)
  524. (file-name-nondirectory buffer-file-name)))
  525. ;; A buffer w/o a file, like dired.
  526. (null buffer-file-name)))
  527. (auto-revert-notify-rm-watch)
  528. (setq-local auto-revert-use-notify nil))))
  529. ;; Loop over all buffers, in order to find the intended one.
  530. (cl-dolist (buffer buffers)
  531. (when (buffer-live-p buffer)
  532. (with-current-buffer buffer
  533. (when (or
  534. ;; A buffer associated with a file.
  535. (and (stringp buffer-file-name)
  536. (or
  537. (and (memq
  538. action '(attribute-changed changed created))
  539. (string-equal
  540. (file-name-nondirectory file)
  541. (file-name-nondirectory buffer-file-name)))
  542. (and (eq action 'renamed)
  543. (string-equal
  544. (file-name-nondirectory file1)
  545. (file-name-nondirectory buffer-file-name)))))
  546. ;; A buffer w/o a file, like dired.
  547. (and (null buffer-file-name)
  548. (memq action '(created renamed deleted))))
  549. ;; Mark buffer modified.
  550. (setq auto-revert-notify-modified-p t)
  551. ;; Revert the buffer now if we're not locked out.
  552. (when (/= auto-revert-buffers-counter-lockedout
  553. auto-revert-buffers-counter)
  554. (auto-revert-handler)
  555. (setq auto-revert-buffers-counter-lockedout
  556. auto-revert-buffers-counter))
  557. ;; No need to check other buffers.
  558. (cl-return)))))))))
  559. (defun auto-revert-active-p ()
  560. "Check if auto-revert is active (in current buffer or globally)."
  561. (or auto-revert-mode
  562. auto-revert-tail-mode
  563. (and
  564. global-auto-revert-mode
  565. (not global-auto-revert-ignore-buffer)
  566. (not (memq major-mode
  567. global-auto-revert-ignore-modes)))))
  568. (defun auto-revert-handler ()
  569. "Revert current buffer, if appropriate.
  570. This is an internal function used by Auto-Revert Mode."
  571. (let* ((buffer (current-buffer)) size
  572. ;; Tramp caches the file attributes. Setting
  573. ;; `remote-file-name-inhibit-cache' forces Tramp to reread
  574. ;; the values.
  575. (remote-file-name-inhibit-cache t)
  576. (revert
  577. (if buffer-file-name
  578. (and (or auto-revert-remote-files
  579. (not (file-remote-p buffer-file-name)))
  580. (or (not auto-revert-use-notify)
  581. auto-revert-notify-modified-p)
  582. (if auto-revert-tail-mode
  583. (and (file-readable-p buffer-file-name)
  584. (/= auto-revert-tail-pos
  585. (setq size
  586. (nth 7 (file-attributes
  587. buffer-file-name)))))
  588. (funcall (or buffer-stale-function
  589. #'buffer-stale--default-function)
  590. t)))
  591. (and (or auto-revert-mode
  592. global-auto-revert-non-file-buffers)
  593. (funcall (or buffer-stale-function
  594. #'buffer-stale--default-function)
  595. t))))
  596. eob eoblist)
  597. (setq auto-revert-notify-modified-p nil)
  598. (when revert
  599. (when (and auto-revert-verbose
  600. (not (eq revert 'fast)))
  601. (message "Reverting buffer `%s'." (buffer-name)))
  602. ;; If point (or a window point) is at the end of the buffer, we
  603. ;; want to keep it at the end after reverting. This allows one
  604. ;; to tail a file.
  605. (when buffer-file-name
  606. (setq eob (eobp))
  607. (walk-windows
  608. (lambda (window)
  609. (and (eq (window-buffer window) buffer)
  610. (= (window-point window) (point-max))
  611. (push window eoblist)))
  612. 'no-mini t))
  613. (if auto-revert-tail-mode
  614. (auto-revert-tail-handler size)
  615. ;; Bind buffer-read-only in case user has done C-x C-q, so as
  616. ;; not to forget that. This gives undesirable results when
  617. ;; the file's mode changes, but that is less common.
  618. (let ((buffer-read-only buffer-read-only))
  619. ;; Bug#23276: When the file has been deleted, keep the
  620. ;; buffer unchanged.
  621. (ignore-errors
  622. (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes))))
  623. (when buffer-file-name
  624. (when eob (goto-char (point-max)))
  625. (dolist (window eoblist)
  626. (set-window-point window (point-max)))))
  627. ;; `preserve-modes' avoids changing the (minor) modes. But we do
  628. ;; want to reset the mode for VC, so we do it manually.
  629. (when (or revert auto-revert-check-vc-info)
  630. (vc-refresh-state))))
  631. (defun auto-revert-tail-handler (size)
  632. (let ((modified (buffer-modified-p))
  633. (inhibit-read-only t) ; Ignore.
  634. (file buffer-file-name)
  635. (buffer-file-name nil)) ; Ignore that file has changed.
  636. (when (/= auto-revert-tail-pos size)
  637. (run-hooks 'before-revert-hook)
  638. (undo-boundary)
  639. (save-restriction
  640. (widen)
  641. (save-excursion
  642. (goto-char (point-max))
  643. (insert-file-contents file nil
  644. (and (< auto-revert-tail-pos size)
  645. auto-revert-tail-pos)
  646. size)))
  647. (run-hooks 'after-revert-hook)
  648. (undo-boundary)
  649. (setq auto-revert-tail-pos size)
  650. (restore-buffer-modified-p modified)))
  651. (set-visited-file-modtime))
  652. (defun auto-revert-buffers ()
  653. "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
  654. Should `global-auto-revert-mode' be active all file buffers are checked.
  655. Should `auto-revert-mode' be active in some buffers, those buffers
  656. are checked.
  657. Non-file buffers that have a custom `revert-buffer-function' and
  658. `buffer-stale-function' are reverted either when Auto-Revert
  659. Mode is active in that buffer, or when the variable
  660. `global-auto-revert-non-file-buffers' is non-nil and Global
  661. Auto-Revert Mode is active.
  662. This function stops whenever there is user input. The buffers not
  663. checked are stored in the variable `auto-revert-remaining-buffers'.
  664. To avoid starvation, the buffers in `auto-revert-remaining-buffers'
  665. are checked first the next time this function is called.
  666. This function is also responsible for removing buffers no longer in
  667. Auto-Revert Mode from `auto-revert-buffer-list', and for canceling
  668. the timer when no buffers need to be checked."
  669. (setq auto-revert-buffers-counter
  670. (1+ auto-revert-buffers-counter))
  671. (save-match-data
  672. (let ((bufs (if global-auto-revert-mode
  673. (buffer-list)
  674. auto-revert-buffer-list))
  675. remaining new)
  676. ;; Partition `bufs' into two halves depending on whether or not
  677. ;; the buffers are in `auto-revert-remaining-buffers'. The two
  678. ;; halves are then re-joined with the "remaining" buffers at the
  679. ;; head of the list.
  680. (dolist (buf auto-revert-remaining-buffers)
  681. (if (memq buf bufs)
  682. (push buf remaining)))
  683. (dolist (buf bufs)
  684. (if (not (memq buf remaining))
  685. (push buf new)))
  686. (setq bufs (nreverse (nconc new remaining)))
  687. (while (and bufs
  688. (not (and auto-revert-stop-on-user-input
  689. (input-pending-p))))
  690. (let ((buf (car bufs)))
  691. (with-current-buffer buf
  692. (if (buffer-live-p buf)
  693. (progn
  694. ;; Test if someone has turned off Auto-Revert Mode
  695. ;; in a non-standard way, for example by changing
  696. ;; major mode.
  697. (if (and (not auto-revert-mode)
  698. (not auto-revert-tail-mode)
  699. (memq buf auto-revert-buffer-list))
  700. (auto-revert-remove-current-buffer))
  701. (when (auto-revert-active-p)
  702. ;; Enable file notification.
  703. (when (and auto-revert-use-notify
  704. (not auto-revert-notify-watch-descriptor))
  705. (auto-revert-notify-add-watch))
  706. (auto-revert-handler)))
  707. ;; Remove dead buffer from `auto-revert-buffer-list'.
  708. (auto-revert-remove-current-buffer))))
  709. (setq bufs (cdr bufs)))
  710. (setq auto-revert-remaining-buffers bufs)
  711. ;; Check if we should cancel the timer.
  712. (when (and (not global-auto-revert-mode)
  713. (null auto-revert-buffer-list))
  714. (cancel-timer auto-revert-timer)
  715. (setq auto-revert-timer nil)))))
  716. ;; The end:
  717. (provide 'autorevert)
  718. (run-hooks 'auto-revert-load-hook)
  719. ;;; autorevert.el ends here