forked from emacsorphanage/cpp-auto-include
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp-auto-include.el
223 lines (200 loc) · 8.94 KB
/
cpp-auto-include.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
;;; cpp-auto-include.el --- auto include header file for C++
;; Copyright (C) 2015 by Syohei YOSHIDA
;; Author: Syohei YOSHIDA <syohex@gmail.com>
;; URL: https://github.com/syohex/emacs-cpp-auto-include
;; Version: 0.01
;; Package-Requires: ((cl-lib "0.5"))
;; 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:
;;; Code:
(require 'cl-lib)
(require 'rx)
(defvar cpp-auto-include--header-regexp
`(("cstdio" nil t
,(rx (and symbol-start
(or (and (or "scanf" "sscanf" "puts" "sprintf" "printf"
"gets" "fgets" "putchar")
(* space) "(")
(and (or "FILE" "stdin" "stdout" "stderr")
symbol-end)))))
("cassert" nil t "\\bassert\\s-+(")
("cstring" nil t
,(rx (and symbol-start
(or "memcpy" "memset" "memcmp" "memncmp"
"strlen" "strcmp" "strncmp" "strcpy" "strncpy" "strerr" "strcat"
"strstr" "strchr")
symbol-end)))
("cstdlib" nil t
,(rx (and symbol-start
(or (and (or "system" "abs" "atoi" "atof" "itoa"
"strtod" "strtold" "strtoul" "strtof" "strtol"
"strtoll" "strtoull" "strtoq" "strtouq"
"free" "exit" "labs" "srand" "srandom" "srandom_r"
"rand" "rand_r" "random" "random_r" "qsort")
(* space) "(")
(and (or (and "EXIT_" (1+ (in "A-Z")))
"NULL"))))))
("cmath" nil t
,(rx (and symbol-start
(or (and (or "powf" "powl"
"acos" "acosf" "acosh" "acoshf" "acoshl" "acosl"
"asin" "asinf" "asinh" "asinhf" "asinhl" "asin"
"atan" "atan2" "atan2f" "atan2l" "atanf" "atanh" "atanhf"
"atanhl" "atanl" "exp" "expf" "expl" "exp10" "exp10f"
"exp10l" "exp2" "exp2f" "exp2l" "expm1" "expm1f" "expm1l"
"fabs" "fabsf" "fabsl" "log" "logf" "logl"
"log2" "log2f" "log2l" "log10" "log10f" "log10l" "log1p"
"log1pf" "log1pl" "nan" "nanf" "nanl"
"ceil" "ceilf" "ceill" "floor" "floorf" "floorl"
"round" "roundf" "roundl" "lround" "lroundf" "lroundl"
"llround" "llroundf" "llroundl" "sqrt" "sqrtf" "sqrtl")
(* space) "(")
(and (or "NAN" "INFINITY" "HUGE_VAL" "HUGE_VALF" "HUGE_VALL")
symbol-end)))))
("strings.h" nil t
,(rx (and symbol-start
(or "bcmp" "bcopy" "bzero" "strcasecmp" "strncasecmp")
(* space) "(")))
("typeinfo" nil t "\\btypeid\\b")
("new" t t ,(rx (and symbol-start
(or "set_new_handler" "nothrow")
(* space) "(")))
("limits" t t "\\bnumeric_limits\\s-*<\\b")
("algorithm" t t
,(rx (and symbol-start
(or "sort" "stable_sort" "partial_sort" "partial_sort_copy"
"unique" "unique_copy" "reverse" "reverse_copy"
"nth_element" "lower_bound" "upper_bound" "binary_search"
"next_permutation" "prev_permutation"
"min" "max" "count" "random_shuffle" "swap")
(* space) "(")))
("numeric" t t
,(rx (and symbol-start
(or "partial_sum" "accumulate" "adjacent_difference" "inner_product")
(* space) "(")))
("iostream" t t ,(rx (and symbol-start
(or "cin" "cout" "cerr")
symbol-end)))
("sstream" t t ,(rx (and symbol-start
(or "stringstream" "istringstream" "ostringstream")
symbol-end)))
("bitset" t t "\\bbitset\\s-*<\\b")
("complex" t t "\\bcomplex\\s-*<\\b")
("deque" t t "\\bdeque\\s-*<\\b")
("queue" t t ,(rx (and symbol-start
(or "queue" "priority_queue")
(* space) "<" word-boundary)))
("list" t t "\\blist\\s-*<")
("map" t t ,(rx (and symbol-start
(or "map" "multimap")
(* space) "<" word-boundary)))
("set" t t ,(rx (and symbol-start
(or "set" "multiset")
(* space) "<" word-boundary)))
("vector" t t "\\bvector\\s-*<")
("iomanip" t t ,(rx (and symbol-start
(or (and (or "setprecision" "setbase" "setw")
(* space) "(")
(and (or "fixed" "hex")
symbol-end)))))
("fstream" t t "\\bfstream\\s-*<")
("ctime" nil t ,(rx (and symbol-start
(or (and (or "time" "clock")
(* space) "(")
(and (or "fixed" "hex")
symbol-end)))))
("string" t t "\\bstring\\b")
("utility" t t "\\b\\(?:pair\\s-*<\\|make_pair\\)")))
(defun cpp-auto-include--include-line (header)
(save-excursion
(goto-char (point-min))
(and (re-search-forward (concat "<" header ">") nil t)
(line-number-at-pos))))
(defsubst cpp-auto-include--in-string-or-comment-p ()
(nth 8 (syntax-ppss)))
(defun cpp-auto-include--has-keyword-p (regexp line)
(save-excursion
(goto-char (point-min))
(when line
(forward-line line))
(let (finish)
(while (and (not finish) (re-search-forward regexp nil t))
(unless (cpp-auto-include--in-string-or-comment-p)
(setq finish t)))
finish)))
(defun cpp-auto-include--parse-file ()
(cl-loop with use-std = nil
with added = nil
with removed = nil
with case-fold-search = nil
for info in cpp-auto-include--header-regexp
for header = (nth 0 info)
for regexp = (nth 3 info)
for included-line = (cpp-auto-include--include-line header)
for has-keyword = (cpp-auto-include--has-keyword-p regexp included-line)
when (and (not use-std) has-keyword)
do (setq use-std t)
do
(cond ((and has-keyword (not included-line))
(cl-pushnew header added :test 'equal))
((and included-line (not has-keyword))
(cl-pushnew (cons header included-line) removed :test 'equal)))
finally
return (list :use-std use-std
:added added :removed removed)))
(defun cpp-auto-include--header-files ()
(save-excursion
(goto-char (point-min))
(let ((re "^\\s-*#\\s-*include\\s-*<\\([^>]+\\)>")
headers)
(while (re-search-forward re nil t)
(cl-pushnew (match-string-no-properties 1) headers :test 'equal))
headers)))
(defun cpp-auto-include--header-insert-point ()
(save-excursion
(goto-char (point-max))
(when (re-search-backward "^#\\s-*include\\s-*[<\"]" nil t)
(forward-line 1)
(point))))
(defun cpp-auto-include--add-headers (headers)
(save-excursion
(let ((insert-point (or (cpp-auto-include--header-insert-point) (point-min))))
(goto-char insert-point)
(dolist (header headers)
(insert (format "#include <%s>\n" header)))
(unless (re-search-forward "^\\s-*$" (line-end-position) t)
(insert "\n")))))
(defun cpp-auto-include--remove-headers (headers)
(save-excursion
(cl-loop with deleted-lines = 0
initially (goto-char (point-min))
for (header . line) in (sort headers (lambda (a b) (< (cdr a) (cdr b))))
for curline = 1 then (line-number-at-pos)
do
(progn
(forward-line (- line curline deleted-lines))
(let ((beg (point)))
(forward-line 1)
(delete-region beg (point))
(cl-incf deleted-lines))))))
;;;###autoload
(defun cpp-auto-include ()
(interactive)
(let* ((info (cpp-auto-include--parse-file))
(added (plist-get info :added))
(removed (plist-get info :removed)))
(when removed
(cpp-auto-include--remove-headers removed))
(when added
(cpp-auto-include--add-headers added))))
(provide 'cpp-auto-include)
;;; cpp-auto-include.el ends here