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))))
53 ;; When point is at the very end of the buffer, the pseudo-tooltip appears very
59 ;; Fixed elisp documentation buffer always showing the same doc.
60 ;; Added `company-echo-strip-common-frontend'.
61 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
62 ;; Don't hide the echo message if it isn't shown.
69 (eval-when-compile (require 'cl))
71 (add-to-list 'debug-ignored-errors
72 "^Pseudo tooltip frontend cannot be used twice$")
73 (add-to-list 'debug-ignored-errors "^Preview frontend cannot be used twice$")
74 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
75 (add-to-list 'debug-ignored-errors "^No documentation available$")
76 (add-to-list 'debug-ignored-errors "^Company not enabled$")
77 (add-to-list 'debug-ignored-errors "^Company not in search mode$")
78 (add-to-list 'debug-ignored-errors "^No candidate number ")
81 "Extensible inline text completion mechanism"
86 (defface company-tooltip
87 '((t :background "yellow"
89 "*Face used for the tool tip."
92 (defface company-tooltip-selection
93 '((default :inherit company-tooltip)
94 (((class color) (min-colors 88)) (:background "orange1"))
95 (t (:background "green")))
96 "*Face used for the selection in the tool tip."
99 (defface company-tooltip-common
100 '((t :inherit company-tooltip
102 "*Face used for the common completion in the tool tip."
105 (defface company-tooltip-common-selection
106 '((t :inherit company-tooltip-selection
108 "*Face used for the selected common completion in the tool tip."
111 (defcustom company-tooltip-limit 10
112 "*The maximum number of candidates in the tool tip"
116 (defface company-preview
117 '((t :background "blue4"
118 :foreground "wheat"))
119 "*Face used for the completion preview."
122 (defface company-preview-common
123 '((t :inherit company-preview
125 "*Face used for the common part of the completion preview."
128 (defface company-echo nil
129 "*Face used for completions in the echo area."
132 (defface company-echo-common
133 '((((background dark)) (:foreground "firebrick1"))
134 (((background light)) (:background "firebrick4")))
135 "*Face used for the common part of completions in the echo area."
138 (defun company-frontends-set (variable value)
140 (let ((remainder value))
141 (setcdr remainder (delq (car remainder) (cdr remainder))))
142 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
143 (memq 'company-pseudo-tooltip-frontend value)
144 (error "Pseudo tooltip frontend cannot be used twice"))
145 (and (memq 'company-preview-if-just-one-frontend value)
146 (memq 'company-preview-frontend value)
147 (error "Preview frontend cannot be used twice"))
148 (and (memq 'company-echo value)
149 (memq 'company-echo-metadata-frontend value)
150 (error "Echo area cannot be used twice"))
151 ;; preview must come last
152 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
154 (setq value (append (delq f value) (list f)))))
155 (set variable value))
157 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
158 company-preview-if-just-one-frontend
159 company-echo-metadata-frontend)
160 "*The list of active front-ends (visualizations).
161 Each front-end is a function that takes one argument. It is called with
162 one of the following arguments:
164 'show: When the visualization should start.
166 'hide: When the visualization should end.
168 'update: When the data has been updated.
170 'pre-command: Before every command that is executed while the
171 visualization is active.
173 'post-command: After every command that is executed while the
174 visualization is active.
176 The visualized data is stored in `company-prefix', `company-candidates',
177 `company-common', `company-selection', `company-point' and
178 `company-search-string'."
179 :set 'company-frontends-set
181 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
182 (const :tag "echo, strip common"
183 company-echo-strip-common-frontend)
184 (const :tag "show echo meta-data in echo"
185 company-echo-metadata-frontend)
186 (const :tag "pseudo tooltip"
187 company-pseudo-tooltip-frontend)
188 (const :tag "pseudo tooltip, multiple only"
189 company-pseudo-tooltip-unless-just-one-frontend)
190 (const :tag "preview" company-preview-frontend)
191 (const :tag "preview, unique only"
192 company-preview-if-just-one-frontend)
193 (function :tag "custom function" nil))))
195 (defcustom company-backends '(company-elisp company-nxml company-css
196 company-semantic company-gtags company-oddmuse
197 company-files company-dabbrev)
198 "*The list of active back-ends (completion engines).
199 Each back-end is a function that takes a variable number of arguments.
200 The first argument is the command requested from the back-end. It is one
203 'prefix: The back-end should return the text to be completed. It must be
204 text immediately before `point'. Returning nil passes control to the next
207 'candidates: The second argument is the prefix to be completed. The
208 return value should be a list of candidates that start with the prefix.
212 'sorted: The back-end may return t here to indicate that the candidates
213 are sorted and will not need to be sorted again.
215 'no-cache: Usually company doesn't ask for candidates again as completion
216 progresses, unless the back-end returns t for this command. The second
217 argument is the latest prefix.
219 'meta: The second argument is a completion candidate. The back-end should
220 return a (short) documentation string for it.
222 'doc-buffer: The second argument is a completion candidate. The back-end should
223 create a buffer (preferably with `company-doc-buffer'), fill it with
224 documentation and return it.
226 The back-end should return nil for all commands it does not support or
227 does not know about."
229 :type '(repeat (function :tag "function" nil)))
231 (defcustom company-minimum-prefix-length 3
232 "*The minimum prefix length for automatic completion."
234 :type '(integer :tag "prefix length"))
236 (defcustom company-idle-delay .7
237 "*The idle delay in seconds until automatic completions starts.
238 A value of nil means never complete automatically, t means complete
239 immediately when a prefix of `company-minimum-prefix-length' is reached."
241 :type '(choice (const :tag "never (nil)" nil)
242 (const :tag "immediate (t)" t)
243 (number :tag "seconds")))
245 (defcustom company-show-numbers nil
246 "*If enabled, show quick-access numbers for the first ten candidates."
248 :type '(choice (const :tag "off" nil)
249 (const :tag "on" t)))
251 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
253 (defvar company-mode-map (make-sparse-keymap)
254 "Keymap used by `company-mode'.")
256 (defvar company-active-map
257 (let ((keymap (make-sparse-keymap)))
258 (define-key keymap (kbd "M-n") 'company-select-next)
259 (define-key keymap (kbd "M-p") 'company-select-previous)
260 (define-key keymap (kbd "<down>") 'company-select-next)
261 (define-key keymap (kbd "<up>") 'company-select-previous)
262 (define-key keymap "\C-m" 'company-complete-selection)
263 (define-key keymap "\t" 'company-complete-common)
264 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
265 (define-key keymap "\C-s" 'company-search-candidates)
267 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
268 `(lambda () (interactive) (company-complete-number ,i))))
271 "Keymap that is enabled during an active completion.")
274 (define-minor-mode company-mode
275 "\"complete anything\"; in in-buffer completion framework.
276 Completion starts automatically, depending on the values
277 `company-idle-delay' and `company-minimum-prefix-length'.
279 Completion can be controlled with the commands:
280 `company-complete-common', `company-complete-selection', `company-complete',
281 `company-select-next', `company-select-previous'. If these commands are
282 called before `company-idle-delay', completion will also start.
284 Completions can be searched with `company-search-candidates'.
286 The completion data is retrieved using `company-backends' and displayed using
292 keymap during active completions:
294 \\{company-active-map}"
295 nil " comp" company-mode-map
298 (add-hook 'pre-command-hook 'company-pre-command nil t)
299 (add-hook 'post-command-hook 'company-post-command nil t)
300 (dolist (backend company-backends)
301 (unless (fboundp backend)
302 (ignore-errors (require backend nil t)))
303 (unless (fboundp backend)
304 (message "Company back-end '%s' could not be initialized"
306 (remove-hook 'pre-command-hook 'company-pre-command t)
307 (remove-hook 'post-command-hook 'company-post-command t)
309 (kill-local-variable 'company-point)))
311 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
313 (defvar company-overriding-keymap-bound nil)
314 (make-variable-buffer-local 'company-overriding-keymap-bound)
316 (defvar company-old-keymap nil)
317 (make-variable-buffer-local 'company-old-keymap)
319 (defvar company-my-keymap nil)
320 (make-variable-buffer-local 'company-my-keymap)
322 (defsubst company-enable-overriding-keymap (keymap)
323 (setq company-my-keymap keymap)
324 (when company-overriding-keymap-bound
325 (company-uninstall-map)))
327 (defun company-install-map ()
328 (unless (or company-overriding-keymap-bound
329 (null company-my-keymap))
330 (setq company-old-keymap overriding-terminal-local-map
331 overriding-terminal-local-map company-my-keymap
332 company-overriding-keymap-bound t)))
334 (defun company-uninstall-map ()
335 (when (and company-overriding-keymap-bound
336 (eq overriding-terminal-local-map company-my-keymap))
337 (setq overriding-terminal-local-map company-old-keymap
338 company-overriding-keymap-bound nil)))
341 ;; Emacs calculates the active keymaps before reading the event. That means we
342 ;; cannot change the keymap from a timer. So we send a bogus command.
343 (defun company-ignore ()
346 (global-set-key '[31415926] 'company-ignore)
348 (defun company-input-noop ()
349 (push 31415926 unread-command-events))
351 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
353 (defun company-grab (regexp &optional expression)
354 (when (looking-back regexp)
355 (or (match-string-no-properties (or expression 0)) "")))
357 (defun company-in-string-or-comment (&optional point)
358 (let ((pos (syntax-ppss)))
359 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
361 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
363 (defvar company-backend nil)
364 (make-variable-buffer-local 'company-backend)
366 (defvar company-prefix nil)
367 (make-variable-buffer-local 'company-prefix)
369 (defvar company-candidates nil)
370 (make-variable-buffer-local 'company-candidates)
372 (defvar company-candidates-length nil)
373 (make-variable-buffer-local 'company-candidates-length)
375 (defvar company-candidates-cache nil)
376 (make-variable-buffer-local 'company-candidates-cache)
378 (defvar company-candidates-predicate nil)
379 (make-variable-buffer-local 'company-candidates-predicate)
381 (defvar company-common nil)
382 (make-variable-buffer-local 'company-common)
384 (defvar company-selection 0)
385 (make-variable-buffer-local 'company-selection)
387 (defvar company-selection-changed nil)
388 (make-variable-buffer-local 'company-selection-changed)
390 (defvar company-point nil)
391 (make-variable-buffer-local 'company-point)
393 (defvar company-timer nil)
395 (defsubst company-strip-prefix (str)
396 (substring str (length company-prefix)))
398 (defsubst company-reformat (candidate)
399 ;; company-ispell needs this, because the results are always lower-case
400 ;; It's mory efficient to fix it only when they are displayed.
401 (concat company-prefix (substring candidate (length company-prefix))))
403 (defsubst company-should-complete (prefix)
404 (and (eq company-idle-delay t)
405 (>= (length prefix) company-minimum-prefix-length)))
407 (defsubst company-call-frontends (command)
408 (dolist (frontend company-frontends)
410 (funcall frontend command)
411 (error (error "Company: Front-end %s error \"%s\" on command %s"
412 frontend (error-message-string err) command)))))
414 (defsubst company-set-selection (selection &optional force-update)
415 (setq selection (max 0 (min (1- company-candidates-length) selection)))
416 (when (or force-update (not (equal selection company-selection)))
417 (setq company-selection selection
418 company-selection-changed t)
419 (company-call-frontends 'update)))
421 (defun company-apply-predicate (candidates predicate)
423 (dolist (c candidates)
424 (when (funcall predicate c)
428 (defun company-update-candidates (candidates)
429 (setq company-candidates-length (length candidates))
430 (if (> company-selection 0)
431 ;; Try to restore the selection
432 (let ((selected (nth company-selection company-candidates)))
433 (setq company-selection 0
434 company-candidates candidates)
436 (while (and candidates (string< (pop candidates) selected))
437 (incf company-selection))
439 ;; Make sure selection isn't out of bounds.
440 (setq company-selection (min (1- company-candidates-length)
441 company-selection)))))
442 (setq company-selection 0
443 company-candidates candidates))
445 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
446 (setq company-common (try-completion company-prefix company-candidates)))
447 (when (eq company-common t)
448 (setq company-candidates nil)))
450 (defsubst company-calculate-candidates (prefix)
451 (setq company-prefix prefix)
452 (company-update-candidates
453 (or (cdr (assoc prefix company-candidates-cache))
454 (when company-candidates-cache
455 (let ((len (length prefix))
456 (completion-ignore-case (funcall company-backend 'ignore-case))
459 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
460 company-candidates-cache)))
461 (return (all-completions prefix prev))))))
462 (let ((candidates (funcall company-backend 'candidates prefix)))
463 (when company-candidates-predicate
465 (company-apply-predicate candidates
466 company-candidates-predicate)))
467 (unless (funcall company-backend 'sorted)
468 (setq candidates (sort candidates 'string<)))
470 (unless (assoc prefix company-candidates-cache)
471 (push (cons prefix company-candidates) company-candidates-cache))
474 (defun company-idle-begin (buf win tick pos)
476 (eq buf (current-buffer))
477 (eq win (selected-window))
478 (eq tick (buffer-chars-modified-tick))
480 (not company-candidates)
481 (not (equal (point) company-point))
482 (let ((company-idle-delay t))
484 (when company-candidates
486 (company-post-command)))))
488 (defun company-manual-begin ()
490 (unless company-mode (error "Company not enabled"))
492 (not company-candidates)
493 (let ((company-idle-delay t)
494 (company-minimum-prefix-length 0))
496 ;; Return non-nil if active.
499 (defun company-continue ()
500 (when company-candidates
501 (when (funcall company-backend 'no-cache company-prefix)
502 ;; Don't complete existing candidates, fetch new ones.
503 (setq company-candidates-cache nil))
504 (let ((new-prefix (funcall company-backend 'prefix)))
505 (unless (and (= (- (point) (length new-prefix))
506 (- company-point (length company-prefix)))
507 (or (equal company-prefix new-prefix)
508 (company-calculate-candidates new-prefix)))
509 (setq company-candidates nil)))))
511 (defun company-begin ()
512 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
513 ;; Don't complete in these cases.
514 (setq company-candidates nil)
516 (unless company-candidates
518 (dolist (backend company-backends)
519 (and (fboundp backend)
520 (setq prefix (funcall backend 'prefix))
521 (company-should-complete prefix)
522 (setq company-backend backend)
523 (company-calculate-candidates prefix))
525 (if company-candidates
527 (setq company-point (point))
528 (company-enable-overriding-keymap company-active-map)
529 (company-call-frontends 'update))
532 (defun company-cancel ()
533 (setq company-backend nil
535 company-candidates nil
536 company-candidates-length nil
537 company-candidates-cache nil
538 company-candidates-predicate nil
541 company-selection-changed nil
544 (cancel-timer company-timer))
545 (company-search-mode 0)
546 (company-call-frontends 'hide)
547 (company-enable-overriding-keymap nil))
549 (defun company-abort ()
551 ;; Don't start again, unless started manually.
552 (setq company-point (point)))
554 (defun company-pre-command ()
555 (unless (eq this-command 'company-show-doc-buffer)
557 (when company-candidates
558 (company-call-frontends 'pre-command))
559 (error (message "Company: An error occurred in pre-command")
560 (message "%s" (error-message-string err))
563 (cancel-timer company-timer))
564 (company-uninstall-map))
566 (defun company-post-command ()
567 (unless (eq this-command 'company-show-doc-buffer)
570 (unless (equal (point) company-point)
572 (when company-candidates
573 (company-call-frontends 'post-command))
574 (when (numberp company-idle-delay)
576 (run-with-timer company-idle-delay nil 'company-idle-begin
577 (current-buffer) (selected-window)
578 (buffer-chars-modified-tick) (point)))))
579 (error (message "Company: An error occurred in post-command")
580 (message "%s" (error-message-string err))
582 (company-install-map))
584 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
586 (defvar company-search-string nil)
587 (make-variable-buffer-local 'company-search-string)
589 (defvar company-search-lighter " Search: \"\"")
590 (make-variable-buffer-local 'company-search-lighter)
592 (defvar company-search-old-map nil)
593 (make-variable-buffer-local 'company-search-old-map)
595 (defvar company-search-old-selection 0)
596 (make-variable-buffer-local 'company-search-old-selection)
598 (defun company-search (text lines)
599 (let ((quoted (regexp-quote text))
602 (when (string-match quoted line (length company-prefix))
606 (defun company-search-printing-char ()
608 (unless company-mode (error "Company not enabled"))
609 (unless company-search-mode (error "Company not in search mode"))
610 (setq company-search-string
611 (concat (or company-search-string "") (string last-command-event))
612 company-search-lighter (concat " Search: \"" company-search-string
614 (let ((pos (company-search company-search-string
615 (nthcdr company-selection company-candidates))))
618 (company-set-selection (+ company-selection pos) t))))
620 (defun company-search-repeat-forward ()
621 "Repeat the incremental search in completion candidates forward."
623 (unless company-mode (error "Company not enabled"))
624 (unless company-search-mode (error "Company not in search mode"))
625 (let ((pos (company-search company-search-string
626 (cdr (nthcdr company-selection
627 company-candidates)))))
630 (company-set-selection (+ company-selection pos 1) t))))
632 (defun company-search-repeat-backward ()
633 "Repeat the incremental search in completion candidates backwards."
635 (unless company-mode (error "Company not enabled"))
636 (unless company-search-mode (error "Company not in search mode"))
637 (let ((pos (company-search company-search-string
638 (nthcdr (- company-candidates-length
640 (reverse company-candidates)))))
643 (company-set-selection (- company-selection pos 1) t))))
645 (defsubst company-create-match-predicate (search-string)
647 ,(if company-candidates-predicate
648 `(and (string-match ,search-string candidate)
649 (funcall ,company-candidates-predicate candidate))
650 `(string-match ,company-search-string candidate))))
652 (defun company-search-kill-others ()
653 "Limit the completion candidates to the ones matching the search string."
655 (unless company-mode (error "Company not enabled"))
656 (unless company-search-mode (error "Company not in search mode"))
657 (let ((predicate (company-create-match-predicate company-search-string)))
658 (setq company-candidates-predicate predicate)
659 (company-update-candidates (company-apply-predicate company-candidates
661 (company-search-mode 0)
662 (company-call-frontends 'update)))
664 (defun company-search-abort ()
665 "Abort searching the completion candidates."
667 (unless company-mode (error "Company not enabled"))
668 (unless company-search-mode (error "Company not in search mode"))
669 (company-set-selection company-search-old-selection t)
670 (company-search-mode 0))
672 (defun company-search-other-char ()
674 (unless company-mode (error "Company not enabled"))
675 (unless company-search-mode (error "Company not in search mode"))
676 (company-search-mode 0)
677 (when last-input-event
678 (clear-this-command-keys t)
679 (setq unread-command-events (list last-input-event))))
681 (defvar company-search-map
683 (keymap (make-keymap)))
684 (if (fboundp 'max-char)
685 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
686 'company-search-printing-char)
688 ;; obselete in Emacs 23
689 (let ((l (generic-character-list))
690 (table (nth 1 keymap)))
692 (set-char-table-default table (car l) 'isearch-printing-char)
694 (define-key keymap [t] 'company-search-other-char)
696 (define-key keymap (make-string 1 i) 'company-search-other-char)
699 (define-key keymap (vector i) 'company-search-printing-char)
701 (let ((meta-map (make-sparse-keymap)))
702 (define-key keymap (char-to-string meta-prefix-char) meta-map)
703 (define-key keymap [escape] meta-map))
704 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
705 (define-key keymap "\e\e\e" 'company-search-other-char)
706 (define-key keymap [escape escape escape] 'company-search-other-char)
708 (define-key keymap "\C-g" 'company-search-abort)
709 (define-key keymap "\C-s" 'company-search-repeat-forward)
710 (define-key keymap "\C-r" 'company-search-repeat-backward)
711 (define-key keymap "\C-o" 'company-search-kill-others)
713 "Keymap used for incrementally searching the completion candidates.")
715 (define-minor-mode company-search-mode
716 "Start searching the completion candidates incrementally.
718 \\<company-search-map>Search can be controlled with the commands:
719 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
720 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
721 - `company-search-abort' (\\[company-search-abort])
723 Regular characters are appended to the search string.
725 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
726 the search string to limit the completion candidates."
727 nil company-search-lighter nil
728 (if company-search-mode
729 (if (company-manual-begin)
731 (setq company-search-old-selection company-selection)
732 (company-enable-overriding-keymap company-search-map)
733 (company-call-frontends 'update))
734 (setq company-search-mode nil))
735 (kill-local-variable 'company-search-string)
736 (kill-local-variable 'company-search-lighter)
737 (kill-local-variable 'company-search-old-selection)
738 (company-enable-overriding-keymap company-active-map)))
740 (defun company-search-candidates ()
741 "Start searching the completion candidates incrementally.
743 \\<company-search-map>Search can be controlled with the commands:
744 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
745 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
746 - `company-search-abort' (\\[company-search-abort])
748 Regular characters are appended to the search string.
750 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
751 the search string to limit the completion candidates."
753 (company-search-mode 1))
755 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
757 (defun company-select-next ()
758 "Select the next candidate in the list."
760 (when (company-manual-begin)
761 (company-set-selection (1+ company-selection))))
763 (defun company-select-previous ()
764 "Select the previous candidate in the list."
766 (when (company-manual-begin)
767 (company-set-selection (1- company-selection))))
769 (defun company-complete-selection ()
770 "Complete the selected candidate."
772 (when (company-manual-begin)
773 (insert (company-strip-prefix (nth company-selection company-candidates)))
776 (defun company-complete-common ()
777 "Complete the common part of all candidates."
779 (when (company-manual-begin)
780 (insert (company-strip-prefix company-common))))
782 (defun company-complete ()
783 "Complete the common part of all candidates or the current selection.
784 The first time this is called, the common part is completed, the second time, or
785 when the selection has been changed, the selected candidate is completed."
787 (when (company-manual-begin)
788 (if (or company-selection-changed
789 (eq last-command 'company-complete-common))
790 (call-interactively 'company-complete-selection)
791 (call-interactively 'company-complete-common)
792 (setq this-command 'company-complete-common))))
794 (defun company-complete-number (n)
795 "Complete the Nth candidate."
796 (when (company-manual-begin)
797 (and (< n 1) (> n company-candidates-length)
798 (error "No candidate number %d" n))
800 (insert (company-strip-prefix (nth n company-candidates)))
803 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
805 (defconst company-space-strings-limit 100)
807 (defconst company-space-strings
809 (dotimes (i company-space-strings-limit)
810 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
811 (apply 'vector lst)))
813 (defsubst company-space-string (len)
814 (if (< len company-space-strings-limit)
815 (aref company-space-strings len)
816 (make-string len ?\ )))
818 (defsubst company-safe-substring (str from &optional to)
819 (let ((len (length str)))
822 (if (and to (> to len))
823 (concat (substring str from)
824 (company-space-string (- to len)))
825 (substring str from to)))))
827 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
829 (defvar company-last-metadata nil)
830 (make-variable-buffer-local 'company-last-metadata)
832 (defun company-fetch-metadata ()
833 (let ((selected (nth company-selection company-candidates)))
834 (unless (equal selected (car company-last-metadata))
835 (setq company-last-metadata
836 (cons selected (funcall company-backend 'meta selected))))
837 (cdr company-last-metadata)))
839 (defun company-doc-buffer (&optional string)
840 (with-current-buffer (get-buffer-create "*Company meta-data*")
844 (defun company-show-doc-buffer ()
845 "Temporarily show a buffer with the complete documentation for the selection."
847 (unless company-mode (error "Company not enabled"))
848 (when (company-manual-begin)
849 (save-window-excursion
850 (let* ((height (window-height))
851 (row (cdr (posn-col-row (posn-at-point))))
852 (selected (nth company-selection company-candidates))
853 (buffer (funcall company-backend 'doc-buffer selected)))
855 (error "No documentation available.")
856 (display-buffer buffer)
857 (and (< (window-height) height)
858 (< (- (window-height) row 2) company-tooltip-limit)
859 (recenter (- (window-height) row 2)))
860 (while (eq 'scroll-other-window
861 (key-binding (vector (list (read-event)))))
862 (scroll-other-window))
863 (when last-input-event
864 (clear-this-command-keys t)
865 (setq unread-command-events (list last-input-event))))))))
867 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
869 (defvar company-pseudo-tooltip-overlay nil)
870 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
872 (defvar company-tooltip-offset 0)
873 (make-variable-buffer-local 'company-tooltip-offset)
875 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
878 (setq company-tooltip-offset
879 (max (min selection company-tooltip-offset)
880 (- selection -1 limit)))
882 (when (<= company-tooltip-offset 1)
884 (setq company-tooltip-offset 0))
886 (when (>= company-tooltip-offset (- num-lines limit 1))
888 (when (= selection (1- num-lines))
889 (decf company-tooltip-offset)
890 (when (<= company-tooltip-offset 1)
891 (setq company-tooltip-offset 0)
898 (defsubst company-round-tab (arg)
899 (* (/ (+ arg tab-width) tab-width) tab-width))
901 (defun company-untabify (str)
902 (let* ((pieces (split-string str "\t"))
905 (setcar copy (company-safe-substring
906 (car copy) 0 (company-round-tab (string-width (car copy)))))
908 (apply 'concat pieces)))
910 (defun company-fill-propertize (line width selected)
911 (setq line (company-safe-substring line 0 width))
912 (add-text-properties 0 width (list 'face 'company-tooltip) line)
913 (add-text-properties 0 (length company-common)
914 (list 'face 'company-tooltip-common) line)
916 (if (and company-search-string
917 (string-match (regexp-quote company-search-string) line
918 (length company-prefix)))
920 (add-text-properties (match-beginning 0) (match-end 0)
921 '(face company-tooltip-selection) line)
922 (when (< (match-beginning 0) (length company-common))
923 (add-text-properties (match-beginning 0) (length company-common)
924 '(face company-tooltip-common-selection)
926 (add-text-properties 0 width '(face company-tooltip-selection) line)
927 (add-text-properties 0 (length company-common)
928 (list 'face 'company-tooltip-common-selection)
934 (defun company-buffer-lines (beg end)
936 (let ((row (cdr (posn-col-row (posn-at-point))))
938 (while (and (equal (move-to-window-line (incf row)) row)
940 (push (buffer-substring beg (min end (1- (point)))) lines)
943 (push (buffer-substring beg end) lines))
946 (defsubst company-modify-line (old new offset)
947 (concat (company-safe-substring old 0 offset)
949 (company-safe-substring old (+ offset (length new)))))
951 (defun company-replacement-string (old lines column nl)
953 ;; Inject into old lines.
955 (push (company-modify-line (pop old) (pop lines) column) new))
956 ;; Append whole new lines.
958 (push (concat (company-space-string column) (pop lines)) new))
959 (concat (when nl "\n")
960 (mapconcat 'identity (nreverse new) "\n")
963 (defun company-create-lines (column selection limit)
965 (let ((len company-candidates-length)
975 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
977 (when (> company-tooltip-offset 0)
978 (setq previous (format "...(%d)" company-tooltip-offset)))
980 (setq remainder (- len limit company-tooltip-offset)
981 remainder (when (> remainder 0)
982 (setq remainder (format "...(%d)" remainder))))
984 (decf selection company-tooltip-offset)
985 (setq width (min (length previous) (length remainder))
986 lines (nthcdr company-tooltip-offset company-candidates)
991 (setq width (max (length (pop lines-copy)) width)))
992 (setq width (min width (- (window-width) column)))
994 (setq lines-copy lines)
996 ;; number can make tooltip too long
997 (and company-show-numbers
998 (< (setq numbered company-tooltip-offset) 10)
1002 (push (propertize (company-safe-substring previous 0 width)
1003 'face 'company-tooltip)
1007 (push (company-fill-propertize
1008 (if (>= numbered 10)
1009 (company-reformat (pop lines))
1012 (company-safe-substring (company-reformat (pop lines))
1015 width (equal i selection))
1019 (push (propertize (company-safe-substring remainder 0 width)
1020 'face 'company-tooltip)
1023 (setq lines (nreverse new))))
1027 (defsubst company-pseudo-tooltip-height ()
1028 "Calculate the appropriate tooltip height."
1029 (max 3 (min company-tooltip-limit
1030 (- (window-height) 2
1031 (count-lines (window-start) (point-at-bol))))))
1033 (defun company-pseudo-tooltip-show (row column selection)
1034 (company-pseudo-tooltip-hide)
1039 (let* ((height (company-pseudo-tooltip-height))
1040 (lines (company-create-lines column selection height))
1041 (nl (< (move-to-window-line row) row))
1043 (end (save-excursion
1044 (move-to-window-line (+ row height))
1047 (mapcar 'company-untabify (company-buffer-lines beg end)))
1050 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1052 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1053 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1054 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1055 (overlay-put company-pseudo-tooltip-overlay 'company-before
1056 (company-replacement-string old-string lines column nl))
1057 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1059 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1061 (defun company-pseudo-tooltip-show-at-point (pos)
1062 (let ((col-row (posn-col-row (posn-at-point pos))))
1063 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1065 (defun company-pseudo-tooltip-edit (lines selection)
1066 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1067 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1068 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1069 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1070 (lines (company-create-lines column selection height)))
1071 (overlay-put company-pseudo-tooltip-overlay 'company-before
1072 (company-replacement-string old-string lines column nl))))
1074 (defun company-pseudo-tooltip-hide ()
1075 (when company-pseudo-tooltip-overlay
1076 (delete-overlay company-pseudo-tooltip-overlay)
1077 (setq company-pseudo-tooltip-overlay nil)))
1079 (defun company-pseudo-tooltip-hide-temporarily ()
1080 (when (overlayp company-pseudo-tooltip-overlay)
1081 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1082 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1084 (defun company-pseudo-tooltip-unhide ()
1085 (when company-pseudo-tooltip-overlay
1086 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1087 (overlay-put company-pseudo-tooltip-overlay 'before-string
1088 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1089 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1091 (defun company-pseudo-tooltip-frontend (command)
1092 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1094 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1096 (unless (and (overlayp company-pseudo-tooltip-overlay)
1097 (equal (overlay-get company-pseudo-tooltip-overlay
1099 (company-pseudo-tooltip-height)))
1101 (company-pseudo-tooltip-show-at-point (- (point)
1102 (length company-prefix))))
1103 (company-pseudo-tooltip-unhide))
1104 ('hide (company-pseudo-tooltip-hide)
1105 (setq company-tooltip-offset 0))
1106 ('update (when (overlayp company-pseudo-tooltip-overlay)
1107 (company-pseudo-tooltip-edit company-candidates
1108 company-selection)))))
1110 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1111 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1112 (unless (and (eq command 'post-command)
1113 (not (cdr company-candidates)))
1114 (company-pseudo-tooltip-frontend command)))
1116 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1118 (defvar company-preview-overlay nil)
1119 (make-variable-buffer-local 'company-preview-overlay)
1121 (defun company-preview-show-at-point (pos)
1122 (company-preview-hide)
1124 (setq company-preview-overlay (make-overlay pos pos))
1126 (let ((completion (company-strip-prefix (nth company-selection
1127 company-candidates))))
1128 (and (equal pos (point))
1129 (not (equal completion ""))
1130 (add-text-properties 0 1 '(cursor t) completion))
1132 (setq completion (propertize completion 'face 'company-preview))
1133 (add-text-properties 0 (- (length company-common) (length company-prefix))
1134 '(face company-preview-common) completion)
1136 (overlay-put company-preview-overlay 'after-string completion)
1137 (overlay-put company-preview-overlay 'window (selected-window))))
1139 (defun company-preview-hide ()
1140 (when company-preview-overlay
1141 (delete-overlay company-preview-overlay)
1142 (setq company-preview-overlay nil)))
1144 (defun company-preview-frontend (command)
1145 "A `company-mode' front-end showing the selection as if it had been inserted."
1147 ('pre-command (company-preview-hide))
1148 ('post-command (company-preview-show-at-point (point)))
1149 ('hide (company-preview-hide))))
1151 (defun company-preview-if-just-one-frontend (command)
1152 "`company-preview-frontend', but only shown for single candidates."
1153 (unless (and (eq command 'post-command)
1154 (cdr company-candidates))
1155 (company-preview-frontend command)))
1157 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1159 (defvar company-echo-last-msg nil)
1160 (make-variable-buffer-local 'company-echo-last-msg)
1162 (defvar company-echo-timer nil)
1164 (defvar company-echo-delay .1)
1166 (defun company-echo-show (&optional getter)
1168 (setq company-echo-last-msg (funcall getter)))
1169 (let ((message-log-max nil))
1170 (if company-echo-last-msg
1171 (message "%s" company-echo-last-msg)
1174 (defsubst company-echo-show-soon (&optional getter)
1175 (when company-echo-timer
1176 (cancel-timer company-echo-timer))
1177 (setq company-echo-timer (run-with-timer company-echo-delay nil
1178 'company-echo-show getter)))
1180 (defun company-echo-format ()
1182 (let ((limit (window-width (minibuffer-window)))
1184 ;; Roll to selection.
1185 (candidates (nthcdr company-selection company-candidates))
1186 (i (if company-show-numbers company-selection 99999))
1190 (setq comp (company-reformat (pop candidates))
1191 len (+ len 1 (length comp)))
1195 (setq comp (propertize (format "%d: %s" i comp)
1196 'face 'company-echo))
1199 (add-text-properties 3 (+ 3 (length company-common))
1200 '(face company-echo-common) comp))
1201 (setq comp (propertize comp 'face 'company-echo))
1202 (add-text-properties 0 (length company-common)
1203 '(face company-echo-common) comp))
1205 (setq candidates nil)
1208 (mapconcat 'identity (nreverse msg) " ")))
1210 (defun company-echo-strip-common-format ()
1212 (let ((limit (window-width (minibuffer-window)))
1213 (len (+ (length company-prefix) 2))
1214 ;; Roll to selection.
1215 (candidates (nthcdr company-selection company-candidates))
1216 (i (if company-show-numbers company-selection 99999))
1220 (setq comp (company-strip-prefix (pop candidates))
1221 len (+ len 2 (length comp)))
1224 (setq comp (format "%s (%d)" comp i))
1228 (setq candidates nil)
1229 (push (propertize comp 'face 'company-echo) msg)))
1231 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1232 (mapconcat 'identity (nreverse msg) ", ")
1235 (defun company-echo-hide ()
1236 (when company-echo-timer
1237 (cancel-timer company-echo-timer))
1238 (unless (equal company-echo-last-msg "")
1239 (setq company-echo-last-msg "")
1240 (company-echo-show)))
1242 (defun company-echo-frontend (command)
1243 "A `company-mode' front-end showing the candidates in the echo area."
1245 ('pre-command (company-echo-show-soon))
1246 ('post-command (company-echo-show-soon 'company-echo-format))
1247 ('hide (company-echo-hide))))
1249 (defun company-echo-strip-common-frontend (command)
1250 "A `company-mode' front-end showing the candidates in the echo area."
1252 ('pre-command (company-echo-show-soon))
1253 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1254 ('hide (company-echo-hide))))
1256 (defun company-echo-metadata-frontend (command)
1257 "A `company-mode' front-end showing the documentation in the echo area."
1259 ('pre-command (company-echo-show-soon))
1260 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1261 ('hide (company-echo-hide))))
1264 ;;; company.el ends here