1

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. third(); } 

--

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). } 
4
  • 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. Commented Nov 9, 2018 at 14:22
  • @Stefan I have added a clarification. Commented Nov 9, 2018 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. Commented Nov 10, 2018 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. Commented Nov 10, 2018 at 10:46

1 Answer 1

2

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" '("k&r" (c-offsets-alist (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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.