10

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?

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
djechlin
  • 923
  • 8
  • 21
  • 1
    Try putting this in your init instead: `(global-auto-revert-mode 1)` – Kaushal Modi Apr 27 '15 at 17:38
  • @kaushalmodi no luck (I would have been really surprised) – djechlin Apr 27 '15 at 17:45
  • Could it be a matter with `auto-revert-interval`? I use it and it works for me, excepting files over tramp. – Swarnendu Biswas Apr 27 '15 at 17:55
  • Looks like a bug that crept in in 24.5 and 25.0. @SwarnenduBiswas are you on emacs 24.4? – Kaushal Modi Apr 27 '15 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? – Kaushal Modi Apr 27 '15 at 17:59
  • @kaushalmodi I recently upgraded to 24.5 about three days back, so my comment is from prior experience with 24.4. Haven't tested auto-revert-mode in 24.5. – Swarnendu Biswas Apr 27 '15 at 18:04
  • @kaushalmodi 24.3.1. – djechlin Apr 27 '15 at 18:21
  • @djechlin Can you recreate the problem in an `emacs -Q` session? Open a file (somefile.txt) in an emacs 24.3 instance with -Q option. Do `M-x global-auto-revert-mode`. From terminal, echo some string (`echo 1234 >> somefile.txt`). Please let us know if somefile.txt still does not auto revert. – Kaushal Modi Apr 27 '15 at 18:22
  • I saw you updated your question with the bug report; but I can see this bug only in emacs 24.5+. I cannot see that on 24.4 and I don't have 24.3 installed (so can't verify that). If you can reproduce this bug on 24.3 in an emacs -Q session, you should file a separate bug report for that. – Kaushal Modi Apr 27 '15 at 18:26
  • @kaushalmodi far simpler problem; see my answer. – djechlin Apr 27 '15 at 18:54
  • @djechlin But this ended up in discovering an actual bug in emacs 24.5+ :) – Kaushal Modi Apr 27 '15 at 18:58
  • Under OS X, 24.5 `auto-revert` works as intended. However, it looks like it uses the older timer system (`auto-revert-use-notify` is nil) and not the newer file notification system. – Lindydancer Apr 27 '15 at 20:37
  • @Lindydancer Thanks! I can confirm that auto revert works fine in emacs 24.5 if I set `auto-revert-use-notify` to `nil` (the default value is `t`). – Kaushal Modi Apr 27 '15 at 20:45
  • @kaushalmodi, good, then there is a practical work-around for other OS:es as well. (On a side note -- it's a great feeling that something I wrote back in 1997 is still widely used.) – Lindydancer Apr 27 '15 at 20:48
  • @Lindydancer Just to clarify, you have have set `auto-revert-use-notify` to `nil` on your emacs setup under OSX? Does it work if set to `t`? – Kaushal Modi Apr 27 '15 at 20:55
  • @kaushalmodi, I have not actively set it. I think it's nil since Emacs on OS X doesn't support file notifications. If I set it to `t`, it's reset to `nil` once `global-auto-revert-mode` is enabled. (It looks like `auto-revert-notify-add-watch` use the timer-based system as a fall-back when it fails to add a notification watch.) – Lindydancer Apr 27 '15 at 21:03
  • @Lindydancer Thanks, glad to see that the outcomes are consistent :) It would have been big pain to debug this; already added this fix to [my init](https://github.com/kaushalmodi/.emacs.d/tree/c5e8fc1f7b8eca22e09106d9dae95d176ca7d0bd/setup-files/setup-misc.el#L115-L119). – Kaushal Modi Apr 27 '15 at 21:11
  • It now works with file-notify too on my system. The problem probably was with glib. Surprisingly the issue resolved on killing and restarting the vnc server on which I work. – Kaushal Modi May 08 '15 at 13:05

3 Answers3

16

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

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
5

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

(global-auto-revert-mode)
djechlin
  • 923
  • 8
  • 21
  • 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](http://ergoemacs.org/emacs/emacs-tip_mode_on_off_toggle.html) for more info. – Kaushal Modi Apr 27 '15 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`. – Lindydancer Apr 27 '15 at 20:57
  • @Lindydancer Yeah, I'd like djechlin to clarify why passing `1` as argument [did not work](http://emacs.stackexchange.com/questions/10966/global-auto-revert-mode-doesnt-seem-to-work/10971#comment17004_10966). – Kaushal Modi Apr 27 '15 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 '15 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. – Kaushal Modi Apr 27 '15 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.

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • 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. – Kaushal Modi Apr 27 '15 at 18:04
  • Then please `M-x report-emacs-bug`. – Stefan Apr 27 '15 at 18:08
  • Done! [**bug # 20441**](http://debbugs.gnu.org/cgi/bugreport.cgi?bug=20441) – Kaushal Modi Apr 27 '15 at 18:19
  • The "bug" got resolved by itself.. was probably an issue with glib on my system. – Kaushal Modi May 08 '15 at 13:04