开发者

In Emacs, how can we apply the new indent style for our existing code?

Being read about indent style, now I must change my current indent style from Banner Style, to Allman Style. I've changed the indent style using c-set-style and choosing bsd. But, now how do I apply the new indent style?

That is, can emacs reformat my code, and apply this new coding sty开发者_如何学Pythone? How can I configure emacs to use this sytle to be my java default style.

Thank you.


You can use indent-region to apply your indentation settings. If you want to re-indent the whole buffer, mark-whole-buffer (C-x h) followed by indent-region (C-M-\) should do it.

EDIT:

If you want to automatically move the braces onto their own lines, as in the style you cite, you'll have to do some regexp replacements as well. The following might be a start:

(query-replace-regexp "^\\([^{}\n]+[^{} \t\n]+[^{}\n]*\\)\\([{}]\\)$" "\\1\n\\2")     

This will take any { or } character that is the last character on a line and put it on its own line. It will ignore any {} characters that are the first character on their lines. You may need to follow this with:

(query-replace-regexp "^\\(\\s *[{}]\\)\\(.+\\)$" "\\1\n\\2") 

This will take any {} characters that are first on their line and are followed by other characters, and insert a newline between the brace and the other characters.

Following this, my original suggestion of mark-whole-buffer and indent-region should correct the indentation.

EDIT 2: fixed the first regexp to account for leading whitespace

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜