-
Notifications
You must be signed in to change notification settings - Fork 0
/
chah-c.el
147 lines (141 loc) · 4.82 KB
/
chah-c.el
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
;;-----------------------------------------------------------------------
;; Preferences for C and C++ code
;;-----------------------------------------------------------------------
;;;###autoload
(defun chah-c-style ()
(setq comment-column 40)
(make-local-variable 'indent-tabs-mode)
(setq indent-tabs-mode t) ; Buffer-local
(make-local-variable 'comment-line-start)
(setq comment-line-start "/*--- ")
(make-local-variable 'comment-line-start-skip)
(setq comment-line-start-skip "/\\*-* *")
; Some syntactic elements are unknown to 19.34
(if (assq 'innamespace c-offsets-alist)
(c-set-offset 'innamespace '0))
(if (assq 'inextern-lang c-offsets-alist)
(c-set-offset 'inextern-lang '0))
;; Some projects have different requirements
(let ((bfn (or (buffer-file-name) ""))) ; nil for some synthetic buffers
(cond ((or (string-match "/micropython" bfn)
(string-match "/mbed" bfn)
(string-match "/mcurses" bfn)
(string-match "/incurses" bfn)
(string-match "/p-csl4" bfn)
(string-match "/ulc4" bfn)
(string-match "/kk1rt" bfn)
(string-match "/csdn" bfn)
(string-match "/p-vxpdf" bfn)
(string-match "/d-xsl8" bfn)
(string-match "/urtc" bfn)
(string-match "/xsl8" bfn))
(setq c-basic-offset 4)
(setq indent-tabs-mode nil))
((or (string-match "/EPICS/" bfn))
(setq c-basic-offset 4)
(setq indent-tabs-mode nil)
(c-set-offset 'arglist-intro 8)
(c-set-offset 'arglist-cont-nonempty 8)
(c-set-offset 'arglist-cont 0))
((or (string-match "/atto" bfn)
(string-match "/f2c/" bfn))
(setq c-basic-offset 8)
(setq indent-tabs-mode t))
((or (string-match "/p-" bfn)
(string-match "/r-" bfn)
(string-match "/dtacq/LOWLATENCY/" bfn))
(setq c-basic-offset 2))
((or (string-match "/das2.0/" bfn))
(setq indent-tabs-mode t)
(setq tab-width 4)
(setq c-basic-offset 4)
;; Leave Crystal's tabbing in DAS_HMI & my old in Lib/App/Util
(when (or (string-match "/das2.0/DAS_Application/" bfn)
(string-match "/das2.0/HMI/" bfn))
(add-clang-format-save-hook)))
((or (string-match "/business-logic/" bfn)
(string-match "/bl2/" bfn)
(string-match "/cpp-doodles/" bfn)
(string-match "/pmac-tests/" bfn))
(setq indent-tabs-mode t)
(setq tab-width 4)
(setq c-basic-offset 4))
(t
(setq c-basic-offset 4)
(setq indent-tabs-mode nil)))
(when (or (string-match "/micropython" bfn)
(string-match "/mbed" bfn))
(setq comment-line-start "// ")
(setq comment-line-start-skip "// *"))))
(or (fboundp 'comment-dwim) ; In newer emacsen
(define-key c-mode-map "\M-;" 'c-indent-for-comment))
(define-key c-mode-map "" 'newline-and-indent)
;; From emacs.stackexchange.com 48500
(defun add-clang-format-save-hook ()
"Set a hook to run clang-format on write"
(when (find-clang-format '("clang-format.el"
"clang-format/clang-format.el"
"clang-format-10/clang-format.el"))
(add-hook 'before-save-hook
(lambda ()
(when (locate-dominating-file "." ".clang-format")
(clang-format-buffer))
;; Continue saving
nil)
nil
;; Buffer local hook
t)))
(defun find-clang-format (paths)
(if (null paths)
nil
(condition-case err
(progn
(require 'clang-format (car paths))
t)
(error (find-clang-format (cdr paths))))))
(defun c-indent-for-comment ()
"Indent this line's comment or insert an empty comment.
If there is nothing currently on the line, indent as for a command."
(interactive "*")
(beginning-of-line 1)
(if (save-excursion (skip-chars-forward " \t") (not (eolp)))
(indent-for-comment)
(c-indent-line)
(insert (or comment-line-start comment-start
(error "No comment syntax defined")))
(save-excursion (insert comment-end))))
(defun text-to-c (start end)
"Convert text in region to C printf statements."
(interactive "r")
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (search-forward "\"" nil t) ; Quote quotes
(replace-match "\\\"" nil t))
(goto-char (point-min))
(insert "printf(\"")
(end-of-line)
(insert "\\n\"")
(forward-line 1)
(while (< (point) (point-max))
(insert " \"")
(end-of-line)
(insert "\\n\"")
(forward-line 1))
(backward-char)
(insert ");")))
(defun c-to-text (start end)
"Convert printf statements produced by text-to-c back text."
(interactive "r")
(save-restriction
(narrow-to-region start end)
(goto-char start)
(while (re-search-forward "^ [^\"]*\"" nil t)
(replace-match "" nil t))
(goto-char start)
(while (re-search-forward "\\\\n\"\\();\\)?" nil t)
(replace-match "" nil t))
(goto-char start)
(while (search-forward "\\\"" nil t)
(replace-match "\"" nil t))))