]> code.delx.au - gnu-emacs-elpa/blob - company.el
Added company-pysmell.
[gnu-emacs-elpa] / company.el
1 ;;; company.el --- extensible inline text completion mechanism
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; Author: Nikolaj Schumacher <bugs * nschum de>
6 ;; Version: 0.4.2
7 ;; Keywords: abbrev, convenience, matchis
8 ;; URL: http://nschum.de/src/emacs/company/
9 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
10 ;;
11 ;; This file is NOT part of GNU Emacs.
12 ;;
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License
15 ;; as published by the Free Software Foundation; either version 2
16 ;; of the License, or (at your option) any later version.
17 ;;
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22 ;;
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ;;
26 ;;; Commentary:
27 ;;
28 ;; Company is a modular completion mechanism. Modules for retrieving completion
29 ;; candidates are called back-ends, modules for displaying them are front-ends.
30 ;;
31 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
32 ;; distributed in individual files and can be used individually.
33 ;;
34 ;; Place company.el and the back-ends you want to use in a directory and add the
35 ;; following to your .emacs:
36 ;; (add-to-list 'load-path "/path/to/company")
37 ;; (autoload 'company-mode "company" nil t)
38 ;;
39 ;; Enable company-mode with M-x company-mode. For further information look at
40 ;; the documentation for `company-mode' (C-h f company-mode RET)
41 ;;
42 ;; If you want to start a specific back-end, call it interactively or use
43 ;; `company-begin-backend'. For example:
44 ;; M-x company-abbrev will prompt for and insert an abbrev.
45 ;;
46 ;; To write your own back-end, look at the documentation for `company-backends'.
47 ;; Here is a simple example completing "foo":
48 ;;
49 ;; (defun company-my-backend (command &optional arg &rest ignored)
50 ;; (case command
51 ;; ('prefix (when (looking-back "foo\\>")
52 ;; (match-string 0)))
53 ;; ('candidates (list "foobar" "foobaz" "foobarbaz"))
54 ;; ('meta (format "This value is named %s" arg))))
55 ;;
56 ;; Sometimes it is a good idea to mix two back-ends together, for example to
57 ;; enrich gtags with dabbrev-code results (to emulate local variables):
58 ;; To do this, add a list with the merged back-ends as an element in
59 ;; company-backends.
60 ;;
61 ;; Known Issues:
62 ;; When point is at the very end of the buffer, the pseudo-tooltip appears very
63 ;; wrong, unless company is allowed to temporarily insert a fake newline.
64 ;; This behavior is enabled by `company-end-of-buffer-workaround'.
65 ;;
66 ;;; Change Log:
67 ;;
68 ;; Added `company-pysmell' back-end.
69 ;;
70 ;; 2009-04-25 (0.4.2)
71 ;; In C modes . and -> now count towards `company-minimum-prefix-length'.
72 ;; Reverted default front-end back to `company-preview-if-just-one-frontend'.
73 ;; The pseudo tooltip will no longer be clipped at the right window edge.
74 ;; Added `company-tooltip-minimum'.
75 ;; Windows compatibility fixes.
76 ;;
77 ;; 2009-04-19 (0.4.1)
78 ;; Added `global-company-mode'.
79 ;; Performance enhancements.
80 ;; Added `company-eclim' back-end.
81 ;; Added safer workaround for Emacs `posn-col-row' bug.
82 ;;
83 ;; 2009-04-18 (0.4)
84 ;; Automatic completion is now aborted if the prefix gets too short.
85 ;; Added option `company-dabbrev-time-limit'.
86 ;; `company-backends' now supports merging back-ends.
87 ;; Added back-end `company-dabbrev-code' for generic code.
88 ;; Fixed `company-begin-with'.
89 ;;
90 ;; 2009-04-15 (0.3.1)
91 ;; Added 'stop prefix to prevent dabbrev from completing inside of symbols.
92 ;; Fixed issues with tabbar-mode and line-spacing.
93 ;; Performance enhancements.
94 ;;
95 ;; 2009-04-12 (0.3)
96 ;; Added `company-begin-commands' option.
97 ;; Added abbrev, tempo and Xcode back-ends.
98 ;; Back-ends are now interactive. You can start them with M-x backend-name.
99 ;; Added `company-begin-with' for starting company from elisp-code.
100 ;; Added hooks.
101 ;; Added `company-require-match' and `company-auto-complete' options.
102 ;;
103 ;; 2009-04-05 (0.2.1)
104 ;; Improved Emacs Lisp back-end behavior for local variables.
105 ;; Added `company-elisp-detect-function-context' option.
106 ;; The mouse can now be used for selection.
107 ;;
108 ;; 2009-03-22 (0.2)
109 ;; Added `company-show-location'.
110 ;; Added etags back-end.
111 ;; Added work-around for end-of-buffer bug.
112 ;; Added `company-filter-candidates'.
113 ;; More local Lisp variables are now included in the candidates.
114 ;;
115 ;; 2009-03-21 (0.1.5)
116 ;; Fixed elisp documentation buffer always showing the same doc.
117 ;; Added `company-echo-strip-common-frontend'.
118 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
119 ;; Don't hide the echo message if it isn't shown.
120 ;;
121 ;; 2009-03-20 (0.1)
122 ;; Initial release.
123 ;;
124 ;;; Code:
125
126 (eval-when-compile (require 'cl))
127
128 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
129 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
130 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
131 (add-to-list 'debug-ignored-errors "^Company not ")
132 (add-to-list 'debug-ignored-errors "^No candidate number ")
133 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
134
135 (defgroup company nil
136 "Extensible inline text completion mechanism"
137 :group 'abbrev
138 :group 'convenience
139 :group 'maching)
140
141 (defface company-tooltip
142 '((t :background "yellow"
143 :foreground "black"))
144 "*Face used for the tool tip."
145 :group 'company)
146
147 (defface company-tooltip-selection
148 '((default :inherit company-tooltip)
149 (((class color) (min-colors 88)) (:background "orange1"))
150 (t (:background "green")))
151 "*Face used for the selection in the tool tip."
152 :group 'company)
153
154 (defface company-tooltip-mouse
155 '((default :inherit highlight))
156 "*Face used for the tool tip item under the mouse."
157 :group 'company)
158
159 (defface company-tooltip-common
160 '((t :inherit company-tooltip
161 :foreground "red"))
162 "*Face used for the common completion in the tool tip."
163 :group 'company)
164
165 (defface company-tooltip-common-selection
166 '((t :inherit company-tooltip-selection
167 :foreground "red"))
168 "*Face used for the selected common completion in the tool tip."
169 :group 'company)
170
171 (defface company-preview
172 '((t :background "blue4"
173 :foreground "wheat"))
174 "*Face used for the completion preview."
175 :group 'company)
176
177 (defface company-preview-common
178 '((t :inherit company-preview
179 :foreground "red"))
180 "*Face used for the common part of the completion preview."
181 :group 'company)
182
183 (defface company-preview-search
184 '((t :inherit company-preview
185 :background "blue1"))
186 "*Face used for the search string in the completion preview."
187 :group 'company)
188
189 (defface company-echo nil
190 "*Face used for completions in the echo area."
191 :group 'company)
192
193 (defface company-echo-common
194 '((((background dark)) (:foreground "firebrick1"))
195 (((background light)) (:background "firebrick4")))
196 "*Face used for the common part of completions in the echo area."
197 :group 'company)
198
199 (defun company-frontends-set (variable value)
200 ;; uniquify
201 (let ((remainder value))
202 (setcdr remainder (delq (car remainder) (cdr remainder))))
203 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
204 (memq 'company-pseudo-tooltip-frontend value)
205 (error "Pseudo tooltip frontend cannot be used twice"))
206 (and (memq 'company-preview-if-just-one-frontend value)
207 (memq 'company-preview-frontend value)
208 (error "Preview frontend cannot be used twice"))
209 (and (memq 'company-echo value)
210 (memq 'company-echo-metadata-frontend value)
211 (error "Echo area cannot be used twice"))
212 ;; preview must come last
213 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
214 (when (memq f value)
215 (setq value (append (delq f value) (list f)))))
216 (set variable value))
217
218 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
219 company-preview-if-just-one-frontend
220 company-echo-metadata-frontend)
221 "*The list of active front-ends (visualizations).
222 Each front-end is a function that takes one argument. It is called with
223 one of the following arguments:
224
225 'show: When the visualization should start.
226
227 'hide: When the visualization should end.
228
229 'update: When the data has been updated.
230
231 'pre-command: Before every command that is executed while the
232 visualization is active.
233
234 'post-command: After every command that is executed while the
235 visualization is active.
236
237 The visualized data is stored in `company-prefix', `company-candidates',
238 `company-common', `company-selection', `company-point' and
239 `company-search-string'."
240 :set 'company-frontends-set
241 :group 'company
242 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
243 (const :tag "echo, strip common"
244 company-echo-strip-common-frontend)
245 (const :tag "show echo meta-data in echo"
246 company-echo-metadata-frontend)
247 (const :tag "pseudo tooltip"
248 company-pseudo-tooltip-frontend)
249 (const :tag "pseudo tooltip, multiple only"
250 company-pseudo-tooltip-unless-just-one-frontend)
251 (const :tag "preview" company-preview-frontend)
252 (const :tag "preview, unique only"
253 company-preview-if-just-one-frontend)
254 (function :tag "custom function" nil))))
255
256 (defcustom company-tooltip-limit 10
257 "*The maximum number of candidates in the tool tip"
258 :group 'company
259 :type 'integer)
260
261 (defcustom company-tooltip-minimum 6
262 "*The minimum height of the tool tip.
263 If this many lines are not available, prefer to display the tooltip above."
264 :group 'company
265 :type 'integer)
266
267 (defvar company-safe-backends
268 '((company-abbrev . "Abbrev")
269 (company-css . "CSS")
270 (company-dabbrev . "dabbrev for plain text")
271 (company-dabbrev-code . "dabbrev for code")
272 (company-eclim . "eclim (an Eclipse interace)")
273 (company-elisp . "Emacs Lisp")
274 (company-etags . "etags")
275 (company-files . "Files")
276 (company-gtags . "GNU Global")
277 (company-ispell . "ispell")
278 (company-keywords . "Programming language keywords")
279 (company-nxml . "nxml")
280 (company-oddmuse . "Oddmuse")
281 (company-pysmell . "PySmell")
282 (company-semantic . "CEDET Semantic")
283 (company-tempo . "Tempo templates")
284 (company-xcode . "Xcode")))
285 (put 'company-safe-backends 'risky-local-variable t)
286
287 (defun company-safe-backends-p (backends)
288 (and (consp backends)
289 (not (dolist (backend backends)
290 (unless (if (consp backend)
291 (company-safe-backends-p backend)
292 (assq backend company-safe-backends))
293 (return t))))))
294
295 (defcustom company-backends '(company-elisp company-nxml company-css
296 company-eclim company-semantic company-xcode
297 (company-gtags company-etags company-dabbrev-code
298 company-pysmell company-keywords)
299 company-oddmuse company-files company-dabbrev)
300 "*The list of active back-ends (completion engines).
301 Each list elements can itself be a list of back-ends. In that case their
302 completions are merged. Otherwise only the first matching back-end returns
303 results.
304
305 Each back-end is a function that takes a variable number of arguments.
306 The first argument is the command requested from the back-end. It is one
307 of the following:
308
309 'prefix: The back-end should return the text to be completed. It must be
310 text immediately before `point'. Returning nil passes control to the next
311 back-end. The function should return 'stop if it should complete but cannot
312 \(e.g. if it is in the middle of a string\). If the returned value is only
313 part of the prefix (e.g. the part after \"->\" in C), the back-end may return a
314 cons of prefix and prefix length, which is then used in the
315 `company-minimum-prefix-length' test.
316
317 'candidates: The second argument is the prefix to be completed. The
318 return value should be a list of candidates that start with the prefix.
319
320 Optional commands:
321
322 'sorted: The back-end may return t here to indicate that the candidates
323 are sorted and will not need to be sorted again.
324
325 'duplicates: If non-nil, company will take care of removing duplicates
326 from the list.
327
328 'no-cache: Usually company doesn't ask for candidates again as completion
329 progresses, unless the back-end returns t for this command. The second
330 argument is the latest prefix.
331
332 'meta: The second argument is a completion candidate. The back-end should
333 return a (short) documentation string for it.
334
335 'doc-buffer: The second argument is a completion candidate. The back-end should
336 create a buffer (preferably with `company-doc-buffer'), fill it with
337 documentation and return it.
338
339 'location: The second argument is a completion candidate. The back-end can
340 return the cons of buffer and buffer location, or of file and line
341 number where the completion candidate was defined.
342
343 'require-match: If this value is t, the user is not allowed to enter anything
344 not offering as a candidate. Use with care! The default value nil gives the
345 user that choice with `company-require-match'. Return value 'never overrides
346 that option the other way around.
347
348 The back-end should return nil for all commands it does not support or
349 does not know about. It should also be callable interactively and use
350 `company-begin-backend' to start itself in that case."
351 :group 'company
352 :type `(repeat
353 (choice
354 :tag "Back-end"
355 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
356 company-safe-backends)
357 (symbol :tag "User defined")
358 (repeat :tag "Merged Back-ends"
359 (choice :tag "Back-end"
360 ,@(mapcar (lambda (b)
361 `(const :tag ,(cdr b) ,(car b)))
362 company-safe-backends)
363 (symbol :tag "User defined"))))))
364
365 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
366
367 (defcustom company-completion-started-hook nil
368 "*Hook run when company starts completing.
369 The hook is called with one argument that is non-nil if the completion was
370 started manually."
371 :group 'company
372 :type 'hook)
373
374 (defcustom company-completion-cancelled-hook nil
375 "*Hook run when company cancels completing.
376 The hook is called with one argument that is non-nil if the completion was
377 aborted manually."
378 :group 'company
379 :type 'hook)
380
381 (defcustom company-completion-finished-hook nil
382 "*Hook run when company successfully completes.
383 The hook is called with the selected candidate as an argument."
384 :group 'company
385 :type 'hook)
386
387 (defcustom company-minimum-prefix-length 3
388 "*The minimum prefix length for automatic completion."
389 :group 'company
390 :type '(integer :tag "prefix length"))
391
392 (defcustom company-require-match 'company-explicit-action-p
393 "*If enabled, disallow non-matching input.
394 This can be a function do determine if a match is required.
395
396 This can be overridden by the back-end, if it returns t or 'never to
397 'require-match. `company-auto-complete' also takes precedence over this."
398 :group 'company
399 :type '(choice (const :tag "Off" nil)
400 (function :tag "Predicate function")
401 (const :tag "On, if user interaction took place"
402 'company-explicit-action-p)
403 (const :tag "On" t)))
404
405 (defcustom company-auto-complete 'company-explicit-action-p
406 "Determines when to auto-complete.
407 If this is enabled, all characters from `company-auto-complete-chars' complete
408 the selected completion. This can also be a function."
409 :group 'company
410 :type '(choice (const :tag "Off" nil)
411 (function :tag "Predicate function")
412 (const :tag "On, if user interaction took place"
413 'company-explicit-action-p)
414 (const :tag "On" t)))
415
416 (defcustom company-auto-complete-chars '(?\ ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
417 "Determines which characters trigger an automatic completion.
418 See `company-auto-complete'. If this is a string, each string character causes
419 completion. If it is a list of syntax description characters (see
420 `modify-syntax-entry'), all characters with that syntax auto-complete.
421
422 This can also be a function, which is called with the new input and should
423 return non-nil if company should auto-complete.
424
425 A character that is part of a valid candidate never starts auto-completion."
426 :group 'company
427 :type '(choice (string :tag "Characters")
428 (set :tag "Syntax"
429 (const :tag "Whitespace" ?\ )
430 (const :tag "Symbol" ?_)
431 (const :tag "Opening parentheses" ?\()
432 (const :tag "Closing parentheses" ?\))
433 (const :tag "Word constituent" ?w)
434 (const :tag "Punctuation." ?.)
435 (const :tag "String quote." ?\")
436 (const :tag "Paired delimiter." ?$)
437 (const :tag "Expression quote or prefix operator." ?\')
438 (const :tag "Comment starter." ?<)
439 (const :tag "Comment ender." ?>)
440 (const :tag "Character-quote." ?/)
441 (const :tag "Generic string fence." ?|)
442 (const :tag "Generic comment fence." ?!))
443 (function :tag "Predicate function")))
444
445 (defcustom company-idle-delay .7
446 "*The idle delay in seconds until automatic completions starts.
447 A value of nil means never complete automatically, t means complete
448 immediately when a prefix of `company-minimum-prefix-length' is reached."
449 :group 'company
450 :type '(choice (const :tag "never (nil)" nil)
451 (const :tag "immediate (t)" t)
452 (number :tag "seconds")))
453
454 (defcustom company-begin-commands t
455 "*A list of commands following which company will start completing.
456 If this is t, it will complete after any command. See `company-idle-delay'.
457
458 Alternatively any command with a non-nil 'company-begin property is treated as
459 if it was on this list."
460 :group 'company
461 :type '(choice (const :tag "Any command" t)
462 (const :tag "Self insert command" '(self-insert-command))
463 (repeat :tag "Commands" function)))
464
465 (defcustom company-show-numbers nil
466 "*If enabled, show quick-access numbers for the first ten candidates."
467 :group 'company
468 :type '(choice (const :tag "off" nil)
469 (const :tag "on" t)))
470
471 (defvar company-end-of-buffer-workaround t
472 "*Work around a visualization bug when completing at the end of the buffer.
473 The work-around consists of adding a newline.")
474
475 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
476
477 (defvar company-mode-map (make-sparse-keymap)
478 "Keymap used by `company-mode'.")
479
480 (defvar company-active-map
481 (let ((keymap (make-sparse-keymap)))
482 (define-key keymap "\e\e\e" 'company-abort)
483 (define-key keymap "\C-g" 'company-abort)
484 (define-key keymap (kbd "M-n") 'company-select-next)
485 (define-key keymap (kbd "M-p") 'company-select-previous)
486 (define-key keymap (kbd "<down>") 'company-select-next)
487 (define-key keymap (kbd "<up>") 'company-select-previous)
488 (define-key keymap [down-mouse-1] 'ignore)
489 (define-key keymap [down-mouse-3] 'ignore)
490 (define-key keymap [mouse-1] 'company-complete-mouse)
491 (define-key keymap [mouse-3] 'company-select-mouse)
492 (define-key keymap [up-mouse-1] 'ignore)
493 (define-key keymap [up-mouse-3] 'ignore)
494 (define-key keymap "\C-m" 'company-complete-selection)
495 (define-key keymap "\t" 'company-complete-common)
496 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
497 (define-key keymap "\C-w" 'company-show-location)
498 (define-key keymap "\C-s" 'company-search-candidates)
499 (define-key keymap "\C-\M-s" 'company-filter-candidates)
500 (dotimes (i 10)
501 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
502 `(lambda () (interactive) (company-complete-number ,i))))
503
504 keymap)
505 "Keymap that is enabled during an active completion.")
506
507 (defvar company--disabled-backends nil)
508
509 (defun company-init-backend (backend)
510 (and (symbolp backend)
511 (not (fboundp backend))
512 (ignore-errors (require backend nil t)))
513
514 (if (or (symbolp backend)
515 (functionp backend))
516 (if (ignore-errors (funcall backend 'init) t)
517 (put backend 'company-init t)
518 (put backend 'company-init 'failed)
519 (unless (memq backend company--disabled-backends)
520 (message "Company back-end '%s' could not be initialized"
521 backend)
522 (push backend company--disabled-backends))
523 nil)
524 (mapc 'company-init-backend backend)))
525
526 (defvar company-default-lighter " company")
527
528 (defvar company-lighter company-default-lighter)
529 (make-variable-buffer-local 'company-lighter)
530
531 ;;;###autoload
532 (define-minor-mode company-mode
533 "\"complete anything\"; in in-buffer completion framework.
534 Completion starts automatically, depending on the values
535 `company-idle-delay' and `company-minimum-prefix-length'.
536
537 Completion can be controlled with the commands:
538 `company-complete-common', `company-complete-selection', `company-complete',
539 `company-select-next', `company-select-previous'. If these commands are
540 called before `company-idle-delay', completion will also start.
541
542 Completions can be searched with `company-search-candidates' or
543 `company-filter-candidates'. These can be used while completion is
544 inactive, as well.
545
546 The completion data is retrieved using `company-backends' and displayed using
547 `company-frontends'. If you want to start a specific back-end, call it
548 interactively or use `company-begin-backend'.
549
550 regular keymap (`company-mode-map'):
551
552 \\{company-mode-map}
553 keymap during active completions (`company-active-map'):
554
555 \\{company-active-map}"
556 nil company-lighter company-mode-map
557 (if company-mode
558 (progn
559 (add-hook 'pre-command-hook 'company-pre-command nil t)
560 (add-hook 'post-command-hook 'company-post-command nil t)
561 (mapc 'company-init-backend company-backends))
562 (remove-hook 'pre-command-hook 'company-pre-command t)
563 (remove-hook 'post-command-hook 'company-post-command t)
564 (company-cancel)
565 (kill-local-variable 'company-point)))
566
567 (define-globalized-minor-mode global-company-mode company-mode
568 (lambda () (company-mode 1)))
569
570 (defsubst company-assert-enabled ()
571 (unless company-mode
572 (company-uninstall-map)
573 (error "Company not enabled")))
574
575 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
576
577 (defvar company-overriding-keymap-bound nil)
578 (make-variable-buffer-local 'company-overriding-keymap-bound)
579
580 (defvar company-old-keymap nil)
581 (make-variable-buffer-local 'company-old-keymap)
582
583 (defvar company-my-keymap nil)
584 (make-variable-buffer-local 'company-my-keymap)
585
586 (defsubst company-enable-overriding-keymap (keymap)
587 (setq company-my-keymap keymap)
588 (when company-overriding-keymap-bound
589 (company-uninstall-map)))
590
591 (defun company-install-map ()
592 (unless (or company-overriding-keymap-bound
593 (null company-my-keymap))
594 (setq company-old-keymap overriding-terminal-local-map
595 overriding-terminal-local-map company-my-keymap
596 company-overriding-keymap-bound t)))
597
598 (defun company-uninstall-map ()
599 (when (eq overriding-terminal-local-map company-my-keymap)
600 (setq overriding-terminal-local-map company-old-keymap
601 company-overriding-keymap-bound nil)))
602
603 ;; Hack:
604 ;; Emacs calculates the active keymaps before reading the event. That means we
605 ;; cannot change the keymap from a timer. So we send a bogus command.
606 (defun company-ignore ()
607 (interactive)
608 (setq this-command last-command))
609
610 (global-set-key '[31415926] 'company-ignore)
611
612 (defun company-input-noop ()
613 (push 31415926 unread-command-events))
614
615 ;; Hack:
616 ;; posn-col-row is incorrect in older Emacsen when line-spacing is set
617 (defun company--col-row (&optional pos)
618 (let ((posn (posn-at-point pos)))
619 (cons (car (posn-col-row posn)) (cdr (posn-actual-col-row posn)))))
620
621 (defsubst company--column (&optional pos)
622 (car (posn-col-row (posn-at-point pos))))
623
624 (defsubst company--row (&optional pos)
625 (cdr (posn-actual-col-row (posn-at-point pos))))
626
627 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
628
629 (defun company-grab (regexp &optional expression limit)
630 (when (looking-back regexp limit)
631 (or (match-string-no-properties (or expression 0)) "")))
632
633 (defun company-grab-line (regexp &optional expression)
634 (company-grab regexp expression (point-at-bol)))
635
636 (defun company-grab-symbol ()
637 (if (looking-at "\\_>")
638 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
639 (point)))
640 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
641 "")))
642
643 (defun company-grab-word ()
644 (if (looking-at "\\>")
645 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
646 (point)))
647 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
648 "")))
649
650 (defun company-in-string-or-comment ()
651 (let ((ppss (syntax-ppss)))
652 (or (car (setq ppss (nthcdr 3 ppss)))
653 (car (setq ppss (cdr ppss)))
654 (nth 3 ppss))))
655
656 (if (fboundp 'locate-dominating-file)
657 (defalias 'company-locate-dominating-file 'locate-dominating-file)
658 (defun company-locate-dominating-file (file name)
659 (catch 'root
660 (let ((dir (file-name-directory file))
661 (prev-dir nil))
662 (while (not (equal dir prev-dir))
663 (when (file-exists-p (expand-file-name name dir))
664 (throw 'root dir))
665 (setq prev-dir dir
666 dir (file-name-directory (directory-file-name dir))))))))
667
668 (defun company-call-backend (&rest args)
669 (if (functionp company-backend)
670 (apply company-backend args)
671 (apply 'company--multi-backend-adapter company-backend args)))
672
673 (defun company--multi-backend-adapter (backends command &rest args)
674 (case command
675 ('candidates
676 (apply 'append (mapcar (lambda (backend) (apply backend command args))
677 backends)))
678 ('sorted nil)
679 ('duplicates t)
680 (otherwise
681 (let (value)
682 (dolist (backend backends)
683 (when (setq value (apply backend command args))
684 (return value)))))))
685
686 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
687
688 (defvar company-backend nil)
689 (make-variable-buffer-local 'company-backend)
690
691 (defvar company-prefix nil)
692 (make-variable-buffer-local 'company-prefix)
693
694 (defvar company-candidates nil)
695 (make-variable-buffer-local 'company-candidates)
696
697 (defvar company-candidates-length nil)
698 (make-variable-buffer-local 'company-candidates-length)
699
700 (defvar company-candidates-cache nil)
701 (make-variable-buffer-local 'company-candidates-cache)
702
703 (defvar company-candidates-predicate nil)
704 (make-variable-buffer-local 'company-candidates-predicate)
705
706 (defvar company-common nil)
707 (make-variable-buffer-local 'company-common)
708
709 (defvar company-selection 0)
710 (make-variable-buffer-local 'company-selection)
711
712 (defvar company-selection-changed nil)
713 (make-variable-buffer-local 'company-selection-changed)
714
715 (defvar company--explicit-action nil
716 "Non-nil, if explicit completion took place.")
717 (make-variable-buffer-local 'company--explicit-action)
718
719 (defvar company--point-max nil)
720 (make-variable-buffer-local 'company--point-max)
721
722 (defvar company-point nil)
723 (make-variable-buffer-local 'company-point)
724
725 (defvar company-timer nil)
726
727 (defvar company-added-newline nil)
728 (make-variable-buffer-local 'company-added-newline)
729
730 (defsubst company-strip-prefix (str)
731 (substring str (length company-prefix)))
732
733 (defun company-explicit-action-p ()
734 "Return whether explicit completion action was taken by the user."
735 (or company--explicit-action
736 company-selection-changed))
737
738 (defsubst company-reformat (candidate)
739 ;; company-ispell needs this, because the results are always lower-case
740 ;; It's mory efficient to fix it only when they are displayed.
741 (concat company-prefix (substring candidate (length company-prefix))))
742
743 (defun company--should-complete ()
744 (and (not (or buffer-read-only overriding-terminal-local-map
745 overriding-local-map))
746 (eq company-idle-delay t)
747 (or (eq t company-begin-commands)
748 (memq this-command company-begin-commands)
749 (and (symbolp this-command) (get this-command 'company-begin)))
750 (not (and transient-mark-mode mark-active))))
751
752 (defsubst company-call-frontends (command)
753 (dolist (frontend company-frontends)
754 (condition-case err
755 (funcall frontend command)
756 (error (error "Company: Front-end %s error \"%s\" on command %s"
757 frontend (error-message-string err) command)))))
758
759 (defsubst company-set-selection (selection &optional force-update)
760 (setq selection (max 0 (min (1- company-candidates-length) selection)))
761 (when (or force-update (not (equal selection company-selection)))
762 (setq company-selection selection
763 company-selection-changed t)
764 (company-call-frontends 'update)))
765
766 (defun company-apply-predicate (candidates predicate)
767 (let (new)
768 (dolist (c candidates)
769 (when (funcall predicate c)
770 (push c new)))
771 (nreverse new)))
772
773 (defun company-update-candidates (candidates)
774 (setq company-candidates-length (length candidates))
775 (if (> company-selection 0)
776 ;; Try to restore the selection
777 (let ((selected (nth company-selection company-candidates)))
778 (setq company-selection 0
779 company-candidates candidates)
780 (when selected
781 (while (and candidates (string< (pop candidates) selected))
782 (incf company-selection))
783 (unless candidates
784 ;; Make sure selection isn't out of bounds.
785 (setq company-selection (min (1- company-candidates-length)
786 company-selection)))))
787 (setq company-selection 0
788 company-candidates candidates))
789 ;; Save in cache:
790 (push (cons company-prefix company-candidates) company-candidates-cache)
791 ;; Calculate common.
792 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
793 (setq company-common (try-completion company-prefix company-candidates)))
794 (when (eq company-common t)
795 (setq company-candidates nil)))
796
797 (defun company-calculate-candidates (prefix)
798 (let ((candidates (cdr (assoc prefix company-candidates-cache))))
799 (or candidates
800 (when company-candidates-cache
801 (let ((len (length prefix))
802 (completion-ignore-case (company-call-backend 'ignore-case))
803 prev)
804 (dotimes (i (1+ len))
805 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
806 company-candidates-cache)))
807 (setq candidates (all-completions prefix prev))
808 (return t)))))
809 ;; no cache match, call back-end
810 (progn
811 (setq candidates (company-call-backend 'candidates prefix))
812 (when company-candidates-predicate
813 (setq candidates
814 (company-apply-predicate candidates
815 company-candidates-predicate)))
816 (unless (company-call-backend 'sorted)
817 (setq candidates (sort candidates 'string<)))
818 (when (company-call-backend 'duplicates)
819 ;; strip duplicates
820 (let ((c2 candidates))
821 (while c2
822 (setcdr c2 (progn (while (equal (pop c2) (car c2)))
823 c2)))))))
824 (if (or (cdr candidates)
825 (not (equal (car candidates) prefix)))
826 ;; Don't start when already completed and unique.
827 candidates
828 ;; Not the right place? maybe when setting?
829 (and company-candidates t))))
830
831 (defun company-idle-begin (buf win tick pos)
832 (and company-mode
833 (eq buf (current-buffer))
834 (eq win (selected-window))
835 (eq tick (buffer-chars-modified-tick))
836 (eq pos (point))
837 (not company-candidates)
838 (not (equal (point) company-point))
839 (let ((company-idle-delay t)
840 (company-begin-commands t))
841 (company-begin)
842 (when company-candidates
843 (company-input-noop)
844 (company-post-command)))))
845
846 (defun company-auto-begin ()
847 (company-assert-enabled)
848 (and company-mode
849 (not company-candidates)
850 (let ((company-idle-delay t)
851 (company-minimum-prefix-length 0)
852 (company-begin-commands t))
853 (company-begin)))
854 ;; Return non-nil if active.
855 company-candidates)
856
857 (defun company-manual-begin ()
858 (interactive)
859 (setq company--explicit-action t)
860 (company-auto-begin))
861
862 (defun company-require-match-p ()
863 (let ((backend-value (company-call-backend 'require-match)))
864 (or (eq backend-value t)
865 (and (if (functionp company-require-match)
866 (funcall company-require-match)
867 (eq company-require-match t))
868 (not (eq backend-value 'never))))))
869
870 (defun company-punctuation-p (input)
871 "Return non-nil, if input starts with punctuation or parentheses."
872 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
873
874 (defun company-auto-complete-p (input)
875 "Return non-nil, if input starts with punctuation or parentheses."
876 (and (if (functionp company-auto-complete)
877 (funcall company-auto-complete)
878 company-auto-complete)
879 (if (functionp company-auto-complete-chars)
880 (funcall company-auto-complete-chars input)
881 (if (consp company-auto-complete-chars)
882 (memq (char-syntax (string-to-char input))
883 company-auto-complete-chars)
884 (string-match (substring input 0 1) company-auto-complete-chars)))))
885
886 (defun company--incremental-p ()
887 (and (> (point) company-point)
888 (> (point-max) company--point-max)
889 (not (eq this-command 'backward-delete-char-untabify))
890 (equal (buffer-substring (- company-point (length company-prefix))
891 company-point)
892 company-prefix)))
893
894 (defsubst company--string-incremental-p (old-prefix new-prefix)
895 (and (> (length new-prefix) (length old-prefix))
896 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
897
898 (defun company--continue-failed (new-prefix)
899 (when (company--incremental-p)
900 (let ((input (buffer-substring-no-properties (point) company-point)))
901 (cond
902 ((company-auto-complete-p input)
903 ;; auto-complete
904 (save-excursion
905 (goto-char company-point)
906 (company-complete-selection)
907 nil))
908 ((and (company--string-incremental-p company-prefix new-prefix)
909 (company-require-match-p))
910 ;; wrong incremental input, but required match
911 (backward-delete-char (length input))
912 (ding)
913 (message "Matching input is required")
914 company-candidates)
915 ((equal company-prefix (car company-candidates))
916 ;; last input was actually success
917 (company-cancel company-prefix)
918 nil)))))
919
920 (defun company--good-prefix-p (prefix)
921 (and (or (company-explicit-action-p)
922 (>= (or (cdr-safe prefix) (length prefix))
923 company-minimum-prefix-length))
924 (stringp (or (car-safe prefix) prefix))))
925
926 (defun company--continue ()
927 (when (company-call-backend 'no-cache company-prefix)
928 ;; Don't complete existing candidates, fetch new ones.
929 (setq company-candidates-cache nil))
930 (let* ((new-prefix (company-call-backend 'prefix))
931 (c (when (and (company--good-prefix-p new-prefix)
932 (setq new-prefix (or (car-safe new-prefix) new-prefix))
933 (= (- (point) (length new-prefix))
934 (- company-point (length company-prefix))))
935 (setq new-prefix (or (car-safe new-prefix) new-prefix))
936 (company-calculate-candidates new-prefix))))
937 (or (cond
938 ((eq c t)
939 ;; t means complete/unique.
940 (company-cancel new-prefix)
941 nil)
942 ((consp c)
943 ;; incremental match
944 (setq company-prefix new-prefix)
945 (company-update-candidates c)
946 c)
947 (t (company--continue-failed new-prefix)))
948 (company-cancel))))
949
950 (defun company--begin-new ()
951 (let (prefix c)
952 (dolist (backend (if company-backend
953 ;; prefer manual override
954 (list company-backend)
955 company-backends))
956 (setq prefix
957 (if (or (symbolp backend)
958 (functionp backend))
959 (when (or (not (symbolp backend))
960 (eq t (get backend 'company-init))
961 (unless (get backend 'company-init)
962 (company-init-backend backend)))
963 (funcall backend 'prefix))
964 (company--multi-backend-adapter backend 'prefix)))
965 (when prefix
966 (when (company--good-prefix-p prefix)
967 (setq prefix (or (car-safe prefix) prefix)
968 company-backend backend
969 c (company-calculate-candidates prefix))
970 ;; t means complete/unique. We don't start, so no hooks.
971 (when (consp c)
972 (setq company-prefix prefix)
973 (when (symbolp backend)
974 (setq company-lighter (concat " " (symbol-name backend))))
975 (company-update-candidates c)
976 (run-hook-with-args 'company-completion-started-hook
977 (company-explicit-action-p))
978 (company-call-frontends 'show)))
979 (return c)))))
980
981 (defun company-begin ()
982 (setq company-candidates
983 (or (and company-candidates (company--continue))
984 (and (company--should-complete) (company--begin-new))))
985 (when company-candidates
986 (when (and company-end-of-buffer-workaround (eobp))
987 (save-excursion (insert "\n"))
988 (setq company-added-newline (buffer-chars-modified-tick)))
989 (setq company-point (point)
990 company--point-max (point-max))
991 (company-enable-overriding-keymap company-active-map)
992 (company-call-frontends 'update)))
993
994 (defun company-cancel (&optional result)
995 (and company-added-newline
996 (> (point-max) (point-min))
997 (let ((tick (buffer-chars-modified-tick)))
998 (delete-region (1- (point-max)) (point-max))
999 (equal tick company-added-newline))
1000 ;; Only set unmodified when tick remained the same since insert.
1001 (set-buffer-modified-p nil))
1002 (when company-prefix
1003 (if (stringp result)
1004 (run-hook-with-args 'company-completion-finished-hook result)
1005 (run-hook-with-args 'company-completion-cancelled-hook result)))
1006 (setq company-added-newline nil
1007 company-backend nil
1008 company-prefix nil
1009 company-candidates nil
1010 company-candidates-length nil
1011 company-candidates-cache nil
1012 company-candidates-predicate nil
1013 company-common nil
1014 company-selection 0
1015 company-selection-changed nil
1016 company--explicit-action nil
1017 company-lighter company-default-lighter
1018 company--point-max nil
1019 company-point nil)
1020 (when company-timer
1021 (cancel-timer company-timer))
1022 (company-search-mode 0)
1023 (company-call-frontends 'hide)
1024 (company-enable-overriding-keymap nil))
1025
1026 (defun company-abort ()
1027 (interactive)
1028 (company-cancel t)
1029 ;; Don't start again, unless started manually.
1030 (setq company-point (point)))
1031
1032 (defun company-finish (result)
1033 (insert (company-strip-prefix result))
1034 (company-cancel result)
1035 ;; Don't start again, unless started manually.
1036 (setq company-point (point)))
1037
1038 (defsubst company-keep (command)
1039 (and (symbolp command) (get command 'company-keep)))
1040
1041 (defun company-pre-command ()
1042 (unless (company-keep this-command)
1043 (condition-case err
1044 (when company-candidates
1045 (company-call-frontends 'pre-command))
1046 (error (message "Company: An error occurred in pre-command")
1047 (message "%s" (error-message-string err))
1048 (company-cancel))))
1049 (when company-timer
1050 (cancel-timer company-timer)
1051 (setq company-timer nil))
1052 (company-uninstall-map))
1053
1054 (defun company-post-command ()
1055 (unless (company-keep this-command)
1056 (condition-case err
1057 (progn
1058 (unless (equal (point) company-point)
1059 (company-begin))
1060 (if company-candidates
1061 (company-call-frontends 'post-command)
1062 (and (numberp company-idle-delay)
1063 (or (eq t company-begin-commands)
1064 (memq this-command company-begin-commands))
1065 (setq company-timer
1066 (run-with-timer company-idle-delay nil
1067 'company-idle-begin
1068 (current-buffer) (selected-window)
1069 (buffer-chars-modified-tick) (point))))))
1070 (error (message "Company: An error occurred in post-command")
1071 (message "%s" (error-message-string err))
1072 (company-cancel))))
1073 (company-install-map))
1074
1075 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1076
1077 (defvar company-search-string nil)
1078 (make-variable-buffer-local 'company-search-string)
1079
1080 (defvar company-search-lighter " Search: \"\"")
1081 (make-variable-buffer-local 'company-search-lighter)
1082
1083 (defvar company-search-old-map nil)
1084 (make-variable-buffer-local 'company-search-old-map)
1085
1086 (defvar company-search-old-selection 0)
1087 (make-variable-buffer-local 'company-search-old-selection)
1088
1089 (defun company-search (text lines)
1090 (let ((quoted (regexp-quote text))
1091 (i 0))
1092 (dolist (line lines)
1093 (when (string-match quoted line (length company-prefix))
1094 (return i))
1095 (incf i))))
1096
1097 (defun company-search-printing-char ()
1098 (interactive)
1099 (company-search-assert-enabled)
1100 (setq company-search-string
1101 (concat (or company-search-string "") (string last-command-event))
1102 company-search-lighter (concat " Search: \"" company-search-string
1103 "\""))
1104 (let ((pos (company-search company-search-string
1105 (nthcdr company-selection company-candidates))))
1106 (if (null pos)
1107 (ding)
1108 (company-set-selection (+ company-selection pos) t))))
1109
1110 (defun company-search-repeat-forward ()
1111 "Repeat the incremental search in completion candidates forward."
1112 (interactive)
1113 (company-search-assert-enabled)
1114 (let ((pos (company-search company-search-string
1115 (cdr (nthcdr company-selection
1116 company-candidates)))))
1117 (if (null pos)
1118 (ding)
1119 (company-set-selection (+ company-selection pos 1) t))))
1120
1121 (defun company-search-repeat-backward ()
1122 "Repeat the incremental search in completion candidates backwards."
1123 (interactive)
1124 (company-search-assert-enabled)
1125 (let ((pos (company-search company-search-string
1126 (nthcdr (- company-candidates-length
1127 company-selection)
1128 (reverse company-candidates)))))
1129 (if (null pos)
1130 (ding)
1131 (company-set-selection (- company-selection pos 1) t))))
1132
1133 (defun company-create-match-predicate ()
1134 (setq company-candidates-predicate
1135 `(lambda (candidate)
1136 ,(if company-candidates-predicate
1137 `(and (string-match ,company-search-string candidate)
1138 (funcall ,company-candidates-predicate
1139 candidate))
1140 `(string-match ,company-search-string candidate))))
1141 (company-update-candidates
1142 (company-apply-predicate company-candidates company-candidates-predicate))
1143 ;; Invalidate cache.
1144 (setq company-candidates-cache (cons company-prefix company-candidates)))
1145
1146 (defun company-filter-printing-char ()
1147 (interactive)
1148 (company-search-assert-enabled)
1149 (company-search-printing-char)
1150 (company-create-match-predicate)
1151 (company-call-frontends 'update))
1152
1153 (defun company-search-kill-others ()
1154 "Limit the completion candidates to the ones matching the search string."
1155 (interactive)
1156 (company-search-assert-enabled)
1157 (company-create-match-predicate)
1158 (company-search-mode 0)
1159 (company-call-frontends 'update))
1160
1161 (defun company-search-abort ()
1162 "Abort searching the completion candidates."
1163 (interactive)
1164 (company-search-assert-enabled)
1165 (company-set-selection company-search-old-selection t)
1166 (company-search-mode 0))
1167
1168 (defun company-search-other-char ()
1169 (interactive)
1170 (company-search-assert-enabled)
1171 (company-search-mode 0)
1172 (when last-input-event
1173 (clear-this-command-keys t)
1174 (setq unread-command-events (list last-input-event))))
1175
1176 (defvar company-search-map
1177 (let ((i 0)
1178 (keymap (make-keymap)))
1179 (if (fboundp 'max-char)
1180 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1181 'company-search-printing-char)
1182 (with-no-warnings
1183 ;; obselete in Emacs 23
1184 (let ((l (generic-character-list))
1185 (table (nth 1 keymap)))
1186 (while l
1187 (set-char-table-default table (car l) 'company-search-printing-char)
1188 (setq l (cdr l))))))
1189 (define-key keymap [t] 'company-search-other-char)
1190 (while (< i ?\s)
1191 (define-key keymap (make-string 1 i) 'company-search-other-char)
1192 (incf i))
1193 (while (< i 256)
1194 (define-key keymap (vector i) 'company-search-printing-char)
1195 (incf i))
1196 (let ((meta-map (make-sparse-keymap)))
1197 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1198 (define-key keymap [escape] meta-map))
1199 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1200 (define-key keymap "\e\e\e" 'company-search-other-char)
1201 (define-key keymap [escape escape escape] 'company-search-other-char)
1202
1203 (define-key keymap "\C-g" 'company-search-abort)
1204 (define-key keymap "\C-s" 'company-search-repeat-forward)
1205 (define-key keymap "\C-r" 'company-search-repeat-backward)
1206 (define-key keymap "\C-o" 'company-search-kill-others)
1207 keymap)
1208 "Keymap used for incrementally searching the completion candidates.")
1209
1210 (define-minor-mode company-search-mode
1211 "Search mode for completion candidates.
1212 Don't start this directly, use `company-search-candidates' or
1213 `company-filter-candidates'."
1214 nil company-search-lighter nil
1215 (if company-search-mode
1216 (if (company-manual-begin)
1217 (progn
1218 (setq company-search-old-selection company-selection)
1219 (company-call-frontends 'update))
1220 (setq company-search-mode nil))
1221 (kill-local-variable 'company-search-string)
1222 (kill-local-variable 'company-search-lighter)
1223 (kill-local-variable 'company-search-old-selection)
1224 (company-enable-overriding-keymap company-active-map)))
1225
1226 (defsubst company-search-assert-enabled ()
1227 (company-assert-enabled)
1228 (unless company-search-mode
1229 (company-uninstall-map)
1230 (error "Company not in search mode")))
1231
1232 (defun company-search-candidates ()
1233 "Start searching the completion candidates incrementally.
1234
1235 \\<company-search-map>Search can be controlled with the commands:
1236 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1237 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1238 - `company-search-abort' (\\[company-search-abort])
1239
1240 Regular characters are appended to the search string.
1241
1242 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
1243 the search string to limit the completion candidates."
1244 (interactive)
1245 (company-search-mode 1)
1246 (company-enable-overriding-keymap company-search-map))
1247
1248 (defvar company-filter-map
1249 (let ((keymap (make-keymap)))
1250 (define-key keymap [remap company-search-printing-char]
1251 'company-filter-printing-char)
1252 (set-keymap-parent keymap company-search-map)
1253 keymap)
1254 "Keymap used for incrementally searching the completion candidates.")
1255
1256 (defun company-filter-candidates ()
1257 "Start filtering the completion candidates incrementally.
1258 This works the same way as `company-search-candidates' immediately
1259 followed by `company-search-kill-others' after each input."
1260 (interactive)
1261 (company-search-mode 1)
1262 (company-enable-overriding-keymap company-filter-map))
1263
1264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1265
1266 (defun company-select-next ()
1267 "Select the next candidate in the list."
1268 (interactive)
1269 (when (company-manual-begin)
1270 (company-set-selection (1+ company-selection))))
1271
1272 (defun company-select-previous ()
1273 "Select the previous candidate in the list."
1274 (interactive)
1275 (when (company-manual-begin)
1276 (company-set-selection (1- company-selection))))
1277
1278 (defun company-select-mouse (event)
1279 "Select the candidate picked by the mouse."
1280 (interactive "e")
1281 (when (nth 4 (event-start event))
1282 (company-set-selection (- (cdr (posn-actual-col-row (event-start event)))
1283 (company--row)
1284 1))
1285 t))
1286
1287 (defun company-complete-mouse (event)
1288 "Complete the candidate picked by the mouse."
1289 (interactive "e")
1290 (when (company-select-mouse event)
1291 (company-complete-selection)))
1292
1293 (defun company-complete-selection ()
1294 "Complete the selected candidate."
1295 (interactive)
1296 (when (company-manual-begin)
1297 (company-finish (nth company-selection company-candidates))))
1298
1299 (defun company-complete-common ()
1300 "Complete the common part of all candidates."
1301 (interactive)
1302 (when (company-manual-begin)
1303 (if (and (not (cdr company-candidates))
1304 (equal company-common (car company-candidates)))
1305 (company-complete-selection)
1306 (insert (company-strip-prefix company-common)))))
1307
1308 (defun company-complete ()
1309 "Complete the common part of all candidates or the current selection.
1310 The first time this is called, the common part is completed, the second time, or
1311 when the selection has been changed, the selected candidate is completed."
1312 (interactive)
1313 (when (company-manual-begin)
1314 (if (or company-selection-changed
1315 (eq last-command 'company-complete-common))
1316 (call-interactively 'company-complete-selection)
1317 (call-interactively 'company-complete-common)
1318 (setq this-command 'company-complete-common))))
1319
1320 (defun company-complete-number (n)
1321 "Complete the Nth candidate.
1322 To show the number next to the candidates in some back-ends, enable
1323 `company-show-numbers'."
1324 (when (company-manual-begin)
1325 (and (< n 1) (> n company-candidates-length)
1326 (error "No candidate number %d" n))
1327 (decf n)
1328 (company-finish (nth n company-candidates))))
1329
1330 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1331
1332 (defconst company-space-strings-limit 100)
1333
1334 (defconst company-space-strings
1335 (let (lst)
1336 (dotimes (i company-space-strings-limit)
1337 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1338 (apply 'vector lst)))
1339
1340 (defsubst company-space-string (len)
1341 (if (< len company-space-strings-limit)
1342 (aref company-space-strings len)
1343 (make-string len ?\ )))
1344
1345 (defsubst company-safe-substring (str from &optional to)
1346 (let ((len (length str)))
1347 (if (> from len)
1348 ""
1349 (if (and to (> to len))
1350 (concat (substring str from)
1351 (company-space-string (- to len)))
1352 (substring str from to)))))
1353
1354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1355
1356 (defvar company-last-metadata nil)
1357 (make-variable-buffer-local 'company-last-metadata)
1358
1359 (defun company-fetch-metadata ()
1360 (let ((selected (nth company-selection company-candidates)))
1361 (unless (equal selected (car company-last-metadata))
1362 (setq company-last-metadata
1363 (cons selected (company-call-backend 'meta selected))))
1364 (cdr company-last-metadata)))
1365
1366 (defun company-doc-buffer (&optional string)
1367 (with-current-buffer (get-buffer-create "*Company meta-data*")
1368 (erase-buffer)
1369 (current-buffer)))
1370
1371 (defmacro company--electric-do (&rest body)
1372 (declare (indent 0) (debug t))
1373 `(when (company-manual-begin)
1374 (save-window-excursion
1375 (let ((height (window-height))
1376 (row (company--row)))
1377 ,@body
1378 (and (< (window-height) height)
1379 (< (- (window-height) row 2) company-tooltip-limit)
1380 (recenter (- (window-height) row 2)))
1381 (while (eq 'scroll-other-window
1382 (key-binding (vector (list (read-event)))))
1383 (call-interactively 'scroll-other-window))
1384 (when last-input-event
1385 (clear-this-command-keys t)
1386 (setq unread-command-events (list last-input-event)))))))
1387
1388 (defun company-show-doc-buffer ()
1389 "Temporarily show a buffer with the complete documentation for the selection."
1390 (interactive)
1391 (company--electric-do
1392 (let ((selected (nth company-selection company-candidates)))
1393 (display-buffer (or (company-call-backend 'doc-buffer selected)
1394 (error "No documentation available")) t))))
1395 (put 'company-show-doc-buffer 'company-keep t)
1396
1397 (defun company-show-location ()
1398 "Temporarily display a buffer showing the selected candidate in context."
1399 (interactive)
1400 (company--electric-do
1401 (let* ((selected (nth company-selection company-candidates))
1402 (location (company-call-backend 'location selected))
1403 (pos (or (cdr location) (error "No location available")))
1404 (buffer (or (and (bufferp (car location)) (car location))
1405 (find-file-noselect (car location) t))))
1406 (with-selected-window (display-buffer buffer t)
1407 (if (bufferp (car location))
1408 (goto-char pos)
1409 (goto-line pos))
1410 (set-window-start nil (point))))))
1411 (put 'company-show-location 'company-keep t)
1412
1413 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1414
1415 (defvar company-callback nil)
1416 (make-variable-buffer-local 'company-callback)
1417
1418 (defvar company-begin-with-marker nil)
1419 (make-variable-buffer-local 'company-begin-with-marker)
1420
1421 (defun company-remove-callback (&optional ignored)
1422 (remove-hook 'company-completion-finished-hook company-callback t)
1423 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1424 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1425 (when company-begin-with-marker
1426 (set-marker company-begin-with-marker nil)))
1427
1428 (defun company-begin-backend (backend &optional callback)
1429 "Start a completion at point using BACKEND."
1430 (interactive (let ((val (completing-read "Company back-end: "
1431 obarray
1432 'functionp nil "company-")))
1433 (when val
1434 (list (intern val)))))
1435 (when (setq company-callback callback)
1436 (add-hook 'company-completion-finished-hook company-callback nil t))
1437 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1438 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1439 (setq company-backend backend)
1440 ;; Return non-nil if active.
1441 (or (company-manual-begin)
1442 (error "Cannot complete at point")))
1443
1444 (defun company-begin-with (candidates
1445 &optional prefix-length require-match callback)
1446 "Start a completion at point.
1447 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
1448 the prefix that already is in the buffer before point. It defaults to 0.
1449
1450 CALLBACK is a function called with the selected result if the user successfully
1451 completes the input.
1452
1453 Example:
1454 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1455 (setq company-begin-with-marker (copy-marker (point) t))
1456 (company-begin-backend
1457 `(lambda (command &optional arg &rest ignored)
1458 (cond
1459 ((eq command 'prefix)
1460 (when (equal (point) (marker-position company-begin-with-marker))
1461 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1462 ((eq command 'candidates)
1463 (all-completions arg ',candidates))
1464 ((eq command 'require-match)
1465 ,require-match)))
1466 callback))
1467
1468 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1469
1470 (defvar company-pseudo-tooltip-overlay nil)
1471 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1472
1473 (defvar company-tooltip-offset 0)
1474 (make-variable-buffer-local 'company-tooltip-offset)
1475
1476 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1477
1478 (decf limit 2)
1479 (setq company-tooltip-offset
1480 (max (min selection company-tooltip-offset)
1481 (- selection -1 limit)))
1482
1483 (when (<= company-tooltip-offset 1)
1484 (incf limit)
1485 (setq company-tooltip-offset 0))
1486
1487 (when (>= company-tooltip-offset (- num-lines limit 1))
1488 (incf limit)
1489 (when (= selection (1- num-lines))
1490 (decf company-tooltip-offset)
1491 (when (<= company-tooltip-offset 1)
1492 (setq company-tooltip-offset 0)
1493 (incf limit))))
1494
1495 limit)
1496
1497 ;;; propertize
1498
1499 (defsubst company-round-tab (arg)
1500 (* (/ (+ arg tab-width) tab-width) tab-width))
1501
1502 (defun company-untabify (str)
1503 (let* ((pieces (split-string str "\t"))
1504 (copy pieces))
1505 (while (cdr copy)
1506 (setcar copy (company-safe-substring
1507 (car copy) 0 (company-round-tab (string-width (car copy)))))
1508 (pop copy))
1509 (apply 'concat pieces)))
1510
1511 (defun company-fill-propertize (line width selected)
1512 (setq line (company-safe-substring line 0 width))
1513 (add-text-properties 0 width '(face company-tooltip
1514 mouse-face company-tooltip-mouse)
1515 line)
1516 (add-text-properties 0 (length company-common)
1517 '(face company-tooltip-common
1518 mouse-face company-tooltip-mouse)
1519 line)
1520 (when selected
1521 (if (and company-search-string
1522 (string-match (regexp-quote company-search-string) line
1523 (length company-prefix)))
1524 (progn
1525 (add-text-properties (match-beginning 0) (match-end 0)
1526 '(face company-tooltip-selection)
1527 line)
1528 (when (< (match-beginning 0) (length company-common))
1529 (add-text-properties (match-beginning 0) (length company-common)
1530 '(face company-tooltip-common-selection)
1531 line)))
1532 (add-text-properties 0 width '(face company-tooltip-selection
1533 mouse-face company-tooltip-selection)
1534 line)
1535 (add-text-properties 0 (length company-common)
1536 '(face company-tooltip-common-selection
1537 mouse-face company-tooltip-selection)
1538 line)))
1539 line)
1540
1541 ;;; replace
1542
1543 (defun company-buffer-lines (beg end)
1544 (goto-char beg)
1545 (let ((row (company--row))
1546 lines)
1547 (while (and (equal (move-to-window-line (incf row)) row)
1548 (<= (point) end))
1549 (push (buffer-substring beg (min end (1- (point)))) lines)
1550 (setq beg (point)))
1551 (unless (eq beg end)
1552 (push (buffer-substring beg end) lines))
1553 (nreverse lines)))
1554
1555 (defsubst company-modify-line (old new offset)
1556 (concat (company-safe-substring old 0 offset)
1557 new
1558 (company-safe-substring old (+ offset (length new)))))
1559
1560 (defsubst company--length-limit (lst limit)
1561 (if (nthcdr limit lst)
1562 limit
1563 (length lst)))
1564
1565 (defun company--replacement-string (lines old column nl &optional align-top)
1566
1567 (let ((width (length (car lines))))
1568 (when (> width (- (window-width) column))
1569 (setq column (max 0 (- (window-width) width)))))
1570
1571 (let (new)
1572 (when align-top
1573 ;; untouched lines first
1574 (dotimes (i (- (length old) (length lines)))
1575 (push (pop old) new)))
1576 ;; length into old lines.
1577 (while old
1578 (push (company-modify-line (pop old) (pop lines) column) new))
1579 ;; Append whole new lines.
1580 (while lines
1581 (push (concat (company-space-string column) (pop lines)) new))
1582 (concat (when nl "\n")
1583 (mapconcat 'identity (nreverse new) "\n")
1584 "\n")))
1585
1586 (defun company--create-lines (selection limit)
1587
1588 (let ((len company-candidates-length)
1589 (numbered 99999)
1590 lines
1591 width
1592 lines-copy
1593 previous
1594 remainder
1595 new)
1596
1597 ;; Scroll to offset.
1598 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1599
1600 (when (> company-tooltip-offset 0)
1601 (setq previous (format "...(%d)" company-tooltip-offset)))
1602
1603 (setq remainder (- len limit company-tooltip-offset)
1604 remainder (when (> remainder 0)
1605 (setq remainder (format "...(%d)" remainder))))
1606
1607 (decf selection company-tooltip-offset)
1608 (setq width (max (length previous) (length remainder))
1609 lines (nthcdr company-tooltip-offset company-candidates)
1610 len (min limit len)
1611 lines-copy lines)
1612
1613 (dotimes (i len)
1614 (setq width (max (length (pop lines-copy)) width)))
1615 (setq width (min width (window-width)))
1616
1617 (setq lines-copy lines)
1618
1619 ;; number can make tooltip too long
1620 (when company-show-numbers
1621 (setq numbered company-tooltip-offset))
1622
1623 (when previous
1624 (push (propertize (company-safe-substring previous 0 width)
1625 'face 'company-tooltip)
1626 new))
1627
1628 (dotimes (i len)
1629 (push (company-fill-propertize
1630 (if (>= numbered 10)
1631 (company-reformat (pop lines))
1632 (incf numbered)
1633 (format "%s %d"
1634 (company-safe-substring (company-reformat (pop lines))
1635 0 (- width 2))
1636 (mod numbered 10)))
1637 width (equal i selection))
1638 new))
1639
1640 (when remainder
1641 (push (propertize (company-safe-substring remainder 0 width)
1642 'face 'company-tooltip)
1643 new))
1644
1645 (setq lines (nreverse new))))
1646
1647 ;; show
1648
1649 (defsubst company--window-inner-height ()
1650 (let ((edges (window-inside-edges (selected-window))))
1651 (- (nth 3 edges) (nth 1 edges))))
1652
1653 (defsubst company--pseudo-tooltip-height ()
1654 "Calculate the appropriate tooltip height.
1655 Returns a negative number if the tooltip should be displayed above point."
1656 (let* ((lines (count-lines (window-start) (point-at-bol)))
1657 (below (- (company--window-inner-height) 1 lines)))
1658 (if (and (< below (min company-tooltip-minimum company-candidates-length))
1659 (> lines below))
1660 (- (max 3 (min company-tooltip-limit lines)))
1661 (max 3 (min company-tooltip-limit below)))))
1662
1663 (defun company-pseudo-tooltip-show (row column selection)
1664 (company-pseudo-tooltip-hide)
1665 (save-excursion
1666
1667 (move-to-column 0)
1668
1669 (let* ((height (company--pseudo-tooltip-height))
1670 above)
1671
1672 (when (< height 0)
1673 (setq row (+ row height -1)
1674 above t))
1675
1676 (let* ((nl (< (move-to-window-line row) row))
1677 (beg (point))
1678 (end (save-excursion
1679 (move-to-window-line (+ row (abs height)))
1680 (point)))
1681 (ov (make-overlay beg end))
1682 (args (list (mapcar 'company-untabify
1683 (company-buffer-lines beg end))
1684 column nl above)))
1685
1686 (setq company-pseudo-tooltip-overlay ov)
1687 (overlay-put ov 'company-replacement-args args)
1688 (overlay-put ov 'company-before
1689 (apply 'company--replacement-string
1690 (company--create-lines selection (abs height))
1691 args))
1692
1693 (overlay-put ov 'company-column column)
1694 (overlay-put ov 'company-height (abs height))
1695 (overlay-put ov 'window (selected-window))))))
1696
1697 (defun company-pseudo-tooltip-show-at-point (pos)
1698 (let ((col-row (company--col-row pos)))
1699 (when col-row
1700 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
1701 company-selection))))
1702
1703 (defun company-pseudo-tooltip-edit (lines selection)
1704 (let ((column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1705 (height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
1706 (overlay-put company-pseudo-tooltip-overlay 'company-before
1707 (apply 'company--replacement-string
1708 (company--create-lines selection height)
1709 (overlay-get company-pseudo-tooltip-overlay
1710 'company-replacement-args)))))
1711
1712 (defun company-pseudo-tooltip-hide ()
1713 (when company-pseudo-tooltip-overlay
1714 (delete-overlay company-pseudo-tooltip-overlay)
1715 (setq company-pseudo-tooltip-overlay nil)))
1716
1717 (defun company-pseudo-tooltip-hide-temporarily ()
1718 (when (overlayp company-pseudo-tooltip-overlay)
1719 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1720 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1721
1722 (defun company-pseudo-tooltip-unhide ()
1723 (when company-pseudo-tooltip-overlay
1724 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1725 (overlay-put company-pseudo-tooltip-overlay 'before-string
1726 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1727 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1728
1729 (defun company-pseudo-tooltip-frontend (command)
1730 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1731 (case command
1732 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1733 ('post-command
1734 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
1735 (overlay-get company-pseudo-tooltip-overlay
1736 'company-height)
1737 0))
1738 (new-height (company--pseudo-tooltip-height)))
1739 (unless (and (>= (* old-height new-height) 0)
1740 (>= (abs old-height) (abs new-height)))
1741 ;; Redraw needed.
1742 (company-pseudo-tooltip-show-at-point (- (point)
1743 (length company-prefix)))))
1744 (company-pseudo-tooltip-unhide))
1745 ('hide (company-pseudo-tooltip-hide)
1746 (setq company-tooltip-offset 0))
1747 ('update (when (overlayp company-pseudo-tooltip-overlay)
1748 (company-pseudo-tooltip-edit company-candidates
1749 company-selection)))))
1750
1751 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1752 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1753 (unless (and (eq command 'post-command)
1754 (not (cdr company-candidates)))
1755 (company-pseudo-tooltip-frontend command)))
1756
1757 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1758
1759 (defvar company-preview-overlay nil)
1760 (make-variable-buffer-local 'company-preview-overlay)
1761
1762 (defun company-preview-show-at-point (pos)
1763 (company-preview-hide)
1764
1765 (setq company-preview-overlay (make-overlay pos pos))
1766
1767 (let ((completion(nth company-selection company-candidates)))
1768 (setq completion (propertize completion 'face 'company-preview))
1769 (add-text-properties 0 (length company-common)
1770 '(face company-preview-common) completion)
1771
1772 ;; Add search string
1773 (and company-search-string
1774 (string-match (regexp-quote company-search-string) completion)
1775 (add-text-properties (match-beginning 0)
1776 (match-end 0)
1777 '(face company-preview-search)
1778 completion))
1779
1780 (setq completion (company-strip-prefix completion))
1781
1782 (and (equal pos (point))
1783 (not (equal completion ""))
1784 (add-text-properties 0 1 '(cursor t) completion))
1785
1786 (overlay-put company-preview-overlay 'after-string completion)
1787 (overlay-put company-preview-overlay 'window (selected-window))))
1788
1789 (defun company-preview-hide ()
1790 (when company-preview-overlay
1791 (delete-overlay company-preview-overlay)
1792 (setq company-preview-overlay nil)))
1793
1794 (defun company-preview-frontend (command)
1795 "A `company-mode' front-end showing the selection as if it had been inserted."
1796 (case command
1797 ('pre-command (company-preview-hide))
1798 ('post-command (company-preview-show-at-point (point)))
1799 ('hide (company-preview-hide))))
1800
1801 (defun company-preview-if-just-one-frontend (command)
1802 "`company-preview-frontend', but only shown for single candidates."
1803 (unless (and (eq command 'post-command)
1804 (cdr company-candidates))
1805 (company-preview-frontend command)))
1806
1807 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1808
1809 (defvar company-echo-last-msg nil)
1810 (make-variable-buffer-local 'company-echo-last-msg)
1811
1812 (defvar company-echo-timer nil)
1813
1814 (defvar company-echo-delay .1)
1815
1816 (defun company-echo-show (&optional getter)
1817 (when getter
1818 (setq company-echo-last-msg (funcall getter)))
1819 (let ((message-log-max nil))
1820 (if company-echo-last-msg
1821 (message "%s" company-echo-last-msg)
1822 (message ""))))
1823
1824 (defsubst company-echo-show-soon (&optional getter)
1825 (when company-echo-timer
1826 (cancel-timer company-echo-timer))
1827 (setq company-echo-timer (run-with-timer company-echo-delay nil
1828 'company-echo-show getter)))
1829
1830 (defun company-echo-format ()
1831
1832 (let ((limit (window-width (minibuffer-window)))
1833 (len -1)
1834 ;; Roll to selection.
1835 (candidates (nthcdr company-selection company-candidates))
1836 (i (if company-show-numbers company-selection 99999))
1837 comp msg)
1838
1839 (while candidates
1840 (setq comp (company-reformat (pop candidates))
1841 len (+ len 1 (length comp)))
1842 (if (< i 10)
1843 ;; Add number.
1844 (progn
1845 (setq comp (propertize (format "%d: %s" i comp)
1846 'face 'company-echo))
1847 (incf len 3)
1848 (incf i)
1849 (add-text-properties 3 (+ 3 (length company-common))
1850 '(face company-echo-common) comp))
1851 (setq comp (propertize comp 'face 'company-echo))
1852 (add-text-properties 0 (length company-common)
1853 '(face company-echo-common) comp))
1854 (if (>= len limit)
1855 (setq candidates nil)
1856 (push comp msg)))
1857
1858 (mapconcat 'identity (nreverse msg) " ")))
1859
1860 (defun company-echo-strip-common-format ()
1861
1862 (let ((limit (window-width (minibuffer-window)))
1863 (len (+ (length company-prefix) 2))
1864 ;; Roll to selection.
1865 (candidates (nthcdr company-selection company-candidates))
1866 (i (if company-show-numbers company-selection 99999))
1867 msg comp)
1868
1869 (while candidates
1870 (setq comp (company-strip-prefix (pop candidates))
1871 len (+ len 2 (length comp)))
1872 (when (< i 10)
1873 ;; Add number.
1874 (setq comp (format "%s (%d)" comp i))
1875 (incf len 4)
1876 (incf i))
1877 (if (>= len limit)
1878 (setq candidates nil)
1879 (push (propertize comp 'face 'company-echo) msg)))
1880
1881 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1882 (mapconcat 'identity (nreverse msg) ", ")
1883 "}")))
1884
1885 (defun company-echo-hide ()
1886 (when company-echo-timer
1887 (cancel-timer company-echo-timer))
1888 (unless (equal company-echo-last-msg "")
1889 (setq company-echo-last-msg "")
1890 (company-echo-show)))
1891
1892 (defun company-echo-frontend (command)
1893 "A `company-mode' front-end showing the candidates in the echo area."
1894 (case command
1895 ('pre-command (company-echo-show-soon))
1896 ('post-command (company-echo-show-soon 'company-echo-format))
1897 ('hide (company-echo-hide))))
1898
1899 (defun company-echo-strip-common-frontend (command)
1900 "A `company-mode' front-end showing the candidates in the echo area."
1901 (case command
1902 ('pre-command (company-echo-show-soon))
1903 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1904 ('hide (company-echo-hide))))
1905
1906 (defun company-echo-metadata-frontend (command)
1907 "A `company-mode' front-end showing the documentation in the echo area."
1908 (case command
1909 ('pre-command (company-echo-show-soon))
1910 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1911 ('hide (company-echo-hide))))
1912
1913 (provide 'company)
1914 ;;; company.el ends here