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