如何在 Emacs 中控制左括号后的缩进

发布于 2024-08-05 06:53:09 字数 414 浏览 4 评论 0原文

当我使用 emacs python-mode 时,如果一行的最后一个字符是左括号,则下一行将从上一行的缩进中缩进一步。

call_some_function(
    some_very_long_argument_that_I_want_to_put_on_its_own_line)

我喜欢这样。现在在 ecmascript 模式(我用于 actionscript 3)中,它总是缩进到前一个括号的水平。

call_some_function(
                   this_is_not_really_saving_me_any_horizontal_space);

在这方面如何使 ecmascript-mode 缩进像 python-mode 一样?

When I use emacs python-mode, if the last character of a line is an open parenthesis it indents the next line just one step in from the indentation of the previous line.

call_some_function(
    some_very_long_argument_that_I_want_to_put_on_its_own_line)

I like that. Now in ecmascript-mode (which I am using for actionscript 3), it always indents to the level of the previous parenthesis.

call_some_function(
                   this_is_not_really_saving_me_any_horizontal_space);

How can I make ecmascript-mode indent like python-mode in this respect?

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

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

发布评论

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

评论(3

丶视觉 2024-08-12 06:53:09

由于 ecmascript-mode 基于 cc-mode,因此您可以使用 c-set-offset 它允许您使用首选值自定义任何语法符号的偏移量。

在您的情况下,转到缩进错误级别的点,点击Cc Co(或输入Mx c-set-offset),接受建议的符号(< code>arglist-intro),并为其设置一个新值(例如+,默认偏移量)。

您还可以在 dotemacs 中以编程方式执行此操作,例如:

(add-hook 'ecmascript-mode-hook
          (lambda ()
            (c-set-offset 'arglist-intro '+)
            (c-set-offset 'arglist-close 0)))

Since ecmascript-mode is based on cc-mode, you can use c-set-offset which allows you to customize any syntactic symbol's offset with the preferred value.

In your case, go to the point which is indented in the wrong level, hit C-c C-o (or type M-x c-set-offset), accept the suggested symbol (arglist-intro), and set it a new value (e.g. +, the default offset).

You can also do it programmatically in your dotemacs, for instance, with:

(add-hook 'ecmascript-mode-hook
          (lambda ()
            (c-set-offset 'arglist-intro '+)
            (c-set-offset 'arglist-close 0)))
小清晰的声音 2024-08-12 06:53:09

ecmascript-mode 似乎是基于cc-mode。如果您为 cc-mode 设置缩进样式,
它也适用于 ecmascript-mode。我的 .emacs 中有以下代码。当我使用
ecmascript-mode 它根据需要缩进:

;;{{{ c/c++ indent style variables

(require 'cc-mode)

(defconst my-c-style
  '(
    (c-electric-pound-behavior     . 'alignleft)
    (c-tab-always-indent           . t)
    (c-hanging-braces-alist        . ((block-open)
                                      (brace-list-open)
                                      (substatement-open)
                                      (defun-open before after)
                                      (defun-close before after)
                                      ))
    (c-hanging-colons-alist        . ((member-init-intro before)
                                      (inher-intro)
                                      (case-label)
                                      (access-label      after)
                                      (label             after)
                                      (access-key        after)))
    (c-cleanup-list                . (scope-operator
                                      empty-defun-braces
                                      defun-close-semi))
    (c-offsets-alist               . ((arglist-close        . c-lineup-arglist)
                                      (case-label           . 4)
                                      (statement-case-intro . 4)
                                      (access-label         . -4)
                                      (label                . -)
                                      (substatement-open    . 0)
                                      (block-open           . 0)
                                      (knr-argdecl-intro    . -)))
    )
  "My C++/C Programming Style")


; Customizations for both c-mode and c++-mode
(defun my-c-mode-common-hook ()
  ; set up for my perferred indentation style, but  only do it once
  (c-add-style "My" my-c-style 'set-this-style)
  ; we like auto-newline and hungry-delete
  (c-toggle-auto-hungry-state 1)
  ; keybindings for both C and C++.  We can put these in c-mode-map
  ; because c++-mode-map inherits it
  (define-key c-mode-map "\C-m" 'newline-and-indent)
  ; insert 8 tabs
  (setq tab-width 8)
 )

;;}}}

ecmascript-mode seems to be based on cc-mode. If you set the indentation style for cc-mode,
it will also work for ecmascript-mode. I have the following code in my .emacs. When I use
ecmascript-mode it indents as desired:

;;{{{ c/c++ indent style variables

(require 'cc-mode)

(defconst my-c-style
  '(
    (c-electric-pound-behavior     . 'alignleft)
    (c-tab-always-indent           . t)
    (c-hanging-braces-alist        . ((block-open)
                                      (brace-list-open)
                                      (substatement-open)
                                      (defun-open before after)
                                      (defun-close before after)
                                      ))
    (c-hanging-colons-alist        . ((member-init-intro before)
                                      (inher-intro)
                                      (case-label)
                                      (access-label      after)
                                      (label             after)
                                      (access-key        after)))
    (c-cleanup-list                . (scope-operator
                                      empty-defun-braces
                                      defun-close-semi))
    (c-offsets-alist               . ((arglist-close        . c-lineup-arglist)
                                      (case-label           . 4)
                                      (statement-case-intro . 4)
                                      (access-label         . -4)
                                      (label                . -)
                                      (substatement-open    . 0)
                                      (block-open           . 0)
                                      (knr-argdecl-intro    . -)))
    )
  "My C++/C Programming Style")


; Customizations for both c-mode and c++-mode
(defun my-c-mode-common-hook ()
  ; set up for my perferred indentation style, but  only do it once
  (c-add-style "My" my-c-style 'set-this-style)
  ; we like auto-newline and hungry-delete
  (c-toggle-auto-hungry-state 1)
  ; keybindings for both C and C++.  We can put these in c-mode-map
  ; because c++-mode-map inherits it
  (define-key c-mode-map "\C-m" 'newline-and-indent)
  ; insert 8 tabs
  (setq tab-width 8)
 )

;;}}}
醉殇 2024-08-12 06:53:09

谢谢 Török Gábor,就我而言,我更喜欢设置

(add-hook 'XXX-mode-hook
      (lambda ()
              (c-set-offset 'arglist-cont-nonempty '+)))

我正在寻找这样的东西:

veryLongFunctionName (bar,
酒吧,
bar)

有关更详尽的变量列表:阅读 emacs 文档

Thank you Török Gábor, in my case I prefered to set

(add-hook 'XXX-mode-hook
      (lambda ()
              (c-set-offset 'arglist-cont-nonempty '+)))

I was looking for something like this :

veryLongFunctionName (bar,
bar,
bar)

For a more exhaustive list of variables : read emacs documentation

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