]> code.delx.au - gnu-emacs-elpa/blob - company.el
Added company-elisp-functions-only-in-context option.
[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-functions-only-in-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
716 (defun company-filter-printing-char ()
717 (interactive)
718 (unless company-mode (error "Company not enabled"))
719 (unless company-search-mode (error "Company not in search mode"))
720 (company-search-printing-char)
721 (company-create-match-predicate)
722 (company-call-frontends 'update))
723
724 (defun company-search-kill-others ()
725 "Limit the completion candidates to the ones matching the search string."
726 (interactive)
727 (unless company-mode (error "Company not enabled"))
728 (unless company-search-mode (error "Company not in search mode"))
729 (company-create-match-predicate)
730 (company-search-mode 0)
731 (company-call-frontends 'update))
732
733 (defun company-search-abort ()
734 "Abort searching the completion candidates."
735 (interactive)
736 (unless company-mode (error "Company not enabled"))
737 (unless company-search-mode (error "Company not in search mode"))
738 (company-set-selection company-search-old-selection t)
739 (company-search-mode 0))
740
741 (defun company-search-other-char ()
742 (interactive)
743 (unless company-mode (error "Company not enabled"))
744 (unless company-search-mode (error "Company not in search mode"))
745 (company-search-mode 0)
746 (when last-input-event
747 (clear-this-command-keys t)
748 (setq unread-command-events (list last-input-event))))
749
750 (defvar company-search-map
751 (let ((i 0)
752 (keymap (make-keymap)))
753 (if (fboundp 'max-char)
754 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
755 'company-search-printing-char)
756 (with-no-warnings
757 ;; obselete in Emacs 23
758 (let ((l (generic-character-list))
759 (table (nth 1 keymap)))
760 (while l
761 (set-char-table-default table (car l) 'company-search-printing-char)
762 (setq l (cdr l))))))
763 (define-key keymap [t] 'company-search-other-char)
764 (while (< i ?\s)
765 (define-key keymap (make-string 1 i) 'company-search-other-char)
766 (incf i))
767 (while (< i 256)
768 (define-key keymap (vector i) 'company-search-printing-char)
769 (incf i))
770 (let ((meta-map (make-sparse-keymap)))
771 (define-key keymap (char-to-string meta-prefix-char) meta-map)
772 (define-key keymap [escape] meta-map))
773 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
774 (define-key keymap "\e\e\e" 'company-search-other-char)
775 (define-key keymap [escape escape escape] 'company-search-other-char)
776
777 (define-key keymap "\C-g" 'company-search-abort)
778 (define-key keymap "\C-s" 'company-search-repeat-forward)
779 (define-key keymap "\C-r" 'company-search-repeat-backward)
780 (define-key keymap "\C-o" 'company-search-kill-others)
781 keymap)
782 "Keymap used for incrementally searching the completion candidates.")
783
784 (define-minor-mode company-search-mode
785 "Search mode for completion candidates.
786 Don't start this directly, use `company-search-candidates' or
787 `company-filter-candidates'."
788 nil company-search-lighter nil
789 (if company-search-mode
790 (if (company-manual-begin)
791 (progn
792 (setq company-search-old-selection company-selection)
793 (company-call-frontends 'update))
794 (setq company-search-mode nil))
795 (kill-local-variable 'company-search-string)
796 (kill-local-variable 'company-search-lighter)
797 (kill-local-variable 'company-search-old-selection)
798 (company-enable-overriding-keymap company-active-map)))
799
800 (defun company-search-candidates ()
801 "Start searching the completion candidates incrementally.
802
803 \\<company-search-map>Search can be controlled with the commands:
804 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
805 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
806 - `company-search-abort' (\\[company-search-abort])
807
808 Regular characters are appended to the search string.
809
810 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
811 the search string to limit the completion candidates."
812 (interactive)
813 (company-search-mode 1)
814 (company-enable-overriding-keymap company-search-map))
815
816 (defvar company-filter-map
817 (let ((keymap (make-keymap)))
818 (define-key keymap [remap company-search-printing-char]
819 'company-filter-printing-char)
820 (set-keymap-parent keymap company-search-map)
821 keymap)
822 "Keymap used for incrementally searching the completion candidates.")
823
824 (defun company-filter-candidates ()
825 "Start filtering the completion candidates incrementally.
826 This works the same way as `company-search-candidates' immediately
827 followed by `company-search-kill-others' after each input."
828 (interactive)
829 (company-search-mode 1)
830 (company-enable-overriding-keymap company-filter-map))
831
832 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
833
834 (defun company-select-next ()
835 "Select the next candidate in the list."
836 (interactive)
837 (when (company-manual-begin)
838 (company-set-selection (1+ company-selection))))
839
840 (defun company-select-previous ()
841 "Select the previous candidate in the list."
842 (interactive)
843 (when (company-manual-begin)
844 (company-set-selection (1- company-selection))))
845
846 (defun company-select-mouse (event)
847 "Select the candidate picked by the mouse."
848 (interactive "e")
849 (when (nth 4 (event-start event))
850 (company-set-selection (- (cdr (posn-col-row (event-start event)))
851 (cdr (posn-col-row (posn-at-point)))
852 1))
853 t))
854
855 (defun company-complete-mouse (event)
856 "Complete the candidate picked by the mouse."
857 (interactive "e")
858 (when (company-select-mouse event)
859 (company-complete-selection)))
860
861 (defun company-complete-selection ()
862 "Complete the selected candidate."
863 (interactive)
864 (when (company-manual-begin)
865 (insert (company-strip-prefix (nth company-selection company-candidates)))
866 (company-abort)))
867
868 (defun company-complete-common ()
869 "Complete the common part of all candidates."
870 (interactive)
871 (when (company-manual-begin)
872 (insert (company-strip-prefix company-common))))
873
874 (defun company-complete ()
875 "Complete the common part of all candidates or the current selection.
876 The first time this is called, the common part is completed, the second time, or
877 when the selection has been changed, the selected candidate is completed."
878 (interactive)
879 (when (company-manual-begin)
880 (if (or company-selection-changed
881 (eq last-command 'company-complete-common))
882 (call-interactively 'company-complete-selection)
883 (call-interactively 'company-complete-common)
884 (setq this-command 'company-complete-common))))
885
886 (defun company-complete-number (n)
887 "Complete the Nth candidate."
888 (when (company-manual-begin)
889 (and (< n 1) (> n company-candidates-length)
890 (error "No candidate number %d" n))
891 (decf n)
892 (insert (company-strip-prefix (nth n company-candidates)))
893 (company-abort)))
894
895 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
896
897 (defconst company-space-strings-limit 100)
898
899 (defconst company-space-strings
900 (let (lst)
901 (dotimes (i company-space-strings-limit)
902 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
903 (apply 'vector lst)))
904
905 (defsubst company-space-string (len)
906 (if (< len company-space-strings-limit)
907 (aref company-space-strings len)
908 (make-string len ?\ )))
909
910 (defsubst company-safe-substring (str from &optional to)
911 (let ((len (length str)))
912 (if (> from len)
913 ""
914 (if (and to (> to len))
915 (concat (substring str from)
916 (company-space-string (- to len)))
917 (substring str from to)))))
918
919 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
920
921 (defvar company-last-metadata nil)
922 (make-variable-buffer-local 'company-last-metadata)
923
924 (defun company-fetch-metadata ()
925 (let ((selected (nth company-selection company-candidates)))
926 (unless (equal selected (car company-last-metadata))
927 (setq company-last-metadata
928 (cons selected (funcall company-backend 'meta selected))))
929 (cdr company-last-metadata)))
930
931 (defun company-doc-buffer (&optional string)
932 (with-current-buffer (get-buffer-create "*Company meta-data*")
933 (erase-buffer)
934 (current-buffer)))
935
936 (defmacro company-electric (&rest body)
937 (declare (indent 0) (debug t))
938 `(if company-mode
939 (when (company-manual-begin)
940 (save-window-excursion
941 (let ((height (window-height))
942 (row (cdr (posn-col-row (posn-at-point)))))
943 ,@body
944 (and (< (window-height) height)
945 (< (- (window-height) row 2) company-tooltip-limit)
946 (recenter (- (window-height) row 2)))
947 (while (eq 'scroll-other-window
948 (key-binding (vector (list (read-event)))))
949 (call-interactively 'scroll-other-window))
950 (when last-input-event
951 (clear-this-command-keys t)
952 (setq unread-command-events (list last-input-event))))))
953 (error "Company not enabled")))
954
955 (defun company-show-doc-buffer ()
956 "Temporarily show a buffer with the complete documentation for the selection."
957 (interactive)
958 (company-electric
959 (let ((selected (nth company-selection company-candidates)))
960 (display-buffer (or (funcall company-backend 'doc-buffer selected)
961 (error "No documentation available")) t))))
962 (put 'company-show-doc-buffer 'company-keep t)
963
964 (defun company-show-location ()
965 "Temporarily display a buffer showing the selected candidate in context."
966 (interactive)
967 (company-electric
968 (let* ((selected (nth company-selection company-candidates))
969 (location (funcall company-backend 'location selected))
970 (pos (or (cdr location) (error "No location available")))
971 (buffer (or (and (bufferp (car location)) (car location))
972 (find-file-noselect (car location) t))))
973 (with-selected-window (display-buffer buffer t)
974 (if (bufferp (car location))
975 (goto-char pos)
976 (goto-line pos))
977 (set-window-start nil (point))))))
978 (put 'company-show-location 'company-keep t)
979
980 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
981
982 (defvar company-pseudo-tooltip-overlay nil)
983 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
984
985 (defvar company-tooltip-offset 0)
986 (make-variable-buffer-local 'company-tooltip-offset)
987
988 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
989
990 (decf limit 2)
991 (setq company-tooltip-offset
992 (max (min selection company-tooltip-offset)
993 (- selection -1 limit)))
994
995 (when (<= company-tooltip-offset 1)
996 (incf limit)
997 (setq company-tooltip-offset 0))
998
999 (when (>= company-tooltip-offset (- num-lines limit 1))
1000 (incf limit)
1001 (when (= selection (1- num-lines))
1002 (decf company-tooltip-offset)
1003 (when (<= company-tooltip-offset 1)
1004 (setq company-tooltip-offset 0)
1005 (incf limit))))
1006
1007 limit)
1008
1009 ;;; propertize
1010
1011 (defsubst company-round-tab (arg)
1012 (* (/ (+ arg tab-width) tab-width) tab-width))
1013
1014 (defun company-untabify (str)
1015 (let* ((pieces (split-string str "\t"))
1016 (copy pieces))
1017 (while (cdr copy)
1018 (setcar copy (company-safe-substring
1019 (car copy) 0 (company-round-tab (string-width (car copy)))))
1020 (pop copy))
1021 (apply 'concat pieces)))
1022
1023 (defun company-fill-propertize (line width selected)
1024 (setq line (company-safe-substring line 0 width))
1025 (add-text-properties 0 width '(face company-tooltip
1026 mouse-face company-tooltip-mouse)
1027 line)
1028 (add-text-properties 0 (length company-common)
1029 '(face company-tooltip-common
1030 mouse-face company-tooltip-mouse)
1031 line)
1032 (when selected
1033 (if (and company-search-string
1034 (string-match (regexp-quote company-search-string) line
1035 (length company-prefix)))
1036 (progn
1037 (add-text-properties (match-beginning 0) (match-end 0)
1038 '(face company-tooltip-selection)
1039 line)
1040 (when (< (match-beginning 0) (length company-common))
1041 (add-text-properties (match-beginning 0) (length company-common)
1042 '(face company-tooltip-common-selection)
1043 line)))
1044 (add-text-properties 0 width '(face company-tooltip-selection
1045 mouse-face company-tooltip-selection)
1046 line)
1047 (add-text-properties 0 (length company-common)
1048 '(face company-tooltip-common-selection
1049 mouse-face company-tooltip-selection)
1050 line)))
1051 line)
1052
1053 ;;; replace
1054
1055 (defun company-buffer-lines (beg end)
1056 (goto-char beg)
1057 (let ((row (cdr (posn-col-row (posn-at-point))))
1058 lines)
1059 (while (and (equal (move-to-window-line (incf row)) row)
1060 (<= (point) end))
1061 (push (buffer-substring beg (min end (1- (point)))) lines)
1062 (setq beg (point)))
1063 (unless (eq beg end)
1064 (push (buffer-substring beg end) lines))
1065 (nreverse lines)))
1066
1067 (defsubst company-modify-line (old new offset)
1068 (concat (company-safe-substring old 0 offset)
1069 new
1070 (company-safe-substring old (+ offset (length new)))))
1071
1072 (defun company-replacement-string (old lines column nl)
1073 (let (new)
1074 ;; Inject into old lines.
1075 (while old
1076 (push (company-modify-line (pop old) (pop lines) column) new))
1077 ;; Append whole new lines.
1078 (while lines
1079 (push (concat (company-space-string column) (pop lines)) new))
1080 (concat (when nl "\n")
1081 (mapconcat 'identity (nreverse new) "\n")
1082 "\n")))
1083
1084 (defun company-create-lines (column selection limit)
1085
1086 (let ((len company-candidates-length)
1087 (numbered 99999)
1088 lines
1089 width
1090 lines-copy
1091 previous
1092 remainder
1093 new)
1094
1095 ;; Scroll to offset.
1096 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1097
1098 (when (> company-tooltip-offset 0)
1099 (setq previous (format "...(%d)" company-tooltip-offset)))
1100
1101 (setq remainder (- len limit company-tooltip-offset)
1102 remainder (when (> remainder 0)
1103 (setq remainder (format "...(%d)" remainder))))
1104
1105 (decf selection company-tooltip-offset)
1106 (setq width (min (length previous) (length remainder))
1107 lines (nthcdr company-tooltip-offset company-candidates)
1108 len (min limit len)
1109 lines-copy lines)
1110
1111 (dotimes (i len)
1112 (setq width (max (length (pop lines-copy)) width)))
1113 (setq width (min width (- (window-width) column)))
1114
1115 (setq lines-copy lines)
1116
1117 ;; number can make tooltip too long
1118 (and company-show-numbers
1119 (< (setq numbered company-tooltip-offset) 10)
1120 (incf width 2))
1121
1122 (when previous
1123 (push (propertize (company-safe-substring previous 0 width)
1124 'face 'company-tooltip)
1125 new))
1126
1127 (dotimes (i len)
1128 (push (company-fill-propertize
1129 (if (>= numbered 10)
1130 (company-reformat (pop lines))
1131 (incf numbered)
1132 (format "%s %d"
1133 (company-safe-substring (company-reformat (pop lines))
1134 0 (- width 2))
1135 (mod numbered 10)))
1136 width (equal i selection))
1137 new))
1138
1139 (when remainder
1140 (push (propertize (company-safe-substring remainder 0 width)
1141 'face 'company-tooltip)
1142 new))
1143
1144 (setq lines (nreverse new))))
1145
1146 ;; show
1147
1148 (defsubst company-pseudo-tooltip-height ()
1149 "Calculate the appropriate tooltip height."
1150 (max 3 (min company-tooltip-limit
1151 (- (window-height) 2
1152 (count-lines (window-start) (point-at-bol))))))
1153
1154 (defun company-pseudo-tooltip-show (row column selection)
1155 (company-pseudo-tooltip-hide)
1156 (save-excursion
1157
1158 (move-to-column 0)
1159
1160 (let* ((height (company-pseudo-tooltip-height))
1161 (lines (company-create-lines column selection height))
1162 (nl (< (move-to-window-line row) row))
1163 (beg (point))
1164 (end (save-excursion
1165 (move-to-window-line (+ row height))
1166 (point)))
1167 (old-string
1168 (mapcar 'company-untabify (company-buffer-lines beg end)))
1169 str)
1170
1171 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1172
1173 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1174 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1175 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1176 (overlay-put company-pseudo-tooltip-overlay 'company-before
1177 (company-replacement-string old-string lines column nl))
1178 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1179
1180 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1181
1182 (defun company-pseudo-tooltip-show-at-point (pos)
1183 (let ((col-row (posn-col-row (posn-at-point pos))))
1184 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1185
1186 (defun company-pseudo-tooltip-edit (lines selection)
1187 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1188 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1189 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1190 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1191 (lines (company-create-lines column selection height)))
1192 (overlay-put company-pseudo-tooltip-overlay 'company-before
1193 (company-replacement-string old-string lines column nl))))
1194
1195 (defun company-pseudo-tooltip-hide ()
1196 (when company-pseudo-tooltip-overlay
1197 (delete-overlay company-pseudo-tooltip-overlay)
1198 (setq company-pseudo-tooltip-overlay nil)))
1199
1200 (defun company-pseudo-tooltip-hide-temporarily ()
1201 (when (overlayp company-pseudo-tooltip-overlay)
1202 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1203 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1204
1205 (defun company-pseudo-tooltip-unhide ()
1206 (when company-pseudo-tooltip-overlay
1207 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1208 (overlay-put company-pseudo-tooltip-overlay 'before-string
1209 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1210 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1211
1212 (defun company-pseudo-tooltip-frontend (command)
1213 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1214 (case command
1215 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1216 ('post-command
1217 (unless (and (overlayp company-pseudo-tooltip-overlay)
1218 (equal (overlay-get company-pseudo-tooltip-overlay
1219 'company-height)
1220 (company-pseudo-tooltip-height)))
1221 ;; Redraw needed.
1222 (company-pseudo-tooltip-show-at-point (- (point)
1223 (length company-prefix))))
1224 (company-pseudo-tooltip-unhide))
1225 ('hide (company-pseudo-tooltip-hide)
1226 (setq company-tooltip-offset 0))
1227 ('update (when (overlayp company-pseudo-tooltip-overlay)
1228 (company-pseudo-tooltip-edit company-candidates
1229 company-selection)))))
1230
1231 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1232 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1233 (unless (and (eq command 'post-command)
1234 (not (cdr company-candidates)))
1235 (company-pseudo-tooltip-frontend command)))
1236
1237 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1238
1239 (defvar company-preview-overlay nil)
1240 (make-variable-buffer-local 'company-preview-overlay)
1241
1242 (defun company-preview-show-at-point (pos)
1243 (company-preview-hide)
1244
1245 (setq company-preview-overlay (make-overlay pos pos))
1246
1247 (let ((completion(nth company-selection company-candidates)))
1248 (setq completion (propertize completion 'face 'company-preview))
1249 (add-text-properties 0 (length company-common)
1250 '(face company-preview-common) completion)
1251
1252 ;; Add search string
1253 (and company-search-string
1254 (string-match (regexp-quote company-search-string) completion)
1255 (add-text-properties (match-beginning 0)
1256 (match-end 0)
1257 '(face company-preview-search)
1258 completion))
1259
1260 (setq completion (company-strip-prefix completion))
1261
1262 (and (equal pos (point))
1263 (not (equal completion ""))
1264 (add-text-properties 0 1 '(cursor t) completion))
1265
1266 (overlay-put company-preview-overlay 'after-string completion)
1267 (overlay-put company-preview-overlay 'window (selected-window))))
1268
1269 (defun company-preview-hide ()
1270 (when company-preview-overlay
1271 (delete-overlay company-preview-overlay)
1272 (setq company-preview-overlay nil)))
1273
1274 (defun company-preview-frontend (command)
1275 "A `company-mode' front-end showing the selection as if it had been inserted."
1276 (case command
1277 ('pre-command (company-preview-hide))
1278 ('post-command (company-preview-show-at-point (point)))
1279 ('hide (company-preview-hide))))
1280
1281 (defun company-preview-if-just-one-frontend (command)
1282 "`company-preview-frontend', but only shown for single candidates."
1283 (unless (and (eq command 'post-command)
1284 (cdr company-candidates))
1285 (company-preview-frontend command)))
1286
1287 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1288
1289 (defvar company-echo-last-msg nil)
1290 (make-variable-buffer-local 'company-echo-last-msg)
1291
1292 (defvar company-echo-timer nil)
1293
1294 (defvar company-echo-delay .1)
1295
1296 (defun company-echo-show (&optional getter)
1297 (when getter
1298 (setq company-echo-last-msg (funcall getter)))
1299 (let ((message-log-max nil))
1300 (if company-echo-last-msg
1301 (message "%s" company-echo-last-msg)
1302 (message ""))))
1303
1304 (defsubst company-echo-show-soon (&optional getter)
1305 (when company-echo-timer
1306 (cancel-timer company-echo-timer))
1307 (setq company-echo-timer (run-with-timer company-echo-delay nil
1308 'company-echo-show getter)))
1309
1310 (defun company-echo-format ()
1311
1312 (let ((limit (window-width (minibuffer-window)))
1313 (len -1)
1314 ;; Roll to selection.
1315 (candidates (nthcdr company-selection company-candidates))
1316 (i (if company-show-numbers company-selection 99999))
1317 comp msg)
1318
1319 (while candidates
1320 (setq comp (company-reformat (pop candidates))
1321 len (+ len 1 (length comp)))
1322 (if (< i 10)
1323 ;; Add number.
1324 (progn
1325 (setq comp (propertize (format "%d: %s" i comp)
1326 'face 'company-echo))
1327 (incf len 3)
1328 (incf i)
1329 (add-text-properties 3 (+ 3 (length company-common))
1330 '(face company-echo-common) comp))
1331 (setq comp (propertize comp 'face 'company-echo))
1332 (add-text-properties 0 (length company-common)
1333 '(face company-echo-common) comp))
1334 (if (>= len limit)
1335 (setq candidates nil)
1336 (push comp msg)))
1337
1338 (mapconcat 'identity (nreverse msg) " ")))
1339
1340 (defun company-echo-strip-common-format ()
1341
1342 (let ((limit (window-width (minibuffer-window)))
1343 (len (+ (length company-prefix) 2))
1344 ;; Roll to selection.
1345 (candidates (nthcdr company-selection company-candidates))
1346 (i (if company-show-numbers company-selection 99999))
1347 msg comp)
1348
1349 (while candidates
1350 (setq comp (company-strip-prefix (pop candidates))
1351 len (+ len 2 (length comp)))
1352 (when (< i 10)
1353 ;; Add number.
1354 (setq comp (format "%s (%d)" comp i))
1355 (incf len 4)
1356 (incf i))
1357 (if (>= len limit)
1358 (setq candidates nil)
1359 (push (propertize comp 'face 'company-echo) msg)))
1360
1361 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1362 (mapconcat 'identity (nreverse msg) ", ")
1363 "}")))
1364
1365 (defun company-echo-hide ()
1366 (when company-echo-timer
1367 (cancel-timer company-echo-timer))
1368 (unless (equal company-echo-last-msg "")
1369 (setq company-echo-last-msg "")
1370 (company-echo-show)))
1371
1372 (defun company-echo-frontend (command)
1373 "A `company-mode' front-end showing the candidates in the echo area."
1374 (case command
1375 ('pre-command (company-echo-show-soon))
1376 ('post-command (company-echo-show-soon 'company-echo-format))
1377 ('hide (company-echo-hide))))
1378
1379 (defun company-echo-strip-common-frontend (command)
1380 "A `company-mode' front-end showing the candidates in the echo area."
1381 (case command
1382 ('pre-command (company-echo-show-soon))
1383 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1384 ('hide (company-echo-hide))))
1385
1386 (defun company-echo-metadata-frontend (command)
1387 "A `company-mode' front-end showing the documentation in the echo area."
1388 (case command
1389 ('pre-command (company-echo-show-soon))
1390 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1391 ('hide (company-echo-hide))))
1392
1393 (provide 'company)
1394 ;;; company.el ends here