I would like to customize the "k&r" indentation style, so that continuation lines are indented 8 spaces, like this:

if (first
        && second) { // <- 8 spaces.


CLARIFICATION: Basically, I would like to distinguish easily between a block header and nested statements, to avoid confusion; for example:

if (first
    && second) { // <- Block header (4 spaces).
    third();     // <- Nested statement (same 4 spaces).

I have seen some code indented like the following, to address the above issue:

if (first
    && second) {  // <- Block header (4 spaces).
        third();  // <- Nested statement (8 spaces).

But, to me, the above indentation looks confusing. I would rather have the opposite indentation:

if (first
        && second) { // <- Block header (8 spaces).
    third();         // <- Nested statement (4 spaces).

Such a differentiation would suffice. Function arguments inside a block header should be distinguishable, too, but - in that case - the actual amount of indentation would not matter:

if (first 
        && second        // <- Block header (8 spaces).
        && third(        // <- Block header (8 spaces).
              fourth)) { // <- Function argument (any indentation).
    fifth();             // <- Nested statement (4 spaces).
  • Could you clarify what you mean by "continuation line"? E.g. why do you consider && ... a continuation line but not }? After all, you could write the whole thing on a single line. – Stefan Nov 9 '18 at 14:22
  • @Stefan I have added a clarification. – Elena Nov 9 '18 at 19:21
  • Thanks. BTW, I believe this is one of the reasons why the GNU Coding Standard recommends breaking lines before infix operators rather than after them: even if the indentation of && second is the same as that of third(); the leading && is a tell-tale sign that this is not the beginning of a block. – Stefan Nov 10 '18 at 1:30
  • @Stefan Yup, that is what I do already. I wonder why I wrote my example code differently... I am editing my question to reflect that. Thanks. – Elena Nov 10 '18 at 10:46

You'll want to modify c-offsets-alist for the "K&R" style by creating a new style and using that. Something like the following should work.

(c-add-style "my/k&r"
       (statement-cont . 3))))
(setq c-default-style "my/k&r")

This creates a new cc-mode style called "my/k&r" based on "k&r", sets the syntactic element statement-cont (A continuation of a C (or like) statement.) to a relative offset of 3. (The default value of c-basic-offset is 5 in the "k&r" style.) Then, we set the default style to the style we just created.

Note that there are multiple syntactic elements that control "continuation" lines. I picked statement-cont for my example as it's the most generic. Please see the documentation in the manual for c-offsets-alist, and within Emacs via Ctrl+h v c-offsets-alist Return. The EmacsWiki page on Indenting C also has some good examples of creating/modifying styles.

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.