When working with Emacs, I can always learn something or come up some ideas about how to improve my editing experience–due to Emacs’s endless capability of customization.
Today’s trick is about revert-buffer. Revert-buffer is one of my most frequently used function as I program in Matlab or other IDE’s. You can intensively edit some file using Emacs, while the IDE (such as Matlab) can detect the change in the file on disk and auto-upload it. This gives you both the efficiency of editing in Emacs, and the nice features of IDE such as debugging, jump over files, view the values/types of variables/functions, etc. If you did some edits in the IDE, then you need to revert-buffer in Emacs to keep your buffer updated. Since I use this function so often, I bound revert-buffer to F8:
(global-set-key [f8] 'revert-buffer)
Besides this key-binding, there are two other convenient features about revert-buffer:
- You can set the variable revert-without-query to some regular expression so that the buffers with file names matching the regexp will be updated without confirming if you really want to revert-buffer. For my own situation, I set it to:
(setq revert-without-query (quote (".*.m")))
- You can turn on the global-auto-revert-mode to activate the automatically-detecting-file-change-then-automatically-revert-buffer feature of Emacs globally.
Besides these nice things about revert-buffer, there are two aspects of it I really don’t like:
- After revert-buffer, your position in the buffer is lost and you are at the beginning of the buffer again.
- The undo-chain is cleared up, so after revert-buffer, you cannot do any undo to your previous changes.
For the bad thing #2 I don’t have an idea yet, or maybe it is not a bad thing at all. After all, it’s your decision to revert the buffer and start loading it from the disk, right?
But for the bad thing #1, today I came up with an idea: how about to define tmp variable, to store the current position in it, then revert-buffer, then goto-char to tmp, and then kill the tmp? Here is the code of this hack:
(defun da-revert-buffer ()
"save the current position to tmp, then call revert-buffer, then goto-char(position)"
(setq tmp (point))
The code is very straight, and after this code I just did (global-set-key [f8] ‘da-revert-buffer).
One thing to mention: (revert-buffer) has 3 optional arguments:
(revert-buffer &optional IGNORE-AUTO NOCONFIRM PRESERVE-MODES)
The first argument is IGNORE-AUTO; revert-buffer only offer to revert from the auto-save file when this is nil. Note that the sense of this argument is the reverse of the prefix argument, for the sake of backward compatibility. IGNORE-AUTO is optional, defaulting to nil.
Optional second argument NOCONFIRM means don’t ask for confirmation at all. (The variable `revert-without-query’ offers another way to revert buffers without querying for confirmation.)
Optional third argument PRESERVE-MODES non-nil means don’t alter the files modes. Normally we reinitialize them using `normal-mode’.
I only used the first one, for disabling using the backup file as the one to revert from. I did not set the second one in the hope my da-revert-buffer can still use my setting of revert-without-query. However, there must be something wrong there and da-revert-buffer don’t recognize my setting of revert-without-query at all. I will fix this in the future.