]> code.delx.au - gnu-emacs-elpa/blob - company-clang.el
Separate executable from arguments in the error output buffer
[gnu-emacs-elpa] / company-clang.el
1 ;;; company-clang.el --- company-mode completion back-end for Clang
2
3 ;; Copyright (C) 2009, 2011, 2013 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22
23 ;;; Commentary:
24 ;;
25
26 ;;; Code:
27
28 (require 'company)
29 (require 'company-template)
30 (eval-when-compile (require 'cl))
31
32 (defgroup company-clang nil
33 "Completion back-end for Clang."
34 :group 'company)
35
36 (defcustom company-clang-executable
37 (executable-find "clang")
38 "Location of clang executable."
39 :type 'file)
40
41 (defcustom company-clang-begin-after-member-access t
42 "When non-nil, automatic completion will start whenever the current
43 symbol is preceded by \".\", \"->\" or \"::\", ignoring
44 `company-minimum-prefix-length'.
45
46 If `company-begin-commands' is a list, it should include `c-electric-lt-gt'
47 and `c-electric-colon', for automatic completion right after \">\" and
48 \":\".")
49
50 (defcustom company-clang-arguments nil
51 "Additional arguments to pass to clang when completing.
52 Prefix files (-include ...) can be selected with `company-clang-set-prefix'
53 or automatically through a custom `company-clang-prefix-guesser'."
54 :type '(repeat (string :tag "Argument" nil)))
55
56 (defcustom company-clang-prefix-guesser 'company-clang-guess-prefix
57 "A function to determine the prefix file for the current buffer."
58 :type '(function :tag "Guesser function" nil))
59
60 (defvar company-clang-modes '(c-mode c++-mode objc-mode)
61 "Major modes which clang may complete.")
62
63 ;; prefix ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
64
65 (defvar company-clang--prefix nil)
66
67 (defsubst company-clang--guess-pch-file (file)
68 (let ((dir (directory-file-name (file-name-directory file))))
69 (when (equal (file-name-nondirectory dir) "Classes")
70 (setq dir (file-name-directory dir)))
71 (car (directory-files dir t "\\([^.]h\\|[^h]\\).pch\\'" t))))
72
73 (defsubst company-clang--file-substring (file beg end)
74 (with-temp-buffer
75 (insert-file-contents-literally file nil beg end)
76 (buffer-string)))
77
78 (defun company-clang-guess-prefix ()
79 "Try to guess the prefix file for the current buffer."
80 ;; Prefixes seem to be called .pch. Pre-compiled headers do, too.
81 ;; So we look at the magic number to rule them out.
82 (let* ((file (company-clang--guess-pch-file buffer-file-name))
83 (magic-number (and file (company-clang--file-substring file 0 4))))
84 (unless (member magic-number '("CPCH" "gpch"))
85 file)))
86
87 (defun company-clang-set-prefix (&optional prefix)
88 "Use PREFIX as a prefix (-include ...) file for clang completion."
89 (interactive (let ((def (funcall company-clang-prefix-guesser)))
90 (unless (stringp def)
91 (setq def default-directory))
92 (list (read-file-name "Prefix file: "
93 (when def (file-name-directory def))
94 def t (when def (file-name-nondirectory def))))))
95 ;; TODO: pre-compile?
96 (setq company-clang--prefix (and (stringp prefix)
97 (file-regular-p prefix)
98 prefix)))
99
100 ;; Clean-up on exit.
101 (add-hook 'kill-emacs-hook 'company-clang-set-prefix)
102
103 ;; parsing ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
104
105 ;; TODO: Handle Pattern (syntactic hints would be neat).
106 ;; Do we ever see OVERLOAD (or OVERRIDE)?
107 (defconst company-clang--completion-pattern
108 "^COMPLETION: \\_<\\(%s[a-zA-Z0-9_:]*\\)\\(?: : \\(.*\\)$\\)?$")
109
110 (defconst company-clang--error-buffer-name "*clang error*")
111
112 (defvar company-clang--meta-cache nil)
113
114 (defun company-clang--lang-option ()
115 (if (eq major-mode 'objc-mode)
116 (if (string= "m" (file-name-extension buffer-file-name))
117 "objective-c" "objective-c++")
118 (substring (symbol-name major-mode) 0 -5)))
119
120 (defun company-clang--parse-output (prefix objc)
121 (goto-char (point-min))
122 (let ((pattern (format company-clang--completion-pattern
123 (regexp-quote prefix)))
124 (case-fold-search nil)
125 lines match)
126 (setq company-clang--meta-cache (make-hash-table :test 'equal))
127 (while (re-search-forward pattern nil t)
128 (setq match (match-string-no-properties 1))
129 (unless (equal match "Pattern")
130 (let ((meta (match-string-no-properties 2)))
131 (when (and meta (not (string= match meta)))
132 (setq meta (company-clang--strip-formatting meta))
133 (when (and (not objc) (string-match "\\((.*)\\)" meta))
134 (setq match (concat match (match-string 1 meta))))
135 (puthash match meta company-clang--meta-cache)))
136 (push match lines)))
137 lines))
138
139 (defun company-clang--strip-formatting (text)
140 (replace-regexp-in-string
141 "#]" " "
142 (replace-regexp-in-string "[<{[]#\\|#[>}]" "" text t)
143 t))
144
145 (defun company-clang--handle-error (res args)
146 (goto-char (point-min))
147 (let* ((buf (get-buffer-create company-clang--error-buffer-name))
148 (cmd (concat company-clang-executable " " (mapconcat 'identity args " ")))
149 (pattern (format company-clang--completion-pattern ""))
150 (err (if (re-search-forward pattern nil t)
151 (buffer-substring-no-properties (point-min)
152 (1- (match-beginning 0)))
153 ;; Warn the user more aggressively if no match was found.
154 (message "clang failed with error %d:\n%s" res cmd)
155 (buffer-string))))
156
157 (with-current-buffer buf
158 (let ((inhibit-read-only t))
159 (erase-buffer)
160 (insert (current-time-string)
161 (format "\nclang failed with error %d:\n" res)
162 cmd "\n\n")
163 (insert err)
164 (setq buffer-read-only t)
165 (goto-char (point-min))))))
166
167 (defun company-clang--call-process (prefix &rest args)
168 (let ((objc (derived-mode-p 'objc-mode))
169 (buf (get-buffer-create "*clang-output*"))
170 res)
171 (with-current-buffer buf (erase-buffer))
172 (setq res (if (company-clang--auto-save-p)
173 (apply 'call-process company-clang-executable nil buf nil args)
174 (apply 'call-process-region (point-min) (point-max)
175 company-clang-executable nil buf nil args)))
176 (with-current-buffer buf
177 (unless (eq 0 res)
178 (company-clang--handle-error res args))
179 ;; Still try to get any useful input.
180 (company-clang--parse-output prefix objc))))
181
182 (defsubst company-clang--build-location (pos)
183 (save-excursion
184 (goto-char pos)
185 (format "%s:%d:%d"
186 (if (company-clang--auto-save-p) buffer-file-name "-")
187 (line-number-at-pos)
188 (1+ (current-column)))))
189
190 (defsubst company-clang--build-complete-args (pos)
191 (append '("-cc1" "-fsyntax-only" "-code-completion-macros")
192 (unless (company-clang--auto-save-p)
193 (list "-x" (company-clang--lang-option)))
194 company-clang-arguments
195 (when (stringp company-clang--prefix)
196 (list "-include" (expand-file-name company-clang--prefix)))
197 '("-code-completion-at")
198 (list (company-clang--build-location pos))
199 (list (if (company-clang--auto-save-p) buffer-file-name "-"))))
200
201 (defun company-clang--candidates (prefix)
202 (and (company-clang--auto-save-p)
203 (buffer-modified-p)
204 (basic-save-buffer))
205 (when (null company-clang--prefix)
206 (company-clang-set-prefix (or (funcall company-clang-prefix-guesser)
207 'none)))
208 (apply 'company-clang--call-process
209 prefix
210 (company-clang--build-complete-args (- (point) (length prefix)))))
211
212 (defun company-clang--prefix ()
213 (let ((symbol (company-grab-symbol)))
214 (if symbol
215 (if (and company-clang-begin-after-member-access
216 (save-excursion
217 (forward-char (- (length symbol)))
218 (looking-back "\\.\\|->\\|::" (- (point) 2))))
219 (cons symbol t)
220 symbol)
221 'stop)))
222
223 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
224
225 (defconst company-clang-required-version 1.1)
226
227 (defvar company-clang--version nil)
228
229 (defun company-clang--auto-save-p ()
230 (< company-clang--version 2.9))
231
232 (defsubst company-clang-version ()
233 "Return the version of `company-clang-executable'."
234 (with-temp-buffer
235 (call-process company-clang-executable nil t nil "--version")
236 (goto-char (point-min))
237 (if (re-search-forward "clang\\(?: version \\|-\\)\\([0-9.]+\\)" nil t)
238 (let ((ver (string-to-number (match-string-no-properties 1))))
239 (if (> ver 100)
240 (/ ver 100)
241 ver))
242 0)))
243
244 (defun company-clang-objc-templatify (selector)
245 (let* ((end (point-marker))
246 (beg (- (point) (length selector)))
247 (templ (company-template-declare-template beg end))
248 (cnt 0))
249 (save-excursion
250 (goto-char beg)
251 (catch 'stop
252 (while (search-forward ":" end t)
253 (company-template-add-field templ (point) (format "arg%d" cnt))
254 (if (< (point) end)
255 (insert " ")
256 (throw 'stop t))
257 (incf cnt))))
258 (company-template-move-to-first templ)))
259
260 (defun company-clang (command &optional arg &rest ignored)
261 "`company-mode' completion back-end for Clang.
262 Clang is a parser for C and ObjC. Clang version 1.1 or newer is required.
263
264 Additional command line arguments can be specified in
265 `company-clang-arguments'. Prefix files (-include ...) can be selected
266 with `company-clang-set-prefix' or automatically through a custom
267 `company-clang-prefix-guesser'.
268
269 With Clang versions before 2.9, we have to save the buffer before
270 performing completion. With Clang 2.9 and later, buffer contents are
271 passed via standard input."
272 (interactive (list 'interactive))
273 (case command
274 (interactive (company-begin-backend 'company-clang))
275 (init (when (memq major-mode company-clang-modes)
276 (unless company-clang-executable
277 (error "Company found no clang executable"))
278 (setq company-clang--version (company-clang-version))
279 (when (< company-clang--version company-clang-required-version)
280 (error "Company requires clang version 1.1"))))
281 (prefix (and (memq major-mode company-clang-modes)
282 buffer-file-name
283 company-clang-executable
284 (not (company-in-string-or-comment))
285 (company-clang--prefix)))
286 (candidates (company-clang--candidates arg))
287 (meta (gethash arg company-clang--meta-cache))
288 (crop (and (string-match ":\\|(" arg)
289 (substring arg 0 (match-beginning 0))))
290 (post-completion (cond
291 ((not (derived-mode-p 'objc-mode))
292 (company-template-c-like-templatify arg))
293 ((string-match ":" arg)
294 (company-clang-objc-templatify arg))))))
295
296 (provide 'company-clang)
297 ;;; company-clang.el ends here