【问题标题】:emacs align function parameters verticallyemacs 垂直对齐函数参数
【发布时间】:2013-05-07 04:06:48
【问题描述】:

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

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

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

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

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

谢谢

【问题讨论】:

    标签: emacs text-alignment


    【解决方案1】:

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

    【讨论】:

    • 没错,这对于align-regexp 来说是一个非常棘手的案例。我可能会改用自定义的align-rules-list 规则(这样您就可以指定多个要对齐的组,这可以简化事情),但这种方法看起来很有趣。我真的需要记住syntax-ppss 存在。 +1
    • @Andreas Röhler,您能否详细解释一下如何使用此功能?我已经把它放在init.el中,重新运行emacs,选择函数并按制表,看没有效果。
    • @klm123 这只是一个在第二个参数处使用点调用时演示算法的示例。扩展了它,所以它也应该在第一个参数中起作用。
    • @Andreas Röhler,好的,我可能离这个很远。不过谢谢。
    【解决方案2】:

    参见 C-hf align-regexp RET,特别是链接的 C-hv align-rules-list RET 它提供了一些最好的对齐文档。

    “要修改的组”是指模式中对齐时将被缩小或扩大的组。您几乎总是希望这个组是纯空白,以避免删除实际内容。

    GROUP 参数——交互式地“修改括号组(如果为负则证明)”——是正则表达式中相关组的编号,从 1 开始计数。

    理由部分有点棘手。如果您提供负数,则使用与正数相同的组,但也会触发 align-rules-list 变量的 'justify' 行为:

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

    【讨论】:

    • 抱歉耽搁了,我遇到了 emacs 正则表达式的问题。我的两个 emacs 版本(21.4 和 22.4)都没有关于 align-rules-list 的文档。因此,如果您有多个括号组,为了对齐,只有“要修改的组”指示的那个是相关的?
    • 对于align-regexp,这是正确的。此外,Emacs 21 和 22 都非常旧。您不妨考虑升级。
    猜你喜欢
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 2018-09-17
    • 2021-12-23
    • 2013-09-15
    • 2012-04-29
    • 2015-09-06
    相关资源
    最近更新 更多