I'm trying to achieve the following:
void foo( int one,
int const & two,
Bar three)
to
void foo( int one,
inst const & two,
Bar three)
Is this possible to do using the align-regex
function (with our without prefix)?
More generally, what does the grouping in the regex signify (is it the part that's considered the 'column')? And what is the 'parentheses group to modify (justify if negative)'?
Thanks
See C-hf align-regexp
RET and, in particular, the linked C-hv align-rules-list
RET which provides some of the best documentation for alignment.
The "group to modify" means the group in the pattern which will be shrunk or expanded when aligning. You almost always want this group to be purely whitespace, in order to avoid deleting actual content.
The GROUP argument -- interactively "Parentheses group to modify (justify if negative)" -- is the number of the group in question in the regexp, counting from 1.
The justification part is a bit trickier. If you provide a negative number, then the same group is used as if the number were positive, but the 'justify' behaviour of the align-rules-list
variable is also triggered:
`justify'
It is possible with `regexp' and `group' to identify a
character group that contains more than just whitespace
characters. By default, any non-whitespace characters in
that group will also be deleted while aligning the
alignment character. However, if the `justify' attribute
is set to a non-nil value, only the initial whitespace
characters within that group will be deleted. This has
the effect of right-justifying the characters that remain,
and can be used for outdenting or just plain old right-
justification.
IMO that's a case where the use of regexps grows being complex. A function which makes use of syntax-ppss is easier:
(defun my-arguments-indent()
"When called from inside an arguments list, indent it. "
(interactive "*")
(save-excursion
(let* ((pps (syntax-ppss))
(orig (point))
indent)
(while (and (nth 1 pps)(not (eobp)))
(setq indent (save-excursion
(when (nth 1 pps)
(goto-char (nth 1 pps))
(forward-char 1)
(skip-chars-forward " \t")
(current-column))))
(when (and (< orig (line-beginning-position)) indent)
(beginning-of-line)
(fixup-whitespace)
(indent-to indent))
(forward-line 1)
(back-to-indentation)
(setq pps (syntax-ppss))))))
来源:https://stackoverflow.com/questions/16411045/emacs-align-function-parameters-vertically