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 ;; Performance enhancements.
75 ;; Added `company-begin-commands' option.
76 ;; Added abbrev, tempo and Xcode back-ends.
77 ;; Back-ends are now interactive. You can start them with M-x backend-name.
78 ;; Added `company-begin-with' for starting company from elisp-code.
80 ;; Added `company-require-match' and `company-auto-complete' options.
83 ;; Improved Emacs Lisp back-end behavior for local variables.
84 ;; Added `company-elisp-detect-function-context' option.
85 ;; The mouse can now be used for selection.
88 ;; Added `company-show-location'.
89 ;; Added etags back-end.
90 ;; Added work-around for end-of-buffer bug.
91 ;; Added `company-filter-candidates'.
92 ;; More local Lisp variables are now included in the candidates.
95 ;; Fixed elisp documentation buffer always showing the same doc.
96 ;; Added `company-echo-strip-common-frontend'.
97 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
98 ;; Don't hide the echo message if it isn't shown.
105 (eval-when-compile (require 'cl))
107 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
108 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
109 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
110 (add-to-list 'debug-ignored-errors "^Company not ")
111 (add-to-list 'debug-ignored-errors "^No candidate number ")
112 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
114 (defgroup company nil
115 "Extensible inline text completion mechanism"
120 (defface company-tooltip
121 '((t :background "yellow"
122 :foreground "black"))
123 "*Face used for the tool tip."
126 (defface company-tooltip-selection
127 '((default :inherit company-tooltip)
128 (((class color) (min-colors 88)) (:background "orange1"))
129 (t (:background "green")))
130 "*Face used for the selection in the tool tip."
133 (defface company-tooltip-mouse
134 '((default :inherit highlight))
135 "*Face used for the tool tip item under the mouse."
138 (defface company-tooltip-common
139 '((t :inherit company-tooltip
141 "*Face used for the common completion in the tool tip."
144 (defface company-tooltip-common-selection
145 '((t :inherit company-tooltip-selection
147 "*Face used for the selected common completion in the tool tip."
150 (defcustom company-tooltip-limit 10
151 "*The maximum number of candidates in the tool tip"
155 (defface company-preview
156 '((t :background "blue4"
157 :foreground "wheat"))
158 "*Face used for the completion preview."
161 (defface company-preview-common
162 '((t :inherit company-preview
164 "*Face used for the common part of the completion preview."
167 (defface company-preview-search
168 '((t :inherit company-preview
169 :background "blue1"))
170 "*Face used for the search string in the completion preview."
173 (defface company-echo nil
174 "*Face used for completions in the echo area."
177 (defface company-echo-common
178 '((((background dark)) (:foreground "firebrick1"))
179 (((background light)) (:background "firebrick4")))
180 "*Face used for the common part of completions in the echo area."
183 (defun company-frontends-set (variable value)
185 (let ((remainder value))
186 (setcdr remainder (delq (car remainder) (cdr remainder))))
187 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
188 (memq 'company-pseudo-tooltip-frontend value)
189 (error "Pseudo tooltip frontend cannot be used twice"))
190 (and (memq 'company-preview-if-just-one-frontend value)
191 (memq 'company-preview-frontend value)
192 (error "Preview frontend cannot be used twice"))
193 (and (memq 'company-echo value)
194 (memq 'company-echo-metadata-frontend value)
195 (error "Echo area cannot be used twice"))
196 ;; preview must come last
197 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
199 (setq value (append (delq f value) (list f)))))
200 (set variable value))
202 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
203 company-preview-frontend
204 company-echo-metadata-frontend)
205 "*The list of active front-ends (visualizations).
206 Each front-end is a function that takes one argument. It is called with
207 one of the following arguments:
209 'show: When the visualization should start.
211 'hide: When the visualization should end.
213 'update: When the data has been updated.
215 'pre-command: Before every command that is executed while the
216 visualization is active.
218 'post-command: After every command that is executed while the
219 visualization is active.
221 The visualized data is stored in `company-prefix', `company-candidates',
222 `company-common', `company-selection', `company-point' and
223 `company-search-string'."
224 :set 'company-frontends-set
226 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
227 (const :tag "echo, strip common"
228 company-echo-strip-common-frontend)
229 (const :tag "show echo meta-data in echo"
230 company-echo-metadata-frontend)
231 (const :tag "pseudo tooltip"
232 company-pseudo-tooltip-frontend)
233 (const :tag "pseudo tooltip, multiple only"
234 company-pseudo-tooltip-unless-just-one-frontend)
235 (const :tag "preview" company-preview-frontend)
236 (const :tag "preview, unique only"
237 company-preview-if-just-one-frontend)
238 (function :tag "custom function" nil))))
240 (defcustom company-backends '(company-elisp company-nxml company-css
241 company-semantic company-xcode company-gtags
242 company-etags company-oddmuse company-files
244 "*The list of active back-ends (completion engines).
245 Each back-end is a function that takes a variable number of arguments.
246 The first argument is the command requested from the back-end. It is one
249 'prefix: The back-end should return the text to be completed. It must be
250 text immediately before `point'. Returning nil passes control to the next
253 'candidates: The second argument is the prefix to be completed. The
254 return value should be a list of candidates that start with the prefix.
258 'sorted: The back-end may return t here to indicate that the candidates
259 are sorted and will not need to be sorted again.
261 'no-cache: Usually company doesn't ask for candidates again as completion
262 progresses, unless the back-end returns t for this command. The second
263 argument is the latest prefix.
265 'meta: The second argument is a completion candidate. The back-end should
266 return a (short) documentation string for it.
268 'doc-buffer: The second argument is a completion candidate. The back-end should
269 create a buffer (preferably with `company-doc-buffer'), fill it with
270 documentation and return it.
272 'location: The second argument is a completion candidate. The back-end can
273 return the cons of buffer and buffer location, or of file and line
274 number where the completion candidate was defined.
276 'require-match: If this value is t, the user is not allowed to enter anything
277 not offering as a candidate. Use with care! The default value nil gives the
278 user that choice with `company-require-match'. Return value 'never overrides
279 that option the other way around.
281 The back-end should return nil for all commands it does not support or
282 does not know about. It should also be callable interactively and use
283 `company-begin-backend' to start itself in that case."
285 :type '(repeat (function :tag "function" nil)))
287 (defvar start-count 0)
289 (defcustom company-completion-started-hook nil
290 "*Hook run when company starts completing.
291 The hook is called with one argument that is non-nil if the completion was
296 (defcustom company-completion-cancelled-hook nil
297 "*Hook run when company cancels completing.
298 The hook is called with one argument that is non-nil if the completion was
303 (defcustom company-completion-finished-hook nil
304 "*Hook run when company successfully completes.
305 The hook is called with the selected candidate as an argument."
309 (defcustom company-minimum-prefix-length 3
310 "*The minimum prefix length for automatic completion."
312 :type '(integer :tag "prefix length"))
314 (defcustom company-require-match 'company-explicit-action-p
315 "*If enabled, disallow non-matching input.
316 This can be a function do determine if a match is required.
318 This can be overridden by the back-end, if it returns t or 'never to
319 'require-match. `company-auto-complete' also takes precedence over this."
321 :type '(choice (const :tag "Off" nil)
322 (function :tag "Predicate function")
323 (const :tag "On, if user interaction took place"
324 'company-explicit-action-p)
325 (const :tag "On" t)))
327 (defcustom company-auto-complete 'company-explicit-action-p
328 "Determines when to auto-complete.
329 If this is enabled, all characters from `company-auto-complete-chars' complete
330 the selected completion. This can also be a function."
332 :type '(choice (const :tag "Off" nil)
333 (function :tag "Predicate function")
334 (const :tag "On, if user interaction took place"
335 'company-explicit-action-p)
336 (const :tag "On" t)))
338 (defcustom company-auto-complete-chars '(?\ ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
339 "Determines which characters trigger an automatic completion.
340 See `company-auto-complete'. If this is a string, each string character causes
341 completion. If it is a list of syntax description characters (see
342 `modify-syntax-entry'), all characters with that syntax auto-complete.
344 This can also be a function, which is called with the new input and should
345 return non-nil if company should auto-complete.
347 A character that is part of a valid candidate never starts auto-completion."
349 :type '(choice (string :tag "Characters")
351 (const :tag "Whitespace" ?\ )
352 (const :tag "Symbol" ?_)
353 (const :tag "Opening parentheses" ?\()
354 (const :tag "Closing parentheses" ?\))
355 (const :tag "Word constituent" ?w)
356 (const :tag "Punctuation." ?.)
357 (const :tag "String quote." ?\")
358 (const :tag "Paired delimiter." ?$)
359 (const :tag "Expression quote or prefix operator." ?\')
360 (const :tag "Comment starter." ?<)
361 (const :tag "Comment ender." ?>)
362 (const :tag "Character-quote." ?/)
363 (const :tag "Generic string fence." ?|)
364 (const :tag "Generic comment fence." ?!))
365 (function :tag "Predicate function")))
367 (defcustom company-idle-delay .7
368 "*The idle delay in seconds until automatic completions starts.
369 A value of nil means never complete automatically, t means complete
370 immediately when a prefix of `company-minimum-prefix-length' is reached."
372 :type '(choice (const :tag "never (nil)" nil)
373 (const :tag "immediate (t)" t)
374 (number :tag "seconds")))
376 (defcustom company-begin-commands t
377 "*A list of commands following which company will start completing.
378 If this is t, it will complete after any command. See `company-idle-delay'.
380 Alternatively any command with a non-nil 'company-begin property is treated as
381 if it was on this list."
383 :type '(choice (const :tag "Any command" t)
384 (const :tag "Self insert command" '(self-insert-command))
385 (repeat :tag "Commands" function)))
387 (defcustom company-show-numbers nil
388 "*If enabled, show quick-access numbers for the first ten candidates."
390 :type '(choice (const :tag "off" nil)
391 (const :tag "on" t)))
393 (defvar company-end-of-buffer-workaround t
394 "*Work around a visualization bug when completing at the end of the buffer.
395 The work-around consists of adding a newline.")
397 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
399 (defvar company-mode-map (make-sparse-keymap)
400 "Keymap used by `company-mode'.")
402 (defvar company-active-map
403 (let ((keymap (make-sparse-keymap)))
404 (define-key keymap "\e\e\e" 'company-abort)
405 (define-key keymap "\C-g" 'company-abort)
406 (define-key keymap (kbd "M-n") 'company-select-next)
407 (define-key keymap (kbd "M-p") 'company-select-previous)
408 (define-key keymap (kbd "<down>") 'company-select-next)
409 (define-key keymap (kbd "<up>") 'company-select-previous)
410 (define-key keymap [down-mouse-1] 'ignore)
411 (define-key keymap [down-mouse-3] 'ignore)
412 (define-key keymap [mouse-1] 'company-complete-mouse)
413 (define-key keymap [mouse-3] 'company-select-mouse)
414 (define-key keymap [up-mouse-1] 'ignore)
415 (define-key keymap [up-mouse-3] 'ignore)
416 (define-key keymap "\C-m" 'company-complete-selection)
417 (define-key keymap "\t" 'company-complete-common)
418 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
419 (define-key keymap "\C-w" 'company-show-location)
420 (define-key keymap "\C-s" 'company-search-candidates)
421 (define-key keymap "\C-\M-s" 'company-filter-candidates)
423 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
424 `(lambda () (interactive) (company-complete-number ,i))))
427 "Keymap that is enabled during an active completion.")
430 (define-minor-mode company-mode
431 "\"complete anything\"; in in-buffer completion framework.
432 Completion starts automatically, depending on the values
433 `company-idle-delay' and `company-minimum-prefix-length'.
435 Completion can be controlled with the commands:
436 `company-complete-common', `company-complete-selection', `company-complete',
437 `company-select-next', `company-select-previous'. If these commands are
438 called before `company-idle-delay', completion will also start.
440 Completions can be searched with `company-search-candidates' or
441 `company-filter-candidates'. These can be used while completion is
444 The completion data is retrieved using `company-backends' and displayed using
445 `company-frontends'. If you want to start a specific back-end, call it
446 interactively or use `company-begin-backend'.
448 regular keymap (`company-mode-map'):
451 keymap during active completions (`company-active-map'):
453 \\{company-active-map}"
454 nil " comp" company-mode-map
457 (add-hook 'pre-command-hook 'company-pre-command nil t)
458 (add-hook 'post-command-hook 'company-post-command nil t)
459 (dolist (backend company-backends)
460 (when (symbolp backend)
461 (unless (fboundp backend)
462 (ignore-errors (require backend nil t)))
463 (unless (fboundp backend)
464 (message "Company back-end '%s' could not be initialized"
466 (remove-hook 'pre-command-hook 'company-pre-command t)
467 (remove-hook 'post-command-hook 'company-post-command t)
469 (kill-local-variable 'company-point)))
471 (defsubst company-assert-enabled ()
473 (company-uninstall-map)
474 (error "Company not enabled")))
476 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
478 (defvar company-overriding-keymap-bound nil)
479 (make-variable-buffer-local 'company-overriding-keymap-bound)
481 (defvar company-old-keymap nil)
482 (make-variable-buffer-local 'company-old-keymap)
484 (defvar company-my-keymap nil)
485 (make-variable-buffer-local 'company-my-keymap)
487 (defsubst company-enable-overriding-keymap (keymap)
488 (setq company-my-keymap keymap)
489 (when company-overriding-keymap-bound
490 (company-uninstall-map)))
492 (defun company-install-map ()
493 (unless (or company-overriding-keymap-bound
494 (null company-my-keymap))
495 (setq company-old-keymap overriding-terminal-local-map
496 overriding-terminal-local-map company-my-keymap
497 company-overriding-keymap-bound t)))
499 (defun company-uninstall-map ()
500 (when (eq overriding-terminal-local-map company-my-keymap)
501 (setq overriding-terminal-local-map company-old-keymap
502 company-overriding-keymap-bound nil)))
505 ;; Emacs calculates the active keymaps before reading the event. That means we
506 ;; cannot change the keymap from a timer. So we send a bogus command.
507 (defun company-ignore ()
510 (global-set-key '[31415926] 'company-ignore)
512 (defun company-input-noop ()
513 (push 31415926 unread-command-events))
515 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
517 (defun company-grab (regexp &optional expression limit)
518 (when (looking-back regexp limit)
519 (or (match-string-no-properties (or expression 0)) "")))
521 (defun company-grab-line (regexp &optional expression)
522 (company-grab regexp expression (point-at-bol)))
524 (defun company-grab-symbol ()
525 (if (looking-at "\\_>")
526 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
530 (defun company-grab-word ()
531 (if (looking-at "\\>")
532 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
536 (defun company-in-string-or-comment (&optional point)
537 (let ((pos (syntax-ppss)))
538 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
540 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
542 (defvar company-backend nil)
543 (make-variable-buffer-local 'company-backend)
545 (defvar company-prefix nil)
546 (make-variable-buffer-local 'company-prefix)
548 (defvar company-candidates nil)
549 (make-variable-buffer-local 'company-candidates)
551 (defvar company-candidates-length nil)
552 (make-variable-buffer-local 'company-candidates-length)
554 (defvar company-candidates-cache nil)
555 (make-variable-buffer-local 'company-candidates-cache)
557 (defvar company-candidates-predicate nil)
558 (make-variable-buffer-local 'company-candidates-predicate)
560 (defvar company-common nil)
561 (make-variable-buffer-local 'company-common)
563 (defvar company-selection 0)
564 (make-variable-buffer-local 'company-selection)
566 (defvar company-selection-changed nil)
567 (make-variable-buffer-local 'company-selection-changed)
569 (defvar company--explicit-action nil
570 "Non-nil, if explicit completion took place.")
571 (make-variable-buffer-local 'company--explicit-action)
573 (defvar company--this-command nil)
575 (defvar company-point nil)
576 (make-variable-buffer-local 'company-point)
578 (defvar company-timer nil)
580 (defvar company-added-newline nil)
581 (make-variable-buffer-local 'company-added-newline)
583 (defsubst company-strip-prefix (str)
584 (substring str (length company-prefix)))
586 (defun company-explicit-action-p ()
587 "Return whether explicit completion action was taken by the user."
588 (or company--explicit-action
589 company-selection-changed))
591 (defsubst company-reformat (candidate)
592 ;; company-ispell needs this, because the results are always lower-case
593 ;; It's mory efficient to fix it only when they are displayed.
594 (concat company-prefix (substring candidate (length company-prefix))))
596 (defsubst company-should-complete (prefix)
597 (and (eq company-idle-delay t)
598 (or (eq t company-begin-commands)
599 (memq company--this-command company-begin-commands)
600 (and (symbolp this-command) (get this-command 'company-begin)))
601 (not (and transient-mark-mode mark-active))
602 (>= (length prefix) company-minimum-prefix-length)))
604 (defsubst company-call-frontends (command)
605 (dolist (frontend company-frontends)
607 (funcall frontend command)
608 (error (error "Company: Front-end %s error \"%s\" on command %s"
609 frontend (error-message-string err) command)))))
611 (defsubst company-set-selection (selection &optional force-update)
612 (setq selection (max 0 (min (1- company-candidates-length) selection)))
613 (when (or force-update (not (equal selection company-selection)))
614 (setq company-selection selection
615 company-selection-changed t)
616 (company-call-frontends 'update)))
618 (defun company-apply-predicate (candidates predicate)
620 (dolist (c candidates)
621 (when (funcall predicate c)
625 (defun company-update-candidates (candidates)
626 (setq company-candidates-length (length candidates))
627 (if (> company-selection 0)
628 ;; Try to restore the selection
629 (let ((selected (nth company-selection company-candidates)))
630 (setq company-selection 0
631 company-candidates candidates)
633 (while (and candidates (string< (pop candidates) selected))
634 (incf company-selection))
636 ;; Make sure selection isn't out of bounds.
637 (setq company-selection (min (1- company-candidates-length)
638 company-selection)))))
639 (setq company-selection 0
640 company-candidates candidates))
642 (push (cons company-prefix company-candidates) company-candidates-cache)
644 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
645 (setq company-common (try-completion company-prefix company-candidates)))
646 (when (eq company-common t)
647 (setq company-candidates nil)))
649 (defun company-calculate-candidates (prefix)
651 (or (cdr (assoc prefix company-candidates-cache))
652 (when company-candidates-cache
653 (let ((len (length prefix))
654 (completion-ignore-case (funcall company-backend
658 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
659 company-candidates-cache)))
660 (return (all-completions prefix prev))))))
661 (let ((c (funcall company-backend 'candidates prefix)))
662 (when company-candidates-predicate
663 (setq c (company-apply-predicate
664 c company-candidates-predicate)))
665 (unless (funcall company-backend 'sorted)
666 (setq c (sort c 'string<)))
668 (if (or (cdr candidates)
669 (not (equal (car candidates) prefix)))
670 ;; Don't start when already completed and unique.
672 ;; Not the right place? maybe when setting?
673 (and company-candidates t))))
675 (defun company-idle-begin (buf win tick pos)
677 (eq buf (current-buffer))
678 (eq win (selected-window))
679 (eq tick (buffer-chars-modified-tick))
681 (not company-candidates)
682 (not (equal (point) company-point))
683 (let ((company-idle-delay t))
685 (when company-candidates
687 (company-post-command)))))
689 (defun company-manual-begin ()
691 (company-assert-enabled)
693 (not company-candidates)
694 (let ((company-idle-delay t)
695 (company-minimum-prefix-length 0)
696 (company-begin-commands t))
697 (setq company--explicit-action t)
699 ;; Return non-nil if active.
702 (defsubst company-incremental-p (old-prefix new-prefix)
703 (and (> (length new-prefix) (length old-prefix))
704 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
706 (defun company-require-match-p ()
707 (let ((backend-value (funcall company-backend 'require-match)))
708 (or (eq backend-value t)
709 (and (if (functionp company-require-match)
710 (funcall company-require-match)
711 (eq company-require-match t))
712 (not (eq backend-value 'never))))))
714 (defun company-punctuation-p (input)
715 "Return non-nil, if input starts with punctuation or parentheses."
716 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
718 (defun company-auto-complete-p (beg end)
719 "Return non-nil, if input starts with punctuation or parentheses."
721 (if (functionp company-auto-complete)
722 (funcall company-auto-complete)
723 company-auto-complete)
724 (if (functionp company-auto-complete-chars)
725 (funcall company-auto-complete-chars (buffer-substring beg end))
726 (if (consp company-auto-complete-chars)
727 (memq (char-syntax (char-after beg)) company-auto-complete-chars)
728 (string-match (buffer-substring beg (1+ beg))
729 company-auto-complete-chars)))))
731 (defun company-continue ()
732 (when company-candidates
733 (when (funcall company-backend 'no-cache company-prefix)
734 ;; Don't complete existing candidates, fetch new ones.
735 (setq company-candidates-cache nil))
736 (let ((new-prefix (funcall company-backend 'prefix)))
737 (unless (and (= (- (point) (length new-prefix))
738 (- company-point (length company-prefix)))
739 (or (equal company-prefix new-prefix)
740 (let ((c (company-calculate-candidates new-prefix)))
741 ;; t means complete/unique.
743 (progn (company-cancel new-prefix) t)
745 (setq company-prefix new-prefix)
746 (company-update-candidates c)
748 (if (company-auto-complete-p company-point (point))
750 (goto-char company-point)
751 (company-complete-selection)
752 (setq company-candidates nil))
753 (if (not (and (company-incremental-p company-prefix new-prefix)
754 (company-require-match-p)))
756 (when (equal company-prefix (car company-candidates))
757 ;; cancel, but last input was actually success
758 (company-cancel company-prefix))
759 (setq company-candidates nil))
760 (backward-delete-char (length new-prefix))
761 (insert company-prefix)
763 (message "Matching input is required")))
764 company-candidates))))
766 (defun company-begin ()
767 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
768 ;; Don't complete in these cases.
769 (setq company-candidates nil)
771 (unless company-candidates
773 (dolist (backend (if company-backend
774 ;; prefer manual override
775 (list company-backend)
776 (cons company-backend company-backends)))
777 (when (and (functionp backend)
778 (setq prefix (funcall backend 'prefix)))
779 (setq company-backend backend)
780 (when (company-should-complete prefix)
781 (let ((c (company-calculate-candidates prefix)))
782 ;; t means complete/unique. We don't start, so no hooks.
784 (setq company-prefix prefix)
785 (company-update-candidates c)
786 (run-hook-with-args 'company-completion-started-hook
787 (company-explicit-action-p))
788 (company-call-frontends 'show))))
790 (if company-candidates
792 (when (and company-end-of-buffer-workaround (eobp))
793 (save-excursion (insert "\n"))
794 (setq company-added-newline (buffer-chars-modified-tick)))
795 (setq company-point (point))
796 (company-enable-overriding-keymap company-active-map)
797 (company-call-frontends 'update))
800 (defun company-cancel (&optional result)
801 (and company-added-newline
802 (> (point-max) (point-min))
803 (let ((tick (buffer-chars-modified-tick)))
804 (delete-region (1- (point-max)) (point-max))
805 (equal tick company-added-newline))
806 ;; Only set unmodified when tick remained the same since insert.
807 (set-buffer-modified-p nil))
810 (run-hook-with-args 'company-completion-finished-hook result)
811 (run-hook-with-args 'company-completion-cancelled-hook result)))
812 (setq company-added-newline nil
815 company-candidates nil
816 company-candidates-length nil
817 company-candidates-cache nil
818 company-candidates-predicate nil
821 company-selection-changed nil
822 company--explicit-action nil
825 (cancel-timer company-timer))
826 (company-search-mode 0)
827 (company-call-frontends 'hide)
828 (company-enable-overriding-keymap nil))
830 (defun company-abort ()
833 ;; Don't start again, unless started manually.
834 (setq company-point (point)))
836 (defun company-finish (result)
837 (insert (company-strip-prefix result))
838 (company-cancel result)
839 ;; Don't start again, unless started manually.
840 (setq company-point (point)))
842 (defsubst company-keep (command)
843 (and (symbolp command) (get command 'company-keep)))
845 (defun company-pre-command ()
846 (unless (company-keep this-command)
848 (when company-candidates
849 (company-call-frontends 'pre-command))
850 (error (message "Company: An error occurred in pre-command")
851 (message "%s" (error-message-string err))
854 (cancel-timer company-timer))
855 (company-uninstall-map))
857 (defun company-post-command ()
858 (unless (company-keep this-command)
861 (setq company--this-command this-command)
862 (unless (equal (point) company-point)
864 (when company-candidates
865 (company-call-frontends 'post-command))
866 (when (numberp company-idle-delay)
868 (run-with-timer company-idle-delay nil 'company-idle-begin
869 (current-buffer) (selected-window)
870 (buffer-chars-modified-tick) (point)))))
871 (error (message "Company: An error occurred in post-command")
872 (message "%s" (error-message-string err))
874 (company-install-map))
876 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
878 (defvar company-search-string nil)
879 (make-variable-buffer-local 'company-search-string)
881 (defvar company-search-lighter " Search: \"\"")
882 (make-variable-buffer-local 'company-search-lighter)
884 (defvar company-search-old-map nil)
885 (make-variable-buffer-local 'company-search-old-map)
887 (defvar company-search-old-selection 0)
888 (make-variable-buffer-local 'company-search-old-selection)
890 (defun company-search (text lines)
891 (let ((quoted (regexp-quote text))
894 (when (string-match quoted line (length company-prefix))
898 (defun company-search-printing-char ()
900 (company-search-assert-enabled)
901 (setq company-search-string
902 (concat (or company-search-string "") (string last-command-event))
903 company-search-lighter (concat " Search: \"" company-search-string
905 (let ((pos (company-search company-search-string
906 (nthcdr company-selection company-candidates))))
909 (company-set-selection (+ company-selection pos) t))))
911 (defun company-search-repeat-forward ()
912 "Repeat the incremental search in completion candidates forward."
914 (company-search-assert-enabled)
915 (let ((pos (company-search company-search-string
916 (cdr (nthcdr company-selection
917 company-candidates)))))
920 (company-set-selection (+ company-selection pos 1) t))))
922 (defun company-search-repeat-backward ()
923 "Repeat the incremental search in completion candidates backwards."
925 (company-search-assert-enabled)
926 (let ((pos (company-search company-search-string
927 (nthcdr (- company-candidates-length
929 (reverse company-candidates)))))
932 (company-set-selection (- company-selection pos 1) t))))
934 (defun company-create-match-predicate ()
935 (setq company-candidates-predicate
937 ,(if company-candidates-predicate
938 `(and (string-match ,company-search-string candidate)
939 (funcall ,company-candidates-predicate
941 `(string-match ,company-search-string candidate))))
942 (company-update-candidates
943 (company-apply-predicate company-candidates company-candidates-predicate))
945 (setq company-candidates-cache (cons company-prefix company-candidates)))
947 (defun company-filter-printing-char ()
949 (company-search-assert-enabled)
950 (company-search-printing-char)
951 (company-create-match-predicate)
952 (company-call-frontends 'update))
954 (defun company-search-kill-others ()
955 "Limit the completion candidates to the ones matching the search string."
957 (company-search-assert-enabled)
958 (company-create-match-predicate)
959 (company-search-mode 0)
960 (company-call-frontends 'update))
962 (defun company-search-abort ()
963 "Abort searching the completion candidates."
965 (company-search-assert-enabled)
966 (company-set-selection company-search-old-selection t)
967 (company-search-mode 0))
969 (defun company-search-other-char ()
971 (company-search-assert-enabled)
972 (company-search-mode 0)
973 (when last-input-event
974 (clear-this-command-keys t)
975 (setq unread-command-events (list last-input-event))))
977 (defvar company-search-map
979 (keymap (make-keymap)))
980 (if (fboundp 'max-char)
981 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
982 'company-search-printing-char)
984 ;; obselete in Emacs 23
985 (let ((l (generic-character-list))
986 (table (nth 1 keymap)))
988 (set-char-table-default table (car l) 'company-search-printing-char)
990 (define-key keymap [t] 'company-search-other-char)
992 (define-key keymap (make-string 1 i) 'company-search-other-char)
995 (define-key keymap (vector i) 'company-search-printing-char)
997 (let ((meta-map (make-sparse-keymap)))
998 (define-key keymap (char-to-string meta-prefix-char) meta-map)
999 (define-key keymap [escape] meta-map))
1000 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1001 (define-key keymap "\e\e\e" 'company-search-other-char)
1002 (define-key keymap [escape escape escape] 'company-search-other-char)
1004 (define-key keymap "\C-g" 'company-search-abort)
1005 (define-key keymap "\C-s" 'company-search-repeat-forward)
1006 (define-key keymap "\C-r" 'company-search-repeat-backward)
1007 (define-key keymap "\C-o" 'company-search-kill-others)
1009 "Keymap used for incrementally searching the completion candidates.")
1011 (define-minor-mode company-search-mode
1012 "Search mode for completion candidates.
1013 Don't start this directly, use `company-search-candidates' or
1014 `company-filter-candidates'."
1015 nil company-search-lighter nil
1016 (if company-search-mode
1017 (if (company-manual-begin)
1019 (setq company-search-old-selection company-selection)
1020 (company-call-frontends 'update))
1021 (setq company-search-mode nil))
1022 (kill-local-variable 'company-search-string)
1023 (kill-local-variable 'company-search-lighter)
1024 (kill-local-variable 'company-search-old-selection)
1025 (company-enable-overriding-keymap company-active-map)))
1027 (defsubst company-search-assert-enabled ()
1028 (company-assert-enabled)
1029 (unless company-search-mode
1030 (company-uninstall-map)
1031 (error "Company not in search mode")))
1033 (defun company-search-candidates ()
1034 "Start searching the completion candidates incrementally.
1036 \\<company-search-map>Search can be controlled with the commands:
1037 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1038 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1039 - `company-search-abort' (\\[company-search-abort])
1041 Regular characters are appended to the search string.
1043 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
1044 the search string to limit the completion candidates."
1046 (company-search-mode 1)
1047 (company-enable-overriding-keymap company-search-map))
1049 (defvar company-filter-map
1050 (let ((keymap (make-keymap)))
1051 (define-key keymap [remap company-search-printing-char]
1052 'company-filter-printing-char)
1053 (set-keymap-parent keymap company-search-map)
1055 "Keymap used for incrementally searching the completion candidates.")
1057 (defun company-filter-candidates ()
1058 "Start filtering the completion candidates incrementally.
1059 This works the same way as `company-search-candidates' immediately
1060 followed by `company-search-kill-others' after each input."
1062 (company-search-mode 1)
1063 (company-enable-overriding-keymap company-filter-map))
1065 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1067 (defun company-select-next ()
1068 "Select the next candidate in the list."
1070 (when (company-manual-begin)
1071 (company-set-selection (1+ company-selection))))
1073 (defun company-select-previous ()
1074 "Select the previous candidate in the list."
1076 (when (company-manual-begin)
1077 (company-set-selection (1- company-selection))))
1079 (defun company-select-mouse (event)
1080 "Select the candidate picked by the mouse."
1082 (when (nth 4 (event-start event))
1083 (company-set-selection (- (cdr (posn-col-row (event-start event)))
1084 (cdr (posn-col-row (posn-at-point)))
1088 (defun company-complete-mouse (event)
1089 "Complete the candidate picked by the mouse."
1091 (when (company-select-mouse event)
1092 (company-complete-selection)))
1094 (defun company-complete-selection ()
1095 "Complete the selected candidate."
1097 (when (company-manual-begin)
1098 (company-finish (nth company-selection company-candidates))))
1100 (defun company-complete-common ()
1101 "Complete the common part of all candidates."
1103 (when (company-manual-begin)
1104 (if (and (not (cdr company-candidates))
1105 (equal company-common (car company-candidates)))
1106 (company-complete-selection)
1107 (insert (company-strip-prefix company-common)))))
1109 (defun company-complete ()
1110 "Complete the common part of all candidates or the current selection.
1111 The first time this is called, the common part is completed, the second time, or
1112 when the selection has been changed, the selected candidate is completed."
1114 (when (company-manual-begin)
1115 (if (or company-selection-changed
1116 (eq last-command 'company-complete-common))
1117 (call-interactively 'company-complete-selection)
1118 (call-interactively 'company-complete-common)
1119 (setq this-command 'company-complete-common))))
1121 (defun company-complete-number (n)
1122 "Complete the Nth candidate."
1123 (when (company-manual-begin)
1124 (and (< n 1) (> n company-candidates-length)
1125 (error "No candidate number %d" n))
1127 (company-finish (nth n company-candidates))))
1129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1131 (defconst company-space-strings-limit 100)
1133 (defconst company-space-strings
1135 (dotimes (i company-space-strings-limit)
1136 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1137 (apply 'vector lst)))
1139 (defsubst company-space-string (len)
1140 (if (< len company-space-strings-limit)
1141 (aref company-space-strings len)
1142 (make-string len ?\ )))
1144 (defsubst company-safe-substring (str from &optional to)
1145 (let ((len (length str)))
1148 (if (and to (> to len))
1149 (concat (substring str from)
1150 (company-space-string (- to len)))
1151 (substring str from to)))))
1153 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1155 (defvar company-last-metadata nil)
1156 (make-variable-buffer-local 'company-last-metadata)
1158 (defun company-fetch-metadata ()
1159 (let ((selected (nth company-selection company-candidates)))
1160 (unless (equal selected (car company-last-metadata))
1161 (setq company-last-metadata
1162 (cons selected (funcall company-backend 'meta selected))))
1163 (cdr company-last-metadata)))
1165 (defun company-doc-buffer (&optional string)
1166 (with-current-buffer (get-buffer-create "*Company meta-data*")
1170 (defmacro company-electric (&rest body)
1171 (declare (indent 0) (debug t))
1172 `(when (company-manual-begin)
1173 (save-window-excursion
1174 (let ((height (window-height))
1175 (row (cdr (posn-col-row (posn-at-point)))))
1177 (and (< (window-height) height)
1178 (< (- (window-height) row 2) company-tooltip-limit)
1179 (recenter (- (window-height) row 2)))
1180 (while (eq 'scroll-other-window
1181 (key-binding (vector (list (read-event)))))
1182 (call-interactively 'scroll-other-window))
1183 (when last-input-event
1184 (clear-this-command-keys t)
1185 (setq unread-command-events (list last-input-event)))))))
1187 (defun company-show-doc-buffer ()
1188 "Temporarily show a buffer with the complete documentation for the selection."
1191 (let ((selected (nth company-selection company-candidates)))
1192 (display-buffer (or (funcall company-backend 'doc-buffer selected)
1193 (error "No documentation available")) t))))
1194 (put 'company-show-doc-buffer 'company-keep t)
1196 (defun company-show-location ()
1197 "Temporarily display a buffer showing the selected candidate in context."
1200 (let* ((selected (nth company-selection company-candidates))
1201 (location (funcall company-backend 'location selected))
1202 (pos (or (cdr location) (error "No location available")))
1203 (buffer (or (and (bufferp (car location)) (car location))
1204 (find-file-noselect (car location) t))))
1205 (with-selected-window (display-buffer buffer t)
1206 (if (bufferp (car location))
1209 (set-window-start nil (point))))))
1210 (put 'company-show-location 'company-keep t)
1212 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1214 (defvar company-callback nil)
1215 (make-variable-buffer-local 'company-callback)
1217 (defun company-remove-callback (&optional ignored)
1218 (remove-hook 'company-completion-finished-hook company-callback t)
1219 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t))
1221 (defun company-begin-backend (backend &optional callback)
1222 "Start a completion at point using BACKEND."
1223 (interactive (let ((val (completing-read "Company back-end: "
1225 'functionp nil "company-")))
1227 (list (intern val)))))
1229 (setq company-callback
1230 `(lambda (completion)
1231 (funcall ',callback completion)
1232 (company-remove-callback)))
1233 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1234 (add-hook 'company-completion-finished-hook company-callback nil t))
1235 (setq company-backend backend)
1236 ;; Return non-nil if active.
1237 (or (company-manual-begin)
1238 (error "Cannot complete at point")))
1240 (defun company-begin-with (candidates
1241 &optional prefix-length require-match callback)
1242 "Start a completion at point.
1243 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
1244 the prefix that already is in the buffer before point. It defaults to 0.
1246 CALLBACK is a function called with the selected result if the user successfully
1247 completes the input.
1250 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1251 (company-begin-backend
1252 (let ((start (- (point) (or prefix-length 0))))
1253 `(lambda (command &optional arg &rest ignored)
1254 (case command-history
1255 ('prefix (message "prefix %s" (buffer-substring ,start (point)))
1256 (when (>= (point) ,start)
1257 (buffer-substring ,start (point))))
1258 ('candidates (all-completions arg ',candidates))
1259 ('require-match ,require-match))))
1262 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1264 (defvar company-pseudo-tooltip-overlay nil)
1265 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1267 (defvar company-tooltip-offset 0)
1268 (make-variable-buffer-local 'company-tooltip-offset)
1270 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1273 (setq company-tooltip-offset
1274 (max (min selection company-tooltip-offset)
1275 (- selection -1 limit)))
1277 (when (<= company-tooltip-offset 1)
1279 (setq company-tooltip-offset 0))
1281 (when (>= company-tooltip-offset (- num-lines limit 1))
1283 (when (= selection (1- num-lines))
1284 (decf company-tooltip-offset)
1285 (when (<= company-tooltip-offset 1)
1286 (setq company-tooltip-offset 0)
1293 (defsubst company-round-tab (arg)
1294 (* (/ (+ arg tab-width) tab-width) tab-width))
1296 (defun company-untabify (str)
1297 (let* ((pieces (split-string str "\t"))
1300 (setcar copy (company-safe-substring
1301 (car copy) 0 (company-round-tab (string-width (car copy)))))
1303 (apply 'concat pieces)))
1305 (defun company-fill-propertize (line width selected)
1306 (setq line (company-safe-substring line 0 width))
1307 (add-text-properties 0 width '(face company-tooltip
1308 mouse-face company-tooltip-mouse)
1310 (add-text-properties 0 (length company-common)
1311 '(face company-tooltip-common
1312 mouse-face company-tooltip-mouse)
1315 (if (and company-search-string
1316 (string-match (regexp-quote company-search-string) line
1317 (length company-prefix)))
1319 (add-text-properties (match-beginning 0) (match-end 0)
1320 '(face company-tooltip-selection)
1322 (when (< (match-beginning 0) (length company-common))
1323 (add-text-properties (match-beginning 0) (length company-common)
1324 '(face company-tooltip-common-selection)
1326 (add-text-properties 0 width '(face company-tooltip-selection
1327 mouse-face company-tooltip-selection)
1329 (add-text-properties 0 (length company-common)
1330 '(face company-tooltip-common-selection
1331 mouse-face company-tooltip-selection)
1337 (defun company-buffer-lines (beg end)
1339 (let ((row (cdr (posn-col-row (posn-at-point))))
1341 (while (and (equal (move-to-window-line (incf row)) row)
1343 (push (buffer-substring beg (min end (1- (point)))) lines)
1345 (unless (eq beg end)
1346 (push (buffer-substring beg end) lines))
1349 (defsubst company-modify-line (old new offset)
1350 (concat (company-safe-substring old 0 offset)
1352 (company-safe-substring old (+ offset (length new)))))
1354 (defun company-replacement-string (old lines column nl)
1356 ;; Inject into old lines.
1358 (push (company-modify-line (pop old) (pop lines) column) new))
1359 ;; Append whole new lines.
1361 (push (concat (company-space-string column) (pop lines)) new))
1362 (concat (when nl "\n")
1363 (mapconcat 'identity (nreverse new) "\n")
1366 (defun company-create-lines (column selection limit)
1368 (let ((len company-candidates-length)
1377 ;; Scroll to offset.
1378 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1380 (when (> company-tooltip-offset 0)
1381 (setq previous (format "...(%d)" company-tooltip-offset)))
1383 (setq remainder (- len limit company-tooltip-offset)
1384 remainder (when (> remainder 0)
1385 (setq remainder (format "...(%d)" remainder))))
1387 (decf selection company-tooltip-offset)
1388 (setq width (min (length previous) (length remainder))
1389 lines (nthcdr company-tooltip-offset company-candidates)
1394 (setq width (max (length (pop lines-copy)) width)))
1395 (setq width (min width (- (window-width) column)))
1397 (setq lines-copy lines)
1399 ;; number can make tooltip too long
1400 (and company-show-numbers
1401 (< (setq numbered company-tooltip-offset) 10)
1405 (push (propertize (company-safe-substring previous 0 width)
1406 'face 'company-tooltip)
1410 (push (company-fill-propertize
1411 (if (>= numbered 10)
1412 (company-reformat (pop lines))
1415 (company-safe-substring (company-reformat (pop lines))
1418 width (equal i selection))
1422 (push (propertize (company-safe-substring remainder 0 width)
1423 'face 'company-tooltip)
1426 (setq lines (nreverse new))))
1430 (defsubst company-pseudo-tooltip-height ()
1431 "Calculate the appropriate tooltip height."
1432 (max 3 (min company-tooltip-limit
1433 (- (window-height) 2
1434 (count-lines (window-start) (point-at-bol))))))
1436 (defun company-pseudo-tooltip-show (row column selection)
1437 (company-pseudo-tooltip-hide)
1442 (let* ((height (company-pseudo-tooltip-height))
1443 (lines (company-create-lines column selection height))
1444 (nl (< (move-to-window-line row) row))
1446 (end (save-excursion
1447 (move-to-window-line (+ row height))
1450 (mapcar 'company-untabify (company-buffer-lines beg end)))
1453 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1455 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1456 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1457 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1458 (overlay-put company-pseudo-tooltip-overlay 'company-before
1459 (company-replacement-string old-string lines column nl))
1460 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1462 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1464 (defun company-pseudo-tooltip-show-at-point (pos)
1465 (let ((col-row (posn-col-row (posn-at-point pos))))
1466 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1468 (defun company-pseudo-tooltip-edit (lines selection)
1469 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1470 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1471 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1472 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1473 (lines (company-create-lines column selection height)))
1474 (overlay-put company-pseudo-tooltip-overlay 'company-before
1475 (company-replacement-string old-string lines column nl))))
1477 (defun company-pseudo-tooltip-hide ()
1478 (when company-pseudo-tooltip-overlay
1479 (delete-overlay company-pseudo-tooltip-overlay)
1480 (setq company-pseudo-tooltip-overlay nil)))
1482 (defun company-pseudo-tooltip-hide-temporarily ()
1483 (when (overlayp company-pseudo-tooltip-overlay)
1484 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1485 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1487 (defun company-pseudo-tooltip-unhide ()
1488 (when company-pseudo-tooltip-overlay
1489 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1490 (overlay-put company-pseudo-tooltip-overlay 'before-string
1491 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1492 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1494 (defun company-pseudo-tooltip-frontend (command)
1495 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1497 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1499 (unless (and (overlayp company-pseudo-tooltip-overlay)
1500 (equal (overlay-get company-pseudo-tooltip-overlay
1502 (company-pseudo-tooltip-height)))
1504 (company-pseudo-tooltip-show-at-point (- (point)
1505 (length company-prefix))))
1506 (company-pseudo-tooltip-unhide))
1507 ('hide (company-pseudo-tooltip-hide)
1508 (setq company-tooltip-offset 0))
1509 ('update (when (overlayp company-pseudo-tooltip-overlay)
1510 (company-pseudo-tooltip-edit company-candidates
1511 company-selection)))))
1513 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1514 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1515 (unless (and (eq command 'post-command)
1516 (not (cdr company-candidates)))
1517 (company-pseudo-tooltip-frontend command)))
1519 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1521 (defvar company-preview-overlay nil)
1522 (make-variable-buffer-local 'company-preview-overlay)
1524 (defun company-preview-show-at-point (pos)
1525 (company-preview-hide)
1527 (setq company-preview-overlay (make-overlay pos pos))
1529 (let ((completion(nth company-selection company-candidates)))
1530 (setq completion (propertize completion 'face 'company-preview))
1531 (add-text-properties 0 (length company-common)
1532 '(face company-preview-common) completion)
1534 ;; Add search string
1535 (and company-search-string
1536 (string-match (regexp-quote company-search-string) completion)
1537 (add-text-properties (match-beginning 0)
1539 '(face company-preview-search)
1542 (setq completion (company-strip-prefix completion))
1544 (and (equal pos (point))
1545 (not (equal completion ""))
1546 (add-text-properties 0 1 '(cursor t) completion))
1548 (overlay-put company-preview-overlay 'after-string completion)
1549 (overlay-put company-preview-overlay 'window (selected-window))))
1551 (defun company-preview-hide ()
1552 (when company-preview-overlay
1553 (delete-overlay company-preview-overlay)
1554 (setq company-preview-overlay nil)))
1556 (defun company-preview-frontend (command)
1557 "A `company-mode' front-end showing the selection as if it had been inserted."
1559 ('pre-command (company-preview-hide))
1560 ('post-command (company-preview-show-at-point (point)))
1561 ('hide (company-preview-hide))))
1563 (defun company-preview-if-just-one-frontend (command)
1564 "`company-preview-frontend', but only shown for single candidates."
1565 (unless (and (eq command 'post-command)
1566 (cdr company-candidates))
1567 (company-preview-frontend command)))
1569 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1571 (defvar company-echo-last-msg nil)
1572 (make-variable-buffer-local 'company-echo-last-msg)
1574 (defvar company-echo-timer nil)
1576 (defvar company-echo-delay .1)
1578 (defun company-echo-show (&optional getter)
1580 (setq company-echo-last-msg (funcall getter)))
1581 (let ((message-log-max nil))
1582 (if company-echo-last-msg
1583 (message "%s" company-echo-last-msg)
1586 (defsubst company-echo-show-soon (&optional getter)
1587 (when company-echo-timer
1588 (cancel-timer company-echo-timer))
1589 (setq company-echo-timer (run-with-timer company-echo-delay nil
1590 'company-echo-show getter)))
1592 (defun company-echo-format ()
1594 (let ((limit (window-width (minibuffer-window)))
1596 ;; Roll to selection.
1597 (candidates (nthcdr company-selection company-candidates))
1598 (i (if company-show-numbers company-selection 99999))
1602 (setq comp (company-reformat (pop candidates))
1603 len (+ len 1 (length comp)))
1607 (setq comp (propertize (format "%d: %s" i comp)
1608 'face 'company-echo))
1611 (add-text-properties 3 (+ 3 (length company-common))
1612 '(face company-echo-common) comp))
1613 (setq comp (propertize comp 'face 'company-echo))
1614 (add-text-properties 0 (length company-common)
1615 '(face company-echo-common) comp))
1617 (setq candidates nil)
1620 (mapconcat 'identity (nreverse msg) " ")))
1622 (defun company-echo-strip-common-format ()
1624 (let ((limit (window-width (minibuffer-window)))
1625 (len (+ (length company-prefix) 2))
1626 ;; Roll to selection.
1627 (candidates (nthcdr company-selection company-candidates))
1628 (i (if company-show-numbers company-selection 99999))
1632 (setq comp (company-strip-prefix (pop candidates))
1633 len (+ len 2 (length comp)))
1636 (setq comp (format "%s (%d)" comp i))
1640 (setq candidates nil)
1641 (push (propertize comp 'face 'company-echo) msg)))
1643 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1644 (mapconcat 'identity (nreverse msg) ", ")
1647 (defun company-echo-hide ()
1648 (when company-echo-timer
1649 (cancel-timer company-echo-timer))
1650 (unless (equal company-echo-last-msg "")
1651 (setq company-echo-last-msg "")
1652 (company-echo-show)))
1654 (defun company-echo-frontend (command)
1655 "A `company-mode' front-end showing the candidates in the echo area."
1657 ('pre-command (company-echo-show-soon))
1658 ('post-command (company-echo-show-soon 'company-echo-format))
1659 ('hide (company-echo-hide))))
1661 (defun company-echo-strip-common-frontend (command)
1662 "A `company-mode' front-end showing the candidates in the echo area."
1664 ('pre-command (company-echo-show-soon))
1665 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1666 ('hide (company-echo-hide))))
1668 (defun company-echo-metadata-frontend (command)
1669 "A `company-mode' front-end showing the documentation in the echo area."
1671 ('pre-command (company-echo-show-soon))
1672 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1673 ('hide (company-echo-hide))))
1676 ;;; company.el ends here