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'.
69 ;; Added `company-require-match' and `company-auto-complete' options.
72 ;; Improved Emacs Lisp back-end behavior for local variables.
73 ;; Added `company-elisp-detect-function-context' option.
74 ;; The mouse can now be used for selection.
77 ;; Added `company-show-location'.
78 ;; Added etags back-end.
79 ;; Added work-around for end-of-buffer bug.
80 ;; Added `company-filter-candidates'.
81 ;; More local Lisp variables are now included in the candidates.
84 ;; Fixed elisp documentation buffer always showing the same doc.
85 ;; Added `company-echo-strip-common-frontend'.
86 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
87 ;; Don't hide the echo message if it isn't shown.
94 (eval-when-compile (require 'cl))
96 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
97 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
98 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
99 (add-to-list 'debug-ignored-errors "^Company not ")
100 (add-to-list 'debug-ignored-errors "^No candidate number ")
102 (defgroup company nil
103 "Extensible inline text completion mechanism"
108 (defface company-tooltip
109 '((t :background "yellow"
110 :foreground "black"))
111 "*Face used for the tool tip."
114 (defface company-tooltip-selection
115 '((default :inherit company-tooltip)
116 (((class color) (min-colors 88)) (:background "orange1"))
117 (t (:background "green")))
118 "*Face used for the selection in the tool tip."
121 (defface company-tooltip-mouse
122 '((default :inherit highlight))
123 "*Face used for the tool tip item under the mouse."
126 (defface company-tooltip-common
127 '((t :inherit company-tooltip
129 "*Face used for the common completion in the tool tip."
132 (defface company-tooltip-common-selection
133 '((t :inherit company-tooltip-selection
135 "*Face used for the selected common completion in the tool tip."
138 (defcustom company-tooltip-limit 10
139 "*The maximum number of candidates in the tool tip"
143 (defface company-preview
144 '((t :background "blue4"
145 :foreground "wheat"))
146 "*Face used for the completion preview."
149 (defface company-preview-common
150 '((t :inherit company-preview
152 "*Face used for the common part of the completion preview."
155 (defface company-preview-search
156 '((t :inherit company-preview
157 :background "blue1"))
158 "*Face used for the search string in the completion preview."
161 (defface company-echo nil
162 "*Face used for completions in the echo area."
165 (defface company-echo-common
166 '((((background dark)) (:foreground "firebrick1"))
167 (((background light)) (:background "firebrick4")))
168 "*Face used for the common part of completions in the echo area."
171 (defun company-frontends-set (variable value)
173 (let ((remainder value))
174 (setcdr remainder (delq (car remainder) (cdr remainder))))
175 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
176 (memq 'company-pseudo-tooltip-frontend value)
177 (error "Pseudo tooltip frontend cannot be used twice"))
178 (and (memq 'company-preview-if-just-one-frontend value)
179 (memq 'company-preview-frontend value)
180 (error "Preview frontend cannot be used twice"))
181 (and (memq 'company-echo value)
182 (memq 'company-echo-metadata-frontend value)
183 (error "Echo area cannot be used twice"))
184 ;; preview must come last
185 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
187 (setq value (append (delq f value) (list f)))))
188 (set variable value))
190 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
191 company-preview-frontend
192 company-echo-metadata-frontend)
193 "*The list of active front-ends (visualizations).
194 Each front-end is a function that takes one argument. It is called with
195 one of the following arguments:
197 'show: When the visualization should start.
199 'hide: When the visualization should end.
201 'update: When the data has been updated.
203 'pre-command: Before every command that is executed while the
204 visualization is active.
206 'post-command: After every command that is executed while the
207 visualization is active.
209 The visualized data is stored in `company-prefix', `company-candidates',
210 `company-common', `company-selection', `company-point' and
211 `company-search-string'."
212 :set 'company-frontends-set
214 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
215 (const :tag "echo, strip common"
216 company-echo-strip-common-frontend)
217 (const :tag "show echo meta-data in echo"
218 company-echo-metadata-frontend)
219 (const :tag "pseudo tooltip"
220 company-pseudo-tooltip-frontend)
221 (const :tag "pseudo tooltip, multiple only"
222 company-pseudo-tooltip-unless-just-one-frontend)
223 (const :tag "preview" company-preview-frontend)
224 (const :tag "preview, unique only"
225 company-preview-if-just-one-frontend)
226 (function :tag "custom function" nil))))
228 (defcustom company-backends '(company-elisp company-nxml company-css
229 company-semantic company-gtags company-etags
230 company-oddmuse company-files company-dabbrev)
231 "*The list of active back-ends (completion engines).
232 Each back-end is a function that takes a variable number of arguments.
233 The first argument is the command requested from the back-end. It is one
236 'prefix: The back-end should return the text to be completed. It must be
237 text immediately before `point'. Returning nil passes control to the next
240 'candidates: The second argument is the prefix to be completed. The
241 return value should be a list of candidates that start with the prefix.
245 'sorted: The back-end may return t here to indicate that the candidates
246 are sorted and will not need to be sorted again.
248 'no-cache: Usually company doesn't ask for candidates again as completion
249 progresses, unless the back-end returns t for this command. The second
250 argument is the latest prefix.
252 'meta: The second argument is a completion candidate. The back-end should
253 return a (short) documentation string for it.
255 'doc-buffer: The second argument is a completion candidate. The back-end should
256 create a buffer (preferably with `company-doc-buffer'), fill it with
257 documentation and return it.
259 'location: The second argument is a completion candidate. The back-end can
260 return the cons of buffer and buffer location, or of file and line
261 number where the completion candidate was defined.
263 'require-match: If this value is t, the user is not allowed to enter anything
264 not offering as a candidate. Use with care! The default value nil gives the
265 user that choice with `company-require-match'. Return value 'never overrides
266 that option the other way around.
268 The back-end should return nil for all commands it does not support or
269 does not know about."
271 :type '(repeat (function :tag "function" nil)))
273 (defvar start-count 0)
275 (defcustom company-completion-started-hook nil
276 "*Hook run when company starts completing.
277 The hook is called with one argument that is non-nil if the completion was
282 (defcustom company-completion-cancelled-hook nil
283 "*Hook run when company cancels completing.
284 The hook is called with one argument that is non-nil if the completion was
289 (defcustom company-completion-finished-hook nil
290 "*Hook run when company successfully completes.
291 The hook is called with the selected candidate as an argument."
295 (defcustom company-minimum-prefix-length 3
296 "*The minimum prefix length for automatic completion."
298 :type '(integer :tag "prefix length"))
300 (defcustom company-require-match 'company-explicit-action-p
301 "*If enabled, disallow non-matching input.
302 This can be a function do determine if a match is required.
304 This can be overridden by the back-end, if it returns t or 'never to
305 'require-match. `company-auto-complete' also takes precedence over this."
307 :type '(choice (const :tag "Off" nil)
308 (function :tag "Predicate function")
309 (const :tag "On, if user interaction took place"
310 'company-explicit-action-p)
311 (const :tag "On" t)))
313 (defcustom company-auto-complete '(?\ ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
314 "Determines which characters trigger an automatic completion.
315 If this is a function, it is called with the new input and should return non-nil
316 if company should auto-complete.
318 If this is a string, all characters in that string will complete automatically.
320 A list of characters represent the syntax (see `modify-syntax-entry') of
321 characters that complete automatically."
323 :type '(choice (const :tag "Off" nil)
324 (function :tag "Predicate function")
325 (string :tag "Characters")
327 (const :tag "Whitespace" ?\ )
328 (const :tag "Symbol" ?_)
329 (const :tag "Opening parentheses" ?\()
330 (const :tag "Closing parentheses" ?\))
331 (const :tag "Word constituent" ?w)
332 (const :tag "Punctuation." ?.)
333 (const :tag "String quote." ?\")
334 (const :tag "Paired delimiter." ?$)
335 (const :tag "Expression quote or prefix operator." ?\')
336 (const :tag "Comment starter." ?<)
337 (const :tag "Comment ender." ?>)
338 (const :tag "Character-quote." ?/)
339 (const :tag "Generic string fence." ?|)
340 (const :tag "Generic comment fence." ?!))))
342 (defcustom company-idle-delay .7
343 "*The idle delay in seconds until automatic completions starts.
344 A value of nil means never complete automatically, t means complete
345 immediately when a prefix of `company-minimum-prefix-length' is reached."
347 :type '(choice (const :tag "never (nil)" nil)
348 (const :tag "immediate (t)" t)
349 (number :tag "seconds")))
351 (defcustom company-show-numbers nil
352 "*If enabled, show quick-access numbers for the first ten candidates."
354 :type '(choice (const :tag "off" nil)
355 (const :tag "on" t)))
357 (defvar company-end-of-buffer-workaround t
358 "*Work around a visualization bug when completing at the end of the buffer.
359 The work-around consists of adding a newline.")
361 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363 (defvar company-mode-map (make-sparse-keymap)
364 "Keymap used by `company-mode'.")
366 (defvar company-active-map
367 (let ((keymap (make-sparse-keymap)))
368 (define-key keymap "\e\e\e" 'company-abort)
369 (define-key keymap "\C-g" 'company-abort)
370 (define-key keymap (kbd "M-n") 'company-select-next)
371 (define-key keymap (kbd "M-p") 'company-select-previous)
372 (define-key keymap (kbd "<down>") 'company-select-next)
373 (define-key keymap (kbd "<up>") 'company-select-previous)
374 (define-key keymap [down-mouse-1] 'ignore)
375 (define-key keymap [down-mouse-3] 'ignore)
376 (define-key keymap [mouse-1] 'company-complete-mouse)
377 (define-key keymap [mouse-3] 'company-select-mouse)
378 (define-key keymap [up-mouse-1] 'ignore)
379 (define-key keymap [up-mouse-3] 'ignore)
380 (define-key keymap "\C-m" 'company-complete-selection)
381 (define-key keymap "\t" 'company-complete-common)
382 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
383 (define-key keymap "\C-w" 'company-show-location)
384 (define-key keymap "\C-s" 'company-search-candidates)
385 (define-key keymap "\C-\M-s" 'company-filter-candidates)
387 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
388 `(lambda () (interactive) (company-complete-number ,i))))
391 "Keymap that is enabled during an active completion.")
394 (define-minor-mode company-mode
395 "\"complete anything\"; in in-buffer completion framework.
396 Completion starts automatically, depending on the values
397 `company-idle-delay' and `company-minimum-prefix-length'.
399 Completion can be controlled with the commands:
400 `company-complete-common', `company-complete-selection', `company-complete',
401 `company-select-next', `company-select-previous'. If these commands are
402 called before `company-idle-delay', completion will also start.
404 Completions can be searched with `company-search-candidates' or
405 `company-filter-candidates'. These can be used while completion is
408 The completion data is retrieved using `company-backends' and displayed using
411 regular keymap (`company-mode-map'):
414 keymap during active completions (`company-active-map'):
416 \\{company-active-map}"
417 nil " comp" company-mode-map
420 (add-hook 'pre-command-hook 'company-pre-command nil t)
421 (add-hook 'post-command-hook 'company-post-command nil t)
422 (dolist (backend company-backends)
423 (unless (fboundp backend)
424 (ignore-errors (require backend nil t)))
425 (unless (fboundp backend)
426 (message "Company back-end '%s' could not be initialized"
428 (remove-hook 'pre-command-hook 'company-pre-command t)
429 (remove-hook 'post-command-hook 'company-post-command t)
431 (kill-local-variable 'company-point)))
433 (defsubst company-assert-enabled ()
435 (company-uninstall-map)
436 (error "Company not enabled")))
438 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
440 (defvar company-overriding-keymap-bound nil)
441 (make-variable-buffer-local 'company-overriding-keymap-bound)
443 (defvar company-old-keymap nil)
444 (make-variable-buffer-local 'company-old-keymap)
446 (defvar company-my-keymap nil)
447 (make-variable-buffer-local 'company-my-keymap)
449 (defsubst company-enable-overriding-keymap (keymap)
450 (setq company-my-keymap keymap)
451 (when company-overriding-keymap-bound
452 (company-uninstall-map)))
454 (defun company-install-map ()
455 (unless (or company-overriding-keymap-bound
456 (null company-my-keymap))
457 (setq company-old-keymap overriding-terminal-local-map
458 overriding-terminal-local-map company-my-keymap
459 company-overriding-keymap-bound t)))
461 (defun company-uninstall-map ()
462 (when (eq overriding-terminal-local-map company-my-keymap)
463 (setq overriding-terminal-local-map company-old-keymap
464 company-overriding-keymap-bound nil)))
467 ;; Emacs calculates the active keymaps before reading the event. That means we
468 ;; cannot change the keymap from a timer. So we send a bogus command.
469 (defun company-ignore ()
472 (global-set-key '[31415926] 'company-ignore)
474 (defun company-input-noop ()
475 (push 31415926 unread-command-events))
477 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
479 (defun company-grab (regexp &optional expression)
480 (when (looking-back regexp)
481 (or (match-string-no-properties (or expression 0)) "")))
483 (defun company-in-string-or-comment (&optional point)
484 (let ((pos (syntax-ppss)))
485 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
487 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
489 (defvar company-backend nil)
490 (make-variable-buffer-local 'company-backend)
492 (defvar company-prefix nil)
493 (make-variable-buffer-local 'company-prefix)
495 (defvar company-candidates nil)
496 (make-variable-buffer-local 'company-candidates)
498 (defvar company-candidates-length nil)
499 (make-variable-buffer-local 'company-candidates-length)
501 (defvar company-candidates-cache nil)
502 (make-variable-buffer-local 'company-candidates-cache)
504 (defvar company-candidates-predicate nil)
505 (make-variable-buffer-local 'company-candidates-predicate)
507 (defvar company-common nil)
508 (make-variable-buffer-local 'company-common)
510 (defvar company-selection 0)
511 (make-variable-buffer-local 'company-selection)
513 (defvar company-selection-changed nil)
514 (make-variable-buffer-local 'company-selection-changed)
516 (defvar company--explicit-action nil
517 "Non-nil, if explicit completion took place.")
518 (make-variable-buffer-local 'company--explicit-action)
520 (defvar company-point nil)
521 (make-variable-buffer-local 'company-point)
523 (defvar company-timer nil)
525 (defvar company-added-newline nil)
526 (make-variable-buffer-local 'company-added-newline)
528 (defsubst company-strip-prefix (str)
529 (substring str (length company-prefix)))
531 (defun company-explicit-action-p ()
532 "Return whether explicit completion action was taken by the user."
533 (or company--explicit-action
534 company-selection-changed))
536 (defsubst company-reformat (candidate)
537 ;; company-ispell needs this, because the results are always lower-case
538 ;; It's mory efficient to fix it only when they are displayed.
539 (concat company-prefix (substring candidate (length company-prefix))))
541 (defsubst company-should-complete (prefix)
542 (and (eq company-idle-delay t)
543 (not (and transient-mark-mode mark-active))
544 (>= (length prefix) company-minimum-prefix-length)))
546 (defsubst company-call-frontends (command)
547 (dolist (frontend company-frontends)
549 (funcall frontend command)
550 (error (error "Company: Front-end %s error \"%s\" on command %s"
551 frontend (error-message-string err) command)))))
553 (defsubst company-set-selection (selection &optional force-update)
554 (setq selection (max 0 (min (1- company-candidates-length) selection)))
555 (when (or force-update (not (equal selection company-selection)))
556 (setq company-selection selection
557 company-selection-changed t)
558 (company-call-frontends 'update)))
560 (defun company-apply-predicate (candidates predicate)
562 (dolist (c candidates)
563 (when (funcall predicate c)
567 (defun company-update-candidates (candidates)
568 (setq company-candidates-length (length candidates))
569 (if (> company-selection 0)
570 ;; Try to restore the selection
571 (let ((selected (nth company-selection company-candidates)))
572 (setq company-selection 0
573 company-candidates candidates)
575 (while (and candidates (string< (pop candidates) selected))
576 (incf company-selection))
578 ;; Make sure selection isn't out of bounds.
579 (setq company-selection (min (1- company-candidates-length)
580 company-selection)))))
581 (setq company-selection 0
582 company-candidates candidates))
584 (push (cons company-prefix company-candidates) company-candidates-cache)
586 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
587 (setq company-common (try-completion company-prefix company-candidates)))
588 (when (eq company-common t)
589 (setq company-candidates nil)))
591 (defun company-calculate-candidates (prefix)
593 (or (cdr (assoc prefix company-candidates-cache))
594 (when company-candidates-cache
595 (let ((len (length prefix))
596 (completion-ignore-case (funcall company-backend
600 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
601 company-candidates-cache)))
602 (return (all-completions prefix prev))))))
603 (let ((c (funcall company-backend 'candidates prefix)))
604 (when company-candidates-predicate
605 (setq c (company-apply-predicate
606 c company-candidates-predicate)))
607 (unless (funcall company-backend 'sorted)
608 (setq c (sort c 'string<)))
610 (if (or (cdr candidates)
611 (not (equal (car candidates) prefix)))
612 ;; Don't start when already completed and unique.
614 ;; Not the right place? maybe when setting?
615 (and company-candidates t))))
617 (defun company-idle-begin (buf win tick pos)
619 (eq buf (current-buffer))
620 (eq win (selected-window))
621 (eq tick (buffer-chars-modified-tick))
623 (not company-candidates)
624 (not (equal (point) company-point))
625 (let ((company-idle-delay t))
627 (when company-candidates
629 (company-post-command)))))
631 (defun company-manual-begin ()
633 (company-assert-enabled)
635 (not company-candidates)
636 (let ((company-idle-delay t)
637 (company-minimum-prefix-length 0))
638 (setq company--explicit-action t)
640 ;; Return non-nil if active.
643 (defsubst company-incremental-p (old-prefix new-prefix)
644 (and (> (length new-prefix) (length old-prefix))
645 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
647 (defun company-require-match-p ()
648 (let ((backend-value (funcall company-backend 'require-match)))
649 (or (eq backend-value t)
650 (and (if (functionp company-require-match)
651 (funcall company-require-match)
652 (eq company-require-match t))
653 (not (eq backend-value 'never))))))
655 (defun company-punctuation-p (input)
656 "Return non-nil, if input starts with punctuation or parentheses."
657 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
659 (defun company-auto-complete-p (beg end)
660 "Return non-nil, if input starts with punctuation or parentheses."
662 (if (functionp company-auto-complete)
663 (funcall company-auto-complete (buffer-substring beg end))
664 (if (consp company-auto-complete)
665 (memq (char-syntax (char-after beg)) company-auto-complete)
666 (string-match (buffer-substring beg (1+ beg))
667 company-auto-complete)))))
669 (defun company-continue ()
670 (when company-candidates
671 (when (funcall company-backend 'no-cache company-prefix)
672 ;; Don't complete existing candidates, fetch new ones.
673 (setq company-candidates-cache nil))
674 (let ((new-prefix (funcall company-backend 'prefix)))
675 (unless (and (= (- (point) (length new-prefix))
676 (- company-point (length company-prefix)))
677 (or (equal company-prefix new-prefix)
678 (let ((c (company-calculate-candidates new-prefix)))
679 ;; t means complete/unique.
681 (progn (company-cancel new-prefix) t)
683 (setq company-prefix new-prefix)
684 (company-update-candidates c)
686 (if (company-auto-complete-p company-point (point))
688 (goto-char company-point)
689 (company-complete-selection)
690 (setq company-candidates nil))
691 (if (not (and (company-incremental-p company-prefix new-prefix)
692 (company-require-match-p)))
694 (when (equal company-prefix (car company-candidates))
695 ;; cancel, but last input was actually success
696 (company-cancel company-prefix))
697 (setq company-candidates nil))
698 (backward-delete-char (length new-prefix))
699 (insert company-prefix)
701 (message "Matching input is required")))
702 company-candidates))))
704 (defun company-begin ()
705 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
706 ;; Don't complete in these cases.
707 (setq company-candidates nil)
709 (unless company-candidates
711 (dolist (backend company-backends)
712 (when (and (fboundp backend)
713 (setq prefix (funcall backend 'prefix)))
714 (setq company-backend backend)
715 (when (company-should-complete prefix)
716 (let ((c (company-calculate-candidates prefix)))
717 ;; t means complete/unique. We don't start, so no hooks.
719 (setq company-prefix prefix)
720 (company-update-candidates c)
721 (run-hook-with-args 'company-completion-started-hook
722 (company-explicit-action-p))
723 (company-call-frontends 'show))))
725 (if company-candidates
727 (when (and company-end-of-buffer-workaround (eobp))
728 (save-excursion (insert "\n"))
729 (setq company-added-newline (buffer-chars-modified-tick)))
730 (setq company-point (point))
731 (company-enable-overriding-keymap company-active-map)
732 (company-call-frontends 'update))
735 (defun company-cancel (&optional result)
736 (and company-added-newline
737 (> (point-max) (point-min))
738 (let ((tick (buffer-chars-modified-tick)))
739 (delete-region (1- (point-max)) (point-max))
740 (equal tick company-added-newline))
741 ;; Only set unmodified when tick remained the same since insert.
742 (set-buffer-modified-p nil))
745 (run-hook-with-args 'company-completion-finished-hook result)
746 (run-hook-with-args 'company-completion-cancelled-hook result)))
747 (setq company-added-newline nil
750 company-candidates nil
751 company-candidates-length nil
752 company-candidates-cache nil
753 company-candidates-predicate nil
756 company-selection-changed nil
757 company--explicit-action nil
760 (cancel-timer company-timer))
761 (company-search-mode 0)
762 (company-call-frontends 'hide)
763 (company-enable-overriding-keymap nil))
765 (defun company-abort ()
768 ;; Don't start again, unless started manually.
769 (setq company-point (point)))
771 (defun company-finish (result)
772 (insert (company-strip-prefix result))
773 (company-cancel result)
774 ;; Don't start again, unless started manually.
775 (setq company-point (point)))
777 (defsubst company-keep (command)
778 (and (symbolp command) (get command 'company-keep)))
780 (defun company-pre-command ()
781 (unless (company-keep this-command)
783 (when company-candidates
784 (company-call-frontends 'pre-command))
785 (error (message "Company: An error occurred in pre-command")
786 (message "%s" (error-message-string err))
789 (cancel-timer company-timer))
790 (company-uninstall-map))
792 (defun company-post-command ()
793 (unless (company-keep this-command)
796 (unless (equal (point) company-point)
798 (when company-candidates
799 (company-call-frontends 'post-command))
800 (when (numberp company-idle-delay)
802 (run-with-timer company-idle-delay nil 'company-idle-begin
803 (current-buffer) (selected-window)
804 (buffer-chars-modified-tick) (point)))))
805 (error (message "Company: An error occurred in post-command")
806 (message "%s" (error-message-string err))
808 (company-install-map))
810 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
812 (defvar company-search-string nil)
813 (make-variable-buffer-local 'company-search-string)
815 (defvar company-search-lighter " Search: \"\"")
816 (make-variable-buffer-local 'company-search-lighter)
818 (defvar company-search-old-map nil)
819 (make-variable-buffer-local 'company-search-old-map)
821 (defvar company-search-old-selection 0)
822 (make-variable-buffer-local 'company-search-old-selection)
824 (defun company-search (text lines)
825 (let ((quoted (regexp-quote text))
828 (when (string-match quoted line (length company-prefix))
832 (defun company-search-printing-char ()
834 (company-search-assert-enabled)
835 (setq company-search-string
836 (concat (or company-search-string "") (string last-command-event))
837 company-search-lighter (concat " Search: \"" company-search-string
839 (let ((pos (company-search company-search-string
840 (nthcdr company-selection company-candidates))))
843 (company-set-selection (+ company-selection pos) t))))
845 (defun company-search-repeat-forward ()
846 "Repeat the incremental search in completion candidates forward."
848 (company-search-assert-enabled)
849 (let ((pos (company-search company-search-string
850 (cdr (nthcdr company-selection
851 company-candidates)))))
854 (company-set-selection (+ company-selection pos 1) t))))
856 (defun company-search-repeat-backward ()
857 "Repeat the incremental search in completion candidates backwards."
859 (company-search-assert-enabled)
860 (let ((pos (company-search company-search-string
861 (nthcdr (- company-candidates-length
863 (reverse company-candidates)))))
866 (company-set-selection (- company-selection pos 1) t))))
868 (defun company-create-match-predicate ()
869 (setq company-candidates-predicate
871 ,(if company-candidates-predicate
872 `(and (string-match ,company-search-string candidate)
873 (funcall ,company-candidates-predicate
875 `(string-match ,company-search-string candidate))))
876 (company-update-candidates
877 (company-apply-predicate company-candidates company-candidates-predicate))
879 (setq company-candidates-cache (cons company-prefix company-candidates)))
881 (defun company-filter-printing-char ()
883 (company-search-assert-enabled)
884 (company-search-printing-char)
885 (company-create-match-predicate)
886 (company-call-frontends 'update))
888 (defun company-search-kill-others ()
889 "Limit the completion candidates to the ones matching the search string."
891 (company-search-assert-enabled)
892 (company-create-match-predicate)
893 (company-search-mode 0)
894 (company-call-frontends 'update))
896 (defun company-search-abort ()
897 "Abort searching the completion candidates."
899 (company-search-assert-enabled)
900 (company-set-selection company-search-old-selection t)
901 (company-search-mode 0))
903 (defun company-search-other-char ()
905 (company-search-assert-enabled)
906 (company-search-mode 0)
907 (when last-input-event
908 (clear-this-command-keys t)
909 (setq unread-command-events (list last-input-event))))
911 (defvar company-search-map
913 (keymap (make-keymap)))
914 (if (fboundp 'max-char)
915 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
916 'company-search-printing-char)
918 ;; obselete in Emacs 23
919 (let ((l (generic-character-list))
920 (table (nth 1 keymap)))
922 (set-char-table-default table (car l) 'company-search-printing-char)
924 (define-key keymap [t] 'company-search-other-char)
926 (define-key keymap (make-string 1 i) 'company-search-other-char)
929 (define-key keymap (vector i) 'company-search-printing-char)
931 (let ((meta-map (make-sparse-keymap)))
932 (define-key keymap (char-to-string meta-prefix-char) meta-map)
933 (define-key keymap [escape] meta-map))
934 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
935 (define-key keymap "\e\e\e" 'company-search-other-char)
936 (define-key keymap [escape escape escape] 'company-search-other-char)
938 (define-key keymap "\C-g" 'company-search-abort)
939 (define-key keymap "\C-s" 'company-search-repeat-forward)
940 (define-key keymap "\C-r" 'company-search-repeat-backward)
941 (define-key keymap "\C-o" 'company-search-kill-others)
943 "Keymap used for incrementally searching the completion candidates.")
945 (define-minor-mode company-search-mode
946 "Search mode for completion candidates.
947 Don't start this directly, use `company-search-candidates' or
948 `company-filter-candidates'."
949 nil company-search-lighter nil
950 (if company-search-mode
951 (if (company-manual-begin)
953 (setq company-search-old-selection company-selection)
954 (company-call-frontends 'update))
955 (setq company-search-mode nil))
956 (kill-local-variable 'company-search-string)
957 (kill-local-variable 'company-search-lighter)
958 (kill-local-variable 'company-search-old-selection)
959 (company-enable-overriding-keymap company-active-map)))
961 (defsubst company-search-assert-enabled ()
962 (company-assert-enabled)
963 (unless company-search-mode
964 (company-uninstall-map)
965 (error "Company not in search mode")))
967 (defun company-search-candidates ()
968 "Start searching the completion candidates incrementally.
970 \\<company-search-map>Search can be controlled with the commands:
971 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
972 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
973 - `company-search-abort' (\\[company-search-abort])
975 Regular characters are appended to the search string.
977 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
978 the search string to limit the completion candidates."
980 (company-search-mode 1)
981 (company-enable-overriding-keymap company-search-map))
983 (defvar company-filter-map
984 (let ((keymap (make-keymap)))
985 (define-key keymap [remap company-search-printing-char]
986 'company-filter-printing-char)
987 (set-keymap-parent keymap company-search-map)
989 "Keymap used for incrementally searching the completion candidates.")
991 (defun company-filter-candidates ()
992 "Start filtering the completion candidates incrementally.
993 This works the same way as `company-search-candidates' immediately
994 followed by `company-search-kill-others' after each input."
996 (company-search-mode 1)
997 (company-enable-overriding-keymap company-filter-map))
999 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1001 (defun company-select-next ()
1002 "Select the next candidate in the list."
1004 (when (company-manual-begin)
1005 (company-set-selection (1+ company-selection))))
1007 (defun company-select-previous ()
1008 "Select the previous candidate in the list."
1010 (when (company-manual-begin)
1011 (company-set-selection (1- company-selection))))
1013 (defun company-select-mouse (event)
1014 "Select the candidate picked by the mouse."
1016 (when (nth 4 (event-start event))
1017 (company-set-selection (- (cdr (posn-col-row (event-start event)))
1018 (cdr (posn-col-row (posn-at-point)))
1022 (defun company-complete-mouse (event)
1023 "Complete the candidate picked by the mouse."
1025 (when (company-select-mouse event)
1026 (company-complete-selection)))
1028 (defun company-complete-selection ()
1029 "Complete the selected candidate."
1031 (when (company-manual-begin)
1032 (company-finish (nth company-selection company-candidates))))
1034 (defun company-complete-common ()
1035 "Complete the common part of all candidates."
1037 (when (company-manual-begin)
1038 (if (equal company-common (car company-candidates))
1039 ;; for success message
1040 (company-complete-selection)
1041 (insert (company-strip-prefix company-common)))))
1043 (defun company-complete ()
1044 "Complete the common part of all candidates or the current selection.
1045 The first time this is called, the common part is completed, the second time, or
1046 when the selection has been changed, the selected candidate is completed."
1048 (when (company-manual-begin)
1049 (if (or company-selection-changed
1050 (eq last-command 'company-complete-common))
1051 (call-interactively 'company-complete-selection)
1052 (call-interactively 'company-complete-common)
1053 (setq this-command 'company-complete-common))))
1055 (defun company-complete-number (n)
1056 "Complete the Nth candidate."
1057 (when (company-manual-begin)
1058 (and (< n 1) (> n company-candidates-length)
1059 (error "No candidate number %d" n))
1061 (company-finish (nth n company-candidates))))
1063 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1065 (defconst company-space-strings-limit 100)
1067 (defconst company-space-strings
1069 (dotimes (i company-space-strings-limit)
1070 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1071 (apply 'vector lst)))
1073 (defsubst company-space-string (len)
1074 (if (< len company-space-strings-limit)
1075 (aref company-space-strings len)
1076 (make-string len ?\ )))
1078 (defsubst company-safe-substring (str from &optional to)
1079 (let ((len (length str)))
1082 (if (and to (> to len))
1083 (concat (substring str from)
1084 (company-space-string (- to len)))
1085 (substring str from to)))))
1087 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1089 (defvar company-last-metadata nil)
1090 (make-variable-buffer-local 'company-last-metadata)
1092 (defun company-fetch-metadata ()
1093 (let ((selected (nth company-selection company-candidates)))
1094 (unless (equal selected (car company-last-metadata))
1095 (setq company-last-metadata
1096 (cons selected (funcall company-backend 'meta selected))))
1097 (cdr company-last-metadata)))
1099 (defun company-doc-buffer (&optional string)
1100 (with-current-buffer (get-buffer-create "*Company meta-data*")
1104 (defmacro company-electric (&rest body)
1105 (declare (indent 0) (debug t))
1106 `(when (company-manual-begin)
1107 (save-window-excursion
1108 (let ((height (window-height))
1109 (row (cdr (posn-col-row (posn-at-point)))))
1111 (and (< (window-height) height)
1112 (< (- (window-height) row 2) company-tooltip-limit)
1113 (recenter (- (window-height) row 2)))
1114 (while (eq 'scroll-other-window
1115 (key-binding (vector (list (read-event)))))
1116 (call-interactively 'scroll-other-window))
1117 (when last-input-event
1118 (clear-this-command-keys t)
1119 (setq unread-command-events (list last-input-event)))))))
1121 (defun company-show-doc-buffer ()
1122 "Temporarily show a buffer with the complete documentation for the selection."
1125 (let ((selected (nth company-selection company-candidates)))
1126 (display-buffer (or (funcall company-backend 'doc-buffer selected)
1127 (error "No documentation available")) t))))
1128 (put 'company-show-doc-buffer 'company-keep t)
1130 (defun company-show-location ()
1131 "Temporarily display a buffer showing the selected candidate in context."
1134 (let* ((selected (nth company-selection company-candidates))
1135 (location (funcall company-backend 'location selected))
1136 (pos (or (cdr location) (error "No location available")))
1137 (buffer (or (and (bufferp (car location)) (car location))
1138 (find-file-noselect (car location) t))))
1139 (with-selected-window (display-buffer buffer t)
1140 (if (bufferp (car location))
1143 (set-window-start nil (point))))))
1144 (put 'company-show-location 'company-keep t)
1146 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1148 (defvar company-pseudo-tooltip-overlay nil)
1149 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1151 (defvar company-tooltip-offset 0)
1152 (make-variable-buffer-local 'company-tooltip-offset)
1154 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1157 (setq company-tooltip-offset
1158 (max (min selection company-tooltip-offset)
1159 (- selection -1 limit)))
1161 (when (<= company-tooltip-offset 1)
1163 (setq company-tooltip-offset 0))
1165 (when (>= company-tooltip-offset (- num-lines limit 1))
1167 (when (= selection (1- num-lines))
1168 (decf company-tooltip-offset)
1169 (when (<= company-tooltip-offset 1)
1170 (setq company-tooltip-offset 0)
1177 (defsubst company-round-tab (arg)
1178 (* (/ (+ arg tab-width) tab-width) tab-width))
1180 (defun company-untabify (str)
1181 (let* ((pieces (split-string str "\t"))
1184 (setcar copy (company-safe-substring
1185 (car copy) 0 (company-round-tab (string-width (car copy)))))
1187 (apply 'concat pieces)))
1189 (defun company-fill-propertize (line width selected)
1190 (setq line (company-safe-substring line 0 width))
1191 (add-text-properties 0 width '(face company-tooltip
1192 mouse-face company-tooltip-mouse)
1194 (add-text-properties 0 (length company-common)
1195 '(face company-tooltip-common
1196 mouse-face company-tooltip-mouse)
1199 (if (and company-search-string
1200 (string-match (regexp-quote company-search-string) line
1201 (length company-prefix)))
1203 (add-text-properties (match-beginning 0) (match-end 0)
1204 '(face company-tooltip-selection)
1206 (when (< (match-beginning 0) (length company-common))
1207 (add-text-properties (match-beginning 0) (length company-common)
1208 '(face company-tooltip-common-selection)
1210 (add-text-properties 0 width '(face company-tooltip-selection
1211 mouse-face company-tooltip-selection)
1213 (add-text-properties 0 (length company-common)
1214 '(face company-tooltip-common-selection
1215 mouse-face company-tooltip-selection)
1221 (defun company-buffer-lines (beg end)
1223 (let ((row (cdr (posn-col-row (posn-at-point))))
1225 (while (and (equal (move-to-window-line (incf row)) row)
1227 (push (buffer-substring beg (min end (1- (point)))) lines)
1229 (unless (eq beg end)
1230 (push (buffer-substring beg end) lines))
1233 (defsubst company-modify-line (old new offset)
1234 (concat (company-safe-substring old 0 offset)
1236 (company-safe-substring old (+ offset (length new)))))
1238 (defun company-replacement-string (old lines column nl)
1240 ;; Inject into old lines.
1242 (push (company-modify-line (pop old) (pop lines) column) new))
1243 ;; Append whole new lines.
1245 (push (concat (company-space-string column) (pop lines)) new))
1246 (concat (when nl "\n")
1247 (mapconcat 'identity (nreverse new) "\n")
1250 (defun company-create-lines (column selection limit)
1252 (let ((len company-candidates-length)
1261 ;; Scroll to offset.
1262 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1264 (when (> company-tooltip-offset 0)
1265 (setq previous (format "...(%d)" company-tooltip-offset)))
1267 (setq remainder (- len limit company-tooltip-offset)
1268 remainder (when (> remainder 0)
1269 (setq remainder (format "...(%d)" remainder))))
1271 (decf selection company-tooltip-offset)
1272 (setq width (min (length previous) (length remainder))
1273 lines (nthcdr company-tooltip-offset company-candidates)
1278 (setq width (max (length (pop lines-copy)) width)))
1279 (setq width (min width (- (window-width) column)))
1281 (setq lines-copy lines)
1283 ;; number can make tooltip too long
1284 (and company-show-numbers
1285 (< (setq numbered company-tooltip-offset) 10)
1289 (push (propertize (company-safe-substring previous 0 width)
1290 'face 'company-tooltip)
1294 (push (company-fill-propertize
1295 (if (>= numbered 10)
1296 (company-reformat (pop lines))
1299 (company-safe-substring (company-reformat (pop lines))
1302 width (equal i selection))
1306 (push (propertize (company-safe-substring remainder 0 width)
1307 'face 'company-tooltip)
1310 (setq lines (nreverse new))))
1314 (defsubst company-pseudo-tooltip-height ()
1315 "Calculate the appropriate tooltip height."
1316 (max 3 (min company-tooltip-limit
1317 (- (window-height) 2
1318 (count-lines (window-start) (point-at-bol))))))
1320 (defun company-pseudo-tooltip-show (row column selection)
1321 (company-pseudo-tooltip-hide)
1326 (let* ((height (company-pseudo-tooltip-height))
1327 (lines (company-create-lines column selection height))
1328 (nl (< (move-to-window-line row) row))
1330 (end (save-excursion
1331 (move-to-window-line (+ row height))
1334 (mapcar 'company-untabify (company-buffer-lines beg end)))
1337 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1339 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1340 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1341 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1342 (overlay-put company-pseudo-tooltip-overlay 'company-before
1343 (company-replacement-string old-string lines column nl))
1344 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1346 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1348 (defun company-pseudo-tooltip-show-at-point (pos)
1349 (let ((col-row (posn-col-row (posn-at-point pos))))
1350 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1352 (defun company-pseudo-tooltip-edit (lines selection)
1353 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1354 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1355 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1356 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1357 (lines (company-create-lines column selection height)))
1358 (overlay-put company-pseudo-tooltip-overlay 'company-before
1359 (company-replacement-string old-string lines column nl))))
1361 (defun company-pseudo-tooltip-hide ()
1362 (when company-pseudo-tooltip-overlay
1363 (delete-overlay company-pseudo-tooltip-overlay)
1364 (setq company-pseudo-tooltip-overlay nil)))
1366 (defun company-pseudo-tooltip-hide-temporarily ()
1367 (when (overlayp company-pseudo-tooltip-overlay)
1368 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1369 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1371 (defun company-pseudo-tooltip-unhide ()
1372 (when company-pseudo-tooltip-overlay
1373 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1374 (overlay-put company-pseudo-tooltip-overlay 'before-string
1375 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1376 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1378 (defun company-pseudo-tooltip-frontend (command)
1379 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1381 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1383 (unless (and (overlayp company-pseudo-tooltip-overlay)
1384 (equal (overlay-get company-pseudo-tooltip-overlay
1386 (company-pseudo-tooltip-height)))
1388 (company-pseudo-tooltip-show-at-point (- (point)
1389 (length company-prefix))))
1390 (company-pseudo-tooltip-unhide))
1391 ('hide (company-pseudo-tooltip-hide)
1392 (setq company-tooltip-offset 0))
1393 ('update (when (overlayp company-pseudo-tooltip-overlay)
1394 (company-pseudo-tooltip-edit company-candidates
1395 company-selection)))))
1397 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1398 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1399 (unless (and (eq command 'post-command)
1400 (not (cdr company-candidates)))
1401 (company-pseudo-tooltip-frontend command)))
1403 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1405 (defvar company-preview-overlay nil)
1406 (make-variable-buffer-local 'company-preview-overlay)
1408 (defun company-preview-show-at-point (pos)
1409 (company-preview-hide)
1411 (setq company-preview-overlay (make-overlay pos pos))
1413 (let ((completion(nth company-selection company-candidates)))
1414 (setq completion (propertize completion 'face 'company-preview))
1415 (add-text-properties 0 (length company-common)
1416 '(face company-preview-common) completion)
1418 ;; Add search string
1419 (and company-search-string
1420 (string-match (regexp-quote company-search-string) completion)
1421 (add-text-properties (match-beginning 0)
1423 '(face company-preview-search)
1426 (setq completion (company-strip-prefix completion))
1428 (and (equal pos (point))
1429 (not (equal completion ""))
1430 (add-text-properties 0 1 '(cursor t) completion))
1432 (overlay-put company-preview-overlay 'after-string completion)
1433 (overlay-put company-preview-overlay 'window (selected-window))))
1435 (defun company-preview-hide ()
1436 (when company-preview-overlay
1437 (delete-overlay company-preview-overlay)
1438 (setq company-preview-overlay nil)))
1440 (defun company-preview-frontend (command)
1441 "A `company-mode' front-end showing the selection as if it had been inserted."
1443 ('pre-command (company-preview-hide))
1444 ('post-command (company-preview-show-at-point (point)))
1445 ('hide (company-preview-hide))))
1447 (defun company-preview-if-just-one-frontend (command)
1448 "`company-preview-frontend', but only shown for single candidates."
1449 (unless (and (eq command 'post-command)
1450 (cdr company-candidates))
1451 (company-preview-frontend command)))
1453 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1455 (defvar company-echo-last-msg nil)
1456 (make-variable-buffer-local 'company-echo-last-msg)
1458 (defvar company-echo-timer nil)
1460 (defvar company-echo-delay .1)
1462 (defun company-echo-show (&optional getter)
1464 (setq company-echo-last-msg (funcall getter)))
1465 (let ((message-log-max nil))
1466 (if company-echo-last-msg
1467 (message "%s" company-echo-last-msg)
1470 (defsubst company-echo-show-soon (&optional getter)
1471 (when company-echo-timer
1472 (cancel-timer company-echo-timer))
1473 (setq company-echo-timer (run-with-timer company-echo-delay nil
1474 'company-echo-show getter)))
1476 (defun company-echo-format ()
1478 (let ((limit (window-width (minibuffer-window)))
1480 ;; Roll to selection.
1481 (candidates (nthcdr company-selection company-candidates))
1482 (i (if company-show-numbers company-selection 99999))
1486 (setq comp (company-reformat (pop candidates))
1487 len (+ len 1 (length comp)))
1491 (setq comp (propertize (format "%d: %s" i comp)
1492 'face 'company-echo))
1495 (add-text-properties 3 (+ 3 (length company-common))
1496 '(face company-echo-common) comp))
1497 (setq comp (propertize comp 'face 'company-echo))
1498 (add-text-properties 0 (length company-common)
1499 '(face company-echo-common) comp))
1501 (setq candidates nil)
1504 (mapconcat 'identity (nreverse msg) " ")))
1506 (defun company-echo-strip-common-format ()
1508 (let ((limit (window-width (minibuffer-window)))
1509 (len (+ (length company-prefix) 2))
1510 ;; Roll to selection.
1511 (candidates (nthcdr company-selection company-candidates))
1512 (i (if company-show-numbers company-selection 99999))
1516 (setq comp (company-strip-prefix (pop candidates))
1517 len (+ len 2 (length comp)))
1520 (setq comp (format "%s (%d)" comp i))
1524 (setq candidates nil)
1525 (push (propertize comp 'face 'company-echo) msg)))
1527 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1528 (mapconcat 'identity (nreverse msg) ", ")
1531 (defun company-echo-hide ()
1532 (when company-echo-timer
1533 (cancel-timer company-echo-timer))
1534 (unless (equal company-echo-last-msg "")
1535 (setq company-echo-last-msg "")
1536 (company-echo-show)))
1538 (defun company-echo-frontend (command)
1539 "A `company-mode' front-end showing the candidates in the echo area."
1541 ('pre-command (company-echo-show-soon))
1542 ('post-command (company-echo-show-soon 'company-echo-format))
1543 ('hide (company-echo-hide))))
1545 (defun company-echo-strip-common-frontend (command)
1546 "A `company-mode' front-end showing the candidates in the echo area."
1548 ('pre-command (company-echo-show-soon))
1549 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1550 ('hide (company-echo-hide))))
1552 (defun company-echo-metadata-frontend (command)
1553 "A `company-mode' front-end showing the documentation in the echo area."
1555 ('pre-command (company-echo-show-soon))
1556 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1557 ('hide (company-echo-hide))))
1560 ;;; company.el ends here