0

I've been trying to setup cc-mode based files to behave in the way I want, but with so many varying opinions and suggested ways of doing things, I've not been able to get things just right.

All I want to be able to do is the following:

void foo() {
   if (something) {
      doSomethingElse();

      while (bar()) {
         zap();
      }
   }
   else {
      orSomeOther();
   }
}
  • Opening braces should be on the same line.
  • When I hit Return after I enter the opening brace, the cursor should move to the next line, and indent.
  • Indentation should be using tabs, each with a width of 3.
  • When I hit Return after entering the opening brace on the line with the while, the cursor should be placed where the z in zap() is.
  • When I do hit Return after opening braces, the closing brace should be added under the blank line where we've just indented and the cursor is now waiting for me to continue typing.
3

This is not necessarily a direct answers to what you should put in your .emacs file but rather a more generic way to quickly figure out the parameters for the style you've chosen.

cc-mode provide several functions to directly guess the right indentation parameters. They are aptly prefixed with c-guess-.

For this case in particular you could use the functions c-guess-no-install followed by c-guess-view. This will open a buffer where cc-mode has guessed the appropriate indentation parameters.

E.g., Copying your code and running those commands give me this:

(c-add-style "STYLE NAME HERE"
             '("linux-tabs-style"
               (c-basic-offset . 3)     ; Guessed value
               (c-offsets-alist
                (block-close . 0)       ; Guessed value
                (defun-block-intro . +) ; Guessed value
                (defun-close . 0)       ; Guessed value
                (else-clause . 0)       ; Guessed value
                (statement . 0)         ; Guessed value
                (statement-block-intro . +) ; Guessed value
                (topmost-intro . 0)         ; Guessed value
                (topmost-intro-cont . c-lineup-topmost-intro-cont))))

Now, you can simply name this style and use c-set-style to use it.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.