]> code.delx.au - gnu-emacs-elpa/blob - company-clang.el
Remove the As
[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 (defcustom company-clang-executable
33 (executable-find "clang")
34 "Location of clang executable."
35 :group 'company-clang
36 :type 'file)
37
38 (defcustom company-clang-auto-save t
39 "Determines whether to save the buffer when retrieving completions.
40 clang can only complete correctly when the buffer has been saved."
41 :group 'company-clang
42 :type '(choice (const :tag "Off" nil)
43 (const :tag "On" t)))
44
45 (defcustom company-clang-arguments nil
46 "Additional arguments to pass to clang when completing.
47 Prefix files (-include ...) can be selected with
48 `company-clang-set-prefix' or automatically through a custom
49 `company-clang-prefix-guesser'."
50 :group 'company-clang
51 :type '(repeat (string :tag "Argument" nil)))
52
53 (defcustom company-clang-prefix-guesser 'company-clang-guess-prefix
54 "A function to determine the prefix file for the current buffer."
55 :group 'company-clang
56 :type '(function :tag "Guesser function" nil))
57
58 (defvar company-clang-modes '(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: How to handle OVERLOAD and Pattern?
104 (defconst company-clang--completion-pattern
105 "^COMPLETION: \\_<\\(%s[a-zA-Z0-9_:]*\\)\\(?: : \\(.*\\)$\\)?")
106
107 (defconst company-clang--error-buffer-name "*clang error*")
108
109 (defvar company-clang--meta-cache nil)
110
111 (defun company-clang--parse-output (prefix)
112 (goto-char (point-min))
113 (let ((pattern (format company-clang--completion-pattern
114 (regexp-quote prefix)))
115 (case-fold-search nil)
116 lines match)
117 (setq company-clang--meta-cache (make-hash-table :test 'equal))
118 (while (re-search-forward pattern nil t)
119 (setq match (match-string-no-properties 1))
120 (let ((meta (match-string-no-properties 2)))
121 (when (and meta (not (string= match meta)))
122 (puthash match meta company-clang--meta-cache)))
123 (unless (equal match "Pattern")
124 (push match lines)))
125 lines))
126
127 (defun company-clang--handle-error (res args)
128 (goto-char (point-min))
129 (let* ((buf (get-buffer-create company-clang--error-buffer-name))
130 (cmd (concat company-clang-executable (mapconcat 'identity args " ")))
131 (pattern (format company-clang--completion-pattern ""))
132 (err (if (re-search-forward pattern nil t)
133 (buffer-substring-no-properties (point-min)
134 (1- (match-beginning 0)))
135 ;; Warn the user more agressively if no match was found.
136 (message "clang failed with error %d:\n%s" res cmd)
137 (buffer-string))))
138
139 (with-current-buffer buf
140 (let ((inhibit-read-only t))
141 (erase-buffer)
142 (insert (current-time-string)
143 (format "\nclang failed with error %d:\n" res)
144 cmd "\n\n")
145 (insert err)
146 (setq buffer-read-only t)
147 (goto-char (point-min))))))
148
149 (defun company-clang--call-process (prefix &rest args)
150 (with-temp-buffer
151 (let ((res (apply 'call-process company-clang-executable nil t nil args)))
152 (unless (eq 0 res)
153 (company-clang--handle-error res args))
154 ;; Still try to get any useful input.
155 (company-clang--parse-output prefix))))
156
157 (defsubst company-clang--build-location (pos)
158 (save-excursion
159 (goto-char pos)
160 (format "%s:%d:%d" buffer-file-name (line-number-at-pos)
161 (1+ (current-column)))))
162
163 (defsubst company-clang--build-complete-args (pos)
164 (append '("-cc1" "-fsyntax-only" "-code-completion-macros")
165 company-clang-arguments
166 (when (stringp company-clang--prefix)
167 (list "-include" (expand-file-name company-clang--prefix)))
168 '("-code-completion-at")
169 (list (company-clang--build-location pos))
170 (list buffer-file-name)))
171
172 (defun company-clang--candidates (prefix)
173 (and company-clang-auto-save
174 (buffer-modified-p)
175 (basic-save-buffer))
176 (when (null company-clang--prefix)
177 (company-clang-set-prefix (or (funcall company-clang-prefix-guesser)
178 'none)))
179 (apply 'company-clang--call-process
180 prefix
181 (company-clang--build-complete-args (- (point) (length prefix)))))
182
183 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
184
185 (defconst company-clang-required-version 1.1)
186
187 (defsubst company-clang-version ()
188 "Return the version of `company-clang-executable'."
189 (with-temp-buffer
190 (call-process company-clang-executable nil t nil "--version")
191 (goto-char (point-min))
192 (if (re-search-forward "clang\\(?: version \\|-\\)\\([0-9.]+\\)" nil t)
193 (let ((ver (string-to-number (match-string-no-properties 1))))
194 (if (> ver 100)
195 (/ ver 100)
196 ver))
197 0)))
198
199 (defun company-clang-objc-templatify (selector)
200 (let* ((end (point))
201 (beg (- (point) (length selector)))
202 (templ (company-template-declare-template beg end))
203 (cnt 0))
204 (save-excursion
205 (goto-char beg)
206 (while (search-forward ":" end t)
207 (let* ((name (format "arg%d" cnt))
208 (len (length name)))
209 (incf end len)
210 (company-template-add-field templ (match-end 0) name)
211 (goto-char (+ (match-end 0) len))
212 (when (< (point) end)
213 (insert " ")
214 (incf end))
215 (incf cnt))))
216 (company-template-move-to-first templ)))
217
218 (defun company-clang (command &optional arg &rest ignored)
219 "`company-mode' completion back-end for clang.
220 Clang is a parser for C and ObjC. Clang version 1.1 or newer is required.
221
222 Additional command line arguments can be specified in
223 `company-clang-arguments'. Prefix files (-include ...) can be selected
224 with `company-clang-set-prefix' or automatically through a custom
225 `company-clang-prefix-guesser'.
226
227 Completions only work correctly when the buffer has been saved.
228 `company-clang-auto-save' determines whether to do this automatically."
229 (interactive (list 'interactive))
230 (case command
231 (interactive (company-begin-backend 'company-clang))
232 (init (when (memq major-mode company-clang-modes)
233 (unless company-clang-executable
234 (error "Company found no clang executable"))
235 (when (< (company-clang-version) company-clang-required-version)
236 (error "Company requires clang version 1.1"))))
237 (prefix (and (memq major-mode company-clang-modes)
238 buffer-file-name
239 company-clang-executable
240 (not (company-in-string-or-comment))
241 (or (company-grab-symbol) 'stop)))
242 (candidates (company-clang--candidates arg))
243 (meta (let ((meta (gethash arg company-clang--meta-cache)))
244 (when meta
245 (replace-regexp-in-string
246 "#]" " "
247 (replace-regexp-in-string "[<{[]#\\|#[>}]" "" meta t)
248 t))))
249 (crop (and (derived-mode-p 'objc-mode)
250 (string-match ":" arg)
251 (substring arg 0 (match-beginning 0))))
252 (post-completion (and (derived-mode-p 'objc-mode)
253 (string-match ":" arg)
254 (company-clang-objc-templatify arg)))))
255
256 (provide 'company-clang)
257 ;;; company-clang.el ends here