-
Notifications
You must be signed in to change notification settings - Fork 27
/
emr-c.el
435 lines (366 loc) · 14.7 KB
/
emr-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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
;;; emr-c.el --- Refactoring commands for C -*- lexical-binding: t; -*-
;; Copyright (C) 2013 Chris Barrett
;; Author: Chris Barrett <chris.d.barrett@me.com>
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Refactoring commands for C and C-Based modes.
;;; Code:
(require 'emr)
(require 's)
(require 'dash)
(require 'thingatpt)
(autoload 'c-mode-map "cc-mode")
(autoload 'projectile-dir-files "projectile")
(autoload 'projectile-project-p "projectile")
(defvar emr-c:standard-headers
'("aio.h" "arpa/inet.h" "assert.h" "complex.h" "cpio.h" "ctype.h"
"curses.h" "dirent.h" "dlfcn.h" "errno.h" "fcntl.h" "fenv.h" "float.h"
"fmtmsg.h" "fnmatch.h" "ftw.h" "glob.h" "grp.h" "iconv.h" "inttypes.h"
"iso646.h" "langinfo.h" "libgen.h" "limits.h" "locale.h" "math.h"
"monetary.h" "mqueue.h" "ndbm.h" "net/if.h" "netdb.h" "netinet/in.h"
"netinet/tcp.h" "nl_types.h" "poll.h" "pthread.h" "pwd.h" "regex.h"
"sched.h" "search.h" "semaphore.h" "setjmp.h" "signal.h" "spawn.h"
"stdalign.h" "stdarg.h" "stdatomic.h" "stdbool.h" "stddef.h" "stdint.h"
"stdio.h" "stdlib.h" "stdnoreturn.h" "string.h" "strings.h" "stropts.h"
"sys/ipc.h" "sys/mman.h" "sys/msg.h" "sys/resource.h" "sys/select.h"
"sys/sem.h" "sys/shm.h" "sys/socket.h" "sys/stat.h" "sys/statvfs.h"
"sys/time.h" "sys/times.h" "sys/types.h" "sys/uio.h" "sys/un.h"
"sys/utsname.h" "sys/wait.h" "syslog.h" "tar.h" "term.h" "termios.h"
"tgmath.h" "threads.h" "time.h" "trace.h" "uchar.h" "ulimit.h"
"uncntrl.h" "unistd.h" "utime.h" "utmpx.h" "wchar.h" "wctype.h"
"wordexp.h"))
(defcustom emr-clang-format-style 'Google
"Style used to format codes with clang.
Refer to http://clang.llvm.org/docs/ClangFormatStyleOptions.html for more
detailed descriptions."
:type '(radio (const :tag "Format with style suggested by Google." Google)
(const :tag "Format used by LLVM project." LLVM)
(const :tag "Format used by Chromium project." Chromium)
(const :tag "Format used by Mozilla project." Mozilla)
(const :tag "Format used by Webkit project." WebKit)
(const :tag "Load style configuration from file." file)
(repeat :tag "Customized alist." (cons (regexp :tag "Tag")
(directory :tag "Format"))))
:group 'emr)
(defvar emr-c-format-fallback-func 'indent-region
"Function to indent a buffer region.
Will be passed start and end positions of region to be formatted.")
; ------------------
(defconst emr-c:rx-include
(rx "#include" (+ space)
(group-n 1
(or "\"" "<") (* (not space)) (or "\"" ">"))))
(defun emr-c:looking-at-include? ()
(thing-at-point-looking-at emr-c:rx-include))
(defun emr-c:bob-after-comments ()
"Move to the first non-comment character in the buffer."
(goto-char (point-min))
(while (emr-looking-at-comment?)
(forward-line 1))
(point))
(defun emr-c:goto-includes-or-buf-start ()
(goto-char (point-min))
(or (search-forward-regexp emr-c:rx-include nil t)
(emr-c:bob-after-comments))
(beginning-of-line)
(point))
;;;###autoload
(defun emr-c-tidy-includes ()
"Collate and reorder include directives in the current buffer.
Library and project includes are kept separate."
(interactive "*")
(let (includes)
(save-excursion
(emr-c:goto-includes-or-buf-start)
;; Collect include statements in buffer.
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp emr-c:rx-include nil t)
(push (match-string 1) includes)
(replace-match "")
(when (emr-blank-line?)
(ignore-errors
(kill-line)))))
;; Partition includes by type, subsort alphabetically and insert into
;; buffer.
(->> includes
(--separate (s-starts-with? "<" it))
(--map (sort it 'string<))
(-flatten)
(--map (concat "#include " it))
(s-join "\n")
(s-append "\n")
(insert)))))
;;; include guards
(defun emr-cc-basename-include-guard ()
"Derive an include guard from the buffer's basename.
All non-identifier characters of either the buffer's filename if
available or the buffer's name are replaced by underscores."
(let ((name (or (-some-> (buffer-file-name) file-name-base)
(buffer-file-name))))
(replace-regexp-in-string (rx (or ".")) "_" name t t)))
(defcustom emr-cc-include-guard-style #'emr-cc-include-guard
"Function used to derive the include guard symbol.
It will be called with no arguments in the context of the buffer
where `emr-cc-add-include-guard' is called and must return a
string to be used as include guard."
:type 'function
:group 'emr-cc)
(defcustom emr-cc-include-guard-value nil
"What include guards are #defined to, or nil.
If this is non-nil, `emr-cc-add-include-guard' will insert this
string after \"#define <symbol> \" (note the space)."
:type '(choice string (const nil))
:group 'emr-cc)
(defcustom emr-cc-include-guard-space nil
"Whether there should be a space after #.
If this is t, `emr-cc-add-include-guard' will insert a space
after the # of #define, #ifndef and #endif. This variable may
also be a string, in which case that is inserted instead."
:type '(choice
(const :tag "Insert a space after #" t)
(const :tag "Don't insert space after #" nil)
(string :tag "Insert after #:"))
:group 'emr-cc)
(defun emr-cc-include-guard-suffix-c89 (guard)
"Insert the include GUARD wrapped in a c89-style comment."
(format "/* %s */" guard))
(defun emr-cc-include-guard-suffix-comment (guard)
"Insert the include GUARD wrapped in a // comment."
(format "// %s" guard))
(defcustom emr-cc-include-guard-suffix nil
"Function to determine the text after #endif.
If this is non-nil, `emr-cc-add-include-guard' will insert the
result of calling this function with the include guard (see
`emr-cc-include-guard-style') as only argument and insert its
result, if non-nil, after \"#endif \" (note the space)."
:type '(choice (const :tag "/* GUARD */" emr-cc-include-guard-suffix-c89)
(const :tag "// GUARD" emr-cc-include-guard-suffix-comment)
(const :tag "No #endif suffix" nil)
(function :tag "custom function")))
(defun emr-cc--include-guard-space ()
"Return a string to insert for `emr-cc-include-guard-space'."
(pcase emr-cc-include-guard-space
((pred stringp) emr-cc-include-guard-space)
(`nil "")
(_ " ")))
(defun emr-cc--beginning-of-header ()
(goto-char (point-min))
;; Skip comment(s) and whitespace (e.g. license header)
(forward-sexp)
(beginning-of-line))
(defun emr-cc-add-include-guard ()
"Add an include guard to the current buffer."
(interactive)
(let ((guard (funcall emr-cc-include-guard-style)))
(save-excursion
(emr-cc--beginning-of-header)
(insert
(format
"#%3$sifndef %1$s
#%3$sdefine %1$s%2$s
"
guard (or emr-cc-include-guard-value "")
(emr-cc--include-guard-space)))
(goto-char (point-max))
(unless (= (char-before) ?\n)
(insert ?\n))
(insert
(format
"\n#%sendif%s"
(emr-cc--include-guard-space)
(or (-some->> (-some-> emr-cc-include-guard-suffix (funcall guard))
(concat " "))
""))))))
(defun emr-cc-delete-include-guard ()
"Remove the current buffer's include guard."
(interactive)
(save-excursion
(emr-cc--beginning-of-header)
(save-match-data
(when (looking-at
(rx bol "#" (* space) "ifndef" (* space) (group (+ any) symbol-end) (* any) "\n"
bol "#" (* space) "define" (* space) (backref 1) symbol-end (* any) "\n"
(? "\n")))
(replace-match "")
(goto-char (point-max))
(backward-sexp)
(beginning-of-line)
(when (looking-at (rx bol "#" (* space) "endif" symbol-end (* any) eol))
(replace-match "")
;; We can't know if the file should end in a newline, so don't delete
;; yet another newline (even if it was inserted by
;; `emr-cc-add-include-guard'). User's own whitespace management
;; solutions (e.g. `ws-butler') can fix this.
(when (eq (char-before) ?\n)
(delete-char -1)))))))
(defun emr-cc-add-pragma-once ()
"Add #pragma once."
(interactive)
(save-excursion
(emr-cc--beginning-of-header)
(insert "#pragma once\n\n")))
(defun emr-cc-delete-pragma-once ()
"Remove #pragma once."
(interactive)
(save-excursion
(emr-cc--beginning-of-header)
(save-match-data
(when (looking-at (rx "#" (* space) "pragma" (* space) "once" (* any) (** 0 2 ?\n)))
(replace-match "")))))
(defun emr-c:headers-in-project ()
"Return a list of available C header files.
Find header files in the current project. If this is not a valid
project, return all header files in the current directory."
(->> (-if-let (proj (projectile-project-p))
(--map (concat proj it) (projectile-dir-files proj))
(-> (buffer-file-name) (file-name-directory) (directory-files t)))
(--filter (-contains? '("h" "hpp") (file-name-extension it)))
(-map 'file-relative-name)))
;;;###autoload
(defun emr-c-insert-include (header)
"Insert an include for HEADER and tidy the includes in the buffer."
(interactive
(list
(if (yes-or-no-p "Library header?")
(format "<%s>" (completing-read "Header: " emr-c:standard-headers))
(format "\"%s\"" (completing-read "Header: " (emr-c:headers-in-project))))))
(let ((str (concat "#include " header)))
(when (s-contains? str (buffer-string))
(user-error "%s is already included" header))
(save-excursion
(atomic-change-group
(emr-reporting-buffer-changes "Inserted header"
;; Insert header.
(emr-c:goto-includes-or-buf-start)
(insert str)
(newline)
(emr-c-tidy-includes))))))
;;; EMR Declarations
(autoload 'clang-format-region "clang-format" "" t)
(autoload 'clang-format-buffer "clang-format" "" t)
(defun emr-clang-available? ()
"Return whether clang-format is available."
(and (featurep 'clang-format)
(executable-find "clang-format")))
(defun emr-cc-get-style ()
"Return style as a string."
(cond
((stringp emr-clang-format-style) emr-clang-format-style)
((listp emr-clang-format-style)
(concat "{"(mapconcat (lambda (x)
(format "%s: %s" (car x) (cdr x)))
emr-clang-format-style ", ") "}"))
((symbolp emr-clang-format-style) (symbol-name emr-clang-format-style))
(t nil)))
(defun emr-cc-format-region (start end)
"Format region (START/END).
Uses either clang-format, if available, or `emr-c-format-fallback-func'."
(interactive "rp")
(if (emr-clang-available?)
(clang-format-region start end (emr-cc-get-style))
(funcall emr-c-format-fallback-func start end)))
(defun emr-cc-format-buffer ()
"Format region (START/END).
Uses either clang-format, if available, or `emr-c-format-fallback-func.'"
(interactive)
(if (emr-clang-available?)
(clang-format-buffer (emr-cc-get-style))
(funcall emr-c-format-fallback-func (point-min) (point-max))))
(defalias 'emr-cc-tidy-includes 'emr-c-tidy-includes)
(defvar emr-cc-surround-var-hist nil
"A collection of variables used by if-defs..")
(defun emr-cc-surround-if-end (start end)
"Surround region between START & END with if-def."
(interactive "rp")
(let ((content (buffer-substring-no-properties start end))
(var (completing-read "Variable Name: " emr-cc-surround-var-hist
nil nil nil 'emr-cc-surround-var-hist)))
(save-excursion
(delete-region start end)
(insert (format "#ifdef %s\n" var))
(insert content)
(insert (format "\n#endif /*%s*/" var))
(emr-cc-format-region start (point)))))
(defun emr-cpp-try-catch (start end)
"Surround region between START & END with try-catch."
(interactive "rp")
(let ((content (buffer-substring-no-properties start end)))
(save-excursion
(delete-region start end)
(insert "try {\n")
(insert content)
(insert
"}\ncatch (exception& e) {\n")
(insert "throw ;\n}\n")
(emr-cc-format-region start (point)))))
(defun emr-region-active? ()
"Return t if a valid region is active."
(and mark-active (not (equal (mark) (point)))))
(defun emr-region-inactive? ()
"Return nil if a valid region is active."
(not (emr-region-active?)))
; ------------------
;;; EMR Declarations
(emr-declare-command 'emr-cc-tidy-includes
:title "tidy"
:description "includes"
:modes '(c++-mode c-mode)
:predicate (lambda ()
(emr-c:looking-at-include?)))
(emr-declare-command 'emr-cc-format-region
:title "format region"
:description (if (emr-clang-available?)
"with clang"
"with the value of emr-c-format-fallback-func")
:modes '(c-mode c++-mode)
:predicate 'emr-region-active?)
(emr-declare-command 'emr-cc-format-buffer
:title "format buffer"
:description (if (emr-clang-available?)
"with clang"
"with the value of emr-c-format-fallback-func")
:modes '(c-mode c++-mode)
:predicate 'emr-region-inactive?)
(emr-declare-command 'emr-cc-surround-if-end
:title "surround"
:description "with if-endif"
:modes '(c++-mode c-mode)
:predicate 'emr-region-active?)
(emr-declare-command 'emr-cpp-try-catch
:title "surround"
:description "with try-catch"
:modes '(c++-mode)
:predicate 'emr-region-active?)
(emr-declare-command 'emr-c-insert-include
:title "insert header"
:description "#include"
:modes '(c-mode)
:predicate (lambda () t))
;; ------------------
(defun emr-c:show-menu ()
(when (boundp 'c-mode-map)
(easy-menu-add-item
nil
'("EMR")
["Insert #include" emr-c-insert-include])))
;;;###autoload
(defun emr-c-initialize ()
"Initialize EMR in C buffers and enable the EMR menu."
(add-hook 'c-mode-hook 'emr-c:show-menu)
(--each (buffer-list)
(with-current-buffer it
(when (derived-mode-p 'c-mode)
(emr-c:show-menu)))))
(provide 'emr-c)
;;; emr-c.el ends here