Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs indentation of break after switch statement

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?

like image 975
John Bellone Avatar asked Jun 05 '09 18:06

John Bellone


People also ask

Does Break work for switch statement?

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.

Do you need break after switch statement?

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.

How do I indent in Emacs?

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.

Why break is optional in switch statement?

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.


1 Answers

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.

like image 120
Trey Jackson Avatar answered Nov 07 '22 09:11

Trey Jackson