1 ;;; company.el --- Modular in-buffer completion framework -*- lexical-binding: t -*-
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
6 ;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
8 ;; Keywords: abbrev, convenience, matching
9 ;; URL: http://company-mode.github.io/
10 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x, GNU Emacs 24.x
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; Company is a modular completion mechanism. Modules for retrieving completion
30 ;; candidates are called back-ends, modules for displaying them are front-ends.
32 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
33 ;; distributed in separate files and can be used individually.
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)
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)
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.
47 ;; To write your own back-end, look at the documentation for `company-backends'.
48 ;; Here is a simple example completing "foo":
50 ;; (defun company-my-backend (command &optional arg &rest ignored)
52 ;; (prefix (when (looking-back "foo\\>")
54 ;; (candidates (list "foobar" "foobaz" "foobarbaz"))
55 ;; (meta (format "This value is named %s" arg))))
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.
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'.
68 ;; See NEWS.md in the repository.
72 (eval-when-compile (require 'cl))
74 ;; FIXME: Use `user-error'.
75 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
76 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
77 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
78 (add-to-list 'debug-ignored-errors "^Company not ")
79 (add-to-list 'debug-ignored-errors "^No candidate number ")
80 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
81 (add-to-list 'debug-ignored-errors "^No other back-end$")
84 "Extensible inline text completion mechanism"
89 (defface company-tooltip
90 '((default :foreground "black")
91 (((class color) (min-colors 88) (background light))
92 (:background "cornsilk"))
93 (((class color) (min-colors 88) (background dark))
94 (:background "yellow")))
95 "Face used for the tooltip.")
97 (defface company-tooltip-selection
98 '((default :inherit company-tooltip)
99 (((class color) (min-colors 88) (background light))
100 (:background "light blue"))
101 (((class color) (min-colors 88) (background dark))
102 (:background "orange1"))
103 (t (:background "green")))
104 "Face used for the selection in the tooltip.")
106 (defface company-tooltip-mouse
107 '((default :inherit highlight))
108 "Face used for the tooltip item under the mouse.")
110 (defface company-tooltip-common
111 '((default :inherit company-tooltip)
112 (((background light))
113 :foreground "darkred")
116 "Face used for the common completion in the tooltip.")
118 (defface company-tooltip-common-selection
119 '((default :inherit company-tooltip-selection)
120 (((background light))
121 :foreground "darkred")
124 "Face used for the selected common completion in the tooltip.")
126 (defface company-tooltip-annotation
127 '((default :inherit company-tooltip)
128 (((background light))
129 :foreground "firebrick4")
132 "Face used for the annotation in the tooltip.")
134 (defface company-scrollbar-fg
135 '((((background light))
136 :background "darkred")
139 "Face used for the tooltip scrollbar thumb.")
141 (defface company-scrollbar-bg
142 '((default :inherit company-tooltip)
143 (((background light))
147 "Face used for the tooltip scrollbar background.")
149 (defface company-preview
150 '((((background light))
151 :inherit company-tooltip-selection)
154 :foreground "wheat"))
155 "Face used for the completion preview.")
157 (defface company-preview-common
158 '((((background light))
159 :inherit company-tooltip-selection)
161 :inherit company-preview
163 "Face used for the common part of the completion preview.")
165 (defface company-preview-search
166 '((((background light))
167 :inherit company-tooltip-common-selection)
169 :inherit company-preview
170 :background "blue1"))
171 "Face used for the search string in the completion preview.")
173 (defface company-echo nil
174 "Face used for completions in the echo area.")
176 (defface company-echo-common
177 '((((background dark)) (:foreground "firebrick1"))
178 (((background light)) (:background "firebrick4")))
179 "Face used for the common part of completions in the echo area.")
181 (defun company-frontends-set (variable value)
183 (let ((remainder value))
184 (setcdr remainder (delq (car remainder) (cdr remainder))))
185 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
186 (memq 'company-pseudo-tooltip-frontend value)
187 (error "Pseudo tooltip frontend cannot be used twice"))
188 (and (memq 'company-preview-if-just-one-frontend value)
189 (memq 'company-preview-frontend value)
190 (error "Preview frontend cannot be used twice"))
191 (and (memq 'company-echo value)
192 (memq 'company-echo-metadata-frontend value)
193 (error "Echo area cannot be used twice"))
194 ;; preview must come last
195 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
197 (setq value (append (delq f value) (list f)))))
198 (set variable value))
200 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
201 company-preview-if-just-one-frontend
202 company-echo-metadata-frontend)
203 "The list of active front-ends (visualizations).
204 Each front-end is a function that takes one argument. It is called with
205 one of the following arguments:
207 `show': When the visualization should start.
209 `hide': When the visualization should end.
211 `update': When the data has been updated.
213 `pre-command': Before every command that is executed while the
214 visualization is active.
216 `post-command': After every command that is executed while the
217 visualization is active.
219 The visualized data is stored in `company-prefix', `company-candidates',
220 `company-common', `company-selection', `company-point' and
221 `company-search-string'."
222 :set 'company-frontends-set
223 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
224 (const :tag "echo, strip common"
225 company-echo-strip-common-frontend)
226 (const :tag "show echo meta-data in echo"
227 company-echo-metadata-frontend)
228 (const :tag "pseudo tooltip"
229 company-pseudo-tooltip-frontend)
230 (const :tag "pseudo tooltip, multiple only"
231 company-pseudo-tooltip-unless-just-one-frontend)
232 (const :tag "preview" company-preview-frontend)
233 (const :tag "preview, unique only"
234 company-preview-if-just-one-frontend)
235 (function :tag "custom function" nil))))
237 (defcustom company-tooltip-limit 10
238 "The maximum number of candidates in the tooltip"
241 (defcustom company-tooltip-minimum 6
242 "The minimum height of the tooltip.
243 If this many lines are not available, prefer to display the tooltip above."
246 (defcustom company-tooltip-margin 1
247 "Width of margin columns to show around the toolip."
250 (defcustom company-tooltip-offset-display 'scrollbar
251 "Method using which the tooltip displays scrolling position.
252 `scrollbar' means draw a scrollbar to the right of the items.
253 `lines' means wrap items in lines with \"before\" and \"after\" counters."
254 :type '(choice (const :tag "Scrollbar" scrollbar)
255 (const :tag "Two lines" lines)))
257 (defvar company-safe-backends
258 '((company-abbrev . "Abbrev")
259 (company-capf . "completion-at-point-functions")
260 (company-clang . "Clang")
261 (company-cmake . "CMake")
262 (company-css . "CSS")
263 (company-dabbrev . "dabbrev for plain text")
264 (company-dabbrev-code . "dabbrev for code")
265 (company-eclim . "Eclim (an Eclipse interface)")
266 (company-elisp . "Emacs Lisp")
267 (company-etags . "etags")
268 (company-files . "Files")
269 (company-gtags . "GNU Global")
270 (company-ispell . "Ispell")
271 (company-keywords . "Programming language keywords")
272 (company-nxml . "nxml")
273 (company-oddmuse . "Oddmuse")
274 (company-pysmell . "PySmell")
275 (company-ropemacs . "ropemacs")
276 (company-semantic . "Semantic")
277 (company-tempo . "Tempo templates")
278 (company-xcode . "Xcode")))
279 (put 'company-safe-backends 'risky-local-variable t)
281 (defun company-safe-backends-p (backends)
282 (and (consp backends)
283 (not (dolist (backend backends)
284 (unless (if (consp backend)
285 (company-safe-backends-p backend)
286 (assq backend company-safe-backends))
289 (defvar company--include-capf (version< "24.3.50" emacs-version))
291 (defcustom company-backends `(,@(unless company--include-capf
292 (list 'company-elisp))
293 company-nxml company-css
294 company-eclim company-semantic company-clang
295 company-xcode company-ropemacs company-cmake
296 ,@(when company--include-capf
297 (list 'company-capf))
298 (company-gtags company-etags company-dabbrev-code
300 company-oddmuse company-files company-dabbrev)
301 "The list of active back-ends (completion engines).
302 Each list elements can itself be a list of back-ends. In that case their
303 completions are merged. Otherwise only the first matching back-end returns
306 `company-begin-backend' can be used to start a specific back-end,
307 `company-other-backend' will skip to the next matching back-end in the list.
309 Each back-end is a function that takes a variable number of arguments.
310 The first argument is the command requested from the back-end. It is one
313 `prefix': The back-end should return the text to be completed. It must be
314 text immediately before point. Returning nil passes control to the next
315 back-end. The function should return `stop' if it should complete but
316 cannot \(e.g. if it is in the middle of a string\). Instead of a string,
317 the back-end may return a cons where car is the prefix and cdr is used in
318 `company-minimum-prefix-length' test. It's either number or t, in which
319 case the test automatically succeeds.
321 `candidates': The second argument is the prefix to be completed. The
322 return value should be a list of candidates that start with the prefix.
326 `sorted': Return t here to indicate that the candidates are sorted and will
327 not need to be sorted again.
329 `duplicates': If non-nil, company will take care of removing duplicates
332 `no-cache': Usually company doesn't ask for candidates again as completion
333 progresses, unless the back-end returns t for this command. The second
334 argument is the latest prefix.
336 `meta': The second argument is a completion candidate. Return a (short)
337 documentation string for it.
339 `doc-buffer': The second argument is a completion candidate. Return a
340 buffer with documentation for it. Preferably use `company-doc-buffer',
342 `location': The second argument is a completion candidate. Return the cons
343 of buffer and buffer location, or of file and line number where the
344 completion candidate was defined.
346 `annotation': The second argument is a completion candidate. Returns a
347 string to be displayed inline with the candidate in the popup. If
348 duplicates are removed by company, candidates with equal string values will
349 be kept if they have different annotations. For that to work properly,
350 backends should store the related information with candidates using text
353 `require-match': If this returns t, the user is not allowed to enter
354 anything not offered as a candidate. Use with care! The default value nil
355 gives the user that choice with `company-require-match'. Return value
356 `never' overrides that option the other way around.
358 `init': Called once for each buffer. The back-end can check for external
359 programs and files and load any required libraries. Raising an error here
360 will show up in message log once, and the back-end will not be used for
363 `post-completion': Called after a completion candidate has been inserted
364 into the buffer. The second argument is the candidate. Can be used to
365 modify it, e.g. to expand a snippet.
367 The back-end should return nil for all commands it does not support or
368 does not know about. It should also be callable interactively and use
369 `company-begin-backend' to start itself in that case."
373 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
374 company-safe-backends)
375 (symbol :tag "User defined")
376 (repeat :tag "Merged Back-ends"
377 (choice :tag "Back-end"
378 ,@(mapcar (lambda (b)
379 `(const :tag ,(cdr b) ,(car b)))
380 company-safe-backends)
381 (symbol :tag "User defined"))))))
383 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
385 (defcustom company-transformers nil
386 "Functions to change the list of candidates received from backends,
387 after sorting and removal of duplicates (if appropriate).
388 Each function gets called with the return value of the previous one."
390 (const :tag "None" nil)
391 (const :tag "Sort by occurrence" (company-sort-by-occurrence))
392 (repeat :tag "User defined" (function))))
394 (defcustom company-completion-started-hook nil
395 "Hook run when company starts completing.
396 The hook is called with one argument that is non-nil if the completion was
400 (defcustom company-completion-cancelled-hook nil
401 "Hook run when company cancels completing.
402 The hook is called with one argument that is non-nil if the completion was
406 (defcustom company-completion-finished-hook nil
407 "Hook run when company successfully completes.
408 The hook is called with the selected candidate as an argument.
410 If you indend to use it to post-process candidates from a specific
411 back-end, consider using the `post-completion' command instead."
414 (defcustom company-minimum-prefix-length 3
415 "The minimum prefix length for idle completion."
416 :type '(integer :tag "prefix length"))
418 (defcustom company-require-match 'company-explicit-action-p
419 "If enabled, disallow non-matching input.
420 This can be a function do determine if a match is required.
422 This can be overridden by the back-end, if it returns t or `never' to
423 `require-match'. `company-auto-complete' also takes precedence over this."
424 :type '(choice (const :tag "Off" nil)
425 (function :tag "Predicate function")
426 (const :tag "On, if user interaction took place"
427 'company-explicit-action-p)
428 (const :tag "On" t)))
430 (defcustom company-auto-complete nil
431 "Determines when to auto-complete.
432 If this is enabled, all characters from `company-auto-complete-chars'
433 trigger insertion of the selected completion candidate.
434 This can also be a function."
435 :type '(choice (const :tag "Off" nil)
436 (function :tag "Predicate function")
437 (const :tag "On, if user interaction took place"
438 'company-explicit-action-p)
439 (const :tag "On" t)))
441 (defcustom company-auto-complete-chars '(?\ ?\) ?.)
442 "Determines which characters trigger auto-completion.
443 See `company-auto-complete'. If this is a string, each string character
444 tiggers auto-completion. If it is a list of syntax description characters (see
445 `modify-syntax-entry'), all characters with that syntax auto-complete.
447 This can also be a function, which is called with the new input and should
448 return non-nil if company should auto-complete.
450 A character that is part of a valid candidate never triggers auto-completion."
451 :type '(choice (string :tag "Characters")
453 (const :tag "Whitespace" ?\ )
454 (const :tag "Symbol" ?_)
455 (const :tag "Opening parentheses" ?\()
456 (const :tag "Closing parentheses" ?\))
457 (const :tag "Word constituent" ?w)
458 (const :tag "Punctuation." ?.)
459 (const :tag "String quote." ?\")
460 (const :tag "Paired delimiter." ?$)
461 (const :tag "Expression quote or prefix operator." ?\')
462 (const :tag "Comment starter." ?<)
463 (const :tag "Comment ender." ?>)
464 (const :tag "Character-quote." ?/)
465 (const :tag "Generic string fence." ?|)
466 (const :tag "Generic comment fence." ?!))
467 (function :tag "Predicate function")))
469 (defcustom company-idle-delay .7
470 "The idle delay in seconds until completion starts automatically.
471 A value of nil means no idle completion, t means show candidates
472 immediately when a prefix of `company-minimum-prefix-length' is reached."
473 :type '(choice (const :tag "never (nil)" nil)
474 (const :tag "immediate (t)" t)
475 (number :tag "seconds")))
477 (defcustom company-begin-commands '(self-insert-command org-self-insert-command)
478 "A list of commands after which idle completion is allowed.
479 If this is t, it can show completions after any command. See
480 `company-idle-delay'.
482 Alternatively, any command with a non-nil `company-begin' property is
483 treated as if it was on this list."
484 :type '(choice (const :tag "Any command" t)
485 (const :tag "Self insert command" '(self-insert-command))
486 (repeat :tag "Commands" function)))
488 (defcustom company-show-numbers nil
489 "If enabled, show quick-access numbers for the first ten candidates."
490 :type '(choice (const :tag "off" nil)
491 (const :tag "on" t)))
493 (defcustom company-selection-wrap-around nil
494 "If enabled, selecting item before first or after last wraps around."
495 :type '(choice (const :tag "off" nil)
496 (const :tag "on" t)))
498 (defvar company-end-of-buffer-workaround t
499 "Work around a visualization bug when completing at the end of the buffer.
500 The work-around consists of adding a newline.")
502 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
504 (defvar company-mode-map (make-sparse-keymap)
505 "Keymap used by `company-mode'.")
507 (defvar company-active-map
508 (let ((keymap (make-sparse-keymap)))
509 (define-key keymap "\e\e\e" 'company-abort)
510 (define-key keymap "\C-g" 'company-abort)
511 (define-key keymap (kbd "M-n") 'company-select-next)
512 (define-key keymap (kbd "M-p") 'company-select-previous)
513 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
514 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
515 (define-key keymap [down-mouse-1] 'ignore)
516 (define-key keymap [down-mouse-3] 'ignore)
517 (define-key keymap [mouse-1] 'company-complete-mouse)
518 (define-key keymap [mouse-3] 'company-select-mouse)
519 (define-key keymap [up-mouse-1] 'ignore)
520 (define-key keymap [up-mouse-3] 'ignore)
521 (define-key keymap [return] 'company-complete-selection)
522 (define-key keymap (kbd "RET") 'company-complete-selection)
523 (define-key keymap [tab] 'company-complete-common)
524 (define-key keymap (kbd "TAB") 'company-complete-common)
525 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
526 (define-key keymap "\C-w" 'company-show-location)
527 (define-key keymap "\C-s" 'company-search-candidates)
528 (define-key keymap "\C-\M-s" 'company-filter-candidates)
530 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
531 `(lambda () (interactive) (company-complete-number ,i))))
534 "Keymap that is enabled during an active completion.")
536 (defvar company--disabled-backends nil)
538 (defun company-init-backend (backend)
539 (and (symbolp backend)
540 (not (fboundp backend))
541 (ignore-errors (require backend nil t)))
543 (if (or (symbolp backend)
547 (funcall backend 'init)
548 (put backend 'company-init t))
550 (put backend 'company-init 'failed)
551 (unless (memq backend company--disabled-backends)
552 (message "Company back-end '%s' could not be initialized:\n%s"
553 backend (error-message-string err)))
554 (pushnew backend company--disabled-backends)
556 (mapc 'company-init-backend backend)))
558 (defvar company-default-lighter " company")
560 (defvar company-lighter company-default-lighter)
561 (make-variable-buffer-local 'company-lighter)
564 (define-minor-mode company-mode
565 "\"complete anything\"; is an in-buffer completion framework.
566 Completion starts automatically, depending on the values
567 `company-idle-delay' and `company-minimum-prefix-length'.
569 Completion can be controlled with the commands:
570 `company-complete-common', `company-complete-selection', `company-complete',
571 `company-select-next', `company-select-previous'. If these commands are
572 called before `company-idle-delay', completion will also start.
574 Completions can be searched with `company-search-candidates' or
575 `company-filter-candidates'. These can be used while completion is
578 The completion data is retrieved using `company-backends' and displayed
579 using `company-frontends'. If you want to start a specific back-end, call
580 it interactively or use `company-begin-backend'.
582 regular keymap (`company-mode-map'):
585 keymap during active completions (`company-active-map'):
587 \\{company-active-map}"
588 nil company-lighter company-mode-map
591 (add-hook 'pre-command-hook 'company-pre-command nil t)
592 (add-hook 'post-command-hook 'company-post-command nil t)
593 (mapc 'company-init-backend company-backends))
594 (remove-hook 'pre-command-hook 'company-pre-command t)
595 (remove-hook 'post-command-hook 'company-post-command t)
597 (kill-local-variable 'company-point)))
599 (defcustom company-global-modes t
600 "Modes for which `company-mode' mode is turned on by `global-company-mode'.
601 If nil, means no modes. If t, then all major modes have it turned on.
602 If a list, it should be a list of `major-mode' symbol names for which
603 `company-mode' should be automatically turned on. The sense of the list is
604 negated if it begins with `not'. For example:
606 means that `company-mode' is turned on for buffers in C and C++ modes only.
608 means that `company-mode' is always turned on except in `message-mode' buffers."
609 :type '(choice (const :tag "none" nil)
611 (set :menu-tag "mode specific" :tag "modes"
613 (const :tag "Except" not)
614 (repeat :inline t (symbol :tag "mode")))))
617 (define-globalized-minor-mode global-company-mode company-mode company-mode-on)
619 (defun company-mode-on ()
620 (when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s)))
621 (cond ((eq company-global-modes t)
623 ((eq (car-safe company-global-modes) 'not)
624 (not (memq major-mode (cdr company-global-modes))))
625 (t (memq major-mode company-global-modes))))
628 (defsubst company-assert-enabled ()
630 (company-uninstall-map)
631 (error "Company not enabled")))
633 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
635 (defvar company-my-keymap nil)
636 (make-variable-buffer-local 'company-my-keymap)
638 (defvar company-emulation-alist '((t . nil)))
640 (defsubst company-enable-overriding-keymap (keymap)
641 (company-uninstall-map)
642 (setq company-my-keymap keymap))
644 (defun company-ensure-emulation-alist ()
645 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
646 (setq emulation-mode-map-alists
647 (cons 'company-emulation-alist
648 (delq 'company-emulation-alist emulation-mode-map-alists)))))
650 (defun company-install-map ()
651 (unless (or (cdar company-emulation-alist)
652 (null company-my-keymap))
653 (setf (cdar company-emulation-alist) company-my-keymap)))
655 (defun company-uninstall-map ()
656 (setf (cdar company-emulation-alist) nil))
659 ;; Emacs calculates the active keymaps before reading the event. That means we
660 ;; cannot change the keymap from a timer. So we send a bogus command.
661 ;; XXX: Seems not to be needed anymore in Emacs 24.4
662 (defun company-ignore ()
664 (setq this-command last-command))
666 (global-set-key '[31415926] 'company-ignore)
668 (defun company-input-noop ()
669 (push 31415926 unread-command-events))
671 (defun company--column (&optional pos)
673 (when pos (goto-char pos))
677 (narrow-to-region (point) (point-max))
678 (let ((prefix (get-text-property (point) 'line-prefix)))
679 (if prefix (length prefix) 0)))
682 (defun company--row (&optional pos)
684 (when pos (goto-char pos))
685 (count-screen-lines (window-start)
686 (progn (vertical-motion 0) (point)))))
688 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
690 (defvar company-backend nil)
691 (make-variable-buffer-local 'company-backend)
693 (defun company-grab (regexp &optional expression limit)
694 (when (looking-back regexp limit)
695 (or (match-string-no-properties (or expression 0)) "")))
697 (defun company-grab-line (regexp &optional expression)
698 (company-grab regexp expression (point-at-bol)))
700 (defun company-grab-symbol ()
701 (if (looking-at "\\_>")
702 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
704 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
707 (defun company-grab-word ()
708 (if (looking-at "\\>")
709 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
711 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
714 (defun company-in-string-or-comment ()
715 (let ((ppss (syntax-ppss)))
716 (or (car (setq ppss (nthcdr 3 ppss)))
717 (car (setq ppss (cdr ppss)))
720 (if (fboundp 'locate-dominating-file)
721 (defalias 'company-locate-dominating-file 'locate-dominating-file)
722 (defun company-locate-dominating-file (file name)
724 (let ((dir (file-name-directory file))
726 (while (not (equal dir prev-dir))
727 (when (file-exists-p (expand-file-name name dir))
730 dir (file-name-directory (directory-file-name dir))))))))
732 (defun company-call-backend (&rest args)
733 (if (functionp company-backend)
734 (apply company-backend args)
735 (apply 'company--multi-backend-adapter company-backend args)))
737 (defun company--multi-backend-adapter (backends command &rest args)
738 (let ((backends (loop for b in backends
739 when (not (and (symbolp b)
740 (eq 'failed (get b 'company-init))))
744 (loop for backend in backends
745 when (equal (funcall backend 'prefix)
747 append (apply backend 'candidates args)))
752 (dolist (backend backends)
753 (when (setq value (apply backend command args))
754 (return value))))))))
756 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
758 (defvar company-prefix nil)
759 (make-variable-buffer-local 'company-prefix)
761 (defvar company-candidates nil)
762 (make-variable-buffer-local 'company-candidates)
764 (defvar company-candidates-length nil)
765 (make-variable-buffer-local 'company-candidates-length)
767 (defvar company-candidates-cache nil)
768 (make-variable-buffer-local 'company-candidates-cache)
770 (defvar company-candidates-predicate nil)
771 (make-variable-buffer-local 'company-candidates-predicate)
773 (defvar company-common nil)
774 (make-variable-buffer-local 'company-common)
776 (defvar company-selection 0)
777 (make-variable-buffer-local 'company-selection)
779 (defvar company-selection-changed nil)
780 (make-variable-buffer-local 'company-selection-changed)
782 (defvar company--explicit-action nil
783 "Non-nil, if explicit completion took place.")
784 (make-variable-buffer-local 'company--explicit-action)
786 (defvar company--auto-completion nil
787 "Non-nil when current candidate is being inserted automatically.
788 Controlled by `company-auto-complete'.")
790 (defvar company--point-max nil)
791 (make-variable-buffer-local 'company--point-max)
793 (defvar company-point nil)
794 (make-variable-buffer-local 'company-point)
796 (defvar company-timer nil)
798 (defvar company-added-newline nil)
799 (make-variable-buffer-local 'company-added-newline)
801 (defsubst company-strip-prefix (str)
802 (substring str (length company-prefix)))
804 (defun company--insert-candidate (candidate)
805 (setq candidate (substring-no-properties candidate))
806 ;; XXX: Return value we check here is subject to change.
807 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
808 (insert (company-strip-prefix candidate))
809 (delete-region (- (point) (length company-prefix)) (point))
812 (defmacro company-with-candidate-inserted (candidate &rest body)
813 "Evaluate BODY with CANDIDATE temporarily inserted.
814 This is a tool for back-ends that need candidates inserted before they
815 can retrieve meta-data for them."
817 `(let ((inhibit-modification-hooks t)
818 (inhibit-point-motion-hooks t)
819 (modified-p (buffer-modified-p)))
820 (company--insert-candidate ,candidate)
823 (delete-region company-point (point)))))
825 (defun company-explicit-action-p ()
826 "Return whether explicit completion action was taken by the user."
827 (or company--explicit-action
828 company-selection-changed))
830 (defun company-reformat (candidate)
831 ;; company-ispell needs this, because the results are always lower-case
832 ;; It's mory efficient to fix it only when they are displayed.
833 ;; FIXME: Adopt the current text's capitalization instead?
834 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
835 (concat company-prefix (substring candidate (length company-prefix)))
838 (defun company--should-complete ()
839 (and (not (or buffer-read-only overriding-terminal-local-map
840 overriding-local-map))
841 ;; Check if in the middle of entering a key combination.
842 (or (equal (this-command-keys-vector) [])
843 (not (keymapp (key-binding (this-command-keys-vector)))))
844 (eq company-idle-delay t)
845 (or (eq t company-begin-commands)
846 (memq this-command company-begin-commands)
847 (and (symbolp this-command) (get this-command 'company-begin)))
848 (not (and transient-mark-mode mark-active))))
850 (defun company-call-frontends (command)
851 (dolist (frontend company-frontends)
853 (funcall frontend command)
854 (error (error "Company: Front-end %s error \"%s\" on command %s"
855 frontend (error-message-string err) command)))))
857 (defun company-set-selection (selection &optional force-update)
859 (if company-selection-wrap-around
860 (mod selection company-candidates-length)
861 (max 0 (min (1- company-candidates-length) selection))))
862 (when (or force-update (not (equal selection company-selection)))
863 (setq company-selection selection
864 company-selection-changed t)
865 (company-call-frontends 'update)))
867 (defun company-apply-predicate (candidates predicate)
869 (dolist (c candidates)
870 (when (funcall predicate c)
874 (defun company-update-candidates (candidates)
875 (setq company-candidates-length (length candidates))
876 (if (> company-selection 0)
877 ;; Try to restore the selection
878 (let ((selected (nth company-selection company-candidates)))
879 (setq company-selection 0
880 company-candidates candidates)
882 (while (and candidates (string< (pop candidates) selected))
883 (incf company-selection))
885 ;; Make sure selection isn't out of bounds.
886 (setq company-selection (min (1- company-candidates-length)
887 company-selection)))))
888 (setq company-selection 0
889 company-candidates candidates))
891 (push (cons company-prefix company-candidates) company-candidates-cache)
893 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
894 ;; We want to support non-prefix completion, so filtering is the
895 ;; responsibility of each respective backend, not ours.
896 ;; On the other hand, we don't want to replace non-prefix input in
897 ;; `company-complete-common'.
899 (if (cdr company-candidates)
900 (company--safe-candidate
901 (let ((common (try-completion company-prefix company-candidates)))
903 ;; Mulple equal strings, probably with different
907 (car company-candidates)))))
909 (defun company--safe-candidate (str)
910 ;; XXX: This feature is deprecated.
911 (or (company-call-backend 'crop str)
914 (defun company-calculate-candidates (prefix)
915 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
916 (ignore-case (company-call-backend 'ignore-case)))
918 (when company-candidates-cache
919 (let ((len (length prefix))
920 (completion-ignore-case ignore-case)
922 (dotimes (i (1+ len))
923 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
924 company-candidates-cache)))
925 (setq candidates (all-completions prefix prev))
927 ;; no cache match, call back-end
929 (setq candidates (company-call-backend 'candidates prefix))
930 (when company-candidates-predicate
932 (company-apply-predicate candidates
933 company-candidates-predicate)))
934 (unless (company-call-backend 'sorted)
935 (setq candidates (sort candidates 'string<)))
936 (when (company-call-backend 'duplicates)
937 (company--strip-duplicates candidates))))
938 (setq candidates (company--transform-candidates candidates))
940 (if (or (cdr candidates)
941 (not (eq t (compare-strings (car candidates) nil nil
942 prefix nil nil ignore-case))))
944 ;; Already completed and unique; don't start.
947 (defun company--strip-duplicates (candidates)
948 (let ((c2 candidates))
954 (while (let ((str2 (car c2)))
955 (if (not (equal str str2))
958 (setq anno (company-call-backend
961 (company-call-backend
966 (defun company--transform-candidates (candidates)
967 (let ((c candidates))
968 (dolist (tr company-transformers)
969 (setq c (funcall tr c)))
972 (defun company-sort-by-occurrence (candidates)
973 "Sort CANDIDATES according to their occurrences.
974 Searches for each in the currently visible part of the current buffer and
975 gives priority to the closest ones above point, then closest ones below
976 point. The rest of the list is appended unchanged.
977 Keywords and function definition names are ignored."
984 (progn (forward-line 0)
985 (search-backward candidate (window-start) t)))
987 (search-forward candidate (window-end) t)))
988 (let ((beg (match-beginning 0))
990 (when (save-excursion
992 (and (not (memq (get-text-property (point) 'face)
993 '(font-lock-function-name-face
994 font-lock-keyword-face)))
995 (let* ((prefix (company-call-backend 'prefix))
996 (prefix (or (car-safe prefix) prefix)))
997 (and (stringp prefix)
998 (= (length prefix) (- end beg))))))
999 (push (cons candidate (if (< beg (point))
1001 (- beg (window-start))))
1006 (mapcar #'car (sort occurs (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
1009 (defun company-idle-begin (buf win tick pos)
1011 (eq buf (current-buffer))
1012 (eq win (selected-window))
1013 (eq tick (buffer-chars-modified-tick))
1015 (not company-candidates)
1016 (not (equal (point) company-point))
1017 (let ((company-idle-delay t)
1018 (company-begin-commands t))
1020 (when company-candidates
1021 (when (version< emacs-version "24.3.50")
1022 (company-input-noop))
1023 (company-post-command)))))
1025 (defun company-auto-begin ()
1026 (company-assert-enabled)
1028 (not company-candidates)
1029 (let ((company-idle-delay t)
1030 (company-minimum-prefix-length 0)
1031 (company-begin-commands t))
1033 ;; Return non-nil if active.
1036 (defun company-manual-begin ()
1038 (setq company--explicit-action t)
1040 (company-auto-begin)
1041 (unless company-candidates
1042 (setq company--explicit-action nil))))
1044 (defun company-other-backend (&optional backward)
1045 (interactive (list current-prefix-arg))
1046 (company-assert-enabled)
1048 (let* ((after (cdr (member company-backend company-backends)))
1049 (before (cdr (member company-backend (reverse company-backends))))
1051 (append before (reverse after))
1052 (append after (reverse before)))))
1054 (dolist (backend next)
1055 (when (ignore-errors (company-begin-backend backend))
1057 (company-manual-begin))
1058 (unless company-candidates
1059 (error "No other back-end")))
1061 (defun company-require-match-p ()
1062 (let ((backend-value (company-call-backend 'require-match)))
1063 (or (eq backend-value t)
1064 (and (not (eq backend-value 'never))
1065 (if (functionp company-require-match)
1066 (funcall company-require-match)
1067 (eq company-require-match t))))))
1069 (defun company-auto-complete-p (input)
1070 "Return non-nil, if input starts with punctuation or parentheses."
1071 (and (if (functionp company-auto-complete)
1072 (funcall company-auto-complete)
1073 company-auto-complete)
1074 (if (functionp company-auto-complete-chars)
1075 (funcall company-auto-complete-chars input)
1076 (if (consp company-auto-complete-chars)
1077 (memq (char-syntax (string-to-char input))
1078 company-auto-complete-chars)
1079 (string-match (substring input 0 1) company-auto-complete-chars)))))
1081 (defun company--incremental-p ()
1082 (and (> (point) company-point)
1083 (> (point-max) company--point-max)
1084 (not (eq this-command 'backward-delete-char-untabify))
1085 (equal (buffer-substring (- company-point (length company-prefix))
1089 (defun company--continue-failed ()
1090 (let ((input (buffer-substring-no-properties (point) company-point)))
1092 ((company-auto-complete-p input)
1095 (goto-char company-point)
1096 (let ((company--auto-completion t))
1097 (company-complete-selection))
1099 ((company-require-match-p)
1100 ;; wrong incremental input, but required match
1101 (delete-char (- (length input)))
1103 (message "Matching input is required")
1105 ((equal company-prefix (car company-candidates))
1106 ;; last input was actually success
1107 (company-cancel company-prefix))
1108 (t (company-cancel)))))
1110 (defun company--good-prefix-p (prefix)
1111 (and (or (company-explicit-action-p)
1112 (unless (eq prefix 'stop)
1113 (or (eq (cdr-safe prefix) t)
1114 (>= (or (cdr-safe prefix) (length prefix))
1115 company-minimum-prefix-length))))
1116 (stringp (or (car-safe prefix) prefix))))
1118 (defun company--continue ()
1119 (when (company-call-backend 'no-cache company-prefix)
1120 ;; Don't complete existing candidates, fetch new ones.
1121 (setq company-candidates-cache nil))
1122 (let* ((new-prefix (company-call-backend 'prefix))
1123 (c (when (and (company--good-prefix-p new-prefix)
1124 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1125 (= (- (point) (length new-prefix))
1126 (- company-point (length company-prefix))))
1127 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1128 (company-calculate-candidates new-prefix))))
1131 ;; t means complete/unique.
1132 (company-cancel new-prefix))
1134 ;; incremental match
1135 (setq company-prefix new-prefix)
1136 (company-update-candidates c)
1138 ((not (company--incremental-p))
1140 (t (company--continue-failed)))))
1142 (defun company--begin-new ()
1144 (dolist (backend (if company-backend
1145 ;; prefer manual override
1146 (list company-backend)
1149 (if (or (symbolp backend)
1150 (functionp backend))
1151 (when (or (not (symbolp backend))
1152 (eq t (get backend 'company-init))
1153 (unless (get backend 'company-init)
1154 (company-init-backend backend)))
1155 (funcall backend 'prefix))
1156 (company--multi-backend-adapter backend 'prefix)))
1158 (when (company--good-prefix-p prefix)
1159 (setq prefix (or (car-safe prefix) prefix)
1160 company-backend backend
1161 c (company-calculate-candidates prefix))
1162 ;; t means complete/unique. We don't start, so no hooks.
1164 (when company--explicit-action
1165 (message "No completion found"))
1166 (setq company-prefix prefix)
1167 (when (symbolp backend)
1168 (setq company-lighter (concat " " (symbol-name backend))))
1169 (company-update-candidates c)
1170 (run-hook-with-args 'company-completion-started-hook
1171 (company-explicit-action-p))
1172 (company-call-frontends 'show)))
1175 (defun company-begin ()
1176 (or (and company-candidates (company--continue))
1177 (and (company--should-complete) (company--begin-new)))
1178 (when company-candidates
1179 (let ((modified (buffer-modified-p)))
1180 (when (and company-end-of-buffer-workaround (eobp))
1181 (save-excursion (insert "\n"))
1182 (setq company-added-newline
1183 (or modified (buffer-chars-modified-tick)))))
1184 (setq company-point (point)
1185 company--point-max (point-max))
1186 (company-ensure-emulation-alist)
1187 (company-enable-overriding-keymap company-active-map)
1188 (company-call-frontends 'update)))
1190 (defun company-cancel (&optional result)
1191 (and company-added-newline
1192 (> (point-max) (point-min))
1193 (let ((tick (buffer-chars-modified-tick)))
1194 (delete-region (1- (point-max)) (point-max))
1195 (equal tick company-added-newline))
1196 ;; Only set unmodified when tick remained the same since insert,
1197 ;; and the buffer wasn't modified before.
1198 (set-buffer-modified-p nil))
1199 (when company-prefix
1200 (if (stringp result)
1202 (company-call-backend 'pre-completion result)
1203 (run-hook-with-args 'company-completion-finished-hook result)
1204 (company-call-backend 'post-completion result))
1205 (run-hook-with-args 'company-completion-cancelled-hook result)))
1206 (setq company-added-newline nil
1209 company-candidates nil
1210 company-candidates-length nil
1211 company-candidates-cache nil
1212 company-candidates-predicate nil
1215 company-selection-changed nil
1216 company--explicit-action nil
1217 company-lighter company-default-lighter
1218 company--point-max nil
1221 (cancel-timer company-timer))
1222 (company-search-mode 0)
1223 (company-call-frontends 'hide)
1224 (company-enable-overriding-keymap nil)
1225 ;; Make return value explicit.
1228 (defun company-abort ()
1231 ;; Don't start again, unless started manually.
1232 (setq company-point (point)))
1234 (defun company-finish (result)
1235 (company--insert-candidate result)
1236 (company-cancel result)
1237 ;; Don't start again, unless started manually.
1238 (setq company-point (point)))
1240 (defsubst company-keep (command)
1241 (and (symbolp command) (get command 'company-keep)))
1243 (defun company-pre-command ()
1244 (unless (company-keep this-command)
1246 (when company-candidates
1247 (company-call-frontends 'pre-command))
1248 (error (message "Company: An error occurred in pre-command")
1249 (message "%s" (error-message-string err))
1252 (cancel-timer company-timer)
1253 (setq company-timer nil))
1254 (company-uninstall-map))
1256 (defun company-post-command ()
1257 (unless (company-keep this-command)
1260 (unless (equal (point) company-point)
1262 (if company-candidates
1263 (company-call-frontends 'post-command)
1264 (and (numberp company-idle-delay)
1265 (or (eq t company-begin-commands)
1266 (memq this-command company-begin-commands))
1268 (run-with-timer company-idle-delay nil
1270 (current-buffer) (selected-window)
1271 (buffer-chars-modified-tick) (point))))))
1272 (error (message "Company: An error occurred in post-command")
1273 (message "%s" (error-message-string err))
1275 (company-install-map))
1277 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1279 (defvar company-search-string nil)
1280 (make-variable-buffer-local 'company-search-string)
1282 (defvar company-search-lighter " Search: \"\"")
1283 (make-variable-buffer-local 'company-search-lighter)
1285 (defvar company-search-old-map nil)
1286 (make-variable-buffer-local 'company-search-old-map)
1288 (defvar company-search-old-selection 0)
1289 (make-variable-buffer-local 'company-search-old-selection)
1291 (defun company-search (text lines)
1292 (let ((quoted (regexp-quote text))
1294 (dolist (line lines)
1295 (when (string-match quoted line (length company-prefix))
1299 (defun company-search-printing-char ()
1301 (company-search-assert-enabled)
1302 (setq company-search-string
1303 (concat (or company-search-string "") (string last-command-event))
1304 company-search-lighter (concat " Search: \"" company-search-string
1306 (let ((pos (company-search company-search-string
1307 (nthcdr company-selection company-candidates))))
1310 (company-set-selection (+ company-selection pos) t))))
1312 (defun company-search-repeat-forward ()
1313 "Repeat the incremental search in completion candidates forward."
1315 (company-search-assert-enabled)
1316 (let ((pos (company-search company-search-string
1317 (cdr (nthcdr company-selection
1318 company-candidates)))))
1321 (company-set-selection (+ company-selection pos 1) t))))
1323 (defun company-search-repeat-backward ()
1324 "Repeat the incremental search in completion candidates backwards."
1326 (company-search-assert-enabled)
1327 (let ((pos (company-search company-search-string
1328 (nthcdr (- company-candidates-length
1330 (reverse company-candidates)))))
1333 (company-set-selection (- company-selection pos 1) t))))
1335 (defun company-create-match-predicate ()
1336 (setq company-candidates-predicate
1337 `(lambda (candidate)
1338 ,(if company-candidates-predicate
1339 `(and (string-match ,company-search-string candidate)
1340 (funcall ,company-candidates-predicate
1342 `(string-match ,company-search-string candidate))))
1343 (company-update-candidates
1344 (company-apply-predicate company-candidates company-candidates-predicate))
1345 ;; Invalidate cache.
1346 (setq company-candidates-cache (cons company-prefix company-candidates)))
1348 (defun company-filter-printing-char ()
1350 (company-search-assert-enabled)
1351 (company-search-printing-char)
1352 (company-create-match-predicate)
1353 (company-call-frontends 'update))
1355 (defun company-search-kill-others ()
1356 "Limit the completion candidates to the ones matching the search string."
1358 (company-search-assert-enabled)
1359 (company-create-match-predicate)
1360 (company-search-mode 0)
1361 (company-call-frontends 'update))
1363 (defun company-search-abort ()
1364 "Abort searching the completion candidates."
1366 (company-search-assert-enabled)
1367 (company-set-selection company-search-old-selection t)
1368 (company-search-mode 0))
1370 (defun company-search-other-char ()
1372 (company-search-assert-enabled)
1373 (company-search-mode 0)
1374 (company--unread-last-input))
1376 (defvar company-search-map
1378 (keymap (make-keymap)))
1379 (if (fboundp 'max-char)
1380 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1381 'company-search-printing-char)
1383 ;; obsolete in Emacs 23
1384 (let ((l (generic-character-list))
1385 (table (nth 1 keymap)))
1387 (set-char-table-default table (car l) 'company-search-printing-char)
1388 (setq l (cdr l))))))
1389 (define-key keymap [t] 'company-search-other-char)
1391 (define-key keymap (make-string 1 i) 'company-search-other-char)
1394 (define-key keymap (vector i) 'company-search-printing-char)
1396 (let ((meta-map (make-sparse-keymap)))
1397 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1398 (define-key keymap [escape] meta-map))
1399 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1400 (define-key keymap "\e\e\e" 'company-search-other-char)
1401 (define-key keymap [escape escape escape] 'company-search-other-char)
1403 (define-key keymap "\C-g" 'company-search-abort)
1404 (define-key keymap "\C-s" 'company-search-repeat-forward)
1405 (define-key keymap "\C-r" 'company-search-repeat-backward)
1406 (define-key keymap "\C-o" 'company-search-kill-others)
1408 "Keymap used for incrementally searching the completion candidates.")
1410 (define-minor-mode company-search-mode
1411 "Search mode for completion candidates.
1412 Don't start this directly, use `company-search-candidates' or
1413 `company-filter-candidates'."
1414 nil company-search-lighter nil
1415 (if company-search-mode
1416 (if (company-manual-begin)
1418 (setq company-search-old-selection company-selection)
1419 (company-call-frontends 'update))
1420 (setq company-search-mode nil))
1421 (kill-local-variable 'company-search-string)
1422 (kill-local-variable 'company-search-lighter)
1423 (kill-local-variable 'company-search-old-selection)
1424 (company-enable-overriding-keymap company-active-map)))
1426 (defun company-search-assert-enabled ()
1427 (company-assert-enabled)
1428 (unless company-search-mode
1429 (company-uninstall-map)
1430 (error "Company not in search mode")))
1432 (defun company-search-candidates ()
1433 "Start searching the completion candidates incrementally.
1435 \\<company-search-map>Search can be controlled with the commands:
1436 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1437 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1438 - `company-search-abort' (\\[company-search-abort])
1440 Regular characters are appended to the search string.
1442 The command `company-search-kill-others' (\\[company-search-kill-others])
1443 uses the search string to limit the completion candidates."
1445 (company-search-mode 1)
1446 (company-enable-overriding-keymap company-search-map))
1448 (defvar company-filter-map
1449 (let ((keymap (make-keymap)))
1450 (define-key keymap [remap company-search-printing-char]
1451 'company-filter-printing-char)
1452 (set-keymap-parent keymap company-search-map)
1454 "Keymap used for incrementally searching the completion candidates.")
1456 (defun company-filter-candidates ()
1457 "Start filtering the completion candidates incrementally.
1458 This works the same way as `company-search-candidates' immediately
1459 followed by `company-search-kill-others' after each input."
1461 (company-search-mode 1)
1462 (company-enable-overriding-keymap company-filter-map))
1464 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1466 (defun company-select-next ()
1467 "Select the next candidate in the list."
1469 (when (company-manual-begin)
1470 (company-set-selection (1+ company-selection))))
1472 (defun company-select-previous ()
1473 "Select the previous candidate in the list."
1475 (when (company-manual-begin)
1476 (company-set-selection (1- company-selection))))
1478 (defun company-select-next-or-abort ()
1479 "Select the next candidate if more than one, else abort
1480 and invoke the normal binding."
1482 (if (> company-candidates-length 1)
1483 (company-select-next)
1485 (company--unread-last-input)))
1487 (defun company-select-previous-or-abort ()
1488 "Select the previous candidate if more than one, else abort
1489 and invoke the normal binding."
1491 (if (> company-candidates-length 1)
1492 (company-select-previous)
1494 (company--unread-last-input)))
1496 (defvar company-pseudo-tooltip-overlay)
1498 (defvar company-tooltip-offset)
1500 (defun company--inside-tooltip-p (event-col-row row height)
1501 (let* ((ovl company-pseudo-tooltip-overlay)
1502 (column (overlay-get ovl 'company-column))
1503 (width (overlay-get ovl 'company-width))
1504 (evt-col (car event-col-row))
1505 (evt-row (cdr event-col-row)))
1506 (and (>= evt-col column)
1507 (< evt-col (+ column width))
1509 (and (> evt-row row)
1510 (<= evt-row (+ row height) ))
1511 (and (< evt-row row)
1512 (>= evt-row (+ row height)))))))
1514 (defun company--event-col-row (event)
1515 (let* ((col-row (posn-actual-col-row (event-start event)))
1517 (row (cdr col-row)))
1518 (incf col (window-hscroll))
1519 (and header-line-format
1520 (version< "24" emacs-version)
1524 (defun company-select-mouse (event)
1525 "Select the candidate picked by the mouse."
1527 (let ((event-col-row (company--event-col-row event))
1528 (ovl-row (company--row))
1529 (ovl-height (and company-pseudo-tooltip-overlay
1530 (min (overlay-get company-pseudo-tooltip-overlay
1532 company-candidates-length))))
1534 (company--inside-tooltip-p event-col-row ovl-row ovl-height))
1536 (company-set-selection (+ (cdr event-col-row)
1537 (1- company-tooltip-offset)
1538 (if (and (eq company-tooltip-offset-display 'lines)
1539 (not (zerop company-tooltip-offset)))
1542 (if (< ovl-height 0)
1547 (company--unread-last-input)
1550 (defun company-complete-mouse (event)
1551 "Insert the candidate picked by the mouse."
1553 (when (company-select-mouse event)
1554 (company-complete-selection)))
1556 (defun company-complete-selection ()
1557 "Insert the selected candidate."
1559 (when (company-manual-begin)
1560 (let ((result (nth company-selection company-candidates)))
1561 (when company--auto-completion
1562 (setq result (company--safe-candidate result)))
1563 (company-finish result))))
1565 (defun company-complete-common ()
1566 "Insert the common part of all candidates."
1568 (when (company-manual-begin)
1569 (if (and (not (cdr company-candidates))
1570 (equal company-common (car company-candidates)))
1571 (company-complete-selection)
1572 (when company-common
1573 (company--insert-candidate company-common)))))
1575 (defun company-complete ()
1576 "Insert the common part of all candidates or the current selection.
1577 The first time this is called, the common part is inserted, the second
1578 time, or when the selection has been changed, the selected candidate is
1581 (when (company-manual-begin)
1582 (if (or company-selection-changed
1583 (eq last-command 'company-complete-common))
1584 (call-interactively 'company-complete-selection)
1585 (call-interactively 'company-complete-common)
1586 (setq this-command 'company-complete-common))))
1588 (defun company-complete-number (n)
1589 "Insert the Nth candidate.
1590 To show the number next to the candidates in some back-ends, enable
1591 `company-show-numbers'."
1592 (when (company-manual-begin)
1593 (and (< n 1) (> n company-candidates-length)
1594 (error "No candidate number %d" n))
1596 (company-finish (nth n company-candidates))))
1598 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1600 (defconst company-space-strings-limit 100)
1602 (defconst company-space-strings
1604 (dotimes (i company-space-strings-limit)
1605 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1606 (apply 'vector lst)))
1608 (defun company-space-string (len)
1609 (if (< len company-space-strings-limit)
1610 (aref company-space-strings len)
1611 (make-string len ?\ )))
1613 (defun company-safe-substring (str from &optional to)
1614 (if (> from (string-width str))
1618 (move-to-column from)
1619 (let ((beg (point)))
1623 (concat (buffer-substring beg (point))
1624 (let ((padding (- to (current-column))))
1626 (company-space-string padding)))))
1627 (buffer-substring beg (point-max)))))))
1629 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1631 (defvar company-last-metadata nil)
1632 (make-variable-buffer-local 'company-last-metadata)
1634 (defun company-fetch-metadata ()
1635 (let ((selected (nth company-selection company-candidates)))
1636 (unless (eq selected (car company-last-metadata))
1637 (setq company-last-metadata
1638 (cons selected (company-call-backend 'meta selected))))
1639 (cdr company-last-metadata)))
1641 (defun company-doc-buffer (&optional string)
1642 (with-current-buffer (get-buffer-create "*company-documentation*")
1649 (defvar company--electric-commands
1650 '(scroll-other-window scroll-other-window-down)
1651 "List of Commands that won't break out of electric commands.")
1653 (defmacro company--electric-do (&rest body)
1654 (declare (indent 0) (debug t))
1655 `(when (company-manual-begin)
1656 (save-window-excursion
1657 (let ((height (window-height))
1658 (row (company--row))
1661 (and (< (window-height) height)
1662 (< (- (window-height) row 2) company-tooltip-limit)
1663 (recenter (- (window-height) row 2)))
1664 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1665 company--electric-commands)
1666 (call-interactively cmd))
1667 (company--unread-last-input)))))
1669 (defun company--unread-last-input ()
1670 (when last-input-event
1671 (clear-this-command-keys t)
1672 (setq unread-command-events (list last-input-event))))
1674 (defun company-show-doc-buffer ()
1675 "Temporarily show the documentation buffer for the selection."
1677 (company--electric-do
1678 (let* ((selected (nth company-selection company-candidates))
1679 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1680 (error "No documentation available"))))
1681 (with-current-buffer doc-buffer
1682 (goto-char (point-min)))
1683 (display-buffer doc-buffer t))))
1684 (put 'company-show-doc-buffer 'company-keep t)
1686 (defun company-show-location ()
1687 "Temporarily display a buffer showing the selected candidate in context."
1689 (company--electric-do
1690 (let* ((selected (nth company-selection company-candidates))
1691 (location (company-call-backend 'location selected))
1692 (pos (or (cdr location) (error "No location available")))
1693 (buffer (or (and (bufferp (car location)) (car location))
1694 (find-file-noselect (car location) t))))
1695 (with-selected-window (display-buffer buffer t)
1698 (if (bufferp (car location))
1700 (goto-char (point-min))
1701 (forward-line (1- pos))))
1702 (set-window-start nil (point))))))
1703 (put 'company-show-location 'company-keep t)
1705 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1707 (defvar company-callback nil)
1708 (make-variable-buffer-local 'company-callback)
1710 (defvar company-begin-with-marker nil)
1711 (make-variable-buffer-local 'company-begin-with-marker)
1713 (defun company-remove-callback (&optional ignored)
1714 (remove-hook 'company-completion-finished-hook company-callback t)
1715 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1716 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1717 (when company-begin-with-marker
1718 (set-marker company-begin-with-marker nil)))
1720 (defun company-begin-backend (backend &optional callback)
1721 "Start a completion at point using BACKEND."
1722 (interactive (let ((val (completing-read "Company back-end: "
1724 'functionp nil "company-")))
1726 (list (intern val)))))
1727 (when (setq company-callback callback)
1728 (add-hook 'company-completion-finished-hook company-callback nil t))
1729 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1730 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1731 (setq company-backend backend)
1732 ;; Return non-nil if active.
1733 (or (company-manual-begin)
1735 (setq company-backend nil)
1736 (error "Cannot complete at point"))))
1738 (defun company-begin-with (candidates
1739 &optional prefix-length require-match callback)
1740 "Start a completion at point.
1741 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length
1742 of the prefix that already is in the buffer before point.
1745 CALLBACK is a function called with the selected result if the user
1746 successfully completes the input.
1748 Example: \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1749 ;; FIXME: When Emacs 23 is no longer a concern, replace
1750 ;; `company-begin-with-marker' with a lexical variable; use a lexical closure.
1751 (setq company-begin-with-marker (copy-marker (point) t))
1752 (company-begin-backend
1753 `(lambda (command &optional arg &rest ignored)
1755 ((eq command 'prefix)
1756 (when (equal (point) (marker-position company-begin-with-marker))
1757 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1758 ((eq command 'candidates)
1759 (all-completions arg ',candidates))
1760 ((eq command 'require-match)
1764 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1766 (defvar company-pseudo-tooltip-overlay nil)
1767 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1769 (defvar company-tooltip-offset 0)
1770 (make-variable-buffer-local 'company-tooltip-offset)
1772 (defun company-tooltip--lines-update-offset (selection num-lines limit)
1774 (setq company-tooltip-offset
1775 (max (min selection company-tooltip-offset)
1776 (- selection -1 limit)))
1778 (when (<= company-tooltip-offset 1)
1780 (setq company-tooltip-offset 0))
1782 (when (>= company-tooltip-offset (- num-lines limit 1))
1784 (when (= selection (1- num-lines))
1785 (decf company-tooltip-offset)
1786 (when (<= company-tooltip-offset 1)
1787 (setq company-tooltip-offset 0)
1792 (defun company-tooltip--simple-update-offset (selection num-lines limit)
1793 (setq company-tooltip-offset
1794 (if (< selection company-tooltip-offset)
1796 (max company-tooltip-offset
1797 (- selection limit -1)))))
1801 (defsubst company-round-tab (arg)
1802 (* (/ (+ arg tab-width) tab-width) tab-width))
1804 (defun company-plainify (str)
1805 (let ((prefix (get-text-property 0 'line-prefix str)))
1806 (when prefix ; Keep the original value unmodified, for no special reason.
1807 (setq str (concat prefix str))
1808 (remove-text-properties 0 (length str) '(line-prefix) str)))
1809 (let* ((pieces (split-string str "\t"))
1812 (setcar copy (company-safe-substring
1813 (car copy) 0 (company-round-tab (string-width (car copy)))))
1815 (apply 'concat pieces)))
1817 (defun company-fill-propertize (value annotation width selected left right)
1818 (let* ((margin (length left))
1819 (common (+ (or (company-call-backend 'common-part value)
1820 (length company-common)) margin))
1821 (ann-start (+ margin (length value)))
1823 (company-safe-substring (concat value annotation)
1826 (setq width (+ width margin (length right)))
1828 (add-text-properties 0 width '(face company-tooltip
1829 mouse-face company-tooltip-mouse)
1831 (add-text-properties margin common
1832 '(face company-tooltip-common
1833 mouse-face company-tooltip-mouse)
1835 (add-text-properties ann-start (+ ann-start (length annotation))
1836 '(face company-tooltip-annotation
1837 mouse-face company-tooltip-mouse)
1840 (if (and company-search-string
1841 (string-match (regexp-quote company-search-string) value
1842 (length company-prefix)))
1843 (let ((beg (+ margin (match-beginning 0)))
1844 (end (+ margin (match-end 0))))
1845 (add-text-properties beg end '(face company-tooltip-selection)
1847 (when (< beg common)
1848 (add-text-properties beg common
1849 '(face company-tooltip-common-selection)
1851 (add-text-properties 0 width '(face company-tooltip-selection
1852 mouse-face company-tooltip-selection)
1854 (add-text-properties margin common
1855 '(face company-tooltip-common-selection
1856 mouse-face company-tooltip-selection)
1862 (defun company-buffer-lines (beg end)
1865 (while (and (= 1 (vertical-motion 1))
1867 (let ((bound (min end (1- (point)))))
1868 ;; A visual line can contain several physical lines (e.g. with outline's
1869 ;; folding overlay). Take only the first one.
1870 (push (buffer-substring beg
1873 (re-search-forward "$" bound 'move)
1877 (unless (eq beg end)
1878 (push (buffer-substring beg end) lines))
1881 (defun company-modify-line (old new offset)
1882 (concat (company-safe-substring old 0 offset)
1884 (company-safe-substring old (+ offset (length new)))))
1886 (defsubst company--length-limit (lst limit)
1887 (if (nthcdr limit lst)
1891 (defun company--replacement-string (lines old column nl &optional align-top)
1892 (decf column company-tooltip-margin)
1894 (let ((width (length (car lines)))
1895 (remaining-cols (- (+ (company--window-width) (window-hscroll))
1897 (when (> width remaining-cols)
1898 (decf column (- width remaining-cols))))
1900 (let ((offset (and (< column 0) (- column)))
1905 ;; untouched lines first
1906 (dotimes (_ (- (length old) (length lines)))
1907 (push (pop old) new)))
1908 ;; length into old lines.
1910 (push (company-modify-line (pop old)
1911 (company--offset-line (pop lines) offset)
1913 ;; Append whole new lines.
1915 (push (concat (company-space-string column)
1916 (company--offset-line (pop lines) offset))
1919 (let ((str (concat (when nl "\n")
1920 (mapconcat 'identity (nreverse new) "\n")
1922 (font-lock-append-text-property 0 (length str) 'face 'default str)
1925 (defun company--offset-line (line offset)
1926 (if (and offset line)
1927 (substring line offset)
1930 (defun company--create-lines (selection limit)
1931 (let ((len company-candidates-length)
1933 (window-width (company--window-width))
1942 ;; Maybe clear old offset.
1943 (when (<= len (+ company-tooltip-offset limit))
1944 (setq company-tooltip-offset 0))
1946 ;; Scroll to offset.
1947 (if (eq company-tooltip-offset-display 'lines)
1948 (setq limit (company-tooltip--lines-update-offset selection len limit))
1949 (company-tooltip--simple-update-offset selection len limit))
1952 ((eq company-tooltip-offset-display 'scrollbar)
1953 (setq scrollbar-bounds (company--scrollbar-bounds company-tooltip-offset
1955 ((eq company-tooltip-offset-display 'lines)
1956 (when (> company-tooltip-offset 0)
1957 (setq previous (format "...(%d)" company-tooltip-offset)))
1958 (setq remainder (- len limit company-tooltip-offset)
1959 remainder (when (> remainder 0)
1960 (setq remainder (format "...(%d)" remainder))))))
1962 (decf selection company-tooltip-offset)
1963 (setq width (max (length previous) (length remainder))
1964 lines (nthcdr company-tooltip-offset company-candidates)
1968 (decf window-width (* 2 company-tooltip-margin))
1969 (when scrollbar-bounds (decf window-width))
1972 (let* ((value (pop lines-copy))
1973 (annotation (company-call-backend 'annotation value)))
1974 (push (cons value annotation) items)
1975 (setq width (max (+ (length value) (length annotation)) width))))
1977 (setq width (min window-width
1978 (if (and company-show-numbers
1979 (< company-tooltip-offset 10))
1983 ;; number can make tooltip too long
1984 (when company-show-numbers
1985 (setq numbered company-tooltip-offset))
1987 (let ((items (nreverse items)) new)
1989 (push (company--scrollpos-line previous width) new))
1992 (let* ((item (pop items))
1993 (str (company-reformat (car item)))
1994 (annotation (cdr item))
1995 (right (company-space-string company-tooltip-margin))
1997 (when (< numbered 10)
2000 (setq right (concat (format " %d" (mod numbered 10)) right)))
2002 (company-fill-propertize str annotation
2003 width (equal i selection)
2004 (company-space-string
2005 company-tooltip-margin)
2007 (when scrollbar-bounds
2008 (company--scrollbar i scrollbar-bounds)))
2012 (push (company--scrollpos-line remainder width) new))
2016 (defun company--scrollbar-bounds (offset limit length)
2017 (when (> length limit)
2018 (let* ((size (ceiling (* limit (float limit)) length))
2019 (lower (floor (* limit (float offset)) length))
2020 (upper (+ lower size -1)))
2021 (cons lower upper))))
2023 (defun company--scrollbar (i bounds)
2024 (propertize " " 'face
2025 (if (and (>= i (car bounds)) (<= i (cdr bounds)))
2026 'company-scrollbar-fg
2027 'company-scrollbar-bg)))
2029 (defun company--scrollpos-line (text width)
2030 (propertize (concat (company-space-string company-tooltip-margin)
2031 (company-safe-substring text 0 width)
2032 (company-space-string company-tooltip-margin))
2033 'face 'company-tooltip))
2037 (defsubst company--window-inner-height ()
2038 (let ((edges (window-inside-edges)))
2039 (- (nth 3 edges) (nth 1 edges))))
2041 (defsubst company--window-width ()
2044 ((display-graphic-p) 0)
2045 ;; Account for the line continuation column.
2046 ((version< "24.3.1" emacs-version) 1)
2047 ;; Emacs 24.3 and earlier included margins
2048 ;; in window-width when in TTY.
2049 (t (1+ (let ((margins (window-margins)))
2050 (+ (or (car margins) 0)
2051 (or (cdr margins) 0))))))))
2053 (defun company--pseudo-tooltip-height ()
2054 "Calculate the appropriate tooltip height.
2055 Returns a negative number if the tooltip should be displayed above point."
2056 (let* ((lines (company--row))
2057 (below (- (company--window-inner-height) 1 lines)))
2058 (if (and (< below (min company-tooltip-minimum company-candidates-length))
2060 (- (max 3 (min company-tooltip-limit lines)))
2061 (max 3 (min company-tooltip-limit below)))))
2063 (defun company-pseudo-tooltip-show (row column selection)
2064 (company-pseudo-tooltip-hide)
2067 (let* ((height (company--pseudo-tooltip-height))
2071 (setq row (+ row height -1)
2074 (let* ((nl (< (move-to-window-line row) row))
2076 (end (save-excursion
2077 (move-to-window-line (+ row (abs height)))
2079 (ov (make-overlay beg end))
2080 (args (list (mapcar 'company-plainify
2081 (company-buffer-lines beg end))
2084 (setq company-pseudo-tooltip-overlay ov)
2085 (overlay-put ov 'company-replacement-args args)
2087 (let ((lines (company--create-lines selection (abs height))))
2088 (overlay-put ov 'company-after
2089 (apply 'company--replacement-string lines args))
2090 (overlay-put ov 'company-width (string-width (car lines))))
2092 (overlay-put ov 'company-column column)
2093 (overlay-put ov 'company-height height)))))
2095 (defun company-pseudo-tooltip-show-at-point (pos)
2096 (let ((row (company--row pos))
2097 (col (company--column pos)))
2098 (company-pseudo-tooltip-show (1+ row) col company-selection)))
2100 (defun company-pseudo-tooltip-edit (selection)
2101 (let ((height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
2102 (overlay-put company-pseudo-tooltip-overlay 'company-after
2103 (apply 'company--replacement-string
2104 (company--create-lines selection (abs height))
2105 (overlay-get company-pseudo-tooltip-overlay
2106 'company-replacement-args)))))
2108 (defun company-pseudo-tooltip-hide ()
2109 (when company-pseudo-tooltip-overlay
2110 (delete-overlay company-pseudo-tooltip-overlay)
2111 (setq company-pseudo-tooltip-overlay nil)))
2113 (defun company-pseudo-tooltip-hide-temporarily ()
2114 (when (overlayp company-pseudo-tooltip-overlay)
2115 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
2116 (overlay-put company-pseudo-tooltip-overlay 'line-prefix nil)
2117 (overlay-put company-pseudo-tooltip-overlay 'after-string nil)))
2119 (defun company-pseudo-tooltip-unhide ()
2120 (when company-pseudo-tooltip-overlay
2121 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
2122 ;; Beat outline's folding overlays, at least.
2123 (overlay-put company-pseudo-tooltip-overlay 'priority 1)
2124 ;; No (extra) prefix for the first line.
2125 (overlay-put company-pseudo-tooltip-overlay 'line-prefix "")
2126 (overlay-put company-pseudo-tooltip-overlay 'after-string
2127 (overlay-get company-pseudo-tooltip-overlay 'company-after))
2128 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
2130 (defun company-pseudo-tooltip-guard ()
2131 (buffer-substring-no-properties
2132 (point) (overlay-start company-pseudo-tooltip-overlay)))
2134 (defun company-pseudo-tooltip-frontend (command)
2135 "`company-mode' front-end similar to a tooltip but based on overlays."
2137 (pre-command (company-pseudo-tooltip-hide-temporarily))
2139 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
2140 (overlay-get company-pseudo-tooltip-overlay
2143 (new-height (company--pseudo-tooltip-height)))
2144 (unless (and (>= (* old-height new-height) 0)
2145 (>= (abs old-height) (abs new-height))
2146 (equal (company-pseudo-tooltip-guard)
2147 (overlay-get company-pseudo-tooltip-overlay
2150 (company-pseudo-tooltip-show-at-point (- (point)
2151 (length company-prefix)))
2152 (overlay-put company-pseudo-tooltip-overlay
2153 'company-guard (company-pseudo-tooltip-guard))))
2154 (company-pseudo-tooltip-unhide))
2155 (hide (company-pseudo-tooltip-hide)
2156 (setq company-tooltip-offset 0))
2157 (update (when (overlayp company-pseudo-tooltip-overlay)
2158 (company-pseudo-tooltip-edit company-selection)))))
2160 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
2161 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
2162 (unless (and (eq command 'post-command)
2163 (company--show-inline-p))
2164 (company-pseudo-tooltip-frontend command)))
2166 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2168 (defvar company-preview-overlay nil)
2169 (make-variable-buffer-local 'company-preview-overlay)
2171 (defun company-preview-show-at-point (pos)
2172 (company-preview-hide)
2174 (setq company-preview-overlay (make-overlay pos (1+ pos)))
2176 (let ((completion (nth company-selection company-candidates)))
2177 (setq completion (propertize completion 'face 'company-preview))
2178 (add-text-properties 0 (length company-common)
2179 '(face company-preview-common) completion)
2181 ;; Add search string
2182 (and company-search-string
2183 (string-match (regexp-quote company-search-string) completion)
2184 (add-text-properties (match-beginning 0)
2186 '(face company-preview-search)
2189 (setq completion (company-strip-prefix completion))
2191 (and (equal pos (point))
2192 (not (equal completion ""))
2193 (add-text-properties 0 1 '(cursor t) completion))
2195 (overlay-put company-preview-overlay 'display
2196 (concat completion (unless (eq pos (point-max))
2197 (buffer-substring pos (1+ pos)))))
2198 (overlay-put company-preview-overlay 'window (selected-window))))
2200 (defun company-preview-hide ()
2201 (when company-preview-overlay
2202 (delete-overlay company-preview-overlay)
2203 (setq company-preview-overlay nil)))
2205 (defun company-preview-frontend (command)
2206 "`company-mode' front-end showing the selection as if it had been inserted."
2208 (pre-command (company-preview-hide))
2209 (post-command (company-preview-show-at-point (point)))
2210 (hide (company-preview-hide))))
2212 (defun company-preview-if-just-one-frontend (command)
2213 "`company-preview-frontend', but only shown for single candidates."
2214 (when (or (not (eq command 'post-command))
2215 (company--show-inline-p))
2216 (company-preview-frontend command)))
2218 (defun company--show-inline-p ()
2219 (and (not (cdr company-candidates))
2221 (string-prefix-p company-prefix company-common
2222 (company-call-backend 'ignore-case))))
2224 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2226 (defvar company-echo-last-msg nil)
2227 (make-variable-buffer-local 'company-echo-last-msg)
2229 (defvar company-echo-timer nil)
2231 (defvar company-echo-delay .01)
2233 (defun company-echo-show (&optional getter)
2235 (setq company-echo-last-msg (funcall getter)))
2236 (let ((message-log-max nil))
2237 (if company-echo-last-msg
2238 (message "%s" company-echo-last-msg)
2241 (defun company-echo-show-soon (&optional getter)
2242 (when company-echo-timer
2243 (cancel-timer company-echo-timer))
2244 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
2246 (defsubst company-echo-show-when-idle (&optional getter)
2247 (when (sit-for company-echo-delay)
2248 (company-echo-show getter)))
2250 (defun company-echo-format ()
2252 (let ((limit (window-width (minibuffer-window)))
2254 ;; Roll to selection.
2255 (candidates (nthcdr company-selection company-candidates))
2256 (i (if company-show-numbers company-selection 99999))
2260 (setq comp (company-reformat (pop candidates))
2261 len (+ len 1 (length comp)))
2265 (setq comp (propertize (format "%d: %s" i comp)
2266 'face 'company-echo))
2269 (add-text-properties 3 (+ 3 (length company-common))
2270 '(face company-echo-common) comp))
2271 (setq comp (propertize comp 'face 'company-echo))
2272 (add-text-properties 0 (length company-common)
2273 '(face company-echo-common) comp))
2275 (setq candidates nil)
2278 (mapconcat 'identity (nreverse msg) " ")))
2280 (defun company-echo-strip-common-format ()
2282 (let ((limit (window-width (minibuffer-window)))
2283 (len (+ (length company-prefix) 2))
2284 ;; Roll to selection.
2285 (candidates (nthcdr company-selection company-candidates))
2286 (i (if company-show-numbers company-selection 99999))
2290 (setq comp (company-strip-prefix (pop candidates))
2291 len (+ len 2 (length comp)))
2294 (setq comp (format "%s (%d)" comp i))
2298 (setq candidates nil)
2299 (push (propertize comp 'face 'company-echo) msg)))
2301 (concat (propertize company-prefix 'face 'company-echo-common) "{"
2302 (mapconcat 'identity (nreverse msg) ", ")
2305 (defun company-echo-hide ()
2306 (unless (equal company-echo-last-msg "")
2307 (setq company-echo-last-msg "")
2308 (company-echo-show)))
2310 (defun company-echo-frontend (command)
2311 "`company-mode' front-end showing the candidates in the echo area."
2313 (post-command (company-echo-show-soon 'company-echo-format))
2314 (hide (company-echo-hide))))
2316 (defun company-echo-strip-common-frontend (command)
2317 "`company-mode' front-end showing the candidates in the echo area."
2319 (post-command (company-echo-show-soon 'company-echo-strip-common-format))
2320 (hide (company-echo-hide))))
2322 (defun company-echo-metadata-frontend (command)
2323 "`company-mode' front-end showing the documentation in the echo area."
2325 (post-command (company-echo-show-when-idle 'company-fetch-metadata))
2326 (hide (company-echo-hide))))
2329 ;;; company.el ends here