3

In one of my windows I've opened "output.png" and I would like emacs to automatically reload it if it has changed.

I would like to do this for all PNG files which are currently open, but only PNG files. How to I do this?

Stein
  • 551
  • 1
  • 5
  • 11
  • Here's a related thread for loading minor modes depending on file suffixes: http://stackoverflow.com/questions/13945782/emacs-auto-minor-mode-based-on-extension. In your case the minor mode would be: auto-revert-mode – Dieter.Wilhelm Sep 29 '15 at 06:19

1 Answers1

2

You need to enable the auto-revert-mode. It will reload the file every 5 seconds. see gnu.org/emacs/manual/Reverting

To enable it in the image buffer for the current session only type M-x auto-revert-mode

Note: in my experience with Emacs 25.3 the buffer will display the image as ascii text after reverting it. To fix that type: M-x auto-image-file-mode

To use these settings for all buffers displaying an image I've added to my ~/.emacs/ file:

(add-hook 'image-mode-hook
  (lambda ()
    (auto-revert-mode)
    (auto-image-file-mode)))
acorello
  • 121
  • 3
  • Thanks to this answer for setting me on the right path: https://emacs.stackexchange.com/a/245/17434 – acorello Jan 15 '18 at 20:22
  • Note that nowadays Emacs tries to use filesystem notifications rather than polling (i.e. changes are reported to Emacs by the OS if and when they occur). `auto-revert-use-notify` was introduced in 24.4, but then disabled by `global-auto-revert-mode` in 25.1 due to [bug#22814](https://debbugs.gnu.org/22814) (which seems like it was specific to OSX, but I haven't read it in detail), and was re-enabled again in 26.1, with a solution for that bug having been committed. – phils Sep 13 '18 at 07:35
  • `auto-image-file-mode` is a global minor mode, so just use `(auto-image-file-mode 1)` in your init file, rather than calling it every time `image-mode-hook` runs. – phils Sep 13 '18 at 07:39
  • After which you can reduce that hook to: `(add-hook 'image-mode-hook 'auto-revert-mode)` – phils Sep 13 '18 at 07:39