1 ;;; company.el --- extensible inline text completion mechanism
3 ;; Copyright (C) 2009 Nikolaj Schumacher
5 ;; Author: Nikolaj Schumacher <bugs * nschum de>
7 ;; Keywords: abbrev, convenience, matchis
8 ;; URL: http://nschum.de/src/emacs/company/
9 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
11 ;; This file is NOT part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License
15 ;; as published by the Free Software Foundation; either version 2
16 ;; of the License, or (at your option) any later version.
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ;; Company is a modular completion mechanism. Modules for retrieving completion
29 ;; candidates are called back-ends, modules for displaying them are front-ends.
31 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
32 ;; distributed in individual files and can be used individually.
34 ;; Place company.el and the back-ends you want to use in a directory and add the
35 ;; following to your .emacs:
36 ;; (add-to-list 'load-path "/path/to/company")
37 ;; (autoload 'company-mode "company" nil t)
39 ;; Enable company-mode with M-x company-mode. For further information look at
40 ;; the documentation for `company-mode' (C-h f company-mode RET)
42 ;; If you want to start a specific back-end, call it interactively or use
43 ;; `company-begin-backend'. For example:
44 ;; M-x company-abbrev will prompt for and insert an abbrev.
46 ;; To write your own back-end, look at the documentation for `company-backends'.
47 ;; Here is a simple example completing "foo":
49 ;; (defun company-my-backend (command &optional arg &rest ignored)
51 ;; ('prefix (when (looking-back "foo\\>")
53 ;; ('candidates (list "foobar" "foobaz" "foobarbaz"))
54 ;; ('meta (format "This value is named %s" arg))))
56 ;; Sometimes it is a good idea to mix two back-ends together, for example to
57 ;; enrich gtags with dabbrev text (to emulate local variables):
59 ;; (defun gtags-gtags-dabbrev-backend (command &optional arg &rest ignored)
61 ;; (prefix (company-gtags 'prefix))
62 ;; (candidates (append (company-gtags 'candidates arg)
63 ;; (company-dabbrev 'candidates arg)))))
66 ;; When point is at the very end of the buffer, the pseudo-tooltip appears very
67 ;; wrong, unless company is allowed to temporarily insert a fake newline.
68 ;; This behavior is enabled by `company-end-of-buffer-workaround'.
72 ;; Added back-end `company-dabbrev-code' for generic code.
73 ;; Fixed `company-begin-with'.
76 ;; Added 'stop prefix to prevent dabbrev from completing inside of symbols.
77 ;; Fixed issues with tabbar-mode and line-spacing.
78 ;; Performance enhancements.
81 ;; Added `company-begin-commands' option.
82 ;; Added abbrev, tempo and Xcode back-ends.
83 ;; Back-ends are now interactive. You can start them with M-x backend-name.
84 ;; Added `company-begin-with' for starting company from elisp-code.
86 ;; Added `company-require-match' and `company-auto-complete' options.
89 ;; Improved Emacs Lisp back-end behavior for local variables.
90 ;; Added `company-elisp-detect-function-context' option.
91 ;; The mouse can now be used for selection.
94 ;; Added `company-show-location'.
95 ;; Added etags back-end.
96 ;; Added work-around for end-of-buffer bug.
97 ;; Added `company-filter-candidates'.
98 ;; More local Lisp variables are now included in the candidates.
100 ;; 2009-03-21 (0.1.5)
101 ;; Fixed elisp documentation buffer always showing the same doc.
102 ;; Added `company-echo-strip-common-frontend'.
103 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
104 ;; Don't hide the echo message if it isn't shown.
111 (eval-when-compile (require 'cl))
113 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
114 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
115 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
116 (add-to-list 'debug-ignored-errors "^Company not ")
117 (add-to-list 'debug-ignored-errors "^No candidate number ")
118 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
120 (defgroup company nil
121 "Extensible inline text completion mechanism"
126 (defface company-tooltip
127 '((t :background "yellow"
128 :foreground "black"))
129 "*Face used for the tool tip."
132 (defface company-tooltip-selection
133 '((default :inherit company-tooltip)
134 (((class color) (min-colors 88)) (:background "orange1"))
135 (t (:background "green")))
136 "*Face used for the selection in the tool tip."
139 (defface company-tooltip-mouse
140 '((default :inherit highlight))
141 "*Face used for the tool tip item under the mouse."
144 (defface company-tooltip-common
145 '((t :inherit company-tooltip
147 "*Face used for the common completion in the tool tip."
150 (defface company-tooltip-common-selection
151 '((t :inherit company-tooltip-selection
153 "*Face used for the selected common completion in the tool tip."
156 (defcustom company-tooltip-limit 10
157 "*The maximum number of candidates in the tool tip"
161 (defface company-preview
162 '((t :background "blue4"
163 :foreground "wheat"))
164 "*Face used for the completion preview."
167 (defface company-preview-common
168 '((t :inherit company-preview
170 "*Face used for the common part of the completion preview."
173 (defface company-preview-search
174 '((t :inherit company-preview
175 :background "blue1"))
176 "*Face used for the search string in the completion preview."
179 (defface company-echo nil
180 "*Face used for completions in the echo area."
183 (defface company-echo-common
184 '((((background dark)) (:foreground "firebrick1"))
185 (((background light)) (:background "firebrick4")))
186 "*Face used for the common part of completions in the echo area."
189 (defun company-frontends-set (variable value)
191 (let ((remainder value))
192 (setcdr remainder (delq (car remainder) (cdr remainder))))
193 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
194 (memq 'company-pseudo-tooltip-frontend value)
195 (error "Pseudo tooltip frontend cannot be used twice"))
196 (and (memq 'company-preview-if-just-one-frontend value)
197 (memq 'company-preview-frontend value)
198 (error "Preview frontend cannot be used twice"))
199 (and (memq 'company-echo value)
200 (memq 'company-echo-metadata-frontend value)
201 (error "Echo area cannot be used twice"))
202 ;; preview must come last
203 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
205 (setq value (append (delq f value) (list f)))))
206 (set variable value))
208 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
209 company-preview-frontend
210 company-echo-metadata-frontend)
211 "*The list of active front-ends (visualizations).
212 Each front-end is a function that takes one argument. It is called with
213 one of the following arguments:
215 'show: When the visualization should start.
217 'hide: When the visualization should end.
219 'update: When the data has been updated.
221 'pre-command: Before every command that is executed while the
222 visualization is active.
224 'post-command: After every command that is executed while the
225 visualization is active.
227 The visualized data is stored in `company-prefix', `company-candidates',
228 `company-common', `company-selection', `company-point' and
229 `company-search-string'."
230 :set 'company-frontends-set
232 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
233 (const :tag "echo, strip common"
234 company-echo-strip-common-frontend)
235 (const :tag "show echo meta-data in echo"
236 company-echo-metadata-frontend)
237 (const :tag "pseudo tooltip"
238 company-pseudo-tooltip-frontend)
239 (const :tag "pseudo tooltip, multiple only"
240 company-pseudo-tooltip-unless-just-one-frontend)
241 (const :tag "preview" company-preview-frontend)
242 (const :tag "preview, unique only"
243 company-preview-if-just-one-frontend)
244 (function :tag "custom function" nil))))
246 (defvar company-safe-backends
247 '(company-abbrev company-css company-dabbrev-code company-dabbrev
248 company-elisp company-etags company-files company-gtags company-ispell
249 company-nxml company-oddmuse company-semantic company-tempo company-xcode))
250 (put 'company-safe-backends 'risky-local-variable t)
252 (defun company-safe-backends-p (backends)
253 (and (consp backends)
254 (not (dolist (backend backends)
255 (unless (memq backend company-safe-backends)
258 (defcustom company-backends '(company-elisp company-nxml company-css
259 company-semantic company-xcode company-gtags
260 company-etags company-oddmuse company-files
261 company-dabbrev-code company-dabbrev)
262 "*The list of active back-ends (completion engines).
263 Each back-end is a function that takes a variable number of arguments.
264 The first argument is the command requested from the back-end. It is one
267 'prefix: The back-end should return the text to be completed. It must be
268 text immediately before `point'. Returning nil passes control to the next
269 back-end. The function should return 'stop if it should complete but cannot
270 \(e.g. if it is in the middle of a string\).
272 'candidates: The second argument is the prefix to be completed. The
273 return value should be a list of candidates that start with the prefix.
277 'sorted: The back-end may return t here to indicate that the candidates
278 are sorted and will not need to be sorted again.
280 'duplicates: If non-nil, company will take care of removing duplicates
283 'no-cache: Usually company doesn't ask for candidates again as completion
284 progresses, unless the back-end returns t for this command. The second
285 argument is the latest prefix.
287 'meta: The second argument is a completion candidate. The back-end should
288 return a (short) documentation string for it.
290 'doc-buffer: The second argument is a completion candidate. The back-end should
291 create a buffer (preferably with `company-doc-buffer'), fill it with
292 documentation and return it.
294 'location: The second argument is a completion candidate. The back-end can
295 return the cons of buffer and buffer location, or of file and line
296 number where the completion candidate was defined.
298 'require-match: If this value is t, the user is not allowed to enter anything
299 not offering as a candidate. Use with care! The default value nil gives the
300 user that choice with `company-require-match'. Return value 'never overrides
301 that option the other way around.
303 The back-end should return nil for all commands it does not support or
304 does not know about. It should also be callable interactively and use
305 `company-begin-backend' to start itself in that case."
307 :type '(repeat (function :tag "function" nil)))
309 (put 'company-backends 'safe-local-variable 'company-safe-backend-p)
311 (defcustom company-completion-started-hook nil
312 "*Hook run when company starts completing.
313 The hook is called with one argument that is non-nil if the completion was
318 (defcustom company-completion-cancelled-hook nil
319 "*Hook run when company cancels completing.
320 The hook is called with one argument that is non-nil if the completion was
325 (defcustom company-completion-finished-hook nil
326 "*Hook run when company successfully completes.
327 The hook is called with the selected candidate as an argument."
331 (defcustom company-minimum-prefix-length 3
332 "*The minimum prefix length for automatic completion."
334 :type '(integer :tag "prefix length"))
336 (defcustom company-require-match 'company-explicit-action-p
337 "*If enabled, disallow non-matching input.
338 This can be a function do determine if a match is required.
340 This can be overridden by the back-end, if it returns t or 'never to
341 'require-match. `company-auto-complete' also takes precedence over this."
343 :type '(choice (const :tag "Off" nil)
344 (function :tag "Predicate function")
345 (const :tag "On, if user interaction took place"
346 'company-explicit-action-p)
347 (const :tag "On" t)))
349 (defcustom company-auto-complete 'company-explicit-action-p
350 "Determines when to auto-complete.
351 If this is enabled, all characters from `company-auto-complete-chars' complete
352 the selected completion. This can also be a function."
354 :type '(choice (const :tag "Off" nil)
355 (function :tag "Predicate function")
356 (const :tag "On, if user interaction took place"
357 'company-explicit-action-p)
358 (const :tag "On" t)))
360 (defcustom company-auto-complete-chars '(?\ ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
361 "Determines which characters trigger an automatic completion.
362 See `company-auto-complete'. If this is a string, each string character causes
363 completion. If it is a list of syntax description characters (see
364 `modify-syntax-entry'), all characters with that syntax auto-complete.
366 This can also be a function, which is called with the new input and should
367 return non-nil if company should auto-complete.
369 A character that is part of a valid candidate never starts auto-completion."
371 :type '(choice (string :tag "Characters")
373 (const :tag "Whitespace" ?\ )
374 (const :tag "Symbol" ?_)
375 (const :tag "Opening parentheses" ?\()
376 (const :tag "Closing parentheses" ?\))
377 (const :tag "Word constituent" ?w)
378 (const :tag "Punctuation." ?.)
379 (const :tag "String quote." ?\")
380 (const :tag "Paired delimiter." ?$)
381 (const :tag "Expression quote or prefix operator." ?\')
382 (const :tag "Comment starter." ?<)
383 (const :tag "Comment ender." ?>)
384 (const :tag "Character-quote." ?/)
385 (const :tag "Generic string fence." ?|)
386 (const :tag "Generic comment fence." ?!))
387 (function :tag "Predicate function")))
389 (defcustom company-idle-delay .7
390 "*The idle delay in seconds until automatic completions starts.
391 A value of nil means never complete automatically, t means complete
392 immediately when a prefix of `company-minimum-prefix-length' is reached."
394 :type '(choice (const :tag "never (nil)" nil)
395 (const :tag "immediate (t)" t)
396 (number :tag "seconds")))
398 (defcustom company-begin-commands t
399 "*A list of commands following which company will start completing.
400 If this is t, it will complete after any command. See `company-idle-delay'.
402 Alternatively any command with a non-nil 'company-begin property is treated as
403 if it was on this list."
405 :type '(choice (const :tag "Any command" t)
406 (const :tag "Self insert command" '(self-insert-command))
407 (repeat :tag "Commands" function)))
409 (defcustom company-show-numbers nil
410 "*If enabled, show quick-access numbers for the first ten candidates."
412 :type '(choice (const :tag "off" nil)
413 (const :tag "on" t)))
415 (defvar company-end-of-buffer-workaround t
416 "*Work around a visualization bug when completing at the end of the buffer.
417 The work-around consists of adding a newline.")
419 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
421 (defvar company-mode-map (make-sparse-keymap)
422 "Keymap used by `company-mode'.")
424 (defvar company-active-map
425 (let ((keymap (make-sparse-keymap)))
426 (define-key keymap "\e\e\e" 'company-abort)
427 (define-key keymap "\C-g" 'company-abort)
428 (define-key keymap (kbd "M-n") 'company-select-next)
429 (define-key keymap (kbd "M-p") 'company-select-previous)
430 (define-key keymap (kbd "<down>") 'company-select-next)
431 (define-key keymap (kbd "<up>") 'company-select-previous)
432 (define-key keymap [down-mouse-1] 'ignore)
433 (define-key keymap [down-mouse-3] 'ignore)
434 (define-key keymap [mouse-1] 'company-complete-mouse)
435 (define-key keymap [mouse-3] 'company-select-mouse)
436 (define-key keymap [up-mouse-1] 'ignore)
437 (define-key keymap [up-mouse-3] 'ignore)
438 (define-key keymap "\C-m" 'company-complete-selection)
439 (define-key keymap "\t" 'company-complete-common)
440 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
441 (define-key keymap "\C-w" 'company-show-location)
442 (define-key keymap "\C-s" 'company-search-candidates)
443 (define-key keymap "\C-\M-s" 'company-filter-candidates)
445 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
446 `(lambda () (interactive) (company-complete-number ,i))))
449 "Keymap that is enabled during an active completion.")
451 (defun company-init-backend (backend)
452 (when (symbolp backend)
453 (unless (fboundp backend)
454 (ignore-errors (require backend nil t)))
455 (if (and (fboundp backend)
456 (ignore-errors (funcall backend 'init) t))
457 (put backend 'company-init t)
458 (message "Company back-end '%s' could not be initialized"
462 (define-minor-mode company-mode
463 "\"complete anything\"; in in-buffer completion framework.
464 Completion starts automatically, depending on the values
465 `company-idle-delay' and `company-minimum-prefix-length'.
467 Completion can be controlled with the commands:
468 `company-complete-common', `company-complete-selection', `company-complete',
469 `company-select-next', `company-select-previous'. If these commands are
470 called before `company-idle-delay', completion will also start.
472 Completions can be searched with `company-search-candidates' or
473 `company-filter-candidates'. These can be used while completion is
476 The completion data is retrieved using `company-backends' and displayed using
477 `company-frontends'. If you want to start a specific back-end, call it
478 interactively or use `company-begin-backend'.
480 regular keymap (`company-mode-map'):
483 keymap during active completions (`company-active-map'):
485 \\{company-active-map}"
486 nil " comp" company-mode-map
489 (add-hook 'pre-command-hook 'company-pre-command nil t)
490 (add-hook 'post-command-hook 'company-post-command nil t)
491 (mapc 'company-init-backend company-backends))
492 (remove-hook 'pre-command-hook 'company-pre-command t)
493 (remove-hook 'post-command-hook 'company-post-command t)
495 (kill-local-variable 'company-point)))
497 (defsubst company-assert-enabled ()
499 (company-uninstall-map)
500 (error "Company not enabled")))
502 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
504 (defvar company-overriding-keymap-bound nil)
505 (make-variable-buffer-local 'company-overriding-keymap-bound)
507 (defvar company-old-keymap nil)
508 (make-variable-buffer-local 'company-old-keymap)
510 (defvar company-my-keymap nil)
511 (make-variable-buffer-local 'company-my-keymap)
513 (defsubst company-enable-overriding-keymap (keymap)
514 (setq company-my-keymap keymap)
515 (when company-overriding-keymap-bound
516 (company-uninstall-map)))
518 (defun company-install-map ()
519 (unless (or company-overriding-keymap-bound
520 (null company-my-keymap))
521 (setq company-old-keymap overriding-terminal-local-map
522 overriding-terminal-local-map company-my-keymap
523 company-overriding-keymap-bound t)))
525 (defun company-uninstall-map ()
526 (when (eq overriding-terminal-local-map company-my-keymap)
527 (setq overriding-terminal-local-map company-old-keymap
528 company-overriding-keymap-bound nil)))
531 ;; Emacs calculates the active keymaps before reading the event. That means we
532 ;; cannot change the keymap from a timer. So we send a bogus command.
533 (defun company-ignore ()
536 (global-set-key '[31415926] 'company-ignore)
538 (defun company-input-noop ()
539 (push 31415926 unread-command-events))
541 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
543 (defun company-grab (regexp &optional expression limit)
544 (when (looking-back regexp limit)
545 (or (match-string-no-properties (or expression 0)) "")))
547 (defun company-grab-line (regexp &optional expression)
548 (company-grab regexp expression (point-at-bol)))
550 (defun company-grab-symbol ()
551 (if (looking-at "\\_>")
552 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
554 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
557 (defun company-grab-word ()
558 (if (looking-at "\\>")
559 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
561 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
564 (defun company-in-string-or-comment ()
565 (let ((ppss (syntax-ppss)))
566 (or (nth 3 ppss) (nth 4 ppss) (nth 7 ppss))))
568 (defun company-call-backend (&rest args)
569 (apply 'company-backend args))
571 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
573 (defvar company-backend nil)
574 (make-variable-buffer-local 'company-backend)
576 (defvar company-prefix nil)
577 (make-variable-buffer-local 'company-prefix)
579 (defvar company-candidates nil)
580 (make-variable-buffer-local 'company-candidates)
582 (defvar company-candidates-length nil)
583 (make-variable-buffer-local 'company-candidates-length)
585 (defvar company-candidates-cache nil)
586 (make-variable-buffer-local 'company-candidates-cache)
588 (defvar company-candidates-predicate nil)
589 (make-variable-buffer-local 'company-candidates-predicate)
591 (defvar company-common nil)
592 (make-variable-buffer-local 'company-common)
594 (defvar company-selection 0)
595 (make-variable-buffer-local 'company-selection)
597 (defvar company-selection-changed nil)
598 (make-variable-buffer-local 'company-selection-changed)
600 (defvar company--explicit-action nil
601 "Non-nil, if explicit completion took place.")
602 (make-variable-buffer-local 'company--explicit-action)
604 (defvar company--this-command nil)
606 (defvar company-point nil)
607 (make-variable-buffer-local 'company-point)
609 (defvar company-timer nil)
611 (defvar company-added-newline nil)
612 (make-variable-buffer-local 'company-added-newline)
614 (defsubst company-strip-prefix (str)
615 (substring str (length company-prefix)))
617 (defun company-explicit-action-p ()
618 "Return whether explicit completion action was taken by the user."
619 (or company--explicit-action
620 company-selection-changed))
622 (defsubst company-reformat (candidate)
623 ;; company-ispell needs this, because the results are always lower-case
624 ;; It's mory efficient to fix it only when they are displayed.
625 (concat company-prefix (substring candidate (length company-prefix))))
627 (defun company--should-complete ()
628 (and (not (or buffer-read-only overriding-terminal-local-map
629 overriding-local-map))
630 (eq company-idle-delay t)
631 (or (eq t company-begin-commands)
632 (memq company--this-command company-begin-commands)
633 (and (symbolp this-command) (get this-command 'company-begin)))
634 (not (and transient-mark-mode mark-active))))
636 (defsubst company-call-frontends (command)
637 (dolist (frontend company-frontends)
639 (funcall frontend command)
640 (error (error "Company: Front-end %s error \"%s\" on command %s"
641 frontend (error-message-string err) command)))))
643 (defsubst company-set-selection (selection &optional force-update)
644 (setq selection (max 0 (min (1- company-candidates-length) selection)))
645 (when (or force-update (not (equal selection company-selection)))
646 (setq company-selection selection
647 company-selection-changed t)
648 (company-call-frontends 'update)))
650 (defun company-apply-predicate (candidates predicate)
652 (dolist (c candidates)
653 (when (funcall predicate c)
657 (defun company-update-candidates (candidates)
658 (setq company-candidates-length (length candidates))
659 (if (> company-selection 0)
660 ;; Try to restore the selection
661 (let ((selected (nth company-selection company-candidates)))
662 (setq company-selection 0
663 company-candidates candidates)
665 (while (and candidates (string< (pop candidates) selected))
666 (incf company-selection))
668 ;; Make sure selection isn't out of bounds.
669 (setq company-selection (min (1- company-candidates-length)
670 company-selection)))))
671 (setq company-selection 0
672 company-candidates candidates))
674 (push (cons company-prefix company-candidates) company-candidates-cache)
676 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
677 (setq company-common (try-completion company-prefix company-candidates)))
678 (when (eq company-common t)
679 (setq company-candidates nil)))
681 (defun company-calculate-candidates (prefix)
683 (or (cdr (assoc prefix company-candidates-cache))
684 (when company-candidates-cache
685 (let ((len (length prefix))
686 (completion-ignore-case (company-call-backend
689 (dotimes (i (1+ len))
690 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
691 company-candidates-cache)))
692 (return (all-completions prefix prev))))))
693 (let ((c (company-call-backend 'candidates prefix)))
694 (when company-candidates-predicate
695 (setq c (company-apply-predicate
696 c company-candidates-predicate)))
697 (unless (company-call-backend 'sorted)
698 (setq c (sort c 'string<)))
699 (when (company-call-backend 'duplicates)
703 (setcdr c2 (progn (while (equal (pop c2) (car c2)))
706 (if (or (cdr candidates)
707 (not (equal (car candidates) prefix)))
708 ;; Don't start when already completed and unique.
710 ;; Not the right place? maybe when setting?
711 (and company-candidates t))))
713 (defun company-idle-begin (buf win tick pos)
715 (eq buf (current-buffer))
716 (eq win (selected-window))
717 (eq tick (buffer-chars-modified-tick))
719 (not company-candidates)
720 (not (equal (point) company-point))
721 (let ((company-idle-delay t))
723 (when company-candidates
725 (company-post-command)))))
727 (defun company-manual-begin ()
729 (company-assert-enabled)
731 (not company-candidates)
732 (let ((company-idle-delay t)
733 (company-minimum-prefix-length 0)
734 (company-begin-commands t))
735 (setq company--explicit-action t)
737 ;; Return non-nil if active.
740 (defsubst company-incremental-p (old-prefix new-prefix)
741 (and (> (length new-prefix) (length old-prefix))
742 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
744 (defun company-require-match-p ()
745 (let ((backend-value (company-call-backend 'require-match)))
746 (or (eq backend-value t)
747 (and (if (functionp company-require-match)
748 (funcall company-require-match)
749 (eq company-require-match t))
750 (not (eq backend-value 'never))))))
752 (defun company-punctuation-p (input)
753 "Return non-nil, if input starts with punctuation or parentheses."
754 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
756 (defun company-auto-complete-p (beg end)
757 "Return non-nil, if input starts with punctuation or parentheses."
759 (if (functionp company-auto-complete)
760 (funcall company-auto-complete)
761 company-auto-complete)
762 (if (functionp company-auto-complete-chars)
763 (funcall company-auto-complete-chars (buffer-substring beg end))
764 (if (consp company-auto-complete-chars)
765 (memq (char-syntax (char-after beg)) company-auto-complete-chars)
766 (string-match (buffer-substring beg (1+ beg))
767 company-auto-complete-chars)))))
769 (defun company-continue ()
770 (when (company-call-backend 'no-cache company-prefix)
771 ;; Don't complete existing candidates, fetch new ones.
772 (setq company-candidates-cache nil))
773 (let ((new-prefix (company-call-backend 'prefix)))
774 (if (= (- (point) (length new-prefix))
775 (- company-point (length company-prefix)))
776 (unless (or (equal company-prefix new-prefix)
777 (let ((c (company-calculate-candidates new-prefix)))
778 ;; t means complete/unique.
780 (progn (company-cancel new-prefix) t)
782 (setq company-prefix new-prefix)
783 (company-update-candidates c)
785 (if (not (and (company-incremental-p company-prefix new-prefix)
786 (company-require-match-p)))
788 (when (equal company-prefix (car company-candidates))
789 ;; cancel, but last input was actually success
790 (company-cancel company-prefix))
791 (setq company-candidates nil))
792 (backward-delete-char (length new-prefix))
793 (insert company-prefix)
795 (message "Matching input is required")))
796 (when (company-auto-complete-p company-point (point))
798 (goto-char company-point)
799 (company-complete-selection)))
800 (setq company-candidates nil))
803 (defun company-begin ()
804 (when (and (not (and company-candidates (company-continue)))
805 (company--should-complete))
807 (dolist (backend (if company-backend
808 ;; prefer manual override
809 (list company-backend)
811 (when (and (get backend 'company-init)
813 (setq prefix (funcall backend 'prefix)))
814 (when (and (stringp prefix)
815 (>= (length prefix) company-minimum-prefix-length))
816 (setq company-backend backend
817 company-prefix prefix)
818 (let ((c (company-calculate-candidates prefix)))
819 ;; t means complete/unique. We don't start, so no hooks.
821 (company-update-candidates c)
822 (run-hook-with-args 'company-completion-started-hook
823 (company-explicit-action-p))
824 (company-call-frontends 'show))))
826 (if company-candidates
828 (when (and company-end-of-buffer-workaround (eobp))
829 (save-excursion (insert "\n"))
830 (setq company-added-newline (buffer-chars-modified-tick)))
831 (setq company-point (point))
832 (company-enable-overriding-keymap company-active-map)
833 (company-call-frontends 'update))
836 (defun company-cancel (&optional result)
837 (and company-added-newline
838 (> (point-max) (point-min))
839 (let ((tick (buffer-chars-modified-tick)))
840 (delete-region (1- (point-max)) (point-max))
841 (equal tick company-added-newline))
842 ;; Only set unmodified when tick remained the same since insert.
843 (set-buffer-modified-p nil))
846 (run-hook-with-args 'company-completion-finished-hook result)
847 (run-hook-with-args 'company-completion-cancelled-hook result)))
848 (setq company-added-newline nil
851 company-candidates nil
852 company-candidates-length nil
853 company-candidates-cache nil
854 company-candidates-predicate nil
857 company-selection-changed nil
858 company--explicit-action nil
861 (cancel-timer company-timer))
862 (company-search-mode 0)
863 (company-call-frontends 'hide)
864 (company-enable-overriding-keymap nil))
866 (defun company-abort ()
869 ;; Don't start again, unless started manually.
870 (setq company-point (point)))
872 (defun company-finish (result)
873 (insert (company-strip-prefix result))
874 (company-cancel result)
875 ;; Don't start again, unless started manually.
876 (setq company-point (point)))
878 (defsubst company-keep (command)
879 (and (symbolp command) (get command 'company-keep)))
881 (defun company-pre-command ()
882 (unless (company-keep this-command)
884 (when company-candidates
885 (company-call-frontends 'pre-command))
886 (error (message "Company: An error occurred in pre-command")
887 (message "%s" (error-message-string err))
890 (cancel-timer company-timer))
891 (company-uninstall-map))
893 (defun company-post-command ()
894 (unless (company-keep this-command)
897 (setq company--this-command this-command)
898 (unless (equal (point) company-point)
900 (when company-candidates
901 (company-call-frontends 'post-command))
902 (when (numberp company-idle-delay)
904 (run-with-timer company-idle-delay nil 'company-idle-begin
905 (current-buffer) (selected-window)
906 (buffer-chars-modified-tick) (point)))))
907 (error (message "Company: An error occurred in post-command")
908 (message "%s" (error-message-string err))
910 (company-install-map))
912 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
914 (defvar company-search-string nil)
915 (make-variable-buffer-local 'company-search-string)
917 (defvar company-search-lighter " Search: \"\"")
918 (make-variable-buffer-local 'company-search-lighter)
920 (defvar company-search-old-map nil)
921 (make-variable-buffer-local 'company-search-old-map)
923 (defvar company-search-old-selection 0)
924 (make-variable-buffer-local 'company-search-old-selection)
926 (defun company-search (text lines)
927 (let ((quoted (regexp-quote text))
930 (when (string-match quoted line (length company-prefix))
934 (defun company-search-printing-char ()
936 (company-search-assert-enabled)
937 (setq company-search-string
938 (concat (or company-search-string "") (string last-command-event))
939 company-search-lighter (concat " Search: \"" company-search-string
941 (let ((pos (company-search company-search-string
942 (nthcdr company-selection company-candidates))))
945 (company-set-selection (+ company-selection pos) t))))
947 (defun company-search-repeat-forward ()
948 "Repeat the incremental search in completion candidates forward."
950 (company-search-assert-enabled)
951 (let ((pos (company-search company-search-string
952 (cdr (nthcdr company-selection
953 company-candidates)))))
956 (company-set-selection (+ company-selection pos 1) t))))
958 (defun company-search-repeat-backward ()
959 "Repeat the incremental search in completion candidates backwards."
961 (company-search-assert-enabled)
962 (let ((pos (company-search company-search-string
963 (nthcdr (- company-candidates-length
965 (reverse company-candidates)))))
968 (company-set-selection (- company-selection pos 1) t))))
970 (defun company-create-match-predicate ()
971 (setq company-candidates-predicate
973 ,(if company-candidates-predicate
974 `(and (string-match ,company-search-string candidate)
975 (funcall ,company-candidates-predicate
977 `(string-match ,company-search-string candidate))))
978 (company-update-candidates
979 (company-apply-predicate company-candidates company-candidates-predicate))
981 (setq company-candidates-cache (cons company-prefix company-candidates)))
983 (defun company-filter-printing-char ()
985 (company-search-assert-enabled)
986 (company-search-printing-char)
987 (company-create-match-predicate)
988 (company-call-frontends 'update))
990 (defun company-search-kill-others ()
991 "Limit the completion candidates to the ones matching the search string."
993 (company-search-assert-enabled)
994 (company-create-match-predicate)
995 (company-search-mode 0)
996 (company-call-frontends 'update))
998 (defun company-search-abort ()
999 "Abort searching the completion candidates."
1001 (company-search-assert-enabled)
1002 (company-set-selection company-search-old-selection t)
1003 (company-search-mode 0))
1005 (defun company-search-other-char ()
1007 (company-search-assert-enabled)
1008 (company-search-mode 0)
1009 (when last-input-event
1010 (clear-this-command-keys t)
1011 (setq unread-command-events (list last-input-event))))
1013 (defvar company-search-map
1015 (keymap (make-keymap)))
1016 (if (fboundp 'max-char)
1017 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1018 'company-search-printing-char)
1020 ;; obselete in Emacs 23
1021 (let ((l (generic-character-list))
1022 (table (nth 1 keymap)))
1024 (set-char-table-default table (car l) 'company-search-printing-char)
1025 (setq l (cdr l))))))
1026 (define-key keymap [t] 'company-search-other-char)
1028 (define-key keymap (make-string 1 i) 'company-search-other-char)
1031 (define-key keymap (vector i) 'company-search-printing-char)
1033 (let ((meta-map (make-sparse-keymap)))
1034 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1035 (define-key keymap [escape] meta-map))
1036 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1037 (define-key keymap "\e\e\e" 'company-search-other-char)
1038 (define-key keymap [escape escape escape] 'company-search-other-char)
1040 (define-key keymap "\C-g" 'company-search-abort)
1041 (define-key keymap "\C-s" 'company-search-repeat-forward)
1042 (define-key keymap "\C-r" 'company-search-repeat-backward)
1043 (define-key keymap "\C-o" 'company-search-kill-others)
1045 "Keymap used for incrementally searching the completion candidates.")
1047 (define-minor-mode company-search-mode
1048 "Search mode for completion candidates.
1049 Don't start this directly, use `company-search-candidates' or
1050 `company-filter-candidates'."
1051 nil company-search-lighter nil
1052 (if company-search-mode
1053 (if (company-manual-begin)
1055 (setq company-search-old-selection company-selection)
1056 (company-call-frontends 'update))
1057 (setq company-search-mode nil))
1058 (kill-local-variable 'company-search-string)
1059 (kill-local-variable 'company-search-lighter)
1060 (kill-local-variable 'company-search-old-selection)
1061 (company-enable-overriding-keymap company-active-map)))
1063 (defsubst company-search-assert-enabled ()
1064 (company-assert-enabled)
1065 (unless company-search-mode
1066 (company-uninstall-map)
1067 (error "Company not in search mode")))
1069 (defun company-search-candidates ()
1070 "Start searching the completion candidates incrementally.
1072 \\<company-search-map>Search can be controlled with the commands:
1073 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1074 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1075 - `company-search-abort' (\\[company-search-abort])
1077 Regular characters are appended to the search string.
1079 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
1080 the search string to limit the completion candidates."
1082 (company-search-mode 1)
1083 (company-enable-overriding-keymap company-search-map))
1085 (defvar company-filter-map
1086 (let ((keymap (make-keymap)))
1087 (define-key keymap [remap company-search-printing-char]
1088 'company-filter-printing-char)
1089 (set-keymap-parent keymap company-search-map)
1091 "Keymap used for incrementally searching the completion candidates.")
1093 (defun company-filter-candidates ()
1094 "Start filtering the completion candidates incrementally.
1095 This works the same way as `company-search-candidates' immediately
1096 followed by `company-search-kill-others' after each input."
1098 (company-search-mode 1)
1099 (company-enable-overriding-keymap company-filter-map))
1101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1103 (defun company-select-next ()
1104 "Select the next candidate in the list."
1106 (when (company-manual-begin)
1107 (company-set-selection (1+ company-selection))))
1109 (defun company-select-previous ()
1110 "Select the previous candidate in the list."
1112 (when (company-manual-begin)
1113 (company-set-selection (1- company-selection))))
1115 (defun company-select-mouse (event)
1116 "Select the candidate picked by the mouse."
1118 (when (nth 4 (event-start event))
1119 (company-set-selection (- (cdr (posn-actual-col-row (event-start event)))
1120 (cdr (posn-actual-col-row (posn-at-point)))
1124 (defun company-complete-mouse (event)
1125 "Complete the candidate picked by the mouse."
1127 (when (company-select-mouse event)
1128 (company-complete-selection)))
1130 (defun company-complete-selection ()
1131 "Complete the selected candidate."
1133 (when (company-manual-begin)
1134 (company-finish (nth company-selection company-candidates))))
1136 (defun company-complete-common ()
1137 "Complete the common part of all candidates."
1139 (when (company-manual-begin)
1140 (if (and (not (cdr company-candidates))
1141 (equal company-common (car company-candidates)))
1142 (company-complete-selection)
1143 (insert (company-strip-prefix company-common)))))
1145 (defun company-complete ()
1146 "Complete the common part of all candidates or the current selection.
1147 The first time this is called, the common part is completed, the second time, or
1148 when the selection has been changed, the selected candidate is completed."
1150 (when (company-manual-begin)
1151 (if (or company-selection-changed
1152 (eq last-command 'company-complete-common))
1153 (call-interactively 'company-complete-selection)
1154 (call-interactively 'company-complete-common)
1155 (setq this-command 'company-complete-common))))
1157 (defun company-complete-number (n)
1158 "Complete the Nth candidate.
1159 To show the number next to the candidates in some back-ends, enable
1160 `company-show-numbers'."
1161 (when (company-manual-begin)
1162 (and (< n 1) (> n company-candidates-length)
1163 (error "No candidate number %d" n))
1165 (company-finish (nth n company-candidates))))
1167 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1169 (defconst company-space-strings-limit 100)
1171 (defconst company-space-strings
1173 (dotimes (i company-space-strings-limit)
1174 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1175 (apply 'vector lst)))
1177 (defsubst company-space-string (len)
1178 (if (< len company-space-strings-limit)
1179 (aref company-space-strings len)
1180 (make-string len ?\ )))
1182 (defsubst company-safe-substring (str from &optional to)
1183 (let ((len (length str)))
1186 (if (and to (> to len))
1187 (concat (substring str from)
1188 (company-space-string (- to len)))
1189 (substring str from to)))))
1191 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1193 (defvar company-last-metadata nil)
1194 (make-variable-buffer-local 'company-last-metadata)
1196 (defun company-fetch-metadata ()
1197 (let ((selected (nth company-selection company-candidates)))
1198 (unless (equal selected (car company-last-metadata))
1199 (setq company-last-metadata
1200 (cons selected (company-call-backend 'meta selected))))
1201 (cdr company-last-metadata)))
1203 (defun company-doc-buffer (&optional string)
1204 (with-current-buffer (get-buffer-create "*Company meta-data*")
1208 (defmacro company-electric (&rest body)
1209 (declare (indent 0) (debug t))
1210 `(when (company-manual-begin)
1211 (save-window-excursion
1212 (let ((height (window-height))
1213 (row (cdr (posn-actual-col-row (posn-at-point)))))
1215 (and (< (window-height) height)
1216 (< (- (window-height) row 2) company-tooltip-limit)
1217 (recenter (- (window-height) row 2)))
1218 (while (eq 'scroll-other-window
1219 (key-binding (vector (list (read-event)))))
1220 (call-interactively 'scroll-other-window))
1221 (when last-input-event
1222 (clear-this-command-keys t)
1223 (setq unread-command-events (list last-input-event)))))))
1225 (defun company-show-doc-buffer ()
1226 "Temporarily show a buffer with the complete documentation for the selection."
1229 (let ((selected (nth company-selection company-candidates)))
1230 (display-buffer (or (company-call-backend 'doc-buffer selected)
1231 (error "No documentation available")) t))))
1232 (put 'company-show-doc-buffer 'company-keep t)
1234 (defun company-show-location ()
1235 "Temporarily display a buffer showing the selected candidate in context."
1238 (let* ((selected (nth company-selection company-candidates))
1239 (location (company-call-backend 'location selected))
1240 (pos (or (cdr location) (error "No location available")))
1241 (buffer (or (and (bufferp (car location)) (car location))
1242 (find-file-noselect (car location) t))))
1243 (with-selected-window (display-buffer buffer t)
1244 (if (bufferp (car location))
1247 (set-window-start nil (point))))))
1248 (put 'company-show-location 'company-keep t)
1250 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1252 (defvar company-callback nil)
1253 (make-variable-buffer-local 'company-callback)
1255 (defvar company-begin-with-marker nil)
1256 (make-variable-buffer-local 'company-begin-with-marker)
1258 (defun company-remove-callback (&optional ignored)
1259 (remove-hook 'company-completion-finished-hook company-callback t)
1260 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1261 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1262 (set-marker company-begin-with-marker nil))
1264 (defun company-begin-backend (backend &optional callback)
1265 "Start a completion at point using BACKEND."
1266 (interactive (let ((val (completing-read "Company back-end: "
1268 'functionp nil "company-")))
1270 (list (intern val)))))
1271 (when (setq company-callback callback)
1272 (add-hook 'company-completion-finished-hook company-callback nil t))
1273 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1274 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1275 (setq company-backend backend)
1276 ;; Return non-nil if active.
1277 (or (company-manual-begin)
1278 (error "Cannot complete at point")))
1280 (defun company-begin-with (candidates
1281 &optional prefix-length require-match callback)
1282 "Start a completion at point.
1283 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
1284 the prefix that already is in the buffer before point. It defaults to 0.
1286 CALLBACK is a function called with the selected result if the user successfully
1287 completes the input.
1290 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1291 (company-begin-backend
1292 (let ((start (- (point) (or prefix-length 0))))
1293 (setq company-begin-with-marker (copy-marker (point) t))
1294 `(lambda (command &optional arg &rest ignored)
1296 ('prefix (when (equal (point)
1297 (marker-position company-begin-with-marker))
1298 (buffer-substring ,start (point))))
1299 ('candidates (all-completions arg ',candidates))
1300 ('require-match ,require-match))))
1303 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1305 (defvar company-pseudo-tooltip-overlay nil)
1306 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1308 (defvar company-tooltip-offset 0)
1309 (make-variable-buffer-local 'company-tooltip-offset)
1311 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1314 (setq company-tooltip-offset
1315 (max (min selection company-tooltip-offset)
1316 (- selection -1 limit)))
1318 (when (<= company-tooltip-offset 1)
1320 (setq company-tooltip-offset 0))
1322 (when (>= company-tooltip-offset (- num-lines limit 1))
1324 (when (= selection (1- num-lines))
1325 (decf company-tooltip-offset)
1326 (when (<= company-tooltip-offset 1)
1327 (setq company-tooltip-offset 0)
1334 (defsubst company-round-tab (arg)
1335 (* (/ (+ arg tab-width) tab-width) tab-width))
1337 (defun company-untabify (str)
1338 (let* ((pieces (split-string str "\t"))
1341 (setcar copy (company-safe-substring
1342 (car copy) 0 (company-round-tab (string-width (car copy)))))
1344 (apply 'concat pieces)))
1346 (defun company-fill-propertize (line width selected)
1347 (setq line (company-safe-substring line 0 width))
1348 (add-text-properties 0 width '(face company-tooltip
1349 mouse-face company-tooltip-mouse)
1351 (add-text-properties 0 (length company-common)
1352 '(face company-tooltip-common
1353 mouse-face company-tooltip-mouse)
1356 (if (and company-search-string
1357 (string-match (regexp-quote company-search-string) line
1358 (length company-prefix)))
1360 (add-text-properties (match-beginning 0) (match-end 0)
1361 '(face company-tooltip-selection)
1363 (when (< (match-beginning 0) (length company-common))
1364 (add-text-properties (match-beginning 0) (length company-common)
1365 '(face company-tooltip-common-selection)
1367 (add-text-properties 0 width '(face company-tooltip-selection
1368 mouse-face company-tooltip-selection)
1370 (add-text-properties 0 (length company-common)
1371 '(face company-tooltip-common-selection
1372 mouse-face company-tooltip-selection)
1378 (defun company-buffer-lines (beg end)
1380 (let ((row (cdr (posn-actual-col-row (posn-at-point))))
1382 (while (and (equal (move-to-window-line (incf row)) row)
1384 (push (buffer-substring beg (min end (1- (point)))) lines)
1386 (unless (eq beg end)
1387 (push (buffer-substring beg end) lines))
1390 (defsubst company-modify-line (old new offset)
1391 (concat (company-safe-substring old 0 offset)
1393 (company-safe-substring old (+ offset (length new)))))
1395 (defun company-replacement-string (old lines column nl)
1397 ;; Inject into old lines.
1399 (push (company-modify-line (pop old) (pop lines) column) new))
1400 ;; Append whole new lines.
1402 (push (concat (company-space-string column) (pop lines)) new))
1403 (concat (when nl "\n")
1404 (mapconcat 'identity (nreverse new) "\n")
1407 (defun company-create-lines (column selection limit)
1409 (let ((len company-candidates-length)
1418 ;; Scroll to offset.
1419 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1421 (when (> company-tooltip-offset 0)
1422 (setq previous (format "...(%d)" company-tooltip-offset)))
1424 (setq remainder (- len limit company-tooltip-offset)
1425 remainder (when (> remainder 0)
1426 (setq remainder (format "...(%d)" remainder))))
1428 (decf selection company-tooltip-offset)
1429 (setq width (min (length previous) (length remainder))
1430 lines (nthcdr company-tooltip-offset company-candidates)
1435 (setq width (max (length (pop lines-copy)) width)))
1436 (setq width (min width (- (window-width) column)))
1438 (setq lines-copy lines)
1440 ;; number can make tooltip too long
1441 (when company-show-numbers
1442 (setq numbered company-tooltip-offset))
1445 (push (propertize (company-safe-substring previous 0 width)
1446 'face 'company-tooltip)
1450 (push (company-fill-propertize
1451 (if (>= numbered 10)
1452 (company-reformat (pop lines))
1455 (company-safe-substring (company-reformat (pop lines))
1458 width (equal i selection))
1462 (push (propertize (company-safe-substring remainder 0 width)
1463 'face 'company-tooltip)
1466 (setq lines (nreverse new))))
1470 (defsubst company-pseudo-tooltip-height ()
1471 "Calculate the appropriate tooltip height."
1472 (max 3 (min company-tooltip-limit
1473 (- (window-height) 2
1474 (count-lines (window-start) (point-at-bol))))))
1476 (defun company-pseudo-tooltip-show (row column selection)
1477 (company-pseudo-tooltip-hide)
1482 (let* ((height (company-pseudo-tooltip-height))
1483 (lines (company-create-lines column selection height))
1484 (nl (< (move-to-window-line row) row))
1486 (end (save-excursion
1487 (move-to-window-line (+ row height))
1490 (mapcar 'company-untabify (company-buffer-lines beg end)))
1493 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1495 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1496 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1497 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1498 (overlay-put company-pseudo-tooltip-overlay 'company-before
1499 (company-replacement-string old-string lines column nl))
1500 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1502 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1504 (defun company-pseudo-tooltip-show-at-point (pos)
1505 (let ((col-row (posn-actual-col-row (posn-at-point pos))))
1507 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
1508 company-selection))))
1510 (defun company-pseudo-tooltip-edit (lines selection)
1511 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1512 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1513 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1514 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1515 (lines (company-create-lines column selection height)))
1516 (overlay-put company-pseudo-tooltip-overlay 'company-before
1517 (company-replacement-string old-string lines column nl))))
1519 (defun company-pseudo-tooltip-hide ()
1520 (when company-pseudo-tooltip-overlay
1521 (delete-overlay company-pseudo-tooltip-overlay)
1522 (setq company-pseudo-tooltip-overlay nil)))
1524 (defun company-pseudo-tooltip-hide-temporarily ()
1525 (when (overlayp company-pseudo-tooltip-overlay)
1526 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1527 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1529 (defun company-pseudo-tooltip-unhide ()
1530 (when company-pseudo-tooltip-overlay
1531 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1532 (overlay-put company-pseudo-tooltip-overlay 'before-string
1533 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1534 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1536 (defun company-pseudo-tooltip-frontend (command)
1537 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1539 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1541 (unless (and (overlayp company-pseudo-tooltip-overlay)
1542 (equal (overlay-get company-pseudo-tooltip-overlay
1544 (company-pseudo-tooltip-height)))
1546 (company-pseudo-tooltip-show-at-point (- (point)
1547 (length company-prefix))))
1548 (company-pseudo-tooltip-unhide))
1549 ('hide (company-pseudo-tooltip-hide)
1550 (setq company-tooltip-offset 0))
1551 ('update (when (overlayp company-pseudo-tooltip-overlay)
1552 (company-pseudo-tooltip-edit company-candidates
1553 company-selection)))))
1555 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1556 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1557 (unless (and (eq command 'post-command)
1558 (not (cdr company-candidates)))
1559 (company-pseudo-tooltip-frontend command)))
1561 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1563 (defvar company-preview-overlay nil)
1564 (make-variable-buffer-local 'company-preview-overlay)
1566 (defun company-preview-show-at-point (pos)
1567 (company-preview-hide)
1569 (setq company-preview-overlay (make-overlay pos pos))
1571 (let ((completion(nth company-selection company-candidates)))
1572 (setq completion (propertize completion 'face 'company-preview))
1573 (add-text-properties 0 (length company-common)
1574 '(face company-preview-common) completion)
1576 ;; Add search string
1577 (and company-search-string
1578 (string-match (regexp-quote company-search-string) completion)
1579 (add-text-properties (match-beginning 0)
1581 '(face company-preview-search)
1584 (setq completion (company-strip-prefix completion))
1586 (and (equal pos (point))
1587 (not (equal completion ""))
1588 (add-text-properties 0 1 '(cursor t) completion))
1590 (overlay-put company-preview-overlay 'after-string completion)
1591 (overlay-put company-preview-overlay 'window (selected-window))))
1593 (defun company-preview-hide ()
1594 (when company-preview-overlay
1595 (delete-overlay company-preview-overlay)
1596 (setq company-preview-overlay nil)))
1598 (defun company-preview-frontend (command)
1599 "A `company-mode' front-end showing the selection as if it had been inserted."
1601 ('pre-command (company-preview-hide))
1602 ('post-command (company-preview-show-at-point (point)))
1603 ('hide (company-preview-hide))))
1605 (defun company-preview-if-just-one-frontend (command)
1606 "`company-preview-frontend', but only shown for single candidates."
1607 (unless (and (eq command 'post-command)
1608 (cdr company-candidates))
1609 (company-preview-frontend command)))
1611 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1613 (defvar company-echo-last-msg nil)
1614 (make-variable-buffer-local 'company-echo-last-msg)
1616 (defvar company-echo-timer nil)
1618 (defvar company-echo-delay .1)
1620 (defun company-echo-show (&optional getter)
1622 (setq company-echo-last-msg (funcall getter)))
1623 (let ((message-log-max nil))
1624 (if company-echo-last-msg
1625 (message "%s" company-echo-last-msg)
1628 (defsubst company-echo-show-soon (&optional getter)
1629 (when company-echo-timer
1630 (cancel-timer company-echo-timer))
1631 (setq company-echo-timer (run-with-timer company-echo-delay nil
1632 'company-echo-show getter)))
1634 (defun company-echo-format ()
1636 (let ((limit (window-width (minibuffer-window)))
1638 ;; Roll to selection.
1639 (candidates (nthcdr company-selection company-candidates))
1640 (i (if company-show-numbers company-selection 99999))
1644 (setq comp (company-reformat (pop candidates))
1645 len (+ len 1 (length comp)))
1649 (setq comp (propertize (format "%d: %s" i comp)
1650 'face 'company-echo))
1653 (add-text-properties 3 (+ 3 (length company-common))
1654 '(face company-echo-common) comp))
1655 (setq comp (propertize comp 'face 'company-echo))
1656 (add-text-properties 0 (length company-common)
1657 '(face company-echo-common) comp))
1659 (setq candidates nil)
1662 (mapconcat 'identity (nreverse msg) " ")))
1664 (defun company-echo-strip-common-format ()
1666 (let ((limit (window-width (minibuffer-window)))
1667 (len (+ (length company-prefix) 2))
1668 ;; Roll to selection.
1669 (candidates (nthcdr company-selection company-candidates))
1670 (i (if company-show-numbers company-selection 99999))
1674 (setq comp (company-strip-prefix (pop candidates))
1675 len (+ len 2 (length comp)))
1678 (setq comp (format "%s (%d)" comp i))
1682 (setq candidates nil)
1683 (push (propertize comp 'face 'company-echo) msg)))
1685 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1686 (mapconcat 'identity (nreverse msg) ", ")
1689 (defun company-echo-hide ()
1690 (when company-echo-timer
1691 (cancel-timer company-echo-timer))
1692 (unless (equal company-echo-last-msg "")
1693 (setq company-echo-last-msg "")
1694 (company-echo-show)))
1696 (defun company-echo-frontend (command)
1697 "A `company-mode' front-end showing the candidates in the echo area."
1699 ('pre-command (company-echo-show-soon))
1700 ('post-command (company-echo-show-soon 'company-echo-format))
1701 ('hide (company-echo-hide))))
1703 (defun company-echo-strip-common-frontend (command)
1704 "A `company-mode' front-end showing the candidates in the echo area."
1706 ('pre-command (company-echo-show-soon))
1707 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1708 ('hide (company-echo-hide))))
1710 (defun company-echo-metadata-frontend (command)
1711 "A `company-mode' front-end showing the documentation in the echo area."
1713 ('pre-command (company-echo-show-soon))
1714 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1715 ('hide (company-echo-hide))))
1718 ;;; company.el ends here