2

revert-buffer is often slower than closing an opening the same file.

I've noticed - for example, I can undo to the state of the file before reverting.

While this might be useful in some cases, I would like to clear undo history and load the file as if I'd just opened emacs.

Some possible solutions:

Fast Revert

  • Disable undo.
  • Clear undo history, jump list ... etc.
  • Clear the buffer.
  • Load the data from the file into the buffer.
  • Re-enable undo.

Fake Revert

  • Store the scroll & cursor position.
  • Close the buffer.
  • Open the buffer.
  • Restore the scroll & cursor position.

While I could write this, it seems like there may be a more elegant solution then writing my own fast-revert-buffer function.

Does Emacs provide a way to do this already?

ideasman42
  • 8,375
  • 1
  • 28
  • 105

1 Answers1

2

The revert-buffer function takes a few parameters which I think make the reload faster. I've got this bound to a keyboard shortcut for reloading the current buffer, it feels as fast as when you first open the file:

(defun reload-file-preserve-point ()
  (interactive)
  (when (or (not (buffer-modified-p))
            (y-or-n-p "Reverting will discard changes. Proceed?"))
    (save-excursion
      (revert-buffer t t t))
    (setq buffer-undo-list nil)
    (message "Buffer reverted")))
saintamh
  • 56
  • 3
  • 5
    Welcome to emacs.SE and thank you for your answer! Maybe you could briefly describe what the args do that make the revert faster (this is something I immediately think I want to know when I read the answer). – JeanPierre Oct 18 '19 at 11:25