PascalVKooten
PascalVKooten

Reputation: 21451

Emacs calling align regexp

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?

Upvotes: 3

Views: 1084

Answers (2)

pmr
pmr

Reputation: 59831

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

Upvotes: 4

Oleg Pavliv
Oleg Pavliv

Reputation: 21182

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

Upvotes: 4

Related Questions