emacs align function parameters vertically

亡梦爱人 提交于 2019-12-10 10:48:50

问题


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


回答1:


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.



回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!