/
cpp-modes.el
335 lines (288 loc) · 11.1 KB
/
cpp-modes.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
;;------------------------------------------------------------------------------
;; C++ mode
(setq-default c-basic-offset 2)
;;------------------------------------------------------------------------------
;; LLVM root directory
(defun find-file-recursive (directory filename)
(if (and directory (file-directory-p directory))
(let ((found-clang-formats
(directory-files-recursively directory
(concat "^" filename "$"))))
(if found-clang-formats
(car found-clang-formats)
nil))
nil))
(defun apply-macro (macro arg-list)
(eval
`(,macro ,@(cl-loop for arg in arg-list
collect `(quote ,arg)))))
(defun find-file-first-dir (directories filename)
(apply-macro 'or (append (mapcar (lambda (dir) (find-file-recursive dir filename))
possible-roots)
(list (executable-find filename)))))
(defun find-llvm-root (possible-roots)
(let ((clang-exe (find-file-first-dir possible-roots "clangd")))
(when clang-exe (file-truename clang-exe))))
(setq-default llvm-roots '("/usr/local/llvm/"))
(setq-default llvm-root
(let ((root (find-llvm-root llvm-roots)))
(when root
(string-remove-suffix "bin/" (file-name-directory root)))))
(defun find-exe (root filename)
(or (find-file-recursive root filename)
(executable-find filename)))
;;------------------------------------------------------------------------------
;; syntax highlighting
(use-package modern-cpp-font-lock
:ensure t
:hook (c++-mode . modern-c++-font-lock-mode))
;;------------------------------------------------------------------------------
;; clang-format
(setq clang-format-executable (find-exe llvm-root "clang-format"))
(use-package clang-format
:ensure t)
(eval-after-load 'cc-mode
'(bind-keys :map c++-mode-map
("C-c f" . clang-format)))
;; clang-format-on-save
(defcustom my-clang-format-enabled t
"If t, run clang-format on cpp buffers upon saving."
:group 'clang-format
:type 'boolean
:safe 'booleanp)
(defun my-clang-format-before-save ()
(interactive)
(when my-clang-format-enabled
(clang-format-buffer)))
;; clang-format files are YAML
(add-to-list 'auto-mode-alist '("\\.clang-format\\'" . yaml-mode))
;;------------------------------------------------------------------------------
;; Auto insertion of headers
(autoload 'cpp-auto-include/namespace-qualify-file "cpp-auto-include"
"Explicitly qualify uses of the standard library with their namespace(s)." t)
(autoload 'cpp-auto-include/ensure-includes-for-file "cpp-auto-include"
"Auto-insert #include line(s) required for the current buffer." t)
(autoload 'cpp-auto-include/ensure-includes-for-current-line "cpp-auto-include"
"Auto-insert #include line(s) required for the current line." t)
(eval-after-load 'cc-mode
'(bind-keys :map c++-mode-map
("C-c q" . cpp-auto-include/namespace-qualify-file)
("C-c i" . cpp-auto-include/ensure-includes-for-file)
("C-c o" . cpp-auto-include/ensure-includes-for-current-line)))
;;------------------------------------------------------------------------------
;; indentation rules
(defun indentation-c-mode-hook ()
(c-set-offset 'substatement-open 0)
(c-set-offset 'brace-list-open 0)
(c-set-offset 'member-init-cont '-)
(c-set-offset 'arglist-intro '+)
(c-set-offset 'arglist-close 0)
(c-set-offset 'case-label '+)
(c-set-offset 'statement-case-open 0))
(add-hook 'c-mode-common-hook 'indentation-c-mode-hook)
;;------------------------------------------------------------------------------
;; Align Boost.SML tables
(autoload 'find-and-align-boost-sml "boost-sml"
"Find and align Boost.SML tables." t)
(eval-after-load 'cc-mode
'(bind-keys :map c++-mode-map
("C-<tab>" . align)
("C-]" . find-and-align-boost-sml)))
;;------------------------------------------------------------------------------
;; lsp + clangd + company
(use-package company
:ensure t)
(when (display-graphic-p)
(use-package company-box
:ensure t
:hook (company-mode . company-box-mode)))
(setq company-clang-executable (find-exe llvm-root "clang"))
(setq my-clangd-executable (find-exe llvm-root "clangd"))
(setq my-clang-tidy-executable (find-exe llvm-root "clang-tidy"))
;; In c++-mode, start lsp mode etc unless we're in a temp buffer
;; (don't do it when exporting org-mode blocks)
(defun my-c++-mode-hook ()
(unless (string-match-p (regexp-quote "*temp*") (buffer-name))
(add-hook 'before-save-hook 'my-clang-format-before-save nil t)
(company-mode)
(lsp)))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(use-package lsp-mode
:ensure t
:init
(require 'lsp-clangd)
(setq lsp-enable-indentation nil
lsp-auto-guess-root t
lsp-clangd-binary-path my-clangd-executable
lsp-prefer-flymake nil))
(use-package lsp-ui
:ensure t
:config
(setq lsp-ui-sideline-enable t
lsp-ui-sideline-show-symbol t
lsp-ui-sideline-show-hover t
lsp-ui-sideline-show-code-actions t
lsp-ui-sideline-update-mode 'point
lsp-ui-doc-header t
lsp-ui-doc-include-signature t
lsp-ui-sideline-ignore-duplicate t
lsp-ui-flycheck-enable t
lsp-ui-imenu-enable t)
(define-key lsp-ui-mode-map [remap xref-find-references] #'lsp-ui-peek-find-references)
(define-key lsp-ui-mode-map (kbd "M-RET") #'lsp-ui-sideline-apply-code-actions)
(define-key lsp-ui-mode-map (kbd "C-c m") #'lsp-ui-imenu)
:hook ((lsp-mode . lsp-enable-imenu)
(lsp-mode . lsp-ui-mode)))
;;------------------------------------------------------------------------------
;; Header completion
(use-package company-c-headers
:ensure t
:config
(push 'company-c-headers company-backends))
;;------------------------------------------------------------------------------
;; Building & error navigation
(setq compilation-scroll-output t)
;; Remove compilation window on success
(setq compilation-finish-functions
(lambda (buf str)
(if (null (string-match ".*exited abnormally.*" str))
;;no errors, make the compilation window go away in a few seconds
(progn
(run-at-time
"1 sec" nil 'delete-windows-on
(get-buffer-create "*compilation*"))
(message "No compilation errors!")))))
(eval-after-load 'cc-mode
'(bind-keys :map c++-mode-map
("M-k" . projectile-compile-project)))
;; make compilation buffers support ANSI colours
(require 'ansi-color)
(defun colorize-compilation-buffer ()
(ansi-color-apply-on-region compilation-filter-start (point)))
(add-hook 'compilation-filter-hook 'colorize-compilation-buffer)
;;------------------------------------------------------------------------------
;; Debugging
(setq gdb-many-windows t
gdb-show-main t)
(defun gdb-run-or-cont (arg)
"Run or continue program with numeric argument ARG."
(interactive "p")
(when (boundp 'gdb-thread-number)
(if (eq gdb-thread-number nil)
(gud-run arg)
(gud-cont arg))))
(use-package gud
:bind (("C-x C-a <f5>" . gdb-run-or-cont)))
;;------------------------------------------------------------------------------
;; Transpose function args
(defun c-forward-to-argsep ()
"Move to the end of the current c function argument.
Returns point."
(interactive)
(while
(progn (comment-forward most-positive-fixnum)
(looking-at "[^,)>}]"))
(forward-sexp))
(point))
(defun c-backward-to-argsep ()
"Move to the beginning of the current c function argument.
Returns point."
(interactive)
(let ((pt (point)) cur)
(up-list -1)
(forward-char)
(while
(progn
(setq cur (point))
(> pt (c-forward-to-argsep)))
(forward-char))
(goto-char cur)))
(defun c-transpose-args-direction (is_forward)
"Transpose two arguments of a c-function.
The first arg is the one with point in it."
(interactive)
(let*
(;; only different to pt when not 'is_forward'
(pt-original (point))
(pt
(progn
(when (not is_forward)
(goto-char (- (c-backward-to-argsep) 1))
(unless (looking-at ",")
(goto-char pt-original)
(user-error "Argument separator not found")))
(point)))
(b (c-backward-to-argsep))
(sep
(progn (goto-char pt)
(c-forward-to-argsep)))
(e
(progn
(unless (looking-at ",")
(goto-char pt-original)
(user-error "Argument separator not found"))
(forward-char)
(c-forward-to-argsep)))
(ws-first
(buffer-substring-no-properties
(goto-char b)
(progn (skip-chars-forward "[[:space:]\n]")
(point))))
(first (buffer-substring-no-properties (point) sep))
(ws-second
(buffer-substring-no-properties
(goto-char (1+ sep))
(progn (skip-chars-forward "[[:space:]\n]")
(point))))
(second (buffer-substring-no-properties (point) e)))
(delete-region b e)
(insert ws-first second "," ws-second first)
;; Correct the cursor location to be on the same character.
(if is_forward
(goto-char
(+
;; word start.
(- (point) (length first))
;; Apply initial offset within the word.
(- pt b (length ws-first))))
(goto-char
(+
b (length ws-first)
;; Apply initial offset within the word.
(- pt-original (+ pt 1 (length ws-second))))))))
(defun c-transpose-args-forward () (interactive) (c-transpose-args-direction t))
(defun c-transpose-args-backward () (interactive) (c-transpose-args-direction nil))
(defun c-transpose-args (prefix)
"Transpose argument at point with the argument before it.
With prefix arg ARG, transpose with the argument after it."
(interactive "P")
(cond ((not prefix) (c-transpose-args-backward))
(t (c-transpose-args-forward))))
(eval-after-load 'cc-mode
'(bind-keys :map c++-mode-map
("C-M-t" . c-transpose-args)))
;;------------------------------------------------------------------------------
;; Flycheck setup for c++-mode
(use-package flycheck-clang-tidy
:ensure t
:init
(setq flycheck-clang-tidy-executable my-clang-tidy-executable)
:after flycheck
:hook
((flycheck-mode . flycheck-clang-tidy-setup)))
;; From https://github.com/flycheck/flycheck/issues/1762#issuecomment-749789589
;; Add buffer local Flycheck checkers after LSP for different major modes.
(defvar-local my-flycheck-local-cache nil)
(defun my-flycheck-local-checker-get (fn checker property)
;; Only check the buffer local cache for the LSP checker, otherwise we get
;; infinite loops.
(if (eq checker 'lsp)
(or (alist-get property my-flycheck-local-cache)
(funcall fn checker property))
(funcall fn checker property)))
(advice-add 'flycheck-checker-get
:around 'my-flycheck-local-checker-get)
(add-hook 'lsp-managed-mode-hook
(lambda ()
(when (derived-mode-p 'c++-mode)
(setq my-flycheck-local-cache '((next-checkers . (c/c++-clang-tidy)))))))