Right now the standard emacs indentation works like the following:
switch (cond) {
case 0: {
command;
}
break;
}
I want the break; to line up with case.
Also, is there a list of the c-set-offset commands somewhere?
In a switch statement, the break statement causes the program to execute the next statement outside the switch statement. Without a break statement, every statement from the matched case label to the end of the switch statement, including the default clause, is executed.
You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.
Move to the end of the block you want to indent and set the mark by pressing C-SPC . Move the cursor to the first line of the block, and indent this line however far you want all the lines in the block to be indented. Press M-C-\ . The text in the marked block will now be reformatted.
A break statement is optional. If we omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them.
The biggest help (I've found) in customizing indentation is figuring out what cc-mode uses to indent the current line. That's what C-c C-o aka M-x c-set-offset can do - it'll allow you to customize the offset for a syntactic element, and it shows you what element was used for the current line!
Here's how you can customize it. Move your cursor to the break;
line.
C-c C-o RET 0 RET
At which point, your code will be indented like:
switch (cond) {
case 0: {
command;
}
break;
}
For documentation on the offsets, check out the docstring for the variable 'c-offsets-alist
C-h v c-offsets-alist RET
Similarly, you can add this to your .emacs:
(setq c-offsets-alist '((statement-case-intro . 0)))
The documentation for customizing indention is here and is worth reading. There are tons of ways to do it, so reading the manual is worth the time (if you want non-default indentation). And here's a pointer to all the syntactic symbols used in cc-mode.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With