qrest
qrest

Reputation: 2417

Emacs hotkey to align equal signs

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?

Upvotes: 12

Views: 1957

Answers (3)

ocodo
ocodo

Reputation: 30269

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!

Upvotes: 16

qrest
qrest

Reputation: 2417

"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 "="?

Upvotes: 2

Jack Kelly
Jack Kelly

Reputation: 18667

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)))

Upvotes: 0

Related Questions