Emacs Align Functions

Tim Visher has a new VimGolf in Emacs up. This time it’s a simple alignment problem using align-regexp, a function that we’ve talked about before. In this case he just used the simple align-regexp case and was done in about 6 keystrokes, much less than the 18 that the best Vim solution took.

It turns out the the align function, of which align-regexp is a special case, is very flexible. It will try to align the region or current group of text in a context dependent way. The action of align is specified by a set of rules that specify context (such as major mode), a regular expression, and some other attributes. The list of rules is in the variable align-rules-list. The documentation for that variable explains the various options and gives its current value. You can also pass in your own list of rules. The align-regexp function basically just prompts you for an ad hoc rule.

Unfortunately, the documentation isn’t very good and the rules can be quite complex. Visher mentions this page from the EmacsWiki that gives some examples but it is by no means a complete explanation. This is a very powerful function and I wish the documentation on it was better. Unfortunately it isn’t even mentioned in the Emacs manual.

This entry was posted in General and tagged . Bookmark the permalink.

One Response to Emacs Align Functions

  1. Phil says:

    The option to use a custom function in place of a regexp is especially poorly documented, and you have to either dig into the code, or look through the align-rules-list variable for one of the few examples to simply find out what arguments the function needs to take.

    Here’s the most useful of those examples from the default list:


    (open-comment
    (regexp . ,(function
    (lambda (end reverse)
    (funcall (if reverse 're-search-backward
    're-search-forward)
    (concat "[^ \t\n\\\\]"
    (regexp-quote comment-start)
    "\\(.+\\)$") end t))))
    (modes . align-open-comment-modes))

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>