3

I would like to have the following behavior:

int main(){ 

to result in:

int main(){ 
    | 
}  

Where pipe is the cursor, and I DON'T have to press RETURN key after '{'.
I have already achieved the following behavior:

int main(){  

which results in:

int main()  
{ 
    | 
}  

Using c-toggle-auto-indent and electric-pair-mode.

So, how do I make it the way I want to? i.e to:

int main(){  
    |  
}  

I have been looking around and found this page:

First you need to find the syntactic context of the brace—type a RET before the brace to get it on a line of its own, then type C-c C-s. That will tell you something like:

((substatement-open 1061))

So here you need to put the entry (substatement-open . (after)) into c-hanging-braces-alist

This is what I have tried:

  1. Found the syntactic context to be: substatement-open
  2. Putting (setq c-hanging-braces-alist '(substatement-open . (after))) in init.el.

But this isn't working.

I really have no idea how to do this. Can someone help me?

4
  • 1
    By using setq, you are clobbering any previous value of the c-hanging-braces-alist variable. You should try customizing that variable: do C-h v c-hanging-braces-alist and then click on the customize link. That will allow you to do selective surgery on it. But I don't know what (key value) pair(s) you should add/modify to get the effect you want. In my case, substatement-open already has the value after. A closer reading of the CC mode manual is indicated.
    – NickD
    Commented Jun 11, 2021 at 14:30
  • hey thanks a lot but I tried setq after I did what you just said (using customize-variables -> c-hanging-braces-alist -> selecting modifiable setting (*) -> then checking 'after' ONLY beside the option 'substatement-open' Commented Jun 11, 2021 at 16:23
  • I tried it so many times I can now recite the steps lol. Since you said yours is already set to 'after', does this mean you are enjoying what I am seeking? Commented Jun 11, 2021 at 16:24
  • After a bit of experimentation, I was able to get the result you want. See my answer below.
    – NickD
    Commented Jun 20, 2021 at 12:41

1 Answer 1

4

First, customize c-hanging-braces-alist to use the style settings: I think that's the default, but since you've been mucking around with customizing it, go back and make sure it is set to the default.

Then try the different built-in styles: Type C-c . and choose a style (there is TAB completion). Enter a brace and see if it does what you want: if it does not, go back and try another style. I wasn't particularly careful so this may be wrong, but the only style I could find that did what you want was the awk style. Whether that satisfies all your other (unstated) requirements is unknown, but even if it does not, you might be able to crib the style and create your own that does exactly what you want. See the doc string for the variable c-style-alist and the function c-add-style.

For completeness, here's a bit of code to add to your init.el to do what you want (assuming that the awk style is good enough for your other purposes:

(defun ndk/auto-newline ()
  "Enable auto-newline."
  (c-toggle-auto-newline 1))

(defun ndk/c-set-style ()
  "Set the default C style to \"awk\"."
  (c-set-style "awk"))

(defun ndk/electric-pair-mode ()
  "Enable electric-pair-mode."
  (electric-pair-mode 1))


(add-hook 'c-mode-hook #'ndk/auto-newline)
(add-hook 'c-mode-hook #'ndk/c-set-style)
(add-hook 'c-mode-hook #'ndk/electric-pair-mode)

You could use lambdas, but I prefer to use named functions in hooks: it keeps things neater.

EDIT: I think the operative detail for what you want is the setting of defun-open to after in the awk style, since that is the syntactic class of a brace in the context you describe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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