8

My init file includes

(setq global-auto-revert-mode t)

and I can confirm it's true with C-h v. But I still have to manually revert all my files with revert-buffer whenever I change git branches. I even get as far as trying to save and getting the "file changed on disk" prompt. Any suggestions for how to debug this?

18
  • Try putting this in your init instead: (global-auto-revert-mode 1) Apr 27, 2015 at 17:38
  • @kaushalmodi no luck (I would have been really surprised)
    – djechlin
    Apr 27, 2015 at 17:45
  • Could it be a matter with auto-revert-interval? I use it and it works for me, excepting files over tramp. Apr 27, 2015 at 17:55
  • Looks like a bug that crept in in 24.5 and 25.0. @SwarnenduBiswas are you on emacs 24.4? Apr 27, 2015 at 17:58
  • @djechlin I verified using emacs -Q sessions that auto revert mode does not work on emacs 24.5 and 25.0 but works fine on 24.4. What version are you on? Apr 27, 2015 at 17:59

3 Answers 3

14

A minor mode is not enabled/disabled by setting a variable. The minor mode variable should be used to check the "mode enabled" status, not to set the mode state. If its value is t, the minor mode is on, else the mode is off.


See the below snippets to see how to control any minor mode. Replace MINOR-MODE-NAME with the actual minor mode name you are dealing with (global-auto-revert-name as is the case in the question).


Enabling a minor mode

Using elisp

Directly

(MINOR-MODE-NAME)
;; or
(MINOR-MODE-NAME 1)

Based on a hook activation

(add-hook 'SOME-HOOK #'MINOR-MODE-NAME)

Disabling a minor mode

Using elisp

Directly

(MINOR-MODE-NAME 0)
;; or
(MINOR-MODE-NAME -1)

Based on a hook activation

(add-hook 'SOME-HOOK (lambda () (MINOR-MODE-NAME -1)))

Toggling a minor mode

Interactively

M-x MINOR-MODE-NAME

Using a key binding

(global-set-key (kbd "<KEY>") #'MINOR-MODE-NAME)

Using elisp (non-interactively)

(MINOR-MODE-NAME 'toggle)

Sources

6
  • I've run into some modes where you have to pass t or nil instead of numbers. Sep 28, 2016 at 18:13
  • @JosephGarvin It might be worthwhile to find out which those modes are.. and may be file a bug report to fix that inconsistency if in fact they are defined as minor modes? Sep 28, 2016 at 18:20
  • The answer is very abstract. I don't know how to use it. Mar 3, 2019 at 16:57
  • @ctrl-alt-delor What's the minor mode you're trying to control that led you to this answer? Mar 5, 2019 at 12:42
  • The one in the question. (global-auto-revert-mode) Mar 6, 2019 at 8:11
5

You don't set the variable using setq, you call the function:

(global-auto-revert-mode)
5
  • I'm wondering how that is different from (global-auto-revert-mode 1). Also if you are adding this to your init, you shouldn't even need that argument. Just (global-auto-revert-mode) should suffice. Check out Xah's blog post for more info. Apr 27, 2015 at 18:57
  • As with any mode defined using define-global-minor-mode, the only valid values are numbers, nil, and toggle. The fact that t works is only due to the fact that prefix-numeric-value doesn't choke on it and treats it like 1. Apr 27, 2015 at 20:57
  • @Lindydancer Yeah, I'd like djechlin to clarify why passing 1 as argument did not work. Apr 27, 2015 at 21:14
  • @kaushalmodi I was still setq-ing - I thought you were just changing t to a 1, I overlooked the change from setq to calling the function.
    – djechlin
    Apr 27, 2015 at 21:26
  • @djechlin Thanks for clarifying. But for the sake of clarify and consistency, it is recommended to pass it an arg 1 or nothing if you want to enable that minor mode from elisp in your init. Apr 27, 2015 at 21:31
1

auto-revert only works for buffers which are not modified. So at least for the "trying to save and getting the file changed on disk prompt" the behavior you describe is normal.

4
  • I opened the same file simultaneously in an emacs 25.0 instance and emacs 24.4 instance (both with -Q option) and did M-x global-auto-revert-mode in both. From terminal when I echoed some string ( echo 1234 >> somefile.txt) to that file, only the emacs 24.4 buffer auto-reverted. Apr 27, 2015 at 18:04
  • Then please M-x report-emacs-bug.
    – Stefan
    Apr 27, 2015 at 18:08
  • Done! bug # 20441 Apr 27, 2015 at 18:19
  • The "bug" got resolved by itself.. was probably an issue with glib on my system. May 8, 2015 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.