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 ;; If you want to start a specific back-end, call it interactively or use
43 ;; `company-begin-backend'. For example:
44 ;; M-x company-abbrev will prompt for and insert an abbrev.
46 ;; To write your own back-end, look at the documentation for `company-backends'.
47 ;; Here is a simple example completing "foo":
49 ;; (defun company-my-backend (command &optional arg &rest ignored)
51 ;; ('prefix (when (looking-back "foo\\>")
53 ;; ('candidates (list "foobar" "foobaz" "foobarbaz"))
54 ;; ('meta (format "This value is named %s" arg))))
56 ;; Sometimes it is a good idea to mix two back-ends together, for example to
57 ;; enrich gtags with dabbrev text (to emulate local variables):
59 ;; (defun gtags-gtags-dabbrev-backend (command &optional arg &rest ignored)
61 ;; (prefix (company-gtags 'prefix))
62 ;; (candidates (append (company-gtags 'candidates arg)
63 ;; (company-dabbrev 'candidates arg)))))
66 ;; When point is at the very end of the buffer, the pseudo-tooltip appears very
67 ;; wrong, unless company is allowed to temporarily insert a fake newline.
68 ;; This behavior is enabled by `company-end-of-buffer-workaround'.
72 ;; Fixed issues with tabbar-mode and line-spacing.
73 ;; Performance enhancements.
76 ;; Added `company-begin-commands' option.
77 ;; Added abbrev, tempo and Xcode back-ends.
78 ;; Back-ends are now interactive. You can start them with M-x backend-name.
79 ;; Added `company-begin-with' for starting company from elisp-code.
81 ;; Added `company-require-match' and `company-auto-complete' options.
84 ;; Improved Emacs Lisp back-end behavior for local variables.
85 ;; Added `company-elisp-detect-function-context' option.
86 ;; The mouse can now be used for selection.
89 ;; Added `company-show-location'.
90 ;; Added etags back-end.
91 ;; Added work-around for end-of-buffer bug.
92 ;; Added `company-filter-candidates'.
93 ;; More local Lisp variables are now included in the candidates.
96 ;; Fixed elisp documentation buffer always showing the same doc.
97 ;; Added `company-echo-strip-common-frontend'.
98 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
99 ;; Don't hide the echo message if it isn't shown.
106 (eval-when-compile (require 'cl))
108 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
109 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
110 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
111 (add-to-list 'debug-ignored-errors "^Company not ")
112 (add-to-list 'debug-ignored-errors "^No candidate number ")
113 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
115 (defgroup company nil
116 "Extensible inline text completion mechanism"
121 (defface company-tooltip
122 '((t :background "yellow"
123 :foreground "black"))
124 "*Face used for the tool tip."
127 (defface company-tooltip-selection
128 '((default :inherit company-tooltip)
129 (((class color) (min-colors 88)) (:background "orange1"))
130 (t (:background "green")))
131 "*Face used for the selection in the tool tip."
134 (defface company-tooltip-mouse
135 '((default :inherit highlight))
136 "*Face used for the tool tip item under the mouse."
139 (defface company-tooltip-common
140 '((t :inherit company-tooltip
142 "*Face used for the common completion in the tool tip."
145 (defface company-tooltip-common-selection
146 '((t :inherit company-tooltip-selection
148 "*Face used for the selected common completion in the tool tip."
151 (defcustom company-tooltip-limit 10
152 "*The maximum number of candidates in the tool tip"
156 (defface company-preview
157 '((t :background "blue4"
158 :foreground "wheat"))
159 "*Face used for the completion preview."
162 (defface company-preview-common
163 '((t :inherit company-preview
165 "*Face used for the common part of the completion preview."
168 (defface company-preview-search
169 '((t :inherit company-preview
170 :background "blue1"))
171 "*Face used for the search string in the completion preview."
174 (defface company-echo nil
175 "*Face used for completions in the echo area."
178 (defface company-echo-common
179 '((((background dark)) (:foreground "firebrick1"))
180 (((background light)) (:background "firebrick4")))
181 "*Face used for the common part of completions in the echo area."
184 (defun company-frontends-set (variable value)
186 (let ((remainder value))
187 (setcdr remainder (delq (car remainder) (cdr remainder))))
188 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
189 (memq 'company-pseudo-tooltip-frontend value)
190 (error "Pseudo tooltip frontend cannot be used twice"))
191 (and (memq 'company-preview-if-just-one-frontend value)
192 (memq 'company-preview-frontend value)
193 (error "Preview frontend cannot be used twice"))
194 (and (memq 'company-echo value)
195 (memq 'company-echo-metadata-frontend value)
196 (error "Echo area cannot be used twice"))
197 ;; preview must come last
198 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
200 (setq value (append (delq f value) (list f)))))
201 (set variable value))
203 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
204 company-preview-frontend
205 company-echo-metadata-frontend)
206 "*The list of active front-ends (visualizations).
207 Each front-end is a function that takes one argument. It is called with
208 one of the following arguments:
210 'show: When the visualization should start.
212 'hide: When the visualization should end.
214 'update: When the data has been updated.
216 'pre-command: Before every command that is executed while the
217 visualization is active.
219 'post-command: After every command that is executed while the
220 visualization is active.
222 The visualized data is stored in `company-prefix', `company-candidates',
223 `company-common', `company-selection', `company-point' and
224 `company-search-string'."
225 :set 'company-frontends-set
227 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
228 (const :tag "echo, strip common"
229 company-echo-strip-common-frontend)
230 (const :tag "show echo meta-data in echo"
231 company-echo-metadata-frontend)
232 (const :tag "pseudo tooltip"
233 company-pseudo-tooltip-frontend)
234 (const :tag "pseudo tooltip, multiple only"
235 company-pseudo-tooltip-unless-just-one-frontend)
236 (const :tag "preview" company-preview-frontend)
237 (const :tag "preview, unique only"
238 company-preview-if-just-one-frontend)
239 (function :tag "custom function" nil))))
241 (defcustom company-backends '(company-elisp company-nxml company-css
242 company-semantic company-xcode company-gtags
243 company-etags company-oddmuse company-files
245 "*The list of active back-ends (completion engines).
246 Each back-end is a function that takes a variable number of arguments.
247 The first argument is the command requested from the back-end. It is one
250 'prefix: The back-end should return the text to be completed. It must be
251 text immediately before `point'. Returning nil passes control to the next
254 'candidates: The second argument is the prefix to be completed. The
255 return value should be a list of candidates that start with the prefix.
259 'sorted: The back-end may return t here to indicate that the candidates
260 are sorted and will not need to be sorted again.
262 'no-cache: Usually company doesn't ask for candidates again as completion
263 progresses, unless the back-end returns t for this command. The second
264 argument is the latest prefix.
266 'meta: The second argument is a completion candidate. The back-end should
267 return a (short) documentation string for it.
269 'doc-buffer: The second argument is a completion candidate. The back-end should
270 create a buffer (preferably with `company-doc-buffer'), fill it with
271 documentation and return it.
273 'location: The second argument is a completion candidate. The back-end can
274 return the cons of buffer and buffer location, or of file and line
275 number where the completion candidate was defined.
277 'require-match: If this value is t, the user is not allowed to enter anything
278 not offering as a candidate. Use with care! The default value nil gives the
279 user that choice with `company-require-match'. Return value 'never overrides
280 that option the other way around.
282 The back-end should return nil for all commands it does not support or
283 does not know about. It should also be callable interactively and use
284 `company-begin-backend' to start itself in that case."
286 :type '(repeat (function :tag "function" nil)))
288 (defvar start-count 0)
290 (defcustom company-completion-started-hook nil
291 "*Hook run when company starts completing.
292 The hook is called with one argument that is non-nil if the completion was
297 (defcustom company-completion-cancelled-hook nil
298 "*Hook run when company cancels completing.
299 The hook is called with one argument that is non-nil if the completion was
304 (defcustom company-completion-finished-hook nil
305 "*Hook run when company successfully completes.
306 The hook is called with the selected candidate as an argument."
310 (defcustom company-minimum-prefix-length 3
311 "*The minimum prefix length for automatic completion."
313 :type '(integer :tag "prefix length"))
315 (defcustom company-require-match 'company-explicit-action-p
316 "*If enabled, disallow non-matching input.
317 This can be a function do determine if a match is required.
319 This can be overridden by the back-end, if it returns t or 'never to
320 'require-match. `company-auto-complete' also takes precedence over this."
322 :type '(choice (const :tag "Off" nil)
323 (function :tag "Predicate function")
324 (const :tag "On, if user interaction took place"
325 'company-explicit-action-p)
326 (const :tag "On" t)))
328 (defcustom company-auto-complete 'company-explicit-action-p
329 "Determines when to auto-complete.
330 If this is enabled, all characters from `company-auto-complete-chars' complete
331 the selected completion. This can also be a function."
333 :type '(choice (const :tag "Off" nil)
334 (function :tag "Predicate function")
335 (const :tag "On, if user interaction took place"
336 'company-explicit-action-p)
337 (const :tag "On" t)))
339 (defcustom company-auto-complete-chars '(?\ ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
340 "Determines which characters trigger an automatic completion.
341 See `company-auto-complete'. If this is a string, each string character causes
342 completion. If it is a list of syntax description characters (see
343 `modify-syntax-entry'), all characters with that syntax auto-complete.
345 This can also be a function, which is called with the new input and should
346 return non-nil if company should auto-complete.
348 A character that is part of a valid candidate never starts auto-completion."
350 :type '(choice (string :tag "Characters")
352 (const :tag "Whitespace" ?\ )
353 (const :tag "Symbol" ?_)
354 (const :tag "Opening parentheses" ?\()
355 (const :tag "Closing parentheses" ?\))
356 (const :tag "Word constituent" ?w)
357 (const :tag "Punctuation." ?.)
358 (const :tag "String quote." ?\")
359 (const :tag "Paired delimiter." ?$)
360 (const :tag "Expression quote or prefix operator." ?\')
361 (const :tag "Comment starter." ?<)
362 (const :tag "Comment ender." ?>)
363 (const :tag "Character-quote." ?/)
364 (const :tag "Generic string fence." ?|)
365 (const :tag "Generic comment fence." ?!))
366 (function :tag "Predicate function")))
368 (defcustom company-idle-delay .7
369 "*The idle delay in seconds until automatic completions starts.
370 A value of nil means never complete automatically, t means complete
371 immediately when a prefix of `company-minimum-prefix-length' is reached."
373 :type '(choice (const :tag "never (nil)" nil)
374 (const :tag "immediate (t)" t)
375 (number :tag "seconds")))
377 (defcustom company-begin-commands t
378 "*A list of commands following which company will start completing.
379 If this is t, it will complete after any command. See `company-idle-delay'.
381 Alternatively any command with a non-nil 'company-begin property is treated as
382 if it was on this list."
384 :type '(choice (const :tag "Any command" t)
385 (const :tag "Self insert command" '(self-insert-command))
386 (repeat :tag "Commands" function)))
388 (defcustom company-show-numbers nil
389 "*If enabled, show quick-access numbers for the first ten candidates."
391 :type '(choice (const :tag "off" nil)
392 (const :tag "on" t)))
394 (defvar company-end-of-buffer-workaround t
395 "*Work around a visualization bug when completing at the end of the buffer.
396 The work-around consists of adding a newline.")
398 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400 (defvar company-mode-map (make-sparse-keymap)
401 "Keymap used by `company-mode'.")
403 (defvar company-active-map
404 (let ((keymap (make-sparse-keymap)))
405 (define-key keymap "\e\e\e" 'company-abort)
406 (define-key keymap "\C-g" 'company-abort)
407 (define-key keymap (kbd "M-n") 'company-select-next)
408 (define-key keymap (kbd "M-p") 'company-select-previous)
409 (define-key keymap (kbd "<down>") 'company-select-next)
410 (define-key keymap (kbd "<up>") 'company-select-previous)
411 (define-key keymap [down-mouse-1] 'ignore)
412 (define-key keymap [down-mouse-3] 'ignore)
413 (define-key keymap [mouse-1] 'company-complete-mouse)
414 (define-key keymap [mouse-3] 'company-select-mouse)
415 (define-key keymap [up-mouse-1] 'ignore)
416 (define-key keymap [up-mouse-3] 'ignore)
417 (define-key keymap "\C-m" 'company-complete-selection)
418 (define-key keymap "\t" 'company-complete-common)
419 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
420 (define-key keymap "\C-w" 'company-show-location)
421 (define-key keymap "\C-s" 'company-search-candidates)
422 (define-key keymap "\C-\M-s" 'company-filter-candidates)
424 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
425 `(lambda () (interactive) (company-complete-number ,i))))
428 "Keymap that is enabled during an active completion.")
431 (define-minor-mode company-mode
432 "\"complete anything\"; in in-buffer completion framework.
433 Completion starts automatically, depending on the values
434 `company-idle-delay' and `company-minimum-prefix-length'.
436 Completion can be controlled with the commands:
437 `company-complete-common', `company-complete-selection', `company-complete',
438 `company-select-next', `company-select-previous'. If these commands are
439 called before `company-idle-delay', completion will also start.
441 Completions can be searched with `company-search-candidates' or
442 `company-filter-candidates'. These can be used while completion is
445 The completion data is retrieved using `company-backends' and displayed using
446 `company-frontends'. If you want to start a specific back-end, call it
447 interactively or use `company-begin-backend'.
449 regular keymap (`company-mode-map'):
452 keymap during active completions (`company-active-map'):
454 \\{company-active-map}"
455 nil " comp" company-mode-map
458 (add-hook 'pre-command-hook 'company-pre-command nil t)
459 (add-hook 'post-command-hook 'company-post-command nil t)
460 (dolist (backend company-backends)
461 (when (symbolp backend)
462 (unless (fboundp backend)
463 (ignore-errors (require backend nil t)))
464 (unless (fboundp backend)
465 (message "Company back-end '%s' could not be initialized"
467 (remove-hook 'pre-command-hook 'company-pre-command t)
468 (remove-hook 'post-command-hook 'company-post-command t)
470 (kill-local-variable 'company-point)))
472 (defsubst company-assert-enabled ()
474 (company-uninstall-map)
475 (error "Company not enabled")))
477 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
479 (defvar company-overriding-keymap-bound nil)
480 (make-variable-buffer-local 'company-overriding-keymap-bound)
482 (defvar company-old-keymap nil)
483 (make-variable-buffer-local 'company-old-keymap)
485 (defvar company-my-keymap nil)
486 (make-variable-buffer-local 'company-my-keymap)
488 (defsubst company-enable-overriding-keymap (keymap)
489 (setq company-my-keymap keymap)
490 (when company-overriding-keymap-bound
491 (company-uninstall-map)))
493 (defun company-install-map ()
494 (unless (or company-overriding-keymap-bound
495 (null company-my-keymap))
496 (setq company-old-keymap overriding-terminal-local-map
497 overriding-terminal-local-map company-my-keymap
498 company-overriding-keymap-bound t)))
500 (defun company-uninstall-map ()
501 (when (eq overriding-terminal-local-map company-my-keymap)
502 (setq overriding-terminal-local-map company-old-keymap
503 company-overriding-keymap-bound nil)))
506 ;; Emacs calculates the active keymaps before reading the event. That means we
507 ;; cannot change the keymap from a timer. So we send a bogus command.
508 (defun company-ignore ()
511 (global-set-key '[31415926] 'company-ignore)
513 (defun company-input-noop ()
514 (push 31415926 unread-command-events))
516 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
518 (defun company-grab (regexp &optional expression limit)
519 (when (looking-back regexp limit)
520 (or (match-string-no-properties (or expression 0)) "")))
522 (defun company-grab-line (regexp &optional expression)
523 (company-grab regexp expression (point-at-bol)))
525 (defun company-grab-symbol ()
526 (if (looking-at "\\_>")
527 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
531 (defun company-grab-word ()
532 (if (looking-at "\\>")
533 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
537 (defun company-in-string-or-comment ()
538 (let ((ppss (syntax-ppss)))
539 (or (nth 3 ppss) (nth 4 ppss) (nth 7 ppss))))
541 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
543 (defvar company-backend nil)
544 (make-variable-buffer-local 'company-backend)
546 (defvar company-prefix nil)
547 (make-variable-buffer-local 'company-prefix)
549 (defvar company-candidates nil)
550 (make-variable-buffer-local 'company-candidates)
552 (defvar company-candidates-length nil)
553 (make-variable-buffer-local 'company-candidates-length)
555 (defvar company-candidates-cache nil)
556 (make-variable-buffer-local 'company-candidates-cache)
558 (defvar company-candidates-predicate nil)
559 (make-variable-buffer-local 'company-candidates-predicate)
561 (defvar company-common nil)
562 (make-variable-buffer-local 'company-common)
564 (defvar company-selection 0)
565 (make-variable-buffer-local 'company-selection)
567 (defvar company-selection-changed nil)
568 (make-variable-buffer-local 'company-selection-changed)
570 (defvar company--explicit-action nil
571 "Non-nil, if explicit completion took place.")
572 (make-variable-buffer-local 'company--explicit-action)
574 (defvar company--this-command nil)
576 (defvar company-point nil)
577 (make-variable-buffer-local 'company-point)
579 (defvar company-timer nil)
581 (defvar company-added-newline nil)
582 (make-variable-buffer-local 'company-added-newline)
584 (defsubst company-strip-prefix (str)
585 (substring str (length company-prefix)))
587 (defun company-explicit-action-p ()
588 "Return whether explicit completion action was taken by the user."
589 (or company--explicit-action
590 company-selection-changed))
592 (defsubst company-reformat (candidate)
593 ;; company-ispell needs this, because the results are always lower-case
594 ;; It's mory efficient to fix it only when they are displayed.
595 (concat company-prefix (substring candidate (length company-prefix))))
597 (defun company--should-complete ()
598 (and (not (or buffer-read-only overriding-terminal-local-map
599 overriding-local-map))
600 (eq company-idle-delay t)
601 (or (eq t company-begin-commands)
602 (memq company--this-command company-begin-commands)
603 (and (symbolp this-command) (get this-command 'company-begin)))
604 (not (and transient-mark-mode mark-active))))
606 (defsubst company-call-frontends (command)
607 (dolist (frontend company-frontends)
609 (funcall frontend command)
610 (error (error "Company: Front-end %s error \"%s\" on command %s"
611 frontend (error-message-string err) command)))))
613 (defsubst company-set-selection (selection &optional force-update)
614 (setq selection (max 0 (min (1- company-candidates-length) selection)))
615 (when (or force-update (not (equal selection company-selection)))
616 (setq company-selection selection
617 company-selection-changed t)
618 (company-call-frontends 'update)))
620 (defun company-apply-predicate (candidates predicate)
622 (dolist (c candidates)
623 (when (funcall predicate c)
627 (defun company-update-candidates (candidates)
628 (setq company-candidates-length (length candidates))
629 (if (> company-selection 0)
630 ;; Try to restore the selection
631 (let ((selected (nth company-selection company-candidates)))
632 (setq company-selection 0
633 company-candidates candidates)
635 (while (and candidates (string< (pop candidates) selected))
636 (incf company-selection))
638 ;; Make sure selection isn't out of bounds.
639 (setq company-selection (min (1- company-candidates-length)
640 company-selection)))))
641 (setq company-selection 0
642 company-candidates candidates))
644 (push (cons company-prefix company-candidates) company-candidates-cache)
646 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
647 (setq company-common (try-completion company-prefix company-candidates)))
648 (when (eq company-common t)
649 (setq company-candidates nil)))
651 (defun company-calculate-candidates (prefix)
653 (or (cdr (assoc prefix company-candidates-cache))
654 (when company-candidates-cache
655 (let ((len (length prefix))
656 (completion-ignore-case (funcall company-backend
660 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
661 company-candidates-cache)))
662 (return (all-completions prefix prev))))))
663 (let ((c (funcall company-backend 'candidates prefix)))
664 (when company-candidates-predicate
665 (setq c (company-apply-predicate
666 c company-candidates-predicate)))
667 (unless (funcall company-backend 'sorted)
668 (setq c (sort c 'string<)))
670 (if (or (cdr candidates)
671 (not (equal (car candidates) prefix)))
672 ;; Don't start when already completed and unique.
674 ;; Not the right place? maybe when setting?
675 (and company-candidates t))))
677 (defun company-idle-begin (buf win tick pos)
679 (eq buf (current-buffer))
680 (eq win (selected-window))
681 (eq tick (buffer-chars-modified-tick))
683 (not company-candidates)
684 (not (equal (point) company-point))
685 (let ((company-idle-delay t))
687 (when company-candidates
689 (company-post-command)))))
691 (defun company-manual-begin ()
693 (company-assert-enabled)
695 (not company-candidates)
696 (let ((company-idle-delay t)
697 (company-minimum-prefix-length 0)
698 (company-begin-commands t))
699 (setq company--explicit-action t)
701 ;; Return non-nil if active.
704 (defsubst company-incremental-p (old-prefix new-prefix)
705 (and (> (length new-prefix) (length old-prefix))
706 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
708 (defun company-require-match-p ()
709 (let ((backend-value (funcall company-backend 'require-match)))
710 (or (eq backend-value t)
711 (and (if (functionp company-require-match)
712 (funcall company-require-match)
713 (eq company-require-match t))
714 (not (eq backend-value 'never))))))
716 (defun company-punctuation-p (input)
717 "Return non-nil, if input starts with punctuation or parentheses."
718 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
720 (defun company-auto-complete-p (beg end)
721 "Return non-nil, if input starts with punctuation or parentheses."
723 (if (functionp company-auto-complete)
724 (funcall company-auto-complete)
725 company-auto-complete)
726 (if (functionp company-auto-complete-chars)
727 (funcall company-auto-complete-chars (buffer-substring beg end))
728 (if (consp company-auto-complete-chars)
729 (memq (char-syntax (char-after beg)) company-auto-complete-chars)
730 (string-match (buffer-substring beg (1+ beg))
731 company-auto-complete-chars)))))
733 (defun company-continue ()
734 (when (funcall company-backend 'no-cache company-prefix)
735 ;; Don't complete existing candidates, fetch new ones.
736 (setq company-candidates-cache nil))
737 (let ((new-prefix (funcall company-backend 'prefix)))
738 (unless (and (= (- (point) (length new-prefix))
739 (- company-point (length company-prefix)))
740 (or (equal company-prefix new-prefix)
741 (let ((c (company-calculate-candidates new-prefix)))
742 ;; t means complete/unique.
744 (progn (company-cancel new-prefix) t)
746 (setq company-prefix new-prefix)
747 (company-update-candidates c)
749 (if (company-auto-complete-p company-point (point))
751 (goto-char company-point)
752 (company-complete-selection)
753 (setq company-candidates nil))
754 (if (not (and (company-incremental-p company-prefix new-prefix)
755 (company-require-match-p)))
757 (when (equal company-prefix (car company-candidates))
758 ;; cancel, but last input was actually success
759 (company-cancel company-prefix))
760 (setq company-candidates nil))
761 (backward-delete-char (length new-prefix))
762 (insert company-prefix)
764 (message "Matching input is required")))
765 company-candidates)))
767 (defun company-begin ()
768 (when (if company-candidates
769 (not (company-continue))
770 (company--should-complete))
772 (dolist (backend (if company-backend
773 ;; prefer manual override
774 (list company-backend)
776 (when (and (functionp backend)
777 (setq prefix (funcall backend 'prefix)))
778 (setq company-backend backend)
779 (when (>= (length prefix) company-minimum-prefix-length)
780 (let ((c (company-calculate-candidates prefix)))
781 ;; t means complete/unique. We don't start, so no hooks.
783 (setq company-prefix prefix)
784 (company-update-candidates c)
785 (run-hook-with-args 'company-completion-started-hook
786 (company-explicit-action-p))
787 (company-call-frontends 'show))))
789 (if company-candidates
791 (when (and company-end-of-buffer-workaround (eobp))
792 (save-excursion (insert "\n"))
793 (setq company-added-newline (buffer-chars-modified-tick)))
794 (setq company-point (point))
795 (company-enable-overriding-keymap company-active-map)
796 (company-call-frontends 'update))
799 (defun company-cancel (&optional result)
800 (and company-added-newline
801 (> (point-max) (point-min))
802 (let ((tick (buffer-chars-modified-tick)))
803 (delete-region (1- (point-max)) (point-max))
804 (equal tick company-added-newline))
805 ;; Only set unmodified when tick remained the same since insert.
806 (set-buffer-modified-p nil))
809 (run-hook-with-args 'company-completion-finished-hook result)
810 (run-hook-with-args 'company-completion-cancelled-hook result)))
811 (setq company-added-newline nil
814 company-candidates nil
815 company-candidates-length nil
816 company-candidates-cache nil
817 company-candidates-predicate nil
820 company-selection-changed nil
821 company--explicit-action nil
824 (cancel-timer company-timer))
825 (company-search-mode 0)
826 (company-call-frontends 'hide)
827 (company-enable-overriding-keymap nil))
829 (defun company-abort ()
832 ;; Don't start again, unless started manually.
833 (setq company-point (point)))
835 (defun company-finish (result)
836 (insert (company-strip-prefix result))
837 (company-cancel result)
838 ;; Don't start again, unless started manually.
839 (setq company-point (point)))
841 (defsubst company-keep (command)
842 (and (symbolp command) (get command 'company-keep)))
844 (defun company-pre-command ()
845 (unless (company-keep this-command)
847 (when company-candidates
848 (company-call-frontends 'pre-command))
849 (error (message "Company: An error occurred in pre-command")
850 (message "%s" (error-message-string err))
853 (cancel-timer company-timer))
854 (company-uninstall-map))
856 (defun company-post-command ()
857 (unless (company-keep this-command)
860 (setq company--this-command this-command)
861 (unless (equal (point) company-point)
863 (when company-candidates
864 (company-call-frontends 'post-command))
865 (when (numberp company-idle-delay)
867 (run-with-timer company-idle-delay nil 'company-idle-begin
868 (current-buffer) (selected-window)
869 (buffer-chars-modified-tick) (point)))))
870 (error (message "Company: An error occurred in post-command")
871 (message "%s" (error-message-string err))
873 (company-install-map))
875 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
877 (defvar company-search-string nil)
878 (make-variable-buffer-local 'company-search-string)
880 (defvar company-search-lighter " Search: \"\"")
881 (make-variable-buffer-local 'company-search-lighter)
883 (defvar company-search-old-map nil)
884 (make-variable-buffer-local 'company-search-old-map)
886 (defvar company-search-old-selection 0)
887 (make-variable-buffer-local 'company-search-old-selection)
889 (defun company-search (text lines)
890 (let ((quoted (regexp-quote text))
893 (when (string-match quoted line (length company-prefix))
897 (defun company-search-printing-char ()
899 (company-search-assert-enabled)
900 (setq company-search-string
901 (concat (or company-search-string "") (string last-command-event))
902 company-search-lighter (concat " Search: \"" company-search-string
904 (let ((pos (company-search company-search-string
905 (nthcdr company-selection company-candidates))))
908 (company-set-selection (+ company-selection pos) t))))
910 (defun company-search-repeat-forward ()
911 "Repeat the incremental search in completion candidates forward."
913 (company-search-assert-enabled)
914 (let ((pos (company-search company-search-string
915 (cdr (nthcdr company-selection
916 company-candidates)))))
919 (company-set-selection (+ company-selection pos 1) t))))
921 (defun company-search-repeat-backward ()
922 "Repeat the incremental search in completion candidates backwards."
924 (company-search-assert-enabled)
925 (let ((pos (company-search company-search-string
926 (nthcdr (- company-candidates-length
928 (reverse company-candidates)))))
931 (company-set-selection (- company-selection pos 1) t))))
933 (defun company-create-match-predicate ()
934 (setq company-candidates-predicate
936 ,(if company-candidates-predicate
937 `(and (string-match ,company-search-string candidate)
938 (funcall ,company-candidates-predicate
940 `(string-match ,company-search-string candidate))))
941 (company-update-candidates
942 (company-apply-predicate company-candidates company-candidates-predicate))
944 (setq company-candidates-cache (cons company-prefix company-candidates)))
946 (defun company-filter-printing-char ()
948 (company-search-assert-enabled)
949 (company-search-printing-char)
950 (company-create-match-predicate)
951 (company-call-frontends 'update))
953 (defun company-search-kill-others ()
954 "Limit the completion candidates to the ones matching the search string."
956 (company-search-assert-enabled)
957 (company-create-match-predicate)
958 (company-search-mode 0)
959 (company-call-frontends 'update))
961 (defun company-search-abort ()
962 "Abort searching the completion candidates."
964 (company-search-assert-enabled)
965 (company-set-selection company-search-old-selection t)
966 (company-search-mode 0))
968 (defun company-search-other-char ()
970 (company-search-assert-enabled)
971 (company-search-mode 0)
972 (when last-input-event
973 (clear-this-command-keys t)
974 (setq unread-command-events (list last-input-event))))
976 (defvar company-search-map
978 (keymap (make-keymap)))
979 (if (fboundp 'max-char)
980 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
981 'company-search-printing-char)
983 ;; obselete in Emacs 23
984 (let ((l (generic-character-list))
985 (table (nth 1 keymap)))
987 (set-char-table-default table (car l) 'company-search-printing-char)
989 (define-key keymap [t] 'company-search-other-char)
991 (define-key keymap (make-string 1 i) 'company-search-other-char)
994 (define-key keymap (vector i) 'company-search-printing-char)
996 (let ((meta-map (make-sparse-keymap)))
997 (define-key keymap (char-to-string meta-prefix-char) meta-map)
998 (define-key keymap [escape] meta-map))
999 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1000 (define-key keymap "\e\e\e" 'company-search-other-char)
1001 (define-key keymap [escape escape escape] 'company-search-other-char)
1003 (define-key keymap "\C-g" 'company-search-abort)
1004 (define-key keymap "\C-s" 'company-search-repeat-forward)
1005 (define-key keymap "\C-r" 'company-search-repeat-backward)
1006 (define-key keymap "\C-o" 'company-search-kill-others)
1008 "Keymap used for incrementally searching the completion candidates.")
1010 (define-minor-mode company-search-mode
1011 "Search mode for completion candidates.
1012 Don't start this directly, use `company-search-candidates' or
1013 `company-filter-candidates'."
1014 nil company-search-lighter nil
1015 (if company-search-mode
1016 (if (company-manual-begin)
1018 (setq company-search-old-selection company-selection)
1019 (company-call-frontends 'update))
1020 (setq company-search-mode nil))
1021 (kill-local-variable 'company-search-string)
1022 (kill-local-variable 'company-search-lighter)
1023 (kill-local-variable 'company-search-old-selection)
1024 (company-enable-overriding-keymap company-active-map)))
1026 (defsubst company-search-assert-enabled ()
1027 (company-assert-enabled)
1028 (unless company-search-mode
1029 (company-uninstall-map)
1030 (error "Company not in search mode")))
1032 (defun company-search-candidates ()
1033 "Start searching the completion candidates incrementally.
1035 \\<company-search-map>Search can be controlled with the commands:
1036 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1037 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1038 - `company-search-abort' (\\[company-search-abort])
1040 Regular characters are appended to the search string.
1042 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
1043 the search string to limit the completion candidates."
1045 (company-search-mode 1)
1046 (company-enable-overriding-keymap company-search-map))
1048 (defvar company-filter-map
1049 (let ((keymap (make-keymap)))
1050 (define-key keymap [remap company-search-printing-char]
1051 'company-filter-printing-char)
1052 (set-keymap-parent keymap company-search-map)
1054 "Keymap used for incrementally searching the completion candidates.")
1056 (defun company-filter-candidates ()
1057 "Start filtering the completion candidates incrementally.
1058 This works the same way as `company-search-candidates' immediately
1059 followed by `company-search-kill-others' after each input."
1061 (company-search-mode 1)
1062 (company-enable-overriding-keymap company-filter-map))
1064 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1066 (defun company-select-next ()
1067 "Select the next candidate in the list."
1069 (when (company-manual-begin)
1070 (company-set-selection (1+ company-selection))))
1072 (defun company-select-previous ()
1073 "Select the previous candidate in the list."
1075 (when (company-manual-begin)
1076 (company-set-selection (1- company-selection))))
1078 (defun company-select-mouse (event)
1079 "Select the candidate picked by the mouse."
1081 (when (nth 4 (event-start event))
1082 (company-set-selection (- (cdr (posn-actual-col-row (event-start event)))
1083 (cdr (posn-actual-col-row (posn-at-point)))
1087 (defun company-complete-mouse (event)
1088 "Complete the candidate picked by the mouse."
1090 (when (company-select-mouse event)
1091 (company-complete-selection)))
1093 (defun company-complete-selection ()
1094 "Complete the selected candidate."
1096 (when (company-manual-begin)
1097 (company-finish (nth company-selection company-candidates))))
1099 (defun company-complete-common ()
1100 "Complete the common part of all candidates."
1102 (when (company-manual-begin)
1103 (if (and (not (cdr company-candidates))
1104 (equal company-common (car company-candidates)))
1105 (company-complete-selection)
1106 (insert (company-strip-prefix company-common)))))
1108 (defun company-complete ()
1109 "Complete the common part of all candidates or the current selection.
1110 The first time this is called, the common part is completed, the second time, or
1111 when the selection has been changed, the selected candidate is completed."
1113 (when (company-manual-begin)
1114 (if (or company-selection-changed
1115 (eq last-command 'company-complete-common))
1116 (call-interactively 'company-complete-selection)
1117 (call-interactively 'company-complete-common)
1118 (setq this-command 'company-complete-common))))
1120 (defun company-complete-number (n)
1121 "Complete the Nth candidate.
1122 To show the number next to the candidates in some back-ends, enable
1123 `company-show-numbers'."
1124 (when (company-manual-begin)
1125 (and (< n 1) (> n company-candidates-length)
1126 (error "No candidate number %d" n))
1128 (company-finish (nth n company-candidates))))
1130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1132 (defconst company-space-strings-limit 100)
1134 (defconst company-space-strings
1136 (dotimes (i company-space-strings-limit)
1137 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1138 (apply 'vector lst)))
1140 (defsubst company-space-string (len)
1141 (if (< len company-space-strings-limit)
1142 (aref company-space-strings len)
1143 (make-string len ?\ )))
1145 (defsubst company-safe-substring (str from &optional to)
1146 (let ((len (length str)))
1149 (if (and to (> to len))
1150 (concat (substring str from)
1151 (company-space-string (- to len)))
1152 (substring str from to)))))
1154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1156 (defvar company-last-metadata nil)
1157 (make-variable-buffer-local 'company-last-metadata)
1159 (defun company-fetch-metadata ()
1160 (let ((selected (nth company-selection company-candidates)))
1161 (unless (equal selected (car company-last-metadata))
1162 (setq company-last-metadata
1163 (cons selected (funcall company-backend 'meta selected))))
1164 (cdr company-last-metadata)))
1166 (defun company-doc-buffer (&optional string)
1167 (with-current-buffer (get-buffer-create "*Company meta-data*")
1171 (defmacro company-electric (&rest body)
1172 (declare (indent 0) (debug t))
1173 `(when (company-manual-begin)
1174 (save-window-excursion
1175 (let ((height (window-height))
1176 (row (cdr (posn-actual-col-row (posn-at-point)))))
1178 (and (< (window-height) height)
1179 (< (- (window-height) row 2) company-tooltip-limit)
1180 (recenter (- (window-height) row 2)))
1181 (while (eq 'scroll-other-window
1182 (key-binding (vector (list (read-event)))))
1183 (call-interactively 'scroll-other-window))
1184 (when last-input-event
1185 (clear-this-command-keys t)
1186 (setq unread-command-events (list last-input-event)))))))
1188 (defun company-show-doc-buffer ()
1189 "Temporarily show a buffer with the complete documentation for the selection."
1192 (let ((selected (nth company-selection company-candidates)))
1193 (display-buffer (or (funcall company-backend 'doc-buffer selected)
1194 (error "No documentation available")) t))))
1195 (put 'company-show-doc-buffer 'company-keep t)
1197 (defun company-show-location ()
1198 "Temporarily display a buffer showing the selected candidate in context."
1201 (let* ((selected (nth company-selection company-candidates))
1202 (location (funcall company-backend 'location selected))
1203 (pos (or (cdr location) (error "No location available")))
1204 (buffer (or (and (bufferp (car location)) (car location))
1205 (find-file-noselect (car location) t))))
1206 (with-selected-window (display-buffer buffer t)
1207 (if (bufferp (car location))
1210 (set-window-start nil (point))))))
1211 (put 'company-show-location 'company-keep t)
1213 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1215 (defvar company-callback nil)
1216 (make-variable-buffer-local 'company-callback)
1218 (defun company-remove-callback (&optional ignored)
1219 (remove-hook 'company-completion-finished-hook company-callback t)
1220 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t))
1222 (defun company-begin-backend (backend &optional callback)
1223 "Start a completion at point using BACKEND."
1224 (interactive (let ((val (completing-read "Company back-end: "
1226 'functionp nil "company-")))
1228 (list (intern val)))))
1230 (setq company-callback
1231 `(lambda (completion)
1232 (funcall ',callback completion)
1233 (company-remove-callback)))
1234 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1235 (add-hook 'company-completion-finished-hook company-callback nil t))
1236 (setq company-backend backend)
1237 ;; Return non-nil if active.
1238 (or (company-manual-begin)
1239 (error "Cannot complete at point")))
1241 (defun company-begin-with (candidates
1242 &optional prefix-length require-match callback)
1243 "Start a completion at point.
1244 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
1245 the prefix that already is in the buffer before point. It defaults to 0.
1247 CALLBACK is a function called with the selected result if the user successfully
1248 completes the input.
1251 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1252 (company-begin-backend
1253 (let ((start (- (point) (or prefix-length 0))))
1254 `(lambda (command &optional arg &rest ignored)
1255 (case command-history
1256 ('prefix (message "prefix %s" (buffer-substring ,start (point)))
1257 (when (>= (point) ,start)
1258 (buffer-substring ,start (point))))
1259 ('candidates (all-completions arg ',candidates))
1260 ('require-match ,require-match))))
1263 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1265 (defvar company-pseudo-tooltip-overlay nil)
1266 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1268 (defvar company-tooltip-offset 0)
1269 (make-variable-buffer-local 'company-tooltip-offset)
1271 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1274 (setq company-tooltip-offset
1275 (max (min selection company-tooltip-offset)
1276 (- selection -1 limit)))
1278 (when (<= company-tooltip-offset 1)
1280 (setq company-tooltip-offset 0))
1282 (when (>= company-tooltip-offset (- num-lines limit 1))
1284 (when (= selection (1- num-lines))
1285 (decf company-tooltip-offset)
1286 (when (<= company-tooltip-offset 1)
1287 (setq company-tooltip-offset 0)
1294 (defsubst company-round-tab (arg)
1295 (* (/ (+ arg tab-width) tab-width) tab-width))
1297 (defun company-untabify (str)
1298 (let* ((pieces (split-string str "\t"))
1301 (setcar copy (company-safe-substring
1302 (car copy) 0 (company-round-tab (string-width (car copy)))))
1304 (apply 'concat pieces)))
1306 (defun company-fill-propertize (line width selected)
1307 (setq line (company-safe-substring line 0 width))
1308 (add-text-properties 0 width '(face company-tooltip
1309 mouse-face company-tooltip-mouse)
1311 (add-text-properties 0 (length company-common)
1312 '(face company-tooltip-common
1313 mouse-face company-tooltip-mouse)
1316 (if (and company-search-string
1317 (string-match (regexp-quote company-search-string) line
1318 (length company-prefix)))
1320 (add-text-properties (match-beginning 0) (match-end 0)
1321 '(face company-tooltip-selection)
1323 (when (< (match-beginning 0) (length company-common))
1324 (add-text-properties (match-beginning 0) (length company-common)
1325 '(face company-tooltip-common-selection)
1327 (add-text-properties 0 width '(face company-tooltip-selection
1328 mouse-face company-tooltip-selection)
1330 (add-text-properties 0 (length company-common)
1331 '(face company-tooltip-common-selection
1332 mouse-face company-tooltip-selection)
1338 (defun company-buffer-lines (beg end)
1340 (let ((row (cdr (posn-actual-col-row (posn-at-point))))
1342 (while (and (equal (move-to-window-line (incf row)) row)
1344 (push (buffer-substring beg (min end (1- (point)))) lines)
1346 (unless (eq beg end)
1347 (push (buffer-substring beg end) lines))
1350 (defsubst company-modify-line (old new offset)
1351 (concat (company-safe-substring old 0 offset)
1353 (company-safe-substring old (+ offset (length new)))))
1355 (defun company-replacement-string (old lines column nl)
1357 ;; Inject into old lines.
1359 (push (company-modify-line (pop old) (pop lines) column) new))
1360 ;; Append whole new lines.
1362 (push (concat (company-space-string column) (pop lines)) new))
1363 (concat (when nl "\n")
1364 (mapconcat 'identity (nreverse new) "\n")
1367 (defun company-create-lines (column selection limit)
1369 (let ((len company-candidates-length)
1378 ;; Scroll to offset.
1379 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1381 (when (> company-tooltip-offset 0)
1382 (setq previous (format "...(%d)" company-tooltip-offset)))
1384 (setq remainder (- len limit company-tooltip-offset)
1385 remainder (when (> remainder 0)
1386 (setq remainder (format "...(%d)" remainder))))
1388 (decf selection company-tooltip-offset)
1389 (setq width (min (length previous) (length remainder))
1390 lines (nthcdr company-tooltip-offset company-candidates)
1395 (setq width (max (length (pop lines-copy)) width)))
1396 (setq width (min width (- (window-width) column)))
1398 (setq lines-copy lines)
1400 ;; number can make tooltip too long
1401 (and company-show-numbers
1402 (< (setq numbered company-tooltip-offset) 10)
1406 (push (propertize (company-safe-substring previous 0 width)
1407 'face 'company-tooltip)
1411 (push (company-fill-propertize
1412 (if (>= numbered 10)
1413 (company-reformat (pop lines))
1416 (company-safe-substring (company-reformat (pop lines))
1419 width (equal i selection))
1423 (push (propertize (company-safe-substring remainder 0 width)
1424 'face 'company-tooltip)
1427 (setq lines (nreverse new))))
1431 (defsubst company-pseudo-tooltip-height ()
1432 "Calculate the appropriate tooltip height."
1433 (max 3 (min company-tooltip-limit
1434 (- (window-height) 2
1435 (count-lines (window-start) (point-at-bol))))))
1437 (defun company-pseudo-tooltip-show (row column selection)
1438 (company-pseudo-tooltip-hide)
1443 (let* ((height (company-pseudo-tooltip-height))
1444 (lines (company-create-lines column selection height))
1445 (nl (< (move-to-window-line row) row))
1447 (end (save-excursion
1448 (move-to-window-line (+ row height))
1451 (mapcar 'company-untabify (company-buffer-lines beg end)))
1454 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1456 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1457 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1458 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1459 (overlay-put company-pseudo-tooltip-overlay 'company-before
1460 (company-replacement-string old-string lines column nl))
1461 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1463 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1465 (defun company-pseudo-tooltip-show-at-point (pos)
1466 (let ((col-row (posn-actual-col-row (posn-at-point pos))))
1467 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
1468 company-selection)))
1470 (defun company-pseudo-tooltip-edit (lines selection)
1471 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1472 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1473 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1474 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1475 (lines (company-create-lines column selection height)))
1476 (overlay-put company-pseudo-tooltip-overlay 'company-before
1477 (company-replacement-string old-string lines column nl))))
1479 (defun company-pseudo-tooltip-hide ()
1480 (when company-pseudo-tooltip-overlay
1481 (delete-overlay company-pseudo-tooltip-overlay)
1482 (setq company-pseudo-tooltip-overlay nil)))
1484 (defun company-pseudo-tooltip-hide-temporarily ()
1485 (when (overlayp company-pseudo-tooltip-overlay)
1486 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1487 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1489 (defun company-pseudo-tooltip-unhide ()
1490 (when company-pseudo-tooltip-overlay
1491 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1492 (overlay-put company-pseudo-tooltip-overlay 'before-string
1493 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1494 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1496 (defun company-pseudo-tooltip-frontend (command)
1497 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1499 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1501 (unless (and (overlayp company-pseudo-tooltip-overlay)
1502 (equal (overlay-get company-pseudo-tooltip-overlay
1504 (company-pseudo-tooltip-height)))
1506 (company-pseudo-tooltip-show-at-point (- (point)
1507 (length company-prefix))))
1508 (company-pseudo-tooltip-unhide))
1509 ('hide (company-pseudo-tooltip-hide)
1510 (setq company-tooltip-offset 0))
1511 ('update (when (overlayp company-pseudo-tooltip-overlay)
1512 (company-pseudo-tooltip-edit company-candidates
1513 company-selection)))))
1515 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1516 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1517 (unless (and (eq command 'post-command)
1518 (not (cdr company-candidates)))
1519 (company-pseudo-tooltip-frontend command)))
1521 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1523 (defvar company-preview-overlay nil)
1524 (make-variable-buffer-local 'company-preview-overlay)
1526 (defun company-preview-show-at-point (pos)
1527 (company-preview-hide)
1529 (setq company-preview-overlay (make-overlay pos pos))
1531 (let ((completion(nth company-selection company-candidates)))
1532 (setq completion (propertize completion 'face 'company-preview))
1533 (add-text-properties 0 (length company-common)
1534 '(face company-preview-common) completion)
1536 ;; Add search string
1537 (and company-search-string
1538 (string-match (regexp-quote company-search-string) completion)
1539 (add-text-properties (match-beginning 0)
1541 '(face company-preview-search)
1544 (setq completion (company-strip-prefix completion))
1546 (and (equal pos (point))
1547 (not (equal completion ""))
1548 (add-text-properties 0 1 '(cursor t) completion))
1550 (overlay-put company-preview-overlay 'after-string completion)
1551 (overlay-put company-preview-overlay 'window (selected-window))))
1553 (defun company-preview-hide ()
1554 (when company-preview-overlay
1555 (delete-overlay company-preview-overlay)
1556 (setq company-preview-overlay nil)))
1558 (defun company-preview-frontend (command)
1559 "A `company-mode' front-end showing the selection as if it had been inserted."
1561 ('pre-command (company-preview-hide))
1562 ('post-command (company-preview-show-at-point (point)))
1563 ('hide (company-preview-hide))))
1565 (defun company-preview-if-just-one-frontend (command)
1566 "`company-preview-frontend', but only shown for single candidates."
1567 (unless (and (eq command 'post-command)
1568 (cdr company-candidates))
1569 (company-preview-frontend command)))
1571 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1573 (defvar company-echo-last-msg nil)
1574 (make-variable-buffer-local 'company-echo-last-msg)
1576 (defvar company-echo-timer nil)
1578 (defvar company-echo-delay .1)
1580 (defun company-echo-show (&optional getter)
1582 (setq company-echo-last-msg (funcall getter)))
1583 (let ((message-log-max nil))
1584 (if company-echo-last-msg
1585 (message "%s" company-echo-last-msg)
1588 (defsubst company-echo-show-soon (&optional getter)
1589 (when company-echo-timer
1590 (cancel-timer company-echo-timer))
1591 (setq company-echo-timer (run-with-timer company-echo-delay nil
1592 'company-echo-show getter)))
1594 (defun company-echo-format ()
1596 (let ((limit (window-width (minibuffer-window)))
1598 ;; Roll to selection.
1599 (candidates (nthcdr company-selection company-candidates))
1600 (i (if company-show-numbers company-selection 99999))
1604 (setq comp (company-reformat (pop candidates))
1605 len (+ len 1 (length comp)))
1609 (setq comp (propertize (format "%d: %s" i comp)
1610 'face 'company-echo))
1613 (add-text-properties 3 (+ 3 (length company-common))
1614 '(face company-echo-common) comp))
1615 (setq comp (propertize comp 'face 'company-echo))
1616 (add-text-properties 0 (length company-common)
1617 '(face company-echo-common) comp))
1619 (setq candidates nil)
1622 (mapconcat 'identity (nreverse msg) " ")))
1624 (defun company-echo-strip-common-format ()
1626 (let ((limit (window-width (minibuffer-window)))
1627 (len (+ (length company-prefix) 2))
1628 ;; Roll to selection.
1629 (candidates (nthcdr company-selection company-candidates))
1630 (i (if company-show-numbers company-selection 99999))
1634 (setq comp (company-strip-prefix (pop candidates))
1635 len (+ len 2 (length comp)))
1638 (setq comp (format "%s (%d)" comp i))
1642 (setq candidates nil)
1643 (push (propertize comp 'face 'company-echo) msg)))
1645 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1646 (mapconcat 'identity (nreverse msg) ", ")
1649 (defun company-echo-hide ()
1650 (when company-echo-timer
1651 (cancel-timer company-echo-timer))
1652 (unless (equal company-echo-last-msg "")
1653 (setq company-echo-last-msg "")
1654 (company-echo-show)))
1656 (defun company-echo-frontend (command)
1657 "A `company-mode' front-end showing the candidates in the echo area."
1659 ('pre-command (company-echo-show-soon))
1660 ('post-command (company-echo-show-soon 'company-echo-format))
1661 ('hide (company-echo-hide))))
1663 (defun company-echo-strip-common-frontend (command)
1664 "A `company-mode' front-end showing the candidates in the echo area."
1666 ('pre-command (company-echo-show-soon))
1667 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1668 ('hide (company-echo-hide))))
1670 (defun company-echo-metadata-frontend (command)
1671 "A `company-mode' front-end showing the documentation in the echo area."
1673 ('pre-command (company-echo-show-soon))
1674 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1675 ('hide (company-echo-hide))))
1678 ;;; company.el ends here