I am working in Haskell and frequently come across code similar to the following:
func i j | i == j = i
| otherwise = j
I want to align on the ‘=’ character using align-regexp but don’t have the elisp knowhow. I have tried just doing ” = ” without the quotes, but this inserts an unwanted space character before each ‘=’. I have found a proposed solution here but I can’t seem to get that to do anything at all.
Please help me write a function or hard-coded macro that will allow me to set a keybinding for this.
C-u M-x
align-regexpRET\(\s-*\) =RET1RET0RETn(n.b. there’s a space after the ‘=’, but it’s not very obvious.)
Which is to say…
Use a prefix argument to tell
align-regexpto ask you for more parameters than it does by default.See C-h f
align-regexpand C-h valign-rules-listfor details, but in short:\(\s-*\)is the default ‘group’ for deletion/expansion. We append our pattern to the end of that: ‘ = ‘. (Note that\s-is Emacs regexp syntax for whitespace.)1simply refers to parenthesised group 1 (as above). This is the default.0for the spacing to use between the two parts of the line. By default this is 1, and is why you were ending up with an additional space.nto not align any subsequent pattern matches after the first in each line.edit: Actually, the Q&A you linked to is near identical, and works fine for me on Emacs 23.2.1, so this is a duplicate, but to continue and answer the key-binding aspect:
You can bind that (or any) sequence via keyboard macros. Here’s the end result, which you can probably just add to your init file, although I recommend you go through the process yourself. Use whatever you like in place of
C-c afor the key.C-c (letter)and F5-F9 are reserved for end-users to bind as they please, so one of those will be safe from being clobbered by a mode’s keymap.I did that by:
align-regexpas above (being careful to type everything verbatim, and not use minibuffer history or yanking).1align-single-equalsRET to give the macro a nameinsert-kbd-macroRETalign-single-equalsRET to get the lisp.(lambda)expression with(global-set-key)to bind it. (Although you could also use the(fset 'align-single-equals ...)code as provided, and then bind the key to that symbol.1 If you make a mistake when recording a complicated macro, don’t fret — Emacs provides a really good macro editor which you can use to fix any mistakes after you finish recording (just type C-x C-k e), so you don’t need to be perfect.
edit 2: May as well add an example of a function, as per comments.