1 ;;; company.el --- Modular text completion framework -*- lexical-binding: t -*-
3 ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
6 ;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
7 ;; URL: http://company-mode.github.io/
9 ;; Keywords: abbrev, convenience, matching
10 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
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 framework. Modules for retrieving completion
30 ;; candidates are called backends, modules for displaying them are frontends.
32 ;; Company comes with many backends, e.g. `company-etags'. These are
33 ;; distributed in separate files and can be used individually.
35 ;; Enable `company-mode' in all buffers with M-x global-company-mode. For
36 ;; further information look at the documentation for `company-mode' (C-h f
39 ;; If you want to start a specific backend, call it interactively or use
40 ;; `company-begin-backend'. For example:
41 ;; M-x company-abbrev will prompt for and insert an abbrev.
43 ;; To write your own backend, look at the documentation for `company-backends'.
44 ;; Here is a simple example completing "foo":
46 ;; (defun company-my-backend (command &optional arg &rest ignored)
48 ;; (`prefix (when (looking-back "foo\\>")
50 ;; (`candidates (list "foobar" "foobaz" "foobarbaz"))
51 ;; (`meta (format "This value is named %s" arg))))
53 ;; Sometimes it is a good idea to mix several backends together, for example to
54 ;; enrich gtags with dabbrev-code results (to emulate local variables). To do
55 ;; this, add a list with both backends as an element in `company-backends'.
59 ;; See NEWS.md in the repository.
67 ;; FIXME: Use `user-error'.
68 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
69 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
70 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
71 (add-to-list 'debug-ignored-errors "^Company not ")
72 (add-to-list 'debug-ignored-errors "^No candidate number ")
73 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
74 (add-to-list 'debug-ignored-errors "^No other backend$")
78 ;; `defvar-local' for Emacs 24.2 and below
79 (unless (fboundp 'defvar-local)
80 (defmacro defvar-local (var val &optional docstring)
81 "Define VAR as a buffer-local variable with default value VAL.
82 Like `defvar' but additionally marks the variable as being automatically
83 buffer-local wherever it is set."
84 (declare (debug defvar) (doc-string 3))
86 (defvar ,var ,val ,docstring)
87 (make-variable-buffer-local ',var)))))
90 "Extensible inline text completion mechanism"
95 (defface company-tooltip
96 '((default :foreground "black")
97 (((class color) (min-colors 88) (background light))
98 (:background "cornsilk"))
99 (((class color) (min-colors 88) (background dark))
100 (:background "yellow")))
101 "Face used for the tooltip.")
103 (defface company-tooltip-selection
104 '((((class color) (min-colors 88) (background light))
105 (:background "light blue"))
106 (((class color) (min-colors 88) (background dark))
107 (:background "orange1"))
108 (t (:background "green")))
109 "Face used for the selection in the tooltip.")
111 (defface company-tooltip-search
112 '((default :inherit company-tooltip-selection))
113 "Face used for the search string in the tooltip.")
115 (defface company-tooltip-mouse
116 '((default :inherit highlight))
117 "Face used for the tooltip item under the mouse.")
119 (defface company-tooltip-common
120 '((((background light))
121 :foreground "darkred")
124 "Face used for the common completion in the tooltip.")
126 (defface company-tooltip-common-selection
127 '((default :inherit company-tooltip-common))
128 "Face used for the selected common completion in the tooltip.")
130 (defface company-tooltip-annotation
131 '((((background light))
132 :foreground "firebrick4")
135 "Face used for the annotation in the tooltip.")
137 (defface company-scrollbar-fg
138 '((((background light))
139 :background "darkred")
142 "Face used for the tooltip scrollbar thumb.")
144 (defface company-scrollbar-bg
145 '((((background light))
149 "Face used for the tooltip scrollbar background.")
151 (defface company-preview
152 '((((background light))
153 :inherit (company-tooltip-selection company-tooltip))
156 :foreground "wheat"))
157 "Face used for the completion preview.")
159 (defface company-preview-common
160 '((((background light))
161 :inherit company-tooltip-common-selection)
163 :inherit company-preview
165 "Face used for the common part of the completion preview.")
167 (defface company-preview-search
168 '((((background light))
169 :inherit company-tooltip-common-selection)
171 :inherit company-preview
172 :background "blue1"))
173 "Face used for the search string in the completion preview.")
175 (defface company-echo nil
176 "Face used for completions in the echo area.")
178 (defface company-echo-common
179 '((((background dark)) (:foreground "firebrick1"))
180 (((background light)) (:background "firebrick4")))
181 "Face used for the common part of completions in the echo area.")
183 (defun company-frontends-set (variable value)
185 (let ((value (delete-dups (copy-sequence value))))
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))
197 (when (cdr (memq f value))
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 frontends (visualizations).
205 Each frontend 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-minimum-width 0
248 "The minimum width of the tooltip's inner area.
249 This doesn't include the margins and the scroll bar."
251 :package-version '(company . "0.8.0"))
253 (defcustom company-tooltip-margin 1
254 "Width of margin columns to show around the toolip."
257 (defcustom company-tooltip-offset-display 'scrollbar
258 "Method using which the tooltip displays scrolling position.
259 `scrollbar' means draw a scrollbar to the right of the items.
260 `lines' means wrap items in lines with \"before\" and \"after\" counters."
261 :type '(choice (const :tag "Scrollbar" scrollbar)
262 (const :tag "Two lines" lines)))
264 (defcustom company-tooltip-align-annotations nil
265 "When non-nil, align annotations to the right tooltip border."
267 :package-version '(company . "0.7.1"))
269 (defcustom company-tooltip-flip-when-above nil
270 "Whether to flip the tooltip when it's above the current line."
272 :package-version '(company . "0.8.1"))
274 (defvar company-safe-backends
275 '((company-abbrev . "Abbrev")
276 (company-bbdb . "BBDB")
277 (company-capf . "completion-at-point-functions")
278 (company-clang . "Clang")
279 (company-cmake . "CMake")
280 (company-css . "CSS")
281 (company-dabbrev . "dabbrev for plain text")
282 (company-dabbrev-code . "dabbrev for code")
283 (company-eclim . "Eclim (an Eclipse interface)")
284 (company-elisp . "Emacs Lisp")
285 (company-etags . "etags")
286 (company-files . "Files")
287 (company-gtags . "GNU Global")
288 (company-ispell . "Ispell")
289 (company-keywords . "Programming language keywords")
290 (company-nxml . "nxml")
291 (company-oddmuse . "Oddmuse")
292 (company-semantic . "Semantic")
293 (company-tempo . "Tempo templates")
294 (company-xcode . "Xcode")))
295 (put 'company-safe-backends 'risky-local-variable t)
297 (defun company-safe-backends-p (backends)
298 (and (consp backends)
299 (not (cl-dolist (backend backends)
300 (unless (if (consp backend)
301 (company-safe-backends-p backend)
302 (assq backend company-safe-backends))
305 (defcustom company-backends `(,@(unless (version< "24.3.51" emacs-version)
306 (list 'company-elisp))
308 company-nxml company-css
309 company-eclim company-semantic company-clang
310 company-xcode company-cmake
312 (company-dabbrev-code company-gtags company-etags
314 company-oddmuse company-files company-dabbrev)
315 "The list of active backends (completion engines).
317 Only one backend is used at a time. The choice depends on the order of
318 the items in this list, and on the values they return in response to the
319 `prefix' command (see below). But a backend can also be a \"grouped\"
322 `company-begin-backend' can be used to start a specific backend,
323 `company-other-backend' will skip to the next matching backend in the list.
325 Each backend is a function that takes a variable number of arguments.
326 The first argument is the command requested from the backend. It is one
329 `prefix': The backend should return the text to be completed. It must be
330 text immediately before point. Returning nil from this command passes
331 control to the next backend. The function should return `stop' if it
332 should complete but cannot (e.g. if it is in the middle of a string).
333 Instead of a string, the backend may return a cons where car is the prefix
334 and cdr is used instead of the actual prefix length in the comparison
335 against `company-minimum-prefix-length'. It must be either number or t,
336 and in the latter case the test automatically succeeds.
338 `candidates': The second argument is the prefix to be completed. The
339 return value should be a list of candidates that match the prefix.
341 Non-prefix matches are also supported (candidates that don't start with the
342 prefix, but match it in some backend-defined way). Backends that use this
343 feature must disable cache (return t to `no-cache') and might also want to
349 `sorted': Return t here to indicate that the candidates are sorted and will
350 not need to be sorted again.
352 `duplicates': If non-nil, company will take care of removing duplicates
355 `no-cache': Usually company doesn't ask for candidates again as completion
356 progresses, unless the backend returns t for this command. The second
357 argument is the latest prefix.
359 `ignore-case': Return t here if the backend returns case-insensitive
360 matches. This value is used to determine the longest common prefix (as
361 used in `company-complete-common'), and to filter completions when fetching
364 `meta': The second argument is a completion candidate. Return a (short)
365 documentation string for it.
367 `doc-buffer': The second argument is a completion candidate. Return a
368 buffer with documentation for it. Preferably use `company-doc-buffer'. If
369 not all buffer contents pertain to this candidate, return a cons of buffer
370 and window start position.
372 `location': The second argument is a completion candidate. Return a cons
373 of buffer and buffer location, or of file and line number where the
374 completion candidate was defined.
376 `annotation': The second argument is a completion candidate. Return a
377 string to be displayed inline with the candidate in the popup. If
378 duplicates are removed by company, candidates with equal string values will
379 be kept if they have different annotations. For that to work properly,
380 backends should store the related information on candidates using text
383 `match': The second argument is a completion candidate. Return the index
384 after the end of text matching `prefix' within the candidate string. It
385 will be used when rendering the popup. This command only makes sense for
386 backends that provide non-prefix completion.
388 `require-match': If this returns t, the user is not allowed to enter
389 anything not offered as a candidate. Please don't use that value in normal
390 backends. The default value nil gives the user that choice with
391 `company-require-match'. Return value `never' overrides that option the
394 `init': Called once for each buffer. The backend can check for external
395 programs and files and load any required libraries. Raising an error here
396 will show up in message log once, and the backend will not be used for
399 `post-completion': Called after a completion candidate has been inserted
400 into the buffer. The second argument is the candidate. Can be used to
401 modify it, e.g. to expand a snippet.
403 The backend should return nil for all commands it does not support or
404 does not know about. It should also be callable interactively and use
405 `company-begin-backend' to start itself in that case.
410 An element of `company-backends' can also be a list of backends. The
411 completions from backends in such groups are merged, but only from those
412 backends which return the same `prefix'.
414 If a backend command takes a candidate as an argument (e.g. `meta'), the
415 call is dispatched to the backend the candidate came from. In other
416 cases (except for `duplicates' and `sorted'), the first non-nil value among
417 all the backends is returned.
419 The group can also contain keywords. Currently, `:with' and `:sorted'
420 keywords are defined. If the group contains keyword `:with', the backends
421 listed after this keyword are ignored for the purpose of the `prefix'
422 command. If the group contains keyword `:sorted', the final list of
423 candidates is not sorted after concatenation.
425 Asynchronous backends
426 =====================
428 The return value of each command can also be a cons (:async . FETCHER)
429 where FETCHER is a function of one argument, CALLBACK. When the data
430 arrives, FETCHER must call CALLBACK and pass it the appropriate return
431 value, as described above.
433 True asynchronous operation is only supported for command `candidates', and
434 only during idle completion. Other commands will block the user interface,
435 even if the backend uses the asynchronous calling convention."
439 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
440 company-safe-backends)
441 (symbol :tag "User defined")
442 (repeat :tag "Merged backends"
443 (choice :tag "backend"
444 ,@(mapcar (lambda (b)
445 `(const :tag ,(cdr b) ,(car b)))
446 company-safe-backends)
447 (const :tag "With" :with)
448 (symbol :tag "User defined"))))))
450 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
452 (defcustom company-transformers nil
453 "Functions to change the list of candidates received from backends.
455 Each function gets called with the return value of the previous one.
456 The first one gets passed the list of candidates, already sorted and
459 (const :tag "None" nil)
460 (const :tag "Sort by occurrence" (company-sort-by-occurrence))
461 (const :tag "Sort by backend importance"
462 (company-sort-by-backend-importance))
463 (repeat :tag "User defined" (function))))
465 (defcustom company-completion-started-hook nil
466 "Hook run when company starts completing.
467 The hook is called with one argument that is non-nil if the completion was
471 (defcustom company-completion-cancelled-hook nil
472 "Hook run when company cancels completing.
473 The hook is called with one argument that is non-nil if the completion was
477 (defcustom company-completion-finished-hook nil
478 "Hook run when company successfully completes.
479 The hook is called with the selected candidate as an argument.
481 If you indend to use it to post-process candidates from a specific
482 backend, consider using the `post-completion' command instead."
485 (defcustom company-minimum-prefix-length 3
486 "The minimum prefix length for idle completion."
487 :type '(integer :tag "prefix length"))
489 (defcustom company-abort-manual-when-too-short nil
490 "If enabled, cancel a manually started completion when the prefix gets
491 shorter than both `company-minimum-prefix-length' and the length of the
492 prefix it was started from."
494 :package-version '(company . "0.8.0"))
496 (defcustom company-require-match 'company-explicit-action-p
497 "If enabled, disallow non-matching input.
498 This can be a function do determine if a match is required.
500 This can be overridden by the backend, if it returns t or `never' to
501 `require-match'. `company-auto-complete' also takes precedence over this."
502 :type '(choice (const :tag "Off" nil)
503 (function :tag "Predicate function")
504 (const :tag "On, if user interaction took place"
505 'company-explicit-action-p)
506 (const :tag "On" t)))
508 (defcustom company-auto-complete nil
509 "Determines when to auto-complete.
510 If this is enabled, all characters from `company-auto-complete-chars'
511 trigger insertion of the selected completion candidate.
512 This can also be a function."
513 :type '(choice (const :tag "Off" nil)
514 (function :tag "Predicate function")
515 (const :tag "On, if user interaction took place"
516 'company-explicit-action-p)
517 (const :tag "On" t)))
519 (defcustom company-auto-complete-chars '(?\ ?\) ?.)
520 "Determines which characters trigger auto-completion.
521 See `company-auto-complete'. If this is a string, each string character
522 tiggers auto-completion. If it is a list of syntax description characters (see
523 `modify-syntax-entry'), all characters with that syntax auto-complete.
525 This can also be a function, which is called with the new input and should
526 return non-nil if company should auto-complete.
528 A character that is part of a valid candidate never triggers auto-completion."
529 :type '(choice (string :tag "Characters")
531 (const :tag "Whitespace" ?\ )
532 (const :tag "Symbol" ?_)
533 (const :tag "Opening parentheses" ?\()
534 (const :tag "Closing parentheses" ?\))
535 (const :tag "Word constituent" ?w)
536 (const :tag "Punctuation." ?.)
537 (const :tag "String quote." ?\")
538 (const :tag "Paired delimiter." ?$)
539 (const :tag "Expression quote or prefix operator." ?\')
540 (const :tag "Comment starter." ?<)
541 (const :tag "Comment ender." ?>)
542 (const :tag "Character-quote." ?/)
543 (const :tag "Generic string fence." ?|)
544 (const :tag "Generic comment fence." ?!))
545 (function :tag "Predicate function")))
547 (defcustom company-idle-delay .5
548 "The idle delay in seconds until completion starts automatically.
549 The prefix still has to satisfy `company-minimum-prefix-length' before that
550 happens. The value of nil means no idle completion."
551 :type '(choice (const :tag "never (nil)" nil)
552 (const :tag "immediate (0)" 0)
553 (number :tag "seconds")))
555 (defcustom company-begin-commands '(self-insert-command
556 org-self-insert-command
557 orgtbl-self-insert-command
562 "A list of commands after which idle completion is allowed.
563 If this is t, it can show completions after any command except a few from a
564 pre-defined list. See `company-idle-delay'.
566 Alternatively, any command with a non-nil `company-begin' property is
567 treated as if it was on this list."
568 :type '(choice (const :tag "Any command" t)
569 (const :tag "Self insert command" '(self-insert-command))
570 (repeat :tag "Commands" function))
571 :package-version '(company . "0.8.4"))
573 (defcustom company-continue-commands '(not save-buffer save-some-buffers
574 save-buffers-kill-terminal
575 save-buffers-kill-emacs)
576 "A list of commands that are allowed during completion.
577 If this is t, or if `company-begin-commands' is t, any command is allowed.
578 Otherwise, the value must be a list of symbols. If it starts with `not',
579 the cdr is the list of commands that abort completion. Otherwise, all
580 commands except those in that list, or in `company-begin-commands', or
581 commands in the `company-' namespace, abort completion."
582 :type '(choice (const :tag "Any command" t)
583 (cons :tag "Any except"
585 (repeat :tag "Commands" function))
586 (repeat :tag "Commands" function)))
588 (defcustom company-show-numbers nil
589 "If enabled, show quick-access numbers for the first ten candidates."
590 :type '(choice (const :tag "off" nil)
591 (const :tag "on" t)))
593 (defcustom company-selection-wrap-around nil
594 "If enabled, selecting item before first or after last wraps around."
595 :type '(choice (const :tag "off" nil)
596 (const :tag "on" t)))
598 (defvar company-async-wait 0.03
599 "Pause between checks to see if the value's been set when turning an
600 asynchronous call into synchronous.")
602 (defvar company-async-timeout 2
603 "Maximum wait time for a value to be set during asynchronous call.")
605 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
607 (defvar company-mode-map (make-sparse-keymap)
608 "Keymap used by `company-mode'.")
610 (defvar company-active-map
611 (let ((keymap (make-sparse-keymap)))
612 (define-key keymap "\e\e\e" 'company-abort)
613 (define-key keymap "\C-g" 'company-abort)
614 (define-key keymap (kbd "M-n") 'company-select-next)
615 (define-key keymap (kbd "M-p") 'company-select-previous)
616 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
617 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
618 (define-key keymap [remap scroll-up-command] 'company-next-page)
619 (define-key keymap [remap scroll-down-command] 'company-previous-page)
620 (define-key keymap [down-mouse-1] 'ignore)
621 (define-key keymap [down-mouse-3] 'ignore)
622 (define-key keymap [mouse-1] 'company-complete-mouse)
623 (define-key keymap [mouse-3] 'company-select-mouse)
624 (define-key keymap [up-mouse-1] 'ignore)
625 (define-key keymap [up-mouse-3] 'ignore)
626 (define-key keymap [return] 'company-complete-selection)
627 (define-key keymap (kbd "RET") 'company-complete-selection)
628 (define-key keymap [tab] 'company-complete-common)
629 (define-key keymap (kbd "TAB") 'company-complete-common)
630 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
631 (define-key keymap (kbd "C-h") 'company-show-doc-buffer)
632 (define-key keymap "\C-w" 'company-show-location)
633 (define-key keymap "\C-s" 'company-search-candidates)
634 (define-key keymap "\C-\M-s" 'company-filter-candidates)
636 (define-key keymap (read-kbd-macro (format "M-%d" i)) 'company-complete-number))
638 "Keymap that is enabled during an active completion.")
640 (defvar company--disabled-backends nil)
642 (defun company-init-backend (backend)
643 (and (symbolp backend)
644 (not (fboundp backend))
645 (ignore-errors (require backend nil t)))
650 (funcall backend 'init)
651 (put backend 'company-init t))
653 (put backend 'company-init 'failed)
654 (unless (memq backend company--disabled-backends)
655 (message "Company backend '%s' could not be initialized:\n%s"
656 backend (error-message-string err)))
657 (cl-pushnew backend company--disabled-backends)
659 ;; No initialization for lambdas.
660 ((functionp backend) t)
661 (t ;; Must be a list.
662 (cl-dolist (b backend)
664 (company-init-backend b))))))
666 (defcustom company-lighter-base "company"
667 "Base string to use for the `company-mode' lighter."
669 :package-version '(company . "0.8.10"))
671 (defvar company-lighter '(" "
674 (if (consp company-backend)
675 (company--group-lighter (nth company-selection
677 company-lighter-base)
678 (symbol-name company-backend)))
679 company-lighter-base))
680 "Mode line lighter for Company.
682 The value of this variable is a mode line template as in
683 `mode-line-format'.")
685 (put 'company-lighter 'risky-local-variable t)
688 (define-minor-mode company-mode
689 "\"complete anything\"; is an in-buffer completion framework.
690 Completion starts automatically, depending on the values
691 `company-idle-delay' and `company-minimum-prefix-length'.
693 Completion can be controlled with the commands:
694 `company-complete-common', `company-complete-selection', `company-complete',
695 `company-select-next', `company-select-previous'. If these commands are
696 called before `company-idle-delay', completion will also start.
698 Completions can be searched with `company-search-candidates' or
699 `company-filter-candidates'. These can be used while completion is
702 The completion data is retrieved using `company-backends' and displayed
703 using `company-frontends'. If you want to start a specific backend, call
704 it interactively or use `company-begin-backend'.
706 By default, the completions list is sorted alphabetically, unless the
707 backend chooses otherwise, or `company-transformers' changes it later.
709 regular keymap (`company-mode-map'):
712 keymap during active completions (`company-active-map'):
714 \\{company-active-map}"
715 nil company-lighter company-mode-map
718 (when (eq company-idle-delay t)
719 (setq company-idle-delay 0)
720 (warn "Setting `company-idle-delay' to t is deprecated. Set it to 0 instead."))
721 (add-hook 'pre-command-hook 'company-pre-command nil t)
722 (add-hook 'post-command-hook 'company-post-command nil t)
723 (mapc 'company-init-backend company-backends))
724 (remove-hook 'pre-command-hook 'company-pre-command t)
725 (remove-hook 'post-command-hook 'company-post-command t)
727 (kill-local-variable 'company-point)))
729 (defcustom company-global-modes t
730 "Modes for which `company-mode' mode is turned on by `global-company-mode'.
731 If nil, means no modes. If t, then all major modes have it turned on.
732 If a list, it should be a list of `major-mode' symbol names for which
733 `company-mode' should be automatically turned on. The sense of the list is
734 negated if it begins with `not'. For example:
736 means that `company-mode' is turned on for buffers in C and C++ modes only.
738 means that `company-mode' is always turned on except in `message-mode' buffers."
739 :type '(choice (const :tag "none" nil)
741 (set :menu-tag "mode specific" :tag "modes"
743 (const :tag "Except" not)
744 (repeat :inline t (symbol :tag "mode")))))
747 (define-globalized-minor-mode global-company-mode company-mode company-mode-on)
749 (defun company-mode-on ()
750 (when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s)))
751 (cond ((eq company-global-modes t)
753 ((eq (car-safe company-global-modes) 'not)
754 (not (memq major-mode (cdr company-global-modes))))
755 (t (memq major-mode company-global-modes))))
758 (defsubst company-assert-enabled ()
760 (company-uninstall-map)
761 (error "Company not enabled")))
763 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
765 (defvar-local company-my-keymap nil)
767 (defvar company-emulation-alist '((t . nil)))
769 (defsubst company-enable-overriding-keymap (keymap)
770 (company-uninstall-map)
771 (setq company-my-keymap keymap))
773 (defun company-ensure-emulation-alist ()
774 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
775 (setq emulation-mode-map-alists
776 (cons 'company-emulation-alist
777 (delq 'company-emulation-alist emulation-mode-map-alists)))))
779 (defun company-install-map ()
780 (unless (or (cdar company-emulation-alist)
781 (null company-my-keymap))
782 (setf (cdar company-emulation-alist) company-my-keymap)))
784 (defun company-uninstall-map ()
785 (setf (cdar company-emulation-alist) nil))
788 ;; Emacs calculates the active keymaps before reading the event. That means we
789 ;; cannot change the keymap from a timer. So we send a bogus command.
790 ;; XXX: Even in Emacs 24.4, seems to be needed in the terminal.
791 (defun company-ignore ()
793 (setq this-command last-command))
795 (global-set-key '[company-dummy-event] 'company-ignore)
797 (defun company-input-noop ()
798 (push 'company-dummy-event unread-command-events))
800 (defun company--posn-col-row (posn)
801 (let ((col (car (posn-col-row posn)))
802 ;; `posn-col-row' doesn't work well with lines of different height.
803 ;; `posn-actual-col-row' doesn't handle multiple-width characters.
804 (row (cdr (posn-actual-col-row posn))))
805 (when (and header-line-format (version< emacs-version "24.3.93.3"))
806 ;; http://debbugs.gnu.org/18384
808 (cons (+ col (window-hscroll)) row)))
810 (defun company--col-row (&optional pos)
811 (company--posn-col-row (posn-at-point pos)))
813 (defun company--row (&optional pos)
814 (cdr (company--col-row pos)))
816 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
818 (defvar-local company-backend nil)
820 (defun company-grab (regexp &optional expression limit)
821 (when (looking-back regexp limit)
822 (or (match-string-no-properties (or expression 0)) "")))
824 (defun company-grab-line (regexp &optional expression)
825 "Return a match string for REGEXP if it matches text before point.
826 If EXPRESSION is non-nil, return the match string for the respective
827 parenthesized expression in REGEXP.
828 Matching is limited to the current line."
829 (company-grab regexp expression (point-at-bol)))
831 (defun company-grab-symbol ()
832 "If point is at the end of a symbol, return it.
833 Otherwise, if point is not inside a symbol, return an empty string."
834 (if (looking-at "\\_>")
835 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
837 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
840 (defun company-grab-word ()
841 "If point is at the end of a word, return it.
842 Otherwise, if point is not inside a symbol, return an empty string."
843 (if (looking-at "\\>")
844 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
846 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
849 (defun company-grab-symbol-cons (idle-begin-after-re &optional max-len)
850 "Return a string SYMBOL or a cons (SYMBOL . t).
851 SYMBOL is as returned by `company-grab-symbol'. If the text before poit
852 matches IDLE-BEGIN-AFTER-RE, return it wrapped in a cons."
853 (let ((symbol (company-grab-symbol)))
856 (forward-char (- (length symbol)))
857 (if (looking-back idle-begin-after-re (if max-len
859 (line-beginning-position)))
863 (defun company-in-string-or-comment ()
864 "Return non-nil if point is within a string or comment."
865 (let ((ppss (syntax-ppss)))
866 (or (car (setq ppss (nthcdr 3 ppss)))
867 (car (setq ppss (cdr ppss)))
870 (defun company-call-backend (&rest args)
871 (company--force-sync #'company-call-backend-raw args company-backend))
873 (defun company--force-sync (fun args backend)
874 (let ((value (apply fun args)))
875 (if (not (eq (car-safe value) :async))
878 (start (time-to-seconds)))
880 (lambda (result) (setq res result)))
881 (while (eq res 'trash)
882 (if (> (- (time-to-seconds) start) company-async-timeout)
883 (error "Company: backend %s async timeout with args %s"
885 (sleep-for company-async-wait)))
888 (defun company-call-backend-raw (&rest args)
889 (condition-case-unless-debug err
890 (if (functionp company-backend)
891 (apply company-backend args)
892 (apply #'company--multi-backend-adapter company-backend args))
893 (error (error "Company: backend %s error \"%s\" with args %s"
894 company-backend (error-message-string err) args))))
896 (defun company--multi-backend-adapter (backends command &rest args)
897 (let ((backends (cl-loop for b in backends
898 when (not (and (symbolp b)
899 (eq 'failed (get b 'company-init))))
902 (when (eq command 'prefix)
903 (setq backends (butlast backends (length (member :with backends)))))
905 (unless (memq command '(sorted))
906 (setq backends (cl-delete-if #'keywordp backends)))
910 (company--multi-backend-adapter-candidates backends (car args)))
911 (`sorted (memq :sorted backends))
913 ((or `prefix `ignore-case `no-cache `require-match)
915 (cl-dolist (backend backends)
916 (when (setq value (company--force-sync
917 backend (cons command args) backend))
918 (cl-return value)))))
920 (let ((arg (car args)))
921 (when (> (length arg) 0)
922 (let ((backend (or (get-text-property 0 'company-backend arg)
924 (apply backend command args))))))))
926 (defun company--multi-backend-adapter-candidates (backends prefix)
927 (let ((pairs (cl-loop for backend in (cdr backends)
928 when (equal (company--prefix-str
929 (funcall backend 'prefix))
931 collect (cons (funcall backend 'candidates prefix)
936 (propertize str 'company-backend b))
938 (when (equal (company--prefix-str (funcall (car backends) 'prefix)) prefix)
939 ;; Small perf optimization: don't tag the candidates received
940 ;; from the first backend in the group.
941 (push (cons (funcall (car backends) 'candidates prefix)
944 (company--merge-async pairs (lambda (values) (apply #'append values)))))
946 (defun company--merge-async (pairs merger)
947 (let ((async (cl-loop for pair in pairs
949 (eq :async (car-safe (car pair))))))
951 (funcall merger (cl-loop for (val . mapper) in pairs
952 collect (funcall mapper val)))
957 (pending (mapcar #'car pairs))
968 (this-finisher (lambda (res)
969 (setq pending (delq val pending))
970 (setcar cell (funcall mapper res))
971 (funcall finisher))))
972 (if (not (eq :async (car-safe val)))
973 (funcall this-finisher val)
974 (let ((fetcher (cdr val)))
975 (funcall fetcher this-finisher)))))))))))
977 (defun company--prefix-str (prefix)
978 (or (car-safe prefix) prefix))
980 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
982 (defvar-local company-prefix nil)
984 (defvar-local company-candidates nil)
986 (defvar-local company-candidates-length nil)
988 (defvar-local company-candidates-cache nil)
990 (defvar-local company-candidates-predicate nil)
992 (defvar-local company-common nil)
994 (defvar-local company-selection 0)
996 (defvar-local company-selection-changed nil)
998 (defvar-local company--manual-action nil
999 "Non-nil, if manual completion took place.")
1001 (defvar-local company--manual-prefix nil)
1003 (defvar company--auto-completion nil
1004 "Non-nil when current candidate is being inserted automatically.
1005 Controlled by `company-auto-complete'.")
1007 (defvar-local company--point-max nil)
1009 (defvar-local company-point nil)
1011 (defvar company-timer nil)
1013 (defsubst company-strip-prefix (str)
1014 (substring str (length company-prefix)))
1016 (defun company--insert-candidate (candidate)
1017 (when (> (length candidate) 0)
1018 (setq candidate (substring-no-properties candidate))
1019 ;; XXX: Return value we check here is subject to change.
1020 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
1021 (insert (company-strip-prefix candidate))
1022 (unless (equal company-prefix candidate)
1023 (delete-region (- (point) (length company-prefix)) (point))
1024 (insert candidate)))))
1026 (defmacro company-with-candidate-inserted (candidate &rest body)
1027 "Evaluate BODY with CANDIDATE temporarily inserted.
1028 This is a tool for backends that need candidates inserted before they
1029 can retrieve meta-data for them."
1030 (declare (indent 1))
1031 `(let ((inhibit-modification-hooks t)
1032 (inhibit-point-motion-hooks t)
1033 (modified-p (buffer-modified-p)))
1034 (company--insert-candidate ,candidate)
1037 (delete-region company-point (point))
1038 (set-buffer-modified-p modified-p))))
1040 (defun company-explicit-action-p ()
1041 "Return whether explicit completion action was taken by the user."
1042 (or company--manual-action
1043 company-selection-changed))
1045 (defun company-reformat (candidate)
1046 ;; company-ispell needs this, because the results are always lower-case
1047 ;; It's mory efficient to fix it only when they are displayed.
1048 ;; FIXME: Adopt the current text's capitalization instead?
1049 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
1050 (concat company-prefix (substring candidate (length company-prefix)))
1053 (defun company--should-complete ()
1054 (and (eq company-idle-delay 'now)
1055 (not (or buffer-read-only overriding-terminal-local-map
1056 overriding-local-map))
1057 ;; Check if in the middle of entering a key combination.
1058 (or (equal (this-command-keys-vector) [])
1059 (not (keymapp (key-binding (this-command-keys-vector)))))
1060 (not (and transient-mark-mode mark-active))))
1062 (defun company--should-continue ()
1063 (or (eq t company-begin-commands)
1064 (eq t company-continue-commands)
1065 (if (eq 'not (car company-continue-commands))
1066 (not (memq this-command (cdr company-continue-commands)))
1067 (or (memq this-command company-begin-commands)
1068 (memq this-command company-continue-commands)
1069 (and (symbolp this-command)
1070 (string-match-p "\\`company-" (symbol-name this-command)))))))
1072 (defun company-call-frontends (command)
1073 (dolist (frontend company-frontends)
1074 (condition-case-unless-debug err
1075 (funcall frontend command)
1076 (error (error "Company: frontend %s error \"%s\" on command %s"
1077 frontend (error-message-string err) command)))))
1079 (defun company-set-selection (selection &optional force-update)
1081 (if company-selection-wrap-around
1082 (mod selection company-candidates-length)
1083 (max 0 (min (1- company-candidates-length) selection))))
1084 (when (or force-update (not (equal selection company-selection)))
1085 (setq company-selection selection
1086 company-selection-changed t)
1087 (company-call-frontends 'update)))
1089 (defun company--group-lighter (candidate base)
1090 (let ((backend (or (get-text-property 0 'company-backend candidate)
1091 (car company-backend))))
1092 (when (and backend (symbolp backend))
1093 (let ((name (replace-regexp-in-string "company-\\|-company" ""
1094 (symbol-name backend))))
1095 (format "%s-<%s>" base name)))))
1097 (defun company-update-candidates (candidates)
1098 (setq company-candidates-length (length candidates))
1099 (if company-selection-changed
1100 ;; Try to restore the selection
1101 (let ((selected (nth company-selection company-candidates)))
1102 (setq company-selection 0
1103 company-candidates candidates)
1107 (let ((candidate (pop candidates)))
1108 (when (and (string= candidate selected)
1109 (equal (company-call-backend 'annotation candidate)
1110 (company-call-backend 'annotation selected)))
1112 (cl-incf company-selection))
1113 (setq company-selection 0
1114 company-selection-changed nil))))
1115 (setq company-selection 0
1116 company-candidates candidates))
1117 ;; Calculate common.
1118 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
1119 ;; We want to support non-prefix completion, so filtering is the
1120 ;; responsibility of each respective backend, not ours.
1121 ;; On the other hand, we don't want to replace non-prefix input in
1122 ;; `company-complete-common', unless there's only one candidate.
1123 (setq company-common
1124 (if (cdr company-candidates)
1125 (let ((common (try-completion "" company-candidates)))
1126 (when (string-prefix-p company-prefix common
1127 completion-ignore-case)
1129 (car company-candidates)))))
1131 (defun company-calculate-candidates (prefix)
1132 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
1133 (ignore-case (company-call-backend 'ignore-case)))
1135 (when company-candidates-cache
1136 (let ((len (length prefix))
1137 (completion-ignore-case ignore-case)
1139 (cl-dotimes (i (1+ len))
1140 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
1141 company-candidates-cache)))
1142 (setq candidates (all-completions prefix prev))
1145 ;; No cache match, call the backend.
1146 (setq candidates (company--preprocess-candidates
1147 (company--fetch-candidates prefix)))
1149 (push (cons prefix candidates) company-candidates-cache)))
1150 ;; Only now apply the predicate and transformers.
1151 (setq candidates (company--postprocess-candidates candidates))
1153 (if (or (cdr candidates)
1154 (not (eq t (compare-strings (car candidates) nil nil
1155 prefix nil nil ignore-case))))
1157 ;; Already completed and unique; don't start.
1160 (defun company--fetch-candidates (prefix)
1161 (let ((c (if company--manual-action
1162 (company-call-backend 'candidates prefix)
1163 (company-call-backend-raw 'candidates prefix)))
1165 (if (not (eq (car c) :async))
1167 (let ((buf (current-buffer))
1168 (win (selected-window))
1169 (tick (buffer-chars-modified-tick))
1171 (backend company-backend))
1174 (lambda (candidates)
1175 (if (not (and candidates (eq res 'done)))
1176 ;; There's no completions to display,
1177 ;; or the fetcher called us back right away.
1178 (setq res candidates)
1179 (setq company-backend backend
1180 company-candidates-cache
1182 (company--preprocess-candidates candidates))))
1183 (company-idle-begin buf win tick pt)))))
1184 ;; FIXME: Relying on the fact that the callers
1185 ;; will interpret nil as "do nothing" is shaky.
1186 ;; A throw-catch would be one possible improvement.
1188 (progn (setq res 'done) nil)))))
1190 (defun company--preprocess-candidates (candidates)
1191 (unless (company-call-backend 'sorted)
1192 (setq candidates (sort candidates 'string<)))
1193 (when (company-call-backend 'duplicates)
1194 (setq candidates (company--strip-duplicates candidates)))
1197 (defun company--postprocess-candidates (candidates)
1198 (when (or company-candidates-predicate company-transformers)
1199 (setq candidates (copy-sequence candidates)))
1200 (when company-candidates-predicate
1201 (setq candidates (cl-delete-if-not company-candidates-predicate candidates)))
1202 (company--transform-candidates candidates))
1204 (defun company--strip-duplicates (candidates)
1206 (str (car candidates))
1207 (ref (cdr candidates))
1210 (setq str2 (pop ref))
1211 (if (not (equal str str2))
1216 (setq anno2 (company-call-backend
1219 ((null anno2)) ; Skip it.
1220 ((when (eq annos 'unk)
1221 (let ((ann1 (company-call-backend 'annotation str)))
1223 ;; No annotation on the earlier element, drop it.
1225 (setq annos (list ann1))
1227 (setq annos (list anno2))
1229 ((member anno2 annos)) ; Also skip.
1232 (push str res) ; Maintain ordering.
1234 (when str (push str res))
1237 (defun company--transform-candidates (candidates)
1238 (let ((c candidates))
1239 (dolist (tr company-transformers)
1240 (setq c (funcall tr c)))
1243 (defcustom company-occurrence-weight-function
1244 #'company-occurrence-prefer-closest-above
1245 "Function to weigh matches in `company-sort-by-occurrence'.
1246 It's called with three arguments: cursor position, the beginning and the
1249 (const :tag "First above point, then below point"
1250 company-occurrence-prefer-closest-above)
1251 (const :tag "Prefer closest in any direction"
1252 company-occurrence-prefer-any-closest)))
1254 (defun company-occurrence-prefer-closest-above (pos match-beg match-end)
1255 "Give priority to the matches above point, then those below point."
1256 (if (< match-beg pos)
1258 (- match-beg (window-start))))
1260 (defun company-occurrence-prefer-any-closest (pos _match-beg match-end)
1261 "Give priority to the matches closest to the point."
1262 (abs (- pos match-end)))
1264 (defun company-sort-by-occurrence (candidates)
1265 "Sort CANDIDATES according to their occurrences.
1266 Searches for each in the currently visible part of the current buffer and
1267 prioritizes the matches according to `company-occurrence-weight-function'.
1268 The rest of the list is appended unchanged.
1269 Keywords and function definition names are ignored."
1270 (let* ((w-start (window-start))
1271 (w-end (window-end))
1272 (start-point (point))
1280 (while (search-forward candidate w-end t)
1281 (when (and (not (eq (point) start-point))
1283 (company--occurrence-predicate)))
1287 (funcall company-occurrence-weight-function
1295 (mapcar #'car (sort occurs (lambda (e1 e2) (<= (cdr e1) (cdr e2)))))
1298 (defun company--occurrence-predicate ()
1299 (let ((beg (match-beginning 0))
1300 (end (match-end 0)))
1303 (and (not (memq (get-text-property (1- (point)) 'face)
1304 '(font-lock-function-name-face
1305 font-lock-keyword-face)))
1306 (let ((prefix (company--prefix-str
1307 (company-call-backend 'prefix))))
1308 (and (stringp prefix)
1309 (= (length prefix) (- end beg))))))))
1311 (defun company-sort-by-backend-importance (candidates)
1312 "Sort CANDIDATES as two priority groups.
1313 If `company-backend' is a function, do nothing. If it's a list, move
1314 candidates from backends before keyword `:with' to the front. Candidates
1315 from the rest of the backends in the group, if any, will be left at the end."
1316 (if (functionp company-backend)
1318 (let ((low-priority (cdr (memq :with company-backend))))
1319 (if (null low-priority)
1324 (let ((b2 (get-text-property 0 'company-backend c2)))
1325 (and b2 (memq b2 low-priority)))
1326 (let ((b1 (get-text-property 0 'company-backend c1)))
1327 (or (not b1) (not (memq b1 low-priority)))))))))))
1329 (defun company-idle-begin (buf win tick pos)
1330 (and (eq buf (current-buffer))
1331 (eq win (selected-window))
1332 (eq tick (buffer-chars-modified-tick))
1334 (when (company-auto-begin)
1335 (company-input-noop)
1336 (let ((this-command 'company-idle-begin))
1337 (company-post-command)))))
1339 (defun company-auto-begin ()
1341 (not company-candidates)
1342 (let ((company-idle-delay 'now))
1343 (condition-case-unless-debug err
1346 ;; Return non-nil if active.
1348 (error (message "Company: An error occurred in auto-begin")
1349 (message "%s" (error-message-string err))
1351 (quit (company-cancel))))))
1353 (defun company-manual-begin ()
1355 (company-assert-enabled)
1356 (setq company--manual-action t)
1358 (let ((company-minimum-prefix-length 0))
1359 (or company-candidates
1360 (company-auto-begin)))
1361 (unless company-candidates
1362 (setq company--manual-action nil))))
1364 (defun company-other-backend (&optional backward)
1365 (interactive (list current-prefix-arg))
1366 (company-assert-enabled)
1367 (let* ((after (if company-backend
1368 (cdr (member company-backend company-backends))
1370 (before (cdr (member company-backend (reverse company-backends))))
1372 (append before (reverse after))
1373 (append after (reverse before)))))
1375 (cl-dolist (backend next)
1376 (when (ignore-errors (company-begin-backend backend))
1378 (unless company-candidates
1379 (error "No other backend")))
1381 (defun company-require-match-p ()
1382 (let ((backend-value (company-call-backend 'require-match)))
1383 (or (eq backend-value t)
1384 (and (not (eq backend-value 'never))
1385 (if (functionp company-require-match)
1386 (funcall company-require-match)
1387 (eq company-require-match t))))))
1389 (defun company-auto-complete-p (input)
1390 "Return non-nil, if input starts with punctuation or parentheses."
1391 (and (if (functionp company-auto-complete)
1392 (funcall company-auto-complete)
1393 company-auto-complete)
1394 (if (functionp company-auto-complete-chars)
1395 (funcall company-auto-complete-chars input)
1396 (if (consp company-auto-complete-chars)
1397 (memq (char-syntax (string-to-char input))
1398 company-auto-complete-chars)
1399 (string-match (substring input 0 1) company-auto-complete-chars)))))
1401 (defun company--incremental-p ()
1402 (and (> (point) company-point)
1403 (> (point-max) company--point-max)
1404 (not (eq this-command 'backward-delete-char-untabify))
1405 (equal (buffer-substring (- company-point (length company-prefix))
1409 (defun company--continue-failed (new-prefix)
1410 (let ((input (buffer-substring-no-properties (point) company-point)))
1412 ((company-auto-complete-p input)
1415 (goto-char company-point)
1416 (let ((company--auto-completion t))
1417 (company-complete-selection))
1419 ((and (or (not (company-require-match-p))
1420 ;; Don't require match if the new prefix
1421 ;; doesn't continue the old one, and the latter was a match.
1422 (not (stringp new-prefix))
1423 (<= (length new-prefix) (length company-prefix)))
1424 (member company-prefix company-candidates))
1425 ;; Last input was a success,
1426 ;; but we're treating it as an abort + input anyway,
1427 ;; like the `unique' case below.
1428 (company-cancel 'non-unique))
1429 ((company-require-match-p)
1430 ;; Wrong incremental input, but required match.
1431 (delete-char (- (length input)))
1433 (message "Matching input is required")
1435 (t (company-cancel)))))
1437 (defun company--good-prefix-p (prefix)
1438 (and (stringp (company--prefix-str prefix)) ;excludes 'stop
1439 (or (eq (cdr-safe prefix) t)
1440 (let ((len (or (cdr-safe prefix) (length prefix))))
1441 (if company--manual-prefix
1442 (or (not company-abort-manual-when-too-short)
1443 ;; Must not be less than minimum or initial length.
1444 (>= len (min company-minimum-prefix-length
1445 (length company--manual-prefix))))
1446 (>= len company-minimum-prefix-length))))))
1448 (defun company--continue ()
1449 (when (company-call-backend 'no-cache company-prefix)
1450 ;; Don't complete existing candidates, fetch new ones.
1451 (setq company-candidates-cache nil))
1452 (let* ((new-prefix (company-call-backend 'prefix))
1453 (c (when (and (company--good-prefix-p new-prefix)
1454 (setq new-prefix (company--prefix-str new-prefix))
1455 (= (- (point) (length new-prefix))
1456 (- company-point (length company-prefix))))
1457 (company-calculate-candidates new-prefix))))
1460 ;; t means complete/unique.
1461 ;; Handle it like completion was aborted, to differentiate from user
1462 ;; calling one of Company's commands to insert the candidate,
1463 ;; not to trigger template expansion, etc.
1464 (company-cancel 'unique))
1466 ;; incremental match
1467 (setq company-prefix new-prefix)
1468 (company-update-candidates c)
1470 ((not (company--incremental-p))
1472 (t (company--continue-failed new-prefix)))))
1474 (defun company--begin-new ()
1476 (cl-dolist (backend (if company-backend
1477 ;; prefer manual override
1478 (list company-backend)
1481 (if (or (symbolp backend)
1482 (functionp backend))
1483 (when (or (not (symbolp backend))
1484 (eq t (get backend 'company-init))
1485 (unless (get backend 'company-init)
1486 (company-init-backend backend)))
1487 (funcall backend 'prefix))
1488 (company--multi-backend-adapter backend 'prefix)))
1490 (when (company--good-prefix-p prefix)
1491 (setq company-prefix (company--prefix-str prefix)
1492 company-backend backend
1493 c (company-calculate-candidates company-prefix))
1496 (when company--manual-action
1497 (message "No completion found"))
1499 ;; t means complete/unique.
1500 ;; Run the hooks anyway, to e.g. clear the cache.
1501 (company-cancel 'unique)))
1502 (when company--manual-action
1503 (setq company--manual-prefix prefix))
1504 (company-update-candidates c)
1505 (run-hook-with-args 'company-completion-started-hook
1506 (company-explicit-action-p))
1507 (company-call-frontends 'show)))
1510 (defun company--perform ()
1511 (or (and company-candidates (company--continue))
1512 (and (company--should-complete) (company--begin-new)))
1513 (if (not company-candidates)
1514 (setq company-backend nil)
1515 (setq company-point (point)
1516 company--point-max (point-max))
1517 (company-ensure-emulation-alist)
1518 (company-enable-overriding-keymap company-active-map)
1519 (company-call-frontends 'update)))
1521 (defun company-cancel (&optional result)
1523 (when company-prefix
1524 (if (stringp result)
1526 (company-call-backend 'pre-completion result)
1527 (run-hook-with-args 'company-completion-finished-hook result)
1528 (company-call-backend 'post-completion result))
1529 (run-hook-with-args 'company-completion-cancelled-hook result)))
1530 (setq company-backend nil
1532 company-candidates nil
1533 company-candidates-length nil
1534 company-candidates-cache nil
1535 company-candidates-predicate nil
1538 company-selection-changed nil
1539 company--manual-action nil
1540 company--manual-prefix nil
1541 company--point-max nil
1544 (cancel-timer company-timer))
1545 (company-echo-cancel t)
1546 (company-search-mode 0)
1547 (company-call-frontends 'hide)
1548 (company-enable-overriding-keymap nil))
1549 ;; Make return value explicit.
1552 (defun company-abort ()
1554 (company-cancel 'abort))
1556 (defun company-finish (result)
1557 (company--insert-candidate result)
1558 (company-cancel result))
1560 (defsubst company-keep (command)
1561 (and (symbolp command) (get command 'company-keep)))
1563 (defun company-pre-command ()
1564 (unless (company-keep this-command)
1565 (condition-case-unless-debug err
1566 (when company-candidates
1567 (company-call-frontends 'pre-command)
1568 (unless (company--should-continue)
1570 (error (message "Company: An error occurred in pre-command")
1571 (message "%s" (error-message-string err))
1574 (cancel-timer company-timer)
1575 (setq company-timer nil))
1576 (company-echo-cancel t)
1577 (company-uninstall-map))
1579 (defun company-post-command ()
1580 (when (null this-command)
1581 ;; Happens when the user presses `C-g' while inside
1582 ;; `flyspell-post-command-hook', for example.
1583 ;; Or any other `post-command-hook' function that can call `sit-for',
1584 ;; or any quittable timer function.
1586 (setq this-command 'company-abort))
1587 (unless (company-keep this-command)
1588 (condition-case-unless-debug err
1590 (unless (equal (point) company-point)
1591 (let (company-idle-delay) ; Against misbehavior while debugging.
1592 (company--perform)))
1593 (if company-candidates
1594 (company-call-frontends 'post-command)
1595 (and (numberp company-idle-delay)
1596 (not defining-kbd-macro)
1597 (company--should-begin)
1599 (run-with-timer company-idle-delay nil
1601 (current-buffer) (selected-window)
1602 (buffer-chars-modified-tick) (point))))))
1603 (error (message "Company: An error occurred in post-command")
1604 (message "%s" (error-message-string err))
1606 (company-install-map))
1608 (defvar company--begin-inhibit-commands '(company-abort
1609 company-complete-mouse
1611 company-complete-common
1612 company-complete-selection
1613 company-complete-number)
1614 "List of commands after which idle completion is (still) disabled when
1615 `company-begin-commands' is t.")
1617 (defun company--should-begin ()
1618 (if (eq t company-begin-commands)
1619 (not (memq this-command company--begin-inhibit-commands))
1621 (memq this-command company-begin-commands)
1622 (and (symbolp this-command) (get this-command 'company-begin)))))
1624 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1626 (defcustom company-search-regexp-function #'regexp-quote
1627 "Function to construct the search regexp from input.
1628 It's called with one argument, the current search input. It must return
1629 either a regexp without groups, or one where groups don't intersect and
1630 each one wraps a part of the input string."
1632 (const :tag "Exact match" regexp-quote)
1633 (const :tag "Words separated with spaces" company-search-words-regexp)
1634 (const :tag "Words separated with spaces, in any order"
1635 company-search-words-in-any-order-regexp)))
1637 (defvar-local company-search-string "")
1639 (defvar company-search-lighter '(" "
1640 (company-search-filtering "Filter" "Search")
1642 company-search-string
1645 (defvar-local company-search-filtering nil
1646 "Non-nil to filter the completion candidates by the search string")
1648 (defvar-local company--search-old-selection 0)
1650 (defvar-local company--search-old-changed nil)
1652 (defun company-search-words-regexp (input)
1653 (mapconcat (lambda (word) (format "\\(%s\\)" (regexp-quote word)))
1654 (split-string input " +" t) ".*"))
1656 (defun company-search-words-in-any-order-regexp (input)
1657 (let* ((words (mapcar (lambda (word) (format "\\(%s\\)" (regexp-quote word)))
1658 (split-string input " +" t)))
1659 (permutations (company--permutations words)))
1660 (mapconcat (lambda (words)
1661 (mapconcat #'identity words ".*"))
1665 (defun company--permutations (lst)
1670 (mapcar (lambda (perm) (cons e perm))
1671 (company--permutations (cl-remove e lst :count 1))))
1674 (defun company--search (text lines)
1675 (let ((re (funcall company-search-regexp-function text))
1677 (cl-dolist (line lines)
1678 (when (string-match-p re line (length company-prefix))
1682 (defun company-search-keypad ()
1684 (let* ((name (symbol-name last-command-event))
1685 (last-command-event (aref name (1- (length name)))))
1686 (company-search-printing-char)))
1688 (defun company-search-printing-char ()
1690 (company--search-assert-enabled)
1691 (let ((ss (concat company-search-string (string last-command-event))))
1692 (when company-search-filtering
1693 (company--search-update-predicate ss))
1694 (company--search-update-string ss)))
1696 (defun company--search-update-predicate (ss)
1697 (let* ((re (funcall company-search-regexp-function ss))
1698 (company-candidates-predicate
1699 (and (not (string= re ""))
1700 company-search-filtering
1701 (lambda (candidate) (string-match re candidate))))
1702 (cc (company-calculate-candidates company-prefix)))
1703 (unless cc (error "No match"))
1704 (company-update-candidates cc)))
1706 (defun company--search-update-string (new)
1707 (let* ((pos (company--search new (nthcdr company-selection company-candidates))))
1710 (setq company-search-string new)
1711 (company-set-selection (+ company-selection pos) t))))
1713 (defun company--search-assert-input ()
1714 (company--search-assert-enabled)
1715 (when (string= company-search-string "")
1716 (error "Empty search string")))
1718 (defun company-search-repeat-forward ()
1719 "Repeat the incremental search in completion candidates forward."
1721 (company--search-assert-input)
1722 (let ((pos (company--search company-search-string
1723 (cdr (nthcdr company-selection
1724 company-candidates)))))
1727 (company-set-selection (+ company-selection pos 1) t))))
1729 (defun company-search-repeat-backward ()
1730 "Repeat the incremental search in completion candidates backwards."
1732 (company--search-assert-input)
1733 (let ((pos (company--search company-search-string
1734 (nthcdr (- company-candidates-length
1736 (reverse company-candidates)))))
1739 (company-set-selection (- company-selection pos 1) t))))
1741 (defun company-search-toggle-filtering ()
1742 "Toggle `company-search-filtering'."
1744 (company--search-assert-enabled)
1745 (setq company-search-filtering (not company-search-filtering))
1746 (let ((ss company-search-string))
1747 (company--search-update-predicate ss)
1748 (company--search-update-string ss)))
1750 (defun company-search-abort ()
1751 "Abort searching the completion candidates."
1753 (company--search-assert-enabled)
1754 (company-search-mode 0)
1755 (company-set-selection company--search-old-selection t)
1756 (setq company-selection-changed company--search-old-changed))
1758 (defun company-search-other-char ()
1760 (company--search-assert-enabled)
1761 (company-search-mode 0)
1762 (company--unread-last-input))
1764 (defun company-search-delete-char ()
1766 (company--search-assert-enabled)
1767 (if (string= company-search-string "")
1769 (let ((ss (substring company-search-string 0 -1)))
1770 (when company-search-filtering
1771 (company--search-update-predicate ss))
1772 (company--search-update-string ss))))
1774 (defvar company-search-map
1776 (keymap (make-keymap)))
1777 (if (fboundp 'max-char)
1778 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1779 'company-search-printing-char)
1781 ;; obsolete in Emacs 23
1782 (let ((l (generic-character-list))
1783 (table (nth 1 keymap)))
1785 (set-char-table-default table (car l) 'company-search-printing-char)
1786 (setq l (cdr l))))))
1787 (define-key keymap [t] 'company-search-other-char)
1789 (define-key keymap (make-string 1 i) 'company-search-other-char)
1792 (define-key keymap (vector i) 'company-search-printing-char)
1795 (define-key keymap (read (format "[kp-%s]" i)) 'company-search-keypad))
1796 (let ((meta-map (make-sparse-keymap)))
1797 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1798 (define-key keymap [escape] meta-map))
1799 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1800 (define-key keymap (kbd "M-n") 'company-select-next)
1801 (define-key keymap (kbd "M-p") 'company-select-previous)
1802 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
1803 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
1804 (define-key keymap "\e\e\e" 'company-search-other-char)
1805 (define-key keymap [escape escape escape] 'company-search-other-char)
1806 (define-key keymap (kbd "DEL") 'company-search-delete-char)
1807 (define-key keymap [backspace] 'company-search-delete-char)
1808 (define-key keymap "\C-g" 'company-search-abort)
1809 (define-key keymap "\C-s" 'company-search-repeat-forward)
1810 (define-key keymap "\C-r" 'company-search-repeat-backward)
1811 (define-key keymap "\C-o" 'company-search-toggle-filtering)
1813 (define-key keymap (read-kbd-macro (format "M-%d" i)) 'company-complete-number))
1815 "Keymap used for incrementally searching the completion candidates.")
1817 (define-minor-mode company-search-mode
1818 "Search mode for completion candidates.
1819 Don't start this directly, use `company-search-candidates' or
1820 `company-filter-candidates'."
1821 nil company-search-lighter nil
1822 (if company-search-mode
1823 (if (company-manual-begin)
1825 (setq company--search-old-selection company-selection
1826 company--search-old-changed company-selection-changed)
1827 (company-call-frontends 'update)
1828 (company-enable-overriding-keymap company-search-map))
1829 (setq company-search-mode nil))
1830 (kill-local-variable 'company-search-string)
1831 (kill-local-variable 'company-search-filtering)
1832 (kill-local-variable 'company--search-old-selection)
1833 (kill-local-variable 'company--search-old-changed)
1834 (when company-backend
1835 (company--search-update-predicate "")
1836 (company-call-frontends 'update))
1837 (company-enable-overriding-keymap company-active-map)))
1839 (defun company--search-assert-enabled ()
1840 (company-assert-enabled)
1841 (unless company-search-mode
1842 (company-uninstall-map)
1843 (error "Company not in search mode")))
1845 (defun company-search-candidates ()
1846 "Start searching the completion candidates incrementally.
1848 \\<company-search-map>Search can be controlled with the commands:
1849 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1850 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1851 - `company-search-abort' (\\[company-search-abort])
1852 - `company-search-delete-char' (\\[company-search-delete-char])
1854 Regular characters are appended to the search string.
1856 Customize `company-search-regexp-function' to change how the input
1857 is interpreted when searching.
1859 The command `company-search-toggle-filtering' (\\[company-search-toggle-filtering])
1860 uses the search string to filter the completion candidates."
1862 (company-search-mode 1))
1864 (defvar company-filter-map
1865 (let ((keymap (make-keymap)))
1866 (define-key keymap [remap company-search-printing-char]
1867 'company-filter-printing-char)
1868 (set-keymap-parent keymap company-search-map)
1870 "Keymap used for incrementally searching the completion candidates.")
1872 (defun company-filter-candidates ()
1873 "Start filtering the completion candidates incrementally.
1874 This works the same way as `company-search-candidates' immediately
1875 followed by `company-search-toggle-filtering'."
1877 (company-search-mode 1)
1878 (setq company-search-filtering t))
1880 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1882 (defun company-select-next (&optional arg)
1883 "Select the next candidate in the list.
1885 With ARG, move by that many elements."
1887 (when (company-manual-begin)
1888 (company-set-selection (+ (or arg 1) company-selection))))
1890 (defun company-select-previous (&optional arg)
1891 "Select the previous candidate in the list.
1893 With ARG, move by that many elements."
1895 (company-select-next (if arg (- arg) -1)))
1897 (defun company-select-next-or-abort (&optional arg)
1898 "Select the next candidate if more than one, else abort
1899 and invoke the normal binding.
1901 With ARG, move by that many elements."
1903 (if (> company-candidates-length 1)
1904 (company-select-next arg)
1906 (company--unread-last-input)))
1908 (defun company-select-previous-or-abort (&optional arg)
1909 "Select the previous candidate if more than one, else abort
1910 and invoke the normal binding.
1912 With ARG, move by that many elements."
1914 (if (> company-candidates-length 1)
1915 (company-select-previous arg)
1917 (company--unread-last-input)))
1919 (defun company-next-page ()
1920 "Select the candidate one page further."
1922 (when (company-manual-begin)
1923 (company-set-selection (+ company-selection
1924 company-tooltip-limit))))
1926 (defun company-previous-page ()
1927 "Select the candidate one page earlier."
1929 (when (company-manual-begin)
1930 (company-set-selection (- company-selection
1931 company-tooltip-limit))))
1933 (defvar company-pseudo-tooltip-overlay)
1935 (defvar company-tooltip-offset)
1937 (defun company--inside-tooltip-p (event-col-row row height)
1938 (let* ((ovl company-pseudo-tooltip-overlay)
1939 (column (overlay-get ovl 'company-column))
1940 (width (overlay-get ovl 'company-width))
1941 (evt-col (car event-col-row))
1942 (evt-row (cdr event-col-row)))
1943 (and (>= evt-col column)
1944 (< evt-col (+ column width))
1946 (and (> evt-row row)
1947 (<= evt-row (+ row height) ))
1948 (and (< evt-row row)
1949 (>= evt-row (+ row height)))))))
1951 (defun company--event-col-row (event)
1952 (company--posn-col-row (event-start event)))
1954 (defun company-select-mouse (event)
1955 "Select the candidate picked by the mouse."
1957 (let ((event-col-row (company--event-col-row event))
1958 (ovl-row (company--row))
1959 (ovl-height (and company-pseudo-tooltip-overlay
1960 (min (overlay-get company-pseudo-tooltip-overlay
1962 company-candidates-length))))
1964 (company--inside-tooltip-p event-col-row ovl-row ovl-height))
1966 (company-set-selection (+ (cdr event-col-row)
1967 (1- company-tooltip-offset)
1968 (if (and (eq company-tooltip-offset-display 'lines)
1969 (not (zerop company-tooltip-offset)))
1972 (if (< ovl-height 0)
1977 (company--unread-last-input)
1980 (defun company-complete-mouse (event)
1981 "Insert the candidate picked by the mouse."
1983 (when (company-select-mouse event)
1984 (company-complete-selection)))
1986 (defun company-complete-selection ()
1987 "Insert the selected candidate."
1989 (when (company-manual-begin)
1990 (let ((result (nth company-selection company-candidates)))
1991 (company-finish result))))
1993 (defun company-complete-common ()
1994 "Insert the common part of all candidates."
1996 (when (company-manual-begin)
1997 (if (and (not (cdr company-candidates))
1998 (equal company-common (car company-candidates)))
1999 (company-complete-selection)
2000 (company--insert-candidate company-common))))
2002 (defun company-complete-common-or-cycle (&optional arg)
2003 "Insert the common part of all candidates, or select the next one.
2005 With ARG, move by that many elements."
2007 (when (company-manual-begin)
2008 (let ((tick (buffer-chars-modified-tick)))
2009 (call-interactively 'company-complete-common)
2010 (when (eq tick (buffer-chars-modified-tick))
2011 (let ((company-selection-wrap-around t)
2012 (current-prefix-arg arg))
2013 (call-interactively 'company-select-next))))))
2015 (defun company-indent-or-complete-common ()
2016 "Indent the current line or region, or complete the common part."
2020 (indent-region (region-beginning) (region-end)))
2021 ((let ((old-point (point))
2022 (old-tick (buffer-chars-modified-tick))
2023 (tab-always-indent t))
2024 (call-interactively #'indent-for-tab-command)
2025 (when (and (eq old-point (point))
2026 (eq old-tick (buffer-chars-modified-tick)))
2027 (company-complete-common))))))
2029 (defun company-complete ()
2030 "Insert the common part of all candidates or the current selection.
2031 The first time this is called, the common part is inserted, the second
2032 time, or when the selection has been changed, the selected candidate is
2035 (when (company-manual-begin)
2036 (if (or company-selection-changed
2037 (eq last-command 'company-complete-common))
2038 (call-interactively 'company-complete-selection)
2039 (call-interactively 'company-complete-common)
2040 (setq this-command 'company-complete-common))))
2042 (defun company-complete-number (n)
2043 "Insert the Nth candidate visible in the tooltip.
2044 To show the number next to the candidates in some backends, enable
2045 `company-show-numbers'. When called interactively, uses the last typed
2046 character, stripping the modifiers. That character must be a digit."
2048 (list (let* ((type (event-basic-type last-command-event))
2049 (char (if (characterp type)
2050 ;; Number on the main row.
2052 ;; Keypad number, if bound directly.
2053 (car (last (string-to-list (symbol-name type))))))
2055 (if (zerop n) 10 n))))
2056 (when (company-manual-begin)
2057 (and (or (< n 1) (> n (- company-candidates-length
2058 company-tooltip-offset)))
2059 (error "No candidate number %d" n))
2061 (company-finish (nth (+ n company-tooltip-offset)
2062 company-candidates))))
2064 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2066 (defconst company-space-strings-limit 100)
2068 (defconst company-space-strings
2070 (dotimes (i company-space-strings-limit)
2071 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
2072 (apply 'vector lst)))
2074 (defun company-space-string (len)
2075 (if (< len company-space-strings-limit)
2076 (aref company-space-strings len)
2077 (make-string len ?\ )))
2079 (defun company-safe-substring (str from &optional to)
2080 (if (> from (string-width str))
2084 (move-to-column from)
2085 (let ((beg (point)))
2089 (concat (buffer-substring beg (point))
2090 (let ((padding (- to (current-column))))
2092 (company-space-string padding)))))
2093 (buffer-substring beg (point-max)))))))
2095 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2097 (defvar-local company-last-metadata nil)
2099 (defun company-fetch-metadata ()
2100 (let ((selected (nth company-selection company-candidates)))
2101 (unless (eq selected (car company-last-metadata))
2102 (setq company-last-metadata
2103 (cons selected (company-call-backend 'meta selected))))
2104 (cdr company-last-metadata)))
2106 (defun company-doc-buffer (&optional string)
2107 (with-current-buffer (get-buffer-create "*company-documentation*")
2114 (defvar company--electric-commands
2115 '(scroll-other-window scroll-other-window-down mwheel-scroll)
2116 "List of Commands that won't break out of electric commands.")
2118 (defmacro company--electric-do (&rest body)
2119 (declare (indent 0) (debug t))
2120 `(when (company-manual-begin)
2121 (save-window-excursion
2122 (let ((height (window-height))
2123 (row (company--row))
2126 (and (< (window-height) height)
2127 (< (- (window-height) row 2) company-tooltip-limit)
2128 (recenter (- (window-height) row 2)))
2129 (while (memq (setq cmd (key-binding (read-key-sequence-vector nil)))
2130 company--electric-commands)
2132 (call-interactively cmd)
2133 ((beginning-of-buffer end-of-buffer)
2134 (message (error-message-string err)))))
2135 (company--unread-last-input)))))
2137 (defun company--unread-last-input ()
2138 (when last-input-event
2139 (clear-this-command-keys t)
2140 (setq unread-command-events (list last-input-event))))
2142 (defun company-show-doc-buffer ()
2143 "Temporarily show the documentation buffer for the selection."
2145 (let (other-window-scroll-buffer)
2146 (company--electric-do
2147 (let* ((selected (nth company-selection company-candidates))
2148 (doc-buffer (or (company-call-backend 'doc-buffer selected)
2149 (error "No documentation available")))
2151 (when (consp doc-buffer)
2152 (setq start (cdr doc-buffer)
2153 doc-buffer (car doc-buffer)))
2154 (setq other-window-scroll-buffer (get-buffer doc-buffer))
2155 (let ((win (display-buffer doc-buffer t)))
2156 (set-window-start win (if start start (point-min))))))))
2157 (put 'company-show-doc-buffer 'company-keep t)
2159 (defun company-show-location ()
2160 "Temporarily display a buffer showing the selected candidate in context."
2162 (let (other-window-scroll-buffer)
2163 (company--electric-do
2164 (let* ((selected (nth company-selection company-candidates))
2165 (location (company-call-backend 'location selected))
2166 (pos (or (cdr location) (error "No location available")))
2167 (buffer (or (and (bufferp (car location)) (car location))
2168 (find-file-noselect (car location) t))))
2169 (setq other-window-scroll-buffer (get-buffer buffer))
2170 (with-selected-window (display-buffer buffer t)
2173 (if (bufferp (car location))
2175 (goto-char (point-min))
2176 (forward-line (1- pos))))
2177 (set-window-start nil (point)))))))
2178 (put 'company-show-location 'company-keep t)
2180 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2182 (defvar-local company-callback nil)
2184 (defun company-remove-callback (&optional ignored)
2185 (remove-hook 'company-completion-finished-hook company-callback t)
2186 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
2187 (remove-hook 'company-completion-finished-hook 'company-remove-callback t))
2189 (defun company-begin-backend (backend &optional callback)
2190 "Start a completion at point using BACKEND."
2191 (interactive (let ((val (completing-read "Company backend: "
2193 'functionp nil "company-")))
2195 (list (intern val)))))
2196 (when (setq company-callback callback)
2197 (add-hook 'company-completion-finished-hook company-callback nil t))
2198 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
2199 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
2200 (setq company-backend backend)
2201 ;; Return non-nil if active.
2202 (or (company-manual-begin)
2203 (error "Cannot complete at point")))
2205 (defun company-begin-with (candidates
2206 &optional prefix-length require-match callback)
2207 "Start a completion at point.
2208 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length
2209 of the prefix that already is in the buffer before point.
2212 CALLBACK is a function called with the selected result if the user
2213 successfully completes the input.
2215 Example: \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
2216 (let ((begin-marker (copy-marker (point) t)))
2217 (company-begin-backend
2218 (lambda (command &optional arg &rest ignored)
2221 (when (equal (point) (marker-position begin-marker))
2222 (buffer-substring (- (point) (or prefix-length 0)) (point))))
2224 (all-completions arg candidates))
2229 (declare-function find-library-name "find-func")
2230 (declare-function lm-version "lisp-mnt")
2232 (defun company-version (&optional show-version)
2233 "Get the Company version as string.
2235 If SHOW-VERSION is non-nil, show the version in the echo area."
2236 (interactive (list t))
2238 (require 'find-func)
2239 (insert-file-contents (find-library-name "company"))
2242 (message "Company version: %s" (lm-version))
2245 (defun company-diag ()
2246 "Pop a buffer with information about completions at point."
2248 (let* ((bb company-backends)
2250 (prefix (cl-loop for b in bb
2251 thereis (let ((company-backend b))
2253 (company-call-backend 'prefix))))
2255 (when (stringp prefix)
2256 (let ((company-backend backend))
2257 (setq cc (company-call-backend 'candidates prefix)
2260 (lambda (c) (cons c (company-call-backend 'annotation c)))
2262 (pop-to-buffer (get-buffer-create "*company-diag*"))
2263 (setq buffer-read-only nil)
2265 (insert (format "Emacs %s (%s) of %s on %s"
2266 emacs-version system-configuration
2267 (format-time-string "%Y-%m-%d" emacs-build-time)
2268 emacs-build-system))
2269 (insert "\nCompany " (company-version) "\n\n")
2270 (insert "company-backends: " (pp-to-string bb))
2272 (insert "Used backend: " (pp-to-string backend))
2274 (insert "Prefix: " (pp-to-string prefix))
2276 (insert (message "Completions:"))
2277 (unless cc (insert " none"))
2279 (dolist (c annotations)
2280 (insert "\n " (prin1-to-string (car c)))
2282 (insert " " (prin1-to-string (cdr c))))))
2285 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2287 (defvar-local company-pseudo-tooltip-overlay nil)
2289 (defvar-local company-tooltip-offset 0)
2291 (defun company-tooltip--lines-update-offset (selection num-lines limit)
2293 (setq company-tooltip-offset
2294 (max (min selection company-tooltip-offset)
2295 (- selection -1 limit)))
2297 (when (<= company-tooltip-offset 1)
2299 (setq company-tooltip-offset 0))
2301 (when (>= company-tooltip-offset (- num-lines limit 1))
2303 (when (= selection (1- num-lines))
2304 (cl-decf company-tooltip-offset)
2305 (when (<= company-tooltip-offset 1)
2306 (setq company-tooltip-offset 0)
2311 (defun company-tooltip--simple-update-offset (selection _num-lines limit)
2312 (setq company-tooltip-offset
2313 (if (< selection company-tooltip-offset)
2315 (max company-tooltip-offset
2316 (- selection limit -1)))))
2320 (defsubst company-round-tab (arg)
2321 (* (/ (+ arg tab-width) tab-width) tab-width))
2323 (defun company-plainify (str)
2324 (let ((prefix (get-text-property 0 'line-prefix str)))
2325 (when prefix ; Keep the original value unmodified, for no special reason.
2326 (setq str (concat prefix str))
2327 (remove-text-properties 0 (length str) '(line-prefix) str)))
2328 (let* ((pieces (split-string str "\t"))
2331 (setcar copy (company-safe-substring
2332 (car copy) 0 (company-round-tab (string-width (car copy)))))
2334 (apply 'concat pieces)))
2336 (defun company-fill-propertize (value annotation width selected left right)
2337 (let* ((margin (length left))
2338 (common (or (company-call-backend 'match value)
2340 (string-width company-common)
2342 (_ (setq value (company--pre-render value)
2343 annotation (and annotation (company--pre-render annotation t))))
2344 (ann-ralign company-tooltip-align-annotations)
2345 (ann-truncate (< width
2346 (+ (length value) (length annotation)
2347 (if ann-ralign 1 0))))
2348 (ann-start (+ margin
2352 (- width (length annotation)))
2354 (ann-end (min (+ ann-start (length annotation)) (+ margin width)))
2356 (if (or ann-truncate (not ann-ralign))
2357 (company-safe-substring
2359 (when (and annotation ann-ralign) " ")
2363 (company-safe-substring value 0
2364 (- width (length annotation)))
2367 (setq common (+ (min common width) margin))
2368 (setq width (+ width margin (length right)))
2370 (font-lock-append-text-property 0 width 'mouse-face
2371 'company-tooltip-mouse
2373 (when (< ann-start ann-end)
2374 (font-lock-append-text-property ann-start ann-end 'face
2375 'company-tooltip-annotation
2377 (font-lock-prepend-text-property margin common 'face
2379 'company-tooltip-common-selection
2380 'company-tooltip-common)
2383 (if (let ((re (funcall company-search-regexp-function
2384 company-search-string)))
2385 (and (not (string= re ""))
2386 (string-match re value (length company-prefix))))
2387 (pcase-dolist (`(,mbeg . ,mend) (company--search-chunks))
2388 (let ((beg (+ margin mbeg))
2389 (end (+ margin mend))
2390 (width (- width (length right))))
2392 (font-lock-prepend-text-property beg (min end width)
2393 'face 'company-tooltip-search
2395 (font-lock-append-text-property 0 width 'face
2396 'company-tooltip-selection
2398 (font-lock-append-text-property 0 width 'face
2403 (defun company--search-chunks ()
2404 (let ((md (match-data t))
2406 (if (<= (length md) 2)
2407 (push (cons (nth 0 md) (nth 1 md)) res)
2408 (while (setq md (nthcdr 2 md))
2410 (push (cons (car md) (cadr md)) res))))
2413 (defun company--pre-render (str &optional annotation-p)
2414 (or (company-call-backend 'pre-render str annotation-p)
2416 (when (or (text-property-not-all 0 (length str) 'face nil str)
2417 (text-property-not-all 0 (length str) 'mouse-face nil str))
2418 (setq str (copy-sequence str))
2419 (remove-text-properties 0 (length str)
2420 '(face nil font-lock-face nil mouse-face nil)
2424 (defun company--clean-string (str)
2425 (replace-regexp-in-string
2426 "\\([^[:graph:] ]\\)\\|\\(\ufeff\\)\\|[[:multibyte:]]"
2429 ((match-beginning 1)
2430 ;; FIXME: Better char for 'non-printable'?
2431 ;; We shouldn't get any of these, but sometimes we might.
2433 ((match-beginning 2)
2434 ;; Zero-width non-breakable space.
2436 ((> (string-width match) 1)
2438 (make-string (1- (string-width match)) ?\ufeff)
2445 (defun company-buffer-lines (beg end)
2447 (let (lines lines-moved)
2448 (while (and (not (eobp)) ; http://debbugs.gnu.org/19553
2449 (> (setq lines-moved (vertical-motion 1)) 0)
2451 (let ((bound (min end (point))))
2452 ;; A visual line can contain several physical lines (e.g. with outline's
2453 ;; folding overlay). Take only the first one.
2454 (push (buffer-substring beg
2457 (re-search-forward "$" bound 'move)
2460 ;; One physical line can be displayed as several visual ones as well:
2461 ;; add empty strings to the list, to even the count.
2462 (dotimes (_ (1- lines-moved))
2465 (unless (eq beg end)
2466 (push (buffer-substring beg end) lines))
2469 (defun company-modify-line (old new offset)
2470 (concat (company-safe-substring old 0 offset)
2472 (company-safe-substring old (+ offset (length new)))))
2474 (defsubst company--length-limit (lst limit)
2475 (if (nthcdr limit lst)
2479 (defsubst company--window-height ()
2480 (if (fboundp 'window-screen-lines)
2481 (floor (window-screen-lines))
2482 (window-body-height)))
2484 (defun company--window-width ()
2485 (let ((ww (window-body-width)))
2486 ;; Account for the line continuation column.
2487 (when (zerop (cadr (window-fringes)))
2489 (unless (or (display-graphic-p)
2490 (version< "24.3.1" emacs-version))
2491 ;; Emacs 24.3 and earlier included margins
2492 ;; in window-width when in TTY.
2494 (let ((margins (window-margins)))
2495 (+ (or (car margins) 0)
2496 (or (cdr margins) 0)))))
2497 (when (and word-wrap
2498 (version< emacs-version "24.4.51.5"))
2499 ;; http://debbugs.gnu.org/19300
2501 ;; whitespace-mode with newline-mark
2502 (when (and buffer-display-table
2503 (aref buffer-display-table ?\n))
2504 (cl-decf ww (1- (length (aref buffer-display-table ?\n)))))
2507 (defun company--replacement-string (lines old column nl &optional align-top)
2508 (cl-decf column company-tooltip-margin)
2510 (when (and align-top company-tooltip-flip-when-above)
2511 (setq lines (reverse lines)))
2513 (let ((width (length (car lines)))
2514 (remaining-cols (- (+ (company--window-width) (window-hscroll))
2516 (when (> width remaining-cols)
2517 (cl-decf column (- width remaining-cols))))
2519 (let ((offset (and (< column 0) (- column)))
2524 ;; untouched lines first
2525 (dotimes (_ (- (length old) (length lines)))
2526 (push (pop old) new)))
2527 ;; length into old lines.
2529 (push (company-modify-line (pop old)
2530 (company--offset-line (pop lines) offset)
2533 ;; Append whole new lines.
2535 (push (concat (company-space-string column)
2536 (company--offset-line (pop lines) offset))
2539 (let ((str (concat (when nl " \n")
2540 (mapconcat 'identity (nreverse new) "\n")
2542 (font-lock-append-text-property 0 (length str) 'face 'default str)
2543 (when nl (put-text-property 0 1 'cursor t str))
2546 (defun company--offset-line (line offset)
2547 (if (and offset line)
2548 (substring line offset)
2551 (defun company--create-lines (selection limit)
2552 (let ((len company-candidates-length)
2553 (window-width (company--window-width))
2562 ;; Maybe clear old offset.
2563 (when (< len (+ company-tooltip-offset limit))
2564 (setq company-tooltip-offset 0))
2566 ;; Scroll to offset.
2567 (if (eq company-tooltip-offset-display 'lines)
2568 (setq limit (company-tooltip--lines-update-offset selection len limit))
2569 (company-tooltip--simple-update-offset selection len limit))
2572 ((eq company-tooltip-offset-display 'scrollbar)
2573 (setq scrollbar-bounds (company--scrollbar-bounds company-tooltip-offset
2575 ((eq company-tooltip-offset-display 'lines)
2576 (when (> company-tooltip-offset 0)
2577 (setq previous (format "...(%d)" company-tooltip-offset)))
2578 (setq remainder (- len limit company-tooltip-offset)
2579 remainder (when (> remainder 0)
2580 (setq remainder (format "...(%d)" remainder))))))
2582 (cl-decf selection company-tooltip-offset)
2583 (setq width (max (length previous) (length remainder))
2584 lines (nthcdr company-tooltip-offset company-candidates)
2588 (cl-decf window-width (* 2 company-tooltip-margin))
2589 (when scrollbar-bounds (cl-decf window-width))
2592 (let* ((value (pop lines-copy))
2593 (annotation (company-call-backend 'annotation value)))
2594 (setq value (company--clean-string (company-reformat value)))
2596 (when company-tooltip-align-annotations
2597 ;; `lisp-completion-at-point' adds a space.
2598 (setq annotation (comment-string-strip annotation t nil)))
2599 (setq annotation (company--clean-string annotation)))
2600 (push (cons value annotation) items)
2601 (setq width (max (+ (length value)
2602 (if (and annotation company-tooltip-align-annotations)
2603 (1+ (length annotation))
2604 (length annotation)))
2607 (setq width (min window-width
2608 (max company-tooltip-minimum-width
2609 (if company-show-numbers
2613 (let ((items (nreverse items))
2614 (numbered (if company-show-numbers 0 99999))
2617 (push (company--scrollpos-line previous width) new))
2620 (let* ((item (pop items))
2622 (annotation (cdr item))
2623 (right (company-space-string company-tooltip-margin))
2625 (when (< numbered 10)
2628 (setq right (concat (format " %d" (mod numbered 10)) right)))
2630 (company-fill-propertize str annotation
2631 width (equal i selection)
2632 (company-space-string
2633 company-tooltip-margin)
2635 (when scrollbar-bounds
2636 (company--scrollbar i scrollbar-bounds)))
2640 (push (company--scrollpos-line remainder width) new))
2644 (defun company--scrollbar-bounds (offset limit length)
2645 (when (> length limit)
2646 (let* ((size (ceiling (* limit (float limit)) length))
2647 (lower (floor (* limit (float offset)) length))
2648 (upper (+ lower size -1)))
2649 (cons lower upper))))
2651 (defun company--scrollbar (i bounds)
2652 (propertize " " 'face
2653 (if (and (>= i (car bounds)) (<= i (cdr bounds)))
2654 'company-scrollbar-fg
2655 'company-scrollbar-bg)))
2657 (defun company--scrollpos-line (text width)
2658 (propertize (concat (company-space-string company-tooltip-margin)
2659 (company-safe-substring text 0 width)
2660 (company-space-string company-tooltip-margin))
2661 'face 'company-tooltip))
2665 (defun company--pseudo-tooltip-height ()
2666 "Calculate the appropriate tooltip height.
2667 Returns a negative number if the tooltip should be displayed above point."
2668 (let* ((lines (company--row))
2669 (below (- (company--window-height) 1 lines)))
2670 (if (and (< below (min company-tooltip-minimum company-candidates-length))
2672 (- (max 3 (min company-tooltip-limit lines)))
2673 (max 3 (min company-tooltip-limit below)))))
2675 (defun company-pseudo-tooltip-show (row column selection)
2676 (company-pseudo-tooltip-hide)
2679 (let* ((height (company--pseudo-tooltip-height))
2683 (setq row (+ row height -1)
2686 (let* ((nl (< (move-to-window-line row) row))
2688 (end (save-excursion
2689 (move-to-window-line (+ row (abs height)))
2691 (ov (make-overlay beg end nil t))
2692 (args (list (mapcar 'company-plainify
2693 (company-buffer-lines beg end))
2696 (setq company-pseudo-tooltip-overlay ov)
2697 (overlay-put ov 'company-replacement-args args)
2699 (let ((lines (company--create-lines selection (abs height))))
2700 (overlay-put ov 'company-display
2701 (apply 'company--replacement-string lines args))
2702 (overlay-put ov 'company-width (string-width (car lines))))
2704 (overlay-put ov 'company-column column)
2705 (overlay-put ov 'company-height height)))))
2707 (defun company-pseudo-tooltip-show-at-point (pos column-offset)
2708 (let* ((col-row (company--col-row pos))
2709 (col (- (car col-row) column-offset)))
2710 (when (< col 0) (setq col 0))
2711 (company-pseudo-tooltip-show (1+ (cdr col-row)) col company-selection)))
2713 (defun company-pseudo-tooltip-edit (selection)
2714 (let* ((height (overlay-get company-pseudo-tooltip-overlay 'company-height))
2715 (lines (company--create-lines selection (abs height))))
2716 (overlay-put company-pseudo-tooltip-overlay 'company-width
2717 (string-width (car lines)))
2718 (overlay-put company-pseudo-tooltip-overlay 'company-display
2719 (apply 'company--replacement-string
2721 (overlay-get company-pseudo-tooltip-overlay
2722 'company-replacement-args)))))
2724 (defun company-pseudo-tooltip-hide ()
2725 (when company-pseudo-tooltip-overlay
2726 (delete-overlay company-pseudo-tooltip-overlay)
2727 (setq company-pseudo-tooltip-overlay nil)))
2729 (defun company-pseudo-tooltip-hide-temporarily ()
2730 (when (overlayp company-pseudo-tooltip-overlay)
2731 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
2732 (overlay-put company-pseudo-tooltip-overlay 'line-prefix nil)
2733 (overlay-put company-pseudo-tooltip-overlay 'after-string nil)
2734 (overlay-put company-pseudo-tooltip-overlay 'display nil)))
2736 (defun company-pseudo-tooltip-unhide ()
2737 (when company-pseudo-tooltip-overlay
2738 (let* ((ov company-pseudo-tooltip-overlay)
2739 (disp (overlay-get ov 'company-display)))
2740 ;; Beat outline's folding overlays, at least.
2741 (overlay-put ov 'priority 1)
2742 ;; No (extra) prefix for the first line.
2743 (overlay-put ov 'line-prefix "")
2744 ;; `display' is better
2745 ;; (http://debbugs.gnu.org/18285, http://debbugs.gnu.org/20847),
2746 ;; but it doesn't work on 0-length overlays.
2747 (if (< (overlay-start ov) (overlay-end ov))
2748 (overlay-put ov 'display disp)
2749 (overlay-put ov 'after-string disp)
2750 (overlay-put ov 'invisible t))
2751 (overlay-put ov 'window (selected-window)))))
2753 (defun company-pseudo-tooltip-guard ()
2755 (save-excursion (beginning-of-visual-line))
2756 (let ((ov company-pseudo-tooltip-overlay)
2757 (overhang (save-excursion (end-of-visual-line)
2758 (- (line-end-position) (point)))))
2759 (when (>= (overlay-get ov 'company-height) 0)
2761 (buffer-substring-no-properties (point) (overlay-start ov))
2762 (when (>= overhang 0) overhang))))))
2764 (defun company-pseudo-tooltip-frontend (command)
2765 "`company-mode' frontend similar to a tooltip but based on overlays."
2767 (pre-command (company-pseudo-tooltip-hide-temporarily))
2769 (unless (when (overlayp company-pseudo-tooltip-overlay)
2770 (let* ((ov company-pseudo-tooltip-overlay)
2771 (old-height (overlay-get ov 'company-height))
2772 (new-height (company--pseudo-tooltip-height)))
2774 (>= (* old-height new-height) 0)
2775 (>= (abs old-height) (abs new-height))
2776 (equal (company-pseudo-tooltip-guard)
2777 (overlay-get ov 'company-guard)))))
2779 (company-pseudo-tooltip-show-at-point (point) (length company-prefix))
2780 (overlay-put company-pseudo-tooltip-overlay
2781 'company-guard (company-pseudo-tooltip-guard)))
2782 (company-pseudo-tooltip-unhide))
2783 (hide (company-pseudo-tooltip-hide)
2784 (setq company-tooltip-offset 0))
2785 (update (when (overlayp company-pseudo-tooltip-overlay)
2786 (company-pseudo-tooltip-edit company-selection)))))
2788 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
2789 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
2790 (unless (and (eq command 'post-command)
2791 (company--show-inline-p))
2792 (company-pseudo-tooltip-frontend command)))
2794 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2796 (defvar-local company-preview-overlay nil)
2798 (defun company-preview-show-at-point (pos)
2799 (company-preview-hide)
2801 (let ((completion (nth company-selection company-candidates)))
2802 (setq completion (copy-sequence (company--pre-render completion)))
2803 (font-lock-append-text-property 0 (length completion)
2804 'face 'company-preview
2806 (font-lock-prepend-text-property 0 (length company-common)
2807 'face 'company-preview-common
2810 ;; Add search string
2811 (and (string-match (funcall company-search-regexp-function
2812 company-search-string)
2814 (pcase-dolist (`(,mbeg . ,mend) (company--search-chunks))
2815 (font-lock-prepend-text-property mbeg mend
2816 'face 'company-preview-search
2819 (setq completion (company-strip-prefix completion))
2821 (and (equal pos (point))
2822 (not (equal completion ""))
2823 (add-text-properties 0 1 '(cursor 1) completion))
2826 (pto company-pseudo-tooltip-overlay)
2827 (ptf-workaround (and
2830 (eq pos (overlay-start pto)))))
2831 ;; Try to accomodate for the pseudo-tooltip overlay,
2832 ;; which may start at the same position if it's at eol.
2833 (when ptf-workaround
2835 (setq completion (concat (buffer-substring beg pos) completion)))
2837 (setq company-preview-overlay (make-overlay beg pos))
2839 (let ((ov company-preview-overlay))
2840 (overlay-put ov (if ptf-workaround 'display 'after-string)
2842 (overlay-put ov 'window (selected-window))))))
2844 (defun company-preview-hide ()
2845 (when company-preview-overlay
2846 (delete-overlay company-preview-overlay)
2847 (setq company-preview-overlay nil)))
2849 (defun company-preview-frontend (command)
2850 "`company-mode' frontend showing the selection as if it had been inserted."
2852 (`pre-command (company-preview-hide))
2853 (`post-command (company-preview-show-at-point (point)))
2854 (`hide (company-preview-hide))))
2856 (defun company-preview-if-just-one-frontend (command)
2857 "`company-preview-frontend', but only shown for single candidates."
2858 (when (or (not (eq command 'post-command))
2859 (company--show-inline-p))
2860 (company-preview-frontend command)))
2862 (defun company--show-inline-p ()
2863 (and (not (cdr company-candidates))
2865 (or (eq (company-call-backend 'ignore-case) 'keep-prefix)
2866 (string-prefix-p company-prefix company-common))))
2868 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2870 (defvar-local company-echo-last-msg nil)
2872 (defvar company-echo-timer nil)
2874 (defvar company-echo-delay .01)
2876 (defun company-echo-show (&optional getter)
2878 (setq company-echo-last-msg (funcall getter)))
2879 (let ((message-log-max nil))
2880 (if company-echo-last-msg
2881 (message "%s" company-echo-last-msg)
2884 (defun company-echo-show-soon (&optional getter)
2885 (company-echo-cancel)
2886 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
2888 (defun company-echo-cancel (&optional unset)
2889 (when company-echo-timer
2890 (cancel-timer company-echo-timer))
2892 (setq company-echo-timer nil)))
2894 (defun company-echo-show-when-idle (&optional getter)
2895 (company-echo-cancel)
2896 (setq company-echo-timer
2897 (run-with-idle-timer company-echo-delay nil 'company-echo-show getter)))
2899 (defun company-echo-format ()
2901 (let ((limit (window-body-width (minibuffer-window)))
2903 ;; Roll to selection.
2904 (candidates (nthcdr company-selection company-candidates))
2905 (i (if company-show-numbers company-selection 99999))
2909 (setq comp (company-reformat (pop candidates))
2910 len (+ len 1 (length comp)))
2914 (setq comp (propertize (format "%d: %s" i comp)
2915 'face 'company-echo))
2918 (add-text-properties 3 (+ 3 (length company-common))
2919 '(face company-echo-common) comp))
2920 (setq comp (propertize comp 'face 'company-echo))
2921 (add-text-properties 0 (length company-common)
2922 '(face company-echo-common) comp))
2924 (setq candidates nil)
2927 (mapconcat 'identity (nreverse msg) " ")))
2929 (defun company-echo-strip-common-format ()
2931 (let ((limit (window-body-width (minibuffer-window)))
2932 (len (+ (length company-prefix) 2))
2933 ;; Roll to selection.
2934 (candidates (nthcdr company-selection company-candidates))
2935 (i (if company-show-numbers company-selection 99999))
2939 (setq comp (company-strip-prefix (pop candidates))
2940 len (+ len 2 (length comp)))
2943 (setq comp (format "%s (%d)" comp i))
2947 (setq candidates nil)
2948 (push (propertize comp 'face 'company-echo) msg)))
2950 (concat (propertize company-prefix 'face 'company-echo-common) "{"
2951 (mapconcat 'identity (nreverse msg) ", ")
2954 (defun company-echo-hide ()
2955 (unless (equal company-echo-last-msg "")
2956 (setq company-echo-last-msg "")
2957 (company-echo-show)))
2959 (defun company-echo-frontend (command)
2960 "`company-mode' frontend showing the candidates in the echo area."
2962 (`post-command (company-echo-show-soon 'company-echo-format))
2963 (`hide (company-echo-hide))))
2965 (defun company-echo-strip-common-frontend (command)
2966 "`company-mode' frontend showing the candidates in the echo area."
2968 (`post-command (company-echo-show-soon 'company-echo-strip-common-format))
2969 (`hide (company-echo-hide))))
2971 (defun company-echo-metadata-frontend (command)
2972 "`company-mode' frontend showing the documentation in the echo area."
2974 (`post-command (company-echo-show-when-idle 'company-fetch-metadata))
2975 (`hide (company-echo-hide))))
2978 ;;; company.el ends here