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