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 t
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)
765 (if (functionp company-backend)
766 (apply company-backend args)
767 (apply 'company--multi-backend-adapter company-backend args)))
769 (defun company--multi-backend-adapter (backends command &rest args)
770 (let ((backends (loop for b in backends
771 when (not (and (symbolp b)
772 (eq 'failed (get b 'company-init))))
776 (loop for backend in backends
777 when (equal (funcall backend 'prefix)
779 append (apply backend 'candidates args)))
784 (dolist (backend backends)
785 (when (setq value (apply backend command args))
786 (return value))))))))
788 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790 (defvar company-prefix nil)
791 (make-variable-buffer-local 'company-prefix)
793 (defvar company-candidates nil)
794 (make-variable-buffer-local 'company-candidates)
796 (defvar company-candidates-length nil)
797 (make-variable-buffer-local 'company-candidates-length)
799 (defvar company-candidates-cache nil)
800 (make-variable-buffer-local 'company-candidates-cache)
802 (defvar company-candidates-predicate nil)
803 (make-variable-buffer-local 'company-candidates-predicate)
805 (defvar company-common nil)
806 (make-variable-buffer-local 'company-common)
808 (defvar company-selection 0)
809 (make-variable-buffer-local 'company-selection)
811 (defvar company-selection-changed nil)
812 (make-variable-buffer-local 'company-selection-changed)
814 (defvar company--explicit-action nil
815 "Non-nil, if explicit completion took place.")
816 (make-variable-buffer-local 'company--explicit-action)
818 (defvar company--auto-completion nil
819 "Non-nil when current candidate is being inserted automatically.
820 Controlled by `company-auto-complete'.")
822 (defvar company--point-max nil)
823 (make-variable-buffer-local 'company--point-max)
825 (defvar company-point nil)
826 (make-variable-buffer-local 'company-point)
828 (defvar company-timer nil)
830 (defvar company-added-newline nil)
831 (make-variable-buffer-local 'company-added-newline)
833 (defsubst company-strip-prefix (str)
834 (substring str (length company-prefix)))
836 (defun company--insert-candidate (candidate)
837 (setq candidate (substring-no-properties candidate))
838 ;; XXX: Return value we check here is subject to change.
839 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
840 (insert (company-strip-prefix candidate))
841 (delete-region (- (point) (length company-prefix)) (point))
844 (defmacro company-with-candidate-inserted (candidate &rest body)
845 "Evaluate BODY with CANDIDATE temporarily inserted.
846 This is a tool for back-ends that need candidates inserted before they
847 can retrieve meta-data for them."
849 `(let ((inhibit-modification-hooks t)
850 (inhibit-point-motion-hooks t)
851 (modified-p (buffer-modified-p)))
852 (company--insert-candidate ,candidate)
855 (delete-region company-point (point)))))
857 (defun company-explicit-action-p ()
858 "Return whether explicit completion action was taken by the user."
859 (or company--explicit-action
860 company-selection-changed))
862 (defun company-reformat (candidate)
863 ;; company-ispell needs this, because the results are always lower-case
864 ;; It's mory efficient to fix it only when they are displayed.
865 ;; FIXME: Adopt the current text's capitalization instead?
866 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
867 (concat company-prefix (substring candidate (length company-prefix)))
870 (defun company--should-complete ()
871 (and (not (or buffer-read-only overriding-terminal-local-map
872 overriding-local-map))
873 ;; Check if in the middle of entering a key combination.
874 (or (equal (this-command-keys-vector) [])
875 (not (keymapp (key-binding (this-command-keys-vector)))))
876 (eq company-idle-delay t)
877 (or (eq t company-begin-commands)
878 (memq this-command company-begin-commands)
879 (and (symbolp this-command) (get this-command 'company-begin)))
880 (not (and transient-mark-mode mark-active))))
882 (defun company--should-continue ()
883 (or (eq t company-begin-commands)
884 (eq t company-continue-commands)
885 (if (eq 'not (car company-continue-commands))
886 (not (memq this-command (cdr company-continue-commands)))
887 (or (memq this-command company-begin-commands)
888 (memq this-command company-continue-commands)
889 (string-match-p "\\`company-" (symbol-name this-command))))))
891 (defun company-call-frontends (command)
892 (dolist (frontend company-frontends)
894 (funcall frontend command)
895 (error (error "Company: Front-end %s error \"%s\" on command %s"
896 frontend (error-message-string err) command)))))
898 (defun company-set-selection (selection &optional force-update)
900 (if company-selection-wrap-around
901 (mod selection company-candidates-length)
902 (max 0 (min (1- company-candidates-length) selection))))
903 (when (or force-update (not (equal selection company-selection)))
904 (setq company-selection selection
905 company-selection-changed t)
906 (company-call-frontends 'update)))
908 (defun company-apply-predicate (candidates predicate)
910 (dolist (c candidates)
911 (when (funcall predicate c)
915 (defun company-update-candidates (candidates)
916 (setq company-candidates-length (length candidates))
917 (if (> company-selection 0)
918 ;; Try to restore the selection
919 (let ((selected (nth company-selection company-candidates)))
920 (setq company-selection 0
921 company-candidates candidates)
923 (while (and candidates (string< (pop candidates) selected))
924 (incf company-selection))
926 ;; Make sure selection isn't out of bounds.
927 (setq company-selection (min (1- company-candidates-length)
928 company-selection)))))
929 (setq company-selection 0
930 company-candidates candidates))
932 (push (cons company-prefix company-candidates) company-candidates-cache)
934 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
935 ;; We want to support non-prefix completion, so filtering is the
936 ;; responsibility of each respective backend, not ours.
937 ;; On the other hand, we don't want to replace non-prefix input in
938 ;; `company-complete-common'.
940 (if (cdr company-candidates)
941 (company--safe-candidate
942 (let ((common (try-completion company-prefix company-candidates)))
944 ;; Mulple equal strings, probably with different
948 (car company-candidates)))))
950 (defun company--safe-candidate (str)
951 ;; XXX: This feature is deprecated.
952 (or (company-call-backend 'crop str)
955 (defun company-calculate-candidates (prefix)
956 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
957 (ignore-case (company-call-backend 'ignore-case)))
959 (when company-candidates-cache
960 (let ((len (length prefix))
961 (completion-ignore-case ignore-case)
963 (dotimes (i (1+ len))
964 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
965 company-candidates-cache)))
966 (setq candidates (all-completions prefix prev))
968 ;; no cache match, call back-end
970 (setq candidates (company-call-backend 'candidates prefix))
971 (when company-candidates-predicate
973 (company-apply-predicate candidates
974 company-candidates-predicate)))
975 (unless (company-call-backend 'sorted)
976 (setq candidates (sort candidates 'string<)))
977 (when (company-call-backend 'duplicates)
978 (company--strip-duplicates candidates))))
979 (setq candidates (company--transform-candidates candidates))
981 (if (or (cdr candidates)
982 (not (eq t (compare-strings (car candidates) nil nil
983 prefix nil nil ignore-case))))
985 ;; Already completed and unique; don't start.
988 (defun company--strip-duplicates (candidates)
989 (let ((c2 candidates))
995 (while (let ((str2 (car c2)))
996 (if (not (equal str str2))
999 (setq anno (company-call-backend
1002 (company-call-backend
1003 'annotation str2))))
1007 (defun company--transform-candidates (candidates)
1008 (let ((c candidates))
1009 (dolist (tr company-transformers)
1010 (setq c (funcall tr c)))
1013 (defun company-sort-by-occurrence (candidates)
1014 "Sort CANDIDATES according to their occurrences.
1015 Searches for each in the currently visible part of the current buffer and
1016 gives priority to the closest ones above point, then closest ones below
1017 point. The rest of the list is appended unchanged.
1018 Keywords and function definition names are ignored."
1025 (progn (forward-line 0)
1026 (search-backward candidate (window-start) t)))
1028 (search-forward candidate (window-end) t)))
1029 (let ((beg (match-beginning 0))
1030 (end (match-end 0)))
1031 (when (save-excursion
1033 (and (not (memq (get-text-property (point) 'face)
1034 '(font-lock-function-name-face
1035 font-lock-keyword-face)))
1036 (let* ((prefix (company-call-backend 'prefix))
1037 (prefix (or (car-safe prefix) prefix)))
1038 (and (stringp prefix)
1039 (= (length prefix) (- end beg))))))
1040 (push (cons candidate (if (< beg (point))
1042 (- beg (window-start))))
1047 (mapcar #'car (sort occurs (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
1050 (defun company-idle-begin (buf win tick pos)
1052 (eq buf (current-buffer))
1053 (eq win (selected-window))
1054 (eq tick (buffer-chars-modified-tick))
1056 (not company-candidates)
1057 (not (equal (point) company-point))
1058 (let ((company-idle-delay t)
1059 (company-begin-commands t))
1061 (when company-candidates
1062 (when (version< emacs-version "24.3.50")
1063 (company-input-noop))
1064 (company-post-command)))))
1066 (defun company-auto-begin ()
1067 (company-assert-enabled)
1069 (not company-candidates)
1070 (let ((company-idle-delay t)
1071 (company-minimum-prefix-length 0)
1072 (company-begin-commands t))
1074 ;; Return non-nil if active.
1077 (defun company-manual-begin ()
1079 (setq company--explicit-action t)
1081 (company-auto-begin)
1082 (unless company-candidates
1083 (setq company--explicit-action nil))))
1085 (defun company-other-backend (&optional backward)
1086 (interactive (list current-prefix-arg))
1087 (company-assert-enabled)
1089 (let* ((after (cdr (member company-backend company-backends)))
1090 (before (cdr (member company-backend (reverse company-backends))))
1092 (append before (reverse after))
1093 (append after (reverse before)))))
1095 (dolist (backend next)
1096 (when (ignore-errors (company-begin-backend backend))
1098 (company-manual-begin))
1099 (unless company-candidates
1100 (error "No other back-end")))
1102 (defun company-require-match-p ()
1103 (let ((backend-value (company-call-backend 'require-match)))
1104 (or (eq backend-value t)
1105 (and (not (eq backend-value 'never))
1106 (if (functionp company-require-match)
1107 (funcall company-require-match)
1108 (eq company-require-match t))))))
1110 (defun company-auto-complete-p (input)
1111 "Return non-nil, if input starts with punctuation or parentheses."
1112 (and (if (functionp company-auto-complete)
1113 (funcall company-auto-complete)
1114 company-auto-complete)
1115 (if (functionp company-auto-complete-chars)
1116 (funcall company-auto-complete-chars input)
1117 (if (consp company-auto-complete-chars)
1118 (memq (char-syntax (string-to-char input))
1119 company-auto-complete-chars)
1120 (string-match (substring input 0 1) company-auto-complete-chars)))))
1122 (defun company--incremental-p ()
1123 (and (> (point) company-point)
1124 (> (point-max) company--point-max)
1125 (not (eq this-command 'backward-delete-char-untabify))
1126 (equal (buffer-substring (- company-point (length company-prefix))
1130 (defun company--continue-failed ()
1131 (let ((input (buffer-substring-no-properties (point) company-point)))
1133 ((company-auto-complete-p input)
1136 (goto-char company-point)
1137 (let ((company--auto-completion t))
1138 (company-complete-selection))
1140 ((company-require-match-p)
1141 ;; wrong incremental input, but required match
1142 (delete-char (- (length input)))
1144 (message "Matching input is required")
1146 ((equal company-prefix (car company-candidates))
1147 ;; last input was actually success
1148 (company-cancel company-prefix))
1149 (t (company-cancel)))))
1151 (defun company--good-prefix-p (prefix)
1152 (and (or (company-explicit-action-p)
1153 (unless (eq prefix 'stop)
1154 (or (eq (cdr-safe prefix) t)
1155 (>= (or (cdr-safe prefix) (length prefix))
1156 company-minimum-prefix-length))))
1157 (stringp (or (car-safe prefix) prefix))))
1159 (defun company--continue ()
1160 (when (company-call-backend 'no-cache company-prefix)
1161 ;; Don't complete existing candidates, fetch new ones.
1162 (setq company-candidates-cache nil))
1163 (let* ((new-prefix (company-call-backend 'prefix))
1164 (c (when (and (company--good-prefix-p new-prefix)
1165 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1166 (= (- (point) (length new-prefix))
1167 (- company-point (length company-prefix))))
1168 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1169 (company-calculate-candidates new-prefix))))
1172 ;; t means complete/unique.
1173 (company-cancel new-prefix))
1175 ;; incremental match
1176 (setq company-prefix new-prefix)
1177 (company-update-candidates c)
1179 ((not (company--incremental-p))
1181 (t (company--continue-failed)))))
1183 (defun company--begin-new ()
1185 (dolist (backend (if company-backend
1186 ;; prefer manual override
1187 (list company-backend)
1190 (if (or (symbolp backend)
1191 (functionp backend))
1192 (when (or (not (symbolp backend))
1193 (eq t (get backend 'company-init))
1194 (unless (get backend 'company-init)
1195 (company-init-backend backend)))
1196 (funcall backend 'prefix))
1197 (company--multi-backend-adapter backend 'prefix)))
1199 (when (company--good-prefix-p prefix)
1200 (setq prefix (or (car-safe prefix) prefix)
1201 company-backend backend
1202 c (company-calculate-candidates prefix))
1203 ;; t means complete/unique. We don't start, so no hooks.
1205 (when company--explicit-action
1206 (message "No completion found"))
1207 (setq company-prefix prefix)
1208 (when (symbolp backend)
1209 (setq company-lighter (concat " " (symbol-name backend))))
1210 (company-update-candidates c)
1211 (run-hook-with-args 'company-completion-started-hook
1212 (company-explicit-action-p))
1213 (company-call-frontends 'show)))
1216 (defun company-begin ()
1217 (or (and company-candidates (company--continue))
1218 (and (company--should-complete) (company--begin-new)))
1219 (when company-candidates
1220 (let ((modified (buffer-modified-p)))
1221 (when (and company-end-of-buffer-workaround (eobp))
1222 (save-excursion (insert "\n"))
1223 (setq company-added-newline
1224 (or modified (buffer-chars-modified-tick)))))
1225 (setq company-point (point)
1226 company--point-max (point-max))
1227 (company-ensure-emulation-alist)
1228 (company-enable-overriding-keymap company-active-map)
1229 (company-call-frontends 'update)))
1231 (defun company-cancel (&optional result)
1232 (and company-added-newline
1233 (> (point-max) (point-min))
1234 (let ((tick (buffer-chars-modified-tick)))
1235 (delete-region (1- (point-max)) (point-max))
1236 (equal tick company-added-newline))
1237 ;; Only set unmodified when tick remained the same since insert,
1238 ;; and the buffer wasn't modified before.
1239 (set-buffer-modified-p nil))
1240 (when company-prefix
1241 (if (stringp result)
1243 (company-call-backend 'pre-completion result)
1244 (run-hook-with-args 'company-completion-finished-hook result)
1245 (company-call-backend 'post-completion result))
1246 (run-hook-with-args 'company-completion-cancelled-hook result)))
1247 (setq company-added-newline nil
1250 company-candidates nil
1251 company-candidates-length nil
1252 company-candidates-cache nil
1253 company-candidates-predicate nil
1256 company-selection-changed nil
1257 company--explicit-action nil
1258 company-lighter company-default-lighter
1259 company--point-max nil
1262 (cancel-timer company-timer))
1263 (company-search-mode 0)
1264 (company-call-frontends 'hide)
1265 (company-enable-overriding-keymap nil)
1266 ;; Make return value explicit.
1269 (defun company-abort ()
1272 ;; Don't start again, unless started manually.
1273 (setq company-point (point)))
1275 (defun company-finish (result)
1276 (company--insert-candidate result)
1277 (company-cancel result)
1278 ;; Don't start again, unless started manually.
1279 (setq company-point (point)))
1281 (defsubst company-keep (command)
1282 (and (symbolp command) (get command 'company-keep)))
1284 (defun company-pre-command ()
1285 (unless (company-keep this-command)
1287 (when company-candidates
1288 (company-call-frontends 'pre-command)
1289 (unless (company--should-continue)
1291 (error (message "Company: An error occurred in pre-command")
1292 (message "%s" (error-message-string err))
1295 (cancel-timer company-timer)
1296 (setq company-timer nil))
1297 (company-uninstall-map))
1299 (defun company-post-command ()
1300 (unless (company-keep this-command)
1303 (unless (equal (point) company-point)
1305 (if company-candidates
1306 (company-call-frontends 'post-command)
1307 (and (numberp company-idle-delay)
1308 (or (eq t company-begin-commands)
1309 (memq this-command company-begin-commands))
1311 (run-with-timer company-idle-delay nil
1313 (current-buffer) (selected-window)
1314 (buffer-chars-modified-tick) (point))))))
1315 (error (message "Company: An error occurred in post-command")
1316 (message "%s" (error-message-string err))
1318 (company-install-map))
1320 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1322 (defvar company-search-string nil)
1323 (make-variable-buffer-local 'company-search-string)
1325 (defvar company-search-lighter " Search: \"\"")
1326 (make-variable-buffer-local 'company-search-lighter)
1328 (defvar company-search-old-map nil)
1329 (make-variable-buffer-local 'company-search-old-map)
1331 (defvar company-search-old-selection 0)
1332 (make-variable-buffer-local 'company-search-old-selection)
1334 (defun company-search (text lines)
1335 (let ((quoted (regexp-quote text))
1337 (dolist (line lines)
1338 (when (string-match quoted line (length company-prefix))
1342 (defun company-search-printing-char ()
1344 (company-search-assert-enabled)
1345 (setq company-search-string
1346 (concat (or company-search-string "") (string last-command-event))
1347 company-search-lighter (concat " Search: \"" company-search-string
1349 (let ((pos (company-search company-search-string
1350 (nthcdr company-selection company-candidates))))
1353 (company-set-selection (+ company-selection pos) t))))
1355 (defun company-search-repeat-forward ()
1356 "Repeat the incremental search in completion candidates forward."
1358 (company-search-assert-enabled)
1359 (let ((pos (company-search company-search-string
1360 (cdr (nthcdr company-selection
1361 company-candidates)))))
1364 (company-set-selection (+ company-selection pos 1) t))))
1366 (defun company-search-repeat-backward ()
1367 "Repeat the incremental search in completion candidates backwards."
1369 (company-search-assert-enabled)
1370 (let ((pos (company-search company-search-string
1371 (nthcdr (- company-candidates-length
1373 (reverse company-candidates)))))
1376 (company-set-selection (- company-selection pos 1) t))))
1378 (defun company-create-match-predicate ()
1379 (setq company-candidates-predicate
1380 `(lambda (candidate)
1381 ,(if company-candidates-predicate
1382 `(and (string-match ,company-search-string candidate)
1383 (funcall ,company-candidates-predicate
1385 `(string-match ,company-search-string candidate))))
1386 (company-update-candidates
1387 (company-apply-predicate company-candidates company-candidates-predicate))
1388 ;; Invalidate cache.
1389 (setq company-candidates-cache (cons company-prefix company-candidates)))
1391 (defun company-filter-printing-char ()
1393 (company-search-assert-enabled)
1394 (company-search-printing-char)
1395 (company-create-match-predicate)
1396 (company-call-frontends 'update))
1398 (defun company-search-kill-others ()
1399 "Limit the completion candidates to the ones matching the search string."
1401 (company-search-assert-enabled)
1402 (company-create-match-predicate)
1403 (company-search-mode 0)
1404 (company-call-frontends 'update))
1406 (defun company-search-abort ()
1407 "Abort searching the completion candidates."
1409 (company-search-assert-enabled)
1410 (company-set-selection company-search-old-selection t)
1411 (company-search-mode 0))
1413 (defun company-search-other-char ()
1415 (company-search-assert-enabled)
1416 (company-search-mode 0)
1417 (company--unread-last-input))
1419 (defvar company-search-map
1421 (keymap (make-keymap)))
1422 (if (fboundp 'max-char)
1423 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1424 'company-search-printing-char)
1426 ;; obsolete in Emacs 23
1427 (let ((l (generic-character-list))
1428 (table (nth 1 keymap)))
1430 (set-char-table-default table (car l) 'company-search-printing-char)
1431 (setq l (cdr l))))))
1432 (define-key keymap [t] 'company-search-other-char)
1434 (define-key keymap (make-string 1 i) 'company-search-other-char)
1437 (define-key keymap (vector i) 'company-search-printing-char)
1439 (let ((meta-map (make-sparse-keymap)))
1440 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1441 (define-key keymap [escape] meta-map))
1442 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1443 (define-key keymap "\e\e\e" 'company-search-other-char)
1444 (define-key keymap [escape escape escape] 'company-search-other-char)
1446 (define-key keymap "\C-g" 'company-search-abort)
1447 (define-key keymap "\C-s" 'company-search-repeat-forward)
1448 (define-key keymap "\C-r" 'company-search-repeat-backward)
1449 (define-key keymap "\C-o" 'company-search-kill-others)
1451 "Keymap used for incrementally searching the completion candidates.")
1453 (define-minor-mode company-search-mode
1454 "Search mode for completion candidates.
1455 Don't start this directly, use `company-search-candidates' or
1456 `company-filter-candidates'."
1457 nil company-search-lighter nil
1458 (if company-search-mode
1459 (if (company-manual-begin)
1461 (setq company-search-old-selection company-selection)
1462 (company-call-frontends 'update))
1463 (setq company-search-mode nil))
1464 (kill-local-variable 'company-search-string)
1465 (kill-local-variable 'company-search-lighter)
1466 (kill-local-variable 'company-search-old-selection)
1467 (company-enable-overriding-keymap company-active-map)))
1469 (defun company-search-assert-enabled ()
1470 (company-assert-enabled)
1471 (unless company-search-mode
1472 (company-uninstall-map)
1473 (error "Company not in search mode")))
1475 (defun company-search-candidates ()
1476 "Start searching the completion candidates incrementally.
1478 \\<company-search-map>Search can be controlled with the commands:
1479 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1480 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1481 - `company-search-abort' (\\[company-search-abort])
1483 Regular characters are appended to the search string.
1485 The command `company-search-kill-others' (\\[company-search-kill-others])
1486 uses the search string to limit the completion candidates."
1488 (company-search-mode 1)
1489 (company-enable-overriding-keymap company-search-map))
1491 (defvar company-filter-map
1492 (let ((keymap (make-keymap)))
1493 (define-key keymap [remap company-search-printing-char]
1494 'company-filter-printing-char)
1495 (set-keymap-parent keymap company-search-map)
1497 "Keymap used for incrementally searching the completion candidates.")
1499 (defun company-filter-candidates ()
1500 "Start filtering the completion candidates incrementally.
1501 This works the same way as `company-search-candidates' immediately
1502 followed by `company-search-kill-others' after each input."
1504 (company-search-mode 1)
1505 (company-enable-overriding-keymap company-filter-map))
1507 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1509 (defun company-select-next ()
1510 "Select the next candidate in the list."
1512 (when (company-manual-begin)
1513 (company-set-selection (1+ company-selection))))
1515 (defun company-select-previous ()
1516 "Select the previous candidate in the list."
1518 (when (company-manual-begin)
1519 (company-set-selection (1- company-selection))))
1521 (defun company-select-next-or-abort ()
1522 "Select the next candidate if more than one, else abort
1523 and invoke the normal binding."
1525 (if (> company-candidates-length 1)
1526 (company-select-next)
1528 (company--unread-last-input)))
1530 (defun company-select-previous-or-abort ()
1531 "Select the previous candidate if more than one, else abort
1532 and invoke the normal binding."
1534 (if (> company-candidates-length 1)
1535 (company-select-previous)
1537 (company--unread-last-input)))
1539 (defvar company-pseudo-tooltip-overlay)
1541 (defvar company-tooltip-offset)
1543 (defun company--inside-tooltip-p (event-col-row row height)
1544 (let* ((ovl company-pseudo-tooltip-overlay)
1545 (column (overlay-get ovl 'company-column))
1546 (width (overlay-get ovl 'company-width))
1547 (evt-col (car event-col-row))
1548 (evt-row (cdr event-col-row)))
1549 (and (>= evt-col column)
1550 (< evt-col (+ column width))
1552 (and (> evt-row row)
1553 (<= evt-row (+ row height) ))
1554 (and (< evt-row row)
1555 (>= evt-row (+ row height)))))))
1557 (defun company--event-col-row (event)
1558 (let* ((col-row (posn-actual-col-row (event-start event)))
1560 (row (cdr col-row)))
1561 (incf col (window-hscroll))
1562 (and header-line-format
1563 (version< "24" emacs-version)
1567 (defun company-select-mouse (event)
1568 "Select the candidate picked by the mouse."
1570 (let ((event-col-row (company--event-col-row event))
1571 (ovl-row (company--row))
1572 (ovl-height (and company-pseudo-tooltip-overlay
1573 (min (overlay-get company-pseudo-tooltip-overlay
1575 company-candidates-length))))
1577 (company--inside-tooltip-p event-col-row ovl-row ovl-height))
1579 (company-set-selection (+ (cdr event-col-row)
1580 (1- company-tooltip-offset)
1581 (if (and (eq company-tooltip-offset-display 'lines)
1582 (not (zerop company-tooltip-offset)))
1585 (if (< ovl-height 0)
1590 (company--unread-last-input)
1593 (defun company-complete-mouse (event)
1594 "Insert the candidate picked by the mouse."
1596 (when (company-select-mouse event)
1597 (company-complete-selection)))
1599 (defun company-complete-selection ()
1600 "Insert the selected candidate."
1602 (when (company-manual-begin)
1603 (let ((result (nth company-selection company-candidates)))
1604 (when company--auto-completion
1605 (setq result (company--safe-candidate result)))
1606 (company-finish result))))
1608 (defun company-complete-common ()
1609 "Insert the common part of all candidates."
1611 (when (company-manual-begin)
1612 (if (and (not (cdr company-candidates))
1613 (equal company-common (car company-candidates)))
1614 (company-complete-selection)
1615 (when company-common
1616 (company--insert-candidate company-common)))))
1618 (defun company-complete ()
1619 "Insert the common part of all candidates or the current selection.
1620 The first time this is called, the common part is inserted, the second
1621 time, or when the selection has been changed, the selected candidate is
1624 (when (company-manual-begin)
1625 (if (or company-selection-changed
1626 (eq last-command 'company-complete-common))
1627 (call-interactively 'company-complete-selection)
1628 (call-interactively 'company-complete-common)
1629 (setq this-command 'company-complete-common))))
1631 (defun company-complete-number (n)
1632 "Insert the Nth candidate.
1633 To show the number next to the candidates in some back-ends, enable
1634 `company-show-numbers'."
1635 (when (company-manual-begin)
1636 (and (< n 1) (> n company-candidates-length)
1637 (error "No candidate number %d" n))
1639 (company-finish (nth n company-candidates))))
1641 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1643 (defconst company-space-strings-limit 100)
1645 (defconst company-space-strings
1647 (dotimes (i company-space-strings-limit)
1648 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1649 (apply 'vector lst)))
1651 (defun company-space-string (len)
1652 (if (< len company-space-strings-limit)
1653 (aref company-space-strings len)
1654 (make-string len ?\ )))
1656 (defun company-safe-substring (str from &optional to)
1657 (if (> from (string-width str))
1661 (move-to-column from)
1662 (let ((beg (point)))
1666 (concat (buffer-substring beg (point))
1667 (let ((padding (- to (current-column))))
1669 (company-space-string padding)))))
1670 (buffer-substring beg (point-max)))))))
1672 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1674 (defvar company-last-metadata nil)
1675 (make-variable-buffer-local 'company-last-metadata)
1677 (defun company-fetch-metadata ()
1678 (let ((selected (nth company-selection company-candidates)))
1679 (unless (eq selected (car company-last-metadata))
1680 (setq company-last-metadata
1681 (cons selected (company-call-backend 'meta selected))))
1682 (cdr company-last-metadata)))
1684 (defun company-doc-buffer (&optional string)
1685 (with-current-buffer (get-buffer-create "*company-documentation*")
1692 (defvar company--electric-commands
1693 '(scroll-other-window scroll-other-window-down)
1694 "List of Commands that won't break out of electric commands.")
1696 (defmacro company--electric-do (&rest body)
1697 (declare (indent 0) (debug t))
1698 `(when (company-manual-begin)
1699 (save-window-excursion
1700 (let ((height (window-height))
1701 (row (company--row))
1704 (and (< (window-height) height)
1705 (< (- (window-height) row 2) company-tooltip-limit)
1706 (recenter (- (window-height) row 2)))
1707 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1708 company--electric-commands)
1709 (call-interactively cmd))
1710 (company--unread-last-input)))))
1712 (defun company--unread-last-input ()
1713 (when last-input-event
1714 (clear-this-command-keys t)
1715 (setq unread-command-events (list last-input-event))))
1717 (defun company-show-doc-buffer ()
1718 "Temporarily show the documentation buffer for the selection."
1720 (company--electric-do
1721 (let* ((selected (nth company-selection company-candidates))
1722 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1723 (error "No documentation available"))))
1724 (with-current-buffer doc-buffer
1725 (goto-char (point-min)))
1726 (display-buffer doc-buffer t))))
1727 (put 'company-show-doc-buffer 'company-keep t)
1729 (defun company-show-location ()
1730 "Temporarily display a buffer showing the selected candidate in context."
1732 (company--electric-do
1733 (let* ((selected (nth company-selection company-candidates))
1734 (location (company-call-backend 'location selected))
1735 (pos (or (cdr location) (error "No location available")))
1736 (buffer (or (and (bufferp (car location)) (car location))
1737 (find-file-noselect (car location) t))))
1738 (with-selected-window (display-buffer buffer t)
1741 (if (bufferp (car location))
1743 (goto-char (point-min))
1744 (forward-line (1- pos))))
1745 (set-window-start nil (point))))))
1746 (put 'company-show-location 'company-keep t)
1748 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1750 (defvar company-callback nil)
1751 (make-variable-buffer-local 'company-callback)
1753 (defvar company-begin-with-marker nil)
1754 (make-variable-buffer-local 'company-begin-with-marker)
1756 (defun company-remove-callback (&optional ignored)
1757 (remove-hook 'company-completion-finished-hook company-callback t)
1758 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1759 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1760 (when company-begin-with-marker
1761 (set-marker company-begin-with-marker nil)))
1763 (defun company-begin-backend (backend &optional callback)
1764 "Start a completion at point using BACKEND."
1765 (interactive (let ((val (completing-read "Company back-end: "
1767 'functionp nil "company-")))
1769 (list (intern val)))))
1770 (when (setq company-callback callback)
1771 (add-hook 'company-completion-finished-hook company-callback nil t))
1772 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1773 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1774 (setq company-backend backend)
1775 ;; Return non-nil if active.
1776 (or (company-manual-begin)
1778 (setq company-backend nil)
1779 (error "Cannot complete at point"))))
1781 (defun company-begin-with (candidates
1782 &optional prefix-length require-match callback)
1783 "Start a completion at point.
1784 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length
1785 of the prefix that already is in the buffer before point.
1788 CALLBACK is a function called with the selected result if the user
1789 successfully completes the input.
1791 Example: \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1792 ;; FIXME: When Emacs 23 is no longer a concern, replace
1793 ;; `company-begin-with-marker' with a lexical variable; use a lexical closure.
1794 (setq company-begin-with-marker (copy-marker (point) t))
1795 (company-begin-backend
1796 `(lambda (command &optional arg &rest ignored)
1798 ((eq command 'prefix)
1799 (when (equal (point) (marker-position company-begin-with-marker))
1800 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1801 ((eq command 'candidates)
1802 (all-completions arg ',candidates))
1803 ((eq command 'require-match)
1807 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1809 (defvar company-pseudo-tooltip-overlay nil)
1810 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1812 (defvar company-tooltip-offset 0)
1813 (make-variable-buffer-local 'company-tooltip-offset)
1815 (defun company-tooltip--lines-update-offset (selection num-lines limit)
1817 (setq company-tooltip-offset
1818 (max (min selection company-tooltip-offset)
1819 (- selection -1 limit)))
1821 (when (<= company-tooltip-offset 1)
1823 (setq company-tooltip-offset 0))
1825 (when (>= company-tooltip-offset (- num-lines limit 1))
1827 (when (= selection (1- num-lines))
1828 (decf company-tooltip-offset)
1829 (when (<= company-tooltip-offset 1)
1830 (setq company-tooltip-offset 0)
1835 (defun company-tooltip--simple-update-offset (selection num-lines limit)
1836 (setq company-tooltip-offset
1837 (if (< selection company-tooltip-offset)
1839 (max company-tooltip-offset
1840 (- selection limit -1)))))
1844 (defsubst company-round-tab (arg)
1845 (* (/ (+ arg tab-width) tab-width) tab-width))
1847 (defun company-plainify (str)
1848 (let ((prefix (get-text-property 0 'line-prefix str)))
1849 (when prefix ; Keep the original value unmodified, for no special reason.
1850 (setq str (concat prefix str))
1851 (remove-text-properties 0 (length str) '(line-prefix) str)))
1852 (let* ((pieces (split-string str "\t"))
1855 (setcar copy (company-safe-substring
1856 (car copy) 0 (company-round-tab (string-width (car copy)))))
1858 (apply 'concat pieces)))
1860 (defun company-fill-propertize (value annotation width selected left right)
1861 (let* ((margin (length left))
1862 (common (+ (or (company-call-backend 'match value)
1863 (length company-common)) margin))
1864 (ann-ralign company-tooltip-align-annotations)
1865 (ann-truncate (< width
1866 (+ (length value) (length annotation)
1867 (if ann-ralign 1 0))))
1868 (ann-start (+ margin
1872 (- width (length annotation)))
1875 (if (or ann-truncate (not ann-ralign))
1876 (company-safe-substring
1878 (when (and annotation ann-ralign) " ")
1882 (company-safe-substring value 0
1883 (- width (length annotation)))
1886 (setq width (+ width margin (length right)))
1888 (add-text-properties 0 width '(face company-tooltip
1889 mouse-face company-tooltip-mouse)
1891 (add-text-properties margin common
1892 '(face company-tooltip-common
1893 mouse-face company-tooltip-mouse)
1895 (add-text-properties ann-start (min (+ ann-start (length annotation)) width)
1896 '(face company-tooltip-annotation
1897 mouse-face company-tooltip-mouse)
1900 (if (and company-search-string
1901 (string-match (regexp-quote company-search-string) value
1902 (length company-prefix)))
1903 (let ((beg (+ margin (match-beginning 0)))
1904 (end (+ margin (match-end 0))))
1905 (add-text-properties beg end '(face company-tooltip-selection)
1907 (when (< beg common)
1908 (add-text-properties beg common
1909 '(face company-tooltip-common-selection)
1911 (add-text-properties 0 width '(face company-tooltip-selection
1912 mouse-face company-tooltip-selection)
1914 (add-text-properties margin common
1915 '(face company-tooltip-common-selection
1916 mouse-face company-tooltip-selection)
1922 (defun company-buffer-lines (beg end)
1925 (while (and (= 1 (vertical-motion 1))
1927 (let ((bound (min end (1- (point)))))
1928 ;; A visual line can contain several physical lines (e.g. with outline's
1929 ;; folding overlay). Take only the first one.
1930 (push (buffer-substring beg
1933 (re-search-forward "$" bound 'move)
1937 (unless (eq beg end)
1938 (push (buffer-substring beg end) lines))
1941 (defun company-modify-line (old new offset)
1942 (concat (company-safe-substring old 0 offset)
1944 (company-safe-substring old (+ offset (length new)))))
1946 (defsubst company--length-limit (lst limit)
1947 (if (nthcdr limit lst)
1951 (defun company--replacement-string (lines old column nl &optional align-top)
1952 (decf column company-tooltip-margin)
1954 (let ((width (length (car lines)))
1955 (remaining-cols (- (+ (company--window-width) (window-hscroll))
1957 (when (> width remaining-cols)
1958 (decf column (- width remaining-cols))))
1960 (let ((offset (and (< column 0) (- column)))
1965 ;; untouched lines first
1966 (dotimes (_ (- (length old) (length lines)))
1967 (push (pop old) new)))
1968 ;; length into old lines.
1970 (push (company-modify-line (pop old)
1971 (company--offset-line (pop lines) offset)
1973 ;; Append whole new lines.
1975 (push (concat (company-space-string column)
1976 (company--offset-line (pop lines) offset))
1979 (let ((str (concat (when nl "\n")
1980 (mapconcat 'identity (nreverse new) "\n")
1982 (font-lock-append-text-property 0 (length str) 'face 'default str)
1985 (defun company--offset-line (line offset)
1986 (if (and offset line)
1987 (substring line offset)
1990 (defun company--create-lines (selection limit)
1991 (let ((len company-candidates-length)
1993 (window-width (company--window-width))
2002 ;; Maybe clear old offset.
2003 (when (< len (+ company-tooltip-offset limit))
2004 (setq company-tooltip-offset 0))
2006 ;; Scroll to offset.
2007 (if (eq company-tooltip-offset-display 'lines)
2008 (setq limit (company-tooltip--lines-update-offset selection len limit))
2009 (company-tooltip--simple-update-offset selection len limit))
2012 ((eq company-tooltip-offset-display 'scrollbar)
2013 (setq scrollbar-bounds (company--scrollbar-bounds company-tooltip-offset
2015 ((eq company-tooltip-offset-display 'lines)
2016 (when (> company-tooltip-offset 0)
2017 (setq previous (format "...(%d)" company-tooltip-offset)))
2018 (setq remainder (- len limit company-tooltip-offset)
2019 remainder (when (> remainder 0)
2020 (setq remainder (format "...(%d)" remainder))))))
2022 (decf selection company-tooltip-offset)
2023 (setq width (max (length previous) (length remainder))
2024 lines (nthcdr company-tooltip-offset company-candidates)
2028 (decf window-width (* 2 company-tooltip-margin))
2029 (when scrollbar-bounds (decf window-width))
2032 (let* ((value (pop lines-copy))
2033 (annotation (company-call-backend 'annotation value)))
2034 (when (and annotation company-tooltip-align-annotations)
2035 ;; `lisp-completion-at-point' adds a space.
2036 (setq annotation (comment-string-strip annotation t nil)))
2037 (push (cons value annotation) items)
2038 (setq width (max (+ (length value)
2039 (if (and annotation company-tooltip-align-annotations)
2040 (1+ (length annotation))
2041 (length annotation)))
2044 (setq width (min window-width
2045 (if (and company-show-numbers
2046 (< company-tooltip-offset 10))
2050 ;; number can make tooltip too long
2051 (when company-show-numbers
2052 (setq numbered company-tooltip-offset))
2054 (let ((items (nreverse items)) new)
2056 (push (company--scrollpos-line previous width) new))
2059 (let* ((item (pop items))
2060 (str (company-reformat (car item)))
2061 (annotation (cdr item))
2062 (right (company-space-string company-tooltip-margin))
2064 (when (< numbered 10)
2067 (setq right (concat (format " %d" (mod numbered 10)) right)))
2069 (company-fill-propertize str annotation
2070 width (equal i selection)
2071 (company-space-string
2072 company-tooltip-margin)
2074 (when scrollbar-bounds
2075 (company--scrollbar i scrollbar-bounds)))
2079 (push (company--scrollpos-line remainder width) new))
2083 (defun company--scrollbar-bounds (offset limit length)
2084 (when (> length limit)
2085 (let* ((size (ceiling (* limit (float limit)) length))
2086 (lower (floor (* limit (float offset)) length))
2087 (upper (+ lower size -1)))
2088 (cons lower upper))))
2090 (defun company--scrollbar (i bounds)
2091 (propertize " " 'face
2092 (if (and (>= i (car bounds)) (<= i (cdr bounds)))
2093 'company-scrollbar-fg
2094 'company-scrollbar-bg)))
2096 (defun company--scrollpos-line (text width)
2097 (propertize (concat (company-space-string company-tooltip-margin)
2098 (company-safe-substring text 0 width)
2099 (company-space-string company-tooltip-margin))
2100 'face 'company-tooltip))
2104 (defsubst company--window-inner-height ()
2105 (let ((edges (window-inside-edges)))
2106 (- (nth 3 edges) (nth 1 edges))))
2108 (defsubst company--window-width ()
2111 ((display-graphic-p) 0)
2112 ;; Account for the line continuation column.
2113 ((version< "24.3.1" emacs-version) 1)
2114 ;; Emacs 24.3 and earlier included margins
2115 ;; in window-width when in TTY.
2116 (t (1+ (let ((margins (window-margins)))
2117 (+ (or (car margins) 0)
2118 (or (cdr margins) 0))))))))
2120 (defun company--pseudo-tooltip-height ()
2121 "Calculate the appropriate tooltip height.
2122 Returns a negative number if the tooltip should be displayed above point."
2123 (let* ((lines (company--row))
2124 (below (- (company--window-inner-height) 1 lines)))
2125 (if (and (< below (min company-tooltip-minimum company-candidates-length))
2127 (- (max 3 (min company-tooltip-limit lines)))
2128 (max 3 (min company-tooltip-limit below)))))
2130 (defun company-pseudo-tooltip-show (row column selection)
2131 (company-pseudo-tooltip-hide)
2134 (let* ((height (company--pseudo-tooltip-height))
2138 (setq row (+ row height -1)
2141 (let* ((nl (< (move-to-window-line row) row))
2143 (end (save-excursion
2144 (move-to-window-line (+ row (abs height)))
2146 (ov (make-overlay beg end))
2147 (args (list (mapcar 'company-plainify
2148 (company-buffer-lines beg end))
2151 (setq company-pseudo-tooltip-overlay ov)
2152 (overlay-put ov 'company-replacement-args args)
2154 (let ((lines (company--create-lines selection (abs height))))
2155 (overlay-put ov 'company-after
2156 (apply 'company--replacement-string lines args))
2157 (overlay-put ov 'company-width (string-width (car lines))))
2159 (overlay-put ov 'company-column column)
2160 (overlay-put ov 'company-height height)))))
2162 (defun company-pseudo-tooltip-show-at-point (pos)
2163 (let ((row (company--row pos))
2164 (col (company--column pos)))
2165 (company-pseudo-tooltip-show (1+ row) col company-selection)))
2167 (defun company-pseudo-tooltip-edit (selection)
2168 (let ((height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
2169 (overlay-put company-pseudo-tooltip-overlay 'company-after
2170 (apply 'company--replacement-string
2171 (company--create-lines selection (abs height))
2172 (overlay-get company-pseudo-tooltip-overlay
2173 'company-replacement-args)))))
2175 (defun company-pseudo-tooltip-hide ()
2176 (when company-pseudo-tooltip-overlay
2177 (delete-overlay company-pseudo-tooltip-overlay)
2178 (setq company-pseudo-tooltip-overlay nil)))
2180 (defun company-pseudo-tooltip-hide-temporarily ()
2181 (when (overlayp company-pseudo-tooltip-overlay)
2182 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
2183 (overlay-put company-pseudo-tooltip-overlay 'line-prefix nil)
2184 (overlay-put company-pseudo-tooltip-overlay 'after-string nil)))
2186 (defun company-pseudo-tooltip-unhide ()
2187 (when company-pseudo-tooltip-overlay
2188 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
2189 ;; Beat outline's folding overlays, at least.
2190 (overlay-put company-pseudo-tooltip-overlay 'priority 1)
2191 ;; No (extra) prefix for the first line.
2192 (overlay-put company-pseudo-tooltip-overlay 'line-prefix "")
2193 (overlay-put company-pseudo-tooltip-overlay 'after-string
2194 (overlay-get company-pseudo-tooltip-overlay 'company-after))
2195 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
2197 (defun company-pseudo-tooltip-guard ()
2198 (buffer-substring-no-properties
2199 (point) (overlay-start company-pseudo-tooltip-overlay)))
2201 (defun company-pseudo-tooltip-frontend (command)
2202 "`company-mode' front-end similar to a tooltip but based on overlays."
2204 (pre-command (company-pseudo-tooltip-hide-temporarily))
2206 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
2207 (overlay-get company-pseudo-tooltip-overlay
2210 (new-height (company--pseudo-tooltip-height)))
2211 (unless (and (>= (* old-height new-height) 0)
2212 (>= (abs old-height) (abs new-height))
2213 (equal (company-pseudo-tooltip-guard)
2214 (overlay-get company-pseudo-tooltip-overlay
2217 (company-pseudo-tooltip-show-at-point (- (point)
2218 (length company-prefix)))
2219 (overlay-put company-pseudo-tooltip-overlay
2220 'company-guard (company-pseudo-tooltip-guard))))
2221 (company-pseudo-tooltip-unhide))
2222 (hide (company-pseudo-tooltip-hide)
2223 (setq company-tooltip-offset 0))
2224 (update (when (overlayp company-pseudo-tooltip-overlay)
2225 (company-pseudo-tooltip-edit company-selection)))))
2227 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
2228 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
2229 (unless (and (eq command 'post-command)
2230 (company--show-inline-p))
2231 (company-pseudo-tooltip-frontend command)))
2233 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2235 (defvar company-preview-overlay nil)
2236 (make-variable-buffer-local 'company-preview-overlay)
2238 (defun company-preview-show-at-point (pos)
2239 (company-preview-hide)
2241 (setq company-preview-overlay (make-overlay pos (1+ pos)))
2243 (let ((completion (nth company-selection company-candidates)))
2244 (setq completion (propertize completion 'face 'company-preview))
2245 (add-text-properties 0 (length company-common)
2246 '(face company-preview-common) completion)
2248 ;; Add search string
2249 (and company-search-string
2250 (string-match (regexp-quote company-search-string) completion)
2251 (add-text-properties (match-beginning 0)
2253 '(face company-preview-search)
2256 (setq completion (company-strip-prefix completion))
2258 (and (equal pos (point))
2259 (not (equal completion ""))
2260 (add-text-properties 0 1 '(cursor t) completion))
2262 (overlay-put company-preview-overlay 'display
2263 (concat completion (unless (eq pos (point-max))
2264 (buffer-substring pos (1+ pos)))))
2265 (overlay-put company-preview-overlay 'window (selected-window))))
2267 (defun company-preview-hide ()
2268 (when company-preview-overlay
2269 (delete-overlay company-preview-overlay)
2270 (setq company-preview-overlay nil)))
2272 (defun company-preview-frontend (command)
2273 "`company-mode' front-end showing the selection as if it had been inserted."
2275 (pre-command (company-preview-hide))
2276 (post-command (company-preview-show-at-point (point)))
2277 (hide (company-preview-hide))))
2279 (defun company-preview-if-just-one-frontend (command)
2280 "`company-preview-frontend', but only shown for single candidates."
2281 (when (or (not (eq command 'post-command))
2282 (company--show-inline-p))
2283 (company-preview-frontend command)))
2285 (defun company--show-inline-p ()
2286 (and (not (cdr company-candidates))
2288 (string-prefix-p company-prefix company-common
2289 (company-call-backend 'ignore-case))))
2291 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2293 (defvar company-echo-last-msg nil)
2294 (make-variable-buffer-local 'company-echo-last-msg)
2296 (defvar company-echo-timer nil)
2298 (defvar company-echo-delay .01)
2300 (defun company-echo-show (&optional getter)
2302 (setq company-echo-last-msg (funcall getter)))
2303 (let ((message-log-max nil))
2304 (if company-echo-last-msg
2305 (message "%s" company-echo-last-msg)
2308 (defun company-echo-show-soon (&optional getter)
2309 (when company-echo-timer
2310 (cancel-timer company-echo-timer))
2311 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
2313 (defsubst company-echo-show-when-idle (&optional getter)
2314 (when (sit-for company-echo-delay)
2315 (company-echo-show getter)))
2317 (defun company-echo-format ()
2319 (let ((limit (window-width (minibuffer-window)))
2321 ;; Roll to selection.
2322 (candidates (nthcdr company-selection company-candidates))
2323 (i (if company-show-numbers company-selection 99999))
2327 (setq comp (company-reformat (pop candidates))
2328 len (+ len 1 (length comp)))
2332 (setq comp (propertize (format "%d: %s" i comp)
2333 'face 'company-echo))
2336 (add-text-properties 3 (+ 3 (length company-common))
2337 '(face company-echo-common) comp))
2338 (setq comp (propertize comp 'face 'company-echo))
2339 (add-text-properties 0 (length company-common)
2340 '(face company-echo-common) comp))
2342 (setq candidates nil)
2345 (mapconcat 'identity (nreverse msg) " ")))
2347 (defun company-echo-strip-common-format ()
2349 (let ((limit (window-width (minibuffer-window)))
2350 (len (+ (length company-prefix) 2))
2351 ;; Roll to selection.
2352 (candidates (nthcdr company-selection company-candidates))
2353 (i (if company-show-numbers company-selection 99999))
2357 (setq comp (company-strip-prefix (pop candidates))
2358 len (+ len 2 (length comp)))
2361 (setq comp (format "%s (%d)" comp i))
2365 (setq candidates nil)
2366 (push (propertize comp 'face 'company-echo) msg)))
2368 (concat (propertize company-prefix 'face 'company-echo-common) "{"
2369 (mapconcat 'identity (nreverse msg) ", ")
2372 (defun company-echo-hide ()
2373 (unless (equal company-echo-last-msg "")
2374 (setq company-echo-last-msg "")
2375 (company-echo-show)))
2377 (defun company-echo-frontend (command)
2378 "`company-mode' front-end showing the candidates in the echo area."
2380 (post-command (company-echo-show-soon 'company-echo-format))
2381 (hide (company-echo-hide))))
2383 (defun company-echo-strip-common-frontend (command)
2384 "`company-mode' front-end showing the candidates in the echo area."
2386 (post-command (company-echo-show-soon 'company-echo-strip-common-format))
2387 (hide (company-echo-hide))))
2389 (defun company-echo-metadata-frontend (command)
2390 "`company-mode' front-end showing the documentation in the echo area."
2392 (post-command (company-echo-show-when-idle 'company-fetch-metadata))
2393 (hide (company-echo-hide))))
2396 ;;; company.el ends here