我怎样才能让emacs cperl-mode 将所有语句延续仅缩进一级?

发布于 2024-09-15 21:55:48 字数 515 浏览 4 评论 0原文

在 emacs 的 cperl 模式中,从前一行继续语句的行缩进一级:

my $var
  = (1+1)
  * (2+2)
  / (3+3);

但是,如果语句由于位于块内而不以零缩进开始,那么如果将语句中断到第三行,你会得到另一个级别的缩进,等等:

sub break_my_indentation {
  my $var
    = (1+1)
      * (2+2)
        / (3+3);
  return "Indentation is broken.";
}

有没有办法解决这个问题,以便语句在块内部的缩进方式与块外部的缩进方式相同?我希望第二个例子看起来像这样:

sub fix_my_indentation {
  my $var
    = (1+1)
    * (2+2)
    / (3+3);
  return "Indentation is fixed.";
}

In emacs' cperl-mode, lines that continute a statement from a previous line are indented by one level:

my $var
  = (1+1)
  * (2+2)
  / (3+3);

However, if the statement does not begin at zero indentation because it is inside a block, then if you break your statement onto a third line, you get another level of indentation, and so on:

sub break_my_indentation {
  my $var
    = (1+1)
      * (2+2)
        / (3+3);
  return "Indentation is broken.";
}

Is there any way to fix this so that statements are indented the same way inside blocks as they are outside? I would like the second example to look like this:

sub fix_my_indentation {
  my $var
    = (1+1)
    * (2+2)
    / (3+3);
  return "Indentation is fixed.";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网白 2024-09-22 21:55:48

我确认您所描述的行为存在,但是,从阅读源代码并在调试器下运行来看,并不明显有一个自定义可以让您获得您(和我)想要的行为。

因此我认为这是 cperl-mode 中的一个错误。

修复方法是:

=== modified file 'lisp/progmodes/cperl-mode.el'
--- lisp/progmodes/cperl-mode.el    2012-12-01 05:09:12 +0000
+++ lisp/progmodes/cperl-mode.el    2012-12-26 16:29:19 +0000
@@ -3120,7 +3121,9 @@ and closing parentheses and brackets."
     ((eq 'continuation (elt i 0))
      ;; [continuation statement-start char-after is-block is-brace]
      (goto-char (elt i 1))     ; statement-start
-     (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
+     (+ (if (or (memq (elt i 2) (append "}])" nil)) ; char-after
+                     (eq 'continuation ; do not repeat cperl-close-paren-offset
+                         (elt (cperl-sniff-for-indent parse-data) 0)))
         0          ; Closing parenth
           cperl-continued-statement-offset)
         (if (or (elt i 3)      ; is-block

请测试此补丁;如果您对此感到满意,我将添加自定义来启用它。

I confirm that the behavior you describe is present, but, from reading the sources and running under the debugger, it is not obvious that there is a customization which gets you the behavior you (and I) want.

Therefore I think this is a bug in cperl-mode.

The fix is:

=== modified file 'lisp/progmodes/cperl-mode.el'
--- lisp/progmodes/cperl-mode.el    2012-12-01 05:09:12 +0000
+++ lisp/progmodes/cperl-mode.el    2012-12-26 16:29:19 +0000
@@ -3120,7 +3121,9 @@ and closing parentheses and brackets."
     ((eq 'continuation (elt i 0))
      ;; [continuation statement-start char-after is-block is-brace]
      (goto-char (elt i 1))     ; statement-start
-     (+ (if (memq (elt i 2) (append "}])" nil)) ; char-after
+     (+ (if (or (memq (elt i 2) (append "}])" nil)) ; char-after
+                     (eq 'continuation ; do not repeat cperl-close-paren-offset
+                         (elt (cperl-sniff-for-indent parse-data) 0)))
         0          ; Closing parenth
           cperl-continued-statement-offset)
         (if (or (elt i 3)      ; is-block

Please test this patch; if you are happy with it, I will add a customization to enable it.

伊面 2024-09-22 21:55:48

cperl-mode 默认情况下没有这个问题。默认情况下,它的缩进如下:

my $var
  = (1+1)
  * (2+2)
  / (3+3);

您进行了自定义,以防止 cperl-mode 正确缩进。请参阅 cperl-indent-rules-alist 变量以了解缩进的配置。

cperl-mode don't have this problem by default. By default, it indents like this :

my $var
  = (1+1)
  * (2+2)
  / (3+3);

You have a customization that prevents cperl-mode to indent correctly. See cperl-indent-rules-alist variable for configuration of the indent.

弥枳 2024-09-22 21:55:48

正如 @jerome-radix 所建议的,默认情况下 cperl-mode 不会出现您遇到的问题。这个问题当然可能出现在cperl-indent-rules-alist中,但是还有很多其他地方需要查找。

您使用的是哪个版本的模式? Ch v cperl-version RET

在调试此类问题时,您需要从一些基本的事情开始,看看是否可以重现错误。首先让大家知道您正在运行什么 Mx emacs-version。然后,通过运行 emacs -q 来消除您自己的自定义设置,以避免在启动时加载您的初始化文件:

--no-init-file, -q          load neither ~/.emacs nor default.el

如果您在关闭自己的初始化文件的情况下仍然可以重现问题,那么请更进一步,使用 emacs -Q 在系统级别将其关闭:

--quick, -Q                 equivalent to:
                              -q --no-site-file --no-site-lisp --no-splash

As @jerome-radix suggests, the problem you're experiencing doesn't happen with cperl-mode by default. The problem could certainly by in cperl-indent-rules-alist, but there are a number of other places to look.

What version of the mode are you using? C-h v cperl-version RET

When debugging this kind of question you need to start with a couple of basic things to see if you can reproduce the error. First let everyone know what M-x emacs-version you are running. Then eliminate your own customizations from the equation by running emacs -q to avoid loading your init file at startup:

--no-init-file, -q          load neither ~/.emacs nor default.el

If you can still reproduce the problem with your own init file turned off, then go a step further and turn it off at the system level with emacs -Q:

--quick, -Q                 equivalent to:
                              -q --no-site-file --no-site-lisp --no-splash
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文