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