1 ;;; company.el --- extensible inline text completion mechanism
3 ;; Copyright (C) 2009 Nikolaj Schumacher
5 ;; Author: Nikolaj Schumacher <bugs * nschum de>
7 ;; Keywords: abbrev, convenience, matchis
8 ;; URL: http://nschum.de/src/emacs/company/
9 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
11 ;; This file is NOT part of GNU Emacs.
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.
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.
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/>.
28 ;; Company is a modular completion mechanism. Modules for retrieving completion
29 ;; candidates are called back-ends, modules for displaying them are front-ends.
31 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
32 ;; distributed in individual files and can be used individually.
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)
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)
42 ;; To write your own back-end, look at the documentation for `company-backends'.
43 ;; Here is a simple example completing "foo":
45 ;; (defun company-my-backend (command &optional arg &rest ignored)
47 ;; ('prefix (when (looking-back "foo\\>")
49 ;; ('candidates (list "foobar" "foobaz" "foobarbaz"))
50 ;; ('meta (format "This value is named %s" arg))))
52 ;; Sometimes it is a good idea to mix two back-ends together, for example to
53 ;; enrich gtags with dabbrev text (to emulate local variables):
55 ;; (defun gtags-gtags-dabbrev-backend (command &optional arg &rest ignored)
57 ;; (prefix (company-gtags 'prefix))
58 ;; (candidates (append (company-gtags 'candidates arg)
59 ;; (company-dabbrev 'candidates arg)))))
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'.
68 ;; Back-ends are now interactive. You can start them with M-x backend-name.
69 ;; Added `company-begin-with' for starting company from elisp-code.
71 ;; Added `company-require-match' and `company-auto-complete' options.
74 ;; Improved Emacs Lisp back-end behavior for local variables.
75 ;; Added `company-elisp-detect-function-context' option.
76 ;; The mouse can now be used for selection.
79 ;; Added `company-show-location'.
80 ;; Added etags back-end.
81 ;; Added work-around for end-of-buffer bug.
82 ;; Added `company-filter-candidates'.
83 ;; More local Lisp variables are now included in the candidates.
86 ;; Fixed elisp documentation buffer always showing the same doc.
87 ;; Added `company-echo-strip-common-frontend'.
88 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
89 ;; Don't hide the echo message if it isn't shown.
96 (eval-when-compile (require 'cl))
98 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
99 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
100 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
101 (add-to-list 'debug-ignored-errors "^Company not ")
102 (add-to-list 'debug-ignored-errors "^No candidate number ")
103 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
105 (defgroup company nil
106 "Extensible inline text completion mechanism"
111 (defface company-tooltip
112 '((t :background "yellow"
113 :foreground "black"))
114 "*Face used for the tool tip."
117 (defface company-tooltip-selection
118 '((default :inherit company-tooltip)
119 (((class color) (min-colors 88)) (:background "orange1"))
120 (t (:background "green")))
121 "*Face used for the selection in the tool tip."
124 (defface company-tooltip-mouse
125 '((default :inherit highlight))
126 "*Face used for the tool tip item under the mouse."
129 (defface company-tooltip-common
130 '((t :inherit company-tooltip
132 "*Face used for the common completion in the tool tip."
135 (defface company-tooltip-common-selection
136 '((t :inherit company-tooltip-selection
138 "*Face used for the selected common completion in the tool tip."
141 (defcustom company-tooltip-limit 10
142 "*The maximum number of candidates in the tool tip"
146 (defface company-preview
147 '((t :background "blue4"
148 :foreground "wheat"))
149 "*Face used for the completion preview."
152 (defface company-preview-common
153 '((t :inherit company-preview
155 "*Face used for the common part of the completion preview."
158 (defface company-preview-search
159 '((t :inherit company-preview
160 :background "blue1"))
161 "*Face used for the search string in the completion preview."
164 (defface company-echo nil
165 "*Face used for completions in the echo area."
168 (defface company-echo-common
169 '((((background dark)) (:foreground "firebrick1"))
170 (((background light)) (:background "firebrick4")))
171 "*Face used for the common part of completions in the echo area."
174 (defun company-frontends-set (variable value)
176 (let ((remainder value))
177 (setcdr remainder (delq (car remainder) (cdr remainder))))
178 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
179 (memq 'company-pseudo-tooltip-frontend value)
180 (error "Pseudo tooltip frontend cannot be used twice"))
181 (and (memq 'company-preview-if-just-one-frontend value)
182 (memq 'company-preview-frontend value)
183 (error "Preview frontend cannot be used twice"))
184 (and (memq 'company-echo value)
185 (memq 'company-echo-metadata-frontend value)
186 (error "Echo area cannot be used twice"))
187 ;; preview must come last
188 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
190 (setq value (append (delq f value) (list f)))))
191 (set variable value))
193 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
194 company-preview-frontend
195 company-echo-metadata-frontend)
196 "*The list of active front-ends (visualizations).
197 Each front-end is a function that takes one argument. It is called with
198 one of the following arguments:
200 'show: When the visualization should start.
202 'hide: When the visualization should end.
204 'update: When the data has been updated.
206 'pre-command: Before every command that is executed while the
207 visualization is active.
209 'post-command: After every command that is executed while the
210 visualization is active.
212 The visualized data is stored in `company-prefix', `company-candidates',
213 `company-common', `company-selection', `company-point' and
214 `company-search-string'."
215 :set 'company-frontends-set
217 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
218 (const :tag "echo, strip common"
219 company-echo-strip-common-frontend)
220 (const :tag "show echo meta-data in echo"
221 company-echo-metadata-frontend)
222 (const :tag "pseudo tooltip"
223 company-pseudo-tooltip-frontend)
224 (const :tag "pseudo tooltip, multiple only"
225 company-pseudo-tooltip-unless-just-one-frontend)
226 (const :tag "preview" company-preview-frontend)
227 (const :tag "preview, unique only"
228 company-preview-if-just-one-frontend)
229 (function :tag "custom function" nil))))
231 (defcustom company-backends '(company-elisp company-nxml company-css
232 company-semantic company-gtags company-etags
233 company-oddmuse company-files company-dabbrev)
234 "*The list of active back-ends (completion engines).
235 Each back-end is a function that takes a variable number of arguments.
236 The first argument is the command requested from the back-end. It is one
239 'prefix: The back-end should return the text to be completed. It must be
240 text immediately before `point'. Returning nil passes control to the next
243 'candidates: The second argument is the prefix to be completed. The
244 return value should be a list of candidates that start with the prefix.
248 'sorted: The back-end may return t here to indicate that the candidates
249 are sorted and will not need to be sorted again.
251 'no-cache: Usually company doesn't ask for candidates again as completion
252 progresses, unless the back-end returns t for this command. The second
253 argument is the latest prefix.
255 'meta: The second argument is a completion candidate. The back-end should
256 return a (short) documentation string for it.
258 'doc-buffer: The second argument is a completion candidate. The back-end should
259 create a buffer (preferably with `company-doc-buffer'), fill it with
260 documentation and return it.
262 'location: The second argument is a completion candidate. The back-end can
263 return the cons of buffer and buffer location, or of file and line
264 number where the completion candidate was defined.
266 'require-match: If this value is t, the user is not allowed to enter anything
267 not offering as a candidate. Use with care! The default value nil gives the
268 user that choice with `company-require-match'. Return value 'never overrides
269 that option the other way around.
271 The back-end should return nil for all commands it does not support or
272 does not know about. It should also be callable interactively and use
273 `company-begin-backend' to start itself in that case."
275 :type '(repeat (function :tag "function" nil)))
277 (defvar start-count 0)
279 (defcustom company-completion-started-hook nil
280 "*Hook run when company starts completing.
281 The hook is called with one argument that is non-nil if the completion was
286 (defcustom company-completion-cancelled-hook nil
287 "*Hook run when company cancels completing.
288 The hook is called with one argument that is non-nil if the completion was
293 (defcustom company-completion-finished-hook nil
294 "*Hook run when company successfully completes.
295 The hook is called with the selected candidate as an argument."
299 (defcustom company-minimum-prefix-length 3
300 "*The minimum prefix length for automatic completion."
302 :type '(integer :tag "prefix length"))
304 (defcustom company-require-match 'company-explicit-action-p
305 "*If enabled, disallow non-matching input.
306 This can be a function do determine if a match is required.
308 This can be overridden by the back-end, if it returns t or 'never to
309 'require-match. `company-auto-complete' also takes precedence over this."
311 :type '(choice (const :tag "Off" nil)
312 (function :tag "Predicate function")
313 (const :tag "On, if user interaction took place"
314 'company-explicit-action-p)
315 (const :tag "On" t)))
317 (defcustom company-auto-complete '(?\ ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
318 "Determines which characters trigger an automatic completion.
319 If this is a function, it is called with the new input and should return non-nil
320 if company should auto-complete.
322 If this is a string, all characters in that string will complete automatically.
324 A list of characters represent the syntax (see `modify-syntax-entry') of
325 characters that complete automatically."
327 :type '(choice (const :tag "Off" nil)
328 (function :tag "Predicate function")
329 (string :tag "Characters")
331 (const :tag "Whitespace" ?\ )
332 (const :tag "Symbol" ?_)
333 (const :tag "Opening parentheses" ?\()
334 (const :tag "Closing parentheses" ?\))
335 (const :tag "Word constituent" ?w)
336 (const :tag "Punctuation." ?.)
337 (const :tag "String quote." ?\")
338 (const :tag "Paired delimiter." ?$)
339 (const :tag "Expression quote or prefix operator." ?\')
340 (const :tag "Comment starter." ?<)
341 (const :tag "Comment ender." ?>)
342 (const :tag "Character-quote." ?/)
343 (const :tag "Generic string fence." ?|)
344 (const :tag "Generic comment fence." ?!))))
346 (defcustom company-idle-delay .7
347 "*The idle delay in seconds until automatic completions starts.
348 A value of nil means never complete automatically, t means complete
349 immediately when a prefix of `company-minimum-prefix-length' is reached."
351 :type '(choice (const :tag "never (nil)" nil)
352 (const :tag "immediate (t)" t)
353 (number :tag "seconds")))
355 (defcustom company-show-numbers nil
356 "*If enabled, show quick-access numbers for the first ten candidates."
358 :type '(choice (const :tag "off" nil)
359 (const :tag "on" t)))
361 (defvar company-end-of-buffer-workaround t
362 "*Work around a visualization bug when completing at the end of the buffer.
363 The work-around consists of adding a newline.")
365 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
367 (defvar company-mode-map (make-sparse-keymap)
368 "Keymap used by `company-mode'.")
370 (defvar company-active-map
371 (let ((keymap (make-sparse-keymap)))
372 (define-key keymap "\e\e\e" 'company-abort)
373 (define-key keymap "\C-g" 'company-abort)
374 (define-key keymap (kbd "M-n") 'company-select-next)
375 (define-key keymap (kbd "M-p") 'company-select-previous)
376 (define-key keymap (kbd "<down>") 'company-select-next)
377 (define-key keymap (kbd "<up>") 'company-select-previous)
378 (define-key keymap [down-mouse-1] 'ignore)
379 (define-key keymap [down-mouse-3] 'ignore)
380 (define-key keymap [mouse-1] 'company-complete-mouse)
381 (define-key keymap [mouse-3] 'company-select-mouse)
382 (define-key keymap [up-mouse-1] 'ignore)
383 (define-key keymap [up-mouse-3] 'ignore)
384 (define-key keymap "\C-m" 'company-complete-selection)
385 (define-key keymap "\t" 'company-complete-common)
386 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
387 (define-key keymap "\C-w" 'company-show-location)
388 (define-key keymap "\C-s" 'company-search-candidates)
389 (define-key keymap "\C-\M-s" 'company-filter-candidates)
391 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
392 `(lambda () (interactive) (company-complete-number ,i))))
395 "Keymap that is enabled during an active completion.")
398 (define-minor-mode company-mode
399 "\"complete anything\"; in in-buffer completion framework.
400 Completion starts automatically, depending on the values
401 `company-idle-delay' and `company-minimum-prefix-length'.
403 Completion can be controlled with the commands:
404 `company-complete-common', `company-complete-selection', `company-complete',
405 `company-select-next', `company-select-previous'. If these commands are
406 called before `company-idle-delay', completion will also start.
408 Completions can be searched with `company-search-candidates' or
409 `company-filter-candidates'. These can be used while completion is
412 The completion data is retrieved using `company-backends' and displayed using
413 `company-frontends'. If you want to start a specific back-end, call it
414 interactively or use `company-begin-backend'.
416 regular keymap (`company-mode-map'):
419 keymap during active completions (`company-active-map'):
421 \\{company-active-map}"
422 nil " comp" company-mode-map
425 (add-hook 'pre-command-hook 'company-pre-command nil t)
426 (add-hook 'post-command-hook 'company-post-command nil t)
427 (dolist (backend company-backends)
428 (when (symbolp backend)
429 (unless (fboundp backend)
430 (ignore-errors (require backend nil t)))
431 (unless (fboundp backend)
432 (message "Company back-end '%s' could not be initialized"
434 (remove-hook 'pre-command-hook 'company-pre-command t)
435 (remove-hook 'post-command-hook 'company-post-command t)
437 (kill-local-variable 'company-point)))
439 (defsubst company-assert-enabled ()
441 (company-uninstall-map)
442 (error "Company not enabled")))
444 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
446 (defvar company-overriding-keymap-bound nil)
447 (make-variable-buffer-local 'company-overriding-keymap-bound)
449 (defvar company-old-keymap nil)
450 (make-variable-buffer-local 'company-old-keymap)
452 (defvar company-my-keymap nil)
453 (make-variable-buffer-local 'company-my-keymap)
455 (defsubst company-enable-overriding-keymap (keymap)
456 (setq company-my-keymap keymap)
457 (when company-overriding-keymap-bound
458 (company-uninstall-map)))
460 (defun company-install-map ()
461 (unless (or company-overriding-keymap-bound
462 (null company-my-keymap))
463 (setq company-old-keymap overriding-terminal-local-map
464 overriding-terminal-local-map company-my-keymap
465 company-overriding-keymap-bound t)))
467 (defun company-uninstall-map ()
468 (when (eq overriding-terminal-local-map company-my-keymap)
469 (setq overriding-terminal-local-map company-old-keymap
470 company-overriding-keymap-bound nil)))
473 ;; Emacs calculates the active keymaps before reading the event. That means we
474 ;; cannot change the keymap from a timer. So we send a bogus command.
475 (defun company-ignore ()
478 (global-set-key '[31415926] 'company-ignore)
480 (defun company-input-noop ()
481 (push 31415926 unread-command-events))
483 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
485 (defun company-grab (regexp &optional expression)
486 (when (looking-back regexp)
487 (or (match-string-no-properties (or expression 0)) "")))
489 (defun company-in-string-or-comment (&optional point)
490 (let ((pos (syntax-ppss)))
491 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
493 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
495 (defvar company-backend nil)
496 (make-variable-buffer-local 'company-backend)
498 (defvar company-prefix nil)
499 (make-variable-buffer-local 'company-prefix)
501 (defvar company-candidates nil)
502 (make-variable-buffer-local 'company-candidates)
504 (defvar company-candidates-length nil)
505 (make-variable-buffer-local 'company-candidates-length)
507 (defvar company-candidates-cache nil)
508 (make-variable-buffer-local 'company-candidates-cache)
510 (defvar company-candidates-predicate nil)
511 (make-variable-buffer-local 'company-candidates-predicate)
513 (defvar company-common nil)
514 (make-variable-buffer-local 'company-common)
516 (defvar company-selection 0)
517 (make-variable-buffer-local 'company-selection)
519 (defvar company-selection-changed nil)
520 (make-variable-buffer-local 'company-selection-changed)
522 (defvar company--explicit-action nil
523 "Non-nil, if explicit completion took place.")
524 (make-variable-buffer-local 'company--explicit-action)
526 (defvar company-point nil)
527 (make-variable-buffer-local 'company-point)
529 (defvar company-timer nil)
531 (defvar company-added-newline nil)
532 (make-variable-buffer-local 'company-added-newline)
534 (defsubst company-strip-prefix (str)
535 (substring str (length company-prefix)))
537 (defun company-explicit-action-p ()
538 "Return whether explicit completion action was taken by the user."
539 (or company--explicit-action
540 company-selection-changed))
542 (defsubst company-reformat (candidate)
543 ;; company-ispell needs this, because the results are always lower-case
544 ;; It's mory efficient to fix it only when they are displayed.
545 (concat company-prefix (substring candidate (length company-prefix))))
547 (defsubst company-should-complete (prefix)
548 (and (eq company-idle-delay t)
549 (not (and transient-mark-mode mark-active))
550 (>= (length prefix) company-minimum-prefix-length)))
552 (defsubst company-call-frontends (command)
553 (dolist (frontend company-frontends)
555 (funcall frontend command)
556 (error (error "Company: Front-end %s error \"%s\" on command %s"
557 frontend (error-message-string err) command)))))
559 (defsubst company-set-selection (selection &optional force-update)
560 (setq selection (max 0 (min (1- company-candidates-length) selection)))
561 (when (or force-update (not (equal selection company-selection)))
562 (setq company-selection selection
563 company-selection-changed t)
564 (company-call-frontends 'update)))
566 (defun company-apply-predicate (candidates predicate)
568 (dolist (c candidates)
569 (when (funcall predicate c)
573 (defun company-update-candidates (candidates)
574 (setq company-candidates-length (length candidates))
575 (if (> company-selection 0)
576 ;; Try to restore the selection
577 (let ((selected (nth company-selection company-candidates)))
578 (setq company-selection 0
579 company-candidates candidates)
581 (while (and candidates (string< (pop candidates) selected))
582 (incf company-selection))
584 ;; Make sure selection isn't out of bounds.
585 (setq company-selection (min (1- company-candidates-length)
586 company-selection)))))
587 (setq company-selection 0
588 company-candidates candidates))
590 (push (cons company-prefix company-candidates) company-candidates-cache)
592 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
593 (setq company-common (try-completion company-prefix company-candidates)))
594 (when (eq company-common t)
595 (setq company-candidates nil)))
597 (defun company-calculate-candidates (prefix)
599 (or (cdr (assoc prefix company-candidates-cache))
600 (when company-candidates-cache
601 (let ((len (length prefix))
602 (completion-ignore-case (funcall company-backend
606 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
607 company-candidates-cache)))
608 (return (all-completions prefix prev))))))
609 (let ((c (funcall company-backend 'candidates prefix)))
610 (when company-candidates-predicate
611 (setq c (company-apply-predicate
612 c company-candidates-predicate)))
613 (unless (funcall company-backend 'sorted)
614 (setq c (sort c 'string<)))
616 (if (or (cdr candidates)
617 (not (equal (car candidates) prefix)))
618 ;; Don't start when already completed and unique.
620 ;; Not the right place? maybe when setting?
621 (and company-candidates t))))
623 (defun company-idle-begin (buf win tick pos)
625 (eq buf (current-buffer))
626 (eq win (selected-window))
627 (eq tick (buffer-chars-modified-tick))
629 (not company-candidates)
630 (not (equal (point) company-point))
631 (let ((company-idle-delay t))
633 (when company-candidates
635 (company-post-command)))))
637 (defun company-manual-begin ()
639 (company-assert-enabled)
641 (not company-candidates)
642 (let ((company-idle-delay t)
643 (company-minimum-prefix-length 0))
644 (setq company--explicit-action t)
646 ;; Return non-nil if active.
649 (defsubst company-incremental-p (old-prefix new-prefix)
650 (and (> (length new-prefix) (length old-prefix))
651 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
653 (defun company-require-match-p ()
654 (let ((backend-value (funcall company-backend 'require-match)))
655 (or (eq backend-value t)
656 (and (if (functionp company-require-match)
657 (funcall company-require-match)
658 (eq company-require-match t))
659 (not (eq backend-value 'never))))))
661 (defun company-punctuation-p (input)
662 "Return non-nil, if input starts with punctuation or parentheses."
663 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
665 (defun company-auto-complete-p (beg end)
666 "Return non-nil, if input starts with punctuation or parentheses."
668 (if (functionp company-auto-complete)
669 (funcall company-auto-complete (buffer-substring beg end))
670 (if (consp company-auto-complete)
671 (memq (char-syntax (char-after beg)) company-auto-complete)
672 (string-match (buffer-substring beg (1+ beg))
673 company-auto-complete)))))
675 (defun company-continue ()
676 (when company-candidates
677 (when (funcall company-backend 'no-cache company-prefix)
678 ;; Don't complete existing candidates, fetch new ones.
679 (setq company-candidates-cache nil))
680 (let ((new-prefix (funcall company-backend 'prefix)))
681 (unless (and (= (- (point) (length new-prefix))
682 (- company-point (length company-prefix)))
683 (or (equal company-prefix new-prefix)
684 (let ((c (company-calculate-candidates new-prefix)))
685 ;; t means complete/unique.
687 (progn (company-cancel new-prefix) t)
689 (setq company-prefix new-prefix)
690 (company-update-candidates c)
692 (if (company-auto-complete-p company-point (point))
694 (goto-char company-point)
695 (company-complete-selection)
696 (setq company-candidates nil))
697 (if (not (and (company-incremental-p company-prefix new-prefix)
698 (company-require-match-p)))
700 (when (equal company-prefix (car company-candidates))
701 ;; cancel, but last input was actually success
702 (company-cancel company-prefix))
703 (setq company-candidates nil))
704 (backward-delete-char (length new-prefix))
705 (insert company-prefix)
707 (message "Matching input is required")))
708 company-candidates))))
710 (defun company-begin ()
711 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
712 ;; Don't complete in these cases.
713 (setq company-candidates nil)
715 (unless company-candidates
717 (dolist (backend (if company-backend
718 ;; prefer manual override
719 (list company-backend)
720 (cons company-backend company-backends)))
721 (when (and (functionp backend)
722 (setq prefix (funcall backend 'prefix)))
723 (setq company-backend backend)
724 (when (company-should-complete prefix)
725 (let ((c (company-calculate-candidates prefix)))
726 ;; t means complete/unique. We don't start, so no hooks.
728 (setq company-prefix prefix)
729 (company-update-candidates c)
730 (run-hook-with-args 'company-completion-started-hook
731 (company-explicit-action-p))
732 (company-call-frontends 'show))))
734 (if company-candidates
736 (when (and company-end-of-buffer-workaround (eobp))
737 (save-excursion (insert "\n"))
738 (setq company-added-newline (buffer-chars-modified-tick)))
739 (setq company-point (point))
740 (company-enable-overriding-keymap company-active-map)
741 (company-call-frontends 'update))
744 (defun company-cancel (&optional result)
745 (and company-added-newline
746 (> (point-max) (point-min))
747 (let ((tick (buffer-chars-modified-tick)))
748 (delete-region (1- (point-max)) (point-max))
749 (equal tick company-added-newline))
750 ;; Only set unmodified when tick remained the same since insert.
751 (set-buffer-modified-p nil))
754 (run-hook-with-args 'company-completion-finished-hook result)
755 (run-hook-with-args 'company-completion-cancelled-hook result)))
756 (setq company-added-newline nil
759 company-candidates nil
760 company-candidates-length nil
761 company-candidates-cache nil
762 company-candidates-predicate nil
765 company-selection-changed nil
766 company--explicit-action nil
769 (cancel-timer company-timer))
770 (company-search-mode 0)
771 (company-call-frontends 'hide)
772 (company-enable-overriding-keymap nil))
774 (defun company-abort ()
777 ;; Don't start again, unless started manually.
778 (setq company-point (point)))
780 (defun company-finish (result)
781 (insert (company-strip-prefix result))
782 (company-cancel result)
783 ;; Don't start again, unless started manually.
784 (setq company-point (point)))
786 (defsubst company-keep (command)
787 (and (symbolp command) (get command 'company-keep)))
789 (defun company-pre-command ()
790 (unless (company-keep this-command)
792 (when company-candidates
793 (company-call-frontends 'pre-command))
794 (error (message "Company: An error occurred in pre-command")
795 (message "%s" (error-message-string err))
798 (cancel-timer company-timer))
799 (company-uninstall-map))
801 (defun company-post-command ()
802 (unless (company-keep this-command)
805 (unless (equal (point) company-point)
807 (when company-candidates
808 (company-call-frontends 'post-command))
809 (when (numberp company-idle-delay)
811 (run-with-timer company-idle-delay nil 'company-idle-begin
812 (current-buffer) (selected-window)
813 (buffer-chars-modified-tick) (point)))))
814 (error (message "Company: An error occurred in post-command")
815 (message "%s" (error-message-string err))
817 (company-install-map))
819 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
821 (defvar company-search-string nil)
822 (make-variable-buffer-local 'company-search-string)
824 (defvar company-search-lighter " Search: \"\"")
825 (make-variable-buffer-local 'company-search-lighter)
827 (defvar company-search-old-map nil)
828 (make-variable-buffer-local 'company-search-old-map)
830 (defvar company-search-old-selection 0)
831 (make-variable-buffer-local 'company-search-old-selection)
833 (defun company-search (text lines)
834 (let ((quoted (regexp-quote text))
837 (when (string-match quoted line (length company-prefix))
841 (defun company-search-printing-char ()
843 (company-search-assert-enabled)
844 (setq company-search-string
845 (concat (or company-search-string "") (string last-command-event))
846 company-search-lighter (concat " Search: \"" company-search-string
848 (let ((pos (company-search company-search-string
849 (nthcdr company-selection company-candidates))))
852 (company-set-selection (+ company-selection pos) t))))
854 (defun company-search-repeat-forward ()
855 "Repeat the incremental search in completion candidates forward."
857 (company-search-assert-enabled)
858 (let ((pos (company-search company-search-string
859 (cdr (nthcdr company-selection
860 company-candidates)))))
863 (company-set-selection (+ company-selection pos 1) t))))
865 (defun company-search-repeat-backward ()
866 "Repeat the incremental search in completion candidates backwards."
868 (company-search-assert-enabled)
869 (let ((pos (company-search company-search-string
870 (nthcdr (- company-candidates-length
872 (reverse company-candidates)))))
875 (company-set-selection (- company-selection pos 1) t))))
877 (defun company-create-match-predicate ()
878 (setq company-candidates-predicate
880 ,(if company-candidates-predicate
881 `(and (string-match ,company-search-string candidate)
882 (funcall ,company-candidates-predicate
884 `(string-match ,company-search-string candidate))))
885 (company-update-candidates
886 (company-apply-predicate company-candidates company-candidates-predicate))
888 (setq company-candidates-cache (cons company-prefix company-candidates)))
890 (defun company-filter-printing-char ()
892 (company-search-assert-enabled)
893 (company-search-printing-char)
894 (company-create-match-predicate)
895 (company-call-frontends 'update))
897 (defun company-search-kill-others ()
898 "Limit the completion candidates to the ones matching the search string."
900 (company-search-assert-enabled)
901 (company-create-match-predicate)
902 (company-search-mode 0)
903 (company-call-frontends 'update))
905 (defun company-search-abort ()
906 "Abort searching the completion candidates."
908 (company-search-assert-enabled)
909 (company-set-selection company-search-old-selection t)
910 (company-search-mode 0))
912 (defun company-search-other-char ()
914 (company-search-assert-enabled)
915 (company-search-mode 0)
916 (when last-input-event
917 (clear-this-command-keys t)
918 (setq unread-command-events (list last-input-event))))
920 (defvar company-search-map
922 (keymap (make-keymap)))
923 (if (fboundp 'max-char)
924 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
925 'company-search-printing-char)
927 ;; obselete in Emacs 23
928 (let ((l (generic-character-list))
929 (table (nth 1 keymap)))
931 (set-char-table-default table (car l) 'company-search-printing-char)
933 (define-key keymap [t] 'company-search-other-char)
935 (define-key keymap (make-string 1 i) 'company-search-other-char)
938 (define-key keymap (vector i) 'company-search-printing-char)
940 (let ((meta-map (make-sparse-keymap)))
941 (define-key keymap (char-to-string meta-prefix-char) meta-map)
942 (define-key keymap [escape] meta-map))
943 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
944 (define-key keymap "\e\e\e" 'company-search-other-char)
945 (define-key keymap [escape escape escape] 'company-search-other-char)
947 (define-key keymap "\C-g" 'company-search-abort)
948 (define-key keymap "\C-s" 'company-search-repeat-forward)
949 (define-key keymap "\C-r" 'company-search-repeat-backward)
950 (define-key keymap "\C-o" 'company-search-kill-others)
952 "Keymap used for incrementally searching the completion candidates.")
954 (define-minor-mode company-search-mode
955 "Search mode for completion candidates.
956 Don't start this directly, use `company-search-candidates' or
957 `company-filter-candidates'."
958 nil company-search-lighter nil
959 (if company-search-mode
960 (if (company-manual-begin)
962 (setq company-search-old-selection company-selection)
963 (company-call-frontends 'update))
964 (setq company-search-mode nil))
965 (kill-local-variable 'company-search-string)
966 (kill-local-variable 'company-search-lighter)
967 (kill-local-variable 'company-search-old-selection)
968 (company-enable-overriding-keymap company-active-map)))
970 (defsubst company-search-assert-enabled ()
971 (company-assert-enabled)
972 (unless company-search-mode
973 (company-uninstall-map)
974 (error "Company not in search mode")))
976 (defun company-search-candidates ()
977 "Start searching the completion candidates incrementally.
979 \\<company-search-map>Search can be controlled with the commands:
980 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
981 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
982 - `company-search-abort' (\\[company-search-abort])
984 Regular characters are appended to the search string.
986 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
987 the search string to limit the completion candidates."
989 (company-search-mode 1)
990 (company-enable-overriding-keymap company-search-map))
992 (defvar company-filter-map
993 (let ((keymap (make-keymap)))
994 (define-key keymap [remap company-search-printing-char]
995 'company-filter-printing-char)
996 (set-keymap-parent keymap company-search-map)
998 "Keymap used for incrementally searching the completion candidates.")
1000 (defun company-filter-candidates ()
1001 "Start filtering the completion candidates incrementally.
1002 This works the same way as `company-search-candidates' immediately
1003 followed by `company-search-kill-others' after each input."
1005 (company-search-mode 1)
1006 (company-enable-overriding-keymap company-filter-map))
1008 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1010 (defun company-select-next ()
1011 "Select the next candidate in the list."
1013 (when (company-manual-begin)
1014 (company-set-selection (1+ company-selection))))
1016 (defun company-select-previous ()
1017 "Select the previous candidate in the list."
1019 (when (company-manual-begin)
1020 (company-set-selection (1- company-selection))))
1022 (defun company-select-mouse (event)
1023 "Select the candidate picked by the mouse."
1025 (when (nth 4 (event-start event))
1026 (company-set-selection (- (cdr (posn-col-row (event-start event)))
1027 (cdr (posn-col-row (posn-at-point)))
1031 (defun company-complete-mouse (event)
1032 "Complete the candidate picked by the mouse."
1034 (when (company-select-mouse event)
1035 (company-complete-selection)))
1037 (defun company-complete-selection ()
1038 "Complete the selected candidate."
1040 (when (company-manual-begin)
1041 (company-finish (nth company-selection company-candidates))))
1043 (defun company-complete-common ()
1044 "Complete the common part of all candidates."
1046 (when (company-manual-begin)
1047 (if (equal company-common (car company-candidates))
1048 ;; for success message
1049 (company-complete-selection)
1050 (insert (company-strip-prefix company-common)))))
1052 (defun company-complete ()
1053 "Complete the common part of all candidates or the current selection.
1054 The first time this is called, the common part is completed, the second time, or
1055 when the selection has been changed, the selected candidate is completed."
1057 (when (company-manual-begin)
1058 (if (or company-selection-changed
1059 (eq last-command 'company-complete-common))
1060 (call-interactively 'company-complete-selection)
1061 (call-interactively 'company-complete-common)
1062 (setq this-command 'company-complete-common))))
1064 (defun company-complete-number (n)
1065 "Complete the Nth candidate."
1066 (when (company-manual-begin)
1067 (and (< n 1) (> n company-candidates-length)
1068 (error "No candidate number %d" n))
1070 (company-finish (nth n company-candidates))))
1072 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1074 (defconst company-space-strings-limit 100)
1076 (defconst company-space-strings
1078 (dotimes (i company-space-strings-limit)
1079 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1080 (apply 'vector lst)))
1082 (defsubst company-space-string (len)
1083 (if (< len company-space-strings-limit)
1084 (aref company-space-strings len)
1085 (make-string len ?\ )))
1087 (defsubst company-safe-substring (str from &optional to)
1088 (let ((len (length str)))
1091 (if (and to (> to len))
1092 (concat (substring str from)
1093 (company-space-string (- to len)))
1094 (substring str from to)))))
1096 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1098 (defvar company-last-metadata nil)
1099 (make-variable-buffer-local 'company-last-metadata)
1101 (defun company-fetch-metadata ()
1102 (let ((selected (nth company-selection company-candidates)))
1103 (unless (equal selected (car company-last-metadata))
1104 (setq company-last-metadata
1105 (cons selected (funcall company-backend 'meta selected))))
1106 (cdr company-last-metadata)))
1108 (defun company-doc-buffer (&optional string)
1109 (with-current-buffer (get-buffer-create "*Company meta-data*")
1113 (defmacro company-electric (&rest body)
1114 (declare (indent 0) (debug t))
1115 `(when (company-manual-begin)
1116 (save-window-excursion
1117 (let ((height (window-height))
1118 (row (cdr (posn-col-row (posn-at-point)))))
1120 (and (< (window-height) height)
1121 (< (- (window-height) row 2) company-tooltip-limit)
1122 (recenter (- (window-height) row 2)))
1123 (while (eq 'scroll-other-window
1124 (key-binding (vector (list (read-event)))))
1125 (call-interactively 'scroll-other-window))
1126 (when last-input-event
1127 (clear-this-command-keys t)
1128 (setq unread-command-events (list last-input-event)))))))
1130 (defun company-show-doc-buffer ()
1131 "Temporarily show a buffer with the complete documentation for the selection."
1134 (let ((selected (nth company-selection company-candidates)))
1135 (display-buffer (or (funcall company-backend 'doc-buffer selected)
1136 (error "No documentation available")) t))))
1137 (put 'company-show-doc-buffer 'company-keep t)
1139 (defun company-show-location ()
1140 "Temporarily display a buffer showing the selected candidate in context."
1143 (let* ((selected (nth company-selection company-candidates))
1144 (location (funcall company-backend 'location selected))
1145 (pos (or (cdr location) (error "No location available")))
1146 (buffer (or (and (bufferp (car location)) (car location))
1147 (find-file-noselect (car location) t))))
1148 (with-selected-window (display-buffer buffer t)
1149 (if (bufferp (car location))
1152 (set-window-start nil (point))))))
1153 (put 'company-show-location 'company-keep t)
1155 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1157 (defvar company-callback nil)
1158 (make-variable-buffer-local 'company-callback)
1160 (defun company-remove-callback (&optional ignored)
1161 (remove-hook 'company-completion-finished-hook company-callback t)
1162 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t))
1164 (defun company-begin-backend (backend &optional callback)
1165 "Start a completion at point using BACKEND."
1166 (interactive (let ((val (completing-read "Company back-end: "
1168 'functionp nil "company-")))
1170 (list (intern val)))))
1172 (setq company-callback
1173 `(lambda (completion)
1174 (funcall ',callback completion)
1175 (company-remove-callback)))
1176 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1177 (add-hook 'company-completion-finished-hook company-callback nil t))
1178 (setq company-backend backend)
1179 ;; Return non-nil if active.
1180 (or (company-manual-begin)
1181 (error "Cannot complete at point")))
1183 (defun company-begin-with (candidates
1184 &optional prefix-length require-match callback)
1185 "Start a completion at point.
1186 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
1187 the prefix that already is in the buffer before point. It defaults to 0.
1189 CALLBACK is a function called with the selected result if the user successfully
1190 completes the input.
1193 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1194 (company-begin-backend
1195 (let ((start (- (point) (or prefix-length 0))))
1196 `(lambda (command &optional arg &rest ignored)
1197 (case command-history
1198 ('prefix (message "prefix %s" (buffer-substring ,start (point)))
1199 (when (>= (point) ,start)
1200 (buffer-substring ,start (point))))
1201 ('candidates (all-completions arg ',candidates))
1202 ('require-match ,require-match))))
1205 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1207 (defvar company-pseudo-tooltip-overlay nil)
1208 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1210 (defvar company-tooltip-offset 0)
1211 (make-variable-buffer-local 'company-tooltip-offset)
1213 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1216 (setq company-tooltip-offset
1217 (max (min selection company-tooltip-offset)
1218 (- selection -1 limit)))
1220 (when (<= company-tooltip-offset 1)
1222 (setq company-tooltip-offset 0))
1224 (when (>= company-tooltip-offset (- num-lines limit 1))
1226 (when (= selection (1- num-lines))
1227 (decf company-tooltip-offset)
1228 (when (<= company-tooltip-offset 1)
1229 (setq company-tooltip-offset 0)
1236 (defsubst company-round-tab (arg)
1237 (* (/ (+ arg tab-width) tab-width) tab-width))
1239 (defun company-untabify (str)
1240 (let* ((pieces (split-string str "\t"))
1243 (setcar copy (company-safe-substring
1244 (car copy) 0 (company-round-tab (string-width (car copy)))))
1246 (apply 'concat pieces)))
1248 (defun company-fill-propertize (line width selected)
1249 (setq line (company-safe-substring line 0 width))
1250 (add-text-properties 0 width '(face company-tooltip
1251 mouse-face company-tooltip-mouse)
1253 (add-text-properties 0 (length company-common)
1254 '(face company-tooltip-common
1255 mouse-face company-tooltip-mouse)
1258 (if (and company-search-string
1259 (string-match (regexp-quote company-search-string) line
1260 (length company-prefix)))
1262 (add-text-properties (match-beginning 0) (match-end 0)
1263 '(face company-tooltip-selection)
1265 (when (< (match-beginning 0) (length company-common))
1266 (add-text-properties (match-beginning 0) (length company-common)
1267 '(face company-tooltip-common-selection)
1269 (add-text-properties 0 width '(face company-tooltip-selection
1270 mouse-face company-tooltip-selection)
1272 (add-text-properties 0 (length company-common)
1273 '(face company-tooltip-common-selection
1274 mouse-face company-tooltip-selection)
1280 (defun company-buffer-lines (beg end)
1282 (let ((row (cdr (posn-col-row (posn-at-point))))
1284 (while (and (equal (move-to-window-line (incf row)) row)
1286 (push (buffer-substring beg (min end (1- (point)))) lines)
1288 (unless (eq beg end)
1289 (push (buffer-substring beg end) lines))
1292 (defsubst company-modify-line (old new offset)
1293 (concat (company-safe-substring old 0 offset)
1295 (company-safe-substring old (+ offset (length new)))))
1297 (defun company-replacement-string (old lines column nl)
1299 ;; Inject into old lines.
1301 (push (company-modify-line (pop old) (pop lines) column) new))
1302 ;; Append whole new lines.
1304 (push (concat (company-space-string column) (pop lines)) new))
1305 (concat (when nl "\n")
1306 (mapconcat 'identity (nreverse new) "\n")
1309 (defun company-create-lines (column selection limit)
1311 (let ((len company-candidates-length)
1320 ;; Scroll to offset.
1321 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1323 (when (> company-tooltip-offset 0)
1324 (setq previous (format "...(%d)" company-tooltip-offset)))
1326 (setq remainder (- len limit company-tooltip-offset)
1327 remainder (when (> remainder 0)
1328 (setq remainder (format "...(%d)" remainder))))
1330 (decf selection company-tooltip-offset)
1331 (setq width (min (length previous) (length remainder))
1332 lines (nthcdr company-tooltip-offset company-candidates)
1337 (setq width (max (length (pop lines-copy)) width)))
1338 (setq width (min width (- (window-width) column)))
1340 (setq lines-copy lines)
1342 ;; number can make tooltip too long
1343 (and company-show-numbers
1344 (< (setq numbered company-tooltip-offset) 10)
1348 (push (propertize (company-safe-substring previous 0 width)
1349 'face 'company-tooltip)
1353 (push (company-fill-propertize
1354 (if (>= numbered 10)
1355 (company-reformat (pop lines))
1358 (company-safe-substring (company-reformat (pop lines))
1361 width (equal i selection))
1365 (push (propertize (company-safe-substring remainder 0 width)
1366 'face 'company-tooltip)
1369 (setq lines (nreverse new))))
1373 (defsubst company-pseudo-tooltip-height ()
1374 "Calculate the appropriate tooltip height."
1375 (max 3 (min company-tooltip-limit
1376 (- (window-height) 2
1377 (count-lines (window-start) (point-at-bol))))))
1379 (defun company-pseudo-tooltip-show (row column selection)
1380 (company-pseudo-tooltip-hide)
1385 (let* ((height (company-pseudo-tooltip-height))
1386 (lines (company-create-lines column selection height))
1387 (nl (< (move-to-window-line row) row))
1389 (end (save-excursion
1390 (move-to-window-line (+ row height))
1393 (mapcar 'company-untabify (company-buffer-lines beg end)))
1396 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1398 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1399 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1400 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1401 (overlay-put company-pseudo-tooltip-overlay 'company-before
1402 (company-replacement-string old-string lines column nl))
1403 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1405 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1407 (defun company-pseudo-tooltip-show-at-point (pos)
1408 (let ((col-row (posn-col-row (posn-at-point pos))))
1409 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1411 (defun company-pseudo-tooltip-edit (lines selection)
1412 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1413 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1414 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1415 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1416 (lines (company-create-lines column selection height)))
1417 (overlay-put company-pseudo-tooltip-overlay 'company-before
1418 (company-replacement-string old-string lines column nl))))
1420 (defun company-pseudo-tooltip-hide ()
1421 (when company-pseudo-tooltip-overlay
1422 (delete-overlay company-pseudo-tooltip-overlay)
1423 (setq company-pseudo-tooltip-overlay nil)))
1425 (defun company-pseudo-tooltip-hide-temporarily ()
1426 (when (overlayp company-pseudo-tooltip-overlay)
1427 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1428 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1430 (defun company-pseudo-tooltip-unhide ()
1431 (when company-pseudo-tooltip-overlay
1432 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1433 (overlay-put company-pseudo-tooltip-overlay 'before-string
1434 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1435 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1437 (defun company-pseudo-tooltip-frontend (command)
1438 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1440 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1442 (unless (and (overlayp company-pseudo-tooltip-overlay)
1443 (equal (overlay-get company-pseudo-tooltip-overlay
1445 (company-pseudo-tooltip-height)))
1447 (company-pseudo-tooltip-show-at-point (- (point)
1448 (length company-prefix))))
1449 (company-pseudo-tooltip-unhide))
1450 ('hide (company-pseudo-tooltip-hide)
1451 (setq company-tooltip-offset 0))
1452 ('update (when (overlayp company-pseudo-tooltip-overlay)
1453 (company-pseudo-tooltip-edit company-candidates
1454 company-selection)))))
1456 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1457 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1458 (unless (and (eq command 'post-command)
1459 (not (cdr company-candidates)))
1460 (company-pseudo-tooltip-frontend command)))
1462 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1464 (defvar company-preview-overlay nil)
1465 (make-variable-buffer-local 'company-preview-overlay)
1467 (defun company-preview-show-at-point (pos)
1468 (company-preview-hide)
1470 (setq company-preview-overlay (make-overlay pos pos))
1472 (let ((completion(nth company-selection company-candidates)))
1473 (setq completion (propertize completion 'face 'company-preview))
1474 (add-text-properties 0 (length company-common)
1475 '(face company-preview-common) completion)
1477 ;; Add search string
1478 (and company-search-string
1479 (string-match (regexp-quote company-search-string) completion)
1480 (add-text-properties (match-beginning 0)
1482 '(face company-preview-search)
1485 (setq completion (company-strip-prefix completion))
1487 (and (equal pos (point))
1488 (not (equal completion ""))
1489 (add-text-properties 0 1 '(cursor t) completion))
1491 (overlay-put company-preview-overlay 'after-string completion)
1492 (overlay-put company-preview-overlay 'window (selected-window))))
1494 (defun company-preview-hide ()
1495 (when company-preview-overlay
1496 (delete-overlay company-preview-overlay)
1497 (setq company-preview-overlay nil)))
1499 (defun company-preview-frontend (command)
1500 "A `company-mode' front-end showing the selection as if it had been inserted."
1502 ('pre-command (company-preview-hide))
1503 ('post-command (company-preview-show-at-point (point)))
1504 ('hide (company-preview-hide))))
1506 (defun company-preview-if-just-one-frontend (command)
1507 "`company-preview-frontend', but only shown for single candidates."
1508 (unless (and (eq command 'post-command)
1509 (cdr company-candidates))
1510 (company-preview-frontend command)))
1512 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1514 (defvar company-echo-last-msg nil)
1515 (make-variable-buffer-local 'company-echo-last-msg)
1517 (defvar company-echo-timer nil)
1519 (defvar company-echo-delay .1)
1521 (defun company-echo-show (&optional getter)
1523 (setq company-echo-last-msg (funcall getter)))
1524 (let ((message-log-max nil))
1525 (if company-echo-last-msg
1526 (message "%s" company-echo-last-msg)
1529 (defsubst company-echo-show-soon (&optional getter)
1530 (when company-echo-timer
1531 (cancel-timer company-echo-timer))
1532 (setq company-echo-timer (run-with-timer company-echo-delay nil
1533 'company-echo-show getter)))
1535 (defun company-echo-format ()
1537 (let ((limit (window-width (minibuffer-window)))
1539 ;; Roll to selection.
1540 (candidates (nthcdr company-selection company-candidates))
1541 (i (if company-show-numbers company-selection 99999))
1545 (setq comp (company-reformat (pop candidates))
1546 len (+ len 1 (length comp)))
1550 (setq comp (propertize (format "%d: %s" i comp)
1551 'face 'company-echo))
1554 (add-text-properties 3 (+ 3 (length company-common))
1555 '(face company-echo-common) comp))
1556 (setq comp (propertize comp 'face 'company-echo))
1557 (add-text-properties 0 (length company-common)
1558 '(face company-echo-common) comp))
1560 (setq candidates nil)
1563 (mapconcat 'identity (nreverse msg) " ")))
1565 (defun company-echo-strip-common-format ()
1567 (let ((limit (window-width (minibuffer-window)))
1568 (len (+ (length company-prefix) 2))
1569 ;; Roll to selection.
1570 (candidates (nthcdr company-selection company-candidates))
1571 (i (if company-show-numbers company-selection 99999))
1575 (setq comp (company-strip-prefix (pop candidates))
1576 len (+ len 2 (length comp)))
1579 (setq comp (format "%s (%d)" comp i))
1583 (setq candidates nil)
1584 (push (propertize comp 'face 'company-echo) msg)))
1586 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1587 (mapconcat 'identity (nreverse msg) ", ")
1590 (defun company-echo-hide ()
1591 (when company-echo-timer
1592 (cancel-timer company-echo-timer))
1593 (unless (equal company-echo-last-msg "")
1594 (setq company-echo-last-msg "")
1595 (company-echo-show)))
1597 (defun company-echo-frontend (command)
1598 "A `company-mode' front-end showing the candidates in the echo area."
1600 ('pre-command (company-echo-show-soon))
1601 ('post-command (company-echo-show-soon 'company-echo-format))
1602 ('hide (company-echo-hide))))
1604 (defun company-echo-strip-common-frontend (command)
1605 "A `company-mode' front-end showing the candidates in the echo area."
1607 ('pre-command (company-echo-show-soon))
1608 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1609 ('hide (company-echo-hide))))
1611 (defun company-echo-metadata-frontend (command)
1612 "A `company-mode' front-end showing the documentation in the echo area."
1614 ('pre-command (company-echo-show-soon))
1615 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1616 ('hide (company-echo-hide))))
1619 ;;; company.el ends here