Wednesday, October 18, 2006

Rotating background colors (Wednesday Emacs blogging)

Like every veteran Emacs user, I've long used multiple buffers, multiple windows in a buffer (C-x 2) and multiple frames (C-x 5 2) to edit multiple files simultaneously during heavy sessions of programming or writing. Lately I've been working on a monitor so vast, and reading so many different files simultaneously, that I've found it useful to run multiple instances of Emacs as well. (Among other things, this speeds up tab-completion when switching buffers with C-x b, because each instance has shorter buffer list.).

When running multiple Emacs instances, it gets hard to tell which frame corresponds to which running instance of Emacs. The following Elisp code enables rotation among a set of background colors on a keystroke:

(defvar background-color-rotation
  '("aliceblue" "thistle" "lemonchiffon" "khaki" "papayawhip"
    "honeydew" "mistyrose" "paleturquoise")
  "List of background color names to rotate")
(defun next-background-color ()
  "Rotates among colors in background-color-rotation."
  (set-variable 'background-color-rotation
                (append (cdr background-color-rotation)
                        (list (car background-color-rotation))))
  (car background-color-rotation))
(define-key global-map [f10]
  (lambda ()
    (interactive)
    (set-face-background 'default (next-background-color))))

Paste this into your startup file for recent FSF Emacs (.emacs) or XEmacs (.xemacs/init.el). As always you can also try it out quickly by pasting into your *scratch* buffer and using C-j to evaluate the defvar, the defun, and define-key expressions in turn. Once the above has been evaluated, hit F10 to rotate among the named background colors.

The code's pretty trivial, so if you know Elisp --- or even if you don't --- it should be relatively straightforward to customize it for different colors, or a different keystroke. To list all the color names your display supports, use M-x list-colors-display.

I considered making the macro save the background color rotation between Emacs invocations, so that each new instance would automatically come up with a different background color. It wouldn't be too hard to do this, but I decided it didn't fit the way I work. I like to have a particular color correspond to a particular task --- "aliceblue" for general hacking (the first Emacs I open when I login), "thistle" for writing notes to myself, etc. --- so I prefer to do the rotation manually.

Incidentally, note the use of string arguments to defvar and defun to document the function and variable. Some Python advocates tout Python's "innovative" and "unique" use of string literals in class and function bodies for documentation comments. I agree that it's a clever idea, but it predates Python by several decades. There's a reason that Emacs is called the "extensible, self-documenting display editor".

P.S. The title of this post declares my intention to post a random section from my Emacs init.el files every week until I run out of things to describe. My startup Elisp has been on the web for ages, but I think that having a well-titled blog post for individual tweaks will make better Google-food, and will therefore be more useful to the world.

No comments:

Post a Comment