January 17, 2011

Erlang Alignment in Emacs

Just realized that the default align function in Emacs is quite capable. To indent, for example, a list of tuples with data, add the following alignment rule to your .emacs-file:

(add-hook 'align-load-hook
          (lambda ()
            (add-to-list 'align-rules-list
                           (regexp . ",\\(\\s-+\\)")
                           (repeat . t)
                           (modes quote (erlang-mode))))))

This will add a new alignment rule that aligns comma separated regions with whitespace. It will align all occurrences over all lines in the region, and only be enabled in the Erlang mode.

The alignment will transform this:

[{a, 19, "test", <<0,0>>},
 {abc, 2, "a", <<12,19,28,11>>}].

Into this:

[{a,   19, "test", <<0,0>>},
 {abc, 2,  "a",    <<12,19,28,11>>}].

I set it to Ctrl+E in my .emacs-file:

(global-set-key (read-kbd-macro "C-E") 'align)

I can also recommend the align-repeat macro from Align Commands on Emacs Wiki. I set it to Ctrl+Shift+E.

Erlang programming Emacs

Previous post
Synchronized, Reliable Message Passing in Erlang To do proper, synchronized message passing you need three things: monitoring, timeout and a unique tag in the messages. The basic case is sending a...
Next post
All Erlang Modules in the Code Path Here’s a little thing of beauty to get all Erlang modules in your code path, should you ever need...