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
58 ;; Added `company-filter-candidates'.
59 ;; More local Lisp variables are now included in the candidates.
62 ;; Fixed elisp documentation buffer always showing the same doc.
63 ;; Added `company-echo-strip-common-frontend'.
64 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
65 ;; Don't hide the echo message if it isn't shown.
72 (eval-when-compile (require 'cl))
74 (add-to-list 'debug-ignored-errors
75 "^Pseudo tooltip frontend cannot be used twice$")
76 (add-to-list 'debug-ignored-errors "^Preview frontend cannot be used twice$")
77 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
78 (add-to-list 'debug-ignored-errors "^No documentation available$")
79 (add-to-list 'debug-ignored-errors "^Company not enabled$")
80 (add-to-list 'debug-ignored-errors "^Company not in search mode$")
81 (add-to-list 'debug-ignored-errors "^No candidate number ")
84 "Extensible inline text completion mechanism"
89 (defface company-tooltip
90 '((t :background "yellow"
92 "*Face used for the tool tip."
95 (defface company-tooltip-selection
96 '((default :inherit company-tooltip)
97 (((class color) (min-colors 88)) (:background "orange1"))
98 (t (:background "green")))
99 "*Face used for the selection in the tool tip."
102 (defface company-tooltip-common
103 '((t :inherit company-tooltip
105 "*Face used for the common completion in the tool tip."
108 (defface company-tooltip-common-selection
109 '((t :inherit company-tooltip-selection
111 "*Face used for the selected common completion in the tool tip."
114 (defcustom company-tooltip-limit 10
115 "*The maximum number of candidates in the tool tip"
119 (defface company-preview
120 '((t :background "blue4"
121 :foreground "wheat"))
122 "*Face used for the completion preview."
125 (defface company-preview-common
126 '((t :inherit company-preview
128 "*Face used for the common part of the completion preview."
131 (defface company-echo nil
132 "*Face used for completions in the echo area."
135 (defface company-echo-common
136 '((((background dark)) (:foreground "firebrick1"))
137 (((background light)) (:background "firebrick4")))
138 "*Face used for the common part of completions in the echo area."
141 (defun company-frontends-set (variable value)
143 (let ((remainder value))
144 (setcdr remainder (delq (car remainder) (cdr remainder))))
145 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
146 (memq 'company-pseudo-tooltip-frontend value)
147 (error "Pseudo tooltip frontend cannot be used twice"))
148 (and (memq 'company-preview-if-just-one-frontend value)
149 (memq 'company-preview-frontend value)
150 (error "Preview frontend cannot be used twice"))
151 (and (memq 'company-echo value)
152 (memq 'company-echo-metadata-frontend value)
153 (error "Echo area cannot be used twice"))
154 ;; preview must come last
155 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
157 (setq value (append (delq f value) (list f)))))
158 (set variable value))
160 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
161 company-preview-if-just-one-frontend
162 company-echo-metadata-frontend)
163 "*The list of active front-ends (visualizations).
164 Each front-end is a function that takes one argument. It is called with
165 one of the following arguments:
167 'show: When the visualization should start.
169 'hide: When the visualization should end.
171 'update: When the data has been updated.
173 'pre-command: Before every command that is executed while the
174 visualization is active.
176 'post-command: After every command that is executed while the
177 visualization is active.
179 The visualized data is stored in `company-prefix', `company-candidates',
180 `company-common', `company-selection', `company-point' and
181 `company-search-string'."
182 :set 'company-frontends-set
184 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
185 (const :tag "echo, strip common"
186 company-echo-strip-common-frontend)
187 (const :tag "show echo meta-data in echo"
188 company-echo-metadata-frontend)
189 (const :tag "pseudo tooltip"
190 company-pseudo-tooltip-frontend)
191 (const :tag "pseudo tooltip, multiple only"
192 company-pseudo-tooltip-unless-just-one-frontend)
193 (const :tag "preview" company-preview-frontend)
194 (const :tag "preview, unique only"
195 company-preview-if-just-one-frontend)
196 (function :tag "custom function" nil))))
198 (defcustom company-backends '(company-elisp company-nxml company-css
199 company-semantic company-gtags company-oddmuse
200 company-files company-dabbrev)
201 "*The list of active back-ends (completion engines).
202 Each back-end is a function that takes a variable number of arguments.
203 The first argument is the command requested from the back-end. It is one
206 'prefix: The back-end should return the text to be completed. It must be
207 text immediately before `point'. Returning nil passes control to the next
210 'candidates: The second argument is the prefix to be completed. The
211 return value should be a list of candidates that start with the prefix.
215 'sorted: The back-end may return t here to indicate that the candidates
216 are sorted and will not need to be sorted again.
218 'no-cache: Usually company doesn't ask for candidates again as completion
219 progresses, unless the back-end returns t for this command. The second
220 argument is the latest prefix.
222 'meta: The second argument is a completion candidate. The back-end should
223 return a (short) documentation string for it.
225 'doc-buffer: The second argument is a completion candidate. The back-end should
226 create a buffer (preferably with `company-doc-buffer'), fill it with
227 documentation and return it.
229 The back-end should return nil for all commands it does not support or
230 does not know about."
232 :type '(repeat (function :tag "function" nil)))
234 (defcustom company-minimum-prefix-length 3
235 "*The minimum prefix length for automatic completion."
237 :type '(integer :tag "prefix length"))
239 (defcustom company-idle-delay .7
240 "*The idle delay in seconds until automatic completions starts.
241 A value of nil means never complete automatically, t means complete
242 immediately when a prefix of `company-minimum-prefix-length' is reached."
244 :type '(choice (const :tag "never (nil)" nil)
245 (const :tag "immediate (t)" t)
246 (number :tag "seconds")))
248 (defcustom company-show-numbers nil
249 "*If enabled, show quick-access numbers for the first ten candidates."
251 :type '(choice (const :tag "off" nil)
252 (const :tag "on" t)))
254 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256 (defvar company-mode-map (make-sparse-keymap)
257 "Keymap used by `company-mode'.")
259 (defvar company-active-map
260 (let ((keymap (make-sparse-keymap)))
261 (define-key keymap (kbd "M-n") 'company-select-next)
262 (define-key keymap (kbd "M-p") 'company-select-previous)
263 (define-key keymap (kbd "<down>") 'company-select-next)
264 (define-key keymap (kbd "<up>") 'company-select-previous)
265 (define-key keymap "\C-m" 'company-complete-selection)
266 (define-key keymap "\t" 'company-complete-common)
267 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
268 (define-key keymap "\C-s" 'company-search-candidates)
269 (define-key keymap "\C-\M-s" 'company-filter-candidates)
271 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
272 `(lambda () (interactive) (company-complete-number ,i))))
275 "Keymap that is enabled during an active completion.")
278 (define-minor-mode company-mode
279 "\"complete anything\"; in in-buffer completion framework.
280 Completion starts automatically, depending on the values
281 `company-idle-delay' and `company-minimum-prefix-length'.
283 Completion can be controlled with the commands:
284 `company-complete-common', `company-complete-selection', `company-complete',
285 `company-select-next', `company-select-previous'. If these commands are
286 called before `company-idle-delay', completion will also start.
288 Completions can be searched with `company-search-candidates' or
289 `company-filter-candidates'.
291 The completion data is retrieved using `company-backends' and displayed using
297 keymap during active completions:
299 \\{company-active-map}"
300 nil " comp" company-mode-map
303 (add-hook 'pre-command-hook 'company-pre-command nil t)
304 (add-hook 'post-command-hook 'company-post-command nil t)
305 (dolist (backend company-backends)
306 (unless (fboundp backend)
307 (ignore-errors (require backend nil t)))
308 (unless (fboundp backend)
309 (message "Company back-end '%s' could not be initialized"
311 (remove-hook 'pre-command-hook 'company-pre-command t)
312 (remove-hook 'post-command-hook 'company-post-command t)
314 (kill-local-variable 'company-point)))
316 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
318 (defvar company-overriding-keymap-bound nil)
319 (make-variable-buffer-local 'company-overriding-keymap-bound)
321 (defvar company-old-keymap nil)
322 (make-variable-buffer-local 'company-old-keymap)
324 (defvar company-my-keymap nil)
325 (make-variable-buffer-local 'company-my-keymap)
327 (defsubst company-enable-overriding-keymap (keymap)
328 (setq company-my-keymap keymap)
329 (when company-overriding-keymap-bound
330 (company-uninstall-map)))
332 (defun company-install-map ()
333 (unless (or company-overriding-keymap-bound
334 (null company-my-keymap))
335 (setq company-old-keymap overriding-terminal-local-map
336 overriding-terminal-local-map company-my-keymap
337 company-overriding-keymap-bound t)))
339 (defun company-uninstall-map ()
340 (when (and company-overriding-keymap-bound
341 (eq overriding-terminal-local-map company-my-keymap))
342 (setq overriding-terminal-local-map company-old-keymap
343 company-overriding-keymap-bound nil)))
346 ;; Emacs calculates the active keymaps before reading the event. That means we
347 ;; cannot change the keymap from a timer. So we send a bogus command.
348 (defun company-ignore ()
351 (global-set-key '[31415926] 'company-ignore)
353 (defun company-input-noop ()
354 (push 31415926 unread-command-events))
356 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
358 (defun company-grab (regexp &optional expression)
359 (when (looking-back regexp)
360 (or (match-string-no-properties (or expression 0)) "")))
362 (defun company-in-string-or-comment (&optional point)
363 (let ((pos (syntax-ppss)))
364 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
366 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
368 (defvar company-backend nil)
369 (make-variable-buffer-local 'company-backend)
371 (defvar company-prefix nil)
372 (make-variable-buffer-local 'company-prefix)
374 (defvar company-candidates nil)
375 (make-variable-buffer-local 'company-candidates)
377 (defvar company-candidates-length nil)
378 (make-variable-buffer-local 'company-candidates-length)
380 (defvar company-candidates-cache nil)
381 (make-variable-buffer-local 'company-candidates-cache)
383 (defvar company-candidates-predicate nil)
384 (make-variable-buffer-local 'company-candidates-predicate)
386 (defvar company-common nil)
387 (make-variable-buffer-local 'company-common)
389 (defvar company-selection 0)
390 (make-variable-buffer-local 'company-selection)
392 (defvar company-selection-changed nil)
393 (make-variable-buffer-local 'company-selection-changed)
395 (defvar company-point nil)
396 (make-variable-buffer-local 'company-point)
398 (defvar company-timer nil)
400 (defsubst company-strip-prefix (str)
401 (substring str (length company-prefix)))
403 (defsubst company-reformat (candidate)
404 ;; company-ispell needs this, because the results are always lower-case
405 ;; It's mory efficient to fix it only when they are displayed.
406 (concat company-prefix (substring candidate (length company-prefix))))
408 (defsubst company-should-complete (prefix)
409 (and (eq company-idle-delay t)
410 (>= (length prefix) company-minimum-prefix-length)))
412 (defsubst company-call-frontends (command)
413 (dolist (frontend company-frontends)
415 (funcall frontend command)
416 (error (error "Company: Front-end %s error \"%s\" on command %s"
417 frontend (error-message-string err) command)))))
419 (defsubst company-set-selection (selection &optional force-update)
420 (setq selection (max 0 (min (1- company-candidates-length) selection)))
421 (when (or force-update (not (equal selection company-selection)))
422 (setq company-selection selection
423 company-selection-changed t)
424 (company-call-frontends 'update)))
426 (defun company-apply-predicate (candidates predicate)
428 (dolist (c candidates)
429 (when (funcall predicate c)
433 (defun company-update-candidates (candidates)
434 (setq company-candidates-length (length candidates))
435 (if (> company-selection 0)
436 ;; Try to restore the selection
437 (let ((selected (nth company-selection company-candidates)))
438 (setq company-selection 0
439 company-candidates candidates)
441 (while (and candidates (string< (pop candidates) selected))
442 (incf company-selection))
444 ;; Make sure selection isn't out of bounds.
445 (setq company-selection (min (1- company-candidates-length)
446 company-selection)))))
447 (setq company-selection 0
448 company-candidates candidates))
450 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
451 (setq company-common (try-completion company-prefix company-candidates)))
452 (when (eq company-common t)
453 (setq company-candidates nil)))
455 (defsubst company-calculate-candidates (prefix)
456 (setq company-prefix prefix)
457 (company-update-candidates
458 (or (cdr (assoc prefix company-candidates-cache))
459 (when company-candidates-cache
460 (let ((len (length prefix))
461 (completion-ignore-case (funcall company-backend 'ignore-case))
464 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
465 company-candidates-cache)))
466 (return (all-completions prefix prev))))))
467 (let ((candidates (funcall company-backend 'candidates prefix)))
468 (when company-candidates-predicate
470 (company-apply-predicate candidates
471 company-candidates-predicate)))
472 (unless (funcall company-backend 'sorted)
473 (setq candidates (sort candidates 'string<)))
475 (unless (assoc prefix company-candidates-cache)
476 (push (cons prefix company-candidates) company-candidates-cache))
479 (defun company-idle-begin (buf win tick pos)
481 (eq buf (current-buffer))
482 (eq win (selected-window))
483 (eq tick (buffer-chars-modified-tick))
485 (not company-candidates)
486 (not (equal (point) company-point))
487 (let ((company-idle-delay t))
489 (when company-candidates
491 (company-post-command)))))
493 (defun company-manual-begin ()
495 (unless company-mode (error "Company not enabled"))
497 (not company-candidates)
498 (let ((company-idle-delay t)
499 (company-minimum-prefix-length 0))
501 ;; Return non-nil if active.
504 (defun company-continue ()
505 (when company-candidates
506 (when (funcall company-backend 'no-cache company-prefix)
507 ;; Don't complete existing candidates, fetch new ones.
508 (setq company-candidates-cache nil))
509 (let ((new-prefix (funcall company-backend 'prefix)))
510 (unless (and (= (- (point) (length new-prefix))
511 (- company-point (length company-prefix)))
512 (or (equal company-prefix new-prefix)
513 (company-calculate-candidates new-prefix)))
514 (setq company-candidates nil)))))
516 (defun company-begin ()
517 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
518 ;; Don't complete in these cases.
519 (setq company-candidates nil)
521 (unless company-candidates
523 (dolist (backend company-backends)
524 (and (fboundp backend)
525 (setq prefix (funcall backend 'prefix))
526 (company-should-complete prefix)
527 (setq company-backend backend)
528 (company-calculate-candidates prefix))
530 (if company-candidates
532 (setq company-point (point))
533 (company-enable-overriding-keymap company-active-map)
534 (company-call-frontends 'update))
537 (defun company-cancel ()
538 (setq company-backend nil
540 company-candidates nil
541 company-candidates-length nil
542 company-candidates-cache nil
543 company-candidates-predicate nil
546 company-selection-changed nil
549 (cancel-timer company-timer))
550 (company-search-mode 0)
551 (company-call-frontends 'hide)
552 (company-enable-overriding-keymap nil))
554 (defun company-abort ()
556 ;; Don't start again, unless started manually.
557 (setq company-point (point)))
559 (defun company-pre-command ()
560 (unless (eq this-command 'company-show-doc-buffer)
562 (when company-candidates
563 (company-call-frontends 'pre-command))
564 (error (message "Company: An error occurred in pre-command")
565 (message "%s" (error-message-string err))
568 (cancel-timer company-timer))
569 (company-uninstall-map))
571 (defun company-post-command ()
572 (unless (eq this-command 'company-show-doc-buffer)
575 (unless (equal (point) company-point)
577 (when company-candidates
578 (company-call-frontends 'post-command))
579 (when (numberp company-idle-delay)
581 (run-with-timer company-idle-delay nil 'company-idle-begin
582 (current-buffer) (selected-window)
583 (buffer-chars-modified-tick) (point)))))
584 (error (message "Company: An error occurred in post-command")
585 (message "%s" (error-message-string err))
587 (company-install-map))
589 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
591 (defvar company-search-string nil)
592 (make-variable-buffer-local 'company-search-string)
594 (defvar company-search-lighter " Search: \"\"")
595 (make-variable-buffer-local 'company-search-lighter)
597 (defvar company-search-old-map nil)
598 (make-variable-buffer-local 'company-search-old-map)
600 (defvar company-search-old-selection 0)
601 (make-variable-buffer-local 'company-search-old-selection)
603 (defun company-search (text lines)
604 (let ((quoted (regexp-quote text))
607 (when (string-match quoted line (length company-prefix))
611 (defun company-search-printing-char ()
613 (unless company-mode (error "Company not enabled"))
614 (unless company-search-mode (error "Company not in search mode"))
615 (setq company-search-string
616 (concat (or company-search-string "") (string last-command-event))
617 company-search-lighter (concat " Search: \"" company-search-string
619 (let ((pos (company-search company-search-string
620 (nthcdr company-selection company-candidates))))
623 (company-set-selection (+ company-selection pos) t))))
625 (defun company-search-repeat-forward ()
626 "Repeat the incremental search in completion candidates forward."
628 (unless company-mode (error "Company not enabled"))
629 (unless company-search-mode (error "Company not in search mode"))
630 (let ((pos (company-search company-search-string
631 (cdr (nthcdr company-selection
632 company-candidates)))))
635 (company-set-selection (+ company-selection pos 1) t))))
637 (defun company-search-repeat-backward ()
638 "Repeat the incremental search in completion candidates backwards."
640 (unless company-mode (error "Company not enabled"))
641 (unless company-search-mode (error "Company not in search mode"))
642 (let ((pos (company-search company-search-string
643 (nthcdr (- company-candidates-length
645 (reverse company-candidates)))))
648 (company-set-selection (- company-selection pos 1) t))))
650 (defun company-create-match-predicate ()
651 (setq company-candidates-predicate
653 ,(if company-candidates-predicate
654 `(and (string-match ,company-search-string candidate)
655 (funcall ,company-candidates-predicate
657 `(string-match ,company-search-string candidate))))
658 (company-update-candidates
659 (company-apply-predicate company-candidates company-candidates-predicate)))
661 (defun company-filter-printing-char ()
663 (unless company-mode (error "Company not enabled"))
664 (unless company-search-mode (error "Company not in search mode"))
665 (company-search-printing-char)
666 (company-create-match-predicate)
667 (company-call-frontends 'update))
669 (defun company-search-kill-others ()
670 "Limit the completion candidates to the ones matching the search string."
672 (unless company-mode (error "Company not enabled"))
673 (unless company-search-mode (error "Company not in search mode"))
674 (company-create-match-predicate)
675 (company-search-mode 0)
676 (company-call-frontends 'update))
678 (defun company-search-abort ()
679 "Abort searching the completion candidates."
681 (unless company-mode (error "Company not enabled"))
682 (unless company-search-mode (error "Company not in search mode"))
683 (company-set-selection company-search-old-selection t)
684 (company-search-mode 0))
686 (defun company-search-other-char ()
688 (unless company-mode (error "Company not enabled"))
689 (unless company-search-mode (error "Company not in search mode"))
690 (company-search-mode 0)
691 (when last-input-event
692 (clear-this-command-keys t)
693 (setq unread-command-events (list last-input-event))))
695 (defvar company-search-map
697 (keymap (make-keymap)))
698 (if (fboundp 'max-char)
699 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
700 'company-search-printing-char)
702 ;; obselete in Emacs 23
703 (let ((l (generic-character-list))
704 (table (nth 1 keymap)))
706 (set-char-table-default table (car l) 'isearch-printing-char)
708 (define-key keymap [t] 'company-search-other-char)
710 (define-key keymap (make-string 1 i) 'company-search-other-char)
713 (define-key keymap (vector i) 'company-search-printing-char)
715 (let ((meta-map (make-sparse-keymap)))
716 (define-key keymap (char-to-string meta-prefix-char) meta-map)
717 (define-key keymap [escape] meta-map))
718 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
719 (define-key keymap "\e\e\e" 'company-search-other-char)
720 (define-key keymap [escape escape escape] 'company-search-other-char)
722 (define-key keymap "\C-g" 'company-search-abort)
723 (define-key keymap "\C-s" 'company-search-repeat-forward)
724 (define-key keymap "\C-r" 'company-search-repeat-backward)
725 (define-key keymap "\C-o" 'company-search-kill-others)
727 "Keymap used for incrementally searching the completion candidates.")
729 (define-minor-mode company-search-mode
730 "Start searching the completion candidates incrementally.
732 \\<company-search-map>Search can be controlled with the commands:
733 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
734 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
735 - `company-search-abort' (\\[company-search-abort])
737 Regular characters are appended to the search string.
739 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
740 the search string to limit the completion candidates."
741 nil company-search-lighter nil
742 (if company-search-mode
743 (if (company-manual-begin)
745 (setq company-search-old-selection company-selection)
746 (company-call-frontends 'update))
747 (setq company-search-mode nil))
748 (kill-local-variable 'company-search-string)
749 (kill-local-variable 'company-search-lighter)
750 (kill-local-variable 'company-search-old-selection)
751 (company-enable-overriding-keymap company-active-map)))
753 (defun company-search-candidates ()
754 "Start searching the completion candidates incrementally.
756 \\<company-search-map>Search can be controlled with the commands:
757 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
758 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
759 - `company-search-abort' (\\[company-search-abort])
761 Regular characters are appended to the search string.
763 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
764 the search string to limit the completion candidates."
766 (company-search-mode 1)
767 (company-enable-overriding-keymap company-search-map))
769 (defvar company-filter-map
770 (let ((keymap (make-keymap)))
771 (define-key keymap [remap company-search-printing-char]
772 'company-filter-printing-char)
773 (set-keymap-parent keymap company-search-map)
775 "Keymap used for incrementally searching the completion candidates.")
777 (defun company-filter-candidates ()
778 "Start filtering the completion candidates incrementally.
779 This works the same way as `company-search-candidates' immediately
780 followed by `company-search-kill-others' after each input."
782 (company-search-mode 1)
783 (company-enable-overriding-keymap company-filter-map))
785 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
787 (defun company-select-next ()
788 "Select the next candidate in the list."
790 (when (company-manual-begin)
791 (company-set-selection (1+ company-selection))))
793 (defun company-select-previous ()
794 "Select the previous candidate in the list."
796 (when (company-manual-begin)
797 (company-set-selection (1- company-selection))))
799 (defun company-complete-selection ()
800 "Complete the selected candidate."
802 (when (company-manual-begin)
803 (insert (company-strip-prefix (nth company-selection company-candidates)))
806 (defun company-complete-common ()
807 "Complete the common part of all candidates."
809 (when (company-manual-begin)
810 (insert (company-strip-prefix company-common))))
812 (defun company-complete ()
813 "Complete the common part of all candidates or the current selection.
814 The first time this is called, the common part is completed, the second time, or
815 when the selection has been changed, the selected candidate is completed."
817 (when (company-manual-begin)
818 (if (or company-selection-changed
819 (eq last-command 'company-complete-common))
820 (call-interactively 'company-complete-selection)
821 (call-interactively 'company-complete-common)
822 (setq this-command 'company-complete-common))))
824 (defun company-complete-number (n)
825 "Complete the Nth candidate."
826 (when (company-manual-begin)
827 (and (< n 1) (> n company-candidates-length)
828 (error "No candidate number %d" n))
830 (insert (company-strip-prefix (nth n company-candidates)))
833 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
835 (defconst company-space-strings-limit 100)
837 (defconst company-space-strings
839 (dotimes (i company-space-strings-limit)
840 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
841 (apply 'vector lst)))
843 (defsubst company-space-string (len)
844 (if (< len company-space-strings-limit)
845 (aref company-space-strings len)
846 (make-string len ?\ )))
848 (defsubst company-safe-substring (str from &optional to)
849 (let ((len (length str)))
852 (if (and to (> to len))
853 (concat (substring str from)
854 (company-space-string (- to len)))
855 (substring str from to)))))
857 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
859 (defvar company-last-metadata nil)
860 (make-variable-buffer-local 'company-last-metadata)
862 (defun company-fetch-metadata ()
863 (let ((selected (nth company-selection company-candidates)))
864 (unless (equal selected (car company-last-metadata))
865 (setq company-last-metadata
866 (cons selected (funcall company-backend 'meta selected))))
867 (cdr company-last-metadata)))
869 (defun company-doc-buffer (&optional string)
870 (with-current-buffer (get-buffer-create "*Company meta-data*")
874 (defun company-show-doc-buffer ()
875 "Temporarily show a buffer with the complete documentation for the selection."
877 (unless company-mode (error "Company not enabled"))
878 (when (company-manual-begin)
879 (save-window-excursion
880 (let* ((height (window-height))
881 (row (cdr (posn-col-row (posn-at-point))))
882 (selected (nth company-selection company-candidates))
883 (buffer (funcall company-backend 'doc-buffer selected)))
885 (error "No documentation available.")
886 (display-buffer buffer)
887 (and (< (window-height) height)
888 (< (- (window-height) row 2) company-tooltip-limit)
889 (recenter (- (window-height) row 2)))
890 (while (eq 'scroll-other-window
891 (key-binding (vector (list (read-event)))))
892 (scroll-other-window))
893 (when last-input-event
894 (clear-this-command-keys t)
895 (setq unread-command-events (list last-input-event))))))))
897 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
899 (defvar company-pseudo-tooltip-overlay nil)
900 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
902 (defvar company-tooltip-offset 0)
903 (make-variable-buffer-local 'company-tooltip-offset)
905 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
908 (setq company-tooltip-offset
909 (max (min selection company-tooltip-offset)
910 (- selection -1 limit)))
912 (when (<= company-tooltip-offset 1)
914 (setq company-tooltip-offset 0))
916 (when (>= company-tooltip-offset (- num-lines limit 1))
918 (when (= selection (1- num-lines))
919 (decf company-tooltip-offset)
920 (when (<= company-tooltip-offset 1)
921 (setq company-tooltip-offset 0)
928 (defsubst company-round-tab (arg)
929 (* (/ (+ arg tab-width) tab-width) tab-width))
931 (defun company-untabify (str)
932 (let* ((pieces (split-string str "\t"))
935 (setcar copy (company-safe-substring
936 (car copy) 0 (company-round-tab (string-width (car copy)))))
938 (apply 'concat pieces)))
940 (defun company-fill-propertize (line width selected)
941 (setq line (company-safe-substring line 0 width))
942 (add-text-properties 0 width (list 'face 'company-tooltip) line)
943 (add-text-properties 0 (length company-common)
944 (list 'face 'company-tooltip-common) line)
946 (if (and company-search-string
947 (string-match (regexp-quote company-search-string) line
948 (length company-prefix)))
950 (add-text-properties (match-beginning 0) (match-end 0)
951 '(face company-tooltip-selection) line)
952 (when (< (match-beginning 0) (length company-common))
953 (add-text-properties (match-beginning 0) (length company-common)
954 '(face company-tooltip-common-selection)
956 (add-text-properties 0 width '(face company-tooltip-selection) line)
957 (add-text-properties 0 (length company-common)
958 (list 'face 'company-tooltip-common-selection)
964 (defun company-buffer-lines (beg end)
966 (let ((row (cdr (posn-col-row (posn-at-point))))
968 (while (and (equal (move-to-window-line (incf row)) row)
970 (push (buffer-substring beg (min end (1- (point)))) lines)
973 (push (buffer-substring beg end) lines))
976 (defsubst company-modify-line (old new offset)
977 (concat (company-safe-substring old 0 offset)
979 (company-safe-substring old (+ offset (length new)))))
981 (defun company-replacement-string (old lines column nl)
983 ;; Inject into old lines.
985 (push (company-modify-line (pop old) (pop lines) column) new))
986 ;; Append whole new lines.
988 (push (concat (company-space-string column) (pop lines)) new))
989 (concat (when nl "\n")
990 (mapconcat 'identity (nreverse new) "\n")
993 (defun company-create-lines (column selection limit)
995 (let ((len company-candidates-length)
1004 ;; Scroll to offset.
1005 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1007 (when (> company-tooltip-offset 0)
1008 (setq previous (format "...(%d)" company-tooltip-offset)))
1010 (setq remainder (- len limit company-tooltip-offset)
1011 remainder (when (> remainder 0)
1012 (setq remainder (format "...(%d)" remainder))))
1014 (decf selection company-tooltip-offset)
1015 (setq width (min (length previous) (length remainder))
1016 lines (nthcdr company-tooltip-offset company-candidates)
1021 (setq width (max (length (pop lines-copy)) width)))
1022 (setq width (min width (- (window-width) column)))
1024 (setq lines-copy lines)
1026 ;; number can make tooltip too long
1027 (and company-show-numbers
1028 (< (setq numbered company-tooltip-offset) 10)
1032 (push (propertize (company-safe-substring previous 0 width)
1033 'face 'company-tooltip)
1037 (push (company-fill-propertize
1038 (if (>= numbered 10)
1039 (company-reformat (pop lines))
1042 (company-safe-substring (company-reformat (pop lines))
1045 width (equal i selection))
1049 (push (propertize (company-safe-substring remainder 0 width)
1050 'face 'company-tooltip)
1053 (setq lines (nreverse new))))
1057 (defsubst company-pseudo-tooltip-height ()
1058 "Calculate the appropriate tooltip height."
1059 (max 3 (min company-tooltip-limit
1060 (- (window-height) 2
1061 (count-lines (window-start) (point-at-bol))))))
1063 (defun company-pseudo-tooltip-show (row column selection)
1064 (company-pseudo-tooltip-hide)
1069 (let* ((height (company-pseudo-tooltip-height))
1070 (lines (company-create-lines column selection height))
1071 (nl (< (move-to-window-line row) row))
1073 (end (save-excursion
1074 (move-to-window-line (+ row height))
1077 (mapcar 'company-untabify (company-buffer-lines beg end)))
1080 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1082 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1083 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1084 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1085 (overlay-put company-pseudo-tooltip-overlay 'company-before
1086 (company-replacement-string old-string lines column nl))
1087 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1089 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1091 (defun company-pseudo-tooltip-show-at-point (pos)
1092 (let ((col-row (posn-col-row (posn-at-point pos))))
1093 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1095 (defun company-pseudo-tooltip-edit (lines selection)
1096 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1097 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1098 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1099 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1100 (lines (company-create-lines column selection height)))
1101 (overlay-put company-pseudo-tooltip-overlay 'company-before
1102 (company-replacement-string old-string lines column nl))))
1104 (defun company-pseudo-tooltip-hide ()
1105 (when company-pseudo-tooltip-overlay
1106 (delete-overlay company-pseudo-tooltip-overlay)
1107 (setq company-pseudo-tooltip-overlay nil)))
1109 (defun company-pseudo-tooltip-hide-temporarily ()
1110 (when (overlayp company-pseudo-tooltip-overlay)
1111 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1112 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1114 (defun company-pseudo-tooltip-unhide ()
1115 (when company-pseudo-tooltip-overlay
1116 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1117 (overlay-put company-pseudo-tooltip-overlay 'before-string
1118 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1119 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1121 (defun company-pseudo-tooltip-frontend (command)
1122 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1124 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1126 (unless (and (overlayp company-pseudo-tooltip-overlay)
1127 (equal (overlay-get company-pseudo-tooltip-overlay
1129 (company-pseudo-tooltip-height)))
1131 (company-pseudo-tooltip-show-at-point (- (point)
1132 (length company-prefix))))
1133 (company-pseudo-tooltip-unhide))
1134 ('hide (company-pseudo-tooltip-hide)
1135 (setq company-tooltip-offset 0))
1136 ('update (when (overlayp company-pseudo-tooltip-overlay)
1137 (company-pseudo-tooltip-edit company-candidates
1138 company-selection)))))
1140 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1141 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1142 (unless (and (eq command 'post-command)
1143 (not (cdr company-candidates)))
1144 (company-pseudo-tooltip-frontend command)))
1146 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1148 (defvar company-preview-overlay nil)
1149 (make-variable-buffer-local 'company-preview-overlay)
1151 (defun company-preview-show-at-point (pos)
1152 (company-preview-hide)
1154 (setq company-preview-overlay (make-overlay pos pos))
1156 (let ((completion (company-strip-prefix (nth company-selection
1157 company-candidates))))
1158 (and (equal pos (point))
1159 (not (equal completion ""))
1160 (add-text-properties 0 1 '(cursor t) completion))
1162 (setq completion (propertize completion 'face 'company-preview))
1163 (add-text-properties 0 (- (length company-common) (length company-prefix))
1164 '(face company-preview-common) completion)
1166 (overlay-put company-preview-overlay 'after-string completion)
1167 (overlay-put company-preview-overlay 'window (selected-window))))
1169 (defun company-preview-hide ()
1170 (when company-preview-overlay
1171 (delete-overlay company-preview-overlay)
1172 (setq company-preview-overlay nil)))
1174 (defun company-preview-frontend (command)
1175 "A `company-mode' front-end showing the selection as if it had been inserted."
1177 ('pre-command (company-preview-hide))
1178 ('post-command (company-preview-show-at-point (point)))
1179 ('hide (company-preview-hide))))
1181 (defun company-preview-if-just-one-frontend (command)
1182 "`company-preview-frontend', but only shown for single candidates."
1183 (unless (and (eq command 'post-command)
1184 (cdr company-candidates))
1185 (company-preview-frontend command)))
1187 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1189 (defvar company-echo-last-msg nil)
1190 (make-variable-buffer-local 'company-echo-last-msg)
1192 (defvar company-echo-timer nil)
1194 (defvar company-echo-delay .1)
1196 (defun company-echo-show (&optional getter)
1198 (setq company-echo-last-msg (funcall getter)))
1199 (let ((message-log-max nil))
1200 (if company-echo-last-msg
1201 (message "%s" company-echo-last-msg)
1204 (defsubst company-echo-show-soon (&optional getter)
1205 (when company-echo-timer
1206 (cancel-timer company-echo-timer))
1207 (setq company-echo-timer (run-with-timer company-echo-delay nil
1208 'company-echo-show getter)))
1210 (defun company-echo-format ()
1212 (let ((limit (window-width (minibuffer-window)))
1214 ;; Roll to selection.
1215 (candidates (nthcdr company-selection company-candidates))
1216 (i (if company-show-numbers company-selection 99999))
1220 (setq comp (company-reformat (pop candidates))
1221 len (+ len 1 (length comp)))
1225 (setq comp (propertize (format "%d: %s" i comp)
1226 'face 'company-echo))
1229 (add-text-properties 3 (+ 3 (length company-common))
1230 '(face company-echo-common) comp))
1231 (setq comp (propertize comp 'face 'company-echo))
1232 (add-text-properties 0 (length company-common)
1233 '(face company-echo-common) comp))
1235 (setq candidates nil)
1238 (mapconcat 'identity (nreverse msg) " ")))
1240 (defun company-echo-strip-common-format ()
1242 (let ((limit (window-width (minibuffer-window)))
1243 (len (+ (length company-prefix) 2))
1244 ;; Roll to selection.
1245 (candidates (nthcdr company-selection company-candidates))
1246 (i (if company-show-numbers company-selection 99999))
1250 (setq comp (company-strip-prefix (pop candidates))
1251 len (+ len 2 (length comp)))
1254 (setq comp (format "%s (%d)" comp i))
1258 (setq candidates nil)
1259 (push (propertize comp 'face 'company-echo) msg)))
1261 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1262 (mapconcat 'identity (nreverse msg) ", ")
1265 (defun company-echo-hide ()
1266 (when company-echo-timer
1267 (cancel-timer company-echo-timer))
1268 (unless (equal company-echo-last-msg "")
1269 (setq company-echo-last-msg "")
1270 (company-echo-show)))
1272 (defun company-echo-frontend (command)
1273 "A `company-mode' front-end showing the candidates in the echo area."
1275 ('pre-command (company-echo-show-soon))
1276 ('post-command (company-echo-show-soon 'company-echo-format))
1277 ('hide (company-echo-hide))))
1279 (defun company-echo-strip-common-frontend (command)
1280 "A `company-mode' front-end showing the candidates in the echo area."
1282 ('pre-command (company-echo-show-soon))
1283 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1284 ('hide (company-echo-hide))))
1286 (defun company-echo-metadata-frontend (command)
1287 "A `company-mode' front-end showing the documentation in the echo area."
1289 ('pre-command (company-echo-show-soon))
1290 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1291 ('hide (company-echo-hide))))
1294 ;;; company.el ends here