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