0

I'm trying to get Emacs to align operands vertically.

Current behaviour

#include <iostream>

void main()
{
    std::cout << 1 + 2
        + 3;
    return 1 + 2
        + 3;
}

Wanted behaviour

#include <iostream>

void main()
{
    std::cout << 1 + 2
                 + 3;
    return 1 + 2
           + 3;
}

My research so far suggests that I probably want to add something to align-rules-list, but I can't figure out what. c-show-syntactic-information on the continued line returns:

Syntactic analysis: ((statement-cont 76))

Update

I've found out that I probably can solve my problem with c-offsets-alist. The following snippet lines up assignments, strings and cascaded calls correctly, but it doesn't work for streams and return.

(setq c-offsets-alist '((statement-cont . (c-lineup-assignments
                                           c-lineup-math
                                           c-lineup-string-cont
                                           c-lineup-cascaded-calls
                                           +))))
0

You can try to adapt the output of

c-guess-view

It will give you something like:

(c-add-style "STYLE NAME HERE"
         '("gnu"
           (c-basic-offset . 7) ; Guessed value
           (c-offsets-alist
            (defun-block-intro . 4) ; Guessed value
            (defun-close . 0)   ; Guessed value
            (defun-open . 0)    ; Guessed value
            (statement . 0)     ; Guessed value
            (statement-cont . +)    ; Guessed value
            (topmost-intro . 0) ; Guessed value
            (access-label . -)
            ......

Change the guessed value as you wish, and execute the function.

1
  • That works for return because c-basic-offset is set to 7. But that also changes the indentation of all other blocks and doesn't work for cout. – tastytea Apr 27 '20 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.