3

I am trying to pass arguments to the align-regexp function in Emacs (Lisp):

(defun align-on-comment-char ()
  (interactive)
  (align-regexp (region-beginning) (region-end) "#")
)

I would actually like to do this for all my modes specifically, where for each mode I want to bind an "align to comment character (; for emacs lisp, % for Latex, # for R)".

What am I missing?

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160

2 Answers2

4

comment-start and comment-end are the usual variables to get the comment-string for the current mode.

You will also need to append some magic to get the matching right when calling align-regexp. Like Oleg, I had to figure that out the hard way by looking at the source. The error message here is not really descriptive and I really consider this worthy of a bug-report or at least a doc fix.

(defun align-comment (beg end)
  (interactive "r")
  (align-regexp beg end (concat "\\(\\s-*\\)" comment-start))
)
pmr
  • 58,701
  • 10
  • 113
  • 156
  • Please elaborate. My goal is to align a region (selected text) on the comment character for that mode. I'm fine with having functions for each mode specifically. Let's try first this one with the # character? – PascalVKooten Oct 14 '12 at 20:02
  • @Dualinity OK, this was really more tricky than I expected. I have posted a full working example. – pmr Oct 14 '12 at 20:29
  • Excellent! Props to you. Kind of gave me hope, since I really tried my best as a Emacs Lisp novice. – PascalVKooten Oct 14 '12 at 20:33
  • I am curious though, the arguments beg and end are coming from the (interactive "r") statement? – PascalVKooten Oct 14 '12 at 20:35
  • @Dualinity Yes, it's a feature of interactive. See http://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Interactive.html and especially the next page http://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes.html#Interactive-Codes which contains all codes. – pmr Oct 14 '12 at 20:46
  • these manuals never feel complete and are somehow never doing it for me. do you know another way to learn more about (let), (concat) and that kind of things? – PascalVKooten Oct 14 '12 at 21:20
  • oh and btw, do you use a function like this one yourself? why (not)? – PascalVKooten Oct 14 '12 at 21:43
  • 1
    @Dualinity The function looks sensible. If you use it, have it. No dogma. I usually just define stuff like that on the fly when I need it. For the manual: The elisp manual itself is really not good (the functionality documentation (C-h f)) usually is. Pick up a lisp book for a more general introduction or start with some of Xah Lee's tutorials. http://ergoemacs.org/emacs/elisp.html – pmr Oct 14 '12 at 23:27
  • 2
    @Dualinity The elisp manual is a reference book, not a tutorial. It's great for details when you already have the general idea. For novices, http://www.gnu.org/software/emacs/emacs-lisp-intro/ is a great introduction, and is available from the Emacs info system. Then look here: http://stackoverflow.com/questions/41522/tips-for-learning-elisp – Tyler Oct 15 '12 at 00:44
4

You should change your code a little bit.

For example like this:

(defun align-on-comment-char (beg end)
  (interactive "r")
  (align-regexp beg end "\\(\\s-*\\)#")
)

A magic string "\\(\\s-*\\)" is taken from the sources of align-regexp.

If you want to have a single function for all modes then use comment-start variable as @pmr pointed out.

(align-regexp beg end (concat "\\(\\s-*\\)" comment-start))
Oleg Pavliv
  • 20,462
  • 7
  • 59
  • 75
  • I saw that about the same string about the time you posted. This really needs better documentation. – pmr Oct 14 '12 at 20:23
  • Well, it's not a "magic string"; it's simply the specified regexp group to modify in the alignment process. It just happens that a zero-or-more-whitespace group is what you'll want to use most of the time. I do agree that the alignment system could do with some documentation improvements. – phils Oct 14 '12 at 22:00