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