autorevert.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. ;;; autorevert.el --- revert buffers when files on disk change
  2. ;; Copyright (C) 1997-1999, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Anders Lindgren <andersl@andersl.com>
  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. ;; Since checking a remote file is too slow, these modes do not check
  35. ;; or revert remote files.
  36. ;;
  37. ;; Both modes operate by checking the time stamp of all files at
  38. ;; intervals of `auto-revert-interval'. The default is every five
  39. ;; seconds. The check is aborted whenever the user actually uses
  40. ;; Emacs. You should never even notice that this package is active
  41. ;; (except that your buffers will be reverted, of course).
  42. ;;
  43. ;; After reverting a file buffer, Auto Revert Mode normally puts point
  44. ;; at the same position that a regular manual revert would. However,
  45. ;; there is one exception to this rule. If point is at the end of the
  46. ;; buffer before reverting, it stays at the end. Similarly if point
  47. ;; is displayed at the end of a file buffer in any window, it will stay
  48. ;; at the end of the buffer in that window, even if the window is not
  49. ;; selected. This way, you can use Auto Revert Mode to `tail' a file.
  50. ;; Just put point at the end of the buffer and it will stay there.
  51. ;; These rules apply to file buffers. For non-file buffers, the
  52. ;; behavior may be mode dependent.
  53. ;;
  54. ;; While you can use Auto Revert Mode to tail a file, this package
  55. ;; contains a third minor mode, Auto Revert Tail Mode, which does so
  56. ;; more efficiently, as long as you are sure that the file will only
  57. ;; change by growing at the end. It only appends the new output,
  58. ;; instead of reverting the entire buffer. It does so even if the
  59. ;; buffer contains unsaved changes. (Because they will not be lost.)
  60. ;; Auto Revert Tail Mode works also for remote files.
  61. ;; Usage:
  62. ;;
  63. ;; Go to the appropriate buffer and press either of:
  64. ;; M-x auto-revert-mode RET
  65. ;; M-x auto-revert-tail-mode RET
  66. ;;
  67. ;; To activate Global Auto-Revert Mode, press:
  68. ;; M-x global-auto-revert-mode RET
  69. ;;
  70. ;; To activate Global Auto-Revert Mode every time Emacs is started
  71. ;; customize the option `global-auto-revert-mode' or the following
  72. ;; line could be added to your ~/.emacs:
  73. ;; (global-auto-revert-mode 1)
  74. ;;
  75. ;; The function `turn-on-auto-revert-mode' could be added to any major
  76. ;; mode hook to activate Auto-Revert Mode for all buffers in that
  77. ;; mode. For example, the following line will activate Auto-Revert
  78. ;; Mode in all C mode buffers:
  79. ;;
  80. ;; (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)
  81. ;;; Code:
  82. ;; Dependencies:
  83. (require 'timer)
  84. (eval-when-compile (require 'cl))
  85. ;; Custom Group:
  86. ;;
  87. ;; The two modes will be placed next to Auto Save Mode under the
  88. ;; Files group under Emacs.
  89. (defgroup auto-revert nil
  90. "Revert individual buffers when files on disk change.
  91. Auto-Revert Mode can be activated for individual buffer.
  92. Global Auto-Revert Mode applies to all buffers."
  93. :group 'files
  94. :group 'convenience)
  95. ;; Variables:
  96. ;;; What's this?: ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'.
  97. ;;; What's this?: ;;;###autoload
  98. (defvar auto-revert-mode nil
  99. "Non-nil when Auto-Revert Mode is active.
  100. Never set this variable directly, use the command `auto-revert-mode' instead.")
  101. (put 'auto-revert-mode 'permanent-local t)
  102. (defvar auto-revert-tail-mode nil
  103. "Non-nil when Auto-Revert Tail Mode is active.
  104. Never set this variable directly, use the command
  105. `auto-revert-tail-mode' instead.")
  106. (put 'auto-revert-tail-mode 'permanent-local t)
  107. (defvar auto-revert-timer nil
  108. "Timer used by Auto-Revert Mode.")
  109. (defcustom auto-revert-interval 5
  110. "Time, in seconds, between Auto-Revert Mode file checks.
  111. The value may be an integer or floating point number.
  112. If a timer is already active, there are two ways to make sure
  113. that the new value will take effect immediately. You can set
  114. this variable through Custom or you can call the command
  115. `auto-revert-set-timer' after setting the variable. Otherwise,
  116. the new value will take effect the first time Auto Revert Mode
  117. calls `auto-revert-set-timer' for internal reasons or in your
  118. next editing session."
  119. :group 'auto-revert
  120. :type 'number
  121. :set (lambda (variable value)
  122. (set-default variable value)
  123. (and (boundp 'auto-revert-timer)
  124. auto-revert-timer
  125. (auto-revert-set-timer))))
  126. (defcustom auto-revert-stop-on-user-input t
  127. "When non-nil, user input temporarily interrupts Auto-Revert Mode.
  128. With this setting, Auto-Revert Mode checks for user input after
  129. handling each buffer and does not process any further buffers
  130. \(until the next run of the timer) if user input is available.
  131. When nil, Auto-Revert Mode checks files and reverts buffers,
  132. with quitting disabled, without paying attention to user input.
  133. Thus, with this setting, Emacs might be non-responsive at times."
  134. :group 'auto-revert
  135. :type 'boolean)
  136. (defcustom auto-revert-verbose t
  137. "When nil, Auto-Revert Mode does not generate any messages.
  138. When non-nil, a message is generated whenever a file is reverted."
  139. :group 'auto-revert
  140. :type 'boolean)
  141. (defcustom auto-revert-mode-text " ARev"
  142. "String to display in the mode line when Auto-Revert Mode is active.
  143. \(When the string is not empty, make sure that it has a leading space.)"
  144. :tag "Auto Revert Mode Text" ; To separate it from `global-...'
  145. :group 'auto-revert
  146. :type 'string)
  147. (defcustom auto-revert-tail-mode-text " Tail"
  148. "String to display in the mode line when Auto-Revert Tail Mode is active.
  149. \(When the string is not empty, make sure that it has a leading space.)"
  150. :group 'auto-revert
  151. :type 'string
  152. :version "22.1")
  153. (defcustom auto-revert-mode-hook nil
  154. "Functions to run when Auto-Revert Mode is activated."
  155. :tag "Auto Revert Mode Hook" ; To separate it from `global-...'
  156. :group 'auto-revert
  157. :type 'hook)
  158. (defcustom global-auto-revert-mode-text ""
  159. "String to display when Global Auto-Revert Mode is active.
  160. The default is nothing since when this mode is active this text doesn't
  161. vary over time, or between buffers. Hence mode line text
  162. would only waste precious space."
  163. :group 'auto-revert
  164. :type 'string)
  165. (defcustom global-auto-revert-mode-hook nil
  166. "Hook called when Global Auto-Revert Mode is activated."
  167. :group 'auto-revert
  168. :type 'hook)
  169. (defcustom global-auto-revert-non-file-buffers nil
  170. "When nil, Global Auto-Revert mode operates only on file-visiting buffers.
  171. When non-nil, both file buffers and buffers with a custom
  172. `revert-buffer-function' and a `buffer-stale-function' are
  173. reverted by Global Auto-Revert mode. These include the Buffer
  174. List buffer displayed by `buffer-menu', and Dired buffers showing
  175. complete local directories. The Buffer List buffer reverts every
  176. `auto-revert-interval' seconds; Dired buffers when the file list of
  177. the main directory changes. Dired buffers do not auto-revert as
  178. a result of changes in subdirectories, or in the contents, size,
  179. modes, etc., of files. You may still sometimes want to revert
  180. them manually.
  181. Use this option with care since it could lead to excessive auto-reverts.
  182. For more information, see Info node `(emacs)Autorevert'."
  183. :group 'auto-revert
  184. :type 'boolean
  185. :link '(info-link "(emacs)Autorevert"))
  186. (defcustom global-auto-revert-ignore-modes ()
  187. "List of major modes Global Auto-Revert Mode should not check."
  188. :group 'auto-revert
  189. :type '(repeat sexp))
  190. (defcustom auto-revert-load-hook nil
  191. "Functions to run when Auto-Revert Mode is first loaded."
  192. :tag "Load Hook"
  193. :group 'auto-revert
  194. :type 'hook)
  195. (defcustom auto-revert-check-vc-info nil
  196. "If non-nil Auto Revert Mode reliably updates version control info.
  197. Auto Revert Mode updates version control info whenever the buffer
  198. needs reverting, regardless of the value of this variable.
  199. However, the version control state can change without changes to
  200. the work file. If the change is made from the current Emacs
  201. session, all info is updated. But if, for instance, a new
  202. version is checked in from outside the current Emacs session, the
  203. version control number in the mode line, as well as other version
  204. control related information, may not be properly updated. If you
  205. are worried about this, set this variable to a non-nil value.
  206. This currently works by automatically updating the version
  207. control info every `auto-revert-interval' seconds. Nevertheless,
  208. it should not cause excessive CPU usage on a reasonably fast
  209. machine, if it does not apply to too many version controlled
  210. buffers. CPU usage depends on the version control system."
  211. :group 'auto-revert
  212. :type 'boolean
  213. :version "22.1")
  214. (defvar global-auto-revert-ignore-buffer nil
  215. "When non-nil, Global Auto-Revert Mode will not revert this buffer.
  216. This variable becomes buffer local when set in any fashion.")
  217. (make-variable-buffer-local 'global-auto-revert-ignore-buffer)
  218. ;; Internal variables:
  219. (defvar auto-revert-buffer-list ()
  220. "List of buffers in Auto-Revert Mode.
  221. Note that only Auto-Revert Mode, never Global Auto-Revert Mode, adds
  222. buffers to this list.
  223. The timer function `auto-revert-buffers' is responsible for purging
  224. the list of old buffers.")
  225. (defvar auto-revert-remaining-buffers ()
  226. "Buffers not checked when user input stopped execution.")
  227. (defvar auto-revert-tail-pos 0
  228. "Position of last known end of file.")
  229. (add-hook 'find-file-hook
  230. (lambda ()
  231. (set (make-local-variable 'auto-revert-tail-pos)
  232. (nth 7 (file-attributes buffer-file-name)))))
  233. ;; Functions:
  234. ;;;###autoload
  235. (define-minor-mode auto-revert-mode
  236. "Toggle reverting buffer when the file changes (Auto Revert mode).
  237. With a prefix argument ARG, enable Auto Revert mode if ARG is
  238. positive, and disable it otherwise. If called from Lisp, enable
  239. the mode if ARG is omitted or nil.
  240. Auto Revert mode is a minor mode that affects only the current
  241. buffer. When enabled, it reverts the buffer when the file on
  242. disk changes.
  243. Use `global-auto-revert-mode' to automatically revert all buffers.
  244. Use `auto-revert-tail-mode' if you know that the file will only grow
  245. without being changed in the part that is already in the buffer."
  246. :group 'auto-revert :lighter auto-revert-mode-text
  247. (if auto-revert-mode
  248. (if (not (memq (current-buffer) auto-revert-buffer-list))
  249. (push (current-buffer) auto-revert-buffer-list))
  250. (setq auto-revert-buffer-list
  251. (delq (current-buffer) auto-revert-buffer-list)))
  252. (auto-revert-set-timer)
  253. (when auto-revert-mode
  254. (auto-revert-buffers)
  255. (setq auto-revert-tail-mode nil)))
  256. ;;;###autoload
  257. (defun turn-on-auto-revert-mode ()
  258. "Turn on Auto-Revert Mode.
  259. This function is designed to be added to hooks, for example:
  260. (add-hook 'c-mode-hook 'turn-on-auto-revert-mode)"
  261. (auto-revert-mode 1))
  262. ;;;###autoload
  263. (define-minor-mode auto-revert-tail-mode
  264. "Toggle reverting tail of buffer when the file grows.
  265. With a prefix argument ARG, enable Auto-Revert Tail mode if ARG
  266. is positive, and disable it otherwise. If called from Lisp,
  267. enable the mode if ARG is omitted or nil.
  268. When Auto Revert Tail mode is enabled, the tail of the file is
  269. constantly followed, as with the shell command `tail -f'. This
  270. means that whenever the file grows on disk (presumably because
  271. some background process is appending to it from time to time),
  272. this is reflected in the current buffer.
  273. You can edit the buffer and turn this mode off and on again as
  274. you please. But make sure the background process has stopped
  275. writing before you save the file!
  276. Use `auto-revert-mode' for changes other than appends!"
  277. :group 'find-file :lighter auto-revert-tail-mode-text
  278. (when auto-revert-tail-mode
  279. (unless buffer-file-name
  280. (auto-revert-tail-mode 0)
  281. (error "This buffer is not visiting a file"))
  282. (if (and (buffer-modified-p)
  283. (zerop auto-revert-tail-pos) ; library was loaded only after finding file
  284. (not (y-or-n-p "Buffer is modified, so tail offset may be wrong. Proceed? ")))
  285. (auto-revert-tail-mode 0)
  286. ;; a-r-tail-pos stores the size of the file at the time of the
  287. ;; last revert. After this package loads, it adds a
  288. ;; find-file-hook to set this variable every time a file is
  289. ;; loaded. If the package is loaded only _after_ visiting the
  290. ;; file to be reverted, then we have no idea what the value of
  291. ;; a-r-tail-pos should have been when the file was visited. If
  292. ;; the file has changed on disk in the meantime, all we can do
  293. ;; is offer to revert the whole thing. If you choose not to
  294. ;; revert, then you might miss some output then happened
  295. ;; between visiting the file and activating a-r-t-mode.
  296. (and (zerop auto-revert-tail-pos)
  297. (not (verify-visited-file-modtime (current-buffer)))
  298. (y-or-n-p "File changed on disk, content may be missing. \
  299. Perform a full revert? ")
  300. ;; Use this (not just revert-buffer) for point-preservation.
  301. (auto-revert-handler))
  302. ;; else we might reappend our own end when we save
  303. (add-hook 'before-save-hook (lambda () (auto-revert-tail-mode 0)) nil t)
  304. (or (local-variable-p 'auto-revert-tail-pos) ; don't lose prior position
  305. (set (make-local-variable 'auto-revert-tail-pos)
  306. (nth 7 (file-attributes buffer-file-name))))
  307. ;; let auto-revert-mode set up the mechanism for us if it isn't already
  308. (or auto-revert-mode
  309. (let ((auto-revert-tail-mode t))
  310. (auto-revert-mode 1)))
  311. (setq auto-revert-mode nil))))
  312. ;;;###autoload
  313. (defun turn-on-auto-revert-tail-mode ()
  314. "Turn on Auto-Revert Tail mode.
  315. This function is designed to be added to hooks, for example:
  316. (add-hook 'my-logfile-mode-hook 'turn-on-auto-revert-tail-mode)"
  317. (auto-revert-tail-mode 1))
  318. ;;;###autoload
  319. (define-minor-mode global-auto-revert-mode
  320. "Toggle Global Auto Revert mode.
  321. With a prefix argument ARG, enable Global Auto Revert mode if ARG
  322. is positive, and disable it otherwise. If called from Lisp,
  323. enable the mode if ARG is omitted or nil.
  324. Global Auto Revert mode is a global minor mode that reverts any
  325. buffer associated with a file when the file changes on disk. Use
  326. `auto-revert-mode' to revert a particular buffer.
  327. If `global-auto-revert-non-file-buffers' is non-nil, this mode
  328. may also revert some non-file buffers, as described in the
  329. documentation of that variable. It ignores buffers with modes
  330. matching `global-auto-revert-ignore-modes', and buffers with a
  331. non-nil vale of `global-auto-revert-ignore-buffer'.
  332. This function calls the hook `global-auto-revert-mode-hook'.
  333. It displays the text that `global-auto-revert-mode-text'
  334. specifies in the mode line."
  335. :global t :group 'auto-revert :lighter global-auto-revert-mode-text
  336. (auto-revert-set-timer)
  337. (when global-auto-revert-mode
  338. (auto-revert-buffers)))
  339. (defun auto-revert-set-timer ()
  340. "Restart or cancel the timer used by Auto-Revert Mode.
  341. If such a timer is active, cancel it. Start a new timer if
  342. Global Auto-Revert Mode is active or if Auto-Revert Mode is active
  343. in some buffer. Restarting the timer ensures that Auto-Revert Mode
  344. will use an up-to-date value of `auto-revert-interval'"
  345. (interactive)
  346. (if (timerp auto-revert-timer)
  347. (cancel-timer auto-revert-timer))
  348. (setq auto-revert-timer
  349. (if (or global-auto-revert-mode auto-revert-buffer-list)
  350. (run-with-timer auto-revert-interval
  351. auto-revert-interval
  352. 'auto-revert-buffers))))
  353. (defun auto-revert-active-p ()
  354. "Check if auto-revert is active (in current buffer or globally)."
  355. (or auto-revert-mode
  356. auto-revert-tail-mode
  357. (and
  358. global-auto-revert-mode
  359. (not global-auto-revert-ignore-buffer)
  360. (not (memq major-mode
  361. global-auto-revert-ignore-modes)))))
  362. (defun auto-revert-handler ()
  363. "Revert current buffer, if appropriate.
  364. This is an internal function used by Auto-Revert Mode."
  365. (when (or auto-revert-tail-mode (not (buffer-modified-p)))
  366. (let* ((buffer (current-buffer)) size
  367. (revert
  368. (or (and buffer-file-name
  369. (if auto-revert-tail-mode
  370. ;; Tramp caches the file attributes. Setting
  371. ;; `remote-file-name-inhibit-cache' forces Tramp
  372. ;; to reread the values.
  373. (let ((remote-file-name-inhibit-cache t))
  374. (and (file-readable-p buffer-file-name)
  375. (/= auto-revert-tail-pos
  376. (setq size
  377. (nth 7 (file-attributes
  378. buffer-file-name))))))
  379. (and (not (file-remote-p buffer-file-name))
  380. (file-readable-p buffer-file-name)
  381. (not (verify-visited-file-modtime buffer)))))
  382. (and (or auto-revert-mode
  383. global-auto-revert-non-file-buffers)
  384. revert-buffer-function
  385. (boundp 'buffer-stale-function)
  386. (functionp buffer-stale-function)
  387. (funcall buffer-stale-function t))))
  388. eob eoblist)
  389. (when revert
  390. (when (and auto-revert-verbose
  391. (not (eq revert 'fast)))
  392. (message "Reverting buffer `%s'." (buffer-name)))
  393. ;; If point (or a window point) is at the end of the buffer,
  394. ;; we want to keep it at the end after reverting. This allows
  395. ;; to tail a file.
  396. (when buffer-file-name
  397. (setq eob (eobp))
  398. (walk-windows
  399. (lambda (window)
  400. (and (eq (window-buffer window) buffer)
  401. (= (window-point window) (point-max))
  402. (push window eoblist)))
  403. 'no-mini t))
  404. (if auto-revert-tail-mode
  405. (auto-revert-tail-handler size)
  406. ;; Bind buffer-read-only in case user has done C-x C-q,
  407. ;; so as not to forget that. This gives undesirable results
  408. ;; when the file's mode changes, but that is less common.
  409. (let ((buffer-read-only buffer-read-only))
  410. (revert-buffer 'ignore-auto 'dont-ask 'preserve-modes)))
  411. (when buffer-file-name
  412. (when eob (goto-char (point-max)))
  413. (dolist (window eoblist)
  414. (set-window-point window (point-max)))))
  415. ;; `preserve-modes' avoids changing the (minor) modes. But we
  416. ;; do want to reset the mode for VC, so we do it manually.
  417. (when (or revert auto-revert-check-vc-info)
  418. (vc-find-file-hook)))))
  419. (defun auto-revert-tail-handler (size)
  420. (let ((modified (buffer-modified-p))
  421. (inhibit-read-only t) ; Ignore.
  422. (file buffer-file-name)
  423. (buffer-file-name nil)) ; Ignore that file has changed.
  424. (when (/= auto-revert-tail-pos size)
  425. (run-hooks 'before-revert-hook)
  426. (undo-boundary)
  427. (save-restriction
  428. (widen)
  429. (save-excursion
  430. (goto-char (point-max))
  431. (insert-file-contents file nil
  432. (and (< auto-revert-tail-pos size)
  433. auto-revert-tail-pos)
  434. size)))
  435. (run-hooks 'after-revert-hook)
  436. (undo-boundary)
  437. (setq auto-revert-tail-pos size)
  438. (restore-buffer-modified-p modified)))
  439. (set-visited-file-modtime))
  440. (defun auto-revert-buffers ()
  441. "Revert buffers as specified by Auto-Revert and Global Auto-Revert Mode.
  442. Should `global-auto-revert-mode' be active all file buffers are checked.
  443. Should `auto-revert-mode' be active in some buffers, those buffers
  444. are checked.
  445. Non-file buffers that have a custom `revert-buffer-function' and
  446. a `buffer-stale-function' are reverted either when Auto-Revert
  447. Mode is active in that buffer, or when the variable
  448. `global-auto-revert-non-file-buffers' is non-nil and Global
  449. Auto-Revert Mode is active.
  450. This function stops whenever there is user input. The buffers not
  451. checked are stored in the variable `auto-revert-remaining-buffers'.
  452. To avoid starvation, the buffers in `auto-revert-remaining-buffers'
  453. are checked first the next time this function is called.
  454. This function is also responsible for removing buffers no longer in
  455. Auto-Revert mode from `auto-revert-buffer-list', and for canceling
  456. the timer when no buffers need to be checked."
  457. (save-match-data
  458. (let ((bufs (if global-auto-revert-mode
  459. (buffer-list)
  460. auto-revert-buffer-list))
  461. (remaining ())
  462. (new ()))
  463. ;; Partition `bufs' into two halves depending on whether or not
  464. ;; the buffers are in `auto-revert-remaining-buffers'. The two
  465. ;; halves are then re-joined with the "remaining" buffers at the
  466. ;; head of the list.
  467. (dolist (buf auto-revert-remaining-buffers)
  468. (if (memq buf bufs)
  469. (push buf remaining)))
  470. (dolist (buf bufs)
  471. (if (not (memq buf remaining))
  472. (push buf new)))
  473. (setq bufs (nreverse (nconc new remaining)))
  474. (while (and bufs
  475. (not (and auto-revert-stop-on-user-input
  476. (input-pending-p))))
  477. (let ((buf (car bufs)))
  478. (if (buffer-live-p buf)
  479. (with-current-buffer buf
  480. ;; Test if someone has turned off Auto-Revert Mode in a
  481. ;; non-standard way, for example by changing major mode.
  482. (if (and (not auto-revert-mode)
  483. (not auto-revert-tail-mode)
  484. (memq buf auto-revert-buffer-list))
  485. (setq auto-revert-buffer-list
  486. (delq buf auto-revert-buffer-list)))
  487. (when (auto-revert-active-p) (auto-revert-handler)))
  488. ;; Remove dead buffer from `auto-revert-buffer-list'.
  489. (setq auto-revert-buffer-list
  490. (delq buf auto-revert-buffer-list))))
  491. (setq bufs (cdr bufs)))
  492. (setq auto-revert-remaining-buffers bufs)
  493. ;; Check if we should cancel the timer.
  494. (when (and (not global-auto-revert-mode)
  495. (null auto-revert-buffer-list))
  496. (cancel-timer auto-revert-timer)
  497. (setq auto-revert-timer nil)))))
  498. ;; The end:
  499. (provide 'autorevert)
  500. (run-hooks 'auto-revert-load-hook)
  501. ;;; autorevert.el ends here