tags:

views:

116

answers:

3

I'd like to put something like this in my .emacs:

(local-set-key (kbd "C-c a =") 
  (lambda () (interactive) 
    (align-regexp (region-beginning) (region-end) "=")))

But whenever I run it, I get an error "Wrong type argument: numberp, nil".

What does this error mean and how do I get the effect I'm looking for?

A: 

I picked apart the source of align-regexp (install emacs23-el on debian) and came up with this:

(local-set-key (kbd "C-c a =") 
  (lambda () (interactive) 
    (align-region (region-beginning)
                  (region-end)
                  'entire
                  (list (list nil
                              (cons 'regexp "\\(\\s-*\\)=")
                              (cons 'group 1)
                              (cons 'bogus nil)
                              (cons 'spacing 1)))
                  nil
                  nil)))
Jack Kelly
But surely it is possible to do this assignment without digging out the innards of the align command?
qrest
I would've thought so, but I don't understand `align.el` all that well.
Jack Kelly
+1  A: 

"thunk" from #emacs solved it:

(local-set-key (kbd "C-c a =") 
  (lambda () (interactive) 
    (align-regexp (region-beginning) (region-end) "\\(\\s-*\\)=" 1 1 nil)))

Someone care to explain the strange prefix to the "="?

qrest
Note that the last nil is optional, and the 1 1 are not. The documentation for align.el needs a tidy up.
slomojo
+2  A: 

Here you are my dear fellow.

(defun align-to-equals (begin end)
  "Align region to equal signs"
   (interactive "r")
   (align-regexp begin end "\\(\\s-*\\)=" 1 1 ))

The (\s-*) prefix is used internally by align-regexp

From the align.el

(list (concat "\\(\\s-*\\)"

John Wiegley just neglected to document it, and I guess most people just use align-regexp interactively, or just record and save a macro!

slomojo
Ah beaten to the punch...
slomojo
But better explanation!
qrest
I'll stil give you a +1 though :) ... that \s-* prefix is very odd.
slomojo
As usual, I beg you all to use "rx" so that those who read your code will not go crazy or blind: (rx (group (zero-or-more (syntax whitespace))) "=")is exactly equivalent to "\\(\\s-*\\)="
offby1