]> code.delx.au - gnu-emacs-elpa/blob - company.el
company--continue: don't pass the new prefix to `company-cancel'
[gnu-emacs-elpa] / company.el
1 ;;; company.el --- Modular text completion framework -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6 ;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
7 ;; URL: http://company-mode.github.io/
8 ;; Version: 0.8.0
9 ;; Keywords: abbrev, convenience, matching
10 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
11
12 ;; This file is part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28 ;;
29 ;; Company is a modular completion mechanism. Modules for retrieving completion
30 ;; candidates are called back-ends, modules for displaying them are front-ends.
31 ;;
32 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
33 ;; distributed in separate files and can be used individually.
34 ;;
35 ;; Place company.el and the back-ends you want to use in a directory and add the
36 ;; following to your .emacs:
37 ;; (add-to-list 'load-path "/path/to/company")
38 ;; (autoload 'company-mode "company" nil t)
39 ;;
40 ;; Enable company-mode with M-x company-mode. For further information look at
41 ;; the documentation for `company-mode' (C-h f company-mode RET)
42 ;;
43 ;; If you want to start a specific back-end, call it interactively or use
44 ;; `company-begin-backend'. For example:
45 ;; M-x company-abbrev will prompt for and insert an abbrev.
46 ;;
47 ;; To write your own back-end, look at the documentation for `company-backends'.
48 ;; Here is a simple example completing "foo":
49 ;;
50 ;; (defun company-my-backend (command &optional arg &rest ignored)
51 ;; (pcase command
52 ;; (`prefix (when (looking-back "foo\\>")
53 ;; (match-string 0)))
54 ;; (`candidates (list "foobar" "foobaz" "foobarbaz"))
55 ;; (`meta (format "This value is named %s" arg))))
56 ;;
57 ;; Sometimes it is a good idea to mix several back-ends together, for example to
58 ;; enrich gtags with dabbrev-code results (to emulate local variables).
59 ;; To do this, add a list with both back-ends as an element in company-backends.
60 ;;
61 ;; Known Issues:
62 ;; When point is at the very end of the buffer, the pseudo-tooltip appears very
63 ;; wrong, unless company is allowed to temporarily insert a fake newline.
64 ;; This behavior is enabled by `company-end-of-buffer-workaround'.
65 ;;
66 ;;; Change Log:
67 ;;
68 ;; See NEWS.md in the repository.
69
70 ;;; Code:
71
72 (require 'cl-lib)
73 (require 'newcomment)
74
75 ;; FIXME: Use `user-error'.
76 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
77 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
78 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
79 (add-to-list 'debug-ignored-errors "^Company not ")
80 (add-to-list 'debug-ignored-errors "^No candidate number ")
81 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
82 (add-to-list 'debug-ignored-errors "^No other back-end$")
83
84 (defgroup company nil
85 "Extensible inline text completion mechanism"
86 :group 'abbrev
87 :group 'convenience
88 :group 'matching)
89
90 (defface company-tooltip
91 '((default :foreground "black")
92 (((class color) (min-colors 88) (background light))
93 (:background "cornsilk"))
94 (((class color) (min-colors 88) (background dark))
95 (:background "yellow")))
96 "Face used for the tooltip.")
97
98 (defface company-tooltip-selection
99 '((default :inherit company-tooltip)
100 (((class color) (min-colors 88) (background light))
101 (:background "light blue"))
102 (((class color) (min-colors 88) (background dark))
103 (:background "orange1"))
104 (t (:background "green")))
105 "Face used for the selection in the tooltip.")
106
107 (defface company-tooltip-mouse
108 '((default :inherit highlight))
109 "Face used for the tooltip item under the mouse.")
110
111 (defface company-tooltip-common
112 '((default :inherit company-tooltip)
113 (((background light))
114 :foreground "darkred")
115 (((background dark))
116 :foreground "red"))
117 "Face used for the common completion in the tooltip.")
118
119 (defface company-tooltip-common-selection
120 '((default :inherit company-tooltip-selection)
121 (((background light))
122 :foreground "darkred")
123 (((background dark))
124 :foreground "red"))
125 "Face used for the selected common completion in the tooltip.")
126
127 (defface company-tooltip-annotation
128 '((default :inherit company-tooltip)
129 (((background light))
130 :foreground "firebrick4")
131 (((background dark))
132 :foreground "red4"))
133 "Face used for the annotation in the tooltip.")
134
135 (defface company-scrollbar-fg
136 '((((background light))
137 :background "darkred")
138 (((background dark))
139 :background "red"))
140 "Face used for the tooltip scrollbar thumb.")
141
142 (defface company-scrollbar-bg
143 '((default :inherit company-tooltip)
144 (((background light))
145 :background "wheat")
146 (((background dark))
147 :background "gold"))
148 "Face used for the tooltip scrollbar background.")
149
150 (defface company-preview
151 '((((background light))
152 :inherit company-tooltip-selection)
153 (((background dark))
154 :background "blue4"
155 :foreground "wheat"))
156 "Face used for the completion preview.")
157
158 (defface company-preview-common
159 '((((background light))
160 :inherit company-tooltip-selection)
161 (((background dark))
162 :inherit company-preview
163 :foreground "red"))
164 "Face used for the common part of the completion preview.")
165
166 (defface company-preview-search
167 '((((background light))
168 :inherit company-tooltip-common-selection)
169 (((background dark))
170 :inherit company-preview
171 :background "blue1"))
172 "Face used for the search string in the completion preview.")
173
174 (defface company-echo nil
175 "Face used for completions in the echo area.")
176
177 (defface company-echo-common
178 '((((background dark)) (:foreground "firebrick1"))
179 (((background light)) (:background "firebrick4")))
180 "Face used for the common part of completions in the echo area.")
181
182 (defun company-frontends-set (variable value)
183 ;; uniquify
184 (let ((remainder value))
185 (setcdr remainder (delq (car remainder) (cdr remainder))))
186 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
187 (memq 'company-pseudo-tooltip-frontend value)
188 (error "Pseudo tooltip frontend cannot be used twice"))
189 (and (memq 'company-preview-if-just-one-frontend value)
190 (memq 'company-preview-frontend value)
191 (error "Preview frontend cannot be used twice"))
192 (and (memq 'company-echo value)
193 (memq 'company-echo-metadata-frontend value)
194 (error "Echo area cannot be used twice"))
195 ;; preview must come last
196 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
197 (when (memq f value)
198 (setq value (append (delq f value) (list f)))))
199 (set variable value))
200
201 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
202 company-preview-if-just-one-frontend
203 company-echo-metadata-frontend)
204 "The list of active front-ends (visualizations).
205 Each front-end is a function that takes one argument. It is called with
206 one of the following arguments:
207
208 `show': When the visualization should start.
209
210 `hide': When the visualization should end.
211
212 `update': When the data has been updated.
213
214 `pre-command': Before every command that is executed while the
215 visualization is active.
216
217 `post-command': After every command that is executed while the
218 visualization is active.
219
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))))
237
238 (defcustom company-tooltip-limit 10
239 "The maximum number of candidates in the tooltip"
240 :type 'integer)
241
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."
245 :type 'integer)
246
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."
250 :type 'integer)
251
252 (defcustom company-tooltip-margin 1
253 "Width of margin columns to show around the toolip."
254 :type 'integer)
255
256 (defcustom company-tooltip-offset-display 'scrollbar
257 "Method using which the tooltip displays scrolling position.
258 `scrollbar' means draw a scrollbar to the right of the items.
259 `lines' means wrap items in lines with \"before\" and \"after\" counters."
260 :type '(choice (const :tag "Scrollbar" scrollbar)
261 (const :tag "Two lines" lines)))
262
263 (defcustom company-tooltip-align-annotations nil
264 "When non-nil, align annotations to the right tooltip border."
265 :type 'boolean)
266
267 (defvar company-safe-backends
268 '((company-abbrev . "Abbrev")
269 (company-bbdb . "BBDB")
270 (company-capf . "completion-at-point-functions")
271 (company-clang . "Clang")
272 (company-cmake . "CMake")
273 (company-css . "CSS")
274 (company-dabbrev . "dabbrev for plain text")
275 (company-dabbrev-code . "dabbrev for code")
276 (company-eclim . "Eclim (an Eclipse interface)")
277 (company-elisp . "Emacs Lisp")
278 (company-etags . "etags")
279 (company-files . "Files")
280 (company-gtags . "GNU Global")
281 (company-ispell . "Ispell")
282 (company-keywords . "Programming language keywords")
283 (company-nxml . "nxml")
284 (company-oddmuse . "Oddmuse")
285 (company-pysmell . "PySmell")
286 (company-ropemacs . "ropemacs")
287 (company-semantic . "Semantic")
288 (company-tempo . "Tempo templates")
289 (company-xcode . "Xcode")))
290 (put 'company-safe-backends 'risky-local-variable t)
291
292 (defun company-safe-backends-p (backends)
293 (and (consp backends)
294 (not (cl-dolist (backend backends)
295 (unless (if (consp backend)
296 (company-safe-backends-p backend)
297 (assq backend company-safe-backends))
298 (cl-return t))))))
299
300 (defcustom company-backends `(,@(unless (version< "24.3.50" emacs-version)
301 (list 'company-elisp))
302 company-bbdb
303 company-nxml company-css
304 company-eclim company-semantic company-clang
305 company-xcode company-ropemacs company-cmake
306 company-capf
307 (company-dabbrev-code company-gtags company-etags
308 company-keywords)
309 company-oddmuse company-files company-dabbrev)
310 "The list of active back-ends (completion engines).
311
312 `company-begin-backend' can be used to start a specific back-end,
313 `company-other-backend' will skip to the next matching back-end in the list.
314
315 Each back-end is a function that takes a variable number of arguments.
316 The first argument is the command requested from the back-end. It is one
317 of the following:
318
319 `prefix': The back-end should return the text to be completed. It must be
320 text immediately before point. Returning nil passes control to the next
321 back-end. The function should return `stop' if it should complete but
322 cannot \(e.g. if it is in the middle of a string\). Instead of a string,
323 the back-end may return a cons where car is the prefix and cdr is used in
324 `company-minimum-prefix-length' test. It must be either number or t, and
325 in the latter case the test automatically succeeds.
326
327 `candidates': The second argument is the prefix to be completed. The
328 return value should be a list of candidates that match the prefix.
329
330 Non-prefix matches are also supported (candidates that don't start with the
331 prefix, but match it in some backend-defined way). Backends that use this
332 feature must disable cache (return t to `no-cache') and should also respond
333 to `match'.
334
335 Optional commands:
336
337 `sorted': Return t here to indicate that the candidates are sorted and will
338 not need to be sorted again.
339
340 `duplicates': If non-nil, company will take care of removing duplicates
341 from the list.
342
343 `no-cache': Usually company doesn't ask for candidates again as completion
344 progresses, unless the back-end returns t for this command. The second
345 argument is the latest prefix.
346
347 `meta': The second argument is a completion candidate. Return a (short)
348 documentation string for it.
349
350 `doc-buffer': The second argument is a completion candidate. Return a
351 buffer with documentation for it. Preferably use `company-doc-buffer',
352
353 `location': The second argument is a completion candidate. Return the cons
354 of buffer and buffer location, or of file and line number where the
355 completion candidate was defined.
356
357 `annotation': The second argument is a completion candidate. Return a
358 string to be displayed inline with the candidate in the popup. If
359 duplicates are removed by company, candidates with equal string values will
360 be kept if they have different annotations. For that to work properly,
361 backends should store the related information on candidates using text
362 properties.
363
364 `match': The second argument is a completion candidate. Backends that
365 provide non-prefix completions should return the position of the end of
366 text in the candidate that matches `prefix'. It will be used when
367 rendering the popup.
368
369 `require-match': If this returns t, the user is not allowed to enter
370 anything not offered as a candidate. Use with care! The default value nil
371 gives the user that choice with `company-require-match'. Return value
372 `never' overrides that option the other way around.
373
374 `init': Called once for each buffer. The back-end can check for external
375 programs and files and load any required libraries. Raising an error here
376 will show up in message log once, and the back-end will not be used for
377 completion.
378
379 `post-completion': Called after a completion candidate has been inserted
380 into the buffer. The second argument is the candidate. Can be used to
381 modify it, e.g. to expand a snippet.
382
383 The back-end should return nil for all commands it does not support or
384 does not know about. It should also be callable interactively and use
385 `company-begin-backend' to start itself in that case.
386
387 Grouped back-ends:
388
389 An element of `company-backends' can also itself be a list of back-ends,
390 then it's considered to be a \"grouped\" back-end.
391
392 When possible, commands taking a candidate as an argument are dispatched to
393 the back-end it came from. In other cases, the first non-nil value among
394 all the back-ends is returned.
395
396 The latter is the case for the `prefix' command. But if the group contains
397 the keyword `:with', the back-ends after it are ignored for this command.
398
399 The completions from back-ends in a group are merged (but only from those
400 that return the same `prefix').
401
402 Asynchronous back-ends:
403
404 The return value of each command can also be a cons (:async . FETCHER)
405 where FETCHER is a function of one argument, CALLBACK. When the data
406 arrives, FETCHER must call CALLBACK and pass it the appropriate return
407 value, as described above.
408
409 True asynchronous operation is only supported for command `candidates', and
410 only during idle completion. Other commands will block the user interface,
411 even if the back-end uses the asynchronous calling convention."
412 :type `(repeat
413 (choice
414 :tag "Back-end"
415 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
416 company-safe-backends)
417 (symbol :tag "User defined")
418 (repeat :tag "Merged Back-ends"
419 (choice :tag "Back-end"
420 ,@(mapcar (lambda (b)
421 `(const :tag ,(cdr b) ,(car b)))
422 company-safe-backends)
423 (const :tag "With" :with)
424 (symbol :tag "User defined"))))))
425
426 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
427
428 (defcustom company-transformers nil
429 "Functions to change the list of candidates received from backends,
430 after sorting and removal of duplicates (if appropriate).
431 Each function gets called with the return value of the previous one."
432 :type '(choice
433 (const :tag "None" nil)
434 (const :tag "Sort by occurrence" (company-sort-by-occurrence))
435 (repeat :tag "User defined" (function))))
436
437 (defcustom company-completion-started-hook nil
438 "Hook run when company starts completing.
439 The hook is called with one argument that is non-nil if the completion was
440 started manually."
441 :type 'hook)
442
443 (defcustom company-completion-cancelled-hook nil
444 "Hook run when company cancels completing.
445 The hook is called with one argument that is non-nil if the completion was
446 aborted manually."
447 :type 'hook)
448
449 (defcustom company-completion-finished-hook nil
450 "Hook run when company successfully completes.
451 The hook is called with the selected candidate as an argument.
452
453 If you indend to use it to post-process candidates from a specific
454 back-end, consider using the `post-completion' command instead."
455 :type 'hook)
456
457 (defcustom company-minimum-prefix-length 3
458 "The minimum prefix length for idle completion."
459 :type '(integer :tag "prefix length"))
460
461 (defcustom company-abort-manual-when-too-short nil
462 "If enabled, cancel a manually started completion when the prefix gets
463 shorter than both `company-minimum-prefix-length' and the length of the
464 prefix it was started from."
465 :type 'boolean)
466
467 (defcustom company-require-match 'company-explicit-action-p
468 "If enabled, disallow non-matching input.
469 This can be a function do determine if a match is required.
470
471 This can be overridden by the back-end, if it returns t or `never' to
472 `require-match'. `company-auto-complete' also takes precedence over this."
473 :type '(choice (const :tag "Off" nil)
474 (function :tag "Predicate function")
475 (const :tag "On, if user interaction took place"
476 'company-explicit-action-p)
477 (const :tag "On" t)))
478
479 (defcustom company-auto-complete nil
480 "Determines when to auto-complete.
481 If this is enabled, all characters from `company-auto-complete-chars'
482 trigger insertion of the selected completion candidate.
483 This can also be a function."
484 :type '(choice (const :tag "Off" nil)
485 (function :tag "Predicate function")
486 (const :tag "On, if user interaction took place"
487 'company-explicit-action-p)
488 (const :tag "On" t)))
489
490 (defcustom company-auto-complete-chars '(?\ ?\) ?.)
491 "Determines which characters trigger auto-completion.
492 See `company-auto-complete'. If this is a string, each string character
493 tiggers auto-completion. If it is a list of syntax description characters (see
494 `modify-syntax-entry'), all characters with that syntax auto-complete.
495
496 This can also be a function, which is called with the new input and should
497 return non-nil if company should auto-complete.
498
499 A character that is part of a valid candidate never triggers auto-completion."
500 :type '(choice (string :tag "Characters")
501 (set :tag "Syntax"
502 (const :tag "Whitespace" ?\ )
503 (const :tag "Symbol" ?_)
504 (const :tag "Opening parentheses" ?\()
505 (const :tag "Closing parentheses" ?\))
506 (const :tag "Word constituent" ?w)
507 (const :tag "Punctuation." ?.)
508 (const :tag "String quote." ?\")
509 (const :tag "Paired delimiter." ?$)
510 (const :tag "Expression quote or prefix operator." ?\')
511 (const :tag "Comment starter." ?<)
512 (const :tag "Comment ender." ?>)
513 (const :tag "Character-quote." ?/)
514 (const :tag "Generic string fence." ?|)
515 (const :tag "Generic comment fence." ?!))
516 (function :tag "Predicate function")))
517
518 (defcustom company-idle-delay .5
519 "The idle delay in seconds until completion starts automatically.
520 A value of nil means no idle completion, t means show candidates
521 immediately when a prefix of `company-minimum-prefix-length' is reached."
522 :type '(choice (const :tag "never (nil)" nil)
523 (const :tag "immediate (t)" t)
524 (number :tag "seconds")))
525
526 (defcustom company-begin-commands '(self-insert-command org-self-insert-command)
527 "A list of commands after which idle completion is allowed.
528 If this is t, it can show completions after any command. See
529 `company-idle-delay'.
530
531 Alternatively, any command with a non-nil `company-begin' property is
532 treated as if it was on this list."
533 :type '(choice (const :tag "Any command" t)
534 (const :tag "Self insert command" '(self-insert-command))
535 (repeat :tag "Commands" function)))
536
537 (defcustom company-continue-commands '(not save-buffer save-some-buffers
538 save-buffers-kill-terminal
539 save-buffers-kill-emacs)
540 "A list of commands that are allowed during completion.
541 If this is t, or if `company-begin-commands' is t, any command is allowed.
542 Otherwise, the value must be a list of symbols. If it starts with `not',
543 the cdr is the list of commands that abort completion. Otherwise, all
544 commands except those in that list, or in `company-begin-commands', or
545 commands in the `company-' namespace, abort completion."
546 :type '(choice (const :tag "Any command" t)
547 (cons :tag "Any except"
548 (const not)
549 (repeat :tag "Commands" function))
550 (repeat :tag "Commands" function)))
551
552 (defcustom company-show-numbers nil
553 "If enabled, show quick-access numbers for the first ten candidates."
554 :type '(choice (const :tag "off" nil)
555 (const :tag "on" t)))
556
557 (defcustom company-selection-wrap-around nil
558 "If enabled, selecting item before first or after last wraps around."
559 :type '(choice (const :tag "off" nil)
560 (const :tag "on" t)))
561
562 (defvar company-end-of-buffer-workaround t
563 "Work around a visualization bug when completing at the end of the buffer.
564 The work-around consists of adding a newline.")
565
566 (defvar company-async-wait 0.03
567 "Pause between checks to see if the value's been set when turning an
568 asynchronous call into synchronous.")
569
570 (defvar company-async-timeout 2
571 "Maximum wait time for a value to be set during asynchronous call.")
572
573 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
574
575 (defvar company-mode-map (make-sparse-keymap)
576 "Keymap used by `company-mode'.")
577
578 (defvar company-active-map
579 (let ((keymap (make-sparse-keymap)))
580 (define-key keymap "\e\e\e" 'company-abort)
581 (define-key keymap "\C-g" 'company-abort)
582 (define-key keymap (kbd "M-n") 'company-select-next)
583 (define-key keymap (kbd "M-p") 'company-select-previous)
584 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
585 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
586 (define-key keymap [down-mouse-1] 'ignore)
587 (define-key keymap [down-mouse-3] 'ignore)
588 (define-key keymap [mouse-1] 'company-complete-mouse)
589 (define-key keymap [mouse-3] 'company-select-mouse)
590 (define-key keymap [up-mouse-1] 'ignore)
591 (define-key keymap [up-mouse-3] 'ignore)
592 (define-key keymap [return] 'company-complete-selection)
593 (define-key keymap (kbd "RET") 'company-complete-selection)
594 (define-key keymap [tab] 'company-complete-common)
595 (define-key keymap (kbd "TAB") 'company-complete-common)
596 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
597 (define-key keymap "\C-w" 'company-show-location)
598 (define-key keymap "\C-s" 'company-search-candidates)
599 (define-key keymap "\C-\M-s" 'company-filter-candidates)
600 (dotimes (i 10)
601 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
602 `(lambda ()
603 (interactive)
604 (company-complete-number ,(if (zerop i) 10 i)))))
605
606 keymap)
607 "Keymap that is enabled during an active completion.")
608
609 (defvar company--disabled-backends nil)
610
611 (defun company-init-backend (backend)
612 (and (symbolp backend)
613 (not (fboundp backend))
614 (ignore-errors (require backend nil t)))
615 (cond
616 ((symbolp backend)
617 (condition-case err
618 (progn
619 (funcall backend 'init)
620 (put backend 'company-init t))
621 (error
622 (put backend 'company-init 'failed)
623 (unless (memq backend company--disabled-backends)
624 (message "Company back-end '%s' could not be initialized:\n%s"
625 backend (error-message-string err)))
626 (cl-pushnew backend company--disabled-backends)
627 nil)))
628 ;; No initialization for lambdas.
629 ((functionp backend) t)
630 (t ;; Must be a list.
631 (cl-dolist (b backend)
632 (unless (keywordp b)
633 (company-init-backend b))))))
634
635 (defvar company-default-lighter " company")
636
637 (defvar company-lighter company-default-lighter)
638 (make-variable-buffer-local 'company-lighter)
639
640 ;;;###autoload
641 (define-minor-mode company-mode
642 "\"complete anything\"; is an in-buffer completion framework.
643 Completion starts automatically, depending on the values
644 `company-idle-delay' and `company-minimum-prefix-length'.
645
646 Completion can be controlled with the commands:
647 `company-complete-common', `company-complete-selection', `company-complete',
648 `company-select-next', `company-select-previous'. If these commands are
649 called before `company-idle-delay', completion will also start.
650
651 Completions can be searched with `company-search-candidates' or
652 `company-filter-candidates'. These can be used while completion is
653 inactive, as well.
654
655 The completion data is retrieved using `company-backends' and displayed
656 using `company-frontends'. If you want to start a specific back-end, call
657 it interactively or use `company-begin-backend'.
658
659 regular keymap (`company-mode-map'):
660
661 \\{company-mode-map}
662 keymap during active completions (`company-active-map'):
663
664 \\{company-active-map}"
665 nil company-lighter company-mode-map
666 (if company-mode
667 (progn
668 (add-hook 'pre-command-hook 'company-pre-command nil t)
669 (add-hook 'post-command-hook 'company-post-command nil t)
670 (mapc 'company-init-backend company-backends))
671 (remove-hook 'pre-command-hook 'company-pre-command t)
672 (remove-hook 'post-command-hook 'company-post-command t)
673 (company-cancel)
674 (kill-local-variable 'company-point)))
675
676 (defcustom company-global-modes t
677 "Modes for which `company-mode' mode is turned on by `global-company-mode'.
678 If nil, means no modes. If t, then all major modes have it turned on.
679 If a list, it should be a list of `major-mode' symbol names for which
680 `company-mode' should be automatically turned on. The sense of the list is
681 negated if it begins with `not'. For example:
682 (c-mode c++-mode)
683 means that `company-mode' is turned on for buffers in C and C++ modes only.
684 (not message-mode)
685 means that `company-mode' is always turned on except in `message-mode' buffers."
686 :type '(choice (const :tag "none" nil)
687 (const :tag "all" t)
688 (set :menu-tag "mode specific" :tag "modes"
689 :value (not)
690 (const :tag "Except" not)
691 (repeat :inline t (symbol :tag "mode")))))
692
693 ;;;###autoload
694 (define-globalized-minor-mode global-company-mode company-mode company-mode-on)
695
696 (defun company-mode-on ()
697 (when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s)))
698 (cond ((eq company-global-modes t)
699 t)
700 ((eq (car-safe company-global-modes) 'not)
701 (not (memq major-mode (cdr company-global-modes))))
702 (t (memq major-mode company-global-modes))))
703 (company-mode 1)))
704
705 (defsubst company-assert-enabled ()
706 (unless company-mode
707 (company-uninstall-map)
708 (error "Company not enabled")))
709
710 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
711
712 (defvar company-my-keymap nil)
713 (make-variable-buffer-local 'company-my-keymap)
714
715 (defvar company-emulation-alist '((t . nil)))
716
717 (defsubst company-enable-overriding-keymap (keymap)
718 (company-uninstall-map)
719 (setq company-my-keymap keymap))
720
721 (defun company-ensure-emulation-alist ()
722 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
723 (setq emulation-mode-map-alists
724 (cons 'company-emulation-alist
725 (delq 'company-emulation-alist emulation-mode-map-alists)))))
726
727 (defun company-install-map ()
728 (unless (or (cdar company-emulation-alist)
729 (null company-my-keymap))
730 (setf (cdar company-emulation-alist) company-my-keymap)))
731
732 (defun company-uninstall-map ()
733 (setf (cdar company-emulation-alist) nil))
734
735 ;; Hack:
736 ;; Emacs calculates the active keymaps before reading the event. That means we
737 ;; cannot change the keymap from a timer. So we send a bogus command.
738 ;; XXX: Seems not to be needed anymore in Emacs 24.4
739 (defun company-ignore ()
740 (interactive)
741 (setq this-command last-command))
742
743 (global-set-key '[31415926] 'company-ignore)
744
745 (defun company-input-noop ()
746 (push 31415926 unread-command-events))
747
748 (defun company--column (&optional pos)
749 (save-excursion
750 (when pos (goto-char pos))
751 (save-restriction
752 (+ (save-excursion
753 (vertical-motion 0)
754 (narrow-to-region (point) (point-max))
755 (let ((prefix (get-text-property (point) 'line-prefix)))
756 (if prefix (length prefix) 0)))
757 (current-column)))))
758
759 (defun company--row (&optional pos)
760 (save-excursion
761 (when pos (goto-char pos))
762 (count-screen-lines (window-start)
763 (progn (vertical-motion 0) (point)))))
764
765 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
766
767 (defvar company-backend nil)
768 (make-variable-buffer-local 'company-backend)
769
770 (defun company-grab (regexp &optional expression limit)
771 (when (looking-back regexp limit)
772 (or (match-string-no-properties (or expression 0)) "")))
773
774 (defun company-grab-line (regexp &optional expression)
775 (company-grab regexp expression (point-at-bol)))
776
777 (defun company-grab-symbol ()
778 (if (looking-at "\\_>")
779 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
780 (point)))
781 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
782 "")))
783
784 (defun company-grab-word ()
785 (if (looking-at "\\>")
786 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
787 (point)))
788 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
789 "")))
790
791 (defun company-grab-symbol-cons (idle-begin-after-re &optional max-len)
792 (let ((symbol (company-grab-symbol)))
793 (when symbol
794 (save-excursion
795 (forward-char (- (length symbol)))
796 (if (looking-back idle-begin-after-re (if max-len
797 (- (point) max-len)
798 (line-beginning-position)))
799 (cons symbol t)
800 symbol)))))
801
802 (defun company-in-string-or-comment ()
803 (let ((ppss (syntax-ppss)))
804 (or (car (setq ppss (nthcdr 3 ppss)))
805 (car (setq ppss (cdr ppss)))
806 (nth 3 ppss))))
807
808 (defun company-call-backend (&rest args)
809 (company--force-sync #'company-call-backend-raw args company-backend))
810
811 (defun company--force-sync (fun args backend)
812 (let ((value (apply fun args)))
813 (if (not (eq (car-safe value) :async))
814 value
815 (let ((res 'trash)
816 (start (time-to-seconds)))
817 (funcall (cdr value)
818 (lambda (result) (setq res result)))
819 (while (eq res 'trash)
820 (if (> (- (time-to-seconds) start) company-async-timeout)
821 (error "Company: Back-end %s async timeout with args %s"
822 backend args)
823 (sleep-for company-async-wait)))
824 res))))
825
826 (defun company-call-backend-raw (&rest args)
827 (condition-case err
828 (if (functionp company-backend)
829 (apply company-backend args)
830 (apply #'company--multi-backend-adapter company-backend args))
831 (error (error "Company: Back-end %s error \"%s\" with args %s"
832 company-backend (error-message-string err) args))))
833
834 (defun company--multi-backend-adapter (backends command &rest args)
835 (let ((backends (cl-loop for b in backends
836 when (not (and (symbolp b)
837 (eq 'failed (get b 'company-init))))
838 collect b)))
839 (setq backends
840 (if (eq command 'prefix)
841 (butlast backends (length (member :with backends)))
842 (delq :with backends)))
843 (pcase command
844 (`candidates
845 (company--multi-backend-adapter-candidates backends (car args)))
846 (`sorted nil)
847 (`duplicates t)
848 ((or `prefix `ignore-case `no-cache `require-match)
849 (let (value)
850 (cl-dolist (backend backends)
851 (when (setq value (company--force-sync
852 backend (cons command args) backend))
853 (cl-return value)))))
854 (_
855 (let ((arg (car args)))
856 (when (> (length arg) 0)
857 (let ((backend (or (get-text-property 0 'company-backend arg)
858 (car backends))))
859 (apply backend command args))))))))
860
861 (defun company--multi-backend-adapter-candidates (backends prefix)
862 (let ((pairs (cl-loop for backend in (cdr backends)
863 when (equal (company--prefix-str
864 (funcall backend 'prefix))
865 prefix)
866 collect (cons (funcall backend 'candidates prefix)
867 (let ((b backend))
868 (lambda (candidates)
869 (mapcar
870 (lambda (str)
871 (propertize str 'company-backend b))
872 candidates)))))))
873 (when (equal (company--prefix-str (funcall (car backends) 'prefix)) prefix)
874 ;; Small perf optimization: don't tag the candidates received
875 ;; from the first backend in the group.
876 (push (cons (funcall (car backends) 'candidates prefix)
877 'identity)
878 pairs))
879 (company--merge-async pairs (lambda (values) (apply #'append values)))))
880
881 (defun company--merge-async (pairs merger)
882 (let ((async (cl-loop for pair in pairs
883 thereis
884 (eq :async (car-safe (car pair))))))
885 (if (not async)
886 (funcall merger (cl-loop for (val . mapper) in pairs
887 collect (funcall mapper val)))
888 (cons
889 :async
890 (lambda (callback)
891 (let* (lst pending
892 (finisher (lambda ()
893 (unless pending
894 (funcall callback
895 (funcall merger
896 (nreverse lst)))))))
897 (dolist (pair pairs)
898 (let ((val (car pair))
899 (mapper (cdr pair)))
900 (if (not (eq :async (car-safe val)))
901 (push (funcall mapper val) lst)
902 (push nil lst)
903 (let ((cell lst)
904 (fetcher (cdr val)))
905 (push fetcher pending)
906 (funcall fetcher
907 (lambda (res)
908 (setq pending (delq fetcher pending))
909 (setcar cell (funcall mapper res))
910 (funcall finisher)))))))))))))
911
912 (defun company--prefix-str (prefix)
913 (or (car-safe prefix) prefix))
914
915 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
916
917 (defvar company-prefix nil)
918 (make-variable-buffer-local 'company-prefix)
919
920 (defvar company-candidates nil)
921 (make-variable-buffer-local 'company-candidates)
922
923 (defvar company-candidates-length nil)
924 (make-variable-buffer-local 'company-candidates-length)
925
926 (defvar company-candidates-cache nil)
927 (make-variable-buffer-local 'company-candidates-cache)
928
929 (defvar company-candidates-predicate nil)
930 (make-variable-buffer-local 'company-candidates-predicate)
931
932 (defvar company-common nil)
933 (make-variable-buffer-local 'company-common)
934
935 (defvar company-selection 0)
936 (make-variable-buffer-local 'company-selection)
937
938 (defvar company-selection-changed nil)
939 (make-variable-buffer-local 'company-selection-changed)
940
941 (defvar company--manual-action nil
942 "Non-nil, if manual completion took place.")
943 (make-variable-buffer-local 'company--manual-action)
944
945 (defvar company--manual-prefix nil)
946 (make-variable-buffer-local 'company--manual-prefix)
947
948 (defvar company--auto-completion nil
949 "Non-nil when current candidate is being inserted automatically.
950 Controlled by `company-auto-complete'.")
951
952 (defvar company--point-max nil)
953 (make-variable-buffer-local 'company--point-max)
954
955 (defvar company-point nil)
956 (make-variable-buffer-local 'company-point)
957
958 (defvar company-timer nil)
959
960 (defvar company-added-newline nil)
961 (make-variable-buffer-local 'company-added-newline)
962
963 (defsubst company-strip-prefix (str)
964 (substring str (length company-prefix)))
965
966 (defun company--insert-candidate (candidate)
967 (setq candidate (substring-no-properties candidate))
968 ;; XXX: Return value we check here is subject to change.
969 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
970 (insert (company-strip-prefix candidate))
971 (delete-region (- (point) (length company-prefix)) (point))
972 (insert candidate)))
973
974 (defmacro company-with-candidate-inserted (candidate &rest body)
975 "Evaluate BODY with CANDIDATE temporarily inserted.
976 This is a tool for back-ends that need candidates inserted before they
977 can retrieve meta-data for them."
978 (declare (indent 1))
979 `(let ((inhibit-modification-hooks t)
980 (inhibit-point-motion-hooks t)
981 (modified-p (buffer-modified-p)))
982 (company--insert-candidate ,candidate)
983 (unwind-protect
984 (progn ,@body)
985 (delete-region company-point (point)))))
986
987 (defun company-explicit-action-p ()
988 "Return whether explicit completion action was taken by the user."
989 (or company--manual-action
990 company-selection-changed))
991
992 (defun company-reformat (candidate)
993 ;; company-ispell needs this, because the results are always lower-case
994 ;; It's mory efficient to fix it only when they are displayed.
995 ;; FIXME: Adopt the current text's capitalization instead?
996 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
997 (concat company-prefix (substring candidate (length company-prefix)))
998 candidate))
999
1000 (defun company--should-complete ()
1001 (and (not (or buffer-read-only overriding-terminal-local-map
1002 overriding-local-map))
1003 ;; Check if in the middle of entering a key combination.
1004 (or (equal (this-command-keys-vector) [])
1005 (not (keymapp (key-binding (this-command-keys-vector)))))
1006 (eq company-idle-delay t)
1007 (or (eq t company-begin-commands)
1008 (memq this-command company-begin-commands)
1009 (and (symbolp this-command) (get this-command 'company-begin)))
1010 (not (and transient-mark-mode mark-active))))
1011
1012 (defun company--should-continue ()
1013 (or (eq t company-begin-commands)
1014 (eq t company-continue-commands)
1015 (if (eq 'not (car company-continue-commands))
1016 (not (memq this-command (cdr company-continue-commands)))
1017 (or (memq this-command company-begin-commands)
1018 (memq this-command company-continue-commands)
1019 (string-match-p "\\`company-" (symbol-name this-command))))))
1020
1021 (defun company-call-frontends (command)
1022 (dolist (frontend company-frontends)
1023 (condition-case err
1024 (funcall frontend command)
1025 (error (error "Company: Front-end %s error \"%s\" on command %s"
1026 frontend (error-message-string err) command)))))
1027
1028 (defun company-set-selection (selection &optional force-update)
1029 (setq selection
1030 (if company-selection-wrap-around
1031 (mod selection company-candidates-length)
1032 (max 0 (min (1- company-candidates-length) selection))))
1033 (when (or force-update (not (equal selection company-selection)))
1034 (setq company-selection selection
1035 company-selection-changed t)
1036 (company-call-frontends 'update)))
1037
1038 (defun company-apply-predicate (candidates predicate)
1039 (let (new)
1040 (dolist (c candidates)
1041 (when (funcall predicate c)
1042 (push c new)))
1043 (nreverse new)))
1044
1045 (defun company-update-candidates (candidates)
1046 (setq company-candidates-length (length candidates))
1047 (if (> company-selection 0)
1048 ;; Try to restore the selection
1049 (let ((selected (nth company-selection company-candidates)))
1050 (setq company-selection 0
1051 company-candidates candidates)
1052 (when selected
1053 (while (and candidates (string< (pop candidates) selected))
1054 (cl-incf company-selection))
1055 (unless candidates
1056 ;; Make sure selection isn't out of bounds.
1057 (setq company-selection (min (1- company-candidates-length)
1058 company-selection)))))
1059 (setq company-selection 0
1060 company-candidates candidates))
1061 ;; Save in cache:
1062 (push (cons company-prefix company-candidates) company-candidates-cache)
1063 ;; Calculate common.
1064 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
1065 ;; We want to support non-prefix completion, so filtering is the
1066 ;; responsibility of each respective backend, not ours.
1067 ;; On the other hand, we don't want to replace non-prefix input in
1068 ;; `company-complete-common'.
1069 (setq company-common
1070 (if (cdr company-candidates)
1071 (let ((common (try-completion company-prefix company-candidates)))
1072 (if (eq common t)
1073 ;; Mulple equal strings, probably with different
1074 ;; annotations.
1075 company-prefix
1076 common))
1077 (car company-candidates)))))
1078
1079 (defun company-calculate-candidates (prefix)
1080 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
1081 (ignore-case (company-call-backend 'ignore-case)))
1082 (or candidates
1083 (when company-candidates-cache
1084 (let ((len (length prefix))
1085 (completion-ignore-case ignore-case)
1086 prev)
1087 (cl-dotimes (i (1+ len))
1088 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
1089 company-candidates-cache)))
1090 (setq candidates (all-completions prefix prev))
1091 (cl-return t)))))
1092 ;; no cache match, call back-end
1093 (setq candidates
1094 (company--process-candidates
1095 (company--fetch-candidates prefix))))
1096 (setq candidates (company--transform-candidates candidates))
1097 (when candidates
1098 (if (or (cdr candidates)
1099 (not (eq t (compare-strings (car candidates) nil nil
1100 prefix nil nil ignore-case))))
1101 candidates
1102 ;; Already completed and unique; don't start.
1103 t))))
1104
1105 (defun company--fetch-candidates (prefix)
1106 (let ((c (if company--manual-action
1107 (company-call-backend 'candidates prefix)
1108 (company-call-backend-raw 'candidates prefix)))
1109 res)
1110 (if (not (eq (car c) :async))
1111 c
1112 (let ((buf (current-buffer))
1113 (win (selected-window))
1114 (tick (buffer-chars-modified-tick))
1115 (pt (point))
1116 (backend company-backend))
1117 (funcall
1118 (cdr c)
1119 (lambda (candidates)
1120 (if (not (and candidates (eq res 'done)))
1121 ;; Fetcher called us right back.
1122 (setq res candidates)
1123 (setq company-backend backend
1124 company-candidates-cache
1125 (list (cons prefix
1126 (company--process-candidates
1127 candidates))))
1128 (company-idle-begin buf win tick pt)))))
1129 ;; FIXME: Relying on the fact that the callers
1130 ;; will interpret nil as "do nothing" is shaky.
1131 ;; A throw-catch would be one possible improvement.
1132 (or res
1133 (progn (setq res 'done) nil)))))
1134
1135 (defun company--process-candidates (candidates)
1136 (when company-candidates-predicate
1137 (setq candidates
1138 (company-apply-predicate candidates
1139 company-candidates-predicate)))
1140 (unless (company-call-backend 'sorted)
1141 (setq candidates (sort candidates 'string<)))
1142 (when (company-call-backend 'duplicates)
1143 (company--strip-duplicates candidates))
1144 candidates)
1145
1146 (defun company--strip-duplicates (candidates)
1147 (let ((c2 candidates))
1148 (while c2
1149 (setcdr c2
1150 (let ((str (car c2))
1151 (anno 'unk))
1152 (pop c2)
1153 (while (let ((str2 (car c2)))
1154 (if (not (equal str str2))
1155 nil
1156 (when (eq anno 'unk)
1157 (setq anno (company-call-backend
1158 'annotation str)))
1159 (equal anno
1160 (company-call-backend
1161 'annotation str2))))
1162 (pop c2))
1163 c2)))))
1164
1165 (defun company--transform-candidates (candidates)
1166 (let ((c candidates))
1167 (dolist (tr company-transformers)
1168 (setq c (funcall tr c)))
1169 c))
1170
1171 (defun company-sort-by-occurrence (candidates)
1172 "Sort CANDIDATES according to their occurrences.
1173 Searches for each in the currently visible part of the current buffer and
1174 gives priority to the closest ones above point, then closest ones below
1175 point. The rest of the list is appended unchanged.
1176 Keywords and function definition names are ignored."
1177 (let* (occurs
1178 (noccurs
1179 (cl-delete-if
1180 (lambda (candidate)
1181 (when (or
1182 (save-excursion
1183 (progn (forward-char (- (length company-prefix)))
1184 (search-backward candidate (window-start) t)))
1185 (save-excursion
1186 (search-forward candidate (window-end) t)))
1187 (let ((beg (match-beginning 0))
1188 (end (match-end 0)))
1189 (when (save-excursion
1190 (goto-char end)
1191 (and (not (memq (get-text-property (point) 'face)
1192 '(font-lock-function-name-face
1193 font-lock-keyword-face)))
1194 (let ((prefix (company--prefix-str
1195 (company-call-backend 'prefix))))
1196 (and (stringp prefix)
1197 (= (length prefix) (- end beg))))))
1198 (push (cons candidate (if (< beg (point))
1199 (- (point) end)
1200 (- beg (window-start))))
1201 occurs)
1202 t))))
1203 candidates)))
1204 (nconc
1205 (mapcar #'car (sort occurs (lambda (e1 e2) (<= (cdr e1) (cdr e2)))))
1206 noccurs)))
1207
1208 (defun company-idle-begin (buf win tick pos)
1209 (and (eq buf (current-buffer))
1210 (eq win (selected-window))
1211 (eq tick (buffer-chars-modified-tick))
1212 (eq pos (point))
1213 (when (company-auto-begin)
1214 (when (version< emacs-version "24.3.50")
1215 (company-input-noop))
1216 (company-post-command))))
1217
1218 (defun company-auto-begin ()
1219 (and company-mode
1220 (not company-candidates)
1221 (let ((company-idle-delay t)
1222 (company-begin-commands t))
1223 (condition-case-unless-debug err
1224 (company-begin)
1225 (error (message "Company: An error occurred in auto-begin")
1226 (message "%s" (error-message-string err))
1227 (company-cancel))
1228 (quit (company-cancel)))))
1229 (unless company-candidates
1230 (setq company-backend nil))
1231 ;; Return non-nil if active.
1232 company-candidates)
1233
1234 (defun company-manual-begin ()
1235 (interactive)
1236 (company-assert-enabled)
1237 (setq company--manual-action t)
1238 (unwind-protect
1239 (let ((company-minimum-prefix-length 0))
1240 (company-auto-begin))
1241 (unless company-candidates
1242 (setq company--manual-action nil))))
1243
1244 (defun company-other-backend (&optional backward)
1245 (interactive (list current-prefix-arg))
1246 (company-assert-enabled)
1247 (let* ((after (if company-backend
1248 (cdr (member company-backend company-backends))
1249 company-backends))
1250 (before (cdr (member company-backend (reverse company-backends))))
1251 (next (if backward
1252 (append before (reverse after))
1253 (append after (reverse before)))))
1254 (company-cancel)
1255 (cl-dolist (backend next)
1256 (when (ignore-errors (company-begin-backend backend))
1257 (cl-return t))))
1258 (unless company-candidates
1259 (error "No other back-end")))
1260
1261 (defun company-require-match-p ()
1262 (let ((backend-value (company-call-backend 'require-match)))
1263 (or (eq backend-value t)
1264 (and (not (eq backend-value 'never))
1265 (if (functionp company-require-match)
1266 (funcall company-require-match)
1267 (eq company-require-match t))))))
1268
1269 (defun company-auto-complete-p (input)
1270 "Return non-nil, if input starts with punctuation or parentheses."
1271 (and (if (functionp company-auto-complete)
1272 (funcall company-auto-complete)
1273 company-auto-complete)
1274 (if (functionp company-auto-complete-chars)
1275 (funcall company-auto-complete-chars input)
1276 (if (consp company-auto-complete-chars)
1277 (memq (char-syntax (string-to-char input))
1278 company-auto-complete-chars)
1279 (string-match (substring input 0 1) company-auto-complete-chars)))))
1280
1281 (defun company--incremental-p ()
1282 (and (> (point) company-point)
1283 (> (point-max) company--point-max)
1284 (not (eq this-command 'backward-delete-char-untabify))
1285 (equal (buffer-substring (- company-point (length company-prefix))
1286 company-point)
1287 company-prefix)))
1288
1289 (defun company--continue-failed ()
1290 (let ((input (buffer-substring-no-properties (point) company-point)))
1291 (cond
1292 ((company-auto-complete-p input)
1293 ;; auto-complete
1294 (save-excursion
1295 (goto-char company-point)
1296 (let ((company--auto-completion t))
1297 (company-complete-selection))
1298 nil))
1299 ((company-require-match-p)
1300 ;; wrong incremental input, but required match
1301 (delete-char (- (length input)))
1302 (ding)
1303 (message "Matching input is required")
1304 company-candidates)
1305 ((equal company-prefix (car company-candidates))
1306 ;; last input was actually success
1307 (company-cancel company-prefix))
1308 (t (company-cancel)))))
1309
1310 (defun company--good-prefix-p (prefix)
1311 (and (stringp (company--prefix-str prefix)) ;excludes 'stop
1312 (or (eq (cdr-safe prefix) t)
1313 (let ((len (or (cdr-safe prefix) (length prefix))))
1314 (if company--manual-prefix
1315 (or (not company-abort-manual-when-too-short)
1316 ;; Must not be less than minimum or initial length.
1317 (>= len (min company-minimum-prefix-length
1318 (length company--manual-prefix))))
1319 (>= len company-minimum-prefix-length))))))
1320
1321 (defun company--continue ()
1322 (when (company-call-backend 'no-cache company-prefix)
1323 ;; Don't complete existing candidates, fetch new ones.
1324 (setq company-candidates-cache nil))
1325 (let* ((new-prefix (company-call-backend 'prefix))
1326 (c (when (and (company--good-prefix-p new-prefix)
1327 (setq new-prefix (company--prefix-str new-prefix))
1328 (= (- (point) (length new-prefix))
1329 (- company-point (length company-prefix))))
1330 (company-calculate-candidates new-prefix))))
1331 (cond
1332 ((eq c t)
1333 ;; t means complete/unique.
1334 ;; Handle it like completion was aborted, to differentiate from user
1335 ;; calling one of Company's commands to insert the candidate.
1336 (company-cancel 'unique))
1337 ((consp c)
1338 ;; incremental match
1339 (setq company-prefix new-prefix)
1340 (company-update-candidates c)
1341 c)
1342 ((not (company--incremental-p))
1343 (company-cancel))
1344 (t (company--continue-failed)))))
1345
1346 (defun company--begin-new ()
1347 (let (prefix c)
1348 (cl-dolist (backend (if company-backend
1349 ;; prefer manual override
1350 (list company-backend)
1351 company-backends))
1352 (setq prefix
1353 (if (or (symbolp backend)
1354 (functionp backend))
1355 (when (or (not (symbolp backend))
1356 (eq t (get backend 'company-init))
1357 (unless (get backend 'company-init)
1358 (company-init-backend backend)))
1359 (funcall backend 'prefix))
1360 (company--multi-backend-adapter backend 'prefix)))
1361 (when prefix
1362 (when (company--good-prefix-p prefix)
1363 (setq company-prefix (company--prefix-str prefix)
1364 company-backend backend
1365 c (company-calculate-candidates company-prefix))
1366 ;; t means complete/unique. We don't start, so no hooks.
1367 (if (not (consp c))
1368 (when company--manual-action
1369 (message "No completion found"))
1370 (when company--manual-action
1371 (setq company--manual-prefix prefix))
1372 (when (symbolp backend)
1373 (setq company-lighter (concat " " (symbol-name backend))))
1374 (company-update-candidates c)
1375 (run-hook-with-args 'company-completion-started-hook
1376 (company-explicit-action-p))
1377 (company-call-frontends 'show)))
1378 (cl-return c)))))
1379
1380 (defun company-begin ()
1381 (or (and company-candidates (company--continue))
1382 (and (company--should-complete) (company--begin-new)))
1383 (when company-candidates
1384 (let ((modified (buffer-modified-p)))
1385 (when (and company-end-of-buffer-workaround (eobp))
1386 (save-excursion (insert "\n"))
1387 (setq company-added-newline
1388 (or modified (buffer-chars-modified-tick)))))
1389 (setq company-point (point)
1390 company--point-max (point-max))
1391 (company-ensure-emulation-alist)
1392 (company-enable-overriding-keymap company-active-map)
1393 (company-call-frontends 'update)))
1394
1395 (defun company-cancel (&optional result)
1396 (and company-added-newline
1397 (> (point-max) (point-min))
1398 (let ((tick (buffer-chars-modified-tick)))
1399 (delete-region (1- (point-max)) (point-max))
1400 (equal tick company-added-newline))
1401 ;; Only set unmodified when tick remained the same since insert,
1402 ;; and the buffer wasn't modified before.
1403 (set-buffer-modified-p nil))
1404 (when company-prefix
1405 (if (stringp result)
1406 (progn
1407 (company-call-backend 'pre-completion result)
1408 (run-hook-with-args 'company-completion-finished-hook result)
1409 (company-call-backend 'post-completion result))
1410 (run-hook-with-args 'company-completion-cancelled-hook result)))
1411 (setq company-added-newline nil
1412 company-backend nil
1413 company-prefix nil
1414 company-candidates nil
1415 company-candidates-length nil
1416 company-candidates-cache nil
1417 company-candidates-predicate nil
1418 company-common nil
1419 company-selection 0
1420 company-selection-changed nil
1421 company--manual-action nil
1422 company--manual-prefix nil
1423 company-lighter company-default-lighter
1424 company--point-max nil
1425 company-point nil)
1426 (when company-timer
1427 (cancel-timer company-timer))
1428 (company-search-mode 0)
1429 (company-call-frontends 'hide)
1430 (company-enable-overriding-keymap nil)
1431 ;; Make return value explicit.
1432 nil)
1433
1434 (defun company-abort ()
1435 (interactive)
1436 (company-cancel t)
1437 ;; Don't start again, unless started manually.
1438 (setq company-point (point)))
1439
1440 (defun company-finish (result)
1441 (company--insert-candidate result)
1442 (company-cancel result)
1443 ;; Don't start again, unless started manually.
1444 (setq company-point (point)))
1445
1446 (defsubst company-keep (command)
1447 (and (symbolp command) (get command 'company-keep)))
1448
1449 (defun company-pre-command ()
1450 (unless (company-keep this-command)
1451 (condition-case err
1452 (when company-candidates
1453 (company-call-frontends 'pre-command)
1454 (unless (company--should-continue)
1455 (company-abort)))
1456 (error (message "Company: An error occurred in pre-command")
1457 (message "%s" (error-message-string err))
1458 (company-cancel))))
1459 (when company-timer
1460 (cancel-timer company-timer)
1461 (setq company-timer nil))
1462 (company-uninstall-map))
1463
1464 (defun company-post-command ()
1465 (unless (company-keep this-command)
1466 (condition-case err
1467 (progn
1468 (unless (equal (point) company-point)
1469 (company-begin))
1470 (if company-candidates
1471 (company-call-frontends 'post-command)
1472 (and (numberp company-idle-delay)
1473 (or (eq t company-begin-commands)
1474 (memq this-command company-begin-commands))
1475 (not (equal (point) company-point))
1476 (setq company-timer
1477 (run-with-timer company-idle-delay nil
1478 'company-idle-begin
1479 (current-buffer) (selected-window)
1480 (buffer-chars-modified-tick) (point))))))
1481 (error (message "Company: An error occurred in post-command")
1482 (message "%s" (error-message-string err))
1483 (company-cancel))))
1484 (company-install-map))
1485
1486 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1487
1488 (defvar company-search-string nil)
1489 (make-variable-buffer-local 'company-search-string)
1490
1491 (defvar company-search-lighter " Search: \"\"")
1492 (make-variable-buffer-local 'company-search-lighter)
1493
1494 (defvar company-search-old-map nil)
1495 (make-variable-buffer-local 'company-search-old-map)
1496
1497 (defvar company-search-old-selection 0)
1498 (make-variable-buffer-local 'company-search-old-selection)
1499
1500 (defun company-search (text lines)
1501 (let ((quoted (regexp-quote text))
1502 (i 0))
1503 (cl-dolist (line lines)
1504 (when (string-match quoted line (length company-prefix))
1505 (cl-return i))
1506 (cl-incf i))))
1507
1508 (defun company-search-printing-char ()
1509 (interactive)
1510 (company-search-assert-enabled)
1511 (setq company-search-string
1512 (concat (or company-search-string "") (string last-command-event))
1513 company-search-lighter (concat " Search: \"" company-search-string
1514 "\""))
1515 (let ((pos (company-search company-search-string
1516 (nthcdr company-selection company-candidates))))
1517 (if (null pos)
1518 (ding)
1519 (company-set-selection (+ company-selection pos) t))))
1520
1521 (defun company-search-repeat-forward ()
1522 "Repeat the incremental search in completion candidates forward."
1523 (interactive)
1524 (company-search-assert-enabled)
1525 (let ((pos (company-search company-search-string
1526 (cdr (nthcdr company-selection
1527 company-candidates)))))
1528 (if (null pos)
1529 (ding)
1530 (company-set-selection (+ company-selection pos 1) t))))
1531
1532 (defun company-search-repeat-backward ()
1533 "Repeat the incremental search in completion candidates backwards."
1534 (interactive)
1535 (company-search-assert-enabled)
1536 (let ((pos (company-search company-search-string
1537 (nthcdr (- company-candidates-length
1538 company-selection)
1539 (reverse company-candidates)))))
1540 (if (null pos)
1541 (ding)
1542 (company-set-selection (- company-selection pos 1) t))))
1543
1544 (defun company-create-match-predicate ()
1545 (setq company-candidates-predicate
1546 `(lambda (candidate)
1547 ,(if company-candidates-predicate
1548 `(and (string-match ,company-search-string candidate)
1549 (funcall ,company-candidates-predicate
1550 candidate))
1551 `(string-match ,company-search-string candidate))))
1552 (company-update-candidates
1553 (company-apply-predicate company-candidates company-candidates-predicate))
1554 ;; Invalidate cache.
1555 (setq company-candidates-cache (cons company-prefix company-candidates)))
1556
1557 (defun company-filter-printing-char ()
1558 (interactive)
1559 (company-search-assert-enabled)
1560 (company-search-printing-char)
1561 (company-create-match-predicate)
1562 (company-call-frontends 'update))
1563
1564 (defun company-search-kill-others ()
1565 "Limit the completion candidates to the ones matching the search string."
1566 (interactive)
1567 (company-search-assert-enabled)
1568 (company-create-match-predicate)
1569 (company-search-mode 0)
1570 (company-call-frontends 'update))
1571
1572 (defun company-search-abort ()
1573 "Abort searching the completion candidates."
1574 (interactive)
1575 (company-search-assert-enabled)
1576 (company-set-selection company-search-old-selection t)
1577 (company-search-mode 0))
1578
1579 (defun company-search-other-char ()
1580 (interactive)
1581 (company-search-assert-enabled)
1582 (company-search-mode 0)
1583 (company--unread-last-input))
1584
1585 (defvar company-search-map
1586 (let ((i 0)
1587 (keymap (make-keymap)))
1588 (if (fboundp 'max-char)
1589 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1590 'company-search-printing-char)
1591 (with-no-warnings
1592 ;; obsolete in Emacs 23
1593 (let ((l (generic-character-list))
1594 (table (nth 1 keymap)))
1595 (while l
1596 (set-char-table-default table (car l) 'company-search-printing-char)
1597 (setq l (cdr l))))))
1598 (define-key keymap [t] 'company-search-other-char)
1599 (while (< i ?\s)
1600 (define-key keymap (make-string 1 i) 'company-search-other-char)
1601 (cl-incf i))
1602 (while (< i 256)
1603 (define-key keymap (vector i) 'company-search-printing-char)
1604 (cl-incf i))
1605 (let ((meta-map (make-sparse-keymap)))
1606 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1607 (define-key keymap [escape] meta-map))
1608 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1609 (define-key keymap "\e\e\e" 'company-search-other-char)
1610 (define-key keymap [escape escape escape] 'company-search-other-char)
1611 (define-key keymap (kbd "DEL") 'company-search-other-char)
1612
1613 (define-key keymap "\C-g" 'company-search-abort)
1614 (define-key keymap "\C-s" 'company-search-repeat-forward)
1615 (define-key keymap "\C-r" 'company-search-repeat-backward)
1616 (define-key keymap "\C-o" 'company-search-kill-others)
1617 keymap)
1618 "Keymap used for incrementally searching the completion candidates.")
1619
1620 (define-minor-mode company-search-mode
1621 "Search mode for completion candidates.
1622 Don't start this directly, use `company-search-candidates' or
1623 `company-filter-candidates'."
1624 nil company-search-lighter nil
1625 (if company-search-mode
1626 (if (company-manual-begin)
1627 (progn
1628 (setq company-search-old-selection company-selection)
1629 (company-call-frontends 'update))
1630 (setq company-search-mode nil))
1631 (kill-local-variable 'company-search-string)
1632 (kill-local-variable 'company-search-lighter)
1633 (kill-local-variable 'company-search-old-selection)
1634 (company-enable-overriding-keymap company-active-map)))
1635
1636 (defun company-search-assert-enabled ()
1637 (company-assert-enabled)
1638 (unless company-search-mode
1639 (company-uninstall-map)
1640 (error "Company not in search mode")))
1641
1642 (defun company-search-candidates ()
1643 "Start searching the completion candidates incrementally.
1644
1645 \\<company-search-map>Search can be controlled with the commands:
1646 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1647 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1648 - `company-search-abort' (\\[company-search-abort])
1649
1650 Regular characters are appended to the search string.
1651
1652 The command `company-search-kill-others' (\\[company-search-kill-others])
1653 uses the search string to limit the completion candidates."
1654 (interactive)
1655 (company-search-mode 1)
1656 (company-enable-overriding-keymap company-search-map))
1657
1658 (defvar company-filter-map
1659 (let ((keymap (make-keymap)))
1660 (define-key keymap [remap company-search-printing-char]
1661 'company-filter-printing-char)
1662 (set-keymap-parent keymap company-search-map)
1663 keymap)
1664 "Keymap used for incrementally searching the completion candidates.")
1665
1666 (defun company-filter-candidates ()
1667 "Start filtering the completion candidates incrementally.
1668 This works the same way as `company-search-candidates' immediately
1669 followed by `company-search-kill-others' after each input."
1670 (interactive)
1671 (company-search-mode 1)
1672 (company-enable-overriding-keymap company-filter-map))
1673
1674 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1675
1676 (defun company-select-next ()
1677 "Select the next candidate in the list."
1678 (interactive)
1679 (when (company-manual-begin)
1680 (company-set-selection (1+ company-selection))))
1681
1682 (defun company-select-previous ()
1683 "Select the previous candidate in the list."
1684 (interactive)
1685 (when (company-manual-begin)
1686 (company-set-selection (1- company-selection))))
1687
1688 (defun company-select-next-or-abort ()
1689 "Select the next candidate if more than one, else abort
1690 and invoke the normal binding."
1691 (interactive)
1692 (if (> company-candidates-length 1)
1693 (company-select-next)
1694 (company-abort)
1695 (company--unread-last-input)))
1696
1697 (defun company-select-previous-or-abort ()
1698 "Select the previous candidate if more than one, else abort
1699 and invoke the normal binding."
1700 (interactive)
1701 (if (> company-candidates-length 1)
1702 (company-select-previous)
1703 (company-abort)
1704 (company--unread-last-input)))
1705
1706 (defvar company-pseudo-tooltip-overlay)
1707
1708 (defvar company-tooltip-offset)
1709
1710 (defun company--inside-tooltip-p (event-col-row row height)
1711 (let* ((ovl company-pseudo-tooltip-overlay)
1712 (column (overlay-get ovl 'company-column))
1713 (width (overlay-get ovl 'company-width))
1714 (evt-col (car event-col-row))
1715 (evt-row (cdr event-col-row)))
1716 (and (>= evt-col column)
1717 (< evt-col (+ column width))
1718 (if (> height 0)
1719 (and (> evt-row row)
1720 (<= evt-row (+ row height) ))
1721 (and (< evt-row row)
1722 (>= evt-row (+ row height)))))))
1723
1724 (defun company--event-col-row (event)
1725 (let* ((col-row (posn-actual-col-row (event-start event)))
1726 (col (car col-row))
1727 (row (cdr col-row)))
1728 (cl-incf col (window-hscroll))
1729 (and header-line-format
1730 (version< "24" emacs-version)
1731 (cl-decf row))
1732 (cons col row)))
1733
1734 (defun company-select-mouse (event)
1735 "Select the candidate picked by the mouse."
1736 (interactive "e")
1737 (let ((event-col-row (company--event-col-row event))
1738 (ovl-row (company--row))
1739 (ovl-height (and company-pseudo-tooltip-overlay
1740 (min (overlay-get company-pseudo-tooltip-overlay
1741 'company-height)
1742 company-candidates-length))))
1743 (if (and ovl-height
1744 (company--inside-tooltip-p event-col-row ovl-row ovl-height))
1745 (progn
1746 (company-set-selection (+ (cdr event-col-row)
1747 (1- company-tooltip-offset)
1748 (if (and (eq company-tooltip-offset-display 'lines)
1749 (not (zerop company-tooltip-offset)))
1750 -1 0)
1751 (- ovl-row)
1752 (if (< ovl-height 0)
1753 (- 1 ovl-height)
1754 0)))
1755 t)
1756 (company-abort)
1757 (company--unread-last-input)
1758 nil)))
1759
1760 (defun company-complete-mouse (event)
1761 "Insert the candidate picked by the mouse."
1762 (interactive "e")
1763 (when (company-select-mouse event)
1764 (company-complete-selection)))
1765
1766 (defun company-complete-selection ()
1767 "Insert the selected candidate."
1768 (interactive)
1769 (when (company-manual-begin)
1770 (let ((result (nth company-selection company-candidates)))
1771 (company-finish result))))
1772
1773 (defun company-complete-common ()
1774 "Insert the common part of all candidates."
1775 (interactive)
1776 (when (company-manual-begin)
1777 (if (and (not (cdr company-candidates))
1778 (equal company-common (car company-candidates)))
1779 (company-complete-selection)
1780 (when company-common
1781 (company--insert-candidate company-common)))))
1782
1783 (defun company-complete ()
1784 "Insert the common part of all candidates or the current selection.
1785 The first time this is called, the common part is inserted, the second
1786 time, or when the selection has been changed, the selected candidate is
1787 inserted."
1788 (interactive)
1789 (when (company-manual-begin)
1790 (if (or company-selection-changed
1791 (eq last-command 'company-complete-common))
1792 (call-interactively 'company-complete-selection)
1793 (call-interactively 'company-complete-common)
1794 (setq this-command 'company-complete-common))))
1795
1796 (defun company-complete-number (n)
1797 "Insert the Nth candidate.
1798 To show the number next to the candidates in some back-ends, enable
1799 `company-show-numbers'."
1800 (when (company-manual-begin)
1801 (and (or (< n 1) (> n company-candidates-length))
1802 (error "No candidate number %d" n))
1803 (cl-decf n)
1804 (company-finish (nth n company-candidates))))
1805
1806 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1807
1808 (defconst company-space-strings-limit 100)
1809
1810 (defconst company-space-strings
1811 (let (lst)
1812 (dotimes (i company-space-strings-limit)
1813 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1814 (apply 'vector lst)))
1815
1816 (defun company-space-string (len)
1817 (if (< len company-space-strings-limit)
1818 (aref company-space-strings len)
1819 (make-string len ?\ )))
1820
1821 (defun company-safe-substring (str from &optional to)
1822 (if (> from (string-width str))
1823 ""
1824 (with-temp-buffer
1825 (insert str)
1826 (move-to-column from)
1827 (let ((beg (point)))
1828 (if to
1829 (progn
1830 (move-to-column to)
1831 (concat (buffer-substring beg (point))
1832 (let ((padding (- to (current-column))))
1833 (when (> padding 0)
1834 (company-space-string padding)))))
1835 (buffer-substring beg (point-max)))))))
1836
1837 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1838
1839 (defvar company-last-metadata nil)
1840 (make-variable-buffer-local 'company-last-metadata)
1841
1842 (defun company-fetch-metadata ()
1843 (let ((selected (nth company-selection company-candidates)))
1844 (unless (eq selected (car company-last-metadata))
1845 (setq company-last-metadata
1846 (cons selected (company-call-backend 'meta selected))))
1847 (cdr company-last-metadata)))
1848
1849 (defun company-doc-buffer (&optional string)
1850 (with-current-buffer (get-buffer-create "*company-documentation*")
1851 (erase-buffer)
1852 (when string
1853 (save-excursion
1854 (insert string)))
1855 (current-buffer)))
1856
1857 (defvar company--electric-commands
1858 '(scroll-other-window scroll-other-window-down)
1859 "List of Commands that won't break out of electric commands.")
1860
1861 (defmacro company--electric-do (&rest body)
1862 (declare (indent 0) (debug t))
1863 `(when (company-manual-begin)
1864 (save-window-excursion
1865 (let ((height (window-height))
1866 (row (company--row))
1867 cmd)
1868 ,@body
1869 (and (< (window-height) height)
1870 (< (- (window-height) row 2) company-tooltip-limit)
1871 (recenter (- (window-height) row 2)))
1872 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1873 company--electric-commands)
1874 (call-interactively cmd))
1875 (company--unread-last-input)))))
1876
1877 (defun company--unread-last-input ()
1878 (when last-input-event
1879 (clear-this-command-keys t)
1880 (setq unread-command-events (list last-input-event))))
1881
1882 (defun company-show-doc-buffer ()
1883 "Temporarily show the documentation buffer for the selection."
1884 (interactive)
1885 (company--electric-do
1886 (let* ((selected (nth company-selection company-candidates))
1887 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1888 (error "No documentation available"))))
1889 (with-current-buffer doc-buffer
1890 (goto-char (point-min)))
1891 (display-buffer doc-buffer t))))
1892 (put 'company-show-doc-buffer 'company-keep t)
1893
1894 (defun company-show-location ()
1895 "Temporarily display a buffer showing the selected candidate in context."
1896 (interactive)
1897 (company--electric-do
1898 (let* ((selected (nth company-selection company-candidates))
1899 (location (company-call-backend 'location selected))
1900 (pos (or (cdr location) (error "No location available")))
1901 (buffer (or (and (bufferp (car location)) (car location))
1902 (find-file-noselect (car location) t))))
1903 (with-selected-window (display-buffer buffer t)
1904 (save-restriction
1905 (widen)
1906 (if (bufferp (car location))
1907 (goto-char pos)
1908 (goto-char (point-min))
1909 (forward-line (1- pos))))
1910 (set-window-start nil (point))))))
1911 (put 'company-show-location 'company-keep t)
1912
1913 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1914
1915 (defvar company-callback nil)
1916 (make-variable-buffer-local 'company-callback)
1917
1918 (defun company-remove-callback (&optional ignored)
1919 (remove-hook 'company-completion-finished-hook company-callback t)
1920 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1921 (remove-hook 'company-completion-finished-hook 'company-remove-callback t))
1922
1923 (defun company-begin-backend (backend &optional callback)
1924 "Start a completion at point using BACKEND."
1925 (interactive (let ((val (completing-read "Company back-end: "
1926 obarray
1927 'functionp nil "company-")))
1928 (when val
1929 (list (intern val)))))
1930 (when (setq company-callback callback)
1931 (add-hook 'company-completion-finished-hook company-callback nil t))
1932 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1933 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1934 (setq company-backend backend)
1935 ;; Return non-nil if active.
1936 (or (company-manual-begin)
1937 (error "Cannot complete at point")))
1938
1939 (defun company-begin-with (candidates
1940 &optional prefix-length require-match callback)
1941 "Start a completion at point.
1942 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length
1943 of the prefix that already is in the buffer before point.
1944 It defaults to 0.
1945
1946 CALLBACK is a function called with the selected result if the user
1947 successfully completes the input.
1948
1949 Example: \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1950 (let ((begin-marker (copy-marker (point) t)))
1951 (company-begin-backend
1952 (lambda (command &optional arg &rest ignored)
1953 (pcase command
1954 (`prefix
1955 (when (equal (point) (marker-position begin-marker))
1956 (buffer-substring (- (point) (or prefix-length 0)) (point))))
1957 (`candidates
1958 (all-completions arg candidates))
1959 (`require-match
1960 require-match)))
1961 callback)))
1962
1963 (defun company-version (&optional show-version)
1964 "Get the Company version as string.
1965
1966 If SHOW-VERSION is non-nil, show the version in the echo area."
1967 (interactive (list t))
1968 (with-temp-buffer
1969 (insert-file-contents (find-library-name "company"))
1970 (require 'lisp-mnt)
1971 (if show-version
1972 (message "Company version: %s" (lm-version))
1973 (lm-version))))
1974
1975 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1976
1977 (defvar company-pseudo-tooltip-overlay nil)
1978 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1979
1980 (defvar company-tooltip-offset 0)
1981 (make-variable-buffer-local 'company-tooltip-offset)
1982
1983 (defun company-tooltip--lines-update-offset (selection num-lines limit)
1984 (cl-decf limit 2)
1985 (setq company-tooltip-offset
1986 (max (min selection company-tooltip-offset)
1987 (- selection -1 limit)))
1988
1989 (when (<= company-tooltip-offset 1)
1990 (cl-incf limit)
1991 (setq company-tooltip-offset 0))
1992
1993 (when (>= company-tooltip-offset (- num-lines limit 1))
1994 (cl-incf limit)
1995 (when (= selection (1- num-lines))
1996 (cl-decf company-tooltip-offset)
1997 (when (<= company-tooltip-offset 1)
1998 (setq company-tooltip-offset 0)
1999 (cl-incf limit))))
2000
2001 limit)
2002
2003 (defun company-tooltip--simple-update-offset (selection _num-lines limit)
2004 (setq company-tooltip-offset
2005 (if (< selection company-tooltip-offset)
2006 selection
2007 (max company-tooltip-offset
2008 (- selection limit -1)))))
2009
2010 ;;; propertize
2011
2012 (defsubst company-round-tab (arg)
2013 (* (/ (+ arg tab-width) tab-width) tab-width))
2014
2015 (defun company-plainify (str)
2016 (let ((prefix (get-text-property 0 'line-prefix str)))
2017 (when prefix ; Keep the original value unmodified, for no special reason.
2018 (setq str (concat prefix str))
2019 (remove-text-properties 0 (length str) '(line-prefix) str)))
2020 (let* ((pieces (split-string str "\t"))
2021 (copy pieces))
2022 (while (cdr copy)
2023 (setcar copy (company-safe-substring
2024 (car copy) 0 (company-round-tab (string-width (car copy)))))
2025 (pop copy))
2026 (apply 'concat pieces)))
2027
2028 (defun company-fill-propertize (value annotation width selected left right)
2029 (let* ((margin (length left))
2030 (common (+ (or (company-call-backend 'match value)
2031 (length company-common)) margin))
2032 (ann-ralign company-tooltip-align-annotations)
2033 (ann-truncate (< width
2034 (+ (length value) (length annotation)
2035 (if ann-ralign 1 0))))
2036 (ann-start (+ margin
2037 (if ann-ralign
2038 (if ann-truncate
2039 (1+ (length value))
2040 (- width (length annotation)))
2041 (length value))))
2042 (ann-end (min (+ ann-start (length annotation)) (+ margin width)))
2043 (line (concat left
2044 (if (or ann-truncate (not ann-ralign))
2045 (company-safe-substring
2046 (concat value
2047 (when (and annotation ann-ralign) " ")
2048 annotation)
2049 0 width)
2050 (concat
2051 (company-safe-substring value 0
2052 (- width (length annotation)))
2053 annotation))
2054 right)))
2055 (setq width (+ width margin (length right)))
2056
2057 (add-text-properties 0 width '(face company-tooltip
2058 mouse-face company-tooltip-mouse)
2059 line)
2060 (add-text-properties margin common
2061 '(face company-tooltip-common
2062 mouse-face company-tooltip-mouse)
2063 line)
2064 (when (< ann-start ann-end)
2065 (add-text-properties ann-start ann-end
2066 '(face company-tooltip-annotation
2067 mouse-face company-tooltip-mouse)
2068 line))
2069 (when selected
2070 (if (and company-search-string
2071 (string-match (regexp-quote company-search-string) value
2072 (length company-prefix)))
2073 (let ((beg (+ margin (match-beginning 0)))
2074 (end (+ margin (match-end 0))))
2075 (add-text-properties beg end '(face company-tooltip-selection)
2076 line)
2077 (when (< beg common)
2078 (add-text-properties beg common
2079 '(face company-tooltip-common-selection)
2080 line)))
2081 (add-text-properties 0 width '(face company-tooltip-selection
2082 mouse-face company-tooltip-selection)
2083 line)
2084 (add-text-properties margin common
2085 '(face company-tooltip-common-selection
2086 mouse-face company-tooltip-selection)
2087 line)))
2088 line))
2089
2090 ;;; replace
2091
2092 (defun company-buffer-lines (beg end)
2093 (goto-char beg)
2094 (let (lines)
2095 (while (and (= 1 (vertical-motion 1))
2096 (<= (point) end))
2097 (let ((bound (min end (1- (point)))))
2098 ;; A visual line can contain several physical lines (e.g. with outline's
2099 ;; folding overlay). Take only the first one.
2100 (push (buffer-substring beg
2101 (save-excursion
2102 (goto-char beg)
2103 (re-search-forward "$" bound 'move)
2104 (point)))
2105 lines))
2106 (setq beg (point)))
2107 (unless (eq beg end)
2108 (push (buffer-substring beg end) lines))
2109 (nreverse lines)))
2110
2111 (defun company-modify-line (old new offset)
2112 (concat (company-safe-substring old 0 offset)
2113 new
2114 (company-safe-substring old (+ offset (length new)))))
2115
2116 (defsubst company--length-limit (lst limit)
2117 (if (nthcdr limit lst)
2118 limit
2119 (length lst)))
2120
2121 (defun company--replacement-string (lines old column nl &optional align-top)
2122 (cl-decf column company-tooltip-margin)
2123
2124 (let ((width (length (car lines)))
2125 (remaining-cols (- (+ (company--window-width) (window-hscroll))
2126 column)))
2127 (when (> width remaining-cols)
2128 (cl-decf column (- width remaining-cols))))
2129
2130 (let ((offset (and (< column 0) (- column)))
2131 new)
2132 (when offset
2133 (setq column 0))
2134 (when align-top
2135 ;; untouched lines first
2136 (dotimes (_ (- (length old) (length lines)))
2137 (push (pop old) new)))
2138 ;; length into old lines.
2139 (while old
2140 (push (company-modify-line (pop old)
2141 (company--offset-line (pop lines) offset)
2142 column) new))
2143 ;; Append whole new lines.
2144 (while lines
2145 (push (concat (company-space-string column)
2146 (company--offset-line (pop lines) offset))
2147 new))
2148
2149 (let ((str (concat (when nl "\n")
2150 (mapconcat 'identity (nreverse new) "\n")
2151 "\n")))
2152 (font-lock-append-text-property 0 (length str) 'face 'default str)
2153 str)))
2154
2155 (defun company--offset-line (line offset)
2156 (if (and offset line)
2157 (substring line offset)
2158 line))
2159
2160 (defun company--create-lines (selection limit)
2161 (let ((len company-candidates-length)
2162 (numbered 99999)
2163 (window-width (company--window-width))
2164 lines
2165 width
2166 lines-copy
2167 items
2168 previous
2169 remainder
2170 scrollbar-bounds)
2171
2172 ;; Maybe clear old offset.
2173 (when (< len (+ company-tooltip-offset limit))
2174 (setq company-tooltip-offset 0))
2175
2176 ;; Scroll to offset.
2177 (if (eq company-tooltip-offset-display 'lines)
2178 (setq limit (company-tooltip--lines-update-offset selection len limit))
2179 (company-tooltip--simple-update-offset selection len limit))
2180
2181 (cond
2182 ((eq company-tooltip-offset-display 'scrollbar)
2183 (setq scrollbar-bounds (company--scrollbar-bounds company-tooltip-offset
2184 limit len)))
2185 ((eq company-tooltip-offset-display 'lines)
2186 (when (> company-tooltip-offset 0)
2187 (setq previous (format "...(%d)" company-tooltip-offset)))
2188 (setq remainder (- len limit company-tooltip-offset)
2189 remainder (when (> remainder 0)
2190 (setq remainder (format "...(%d)" remainder))))))
2191
2192 (cl-decf selection company-tooltip-offset)
2193 (setq width (max (length previous) (length remainder))
2194 lines (nthcdr company-tooltip-offset company-candidates)
2195 len (min limit len)
2196 lines-copy lines)
2197
2198 (cl-decf window-width (* 2 company-tooltip-margin))
2199 (when scrollbar-bounds (cl-decf window-width))
2200
2201 (dotimes (_ len)
2202 (let* ((value (pop lines-copy))
2203 (annotation (company-call-backend 'annotation value)))
2204 (when (and annotation company-tooltip-align-annotations)
2205 ;; `lisp-completion-at-point' adds a space.
2206 (setq annotation (comment-string-strip annotation t nil)))
2207 (push (cons value annotation) items)
2208 (setq width (max (+ (length value)
2209 (if (and annotation company-tooltip-align-annotations)
2210 (1+ (length annotation))
2211 (length annotation)))
2212 width))))
2213
2214 (setq width (min window-width
2215 (max company-tooltip-minimum-width
2216 (if (and company-show-numbers
2217 (< company-tooltip-offset 10))
2218 (+ 2 width)
2219 width))))
2220
2221 ;; number can make tooltip too long
2222 (when company-show-numbers
2223 (setq numbered company-tooltip-offset))
2224
2225 (let ((items (nreverse items)) new)
2226 (when previous
2227 (push (company--scrollpos-line previous width) new))
2228
2229 (dotimes (i len)
2230 (let* ((item (pop items))
2231 (str (company-reformat (car item)))
2232 (annotation (cdr item))
2233 (right (company-space-string company-tooltip-margin))
2234 (width width))
2235 (when (< numbered 10)
2236 (cl-decf width 2)
2237 (cl-incf numbered)
2238 (setq right (concat (format " %d" (mod numbered 10)) right)))
2239 (push (concat
2240 (company-fill-propertize str annotation
2241 width (equal i selection)
2242 (company-space-string
2243 company-tooltip-margin)
2244 right)
2245 (when scrollbar-bounds
2246 (company--scrollbar i scrollbar-bounds)))
2247 new)))
2248
2249 (when remainder
2250 (push (company--scrollpos-line remainder width) new))
2251
2252 (nreverse new))))
2253
2254 (defun company--scrollbar-bounds (offset limit length)
2255 (when (> length limit)
2256 (let* ((size (ceiling (* limit (float limit)) length))
2257 (lower (floor (* limit (float offset)) length))
2258 (upper (+ lower size -1)))
2259 (cons lower upper))))
2260
2261 (defun company--scrollbar (i bounds)
2262 (propertize " " 'face
2263 (if (and (>= i (car bounds)) (<= i (cdr bounds)))
2264 'company-scrollbar-fg
2265 'company-scrollbar-bg)))
2266
2267 (defun company--scrollpos-line (text width)
2268 (propertize (concat (company-space-string company-tooltip-margin)
2269 (company-safe-substring text 0 width)
2270 (company-space-string company-tooltip-margin))
2271 'face 'company-tooltip))
2272
2273 ;; show
2274
2275 (defsubst company--window-inner-height ()
2276 (let ((edges (window-inside-edges)))
2277 (- (nth 3 edges) (nth 1 edges))))
2278
2279 (defsubst company--window-width ()
2280 (let ((ww (window-width)))
2281 ;; Account for the line continuation column.
2282 (when (zerop (cadr (window-fringes)))
2283 (cl-decf ww))
2284 (unless (or (display-graphic-p)
2285 (version< "24.3.1" emacs-version))
2286 ;; Emacs 24.3 and earlier included margins
2287 ;; in window-width when in TTY.
2288 (cl-decf ww
2289 (let ((margins (window-margins)))
2290 (+ (or (car margins) 0)
2291 (or (cdr margins) 0)))))
2292 ww))
2293
2294 (defun company--pseudo-tooltip-height ()
2295 "Calculate the appropriate tooltip height.
2296 Returns a negative number if the tooltip should be displayed above point."
2297 (let* ((lines (company--row))
2298 (below (- (company--window-inner-height) 1 lines)))
2299 (if (and (< below (min company-tooltip-minimum company-candidates-length))
2300 (> lines below))
2301 (- (max 3 (min company-tooltip-limit lines)))
2302 (max 3 (min company-tooltip-limit below)))))
2303
2304 (defun company-pseudo-tooltip-show (row column selection)
2305 (company-pseudo-tooltip-hide)
2306 (save-excursion
2307
2308 (let* ((height (company--pseudo-tooltip-height))
2309 above)
2310
2311 (when (< height 0)
2312 (setq row (+ row height -1)
2313 above t))
2314
2315 (let* ((nl (< (move-to-window-line row) row))
2316 (beg (point))
2317 (end (save-excursion
2318 (move-to-window-line (+ row (abs height)))
2319 (point)))
2320 (ov (make-overlay beg end))
2321 (args (list (mapcar 'company-plainify
2322 (company-buffer-lines beg end))
2323 column nl above)))
2324
2325 (setq company-pseudo-tooltip-overlay ov)
2326 (overlay-put ov 'company-replacement-args args)
2327
2328 (let ((lines (company--create-lines selection (abs height))))
2329 (overlay-put ov 'company-after
2330 (apply 'company--replacement-string lines args))
2331 (overlay-put ov 'company-width (string-width (car lines))))
2332
2333 (overlay-put ov 'company-column column)
2334 (overlay-put ov 'company-height height)))))
2335
2336 (defun company-pseudo-tooltip-show-at-point (pos)
2337 (let ((row (company--row pos))
2338 (col (company--column pos)))
2339 (company-pseudo-tooltip-show (1+ row) col company-selection)))
2340
2341 (defun company-pseudo-tooltip-edit (selection)
2342 (let ((height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
2343 (overlay-put company-pseudo-tooltip-overlay 'company-after
2344 (apply 'company--replacement-string
2345 (company--create-lines selection (abs height))
2346 (overlay-get company-pseudo-tooltip-overlay
2347 'company-replacement-args)))))
2348
2349 (defun company-pseudo-tooltip-hide ()
2350 (when company-pseudo-tooltip-overlay
2351 (delete-overlay company-pseudo-tooltip-overlay)
2352 (setq company-pseudo-tooltip-overlay nil)))
2353
2354 (defun company-pseudo-tooltip-hide-temporarily ()
2355 (when (overlayp company-pseudo-tooltip-overlay)
2356 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
2357 (overlay-put company-pseudo-tooltip-overlay 'line-prefix nil)
2358 (overlay-put company-pseudo-tooltip-overlay 'after-string nil)))
2359
2360 (defun company-pseudo-tooltip-unhide ()
2361 (when company-pseudo-tooltip-overlay
2362 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
2363 ;; Beat outline's folding overlays, at least.
2364 (overlay-put company-pseudo-tooltip-overlay 'priority 1)
2365 ;; No (extra) prefix for the first line.
2366 (overlay-put company-pseudo-tooltip-overlay 'line-prefix "")
2367 (overlay-put company-pseudo-tooltip-overlay 'after-string
2368 (overlay-get company-pseudo-tooltip-overlay 'company-after))
2369 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
2370
2371 (defun company-pseudo-tooltip-guard ()
2372 (buffer-substring-no-properties
2373 (point) (overlay-start company-pseudo-tooltip-overlay)))
2374
2375 (defun company-pseudo-tooltip-frontend (command)
2376 "`company-mode' front-end similar to a tooltip but based on overlays."
2377 (cl-case command
2378 (pre-command (company-pseudo-tooltip-hide-temporarily))
2379 (post-command
2380 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
2381 (overlay-get company-pseudo-tooltip-overlay
2382 'company-height)
2383 0))
2384 (new-height (company--pseudo-tooltip-height)))
2385 (unless (and (>= (* old-height new-height) 0)
2386 (>= (abs old-height) (abs new-height))
2387 (equal (company-pseudo-tooltip-guard)
2388 (overlay-get company-pseudo-tooltip-overlay
2389 'company-guard)))
2390 ;; Redraw needed.
2391 (company-pseudo-tooltip-show-at-point (- (point)
2392 (length company-prefix)))
2393 (overlay-put company-pseudo-tooltip-overlay
2394 'company-guard (company-pseudo-tooltip-guard))))
2395 (company-pseudo-tooltip-unhide))
2396 (hide (company-pseudo-tooltip-hide)
2397 (setq company-tooltip-offset 0))
2398 (update (when (overlayp company-pseudo-tooltip-overlay)
2399 (company-pseudo-tooltip-edit company-selection)))))
2400
2401 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
2402 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
2403 (unless (and (eq command 'post-command)
2404 (company--show-inline-p))
2405 (company-pseudo-tooltip-frontend command)))
2406
2407 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2408
2409 (defvar company-preview-overlay nil)
2410 (make-variable-buffer-local 'company-preview-overlay)
2411
2412 (defun company-preview-show-at-point (pos)
2413 (company-preview-hide)
2414
2415 (setq company-preview-overlay (make-overlay pos (1+ pos)))
2416
2417 (let ((completion (nth company-selection company-candidates)))
2418 (setq completion (propertize completion 'face 'company-preview))
2419 (add-text-properties 0 (length company-common)
2420 '(face company-preview-common) completion)
2421
2422 ;; Add search string
2423 (and company-search-string
2424 (string-match (regexp-quote company-search-string) completion)
2425 (add-text-properties (match-beginning 0)
2426 (match-end 0)
2427 '(face company-preview-search)
2428 completion))
2429
2430 (setq completion (company-strip-prefix completion))
2431
2432 (and (equal pos (point))
2433 (not (equal completion ""))
2434 (add-text-properties 0 1 '(cursor t) completion))
2435
2436 (overlay-put company-preview-overlay 'display
2437 (concat completion (unless (eq pos (point-max))
2438 (buffer-substring pos (1+ pos)))))
2439 (overlay-put company-preview-overlay 'window (selected-window))))
2440
2441 (defun company-preview-hide ()
2442 (when company-preview-overlay
2443 (delete-overlay company-preview-overlay)
2444 (setq company-preview-overlay nil)))
2445
2446 (defun company-preview-frontend (command)
2447 "`company-mode' front-end showing the selection as if it had been inserted."
2448 (pcase command
2449 (`pre-command (company-preview-hide))
2450 (`post-command (company-preview-show-at-point (point)))
2451 (`hide (company-preview-hide))))
2452
2453 (defun company-preview-if-just-one-frontend (command)
2454 "`company-preview-frontend', but only shown for single candidates."
2455 (when (or (not (eq command 'post-command))
2456 (company--show-inline-p))
2457 (company-preview-frontend command)))
2458
2459 (defun company--show-inline-p ()
2460 (and (not (cdr company-candidates))
2461 company-common
2462 (string-prefix-p company-prefix company-common
2463 (company-call-backend 'ignore-case))))
2464
2465 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2466
2467 (defvar company-echo-last-msg nil)
2468 (make-variable-buffer-local 'company-echo-last-msg)
2469
2470 (defvar company-echo-timer nil)
2471
2472 (defvar company-echo-delay .01)
2473
2474 (defun company-echo-show (&optional getter)
2475 (when getter
2476 (setq company-echo-last-msg (funcall getter)))
2477 (let ((message-log-max nil))
2478 (if company-echo-last-msg
2479 (message "%s" company-echo-last-msg)
2480 (message ""))))
2481
2482 (defun company-echo-show-soon (&optional getter)
2483 (when company-echo-timer
2484 (cancel-timer company-echo-timer))
2485 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
2486
2487 (defsubst company-echo-show-when-idle (&optional getter)
2488 (when (sit-for company-echo-delay)
2489 (company-echo-show getter)))
2490
2491 (defun company-echo-format ()
2492
2493 (let ((limit (window-width (minibuffer-window)))
2494 (len -1)
2495 ;; Roll to selection.
2496 (candidates (nthcdr company-selection company-candidates))
2497 (i (if company-show-numbers company-selection 99999))
2498 comp msg)
2499
2500 (while candidates
2501 (setq comp (company-reformat (pop candidates))
2502 len (+ len 1 (length comp)))
2503 (if (< i 10)
2504 ;; Add number.
2505 (progn
2506 (setq comp (propertize (format "%d: %s" i comp)
2507 'face 'company-echo))
2508 (cl-incf len 3)
2509 (cl-incf i)
2510 (add-text-properties 3 (+ 3 (length company-common))
2511 '(face company-echo-common) comp))
2512 (setq comp (propertize comp 'face 'company-echo))
2513 (add-text-properties 0 (length company-common)
2514 '(face company-echo-common) comp))
2515 (if (>= len limit)
2516 (setq candidates nil)
2517 (push comp msg)))
2518
2519 (mapconcat 'identity (nreverse msg) " ")))
2520
2521 (defun company-echo-strip-common-format ()
2522
2523 (let ((limit (window-width (minibuffer-window)))
2524 (len (+ (length company-prefix) 2))
2525 ;; Roll to selection.
2526 (candidates (nthcdr company-selection company-candidates))
2527 (i (if company-show-numbers company-selection 99999))
2528 msg comp)
2529
2530 (while candidates
2531 (setq comp (company-strip-prefix (pop candidates))
2532 len (+ len 2 (length comp)))
2533 (when (< i 10)
2534 ;; Add number.
2535 (setq comp (format "%s (%d)" comp i))
2536 (cl-incf len 4)
2537 (cl-incf i))
2538 (if (>= len limit)
2539 (setq candidates nil)
2540 (push (propertize comp 'face 'company-echo) msg)))
2541
2542 (concat (propertize company-prefix 'face 'company-echo-common) "{"
2543 (mapconcat 'identity (nreverse msg) ", ")
2544 "}")))
2545
2546 (defun company-echo-hide ()
2547 (unless (equal company-echo-last-msg "")
2548 (setq company-echo-last-msg "")
2549 (company-echo-show)))
2550
2551 (defun company-echo-frontend (command)
2552 "`company-mode' front-end showing the candidates in the echo area."
2553 (pcase command
2554 (`post-command (company-echo-show-soon 'company-echo-format))
2555 (`hide (company-echo-hide))))
2556
2557 (defun company-echo-strip-common-frontend (command)
2558 "`company-mode' front-end showing the candidates in the echo area."
2559 (pcase command
2560 (`post-command (company-echo-show-soon 'company-echo-strip-common-format))
2561 (`hide (company-echo-hide))))
2562
2563 (defun company-echo-metadata-frontend (command)
2564 "`company-mode' front-end showing the documentation in the echo area."
2565 (pcase command
2566 (`post-command (company-echo-show-when-idle 'company-fetch-metadata))
2567 (`hide (company-echo-hide))))
2568
2569 (provide 'company)
2570 ;;; company.el ends here