]> code.delx.au - gnu-emacs-elpa/blob - company-clang.el
Merge changes from the GNU ELPA repository
[gnu-emacs-elpa] / company-clang.el
1 ;;; company-clang.el --- A company-mode completion back-end for clang
2
3 ;; Copyright (C) 2009, 2011 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 (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 (defun company-clang--parse-output (prefix)
109 (goto-char (point-min))
110 (let ((pattern (format company-clang--completion-pattern
111 (regexp-quote prefix)))
112 (case-fold-search nil)
113 lines match)
114 (while (re-search-forward pattern nil t)
115 (setq match (match-string-no-properties 1))
116 (unless (equal match "Pattern")
117 (push match lines)))
118 lines))
119
120 (defun company-clang--handle-error (res args)
121 (goto-char (point-min))
122 (let* ((buf (get-buffer-create company-clang--error-buffer-name))
123 (cmd (concat company-clang-executable (mapconcat 'identity args " ")))
124 (pattern (format company-clang--completion-pattern ""))
125 (err (if (re-search-forward pattern nil t)
126 (buffer-substring-no-properties (point-min)
127 (1- (match-beginning 0)))
128 ;; Warn the user more agressively if no match was found.
129 (message "clang failed with error %d:\n%s" res cmd)
130 (buffer-string))))
131
132 (with-current-buffer buf
133 (let ((inhibit-read-only t))
134 (erase-buffer)
135 (insert (current-time-string)
136 (format "\nclang failed with error %d:\n" res)
137 cmd "\n\n")
138 (insert err)
139 (setq buffer-read-only t)
140 (goto-char (point-min))))))
141
142 (defun company-clang--call-process (prefix &rest args)
143 (with-temp-buffer
144 (let ((res (apply 'call-process company-clang-executable nil t nil args)))
145 (unless (eq 0 res)
146 (company-clang--handle-error res args))
147 ;; Still try to get any useful input.
148 (company-clang--parse-output prefix))))
149
150 (defsubst company-clang--build-location (pos)
151 (save-excursion
152 (goto-char pos)
153 (format "%s:%d:%d" buffer-file-name (line-number-at-pos)
154 (1+ (current-column)))))
155
156 (defsubst company-clang--build-complete-args (pos)
157 (append '("-cc1" "-fsyntax-only" "-code-completion-macros")
158 company-clang-arguments
159 (when (stringp company-clang--prefix)
160 (list "-include" (expand-file-name company-clang--prefix)))
161 '("-code-completion-at")
162 (list (company-clang--build-location pos))
163 (list buffer-file-name)))
164
165 (defun company-clang--candidates (prefix)
166 (and company-clang-auto-save
167 (buffer-modified-p)
168 (basic-save-buffer))
169 (when (null company-clang--prefix)
170 (company-clang-set-prefix (or (funcall company-clang-prefix-guesser)
171 'none)))
172 (apply 'company-clang--call-process
173 prefix
174 (company-clang--build-complete-args (- (point) (length prefix)))))
175
176 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
177
178 (defconst company-clang-required-version "1.1")
179
180 (defsubst company-clang-version ()
181 "Return the version of `company-clang-executable'."
182 (with-temp-buffer
183 (switch-to-buffer (current-buffer))
184 (call-process company-clang-executable nil t nil "--version")
185 (goto-char (point-min))
186 (if (re-search-forward "\\clang version \\([0-9.]+\\)" nil t)
187 (match-string-no-properties 1)
188 "0")))
189
190 (defun company-clang-objc-templatify (selector)
191 (let* ((end (point))
192 (beg (- (point) (length selector)))
193 (templ (company-template-declare-template beg end)))
194 (save-excursion
195 (goto-char beg)
196 (while (search-forward ":" end t)
197 (replace-match ": ")
198 (incf end 2)
199 (company-template-add-field templ (1- (match-end 0)) "<arg>"))
200 (delete-char -1))
201 (company-template-move-to-first templ)))
202
203 (defun company-clang (command &optional arg &rest ignored)
204 "A `company-mode' completion back-end for clang.
205 Clang is a parser for C and ObjC. The unreleased development version of
206 clang (1.1) is required.
207
208 Additional command line arguments can be specified in
209 `company-clang-arguments'. Prefix files (-include ...) can be selected
210 with `company-clang-set-prefix' or automatically through a custom
211 `company-clang-prefix-guesser'.
212
213 Completions only work correctly when the buffer has been saved.
214 `company-clang-auto-save' determines whether to do this automatically."
215 (interactive (list 'interactive))
216 (case command
217 (interactive (company-begin-backend 'company-clang))
218 (init (unless company-clang-executable
219 (error "Company found no clang executable"))
220 (when (version< (company-clang-version)
221 company-clang-required-version)
222 (error "Company requires clang version 1.1")))
223 (prefix (and (memq major-mode company-clang-modes)
224 buffer-file-name
225 company-clang-executable
226 (not (company-in-string-or-comment))
227 (or (company-grab-symbol) 'stop)))
228 (candidates (company-clang--candidates arg))
229 (post-completion (and (derived-mode-p 'objc-mode)
230 (string-match ":" arg)
231 (company-clang-objc-templatify arg)))))
232
233 (provide 'company-clang)
234 ;;; company-clang.el ends here