]> code.delx.au - gnu-emacs-elpa/blob - company.el
Don't start when region is active.
[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 "\e\e\e" 'company-abort)
340 (define-key keymap "\C-g" 'company-abort)
341 (define-key keymap (kbd "M-n") 'company-select-next)
342 (define-key keymap (kbd "M-p") 'company-select-previous)
343 (define-key keymap (kbd "<down>") 'company-select-next)
344 (define-key keymap (kbd "<up>") 'company-select-previous)
345 (define-key keymap [down-mouse-1] 'ignore)
346 (define-key keymap [down-mouse-3] 'ignore)
347 (define-key keymap [mouse-1] 'company-complete-mouse)
348 (define-key keymap [mouse-3] 'company-select-mouse)
349 (define-key keymap [up-mouse-1] 'ignore)
350 (define-key keymap [up-mouse-3] 'ignore)
351 (define-key keymap "\C-m" 'company-complete-selection)
352 (define-key keymap "\t" 'company-complete-common)
353 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
354 (define-key keymap "\C-w" 'company-show-location)
355 (define-key keymap "\C-s" 'company-search-candidates)
356 (define-key keymap "\C-\M-s" 'company-filter-candidates)
357 (dotimes (i 10)
358 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
359 `(lambda () (interactive) (company-complete-number ,i))))
360
361 keymap)
362 "Keymap that is enabled during an active completion.")
363
364 ;;;###autoload
365 (define-minor-mode company-mode
366 "\"complete anything\"; in in-buffer completion framework.
367 Completion starts automatically, depending on the values
368 `company-idle-delay' and `company-minimum-prefix-length'.
369
370 Completion can be controlled with the commands:
371 `company-complete-common', `company-complete-selection', `company-complete',
372 `company-select-next', `company-select-previous'. If these commands are
373 called before `company-idle-delay', completion will also start.
374
375 Completions can be searched with `company-search-candidates' or
376 `company-filter-candidates'. These can be used while completion is
377 inactive, as well.
378
379 The completion data is retrieved using `company-backends' and displayed using
380 `company-frontends'.
381
382 regular keymap (`company-mode-map'):
383
384 \\{company-mode-map}
385 keymap during active completions (`company-active-map'):
386
387 \\{company-active-map}"
388 nil " comp" company-mode-map
389 (if company-mode
390 (progn
391 (add-hook 'pre-command-hook 'company-pre-command nil t)
392 (add-hook 'post-command-hook 'company-post-command nil t)
393 (dolist (backend company-backends)
394 (unless (fboundp backend)
395 (ignore-errors (require backend nil t)))
396 (unless (fboundp backend)
397 (message "Company back-end '%s' could not be initialized"
398 backend))))
399 (remove-hook 'pre-command-hook 'company-pre-command t)
400 (remove-hook 'post-command-hook 'company-post-command t)
401 (company-cancel)
402 (kill-local-variable 'company-point)))
403
404 (defsubst company-assert-enabled ()
405 (unless company-mode
406 (company-uninstall-map)
407 (error "Company not enabled")))
408
409 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410
411 (defvar company-overriding-keymap-bound nil)
412 (make-variable-buffer-local 'company-overriding-keymap-bound)
413
414 (defvar company-old-keymap nil)
415 (make-variable-buffer-local 'company-old-keymap)
416
417 (defvar company-my-keymap nil)
418 (make-variable-buffer-local 'company-my-keymap)
419
420 (defsubst company-enable-overriding-keymap (keymap)
421 (setq company-my-keymap keymap)
422 (when company-overriding-keymap-bound
423 (company-uninstall-map)))
424
425 (defun company-install-map ()
426 (unless (or company-overriding-keymap-bound
427 (null company-my-keymap))
428 (setq company-old-keymap overriding-terminal-local-map
429 overriding-terminal-local-map company-my-keymap
430 company-overriding-keymap-bound t)))
431
432 (defun company-uninstall-map ()
433 (when (eq overriding-terminal-local-map company-my-keymap)
434 (setq overriding-terminal-local-map company-old-keymap
435 company-overriding-keymap-bound nil)))
436
437 ;; Hack:
438 ;; Emacs calculates the active keymaps before reading the event. That means we
439 ;; cannot change the keymap from a timer. So we send a bogus command.
440 (defun company-ignore ()
441 (interactive))
442
443 (global-set-key '[31415926] 'company-ignore)
444
445 (defun company-input-noop ()
446 (push 31415926 unread-command-events))
447
448 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
449
450 (defun company-grab (regexp &optional expression)
451 (when (looking-back regexp)
452 (or (match-string-no-properties (or expression 0)) "")))
453
454 (defun company-in-string-or-comment (&optional point)
455 (let ((pos (syntax-ppss)))
456 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
457
458 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
459
460 (defvar company-backend nil)
461 (make-variable-buffer-local 'company-backend)
462
463 (defvar company-prefix nil)
464 (make-variable-buffer-local 'company-prefix)
465
466 (defvar company-candidates nil)
467 (make-variable-buffer-local 'company-candidates)
468
469 (defvar company-candidates-length nil)
470 (make-variable-buffer-local 'company-candidates-length)
471
472 (defvar company-candidates-cache nil)
473 (make-variable-buffer-local 'company-candidates-cache)
474
475 (defvar company-candidates-predicate nil)
476 (make-variable-buffer-local 'company-candidates-predicate)
477
478 (defvar company-common nil)
479 (make-variable-buffer-local 'company-common)
480
481 (defvar company-selection 0)
482 (make-variable-buffer-local 'company-selection)
483
484 (defvar company-selection-changed nil)
485 (make-variable-buffer-local 'company-selection-changed)
486
487 (defvar company--explicit-action nil
488 "Non-nil, if explicit completion took place.")
489 (make-variable-buffer-local 'company--explicit-action)
490
491 (defvar company-point nil)
492 (make-variable-buffer-local 'company-point)
493
494 (defvar company-timer nil)
495
496 (defvar company-added-newline nil)
497 (make-variable-buffer-local 'company-added-newline)
498
499 (defsubst company-strip-prefix (str)
500 (substring str (length company-prefix)))
501
502 (defun company-explicit-action-p ()
503 "Return whether explicit completion action was taken by the user."
504 (or company--explicit-action
505 company-selection-changed))
506
507 (defsubst company-reformat (candidate)
508 ;; company-ispell needs this, because the results are always lower-case
509 ;; It's mory efficient to fix it only when they are displayed.
510 (concat company-prefix (substring candidate (length company-prefix))))
511
512 (defsubst company-should-complete (prefix)
513 (and (eq company-idle-delay t)
514 (not (and transient-mark-mode mark-active))
515 (>= (length prefix) company-minimum-prefix-length)))
516
517 (defsubst company-call-frontends (command)
518 (dolist (frontend company-frontends)
519 (condition-case err
520 (funcall frontend command)
521 (error (error "Company: Front-end %s error \"%s\" on command %s"
522 frontend (error-message-string err) command)))))
523
524 (defsubst company-set-selection (selection &optional force-update)
525 (setq selection (max 0 (min (1- company-candidates-length) selection)))
526 (when (or force-update (not (equal selection company-selection)))
527 (setq company-selection selection
528 company-selection-changed t)
529 (company-call-frontends 'update)))
530
531 (defun company-apply-predicate (candidates predicate)
532 (let (new)
533 (dolist (c candidates)
534 (when (funcall predicate c)
535 (push c new)))
536 (nreverse new)))
537
538 (defun company-update-candidates (candidates)
539 (setq company-candidates-length (length candidates))
540 (if (> company-selection 0)
541 ;; Try to restore the selection
542 (let ((selected (nth company-selection company-candidates)))
543 (setq company-selection 0
544 company-candidates candidates)
545 (when selected
546 (while (and candidates (string< (pop candidates) selected))
547 (incf company-selection))
548 (unless candidates
549 ;; Make sure selection isn't out of bounds.
550 (setq company-selection (min (1- company-candidates-length)
551 company-selection)))))
552 (setq company-selection 0
553 company-candidates candidates))
554 ;; Save in cache:
555 (push (cons company-prefix company-candidates) company-candidates-cache)
556 ;; Calculate common.
557 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
558 (setq company-common (try-completion company-prefix company-candidates)))
559 (when (eq company-common t)
560 (setq company-candidates nil)))
561
562 (defun company-calculate-candidates (prefix)
563 (let ((candidates
564 (or (cdr (assoc prefix company-candidates-cache))
565 (when company-candidates-cache
566 (let ((len (length prefix))
567 (completion-ignore-case (funcall company-backend
568 'ignore-case))
569 prev)
570 (dotimes (i len)
571 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
572 company-candidates-cache)))
573 (return (all-completions prefix prev))))))
574 (let ((c (funcall company-backend 'candidates prefix)))
575 (when company-candidates-predicate
576 (setq c (company-apply-predicate
577 c company-candidates-predicate)))
578 (unless (funcall company-backend 'sorted)
579 (setq c (sort c 'string<)))
580 c))))
581 (if (or (cdr candidates)
582 (not (equal (car candidates) prefix)))
583 ;; Don't start when already completed and unique.
584 candidates
585 ;; Not the right place? maybe when setting?
586 (and company-candidates t))))
587
588 (defun company-idle-begin (buf win tick pos)
589 (and company-mode
590 (eq buf (current-buffer))
591 (eq win (selected-window))
592 (eq tick (buffer-chars-modified-tick))
593 (eq pos (point))
594 (not company-candidates)
595 (not (equal (point) company-point))
596 (let ((company-idle-delay t))
597 (company-begin)
598 (when company-candidates
599 (company-input-noop)
600 (company-post-command)))))
601
602 (defun company-manual-begin ()
603 (interactive)
604 (company-assert-enabled)
605 (and company-mode
606 (not company-candidates)
607 (let ((company-idle-delay t)
608 (company-minimum-prefix-length 0))
609 (setq company--explicit-action t)
610 (company-begin)))
611 ;; Return non-nil if active.
612 company-candidates)
613
614 (defsubst company-incremental-p (old-prefix new-prefix)
615 (and (> (length new-prefix) (length old-prefix))
616 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
617
618 (defun company-require-match-p ()
619 (let ((backend-value (funcall company-backend 'require-match)))
620 (or (eq backend-value t)
621 (and (if (functionp company-require-match)
622 (funcall company-require-match)
623 (eq company-require-match t))
624 (not (eq backend-value 'never))))))
625
626 (defun company-continue ()
627 (when company-candidates
628 (when (funcall company-backend 'no-cache company-prefix)
629 ;; Don't complete existing candidates, fetch new ones.
630 (setq company-candidates-cache nil))
631 (let ((new-prefix (funcall company-backend 'prefix)))
632 (unless (and (= (- (point) (length new-prefix))
633 (- company-point (length company-prefix)))
634 (or (equal company-prefix new-prefix)
635 (let ((c (company-calculate-candidates new-prefix)))
636 ;; t means complete/unique.
637 (if (eq c t)
638 (progn (company-cancel new-prefix) t)
639 (when (consp c)
640 (setq company-prefix new-prefix)
641 (company-update-candidates c)
642 t)))))
643 (if (not (and (company-incremental-p company-prefix new-prefix)
644 (company-require-match-p)))
645 (progn
646 (when (equal company-prefix (car company-candidates))
647 ;; cancel, but last input was actually success
648 (company-cancel company-prefix))
649 (setq company-candidates nil))
650 (backward-delete-char (length new-prefix))
651 (insert company-prefix)
652 (ding)
653 (message "Matching input is required")
654 company-candidates)))))
655
656 (defun company-begin ()
657 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
658 ;; Don't complete in these cases.
659 (setq company-candidates nil)
660 (company-continue)
661 (unless company-candidates
662 (let (prefix)
663 (dolist (backend company-backends)
664 (when (and (fboundp backend)
665 (setq prefix (funcall backend 'prefix)))
666 (setq company-backend backend)
667 (when (company-should-complete prefix)
668 (let ((c (company-calculate-candidates prefix)))
669 ;; t means complete/unique. We don't start, so no hooks.
670 (when (consp c)
671 (setq company-prefix prefix)
672 (company-update-candidates c)
673 (run-hook-with-args 'company-completion-started-hook
674 (company-explicit-action-p))
675 (company-call-frontends 'show))))
676 (return prefix))))))
677 (if company-candidates
678 (progn
679 (when (and company-end-of-buffer-workaround (eobp))
680 (save-excursion (insert "\n"))
681 (setq company-added-newline (buffer-chars-modified-tick)))
682 (setq company-point (point))
683 (company-enable-overriding-keymap company-active-map)
684 (company-call-frontends 'update))
685 (company-cancel)))
686
687 (defun company-cancel (&optional result)
688 (and company-added-newline
689 (> (point-max) (point-min))
690 (let ((tick (buffer-chars-modified-tick)))
691 (delete-region (1- (point-max)) (point-max))
692 (equal tick company-added-newline))
693 ;; Only set unmodified when tick remained the same since insert.
694 (set-buffer-modified-p nil))
695 (when company-prefix
696 (if (stringp result)
697 (run-hook-with-args 'company-completion-finished-hook result)
698 (run-hook-with-args 'company-completion-cancelled-hook result)))
699 (setq company-added-newline nil
700 company-backend nil
701 company-prefix nil
702 company-candidates nil
703 company-candidates-length nil
704 company-candidates-cache nil
705 company-candidates-predicate nil
706 company-common nil
707 company-selection 0
708 company-selection-changed nil
709 company--explicit-action nil
710 company-point nil)
711 (when company-timer
712 (cancel-timer company-timer))
713 (company-search-mode 0)
714 (company-call-frontends 'hide)
715 (company-enable-overriding-keymap nil))
716
717 (defun company-abort ()
718 (interactive)
719 (company-cancel t)
720 ;; Don't start again, unless started manually.
721 (setq company-point (point)))
722
723 (defun company-finish (result)
724 (insert (company-strip-prefix result))
725 (company-cancel result)
726 ;; Don't start again, unless started manually.
727 (setq company-point (point)))
728
729 (defsubst company-keep (command)
730 (and (symbolp command) (get command 'company-keep)))
731
732 (defun company-pre-command ()
733 (unless (company-keep this-command)
734 (condition-case err
735 (when company-candidates
736 (company-call-frontends 'pre-command))
737 (error (message "Company: An error occurred in pre-command")
738 (message "%s" (error-message-string err))
739 (company-cancel))))
740 (when company-timer
741 (cancel-timer company-timer))
742 (company-uninstall-map))
743
744 (defun company-post-command ()
745 (unless (company-keep this-command)
746 (condition-case err
747 (progn
748 (unless (equal (point) company-point)
749 (company-begin))
750 (when company-candidates
751 (company-call-frontends 'post-command))
752 (when (numberp company-idle-delay)
753 (setq company-timer
754 (run-with-timer company-idle-delay nil 'company-idle-begin
755 (current-buffer) (selected-window)
756 (buffer-chars-modified-tick) (point)))))
757 (error (message "Company: An error occurred in post-command")
758 (message "%s" (error-message-string err))
759 (company-cancel))))
760 (company-install-map))
761
762 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
763
764 (defvar company-search-string nil)
765 (make-variable-buffer-local 'company-search-string)
766
767 (defvar company-search-lighter " Search: \"\"")
768 (make-variable-buffer-local 'company-search-lighter)
769
770 (defvar company-search-old-map nil)
771 (make-variable-buffer-local 'company-search-old-map)
772
773 (defvar company-search-old-selection 0)
774 (make-variable-buffer-local 'company-search-old-selection)
775
776 (defun company-search (text lines)
777 (let ((quoted (regexp-quote text))
778 (i 0))
779 (dolist (line lines)
780 (when (string-match quoted line (length company-prefix))
781 (return i))
782 (incf i))))
783
784 (defun company-search-printing-char ()
785 (interactive)
786 (company-search-assert-enabled)
787 (setq company-search-string
788 (concat (or company-search-string "") (string last-command-event))
789 company-search-lighter (concat " Search: \"" company-search-string
790 "\""))
791 (let ((pos (company-search company-search-string
792 (nthcdr company-selection company-candidates))))
793 (if (null pos)
794 (ding)
795 (company-set-selection (+ company-selection pos) t))))
796
797 (defun company-search-repeat-forward ()
798 "Repeat the incremental search in completion candidates forward."
799 (interactive)
800 (company-search-assert-enabled)
801 (let ((pos (company-search company-search-string
802 (cdr (nthcdr company-selection
803 company-candidates)))))
804 (if (null pos)
805 (ding)
806 (company-set-selection (+ company-selection pos 1) t))))
807
808 (defun company-search-repeat-backward ()
809 "Repeat the incremental search in completion candidates backwards."
810 (interactive)
811 (company-search-assert-enabled)
812 (let ((pos (company-search company-search-string
813 (nthcdr (- company-candidates-length
814 company-selection)
815 (reverse company-candidates)))))
816 (if (null pos)
817 (ding)
818 (company-set-selection (- company-selection pos 1) t))))
819
820 (defun company-create-match-predicate ()
821 (setq company-candidates-predicate
822 `(lambda (candidate)
823 ,(if company-candidates-predicate
824 `(and (string-match ,company-search-string candidate)
825 (funcall ,company-candidates-predicate
826 candidate))
827 `(string-match ,company-search-string candidate))))
828 (company-update-candidates
829 (company-apply-predicate company-candidates company-candidates-predicate))
830 ;; Invalidate cache.
831 (setq company-candidates-cache (cons company-prefix company-candidates)))
832
833 (defun company-filter-printing-char ()
834 (interactive)
835 (company-search-assert-enabled)
836 (company-search-printing-char)
837 (company-create-match-predicate)
838 (company-call-frontends 'update))
839
840 (defun company-search-kill-others ()
841 "Limit the completion candidates to the ones matching the search string."
842 (interactive)
843 (company-search-assert-enabled)
844 (company-create-match-predicate)
845 (company-search-mode 0)
846 (company-call-frontends 'update))
847
848 (defun company-search-abort ()
849 "Abort searching the completion candidates."
850 (interactive)
851 (company-search-assert-enabled)
852 (company-set-selection company-search-old-selection t)
853 (company-search-mode 0))
854
855 (defun company-search-other-char ()
856 (interactive)
857 (company-search-assert-enabled)
858 (company-search-mode 0)
859 (when last-input-event
860 (clear-this-command-keys t)
861 (setq unread-command-events (list last-input-event))))
862
863 (defvar company-search-map
864 (let ((i 0)
865 (keymap (make-keymap)))
866 (if (fboundp 'max-char)
867 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
868 'company-search-printing-char)
869 (with-no-warnings
870 ;; obselete in Emacs 23
871 (let ((l (generic-character-list))
872 (table (nth 1 keymap)))
873 (while l
874 (set-char-table-default table (car l) 'company-search-printing-char)
875 (setq l (cdr l))))))
876 (define-key keymap [t] 'company-search-other-char)
877 (while (< i ?\s)
878 (define-key keymap (make-string 1 i) 'company-search-other-char)
879 (incf i))
880 (while (< i 256)
881 (define-key keymap (vector i) 'company-search-printing-char)
882 (incf i))
883 (let ((meta-map (make-sparse-keymap)))
884 (define-key keymap (char-to-string meta-prefix-char) meta-map)
885 (define-key keymap [escape] meta-map))
886 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
887 (define-key keymap "\e\e\e" 'company-search-other-char)
888 (define-key keymap [escape escape escape] 'company-search-other-char)
889
890 (define-key keymap "\C-g" 'company-search-abort)
891 (define-key keymap "\C-s" 'company-search-repeat-forward)
892 (define-key keymap "\C-r" 'company-search-repeat-backward)
893 (define-key keymap "\C-o" 'company-search-kill-others)
894 keymap)
895 "Keymap used for incrementally searching the completion candidates.")
896
897 (define-minor-mode company-search-mode
898 "Search mode for completion candidates.
899 Don't start this directly, use `company-search-candidates' or
900 `company-filter-candidates'."
901 nil company-search-lighter nil
902 (if company-search-mode
903 (if (company-manual-begin)
904 (progn
905 (setq company-search-old-selection company-selection)
906 (company-call-frontends 'update))
907 (setq company-search-mode nil))
908 (kill-local-variable 'company-search-string)
909 (kill-local-variable 'company-search-lighter)
910 (kill-local-variable 'company-search-old-selection)
911 (company-enable-overriding-keymap company-active-map)))
912
913 (defsubst company-search-assert-enabled ()
914 (company-assert-enabled)
915 (unless company-search-mode
916 (company-uninstall-map)
917 (error "Company not in search mode")))
918
919 (defun company-search-candidates ()
920 "Start searching the completion candidates incrementally.
921
922 \\<company-search-map>Search can be controlled with the commands:
923 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
924 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
925 - `company-search-abort' (\\[company-search-abort])
926
927 Regular characters are appended to the search string.
928
929 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
930 the search string to limit the completion candidates."
931 (interactive)
932 (company-search-mode 1)
933 (company-enable-overriding-keymap company-search-map))
934
935 (defvar company-filter-map
936 (let ((keymap (make-keymap)))
937 (define-key keymap [remap company-search-printing-char]
938 'company-filter-printing-char)
939 (set-keymap-parent keymap company-search-map)
940 keymap)
941 "Keymap used for incrementally searching the completion candidates.")
942
943 (defun company-filter-candidates ()
944 "Start filtering the completion candidates incrementally.
945 This works the same way as `company-search-candidates' immediately
946 followed by `company-search-kill-others' after each input."
947 (interactive)
948 (company-search-mode 1)
949 (company-enable-overriding-keymap company-filter-map))
950
951 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
952
953 (defun company-select-next ()
954 "Select the next candidate in the list."
955 (interactive)
956 (when (company-manual-begin)
957 (company-set-selection (1+ company-selection))))
958
959 (defun company-select-previous ()
960 "Select the previous candidate in the list."
961 (interactive)
962 (when (company-manual-begin)
963 (company-set-selection (1- company-selection))))
964
965 (defun company-select-mouse (event)
966 "Select the candidate picked by the mouse."
967 (interactive "e")
968 (when (nth 4 (event-start event))
969 (company-set-selection (- (cdr (posn-col-row (event-start event)))
970 (cdr (posn-col-row (posn-at-point)))
971 1))
972 t))
973
974 (defun company-complete-mouse (event)
975 "Complete the candidate picked by the mouse."
976 (interactive "e")
977 (when (company-select-mouse event)
978 (company-complete-selection)))
979
980 (defun company-complete-selection ()
981 "Complete the selected candidate."
982 (interactive)
983 (when (company-manual-begin)
984 (company-finish (nth company-selection company-candidates))))
985
986 (defun company-complete-common ()
987 "Complete the common part of all candidates."
988 (interactive)
989 (when (company-manual-begin)
990 (if (equal company-common (car company-candidates))
991 ;; for success message
992 (company-complete-selection)
993 (insert (company-strip-prefix company-common)))))
994
995 (defun company-complete ()
996 "Complete the common part of all candidates or the current selection.
997 The first time this is called, the common part is completed, the second time, or
998 when the selection has been changed, the selected candidate is completed."
999 (interactive)
1000 (when (company-manual-begin)
1001 (if (or company-selection-changed
1002 (eq last-command 'company-complete-common))
1003 (call-interactively 'company-complete-selection)
1004 (call-interactively 'company-complete-common)
1005 (setq this-command 'company-complete-common))))
1006
1007 (defun company-complete-number (n)
1008 "Complete the Nth candidate."
1009 (when (company-manual-begin)
1010 (and (< n 1) (> n company-candidates-length)
1011 (error "No candidate number %d" n))
1012 (decf n)
1013 (company-finish (nth n company-candidates))))
1014
1015 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1016
1017 (defconst company-space-strings-limit 100)
1018
1019 (defconst company-space-strings
1020 (let (lst)
1021 (dotimes (i company-space-strings-limit)
1022 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1023 (apply 'vector lst)))
1024
1025 (defsubst company-space-string (len)
1026 (if (< len company-space-strings-limit)
1027 (aref company-space-strings len)
1028 (make-string len ?\ )))
1029
1030 (defsubst company-safe-substring (str from &optional to)
1031 (let ((len (length str)))
1032 (if (> from len)
1033 ""
1034 (if (and to (> to len))
1035 (concat (substring str from)
1036 (company-space-string (- to len)))
1037 (substring str from to)))))
1038
1039 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1040
1041 (defvar company-last-metadata nil)
1042 (make-variable-buffer-local 'company-last-metadata)
1043
1044 (defun company-fetch-metadata ()
1045 (let ((selected (nth company-selection company-candidates)))
1046 (unless (equal selected (car company-last-metadata))
1047 (setq company-last-metadata
1048 (cons selected (funcall company-backend 'meta selected))))
1049 (cdr company-last-metadata)))
1050
1051 (defun company-doc-buffer (&optional string)
1052 (with-current-buffer (get-buffer-create "*Company meta-data*")
1053 (erase-buffer)
1054 (current-buffer)))
1055
1056 (defmacro company-electric (&rest body)
1057 (declare (indent 0) (debug t))
1058 `(when (company-manual-begin)
1059 (save-window-excursion
1060 (let ((height (window-height))
1061 (row (cdr (posn-col-row (posn-at-point)))))
1062 ,@body
1063 (and (< (window-height) height)
1064 (< (- (window-height) row 2) company-tooltip-limit)
1065 (recenter (- (window-height) row 2)))
1066 (while (eq 'scroll-other-window
1067 (key-binding (vector (list (read-event)))))
1068 (call-interactively 'scroll-other-window))
1069 (when last-input-event
1070 (clear-this-command-keys t)
1071 (setq unread-command-events (list last-input-event)))))))
1072
1073 (defun company-show-doc-buffer ()
1074 "Temporarily show a buffer with the complete documentation for the selection."
1075 (interactive)
1076 (company-electric
1077 (let ((selected (nth company-selection company-candidates)))
1078 (display-buffer (or (funcall company-backend 'doc-buffer selected)
1079 (error "No documentation available")) t))))
1080 (put 'company-show-doc-buffer 'company-keep t)
1081
1082 (defun company-show-location ()
1083 "Temporarily display a buffer showing the selected candidate in context."
1084 (interactive)
1085 (company-electric
1086 (let* ((selected (nth company-selection company-candidates))
1087 (location (funcall company-backend 'location selected))
1088 (pos (or (cdr location) (error "No location available")))
1089 (buffer (or (and (bufferp (car location)) (car location))
1090 (find-file-noselect (car location) t))))
1091 (with-selected-window (display-buffer buffer t)
1092 (if (bufferp (car location))
1093 (goto-char pos)
1094 (goto-line pos))
1095 (set-window-start nil (point))))))
1096 (put 'company-show-location 'company-keep t)
1097
1098 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1099
1100 (defvar company-pseudo-tooltip-overlay nil)
1101 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1102
1103 (defvar company-tooltip-offset 0)
1104 (make-variable-buffer-local 'company-tooltip-offset)
1105
1106 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1107
1108 (decf limit 2)
1109 (setq company-tooltip-offset
1110 (max (min selection company-tooltip-offset)
1111 (- selection -1 limit)))
1112
1113 (when (<= company-tooltip-offset 1)
1114 (incf limit)
1115 (setq company-tooltip-offset 0))
1116
1117 (when (>= company-tooltip-offset (- num-lines limit 1))
1118 (incf limit)
1119 (when (= selection (1- num-lines))
1120 (decf company-tooltip-offset)
1121 (when (<= company-tooltip-offset 1)
1122 (setq company-tooltip-offset 0)
1123 (incf limit))))
1124
1125 limit)
1126
1127 ;;; propertize
1128
1129 (defsubst company-round-tab (arg)
1130 (* (/ (+ arg tab-width) tab-width) tab-width))
1131
1132 (defun company-untabify (str)
1133 (let* ((pieces (split-string str "\t"))
1134 (copy pieces))
1135 (while (cdr copy)
1136 (setcar copy (company-safe-substring
1137 (car copy) 0 (company-round-tab (string-width (car copy)))))
1138 (pop copy))
1139 (apply 'concat pieces)))
1140
1141 (defun company-fill-propertize (line width selected)
1142 (setq line (company-safe-substring line 0 width))
1143 (add-text-properties 0 width '(face company-tooltip
1144 mouse-face company-tooltip-mouse)
1145 line)
1146 (add-text-properties 0 (length company-common)
1147 '(face company-tooltip-common
1148 mouse-face company-tooltip-mouse)
1149 line)
1150 (when selected
1151 (if (and company-search-string
1152 (string-match (regexp-quote company-search-string) line
1153 (length company-prefix)))
1154 (progn
1155 (add-text-properties (match-beginning 0) (match-end 0)
1156 '(face company-tooltip-selection)
1157 line)
1158 (when (< (match-beginning 0) (length company-common))
1159 (add-text-properties (match-beginning 0) (length company-common)
1160 '(face company-tooltip-common-selection)
1161 line)))
1162 (add-text-properties 0 width '(face company-tooltip-selection
1163 mouse-face company-tooltip-selection)
1164 line)
1165 (add-text-properties 0 (length company-common)
1166 '(face company-tooltip-common-selection
1167 mouse-face company-tooltip-selection)
1168 line)))
1169 line)
1170
1171 ;;; replace
1172
1173 (defun company-buffer-lines (beg end)
1174 (goto-char beg)
1175 (let ((row (cdr (posn-col-row (posn-at-point))))
1176 lines)
1177 (while (and (equal (move-to-window-line (incf row)) row)
1178 (<= (point) end))
1179 (push (buffer-substring beg (min end (1- (point)))) lines)
1180 (setq beg (point)))
1181 (unless (eq beg end)
1182 (push (buffer-substring beg end) lines))
1183 (nreverse lines)))
1184
1185 (defsubst company-modify-line (old new offset)
1186 (concat (company-safe-substring old 0 offset)
1187 new
1188 (company-safe-substring old (+ offset (length new)))))
1189
1190 (defun company-replacement-string (old lines column nl)
1191 (let (new)
1192 ;; Inject into old lines.
1193 (while old
1194 (push (company-modify-line (pop old) (pop lines) column) new))
1195 ;; Append whole new lines.
1196 (while lines
1197 (push (concat (company-space-string column) (pop lines)) new))
1198 (concat (when nl "\n")
1199 (mapconcat 'identity (nreverse new) "\n")
1200 "\n")))
1201
1202 (defun company-create-lines (column selection limit)
1203
1204 (let ((len company-candidates-length)
1205 (numbered 99999)
1206 lines
1207 width
1208 lines-copy
1209 previous
1210 remainder
1211 new)
1212
1213 ;; Scroll to offset.
1214 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1215
1216 (when (> company-tooltip-offset 0)
1217 (setq previous (format "...(%d)" company-tooltip-offset)))
1218
1219 (setq remainder (- len limit company-tooltip-offset)
1220 remainder (when (> remainder 0)
1221 (setq remainder (format "...(%d)" remainder))))
1222
1223 (decf selection company-tooltip-offset)
1224 (setq width (min (length previous) (length remainder))
1225 lines (nthcdr company-tooltip-offset company-candidates)
1226 len (min limit len)
1227 lines-copy lines)
1228
1229 (dotimes (i len)
1230 (setq width (max (length (pop lines-copy)) width)))
1231 (setq width (min width (- (window-width) column)))
1232
1233 (setq lines-copy lines)
1234
1235 ;; number can make tooltip too long
1236 (and company-show-numbers
1237 (< (setq numbered company-tooltip-offset) 10)
1238 (incf width 2))
1239
1240 (when previous
1241 (push (propertize (company-safe-substring previous 0 width)
1242 'face 'company-tooltip)
1243 new))
1244
1245 (dotimes (i len)
1246 (push (company-fill-propertize
1247 (if (>= numbered 10)
1248 (company-reformat (pop lines))
1249 (incf numbered)
1250 (format "%s %d"
1251 (company-safe-substring (company-reformat (pop lines))
1252 0 (- width 2))
1253 (mod numbered 10)))
1254 width (equal i selection))
1255 new))
1256
1257 (when remainder
1258 (push (propertize (company-safe-substring remainder 0 width)
1259 'face 'company-tooltip)
1260 new))
1261
1262 (setq lines (nreverse new))))
1263
1264 ;; show
1265
1266 (defsubst company-pseudo-tooltip-height ()
1267 "Calculate the appropriate tooltip height."
1268 (max 3 (min company-tooltip-limit
1269 (- (window-height) 2
1270 (count-lines (window-start) (point-at-bol))))))
1271
1272 (defun company-pseudo-tooltip-show (row column selection)
1273 (company-pseudo-tooltip-hide)
1274 (save-excursion
1275
1276 (move-to-column 0)
1277
1278 (let* ((height (company-pseudo-tooltip-height))
1279 (lines (company-create-lines column selection height))
1280 (nl (< (move-to-window-line row) row))
1281 (beg (point))
1282 (end (save-excursion
1283 (move-to-window-line (+ row height))
1284 (point)))
1285 (old-string
1286 (mapcar 'company-untabify (company-buffer-lines beg end)))
1287 str)
1288
1289 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1290
1291 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1292 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1293 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1294 (overlay-put company-pseudo-tooltip-overlay 'company-before
1295 (company-replacement-string old-string lines column nl))
1296 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1297
1298 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1299
1300 (defun company-pseudo-tooltip-show-at-point (pos)
1301 (let ((col-row (posn-col-row (posn-at-point pos))))
1302 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1303
1304 (defun company-pseudo-tooltip-edit (lines selection)
1305 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1306 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1307 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1308 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1309 (lines (company-create-lines column selection height)))
1310 (overlay-put company-pseudo-tooltip-overlay 'company-before
1311 (company-replacement-string old-string lines column nl))))
1312
1313 (defun company-pseudo-tooltip-hide ()
1314 (when company-pseudo-tooltip-overlay
1315 (delete-overlay company-pseudo-tooltip-overlay)
1316 (setq company-pseudo-tooltip-overlay nil)))
1317
1318 (defun company-pseudo-tooltip-hide-temporarily ()
1319 (when (overlayp company-pseudo-tooltip-overlay)
1320 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1321 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1322
1323 (defun company-pseudo-tooltip-unhide ()
1324 (when company-pseudo-tooltip-overlay
1325 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1326 (overlay-put company-pseudo-tooltip-overlay 'before-string
1327 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1328 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1329
1330 (defun company-pseudo-tooltip-frontend (command)
1331 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1332 (case command
1333 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1334 ('post-command
1335 (unless (and (overlayp company-pseudo-tooltip-overlay)
1336 (equal (overlay-get company-pseudo-tooltip-overlay
1337 'company-height)
1338 (company-pseudo-tooltip-height)))
1339 ;; Redraw needed.
1340 (company-pseudo-tooltip-show-at-point (- (point)
1341 (length company-prefix))))
1342 (company-pseudo-tooltip-unhide))
1343 ('hide (company-pseudo-tooltip-hide)
1344 (setq company-tooltip-offset 0))
1345 ('update (when (overlayp company-pseudo-tooltip-overlay)
1346 (company-pseudo-tooltip-edit company-candidates
1347 company-selection)))))
1348
1349 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1350 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1351 (unless (and (eq command 'post-command)
1352 (not (cdr company-candidates)))
1353 (company-pseudo-tooltip-frontend command)))
1354
1355 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1356
1357 (defvar company-preview-overlay nil)
1358 (make-variable-buffer-local 'company-preview-overlay)
1359
1360 (defun company-preview-show-at-point (pos)
1361 (company-preview-hide)
1362
1363 (setq company-preview-overlay (make-overlay pos pos))
1364
1365 (let ((completion(nth company-selection company-candidates)))
1366 (setq completion (propertize completion 'face 'company-preview))
1367 (add-text-properties 0 (length company-common)
1368 '(face company-preview-common) completion)
1369
1370 ;; Add search string
1371 (and company-search-string
1372 (string-match (regexp-quote company-search-string) completion)
1373 (add-text-properties (match-beginning 0)
1374 (match-end 0)
1375 '(face company-preview-search)
1376 completion))
1377
1378 (setq completion (company-strip-prefix completion))
1379
1380 (and (equal pos (point))
1381 (not (equal completion ""))
1382 (add-text-properties 0 1 '(cursor t) completion))
1383
1384 (overlay-put company-preview-overlay 'after-string completion)
1385 (overlay-put company-preview-overlay 'window (selected-window))))
1386
1387 (defun company-preview-hide ()
1388 (when company-preview-overlay
1389 (delete-overlay company-preview-overlay)
1390 (setq company-preview-overlay nil)))
1391
1392 (defun company-preview-frontend (command)
1393 "A `company-mode' front-end showing the selection as if it had been inserted."
1394 (case command
1395 ('pre-command (company-preview-hide))
1396 ('post-command (company-preview-show-at-point (point)))
1397 ('hide (company-preview-hide))))
1398
1399 (defun company-preview-if-just-one-frontend (command)
1400 "`company-preview-frontend', but only shown for single candidates."
1401 (unless (and (eq command 'post-command)
1402 (cdr company-candidates))
1403 (company-preview-frontend command)))
1404
1405 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1406
1407 (defvar company-echo-last-msg nil)
1408 (make-variable-buffer-local 'company-echo-last-msg)
1409
1410 (defvar company-echo-timer nil)
1411
1412 (defvar company-echo-delay .1)
1413
1414 (defun company-echo-show (&optional getter)
1415 (when getter
1416 (setq company-echo-last-msg (funcall getter)))
1417 (let ((message-log-max nil))
1418 (if company-echo-last-msg
1419 (message "%s" company-echo-last-msg)
1420 (message ""))))
1421
1422 (defsubst company-echo-show-soon (&optional getter)
1423 (when company-echo-timer
1424 (cancel-timer company-echo-timer))
1425 (setq company-echo-timer (run-with-timer company-echo-delay nil
1426 'company-echo-show getter)))
1427
1428 (defun company-echo-format ()
1429
1430 (let ((limit (window-width (minibuffer-window)))
1431 (len -1)
1432 ;; Roll to selection.
1433 (candidates (nthcdr company-selection company-candidates))
1434 (i (if company-show-numbers company-selection 99999))
1435 comp msg)
1436
1437 (while candidates
1438 (setq comp (company-reformat (pop candidates))
1439 len (+ len 1 (length comp)))
1440 (if (< i 10)
1441 ;; Add number.
1442 (progn
1443 (setq comp (propertize (format "%d: %s" i comp)
1444 'face 'company-echo))
1445 (incf len 3)
1446 (incf i)
1447 (add-text-properties 3 (+ 3 (length company-common))
1448 '(face company-echo-common) comp))
1449 (setq comp (propertize comp 'face 'company-echo))
1450 (add-text-properties 0 (length company-common)
1451 '(face company-echo-common) comp))
1452 (if (>= len limit)
1453 (setq candidates nil)
1454 (push comp msg)))
1455
1456 (mapconcat 'identity (nreverse msg) " ")))
1457
1458 (defun company-echo-strip-common-format ()
1459
1460 (let ((limit (window-width (minibuffer-window)))
1461 (len (+ (length company-prefix) 2))
1462 ;; Roll to selection.
1463 (candidates (nthcdr company-selection company-candidates))
1464 (i (if company-show-numbers company-selection 99999))
1465 msg comp)
1466
1467 (while candidates
1468 (setq comp (company-strip-prefix (pop candidates))
1469 len (+ len 2 (length comp)))
1470 (when (< i 10)
1471 ;; Add number.
1472 (setq comp (format "%s (%d)" comp i))
1473 (incf len 4)
1474 (incf i))
1475 (if (>= len limit)
1476 (setq candidates nil)
1477 (push (propertize comp 'face 'company-echo) msg)))
1478
1479 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1480 (mapconcat 'identity (nreverse msg) ", ")
1481 "}")))
1482
1483 (defun company-echo-hide ()
1484 (when company-echo-timer
1485 (cancel-timer company-echo-timer))
1486 (unless (equal company-echo-last-msg "")
1487 (setq company-echo-last-msg "")
1488 (company-echo-show)))
1489
1490 (defun company-echo-frontend (command)
1491 "A `company-mode' front-end showing the candidates in the echo area."
1492 (case command
1493 ('pre-command (company-echo-show-soon))
1494 ('post-command (company-echo-show-soon 'company-echo-format))
1495 ('hide (company-echo-hide))))
1496
1497 (defun company-echo-strip-common-frontend (command)
1498 "A `company-mode' front-end showing the candidates in the echo area."
1499 (case command
1500 ('pre-command (company-echo-show-soon))
1501 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1502 ('hide (company-echo-hide))))
1503
1504 (defun company-echo-metadata-frontend (command)
1505 "A `company-mode' front-end showing the documentation in the echo area."
1506 (case command
1507 ('pre-command (company-echo-show-soon))
1508 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1509 ('hide (company-echo-hide))))
1510
1511 (provide 'company)
1512 ;;; company.el ends here