为Emacs添加Astyle支持

楔子

我经常去BCCN回答问题 但里面提问多是新手 代码风格比较差 每次给他们改代码都得现改代码风格

当然 我用的是Emacs 改代码排版不算太麻烦 但时间长了也烦啊

Astyle的Emacs支持

今天试了一下Artistic style(简称astyle) 发现挺好用的 于是做了一个Emacs的支持 其实就是一个函数 上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
;; --indent=force-tab=# OR -T#
;; Indent using tab characters, assuming that each
;; indent is # spaces long. Force tabs to be used in areas
;; AStyle would prefer to use spaces.
;; --break-blocks OR -f
;; Insert empty lines around unrelated blocks, labels, classes, ...
;; --pad-oper OR -p
;; Insert space padding around operators.
;; --pad-header OR -H
;; Insert space padding after paren headers (e.g. 'if', 'for'...).
;; --unpad-paren OR -U
;; Remove unnecessary space padding around parenthesis. This
;; can be used in combination with the 'pad' options above.
;; --break-elseifs OR -e
;; Break 'else if()' statements into two different lines.
(defvar astyle-command "astyle --style=bsd -T8 -f -p -H -U -e")
(defun astyle (start end)
"Run astyle on region or buffer"
(interactive (if mark-active
(list (region-beginning) (region-end))
(list (point-min) (point-max))
))
(save-restriction
(shell-command-on-region start end
astyle-command
(current-buffer) t
(get-buffer-create "*Astyle Errors*") t)))

Update 2:

现在单独放到GitHub上了 地址是

https://github.com/zklhp/some-utils