]> code.delx.au - gnu-emacs-elpa/blob - company-clang.el
company-clang docstring: Clang with capital C
[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-auto-save t
42 "Determines whether to save the buffer when retrieving completions.
43 clang can only complete correctly when the buffer has been saved."
44 :type '(choice (const :tag "Off" nil)
45 (const :tag "On" t)))
46
47 (defcustom company-clang-arguments nil
48 "Additional arguments to pass to clang when completing.
49 Prefix files (-include ...) can be selected with
50 `company-clang-set-prefix' or automatically through a custom
51 `company-clang-prefix-guesser'."
52 :type '(repeat (string :tag "Argument" nil)))
53
54 (defcustom company-clang-prefix-guesser 'company-clang-guess-prefix
55 "A function to determine the prefix file for the current buffer."
56 :type '(function :tag "Guesser function" nil))
57
58 (defvar company-clang-modes '(c-mode c++-mode objc-mode)
59 "Major modes which clang may complete.")
60
61 ;; prefix ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62
63 (defvar company-clang--prefix nil)
64
65 (defsubst company-clang--guess-pch-file (file)
66 (let ((dir (directory-file-name (file-name-directory file))))
67 (when (equal (file-name-nondirectory dir) "Classes")
68 (setq dir (file-name-directory dir)))
69 (car (directory-files dir t "\\([^.]h\\|[^h]\\).pch\\'" t))))
70
71 (defsubst company-clang--file-substring (file beg end)
72 (with-temp-buffer
73 (insert-file-contents-literally file nil beg end)
74 (buffer-string)))
75
76 (defun company-clang-guess-prefix ()
77 "Try to guess the prefix file for the current buffer."
78 ;; Prefixes seem to be called .pch. Pre-compiled headers do, too.
79 ;; So we look at the magic number to rule them out.
80 (let* ((file (company-clang--guess-pch-file buffer-file-name))
81 (magic-number (and file (company-clang--file-substring file 0 4))))
82 (unless (member magic-number '("CPCH" "gpch"))
83 file)))
84
85 (defun company-clang-set-prefix (&optional prefix)
86 "Use PREFIX as a prefix (-include ...) file for clang completion."
87 (interactive (let ((def (funcall company-clang-prefix-guesser)))
88 (unless (stringp def)
89 (setq def default-directory))
90 (list (read-file-name "Prefix file: "
91 (when def (file-name-directory def))
92 def t (when def (file-name-nondirectory def))))))
93 ;; TODO: pre-compile?
94 (setq company-clang--prefix (and (stringp prefix)
95 (file-regular-p prefix)
96 prefix)))
97
98 ;; Clean-up on exit.
99 (add-hook 'kill-emacs-hook 'company-clang-set-prefix)
100
101 ;; parsing ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102
103 ;; TODO: Handle Pattern (syntactic hints would be neat).
104 ;; Do we ever see OVERLOAD (or OVERRIDE)?
105 (defconst company-clang--completion-pattern
106 "^COMPLETION: \\_<\\(%s[a-zA-Z0-9_:]*\\)\\(?: : \\(.*\\)$\\)?$")
107
108 (defconst company-clang--error-buffer-name "*clang error*")
109
110 (defvar company-clang--meta-cache nil)
111
112 (defun company-clang--parse-output (prefix objc)
113 (goto-char (point-min))
114 (let ((pattern (format company-clang--completion-pattern
115 (regexp-quote prefix)))
116 (case-fold-search nil)
117 lines match)
118 (setq company-clang--meta-cache (make-hash-table :test 'equal))
119 (while (re-search-forward pattern nil t)
120 (setq match (match-string-no-properties 1))
121 (unless (equal match "Pattern")
122 (let ((meta (match-string-no-properties 2)))
123 (when (and meta (not (string= match meta)))
124 (setq meta (company-clang--strip-formatting meta))
125 (when (and (not objc) (string-match "\\((.*)\\)" meta))
126 (setq match (concat match (match-string 1 meta))))
127 (puthash match meta company-clang--meta-cache)))
128 (push match lines)))
129 lines))
130
131 (defun company-clang--strip-formatting (text)
132 (replace-regexp-in-string
133 "#]" " "
134 (replace-regexp-in-string "[<{[]#\\|#[>}]" "" text t)
135 t))
136
137 (defun company-clang--handle-error (res args)
138 (goto-char (point-min))
139 (let* ((buf (get-buffer-create company-clang--error-buffer-name))
140 (cmd (concat company-clang-executable (mapconcat 'identity args " ")))
141 (pattern (format company-clang--completion-pattern ""))
142 (err (if (re-search-forward pattern nil t)
143 (buffer-substring-no-properties (point-min)
144 (1- (match-beginning 0)))
145 ;; Warn the user more agressively if no match was found.
146 (message "clang failed with error %d:\n%s" res cmd)
147 (buffer-string))))
148
149 (with-current-buffer buf
150 (let ((inhibit-read-only t))
151 (erase-buffer)
152 (insert (current-time-string)
153 (format "\nclang failed with error %d:\n" res)
154 cmd "\n\n")
155 (insert err)
156 (setq buffer-read-only t)
157 (goto-char (point-min))))))
158
159 (defun company-clang--call-process (prefix &rest args)
160 (let ((objc (derived-mode-p 'objc-mode)))
161 (with-temp-buffer
162 (let ((res (apply 'call-process company-clang-executable nil t nil args)))
163 (unless (eq 0 res)
164 (company-clang--handle-error res args))
165 ;; Still try to get any useful input.
166 (company-clang--parse-output prefix objc)))))
167
168 (defsubst company-clang--build-location (pos)
169 (save-excursion
170 (goto-char pos)
171 (format "%s:%d:%d" buffer-file-name (line-number-at-pos)
172 (1+ (current-column)))))
173
174 (defsubst company-clang--build-complete-args (pos)
175 (append '("-cc1" "-fsyntax-only" "-code-completion-macros")
176 company-clang-arguments
177 (when (stringp company-clang--prefix)
178 (list "-include" (expand-file-name company-clang--prefix)))
179 '("-code-completion-at")
180 (list (company-clang--build-location pos))
181 (list buffer-file-name)))
182
183 (defun company-clang--candidates (prefix)
184 (and company-clang-auto-save
185 (buffer-modified-p)
186 (basic-save-buffer))
187 (when (null company-clang--prefix)
188 (company-clang-set-prefix (or (funcall company-clang-prefix-guesser)
189 'none)))
190 (apply 'company-clang--call-process
191 prefix
192 (company-clang--build-complete-args (- (point) (length prefix)))))
193
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
195
196 (defconst company-clang-required-version 1.1)
197
198 (defsubst company-clang-version ()
199 "Return the version of `company-clang-executable'."
200 (with-temp-buffer
201 (call-process company-clang-executable nil t nil "--version")
202 (goto-char (point-min))
203 (if (re-search-forward "clang\\(?: version \\|-\\)\\([0-9.]+\\)" nil t)
204 (let ((ver (string-to-number (match-string-no-properties 1))))
205 (if (> ver 100)
206 (/ ver 100)
207 ver))
208 0)))
209
210 (defun company-clang-objc-templatify (selector)
211 (let* ((end (point))
212 (beg (- (point) (length selector)))
213 (templ (company-template-declare-template beg end))
214 (cnt 0))
215 (save-excursion
216 (goto-char beg)
217 (while (search-forward ":" end t)
218 (let* ((name (format "arg%d" cnt))
219 (len (length name)))
220 (incf end len)
221 (let ((pt (point)))
222 (insert name)
223 (company-template-add-field templ pt (point)))
224 (when (< (point) end)
225 (insert " ")
226 (incf end))
227 (incf cnt))))
228 (company-template-move-to-first templ)))
229
230 (defun company-clang (command &optional arg &rest ignored)
231 "`company-mode' completion back-end for Clang.
232 Clang is a parser for C and ObjC. Clang version 1.1 or newer is required.
233
234 Additional command line arguments can be specified in
235 `company-clang-arguments'. Prefix files (-include ...) can be selected
236 with `company-clang-set-prefix' or automatically through a custom
237 `company-clang-prefix-guesser'.
238
239 Completions only work correctly when the buffer has been saved.
240 `company-clang-auto-save' determines whether to do this automatically."
241 (interactive (list 'interactive))
242 (case command
243 (interactive (company-begin-backend 'company-clang))
244 (init (when (memq major-mode company-clang-modes)
245 (unless company-clang-executable
246 (error "Company found no clang executable"))
247 (when (< (company-clang-version) company-clang-required-version)
248 (error "Company requires clang version 1.1"))))
249 (prefix (and (memq major-mode company-clang-modes)
250 buffer-file-name
251 company-clang-executable
252 (not (company-in-string-or-comment))
253 (or (company-grab-symbol) 'stop)))
254 (candidates (company-clang--candidates arg))
255 (meta (gethash arg company-clang--meta-cache))
256 (crop (and (string-match ":\\|(" arg)
257 (substring arg 0 (match-beginning 0))))
258 (post-completion (cond
259 ((not (derived-mode-p 'objc-mode))
260 (company-template-c-like-templatify arg))
261 ((string-match ":" arg)
262 (company-clang-objc-templatify arg))))))
263
264 (provide 'company-clang)
265 ;;; company-clang.el ends here