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 `company-elisp-functions-only-in-context' option.
60 ;; The mouse can now be used for selection.
63 ;; Added `company-show-location'.
64 ;; Added etags back-end.
65 ;; Added work-around for end-of-buffer bug.
66 ;; Added `company-filter-candidates'.
67 ;; More local Lisp variables are now included in the candidates.
70 ;; Fixed elisp documentation buffer always showing the same doc.
71 ;; Added `company-echo-strip-common-frontend'.
72 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
73 ;; Don't hide the echo message if it isn't shown.
80 (eval-when-compile (require 'cl))
82 (add-to-list 'debug-ignored-errors
83 "^Pseudo tooltip frontend cannot be used twice$")
84 (add-to-list 'debug-ignored-errors "^Preview frontend cannot be used twice$")
85 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
86 (add-to-list 'debug-ignored-errors "^No documentation available$")
87 (add-to-list 'debug-ignored-errors "^No location available$")
88 (add-to-list 'debug-ignored-errors "^Company not enabled$")
89 (add-to-list 'debug-ignored-errors "^Company not in search mode$")
90 (add-to-list 'debug-ignored-errors "^No candidate number ")
93 "Extensible inline text completion mechanism"
98 (defface company-tooltip
99 '((t :background "yellow"
100 :foreground "black"))
101 "*Face used for the tool tip."
104 (defface company-tooltip-selection
105 '((default :inherit company-tooltip)
106 (((class color) (min-colors 88)) (:background "orange1"))
107 (t (:background "green")))
108 "*Face used for the selection in the tool tip."
111 (defface company-tooltip-mouse
112 '((default :inherit highlight))
113 "*Face used for the tool tip item under the mouse."
116 (defface company-tooltip-common
117 '((t :inherit company-tooltip
119 "*Face used for the common completion in the tool tip."
122 (defface company-tooltip-common-selection
123 '((t :inherit company-tooltip-selection
125 "*Face used for the selected common completion in the tool tip."
128 (defcustom company-tooltip-limit 10
129 "*The maximum number of candidates in the tool tip"
133 (defface company-preview
134 '((t :background "blue4"
135 :foreground "wheat"))
136 "*Face used for the completion preview."
139 (defface company-preview-common
140 '((t :inherit company-preview
142 "*Face used for the common part of the completion preview."
145 (defface company-preview-search
146 '((t :inherit company-preview
147 :background "blue1"))
148 "*Face used for the search string in the completion preview."
151 (defface company-echo nil
152 "*Face used for completions in the echo area."
155 (defface company-echo-common
156 '((((background dark)) (:foreground "firebrick1"))
157 (((background light)) (:background "firebrick4")))
158 "*Face used for the common part of completions in the echo area."
161 (defun company-frontends-set (variable value)
163 (let ((remainder value))
164 (setcdr remainder (delq (car remainder) (cdr remainder))))
165 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
166 (memq 'company-pseudo-tooltip-frontend value)
167 (error "Pseudo tooltip frontend cannot be used twice"))
168 (and (memq 'company-preview-if-just-one-frontend value)
169 (memq 'company-preview-frontend value)
170 (error "Preview frontend cannot be used twice"))
171 (and (memq 'company-echo value)
172 (memq 'company-echo-metadata-frontend value)
173 (error "Echo area cannot be used twice"))
174 ;; preview must come last
175 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
177 (setq value (append (delq f value) (list f)))))
178 (set variable value))
180 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
181 company-preview-frontend
182 company-echo-metadata-frontend)
183 "*The list of active front-ends (visualizations).
184 Each front-end is a function that takes one argument. It is called with
185 one of the following arguments:
187 'show: When the visualization should start.
189 'hide: When the visualization should end.
191 'update: When the data has been updated.
193 'pre-command: Before every command that is executed while the
194 visualization is active.
196 'post-command: After every command that is executed while the
197 visualization is active.
199 The visualized data is stored in `company-prefix', `company-candidates',
200 `company-common', `company-selection', `company-point' and
201 `company-search-string'."
202 :set 'company-frontends-set
204 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
205 (const :tag "echo, strip common"
206 company-echo-strip-common-frontend)
207 (const :tag "show echo meta-data in echo"
208 company-echo-metadata-frontend)
209 (const :tag "pseudo tooltip"
210 company-pseudo-tooltip-frontend)
211 (const :tag "pseudo tooltip, multiple only"
212 company-pseudo-tooltip-unless-just-one-frontend)
213 (const :tag "preview" company-preview-frontend)
214 (const :tag "preview, unique only"
215 company-preview-if-just-one-frontend)
216 (function :tag "custom function" nil))))
218 (defcustom company-backends '(company-elisp company-nxml company-css
219 company-semantic company-gtags company-etags
220 company-oddmuse company-files company-dabbrev)
221 "*The list of active back-ends (completion engines).
222 Each back-end is a function that takes a variable number of arguments.
223 The first argument is the command requested from the back-end. It is one
226 'prefix: The back-end should return the text to be completed. It must be
227 text immediately before `point'. Returning nil passes control to the next
230 'candidates: The second argument is the prefix to be completed. The
231 return value should be a list of candidates that start with the prefix.
235 'sorted: The back-end may return t here to indicate that the candidates
236 are sorted and will not need to be sorted again.
238 'no-cache: Usually company doesn't ask for candidates again as completion
239 progresses, unless the back-end returns t for this command. The second
240 argument is the latest prefix.
242 'meta: The second argument is a completion candidate. The back-end should
243 return a (short) documentation string for it.
245 'doc-buffer: The second argument is a completion candidate. The back-end should
246 create a buffer (preferably with `company-doc-buffer'), fill it with
247 documentation and return it.
249 'location: The second argument is a completion candidate. The back-end can
250 return the cons of buffer and buffer location, or of file and line
251 number where the completion candidate was defined.
253 The back-end should return nil for all commands it does not support or
254 does not know about."
256 :type '(repeat (function :tag "function" nil)))
258 (defcustom company-minimum-prefix-length 3
259 "*The minimum prefix length for automatic completion."
261 :type '(integer :tag "prefix length"))
263 (defcustom company-idle-delay .7
264 "*The idle delay in seconds until automatic completions starts.
265 A value of nil means never complete automatically, t means complete
266 immediately when a prefix of `company-minimum-prefix-length' is reached."
268 :type '(choice (const :tag "never (nil)" nil)
269 (const :tag "immediate (t)" t)
270 (number :tag "seconds")))
272 (defcustom company-show-numbers nil
273 "*If enabled, show quick-access numbers for the first ten candidates."
275 :type '(choice (const :tag "off" nil)
276 (const :tag "on" t)))
278 (defvar company-end-of-buffer-workaround t
279 "*Work around a visualization bug when completing at the end of the buffer.
280 The work-around consists of adding a newline.")
282 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 (defvar company-mode-map (make-sparse-keymap)
285 "Keymap used by `company-mode'.")
287 (defvar company-active-map
288 (let ((keymap (make-sparse-keymap)))
289 (define-key keymap (kbd "M-n") 'company-select-next)
290 (define-key keymap (kbd "M-p") 'company-select-previous)
291 (define-key keymap (kbd "<down>") 'company-select-next)
292 (define-key keymap (kbd "<up>") 'company-select-previous)
293 (define-key keymap [down-mouse-1] 'ignore)
294 (define-key keymap [down-mouse-3] 'ignore)
295 (define-key keymap [mouse-1] 'company-complete-mouse)
296 (define-key keymap [mouse-3] 'company-select-mouse)
297 (define-key keymap [up-mouse-1] 'ignore)
298 (define-key keymap [up-mouse-3] 'ignore)
299 (define-key keymap "\C-m" 'company-complete-selection)
300 (define-key keymap "\t" 'company-complete-common)
301 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
302 (define-key keymap "\C-w" 'company-show-location)
303 (define-key keymap "\C-s" 'company-search-candidates)
304 (define-key keymap "\C-\M-s" 'company-filter-candidates)
306 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
307 `(lambda () (interactive) (company-complete-number ,i))))
310 "Keymap that is enabled during an active completion.")
313 (define-minor-mode company-mode
314 "\"complete anything\"; in in-buffer completion framework.
315 Completion starts automatically, depending on the values
316 `company-idle-delay' and `company-minimum-prefix-length'.
318 Completion can be controlled with the commands:
319 `company-complete-common', `company-complete-selection', `company-complete',
320 `company-select-next', `company-select-previous'. If these commands are
321 called before `company-idle-delay', completion will also start.
323 Completions can be searched with `company-search-candidates' or
324 `company-filter-candidates'. These can be used while completion is
327 The completion data is retrieved using `company-backends' and displayed using
330 regular keymap (`company-mode-map'):
333 keymap during active completions (`company-active-map'):
335 \\{company-active-map}"
336 nil " comp" company-mode-map
339 (add-hook 'pre-command-hook 'company-pre-command nil t)
340 (add-hook 'post-command-hook 'company-post-command nil t)
341 (dolist (backend company-backends)
342 (unless (fboundp backend)
343 (ignore-errors (require backend nil t)))
344 (unless (fboundp backend)
345 (message "Company back-end '%s' could not be initialized"
347 (remove-hook 'pre-command-hook 'company-pre-command t)
348 (remove-hook 'post-command-hook 'company-post-command t)
350 (kill-local-variable 'company-point)))
352 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
354 (defvar company-overriding-keymap-bound nil)
355 (make-variable-buffer-local 'company-overriding-keymap-bound)
357 (defvar company-old-keymap nil)
358 (make-variable-buffer-local 'company-old-keymap)
360 (defvar company-my-keymap nil)
361 (make-variable-buffer-local 'company-my-keymap)
363 (defsubst company-enable-overriding-keymap (keymap)
364 (setq company-my-keymap keymap)
365 (when company-overriding-keymap-bound
366 (company-uninstall-map)))
368 (defun company-install-map ()
369 (unless (or company-overriding-keymap-bound
370 (null company-my-keymap))
371 (setq company-old-keymap overriding-terminal-local-map
372 overriding-terminal-local-map company-my-keymap
373 company-overriding-keymap-bound t)))
375 (defun company-uninstall-map ()
376 (when (and company-overriding-keymap-bound
377 (eq overriding-terminal-local-map company-my-keymap))
378 (setq overriding-terminal-local-map company-old-keymap
379 company-overriding-keymap-bound nil)))
382 ;; Emacs calculates the active keymaps before reading the event. That means we
383 ;; cannot change the keymap from a timer. So we send a bogus command.
384 (defun company-ignore ()
387 (global-set-key '[31415926] 'company-ignore)
389 (defun company-input-noop ()
390 (push 31415926 unread-command-events))
392 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394 (defun company-grab (regexp &optional expression)
395 (when (looking-back regexp)
396 (or (match-string-no-properties (or expression 0)) "")))
398 (defun company-in-string-or-comment (&optional point)
399 (let ((pos (syntax-ppss)))
400 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
402 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
404 (defvar company-backend nil)
405 (make-variable-buffer-local 'company-backend)
407 (defvar company-prefix nil)
408 (make-variable-buffer-local 'company-prefix)
410 (defvar company-candidates nil)
411 (make-variable-buffer-local 'company-candidates)
413 (defvar company-candidates-length nil)
414 (make-variable-buffer-local 'company-candidates-length)
416 (defvar company-candidates-cache nil)
417 (make-variable-buffer-local 'company-candidates-cache)
419 (defvar company-candidates-predicate nil)
420 (make-variable-buffer-local 'company-candidates-predicate)
422 (defvar company-common nil)
423 (make-variable-buffer-local 'company-common)
425 (defvar company-selection 0)
426 (make-variable-buffer-local 'company-selection)
428 (defvar company-selection-changed nil)
429 (make-variable-buffer-local 'company-selection-changed)
431 (defvar company-point nil)
432 (make-variable-buffer-local 'company-point)
434 (defvar company-timer nil)
436 (defvar company-added-newline nil)
437 (make-variable-buffer-local 'company-added-newline)
439 (defsubst company-strip-prefix (str)
440 (substring str (length company-prefix)))
442 (defsubst company-reformat (candidate)
443 ;; company-ispell needs this, because the results are always lower-case
444 ;; It's mory efficient to fix it only when they are displayed.
445 (concat company-prefix (substring candidate (length company-prefix))))
447 (defsubst company-should-complete (prefix)
448 (and (eq company-idle-delay t)
449 (>= (length prefix) company-minimum-prefix-length)))
451 (defsubst company-call-frontends (command)
452 (dolist (frontend company-frontends)
454 (funcall frontend command)
455 (error (error "Company: Front-end %s error \"%s\" on command %s"
456 frontend (error-message-string err) command)))))
458 (defsubst company-set-selection (selection &optional force-update)
459 (setq selection (max 0 (min (1- company-candidates-length) selection)))
460 (when (or force-update (not (equal selection company-selection)))
461 (setq company-selection selection
462 company-selection-changed t)
463 (company-call-frontends 'update)))
465 (defun company-apply-predicate (candidates predicate)
467 (dolist (c candidates)
468 (when (funcall predicate c)
472 (defun company-update-candidates (candidates)
473 (setq company-candidates-length (length candidates))
474 (if (> company-selection 0)
475 ;; Try to restore the selection
476 (let ((selected (nth company-selection company-candidates)))
477 (setq company-selection 0
478 company-candidates candidates)
480 (while (and candidates (string< (pop candidates) selected))
481 (incf company-selection))
483 ;; Make sure selection isn't out of bounds.
484 (setq company-selection (min (1- company-candidates-length)
485 company-selection)))))
486 (setq company-selection 0
487 company-candidates candidates))
489 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
490 (setq company-common (try-completion company-prefix company-candidates)))
491 (when (eq company-common t)
492 (setq company-candidates nil)))
494 (defsubst company-calculate-candidates (prefix)
495 (setq company-prefix prefix)
496 (company-update-candidates
497 (or (cdr (assoc prefix company-candidates-cache))
498 (when company-candidates-cache
499 (let ((len (length prefix))
500 (completion-ignore-case (funcall company-backend 'ignore-case))
503 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
504 company-candidates-cache)))
505 (return (all-completions prefix prev))))))
506 (let ((candidates (funcall company-backend 'candidates prefix)))
507 (when company-candidates-predicate
509 (company-apply-predicate candidates
510 company-candidates-predicate)))
511 (unless (funcall company-backend 'sorted)
512 (setq candidates (sort candidates 'string<)))
514 (unless company-candidates-cache
515 (company-call-frontends 'show))
516 (unless (assoc prefix company-candidates-cache)
517 (push (cons prefix company-candidates) company-candidates-cache))
520 (defun company-idle-begin (buf win tick pos)
522 (eq buf (current-buffer))
523 (eq win (selected-window))
524 (eq tick (buffer-chars-modified-tick))
526 (not company-candidates)
527 (not (equal (point) company-point))
528 (let ((company-idle-delay t))
530 (when company-candidates
532 (company-post-command)))))
534 (defun company-manual-begin ()
536 (unless company-mode (error "Company not enabled"))
538 (not company-candidates)
539 (let ((company-idle-delay t)
540 (company-minimum-prefix-length 0))
542 ;; Return non-nil if active.
545 (defun company-continue ()
546 (when company-candidates
547 (when (funcall company-backend 'no-cache company-prefix)
548 ;; Don't complete existing candidates, fetch new ones.
549 (setq company-candidates-cache nil))
550 (let ((new-prefix (funcall company-backend 'prefix)))
551 (unless (and (= (- (point) (length new-prefix))
552 (- company-point (length company-prefix)))
553 (or (equal company-prefix new-prefix)
554 (company-calculate-candidates new-prefix)))
555 (setq company-candidates nil)))))
557 (defun company-begin ()
558 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
559 ;; Don't complete in these cases.
560 (setq company-candidates nil)
562 (unless company-candidates
564 (dolist (backend company-backends)
565 (when (and (fboundp backend)
566 (setq prefix (funcall backend 'prefix)))
567 (setq company-backend backend)
568 (when (company-should-complete prefix)
569 (company-calculate-candidates prefix))
571 (if company-candidates
573 (when (and company-end-of-buffer-workaround (eobp))
574 (save-excursion (insert "\n"))
575 (setq company-added-newline (buffer-chars-modified-tick)))
576 (setq company-point (point))
577 (company-enable-overriding-keymap company-active-map)
578 (company-call-frontends 'update))
581 (defun company-cancel ()
582 (and company-added-newline
583 (> (point-max) (point-min))
584 (let ((tick (buffer-chars-modified-tick)))
585 (delete-region (1- (point-max)) (point-max))
586 (equal tick company-added-newline))
587 ;; Only set unmodified when tick remained the same since insert.
588 (set-buffer-modified-p nil))
589 (setq company-added-newline nil
592 company-candidates nil
593 company-candidates-length nil
594 company-candidates-cache nil
595 company-candidates-predicate nil
598 company-selection-changed nil
601 (cancel-timer company-timer))
602 (company-search-mode 0)
603 (company-call-frontends 'hide)
604 (company-enable-overriding-keymap nil))
606 (defun company-abort ()
608 ;; Don't start again, unless started manually.
609 (setq company-point (point)))
611 (defsubst company-keep (command)
612 (and (symbolp command) (get command 'company-keep)))
614 (defun company-pre-command ()
615 (unless (company-keep this-command)
617 (when company-candidates
618 (company-call-frontends 'pre-command))
619 (error (message "Company: An error occurred in pre-command")
620 (message "%s" (error-message-string err))
623 (cancel-timer company-timer))
624 (company-uninstall-map))
626 (defun company-post-command ()
627 (unless (company-keep this-command)
630 (unless (equal (point) company-point)
632 (when company-candidates
633 (company-call-frontends 'post-command))
634 (when (numberp company-idle-delay)
636 (run-with-timer company-idle-delay nil 'company-idle-begin
637 (current-buffer) (selected-window)
638 (buffer-chars-modified-tick) (point)))))
639 (error (message "Company: An error occurred in post-command")
640 (message "%s" (error-message-string err))
642 (company-install-map))
644 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
646 (defvar company-search-string nil)
647 (make-variable-buffer-local 'company-search-string)
649 (defvar company-search-lighter " Search: \"\"")
650 (make-variable-buffer-local 'company-search-lighter)
652 (defvar company-search-old-map nil)
653 (make-variable-buffer-local 'company-search-old-map)
655 (defvar company-search-old-selection 0)
656 (make-variable-buffer-local 'company-search-old-selection)
658 (defun company-search (text lines)
659 (let ((quoted (regexp-quote text))
662 (when (string-match quoted line (length company-prefix))
666 (defun company-search-printing-char ()
668 (unless company-mode (error "Company not enabled"))
669 (unless company-search-mode (error "Company not in search mode"))
670 (setq company-search-string
671 (concat (or company-search-string "") (string last-command-event))
672 company-search-lighter (concat " Search: \"" company-search-string
674 (let ((pos (company-search company-search-string
675 (nthcdr company-selection company-candidates))))
678 (company-set-selection (+ company-selection pos) t))))
680 (defun company-search-repeat-forward ()
681 "Repeat the incremental search in completion candidates forward."
683 (unless company-mode (error "Company not enabled"))
684 (unless company-search-mode (error "Company not in search mode"))
685 (let ((pos (company-search company-search-string
686 (cdr (nthcdr company-selection
687 company-candidates)))))
690 (company-set-selection (+ company-selection pos 1) t))))
692 (defun company-search-repeat-backward ()
693 "Repeat the incremental search in completion candidates backwards."
695 (unless company-mode (error "Company not enabled"))
696 (unless company-search-mode (error "Company not in search mode"))
697 (let ((pos (company-search company-search-string
698 (nthcdr (- company-candidates-length
700 (reverse company-candidates)))))
703 (company-set-selection (- company-selection pos 1) t))))
705 (defun company-create-match-predicate ()
706 (setq company-candidates-predicate
708 ,(if company-candidates-predicate
709 `(and (string-match ,company-search-string candidate)
710 (funcall ,company-candidates-predicate
712 `(string-match ,company-search-string candidate))))
713 (company-update-candidates
714 (company-apply-predicate company-candidates company-candidates-predicate)))
716 (defun company-filter-printing-char ()
718 (unless company-mode (error "Company not enabled"))
719 (unless company-search-mode (error "Company not in search mode"))
720 (company-search-printing-char)
721 (company-create-match-predicate)
722 (company-call-frontends 'update))
724 (defun company-search-kill-others ()
725 "Limit the completion candidates to the ones matching the search string."
727 (unless company-mode (error "Company not enabled"))
728 (unless company-search-mode (error "Company not in search mode"))
729 (company-create-match-predicate)
730 (company-search-mode 0)
731 (company-call-frontends 'update))
733 (defun company-search-abort ()
734 "Abort searching the completion candidates."
736 (unless company-mode (error "Company not enabled"))
737 (unless company-search-mode (error "Company not in search mode"))
738 (company-set-selection company-search-old-selection t)
739 (company-search-mode 0))
741 (defun company-search-other-char ()
743 (unless company-mode (error "Company not enabled"))
744 (unless company-search-mode (error "Company not in search mode"))
745 (company-search-mode 0)
746 (when last-input-event
747 (clear-this-command-keys t)
748 (setq unread-command-events (list last-input-event))))
750 (defvar company-search-map
752 (keymap (make-keymap)))
753 (if (fboundp 'max-char)
754 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
755 'company-search-printing-char)
757 ;; obselete in Emacs 23
758 (let ((l (generic-character-list))
759 (table (nth 1 keymap)))
761 (set-char-table-default table (car l) 'company-search-printing-char)
763 (define-key keymap [t] 'company-search-other-char)
765 (define-key keymap (make-string 1 i) 'company-search-other-char)
768 (define-key keymap (vector i) 'company-search-printing-char)
770 (let ((meta-map (make-sparse-keymap)))
771 (define-key keymap (char-to-string meta-prefix-char) meta-map)
772 (define-key keymap [escape] meta-map))
773 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
774 (define-key keymap "\e\e\e" 'company-search-other-char)
775 (define-key keymap [escape escape escape] 'company-search-other-char)
777 (define-key keymap "\C-g" 'company-search-abort)
778 (define-key keymap "\C-s" 'company-search-repeat-forward)
779 (define-key keymap "\C-r" 'company-search-repeat-backward)
780 (define-key keymap "\C-o" 'company-search-kill-others)
782 "Keymap used for incrementally searching the completion candidates.")
784 (define-minor-mode company-search-mode
785 "Search mode for completion candidates.
786 Don't start this directly, use `company-search-candidates' or
787 `company-filter-candidates'."
788 nil company-search-lighter nil
789 (if company-search-mode
790 (if (company-manual-begin)
792 (setq company-search-old-selection company-selection)
793 (company-call-frontends 'update))
794 (setq company-search-mode nil))
795 (kill-local-variable 'company-search-string)
796 (kill-local-variable 'company-search-lighter)
797 (kill-local-variable 'company-search-old-selection)
798 (company-enable-overriding-keymap company-active-map)))
800 (defun company-search-candidates ()
801 "Start searching the completion candidates incrementally.
803 \\<company-search-map>Search can be controlled with the commands:
804 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
805 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
806 - `company-search-abort' (\\[company-search-abort])
808 Regular characters are appended to the search string.
810 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
811 the search string to limit the completion candidates."
813 (company-search-mode 1)
814 (company-enable-overriding-keymap company-search-map))
816 (defvar company-filter-map
817 (let ((keymap (make-keymap)))
818 (define-key keymap [remap company-search-printing-char]
819 'company-filter-printing-char)
820 (set-keymap-parent keymap company-search-map)
822 "Keymap used for incrementally searching the completion candidates.")
824 (defun company-filter-candidates ()
825 "Start filtering the completion candidates incrementally.
826 This works the same way as `company-search-candidates' immediately
827 followed by `company-search-kill-others' after each input."
829 (company-search-mode 1)
830 (company-enable-overriding-keymap company-filter-map))
832 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
834 (defun company-select-next ()
835 "Select the next candidate in the list."
837 (when (company-manual-begin)
838 (company-set-selection (1+ company-selection))))
840 (defun company-select-previous ()
841 "Select the previous candidate in the list."
843 (when (company-manual-begin)
844 (company-set-selection (1- company-selection))))
846 (defun company-select-mouse (event)
847 "Select the candidate picked by the mouse."
849 (when (nth 4 (event-start event))
850 (company-set-selection (- (cdr (posn-col-row (event-start event)))
851 (cdr (posn-col-row (posn-at-point)))
855 (defun company-complete-mouse (event)
856 "Complete the candidate picked by the mouse."
858 (when (company-select-mouse event)
859 (company-complete-selection)))
861 (defun company-complete-selection ()
862 "Complete the selected candidate."
864 (when (company-manual-begin)
865 (insert (company-strip-prefix (nth company-selection company-candidates)))
868 (defun company-complete-common ()
869 "Complete the common part of all candidates."
871 (when (company-manual-begin)
872 (insert (company-strip-prefix company-common))))
874 (defun company-complete ()
875 "Complete the common part of all candidates or the current selection.
876 The first time this is called, the common part is completed, the second time, or
877 when the selection has been changed, the selected candidate is completed."
879 (when (company-manual-begin)
880 (if (or company-selection-changed
881 (eq last-command 'company-complete-common))
882 (call-interactively 'company-complete-selection)
883 (call-interactively 'company-complete-common)
884 (setq this-command 'company-complete-common))))
886 (defun company-complete-number (n)
887 "Complete the Nth candidate."
888 (when (company-manual-begin)
889 (and (< n 1) (> n company-candidates-length)
890 (error "No candidate number %d" n))
892 (insert (company-strip-prefix (nth n company-candidates)))
895 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
897 (defconst company-space-strings-limit 100)
899 (defconst company-space-strings
901 (dotimes (i company-space-strings-limit)
902 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
903 (apply 'vector lst)))
905 (defsubst company-space-string (len)
906 (if (< len company-space-strings-limit)
907 (aref company-space-strings len)
908 (make-string len ?\ )))
910 (defsubst company-safe-substring (str from &optional to)
911 (let ((len (length str)))
914 (if (and to (> to len))
915 (concat (substring str from)
916 (company-space-string (- to len)))
917 (substring str from to)))))
919 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
921 (defvar company-last-metadata nil)
922 (make-variable-buffer-local 'company-last-metadata)
924 (defun company-fetch-metadata ()
925 (let ((selected (nth company-selection company-candidates)))
926 (unless (equal selected (car company-last-metadata))
927 (setq company-last-metadata
928 (cons selected (funcall company-backend 'meta selected))))
929 (cdr company-last-metadata)))
931 (defun company-doc-buffer (&optional string)
932 (with-current-buffer (get-buffer-create "*Company meta-data*")
936 (defmacro company-electric (&rest body)
937 (declare (indent 0) (debug t))
939 (when (company-manual-begin)
940 (save-window-excursion
941 (let ((height (window-height))
942 (row (cdr (posn-col-row (posn-at-point)))))
944 (and (< (window-height) height)
945 (< (- (window-height) row 2) company-tooltip-limit)
946 (recenter (- (window-height) row 2)))
947 (while (eq 'scroll-other-window
948 (key-binding (vector (list (read-event)))))
949 (call-interactively 'scroll-other-window))
950 (when last-input-event
951 (clear-this-command-keys t)
952 (setq unread-command-events (list last-input-event))))))
953 (error "Company not enabled")))
955 (defun company-show-doc-buffer ()
956 "Temporarily show a buffer with the complete documentation for the selection."
959 (let ((selected (nth company-selection company-candidates)))
960 (display-buffer (or (funcall company-backend 'doc-buffer selected)
961 (error "No documentation available")) t))))
962 (put 'company-show-doc-buffer 'company-keep t)
964 (defun company-show-location ()
965 "Temporarily display a buffer showing the selected candidate in context."
968 (let* ((selected (nth company-selection company-candidates))
969 (location (funcall company-backend 'location selected))
970 (pos (or (cdr location) (error "No location available")))
971 (buffer (or (and (bufferp (car location)) (car location))
972 (find-file-noselect (car location) t))))
973 (with-selected-window (display-buffer buffer t)
974 (if (bufferp (car location))
977 (set-window-start nil (point))))))
978 (put 'company-show-location 'company-keep t)
980 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
982 (defvar company-pseudo-tooltip-overlay nil)
983 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
985 (defvar company-tooltip-offset 0)
986 (make-variable-buffer-local 'company-tooltip-offset)
988 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
991 (setq company-tooltip-offset
992 (max (min selection company-tooltip-offset)
993 (- selection -1 limit)))
995 (when (<= company-tooltip-offset 1)
997 (setq company-tooltip-offset 0))
999 (when (>= company-tooltip-offset (- num-lines limit 1))
1001 (when (= selection (1- num-lines))
1002 (decf company-tooltip-offset)
1003 (when (<= company-tooltip-offset 1)
1004 (setq company-tooltip-offset 0)
1011 (defsubst company-round-tab (arg)
1012 (* (/ (+ arg tab-width) tab-width) tab-width))
1014 (defun company-untabify (str)
1015 (let* ((pieces (split-string str "\t"))
1018 (setcar copy (company-safe-substring
1019 (car copy) 0 (company-round-tab (string-width (car copy)))))
1021 (apply 'concat pieces)))
1023 (defun company-fill-propertize (line width selected)
1024 (setq line (company-safe-substring line 0 width))
1025 (add-text-properties 0 width '(face company-tooltip
1026 mouse-face company-tooltip-mouse)
1028 (add-text-properties 0 (length company-common)
1029 '(face company-tooltip-common
1030 mouse-face company-tooltip-mouse)
1033 (if (and company-search-string
1034 (string-match (regexp-quote company-search-string) line
1035 (length company-prefix)))
1037 (add-text-properties (match-beginning 0) (match-end 0)
1038 '(face company-tooltip-selection)
1040 (when (< (match-beginning 0) (length company-common))
1041 (add-text-properties (match-beginning 0) (length company-common)
1042 '(face company-tooltip-common-selection)
1044 (add-text-properties 0 width '(face company-tooltip-selection
1045 mouse-face company-tooltip-selection)
1047 (add-text-properties 0 (length company-common)
1048 '(face company-tooltip-common-selection
1049 mouse-face company-tooltip-selection)
1055 (defun company-buffer-lines (beg end)
1057 (let ((row (cdr (posn-col-row (posn-at-point))))
1059 (while (and (equal (move-to-window-line (incf row)) row)
1061 (push (buffer-substring beg (min end (1- (point)))) lines)
1063 (unless (eq beg end)
1064 (push (buffer-substring beg end) lines))
1067 (defsubst company-modify-line (old new offset)
1068 (concat (company-safe-substring old 0 offset)
1070 (company-safe-substring old (+ offset (length new)))))
1072 (defun company-replacement-string (old lines column nl)
1074 ;; Inject into old lines.
1076 (push (company-modify-line (pop old) (pop lines) column) new))
1077 ;; Append whole new lines.
1079 (push (concat (company-space-string column) (pop lines)) new))
1080 (concat (when nl "\n")
1081 (mapconcat 'identity (nreverse new) "\n")
1084 (defun company-create-lines (column selection limit)
1086 (let ((len company-candidates-length)
1095 ;; Scroll to offset.
1096 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1098 (when (> company-tooltip-offset 0)
1099 (setq previous (format "...(%d)" company-tooltip-offset)))
1101 (setq remainder (- len limit company-tooltip-offset)
1102 remainder (when (> remainder 0)
1103 (setq remainder (format "...(%d)" remainder))))
1105 (decf selection company-tooltip-offset)
1106 (setq width (min (length previous) (length remainder))
1107 lines (nthcdr company-tooltip-offset company-candidates)
1112 (setq width (max (length (pop lines-copy)) width)))
1113 (setq width (min width (- (window-width) column)))
1115 (setq lines-copy lines)
1117 ;; number can make tooltip too long
1118 (and company-show-numbers
1119 (< (setq numbered company-tooltip-offset) 10)
1123 (push (propertize (company-safe-substring previous 0 width)
1124 'face 'company-tooltip)
1128 (push (company-fill-propertize
1129 (if (>= numbered 10)
1130 (company-reformat (pop lines))
1133 (company-safe-substring (company-reformat (pop lines))
1136 width (equal i selection))
1140 (push (propertize (company-safe-substring remainder 0 width)
1141 'face 'company-tooltip)
1144 (setq lines (nreverse new))))
1148 (defsubst company-pseudo-tooltip-height ()
1149 "Calculate the appropriate tooltip height."
1150 (max 3 (min company-tooltip-limit
1151 (- (window-height) 2
1152 (count-lines (window-start) (point-at-bol))))))
1154 (defun company-pseudo-tooltip-show (row column selection)
1155 (company-pseudo-tooltip-hide)
1160 (let* ((height (company-pseudo-tooltip-height))
1161 (lines (company-create-lines column selection height))
1162 (nl (< (move-to-window-line row) row))
1164 (end (save-excursion
1165 (move-to-window-line (+ row height))
1168 (mapcar 'company-untabify (company-buffer-lines beg end)))
1171 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1173 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1174 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1175 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1176 (overlay-put company-pseudo-tooltip-overlay 'company-before
1177 (company-replacement-string old-string lines column nl))
1178 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1180 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1182 (defun company-pseudo-tooltip-show-at-point (pos)
1183 (let ((col-row (posn-col-row (posn-at-point pos))))
1184 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1186 (defun company-pseudo-tooltip-edit (lines selection)
1187 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1188 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1189 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1190 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1191 (lines (company-create-lines column selection height)))
1192 (overlay-put company-pseudo-tooltip-overlay 'company-before
1193 (company-replacement-string old-string lines column nl))))
1195 (defun company-pseudo-tooltip-hide ()
1196 (when company-pseudo-tooltip-overlay
1197 (delete-overlay company-pseudo-tooltip-overlay)
1198 (setq company-pseudo-tooltip-overlay nil)))
1200 (defun company-pseudo-tooltip-hide-temporarily ()
1201 (when (overlayp company-pseudo-tooltip-overlay)
1202 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1203 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1205 (defun company-pseudo-tooltip-unhide ()
1206 (when company-pseudo-tooltip-overlay
1207 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1208 (overlay-put company-pseudo-tooltip-overlay 'before-string
1209 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1210 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1212 (defun company-pseudo-tooltip-frontend (command)
1213 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1215 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1217 (unless (and (overlayp company-pseudo-tooltip-overlay)
1218 (equal (overlay-get company-pseudo-tooltip-overlay
1220 (company-pseudo-tooltip-height)))
1222 (company-pseudo-tooltip-show-at-point (- (point)
1223 (length company-prefix))))
1224 (company-pseudo-tooltip-unhide))
1225 ('hide (company-pseudo-tooltip-hide)
1226 (setq company-tooltip-offset 0))
1227 ('update (when (overlayp company-pseudo-tooltip-overlay)
1228 (company-pseudo-tooltip-edit company-candidates
1229 company-selection)))))
1231 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1232 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1233 (unless (and (eq command 'post-command)
1234 (not (cdr company-candidates)))
1235 (company-pseudo-tooltip-frontend command)))
1237 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1239 (defvar company-preview-overlay nil)
1240 (make-variable-buffer-local 'company-preview-overlay)
1242 (defun company-preview-show-at-point (pos)
1243 (company-preview-hide)
1245 (setq company-preview-overlay (make-overlay pos pos))
1247 (let ((completion(nth company-selection company-candidates)))
1248 (setq completion (propertize completion 'face 'company-preview))
1249 (add-text-properties 0 (length company-common)
1250 '(face company-preview-common) completion)
1252 ;; Add search string
1253 (and company-search-string
1254 (string-match (regexp-quote company-search-string) completion)
1255 (add-text-properties (match-beginning 0)
1257 '(face company-preview-search)
1260 (setq completion (company-strip-prefix completion))
1262 (and (equal pos (point))
1263 (not (equal completion ""))
1264 (add-text-properties 0 1 '(cursor t) completion))
1266 (overlay-put company-preview-overlay 'after-string completion)
1267 (overlay-put company-preview-overlay 'window (selected-window))))
1269 (defun company-preview-hide ()
1270 (when company-preview-overlay
1271 (delete-overlay company-preview-overlay)
1272 (setq company-preview-overlay nil)))
1274 (defun company-preview-frontend (command)
1275 "A `company-mode' front-end showing the selection as if it had been inserted."
1277 ('pre-command (company-preview-hide))
1278 ('post-command (company-preview-show-at-point (point)))
1279 ('hide (company-preview-hide))))
1281 (defun company-preview-if-just-one-frontend (command)
1282 "`company-preview-frontend', but only shown for single candidates."
1283 (unless (and (eq command 'post-command)
1284 (cdr company-candidates))
1285 (company-preview-frontend command)))
1287 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1289 (defvar company-echo-last-msg nil)
1290 (make-variable-buffer-local 'company-echo-last-msg)
1292 (defvar company-echo-timer nil)
1294 (defvar company-echo-delay .1)
1296 (defun company-echo-show (&optional getter)
1298 (setq company-echo-last-msg (funcall getter)))
1299 (let ((message-log-max nil))
1300 (if company-echo-last-msg
1301 (message "%s" company-echo-last-msg)
1304 (defsubst company-echo-show-soon (&optional getter)
1305 (when company-echo-timer
1306 (cancel-timer company-echo-timer))
1307 (setq company-echo-timer (run-with-timer company-echo-delay nil
1308 'company-echo-show getter)))
1310 (defun company-echo-format ()
1312 (let ((limit (window-width (minibuffer-window)))
1314 ;; Roll to selection.
1315 (candidates (nthcdr company-selection company-candidates))
1316 (i (if company-show-numbers company-selection 99999))
1320 (setq comp (company-reformat (pop candidates))
1321 len (+ len 1 (length comp)))
1325 (setq comp (propertize (format "%d: %s" i comp)
1326 'face 'company-echo))
1329 (add-text-properties 3 (+ 3 (length company-common))
1330 '(face company-echo-common) comp))
1331 (setq comp (propertize comp 'face 'company-echo))
1332 (add-text-properties 0 (length company-common)
1333 '(face company-echo-common) comp))
1335 (setq candidates nil)
1338 (mapconcat 'identity (nreverse msg) " ")))
1340 (defun company-echo-strip-common-format ()
1342 (let ((limit (window-width (minibuffer-window)))
1343 (len (+ (length company-prefix) 2))
1344 ;; Roll to selection.
1345 (candidates (nthcdr company-selection company-candidates))
1346 (i (if company-show-numbers company-selection 99999))
1350 (setq comp (company-strip-prefix (pop candidates))
1351 len (+ len 2 (length comp)))
1354 (setq comp (format "%s (%d)" comp i))
1358 (setq candidates nil)
1359 (push (propertize comp 'face 'company-echo) msg)))
1361 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1362 (mapconcat 'identity (nreverse msg) ", ")
1365 (defun company-echo-hide ()
1366 (when company-echo-timer
1367 (cancel-timer company-echo-timer))
1368 (unless (equal company-echo-last-msg "")
1369 (setq company-echo-last-msg "")
1370 (company-echo-show)))
1372 (defun company-echo-frontend (command)
1373 "A `company-mode' front-end showing the candidates in the echo area."
1375 ('pre-command (company-echo-show-soon))
1376 ('post-command (company-echo-show-soon 'company-echo-format))
1377 ('hide (company-echo-hide))))
1379 (defun company-echo-strip-common-frontend (command)
1380 "A `company-mode' front-end showing the candidates in the echo area."
1382 ('pre-command (company-echo-show-soon))
1383 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1384 ('hide (company-echo-hide))))
1386 (defun company-echo-metadata-frontend (command)
1387 "A `company-mode' front-end showing the documentation in the echo area."
1389 ('pre-command (company-echo-show-soon))
1390 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1391 ('hide (company-echo-hide))))
1394 ;;; company.el ends here