1 ;;; company.el --- Modular in-buffer completion framework -*- lexical-binding: t -*-
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
6 ;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
8 ;; Keywords: abbrev, convenience, matching
9 ;; URL: http://company-mode.github.io/
10 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x, GNU Emacs 24.x
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; Company is a modular completion mechanism. Modules for retrieving completion
30 ;; candidates are called back-ends, modules for displaying them are front-ends.
32 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
33 ;; distributed in separate files and can be used individually.
35 ;; Place company.el and the back-ends you want to use in a directory and add the
36 ;; following to your .emacs:
37 ;; (add-to-list 'load-path "/path/to/company")
38 ;; (autoload 'company-mode "company" nil t)
40 ;; Enable company-mode with M-x company-mode. For further information look at
41 ;; the documentation for `company-mode' (C-h f company-mode RET)
43 ;; If you want to start a specific back-end, call it interactively or use
44 ;; `company-begin-backend'. For example:
45 ;; M-x company-abbrev will prompt for and insert an abbrev.
47 ;; To write your own back-end, look at the documentation for `company-backends'.
48 ;; Here is a simple example completing "foo":
50 ;; (defun company-my-backend (command &optional arg &rest ignored)
52 ;; (prefix (when (looking-back "foo\\>")
54 ;; (candidates (list "foobar" "foobaz" "foobarbaz"))
55 ;; (meta (format "This value is named %s" arg))))
57 ;; Sometimes it is a good idea to mix several back-ends together, for example to
58 ;; enrich gtags with dabbrev-code results (to emulate local variables).
59 ;; To do this, add a list with both back-ends as an element in company-backends.
62 ;; When point is at the very end of the buffer, the pseudo-tooltip appears very
63 ;; wrong, unless company is allowed to temporarily insert a fake newline.
64 ;; This behavior is enabled by `company-end-of-buffer-workaround'.
68 ;; See NEWS.md in the repository.
72 (eval-when-compile (require 'cl))
75 ;; FIXME: Use `user-error'.
76 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
77 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
78 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
79 (add-to-list 'debug-ignored-errors "^Company not ")
80 (add-to-list 'debug-ignored-errors "^No candidate number ")
81 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
82 (add-to-list 'debug-ignored-errors "^No other back-end$")
85 "Extensible inline text completion mechanism"
90 (defface company-tooltip
91 '((default :foreground "black")
92 (((class color) (min-colors 88) (background light))
93 (:background "cornsilk"))
94 (((class color) (min-colors 88) (background dark))
95 (:background "yellow")))
96 "Face used for the tooltip.")
98 (defface company-tooltip-selection
99 '((default :inherit company-tooltip)
100 (((class color) (min-colors 88) (background light))
101 (:background "light blue"))
102 (((class color) (min-colors 88) (background dark))
103 (:background "orange1"))
104 (t (:background "green")))
105 "Face used for the selection in the tooltip.")
107 (defface company-tooltip-mouse
108 '((default :inherit highlight))
109 "Face used for the tooltip item under the mouse.")
111 (defface company-tooltip-common
112 '((default :inherit company-tooltip)
113 (((background light))
114 :foreground "darkred")
117 "Face used for the common completion in the tooltip.")
119 (defface company-tooltip-common-selection
120 '((default :inherit company-tooltip-selection)
121 (((background light))
122 :foreground "darkred")
125 "Face used for the selected common completion in the tooltip.")
127 (defface company-tooltip-annotation
128 '((default :inherit company-tooltip)
129 (((background light))
130 :foreground "firebrick4")
133 "Face used for the annotation in the tooltip.")
135 (defface company-scrollbar-fg
136 '((((background light))
137 :background "darkred")
140 "Face used for the tooltip scrollbar thumb.")
142 (defface company-scrollbar-bg
143 '((default :inherit company-tooltip)
144 (((background light))
148 "Face used for the tooltip scrollbar background.")
150 (defface company-preview
151 '((((background light))
152 :inherit company-tooltip-selection)
155 :foreground "wheat"))
156 "Face used for the completion preview.")
158 (defface company-preview-common
159 '((((background light))
160 :inherit company-tooltip-selection)
162 :inherit company-preview
164 "Face used for the common part of the completion preview.")
166 (defface company-preview-search
167 '((((background light))
168 :inherit company-tooltip-common-selection)
170 :inherit company-preview
171 :background "blue1"))
172 "Face used for the search string in the completion preview.")
174 (defface company-echo nil
175 "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.")
182 (defun company-frontends-set (variable value)
184 (let ((remainder value))
185 (setcdr remainder (delq (car remainder) (cdr remainder))))
186 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
187 (memq 'company-pseudo-tooltip-frontend value)
188 (error "Pseudo tooltip frontend cannot be used twice"))
189 (and (memq 'company-preview-if-just-one-frontend value)
190 (memq 'company-preview-frontend value)
191 (error "Preview frontend cannot be used twice"))
192 (and (memq 'company-echo value)
193 (memq 'company-echo-metadata-frontend value)
194 (error "Echo area cannot be used twice"))
195 ;; preview must come last
196 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
198 (setq value (append (delq f value) (list f)))))
199 (set variable value))
201 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
202 company-preview-if-just-one-frontend
203 company-echo-metadata-frontend)
204 "The list of active front-ends (visualizations).
205 Each front-end is a function that takes one argument. It is called with
206 one of the following arguments:
208 `show': When the visualization should start.
210 `hide': When the visualization should end.
212 `update': When the data has been updated.
214 `pre-command': Before every command that is executed while the
215 visualization is active.
217 `post-command': After every command that is executed while the
218 visualization is active.
220 The visualized data is stored in `company-prefix', `company-candidates',
221 `company-common', `company-selection', `company-point' and
222 `company-search-string'."
223 :set 'company-frontends-set
224 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
225 (const :tag "echo, strip common"
226 company-echo-strip-common-frontend)
227 (const :tag "show echo meta-data in echo"
228 company-echo-metadata-frontend)
229 (const :tag "pseudo tooltip"
230 company-pseudo-tooltip-frontend)
231 (const :tag "pseudo tooltip, multiple only"
232 company-pseudo-tooltip-unless-just-one-frontend)
233 (const :tag "preview" company-preview-frontend)
234 (const :tag "preview, unique only"
235 company-preview-if-just-one-frontend)
236 (function :tag "custom function" nil))))
238 (defcustom company-tooltip-limit 10
239 "The maximum number of candidates in the tooltip"
242 (defcustom company-tooltip-minimum 6
243 "The minimum height of the tooltip.
244 If this many lines are not available, prefer to display the tooltip above."
247 (defcustom company-tooltip-margin 1
248 "Width of margin columns to show around the toolip."
251 (defcustom company-tooltip-offset-display 'scrollbar
252 "Method using which the tooltip displays scrolling position.
253 `scrollbar' means draw a scrollbar to the right of the items.
254 `lines' means wrap items in lines with \"before\" and \"after\" counters."
255 :type '(choice (const :tag "Scrollbar" scrollbar)
256 (const :tag "Two lines" lines)))
258 (defcustom company-tooltip-align-annotations nil
259 "When non-nil, align annotations to the right tooltip border."
262 (defvar company-safe-backends
263 '((company-abbrev . "Abbrev")
264 (company-bbdb . "BBDB")
265 (company-capf . "completion-at-point-functions")
266 (company-clang . "Clang")
267 (company-cmake . "CMake")
268 (company-css . "CSS")
269 (company-dabbrev . "dabbrev for plain text")
270 (company-dabbrev-code . "dabbrev for code")
271 (company-eclim . "Eclim (an Eclipse interface)")
272 (company-elisp . "Emacs Lisp")
273 (company-etags . "etags")
274 (company-files . "Files")
275 (company-gtags . "GNU Global")
276 (company-ispell . "Ispell")
277 (company-keywords . "Programming language keywords")
278 (company-nxml . "nxml")
279 (company-oddmuse . "Oddmuse")
280 (company-pysmell . "PySmell")
281 (company-ropemacs . "ropemacs")
282 (company-semantic . "Semantic")
283 (company-tempo . "Tempo templates")
284 (company-xcode . "Xcode")))
285 (put 'company-safe-backends 'risky-local-variable t)
287 (defun company-safe-backends-p (backends)
288 (and (consp backends)
289 (not (dolist (backend backends)
290 (unless (if (consp backend)
291 (company-safe-backends-p backend)
292 (assq backend company-safe-backends))
295 (defvar company--include-capf (version< "24.3.50" emacs-version))
297 (defcustom company-backends `(,@(unless company--include-capf
298 (list 'company-elisp))
300 company-nxml company-css
301 company-eclim company-semantic company-clang
302 company-xcode company-ropemacs company-cmake
303 ,@(when company--include-capf
304 (list 'company-capf))
305 (company-gtags company-etags company-dabbrev-code
307 company-oddmuse company-files company-dabbrev)
308 "The list of active back-ends (completion engines).
309 Each list elements can itself be a list of back-ends. In that case their
310 completions are merged. Otherwise only the first matching back-end returns
313 `company-begin-backend' can be used to start a specific back-end,
314 `company-other-backend' will skip to the next matching back-end in the list.
316 Each back-end is a function that takes a variable number of arguments.
317 The first argument is the command requested from the back-end. It is one
320 `prefix': The back-end should return the text to be completed. It must be
321 text immediately before point. Returning nil passes control to the next
322 back-end. The function should return `stop' if it should complete but
323 cannot \(e.g. if it is in the middle of a string\). Instead of a string,
324 the back-end may return a cons where car is the prefix and cdr is used in
325 `company-minimum-prefix-length' test. It must be either number or t, and
326 in the latter case the test automatically succeeds.
328 `candidates': The second argument is the prefix to be completed. The
329 return value should be a list of candidates that match the prefix.
331 Non-prefix matches are also supported (candidates that don't start with the
332 prefix, but match it in some backend-defined way). Backends that use this
333 feature must disable cache (return t to `no-cache') and should also respond
338 `sorted': Return t here to indicate that the candidates are sorted and will
339 not need to be sorted again.
341 `duplicates': If non-nil, company will take care of removing duplicates
344 `no-cache': Usually company doesn't ask for candidates again as completion
345 progresses, unless the back-end returns t for this command. The second
346 argument is the latest prefix.
348 `meta': The second argument is a completion candidate. Return a (short)
349 documentation string for it.
351 `doc-buffer': The second argument is a completion candidate. Return a
352 buffer with documentation for it. Preferably use `company-doc-buffer',
354 `location': The second argument is a completion candidate. Return the cons
355 of buffer and buffer location, or of file and line number where the
356 completion candidate was defined.
358 `annotation': The second argument is a completion candidate. Return a
359 string to be displayed inline with the candidate in the popup. If
360 duplicates are removed by company, candidates with equal string values will
361 be kept if they have different annotations. For that to work properly,
362 backends should store the related information on candidates using text
365 `match': The second argument is a completion candidate. Backends that
366 provide non-prefix completions should return the position of the end of
367 text in the candidate that matches `prefix'. It will be used when
370 `require-match': If this returns t, the user is not allowed to enter
371 anything not offered as a candidate. Use with care! The default value nil
372 gives the user that choice with `company-require-match'. Return value
373 `never' overrides that option the other way around.
375 `init': Called once for each buffer. The back-end can check for external
376 programs and files and load any required libraries. Raising an error here
377 will show up in message log once, and the back-end will not be used for
380 `post-completion': Called after a completion candidate has been inserted
381 into the buffer. The second argument is the candidate. Can be used to
382 modify it, e.g. to expand a snippet.
384 The back-end should return nil for all commands it does not support or
385 does not know about. It should also be callable interactively and use
386 `company-begin-backend' to start itself in that case."
390 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
391 company-safe-backends)
392 (symbol :tag "User defined")
393 (repeat :tag "Merged Back-ends"
394 (choice :tag "Back-end"
395 ,@(mapcar (lambda (b)
396 `(const :tag ,(cdr b) ,(car b)))
397 company-safe-backends)
398 (symbol :tag "User defined"))))))
400 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
402 (defcustom company-transformers nil
403 "Functions to change the list of candidates received from backends,
404 after sorting and removal of duplicates (if appropriate).
405 Each function gets called with the return value of the previous one."
407 (const :tag "None" nil)
408 (const :tag "Sort by occurrence" (company-sort-by-occurrence))
409 (repeat :tag "User defined" (function))))
411 (defcustom company-completion-started-hook nil
412 "Hook run when company starts completing.
413 The hook is called with one argument that is non-nil if the completion was
417 (defcustom company-completion-cancelled-hook nil
418 "Hook run when company cancels completing.
419 The hook is called with one argument that is non-nil if the completion was
423 (defcustom company-completion-finished-hook nil
424 "Hook run when company successfully completes.
425 The hook is called with the selected candidate as an argument.
427 If you indend to use it to post-process candidates from a specific
428 back-end, consider using the `post-completion' command instead."
431 (defcustom company-minimum-prefix-length 3
432 "The minimum prefix length for idle completion."
433 :type '(integer :tag "prefix length"))
435 (defcustom company-require-match 'company-explicit-action-p
436 "If enabled, disallow non-matching input.
437 This can be a function do determine if a match is required.
439 This can be overridden by the back-end, if it returns t or `never' to
440 `require-match'. `company-auto-complete' also takes precedence over this."
441 :type '(choice (const :tag "Off" nil)
442 (function :tag "Predicate function")
443 (const :tag "On, if user interaction took place"
444 'company-explicit-action-p)
445 (const :tag "On" t)))
447 (defcustom company-auto-complete nil
448 "Determines when to auto-complete.
449 If this is enabled, all characters from `company-auto-complete-chars'
450 trigger insertion of the selected completion candidate.
451 This can also be a function."
452 :type '(choice (const :tag "Off" nil)
453 (function :tag "Predicate function")
454 (const :tag "On, if user interaction took place"
455 'company-explicit-action-p)
456 (const :tag "On" t)))
458 (defcustom company-auto-complete-chars '(?\ ?\) ?.)
459 "Determines which characters trigger auto-completion.
460 See `company-auto-complete'. If this is a string, each string character
461 tiggers auto-completion. If it is a list of syntax description characters (see
462 `modify-syntax-entry'), all characters with that syntax auto-complete.
464 This can also be a function, which is called with the new input and should
465 return non-nil if company should auto-complete.
467 A character that is part of a valid candidate never triggers auto-completion."
468 :type '(choice (string :tag "Characters")
470 (const :tag "Whitespace" ?\ )
471 (const :tag "Symbol" ?_)
472 (const :tag "Opening parentheses" ?\()
473 (const :tag "Closing parentheses" ?\))
474 (const :tag "Word constituent" ?w)
475 (const :tag "Punctuation." ?.)
476 (const :tag "String quote." ?\")
477 (const :tag "Paired delimiter." ?$)
478 (const :tag "Expression quote or prefix operator." ?\')
479 (const :tag "Comment starter." ?<)
480 (const :tag "Comment ender." ?>)
481 (const :tag "Character-quote." ?/)
482 (const :tag "Generic string fence." ?|)
483 (const :tag "Generic comment fence." ?!))
484 (function :tag "Predicate function")))
486 (defcustom company-idle-delay .7
487 "The idle delay in seconds until completion starts automatically.
488 A value of nil means no idle completion, t means show candidates
489 immediately when a prefix of `company-minimum-prefix-length' is reached."
490 :type '(choice (const :tag "never (nil)" nil)
491 (const :tag "immediate (t)" t)
492 (number :tag "seconds")))
494 (defcustom company-begin-commands '(self-insert-command org-self-insert-command)
495 "A list of commands after which idle completion is allowed.
496 If this is t, it can show completions after any command. See
497 `company-idle-delay'.
499 Alternatively, any command with a non-nil `company-begin' property is
500 treated as if it was on this list."
501 :type '(choice (const :tag "Any command" t)
502 (const :tag "Self insert command" '(self-insert-command))
503 (repeat :tag "Commands" function)))
505 (defcustom company-continue-commands '(not save-buffer save-some-buffers
506 save-buffers-kill-terminal
507 save-buffers-kill-emacs)
508 "A list of commands that are allowed during completion.
509 If this is t, or if `company-begin-commands' is t, any command is allowed.
510 Otherwise, the value must be a list of symbols. If it starts with `not',
511 the cdr is the list of commands that abort completion. Otherwise, all
512 commands except those in that list, or in `company-begin-commands', or
513 commands in the `company-' namespace, abort completion."
514 :type '(choice (const :tag "Any command" t)
515 (cons :tag "Any except"
517 (repeat :tag "Commands" function))
518 (repeat :tag "Commands" function)))
520 (defcustom company-show-numbers nil
521 "If enabled, show quick-access numbers for the first ten candidates."
522 :type '(choice (const :tag "off" nil)
523 (const :tag "on" t)))
525 (defcustom company-selection-wrap-around nil
526 "If enabled, selecting item before first or after last wraps around."
527 :type '(choice (const :tag "off" nil)
528 (const :tag "on" t)))
530 (defvar company-end-of-buffer-workaround t
531 "Work around a visualization bug when completing at the end of the buffer.
532 The work-around consists of adding a newline.")
534 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
536 (defvar company-mode-map (make-sparse-keymap)
537 "Keymap used by `company-mode'.")
539 (defvar company-active-map
540 (let ((keymap (make-sparse-keymap)))
541 (define-key keymap "\e\e\e" 'company-abort)
542 (define-key keymap "\C-g" 'company-abort)
543 (define-key keymap (kbd "M-n") 'company-select-next)
544 (define-key keymap (kbd "M-p") 'company-select-previous)
545 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
546 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
547 (define-key keymap [down-mouse-1] 'ignore)
548 (define-key keymap [down-mouse-3] 'ignore)
549 (define-key keymap [mouse-1] 'company-complete-mouse)
550 (define-key keymap [mouse-3] 'company-select-mouse)
551 (define-key keymap [up-mouse-1] 'ignore)
552 (define-key keymap [up-mouse-3] 'ignore)
553 (define-key keymap [return] 'company-complete-selection)
554 (define-key keymap (kbd "RET") 'company-complete-selection)
555 (define-key keymap [tab] 'company-complete-common)
556 (define-key keymap (kbd "TAB") 'company-complete-common)
557 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
558 (define-key keymap "\C-w" 'company-show-location)
559 (define-key keymap "\C-s" 'company-search-candidates)
560 (define-key keymap "\C-\M-s" 'company-filter-candidates)
562 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
563 `(lambda () (interactive) (company-complete-number ,i))))
566 "Keymap that is enabled during an active completion.")
568 (defvar company--disabled-backends nil)
570 (defun company-init-backend (backend)
571 (and (symbolp backend)
572 (not (fboundp backend))
573 (ignore-errors (require backend nil t)))
575 (if (or (symbolp backend)
579 (funcall backend 'init)
580 (put backend 'company-init t))
582 (put backend 'company-init 'failed)
583 (unless (memq backend company--disabled-backends)
584 (message "Company back-end '%s' could not be initialized:\n%s"
585 backend (error-message-string err)))
586 (pushnew backend company--disabled-backends)
588 (mapc 'company-init-backend backend)))
590 (defvar company-default-lighter " company")
592 (defvar company-lighter company-default-lighter)
593 (make-variable-buffer-local 'company-lighter)
596 (define-minor-mode company-mode
597 "\"complete anything\"; is an in-buffer completion framework.
598 Completion starts automatically, depending on the values
599 `company-idle-delay' and `company-minimum-prefix-length'.
601 Completion can be controlled with the commands:
602 `company-complete-common', `company-complete-selection', `company-complete',
603 `company-select-next', `company-select-previous'. If these commands are
604 called before `company-idle-delay', completion will also start.
606 Completions can be searched with `company-search-candidates' or
607 `company-filter-candidates'. These can be used while completion is
610 The completion data is retrieved using `company-backends' and displayed
611 using `company-frontends'. If you want to start a specific back-end, call
612 it interactively or use `company-begin-backend'.
614 regular keymap (`company-mode-map'):
617 keymap during active completions (`company-active-map'):
619 \\{company-active-map}"
620 nil company-lighter company-mode-map
623 (add-hook 'pre-command-hook 'company-pre-command nil t)
624 (add-hook 'post-command-hook 'company-post-command nil t)
625 (mapc 'company-init-backend company-backends))
626 (remove-hook 'pre-command-hook 'company-pre-command t)
627 (remove-hook 'post-command-hook 'company-post-command t)
629 (kill-local-variable 'company-point)))
631 (defcustom company-global-modes t
632 "Modes for which `company-mode' mode is turned on by `global-company-mode'.
633 If nil, means no modes. If t, then all major modes have it turned on.
634 If a list, it should be a list of `major-mode' symbol names for which
635 `company-mode' should be automatically turned on. The sense of the list is
636 negated if it begins with `not'. For example:
638 means that `company-mode' is turned on for buffers in C and C++ modes only.
640 means that `company-mode' is always turned on except in `message-mode' buffers."
641 :type '(choice (const :tag "none" nil)
643 (set :menu-tag "mode specific" :tag "modes"
645 (const :tag "Except" not)
646 (repeat :inline t (symbol :tag "mode")))))
649 (define-globalized-minor-mode global-company-mode company-mode company-mode-on)
651 (defun company-mode-on ()
652 (when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s)))
653 (cond ((eq company-global-modes t)
655 ((eq (car-safe company-global-modes) 'not)
656 (not (memq major-mode (cdr company-global-modes))))
657 (t (memq major-mode company-global-modes))))
660 (defsubst company-assert-enabled ()
662 (company-uninstall-map)
663 (error "Company not enabled")))
665 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
667 (defvar company-my-keymap nil)
668 (make-variable-buffer-local 'company-my-keymap)
670 (defvar company-emulation-alist '((t . nil)))
672 (defsubst company-enable-overriding-keymap (keymap)
673 (company-uninstall-map)
674 (setq company-my-keymap keymap))
676 (defun company-ensure-emulation-alist ()
677 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
678 (setq emulation-mode-map-alists
679 (cons 'company-emulation-alist
680 (delq 'company-emulation-alist emulation-mode-map-alists)))))
682 (defun company-install-map ()
683 (unless (or (cdar company-emulation-alist)
684 (null company-my-keymap))
685 (setf (cdar company-emulation-alist) company-my-keymap)))
687 (defun company-uninstall-map ()
688 (setf (cdar company-emulation-alist) nil))
691 ;; Emacs calculates the active keymaps before reading the event. That means we
692 ;; cannot change the keymap from a timer. So we send a bogus command.
693 ;; XXX: Seems not to be needed anymore in Emacs 24.4
694 (defun company-ignore ()
696 (setq this-command last-command))
698 (global-set-key '[31415926] 'company-ignore)
700 (defun company-input-noop ()
701 (push 31415926 unread-command-events))
703 (defun company--column (&optional pos)
705 (when pos (goto-char pos))
709 (narrow-to-region (point) (point-max))
710 (let ((prefix (get-text-property (point) 'line-prefix)))
711 (if prefix (length prefix) 0)))
714 (defun company--row (&optional pos)
716 (when pos (goto-char pos))
717 (count-screen-lines (window-start)
718 (progn (vertical-motion 0) (point)))))
720 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
722 (defvar company-backend nil)
723 (make-variable-buffer-local 'company-backend)
725 (defun company-grab (regexp &optional expression limit)
726 (when (looking-back regexp limit)
727 (or (match-string-no-properties (or expression 0)) "")))
729 (defun company-grab-line (regexp &optional expression)
730 (company-grab regexp expression (point-at-bol)))
732 (defun company-grab-symbol ()
733 (if (looking-at "\\_>")
734 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
736 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
739 (defun company-grab-word ()
740 (if (looking-at "\\>")
741 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
743 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
746 (defun company-in-string-or-comment ()
747 (let ((ppss (syntax-ppss)))
748 (or (car (setq ppss (nthcdr 3 ppss)))
749 (car (setq ppss (cdr ppss)))
752 (if (fboundp 'locate-dominating-file)
753 (defalias 'company-locate-dominating-file 'locate-dominating-file)
754 (defun company-locate-dominating-file (file name)
756 (let ((dir (file-name-directory file))
758 (while (not (equal dir prev-dir))
759 (when (file-exists-p (expand-file-name name dir))
762 dir (file-name-directory (directory-file-name dir))))))))
764 (defun company-call-backend (&rest args)
766 (if (functionp company-backend)
767 (apply company-backend args)
768 (apply 'company--multi-backend-adapter company-backend args))
769 (error (error "Company: Back-end %s error \"%s\" with args %s"
770 company-backend (error-message-string err) args))))
772 (defun company--multi-backend-adapter (backends command &rest args)
773 (let ((backends (loop for b in backends
774 when (not (and (symbolp b)
775 (eq 'failed (get b 'company-init))))
779 (loop for backend in backends
780 when (equal (funcall backend 'prefix)
782 append (apply backend 'candidates args)))
787 (dolist (backend backends)
788 (when (setq value (apply backend command args))
789 (return value))))))))
791 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
793 (defvar company-prefix nil)
794 (make-variable-buffer-local 'company-prefix)
796 (defvar company-candidates nil)
797 (make-variable-buffer-local 'company-candidates)
799 (defvar company-candidates-length nil)
800 (make-variable-buffer-local 'company-candidates-length)
802 (defvar company-candidates-cache nil)
803 (make-variable-buffer-local 'company-candidates-cache)
805 (defvar company-candidates-predicate nil)
806 (make-variable-buffer-local 'company-candidates-predicate)
808 (defvar company-common nil)
809 (make-variable-buffer-local 'company-common)
811 (defvar company-selection 0)
812 (make-variable-buffer-local 'company-selection)
814 (defvar company-selection-changed nil)
815 (make-variable-buffer-local 'company-selection-changed)
817 (defvar company--explicit-action nil
818 "Non-nil, if explicit completion took place.")
819 (make-variable-buffer-local 'company--explicit-action)
821 (defvar company--auto-completion nil
822 "Non-nil when current candidate is being inserted automatically.
823 Controlled by `company-auto-complete'.")
825 (defvar company--point-max nil)
826 (make-variable-buffer-local 'company--point-max)
828 (defvar company-point nil)
829 (make-variable-buffer-local 'company-point)
831 (defvar company-timer nil)
833 (defvar company-added-newline nil)
834 (make-variable-buffer-local 'company-added-newline)
836 (defsubst company-strip-prefix (str)
837 (substring str (length company-prefix)))
839 (defun company--insert-candidate (candidate)
840 (setq candidate (substring-no-properties candidate))
841 ;; XXX: Return value we check here is subject to change.
842 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
843 (insert (company-strip-prefix candidate))
844 (delete-region (- (point) (length company-prefix)) (point))
847 (defmacro company-with-candidate-inserted (candidate &rest body)
848 "Evaluate BODY with CANDIDATE temporarily inserted.
849 This is a tool for back-ends that need candidates inserted before they
850 can retrieve meta-data for them."
852 `(let ((inhibit-modification-hooks t)
853 (inhibit-point-motion-hooks t)
854 (modified-p (buffer-modified-p)))
855 (company--insert-candidate ,candidate)
858 (delete-region company-point (point)))))
860 (defun company-explicit-action-p ()
861 "Return whether explicit completion action was taken by the user."
862 (or company--explicit-action
863 company-selection-changed))
865 (defun company-reformat (candidate)
866 ;; company-ispell needs this, because the results are always lower-case
867 ;; It's mory efficient to fix it only when they are displayed.
868 ;; FIXME: Adopt the current text's capitalization instead?
869 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
870 (concat company-prefix (substring candidate (length company-prefix)))
873 (defun company--should-complete ()
874 (and (not (or buffer-read-only overriding-terminal-local-map
875 overriding-local-map))
876 ;; Check if in the middle of entering a key combination.
877 (or (equal (this-command-keys-vector) [])
878 (not (keymapp (key-binding (this-command-keys-vector)))))
879 (eq company-idle-delay t)
880 (or (eq t company-begin-commands)
881 (memq this-command company-begin-commands)
882 (and (symbolp this-command) (get this-command 'company-begin)))
883 (not (and transient-mark-mode mark-active))))
885 (defun company--should-continue ()
886 (or (eq t company-begin-commands)
887 (eq t company-continue-commands)
888 (if (eq 'not (car company-continue-commands))
889 (not (memq this-command (cdr company-continue-commands)))
890 (or (memq this-command company-begin-commands)
891 (memq this-command company-continue-commands)
892 (string-match-p "\\`company-" (symbol-name this-command))))))
894 (defun company-call-frontends (command)
895 (dolist (frontend company-frontends)
897 (funcall frontend command)
898 (error (error "Company: Front-end %s error \"%s\" on command %s"
899 frontend (error-message-string err) command)))))
901 (defun company-set-selection (selection &optional force-update)
903 (if company-selection-wrap-around
904 (mod selection company-candidates-length)
905 (max 0 (min (1- company-candidates-length) selection))))
906 (when (or force-update (not (equal selection company-selection)))
907 (setq company-selection selection
908 company-selection-changed t)
909 (company-call-frontends 'update)))
911 (defun company-apply-predicate (candidates predicate)
913 (dolist (c candidates)
914 (when (funcall predicate c)
918 (defun company-update-candidates (candidates)
919 (setq company-candidates-length (length candidates))
920 (if (> company-selection 0)
921 ;; Try to restore the selection
922 (let ((selected (nth company-selection company-candidates)))
923 (setq company-selection 0
924 company-candidates candidates)
926 (while (and candidates (string< (pop candidates) selected))
927 (incf company-selection))
929 ;; Make sure selection isn't out of bounds.
930 (setq company-selection (min (1- company-candidates-length)
931 company-selection)))))
932 (setq company-selection 0
933 company-candidates candidates))
935 (push (cons company-prefix company-candidates) company-candidates-cache)
937 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
938 ;; We want to support non-prefix completion, so filtering is the
939 ;; responsibility of each respective backend, not ours.
940 ;; On the other hand, we don't want to replace non-prefix input in
941 ;; `company-complete-common'.
943 (if (cdr company-candidates)
944 (company--safe-candidate
945 (let ((common (try-completion company-prefix company-candidates)))
947 ;; Mulple equal strings, probably with different
951 (car company-candidates)))))
953 (defun company--safe-candidate (str)
954 ;; XXX: This feature is deprecated.
955 (or (company-call-backend 'crop str)
958 (defun company-calculate-candidates (prefix)
959 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
960 (ignore-case (company-call-backend 'ignore-case)))
962 (when company-candidates-cache
963 (let ((len (length prefix))
964 (completion-ignore-case ignore-case)
966 (dotimes (i (1+ len))
967 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
968 company-candidates-cache)))
969 (setq candidates (all-completions prefix prev))
971 ;; no cache match, call back-end
973 (setq candidates (company-call-backend 'candidates prefix))
974 (when company-candidates-predicate
976 (company-apply-predicate candidates
977 company-candidates-predicate)))
978 (unless (company-call-backend 'sorted)
979 (setq candidates (sort candidates 'string<)))
980 (when (company-call-backend 'duplicates)
981 (company--strip-duplicates candidates))))
982 (setq candidates (company--transform-candidates candidates))
984 (if (or (cdr candidates)
985 (not (eq t (compare-strings (car candidates) nil nil
986 prefix nil nil ignore-case))))
988 ;; Already completed and unique; don't start.
991 (defun company--strip-duplicates (candidates)
992 (let ((c2 candidates))
998 (while (let ((str2 (car c2)))
999 (if (not (equal str str2))
1001 (when (eq anno 'unk)
1002 (setq anno (company-call-backend
1005 (company-call-backend
1006 'annotation str2))))
1010 (defun company--transform-candidates (candidates)
1011 (let ((c candidates))
1012 (dolist (tr company-transformers)
1013 (setq c (funcall tr c)))
1016 (defun company-sort-by-occurrence (candidates)
1017 "Sort CANDIDATES according to their occurrences.
1018 Searches for each in the currently visible part of the current buffer and
1019 gives priority to the closest ones above point, then closest ones below
1020 point. The rest of the list is appended unchanged.
1021 Keywords and function definition names are ignored."
1028 (progn (forward-line 0)
1029 (search-backward candidate (window-start) t)))
1031 (search-forward candidate (window-end) t)))
1032 (let ((beg (match-beginning 0))
1033 (end (match-end 0)))
1034 (when (save-excursion
1036 (and (not (memq (get-text-property (point) 'face)
1037 '(font-lock-function-name-face
1038 font-lock-keyword-face)))
1039 (let* ((prefix (company-call-backend 'prefix))
1040 (prefix (or (car-safe prefix) prefix)))
1041 (and (stringp prefix)
1042 (= (length prefix) (- end beg))))))
1043 (push (cons candidate (if (< beg (point))
1045 (- beg (window-start))))
1050 (mapcar #'car (sort occurs (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
1053 (defun company-idle-begin (buf win tick pos)
1054 (and (eq buf (current-buffer))
1055 (eq win (selected-window))
1056 (eq tick (buffer-chars-modified-tick))
1058 (not (equal (point) company-point))
1059 (when (company-auto-begin)
1060 (when (version< emacs-version "24.3.50")
1061 (company-input-noop))
1062 (company-post-command))))
1064 (defun company-auto-begin ()
1066 (not company-candidates)
1067 (let ((company-idle-delay t)
1068 (company-begin-commands t))
1069 (condition-case-no-debug err
1071 (error (message "Company: An error occurred in auto-begin")
1072 (message "%s" (error-message-string err))
1074 (quit (company-cancel)))))
1075 (unless company-candidates
1076 (setq company-backend nil))
1077 ;; Return non-nil if active.
1080 (defun company-manual-begin ()
1082 (company-assert-enabled)
1083 (setq company--explicit-action t)
1085 (let ((company-minimum-prefix-length 0))
1086 (company-auto-begin))
1087 (unless company-candidates
1088 (setq company--explicit-action nil))))
1090 (defun company-other-backend (&optional backward)
1091 (interactive (list current-prefix-arg))
1092 (company-assert-enabled)
1094 (let* ((after (cdr (member company-backend company-backends)))
1095 (before (cdr (member company-backend (reverse company-backends))))
1097 (append before (reverse after))
1098 (append after (reverse before)))))
1100 (dolist (backend next)
1101 (when (ignore-errors (company-begin-backend backend))
1103 (company-manual-begin))
1104 (unless company-candidates
1105 (error "No other back-end")))
1107 (defun company-require-match-p ()
1108 (let ((backend-value (company-call-backend 'require-match)))
1109 (or (eq backend-value t)
1110 (and (not (eq backend-value 'never))
1111 (if (functionp company-require-match)
1112 (funcall company-require-match)
1113 (eq company-require-match t))))))
1115 (defun company-auto-complete-p (input)
1116 "Return non-nil, if input starts with punctuation or parentheses."
1117 (and (if (functionp company-auto-complete)
1118 (funcall company-auto-complete)
1119 company-auto-complete)
1120 (if (functionp company-auto-complete-chars)
1121 (funcall company-auto-complete-chars input)
1122 (if (consp company-auto-complete-chars)
1123 (memq (char-syntax (string-to-char input))
1124 company-auto-complete-chars)
1125 (string-match (substring input 0 1) company-auto-complete-chars)))))
1127 (defun company--incremental-p ()
1128 (and (> (point) company-point)
1129 (> (point-max) company--point-max)
1130 (not (eq this-command 'backward-delete-char-untabify))
1131 (equal (buffer-substring (- company-point (length company-prefix))
1135 (defun company--continue-failed ()
1136 (let ((input (buffer-substring-no-properties (point) company-point)))
1138 ((company-auto-complete-p input)
1141 (goto-char company-point)
1142 (let ((company--auto-completion t))
1143 (company-complete-selection))
1145 ((company-require-match-p)
1146 ;; wrong incremental input, but required match
1147 (delete-char (- (length input)))
1149 (message "Matching input is required")
1151 ((equal company-prefix (car company-candidates))
1152 ;; last input was actually success
1153 (company-cancel company-prefix))
1154 (t (company-cancel)))))
1156 (defun company--good-prefix-p (prefix)
1157 (and (or (company-explicit-action-p)
1158 (unless (eq prefix 'stop)
1159 (or (eq (cdr-safe prefix) t)
1160 (>= (or (cdr-safe prefix) (length prefix))
1161 company-minimum-prefix-length))))
1162 (stringp (or (car-safe prefix) prefix))))
1164 (defun company--continue ()
1165 (when (company-call-backend 'no-cache company-prefix)
1166 ;; Don't complete existing candidates, fetch new ones.
1167 (setq company-candidates-cache nil))
1168 (let* ((new-prefix (company-call-backend 'prefix))
1169 (c (when (and (company--good-prefix-p new-prefix)
1170 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1171 (= (- (point) (length new-prefix))
1172 (- company-point (length company-prefix))))
1173 (company-calculate-candidates new-prefix))))
1176 ;; t means complete/unique.
1177 (company-cancel new-prefix))
1179 ;; incremental match
1180 (setq company-prefix new-prefix)
1181 (company-update-candidates c)
1183 ((not (company--incremental-p))
1185 (t (company--continue-failed)))))
1187 (defun company--begin-new ()
1189 (dolist (backend (if company-backend
1190 ;; prefer manual override
1191 (list company-backend)
1194 (if (or (symbolp backend)
1195 (functionp backend))
1196 (when (or (not (symbolp backend))
1197 (eq t (get backend 'company-init))
1198 (unless (get backend 'company-init)
1199 (company-init-backend backend)))
1200 (funcall backend 'prefix))
1201 (company--multi-backend-adapter backend 'prefix)))
1203 (when (company--good-prefix-p prefix)
1204 (setq prefix (or (car-safe prefix) prefix)
1205 company-backend backend
1206 c (company-calculate-candidates prefix))
1207 ;; t means complete/unique. We don't start, so no hooks.
1209 (when company--explicit-action
1210 (message "No completion found"))
1211 (setq company-prefix prefix)
1212 (when (symbolp backend)
1213 (setq company-lighter (concat " " (symbol-name backend))))
1214 (company-update-candidates c)
1215 (run-hook-with-args 'company-completion-started-hook
1216 (company-explicit-action-p))
1217 (company-call-frontends 'show)))
1220 (defun company-begin ()
1221 (or (and company-candidates (company--continue))
1222 (and (company--should-complete) (company--begin-new)))
1223 (when company-candidates
1224 (let ((modified (buffer-modified-p)))
1225 (when (and company-end-of-buffer-workaround (eobp))
1226 (save-excursion (insert "\n"))
1227 (setq company-added-newline
1228 (or modified (buffer-chars-modified-tick)))))
1229 (setq company-point (point)
1230 company--point-max (point-max))
1231 (company-ensure-emulation-alist)
1232 (company-enable-overriding-keymap company-active-map)
1233 (company-call-frontends 'update)))
1235 (defun company-cancel (&optional result)
1236 (and company-added-newline
1237 (> (point-max) (point-min))
1238 (let ((tick (buffer-chars-modified-tick)))
1239 (delete-region (1- (point-max)) (point-max))
1240 (equal tick company-added-newline))
1241 ;; Only set unmodified when tick remained the same since insert,
1242 ;; and the buffer wasn't modified before.
1243 (set-buffer-modified-p nil))
1244 (when company-prefix
1245 (if (stringp result)
1247 (company-call-backend 'pre-completion result)
1248 (run-hook-with-args 'company-completion-finished-hook result)
1249 (company-call-backend 'post-completion result))
1250 (run-hook-with-args 'company-completion-cancelled-hook result)))
1251 (setq company-added-newline nil
1254 company-candidates nil
1255 company-candidates-length nil
1256 company-candidates-cache nil
1257 company-candidates-predicate nil
1260 company-selection-changed nil
1261 company--explicit-action nil
1262 company-lighter company-default-lighter
1263 company--point-max nil
1266 (cancel-timer company-timer))
1267 (company-search-mode 0)
1268 (company-call-frontends 'hide)
1269 (company-enable-overriding-keymap nil)
1270 ;; Make return value explicit.
1273 (defun company-abort ()
1276 ;; Don't start again, unless started manually.
1277 (setq company-point (point)))
1279 (defun company-finish (result)
1280 (company--insert-candidate result)
1281 (company-cancel result)
1282 ;; Don't start again, unless started manually.
1283 (setq company-point (point)))
1285 (defsubst company-keep (command)
1286 (and (symbolp command) (get command 'company-keep)))
1288 (defun company-pre-command ()
1289 (unless (company-keep this-command)
1291 (when company-candidates
1292 (company-call-frontends 'pre-command)
1293 (unless (company--should-continue)
1295 (error (message "Company: An error occurred in pre-command")
1296 (message "%s" (error-message-string err))
1299 (cancel-timer company-timer)
1300 (setq company-timer nil))
1301 (company-uninstall-map))
1303 (defun company-post-command ()
1304 (unless (company-keep this-command)
1307 (unless (equal (point) company-point)
1309 (if company-candidates
1310 (company-call-frontends 'post-command)
1311 (and (numberp company-idle-delay)
1312 (or (eq t company-begin-commands)
1313 (memq this-command company-begin-commands))
1315 (run-with-timer company-idle-delay nil
1317 (current-buffer) (selected-window)
1318 (buffer-chars-modified-tick) (point))))))
1319 (error (message "Company: An error occurred in post-command")
1320 (message "%s" (error-message-string err))
1322 (company-install-map))
1324 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1326 (defvar company-search-string nil)
1327 (make-variable-buffer-local 'company-search-string)
1329 (defvar company-search-lighter " Search: \"\"")
1330 (make-variable-buffer-local 'company-search-lighter)
1332 (defvar company-search-old-map nil)
1333 (make-variable-buffer-local 'company-search-old-map)
1335 (defvar company-search-old-selection 0)
1336 (make-variable-buffer-local 'company-search-old-selection)
1338 (defun company-search (text lines)
1339 (let ((quoted (regexp-quote text))
1341 (dolist (line lines)
1342 (when (string-match quoted line (length company-prefix))
1346 (defun company-search-printing-char ()
1348 (company-search-assert-enabled)
1349 (setq company-search-string
1350 (concat (or company-search-string "") (string last-command-event))
1351 company-search-lighter (concat " Search: \"" company-search-string
1353 (let ((pos (company-search company-search-string
1354 (nthcdr company-selection company-candidates))))
1357 (company-set-selection (+ company-selection pos) t))))
1359 (defun company-search-repeat-forward ()
1360 "Repeat the incremental search in completion candidates forward."
1362 (company-search-assert-enabled)
1363 (let ((pos (company-search company-search-string
1364 (cdr (nthcdr company-selection
1365 company-candidates)))))
1368 (company-set-selection (+ company-selection pos 1) t))))
1370 (defun company-search-repeat-backward ()
1371 "Repeat the incremental search in completion candidates backwards."
1373 (company-search-assert-enabled)
1374 (let ((pos (company-search company-search-string
1375 (nthcdr (- company-candidates-length
1377 (reverse company-candidates)))))
1380 (company-set-selection (- company-selection pos 1) t))))
1382 (defun company-create-match-predicate ()
1383 (setq company-candidates-predicate
1384 `(lambda (candidate)
1385 ,(if company-candidates-predicate
1386 `(and (string-match ,company-search-string candidate)
1387 (funcall ,company-candidates-predicate
1389 `(string-match ,company-search-string candidate))))
1390 (company-update-candidates
1391 (company-apply-predicate company-candidates company-candidates-predicate))
1392 ;; Invalidate cache.
1393 (setq company-candidates-cache (cons company-prefix company-candidates)))
1395 (defun company-filter-printing-char ()
1397 (company-search-assert-enabled)
1398 (company-search-printing-char)
1399 (company-create-match-predicate)
1400 (company-call-frontends 'update))
1402 (defun company-search-kill-others ()
1403 "Limit the completion candidates to the ones matching the search string."
1405 (company-search-assert-enabled)
1406 (company-create-match-predicate)
1407 (company-search-mode 0)
1408 (company-call-frontends 'update))
1410 (defun company-search-abort ()
1411 "Abort searching the completion candidates."
1413 (company-search-assert-enabled)
1414 (company-set-selection company-search-old-selection t)
1415 (company-search-mode 0))
1417 (defun company-search-other-char ()
1419 (company-search-assert-enabled)
1420 (company-search-mode 0)
1421 (company--unread-last-input))
1423 (defvar company-search-map
1425 (keymap (make-keymap)))
1426 (if (fboundp 'max-char)
1427 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1428 'company-search-printing-char)
1430 ;; obsolete in Emacs 23
1431 (let ((l (generic-character-list))
1432 (table (nth 1 keymap)))
1434 (set-char-table-default table (car l) 'company-search-printing-char)
1435 (setq l (cdr l))))))
1436 (define-key keymap [t] 'company-search-other-char)
1438 (define-key keymap (make-string 1 i) 'company-search-other-char)
1441 (define-key keymap (vector i) 'company-search-printing-char)
1443 (let ((meta-map (make-sparse-keymap)))
1444 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1445 (define-key keymap [escape] meta-map))
1446 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1447 (define-key keymap "\e\e\e" 'company-search-other-char)
1448 (define-key keymap [escape escape escape] 'company-search-other-char)
1450 (define-key keymap "\C-g" 'company-search-abort)
1451 (define-key keymap "\C-s" 'company-search-repeat-forward)
1452 (define-key keymap "\C-r" 'company-search-repeat-backward)
1453 (define-key keymap "\C-o" 'company-search-kill-others)
1455 "Keymap used for incrementally searching the completion candidates.")
1457 (define-minor-mode company-search-mode
1458 "Search mode for completion candidates.
1459 Don't start this directly, use `company-search-candidates' or
1460 `company-filter-candidates'."
1461 nil company-search-lighter nil
1462 (if company-search-mode
1463 (if (company-manual-begin)
1465 (setq company-search-old-selection company-selection)
1466 (company-call-frontends 'update))
1467 (setq company-search-mode nil))
1468 (kill-local-variable 'company-search-string)
1469 (kill-local-variable 'company-search-lighter)
1470 (kill-local-variable 'company-search-old-selection)
1471 (company-enable-overriding-keymap company-active-map)))
1473 (defun company-search-assert-enabled ()
1474 (company-assert-enabled)
1475 (unless company-search-mode
1476 (company-uninstall-map)
1477 (error "Company not in search mode")))
1479 (defun company-search-candidates ()
1480 "Start searching the completion candidates incrementally.
1482 \\<company-search-map>Search can be controlled with the commands:
1483 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1484 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1485 - `company-search-abort' (\\[company-search-abort])
1487 Regular characters are appended to the search string.
1489 The command `company-search-kill-others' (\\[company-search-kill-others])
1490 uses the search string to limit the completion candidates."
1492 (company-search-mode 1)
1493 (company-enable-overriding-keymap company-search-map))
1495 (defvar company-filter-map
1496 (let ((keymap (make-keymap)))
1497 (define-key keymap [remap company-search-printing-char]
1498 'company-filter-printing-char)
1499 (set-keymap-parent keymap company-search-map)
1501 "Keymap used for incrementally searching the completion candidates.")
1503 (defun company-filter-candidates ()
1504 "Start filtering the completion candidates incrementally.
1505 This works the same way as `company-search-candidates' immediately
1506 followed by `company-search-kill-others' after each input."
1508 (company-search-mode 1)
1509 (company-enable-overriding-keymap company-filter-map))
1511 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1513 (defun company-select-next ()
1514 "Select the next candidate in the list."
1516 (when (company-manual-begin)
1517 (company-set-selection (1+ company-selection))))
1519 (defun company-select-previous ()
1520 "Select the previous candidate in the list."
1522 (when (company-manual-begin)
1523 (company-set-selection (1- company-selection))))
1525 (defun company-select-next-or-abort ()
1526 "Select the next candidate if more than one, else abort
1527 and invoke the normal binding."
1529 (if (> company-candidates-length 1)
1530 (company-select-next)
1532 (company--unread-last-input)))
1534 (defun company-select-previous-or-abort ()
1535 "Select the previous candidate if more than one, else abort
1536 and invoke the normal binding."
1538 (if (> company-candidates-length 1)
1539 (company-select-previous)
1541 (company--unread-last-input)))
1543 (defvar company-pseudo-tooltip-overlay)
1545 (defvar company-tooltip-offset)
1547 (defun company--inside-tooltip-p (event-col-row row height)
1548 (let* ((ovl company-pseudo-tooltip-overlay)
1549 (column (overlay-get ovl 'company-column))
1550 (width (overlay-get ovl 'company-width))
1551 (evt-col (car event-col-row))
1552 (evt-row (cdr event-col-row)))
1553 (and (>= evt-col column)
1554 (< evt-col (+ column width))
1556 (and (> evt-row row)
1557 (<= evt-row (+ row height) ))
1558 (and (< evt-row row)
1559 (>= evt-row (+ row height)))))))
1561 (defun company--event-col-row (event)
1562 (let* ((col-row (posn-actual-col-row (event-start event)))
1564 (row (cdr col-row)))
1565 (incf col (window-hscroll))
1566 (and header-line-format
1567 (version< "24" emacs-version)
1571 (defun company-select-mouse (event)
1572 "Select the candidate picked by the mouse."
1574 (let ((event-col-row (company--event-col-row event))
1575 (ovl-row (company--row))
1576 (ovl-height (and company-pseudo-tooltip-overlay
1577 (min (overlay-get company-pseudo-tooltip-overlay
1579 company-candidates-length))))
1581 (company--inside-tooltip-p event-col-row ovl-row ovl-height))
1583 (company-set-selection (+ (cdr event-col-row)
1584 (1- company-tooltip-offset)
1585 (if (and (eq company-tooltip-offset-display 'lines)
1586 (not (zerop company-tooltip-offset)))
1589 (if (< ovl-height 0)
1594 (company--unread-last-input)
1597 (defun company-complete-mouse (event)
1598 "Insert the candidate picked by the mouse."
1600 (when (company-select-mouse event)
1601 (company-complete-selection)))
1603 (defun company-complete-selection ()
1604 "Insert the selected candidate."
1606 (when (company-manual-begin)
1607 (let ((result (nth company-selection company-candidates)))
1608 (when company--auto-completion
1609 (setq result (company--safe-candidate result)))
1610 (company-finish result))))
1612 (defun company-complete-common ()
1613 "Insert the common part of all candidates."
1615 (when (company-manual-begin)
1616 (if (and (not (cdr company-candidates))
1617 (equal company-common (car company-candidates)))
1618 (company-complete-selection)
1619 (when company-common
1620 (company--insert-candidate company-common)))))
1622 (defun company-complete ()
1623 "Insert the common part of all candidates or the current selection.
1624 The first time this is called, the common part is inserted, the second
1625 time, or when the selection has been changed, the selected candidate is
1628 (when (company-manual-begin)
1629 (if (or company-selection-changed
1630 (eq last-command 'company-complete-common))
1631 (call-interactively 'company-complete-selection)
1632 (call-interactively 'company-complete-common)
1633 (setq this-command 'company-complete-common))))
1635 (defun company-complete-number (n)
1636 "Insert the Nth candidate.
1637 To show the number next to the candidates in some back-ends, enable
1638 `company-show-numbers'."
1639 (when (company-manual-begin)
1640 (and (< n 1) (> n company-candidates-length)
1641 (error "No candidate number %d" n))
1643 (company-finish (nth n company-candidates))))
1645 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1647 (defconst company-space-strings-limit 100)
1649 (defconst company-space-strings
1651 (dotimes (i company-space-strings-limit)
1652 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1653 (apply 'vector lst)))
1655 (defun company-space-string (len)
1656 (if (< len company-space-strings-limit)
1657 (aref company-space-strings len)
1658 (make-string len ?\ )))
1660 (defun company-safe-substring (str from &optional to)
1661 (if (> from (string-width str))
1665 (move-to-column from)
1666 (let ((beg (point)))
1670 (concat (buffer-substring beg (point))
1671 (let ((padding (- to (current-column))))
1673 (company-space-string padding)))))
1674 (buffer-substring beg (point-max)))))))
1676 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1678 (defvar company-last-metadata nil)
1679 (make-variable-buffer-local 'company-last-metadata)
1681 (defun company-fetch-metadata ()
1682 (let ((selected (nth company-selection company-candidates)))
1683 (unless (eq selected (car company-last-metadata))
1684 (setq company-last-metadata
1685 (cons selected (company-call-backend 'meta selected))))
1686 (cdr company-last-metadata)))
1688 (defun company-doc-buffer (&optional string)
1689 (with-current-buffer (get-buffer-create "*company-documentation*")
1696 (defvar company--electric-commands
1697 '(scroll-other-window scroll-other-window-down)
1698 "List of Commands that won't break out of electric commands.")
1700 (defmacro company--electric-do (&rest body)
1701 (declare (indent 0) (debug t))
1702 `(when (company-manual-begin)
1703 (save-window-excursion
1704 (let ((height (window-height))
1705 (row (company--row))
1708 (and (< (window-height) height)
1709 (< (- (window-height) row 2) company-tooltip-limit)
1710 (recenter (- (window-height) row 2)))
1711 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1712 company--electric-commands)
1713 (call-interactively cmd))
1714 (company--unread-last-input)))))
1716 (defun company--unread-last-input ()
1717 (when last-input-event
1718 (clear-this-command-keys t)
1719 (setq unread-command-events (list last-input-event))))
1721 (defun company-show-doc-buffer ()
1722 "Temporarily show the documentation buffer for the selection."
1724 (company--electric-do
1725 (let* ((selected (nth company-selection company-candidates))
1726 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1727 (error "No documentation available"))))
1728 (with-current-buffer doc-buffer
1729 (goto-char (point-min)))
1730 (display-buffer doc-buffer t))))
1731 (put 'company-show-doc-buffer 'company-keep t)
1733 (defun company-show-location ()
1734 "Temporarily display a buffer showing the selected candidate in context."
1736 (company--electric-do
1737 (let* ((selected (nth company-selection company-candidates))
1738 (location (company-call-backend 'location selected))
1739 (pos (or (cdr location) (error "No location available")))
1740 (buffer (or (and (bufferp (car location)) (car location))
1741 (find-file-noselect (car location) t))))
1742 (with-selected-window (display-buffer buffer t)
1745 (if (bufferp (car location))
1747 (goto-char (point-min))
1748 (forward-line (1- pos))))
1749 (set-window-start nil (point))))))
1750 (put 'company-show-location 'company-keep t)
1752 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1754 (defvar company-callback nil)
1755 (make-variable-buffer-local 'company-callback)
1757 (defvar company-begin-with-marker nil)
1758 (make-variable-buffer-local 'company-begin-with-marker)
1760 (defun company-remove-callback (&optional ignored)
1761 (remove-hook 'company-completion-finished-hook company-callback t)
1762 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1763 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1764 (when company-begin-with-marker
1765 (set-marker company-begin-with-marker nil)))
1767 (defun company-begin-backend (backend &optional callback)
1768 "Start a completion at point using BACKEND."
1769 (interactive (let ((val (completing-read "Company back-end: "
1771 'functionp nil "company-")))
1773 (list (intern val)))))
1774 (when (setq company-callback callback)
1775 (add-hook 'company-completion-finished-hook company-callback nil t))
1776 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1777 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1778 (setq company-backend backend)
1779 ;; Return non-nil if active.
1780 (or (company-manual-begin)
1781 (error "Cannot complete at point")))
1783 (defun company-begin-with (candidates
1784 &optional prefix-length require-match callback)
1785 "Start a completion at point.
1786 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length
1787 of the prefix that already is in the buffer before point.
1790 CALLBACK is a function called with the selected result if the user
1791 successfully completes the input.
1793 Example: \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1794 ;; FIXME: When Emacs 23 is no longer a concern, replace
1795 ;; `company-begin-with-marker' with a lexical variable; use a lexical closure.
1796 (setq company-begin-with-marker (copy-marker (point) t))
1797 (company-begin-backend
1798 `(lambda (command &optional arg &rest ignored)
1800 ((eq command 'prefix)
1801 (when (equal (point) (marker-position company-begin-with-marker))
1802 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1803 ((eq command 'candidates)
1804 (all-completions arg ',candidates))
1805 ((eq command 'require-match)
1809 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1811 (defvar company-pseudo-tooltip-overlay nil)
1812 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1814 (defvar company-tooltip-offset 0)
1815 (make-variable-buffer-local 'company-tooltip-offset)
1817 (defun company-tooltip--lines-update-offset (selection num-lines limit)
1819 (setq company-tooltip-offset
1820 (max (min selection company-tooltip-offset)
1821 (- selection -1 limit)))
1823 (when (<= company-tooltip-offset 1)
1825 (setq company-tooltip-offset 0))
1827 (when (>= company-tooltip-offset (- num-lines limit 1))
1829 (when (= selection (1- num-lines))
1830 (decf company-tooltip-offset)
1831 (when (<= company-tooltip-offset 1)
1832 (setq company-tooltip-offset 0)
1837 (defun company-tooltip--simple-update-offset (selection num-lines limit)
1838 (setq company-tooltip-offset
1839 (if (< selection company-tooltip-offset)
1841 (max company-tooltip-offset
1842 (- selection limit -1)))))
1846 (defsubst company-round-tab (arg)
1847 (* (/ (+ arg tab-width) tab-width) tab-width))
1849 (defun company-plainify (str)
1850 (let ((prefix (get-text-property 0 'line-prefix str)))
1851 (when prefix ; Keep the original value unmodified, for no special reason.
1852 (setq str (concat prefix str))
1853 (remove-text-properties 0 (length str) '(line-prefix) str)))
1854 (let* ((pieces (split-string str "\t"))
1857 (setcar copy (company-safe-substring
1858 (car copy) 0 (company-round-tab (string-width (car copy)))))
1860 (apply 'concat pieces)))
1862 (defun company-fill-propertize (value annotation width selected left right)
1863 (let* ((margin (length left))
1864 (common (+ (or (company-call-backend 'match value)
1865 (length company-common)) margin))
1866 (ann-ralign company-tooltip-align-annotations)
1867 (ann-truncate (< width
1868 (+ (length value) (length annotation)
1869 (if ann-ralign 1 0))))
1870 (ann-start (+ margin
1874 (- width (length annotation)))
1876 (ann-end (min (+ ann-start (length annotation)) width))
1878 (if (or ann-truncate (not ann-ralign))
1879 (company-safe-substring
1881 (when (and annotation ann-ralign) " ")
1885 (company-safe-substring value 0
1886 (- width (length annotation)))
1889 (setq width (+ width margin (length right)))
1891 (add-text-properties 0 width '(face company-tooltip
1892 mouse-face company-tooltip-mouse)
1894 (add-text-properties margin common
1895 '(face company-tooltip-common
1896 mouse-face company-tooltip-mouse)
1898 (when (< ann-start ann-end)
1899 (add-text-properties ann-start ann-end
1900 '(face company-tooltip-annotation
1901 mouse-face company-tooltip-mouse)
1904 (if (and company-search-string
1905 (string-match (regexp-quote company-search-string) value
1906 (length company-prefix)))
1907 (let ((beg (+ margin (match-beginning 0)))
1908 (end (+ margin (match-end 0))))
1909 (add-text-properties beg end '(face company-tooltip-selection)
1911 (when (< beg common)
1912 (add-text-properties beg common
1913 '(face company-tooltip-common-selection)
1915 (add-text-properties 0 width '(face company-tooltip-selection
1916 mouse-face company-tooltip-selection)
1918 (add-text-properties margin common
1919 '(face company-tooltip-common-selection
1920 mouse-face company-tooltip-selection)
1926 (defun company-buffer-lines (beg end)
1929 (while (and (= 1 (vertical-motion 1))
1931 (let ((bound (min end (1- (point)))))
1932 ;; A visual line can contain several physical lines (e.g. with outline's
1933 ;; folding overlay). Take only the first one.
1934 (push (buffer-substring beg
1937 (re-search-forward "$" bound 'move)
1941 (unless (eq beg end)
1942 (push (buffer-substring beg end) lines))
1945 (defun company-modify-line (old new offset)
1946 (concat (company-safe-substring old 0 offset)
1948 (company-safe-substring old (+ offset (length new)))))
1950 (defsubst company--length-limit (lst limit)
1951 (if (nthcdr limit lst)
1955 (defun company--replacement-string (lines old column nl &optional align-top)
1956 (decf column company-tooltip-margin)
1958 (let ((width (length (car lines)))
1959 (remaining-cols (- (+ (company--window-width) (window-hscroll))
1961 (when (> width remaining-cols)
1962 (decf column (- width remaining-cols))))
1964 (let ((offset (and (< column 0) (- column)))
1969 ;; untouched lines first
1970 (dotimes (_ (- (length old) (length lines)))
1971 (push (pop old) new)))
1972 ;; length into old lines.
1974 (push (company-modify-line (pop old)
1975 (company--offset-line (pop lines) offset)
1977 ;; Append whole new lines.
1979 (push (concat (company-space-string column)
1980 (company--offset-line (pop lines) offset))
1983 (let ((str (concat (when nl "\n")
1984 (mapconcat 'identity (nreverse new) "\n")
1986 (font-lock-append-text-property 0 (length str) 'face 'default str)
1989 (defun company--offset-line (line offset)
1990 (if (and offset line)
1991 (substring line offset)
1994 (defun company--create-lines (selection limit)
1995 (let ((len company-candidates-length)
1997 (window-width (company--window-width))
2006 ;; Maybe clear old offset.
2007 (when (< len (+ company-tooltip-offset limit))
2008 (setq company-tooltip-offset 0))
2010 ;; Scroll to offset.
2011 (if (eq company-tooltip-offset-display 'lines)
2012 (setq limit (company-tooltip--lines-update-offset selection len limit))
2013 (company-tooltip--simple-update-offset selection len limit))
2016 ((eq company-tooltip-offset-display 'scrollbar)
2017 (setq scrollbar-bounds (company--scrollbar-bounds company-tooltip-offset
2019 ((eq company-tooltip-offset-display 'lines)
2020 (when (> company-tooltip-offset 0)
2021 (setq previous (format "...(%d)" company-tooltip-offset)))
2022 (setq remainder (- len limit company-tooltip-offset)
2023 remainder (when (> remainder 0)
2024 (setq remainder (format "...(%d)" remainder))))))
2026 (decf selection company-tooltip-offset)
2027 (setq width (max (length previous) (length remainder))
2028 lines (nthcdr company-tooltip-offset company-candidates)
2032 (decf window-width (* 2 company-tooltip-margin))
2033 (when scrollbar-bounds (decf window-width))
2036 (let* ((value (pop lines-copy))
2037 (annotation (company-call-backend 'annotation value)))
2038 (when (and annotation company-tooltip-align-annotations)
2039 ;; `lisp-completion-at-point' adds a space.
2040 (setq annotation (comment-string-strip annotation t nil)))
2041 (push (cons value annotation) items)
2042 (setq width (max (+ (length value)
2043 (if (and annotation company-tooltip-align-annotations)
2044 (1+ (length annotation))
2045 (length annotation)))
2048 (setq width (min window-width
2049 (if (and company-show-numbers
2050 (< company-tooltip-offset 10))
2054 ;; number can make tooltip too long
2055 (when company-show-numbers
2056 (setq numbered company-tooltip-offset))
2058 (let ((items (nreverse items)) new)
2060 (push (company--scrollpos-line previous width) new))
2063 (let* ((item (pop items))
2064 (str (company-reformat (car item)))
2065 (annotation (cdr item))
2066 (right (company-space-string company-tooltip-margin))
2068 (when (< numbered 10)
2071 (setq right (concat (format " %d" (mod numbered 10)) right)))
2073 (company-fill-propertize str annotation
2074 width (equal i selection)
2075 (company-space-string
2076 company-tooltip-margin)
2078 (when scrollbar-bounds
2079 (company--scrollbar i scrollbar-bounds)))
2083 (push (company--scrollpos-line remainder width) new))
2087 (defun company--scrollbar-bounds (offset limit length)
2088 (when (> length limit)
2089 (let* ((size (ceiling (* limit (float limit)) length))
2090 (lower (floor (* limit (float offset)) length))
2091 (upper (+ lower size -1)))
2092 (cons lower upper))))
2094 (defun company--scrollbar (i bounds)
2095 (propertize " " 'face
2096 (if (and (>= i (car bounds)) (<= i (cdr bounds)))
2097 'company-scrollbar-fg
2098 'company-scrollbar-bg)))
2100 (defun company--scrollpos-line (text width)
2101 (propertize (concat (company-space-string company-tooltip-margin)
2102 (company-safe-substring text 0 width)
2103 (company-space-string company-tooltip-margin))
2104 'face 'company-tooltip))
2108 (defsubst company--window-inner-height ()
2109 (let ((edges (window-inside-edges)))
2110 (- (nth 3 edges) (nth 1 edges))))
2112 (defsubst company--window-width ()
2115 ((display-graphic-p) 0)
2116 ;; Account for the line continuation column.
2117 ((version< "24.3.1" emacs-version) 1)
2118 ;; Emacs 24.3 and earlier included margins
2119 ;; in window-width when in TTY.
2120 (t (1+ (let ((margins (window-margins)))
2121 (+ (or (car margins) 0)
2122 (or (cdr margins) 0))))))))
2124 (defun company--pseudo-tooltip-height ()
2125 "Calculate the appropriate tooltip height.
2126 Returns a negative number if the tooltip should be displayed above point."
2127 (let* ((lines (company--row))
2128 (below (- (company--window-inner-height) 1 lines)))
2129 (if (and (< below (min company-tooltip-minimum company-candidates-length))
2131 (- (max 3 (min company-tooltip-limit lines)))
2132 (max 3 (min company-tooltip-limit below)))))
2134 (defun company-pseudo-tooltip-show (row column selection)
2135 (company-pseudo-tooltip-hide)
2138 (let* ((height (company--pseudo-tooltip-height))
2142 (setq row (+ row height -1)
2145 (let* ((nl (< (move-to-window-line row) row))
2147 (end (save-excursion
2148 (move-to-window-line (+ row (abs height)))
2150 (ov (make-overlay beg end))
2151 (args (list (mapcar 'company-plainify
2152 (company-buffer-lines beg end))
2155 (setq company-pseudo-tooltip-overlay ov)
2156 (overlay-put ov 'company-replacement-args args)
2158 (let ((lines (company--create-lines selection (abs height))))
2159 (overlay-put ov 'company-after
2160 (apply 'company--replacement-string lines args))
2161 (overlay-put ov 'company-width (string-width (car lines))))
2163 (overlay-put ov 'company-column column)
2164 (overlay-put ov 'company-height height)))))
2166 (defun company-pseudo-tooltip-show-at-point (pos)
2167 (let ((row (company--row pos))
2168 (col (company--column pos)))
2169 (company-pseudo-tooltip-show (1+ row) col company-selection)))
2171 (defun company-pseudo-tooltip-edit (selection)
2172 (let ((height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
2173 (overlay-put company-pseudo-tooltip-overlay 'company-after
2174 (apply 'company--replacement-string
2175 (company--create-lines selection (abs height))
2176 (overlay-get company-pseudo-tooltip-overlay
2177 'company-replacement-args)))))
2179 (defun company-pseudo-tooltip-hide ()
2180 (when company-pseudo-tooltip-overlay
2181 (delete-overlay company-pseudo-tooltip-overlay)
2182 (setq company-pseudo-tooltip-overlay nil)))
2184 (defun company-pseudo-tooltip-hide-temporarily ()
2185 (when (overlayp company-pseudo-tooltip-overlay)
2186 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
2187 (overlay-put company-pseudo-tooltip-overlay 'line-prefix nil)
2188 (overlay-put company-pseudo-tooltip-overlay 'after-string nil)))
2190 (defun company-pseudo-tooltip-unhide ()
2191 (when company-pseudo-tooltip-overlay
2192 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
2193 ;; Beat outline's folding overlays, at least.
2194 (overlay-put company-pseudo-tooltip-overlay 'priority 1)
2195 ;; No (extra) prefix for the first line.
2196 (overlay-put company-pseudo-tooltip-overlay 'line-prefix "")
2197 (overlay-put company-pseudo-tooltip-overlay 'after-string
2198 (overlay-get company-pseudo-tooltip-overlay 'company-after))
2199 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
2201 (defun company-pseudo-tooltip-guard ()
2202 (buffer-substring-no-properties
2203 (point) (overlay-start company-pseudo-tooltip-overlay)))
2205 (defun company-pseudo-tooltip-frontend (command)
2206 "`company-mode' front-end similar to a tooltip but based on overlays."
2208 (pre-command (company-pseudo-tooltip-hide-temporarily))
2210 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
2211 (overlay-get company-pseudo-tooltip-overlay
2214 (new-height (company--pseudo-tooltip-height)))
2215 (unless (and (>= (* old-height new-height) 0)
2216 (>= (abs old-height) (abs new-height))
2217 (equal (company-pseudo-tooltip-guard)
2218 (overlay-get company-pseudo-tooltip-overlay
2221 (company-pseudo-tooltip-show-at-point (- (point)
2222 (length company-prefix)))
2223 (overlay-put company-pseudo-tooltip-overlay
2224 'company-guard (company-pseudo-tooltip-guard))))
2225 (company-pseudo-tooltip-unhide))
2226 (hide (company-pseudo-tooltip-hide)
2227 (setq company-tooltip-offset 0))
2228 (update (when (overlayp company-pseudo-tooltip-overlay)
2229 (company-pseudo-tooltip-edit company-selection)))))
2231 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
2232 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
2233 (unless (and (eq command 'post-command)
2234 (company--show-inline-p))
2235 (company-pseudo-tooltip-frontend command)))
2237 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2239 (defvar company-preview-overlay nil)
2240 (make-variable-buffer-local 'company-preview-overlay)
2242 (defun company-preview-show-at-point (pos)
2243 (company-preview-hide)
2245 (setq company-preview-overlay (make-overlay pos (1+ pos)))
2247 (let ((completion (nth company-selection company-candidates)))
2248 (setq completion (propertize completion 'face 'company-preview))
2249 (add-text-properties 0 (length company-common)
2250 '(face company-preview-common) completion)
2252 ;; Add search string
2253 (and company-search-string
2254 (string-match (regexp-quote company-search-string) completion)
2255 (add-text-properties (match-beginning 0)
2257 '(face company-preview-search)
2260 (setq completion (company-strip-prefix completion))
2262 (and (equal pos (point))
2263 (not (equal completion ""))
2264 (add-text-properties 0 1 '(cursor t) completion))
2266 (overlay-put company-preview-overlay 'display
2267 (concat completion (unless (eq pos (point-max))
2268 (buffer-substring pos (1+ pos)))))
2269 (overlay-put company-preview-overlay 'window (selected-window))))
2271 (defun company-preview-hide ()
2272 (when company-preview-overlay
2273 (delete-overlay company-preview-overlay)
2274 (setq company-preview-overlay nil)))
2276 (defun company-preview-frontend (command)
2277 "`company-mode' front-end showing the selection as if it had been inserted."
2279 (pre-command (company-preview-hide))
2280 (post-command (company-preview-show-at-point (point)))
2281 (hide (company-preview-hide))))
2283 (defun company-preview-if-just-one-frontend (command)
2284 "`company-preview-frontend', but only shown for single candidates."
2285 (when (or (not (eq command 'post-command))
2286 (company--show-inline-p))
2287 (company-preview-frontend command)))
2289 (defun company--show-inline-p ()
2290 (and (not (cdr company-candidates))
2292 (string-prefix-p company-prefix company-common
2293 (company-call-backend 'ignore-case))))
2295 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2297 (defvar company-echo-last-msg nil)
2298 (make-variable-buffer-local 'company-echo-last-msg)
2300 (defvar company-echo-timer nil)
2302 (defvar company-echo-delay .01)
2304 (defun company-echo-show (&optional getter)
2306 (setq company-echo-last-msg (funcall getter)))
2307 (let ((message-log-max nil))
2308 (if company-echo-last-msg
2309 (message "%s" company-echo-last-msg)
2312 (defun company-echo-show-soon (&optional getter)
2313 (when company-echo-timer
2314 (cancel-timer company-echo-timer))
2315 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
2317 (defsubst company-echo-show-when-idle (&optional getter)
2318 (when (sit-for company-echo-delay)
2319 (company-echo-show getter)))
2321 (defun company-echo-format ()
2323 (let ((limit (window-width (minibuffer-window)))
2325 ;; Roll to selection.
2326 (candidates (nthcdr company-selection company-candidates))
2327 (i (if company-show-numbers company-selection 99999))
2331 (setq comp (company-reformat (pop candidates))
2332 len (+ len 1 (length comp)))
2336 (setq comp (propertize (format "%d: %s" i comp)
2337 'face 'company-echo))
2340 (add-text-properties 3 (+ 3 (length company-common))
2341 '(face company-echo-common) comp))
2342 (setq comp (propertize comp 'face 'company-echo))
2343 (add-text-properties 0 (length company-common)
2344 '(face company-echo-common) comp))
2346 (setq candidates nil)
2349 (mapconcat 'identity (nreverse msg) " ")))
2351 (defun company-echo-strip-common-format ()
2353 (let ((limit (window-width (minibuffer-window)))
2354 (len (+ (length company-prefix) 2))
2355 ;; Roll to selection.
2356 (candidates (nthcdr company-selection company-candidates))
2357 (i (if company-show-numbers company-selection 99999))
2361 (setq comp (company-strip-prefix (pop candidates))
2362 len (+ len 2 (length comp)))
2365 (setq comp (format "%s (%d)" comp i))
2369 (setq candidates nil)
2370 (push (propertize comp 'face 'company-echo) msg)))
2372 (concat (propertize company-prefix 'face 'company-echo-common) "{"
2373 (mapconcat 'identity (nreverse msg) ", ")
2376 (defun company-echo-hide ()
2377 (unless (equal company-echo-last-msg "")
2378 (setq company-echo-last-msg "")
2379 (company-echo-show)))
2381 (defun company-echo-frontend (command)
2382 "`company-mode' front-end showing the candidates in the echo area."
2384 (post-command (company-echo-show-soon 'company-echo-format))
2385 (hide (company-echo-hide))))
2387 (defun company-echo-strip-common-frontend (command)
2388 "`company-mode' front-end showing the candidates in the echo area."
2390 (post-command (company-echo-show-soon 'company-echo-strip-common-format))
2391 (hide (company-echo-hide))))
2393 (defun company-echo-metadata-frontend (command)
2394 "`company-mode' front-end showing the documentation in the echo area."
2396 (post-command (company-echo-show-when-idle 'company-fetch-metadata))
2397 (hide (company-echo-hide))))
2400 ;;; company.el ends here