Naming Registers

Magnar Sveen has another great post on What the .emacs.d!?. This time it's a nice hack to make magit run in the full frame and then restore the window configuration that existed when magit was invoked. I've been annoyed several times by having magit mess up my window configuration so I added Sveen's bit of Elisp to my init.el. But then I got to thinking: Sveen's code saves the window configuration across the call to magit-status with

(window-configuration-to-register :magit-fullscreen)

At first look this is merely the expected code to do that sort of thing but what about that :magit-fullscreen? Registers are named with a single character so what's going on with the :magit-fullscreen?

It turns out that this works because of an implementation detail in register.el. The list of active registers is kept in an alist that is searched with assq. Since identical symbols are eq in Elisp, using a symbol for the register name works. Of course, you can't do that interactively because the interactive declarations for the register functions specify a character input for the register name.

One could argue, I suppose, that naming a register with a symbol is taking advantage of undocumented behavior that could change in the future but that risk seems minimal. The benefit, on the other hand, is large. By using a symbol to name registers in Elisp code you avoid the risk of clobbering a register that the user may have set interactively. So Sveen's post provides not only an improvement to the behavior of magit-status but also teaches us a useful technique in Elisp programming.

This entry was posted in Programming and tagged . Bookmark the permalink.
  • Phil

    Tangentially, anyone who isn't using `winner-mode` (so that they can instantly undo any unwanted changes to their window configuration) is really missing out. It's also extremely useful for undoing intentional changes, of course -- once you know that you can restore things trivially, you have no issues with messing them up if you temporarily want something different.

    • jcs

      Hmmm. That’s interesting. I’ve never understood the point of winner-mode so I find your comment useful. Do you have a blog? If you do, could you do a post on winner-mode? If so, please leave a comment here and I’ll follow up with a link. If anyone else some wisdom on this, feel free to leave a comment.

      • Phil

        No blog, sorry. However I think it's a very easy sell -- you justified the use of winner-mode yourself with your comment that "I’ve been annoyed several times by having magit mess up my window configuration" (or you can substitute any relevant function for "magit" in that sentence).

        The point of winner-mode is that nothing can ever annoy you in this fashion again, because you gain the ability to trivially restore the window configuration to the way it was before something messed it up. Just undo the changes until you get back to where you want to be.

        It also enables you to do things like temporarily delete some (or all) windows in order to maximise the view of a particular buffer, and then simply undo that change to restore the previous configuration.

        In addition, because changing the buffer being displayed in a window counts as a window configuration change, winner-mode is also useful for switching back to the buffer(s) you were previously looking at, even if the size and arrangement of windows in the frame has not changed.