]> code.delx.au - gnu-emacs-elpa/blob - company.el
Added hooks.
[gnu-emacs-elpa] / company.el
1 ;;; company.el --- extensible inline text completion mechanism
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; Author: Nikolaj Schumacher <bugs * nschum de>
6 ;; Version: 0.2.1
7 ;; Keywords: abbrev, convenience, matchis
8 ;; URL: http://nschum.de/src/emacs/company/
9 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
10 ;;
11 ;; This file is NOT part of GNU Emacs.
12 ;;
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.
17 ;;
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.
22 ;;
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/>.
25 ;;
26 ;;; Commentary:
27 ;;
28 ;; Company is a modular completion mechanism. Modules for retrieving completion
29 ;; candidates are called back-ends, modules for displaying them are front-ends.
30 ;;
31 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
32 ;; distributed in individual files and can be used individually.
33 ;;
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)
38 ;;
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)
41 ;;
42 ;; To write your own back-end, look at the documentation for `company-backends'.
43 ;; Here is a simple example completing "foo":
44 ;;
45 ;; (defun company-my-backend (command &optional arg &rest ignored)
46 ;; (case command
47 ;; ('prefix (when (looking-back "foo\\>")
48 ;; (match-string 0)))
49 ;; ('candidates (list "foobar" "foobaz" "foobarbaz"))
50 ;; ('meta (format "This value is named %s" arg))))
51 ;;
52 ;; Sometimes it is a good idea to mix two back-ends together, for example to
53 ;; enrich gtags with dabbrev text (to emulate local variables):
54 ;;
55 ;; (defun gtags-gtags-dabbrev-backend (command &optional arg &rest ignored)
56 ;; (case command
57 ;; (prefix (company-gtags 'prefix))
58 ;; (candidates (append (company-gtags 'candidates arg)
59 ;; (company-dabbrev 'candidates arg)))))
60 ;;
61 ;; Known Issues:
62 ;; When point is at the very end of the buffer, the pseudo-tooltip appears very
63 ;; wrong, unless company is allowed to temporarily insert a fake newline.
64 ;; This behavior is enabled by `company-end-of-buffer-workaround'.
65 ;;
66 ;;; Change Log:
67 ;;
68 ;; Added hooks.
69 ;; Added `company-require-match' option.
70 ;;
71 ;; 2009-04-05 (0.2.1)
72 ;; Improved Emacs Lisp back-end behavior for local variables.
73 ;; Added `company-elisp-detect-function-context' option.
74 ;; The mouse can now be used for selection.
75 ;;
76 ;; 2009-03-22 (0.2)
77 ;; Added `company-show-location'.
78 ;; Added etags back-end.
79 ;; Added work-around for end-of-buffer bug.
80 ;; Added `company-filter-candidates'.
81 ;; More local Lisp variables are now included in the candidates.
82 ;;
83 ;; 2009-03-21 (0.1.5)
84 ;; Fixed elisp documentation buffer always showing the same doc.
85 ;; Added `company-echo-strip-common-frontend'.
86 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
87 ;; Don't hide the echo message if it isn't shown.
88 ;;
89 ;; 2009-03-20 (0.1)
90 ;; Initial release.
91 ;;
92 ;;; Code:
93
94 (eval-when-compile (require 'cl))
95
96 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
97 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
98 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
99 (add-to-list 'debug-ignored-errors "^Company not ")
100 (add-to-list 'debug-ignored-errors "^No candidate number ")
101
102 (defgroup company nil
103 "Extensible inline text completion mechanism"
104 :group 'abbrev
105 :group 'convenience
106 :group 'maching)
107
108 (defface company-tooltip
109 '((t :background "yellow"
110 :foreground "black"))
111 "*Face used for the tool tip."
112 :group 'company)
113
114 (defface company-tooltip-selection
115 '((default :inherit company-tooltip)
116 (((class color) (min-colors 88)) (:background "orange1"))
117 (t (:background "green")))
118 "*Face used for the selection in the tool tip."
119 :group 'company)
120
121 (defface company-tooltip-mouse
122 '((default :inherit highlight))
123 "*Face used for the tool tip item under the mouse."
124 :group 'company)
125
126 (defface company-tooltip-common
127 '((t :inherit company-tooltip
128 :foreground "red"))
129 "*Face used for the common completion in the tool tip."
130 :group 'company)
131
132 (defface company-tooltip-common-selection
133 '((t :inherit company-tooltip-selection
134 :foreground "red"))
135 "*Face used for the selected common completion in the tool tip."
136 :group 'company)
137
138 (defcustom company-tooltip-limit 10
139 "*The maximum number of candidates in the tool tip"
140 :group 'company
141 :type 'integer)
142
143 (defface company-preview
144 '((t :background "blue4"
145 :foreground "wheat"))
146 "*Face used for the completion preview."
147 :group 'company)
148
149 (defface company-preview-common
150 '((t :inherit company-preview
151 :foreground "red"))
152 "*Face used for the common part of the completion preview."
153 :group 'company)
154
155 (defface company-preview-search
156 '((t :inherit company-preview
157 :background "blue1"))
158 "*Face used for the search string in the completion preview."
159 :group 'company)
160
161 (defface company-echo nil
162 "*Face used for completions in the echo area."
163 :group 'company)
164
165 (defface company-echo-common
166 '((((background dark)) (:foreground "firebrick1"))
167 (((background light)) (:background "firebrick4")))
168 "*Face used for the common part of completions in the echo area."
169 :group 'company)
170
171 (defun company-frontends-set (variable value)
172 ;; uniquify
173 (let ((remainder value))
174 (setcdr remainder (delq (car remainder) (cdr remainder))))
175 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
176 (memq 'company-pseudo-tooltip-frontend value)
177 (error "Pseudo tooltip frontend cannot be used twice"))
178 (and (memq 'company-preview-if-just-one-frontend value)
179 (memq 'company-preview-frontend value)
180 (error "Preview frontend cannot be used twice"))
181 (and (memq 'company-echo value)
182 (memq 'company-echo-metadata-frontend value)
183 (error "Echo area cannot be used twice"))
184 ;; preview must come last
185 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
186 (when (memq f value)
187 (setq value (append (delq f value) (list f)))))
188 (set variable value))
189
190 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
191 company-preview-frontend
192 company-echo-metadata-frontend)
193 "*The list of active front-ends (visualizations).
194 Each front-end is a function that takes one argument. It is called with
195 one of the following arguments:
196
197 'show: When the visualization should start.
198
199 'hide: When the visualization should end.
200
201 'update: When the data has been updated.
202
203 'pre-command: Before every command that is executed while the
204 visualization is active.
205
206 'post-command: After every command that is executed while the
207 visualization is active.
208
209 The visualized data is stored in `company-prefix', `company-candidates',
210 `company-common', `company-selection', `company-point' and
211 `company-search-string'."
212 :set 'company-frontends-set
213 :group 'company
214 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
215 (const :tag "echo, strip common"
216 company-echo-strip-common-frontend)
217 (const :tag "show echo meta-data in echo"
218 company-echo-metadata-frontend)
219 (const :tag "pseudo tooltip"
220 company-pseudo-tooltip-frontend)
221 (const :tag "pseudo tooltip, multiple only"
222 company-pseudo-tooltip-unless-just-one-frontend)
223 (const :tag "preview" company-preview-frontend)
224 (const :tag "preview, unique only"
225 company-preview-if-just-one-frontend)
226 (function :tag "custom function" nil))))
227
228 (defcustom company-backends '(company-elisp company-nxml company-css
229 company-semantic company-gtags company-etags
230 company-oddmuse company-files company-dabbrev)
231 "*The list of active back-ends (completion engines).
232 Each back-end is a function that takes a variable number of arguments.
233 The first argument is the command requested from the back-end. It is one
234 of the following:
235
236 'prefix: The back-end should return the text to be completed. It must be
237 text immediately before `point'. Returning nil passes control to the next
238 back-end.
239
240 'candidates: The second argument is the prefix to be completed. The
241 return value should be a list of candidates that start with the prefix.
242
243 Optional commands:
244
245 'sorted: The back-end may return t here to indicate that the candidates
246 are sorted and will not need to be sorted again.
247
248 'no-cache: Usually company doesn't ask for candidates again as completion
249 progresses, unless the back-end returns t for this command. The second
250 argument is the latest prefix.
251
252 'meta: The second argument is a completion candidate. The back-end should
253 return a (short) documentation string for it.
254
255 'doc-buffer: The second argument is a completion candidate. The back-end should
256 create a buffer (preferably with `company-doc-buffer'), fill it with
257 documentation and return it.
258
259 'location: The second argument is a completion candidate. The back-end can
260 return the cons of buffer and buffer location, or of file and line
261 number where the completion candidate was defined.
262
263 'require-match: If this value is t, the user is not allowed to enter anything
264 not offering as a candidate. Use with care! The default value nil gives the
265 user that choice with `company-require-match'. Return value 'never overrides
266 that option the other way around.
267
268 The back-end should return nil for all commands it does not support or
269 does not know about."
270 :group 'company
271 :type '(repeat (function :tag "function" nil)))
272
273 (defvar start-count 0)
274
275 (defcustom company-completion-started-hook nil
276 "*Hook run when company starts completing.
277 The hook is called with one argument that is non-nil if the completion was
278 started manually."
279 :group 'company
280 :type 'hook)
281
282 (defcustom company-completion-cancelled-hook nil
283 "*Hook run when company cancels completing.
284 The hook is called with one argument that is non-nil if the completion was
285 aborted manually."
286 :group 'company
287 :type 'hook)
288
289 (defcustom company-completion-finished-hook nil
290 "*Hook run when company successfully completes.
291 The hook is called with the selected candidate as an argument."
292 :group 'company
293 :type 'hook)
294
295 (defcustom company-minimum-prefix-length 3
296 "*The minimum prefix length for automatic completion."
297 :group 'company
298 :type '(integer :tag "prefix length"))
299
300 (defcustom company-require-match 'company-explicit-action-p
301 "*If enabled, disallow non-matching input.
302 This can be a function do determine if a match is required.
303
304 This can be overridden by the back-end, if it returns t or 'never to
305 'require-match."
306 :group 'company
307 :type '(choice (const :tag "Off" nil)
308 (function :tag "Predicate function")
309 (const :tag "On, if user interaction took place"
310 'company-explicit-action-p)
311 (const :tag "On" t)))
312
313 (defcustom company-idle-delay .7
314 "*The idle delay in seconds until automatic completions starts.
315 A value of nil means never complete automatically, t means complete
316 immediately when a prefix of `company-minimum-prefix-length' is reached."
317 :group 'company
318 :type '(choice (const :tag "never (nil)" nil)
319 (const :tag "immediate (t)" t)
320 (number :tag "seconds")))
321
322 (defcustom company-show-numbers nil
323 "*If enabled, show quick-access numbers for the first ten candidates."
324 :group 'company
325 :type '(choice (const :tag "off" nil)
326 (const :tag "on" t)))
327
328 (defvar company-end-of-buffer-workaround t
329 "*Work around a visualization bug when completing at the end of the buffer.
330 The work-around consists of adding a newline.")
331
332 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
333
334 (defvar company-mode-map (make-sparse-keymap)
335 "Keymap used by `company-mode'.")
336
337 (defvar company-active-map
338 (let ((keymap (make-sparse-keymap)))
339 (define-key keymap (kbd "M-n") 'company-select-next)
340 (define-key keymap (kbd "M-p") 'company-select-previous)
341 (define-key keymap (kbd "<down>") 'company-select-next)
342 (define-key keymap (kbd "<up>") 'company-select-previous)
343 (define-key keymap [down-mouse-1] 'ignore)
344 (define-key keymap [down-mouse-3] 'ignore)
345 (define-key keymap [mouse-1] 'company-complete-mouse)
346 (define-key keymap [mouse-3] 'company-select-mouse)
347 (define-key keymap [up-mouse-1] 'ignore)
348 (define-key keymap [up-mouse-3] 'ignore)
349 (define-key keymap "\C-m" 'company-complete-selection)
350 (define-key keymap "\t" 'company-complete-common)
351 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
352 (define-key keymap "\C-w" 'company-show-location)
353 (define-key keymap "\C-s" 'company-search-candidates)
354 (define-key keymap "\C-\M-s" 'company-filter-candidates)
355 (dotimes (i 10)
356 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
357 `(lambda () (interactive) (company-complete-number ,i))))
358
359 keymap)
360 "Keymap that is enabled during an active completion.")
361
362 ;;;###autoload
363 (define-minor-mode company-mode
364 "\"complete anything\"; in in-buffer completion framework.
365 Completion starts automatically, depending on the values
366 `company-idle-delay' and `company-minimum-prefix-length'.
367
368 Completion can be controlled with the commands:
369 `company-complete-common', `company-complete-selection', `company-complete',
370 `company-select-next', `company-select-previous'. If these commands are
371 called before `company-idle-delay', completion will also start.
372
373 Completions can be searched with `company-search-candidates' or
374 `company-filter-candidates'. These can be used while completion is
375 inactive, as well.
376
377 The completion data is retrieved using `company-backends' and displayed using
378 `company-frontends'.
379
380 regular keymap (`company-mode-map'):
381
382 \\{company-mode-map}
383 keymap during active completions (`company-active-map'):
384
385 \\{company-active-map}"
386 nil " comp" company-mode-map
387 (if company-mode
388 (progn
389 (add-hook 'pre-command-hook 'company-pre-command nil t)
390 (add-hook 'post-command-hook 'company-post-command nil t)
391 (dolist (backend company-backends)
392 (unless (fboundp backend)
393 (ignore-errors (require backend nil t)))
394 (unless (fboundp backend)
395 (message "Company back-end '%s' could not be initialized"
396 backend))))
397 (remove-hook 'pre-command-hook 'company-pre-command t)
398 (remove-hook 'post-command-hook 'company-post-command t)
399 (company-cancel)
400 (kill-local-variable 'company-point)))
401
402 (defsubst company-assert-enabled ()
403 (unless company-mode
404 (company-uninstall-map)
405 (error "Company not enabled")))
406
407 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
408
409 (defvar company-overriding-keymap-bound nil)
410 (make-variable-buffer-local 'company-overriding-keymap-bound)
411
412 (defvar company-old-keymap nil)
413 (make-variable-buffer-local 'company-old-keymap)
414
415 (defvar company-my-keymap nil)
416 (make-variable-buffer-local 'company-my-keymap)
417
418 (defsubst company-enable-overriding-keymap (keymap)
419 (setq company-my-keymap keymap)
420 (when company-overriding-keymap-bound
421 (company-uninstall-map)))
422
423 (defun company-install-map ()
424 (unless (or company-overriding-keymap-bound
425 (null company-my-keymap))
426 (setq company-old-keymap overriding-terminal-local-map
427 overriding-terminal-local-map company-my-keymap
428 company-overriding-keymap-bound t)))
429
430 (defun company-uninstall-map ()
431 (when (eq overriding-terminal-local-map company-my-keymap)
432 (setq overriding-terminal-local-map company-old-keymap
433 company-overriding-keymap-bound nil)))
434
435 ;; Hack:
436 ;; Emacs calculates the active keymaps before reading the event. That means we
437 ;; cannot change the keymap from a timer. So we send a bogus command.
438 (defun company-ignore ()
439 (interactive))
440
441 (global-set-key '[31415926] 'company-ignore)
442
443 (defun company-input-noop ()
444 (push 31415926 unread-command-events))
445
446 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
447
448 (defun company-grab (regexp &optional expression)
449 (when (looking-back regexp)
450 (or (match-string-no-properties (or expression 0)) "")))
451
452 (defun company-in-string-or-comment (&optional point)
453 (let ((pos (syntax-ppss)))
454 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
455
456 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
457
458 (defvar company-backend nil)
459 (make-variable-buffer-local 'company-backend)
460
461 (defvar company-prefix nil)
462 (make-variable-buffer-local 'company-prefix)
463
464 (defvar company-candidates nil)
465 (make-variable-buffer-local 'company-candidates)
466
467 (defvar company-candidates-length nil)
468 (make-variable-buffer-local 'company-candidates-length)
469
470 (defvar company-candidates-cache nil)
471 (make-variable-buffer-local 'company-candidates-cache)
472
473 (defvar company-candidates-predicate nil)
474 (make-variable-buffer-local 'company-candidates-predicate)
475
476 (defvar company-common nil)
477 (make-variable-buffer-local 'company-common)
478
479 (defvar company-selection 0)
480 (make-variable-buffer-local 'company-selection)
481
482 (defvar company-selection-changed nil)
483 (make-variable-buffer-local 'company-selection-changed)
484
485 (defvar company--explicit-action nil
486 "Non-nil, if explicit completion took place.")
487 (make-variable-buffer-local 'company--explicit-action)
488
489 (defvar company-point nil)
490 (make-variable-buffer-local 'company-point)
491
492 (defvar company-timer nil)
493
494 (defvar company-added-newline nil)
495 (make-variable-buffer-local 'company-added-newline)
496
497 (defsubst company-strip-prefix (str)
498 (substring str (length company-prefix)))
499
500 (defun company-explicit-action-p ()
501 "Return whether explicit completion action was taken by the user."
502 (or company--explicit-action
503 company-selection-changed))
504
505 (defsubst company-reformat (candidate)
506 ;; company-ispell needs this, because the results are always lower-case
507 ;; It's mory efficient to fix it only when they are displayed.
508 (concat company-prefix (substring candidate (length company-prefix))))
509
510 (defsubst company-should-complete (prefix)
511 (and (eq company-idle-delay t)
512 (>= (length prefix) company-minimum-prefix-length)))
513
514 (defsubst company-call-frontends (command)
515 (dolist (frontend company-frontends)
516 (condition-case err
517 (funcall frontend command)
518 (error (error "Company: Front-end %s error \"%s\" on command %s"
519 frontend (error-message-string err) command)))))
520
521 (defsubst company-set-selection (selection &optional force-update)
522 (setq selection (max 0 (min (1- company-candidates-length) selection)))
523 (when (or force-update (not (equal selection company-selection)))
524 (setq company-selection selection
525 company-selection-changed t)
526 (company-call-frontends 'update)))
527
528 (defun company-apply-predicate (candidates predicate)
529 (let (new)
530 (dolist (c candidates)
531 (when (funcall predicate c)
532 (push c new)))
533 (nreverse new)))
534
535 (defun company-update-candidates (candidates)
536 (setq company-candidates-length (length candidates))
537 (if (> company-selection 0)
538 ;; Try to restore the selection
539 (let ((selected (nth company-selection company-candidates)))
540 (setq company-selection 0
541 company-candidates candidates)
542 (when selected
543 (while (and candidates (string< (pop candidates) selected))
544 (incf company-selection))
545 (unless candidates
546 ;; Make sure selection isn't out of bounds.
547 (setq company-selection (min (1- company-candidates-length)
548 company-selection)))))
549 (setq company-selection 0
550 company-candidates candidates))
551 ;; Save in cache:
552 (push (cons company-prefix company-candidates) company-candidates-cache)
553 ;; Calculate common.
554 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
555 (setq company-common (try-completion company-prefix company-candidates)))
556 (when (eq company-common t)
557 (setq company-candidates nil)))
558
559 (defun company-calculate-candidates (prefix)
560 (or (cdr (assoc prefix company-candidates-cache))
561 (when company-candidates-cache
562 (let ((len (length prefix))
563 (completion-ignore-case (funcall company-backend 'ignore-case))
564 prev)
565 (dotimes (i len)
566 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
567 company-candidates-cache)))
568 (return (all-completions prefix prev))))))
569 (let ((candidates (funcall company-backend 'candidates prefix)))
570 (when company-candidates-predicate
571 (setq candidates
572 (company-apply-predicate candidates
573 company-candidates-predicate)))
574 (unless (funcall company-backend 'sorted)
575 (setq candidates (sort candidates 'string<)))
576 (when (or (cdr candidates)
577 (not (equal (car candidates) prefix)))
578 ;; Don't start when already completed and unique.
579 candidates))))
580
581 (defun company-idle-begin (buf win tick pos)
582 (and company-mode
583 (eq buf (current-buffer))
584 (eq win (selected-window))
585 (eq tick (buffer-chars-modified-tick))
586 (eq pos (point))
587 (not company-candidates)
588 (not (equal (point) company-point))
589 (let ((company-idle-delay t))
590 (company-begin)
591 (when company-candidates
592 (company-input-noop)
593 (company-post-command)))))
594
595 (defun company-manual-begin ()
596 (interactive)
597 (company-assert-enabled)
598 (and company-mode
599 (not company-candidates)
600 (let ((company-idle-delay t)
601 (company-minimum-prefix-length 0))
602 (setq company--explicit-action t)
603 (company-begin)))
604 ;; Return non-nil if active.
605 company-candidates)
606
607 (defsubst company-incremental-p (old-prefix new-prefix)
608 (and (> (length new-prefix) (length old-prefix))
609 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
610
611 (defun company-require-match-p ()
612 (let ((backend-value (funcall company-backend 'require-match)))
613 (or (eq backend-value t)
614 (and (if (functionp company-require-match)
615 (funcall company-require-match)
616 (eq company-require-match t))
617 (not (eq backend-value 'never))))))
618
619 (defun company-continue ()
620 (when company-candidates
621 (when (funcall company-backend 'no-cache company-prefix)
622 ;; Don't complete existing candidates, fetch new ones.
623 (setq company-candidates-cache nil))
624 (let ((new-prefix (funcall company-backend 'prefix)))
625 (unless (and (= (- (point) (length new-prefix))
626 (- company-point (length company-prefix)))
627 (or (equal company-prefix new-prefix)
628 (let ((c (company-calculate-candidates new-prefix)))
629 (when c
630 (setq company-prefix new-prefix)
631 (company-update-candidates c)
632 t))))
633 (if (not (and (company-incremental-p company-prefix new-prefix)
634 (company-require-match-p)))
635 (setq company-candidates nil)
636 (backward-delete-char (length new-prefix))
637 (insert company-prefix)
638 (ding)
639 (message "Matching input is required")
640 company-candidates)))))
641
642 (defun company-begin ()
643 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
644 ;; Don't complete in these cases.
645 (setq company-candidates nil)
646 (company-continue)
647 (unless company-candidates
648 (let (prefix)
649 (dolist (backend company-backends)
650 (when (and (fboundp backend)
651 (setq prefix (funcall backend 'prefix)))
652 (setq company-backend backend)
653 (when (company-should-complete prefix)
654 (let ((c (company-calculate-candidates prefix)))
655 (when c
656 (setq company-prefix prefix)
657 (company-update-candidates c)
658 (run-hook-with-args 'company-completion-started-hook
659 (company-explicit-action-p))
660 (company-call-frontends 'show))))
661 (return prefix))))))
662 (if company-candidates
663 (progn
664 (when (and company-end-of-buffer-workaround (eobp))
665 (save-excursion (insert "\n"))
666 (setq company-added-newline (buffer-chars-modified-tick)))
667 (setq company-point (point))
668 (company-enable-overriding-keymap company-active-map)
669 (company-call-frontends 'update))
670 (company-cancel)))
671
672 (defun company-cancel (&optional result)
673 (and company-added-newline
674 (> (point-max) (point-min))
675 (let ((tick (buffer-chars-modified-tick)))
676 (delete-region (1- (point-max)) (point-max))
677 (equal tick company-added-newline))
678 ;; Only set unmodified when tick remained the same since insert.
679 (set-buffer-modified-p nil))
680 (when company-prefix
681 (if (stringp result)
682 (run-hook-with-args 'company-completion-finished-hook result)
683 (run-hook-with-args 'company-completion-cancelled-hook result)))
684 (setq company-added-newline nil
685 company-backend nil
686 company-prefix nil
687 company-candidates nil
688 company-candidates-length nil
689 company-candidates-cache nil
690 company-candidates-predicate nil
691 company-common nil
692 company-selection 0
693 company-selection-changed nil
694 company--explicit-action nil
695 company-point nil)
696 (when company-timer
697 (cancel-timer company-timer))
698 (company-search-mode 0)
699 (company-call-frontends 'hide)
700 (company-enable-overriding-keymap nil))
701
702 (defun company-abort ()
703 (company-cancel t)
704 ;; Don't start again, unless started manually.
705 (setq company-point (point)))
706
707 (defun company-finish (result)
708 (insert (company-strip-prefix result))
709 (company-cancel result)
710 ;; Don't start again, unless started manually.
711 (setq company-point (point)))
712
713 (defsubst company-keep (command)
714 (and (symbolp command) (get command 'company-keep)))
715
716 (defun company-pre-command ()
717 (unless (company-keep this-command)
718 (condition-case err
719 (when company-candidates
720 (company-call-frontends 'pre-command))
721 (error (message "Company: An error occurred in pre-command")
722 (message "%s" (error-message-string err))
723 (company-cancel))))
724 (when company-timer
725 (cancel-timer company-timer))
726 (company-uninstall-map))
727
728 (defun company-post-command ()
729 (unless (company-keep this-command)
730 (condition-case err
731 (progn
732 (unless (equal (point) company-point)
733 (company-begin))
734 (when company-candidates
735 (company-call-frontends 'post-command))
736 (when (numberp company-idle-delay)
737 (setq company-timer
738 (run-with-timer company-idle-delay nil 'company-idle-begin
739 (current-buffer) (selected-window)
740 (buffer-chars-modified-tick) (point)))))
741 (error (message "Company: An error occurred in post-command")
742 (message "%s" (error-message-string err))
743 (company-cancel))))
744 (company-install-map))
745
746 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
747
748 (defvar company-search-string nil)
749 (make-variable-buffer-local 'company-search-string)
750
751 (defvar company-search-lighter " Search: \"\"")
752 (make-variable-buffer-local 'company-search-lighter)
753
754 (defvar company-search-old-map nil)
755 (make-variable-buffer-local 'company-search-old-map)
756
757 (defvar company-search-old-selection 0)
758 (make-variable-buffer-local 'company-search-old-selection)
759
760 (defun company-search (text lines)
761 (let ((quoted (regexp-quote text))
762 (i 0))
763 (dolist (line lines)
764 (when (string-match quoted line (length company-prefix))
765 (return i))
766 (incf i))))
767
768 (defun company-search-printing-char ()
769 (interactive)
770 (company-search-assert-enabled)
771 (setq company-search-string
772 (concat (or company-search-string "") (string last-command-event))
773 company-search-lighter (concat " Search: \"" company-search-string
774 "\""))
775 (let ((pos (company-search company-search-string
776 (nthcdr company-selection company-candidates))))
777 (if (null pos)
778 (ding)
779 (company-set-selection (+ company-selection pos) t))))
780
781 (defun company-search-repeat-forward ()
782 "Repeat the incremental search in completion candidates forward."
783 (interactive)
784 (company-search-assert-enabled)
785 (let ((pos (company-search company-search-string
786 (cdr (nthcdr company-selection
787 company-candidates)))))
788 (if (null pos)
789 (ding)
790 (company-set-selection (+ company-selection pos 1) t))))
791
792 (defun company-search-repeat-backward ()
793 "Repeat the incremental search in completion candidates backwards."
794 (interactive)
795 (company-search-assert-enabled)
796 (let ((pos (company-search company-search-string
797 (nthcdr (- company-candidates-length
798 company-selection)
799 (reverse company-candidates)))))
800 (if (null pos)
801 (ding)
802 (company-set-selection (- company-selection pos 1) t))))
803
804 (defun company-create-match-predicate ()
805 (setq company-candidates-predicate
806 `(lambda (candidate)
807 ,(if company-candidates-predicate
808 `(and (string-match ,company-search-string candidate)
809 (funcall ,company-candidates-predicate
810 candidate))
811 `(string-match ,company-search-string candidate))))
812 (company-update-candidates
813 (company-apply-predicate company-candidates company-candidates-predicate))
814 ;; Invalidate cache.
815 (setq company-candidates-cache (cons company-prefix company-candidates)))
816
817 (defun company-filter-printing-char ()
818 (interactive)
819 (company-search-assert-enabled)
820 (company-search-printing-char)
821 (company-create-match-predicate)
822 (company-call-frontends 'update))
823
824 (defun company-search-kill-others ()
825 "Limit the completion candidates to the ones matching the search string."
826 (interactive)
827 (company-search-assert-enabled)
828 (company-create-match-predicate)
829 (company-search-mode 0)
830 (company-call-frontends 'update))
831
832 (defun company-search-abort ()
833 "Abort searching the completion candidates."
834 (interactive)
835 (company-search-assert-enabled)
836 (company-set-selection company-search-old-selection t)
837 (company-search-mode 0))
838
839 (defun company-search-other-char ()
840 (interactive)
841 (company-search-assert-enabled)
842 (company-search-mode 0)
843 (when last-input-event
844 (clear-this-command-keys t)
845 (setq unread-command-events (list last-input-event))))
846
847 (defvar company-search-map
848 (let ((i 0)
849 (keymap (make-keymap)))
850 (if (fboundp 'max-char)
851 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
852 'company-search-printing-char)
853 (with-no-warnings
854 ;; obselete in Emacs 23
855 (let ((l (generic-character-list))
856 (table (nth 1 keymap)))
857 (while l
858 (set-char-table-default table (car l) 'company-search-printing-char)
859 (setq l (cdr l))))))
860 (define-key keymap [t] 'company-search-other-char)
861 (while (< i ?\s)
862 (define-key keymap (make-string 1 i) 'company-search-other-char)
863 (incf i))
864 (while (< i 256)
865 (define-key keymap (vector i) 'company-search-printing-char)
866 (incf i))
867 (let ((meta-map (make-sparse-keymap)))
868 (define-key keymap (char-to-string meta-prefix-char) meta-map)
869 (define-key keymap [escape] meta-map))
870 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
871 (define-key keymap "\e\e\e" 'company-search-other-char)
872 (define-key keymap [escape escape escape] 'company-search-other-char)
873
874 (define-key keymap "\C-g" 'company-search-abort)
875 (define-key keymap "\C-s" 'company-search-repeat-forward)
876 (define-key keymap "\C-r" 'company-search-repeat-backward)
877 (define-key keymap "\C-o" 'company-search-kill-others)
878 keymap)
879 "Keymap used for incrementally searching the completion candidates.")
880
881 (define-minor-mode company-search-mode
882 "Search mode for completion candidates.
883 Don't start this directly, use `company-search-candidates' or
884 `company-filter-candidates'."
885 nil company-search-lighter nil
886 (if company-search-mode
887 (if (company-manual-begin)
888 (progn
889 (setq company-search-old-selection company-selection)
890 (company-call-frontends 'update))
891 (setq company-search-mode nil))
892 (kill-local-variable 'company-search-string)
893 (kill-local-variable 'company-search-lighter)
894 (kill-local-variable 'company-search-old-selection)
895 (company-enable-overriding-keymap company-active-map)))
896
897 (defsubst company-search-assert-enabled ()
898 (company-assert-enabled)
899 (unless company-search-mode
900 (company-uninstall-map)
901 (error "Company not in search mode")))
902
903 (defun company-search-candidates ()
904 "Start searching the completion candidates incrementally.
905
906 \\<company-search-map>Search can be controlled with the commands:
907 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
908 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
909 - `company-search-abort' (\\[company-search-abort])
910
911 Regular characters are appended to the search string.
912
913 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
914 the search string to limit the completion candidates."
915 (interactive)
916 (company-search-mode 1)
917 (company-enable-overriding-keymap company-search-map))
918
919 (defvar company-filter-map
920 (let ((keymap (make-keymap)))
921 (define-key keymap [remap company-search-printing-char]
922 'company-filter-printing-char)
923 (set-keymap-parent keymap company-search-map)
924 keymap)
925 "Keymap used for incrementally searching the completion candidates.")
926
927 (defun company-filter-candidates ()
928 "Start filtering the completion candidates incrementally.
929 This works the same way as `company-search-candidates' immediately
930 followed by `company-search-kill-others' after each input."
931 (interactive)
932 (company-search-mode 1)
933 (company-enable-overriding-keymap company-filter-map))
934
935 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
936
937 (defun company-select-next ()
938 "Select the next candidate in the list."
939 (interactive)
940 (when (company-manual-begin)
941 (company-set-selection (1+ company-selection))))
942
943 (defun company-select-previous ()
944 "Select the previous candidate in the list."
945 (interactive)
946 (when (company-manual-begin)
947 (company-set-selection (1- company-selection))))
948
949 (defun company-select-mouse (event)
950 "Select the candidate picked by the mouse."
951 (interactive "e")
952 (when (nth 4 (event-start event))
953 (company-set-selection (- (cdr (posn-col-row (event-start event)))
954 (cdr (posn-col-row (posn-at-point)))
955 1))
956 t))
957
958 (defun company-complete-mouse (event)
959 "Complete the candidate picked by the mouse."
960 (interactive "e")
961 (when (company-select-mouse event)
962 (company-complete-selection)))
963
964 (defun company-complete-selection ()
965 "Complete the selected candidate."
966 (interactive)
967 (when (company-manual-begin)
968 (company-finish (nth company-selection company-candidates))))
969
970 (defun company-complete-common ()
971 "Complete the common part of all candidates."
972 (interactive)
973 (when (company-manual-begin)
974 (if (equal company-common (car company-candidates))
975 ;; for success message
976 (company-complete-selection)
977 (insert (company-strip-prefix company-common)))))
978
979 (defun company-complete ()
980 "Complete the common part of all candidates or the current selection.
981 The first time this is called, the common part is completed, the second time, or
982 when the selection has been changed, the selected candidate is completed."
983 (interactive)
984 (when (company-manual-begin)
985 (if (or company-selection-changed
986 (eq last-command 'company-complete-common))
987 (call-interactively 'company-complete-selection)
988 (call-interactively 'company-complete-common)
989 (setq this-command 'company-complete-common))))
990
991 (defun company-complete-number (n)
992 "Complete the Nth candidate."
993 (when (company-manual-begin)
994 (and (< n 1) (> n company-candidates-length)
995 (error "No candidate number %d" n))
996 (decf n)
997 (company-finish (nth n company-candidates))))
998
999 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1000
1001 (defconst company-space-strings-limit 100)
1002
1003 (defconst company-space-strings
1004 (let (lst)
1005 (dotimes (i company-space-strings-limit)
1006 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1007 (apply 'vector lst)))
1008
1009 (defsubst company-space-string (len)
1010 (if (< len company-space-strings-limit)
1011 (aref company-space-strings len)
1012 (make-string len ?\ )))
1013
1014 (defsubst company-safe-substring (str from &optional to)
1015 (let ((len (length str)))
1016 (if (> from len)
1017 ""
1018 (if (and to (> to len))
1019 (concat (substring str from)
1020 (company-space-string (- to len)))
1021 (substring str from to)))))
1022
1023 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1024
1025 (defvar company-last-metadata nil)
1026 (make-variable-buffer-local 'company-last-metadata)
1027
1028 (defun company-fetch-metadata ()
1029 (let ((selected (nth company-selection company-candidates)))
1030 (unless (equal selected (car company-last-metadata))
1031 (setq company-last-metadata
1032 (cons selected (funcall company-backend 'meta selected))))
1033 (cdr company-last-metadata)))
1034
1035 (defun company-doc-buffer (&optional string)
1036 (with-current-buffer (get-buffer-create "*Company meta-data*")
1037 (erase-buffer)
1038 (current-buffer)))
1039
1040 (defmacro company-electric (&rest body)
1041 (declare (indent 0) (debug t))
1042 `(when (company-manual-begin)
1043 (save-window-excursion
1044 (let ((height (window-height))
1045 (row (cdr (posn-col-row (posn-at-point)))))
1046 ,@body
1047 (and (< (window-height) height)
1048 (< (- (window-height) row 2) company-tooltip-limit)
1049 (recenter (- (window-height) row 2)))
1050 (while (eq 'scroll-other-window
1051 (key-binding (vector (list (read-event)))))
1052 (call-interactively 'scroll-other-window))
1053 (when last-input-event
1054 (clear-this-command-keys t)
1055 (setq unread-command-events (list last-input-event)))))))
1056
1057 (defun company-show-doc-buffer ()
1058 "Temporarily show a buffer with the complete documentation for the selection."
1059 (interactive)
1060 (company-electric
1061 (let ((selected (nth company-selection company-candidates)))
1062 (display-buffer (or (funcall company-backend 'doc-buffer selected)
1063 (error "No documentation available")) t))))
1064 (put 'company-show-doc-buffer 'company-keep t)
1065
1066 (defun company-show-location ()
1067 "Temporarily display a buffer showing the selected candidate in context."
1068 (interactive)
1069 (company-electric
1070 (let* ((selected (nth company-selection company-candidates))
1071 (location (funcall company-backend 'location selected))
1072 (pos (or (cdr location) (error "No location available")))
1073 (buffer (or (and (bufferp (car location)) (car location))
1074 (find-file-noselect (car location) t))))
1075 (with-selected-window (display-buffer buffer t)
1076 (if (bufferp (car location))
1077 (goto-char pos)
1078 (goto-line pos))
1079 (set-window-start nil (point))))))
1080 (put 'company-show-location 'company-keep t)
1081
1082 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1083
1084 (defvar company-pseudo-tooltip-overlay nil)
1085 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1086
1087 (defvar company-tooltip-offset 0)
1088 (make-variable-buffer-local 'company-tooltip-offset)
1089
1090 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1091
1092 (decf limit 2)
1093 (setq company-tooltip-offset
1094 (max (min selection company-tooltip-offset)
1095 (- selection -1 limit)))
1096
1097 (when (<= company-tooltip-offset 1)
1098 (incf limit)
1099 (setq company-tooltip-offset 0))
1100
1101 (when (>= company-tooltip-offset (- num-lines limit 1))
1102 (incf limit)
1103 (when (= selection (1- num-lines))
1104 (decf company-tooltip-offset)
1105 (when (<= company-tooltip-offset 1)
1106 (setq company-tooltip-offset 0)
1107 (incf limit))))
1108
1109 limit)
1110
1111 ;;; propertize
1112
1113 (defsubst company-round-tab (arg)
1114 (* (/ (+ arg tab-width) tab-width) tab-width))
1115
1116 (defun company-untabify (str)
1117 (let* ((pieces (split-string str "\t"))
1118 (copy pieces))
1119 (while (cdr copy)
1120 (setcar copy (company-safe-substring
1121 (car copy) 0 (company-round-tab (string-width (car copy)))))
1122 (pop copy))
1123 (apply 'concat pieces)))
1124
1125 (defun company-fill-propertize (line width selected)
1126 (setq line (company-safe-substring line 0 width))
1127 (add-text-properties 0 width '(face company-tooltip
1128 mouse-face company-tooltip-mouse)
1129 line)
1130 (add-text-properties 0 (length company-common)
1131 '(face company-tooltip-common
1132 mouse-face company-tooltip-mouse)
1133 line)
1134 (when selected
1135 (if (and company-search-string
1136 (string-match (regexp-quote company-search-string) line
1137 (length company-prefix)))
1138 (progn
1139 (add-text-properties (match-beginning 0) (match-end 0)
1140 '(face company-tooltip-selection)
1141 line)
1142 (when (< (match-beginning 0) (length company-common))
1143 (add-text-properties (match-beginning 0) (length company-common)
1144 '(face company-tooltip-common-selection)
1145 line)))
1146 (add-text-properties 0 width '(face company-tooltip-selection
1147 mouse-face company-tooltip-selection)
1148 line)
1149 (add-text-properties 0 (length company-common)
1150 '(face company-tooltip-common-selection
1151 mouse-face company-tooltip-selection)
1152 line)))
1153 line)
1154
1155 ;;; replace
1156
1157 (defun company-buffer-lines (beg end)
1158 (goto-char beg)
1159 (let ((row (cdr (posn-col-row (posn-at-point))))
1160 lines)
1161 (while (and (equal (move-to-window-line (incf row)) row)
1162 (<= (point) end))
1163 (push (buffer-substring beg (min end (1- (point)))) lines)
1164 (setq beg (point)))
1165 (unless (eq beg end)
1166 (push (buffer-substring beg end) lines))
1167 (nreverse lines)))
1168
1169 (defsubst company-modify-line (old new offset)
1170 (concat (company-safe-substring old 0 offset)
1171 new
1172 (company-safe-substring old (+ offset (length new)))))
1173
1174 (defun company-replacement-string (old lines column nl)
1175 (let (new)
1176 ;; Inject into old lines.
1177 (while old
1178 (push (company-modify-line (pop old) (pop lines) column) new))
1179 ;; Append whole new lines.
1180 (while lines
1181 (push (concat (company-space-string column) (pop lines)) new))
1182 (concat (when nl "\n")
1183 (mapconcat 'identity (nreverse new) "\n")
1184 "\n")))
1185
1186 (defun company-create-lines (column selection limit)
1187
1188 (let ((len company-candidates-length)
1189 (numbered 99999)
1190 lines
1191 width
1192 lines-copy
1193 previous
1194 remainder
1195 new)
1196
1197 ;; Scroll to offset.
1198 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1199
1200 (when (> company-tooltip-offset 0)
1201 (setq previous (format "...(%d)" company-tooltip-offset)))
1202
1203 (setq remainder (- len limit company-tooltip-offset)
1204 remainder (when (> remainder 0)
1205 (setq remainder (format "...(%d)" remainder))))
1206
1207 (decf selection company-tooltip-offset)
1208 (setq width (min (length previous) (length remainder))
1209 lines (nthcdr company-tooltip-offset company-candidates)
1210 len (min limit len)
1211 lines-copy lines)
1212
1213 (dotimes (i len)
1214 (setq width (max (length (pop lines-copy)) width)))
1215 (setq width (min width (- (window-width) column)))
1216
1217 (setq lines-copy lines)
1218
1219 ;; number can make tooltip too long
1220 (and company-show-numbers
1221 (< (setq numbered company-tooltip-offset) 10)
1222 (incf width 2))
1223
1224 (when previous
1225 (push (propertize (company-safe-substring previous 0 width)
1226 'face 'company-tooltip)
1227 new))
1228
1229 (dotimes (i len)
1230 (push (company-fill-propertize
1231 (if (>= numbered 10)
1232 (company-reformat (pop lines))
1233 (incf numbered)
1234 (format "%s %d"
1235 (company-safe-substring (company-reformat (pop lines))
1236 0 (- width 2))
1237 (mod numbered 10)))
1238 width (equal i selection))
1239 new))
1240
1241 (when remainder
1242 (push (propertize (company-safe-substring remainder 0 width)
1243 'face 'company-tooltip)
1244 new))
1245
1246 (setq lines (nreverse new))))
1247
1248 ;; show
1249
1250 (defsubst company-pseudo-tooltip-height ()
1251 "Calculate the appropriate tooltip height."
1252 (max 3 (min company-tooltip-limit
1253 (- (window-height) 2
1254 (count-lines (window-start) (point-at-bol))))))
1255
1256 (defun company-pseudo-tooltip-show (row column selection)
1257 (company-pseudo-tooltip-hide)
1258 (save-excursion
1259
1260 (move-to-column 0)
1261
1262 (let* ((height (company-pseudo-tooltip-height))
1263 (lines (company-create-lines column selection height))
1264 (nl (< (move-to-window-line row) row))
1265 (beg (point))
1266 (end (save-excursion
1267 (move-to-window-line (+ row height))
1268 (point)))
1269 (old-string
1270 (mapcar 'company-untabify (company-buffer-lines beg end)))
1271 str)
1272
1273 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1274
1275 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1276 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1277 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1278 (overlay-put company-pseudo-tooltip-overlay 'company-before
1279 (company-replacement-string old-string lines column nl))
1280 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1281
1282 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1283
1284 (defun company-pseudo-tooltip-show-at-point (pos)
1285 (let ((col-row (posn-col-row (posn-at-point pos))))
1286 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1287
1288 (defun company-pseudo-tooltip-edit (lines selection)
1289 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1290 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1291 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1292 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1293 (lines (company-create-lines column selection height)))
1294 (overlay-put company-pseudo-tooltip-overlay 'company-before
1295 (company-replacement-string old-string lines column nl))))
1296
1297 (defun company-pseudo-tooltip-hide ()
1298 (when company-pseudo-tooltip-overlay
1299 (delete-overlay company-pseudo-tooltip-overlay)
1300 (setq company-pseudo-tooltip-overlay nil)))
1301
1302 (defun company-pseudo-tooltip-hide-temporarily ()
1303 (when (overlayp company-pseudo-tooltip-overlay)
1304 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1305 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1306
1307 (defun company-pseudo-tooltip-unhide ()
1308 (when company-pseudo-tooltip-overlay
1309 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1310 (overlay-put company-pseudo-tooltip-overlay 'before-string
1311 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1312 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1313
1314 (defun company-pseudo-tooltip-frontend (command)
1315 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1316 (case command
1317 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1318 ('post-command
1319 (unless (and (overlayp company-pseudo-tooltip-overlay)
1320 (equal (overlay-get company-pseudo-tooltip-overlay
1321 'company-height)
1322 (company-pseudo-tooltip-height)))
1323 ;; Redraw needed.
1324 (company-pseudo-tooltip-show-at-point (- (point)
1325 (length company-prefix))))
1326 (company-pseudo-tooltip-unhide))
1327 ('hide (company-pseudo-tooltip-hide)
1328 (setq company-tooltip-offset 0))
1329 ('update (when (overlayp company-pseudo-tooltip-overlay)
1330 (company-pseudo-tooltip-edit company-candidates
1331 company-selection)))))
1332
1333 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1334 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1335 (unless (and (eq command 'post-command)
1336 (not (cdr company-candidates)))
1337 (company-pseudo-tooltip-frontend command)))
1338
1339 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1340
1341 (defvar company-preview-overlay nil)
1342 (make-variable-buffer-local 'company-preview-overlay)
1343
1344 (defun company-preview-show-at-point (pos)
1345 (company-preview-hide)
1346
1347 (setq company-preview-overlay (make-overlay pos pos))
1348
1349 (let ((completion(nth company-selection company-candidates)))
1350 (setq completion (propertize completion 'face 'company-preview))
1351 (add-text-properties 0 (length company-common)
1352 '(face company-preview-common) completion)
1353
1354 ;; Add search string
1355 (and company-search-string
1356 (string-match (regexp-quote company-search-string) completion)
1357 (add-text-properties (match-beginning 0)
1358 (match-end 0)
1359 '(face company-preview-search)
1360 completion))
1361
1362 (setq completion (company-strip-prefix completion))
1363
1364 (and (equal pos (point))
1365 (not (equal completion ""))
1366 (add-text-properties 0 1 '(cursor t) completion))
1367
1368 (overlay-put company-preview-overlay 'after-string completion)
1369 (overlay-put company-preview-overlay 'window (selected-window))))
1370
1371 (defun company-preview-hide ()
1372 (when company-preview-overlay
1373 (delete-overlay company-preview-overlay)
1374 (setq company-preview-overlay nil)))
1375
1376 (defun company-preview-frontend (command)
1377 "A `company-mode' front-end showing the selection as if it had been inserted."
1378 (case command
1379 ('pre-command (company-preview-hide))
1380 ('post-command (company-preview-show-at-point (point)))
1381 ('hide (company-preview-hide))))
1382
1383 (defun company-preview-if-just-one-frontend (command)
1384 "`company-preview-frontend', but only shown for single candidates."
1385 (unless (and (eq command 'post-command)
1386 (cdr company-candidates))
1387 (company-preview-frontend command)))
1388
1389 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1390
1391 (defvar company-echo-last-msg nil)
1392 (make-variable-buffer-local 'company-echo-last-msg)
1393
1394 (defvar company-echo-timer nil)
1395
1396 (defvar company-echo-delay .1)
1397
1398 (defun company-echo-show (&optional getter)
1399 (when getter
1400 (setq company-echo-last-msg (funcall getter)))
1401 (let ((message-log-max nil))
1402 (if company-echo-last-msg
1403 (message "%s" company-echo-last-msg)
1404 (message ""))))
1405
1406 (defsubst company-echo-show-soon (&optional getter)
1407 (when company-echo-timer
1408 (cancel-timer company-echo-timer))
1409 (setq company-echo-timer (run-with-timer company-echo-delay nil
1410 'company-echo-show getter)))
1411
1412 (defun company-echo-format ()
1413
1414 (let ((limit (window-width (minibuffer-window)))
1415 (len -1)
1416 ;; Roll to selection.
1417 (candidates (nthcdr company-selection company-candidates))
1418 (i (if company-show-numbers company-selection 99999))
1419 comp msg)
1420
1421 (while candidates
1422 (setq comp (company-reformat (pop candidates))
1423 len (+ len 1 (length comp)))
1424 (if (< i 10)
1425 ;; Add number.
1426 (progn
1427 (setq comp (propertize (format "%d: %s" i comp)
1428 'face 'company-echo))
1429 (incf len 3)
1430 (incf i)
1431 (add-text-properties 3 (+ 3 (length company-common))
1432 '(face company-echo-common) comp))
1433 (setq comp (propertize comp 'face 'company-echo))
1434 (add-text-properties 0 (length company-common)
1435 '(face company-echo-common) comp))
1436 (if (>= len limit)
1437 (setq candidates nil)
1438 (push comp msg)))
1439
1440 (mapconcat 'identity (nreverse msg) " ")))
1441
1442 (defun company-echo-strip-common-format ()
1443
1444 (let ((limit (window-width (minibuffer-window)))
1445 (len (+ (length company-prefix) 2))
1446 ;; Roll to selection.
1447 (candidates (nthcdr company-selection company-candidates))
1448 (i (if company-show-numbers company-selection 99999))
1449 msg comp)
1450
1451 (while candidates
1452 (setq comp (company-strip-prefix (pop candidates))
1453 len (+ len 2 (length comp)))
1454 (when (< i 10)
1455 ;; Add number.
1456 (setq comp (format "%s (%d)" comp i))
1457 (incf len 4)
1458 (incf i))
1459 (if (>= len limit)
1460 (setq candidates nil)
1461 (push (propertize comp 'face 'company-echo) msg)))
1462
1463 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1464 (mapconcat 'identity (nreverse msg) ", ")
1465 "}")))
1466
1467 (defun company-echo-hide ()
1468 (when company-echo-timer
1469 (cancel-timer company-echo-timer))
1470 (unless (equal company-echo-last-msg "")
1471 (setq company-echo-last-msg "")
1472 (company-echo-show)))
1473
1474 (defun company-echo-frontend (command)
1475 "A `company-mode' front-end showing the candidates in the echo area."
1476 (case command
1477 ('pre-command (company-echo-show-soon))
1478 ('post-command (company-echo-show-soon 'company-echo-format))
1479 ('hide (company-echo-hide))))
1480
1481 (defun company-echo-strip-common-frontend (command)
1482 "A `company-mode' front-end showing the candidates in the echo area."
1483 (case command
1484 ('pre-command (company-echo-show-soon))
1485 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1486 ('hide (company-echo-hide))))
1487
1488 (defun company-echo-metadata-frontend (command)
1489 "A `company-mode' front-end showing the documentation in the echo area."
1490 (case command
1491 ('pre-command (company-echo-show-soon))
1492 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1493 ('hide (company-echo-hide))))
1494
1495 (provide 'company)
1496 ;;; company.el ends here