Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 618 lines (520 sloc) 22.86 kB
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
1 ;;; auto-complete-clang-async.el --- Auto Completion source for clang for GNU Emacs
677ffdc @Golevka first commit
Golevka authored
2
3 ;; Copyright (C) 2010 Brian Jiang
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
4 ;; Copyright (C) 2012 Taylan Ulrich Bayirli/Kammer
677ffdc @Golevka first commit
Golevka authored
5
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
6 ;; Authors: Brian Jiang <brianjcj@gmail.com>
7 ;; Golevka(?) [https://github.com/Golevka]
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
8 ;; Taylan Ulrich Bayirli/Kammer <taylanbayirli@gmail.com>
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
9 ;; Many others
677ffdc @Golevka first commit
Golevka authored
10 ;; Keywords: completion, convenience
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
11 ;; Version: 0
677ffdc @Golevka first commit
Golevka authored
12
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25
26
27 ;;; Commentary:
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
28
29 ;; Auto Completion source for clang.
30 ;; Uses a "completion server" process to utilize libclang.
31 ;; Also provides flymake syntax checking.
677ffdc @Golevka first commit
Golevka authored
32
33 ;;; Code:
34
35
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
36 (provide 'auto-complete-clang-async)
be5edfa required cl for case and setf
Workstation authored
37 (require 'cl)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
38 (require 'auto-complete)
39 (require 'flymake)
40
41
42 (defcustom ac-clang-complete-executable
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
43 (executable-find "clang-complete")
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
44 "Location of clang-complete executable."
677ffdc @Golevka first commit
Golevka authored
45 :group 'auto-complete
46 :type 'file)
47
48 (defcustom ac-clang-lang-option-function nil
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
49 "Function to return the lang type for option -x."
677ffdc @Golevka first commit
Golevka authored
50 :group 'auto-complete
51 :type 'function)
52
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
53 (defcustom ac-clang-cflags nil
677ffdc @Golevka first commit
Golevka authored
54 "Extra flags to pass to the Clang executable.
eb9369a @TaylanUB Make `ac-clang-flags' buffer-local.
TaylanUB authored
55 This variable will typically contain include paths, e.g., (\"-I~/MyProject\" \"-I.\")."
677ffdc @Golevka first commit
Golevka authored
56 :group 'auto-complete
57 :type '(repeat (string :tag "Argument" "")))
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
58 (make-variable-buffer-local 'ac-clang-cflags)
677ffdc @Golevka first commit
Golevka authored
59
60 (defun ac-clang-set-cflags ()
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
61 "Set `ac-clang-cflags' interactively."
677ffdc @Golevka first commit
Golevka authored
62 (interactive)
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
63 (setq ac-clang-cflags (split-string (read-string "New cflags: ")))
677ffdc @Golevka first commit
Golevka authored
64 (ac-clang-update-cmdlineargs))
65
66 (defun ac-clang-set-cflags-from-shell-command ()
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
67 "Set `ac-clang-cflags' to a shell command's output."
677ffdc @Golevka first commit
Golevka authored
68 "set new cflags for ac-clang from shell command output"
69 (interactive)
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
70 (setq ac-clang-cflags
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
71 (split-string
72 (shell-command-to-string
73 (read-shell-command "Shell command: " nil nil
74 (and buffer-file-name
75 (file-relative-name buffer-file-name))))))
677ffdc @Golevka first commit
Golevka authored
76 (ac-clang-update-cmdlineargs))
77
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
78 (defvar ac-clang-prefix-header nil
79 "The prefix header to pass to the Clang executable.")
80 (make-variable-buffer-local 'ac-clang-prefix-header)
677ffdc @Golevka first commit
Golevka authored
81
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
82 (defun ac-clang-set-prefix-header (prefix-header)
83 "Set `ac-clang-prefix-header' interactively."
84 (interactive
85 (let ((default (car (directory-files "." t "\\([^.]h\\|[^h]\\).pch\\'" t))))
86 (list
87 (read-file-name (concat "Clang prefix header (currently " (or ac-clang-prefix-header "nil") "): ")
88 (when default (file-name-directory default))
89 default nil (when default (file-name-nondirectory default))))))
90 (cond
91 ((string-match "^[\s\t]*$" prefix-header)
92 (setq ac-clang-prefix-header nil))
93 (t
94 (setq ac-clang-prefix-header prefix-header))))
677ffdc @Golevka first commit
Golevka authored
95
96
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
97 (defconst ac-clang-completion-pattern
98 "^COMPLETION: \\(%s[^\s\n:]*\\)\\(?: : \\)*\\(.*$\\)")
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
99
677ffdc @Golevka first commit
Golevka authored
100 (defun ac-clang-parse-output (prefix)
101 (goto-char (point-min))
102 (let ((pattern (format ac-clang-completion-pattern
103 (regexp-quote prefix)))
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
104 lines match detailed-info
677ffdc @Golevka first commit
Golevka authored
105 (prev-match ""))
106 (while (re-search-forward pattern nil t)
107 (setq match (match-string-no-properties 1))
108 (unless (string= "Pattern" match)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
109 (setq detailed-info (match-string-no-properties 2))
110
677ffdc @Golevka first commit
Golevka authored
111 (if (string= match prev-match)
112 (progn
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
113 (when detailed-info
677ffdc @Golevka first commit
Golevka authored
114 (setq match (propertize match
115 'ac-clang-help
116 (concat
117 (get-text-property 0 'ac-clang-help (car lines))
118 "\n"
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
119 detailed-info)))
677ffdc @Golevka first commit
Golevka authored
120 (setf (car lines) match)
121 ))
122 (setq prev-match match)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
123 (when detailed-info
124 (setq match (propertize match 'ac-clang-help detailed-info)))
677ffdc @Golevka first commit
Golevka authored
125 (push match lines))))
126 lines))
127
128
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
129 (defconst ac-clang-error-buffer-name "*clang error*")
130
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
131 (defun ac-clang-handle-error (res args)
132 (goto-char (point-min))
133 (let* ((buf (get-buffer-create ac-clang-error-buffer-name))
134 (cmd (concat ac-clang-executable " " (mapconcat 'identity args " ")))
135 (pattern (format ac-clang-completion-pattern ""))
136 (err (if (re-search-forward pattern nil t)
137 (buffer-substring-no-properties (point-min)
138 (1- (match-beginning 0)))
139 ;; Warn the user more agressively if no match was found.
140 (message "clang failed with error %d:\n%s" res cmd)
141 (buffer-string))))
142
143 (with-current-buffer buf
144 (let ((inhibit-read-only t))
145 (erase-buffer)
146 (insert (current-time-string)
147 (format "\nclang failed with error %d:\n" res)
148 cmd "\n\n")
149 (insert err)
150 (setq buffer-read-only t)
151 (goto-char (point-min))))))
152
153 (defun ac-clang-call-process (prefix &rest args)
154 (let ((buf (get-buffer-create "*clang-output*"))
155 res)
156 (with-current-buffer buf (erase-buffer))
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
157 (setq res (apply 'call-process-region (point-min) (point-max)
158 ac-clang-executable nil buf nil args))
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
159 (with-current-buffer buf
160 (unless (eq 0 res)
161 (ac-clang-handle-error res args))
162 ;; Still try to get any useful input.
163 (ac-clang-parse-output prefix))))
164
165
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
166 (defsubst ac-clang-create-position-string (pos)
677ffdc @Golevka first commit
Golevka authored
167 (save-excursion
168 (goto-char pos)
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
169 (format "row:%d\ncolumn:%d\n"
170 (line-number-at-pos)
677ffdc @Golevka first commit
Golevka authored
171 (1+ (- (point) (line-beginning-position))))))
172
173 (defsubst ac-clang-lang-option ()
174 (or (and ac-clang-lang-option-function
175 (funcall ac-clang-lang-option-function))
176 (cond ((eq major-mode 'c++-mode)
177 "c++")
178 ((eq major-mode 'c-mode)
179 "c")
180 ((eq major-mode 'objc-mode)
181 (cond ((string= "m" (file-name-extension (buffer-file-name)))
182 "objective-c")
183 (t
184 "objective-c++")))
185 (t
186 "c++"))))
187
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
188 (defsubst ac-clang-build-complete-args ()
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
189 (append '("-cc1" "-fsyntax-only")
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
190 (list "-x" (ac-clang-lang-option))
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
191 ac-clang-cflags
677ffdc @Golevka first commit
Golevka authored
192 (when (stringp ac-clang-prefix-header)
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
193 (list "-include-pch" (expand-file-name ac-clang-prefix-header)))))
677ffdc @Golevka first commit
Golevka authored
194
195
196 (defsubst ac-clang-clean-document (s)
197 (when s
198 (setq s (replace-regexp-in-string "<#\\|#>\\|\\[#" "" s))
199 (setq s (replace-regexp-in-string "#\\]" " " s)))
200 s)
201
202 (defun ac-clang-document (item)
203 (if (stringp item)
204 (let (s)
205 (setq s (get-text-property 0 'ac-clang-help item))
206 (ac-clang-clean-document s)))
207 ;; (popup-item-property item 'ac-clang-help)
208 )
209
210
211 (defface ac-clang-candidate-face
212 '((t (:background "lightgray" :foreground "navy")))
213 "Face for clang candidate"
214 :group 'auto-complete)
215
216 (defface ac-clang-selection-face
217 '((t (:background "navy" :foreground "white")))
218 "Face for the clang selected candidate."
219 :group 'auto-complete)
220
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
221 (defsubst ac-clang-in-string/comment ()
222 "Return non-nil if point is in a literal (a comment or string)."
223 (nth 8 (syntax-ppss)))
224
677ffdc @Golevka first commit
Golevka authored
225
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
226 (defvar ac-clang-template-start-point nil)
227 (defvar ac-clang-template-candidates (list "ok" "no" "yes:)"))
677ffdc @Golevka first commit
Golevka authored
228
229 (defun ac-clang-action ()
230 (interactive)
231 ;; (ac-last-quick-help)
232 (let ((help (ac-clang-clean-document (get-text-property 0 'ac-clang-help (cdr ac-last-completion))))
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
233 (raw-help (get-text-property 0 'ac-clang-help (cdr ac-last-completion)))
234 (candidates (list)) ss fn args (ret-t "") ret-f)
235 (setq ss (split-string raw-help "\n"))
677ffdc @Golevka first commit
Golevka authored
236 (dolist (s ss)
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
237 (when (string-match "\\[#\\(.*\\)#\\]" s)
238 (setq ret-t (match-string 1 s)))
239 (setq s (replace-regexp-in-string "\\[#.*?#\\]" "" s))
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
240 (cond ((string-match "^\\([^(<]*\\)\\(:.*\\)" s)
241 (setq fn (match-string 1 s)
242 args (match-string 2 s))
243 (push (propertize (ac-clang-clean-document args) 'ac-clang-help ret-t
244 'raw-args args) candidates))
245 ((string-match "^\\([^(]*\\)\\((.*)\\)" s)
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
246 (setq fn (match-string 1 s)
247 args (match-string 2 s))
248 (push (propertize (ac-clang-clean-document args) 'ac-clang-help ret-t
249 'raw-args args) candidates)
250 (when (string-match "\{#" args)
251 (setq args (replace-regexp-in-string "\{#.*#\}" "" args))
252 (push (propertize (ac-clang-clean-document args) 'ac-clang-help ret-t
253 'raw-args args) candidates))
254 (when (string-match ", \\.\\.\\." args)
255 (setq args (replace-regexp-in-string ", \\.\\.\\." "" args))
256 (push (propertize (ac-clang-clean-document args) 'ac-clang-help ret-t
257 'raw-args args) candidates)))
258 ((string-match "^\\([^(]*\\)(\\*)\\((.*)\\)" ret-t) ;; check whether it is a function ptr
259 (setq ret-f (match-string 1 ret-t)
260 args (match-string 2 ret-t))
261 (push (propertize args 'ac-clang-help ret-f 'raw-args "") candidates)
262 (when (string-match ", \\.\\.\\." args)
263 (setq args (replace-regexp-in-string ", \\.\\.\\." "" args))
264 (push (propertize args 'ac-clang-help ret-f 'raw-args "") candidates)))))
265 (cond (candidates
266 (setq candidates (delete-dups candidates))
267 (setq candidates (nreverse candidates))
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
268 (setq ac-clang-template-candidates candidates)
269 (setq ac-clang-template-start-point (point))
554e680 @Golevka yasnippet also worked
Golevka authored
270 (ac-complete-clang-template)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
271
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
272 (unless (cdr candidates) ;; unless length > 1
273 (message (replace-regexp-in-string "\n" " ; " help))))
274 (t
275 (message (replace-regexp-in-string "\n" " ; " help))))))
276
677ffdc @Golevka first commit
Golevka authored
277 (defun ac-clang-prefix ()
278 (or (ac-prefix-symbol)
279 (let ((c (char-before)))
280 (when (or (eq ?\. c)
281 ;; ->
282 (and (eq ?> c)
283 (eq ?- (char-before (1- (point)))))
284 ;; ::
285 (and (eq ?: c)
286 (eq ?: (char-before (1- (point))))))
287 (point)))))
288
289 (defun ac-clang-same-count-in-string (c1 c2 s)
290 (let ((count 0) (cur 0) (end (length s)) c)
291 (while (< cur end)
292 (setq c (aref s cur))
293 (cond ((eq c1 c)
294 (setq count (1+ count)))
295 ((eq c2 c)
296 (setq count (1- count))))
297 (setq cur (1+ cur)))
298 (= count 0)))
299
300 (defun ac-clang-split-args (s)
301 (let ((sl (split-string s ", *")))
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
302 (cond ((string-match "<\\|(" s)
677ffdc @Golevka first commit
Golevka authored
303 (let ((res (list)) (pre "") subs)
304 (while sl
305 (setq subs (pop sl))
306 (unless (string= pre "")
307 (setq subs (concat pre ", " subs))
308 (setq pre ""))
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
309 (cond ((and (ac-clang-same-count-in-string ?\< ?\> subs)
310 (ac-clang-same-count-in-string ?\( ?\) subs))
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
311 ;; (cond ((ac-clang-same-count-in-string ?\< ?\> subs)
677ffdc @Golevka first commit
Golevka authored
312 (push subs res))
313 (t
314 (setq pre subs))))
315 (nreverse res)))
316 (t
317 sl))))
318
319
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
320 (defun ac-clang-template-candidate ()
321 ac-clang-template-candidates)
677ffdc @Golevka first commit
Golevka authored
322
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
323 (defun ac-clang-template-action ()
677ffdc @Golevka first commit
Golevka authored
324 (interactive)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
325 (unless (null ac-clang-template-start-point)
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
326 (let ((pos (point)) sl (snp "")
327 (s (get-text-property 0 'raw-args (cdr ac-last-completion))))
328 (cond ((string= s "")
329 ;; function ptr call
330 (setq s (cdr ac-last-completion))
331 (setq s (replace-regexp-in-string "^(\\|)$" "" s))
332 (setq sl (ac-clang-split-args s))
333 (cond ((featurep 'yasnippet)
334 (dolist (arg sl)
335 (setq snp (concat snp ", ${" arg "}")))
336 (condition-case nil
337 (yas/expand-snippet (concat "(" (substring snp 2) ")")
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
338 ac-clang-template-start-point pos) ;; 0.6.1c
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
339 (error
340 ;; try this one:
341 (ignore-errors (yas/expand-snippet
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
342 ac-clang-template-start-point pos
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
343 (concat "(" (substring snp 2) ")"))) ;; work in 0.5.7
344 )))
345 ((featurep 'snippet)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
346 (delete-region ac-clang-template-start-point pos)
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
347 (dolist (arg sl)
348 (setq snp (concat snp ", $${" arg "}")))
349 (snippet-insert (concat "(" (substring snp 2) ")")))
350 (t
351 (message "Dude! You are too out! Please install a yasnippet or a snippet script:)"))))
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
352 (t
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
353 (unless (string= s "()")
354 (setq s (replace-regexp-in-string "{#" "" s))
355 (setq s (replace-regexp-in-string "#}" "" s))
356 (cond ((featurep 'yasnippet)
357 (setq s (replace-regexp-in-string "<#" "${" s))
358 (setq s (replace-regexp-in-string "#>" "}" s))
359 (setq s (replace-regexp-in-string ", \\.\\.\\." "}, ${..." s))
360 (condition-case nil
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
361 (yas/expand-snippet s ac-clang-template-start-point pos) ;; 0.6.1c
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
362 (error
363 ;; try this one:
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
364 (ignore-errors (yas/expand-snippet ac-clang-template-start-point pos s)) ;; work in 0.5.7
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
365 )))
366 ((featurep 'snippet)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
367 (delete-region ac-clang-template-start-point pos)
6528bfb @Golevka Nice changes from upstream, added function pointer parameter support
Golevka authored
368 (setq s (replace-regexp-in-string "<#" "$${" s))
369 (setq s (replace-regexp-in-string "#>" "}" s))
370 (setq s (replace-regexp-in-string ", \\.\\.\\." "}, $${..." s))
371 (snippet-insert s))
372 (t
373 (message "Dude! You are too out! Please install a yasnippet or a snippet script:)")))))))))
677ffdc @Golevka first commit
Golevka authored
374
375
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
376 (defun ac-clang-template-prefix ()
377 ac-clang-template-start-point)
677ffdc @Golevka first commit
Golevka authored
378
379
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
380 ;; This source shall only be used internally.
554e680 @Golevka yasnippet also worked
Golevka authored
381 (ac-define-source clang-template
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
382 '((candidates . ac-clang-template-candidate)
383 (prefix . ac-clang-template-prefix)
677ffdc @Golevka first commit
Golevka authored
384 (requires . 0)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
385 (action . ac-clang-template-action)
677ffdc @Golevka first commit
Golevka authored
386 (document . ac-clang-document)
387 (cache)
388 (symbol . "t")))
389
390
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
391 ;;;
392 ;;; Rest of the file is related to async.
393 ;;;
394
395 (defvar ac-clang-status 'idle)
396 (defvar ac-clang-current-candidate nil)
397 (defvar ac-clang-completion-process nil)
398 (defvar ac-clang-saved-prefix "")
677ffdc @Golevka first commit
Golevka authored
399
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
400 (make-variable-buffer-local 'ac-clang-status)
401 (make-variable-buffer-local 'ac-clang-current-candidate)
402 (make-variable-buffer-local 'ac-clang-completion-process)
677ffdc @Golevka first commit
Golevka authored
403
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
404 ;;;
405 ;;; Functions to speak with the clang-complete process
406 ;;;
677ffdc @Golevka first commit
Golevka authored
407
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
408 (defun ac-clang-send-source-code (proc)
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
409 (save-restriction
410 (widen)
3e62592 @Golevka fixed multibyte character problem (again)
Golevka authored
411 (process-send-string
412 proc (format "source_length:%d\n"
413 (length (string-as-unibyte ; fix non-ascii character problem
414 (buffer-substring-no-properties (point-min) (point-max)))
415 )))
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
416 (process-send-string proc (buffer-substring-no-properties (point-min) (point-max)))
417 (process-send-string proc "\n\n")))
677ffdc @Golevka first commit
Golevka authored
418
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
419 (defun ac-clang-send-reparse-request (proc)
677ffdc @Golevka first commit
Golevka authored
420 (save-restriction
421 (widen)
422 (process-send-string proc "SOURCEFILE\n")
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
423 (ac-clang-send-source-code proc)
424 (process-send-string proc "REPARSE\n\n")))
677ffdc @Golevka first commit
Golevka authored
425
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
426 (defun ac-clang-send-completion-request (proc)
677ffdc @Golevka first commit
Golevka authored
427 (save-restriction
428 (widen)
429 (process-send-string proc "COMPLETION\n")
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
430 (process-send-string proc (ac-clang-create-position-string (- (point) (length ac-prefix))))
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
431 (ac-clang-send-source-code proc)))
677ffdc @Golevka first commit
Golevka authored
432
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
433 (defun ac-clang-send-syntaxcheck-request (proc)
8b5dfbb @Golevka basic test ground established
Golevka authored
434 (save-restriction
435 (widen)
436 (process-send-string proc "SYNTAXCHECK\n")
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
437 (ac-clang-send-source-code proc)))
8b5dfbb @Golevka basic test ground established
Golevka authored
438
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
439 (defun ac-clang-send-cmdline-args (proc)
677ffdc @Golevka first commit
Golevka authored
440 ;; send message head and num_args
441 (process-send-string proc "CMDLINEARGS\n")
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
442 (process-send-string
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
443 proc (format "num_args:%d\n" (length (ac-clang-build-complete-args))))
677ffdc @Golevka first commit
Golevka authored
444
445 ;; send arguments
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
446 (mapc
447 (lambda (arg)
677ffdc @Golevka first commit
Golevka authored
448 (process-send-string proc (format "%s " arg)))
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
449 (ac-clang-build-complete-args))
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
450 (process-send-string proc "\n"))
677ffdc @Golevka first commit
Golevka authored
451
452 (defun ac-clang-update-cmdlineargs ()
453 (interactive)
ccd0a08 show a warning message when ac-clang-cflags is not a list
Workstation authored
454 (if (listp ac-clang-cflags)
455 (ac-clang-send-cmdline-args ac-clang-completion-process)
456 (message "`ac-clang-cflags' should be a list of strings")))
677ffdc @Golevka first commit
Golevka authored
457
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
458 (defun ac-clang-send-shutdown-command (proc)
0bb183f @Golevka fixed clang-complete process termination problem
Golevka authored
459 (if (eq (process-status "clang-complete") 'run)
460 (process-send-string proc "SHUTDOWN\n"))
461 )
677ffdc @Golevka first commit
Golevka authored
462
463
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
464 (defun ac-clang-append-process-output-to-process-buffer (process output)
399fd83 @TaylanUB Clean up, make `ac-clang-cflags' buffer-local
TaylanUB authored
465 "Append process output to the process buffer."
27c18fb @Golevka redirect diagnostic message to process buffer
Golevka authored
466 (with-current-buffer (process-buffer process)
467 (save-excursion
468 ;; Insert the text, advancing the process marker.
469 (goto-char (process-mark process))
470 (insert output)
471 (set-marker (process-mark process) (point)))
472 (goto-char (process-mark process))))
473
474
677ffdc @Golevka first commit
Golevka authored
475 ;;
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
476 ;; Receive server responses (completion candidates) and fire auto-complete
477 ;;
478 (defun ac-clang-parse-completion-results (proc)
677ffdc @Golevka first commit
Golevka authored
479 (with-current-buffer (process-buffer proc)
480 (ac-clang-parse-output ac-clang-saved-prefix)))
481
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
482 (defun ac-clang-filter-output (proc string)
483 (ac-clang-append-process-output-to-process-buffer proc string)
677ffdc @Golevka first commit
Golevka authored
484 (if (string= (substring string -1 nil) "$")
2898cf5 @TaylanUB Change some `cond' usages to `case'
TaylanUB authored
485 (case ac-clang-status
486 (preempted
487 (setq ac-clang-status 'idle)
488 (ac-start)
489 (ac-update))
490
491 (otherwise
492 (setq ac-clang-current-candidate (ac-clang-parse-completion-results proc))
493 ;; (message "ac-clang results arrived")
494 (setq ac-clang-status 'acknowledged)
495 (ac-start :force-init t)
496 (ac-update)
497 (setq ac-clang-status 'idle)))))
677ffdc @Golevka first commit
Golevka authored
498
499
500 (defun ac-clang-candidate ()
2898cf5 @TaylanUB Change some `cond' usages to `case'
TaylanUB authored
501 (case ac-clang-status
502 (idle
503 ;; (message "ac-clang-candidate triggered - fetching candidates...")
504 (setq ac-clang-saved-prefix ac-prefix)
505
506 ;; NOTE: although auto-complete would filter the result for us, but when there's
507 ;; a HUGE number of candidates avaliable it would cause auto-complete to
508 ;; block. So we filter it uncompletely here, then let auto-complete filter
509 ;; the rest later, this would ease the feeling of being "stalled" at some degree.
510
511 ;; (message "saved prefix: %s" ac-clang-saved-prefix)
512 (with-current-buffer (process-buffer ac-clang-completion-process)
513 (erase-buffer))
514 (setq ac-clang-status 'wait)
515 (setq ac-clang-current-candidate nil)
516
517 ;; send completion request
518 (ac-clang-send-completion-request ac-clang-completion-process)
519 ac-clang-current-candidate)
520
521 (wait
522 ;; (message "ac-clang-candidate triggered - wait")
523 ac-clang-current-candidate)
524
525 (acknowledged
526 ;; (message "ac-clang-candidate triggered - ack")
527 (setq ac-clang-status 'idle)
528 ac-clang-current-candidate)
529
530 (preempted
531 ;; (message "clang-async is preempted by a critical request")
532 nil)))
677ffdc @Golevka first commit
Golevka authored
533
534
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
535 ;; Syntax checking with flymake
dbb04e1 @Golevka flymake worked, but have no idea of migrating syntax check with code …
Golevka authored
536
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
537 (defun ac-clang-flymake-process-sentinel ()
dbb04e1 @Golevka flymake worked, but have no idea of migrating syntax check with code …
Golevka authored
538 (interactive)
da183af @Golevka code cleaned up
Golevka authored
539 (setq flymake-err-info flymake-new-err-info)
540 (setq flymake-new-err-info nil)
541 (setq flymake-err-info
542 (flymake-fix-line-numbers
543 flymake-err-info 1 (flymake-count-lines)))
544 (flymake-delete-own-overlays)
545 (flymake-highlight-err-lines flymake-err-info))
dbb04e1 @Golevka flymake worked, but have no idea of migrating syntax check with code …
Golevka authored
546
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
547 (defun ac-clang-flymake-process-filter (process output)
548 (ac-clang-append-process-output-to-process-buffer process output)
549 (flymake-log 3 "received %d byte(s) of output from process %d"
550 (length output) (process-id process))
551 (flymake-parse-output-and-residual output)
552 (when (string= (substring output -1 nil) "$")
553 (flymake-parse-residual)
554 (ac-clang-flymake-process-sentinel)
555 (setq ac-clang-status 'idle)
556 (set-process-filter ac-clang-completion-process 'ac-clang-filter-output)))
da183af @Golevka code cleaned up
Golevka authored
557
558 (defun ac-clang-syntax-check ()
559 (interactive)
560 (when (eq ac-clang-status 'idle)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
561 (with-current-buffer (process-buffer ac-clang-completion-process)
562 (erase-buffer))
da183af @Golevka code cleaned up
Golevka authored
563 (setq ac-clang-status 'wait)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
564 (set-process-filter ac-clang-completion-process 'ac-clang-flymake-process-filter)
565 (ac-clang-send-syntaxcheck-request ac-clang-completion-process)))
dbb04e1 @Golevka flymake worked, but have no idea of migrating syntax check with code …
Golevka authored
566
567
568
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
569 (defun ac-clang-shutdown-process ()
570 (if ac-clang-completion-process
571 (ac-clang-send-shutdown-command ac-clang-completion-process)))
572
573 (defun ac-clang-reparse-buffer ()
574 (if ac-clang-completion-process
575 (ac-clang-send-reparse-request ac-clang-completion-process)))
576
577 (defun ac-clang-async-preemptive ()
578 (interactive)
579 (self-insert-command 1)
580 (if (eq ac-clang-status 'idle)
581 (ac-start)
582 (setq ac-clang-status 'preempted)))
583
584 (defun ac-clang-launch-completion-process ()
585 (setq ac-clang-completion-process
586 (let ((process-connection-type nil))
587 (apply 'start-process
588 "clang-complete" "*clang-complete*"
589 ac-clang-complete-executable
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
590 (append (ac-clang-build-complete-args)
677ffdc @Golevka first commit
Golevka authored
591 (list (buffer-file-name))))))
592
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
593 (set-process-filter ac-clang-completion-process 'ac-clang-filter-output)
96116ed @TaylanUB Fixed some stupid mistakes that sneaked in while mering manually. Thi…
TaylanUB authored
594 (set-process-query-on-exit-flag ac-clang-completion-process nil)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
595 ;; Pre-parse source code.
596 (ac-clang-send-reparse-request ac-clang-completion-process)
597
598 (add-hook 'kill-buffer-hook 'ac-clang-shutdown-process nil t)
599 (add-hook 'before-save-hook 'ac-clang-reparse-buffer)
677ffdc @Golevka first commit
Golevka authored
600
601 (local-set-key (kbd ".") 'ac-clang-async-preemptive)
602 (local-set-key (kbd ":") 'ac-clang-async-preemptive)
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
603 (local-set-key (kbd ">") 'ac-clang-async-preemptive))
677ffdc @Golevka first commit
Golevka authored
604
605
606 (ac-define-source clang-async
607 '((candidates . ac-clang-candidate)
608 (candidate-face . ac-clang-candidate-face)
609 (selection-face . ac-clang-selection-face)
610 (prefix . ac-clang-prefix)
611 (requires . 0)
612 (document . ac-clang-document)
613 (action . ac-clang-action)
614 (cache)
615 (symbol . "c")))
616
3176ea3 @TaylanUB Giant cleanup. Manually merge Yoshihoro Hara's auto-complete-clang br…
TaylanUB authored
617 ;;; auto-complete-clang-async.el ends here
Something went wrong with that request. Please try again.