Search code examples
c++emacsindentation

Emacs indentation with sequential parenthesis in C++


Emacs auto indent on sequential parenthesis doesn't work fine:

int main() {
    something(int i)(
        "test", "something"
                    );
}

How to fix this indentation to be like normal indentation:

int main() {
  something(int i)(
      "test", "something"
  );
}

major mode info:

  • major-mode is a variable defined in ‘C source code’.
  • Its value is ‘c++-mode’

SOLUTION:

Thanks, @pickle rick, and @0x5453,

(c-add-style "cc-style"
         '("linux"  ;; it can be google or k&r or other c-style.
           (c-basic-offset . 2)
           (c-offsets-alist
        (arglist-close . c-lineup-close-paren))))
(add-hook 'c++-mode-hook
      (lambda()
        (c-set-style "cc-style")))

Solution

  • cc-mode is able to intelligently guess the settings you want for c-offsets-alist based on the indentation in the buffer. To do this, align the code in the buffer as you want it, and evaluate M-xc-guess.

    Indent the buffer, and assuming it looks OK, you can see the guessed settings with M-xc-guess-view. Then, you could copy the entire style into your init somewhere, or cherry pick the relevant settings.

    Additionally, you can get an idea of which rules apply to a given line by running M-xc-show-syntactic-information with the point positioned there. Note, however, that this won't give all the relevant info since other rules/styles affect each others, but will tell identify arglist-close as the relevant rule in your example.