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