Search code examples
emacsc-mode

Indenting deeply nested function calls


I would like emacs to indent my c/c++ code like this:

auto LoopMatcher = forStmt(hasLoopInit(declStmt(hasSingleDecl(varDecl(
    hasInitializer(integerLiteral(equals(0)))))))).bind("forLoop");

(code taken from clang's AST matcher tutorial).

In other words, I want emacs to indent by the default offset after one or more open parentheses.


Solution

  • Here you have a solution for that:

    (defun custom-indent (langelem)
      (save-excursion
        (goto-char (cdr langelem))
        (vector (+ (current-column) c-basic-offset))))
    
    (c-add-style "custom" '((c-offsets-alist . ((arglist-intro . custom-indent)))))
    
    (c-set-style "custom")