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