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
54 ;; wrong, unless company is allowed to temporarily insert a fake newline.
55 ;; This behavior is enabled by `company-end-of-buffer-workaround'.
59 ;; Added etags back-end.
60 ;; Added work-around for end-of-buffer bug.
61 ;; Added `company-filter-candidates'.
62 ;; More local Lisp variables are now included in the candidates.
65 ;; Fixed elisp documentation buffer always showing the same doc.
66 ;; Added `company-echo-strip-common-frontend'.
67 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
68 ;; Don't hide the echo message if it isn't shown.
75 (eval-when-compile (require 'cl))
77 (add-to-list 'debug-ignored-errors
78 "^Pseudo tooltip frontend cannot be used twice$")
79 (add-to-list 'debug-ignored-errors "^Preview frontend cannot be used twice$")
80 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
81 (add-to-list 'debug-ignored-errors "^No documentation available$")
82 (add-to-list 'debug-ignored-errors "^Company not enabled$")
83 (add-to-list 'debug-ignored-errors "^Company not in search mode$")
84 (add-to-list 'debug-ignored-errors "^No candidate number ")
87 "Extensible inline text completion mechanism"
92 (defface company-tooltip
93 '((t :background "yellow"
95 "*Face used for the tool tip."
98 (defface company-tooltip-selection
99 '((default :inherit company-tooltip)
100 (((class color) (min-colors 88)) (:background "orange1"))
101 (t (:background "green")))
102 "*Face used for the selection in the tool tip."
105 (defface company-tooltip-common
106 '((t :inherit company-tooltip
108 "*Face used for the common completion in the tool tip."
111 (defface company-tooltip-common-selection
112 '((t :inherit company-tooltip-selection
114 "*Face used for the selected common completion in the tool tip."
117 (defcustom company-tooltip-limit 10
118 "*The maximum number of candidates in the tool tip"
122 (defface company-preview
123 '((t :background "blue4"
124 :foreground "wheat"))
125 "*Face used for the completion preview."
128 (defface company-preview-common
129 '((t :inherit company-preview
131 "*Face used for the common part of the completion preview."
134 (defface company-preview-search
135 '((t :inherit company-preview
136 :background "blue1"))
137 "*Face used for the search string in the completion preview."
140 (defface company-echo nil
141 "*Face used for completions in the echo area."
144 (defface company-echo-common
145 '((((background dark)) (:foreground "firebrick1"))
146 (((background light)) (:background "firebrick4")))
147 "*Face used for the common part of completions in the echo area."
150 (defun company-frontends-set (variable value)
152 (let ((remainder value))
153 (setcdr remainder (delq (car remainder) (cdr remainder))))
154 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
155 (memq 'company-pseudo-tooltip-frontend value)
156 (error "Pseudo tooltip frontend cannot be used twice"))
157 (and (memq 'company-preview-if-just-one-frontend value)
158 (memq 'company-preview-frontend value)
159 (error "Preview frontend cannot be used twice"))
160 (and (memq 'company-echo value)
161 (memq 'company-echo-metadata-frontend value)
162 (error "Echo area cannot be used twice"))
163 ;; preview must come last
164 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
166 (setq value (append (delq f value) (list f)))))
167 (set variable value))
169 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
170 company-preview-frontend
171 company-echo-metadata-frontend)
172 "*The list of active front-ends (visualizations).
173 Each front-end is a function that takes one argument. It is called with
174 one of the following arguments:
176 'show: When the visualization should start.
178 'hide: When the visualization should end.
180 'update: When the data has been updated.
182 'pre-command: Before every command that is executed while the
183 visualization is active.
185 'post-command: After every command that is executed while the
186 visualization is active.
188 The visualized data is stored in `company-prefix', `company-candidates',
189 `company-common', `company-selection', `company-point' and
190 `company-search-string'."
191 :set 'company-frontends-set
193 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
194 (const :tag "echo, strip common"
195 company-echo-strip-common-frontend)
196 (const :tag "show echo meta-data in echo"
197 company-echo-metadata-frontend)
198 (const :tag "pseudo tooltip"
199 company-pseudo-tooltip-frontend)
200 (const :tag "pseudo tooltip, multiple only"
201 company-pseudo-tooltip-unless-just-one-frontend)
202 (const :tag "preview" company-preview-frontend)
203 (const :tag "preview, unique only"
204 company-preview-if-just-one-frontend)
205 (function :tag "custom function" nil))))
207 (defcustom company-backends '(company-elisp company-nxml company-css
208 company-semantic company-gtags company-etags
209 company-oddmuse company-files company-dabbrev)
210 "*The list of active back-ends (completion engines).
211 Each back-end is a function that takes a variable number of arguments.
212 The first argument is the command requested from the back-end. It is one
215 'prefix: The back-end should return the text to be completed. It must be
216 text immediately before `point'. Returning nil passes control to the next
219 'candidates: The second argument is the prefix to be completed. The
220 return value should be a list of candidates that start with the prefix.
224 'sorted: The back-end may return t here to indicate that the candidates
225 are sorted and will not need to be sorted again.
227 'no-cache: Usually company doesn't ask for candidates again as completion
228 progresses, unless the back-end returns t for this command. The second
229 argument is the latest prefix.
231 'meta: The second argument is a completion candidate. The back-end should
232 return a (short) documentation string for it.
234 'doc-buffer: The second argument is a completion candidate. The back-end should
235 create a buffer (preferably with `company-doc-buffer'), fill it with
236 documentation and return it.
238 The back-end should return nil for all commands it does not support or
239 does not know about."
241 :type '(repeat (function :tag "function" nil)))
243 (defcustom company-minimum-prefix-length 3
244 "*The minimum prefix length for automatic completion."
246 :type '(integer :tag "prefix length"))
248 (defcustom company-idle-delay .7
249 "*The idle delay in seconds until automatic completions starts.
250 A value of nil means never complete automatically, t means complete
251 immediately when a prefix of `company-minimum-prefix-length' is reached."
253 :type '(choice (const :tag "never (nil)" nil)
254 (const :tag "immediate (t)" t)
255 (number :tag "seconds")))
257 (defcustom company-show-numbers nil
258 "*If enabled, show quick-access numbers for the first ten candidates."
260 :type '(choice (const :tag "off" nil)
261 (const :tag "on" t)))
263 (defvar company-end-of-buffer-workaround t
264 "*Work around a visualization bug when completing at the end of the buffer.
265 The work-around consists of adding a newline.")
267 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
269 (defvar company-mode-map (make-sparse-keymap)
270 "Keymap used by `company-mode'.")
272 (defvar company-active-map
273 (let ((keymap (make-sparse-keymap)))
274 (define-key keymap (kbd "M-n") 'company-select-next)
275 (define-key keymap (kbd "M-p") 'company-select-previous)
276 (define-key keymap (kbd "<down>") 'company-select-next)
277 (define-key keymap (kbd "<up>") 'company-select-previous)
278 (define-key keymap "\C-m" 'company-complete-selection)
279 (define-key keymap "\t" 'company-complete-common)
280 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
281 (define-key keymap "\C-s" 'company-search-candidates)
282 (define-key keymap "\C-\M-s" 'company-filter-candidates)
284 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
285 `(lambda () (interactive) (company-complete-number ,i))))
288 "Keymap that is enabled during an active completion.")
291 (define-minor-mode company-mode
292 "\"complete anything\"; in in-buffer completion framework.
293 Completion starts automatically, depending on the values
294 `company-idle-delay' and `company-minimum-prefix-length'.
296 Completion can be controlled with the commands:
297 `company-complete-common', `company-complete-selection', `company-complete',
298 `company-select-next', `company-select-previous'. If these commands are
299 called before `company-idle-delay', completion will also start.
301 Completions can be searched with `company-search-candidates' or
302 `company-filter-candidates'. These can be used while completion is
305 The completion data is retrieved using `company-backends' and displayed using
308 regular keymap (`company-mode-map'):
311 keymap during active completions (`company-active-map'):
313 \\{company-active-map}"
314 nil " comp" company-mode-map
317 (add-hook 'pre-command-hook 'company-pre-command nil t)
318 (add-hook 'post-command-hook 'company-post-command nil t)
319 (dolist (backend company-backends)
320 (unless (fboundp backend)
321 (ignore-errors (require backend nil t)))
322 (unless (fboundp backend)
323 (message "Company back-end '%s' could not be initialized"
325 (remove-hook 'pre-command-hook 'company-pre-command t)
326 (remove-hook 'post-command-hook 'company-post-command t)
328 (kill-local-variable 'company-point)))
330 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
332 (defvar company-overriding-keymap-bound nil)
333 (make-variable-buffer-local 'company-overriding-keymap-bound)
335 (defvar company-old-keymap nil)
336 (make-variable-buffer-local 'company-old-keymap)
338 (defvar company-my-keymap nil)
339 (make-variable-buffer-local 'company-my-keymap)
341 (defsubst company-enable-overriding-keymap (keymap)
342 (setq company-my-keymap keymap)
343 (when company-overriding-keymap-bound
344 (company-uninstall-map)))
346 (defun company-install-map ()
347 (unless (or company-overriding-keymap-bound
348 (null company-my-keymap))
349 (setq company-old-keymap overriding-terminal-local-map
350 overriding-terminal-local-map company-my-keymap
351 company-overriding-keymap-bound t)))
353 (defun company-uninstall-map ()
354 (when (and company-overriding-keymap-bound
355 (eq overriding-terminal-local-map company-my-keymap))
356 (setq overriding-terminal-local-map company-old-keymap
357 company-overriding-keymap-bound nil)))
360 ;; Emacs calculates the active keymaps before reading the event. That means we
361 ;; cannot change the keymap from a timer. So we send a bogus command.
362 (defun company-ignore ()
365 (global-set-key '[31415926] 'company-ignore)
367 (defun company-input-noop ()
368 (push 31415926 unread-command-events))
370 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 (defun company-grab (regexp &optional expression)
373 (when (looking-back regexp)
374 (or (match-string-no-properties (or expression 0)) "")))
376 (defun company-in-string-or-comment (&optional point)
377 (let ((pos (syntax-ppss)))
378 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
380 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
382 (defvar company-backend nil)
383 (make-variable-buffer-local 'company-backend)
385 (defvar company-prefix nil)
386 (make-variable-buffer-local 'company-prefix)
388 (defvar company-candidates nil)
389 (make-variable-buffer-local 'company-candidates)
391 (defvar company-candidates-length nil)
392 (make-variable-buffer-local 'company-candidates-length)
394 (defvar company-candidates-cache nil)
395 (make-variable-buffer-local 'company-candidates-cache)
397 (defvar company-candidates-predicate nil)
398 (make-variable-buffer-local 'company-candidates-predicate)
400 (defvar company-common nil)
401 (make-variable-buffer-local 'company-common)
403 (defvar company-selection 0)
404 (make-variable-buffer-local 'company-selection)
406 (defvar company-selection-changed nil)
407 (make-variable-buffer-local 'company-selection-changed)
409 (defvar company-point nil)
410 (make-variable-buffer-local 'company-point)
412 (defvar company-timer nil)
414 (defvar company-added-newline nil)
415 (make-variable-buffer-local 'company-added-newline)
417 (defsubst company-strip-prefix (str)
418 (substring str (length company-prefix)))
420 (defsubst company-reformat (candidate)
421 ;; company-ispell needs this, because the results are always lower-case
422 ;; It's mory efficient to fix it only when they are displayed.
423 (concat company-prefix (substring candidate (length company-prefix))))
425 (defsubst company-should-complete (prefix)
426 (and (eq company-idle-delay t)
427 (>= (length prefix) company-minimum-prefix-length)))
429 (defsubst company-call-frontends (command)
430 (dolist (frontend company-frontends)
432 (funcall frontend command)
433 (error (error "Company: Front-end %s error \"%s\" on command %s"
434 frontend (error-message-string err) command)))))
436 (defsubst company-set-selection (selection &optional force-update)
437 (setq selection (max 0 (min (1- company-candidates-length) selection)))
438 (when (or force-update (not (equal selection company-selection)))
439 (setq company-selection selection
440 company-selection-changed t)
441 (company-call-frontends 'update)))
443 (defun company-apply-predicate (candidates predicate)
445 (dolist (c candidates)
446 (when (funcall predicate c)
450 (defun company-update-candidates (candidates)
451 (setq company-candidates-length (length candidates))
452 (if (> company-selection 0)
453 ;; Try to restore the selection
454 (let ((selected (nth company-selection company-candidates)))
455 (setq company-selection 0
456 company-candidates candidates)
458 (while (and candidates (string< (pop candidates) selected))
459 (incf company-selection))
461 ;; Make sure selection isn't out of bounds.
462 (setq company-selection (min (1- company-candidates-length)
463 company-selection)))))
464 (setq company-selection 0
465 company-candidates candidates))
467 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
468 (setq company-common (try-completion company-prefix company-candidates)))
469 (when (eq company-common t)
470 (setq company-candidates nil)))
472 (defsubst company-calculate-candidates (prefix)
473 (setq company-prefix prefix)
474 (company-update-candidates
475 (or (cdr (assoc prefix company-candidates-cache))
476 (when company-candidates-cache
477 (let ((len (length prefix))
478 (completion-ignore-case (funcall company-backend 'ignore-case))
481 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
482 company-candidates-cache)))
483 (return (all-completions prefix prev))))))
484 (let ((candidates (funcall company-backend 'candidates prefix)))
485 (when company-candidates-predicate
487 (company-apply-predicate candidates
488 company-candidates-predicate)))
489 (unless (funcall company-backend 'sorted)
490 (setq candidates (sort candidates 'string<)))
492 (unless company-candidates-cache
493 (company-call-frontends 'show))
494 (unless (assoc prefix company-candidates-cache)
495 (push (cons prefix company-candidates) company-candidates-cache))
498 (defun company-idle-begin (buf win tick pos)
500 (eq buf (current-buffer))
501 (eq win (selected-window))
502 (eq tick (buffer-chars-modified-tick))
504 (not company-candidates)
505 (not (equal (point) company-point))
506 (let ((company-idle-delay t))
508 (when company-candidates
510 (company-post-command)))))
512 (defun company-manual-begin ()
514 (unless company-mode (error "Company not enabled"))
516 (not company-candidates)
517 (let ((company-idle-delay t)
518 (company-minimum-prefix-length 0))
520 ;; Return non-nil if active.
523 (defun company-continue ()
524 (when company-candidates
525 (when (funcall company-backend 'no-cache company-prefix)
526 ;; Don't complete existing candidates, fetch new ones.
527 (setq company-candidates-cache nil))
528 (let ((new-prefix (funcall company-backend 'prefix)))
529 (unless (and (= (- (point) (length new-prefix))
530 (- company-point (length company-prefix)))
531 (or (equal company-prefix new-prefix)
532 (company-calculate-candidates new-prefix)))
533 (setq company-candidates nil)))))
535 (defun company-begin ()
536 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
537 ;; Don't complete in these cases.
538 (setq company-candidates nil)
540 (unless company-candidates
542 (dolist (backend company-backends)
543 (and (fboundp backend)
544 (setq prefix (funcall backend 'prefix))
545 (company-should-complete prefix)
546 (setq company-backend backend)
547 (company-calculate-candidates prefix))
549 (if company-candidates
551 (when (and company-end-of-buffer-workaround (eobp))
552 (save-excursion (insert "\n"))
553 (setq company-added-newline (buffer-chars-modified-tick)))
554 (setq company-point (point))
555 (company-enable-overriding-keymap company-active-map)
556 (company-call-frontends 'update))
559 (defun company-cancel ()
560 (and company-added-newline
561 (> (point-max) (point-min))
562 (let ((tick (buffer-chars-modified-tick)))
563 (delete-region (1- (point-max)) (point-max))
564 (equal tick company-added-newline))
565 ;; Only set unmodified when tick remained the same since insert.
566 (set-buffer-modified-p nil))
567 (setq company-added-newline nil
570 company-candidates nil
571 company-candidates-length nil
572 company-candidates-cache nil
573 company-candidates-predicate nil
576 company-selection-changed nil
579 (cancel-timer company-timer))
580 (company-search-mode 0)
581 (company-call-frontends 'hide)
582 (company-enable-overriding-keymap nil))
584 (defun company-abort ()
586 ;; Don't start again, unless started manually.
587 (setq company-point (point)))
589 (defsubst company-keep (command)
590 (and (symbolp command) (get command 'company-keep)))
592 (defun company-pre-command ()
593 (unless (company-keep this-command)
595 (when company-candidates
596 (company-call-frontends 'pre-command))
597 (error (message "Company: An error occurred in pre-command")
598 (message "%s" (error-message-string err))
601 (cancel-timer company-timer))
602 (company-uninstall-map))
604 (defun company-post-command ()
605 (unless (company-keep this-command)
608 (unless (equal (point) company-point)
610 (when company-candidates
611 (company-call-frontends 'post-command))
612 (when (numberp company-idle-delay)
614 (run-with-timer company-idle-delay nil 'company-idle-begin
615 (current-buffer) (selected-window)
616 (buffer-chars-modified-tick) (point)))))
617 (error (message "Company: An error occurred in post-command")
618 (message "%s" (error-message-string err))
620 (company-install-map))
622 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
624 (defvar company-search-string nil)
625 (make-variable-buffer-local 'company-search-string)
627 (defvar company-search-lighter " Search: \"\"")
628 (make-variable-buffer-local 'company-search-lighter)
630 (defvar company-search-old-map nil)
631 (make-variable-buffer-local 'company-search-old-map)
633 (defvar company-search-old-selection 0)
634 (make-variable-buffer-local 'company-search-old-selection)
636 (defun company-search (text lines)
637 (let ((quoted (regexp-quote text))
640 (when (string-match quoted line (length company-prefix))
644 (defun company-search-printing-char ()
646 (unless company-mode (error "Company not enabled"))
647 (unless company-search-mode (error "Company not in search mode"))
648 (setq company-search-string
649 (concat (or company-search-string "") (string last-command-event))
650 company-search-lighter (concat " Search: \"" company-search-string
652 (let ((pos (company-search company-search-string
653 (nthcdr company-selection company-candidates))))
656 (company-set-selection (+ company-selection pos) t))))
658 (defun company-search-repeat-forward ()
659 "Repeat the incremental search in completion candidates forward."
661 (unless company-mode (error "Company not enabled"))
662 (unless company-search-mode (error "Company not in search mode"))
663 (let ((pos (company-search company-search-string
664 (cdr (nthcdr company-selection
665 company-candidates)))))
668 (company-set-selection (+ company-selection pos 1) t))))
670 (defun company-search-repeat-backward ()
671 "Repeat the incremental search in completion candidates backwards."
673 (unless company-mode (error "Company not enabled"))
674 (unless company-search-mode (error "Company not in search mode"))
675 (let ((pos (company-search company-search-string
676 (nthcdr (- company-candidates-length
678 (reverse company-candidates)))))
681 (company-set-selection (- company-selection pos 1) t))))
683 (defun company-create-match-predicate ()
684 (setq company-candidates-predicate
686 ,(if company-candidates-predicate
687 `(and (string-match ,company-search-string candidate)
688 (funcall ,company-candidates-predicate
690 `(string-match ,company-search-string candidate))))
691 (company-update-candidates
692 (company-apply-predicate company-candidates company-candidates-predicate)))
694 (defun company-filter-printing-char ()
696 (unless company-mode (error "Company not enabled"))
697 (unless company-search-mode (error "Company not in search mode"))
698 (company-search-printing-char)
699 (company-create-match-predicate)
700 (company-call-frontends 'update))
702 (defun company-search-kill-others ()
703 "Limit the completion candidates to the ones matching the search string."
705 (unless company-mode (error "Company not enabled"))
706 (unless company-search-mode (error "Company not in search mode"))
707 (company-create-match-predicate)
708 (company-search-mode 0)
709 (company-call-frontends 'update))
711 (defun company-search-abort ()
712 "Abort searching the completion candidates."
714 (unless company-mode (error "Company not enabled"))
715 (unless company-search-mode (error "Company not in search mode"))
716 (company-set-selection company-search-old-selection t)
717 (company-search-mode 0))
719 (defun company-search-other-char ()
721 (unless company-mode (error "Company not enabled"))
722 (unless company-search-mode (error "Company not in search mode"))
723 (company-search-mode 0)
724 (when last-input-event
725 (clear-this-command-keys t)
726 (setq unread-command-events (list last-input-event))))
728 (defvar company-search-map
730 (keymap (make-keymap)))
731 (if (fboundp 'max-char)
732 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
733 'company-search-printing-char)
735 ;; obselete in Emacs 23
736 (let ((l (generic-character-list))
737 (table (nth 1 keymap)))
739 (set-char-table-default table (car l) 'company-search-printing-char)
741 (define-key keymap [t] 'company-search-other-char)
743 (define-key keymap (make-string 1 i) 'company-search-other-char)
746 (define-key keymap (vector i) 'company-search-printing-char)
748 (let ((meta-map (make-sparse-keymap)))
749 (define-key keymap (char-to-string meta-prefix-char) meta-map)
750 (define-key keymap [escape] meta-map))
751 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
752 (define-key keymap "\e\e\e" 'company-search-other-char)
753 (define-key keymap [escape escape escape] 'company-search-other-char)
755 (define-key keymap "\C-g" 'company-search-abort)
756 (define-key keymap "\C-s" 'company-search-repeat-forward)
757 (define-key keymap "\C-r" 'company-search-repeat-backward)
758 (define-key keymap "\C-o" 'company-search-kill-others)
760 "Keymap used for incrementally searching the completion candidates.")
762 (define-minor-mode company-search-mode
763 "Search mode for completion candidates.
764 Don't start this directly, use `company-search-candidates' or
765 `company-filter-candidates'."
766 nil company-search-lighter nil
767 (if company-search-mode
768 (if (company-manual-begin)
770 (setq company-search-old-selection company-selection)
771 (company-call-frontends 'update))
772 (setq company-search-mode nil))
773 (kill-local-variable 'company-search-string)
774 (kill-local-variable 'company-search-lighter)
775 (kill-local-variable 'company-search-old-selection)
776 (company-enable-overriding-keymap company-active-map)))
778 (defun company-search-candidates ()
779 "Start searching the completion candidates incrementally.
781 \\<company-search-map>Search can be controlled with the commands:
782 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
783 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
784 - `company-search-abort' (\\[company-search-abort])
786 Regular characters are appended to the search string.
788 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
789 the search string to limit the completion candidates."
791 (company-search-mode 1)
792 (company-enable-overriding-keymap company-search-map))
794 (defvar company-filter-map
795 (let ((keymap (make-keymap)))
796 (define-key keymap [remap company-search-printing-char]
797 'company-filter-printing-char)
798 (set-keymap-parent keymap company-search-map)
800 "Keymap used for incrementally searching the completion candidates.")
802 (defun company-filter-candidates ()
803 "Start filtering the completion candidates incrementally.
804 This works the same way as `company-search-candidates' immediately
805 followed by `company-search-kill-others' after each input."
807 (company-search-mode 1)
808 (company-enable-overriding-keymap company-filter-map))
810 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
812 (defun company-select-next ()
813 "Select the next candidate in the list."
815 (when (company-manual-begin)
816 (company-set-selection (1+ company-selection))))
818 (defun company-select-previous ()
819 "Select the previous candidate in the list."
821 (when (company-manual-begin)
822 (company-set-selection (1- company-selection))))
824 (defun company-complete-selection ()
825 "Complete the selected candidate."
827 (when (company-manual-begin)
828 (insert (company-strip-prefix (nth company-selection company-candidates)))
831 (defun company-complete-common ()
832 "Complete the common part of all candidates."
834 (when (company-manual-begin)
835 (insert (company-strip-prefix company-common))))
837 (defun company-complete ()
838 "Complete the common part of all candidates or the current selection.
839 The first time this is called, the common part is completed, the second time, or
840 when the selection has been changed, the selected candidate is completed."
842 (when (company-manual-begin)
843 (if (or company-selection-changed
844 (eq last-command 'company-complete-common))
845 (call-interactively 'company-complete-selection)
846 (call-interactively 'company-complete-common)
847 (setq this-command 'company-complete-common))))
849 (defun company-complete-number (n)
850 "Complete the Nth candidate."
851 (when (company-manual-begin)
852 (and (< n 1) (> n company-candidates-length)
853 (error "No candidate number %d" n))
855 (insert (company-strip-prefix (nth n company-candidates)))
858 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
860 (defconst company-space-strings-limit 100)
862 (defconst company-space-strings
864 (dotimes (i company-space-strings-limit)
865 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
866 (apply 'vector lst)))
868 (defsubst company-space-string (len)
869 (if (< len company-space-strings-limit)
870 (aref company-space-strings len)
871 (make-string len ?\ )))
873 (defsubst company-safe-substring (str from &optional to)
874 (let ((len (length str)))
877 (if (and to (> to len))
878 (concat (substring str from)
879 (company-space-string (- to len)))
880 (substring str from to)))))
882 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
884 (defvar company-last-metadata nil)
885 (make-variable-buffer-local 'company-last-metadata)
887 (defun company-fetch-metadata ()
888 (let ((selected (nth company-selection company-candidates)))
889 (unless (equal selected (car company-last-metadata))
890 (setq company-last-metadata
891 (cons selected (funcall company-backend 'meta selected))))
892 (cdr company-last-metadata)))
894 (defun company-doc-buffer (&optional string)
895 (with-current-buffer (get-buffer-create "*Company meta-data*")
899 (defun company-show-doc-buffer ()
900 "Temporarily show a buffer with the complete documentation for the selection."
902 (unless company-mode (error "Company not enabled"))
903 (when (company-manual-begin)
904 (save-window-excursion
905 (let* ((height (window-height))
906 (row (cdr (posn-col-row (posn-at-point))))
907 (selected (nth company-selection company-candidates))
908 (buffer (funcall company-backend 'doc-buffer selected)))
910 (error "No documentation available.")
911 (display-buffer buffer)
912 (and (< (window-height) height)
913 (< (- (window-height) row 2) company-tooltip-limit)
914 (recenter (- (window-height) row 2)))
915 (while (eq 'scroll-other-window
916 (key-binding (vector (list (read-event)))))
917 (scroll-other-window))
918 (when last-input-event
919 (clear-this-command-keys t)
920 (setq unread-command-events (list last-input-event))))))))
921 (put 'company-show-doc-buffer 'company-keep t)
923 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
925 (defvar company-pseudo-tooltip-overlay nil)
926 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
928 (defvar company-tooltip-offset 0)
929 (make-variable-buffer-local 'company-tooltip-offset)
931 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
934 (setq company-tooltip-offset
935 (max (min selection company-tooltip-offset)
936 (- selection -1 limit)))
938 (when (<= company-tooltip-offset 1)
940 (setq company-tooltip-offset 0))
942 (when (>= company-tooltip-offset (- num-lines limit 1))
944 (when (= selection (1- num-lines))
945 (decf company-tooltip-offset)
946 (when (<= company-tooltip-offset 1)
947 (setq company-tooltip-offset 0)
954 (defsubst company-round-tab (arg)
955 (* (/ (+ arg tab-width) tab-width) tab-width))
957 (defun company-untabify (str)
958 (let* ((pieces (split-string str "\t"))
961 (setcar copy (company-safe-substring
962 (car copy) 0 (company-round-tab (string-width (car copy)))))
964 (apply 'concat pieces)))
966 (defun company-fill-propertize (line width selected)
967 (setq line (company-safe-substring line 0 width))
968 (add-text-properties 0 width (list 'face 'company-tooltip) line)
969 (add-text-properties 0 (length company-common)
970 (list 'face 'company-tooltip-common) line)
972 (if (and company-search-string
973 (string-match (regexp-quote company-search-string) line
974 (length company-prefix)))
976 (add-text-properties (match-beginning 0) (match-end 0)
977 '(face company-tooltip-selection) line)
978 (when (< (match-beginning 0) (length company-common))
979 (add-text-properties (match-beginning 0) (length company-common)
980 '(face company-tooltip-common-selection)
982 (add-text-properties 0 width '(face company-tooltip-selection) line)
983 (add-text-properties 0 (length company-common)
984 (list 'face 'company-tooltip-common-selection)
990 (defun company-buffer-lines (beg end)
992 (let ((row (cdr (posn-col-row (posn-at-point))))
994 (while (and (equal (move-to-window-line (incf row)) row)
996 (push (buffer-substring beg (min end (1- (point)))) lines)
999 (push (buffer-substring beg end) lines))
1002 (defsubst company-modify-line (old new offset)
1003 (concat (company-safe-substring old 0 offset)
1005 (company-safe-substring old (+ offset (length new)))))
1007 (defun company-replacement-string (old lines column nl)
1009 ;; Inject into old lines.
1011 (push (company-modify-line (pop old) (pop lines) column) new))
1012 ;; Append whole new lines.
1014 (push (concat (company-space-string column) (pop lines)) new))
1015 (concat (when nl "\n")
1016 (mapconcat 'identity (nreverse new) "\n")
1019 (defun company-create-lines (column selection limit)
1021 (let ((len company-candidates-length)
1030 ;; Scroll to offset.
1031 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1033 (when (> company-tooltip-offset 0)
1034 (setq previous (format "...(%d)" company-tooltip-offset)))
1036 (setq remainder (- len limit company-tooltip-offset)
1037 remainder (when (> remainder 0)
1038 (setq remainder (format "...(%d)" remainder))))
1040 (decf selection company-tooltip-offset)
1041 (setq width (min (length previous) (length remainder))
1042 lines (nthcdr company-tooltip-offset company-candidates)
1047 (setq width (max (length (pop lines-copy)) width)))
1048 (setq width (min width (- (window-width) column)))
1050 (setq lines-copy lines)
1052 ;; number can make tooltip too long
1053 (and company-show-numbers
1054 (< (setq numbered company-tooltip-offset) 10)
1058 (push (propertize (company-safe-substring previous 0 width)
1059 'face 'company-tooltip)
1063 (push (company-fill-propertize
1064 (if (>= numbered 10)
1065 (company-reformat (pop lines))
1068 (company-safe-substring (company-reformat (pop lines))
1071 width (equal i selection))
1075 (push (propertize (company-safe-substring remainder 0 width)
1076 'face 'company-tooltip)
1079 (setq lines (nreverse new))))
1083 (defsubst company-pseudo-tooltip-height ()
1084 "Calculate the appropriate tooltip height."
1085 (max 3 (min company-tooltip-limit
1086 (- (window-height) 2
1087 (count-lines (window-start) (point-at-bol))))))
1089 (defun company-pseudo-tooltip-show (row column selection)
1090 (company-pseudo-tooltip-hide)
1095 (let* ((height (company-pseudo-tooltip-height))
1096 (lines (company-create-lines column selection height))
1097 (nl (< (move-to-window-line row) row))
1099 (end (save-excursion
1100 (move-to-window-line (+ row height))
1103 (mapcar 'company-untabify (company-buffer-lines beg end)))
1106 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1108 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1109 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1110 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1111 (overlay-put company-pseudo-tooltip-overlay 'company-before
1112 (company-replacement-string old-string lines column nl))
1113 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1115 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1117 (defun company-pseudo-tooltip-show-at-point (pos)
1118 (let ((col-row (posn-col-row (posn-at-point pos))))
1119 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1121 (defun company-pseudo-tooltip-edit (lines selection)
1122 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1123 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1124 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1125 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1126 (lines (company-create-lines column selection height)))
1127 (overlay-put company-pseudo-tooltip-overlay 'company-before
1128 (company-replacement-string old-string lines column nl))))
1130 (defun company-pseudo-tooltip-hide ()
1131 (when company-pseudo-tooltip-overlay
1132 (delete-overlay company-pseudo-tooltip-overlay)
1133 (setq company-pseudo-tooltip-overlay nil)))
1135 (defun company-pseudo-tooltip-hide-temporarily ()
1136 (when (overlayp company-pseudo-tooltip-overlay)
1137 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1138 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1140 (defun company-pseudo-tooltip-unhide ()
1141 (when company-pseudo-tooltip-overlay
1142 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1143 (overlay-put company-pseudo-tooltip-overlay 'before-string
1144 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1145 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1147 (defun company-pseudo-tooltip-frontend (command)
1148 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1150 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1152 (unless (and (overlayp company-pseudo-tooltip-overlay)
1153 (equal (overlay-get company-pseudo-tooltip-overlay
1155 (company-pseudo-tooltip-height)))
1157 (company-pseudo-tooltip-show-at-point (- (point)
1158 (length company-prefix))))
1159 (company-pseudo-tooltip-unhide))
1160 ('hide (company-pseudo-tooltip-hide)
1161 (setq company-tooltip-offset 0))
1162 ('update (when (overlayp company-pseudo-tooltip-overlay)
1163 (company-pseudo-tooltip-edit company-candidates
1164 company-selection)))))
1166 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1167 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1168 (unless (and (eq command 'post-command)
1169 (not (cdr company-candidates)))
1170 (company-pseudo-tooltip-frontend command)))
1172 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1174 (defvar company-preview-overlay nil)
1175 (make-variable-buffer-local 'company-preview-overlay)
1177 (defun company-preview-show-at-point (pos)
1178 (company-preview-hide)
1180 (setq company-preview-overlay (make-overlay pos pos))
1182 (let ((completion(nth company-selection company-candidates)))
1183 (setq completion (propertize completion 'face 'company-preview))
1184 (add-text-properties 0 (length company-common)
1185 '(face company-preview-common) completion)
1187 ;; Add search string
1188 (and company-search-string
1189 (string-match (regexp-quote company-search-string) completion)
1190 (add-text-properties (match-beginning 0)
1192 '(face company-preview-search)
1195 (setq completion (company-strip-prefix completion))
1197 (and (equal pos (point))
1198 (not (equal completion ""))
1199 (add-text-properties 0 1 '(cursor t) completion))
1201 (overlay-put company-preview-overlay 'after-string completion)
1202 (overlay-put company-preview-overlay 'window (selected-window))))
1204 (defun company-preview-hide ()
1205 (when company-preview-overlay
1206 (delete-overlay company-preview-overlay)
1207 (setq company-preview-overlay nil)))
1209 (defun company-preview-frontend (command)
1210 "A `company-mode' front-end showing the selection as if it had been inserted."
1212 ('pre-command (company-preview-hide))
1213 ('post-command (company-preview-show-at-point (point)))
1214 ('hide (company-preview-hide))))
1216 (defun company-preview-if-just-one-frontend (command)
1217 "`company-preview-frontend', but only shown for single candidates."
1218 (unless (and (eq command 'post-command)
1219 (cdr company-candidates))
1220 (company-preview-frontend command)))
1222 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1224 (defvar company-echo-last-msg nil)
1225 (make-variable-buffer-local 'company-echo-last-msg)
1227 (defvar company-echo-timer nil)
1229 (defvar company-echo-delay .1)
1231 (defun company-echo-show (&optional getter)
1233 (setq company-echo-last-msg (funcall getter)))
1234 (let ((message-log-max nil))
1235 (if company-echo-last-msg
1236 (message "%s" company-echo-last-msg)
1239 (defsubst company-echo-show-soon (&optional getter)
1240 (when company-echo-timer
1241 (cancel-timer company-echo-timer))
1242 (setq company-echo-timer (run-with-timer company-echo-delay nil
1243 'company-echo-show getter)))
1245 (defun company-echo-format ()
1247 (let ((limit (window-width (minibuffer-window)))
1249 ;; Roll to selection.
1250 (candidates (nthcdr company-selection company-candidates))
1251 (i (if company-show-numbers company-selection 99999))
1255 (setq comp (company-reformat (pop candidates))
1256 len (+ len 1 (length comp)))
1260 (setq comp (propertize (format "%d: %s" i comp)
1261 'face 'company-echo))
1264 (add-text-properties 3 (+ 3 (length company-common))
1265 '(face company-echo-common) comp))
1266 (setq comp (propertize comp 'face 'company-echo))
1267 (add-text-properties 0 (length company-common)
1268 '(face company-echo-common) comp))
1270 (setq candidates nil)
1273 (mapconcat 'identity (nreverse msg) " ")))
1275 (defun company-echo-strip-common-format ()
1277 (let ((limit (window-width (minibuffer-window)))
1278 (len (+ (length company-prefix) 2))
1279 ;; Roll to selection.
1280 (candidates (nthcdr company-selection company-candidates))
1281 (i (if company-show-numbers company-selection 99999))
1285 (setq comp (company-strip-prefix (pop candidates))
1286 len (+ len 2 (length comp)))
1289 (setq comp (format "%s (%d)" comp i))
1293 (setq candidates nil)
1294 (push (propertize comp 'face 'company-echo) msg)))
1296 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1297 (mapconcat 'identity (nreverse msg) ", ")
1300 (defun company-echo-hide ()
1301 (when company-echo-timer
1302 (cancel-timer company-echo-timer))
1303 (unless (equal company-echo-last-msg "")
1304 (setq company-echo-last-msg "")
1305 (company-echo-show)))
1307 (defun company-echo-frontend (command)
1308 "A `company-mode' front-end showing the candidates in the echo area."
1310 ('pre-command (company-echo-show-soon))
1311 ('post-command (company-echo-show-soon 'company-echo-format))
1312 ('hide (company-echo-hide))))
1314 (defun company-echo-strip-common-frontend (command)
1315 "A `company-mode' front-end showing the candidates in the echo area."
1317 ('pre-command (company-echo-show-soon))
1318 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1319 ('hide (company-echo-hide))))
1321 (defun company-echo-metadata-frontend (command)
1322 "A `company-mode' front-end showing the documentation in the echo area."
1324 ('pre-command (company-echo-show-soon))
1325 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1326 ('hide (company-echo-hide))))
1329 ;;; company.el ends here