]> code.delx.au - gnu-emacs-elpa/blob - company-clang.el
Removed superfluous quotes.
[gnu-emacs-elpa] / company-clang.el
1 ;;; company-clang.el --- a company-mode completion back-end for clang
2 ;;
3 ;; Copyright (C) 2010 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.5.
6 ;;
7 ;; This program is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License
9 ;; as published by the Free Software Foundation; either version 2
10 ;; of the License, or (at your option) any later version.
11 ;;
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 (require 'company)
21 (eval-when-compile (require 'cl))
22
23 (defcustom company-clang-executable
24 (executable-find "clang")
25 "*Location of clang executable"
26 :group 'company-clang
27 :type 'file)
28
29 (defcustom company-clang-auto-save t
30 "*Determines whether to save the buffer when retrieving completions.
31 clang can only complete correctly when the buffer has been saved."
32 :group 'company-clang
33 :type '(choice (const :tag "Off" nil)
34 (const :tag "On" t)))
35
36 (defcustom company-clang-arguments nil
37 "*Additional arguments to pass to clang when completing.
38 Prefix files (-include ...) can be selected with
39 `company-clang-set-prefix' or automatically through a custom
40 `company-clang-prefix-guesser'."
41 :group 'company-clang
42 :type '(repeat (string :tag "Argument" nil)))
43
44 (defcustom company-clang-prefix-guesser 'company-clang-guess-prefix
45 "*A function to determine the prefix file for the current buffer."
46 :group 'company-clang
47 :type '(function :tag "Guesser function" nil))
48
49 (defvar company-clang-modes '(c-mode objc-mode)
50 "Major modes which clang may complete.")
51
52 ;; prefix ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
53
54 (defvar company-clang--prefix nil)
55
56 (defsubst company-clang--guess-pch-file (file)
57 (let ((dir (directory-file-name (file-name-directory file))))
58 (when (equal (file-name-nondirectory dir) "Classes")
59 (setq dir (file-name-directory dir)))
60 (car (directory-files dir t "\\([^.]h\\|[^h]\\).pch\\'" t))))
61
62 (defsubst company-clang--file-substring (file beg end)
63 (with-temp-buffer
64 (insert-file-contents-literally file nil beg end)
65 (buffer-string)))
66
67 (defun company-clang-guess-prefix ()
68 "Try to guess the prefix file for the current buffer."
69 ;; Prefixes seem to be called .pch. Pre-compiled headers do, too.
70 ;; So we look at the magic number to rule them out.
71 (let* ((file (company-clang--guess-pch-file buffer-file-name))
72 (magic-number (company-clang--file-substring file 0 4)))
73 (unless (member magic-number '("CPCH" "gpch"))
74 file)))
75
76 (defun company-clang-set-prefix (&optional prefix)
77 "Use PREFIX as a prefix (-include ...) file for clang completion."
78 (interactive (let ((def (funcall company-clang-prefix-guesser)))
79 (unless (stringp def)
80 (setq def default-directory))
81 (list (read-file-name "Prefix file: "
82 (when def (file-name-directory def))
83 def t (when def (file-name-nondirectory def))))))
84 ;; TODO: pre-compile?
85 (setq company-clang--prefix (and (stringp prefix)
86 (file-regular-p prefix)
87 prefix)))
88
89 ;; Clean-up on exit.
90 (add-hook 'kill-emacs-hook 'company-clang-set-prefix)
91
92 ;; parsing ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93
94 ;; TODO: How to handle OVERLOAD and Pattern?
95 (defconst company-clang--completion-pattern
96 "^COMPLETION: \\_<\\(%s[a-zA-Z0-9_:]*\\)")
97
98 (defconst company-clang--error-buffer-name "*clang error*")
99
100 (defun company-clang--parse-output (prefix)
101 (goto-char (point-min))
102 (let ((pattern (format company-clang--completion-pattern
103 (regexp-quote prefix)))
104 (case-fold-search nil)
105 lines match)
106 (while (re-search-forward pattern nil t)
107 (setq match (match-string-no-properties 1))
108 (unless (equal match "Pattern")
109 (push match lines)))
110 lines))
111
112 (defun company-clang--handle-error (res args)
113 (goto-char (point-min))
114 (let* ((buf (get-buffer-create company-clang--error-buffer-name))
115 (cmd (concat company-clang-executable (mapconcat 'identity args " ")))
116 (pattern (format company-clang--completion-pattern ""))
117 (err (if (re-search-forward pattern nil t)
118 (buffer-substring-no-properties (point-min)
119 (1- (match-beginning 0)))
120 ;; Warn the user more agressively if no match was found.
121 (message "clang failed with error %d:\n%s" res cmd)
122 (buffer-string))))
123
124 (with-current-buffer buf
125 (let ((inhibit-read-only t))
126 (erase-buffer)
127 (insert (current-time-string)
128 (format "\nclang failed with error %d:\n" res)
129 cmd "\n\n")
130 (insert err)
131 (setq buffer-read-only t)
132 (goto-char (point-min))))))
133
134 (defun company-clang--call-process (prefix &rest args)
135 (with-temp-buffer
136 (let ((res (apply 'call-process company-clang-executable nil t nil args)))
137 (unless (eq 0 res)
138 (company-clang--handle-error res args))
139 ;; Still try to get any useful input.
140 (company-clang--parse-output prefix))))
141
142 (defsubst company-clang--build-location (pos)
143 (save-excursion
144 (goto-char pos)
145 (format "%s:%d:%d" buffer-file-name (line-number-at-pos)
146 (1+ (current-column)))))
147
148 (defsubst company-clang--build-complete-args (pos)
149 (append '("-cc1" "-fsyntax-only" "-code-completion-macros")
150 company-clang-arguments
151 (when (stringp company-clang--prefix)
152 (list "-include" (expand-file-name company-clang--prefix)))
153 '("-code-completion-at")
154 (list (company-clang--build-location pos))
155 (list buffer-file-name)))
156
157 (defun company-clang--candidates (prefix)
158 (and company-clang-auto-save
159 (buffer-modified-p)
160 (basic-save-buffer))
161 (when (null company-clang--prefix)
162 (company-clang-set-prefix (or (funcall company-clang-prefix-guesser)
163 'none)))
164 (apply 'company-clang--call-process
165 prefix
166 (company-clang--build-complete-args (- (point) (length prefix)))))
167
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169
170 (defconst company-clang-required-version "1.1")
171
172 (defsubst company-clang-version ()
173 "Return the version of `company-clang-executable'."
174 (with-temp-buffer
175 (call-process company-clang-executable nil t nil "--version")
176 (goto-char (point-min))
177 (when (re-search-forward "\\`clang version \\([0-9.]+\\)" nil t)
178 (match-string-no-properties 1))))
179
180 (defun company-clang-objc-templatify (selector)
181 (let* ((end (point))
182 (beg (- (point) (length selector)))
183 (templ (company-template-declare-template beg end)))
184 (save-excursion
185 (goto-char beg)
186 (while (search-forward ":" end t)
187 (replace-match ": ")
188 (incf end 2)
189 (company-template-add-field templ (1- (match-end 0)) "<arg>"))
190 (delete-char -1))
191 (company-template-move-to-first templ)))
192
193 (defun company-clang (command &optional arg &rest ignored)
194 "A `company-mode' completion back-end for clang.
195 Clang is a parser for C and ObjC. The unreleased development version of
196 clang (1.1) is required.
197
198 Additional command line arguments can be specified in
199 `company-clang-arguments'. Prefix files (-include ...) can be selected
200 with `company-clang-set-prefix' or automatically through a custom
201 `company-clang-prefix-guesser'.
202
203 Completions only work correctly when the buffer has been saved.
204 `company-clang-auto-save' determines whether to do this automatically."
205 (interactive (list 'interactive))
206 (case command
207 (interactive (company-begin-backend 'company-clang))
208 (init (unless company-clang-executable
209 (error "Company found no clang executable"))
210 (when (version< (company-clang-version)
211 company-clang-required-version)
212 (error "Company requires clang version 1.1")))
213 (prefix (and (memq major-mode company-clang-modes)
214 buffer-file-name
215 company-clang-executable
216 (not (company-in-string-or-comment))
217 (or (company-grab-symbol) 'stop)))
218 (candidates (company-clang--candidates arg))
219 (post-completion (and (derived-mode-p 'objc-mode)
220 (string-match ":" arg)
221 (company-clang-objc-templatify arg)))))
222
223 (provide 'company-clang)
224 ;;; company-clang.el ends here