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.