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))
74 ;; FIXME: Use `user-error'.
75 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
76 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
77 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
78 (add-to-list 'debug-ignored-errors "^Company not ")
79 (add-to-list 'debug-ignored-errors "^No candidate number ")
80 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
81 (add-to-list 'debug-ignored-errors "^No other back-end$")
84 "Extensible inline text completion mechanism"
89 (defface company-tooltip
90 '((default :foreground "black")
91 (((class color) (min-colors 88) (background light))
92 (:background "cornsilk"))
93 (((class color) (min-colors 88) (background dark))
94 (:background "yellow")))
95 "Face used for the tooltip.")
97 (defface company-tooltip-selection
98 '((default :inherit company-tooltip)
99 (((class color) (min-colors 88) (background light))
100 (:background "light blue"))
101 (((class color) (min-colors 88) (background dark))
102 (:background "orange1"))
103 (t (:background "green")))
104 "Face used for the selection in the tooltip.")
106 (defface company-tooltip-mouse
107 '((default :inherit highlight))
108 "Face used for the tooltip item under the mouse.")
110 (defface company-tooltip-common
111 '((default :inherit company-tooltip)
112 (((background light))
113 :foreground "darkred")
116 "Face used for the common completion in the tooltip.")
118 (defface company-tooltip-common-selection
119 '((default :inherit company-tooltip-selection)
120 (((background light))
121 :foreground "darkred")
124 "Face used for the selected common completion in the tooltip.")
126 (defface company-tooltip-annotation
127 '((default :inherit company-tooltip)
128 (((background light))
129 :foreground "firebrick4")
132 "Face used for the annotation in the tooltip.")
134 (defface company-scrollbar-fg
135 '((((background light))
136 :background "darkred")
139 "Face used for the tooltip scrollbar thumb.")
141 (defface company-scrollbar-bg
142 '((default :inherit company-tooltip)
143 (((background light))
147 "Face used for the tooltip scrollbar background.")
149 (defface company-preview
150 '((((background light))
151 :inherit company-tooltip-selection)
154 :foreground "wheat"))
155 "Face used for the completion preview.")
157 (defface company-preview-common
158 '((((background light))
159 :inherit company-tooltip-selection)
161 :inherit company-preview
163 "Face used for the common part of the completion preview.")
165 (defface company-preview-search
166 '((((background light))
167 :inherit company-tooltip-common-selection)
169 :inherit company-preview
170 :background "blue1"))
171 "Face used for the search string in the completion preview.")
173 (defface company-echo nil
174 "Face used for completions in the echo area.")
176 (defface company-echo-common
177 '((((background dark)) (:foreground "firebrick1"))
178 (((background light)) (:background "firebrick4")))
179 "Face used for the common part of completions in the echo area.")
181 (defun company-frontends-set (variable value)
183 (let ((remainder value))
184 (setcdr remainder (delq (car remainder) (cdr remainder))))
185 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
186 (memq 'company-pseudo-tooltip-frontend value)
187 (error "Pseudo tooltip frontend cannot be used twice"))
188 (and (memq 'company-preview-if-just-one-frontend value)
189 (memq 'company-preview-frontend value)
190 (error "Preview frontend cannot be used twice"))
191 (and (memq 'company-echo value)
192 (memq 'company-echo-metadata-frontend value)
193 (error "Echo area cannot be used twice"))
194 ;; preview must come last
195 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
197 (setq value (append (delq f value) (list f)))))
198 (set variable value))
200 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
201 company-preview-if-just-one-frontend
202 company-echo-metadata-frontend)
203 "The list of active front-ends (visualizations).
204 Each front-end is a function that takes one argument. It is called with
205 one of the following arguments:
207 `show': When the visualization should start.
209 `hide': When the visualization should end.
211 `update': When the data has been updated.
213 `pre-command': Before every command that is executed while the
214 visualization is active.
216 `post-command': After every command that is executed while the
217 visualization is active.
219 The visualized data is stored in `company-prefix', `company-candidates',
220 `company-common', `company-selection', `company-point' and
221 `company-search-string'."
222 :set 'company-frontends-set
223 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
224 (const :tag "echo, strip common"
225 company-echo-strip-common-frontend)
226 (const :tag "show echo meta-data in echo"
227 company-echo-metadata-frontend)
228 (const :tag "pseudo tooltip"
229 company-pseudo-tooltip-frontend)
230 (const :tag "pseudo tooltip, multiple only"
231 company-pseudo-tooltip-unless-just-one-frontend)
232 (const :tag "preview" company-preview-frontend)
233 (const :tag "preview, unique only"
234 company-preview-if-just-one-frontend)
235 (function :tag "custom function" nil))))
237 (defcustom company-tooltip-limit 10
238 "The maximum number of candidates in the tooltip"
241 (defcustom company-tooltip-minimum 6
242 "The minimum height of the tooltip.
243 If this many lines are not available, prefer to display the tooltip above."
246 (defcustom company-tooltip-margin 1
247 "Width of margin columns to show around the toolip."
250 (defcustom company-tooltip-offset-display 'scrollbar
251 "Method using which the tooltip displays scrolling position.
252 `scrollbar' means draw a scrollbar to the right of the items.
253 `lines' means wrap items in lines with \"before\" and \"after\" counters."
254 :type '(choice (const :tag "Scrollbar" scrollbar)
255 (const :tag "Two lines" lines)))
257 (defcustom company-tooltip-align-annotations t
258 "When non-nil, align annotations to the right tooltip border."
261 (defvar company-safe-backends
262 '((company-abbrev . "Abbrev")
263 (company-bbdb . "BBDB")
264 (company-capf . "completion-at-point-functions")
265 (company-clang . "Clang")
266 (company-cmake . "CMake")
267 (company-css . "CSS")
268 (company-dabbrev . "dabbrev for plain text")
269 (company-dabbrev-code . "dabbrev for code")
270 (company-eclim . "Eclim (an Eclipse interface)")
271 (company-elisp . "Emacs Lisp")
272 (company-etags . "etags")
273 (company-files . "Files")
274 (company-gtags . "GNU Global")
275 (company-ispell . "Ispell")
276 (company-keywords . "Programming language keywords")
277 (company-nxml . "nxml")
278 (company-oddmuse . "Oddmuse")
279 (company-pysmell . "PySmell")
280 (company-ropemacs . "ropemacs")
281 (company-semantic . "Semantic")
282 (company-tempo . "Tempo templates")
283 (company-xcode . "Xcode")))
284 (put 'company-safe-backends 'risky-local-variable t)
286 (defun company-safe-backends-p (backends)
287 (and (consp backends)
288 (not (dolist (backend backends)
289 (unless (if (consp backend)
290 (company-safe-backends-p backend)
291 (assq backend company-safe-backends))
294 (defvar company--include-capf (version< "24.3.50" emacs-version))
296 (defcustom company-backends `(,@(unless company--include-capf
297 (list 'company-elisp))
299 company-nxml company-css
300 company-eclim company-semantic company-clang
301 company-xcode company-ropemacs company-cmake
302 ,@(when company--include-capf
303 (list 'company-capf))
304 (company-gtags company-etags company-dabbrev-code
306 company-oddmuse company-files company-dabbrev)
307 "The list of active back-ends (completion engines).
308 Each list elements can itself be a list of back-ends. In that case their
309 completions are merged. Otherwise only the first matching back-end returns
312 `company-begin-backend' can be used to start a specific back-end,
313 `company-other-backend' will skip to the next matching back-end in the list.
315 Each back-end is a function that takes a variable number of arguments.
316 The first argument is the command requested from the back-end. It is one
319 `prefix': The back-end should return the text to be completed. It must be
320 text immediately before point. Returning nil passes control to the next
321 back-end. The function should return `stop' if it should complete but
322 cannot \(e.g. if it is in the middle of a string\). Instead of a string,
323 the back-end may return a cons where car is the prefix and cdr is used in
324 `company-minimum-prefix-length' test. It must be either number or t, and
325 in the latter case the test automatically succeeds.
327 `candidates': The second argument is the prefix to be completed. The
328 return value should be a list of candidates that match the prefix.
330 Non-prefix matches are also supported (candidates that don't start with the
331 prefix, but match it in some backend-defined way). Backends that use this
332 feature must disable cache (return t to `no-cache') and should also respond
337 `sorted': Return t here to indicate that the candidates are sorted and will
338 not need to be sorted again.
340 `duplicates': If non-nil, company will take care of removing duplicates
343 `no-cache': Usually company doesn't ask for candidates again as completion
344 progresses, unless the back-end returns t for this command. The second
345 argument is the latest prefix.
347 `meta': The second argument is a completion candidate. Return a (short)
348 documentation string for it.
350 `doc-buffer': The second argument is a completion candidate. Return a
351 buffer with documentation for it. Preferably use `company-doc-buffer',
353 `location': The second argument is a completion candidate. Return the cons
354 of buffer and buffer location, or of file and line number where the
355 completion candidate was defined.
357 `annotation': The second argument is a completion candidate. Return a
358 string to be displayed inline with the candidate in the popup. If
359 duplicates are removed by company, candidates with equal string values will
360 be kept if they have different annotations. For that to work properly,
361 backends should store the related information on candidates using text
364 `match': The second argument is a completion candidate. Backends that
365 provide non-prefix completions should return the position of the end of
366 text in the candidate that matches `prefix'. It will be used when
369 `require-match': If this returns t, the user is not allowed to enter
370 anything not offered as a candidate. Use with care! The default value nil
371 gives the user that choice with `company-require-match'. Return value
372 `never' overrides that option the other way around.
374 `init': Called once for each buffer. The back-end can check for external
375 programs and files and load any required libraries. Raising an error here
376 will show up in message log once, and the back-end will not be used for
379 `post-completion': Called after a completion candidate has been inserted
380 into the buffer. The second argument is the candidate. Can be used to
381 modify it, e.g. to expand a snippet.
383 The back-end should return nil for all commands it does not support or
384 does not know about. It should also be callable interactively and use
385 `company-begin-backend' to start itself in that case."
389 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
390 company-safe-backends)
391 (symbol :tag "User defined")
392 (repeat :tag "Merged Back-ends"
393 (choice :tag "Back-end"
394 ,@(mapcar (lambda (b)
395 `(const :tag ,(cdr b) ,(car b)))
396 company-safe-backends)
397 (symbol :tag "User defined"))))))
399 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
401 (defcustom company-transformers nil
402 "Functions to change the list of candidates received from backends,
403 after sorting and removal of duplicates (if appropriate).
404 Each function gets called with the return value of the previous one."
406 (const :tag "None" nil)
407 (const :tag "Sort by occurrence" (company-sort-by-occurrence))
408 (repeat :tag "User defined" (function))))
410 (defcustom company-completion-started-hook nil
411 "Hook run when company starts completing.
412 The hook is called with one argument that is non-nil if the completion was
416 (defcustom company-completion-cancelled-hook nil
417 "Hook run when company cancels completing.
418 The hook is called with one argument that is non-nil if the completion was
422 (defcustom company-completion-finished-hook nil
423 "Hook run when company successfully completes.
424 The hook is called with the selected candidate as an argument.
426 If you indend to use it to post-process candidates from a specific
427 back-end, consider using the `post-completion' command instead."
430 (defcustom company-minimum-prefix-length 3
431 "The minimum prefix length for idle completion."
432 :type '(integer :tag "prefix length"))
434 (defcustom company-require-match 'company-explicit-action-p
435 "If enabled, disallow non-matching input.
436 This can be a function do determine if a match is required.
438 This can be overridden by the back-end, if it returns t or `never' to
439 `require-match'. `company-auto-complete' also takes precedence over this."
440 :type '(choice (const :tag "Off" nil)
441 (function :tag "Predicate function")
442 (const :tag "On, if user interaction took place"
443 'company-explicit-action-p)
444 (const :tag "On" t)))
446 (defcustom company-auto-complete nil
447 "Determines when to auto-complete.
448 If this is enabled, all characters from `company-auto-complete-chars'
449 trigger insertion of the selected completion candidate.
450 This can also be a function."
451 :type '(choice (const :tag "Off" nil)
452 (function :tag "Predicate function")
453 (const :tag "On, if user interaction took place"
454 'company-explicit-action-p)
455 (const :tag "On" t)))
457 (defcustom company-auto-complete-chars '(?\ ?\) ?.)
458 "Determines which characters trigger auto-completion.
459 See `company-auto-complete'. If this is a string, each string character
460 tiggers auto-completion. If it is a list of syntax description characters (see
461 `modify-syntax-entry'), all characters with that syntax auto-complete.
463 This can also be a function, which is called with the new input and should
464 return non-nil if company should auto-complete.
466 A character that is part of a valid candidate never triggers auto-completion."
467 :type '(choice (string :tag "Characters")
469 (const :tag "Whitespace" ?\ )
470 (const :tag "Symbol" ?_)
471 (const :tag "Opening parentheses" ?\()
472 (const :tag "Closing parentheses" ?\))
473 (const :tag "Word constituent" ?w)
474 (const :tag "Punctuation." ?.)
475 (const :tag "String quote." ?\")
476 (const :tag "Paired delimiter." ?$)
477 (const :tag "Expression quote or prefix operator." ?\')
478 (const :tag "Comment starter." ?<)
479 (const :tag "Comment ender." ?>)
480 (const :tag "Character-quote." ?/)
481 (const :tag "Generic string fence." ?|)
482 (const :tag "Generic comment fence." ?!))
483 (function :tag "Predicate function")))
485 (defcustom company-idle-delay .7
486 "The idle delay in seconds until completion starts automatically.
487 A value of nil means no idle completion, t means show candidates
488 immediately when a prefix of `company-minimum-prefix-length' is reached."
489 :type '(choice (const :tag "never (nil)" nil)
490 (const :tag "immediate (t)" t)
491 (number :tag "seconds")))
493 (defcustom company-begin-commands '(self-insert-command org-self-insert-command)
494 "A list of commands after which idle completion is allowed.
495 If this is t, it can show completions after any command. See
496 `company-idle-delay'.
498 Alternatively, any command with a non-nil `company-begin' property is
499 treated as if it was on this list."
500 :type '(choice (const :tag "Any command" t)
501 (const :tag "Self insert command" '(self-insert-command))
502 (repeat :tag "Commands" function)))
504 (defcustom company-continue-commands '(not save-buffer save-some-buffers
505 save-buffers-kill-terminal
506 save-buffers-kill-emacs)
507 "A list of commands that are allowed during completion.
508 If this is t, or if `company-begin-commands' is t, any command is allowed.
509 Otherwise, the value must be a list of symbols. If it starts with `not',
510 the cdr is the list of commands that abort completion. Otherwise, all
511 commands except those in that list, or in `company-begin-commands', or
512 commands in the `company-' namespace, abort completion."
513 :type '(choice (const :tag "Any command" t)
514 (cons :tag "Any except"
516 (repeat :tag "Commands" function))
517 (repeat :tag "Commands" function)))
519 (defcustom company-show-numbers nil
520 "If enabled, show quick-access numbers for the first ten candidates."
521 :type '(choice (const :tag "off" nil)
522 (const :tag "on" t)))
524 (defcustom company-selection-wrap-around nil
525 "If enabled, selecting item before first or after last wraps around."
526 :type '(choice (const :tag "off" nil)
527 (const :tag "on" t)))
529 (defvar company-end-of-buffer-workaround t
530 "Work around a visualization bug when completing at the end of the buffer.
531 The work-around consists of adding a newline.")
533 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
535 (defvar company-mode-map (make-sparse-keymap)
536 "Keymap used by `company-mode'.")
538 (defvar company-active-map
539 (let ((keymap (make-sparse-keymap)))
540 (define-key keymap "\e\e\e" 'company-abort)
541 (define-key keymap "\C-g" 'company-abort)
542 (define-key keymap (kbd "M-n") 'company-select-next)
543 (define-key keymap (kbd "M-p") 'company-select-previous)
544 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
545 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
546 (define-key keymap [down-mouse-1] 'ignore)
547 (define-key keymap [down-mouse-3] 'ignore)
548 (define-key keymap [mouse-1] 'company-complete-mouse)
549 (define-key keymap [mouse-3] 'company-select-mouse)
550 (define-key keymap [up-mouse-1] 'ignore)
551 (define-key keymap [up-mouse-3] 'ignore)
552 (define-key keymap [return] 'company-complete-selection)
553 (define-key keymap (kbd "RET") 'company-complete-selection)
554 (define-key keymap [tab] 'company-complete-common)
555 (define-key keymap (kbd "TAB") 'company-complete-common)
556 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
557 (define-key keymap "\C-w" 'company-show-location)
558 (define-key keymap "\C-s" 'company-search-candidates)
559 (define-key keymap "\C-\M-s" 'company-filter-candidates)
561 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
562 `(lambda () (interactive) (company-complete-number ,i))))
565 "Keymap that is enabled during an active completion.")
567 (defvar company--disabled-backends nil)
569 (defun company-init-backend (backend)
570 (and (symbolp backend)
571 (not (fboundp backend))
572 (ignore-errors (require backend nil t)))
574 (if (or (symbolp backend)
578 (funcall backend 'init)
579 (put backend 'company-init t))
581 (put backend 'company-init 'failed)
582 (unless (memq backend company--disabled-backends)
583 (message "Company back-end '%s' could not be initialized:\n%s"
584 backend (error-message-string err)))
585 (pushnew backend company--disabled-backends)
587 (mapc 'company-init-backend backend)))
589 (defvar company-default-lighter " company")
591 (defvar company-lighter company-default-lighter)
592 (make-variable-buffer-local 'company-lighter)
595 (define-minor-mode company-mode
596 "\"complete anything\"; is an in-buffer completion framework.
597 Completion starts automatically, depending on the values
598 `company-idle-delay' and `company-minimum-prefix-length'.
600 Completion can be controlled with the commands:
601 `company-complete-common', `company-complete-selection', `company-complete',
602 `company-select-next', `company-select-previous'. If these commands are
603 called before `company-idle-delay', completion will also start.
605 Completions can be searched with `company-search-candidates' or
606 `company-filter-candidates'. These can be used while completion is
609 The completion data is retrieved using `company-backends' and displayed
610 using `company-frontends'. If you want to start a specific back-end, call
611 it interactively or use `company-begin-backend'.
613 regular keymap (`company-mode-map'):
616 keymap during active completions (`company-active-map'):
618 \\{company-active-map}"
619 nil company-lighter company-mode-map
622 (add-hook 'pre-command-hook 'company-pre-command nil t)
623 (add-hook 'post-command-hook 'company-post-command nil t)
624 (mapc 'company-init-backend company-backends))
625 (remove-hook 'pre-command-hook 'company-pre-command t)
626 (remove-hook 'post-command-hook 'company-post-command t)
628 (kill-local-variable 'company-point)))
630 (defcustom company-global-modes t
631 "Modes for which `company-mode' mode is turned on by `global-company-mode'.
632 If nil, means no modes. If t, then all major modes have it turned on.
633 If a list, it should be a list of `major-mode' symbol names for which
634 `company-mode' should be automatically turned on. The sense of the list is
635 negated if it begins with `not'. For example:
637 means that `company-mode' is turned on for buffers in C and C++ modes only.
639 means that `company-mode' is always turned on except in `message-mode' buffers."
640 :type '(choice (const :tag "none" nil)
642 (set :menu-tag "mode specific" :tag "modes"
644 (const :tag "Except" not)
645 (repeat :inline t (symbol :tag "mode")))))
648 (define-globalized-minor-mode global-company-mode company-mode company-mode-on)
650 (defun company-mode-on ()
651 (when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s)))
652 (cond ((eq company-global-modes t)
654 ((eq (car-safe company-global-modes) 'not)
655 (not (memq major-mode (cdr company-global-modes))))
656 (t (memq major-mode company-global-modes))))
659 (defsubst company-assert-enabled ()
661 (company-uninstall-map)
662 (error "Company not enabled")))
664 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
666 (defvar company-my-keymap nil)
667 (make-variable-buffer-local 'company-my-keymap)
669 (defvar company-emulation-alist '((t . nil)))
671 (defsubst company-enable-overriding-keymap (keymap)
672 (company-uninstall-map)
673 (setq company-my-keymap keymap))
675 (defun company-ensure-emulation-alist ()
676 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
677 (setq emulation-mode-map-alists
678 (cons 'company-emulation-alist
679 (delq 'company-emulation-alist emulation-mode-map-alists)))))
681 (defun company-install-map ()
682 (unless (or (cdar company-emulation-alist)
683 (null company-my-keymap))
684 (setf (cdar company-emulation-alist) company-my-keymap)))
686 (defun company-uninstall-map ()
687 (setf (cdar company-emulation-alist) nil))
690 ;; Emacs calculates the active keymaps before reading the event. That means we
691 ;; cannot change the keymap from a timer. So we send a bogus command.
692 ;; XXX: Seems not to be needed anymore in Emacs 24.4
693 (defun company-ignore ()
695 (setq this-command last-command))
697 (global-set-key '[31415926] 'company-ignore)
699 (defun company-input-noop ()
700 (push 31415926 unread-command-events))
702 (defun company--column (&optional pos)
704 (when pos (goto-char pos))
708 (narrow-to-region (point) (point-max))
709 (let ((prefix (get-text-property (point) 'line-prefix)))
710 (if prefix (length prefix) 0)))
713 (defun company--row (&optional pos)
715 (when pos (goto-char pos))
716 (count-screen-lines (window-start)
717 (progn (vertical-motion 0) (point)))))
719 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
721 (defvar company-backend nil)
722 (make-variable-buffer-local 'company-backend)
724 (defun company-grab (regexp &optional expression limit)
725 (when (looking-back regexp limit)
726 (or (match-string-no-properties (or expression 0)) "")))
728 (defun company-grab-line (regexp &optional expression)
729 (company-grab regexp expression (point-at-bol)))
731 (defun company-grab-symbol ()
732 (if (looking-at "\\_>")
733 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
735 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
738 (defun company-grab-word ()
739 (if (looking-at "\\>")
740 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
742 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
745 (defun company-in-string-or-comment ()
746 (let ((ppss (syntax-ppss)))
747 (or (car (setq ppss (nthcdr 3 ppss)))
748 (car (setq ppss (cdr ppss)))
751 (if (fboundp 'locate-dominating-file)
752 (defalias 'company-locate-dominating-file 'locate-dominating-file)
753 (defun company-locate-dominating-file (file name)
755 (let ((dir (file-name-directory file))
757 (while (not (equal dir prev-dir))
758 (when (file-exists-p (expand-file-name name dir))
761 dir (file-name-directory (directory-file-name dir))))))))
763 (defun company-call-backend (&rest args)
764 (if (functionp company-backend)
765 (apply company-backend args)
766 (apply 'company--multi-backend-adapter company-backend args)))
768 (defun company--multi-backend-adapter (backends command &rest args)
769 (let ((backends (loop for b in backends
770 when (not (and (symbolp b)
771 (eq 'failed (get b 'company-init))))
775 (loop for backend in backends
776 when (equal (funcall backend 'prefix)
778 append (apply backend 'candidates args)))
783 (dolist (backend backends)
784 (when (setq value (apply backend command args))
785 (return value))))))))
787 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
789 (defvar company-prefix nil)
790 (make-variable-buffer-local 'company-prefix)
792 (defvar company-candidates nil)
793 (make-variable-buffer-local 'company-candidates)
795 (defvar company-candidates-length nil)
796 (make-variable-buffer-local 'company-candidates-length)
798 (defvar company-candidates-cache nil)
799 (make-variable-buffer-local 'company-candidates-cache)
801 (defvar company-candidates-predicate nil)
802 (make-variable-buffer-local 'company-candidates-predicate)
804 (defvar company-common nil)
805 (make-variable-buffer-local 'company-common)
807 (defvar company-selection 0)
808 (make-variable-buffer-local 'company-selection)
810 (defvar company-selection-changed nil)
811 (make-variable-buffer-local 'company-selection-changed)
813 (defvar company--explicit-action nil
814 "Non-nil, if explicit completion took place.")
815 (make-variable-buffer-local 'company--explicit-action)
817 (defvar company--auto-completion nil
818 "Non-nil when current candidate is being inserted automatically.
819 Controlled by `company-auto-complete'.")
821 (defvar company--point-max nil)
822 (make-variable-buffer-local 'company--point-max)
824 (defvar company-point nil)
825 (make-variable-buffer-local 'company-point)
827 (defvar company-timer nil)
829 (defvar company-added-newline nil)
830 (make-variable-buffer-local 'company-added-newline)
832 (defsubst company-strip-prefix (str)
833 (substring str (length company-prefix)))
835 (defun company--insert-candidate (candidate)
836 (setq candidate (substring-no-properties candidate))
837 ;; XXX: Return value we check here is subject to change.
838 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
839 (insert (company-strip-prefix candidate))
840 (delete-region (- (point) (length company-prefix)) (point))
843 (defmacro company-with-candidate-inserted (candidate &rest body)
844 "Evaluate BODY with CANDIDATE temporarily inserted.
845 This is a tool for back-ends that need candidates inserted before they
846 can retrieve meta-data for them."
848 `(let ((inhibit-modification-hooks t)
849 (inhibit-point-motion-hooks t)
850 (modified-p (buffer-modified-p)))
851 (company--insert-candidate ,candidate)
854 (delete-region company-point (point)))))
856 (defun company-explicit-action-p ()
857 "Return whether explicit completion action was taken by the user."
858 (or company--explicit-action
859 company-selection-changed))
861 (defun company-reformat (candidate)
862 ;; company-ispell needs this, because the results are always lower-case
863 ;; It's mory efficient to fix it only when they are displayed.
864 ;; FIXME: Adopt the current text's capitalization instead?
865 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
866 (concat company-prefix (substring candidate (length company-prefix)))
869 (defun company--should-complete ()
870 (and (not (or buffer-read-only overriding-terminal-local-map
871 overriding-local-map))
872 ;; Check if in the middle of entering a key combination.
873 (or (equal (this-command-keys-vector) [])
874 (not (keymapp (key-binding (this-command-keys-vector)))))
875 (eq company-idle-delay t)
876 (or (eq t company-begin-commands)
877 (memq this-command company-begin-commands)
878 (and (symbolp this-command) (get this-command 'company-begin)))
879 (not (and transient-mark-mode mark-active))))
881 (defun company--should-continue ()
882 (or (eq t company-begin-commands)
883 (eq t company-continue-commands)
884 (if (eq 'not (car company-continue-commands))
885 (not (memq this-command (cdr company-continue-commands)))
886 (or (memq this-command company-begin-commands)
887 (memq this-command company-continue-commands)
888 (string-match-p "\\`company-" (symbol-name this-command))))))
890 (defun company-call-frontends (command)
891 (dolist (frontend company-frontends)
893 (funcall frontend command)
894 (error (error "Company: Front-end %s error \"%s\" on command %s"
895 frontend (error-message-string err) command)))))
897 (defun company-set-selection (selection &optional force-update)
899 (if company-selection-wrap-around
900 (mod selection company-candidates-length)
901 (max 0 (min (1- company-candidates-length) selection))))
902 (when (or force-update (not (equal selection company-selection)))
903 (setq company-selection selection
904 company-selection-changed t)
905 (company-call-frontends 'update)))
907 (defun company-apply-predicate (candidates predicate)
909 (dolist (c candidates)
910 (when (funcall predicate c)
914 (defun company-update-candidates (candidates)
915 (setq company-candidates-length (length candidates))
916 (if (> company-selection 0)
917 ;; Try to restore the selection
918 (let ((selected (nth company-selection company-candidates)))
919 (setq company-selection 0
920 company-candidates candidates)
922 (while (and candidates (string< (pop candidates) selected))
923 (incf company-selection))
925 ;; Make sure selection isn't out of bounds.
926 (setq company-selection (min (1- company-candidates-length)
927 company-selection)))))
928 (setq company-selection 0
929 company-candidates candidates))
931 (push (cons company-prefix company-candidates) company-candidates-cache)
933 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
934 ;; We want to support non-prefix completion, so filtering is the
935 ;; responsibility of each respective backend, not ours.
936 ;; On the other hand, we don't want to replace non-prefix input in
937 ;; `company-complete-common'.
939 (if (cdr company-candidates)
940 (company--safe-candidate
941 (let ((common (try-completion company-prefix company-candidates)))
943 ;; Mulple equal strings, probably with different
947 (car company-candidates)))))
949 (defun company--safe-candidate (str)
950 ;; XXX: This feature is deprecated.
951 (or (company-call-backend 'crop str)
954 (defun company-calculate-candidates (prefix)
955 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
956 (ignore-case (company-call-backend 'ignore-case)))
958 (when company-candidates-cache
959 (let ((len (length prefix))
960 (completion-ignore-case ignore-case)
962 (dotimes (i (1+ len))
963 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
964 company-candidates-cache)))
965 (setq candidates (all-completions prefix prev))
967 ;; no cache match, call back-end
969 (setq candidates (company-call-backend 'candidates prefix))
970 (when company-candidates-predicate
972 (company-apply-predicate candidates
973 company-candidates-predicate)))
974 (unless (company-call-backend 'sorted)
975 (setq candidates (sort candidates 'string<)))
976 (when (company-call-backend 'duplicates)
977 (company--strip-duplicates candidates))))
978 (setq candidates (company--transform-candidates candidates))
980 (if (or (cdr candidates)
981 (not (eq t (compare-strings (car candidates) nil nil
982 prefix nil nil ignore-case))))
984 ;; Already completed and unique; don't start.
987 (defun company--strip-duplicates (candidates)
988 (let ((c2 candidates))
994 (while (let ((str2 (car c2)))
995 (if (not (equal str str2))
998 (setq anno (company-call-backend
1001 (company-call-backend
1002 'annotation str2))))
1006 (defun company--transform-candidates (candidates)
1007 (let ((c candidates))
1008 (dolist (tr company-transformers)
1009 (setq c (funcall tr c)))
1012 (defun company-sort-by-occurrence (candidates)
1013 "Sort CANDIDATES according to their occurrences.
1014 Searches for each in the currently visible part of the current buffer and
1015 gives priority to the closest ones above point, then closest ones below
1016 point. The rest of the list is appended unchanged.
1017 Keywords and function definition names are ignored."
1024 (progn (forward-line 0)
1025 (search-backward candidate (window-start) t)))
1027 (search-forward candidate (window-end) t)))
1028 (let ((beg (match-beginning 0))
1029 (end (match-end 0)))
1030 (when (save-excursion
1032 (and (not (memq (get-text-property (point) 'face)
1033 '(font-lock-function-name-face
1034 font-lock-keyword-face)))
1035 (let* ((prefix (company-call-backend 'prefix))
1036 (prefix (or (car-safe prefix) prefix)))
1037 (and (stringp prefix)
1038 (= (length prefix) (- end beg))))))
1039 (push (cons candidate (if (< beg (point))
1041 (- beg (window-start))))
1046 (mapcar #'car (sort occurs (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
1049 (defun company-idle-begin (buf win tick pos)
1051 (eq buf (current-buffer))
1052 (eq win (selected-window))
1053 (eq tick (buffer-chars-modified-tick))
1055 (not company-candidates)
1056 (not (equal (point) company-point))
1057 (let ((company-idle-delay t)
1058 (company-begin-commands t))
1060 (when company-candidates
1061 (when (version< emacs-version "24.3.50")
1062 (company-input-noop))
1063 (company-post-command)))))
1065 (defun company-auto-begin ()
1066 (company-assert-enabled)
1068 (not company-candidates)
1069 (let ((company-idle-delay t)
1070 (company-minimum-prefix-length 0)
1071 (company-begin-commands t))
1073 ;; Return non-nil if active.
1076 (defun company-manual-begin ()
1078 (setq company--explicit-action t)
1080 (company-auto-begin)
1081 (unless company-candidates
1082 (setq company--explicit-action nil))))
1084 (defun company-other-backend (&optional backward)
1085 (interactive (list current-prefix-arg))
1086 (company-assert-enabled)
1088 (let* ((after (cdr (member company-backend company-backends)))
1089 (before (cdr (member company-backend (reverse company-backends))))
1091 (append before (reverse after))
1092 (append after (reverse before)))))
1094 (dolist (backend next)
1095 (when (ignore-errors (company-begin-backend backend))
1097 (company-manual-begin))
1098 (unless company-candidates
1099 (error "No other back-end")))
1101 (defun company-require-match-p ()
1102 (let ((backend-value (company-call-backend 'require-match)))
1103 (or (eq backend-value t)
1104 (and (not (eq backend-value 'never))
1105 (if (functionp company-require-match)
1106 (funcall company-require-match)
1107 (eq company-require-match t))))))
1109 (defun company-auto-complete-p (input)
1110 "Return non-nil, if input starts with punctuation or parentheses."
1111 (and (if (functionp company-auto-complete)
1112 (funcall company-auto-complete)
1113 company-auto-complete)
1114 (if (functionp company-auto-complete-chars)
1115 (funcall company-auto-complete-chars input)
1116 (if (consp company-auto-complete-chars)
1117 (memq (char-syntax (string-to-char input))
1118 company-auto-complete-chars)
1119 (string-match (substring input 0 1) company-auto-complete-chars)))))
1121 (defun company--incremental-p ()
1122 (and (> (point) company-point)
1123 (> (point-max) company--point-max)
1124 (not (eq this-command 'backward-delete-char-untabify))
1125 (equal (buffer-substring (- company-point (length company-prefix))
1129 (defun company--continue-failed ()
1130 (let ((input (buffer-substring-no-properties (point) company-point)))
1132 ((company-auto-complete-p input)
1135 (goto-char company-point)
1136 (let ((company--auto-completion t))
1137 (company-complete-selection))
1139 ((company-require-match-p)
1140 ;; wrong incremental input, but required match
1141 (delete-char (- (length input)))
1143 (message "Matching input is required")
1145 ((equal company-prefix (car company-candidates))
1146 ;; last input was actually success
1147 (company-cancel company-prefix))
1148 (t (company-cancel)))))
1150 (defun company--good-prefix-p (prefix)
1151 (and (or (company-explicit-action-p)
1152 (unless (eq prefix 'stop)
1153 (or (eq (cdr-safe prefix) t)
1154 (>= (or (cdr-safe prefix) (length prefix))
1155 company-minimum-prefix-length))))
1156 (stringp (or (car-safe prefix) prefix))))
1158 (defun company--continue ()
1159 (when (company-call-backend 'no-cache company-prefix)
1160 ;; Don't complete existing candidates, fetch new ones.
1161 (setq company-candidates-cache nil))
1162 (let* ((new-prefix (company-call-backend 'prefix))
1163 (c (when (and (company--good-prefix-p new-prefix)
1164 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1165 (= (- (point) (length new-prefix))
1166 (- company-point (length company-prefix))))
1167 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1168 (company-calculate-candidates new-prefix))))
1171 ;; t means complete/unique.
1172 (company-cancel new-prefix))
1174 ;; incremental match
1175 (setq company-prefix new-prefix)
1176 (company-update-candidates c)
1178 ((not (company--incremental-p))
1180 (t (company--continue-failed)))))
1182 (defun company--begin-new ()
1184 (dolist (backend (if company-backend
1185 ;; prefer manual override
1186 (list company-backend)
1189 (if (or (symbolp backend)
1190 (functionp backend))
1191 (when (or (not (symbolp backend))
1192 (eq t (get backend 'company-init))
1193 (unless (get backend 'company-init)
1194 (company-init-backend backend)))
1195 (funcall backend 'prefix))
1196 (company--multi-backend-adapter backend 'prefix)))
1198 (when (company--good-prefix-p prefix)
1199 (setq prefix (or (car-safe prefix) prefix)
1200 company-backend backend
1201 c (company-calculate-candidates prefix))
1202 ;; t means complete/unique. We don't start, so no hooks.
1204 (when company--explicit-action
1205 (message "No completion found"))
1206 (setq company-prefix prefix)
1207 (when (symbolp backend)
1208 (setq company-lighter (concat " " (symbol-name backend))))
1209 (company-update-candidates c)
1210 (run-hook-with-args 'company-completion-started-hook
1211 (company-explicit-action-p))
1212 (company-call-frontends 'show)))
1215 (defun company-begin ()
1216 (or (and company-candidates (company--continue))
1217 (and (company--should-complete) (company--begin-new)))
1218 (when company-candidates
1219 (let ((modified (buffer-modified-p)))
1220 (when (and company-end-of-buffer-workaround (eobp))
1221 (save-excursion (insert "\n"))
1222 (setq company-added-newline
1223 (or modified (buffer-chars-modified-tick)))))
1224 (setq company-point (point)
1225 company--point-max (point-max))
1226 (company-ensure-emulation-alist)
1227 (company-enable-overriding-keymap company-active-map)
1228 (company-call-frontends 'update)))
1230 (defun company-cancel (&optional result)
1231 (and company-added-newline
1232 (> (point-max) (point-min))
1233 (let ((tick (buffer-chars-modified-tick)))
1234 (delete-region (1- (point-max)) (point-max))
1235 (equal tick company-added-newline))
1236 ;; Only set unmodified when tick remained the same since insert,
1237 ;; and the buffer wasn't modified before.
1238 (set-buffer-modified-p nil))
1239 (when company-prefix
1240 (if (stringp result)
1242 (company-call-backend 'pre-completion result)
1243 (run-hook-with-args 'company-completion-finished-hook result)
1244 (company-call-backend 'post-completion result))
1245 (run-hook-with-args 'company-completion-cancelled-hook result)))
1246 (setq company-added-newline nil
1249 company-candidates nil
1250 company-candidates-length nil
1251 company-candidates-cache nil
1252 company-candidates-predicate nil
1255 company-selection-changed nil
1256 company--explicit-action nil
1257 company-lighter company-default-lighter
1258 company--point-max nil
1261 (cancel-timer company-timer))
1262 (company-search-mode 0)
1263 (company-call-frontends 'hide)
1264 (company-enable-overriding-keymap nil)
1265 ;; Make return value explicit.
1268 (defun company-abort ()
1271 ;; Don't start again, unless started manually.
1272 (setq company-point (point)))
1274 (defun company-finish (result)
1275 (company--insert-candidate result)
1276 (company-cancel result)
1277 ;; Don't start again, unless started manually.
1278 (setq company-point (point)))
1280 (defsubst company-keep (command)
1281 (and (symbolp command) (get command 'company-keep)))
1283 (defun company-pre-command ()
1284 (unless (company-keep this-command)
1286 (when company-candidates
1287 (company-call-frontends 'pre-command)
1288 (unless (company--should-continue)
1290 (error (message "Company: An error occurred in pre-command")
1291 (message "%s" (error-message-string err))
1294 (cancel-timer company-timer)
1295 (setq company-timer nil))
1296 (company-uninstall-map))
1298 (defun company-post-command ()
1299 (unless (company-keep this-command)
1302 (unless (equal (point) company-point)
1304 (if company-candidates
1305 (company-call-frontends 'post-command)
1306 (and (numberp company-idle-delay)
1307 (or (eq t company-begin-commands)
1308 (memq this-command company-begin-commands))
1310 (run-with-timer company-idle-delay nil
1312 (current-buffer) (selected-window)
1313 (buffer-chars-modified-tick) (point))))))
1314 (error (message "Company: An error occurred in post-command")
1315 (message "%s" (error-message-string err))
1317 (company-install-map))
1319 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1321 (defvar company-search-string nil)
1322 (make-variable-buffer-local 'company-search-string)
1324 (defvar company-search-lighter " Search: \"\"")
1325 (make-variable-buffer-local 'company-search-lighter)
1327 (defvar company-search-old-map nil)
1328 (make-variable-buffer-local 'company-search-old-map)
1330 (defvar company-search-old-selection 0)
1331 (make-variable-buffer-local 'company-search-old-selection)
1333 (defun company-search (text lines)
1334 (let ((quoted (regexp-quote text))
1336 (dolist (line lines)
1337 (when (string-match quoted line (length company-prefix))
1341 (defun company-search-printing-char ()
1343 (company-search-assert-enabled)
1344 (setq company-search-string
1345 (concat (or company-search-string "") (string last-command-event))
1346 company-search-lighter (concat " Search: \"" company-search-string
1348 (let ((pos (company-search company-search-string
1349 (nthcdr company-selection company-candidates))))
1352 (company-set-selection (+ company-selection pos) t))))
1354 (defun company-search-repeat-forward ()
1355 "Repeat the incremental search in completion candidates forward."
1357 (company-search-assert-enabled)
1358 (let ((pos (company-search company-search-string
1359 (cdr (nthcdr company-selection
1360 company-candidates)))))
1363 (company-set-selection (+ company-selection pos 1) t))))
1365 (defun company-search-repeat-backward ()
1366 "Repeat the incremental search in completion candidates backwards."
1368 (company-search-assert-enabled)
1369 (let ((pos (company-search company-search-string
1370 (nthcdr (- company-candidates-length
1372 (reverse company-candidates)))))
1375 (company-set-selection (- company-selection pos 1) t))))
1377 (defun company-create-match-predicate ()
1378 (setq company-candidates-predicate
1379 `(lambda (candidate)
1380 ,(if company-candidates-predicate
1381 `(and (string-match ,company-search-string candidate)
1382 (funcall ,company-candidates-predicate
1384 `(string-match ,company-search-string candidate))))
1385 (company-update-candidates
1386 (company-apply-predicate company-candidates company-candidates-predicate))
1387 ;; Invalidate cache.
1388 (setq company-candidates-cache (cons company-prefix company-candidates)))
1390 (defun company-filter-printing-char ()
1392 (company-search-assert-enabled)
1393 (company-search-printing-char)
1394 (company-create-match-predicate)
1395 (company-call-frontends 'update))
1397 (defun company-search-kill-others ()
1398 "Limit the completion candidates to the ones matching the search string."
1400 (company-search-assert-enabled)
1401 (company-create-match-predicate)
1402 (company-search-mode 0)
1403 (company-call-frontends 'update))
1405 (defun company-search-abort ()
1406 "Abort searching the completion candidates."
1408 (company-search-assert-enabled)
1409 (company-set-selection company-search-old-selection t)
1410 (company-search-mode 0))
1412 (defun company-search-other-char ()
1414 (company-search-assert-enabled)
1415 (company-search-mode 0)
1416 (company--unread-last-input))
1418 (defvar company-search-map
1420 (keymap (make-keymap)))
1421 (if (fboundp 'max-char)
1422 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1423 'company-search-printing-char)
1425 ;; obsolete in Emacs 23
1426 (let ((l (generic-character-list))
1427 (table (nth 1 keymap)))
1429 (set-char-table-default table (car l) 'company-search-printing-char)
1430 (setq l (cdr l))))))
1431 (define-key keymap [t] 'company-search-other-char)
1433 (define-key keymap (make-string 1 i) 'company-search-other-char)
1436 (define-key keymap (vector i) 'company-search-printing-char)
1438 (let ((meta-map (make-sparse-keymap)))
1439 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1440 (define-key keymap [escape] meta-map))
1441 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1442 (define-key keymap "\e\e\e" 'company-search-other-char)
1443 (define-key keymap [escape escape escape] 'company-search-other-char)
1445 (define-key keymap "\C-g" 'company-search-abort)
1446 (define-key keymap "\C-s" 'company-search-repeat-forward)
1447 (define-key keymap "\C-r" 'company-search-repeat-backward)
1448 (define-key keymap "\C-o" 'company-search-kill-others)
1450 "Keymap used for incrementally searching the completion candidates.")
1452 (define-minor-mode company-search-mode
1453 "Search mode for completion candidates.
1454 Don't start this directly, use `company-search-candidates' or
1455 `company-filter-candidates'."
1456 nil company-search-lighter nil
1457 (if company-search-mode
1458 (if (company-manual-begin)
1460 (setq company-search-old-selection company-selection)
1461 (company-call-frontends 'update))
1462 (setq company-search-mode nil))
1463 (kill-local-variable 'company-search-string)
1464 (kill-local-variable 'company-search-lighter)
1465 (kill-local-variable 'company-search-old-selection)
1466 (company-enable-overriding-keymap company-active-map)))
1468 (defun company-search-assert-enabled ()
1469 (company-assert-enabled)
1470 (unless company-search-mode
1471 (company-uninstall-map)
1472 (error "Company not in search mode")))
1474 (defun company-search-candidates ()
1475 "Start searching the completion candidates incrementally.
1477 \\<company-search-map>Search can be controlled with the commands:
1478 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1479 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1480 - `company-search-abort' (\\[company-search-abort])
1482 Regular characters are appended to the search string.
1484 The command `company-search-kill-others' (\\[company-search-kill-others])
1485 uses the search string to limit the completion candidates."
1487 (company-search-mode 1)
1488 (company-enable-overriding-keymap company-search-map))
1490 (defvar company-filter-map
1491 (let ((keymap (make-keymap)))
1492 (define-key keymap [remap company-search-printing-char]
1493 'company-filter-printing-char)
1494 (set-keymap-parent keymap company-search-map)
1496 "Keymap used for incrementally searching the completion candidates.")
1498 (defun company-filter-candidates ()
1499 "Start filtering the completion candidates incrementally.
1500 This works the same way as `company-search-candidates' immediately
1501 followed by `company-search-kill-others' after each input."
1503 (company-search-mode 1)
1504 (company-enable-overriding-keymap company-filter-map))
1506 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1508 (defun company-select-next ()
1509 "Select the next candidate in the list."
1511 (when (company-manual-begin)
1512 (company-set-selection (1+ company-selection))))
1514 (defun company-select-previous ()
1515 "Select the previous candidate in the list."
1517 (when (company-manual-begin)
1518 (company-set-selection (1- company-selection))))
1520 (defun company-select-next-or-abort ()
1521 "Select the next candidate if more than one, else abort
1522 and invoke the normal binding."
1524 (if (> company-candidates-length 1)
1525 (company-select-next)
1527 (company--unread-last-input)))
1529 (defun company-select-previous-or-abort ()
1530 "Select the previous candidate if more than one, else abort
1531 and invoke the normal binding."
1533 (if (> company-candidates-length 1)
1534 (company-select-previous)
1536 (company--unread-last-input)))
1538 (defvar company-pseudo-tooltip-overlay)
1540 (defvar company-tooltip-offset)
1542 (defun company--inside-tooltip-p (event-col-row row height)
1543 (let* ((ovl company-pseudo-tooltip-overlay)
1544 (column (overlay-get ovl 'company-column))
1545 (width (overlay-get ovl 'company-width))
1546 (evt-col (car event-col-row))
1547 (evt-row (cdr event-col-row)))
1548 (and (>= evt-col column)
1549 (< evt-col (+ column width))
1551 (and (> evt-row row)
1552 (<= evt-row (+ row height) ))
1553 (and (< evt-row row)
1554 (>= evt-row (+ row height)))))))
1556 (defun company--event-col-row (event)
1557 (let* ((col-row (posn-actual-col-row (event-start event)))
1559 (row (cdr col-row)))
1560 (incf col (window-hscroll))
1561 (and header-line-format
1562 (version< "24" emacs-version)
1566 (defun company-select-mouse (event)
1567 "Select the candidate picked by the mouse."
1569 (let ((event-col-row (company--event-col-row event))
1570 (ovl-row (company--row))
1571 (ovl-height (and company-pseudo-tooltip-overlay
1572 (min (overlay-get company-pseudo-tooltip-overlay
1574 company-candidates-length))))
1576 (company--inside-tooltip-p event-col-row ovl-row ovl-height))
1578 (company-set-selection (+ (cdr event-col-row)
1579 (1- company-tooltip-offset)
1580 (if (and (eq company-tooltip-offset-display 'lines)
1581 (not (zerop company-tooltip-offset)))
1584 (if (< ovl-height 0)
1589 (company--unread-last-input)
1592 (defun company-complete-mouse (event)
1593 "Insert the candidate picked by the mouse."
1595 (when (company-select-mouse event)
1596 (company-complete-selection)))
1598 (defun company-complete-selection ()
1599 "Insert the selected candidate."
1601 (when (company-manual-begin)
1602 (let ((result (nth company-selection company-candidates)))
1603 (when company--auto-completion
1604 (setq result (company--safe-candidate result)))
1605 (company-finish result))))
1607 (defun company-complete-common ()
1608 "Insert the common part of all candidates."
1610 (when (company-manual-begin)
1611 (if (and (not (cdr company-candidates))
1612 (equal company-common (car company-candidates)))
1613 (company-complete-selection)
1614 (when company-common
1615 (company--insert-candidate company-common)))))
1617 (defun company-complete ()
1618 "Insert the common part of all candidates or the current selection.
1619 The first time this is called, the common part is inserted, the second
1620 time, or when the selection has been changed, the selected candidate is
1623 (when (company-manual-begin)
1624 (if (or company-selection-changed
1625 (eq last-command 'company-complete-common))
1626 (call-interactively 'company-complete-selection)
1627 (call-interactively 'company-complete-common)
1628 (setq this-command 'company-complete-common))))
1630 (defun company-complete-number (n)
1631 "Insert the Nth candidate.
1632 To show the number next to the candidates in some back-ends, enable
1633 `company-show-numbers'."
1634 (when (company-manual-begin)
1635 (and (< n 1) (> n company-candidates-length)
1636 (error "No candidate number %d" n))
1638 (company-finish (nth n company-candidates))))
1640 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1642 (defconst company-space-strings-limit 100)
1644 (defconst company-space-strings
1646 (dotimes (i company-space-strings-limit)
1647 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1648 (apply 'vector lst)))
1650 (defun company-space-string (len)
1651 (if (< len company-space-strings-limit)
1652 (aref company-space-strings len)
1653 (make-string len ?\ )))
1655 (defun company-safe-substring (str from &optional to)
1656 (if (> from (string-width str))
1660 (move-to-column from)
1661 (let ((beg (point)))
1665 (concat (buffer-substring beg (point))
1666 (let ((padding (- to (current-column))))
1668 (company-space-string padding)))))
1669 (buffer-substring beg (point-max)))))))
1671 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1673 (defvar company-last-metadata nil)
1674 (make-variable-buffer-local 'company-last-metadata)
1676 (defun company-fetch-metadata ()
1677 (let ((selected (nth company-selection company-candidates)))
1678 (unless (eq selected (car company-last-metadata))
1679 (setq company-last-metadata
1680 (cons selected (company-call-backend 'meta selected))))
1681 (cdr company-last-metadata)))
1683 (defun company-doc-buffer (&optional string)
1684 (with-current-buffer (get-buffer-create "*company-documentation*")
1691 (defvar company--electric-commands
1692 '(scroll-other-window scroll-other-window-down)
1693 "List of Commands that won't break out of electric commands.")
1695 (defmacro company--electric-do (&rest body)
1696 (declare (indent 0) (debug t))
1697 `(when (company-manual-begin)
1698 (save-window-excursion
1699 (let ((height (window-height))
1700 (row (company--row))
1703 (and (< (window-height) height)
1704 (< (- (window-height) row 2) company-tooltip-limit)
1705 (recenter (- (window-height) row 2)))
1706 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1707 company--electric-commands)
1708 (call-interactively cmd))
1709 (company--unread-last-input)))))
1711 (defun company--unread-last-input ()
1712 (when last-input-event
1713 (clear-this-command-keys t)
1714 (setq unread-command-events (list last-input-event))))
1716 (defun company-show-doc-buffer ()
1717 "Temporarily show the documentation buffer for the selection."
1719 (company--electric-do
1720 (let* ((selected (nth company-selection company-candidates))
1721 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1722 (error "No documentation available"))))
1723 (with-current-buffer doc-buffer
1724 (goto-char (point-min)))
1725 (display-buffer doc-buffer t))))
1726 (put 'company-show-doc-buffer 'company-keep t)
1728 (defun company-show-location ()
1729 "Temporarily display a buffer showing the selected candidate in context."
1731 (company--electric-do
1732 (let* ((selected (nth company-selection company-candidates))
1733 (location (company-call-backend 'location selected))
1734 (pos (or (cdr location) (error "No location available")))
1735 (buffer (or (and (bufferp (car location)) (car location))
1736 (find-file-noselect (car location) t))))
1737 (with-selected-window (display-buffer buffer t)
1740 (if (bufferp (car location))
1742 (goto-char (point-min))
1743 (forward-line (1- pos))))
1744 (set-window-start nil (point))))))
1745 (put 'company-show-location 'company-keep t)
1747 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1749 (defvar company-callback nil)
1750 (make-variable-buffer-local 'company-callback)
1752 (defvar company-begin-with-marker nil)
1753 (make-variable-buffer-local 'company-begin-with-marker)
1755 (defun company-remove-callback (&optional ignored)
1756 (remove-hook 'company-completion-finished-hook company-callback t)
1757 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1758 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1759 (when company-begin-with-marker
1760 (set-marker company-begin-with-marker nil)))
1762 (defun company-begin-backend (backend &optional callback)
1763 "Start a completion at point using BACKEND."
1764 (interactive (let ((val (completing-read "Company back-end: "
1766 'functionp nil "company-")))
1768 (list (intern val)))))
1769 (when (setq company-callback callback)
1770 (add-hook 'company-completion-finished-hook company-callback nil t))
1771 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1772 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1773 (setq company-backend backend)
1774 ;; Return non-nil if active.
1775 (or (company-manual-begin)
1777 (setq company-backend nil)
1778 (error "Cannot complete at point"))))
1780 (defun company-begin-with (candidates
1781 &optional prefix-length require-match callback)
1782 "Start a completion at point.
1783 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length
1784 of the prefix that already is in the buffer before point.
1787 CALLBACK is a function called with the selected result if the user
1788 successfully completes the input.
1790 Example: \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1791 ;; FIXME: When Emacs 23 is no longer a concern, replace
1792 ;; `company-begin-with-marker' with a lexical variable; use a lexical closure.
1793 (setq company-begin-with-marker (copy-marker (point) t))
1794 (company-begin-backend
1795 `(lambda (command &optional arg &rest ignored)
1797 ((eq command 'prefix)
1798 (when (equal (point) (marker-position company-begin-with-marker))
1799 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1800 ((eq command 'candidates)
1801 (all-completions arg ',candidates))
1802 ((eq command 'require-match)
1806 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1808 (defvar company-pseudo-tooltip-overlay nil)
1809 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1811 (defvar company-tooltip-offset 0)
1812 (make-variable-buffer-local 'company-tooltip-offset)
1814 (defun company-tooltip--lines-update-offset (selection num-lines limit)
1816 (setq company-tooltip-offset
1817 (max (min selection company-tooltip-offset)
1818 (- selection -1 limit)))
1820 (when (<= company-tooltip-offset 1)
1822 (setq company-tooltip-offset 0))
1824 (when (>= company-tooltip-offset (- num-lines limit 1))
1826 (when (= selection (1- num-lines))
1827 (decf company-tooltip-offset)
1828 (when (<= company-tooltip-offset 1)
1829 (setq company-tooltip-offset 0)
1834 (defun company-tooltip--simple-update-offset (selection num-lines limit)
1835 (setq company-tooltip-offset
1836 (if (< selection company-tooltip-offset)
1838 (max company-tooltip-offset
1839 (- selection limit -1)))))
1843 (defsubst company-round-tab (arg)
1844 (* (/ (+ arg tab-width) tab-width) tab-width))
1846 (defun company-plainify (str)
1847 (let ((prefix (get-text-property 0 'line-prefix str)))
1848 (when prefix ; Keep the original value unmodified, for no special reason.
1849 (setq str (concat prefix str))
1850 (remove-text-properties 0 (length str) '(line-prefix) str)))
1851 (let* ((pieces (split-string str "\t"))
1854 (setcar copy (company-safe-substring
1855 (car copy) 0 (company-round-tab (string-width (car copy)))))
1857 (apply 'concat pieces)))
1859 (defun company-fill-propertize (value annotation width selected left right)
1860 (let* ((margin (length left))
1861 (common (+ (or (company-call-backend 'match value)
1862 (length company-common)) margin))
1863 (ann-ralign company-tooltip-align-annotations)
1864 (ann-truncate (< width
1865 (+ (length value) (length annotation)
1866 (if ann-ralign 1 0))))
1867 (ann-start (+ margin
1871 (- width (length annotation)))
1874 (if (or ann-truncate (not ann-ralign))
1875 (company-safe-substring
1877 (when (and annotation ann-ralign) " ")
1881 (company-safe-substring value 0
1882 (- width (length annotation)))
1885 (setq width (+ width margin (length right)))
1887 (add-text-properties 0 width '(face company-tooltip
1888 mouse-face company-tooltip-mouse)
1890 (add-text-properties margin common
1891 '(face company-tooltip-common
1892 mouse-face company-tooltip-mouse)
1894 (add-text-properties ann-start (min (+ ann-start (length annotation)) width)
1895 '(face company-tooltip-annotation
1896 mouse-face company-tooltip-mouse)
1899 (if (and company-search-string
1900 (string-match (regexp-quote company-search-string) value
1901 (length company-prefix)))
1902 (let ((beg (+ margin (match-beginning 0)))
1903 (end (+ margin (match-end 0))))
1904 (add-text-properties beg end '(face company-tooltip-selection)
1906 (when (< beg common)
1907 (add-text-properties beg common
1908 '(face company-tooltip-common-selection)
1910 (add-text-properties 0 width '(face company-tooltip-selection
1911 mouse-face company-tooltip-selection)
1913 (add-text-properties margin common
1914 '(face company-tooltip-common-selection
1915 mouse-face company-tooltip-selection)
1921 (defun company-buffer-lines (beg end)
1924 (while (and (= 1 (vertical-motion 1))
1926 (let ((bound (min end (1- (point)))))
1927 ;; A visual line can contain several physical lines (e.g. with outline's
1928 ;; folding overlay). Take only the first one.
1929 (push (buffer-substring beg
1932 (re-search-forward "$" bound 'move)
1936 (unless (eq beg end)
1937 (push (buffer-substring beg end) lines))
1940 (defun company-modify-line (old new offset)
1941 (concat (company-safe-substring old 0 offset)
1943 (company-safe-substring old (+ offset (length new)))))
1945 (defsubst company--length-limit (lst limit)
1946 (if (nthcdr limit lst)
1950 (defun company--replacement-string (lines old column nl &optional align-top)
1951 (decf column company-tooltip-margin)
1953 (let ((width (length (car lines)))
1954 (remaining-cols (- (+ (company--window-width) (window-hscroll))
1956 (when (> width remaining-cols)
1957 (decf column (- width remaining-cols))))
1959 (let ((offset (and (< column 0) (- column)))
1964 ;; untouched lines first
1965 (dotimes (_ (- (length old) (length lines)))
1966 (push (pop old) new)))
1967 ;; length into old lines.
1969 (push (company-modify-line (pop old)
1970 (company--offset-line (pop lines) offset)
1972 ;; Append whole new lines.
1974 (push (concat (company-space-string column)
1975 (company--offset-line (pop lines) offset))
1978 (let ((str (concat (when nl "\n")
1979 (mapconcat 'identity (nreverse new) "\n")
1981 (font-lock-append-text-property 0 (length str) 'face 'default str)
1984 (defun company--offset-line (line offset)
1985 (if (and offset line)
1986 (substring line offset)
1989 (defun company--create-lines (selection limit)
1990 (let ((len company-candidates-length)
1992 (window-width (company--window-width))
2001 ;; Maybe clear old offset.
2002 (when (< len (+ company-tooltip-offset limit))
2003 (setq company-tooltip-offset 0))
2005 ;; Scroll to offset.
2006 (if (eq company-tooltip-offset-display 'lines)
2007 (setq limit (company-tooltip--lines-update-offset selection len limit))
2008 (company-tooltip--simple-update-offset selection len limit))
2011 ((eq company-tooltip-offset-display 'scrollbar)
2012 (setq scrollbar-bounds (company--scrollbar-bounds company-tooltip-offset
2014 ((eq company-tooltip-offset-display 'lines)
2015 (when (> company-tooltip-offset 0)
2016 (setq previous (format "...(%d)" company-tooltip-offset)))
2017 (setq remainder (- len limit company-tooltip-offset)
2018 remainder (when (> remainder 0)
2019 (setq remainder (format "...(%d)" remainder))))))
2021 (decf selection company-tooltip-offset)
2022 (setq width (max (length previous) (length remainder))
2023 lines (nthcdr company-tooltip-offset company-candidates)
2027 (decf window-width (* 2 company-tooltip-margin))
2028 (when scrollbar-bounds (decf window-width))
2031 (let* ((value (pop lines-copy))
2032 (annotation (company-call-backend 'annotation value)))
2033 (when (and annotation company-tooltip-align-annotations)
2034 ;; `lisp-completion-at-point' adds a space.
2035 (setq annotation (comment-string-strip annotation t nil)))
2036 (push (cons value annotation) items)
2037 (setq width (max (+ (length value)
2038 (if (and annotation company-tooltip-align-annotations)
2039 (1+ (length annotation))
2040 (length annotation)))
2043 (setq width (min window-width
2044 (if (and company-show-numbers
2045 (< company-tooltip-offset 10))
2049 ;; number can make tooltip too long
2050 (when company-show-numbers
2051 (setq numbered company-tooltip-offset))
2053 (let ((items (nreverse items)) new)
2055 (push (company--scrollpos-line previous width) new))
2058 (let* ((item (pop items))
2059 (str (company-reformat (car item)))
2060 (annotation (cdr item))
2061 (right (company-space-string company-tooltip-margin))
2063 (when (< numbered 10)
2066 (setq right (concat (format " %d" (mod numbered 10)) right)))
2068 (company-fill-propertize str annotation
2069 width (equal i selection)
2070 (company-space-string
2071 company-tooltip-margin)
2073 (when scrollbar-bounds
2074 (company--scrollbar i scrollbar-bounds)))
2078 (push (company--scrollpos-line remainder width) new))
2082 (defun company--scrollbar-bounds (offset limit length)
2083 (when (> length limit)
2084 (let* ((size (ceiling (* limit (float limit)) length))
2085 (lower (floor (* limit (float offset)) length))
2086 (upper (+ lower size -1)))
2087 (cons lower upper))))
2089 (defun company--scrollbar (i bounds)
2090 (propertize " " 'face
2091 (if (and (>= i (car bounds)) (<= i (cdr bounds)))
2092 'company-scrollbar-fg
2093 'company-scrollbar-bg)))
2095 (defun company--scrollpos-line (text width)
2096 (propertize (concat (company-space-string company-tooltip-margin)
2097 (company-safe-substring text 0 width)
2098 (company-space-string company-tooltip-margin))
2099 'face 'company-tooltip))
2103 (defsubst company--window-inner-height ()
2104 (let ((edges (window-inside-edges)))
2105 (- (nth 3 edges) (nth 1 edges))))
2107 (defsubst company--window-width ()
2110 ((display-graphic-p) 0)
2111 ;; Account for the line continuation column.
2112 ((version< "24.3.1" emacs-version) 1)
2113 ;; Emacs 24.3 and earlier included margins
2114 ;; in window-width when in TTY.
2115 (t (1+ (let ((margins (window-margins)))
2116 (+ (or (car margins) 0)
2117 (or (cdr margins) 0))))))))
2119 (defun company--pseudo-tooltip-height ()
2120 "Calculate the appropriate tooltip height.
2121 Returns a negative number if the tooltip should be displayed above point."
2122 (let* ((lines (company--row))
2123 (below (- (company--window-inner-height) 1 lines)))
2124 (if (and (< below (min company-tooltip-minimum company-candidates-length))
2126 (- (max 3 (min company-tooltip-limit lines)))
2127 (max 3 (min company-tooltip-limit below)))))
2129 (defun company-pseudo-tooltip-show (row column selection)
2130 (company-pseudo-tooltip-hide)
2133 (let* ((height (company--pseudo-tooltip-height))
2137 (setq row (+ row height -1)
2140 (let* ((nl (< (move-to-window-line row) row))
2142 (end (save-excursion
2143 (move-to-window-line (+ row (abs height)))
2145 (ov (make-overlay beg end))
2146 (args (list (mapcar 'company-plainify
2147 (company-buffer-lines beg end))
2150 (setq company-pseudo-tooltip-overlay ov)
2151 (overlay-put ov 'company-replacement-args args)
2153 (let ((lines (company--create-lines selection (abs height))))
2154 (overlay-put ov 'company-after
2155 (apply 'company--replacement-string lines args))
2156 (overlay-put ov 'company-width (string-width (car lines))))
2158 (overlay-put ov 'company-column column)
2159 (overlay-put ov 'company-height height)))))
2161 (defun company-pseudo-tooltip-show-at-point (pos)
2162 (let ((row (company--row pos))
2163 (col (company--column pos)))
2164 (company-pseudo-tooltip-show (1+ row) col company-selection)))
2166 (defun company-pseudo-tooltip-edit (selection)
2167 (let ((height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
2168 (overlay-put company-pseudo-tooltip-overlay 'company-after
2169 (apply 'company--replacement-string
2170 (company--create-lines selection (abs height))
2171 (overlay-get company-pseudo-tooltip-overlay
2172 'company-replacement-args)))))
2174 (defun company-pseudo-tooltip-hide ()
2175 (when company-pseudo-tooltip-overlay
2176 (delete-overlay company-pseudo-tooltip-overlay)
2177 (setq company-pseudo-tooltip-overlay nil)))
2179 (defun company-pseudo-tooltip-hide-temporarily ()
2180 (when (overlayp company-pseudo-tooltip-overlay)
2181 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
2182 (overlay-put company-pseudo-tooltip-overlay 'line-prefix nil)
2183 (overlay-put company-pseudo-tooltip-overlay 'after-string nil)))
2185 (defun company-pseudo-tooltip-unhide ()
2186 (when company-pseudo-tooltip-overlay
2187 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
2188 ;; Beat outline's folding overlays, at least.
2189 (overlay-put company-pseudo-tooltip-overlay 'priority 1)
2190 ;; No (extra) prefix for the first line.
2191 (overlay-put company-pseudo-tooltip-overlay 'line-prefix "")
2192 (overlay-put company-pseudo-tooltip-overlay 'after-string
2193 (overlay-get company-pseudo-tooltip-overlay 'company-after))
2194 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
2196 (defun company-pseudo-tooltip-guard ()
2197 (buffer-substring-no-properties
2198 (point) (overlay-start company-pseudo-tooltip-overlay)))
2200 (defun company-pseudo-tooltip-frontend (command)
2201 "`company-mode' front-end similar to a tooltip but based on overlays."
2203 (pre-command (company-pseudo-tooltip-hide-temporarily))
2205 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
2206 (overlay-get company-pseudo-tooltip-overlay
2209 (new-height (company--pseudo-tooltip-height)))
2210 (unless (and (>= (* old-height new-height) 0)
2211 (>= (abs old-height) (abs new-height))
2212 (equal (company-pseudo-tooltip-guard)
2213 (overlay-get company-pseudo-tooltip-overlay
2216 (company-pseudo-tooltip-show-at-point (- (point)
2217 (length company-prefix)))
2218 (overlay-put company-pseudo-tooltip-overlay
2219 'company-guard (company-pseudo-tooltip-guard))))
2220 (company-pseudo-tooltip-unhide))
2221 (hide (company-pseudo-tooltip-hide)
2222 (setq company-tooltip-offset 0))
2223 (update (when (overlayp company-pseudo-tooltip-overlay)
2224 (company-pseudo-tooltip-edit company-selection)))))
2226 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
2227 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
2228 (unless (and (eq command 'post-command)
2229 (company--show-inline-p))
2230 (company-pseudo-tooltip-frontend command)))
2232 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2234 (defvar company-preview-overlay nil)
2235 (make-variable-buffer-local 'company-preview-overlay)
2237 (defun company-preview-show-at-point (pos)
2238 (company-preview-hide)
2240 (setq company-preview-overlay (make-overlay pos (1+ pos)))
2242 (let ((completion (nth company-selection company-candidates)))
2243 (setq completion (propertize completion 'face 'company-preview))
2244 (add-text-properties 0 (length company-common)
2245 '(face company-preview-common) completion)
2247 ;; Add search string
2248 (and company-search-string
2249 (string-match (regexp-quote company-search-string) completion)
2250 (add-text-properties (match-beginning 0)
2252 '(face company-preview-search)
2255 (setq completion (company-strip-prefix completion))
2257 (and (equal pos (point))
2258 (not (equal completion ""))
2259 (add-text-properties 0 1 '(cursor t) completion))
2261 (overlay-put company-preview-overlay 'display
2262 (concat completion (unless (eq pos (point-max))
2263 (buffer-substring pos (1+ pos)))))
2264 (overlay-put company-preview-overlay 'window (selected-window))))
2266 (defun company-preview-hide ()
2267 (when company-preview-overlay
2268 (delete-overlay company-preview-overlay)
2269 (setq company-preview-overlay nil)))
2271 (defun company-preview-frontend (command)
2272 "`company-mode' front-end showing the selection as if it had been inserted."
2274 (pre-command (company-preview-hide))
2275 (post-command (company-preview-show-at-point (point)))
2276 (hide (company-preview-hide))))
2278 (defun company-preview-if-just-one-frontend (command)
2279 "`company-preview-frontend', but only shown for single candidates."
2280 (when (or (not (eq command 'post-command))
2281 (company--show-inline-p))
2282 (company-preview-frontend command)))
2284 (defun company--show-inline-p ()
2285 (and (not (cdr company-candidates))
2287 (string-prefix-p company-prefix company-common
2288 (company-call-backend 'ignore-case))))
2290 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2292 (defvar company-echo-last-msg nil)
2293 (make-variable-buffer-local 'company-echo-last-msg)
2295 (defvar company-echo-timer nil)
2297 (defvar company-echo-delay .01)
2299 (defun company-echo-show (&optional getter)
2301 (setq company-echo-last-msg (funcall getter)))
2302 (let ((message-log-max nil))
2303 (if company-echo-last-msg
2304 (message "%s" company-echo-last-msg)
2307 (defun company-echo-show-soon (&optional getter)
2308 (when company-echo-timer
2309 (cancel-timer company-echo-timer))
2310 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
2312 (defsubst company-echo-show-when-idle (&optional getter)
2313 (when (sit-for company-echo-delay)
2314 (company-echo-show getter)))
2316 (defun company-echo-format ()
2318 (let ((limit (window-width (minibuffer-window)))
2320 ;; Roll to selection.
2321 (candidates (nthcdr company-selection company-candidates))
2322 (i (if company-show-numbers company-selection 99999))
2326 (setq comp (company-reformat (pop candidates))
2327 len (+ len 1 (length comp)))
2331 (setq comp (propertize (format "%d: %s" i comp)
2332 'face 'company-echo))
2335 (add-text-properties 3 (+ 3 (length company-common))
2336 '(face company-echo-common) comp))
2337 (setq comp (propertize comp 'face 'company-echo))
2338 (add-text-properties 0 (length company-common)
2339 '(face company-echo-common) comp))
2341 (setq candidates nil)
2344 (mapconcat 'identity (nreverse msg) " ")))
2346 (defun company-echo-strip-common-format ()
2348 (let ((limit (window-width (minibuffer-window)))
2349 (len (+ (length company-prefix) 2))
2350 ;; Roll to selection.
2351 (candidates (nthcdr company-selection company-candidates))
2352 (i (if company-show-numbers company-selection 99999))
2356 (setq comp (company-strip-prefix (pop candidates))
2357 len (+ len 2 (length comp)))
2360 (setq comp (format "%s (%d)" comp i))
2364 (setq candidates nil)
2365 (push (propertize comp 'face 'company-echo) msg)))
2367 (concat (propertize company-prefix 'face 'company-echo-common) "{"
2368 (mapconcat 'identity (nreverse msg) ", ")
2371 (defun company-echo-hide ()
2372 (unless (equal company-echo-last-msg "")
2373 (setq company-echo-last-msg "")
2374 (company-echo-show)))
2376 (defun company-echo-frontend (command)
2377 "`company-mode' front-end showing the candidates in the echo area."
2379 (post-command (company-echo-show-soon 'company-echo-format))
2380 (hide (company-echo-hide))))
2382 (defun company-echo-strip-common-frontend (command)
2383 "`company-mode' front-end showing the candidates in the echo area."
2385 (post-command (company-echo-show-soon 'company-echo-strip-common-format))
2386 (hide (company-echo-hide))))
2388 (defun company-echo-metadata-frontend (command)
2389 "`company-mode' front-end showing the documentation in the echo area."
2391 (post-command (company-echo-show-when-idle 'company-fetch-metadata))
2392 (hide (company-echo-hide))))
2395 ;;; company.el ends here