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-scrollbar-fg
127 '((((background light))
128 :background "darkred")
131 "Face used for the tooltip scrollbar thumb.")
133 (defface company-scrollbar-bg
134 '((default :inherit company-tooltip)
135 (((background light))
139 "Face used for the tooltip scrollbar background.")
141 (defface company-preview
142 '((((background light))
143 :inherit company-tooltip-selection)
146 :foreground "wheat"))
147 "Face used for the completion preview.")
149 (defface company-preview-common
150 '((((background light))
151 :inherit company-tooltip-selection)
153 :inherit company-preview
155 "Face used for the common part of the completion preview.")
157 (defface company-preview-search
158 '((((background light))
159 :inherit company-tooltip-common-selection)
161 :inherit company-preview
162 :background "blue1"))
163 "Face used for the search string in the completion preview.")
165 (defface company-echo nil
166 "Face used for completions in the echo area.")
168 (defface company-echo-common
169 '((((background dark)) (:foreground "firebrick1"))
170 (((background light)) (:background "firebrick4")))
171 "Face used for the common part of completions in the echo area.")
173 (defun company-frontends-set (variable value)
175 (let ((remainder value))
176 (setcdr remainder (delq (car remainder) (cdr remainder))))
177 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
178 (memq 'company-pseudo-tooltip-frontend value)
179 (error "Pseudo tooltip frontend cannot be used twice"))
180 (and (memq 'company-preview-if-just-one-frontend value)
181 (memq 'company-preview-frontend value)
182 (error "Preview frontend cannot be used twice"))
183 (and (memq 'company-echo value)
184 (memq 'company-echo-metadata-frontend value)
185 (error "Echo area cannot be used twice"))
186 ;; preview must come last
187 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
189 (setq value (append (delq f value) (list f)))))
190 (set variable value))
192 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
193 company-preview-if-just-one-frontend
194 company-echo-metadata-frontend)
195 "The list of active front-ends (visualizations).
196 Each front-end is a function that takes one argument. It is called with
197 one of the following arguments:
199 `show': When the visualization should start.
201 `hide': When the visualization should end.
203 `update': When the data has been updated.
205 `pre-command': Before every command that is executed while the
206 visualization is active.
208 `post-command': After every command that is executed while the
209 visualization is active.
211 The visualized data is stored in `company-prefix', `company-candidates',
212 `company-common', `company-selection', `company-point' and
213 `company-search-string'."
214 :set 'company-frontends-set
215 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
216 (const :tag "echo, strip common"
217 company-echo-strip-common-frontend)
218 (const :tag "show echo meta-data in echo"
219 company-echo-metadata-frontend)
220 (const :tag "pseudo tooltip"
221 company-pseudo-tooltip-frontend)
222 (const :tag "pseudo tooltip, multiple only"
223 company-pseudo-tooltip-unless-just-one-frontend)
224 (const :tag "preview" company-preview-frontend)
225 (const :tag "preview, unique only"
226 company-preview-if-just-one-frontend)
227 (function :tag "custom function" nil))))
229 (defcustom company-tooltip-limit 10
230 "The maximum number of candidates in the tooltip"
233 (defcustom company-tooltip-minimum 6
234 "The minimum height of the tooltip.
235 If this many lines are not available, prefer to display the tooltip above."
238 (defcustom company-tooltip-margin 1
239 "Width of margin columns to show around the toolip."
242 (defcustom company-tooltip-offset-display 'scrollbar
243 "Method using which the tooltip displays scrolling position.
244 `scrollbar' means draw a scrollbar to the right of the items.
245 `lines' means wrap items in lines with \"before\" and \"after\" counters."
246 :type '(choice (const :tag "Scrollbar" scrollbar)
247 (const :tag "Two lines" lines)))
249 (defvar company-safe-backends
250 '((company-abbrev . "Abbrev")
251 (company-capf . "completion-at-point-functions")
252 (company-clang . "Clang")
253 (company-cmake . "CMake")
254 (company-css . "CSS")
255 (company-dabbrev . "dabbrev for plain text")
256 (company-dabbrev-code . "dabbrev for code")
257 (company-eclim . "Eclim (an Eclipse interface)")
258 (company-elisp . "Emacs Lisp")
259 (company-etags . "etags")
260 (company-files . "Files")
261 (company-gtags . "GNU Global")
262 (company-ispell . "Ispell")
263 (company-keywords . "Programming language keywords")
264 (company-nxml . "nxml")
265 (company-oddmuse . "Oddmuse")
266 (company-pysmell . "PySmell")
267 (company-ropemacs . "ropemacs")
268 (company-semantic . "Semantic")
269 (company-tempo . "Tempo templates")
270 (company-xcode . "Xcode")))
271 (put 'company-safe-backends 'risky-local-variable t)
273 (defun company-safe-backends-p (backends)
274 (and (consp backends)
275 (not (dolist (backend backends)
276 (unless (if (consp backend)
277 (company-safe-backends-p backend)
278 (assq backend company-safe-backends))
281 (defvar company--include-capf (version< "24.3.50" emacs-version))
283 (defcustom company-backends `(,@(unless company--include-capf
284 (list 'company-elisp))
285 company-nxml company-css
286 company-eclim company-semantic company-clang
287 company-xcode company-ropemacs company-cmake
288 ,@(when company--include-capf
289 (list 'company-capf))
290 (company-gtags company-etags company-dabbrev-code
292 company-oddmuse company-files company-dabbrev)
293 "The list of active back-ends (completion engines).
294 Each list elements can itself be a list of back-ends. In that case their
295 completions are merged. Otherwise only the first matching back-end returns
298 `company-begin-backend' can be used to start a specific back-end,
299 `company-other-backend' will skip to the next matching back-end in the list.
301 Each back-end is a function that takes a variable number of arguments.
302 The first argument is the command requested from the back-end. It is one
305 `prefix': The back-end should return the text to be completed. It must be
306 text immediately before point. Returning nil passes control to the next
307 back-end. The function should return `stop' if it should complete but
308 cannot \(e.g. if it is in the middle of a string\). Instead of a string,
309 the back-end may return a cons where car is the prefix and cdr is used in
310 `company-minimum-prefix-length' test. It's either number or t, in which
311 case the test automatically succeeds.
313 `candidates': The second argument is the prefix to be completed. The
314 return value should be a list of candidates that start with the prefix.
318 `sorted': Return t here to indicate that the candidates are sorted and will
319 not need to be sorted again.
321 `duplicates': If non-nil, company will take care of removing duplicates
324 `no-cache': Usually company doesn't ask for candidates again as completion
325 progresses, unless the back-end returns t for this command. The second
326 argument is the latest prefix.
328 `meta': The second argument is a completion candidate. Return a (short)
329 documentation string for it.
331 `doc-buffer': The second argument is a completion candidate. Return a
332 buffer with documentation for it. Preferably use `company-doc-buffer',
334 `location': The second argument is a completion candidate. Return the cons
335 of buffer and buffer location, or of file and line number where the
336 completion candidate was defined.
338 `require-match': If this returns t, the user is not allowed to enter
339 anything not offered as a candidate. Use with care! The default value nil
340 gives the user that choice with `company-require-match'. Return value
341 `never' overrides that option the other way around.
343 `init': Called once for each buffer. The back-end can check for external
344 programs and files and load any required libraries. Raising an error here
345 will show up in message log once, and the back-end will not be used for
348 `post-completion': Called after a completion candidate has been inserted
349 into the buffer. The second argument is the candidate. Can be used to
350 modify it, e.g. to expand a snippet.
352 The back-end should return nil for all commands it does not support or
353 does not know about. It should also be callable interactively and use
354 `company-begin-backend' to start itself in that case."
358 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
359 company-safe-backends)
360 (symbol :tag "User defined")
361 (repeat :tag "Merged Back-ends"
362 (choice :tag "Back-end"
363 ,@(mapcar (lambda (b)
364 `(const :tag ,(cdr b) ,(car b)))
365 company-safe-backends)
366 (symbol :tag "User defined"))))))
368 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
370 (defcustom company-transformers nil
371 "Functions to change the list of candidates received from backends,
372 after sorting and removal of duplicates (if appropriate).
373 Each function gets called with the return value of the previous one."
375 (const :tag "None" nil)
376 (const :tag "Sort by occurrence" (company-sort-by-occurrence))
377 (repeat :tag "User defined" (function))))
379 (defcustom company-completion-started-hook nil
380 "Hook run when company starts completing.
381 The hook is called with one argument that is non-nil if the completion was
385 (defcustom company-completion-cancelled-hook nil
386 "Hook run when company cancels completing.
387 The hook is called with one argument that is non-nil if the completion was
391 (defcustom company-completion-finished-hook nil
392 "Hook run when company successfully completes.
393 The hook is called with the selected candidate as an argument.
395 If you indend to use it to post-process candidates from a specific
396 back-end, consider using the `post-completion' command instead."
399 (defcustom company-minimum-prefix-length 3
400 "The minimum prefix length for idle completion."
401 :type '(integer :tag "prefix length"))
403 (defcustom company-require-match 'company-explicit-action-p
404 "If enabled, disallow non-matching input.
405 This can be a function do determine if a match is required.
407 This can be overridden by the back-end, if it returns t or `never' to
408 `require-match'. `company-auto-complete' also takes precedence over this."
409 :type '(choice (const :tag "Off" nil)
410 (function :tag "Predicate function")
411 (const :tag "On, if user interaction took place"
412 'company-explicit-action-p)
413 (const :tag "On" t)))
415 (defcustom company-auto-complete nil
416 "Determines when to auto-complete.
417 If this is enabled, all characters from `company-auto-complete-chars'
418 trigger insertion of the selected completion candidate.
419 This can also be a function."
420 :type '(choice (const :tag "Off" nil)
421 (function :tag "Predicate function")
422 (const :tag "On, if user interaction took place"
423 'company-explicit-action-p)
424 (const :tag "On" t)))
426 (defcustom company-auto-complete-chars '(?\ ?\) ?.)
427 "Determines which characters trigger auto-completion.
428 See `company-auto-complete'. If this is a string, each string character
429 tiggers auto-completion. If it is a list of syntax description characters (see
430 `modify-syntax-entry'), all characters with that syntax auto-complete.
432 This can also be a function, which is called with the new input and should
433 return non-nil if company should auto-complete.
435 A character that is part of a valid candidate never triggers auto-completion."
436 :type '(choice (string :tag "Characters")
438 (const :tag "Whitespace" ?\ )
439 (const :tag "Symbol" ?_)
440 (const :tag "Opening parentheses" ?\()
441 (const :tag "Closing parentheses" ?\))
442 (const :tag "Word constituent" ?w)
443 (const :tag "Punctuation." ?.)
444 (const :tag "String quote." ?\")
445 (const :tag "Paired delimiter." ?$)
446 (const :tag "Expression quote or prefix operator." ?\')
447 (const :tag "Comment starter." ?<)
448 (const :tag "Comment ender." ?>)
449 (const :tag "Character-quote." ?/)
450 (const :tag "Generic string fence." ?|)
451 (const :tag "Generic comment fence." ?!))
452 (function :tag "Predicate function")))
454 (defcustom company-idle-delay .7
455 "The idle delay in seconds until completion starts automatically.
456 A value of nil means no idle completion, t means show candidates
457 immediately when a prefix of `company-minimum-prefix-length' is reached."
458 :type '(choice (const :tag "never (nil)" nil)
459 (const :tag "immediate (t)" t)
460 (number :tag "seconds")))
462 (defcustom company-begin-commands '(self-insert-command org-self-insert-command)
463 "A list of commands after which idle completion is allowed.
464 If this is t, it can show completions after any command. See
465 `company-idle-delay'.
467 Alternatively, any command with a non-nil `company-begin' property is
468 treated as if it was on this list."
469 :type '(choice (const :tag "Any command" t)
470 (const :tag "Self insert command" '(self-insert-command))
471 (repeat :tag "Commands" function)))
473 (defcustom company-show-numbers nil
474 "If enabled, show quick-access numbers for the first ten candidates."
475 :type '(choice (const :tag "off" nil)
476 (const :tag "on" t)))
478 (defcustom company-selection-wrap-around nil
479 "If enabled, selecting item before first or after last wraps around."
480 :type '(choice (const :tag "off" nil)
481 (const :tag "on" t)))
483 (defvar company-end-of-buffer-workaround t
484 "Work around a visualization bug when completing at the end of the buffer.
485 The work-around consists of adding a newline.")
487 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
489 (defvar company-mode-map (make-sparse-keymap)
490 "Keymap used by `company-mode'.")
492 (defvar company-active-map
493 (let ((keymap (make-sparse-keymap)))
494 (define-key keymap "\e\e\e" 'company-abort)
495 (define-key keymap "\C-g" 'company-abort)
496 (define-key keymap (kbd "M-n") 'company-select-next)
497 (define-key keymap (kbd "M-p") 'company-select-previous)
498 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
499 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
500 (define-key keymap [down-mouse-1] 'ignore)
501 (define-key keymap [down-mouse-3] 'ignore)
502 (define-key keymap [mouse-1] 'company-complete-mouse)
503 (define-key keymap [mouse-3] 'company-select-mouse)
504 (define-key keymap [up-mouse-1] 'ignore)
505 (define-key keymap [up-mouse-3] 'ignore)
506 (define-key keymap [return] 'company-complete-selection)
507 (define-key keymap (kbd "RET") 'company-complete-selection)
508 (define-key keymap [tab] 'company-complete-common)
509 (define-key keymap (kbd "TAB") 'company-complete-common)
510 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
511 (define-key keymap "\C-w" 'company-show-location)
512 (define-key keymap "\C-s" 'company-search-candidates)
513 (define-key keymap "\C-\M-s" 'company-filter-candidates)
515 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
516 `(lambda () (interactive) (company-complete-number ,i))))
519 "Keymap that is enabled during an active completion.")
521 (defvar company--disabled-backends nil)
523 (defun company-init-backend (backend)
524 (and (symbolp backend)
525 (not (fboundp backend))
526 (ignore-errors (require backend nil t)))
528 (if (or (symbolp backend)
532 (funcall backend 'init)
533 (put backend 'company-init t))
535 (put backend 'company-init 'failed)
536 (unless (memq backend company--disabled-backends)
537 (message "Company back-end '%s' could not be initialized:\n%s"
538 backend (error-message-string err)))
539 (pushnew backend company--disabled-backends)
541 (mapc 'company-init-backend backend)))
543 (defvar company-default-lighter " company")
545 (defvar company-lighter company-default-lighter)
546 (make-variable-buffer-local 'company-lighter)
549 (define-minor-mode company-mode
550 "\"complete anything\"; is an in-buffer completion framework.
551 Completion starts automatically, depending on the values
552 `company-idle-delay' and `company-minimum-prefix-length'.
554 Completion can be controlled with the commands:
555 `company-complete-common', `company-complete-selection', `company-complete',
556 `company-select-next', `company-select-previous'. If these commands are
557 called before `company-idle-delay', completion will also start.
559 Completions can be searched with `company-search-candidates' or
560 `company-filter-candidates'. These can be used while completion is
563 The completion data is retrieved using `company-backends' and displayed
564 using `company-frontends'. If you want to start a specific back-end, call
565 it interactively or use `company-begin-backend'.
567 regular keymap (`company-mode-map'):
570 keymap during active completions (`company-active-map'):
572 \\{company-active-map}"
573 nil company-lighter company-mode-map
576 (add-hook 'pre-command-hook 'company-pre-command nil t)
577 (add-hook 'post-command-hook 'company-post-command nil t)
578 (mapc 'company-init-backend company-backends))
579 (remove-hook 'pre-command-hook 'company-pre-command t)
580 (remove-hook 'post-command-hook 'company-post-command t)
582 (kill-local-variable 'company-point)))
584 (defcustom company-global-modes t
585 "Modes for which `company-mode' mode is turned on by `global-company-mode'.
586 If nil, means no modes. If t, then all major modes have it turned on.
587 If a list, it should be a list of `major-mode' symbol names for which
588 `company-mode' should be automatically turned on. The sense of the list is
589 negated if it begins with `not'. For example:
591 means that `company-mode' is turned on for buffers in C and C++ modes only.
593 means that `company-mode' is always turned on except in `message-mode' buffers."
594 :type '(choice (const :tag "none" nil)
596 (set :menu-tag "mode specific" :tag "modes"
598 (const :tag "Except" not)
599 (repeat :inline t (symbol :tag "mode")))))
602 (define-globalized-minor-mode global-company-mode company-mode company-mode-on)
604 (defun company-mode-on ()
605 (when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s)))
606 (cond ((eq company-global-modes t)
608 ((eq (car-safe company-global-modes) 'not)
609 (not (memq major-mode (cdr company-global-modes))))
610 (t (memq major-mode company-global-modes))))
613 (defsubst company-assert-enabled ()
615 (company-uninstall-map)
616 (error "Company not enabled")))
618 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 (defvar company-my-keymap nil)
621 (make-variable-buffer-local 'company-my-keymap)
623 (defvar company-emulation-alist '((t . nil)))
625 (defsubst company-enable-overriding-keymap (keymap)
626 (company-uninstall-map)
627 (setq company-my-keymap keymap))
629 (defun company-ensure-emulation-alist ()
630 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
631 (setq emulation-mode-map-alists
632 (cons 'company-emulation-alist
633 (delq 'company-emulation-alist emulation-mode-map-alists)))))
635 (defun company-install-map ()
636 (unless (or (cdar company-emulation-alist)
637 (null company-my-keymap))
638 (setf (cdar company-emulation-alist) company-my-keymap)))
640 (defun company-uninstall-map ()
641 (setf (cdar company-emulation-alist) nil))
644 ;; Emacs calculates the active keymaps before reading the event. That means we
645 ;; cannot change the keymap from a timer. So we send a bogus command.
646 ;; XXX: Seems not to be needed anymore in Emacs 24.4
647 (defun company-ignore ()
649 (setq this-command last-command))
651 (global-set-key '[31415926] 'company-ignore)
653 (defun company-input-noop ()
654 (push 31415926 unread-command-events))
656 (defun company--column (&optional pos)
658 (when pos (goto-char pos))
662 (narrow-to-region (point) (point-max))
663 (let ((prefix (get-text-property (point) 'line-prefix)))
664 (if prefix (length prefix) 0)))
667 (defun company--row (&optional pos)
669 (when pos (goto-char pos))
670 (count-screen-lines (window-start)
671 (progn (vertical-motion 0) (point)))))
673 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
675 (defvar company-backend nil)
676 (make-variable-buffer-local 'company-backend)
678 (defun company-grab (regexp &optional expression limit)
679 (when (looking-back regexp limit)
680 (or (match-string-no-properties (or expression 0)) "")))
682 (defun company-grab-line (regexp &optional expression)
683 (company-grab regexp expression (point-at-bol)))
685 (defun company-grab-symbol ()
686 (if (looking-at "\\_>")
687 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
689 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
692 (defun company-grab-word ()
693 (if (looking-at "\\>")
694 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
696 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
699 (defun company-in-string-or-comment ()
700 (let ((ppss (syntax-ppss)))
701 (or (car (setq ppss (nthcdr 3 ppss)))
702 (car (setq ppss (cdr ppss)))
705 (if (fboundp 'locate-dominating-file)
706 (defalias 'company-locate-dominating-file 'locate-dominating-file)
707 (defun company-locate-dominating-file (file name)
709 (let ((dir (file-name-directory file))
711 (while (not (equal dir prev-dir))
712 (when (file-exists-p (expand-file-name name dir))
715 dir (file-name-directory (directory-file-name dir))))))))
717 (defun company-call-backend (&rest args)
718 (if (functionp company-backend)
719 (apply company-backend args)
720 (apply 'company--multi-backend-adapter company-backend args)))
722 (defun company--multi-backend-adapter (backends command &rest args)
723 (let ((backends (loop for b in backends
724 when (not (and (symbolp b)
725 (eq 'failed (get b 'company-init))))
729 (loop for backend in backends
730 when (equal (funcall backend 'prefix)
732 append (apply backend 'candidates args)))
737 (dolist (backend backends)
738 (when (setq value (apply backend command args))
739 (return value))))))))
741 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
743 (defvar company-prefix nil)
744 (make-variable-buffer-local 'company-prefix)
746 (defvar company-candidates nil)
747 (make-variable-buffer-local 'company-candidates)
749 (defvar company-candidates-length nil)
750 (make-variable-buffer-local 'company-candidates-length)
752 (defvar company-candidates-cache nil)
753 (make-variable-buffer-local 'company-candidates-cache)
755 (defvar company-candidates-predicate nil)
756 (make-variable-buffer-local 'company-candidates-predicate)
758 (defvar company-common nil)
759 (make-variable-buffer-local 'company-common)
761 (defvar company-selection 0)
762 (make-variable-buffer-local 'company-selection)
764 (defvar company-selection-changed nil)
765 (make-variable-buffer-local 'company-selection-changed)
767 (defvar company--explicit-action nil
768 "Non-nil, if explicit completion took place.")
769 (make-variable-buffer-local 'company--explicit-action)
771 (defvar company--auto-completion nil
772 "Non-nil when current candidate is being inserted automatically.
773 Controlled by `company-auto-complete'.")
775 (defvar company--point-max nil)
776 (make-variable-buffer-local 'company--point-max)
778 (defvar company-point nil)
779 (make-variable-buffer-local 'company-point)
781 (defvar company-timer nil)
783 (defvar company-added-newline nil)
784 (make-variable-buffer-local 'company-added-newline)
786 (defsubst company-strip-prefix (str)
787 (substring str (length company-prefix)))
789 (defun company--insert-candidate (candidate)
790 ;; XXX: Return value we check here is subject to change.
791 (set-text-properties 0 (length candidate) nil candidate)
792 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
793 (insert (company-strip-prefix candidate))
794 (delete-region (- (point) (length company-prefix)) (point))
797 (defmacro company-with-candidate-inserted (candidate &rest body)
798 "Evaluate BODY with CANDIDATE temporarily inserted.
799 This is a tool for back-ends that need candidates inserted before they
800 can retrieve meta-data for them."
802 `(let ((inhibit-modification-hooks t)
803 (inhibit-point-motion-hooks t)
804 (modified-p (buffer-modified-p)))
805 (company--insert-candidate ,candidate)
808 (delete-region company-point (point)))))
810 (defun company-explicit-action-p ()
811 "Return whether explicit completion action was taken by the user."
812 (or company--explicit-action
813 company-selection-changed))
815 (defun company-reformat (candidate)
816 ;; company-ispell needs this, because the results are always lower-case
817 ;; It's mory efficient to fix it only when they are displayed.
818 ;; FIXME: Adopt the current text's capitalization instead?
819 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
820 (concat company-prefix (substring candidate (length company-prefix)))
823 (defun company--should-complete ()
824 (and (not (or buffer-read-only overriding-terminal-local-map
825 overriding-local-map))
826 ;; Check if in the middle of entering a key combination.
827 (or (equal (this-command-keys-vector) [])
828 (not (keymapp (key-binding (this-command-keys-vector)))))
829 (eq company-idle-delay t)
830 (or (eq t company-begin-commands)
831 (memq this-command company-begin-commands)
832 (and (symbolp this-command) (get this-command 'company-begin)))
833 (not (and transient-mark-mode mark-active))))
835 (defun company-call-frontends (command)
836 (dolist (frontend company-frontends)
838 (funcall frontend command)
839 (error (error "Company: Front-end %s error \"%s\" on command %s"
840 frontend (error-message-string err) command)))))
842 (defun company-set-selection (selection &optional force-update)
844 (if company-selection-wrap-around
845 (mod selection company-candidates-length)
846 (max 0 (min (1- company-candidates-length) selection))))
847 (when (or force-update (not (equal selection company-selection)))
848 (setq company-selection selection
849 company-selection-changed t)
850 (company-call-frontends 'update)))
852 (defun company-apply-predicate (candidates predicate)
854 (dolist (c candidates)
855 (when (funcall predicate c)
859 (defun company-update-candidates (candidates)
860 (setq company-candidates-length (length candidates))
861 (if (> company-selection 0)
862 ;; Try to restore the selection
863 (let ((selected (nth company-selection company-candidates)))
864 (setq company-selection 0
865 company-candidates candidates)
867 (while (and candidates (string< (pop candidates) selected))
868 (incf company-selection))
870 ;; Make sure selection isn't out of bounds.
871 (setq company-selection (min (1- company-candidates-length)
872 company-selection)))))
873 (setq company-selection 0
874 company-candidates candidates))
876 (push (cons company-prefix company-candidates) company-candidates-cache)
878 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
879 ;; We want to support non-prefix completion, so filtering is the
880 ;; responsibility of each respective backend, not ours.
881 ;; On the other hand, we don't want to replace non-prefix input in
882 ;; `company-complete-common'.
884 (if (cdr company-candidates)
885 (company--safe-candidate
886 (try-completion company-prefix company-candidates))
887 (car company-candidates)))))
889 (defun company--safe-candidate (str)
890 (or (company-call-backend 'crop str)
893 (defun company-calculate-candidates (prefix)
894 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
895 (ignore-case (company-call-backend 'ignore-case)))
897 (when company-candidates-cache
898 (let ((len (length prefix))
899 (completion-ignore-case ignore-case)
901 (dotimes (i (1+ len))
902 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
903 company-candidates-cache)))
904 (setq candidates (all-completions prefix prev))
906 ;; no cache match, call back-end
908 (setq candidates (company-call-backend 'candidates prefix))
909 (when company-candidates-predicate
911 (company-apply-predicate candidates
912 company-candidates-predicate)))
913 (unless (company-call-backend 'sorted)
914 (setq candidates (sort candidates 'string<)))
915 (when (company-call-backend 'duplicates)
917 (let ((c2 candidates))
919 (setcdr c2 (progn (while (equal (pop c2) (car c2)))
921 (setq candidates (company--transform-candidates candidates))
923 (if (or (cdr candidates)
924 (not (eq t (compare-strings (car candidates) nil nil
925 prefix nil nil ignore-case))))
926 (company--transform-candidates candidates)
927 ;; Already completed and unique; don't start.
930 (defun company--transform-candidates (candidates)
931 (let ((c candidates))
932 (dolist (tr company-transformers)
933 (setq c (funcall tr c)))
936 (defun company-sort-by-occurrence (candidates)
937 "Sort CANDIDATES according to their occurrences.
938 Searches for each in the currently visible part of the current buffer and
939 gives priority to the closest ones above point, then closest ones below
940 point. The rest of the list is appended unchanged.
941 Keywords and function definition names are ignored."
948 (and (zerop (forward-line -1))
949 (search-backward candidate (window-start) t)))
951 (search-forward candidate (window-end) t)))
952 (let ((beg (match-beginning 0))
954 (when (save-excursion
956 (and (not (memq (get-text-property (point) 'face)
957 '(font-lock-function-name-face
958 font-lock-keyword-face)))
959 (let* ((prefix (company-call-backend 'prefix))
960 (prefix (or (car-safe prefix) prefix)))
961 (and (stringp prefix)
962 (= (length prefix) (- end beg))))))
963 (push (cons candidate (if (< beg (point))
965 (- beg (window-start))))
970 (mapcar #'car (sort occurs (lambda (e1 e2) (< (cdr e1) (cdr e2)))))
973 (defun company-idle-begin (buf win tick pos)
975 (eq buf (current-buffer))
976 (eq win (selected-window))
977 (eq tick (buffer-chars-modified-tick))
979 (not company-candidates)
980 (not (equal (point) company-point))
981 (let ((company-idle-delay t)
982 (company-begin-commands t))
984 (when company-candidates
985 (when (version< emacs-version "24.3.50")
986 (company-input-noop))
987 (company-post-command)))))
989 (defun company-auto-begin ()
990 (company-assert-enabled)
992 (not company-candidates)
993 (let ((company-idle-delay t)
994 (company-minimum-prefix-length 0)
995 (company-begin-commands t))
997 ;; Return non-nil if active.
1000 (defun company-manual-begin ()
1002 (setq company--explicit-action t)
1004 (company-auto-begin)
1005 (unless company-candidates
1006 (setq company--explicit-action nil))))
1008 (defun company-other-backend (&optional backward)
1009 (interactive (list current-prefix-arg))
1010 (company-assert-enabled)
1012 (let* ((after (cdr (member company-backend company-backends)))
1013 (before (cdr (member company-backend (reverse company-backends))))
1015 (append before (reverse after))
1016 (append after (reverse before)))))
1018 (dolist (backend next)
1019 (when (ignore-errors (company-begin-backend backend))
1021 (company-manual-begin))
1022 (unless company-candidates
1023 (error "No other back-end")))
1025 (defun company-require-match-p ()
1026 (let ((backend-value (company-call-backend 'require-match)))
1027 (or (eq backend-value t)
1028 (and (not (eq backend-value 'never))
1029 (if (functionp company-require-match)
1030 (funcall company-require-match)
1031 (eq company-require-match t))))))
1033 (defun company-auto-complete-p (input)
1034 "Return non-nil, if input starts with punctuation or parentheses."
1035 (and (if (functionp company-auto-complete)
1036 (funcall company-auto-complete)
1037 company-auto-complete)
1038 (if (functionp company-auto-complete-chars)
1039 (funcall company-auto-complete-chars input)
1040 (if (consp company-auto-complete-chars)
1041 (memq (char-syntax (string-to-char input))
1042 company-auto-complete-chars)
1043 (string-match (substring input 0 1) company-auto-complete-chars)))))
1045 (defun company--incremental-p ()
1046 (and (> (point) company-point)
1047 (> (point-max) company--point-max)
1048 (not (eq this-command 'backward-delete-char-untabify))
1049 (equal (buffer-substring (- company-point (length company-prefix))
1053 (defun company--continue-failed ()
1054 (when (company--incremental-p)
1055 (let ((input (buffer-substring-no-properties (point) company-point)))
1057 ((company-auto-complete-p input)
1060 (goto-char company-point)
1061 (let ((company--auto-completion t))
1062 (company-complete-selection))
1064 ((company-require-match-p)
1065 ;; wrong incremental input, but required match
1066 (delete-char (- (length input)))
1068 (message "Matching input is required")
1070 ((equal company-prefix (car company-candidates))
1071 ;; last input was actually success
1072 (company-cancel company-prefix)
1075 (defun company--good-prefix-p (prefix)
1076 (and (or (company-explicit-action-p)
1077 (unless (eq prefix 'stop)
1078 (or (eq (cdr-safe prefix) t)
1079 (>= (or (cdr-safe prefix) (length prefix))
1080 company-minimum-prefix-length))))
1081 (stringp (or (car-safe prefix) prefix))))
1083 (defun company--continue ()
1084 (when (company-call-backend 'no-cache company-prefix)
1085 ;; Don't complete existing candidates, fetch new ones.
1086 (setq company-candidates-cache nil))
1087 (let* ((new-prefix (company-call-backend 'prefix))
1088 (c (when (and (company--good-prefix-p new-prefix)
1089 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1090 (= (- (point) (length new-prefix))
1091 (- company-point (length company-prefix))))
1092 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1093 (company-calculate-candidates new-prefix))))
1096 ;; t means complete/unique.
1097 (company-cancel new-prefix)
1100 ;; incremental match
1101 (setq company-prefix new-prefix)
1102 (company-update-candidates c)
1104 (t (company--continue-failed)))
1107 (defun company--begin-new ()
1109 (dolist (backend (if company-backend
1110 ;; prefer manual override
1111 (list company-backend)
1114 (if (or (symbolp backend)
1115 (functionp backend))
1116 (when (or (not (symbolp backend))
1117 (eq t (get backend 'company-init))
1118 (unless (get backend 'company-init)
1119 (company-init-backend backend)))
1120 (funcall backend 'prefix))
1121 (company--multi-backend-adapter backend 'prefix)))
1123 (when (company--good-prefix-p prefix)
1124 (setq prefix (or (car-safe prefix) prefix)
1125 company-backend backend
1126 c (company-calculate-candidates prefix))
1127 ;; t means complete/unique. We don't start, so no hooks.
1129 (when company--explicit-action
1130 (message "No completion found"))
1131 (setq company-prefix prefix)
1132 (when (symbolp backend)
1133 (setq company-lighter (concat " " (symbol-name backend))))
1134 (company-update-candidates c)
1135 (run-hook-with-args 'company-completion-started-hook
1136 (company-explicit-action-p))
1137 (company-call-frontends 'show)))
1140 (defun company-begin ()
1141 (or (and company-candidates (company--continue))
1142 (and (company--should-complete) (company--begin-new)))
1143 (when company-candidates
1144 (when (and company-end-of-buffer-workaround (eobp))
1145 (save-excursion (insert "\n"))
1146 (setq company-added-newline (buffer-chars-modified-tick)))
1147 (setq company-point (point)
1148 company--point-max (point-max))
1149 (company-ensure-emulation-alist)
1150 (company-enable-overriding-keymap company-active-map)
1151 (company-call-frontends 'update)))
1153 (defun company-cancel (&optional result)
1154 (and company-added-newline
1155 (> (point-max) (point-min))
1156 (let ((tick (buffer-chars-modified-tick)))
1157 (delete-region (1- (point-max)) (point-max))
1158 (equal tick company-added-newline))
1159 ;; Only set unmodified when tick remained the same since insert.
1160 (set-buffer-modified-p nil))
1161 (when company-prefix
1162 (if (stringp result)
1164 (company-call-backend 'pre-completion result)
1165 (run-hook-with-args 'company-completion-finished-hook result)
1166 (company-call-backend 'post-completion result))
1167 (run-hook-with-args 'company-completion-cancelled-hook result)))
1168 (setq company-added-newline nil
1171 company-candidates nil
1172 company-candidates-length nil
1173 company-candidates-cache nil
1174 company-candidates-predicate nil
1177 company-selection-changed nil
1178 company--explicit-action nil
1179 company-lighter company-default-lighter
1180 company--point-max nil
1183 (cancel-timer company-timer))
1184 (company-search-mode 0)
1185 (company-call-frontends 'hide)
1186 (company-enable-overriding-keymap nil))
1188 (defun company-abort ()
1191 ;; Don't start again, unless started manually.
1192 (setq company-point (point)))
1194 (defun company-finish (result)
1195 (company--insert-candidate result)
1196 (company-cancel result)
1197 ;; Don't start again, unless started manually.
1198 (setq company-point (point)))
1200 (defsubst company-keep (command)
1201 (and (symbolp command) (get command 'company-keep)))
1203 (defun company-pre-command ()
1204 (unless (company-keep this-command)
1206 (when company-candidates
1207 (company-call-frontends 'pre-command))
1208 (error (message "Company: An error occurred in pre-command")
1209 (message "%s" (error-message-string err))
1212 (cancel-timer company-timer)
1213 (setq company-timer nil))
1214 (company-uninstall-map))
1216 (defun company-post-command ()
1217 (unless (company-keep this-command)
1220 (unless (equal (point) company-point)
1222 (if company-candidates
1223 (company-call-frontends 'post-command)
1224 (and (numberp company-idle-delay)
1225 (or (eq t company-begin-commands)
1226 (memq this-command company-begin-commands))
1228 (run-with-timer company-idle-delay nil
1230 (current-buffer) (selected-window)
1231 (buffer-chars-modified-tick) (point))))))
1232 (error (message "Company: An error occurred in post-command")
1233 (message "%s" (error-message-string err))
1235 (company-install-map))
1237 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1239 (defvar company-search-string nil)
1240 (make-variable-buffer-local 'company-search-string)
1242 (defvar company-search-lighter " Search: \"\"")
1243 (make-variable-buffer-local 'company-search-lighter)
1245 (defvar company-search-old-map nil)
1246 (make-variable-buffer-local 'company-search-old-map)
1248 (defvar company-search-old-selection 0)
1249 (make-variable-buffer-local 'company-search-old-selection)
1251 (defun company-search (text lines)
1252 (let ((quoted (regexp-quote text))
1254 (dolist (line lines)
1255 (when (string-match quoted line (length company-prefix))
1259 (defun company-search-printing-char ()
1261 (company-search-assert-enabled)
1262 (setq company-search-string
1263 (concat (or company-search-string "") (string last-command-event))
1264 company-search-lighter (concat " Search: \"" company-search-string
1266 (let ((pos (company-search company-search-string
1267 (nthcdr company-selection company-candidates))))
1270 (company-set-selection (+ company-selection pos) t))))
1272 (defun company-search-repeat-forward ()
1273 "Repeat the incremental search in completion candidates forward."
1275 (company-search-assert-enabled)
1276 (let ((pos (company-search company-search-string
1277 (cdr (nthcdr company-selection
1278 company-candidates)))))
1281 (company-set-selection (+ company-selection pos 1) t))))
1283 (defun company-search-repeat-backward ()
1284 "Repeat the incremental search in completion candidates backwards."
1286 (company-search-assert-enabled)
1287 (let ((pos (company-search company-search-string
1288 (nthcdr (- company-candidates-length
1290 (reverse company-candidates)))))
1293 (company-set-selection (- company-selection pos 1) t))))
1295 (defun company-create-match-predicate ()
1296 (setq company-candidates-predicate
1297 `(lambda (candidate)
1298 ,(if company-candidates-predicate
1299 `(and (string-match ,company-search-string candidate)
1300 (funcall ,company-candidates-predicate
1302 `(string-match ,company-search-string candidate))))
1303 (company-update-candidates
1304 (company-apply-predicate company-candidates company-candidates-predicate))
1305 ;; Invalidate cache.
1306 (setq company-candidates-cache (cons company-prefix company-candidates)))
1308 (defun company-filter-printing-char ()
1310 (company-search-assert-enabled)
1311 (company-search-printing-char)
1312 (company-create-match-predicate)
1313 (company-call-frontends 'update))
1315 (defun company-search-kill-others ()
1316 "Limit the completion candidates to the ones matching the search string."
1318 (company-search-assert-enabled)
1319 (company-create-match-predicate)
1320 (company-search-mode 0)
1321 (company-call-frontends 'update))
1323 (defun company-search-abort ()
1324 "Abort searching the completion candidates."
1326 (company-search-assert-enabled)
1327 (company-set-selection company-search-old-selection t)
1328 (company-search-mode 0))
1330 (defun company-search-other-char ()
1332 (company-search-assert-enabled)
1333 (company-search-mode 0)
1334 (company--unread-last-input))
1336 (defvar company-search-map
1338 (keymap (make-keymap)))
1339 (if (fboundp 'max-char)
1340 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1341 'company-search-printing-char)
1343 ;; obsolete in Emacs 23
1344 (let ((l (generic-character-list))
1345 (table (nth 1 keymap)))
1347 (set-char-table-default table (car l) 'company-search-printing-char)
1348 (setq l (cdr l))))))
1349 (define-key keymap [t] 'company-search-other-char)
1351 (define-key keymap (make-string 1 i) 'company-search-other-char)
1354 (define-key keymap (vector i) 'company-search-printing-char)
1356 (let ((meta-map (make-sparse-keymap)))
1357 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1358 (define-key keymap [escape] meta-map))
1359 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1360 (define-key keymap "\e\e\e" 'company-search-other-char)
1361 (define-key keymap [escape escape escape] 'company-search-other-char)
1363 (define-key keymap "\C-g" 'company-search-abort)
1364 (define-key keymap "\C-s" 'company-search-repeat-forward)
1365 (define-key keymap "\C-r" 'company-search-repeat-backward)
1366 (define-key keymap "\C-o" 'company-search-kill-others)
1368 "Keymap used for incrementally searching the completion candidates.")
1370 (define-minor-mode company-search-mode
1371 "Search mode for completion candidates.
1372 Don't start this directly, use `company-search-candidates' or
1373 `company-filter-candidates'."
1374 nil company-search-lighter nil
1375 (if company-search-mode
1376 (if (company-manual-begin)
1378 (setq company-search-old-selection company-selection)
1379 (company-call-frontends 'update))
1380 (setq company-search-mode nil))
1381 (kill-local-variable 'company-search-string)
1382 (kill-local-variable 'company-search-lighter)
1383 (kill-local-variable 'company-search-old-selection)
1384 (company-enable-overriding-keymap company-active-map)))
1386 (defun company-search-assert-enabled ()
1387 (company-assert-enabled)
1388 (unless company-search-mode
1389 (company-uninstall-map)
1390 (error "Company not in search mode")))
1392 (defun company-search-candidates ()
1393 "Start searching the completion candidates incrementally.
1395 \\<company-search-map>Search can be controlled with the commands:
1396 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1397 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1398 - `company-search-abort' (\\[company-search-abort])
1400 Regular characters are appended to the search string.
1402 The command `company-search-kill-others' (\\[company-search-kill-others])
1403 uses the search string to limit the completion candidates."
1405 (company-search-mode 1)
1406 (company-enable-overriding-keymap company-search-map))
1408 (defvar company-filter-map
1409 (let ((keymap (make-keymap)))
1410 (define-key keymap [remap company-search-printing-char]
1411 'company-filter-printing-char)
1412 (set-keymap-parent keymap company-search-map)
1414 "Keymap used for incrementally searching the completion candidates.")
1416 (defun company-filter-candidates ()
1417 "Start filtering the completion candidates incrementally.
1418 This works the same way as `company-search-candidates' immediately
1419 followed by `company-search-kill-others' after each input."
1421 (company-search-mode 1)
1422 (company-enable-overriding-keymap company-filter-map))
1424 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1426 (defun company-select-next ()
1427 "Select the next candidate in the list."
1429 (when (company-manual-begin)
1430 (company-set-selection (1+ company-selection))))
1432 (defun company-select-previous ()
1433 "Select the previous candidate in the list."
1435 (when (company-manual-begin)
1436 (company-set-selection (1- company-selection))))
1438 (defun company-select-next-or-abort ()
1439 "Select the next candidate if more than one, else abort
1440 and invoke the normal binding."
1442 (if (> company-candidates-length 1)
1443 (company-select-next)
1445 (company--unread-last-input)))
1447 (defun company-select-previous-or-abort ()
1448 "Select the previous candidate if more than one, else abort
1449 and invoke the normal binding."
1451 (if (> company-candidates-length 1)
1452 (company-select-previous)
1454 (company--unread-last-input)))
1456 (defvar company-pseudo-tooltip-overlay)
1458 (defvar company-tooltip-offset)
1460 (defun company--inside-tooltip-p (event-col-row row height)
1461 (let* ((ovl company-pseudo-tooltip-overlay)
1462 (column (overlay-get ovl 'company-column))
1463 (width (overlay-get ovl 'company-width))
1464 (evt-col (car event-col-row))
1465 (evt-row (cdr event-col-row)))
1466 (and (>= evt-col column)
1467 (< evt-col (+ column width))
1469 (and (> evt-row row)
1470 (<= evt-row (+ row height) ))
1471 (and (< evt-row row)
1472 (>= evt-row (+ row height)))))))
1474 (defun company--event-col-row (event)
1475 (let* ((col-row (posn-actual-col-row (event-start event)))
1477 (row (cdr col-row)))
1478 (incf col (window-hscroll))
1479 (and header-line-format
1480 (version< "24" emacs-version)
1484 (defun company-select-mouse (event)
1485 "Select the candidate picked by the mouse."
1487 (let ((event-col-row (company--event-col-row event))
1488 (ovl-row (company--row))
1489 (ovl-height (and company-pseudo-tooltip-overlay
1490 (min (overlay-get company-pseudo-tooltip-overlay
1492 company-candidates-length))))
1494 (company--inside-tooltip-p event-col-row ovl-row ovl-height))
1496 (company-set-selection (+ (cdr event-col-row)
1497 (1- company-tooltip-offset)
1498 (if (and (eq company-tooltip-offset-display 'lines)
1499 (not (zerop company-tooltip-offset)))
1502 (if (< ovl-height 0)
1507 (company--unread-last-input)
1510 (defun company-complete-mouse (event)
1511 "Insert the candidate picked by the mouse."
1513 (when (company-select-mouse event)
1514 (company-complete-selection)))
1516 (defun company-complete-selection ()
1517 "Insert the selected candidate."
1519 (when (company-manual-begin)
1520 (let ((result (nth company-selection company-candidates)))
1521 (when company--auto-completion
1522 (setq result (company--safe-candidate result)))
1523 (company-finish result))))
1525 (defun company-complete-common ()
1526 "Insert the common part of all candidates."
1528 (when (company-manual-begin)
1529 (if (and (not (cdr company-candidates))
1530 (equal company-common (car company-candidates)))
1531 (company-complete-selection)
1532 (when company-common
1533 (company--insert-candidate company-common)))))
1535 (defun company-complete ()
1536 "Insert the common part of all candidates or the current selection.
1537 The first time this is called, the common part is inserted, the second
1538 time, or when the selection has been changed, the selected candidate is
1541 (when (company-manual-begin)
1542 (if (or company-selection-changed
1543 (eq last-command 'company-complete-common))
1544 (call-interactively 'company-complete-selection)
1545 (call-interactively 'company-complete-common)
1546 (setq this-command 'company-complete-common))))
1548 (defun company-complete-number (n)
1549 "Insert the Nth candidate.
1550 To show the number next to the candidates in some back-ends, enable
1551 `company-show-numbers'."
1552 (when (company-manual-begin)
1553 (and (< n 1) (> n company-candidates-length)
1554 (error "No candidate number %d" n))
1556 (company-finish (nth n company-candidates))))
1558 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1560 (defconst company-space-strings-limit 100)
1562 (defconst company-space-strings
1564 (dotimes (i company-space-strings-limit)
1565 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1566 (apply 'vector lst)))
1568 (defun company-space-string (len)
1569 (if (< len company-space-strings-limit)
1570 (aref company-space-strings len)
1571 (make-string len ?\ )))
1573 (defun company-safe-substring (str from &optional to)
1574 (if (> from (string-width str))
1578 (move-to-column from)
1579 (let ((beg (point)))
1583 (concat (buffer-substring beg (point))
1584 (let ((padding (- to (current-column))))
1586 (company-space-string padding)))))
1587 (buffer-substring beg (point-max)))))))
1589 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1591 (defvar company-last-metadata nil)
1592 (make-variable-buffer-local 'company-last-metadata)
1594 (defun company-fetch-metadata ()
1595 (let ((selected (nth company-selection company-candidates)))
1596 (unless (equal selected (car company-last-metadata))
1597 (setq company-last-metadata
1598 (cons selected (company-call-backend 'meta selected))))
1599 (cdr company-last-metadata)))
1601 (defun company-doc-buffer (&optional string)
1602 (with-current-buffer (get-buffer-create "*company-documentation*")
1609 (defvar company--electric-commands
1610 '(scroll-other-window scroll-other-window-down)
1611 "List of Commands that won't break out of electric commands.")
1613 (defmacro company--electric-do (&rest body)
1614 (declare (indent 0) (debug t))
1615 `(when (company-manual-begin)
1616 (save-window-excursion
1617 (let ((height (window-height))
1618 (row (company--row))
1621 (and (< (window-height) height)
1622 (< (- (window-height) row 2) company-tooltip-limit)
1623 (recenter (- (window-height) row 2)))
1624 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1625 company--electric-commands)
1626 (call-interactively cmd))
1627 (company--unread-last-input)))))
1629 (defun company--unread-last-input ()
1630 (when last-input-event
1631 (clear-this-command-keys t)
1632 (setq unread-command-events (list last-input-event))))
1634 (defun company-show-doc-buffer ()
1635 "Temporarily show the documentation buffer for the selection."
1637 (company--electric-do
1638 (let* ((selected (nth company-selection company-candidates))
1639 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1640 (error "No documentation available"))))
1641 (with-current-buffer doc-buffer
1642 (goto-char (point-min)))
1643 (display-buffer doc-buffer t))))
1644 (put 'company-show-doc-buffer 'company-keep t)
1646 (defun company-show-location ()
1647 "Temporarily display a buffer showing the selected candidate in context."
1649 (company--electric-do
1650 (let* ((selected (nth company-selection company-candidates))
1651 (location (company-call-backend 'location selected))
1652 (pos (or (cdr location) (error "No location available")))
1653 (buffer (or (and (bufferp (car location)) (car location))
1654 (find-file-noselect (car location) t))))
1655 (with-selected-window (display-buffer buffer t)
1658 (if (bufferp (car location))
1660 (goto-char (point-min))
1661 (forward-line (1- pos))))
1662 (set-window-start nil (point))))))
1663 (put 'company-show-location 'company-keep t)
1665 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1667 (defvar company-callback nil)
1668 (make-variable-buffer-local 'company-callback)
1670 (defvar company-begin-with-marker nil)
1671 (make-variable-buffer-local 'company-begin-with-marker)
1673 (defun company-remove-callback (&optional ignored)
1674 (remove-hook 'company-completion-finished-hook company-callback t)
1675 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1676 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1677 (when company-begin-with-marker
1678 (set-marker company-begin-with-marker nil)))
1680 (defun company-begin-backend (backend &optional callback)
1681 "Start a completion at point using BACKEND."
1682 (interactive (let ((val (completing-read "Company back-end: "
1684 'functionp nil "company-")))
1686 (list (intern val)))))
1687 (when (setq company-callback callback)
1688 (add-hook 'company-completion-finished-hook company-callback nil t))
1689 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1690 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1691 (setq company-backend backend)
1692 ;; Return non-nil if active.
1693 (or (company-manual-begin)
1695 (setq company-backend nil)
1696 (error "Cannot complete at point"))))
1698 (defun company-begin-with (candidates
1699 &optional prefix-length require-match callback)
1700 "Start a completion at point.
1701 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length
1702 of the prefix that already is in the buffer before point.
1705 CALLBACK is a function called with the selected result if the user
1706 successfully completes the input.
1708 Example: \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1709 ;; FIXME: When Emacs 23 is no longer a concern, replace
1710 ;; `company-begin-with-marker' with a lexical variable; use a lexical closure.
1711 (setq company-begin-with-marker (copy-marker (point) t))
1712 (company-begin-backend
1713 `(lambda (command &optional arg &rest ignored)
1715 ((eq command 'prefix)
1716 (when (equal (point) (marker-position company-begin-with-marker))
1717 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1718 ((eq command 'candidates)
1719 (all-completions arg ',candidates))
1720 ((eq command 'require-match)
1724 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1726 (defvar company-pseudo-tooltip-overlay nil)
1727 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1729 (defvar company-tooltip-offset 0)
1730 (make-variable-buffer-local 'company-tooltip-offset)
1732 (defun company-tooltip--lines-update-offset (selection num-lines limit)
1734 (setq company-tooltip-offset
1735 (max (min selection company-tooltip-offset)
1736 (- selection -1 limit)))
1738 (when (<= company-tooltip-offset 1)
1740 (setq company-tooltip-offset 0))
1742 (when (>= company-tooltip-offset (- num-lines limit 1))
1744 (when (= selection (1- num-lines))
1745 (decf company-tooltip-offset)
1746 (when (<= company-tooltip-offset 1)
1747 (setq company-tooltip-offset 0)
1752 (defun company-tooltip--simple-update-offset (selection num-lines limit)
1753 (setq company-tooltip-offset
1754 (if (< selection company-tooltip-offset)
1756 (max company-tooltip-offset
1757 (- selection limit -1)))))
1761 (defsubst company-round-tab (arg)
1762 (* (/ (+ arg tab-width) tab-width) tab-width))
1764 (defun company-plainify (str)
1765 (let ((prefix (get-text-property 0 'line-prefix str)))
1766 (when prefix ; Keep the original value unmodified, for no special reason.
1767 (setq str (concat prefix str))
1768 (remove-text-properties 0 (length str) '(line-prefix) str)))
1769 (let* ((pieces (split-string str "\t"))
1772 (setcar copy (company-safe-substring
1773 (car copy) 0 (company-round-tab (string-width (car copy)))))
1775 (apply 'concat pieces)))
1777 (defun company--highlight-common (line properties)
1778 ;; XXX: Subject to change.
1779 (let ((common (or (company-call-backend 'common-part line)
1780 (length company-common))))
1781 (add-text-properties 0 common properties line)))
1783 (defun company-fill-propertize (line width selected)
1784 (let* ((margin company-tooltip-margin)
1785 (common (+ (or (company-call-backend 'common-part line)
1786 (length company-common)) margin)))
1787 (setq line (concat (company-space-string company-tooltip-margin)
1788 (company-safe-substring
1789 line 0 (+ width company-tooltip-margin)))
1790 width (+ width (* 2 margin)))
1792 (add-text-properties 0 width '(face company-tooltip
1793 mouse-face company-tooltip-mouse)
1795 (add-text-properties margin common
1796 '(face company-tooltip-common
1797 mouse-face company-tooltip-mouse)
1800 (if (and company-search-string
1801 (string-match (regexp-quote company-search-string) line
1802 (length company-prefix)))
1804 (add-text-properties (match-beginning 0) (match-end 0)
1805 '(face company-tooltip-selection)
1807 (when (< (match-beginning 0) common)
1808 (add-text-properties (match-beginning 0) common
1809 '(face company-tooltip-common-selection)
1811 (add-text-properties 0 width '(face company-tooltip-selection
1812 mouse-face company-tooltip-selection)
1814 (add-text-properties margin common
1815 '(face company-tooltip-common-selection
1816 mouse-face company-tooltip-selection)
1822 (defun company-buffer-lines (beg end)
1825 (while (and (= 1 (vertical-motion 1))
1827 (let ((bound (min end (1- (point)))))
1828 ;; A visual line can contain several physical lines (e.g. with outline's
1829 ;; folding overlay). Take only the first one.
1830 (push (buffer-substring beg
1833 (re-search-forward "$" bound 'move)
1837 (unless (eq beg end)
1838 (push (buffer-substring beg end) lines))
1841 (defun company-modify-line (old new offset)
1842 (concat (company-safe-substring old 0 offset)
1844 (company-safe-substring old (+ offset (length new)))))
1846 (defsubst company--length-limit (lst limit)
1847 (if (nthcdr limit lst)
1851 (defun company--replacement-string (lines old column nl &optional align-top)
1852 (decf column company-tooltip-margin)
1854 (let ((width (length (car lines)))
1855 (remaining-cols (- (+ (company--window-width) (window-hscroll))
1857 (when (> width remaining-cols)
1858 (decf column (- width remaining-cols))))
1860 (let ((offset (and (< column 0) (- column)))
1865 ;; untouched lines first
1866 (dotimes (_ (- (length old) (length lines)))
1867 (push (pop old) new)))
1868 ;; length into old lines.
1870 (push (company-modify-line (pop old)
1871 (company--offset-line (pop lines) offset)
1873 ;; Append whole new lines.
1875 (push (concat (company-space-string column)
1876 (company--offset-line (pop lines) offset))
1879 (let ((str (concat (when nl "\n")
1880 (mapconcat 'identity (nreverse new) "\n")
1882 (font-lock-append-text-property 0 (length str) 'face 'default str)
1885 (defun company--offset-line (line offset)
1886 (if (and offset line)
1887 (substring line offset)
1890 (defun company--create-lines (selection limit)
1892 (let ((len company-candidates-length)
1894 (window-width (company--window-width))
1903 ;; Maybe clear old offset.
1904 (when (<= len (+ company-tooltip-offset limit))
1905 (setq company-tooltip-offset 0))
1907 ;; Scroll to offset.
1908 (if (eq company-tooltip-offset-display 'lines)
1909 (setq limit (company-tooltip--lines-update-offset selection len limit))
1910 (company-tooltip--simple-update-offset selection len limit))
1913 ((eq company-tooltip-offset-display 'scrollbar)
1914 (setq scrollbar-bounds (company--scrollbar-bounds company-tooltip-offset
1916 ((eq company-tooltip-offset-display 'lines)
1917 (when (> company-tooltip-offset 0)
1918 (setq previous (format "...(%d)" company-tooltip-offset)))
1919 (setq remainder (- len limit company-tooltip-offset)
1920 remainder (when (> remainder 0)
1921 (setq remainder (format "...(%d)" remainder))))))
1923 (decf selection company-tooltip-offset)
1924 (setq width (max (length previous) (length remainder))
1925 lines (nthcdr company-tooltip-offset company-candidates)
1929 (decf window-width (* 2 company-tooltip-margin))
1930 (when scrollbar-bounds (decf window-width))
1933 (setq width (max (length (pop lines-copy)) width)))
1934 (setq width (min window-width
1935 (if (and company-show-numbers
1936 (< company-tooltip-offset 10))
1939 (setq lines-copy lines)
1941 ;; number can make tooltip too long
1942 (when company-show-numbers
1943 (setq numbered company-tooltip-offset))
1946 (push (company--scrollpos-line previous width) new))
1949 (let ((line (company-fill-propertize
1950 (if (>= numbered 10)
1951 (company-reformat (pop lines))
1954 (company-safe-substring
1955 (company-reformat (pop lines)) 0 (- width 2))
1957 width (equal i selection))))
1958 (push (if scrollbar-bounds
1959 (company--scrollbarize line i scrollbar-bounds)
1964 (push (company--scrollpos-line remainder width) new))
1966 (setq lines (nreverse new))))
1968 (defun company--scrollbar-bounds (offset limit length)
1969 (when (> length limit)
1970 (let* ((size (ceiling (* limit (float limit)) length))
1971 (lower (floor (* limit (float offset)) length))
1972 (upper (+ lower size -1)))
1973 (cons lower upper))))
1975 (defun company--scrollbarize (line i bounds)
1977 (propertize " " 'face
1978 (if (and (>= i (car bounds)) (<= i (cdr bounds)))
1979 'company-scrollbar-fg
1980 'company-scrollbar-bg))))
1982 (defun company--scrollpos-line (text width)
1983 (propertize (concat (company-space-string company-tooltip-margin)
1984 (company-safe-substring text 0 width)
1985 (company-space-string company-tooltip-margin))
1986 'face 'company-tooltip))
1990 (defsubst company--window-inner-height ()
1991 (let ((edges (window-inside-edges)))
1992 (- (nth 3 edges) (nth 1 edges))))
1994 (defsubst company--window-width ()
1997 ((display-graphic-p) 0)
1998 ;; Account for the line continuation column.
1999 ((version< "24.3.1" emacs-version) 1)
2000 ;; Emacs 24.3 and earlier included margins
2001 ;; in window-width when in TTY.
2002 (t (1+ (let ((margins (window-margins)))
2003 (+ (or (car margins) 0)
2004 (or (cdr margins) 0))))))))
2006 (defun company--pseudo-tooltip-height ()
2007 "Calculate the appropriate tooltip height.
2008 Returns a negative number if the tooltip should be displayed above point."
2009 (let* ((lines (company--row))
2010 (below (- (company--window-inner-height) 1 lines)))
2011 (if (and (< below (min company-tooltip-minimum company-candidates-length))
2013 (- (max 3 (min company-tooltip-limit lines)))
2014 (max 3 (min company-tooltip-limit below)))))
2016 (defun company-pseudo-tooltip-show (row column selection)
2017 (company-pseudo-tooltip-hide)
2020 (let* ((height (company--pseudo-tooltip-height))
2024 (setq row (+ row height -1)
2027 (let* ((nl (< (move-to-window-line row) row))
2029 (end (save-excursion
2030 (move-to-window-line (+ row (abs height)))
2032 (ov (make-overlay beg end))
2033 (args (list (mapcar 'company-plainify
2034 (company-buffer-lines beg end))
2037 (setq company-pseudo-tooltip-overlay ov)
2038 (overlay-put ov 'company-replacement-args args)
2040 (let ((lines (company--create-lines selection (abs height))))
2041 (overlay-put ov 'company-after
2042 (apply 'company--replacement-string lines args))
2043 (overlay-put ov 'company-width (string-width (car lines))))
2045 (overlay-put ov 'company-column column)
2046 (overlay-put ov 'company-height height)))))
2048 (defun company-pseudo-tooltip-show-at-point (pos)
2049 (let ((row (company--row pos))
2050 (col (company--column pos)))
2051 (company-pseudo-tooltip-show (1+ row) col company-selection)))
2053 (defun company-pseudo-tooltip-edit (selection)
2054 (let ((height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
2055 (overlay-put company-pseudo-tooltip-overlay 'company-after
2056 (apply 'company--replacement-string
2057 (company--create-lines selection (abs height))
2058 (overlay-get company-pseudo-tooltip-overlay
2059 'company-replacement-args)))))
2061 (defun company-pseudo-tooltip-hide ()
2062 (when company-pseudo-tooltip-overlay
2063 (delete-overlay company-pseudo-tooltip-overlay)
2064 (setq company-pseudo-tooltip-overlay nil)))
2066 (defun company-pseudo-tooltip-hide-temporarily ()
2067 (when (overlayp company-pseudo-tooltip-overlay)
2068 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
2069 (overlay-put company-pseudo-tooltip-overlay 'line-prefix nil)
2070 (overlay-put company-pseudo-tooltip-overlay 'after-string nil)))
2072 (defun company-pseudo-tooltip-unhide ()
2073 (when company-pseudo-tooltip-overlay
2074 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
2075 ;; Beat outline's folding overlays, at least.
2076 (overlay-put company-pseudo-tooltip-overlay 'priority 1)
2077 ;; No (extra) prefix for the first line.
2078 (overlay-put company-pseudo-tooltip-overlay 'line-prefix "")
2079 (overlay-put company-pseudo-tooltip-overlay 'after-string
2080 (overlay-get company-pseudo-tooltip-overlay 'company-after))
2081 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
2083 (defun company-pseudo-tooltip-guard ()
2084 (buffer-substring-no-properties
2085 (point) (overlay-start company-pseudo-tooltip-overlay)))
2087 (defun company-pseudo-tooltip-frontend (command)
2088 "`company-mode' front-end similar to a tooltip but based on overlays."
2090 (pre-command (company-pseudo-tooltip-hide-temporarily))
2092 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
2093 (overlay-get company-pseudo-tooltip-overlay
2096 (new-height (company--pseudo-tooltip-height)))
2097 (unless (and (>= (* old-height new-height) 0)
2098 (>= (abs old-height) (abs new-height))
2099 (equal (company-pseudo-tooltip-guard)
2100 (overlay-get company-pseudo-tooltip-overlay
2103 (company-pseudo-tooltip-show-at-point (- (point)
2104 (length company-prefix)))
2105 (overlay-put company-pseudo-tooltip-overlay
2106 'company-guard (company-pseudo-tooltip-guard))))
2107 (company-pseudo-tooltip-unhide))
2108 (hide (company-pseudo-tooltip-hide)
2109 (setq company-tooltip-offset 0))
2110 (update (when (overlayp company-pseudo-tooltip-overlay)
2111 (company-pseudo-tooltip-edit company-selection)))))
2113 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
2114 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
2115 (unless (and (eq command 'post-command)
2116 (company--show-inline-p))
2117 (company-pseudo-tooltip-frontend command)))
2119 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2121 (defvar company-preview-overlay nil)
2122 (make-variable-buffer-local 'company-preview-overlay)
2124 (defun company-preview-show-at-point (pos)
2125 (company-preview-hide)
2127 (setq company-preview-overlay (make-overlay pos (1+ pos)))
2129 (let ((completion (nth company-selection company-candidates)))
2130 (setq completion (propertize completion 'face 'company-preview))
2131 (add-text-properties 0 (length company-common)
2132 '(face company-preview-common) completion)
2134 ;; Add search string
2135 (and company-search-string
2136 (string-match (regexp-quote company-search-string) completion)
2137 (add-text-properties (match-beginning 0)
2139 '(face company-preview-search)
2142 (setq completion (company-strip-prefix completion))
2144 (and (equal pos (point))
2145 (not (equal completion ""))
2146 (add-text-properties 0 1 '(cursor t) completion))
2148 (overlay-put company-preview-overlay 'display
2149 (concat completion (unless (eq pos (point-max))
2150 (buffer-substring pos (1+ pos)))))
2151 (overlay-put company-preview-overlay 'window (selected-window))))
2153 (defun company-preview-hide ()
2154 (when company-preview-overlay
2155 (delete-overlay company-preview-overlay)
2156 (setq company-preview-overlay nil)))
2158 (defun company-preview-frontend (command)
2159 "`company-mode' front-end showing the selection as if it had been inserted."
2161 (pre-command (company-preview-hide))
2162 (post-command (company-preview-show-at-point (point)))
2163 (hide (company-preview-hide))))
2165 (defun company-preview-if-just-one-frontend (command)
2166 "`company-preview-frontend', but only shown for single candidates."
2167 (when (or (not (eq command 'post-command))
2168 (company--show-inline-p))
2169 (company-preview-frontend command)))
2171 (defun company--show-inline-p ()
2172 (and (not (cdr company-candidates))
2174 (string-prefix-p company-prefix company-common
2175 (company-call-backend 'ignore-case))))
2177 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2179 (defvar company-echo-last-msg nil)
2180 (make-variable-buffer-local 'company-echo-last-msg)
2182 (defvar company-echo-timer nil)
2184 (defvar company-echo-delay .01)
2186 (defun company-echo-show (&optional getter)
2188 (setq company-echo-last-msg (funcall getter)))
2189 (let ((message-log-max nil))
2190 (if company-echo-last-msg
2191 (message "%s" company-echo-last-msg)
2194 (defun company-echo-show-soon (&optional getter)
2195 (when company-echo-timer
2196 (cancel-timer company-echo-timer))
2197 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
2199 (defsubst company-echo-show-when-idle (&optional getter)
2200 (when (sit-for company-echo-delay)
2201 (company-echo-show getter)))
2203 (defun company-echo-format ()
2205 (let ((limit (window-width (minibuffer-window)))
2207 ;; Roll to selection.
2208 (candidates (nthcdr company-selection company-candidates))
2209 (i (if company-show-numbers company-selection 99999))
2213 (setq comp (company-reformat (pop candidates))
2214 len (+ len 1 (length comp)))
2218 (setq comp (propertize (format "%d: %s" i comp)
2219 'face 'company-echo))
2222 (add-text-properties 3 (+ 3 (length company-common))
2223 '(face company-echo-common) comp))
2224 (setq comp (propertize comp 'face 'company-echo))
2225 (add-text-properties 0 (length company-common)
2226 '(face company-echo-common) comp))
2228 (setq candidates nil)
2231 (mapconcat 'identity (nreverse msg) " ")))
2233 (defun company-echo-strip-common-format ()
2235 (let ((limit (window-width (minibuffer-window)))
2236 (len (+ (length company-prefix) 2))
2237 ;; Roll to selection.
2238 (candidates (nthcdr company-selection company-candidates))
2239 (i (if company-show-numbers company-selection 99999))
2243 (setq comp (company-strip-prefix (pop candidates))
2244 len (+ len 2 (length comp)))
2247 (setq comp (format "%s (%d)" comp i))
2251 (setq candidates nil)
2252 (push (propertize comp 'face 'company-echo) msg)))
2254 (concat (propertize company-prefix 'face 'company-echo-common) "{"
2255 (mapconcat 'identity (nreverse msg) ", ")
2258 (defun company-echo-hide ()
2259 (unless (equal company-echo-last-msg "")
2260 (setq company-echo-last-msg "")
2261 (company-echo-show)))
2263 (defun company-echo-frontend (command)
2264 "`company-mode' front-end showing the candidates in the echo area."
2266 (post-command (company-echo-show-soon 'company-echo-format))
2267 (hide (company-echo-hide))))
2269 (defun company-echo-strip-common-frontend (command)
2270 "`company-mode' front-end showing the candidates in the echo area."
2272 (post-command (company-echo-show-soon 'company-echo-strip-common-format))
2273 (hide (company-echo-hide))))
2275 (defun company-echo-metadata-frontend (command)
2276 "`company-mode' front-end showing the documentation in the echo area."
2278 (post-command (company-echo-show-when-idle 'company-fetch-metadata))
2279 (hide (company-echo-hide))))
2282 ;;; company.el ends here