Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make cases in switch statement to be indented in Emacs

How to make Emacs to indent cases like this

switch ($foo) {
    case "foo":
        $foo .= " bar";
        break
    case "bar":
        $foo .= " baz";
        break
    default:
        $foo .= " undefined";
}

instead of

switch ($foo) {
case "foo":
    $foo .= " bar";
    break
case "bar":
    $foo .= " baz";
    break
default:
    $foo .= " undefined";
}
like image 940
jcubic Avatar asked Jan 24 '12 10:01

jcubic


People also ask

How do I indent code 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.

What is the syntax for switch case statement?

A general syntax of how switch-case is implemented in a 'C' program is as follows: switch( expression ) { case value-1: Block-1; Break; case value-2: Block-2; Break; case value-n: Block-n; Break; default: Block-1; Break; } Statement-x; The expression can be integer expression or a character expression.


1 Answers

You need to add something like this to your .emacs (either as a general setting or for the specific programming modes you care about):

;; set this in all c-based programming modes
(add-hook 'c-mode-common-hook
          (lambda ()
             (c-set-offset 'case-label '+)))

to add this to another mode, use the same pattern above with the relevant mode name substituted in for the hook, e.g.: <mode-name>-hook.

like image 180
jtahlborn Avatar answered Oct 21 '22 06:10

jtahlborn