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

21

当我使用Emacs Python模式时,如果一行的最后一个字符是开放括号,它会将下一行缩进到前一行的缩进只有一个步骤。

call_some_function(
    some_very_long_argument_that_I_want_to_put_on_its_own_line)

我喜欢这个功能。现在在 ecmascript-mode 中(我正在用它来编写 ActionScript 3),它总是缩进到前一个括号的层级。

call_some_function(
                   this_is_not_really_saving_me_any_horizontal_space);

我该如何让 ecmascript-mode 在这一点上像 python-mode 一样缩进?

3个回答

21

由于 ecmascript-mode 基于 cc-mode,因此您可以使用 c-set-offset 来自定义任何语法符号的偏移量,以达到所需的值。

对于您的情况,进入缩进级别错误的位置,按下 C-c C-o(或者输入 M-x c-set-offset),接受建议的符号(arglist-intro),并设置一个新值(例如默认偏移量为+)。

您还可以在您的 dotemacs 中以编程方式实现这一点,例如:

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

谢谢!这个钩子完美地工作,而且不需要我去搞其他模式。另外,我之前不知道 C-c C-o 这个快捷键,真是太方便了。 - lacker
这个如何作为一个 modeline 添加到文件末尾?就像这里:http://stackoverflow.com/questions/5382475/emacs-modeline-at-the-end-of-file-in-one-line - alfC

3

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

;;}}}

2
如果你把足够的部分移出来,专注于缩进定制,那就太好了。 - viam0Zah

1
感谢Török Gábor,对于我的情况,我更喜欢设置


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

我正在寻找这样的内容: 非常长的函数名称(bar, bar, bar) 有关变量的更详尽列表,请参阅阅读Emacs文档

请查看此链接,它将有助于提高您的内容质量。 - Willie Cheng
我不太明白你的意思。该URL指向帮助页面,但没有任何明确说明:我应该在那里检查什么?我的评论有什么问题? - Meinew
我还发现我必须设置 arglist-cont-nonempty 才能获得正确的行为。 - Metamorphic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接