emacs垂直对齐函数参数

时间:2013-05-07 04:06:49

标签: emacs text-alignment

我正在努力实现以下目标:

void foo( int one,
int const & two,
Bar three)

void foo( int          one,
          inst const & two,
          Bar          three)

这可以使用align-regex函数(使用我们的无前缀)吗?

更一般地说,正则表达式中的分组表示什么(它是被认为是“列”的部分)?什么是'修改的括号组(证明是否为负)?

由于

2 个答案:

答案 0 :(得分:2)

IMO认为使用正则表达式变得复杂。使用syntax-ppss的函数更容易:

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

答案 1 :(得分:1)

参见 Ch f align-regexp RET ,特别是链接的 Ch v align-rules-list RET ,它提供了一些最佳的对齐文档。

“要修改的组”表示模式中的组在对齐时将缩小或展开。你几乎总是希望这个组是纯空格,以避免删除实际内容。

GROUP参数 - 交互式地“要修改的括号组(如果为负,则证明是正确的)” - 是正则表达式中有问题的组的编号,从1开始计算。

理由部分有点棘手。如果您提供负数,则使用相同的组,就好像数字是正数一样,但align-rules-list变量的'对齐'行为也会被触发:

`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.