21

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?

lacker
  • 5,470
  • 6
  • 36
  • 38

3 Answers3

21

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)))
viam0Zah
  • 25,949
  • 8
  • 77
  • 100
  • Thanks! This hook works perfectly and doesn't require me to mess with any other modes. Also I didn't know about C-c C-o, that's handy. – lacker Sep 25 '09 at 16:18
  • how can this be added as a modeline at the end of file? like here http://stackoverflow.com/questions/5382475/emacs-modeline-at-the-end-of-file-in-one-line – alfC Aug 29 '11 at 02:26
3

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

;;}}}
Ralph
  • 5,154
  • 1
  • 21
  • 19
1

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

Meinew
  • 484
  • 8
  • 17
  • Please check this [URL](http://stackoverflow.com/help) it will be useful to lift your content quality up – Willie Cheng May 19 '16 at 09:37
  • I don't really understand what your mean. The URL points to help page but without any precision : what should I check there? What was wrong in my comment? – Meinew May 19 '16 at 10:42
  • I also found that I had to set `arglist-cont-nonempty` to get the correct behavior. – Metamorphic Jan 19 '20 at 05:40