]> code.delx.au - gnu-emacs-elpa/blob - company.el
Added note that commands will start completion manually.
[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'. If these commands are
280 called before `company-idle-delay', completion will also start.
281
282 Completions can be searched with `company-search-candidates'.
283
284 The completion data is retrieved using `company-backends' and displayed using
285 `company-frontends'.
286
287 regular keymap:
288
289 \\{company-mode-map}
290 keymap during active completions:
291
292 \\{company-active-map}"
293 nil " comp" company-mode-map
294 (if company-mode
295 (progn
296 (add-hook 'pre-command-hook 'company-pre-command nil t)
297 (add-hook 'post-command-hook 'company-post-command nil t)
298 (dolist (backend company-backends)
299 (unless (fboundp backend)
300 (ignore-errors (require backend nil t)))
301 (unless (fboundp backend)
302 (message "Company back-end '%s' could not be initialized"
303 backend))))
304 (remove-hook 'pre-command-hook 'company-pre-command t)
305 (remove-hook 'post-command-hook 'company-post-command t)
306 (company-cancel)
307 (kill-local-variable 'company-point)))
308
309 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
310
311 (defvar company-overriding-keymap-bound nil)
312 (make-variable-buffer-local 'company-overriding-keymap-bound)
313
314 (defvar company-old-keymap nil)
315 (make-variable-buffer-local 'company-old-keymap)
316
317 (defvar company-my-keymap nil)
318 (make-variable-buffer-local 'company-my-keymap)
319
320 (defsubst company-enable-overriding-keymap (keymap)
321 (setq company-my-keymap keymap)
322 (when company-overriding-keymap-bound
323 (company-uninstall-map)))
324
325 (defun company-install-map ()
326 (unless (or company-overriding-keymap-bound
327 (null company-my-keymap))
328 (setq company-old-keymap overriding-terminal-local-map
329 overriding-terminal-local-map company-my-keymap
330 company-overriding-keymap-bound t)))
331
332 (defun company-uninstall-map ()
333 (when (and company-overriding-keymap-bound
334 (eq overriding-terminal-local-map company-my-keymap))
335 (setq overriding-terminal-local-map company-old-keymap
336 company-overriding-keymap-bound nil)))
337
338 ;; Hack:
339 ;; Emacs calculates the active keymaps before reading the event. That means we
340 ;; cannot change the keymap from a timer. So we send a bogus command.
341 (defun company-ignore ()
342 (interactive))
343
344 (global-set-key '[31415926] 'company-ignore)
345
346 (defun company-input-noop ()
347 (push 31415926 unread-command-events))
348
349 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
350
351 (defun company-grab (regexp &optional expression)
352 (when (looking-back regexp)
353 (or (match-string-no-properties (or expression 0)) "")))
354
355 (defun company-in-string-or-comment (&optional point)
356 (let ((pos (syntax-ppss)))
357 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
358
359 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
360
361 (defvar company-backend nil)
362 (make-variable-buffer-local 'company-backend)
363
364 (defvar company-prefix nil)
365 (make-variable-buffer-local 'company-prefix)
366
367 (defvar company-candidates nil)
368 (make-variable-buffer-local 'company-candidates)
369
370 (defvar company-candidates-length nil)
371 (make-variable-buffer-local 'company-candidates-length)
372
373 (defvar company-candidates-cache nil)
374 (make-variable-buffer-local 'company-candidates-cache)
375
376 (defvar company-candidates-predicate nil)
377 (make-variable-buffer-local 'company-candidates-predicate)
378
379 (defvar company-common nil)
380 (make-variable-buffer-local 'company-common)
381
382 (defvar company-selection 0)
383 (make-variable-buffer-local 'company-selection)
384
385 (defvar company-selection-changed nil)
386 (make-variable-buffer-local 'company-selection-changed)
387
388 (defvar company-point nil)
389 (make-variable-buffer-local 'company-point)
390
391 (defvar company-timer nil)
392
393 (defsubst company-strip-prefix (str)
394 (substring str (length company-prefix)))
395
396 (defsubst company-reformat (candidate)
397 ;; company-ispell needs this, because the results are always lower-case
398 ;; It's mory efficient to fix it only when they are displayed.
399 (concat company-prefix (substring candidate (length company-prefix))))
400
401 (defsubst company-should-complete (prefix)
402 (and (eq company-idle-delay t)
403 (>= (length prefix) company-minimum-prefix-length)))
404
405 (defsubst company-call-frontends (command)
406 (dolist (frontend company-frontends)
407 (condition-case err
408 (funcall frontend command)
409 (error (error "Company: Front-end %s error \"%s\" on command %s"
410 frontend (error-message-string err) command)))))
411
412 (defsubst company-set-selection (selection &optional force-update)
413 (setq selection (max 0 (min (1- company-candidates-length) selection)))
414 (when (or force-update (not (equal selection company-selection)))
415 (setq company-selection selection
416 company-selection-changed t)
417 (company-call-frontends 'update)))
418
419 (defun company-apply-predicate (candidates predicate)
420 (let (new)
421 (dolist (c candidates)
422 (when (funcall predicate c)
423 (push c new)))
424 (nreverse new)))
425
426 (defun company-update-candidates (candidates)
427 (setq company-candidates-length (length candidates))
428 (if (> company-selection 0)
429 ;; Try to restore the selection
430 (let ((selected (nth company-selection company-candidates)))
431 (setq company-selection 0
432 company-candidates candidates)
433 (when selected
434 (while (and candidates (string< (pop candidates) selected))
435 (incf company-selection))
436 (unless candidates
437 ;; Make sure selection isn't out of bounds.
438 (setq company-selection (min (1- company-candidates-length)
439 company-selection)))))
440 (setq company-selection 0
441 company-candidates candidates))
442 ;; Calculate common.
443 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
444 (setq company-common (try-completion company-prefix company-candidates)))
445 (when (eq company-common t)
446 (setq company-candidates nil)))
447
448 (defsubst company-calculate-candidates (prefix)
449 (setq company-prefix prefix)
450 (company-update-candidates
451 (or (cdr (assoc prefix company-candidates-cache))
452 (when company-candidates-cache
453 (let ((len (length prefix))
454 (completion-ignore-case (funcall company-backend 'ignore-case))
455 prev)
456 (dotimes (i len)
457 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
458 company-candidates-cache)))
459 (return (all-completions prefix prev))))))
460 (let ((candidates (funcall company-backend 'candidates prefix)))
461 (when company-candidates-predicate
462 (setq candidates
463 (company-apply-predicate candidates
464 company-candidates-predicate)))
465 (unless (funcall company-backend 'sorted)
466 (setq candidates (sort candidates 'string<)))
467 candidates)))
468 (unless (assoc prefix company-candidates-cache)
469 (push (cons prefix company-candidates) company-candidates-cache))
470 company-candidates)
471
472 (defun company-idle-begin (buf win tick pos)
473 (and company-mode
474 (eq buf (current-buffer))
475 (eq win (selected-window))
476 (eq tick (buffer-chars-modified-tick))
477 (eq pos (point))
478 (not company-candidates)
479 (not (equal (point) company-point))
480 (let ((company-idle-delay t))
481 (company-begin)
482 (when company-candidates
483 (company-input-noop)
484 (company-post-command)))))
485
486 (defun company-manual-begin ()
487 (interactive)
488 (unless company-mode (error "Company not enabled"))
489 (and company-mode
490 (not company-candidates)
491 (let ((company-idle-delay t)
492 (company-minimum-prefix-length 0))
493 (company-begin)))
494 ;; Return non-nil if active.
495 company-candidates)
496
497 (defun company-continue ()
498 (when company-candidates
499 (when (funcall company-backend 'no-cache company-prefix)
500 ;; Don't complete existing candidates, fetch new ones.
501 (setq company-candidates-cache nil))
502 (let ((new-prefix (funcall company-backend 'prefix)))
503 (unless (and (= (- (point) (length new-prefix))
504 (- company-point (length company-prefix)))
505 (or (equal company-prefix new-prefix)
506 (company-calculate-candidates new-prefix)))
507 (setq company-candidates nil)))))
508
509 (defun company-begin ()
510 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
511 ;; Don't complete in these cases.
512 (setq company-candidates nil)
513 (company-continue)
514 (unless company-candidates
515 (let (prefix)
516 (dolist (backend company-backends)
517 (and (fboundp backend)
518 (setq prefix (funcall backend 'prefix))
519 (company-should-complete prefix)
520 (setq company-backend backend)
521 (company-calculate-candidates prefix))
522 (return prefix)))))
523 (if company-candidates
524 (progn
525 (setq company-point (point))
526 (company-enable-overriding-keymap company-active-map)
527 (company-call-frontends 'update))
528 (company-cancel)))
529
530 (defun company-cancel ()
531 (setq company-backend nil
532 company-prefix nil
533 company-candidates nil
534 company-candidates-length nil
535 company-candidates-cache nil
536 company-candidates-predicate nil
537 company-common nil
538 company-selection 0
539 company-selection-changed nil
540 company-point nil)
541 (when company-timer
542 (cancel-timer company-timer))
543 (company-search-mode 0)
544 (company-call-frontends 'hide)
545 (company-enable-overriding-keymap nil))
546
547 (defun company-abort ()
548 (company-cancel)
549 ;; Don't start again, unless started manually.
550 (setq company-point (point)))
551
552 (defun company-pre-command ()
553 (unless (eq this-command 'company-show-doc-buffer)
554 (condition-case err
555 (when company-candidates
556 (company-call-frontends 'pre-command))
557 (error (message "Company: An error occurred in pre-command")
558 (message "%s" (error-message-string err))
559 (company-cancel))))
560 (when company-timer
561 (cancel-timer company-timer))
562 (company-uninstall-map))
563
564 (defun company-post-command ()
565 (unless (eq this-command 'company-show-doc-buffer)
566 (condition-case err
567 (progn
568 (unless (equal (point) company-point)
569 (company-begin))
570 (when company-candidates
571 (company-call-frontends 'post-command))
572 (when (numberp company-idle-delay)
573 (setq company-timer
574 (run-with-timer company-idle-delay nil 'company-idle-begin
575 (current-buffer) (selected-window)
576 (buffer-chars-modified-tick) (point)))))
577 (error (message "Company: An error occurred in post-command")
578 (message "%s" (error-message-string err))
579 (company-cancel))))
580 (company-install-map))
581
582 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
583
584 (defvar company-search-string nil)
585 (make-variable-buffer-local 'company-search-string)
586
587 (defvar company-search-lighter " Search: \"\"")
588 (make-variable-buffer-local 'company-search-lighter)
589
590 (defvar company-search-old-map nil)
591 (make-variable-buffer-local 'company-search-old-map)
592
593 (defvar company-search-old-selection 0)
594 (make-variable-buffer-local 'company-search-old-selection)
595
596 (defun company-search (text lines)
597 (let ((quoted (regexp-quote text))
598 (i 0))
599 (dolist (line lines)
600 (when (string-match quoted line (length company-prefix))
601 (return i))
602 (incf i))))
603
604 (defun company-search-printing-char ()
605 (interactive)
606 (unless company-mode (error "Company not enabled"))
607 (unless company-search-mode (error "Company not in search mode"))
608 (setq company-search-string
609 (concat (or company-search-string "") (string last-command-event))
610 company-search-lighter (concat " Search: \"" company-search-string
611 "\""))
612 (let ((pos (company-search company-search-string
613 (nthcdr company-selection company-candidates))))
614 (if (null pos)
615 (ding)
616 (company-set-selection (+ company-selection pos) t))))
617
618 (defun company-search-repeat-forward ()
619 "Repeat the incremental search in completion candidates forward."
620 (interactive)
621 (unless company-mode (error "Company not enabled"))
622 (unless company-search-mode (error "Company not in search mode"))
623 (let ((pos (company-search company-search-string
624 (cdr (nthcdr company-selection
625 company-candidates)))))
626 (if (null pos)
627 (ding)
628 (company-set-selection (+ company-selection pos 1) t))))
629
630 (defun company-search-repeat-backward ()
631 "Repeat the incremental search in completion candidates backwards."
632 (interactive)
633 (unless company-mode (error "Company not enabled"))
634 (unless company-search-mode (error "Company not in search mode"))
635 (let ((pos (company-search company-search-string
636 (nthcdr (- company-candidates-length
637 company-selection)
638 (reverse company-candidates)))))
639 (if (null pos)
640 (ding)
641 (company-set-selection (- company-selection pos 1) t))))
642
643 (defsubst company-create-match-predicate (search-string)
644 `(lambda (candidate)
645 ,(if company-candidates-predicate
646 `(and (string-match ,search-string candidate)
647 (funcall ,company-candidates-predicate candidate))
648 `(string-match ,company-search-string candidate))))
649
650 (defun company-search-kill-others ()
651 "Limit the completion candidates to the ones matching the search string."
652 (interactive)
653 (unless company-mode (error "Company not enabled"))
654 (unless company-search-mode (error "Company not in search mode"))
655 (let ((predicate (company-create-match-predicate company-search-string)))
656 (setq company-candidates-predicate predicate)
657 (company-update-candidates (company-apply-predicate company-candidates
658 predicate))
659 (company-search-mode 0)
660 (company-call-frontends 'update)))
661
662 (defun company-search-abort ()
663 "Abort searching the completion candidates."
664 (interactive)
665 (unless company-mode (error "Company not enabled"))
666 (unless company-search-mode (error "Company not in search mode"))
667 (company-set-selection company-search-old-selection t)
668 (company-search-mode 0))
669
670 (defun company-search-other-char ()
671 (interactive)
672 (unless company-mode (error "Company not enabled"))
673 (unless company-search-mode (error "Company not in search mode"))
674 (company-search-mode 0)
675 (when last-input-event
676 (clear-this-command-keys t)
677 (setq unread-command-events (list last-input-event))))
678
679 (defvar company-search-map
680 (let ((i 0)
681 (keymap (make-keymap)))
682 (if (fboundp 'max-char)
683 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
684 'company-search-printing-char)
685 (with-no-warnings
686 ;; obselete in Emacs 23
687 (let ((l (generic-character-list))
688 (table (nth 1 keymap)))
689 (while l
690 (set-char-table-default table (car l) 'isearch-printing-char)
691 (setq l (cdr l))))))
692 (define-key keymap [t] 'company-search-other-char)
693 (while (< i ?\s)
694 (define-key keymap (make-string 1 i) 'company-search-other-char)
695 (incf i))
696 (while (< i 256)
697 (define-key keymap (vector i) 'company-search-printing-char)
698 (incf i))
699 (let ((meta-map (make-sparse-keymap)))
700 (define-key keymap (char-to-string meta-prefix-char) meta-map)
701 (define-key keymap [escape] meta-map))
702 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
703 (define-key keymap "\e\e\e" 'company-search-other-char)
704 (define-key keymap [escape escape escape] 'company-search-other-char)
705
706 (define-key keymap "\C-g" 'company-search-abort)
707 (define-key keymap "\C-s" 'company-search-repeat-forward)
708 (define-key keymap "\C-r" 'company-search-repeat-backward)
709 (define-key keymap "\C-o" 'company-search-kill-others)
710 keymap)
711 "Keymap used for incrementally searching the completion candidates.")
712
713 (define-minor-mode company-search-mode
714 "Start searching the completion candidates incrementally.
715
716 \\<company-search-map>Search can be controlled with the commands:
717 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
718 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
719 - `company-search-abort' (\\[company-search-abort])
720
721 Regular characters are appended to the search string.
722
723 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
724 the search string to limit the completion candidates."
725 nil company-search-lighter nil
726 (if company-search-mode
727 (if (company-manual-begin)
728 (progn
729 (setq company-search-old-selection company-selection)
730 (company-enable-overriding-keymap company-search-map)
731 (company-call-frontends 'update))
732 (setq company-search-mode nil))
733 (kill-local-variable 'company-search-string)
734 (kill-local-variable 'company-search-lighter)
735 (kill-local-variable 'company-search-old-selection)
736 (company-enable-overriding-keymap company-active-map)))
737
738 (defun company-search-candidates ()
739 "Start searching the completion candidates incrementally.
740
741 \\<company-search-map>Search can be controlled with the commands:
742 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
743 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
744 - `company-search-abort' (\\[company-search-abort])
745
746 Regular characters are appended to the search string.
747
748 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
749 the search string to limit the completion candidates."
750 (interactive)
751 (company-search-mode 1))
752
753 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
754
755 (defun company-select-next ()
756 "Select the next candidate in the list."
757 (interactive)
758 (when (company-manual-begin)
759 (company-set-selection (1+ company-selection))))
760
761 (defun company-select-previous ()
762 "Select the previous candidate in the list."
763 (interactive)
764 (when (company-manual-begin)
765 (company-set-selection (1- company-selection))))
766
767 (defun company-complete-selection ()
768 "Complete the selected candidate."
769 (interactive)
770 (when (company-manual-begin)
771 (insert (company-strip-prefix (nth company-selection company-candidates)))
772 (company-abort)))
773
774 (defun company-complete-common ()
775 "Complete the common part of all candidates."
776 (interactive)
777 (when (company-manual-begin)
778 (insert (company-strip-prefix company-common))))
779
780 (defun company-complete ()
781 "Complete the common part of all candidates or the current selection.
782 The first time this is called, the common part is completed, the second time, or
783 when the selection has been changed, the selected candidate is completed."
784 (interactive)
785 (when (company-manual-begin)
786 (if (or company-selection-changed
787 (eq last-command 'company-complete-common))
788 (call-interactively 'company-complete-selection)
789 (call-interactively 'company-complete-common)
790 (setq this-command 'company-complete-common))))
791
792 (defun company-complete-number (n)
793 "Complete the Nth candidate."
794 (when (company-manual-begin)
795 (and (< n 1) (> n company-candidates-length)
796 (error "No candidate number %d" n))
797 (decf n)
798 (insert (company-strip-prefix (nth n company-candidates)))
799 (company-abort)))
800
801 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
802
803 (defconst company-space-strings-limit 100)
804
805 (defconst company-space-strings
806 (let (lst)
807 (dotimes (i company-space-strings-limit)
808 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
809 (apply 'vector lst)))
810
811 (defsubst company-space-string (len)
812 (if (< len company-space-strings-limit)
813 (aref company-space-strings len)
814 (make-string len ?\ )))
815
816 (defsubst company-safe-substring (str from &optional to)
817 (let ((len (length str)))
818 (if (> from len)
819 ""
820 (if (and to (> to len))
821 (concat (substring str from)
822 (company-space-string (- to len)))
823 (substring str from to)))))
824
825 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
826
827 (defvar company-last-metadata nil)
828 (make-variable-buffer-local 'company-last-metadata)
829
830 (defun company-fetch-metadata ()
831 (let ((selected (nth company-selection company-candidates)))
832 (unless (equal selected (car company-last-metadata))
833 (setq company-last-metadata
834 (cons selected (funcall company-backend 'meta selected))))
835 (cdr company-last-metadata)))
836
837 (defun company-doc-buffer (&optional string)
838 (with-current-buffer (get-buffer-create "*Company meta-data*")
839 (erase-buffer)
840 (current-buffer)))
841
842 (defun company-show-doc-buffer ()
843 "Temporarily show a buffer with the complete documentation for the selection."
844 (interactive)
845 (unless company-mode (error "Company not enabled"))
846 (when (company-manual-begin)
847 (save-window-excursion
848 (let* ((height (window-height))
849 (row (cdr (posn-col-row (posn-at-point))))
850 (selected (nth company-selection company-candidates))
851 (buffer (funcall company-backend 'doc-buffer selected)))
852 (if (not buffer)
853 (error "No documentation available.")
854 (display-buffer buffer)
855 (and (< (window-height) height)
856 (< (- (window-height) row 2) company-tooltip-limit)
857 (recenter (- (window-height) row 2)))
858 (while (eq 'scroll-other-window
859 (key-binding (vector (list (read-event)))))
860 (scroll-other-window))
861 (when last-input-event
862 (clear-this-command-keys t)
863 (setq unread-command-events (list last-input-event))))))))
864
865 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
866
867 (defvar company-pseudo-tooltip-overlay nil)
868 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
869
870 (defvar company-tooltip-offset 0)
871 (make-variable-buffer-local 'company-tooltip-offset)
872
873 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
874
875 (decf limit 2)
876 (setq company-tooltip-offset
877 (max (min selection company-tooltip-offset)
878 (- selection -1 limit)))
879
880 (when (<= company-tooltip-offset 1)
881 (incf limit)
882 (setq company-tooltip-offset 0))
883
884 (when (>= company-tooltip-offset (- num-lines limit 1))
885 (incf limit)
886 (when (= selection (1- num-lines))
887 (decf company-tooltip-offset)
888 (when (<= company-tooltip-offset 1)
889 (setq company-tooltip-offset 0)
890 (incf limit))))
891
892 limit)
893
894 ;;; propertize
895
896 (defsubst company-round-tab (arg)
897 (* (/ (+ arg tab-width) tab-width) tab-width))
898
899 (defun company-untabify (str)
900 (let* ((pieces (split-string str "\t"))
901 (copy pieces))
902 (while (cdr copy)
903 (setcar copy (company-safe-substring
904 (car copy) 0 (company-round-tab (string-width (car copy)))))
905 (pop copy))
906 (apply 'concat pieces)))
907
908 (defun company-fill-propertize (line width selected)
909 (setq line (company-safe-substring line 0 width))
910 (add-text-properties 0 width (list 'face 'company-tooltip) line)
911 (add-text-properties 0 (length company-common)
912 (list 'face 'company-tooltip-common) line)
913 (when selected
914 (if (and company-search-string
915 (string-match (regexp-quote company-search-string) line
916 (length company-prefix)))
917 (progn
918 (add-text-properties (match-beginning 0) (match-end 0)
919 '(face company-tooltip-selection) line)
920 (when (< (match-beginning 0) (length company-common))
921 (add-text-properties (match-beginning 0) (length company-common)
922 '(face company-tooltip-common-selection)
923 line)))
924 (add-text-properties 0 width '(face company-tooltip-selection) line)
925 (add-text-properties 0 (length company-common)
926 (list 'face 'company-tooltip-common-selection)
927 line)))
928 line)
929
930 ;;; replace
931
932 (defun company-buffer-lines (beg end)
933 (goto-char beg)
934 (let ((row (cdr (posn-col-row (posn-at-point))))
935 lines)
936 (while (and (equal (move-to-window-line (incf row)) row)
937 (<= (point) end))
938 (push (buffer-substring beg (min end (1- (point)))) lines)
939 (setq beg (point)))
940 (unless (eq beg end)
941 (push (buffer-substring beg end) lines))
942 (nreverse lines)))
943
944 (defsubst company-modify-line (old new offset)
945 (concat (company-safe-substring old 0 offset)
946 new
947 (company-safe-substring old (+ offset (length new)))))
948
949 (defun company-replacement-string (old lines column nl)
950 (let (new)
951 ;; Inject into old lines.
952 (while old
953 (push (company-modify-line (pop old) (pop lines) column) new))
954 ;; Append whole new lines.
955 (while lines
956 (push (concat (company-space-string column) (pop lines)) new))
957 (concat (when nl "\n")
958 (mapconcat 'identity (nreverse new) "\n")
959 "\n")))
960
961 (defun company-create-lines (column selection limit)
962
963 (let ((len company-candidates-length)
964 (numbered 99999)
965 lines
966 width
967 lines-copy
968 previous
969 remainder
970 new)
971
972 ;; Scroll to offset.
973 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
974
975 (when (> company-tooltip-offset 0)
976 (setq previous (format "...(%d)" company-tooltip-offset)))
977
978 (setq remainder (- len limit company-tooltip-offset)
979 remainder (when (> remainder 0)
980 (setq remainder (format "...(%d)" remainder))))
981
982 (decf selection company-tooltip-offset)
983 (setq width (min (length previous) (length remainder))
984 lines (nthcdr company-tooltip-offset company-candidates)
985 len (min limit len)
986 lines-copy lines)
987
988 (dotimes (i len)
989 (setq width (max (length (pop lines-copy)) width)))
990 (setq width (min width (- (window-width) column)))
991
992 (setq lines-copy lines)
993
994 ;; number can make tooltip too long
995 (and company-show-numbers
996 (< (setq numbered company-tooltip-offset) 10)
997 (incf width 2))
998
999 (when previous
1000 (push (propertize (company-safe-substring previous 0 width)
1001 'face 'company-tooltip)
1002 new))
1003
1004 (dotimes (i len)
1005 (push (company-fill-propertize
1006 (if (>= numbered 10)
1007 (company-reformat (pop lines))
1008 (incf numbered)
1009 (format "%s %d"
1010 (company-safe-substring (company-reformat (pop lines))
1011 0 (- width 2))
1012 (mod numbered 10)))
1013 width (equal i selection))
1014 new))
1015
1016 (when remainder
1017 (push (propertize (company-safe-substring remainder 0 width)
1018 'face 'company-tooltip)
1019 new))
1020
1021 (setq lines (nreverse new))))
1022
1023 ;; show
1024
1025 (defsubst company-pseudo-tooltip-height ()
1026 "Calculate the appropriate tooltip height."
1027 (max 3 (min company-tooltip-limit
1028 (- (window-height) 2
1029 (count-lines (window-start) (point-at-bol))))))
1030
1031 (defun company-pseudo-tooltip-show (row column selection)
1032 (company-pseudo-tooltip-hide)
1033 (save-excursion
1034
1035 (move-to-column 0)
1036
1037 (let* ((height (company-pseudo-tooltip-height))
1038 (lines (company-create-lines column selection height))
1039 (nl (< (move-to-window-line row) row))
1040 (beg (point))
1041 (end (save-excursion
1042 (move-to-window-line (+ row height))
1043 (point)))
1044 (old-string
1045 (mapcar 'company-untabify (company-buffer-lines beg end)))
1046 str)
1047
1048 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
1049
1050 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
1051 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
1052 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
1053 (overlay-put company-pseudo-tooltip-overlay 'company-before
1054 (company-replacement-string old-string lines column nl))
1055 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
1056
1057 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
1058
1059 (defun company-pseudo-tooltip-show-at-point (pos)
1060 (let ((col-row (posn-col-row (posn-at-point pos))))
1061 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
1062
1063 (defun company-pseudo-tooltip-edit (lines selection)
1064 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
1065 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1066 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
1067 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
1068 (lines (company-create-lines column selection height)))
1069 (overlay-put company-pseudo-tooltip-overlay 'company-before
1070 (company-replacement-string old-string lines column nl))))
1071
1072 (defun company-pseudo-tooltip-hide ()
1073 (when company-pseudo-tooltip-overlay
1074 (delete-overlay company-pseudo-tooltip-overlay)
1075 (setq company-pseudo-tooltip-overlay nil)))
1076
1077 (defun company-pseudo-tooltip-hide-temporarily ()
1078 (when (overlayp company-pseudo-tooltip-overlay)
1079 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1080 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1081
1082 (defun company-pseudo-tooltip-unhide ()
1083 (when company-pseudo-tooltip-overlay
1084 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1085 (overlay-put company-pseudo-tooltip-overlay 'before-string
1086 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1087 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1088
1089 (defun company-pseudo-tooltip-frontend (command)
1090 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1091 (case command
1092 ('pre-command (company-pseudo-tooltip-hide-temporarily))
1093 ('post-command
1094 (unless (and (overlayp company-pseudo-tooltip-overlay)
1095 (equal (overlay-get company-pseudo-tooltip-overlay
1096 'company-height)
1097 (company-pseudo-tooltip-height)))
1098 ;; Redraw needed.
1099 (company-pseudo-tooltip-show-at-point (- (point)
1100 (length company-prefix))))
1101 (company-pseudo-tooltip-unhide))
1102 ('hide (company-pseudo-tooltip-hide)
1103 (setq company-tooltip-offset 0))
1104 ('update (when (overlayp company-pseudo-tooltip-overlay)
1105 (company-pseudo-tooltip-edit company-candidates
1106 company-selection)))))
1107
1108 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1109 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1110 (unless (and (eq command 'post-command)
1111 (not (cdr company-candidates)))
1112 (company-pseudo-tooltip-frontend command)))
1113
1114 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1115
1116 (defvar company-preview-overlay nil)
1117 (make-variable-buffer-local 'company-preview-overlay)
1118
1119 (defun company-preview-show-at-point (pos)
1120 (company-preview-hide)
1121
1122 (setq company-preview-overlay (make-overlay pos pos))
1123
1124 (let ((completion (company-strip-prefix (nth company-selection
1125 company-candidates))))
1126 (and (equal pos (point))
1127 (not (equal completion ""))
1128 (add-text-properties 0 1 '(cursor t) completion))
1129
1130 (setq completion (propertize completion 'face 'company-preview))
1131 (add-text-properties 0 (- (length company-common) (length company-prefix))
1132 '(face company-preview-common) completion)
1133
1134 (overlay-put company-preview-overlay 'after-string completion)
1135 (overlay-put company-preview-overlay 'window (selected-window))))
1136
1137 (defun company-preview-hide ()
1138 (when company-preview-overlay
1139 (delete-overlay company-preview-overlay)
1140 (setq company-preview-overlay nil)))
1141
1142 (defun company-preview-frontend (command)
1143 "A `company-mode' front-end showing the selection as if it had been inserted."
1144 (case command
1145 ('pre-command (company-preview-hide))
1146 ('post-command (company-preview-show-at-point (point)))
1147 ('hide (company-preview-hide))))
1148
1149 (defun company-preview-if-just-one-frontend (command)
1150 "`company-preview-frontend', but only shown for single candidates."
1151 (unless (and (eq command 'post-command)
1152 (cdr company-candidates))
1153 (company-preview-frontend command)))
1154
1155 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1156
1157 (defvar company-echo-last-msg nil)
1158 (make-variable-buffer-local 'company-echo-last-msg)
1159
1160 (defvar company-echo-timer nil)
1161
1162 (defvar company-echo-delay .1)
1163
1164 (defun company-echo-show (&optional getter)
1165 (when getter
1166 (setq company-echo-last-msg (funcall getter)))
1167 (let ((message-log-max nil))
1168 (if company-echo-last-msg
1169 (message "%s" company-echo-last-msg)
1170 (message ""))))
1171
1172 (defsubst company-echo-show-soon (&optional getter)
1173 (when company-echo-timer
1174 (cancel-timer company-echo-timer))
1175 (setq company-echo-timer (run-with-timer company-echo-delay nil
1176 'company-echo-show getter)))
1177
1178 (defun company-echo-format ()
1179
1180 (let ((limit (window-width (minibuffer-window)))
1181 (len -1)
1182 ;; Roll to selection.
1183 (candidates (nthcdr company-selection company-candidates))
1184 (i (if company-show-numbers company-selection 99999))
1185 comp msg)
1186
1187 (while candidates
1188 (setq comp (company-reformat (pop candidates))
1189 len (+ len 1 (length comp)))
1190 (if (< i 10)
1191 ;; Add number.
1192 (progn
1193 (setq comp (propertize (format "%d: %s" i comp)
1194 'face 'company-echo))
1195 (incf len 3)
1196 (incf i)
1197 (add-text-properties 3 (+ 3 (length company-common))
1198 '(face company-echo-common) comp))
1199 (setq comp (propertize comp 'face 'company-echo))
1200 (add-text-properties 0 (length company-common)
1201 '(face company-echo-common) comp))
1202 (if (>= len limit)
1203 (setq candidates nil)
1204 (push comp msg)))
1205
1206 (mapconcat 'identity (nreverse msg) " ")))
1207
1208 (defun company-echo-strip-common-format ()
1209
1210 (let ((limit (window-width (minibuffer-window)))
1211 (len (+ (length company-prefix) 2))
1212 ;; Roll to selection.
1213 (candidates (nthcdr company-selection company-candidates))
1214 (i (if company-show-numbers company-selection 99999))
1215 msg comp)
1216
1217 (while candidates
1218 (setq comp (company-strip-prefix (pop candidates))
1219 len (+ len 2 (length comp)))
1220 (when (< i 10)
1221 ;; Add number.
1222 (setq comp (format "%s (%d)" comp i))
1223 (incf len 4)
1224 (incf i))
1225 (if (>= len limit)
1226 (setq candidates nil)
1227 (push (propertize comp 'face 'company-echo) msg)))
1228
1229 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1230 (mapconcat 'identity (nreverse msg) ", ")
1231 "}")))
1232
1233 (defun company-echo-hide ()
1234 (when company-echo-timer
1235 (cancel-timer company-echo-timer))
1236 (unless (equal company-echo-last-msg "")
1237 (setq company-echo-last-msg "")
1238 (company-echo-show)))
1239
1240 (defun company-echo-frontend (command)
1241 "A `company-mode' front-end showing the candidates in the echo area."
1242 (case command
1243 ('pre-command (company-echo-show-soon))
1244 ('post-command (company-echo-show-soon 'company-echo-format))
1245 ('hide (company-echo-hide))))
1246
1247 (defun company-echo-strip-common-frontend (command)
1248 "A `company-mode' front-end showing the candidates in the echo area."
1249 (case command
1250 ('pre-command (company-echo-show-soon))
1251 ('post-command (company-echo-show-soon 'company-echo-strip-common-format))
1252 ('hide (company-echo-hide))))
1253
1254 (defun company-echo-metadata-frontend (command)
1255 "A `company-mode' front-end showing the documentation in the echo area."
1256 (case command
1257 ('pre-command (company-echo-show-soon))
1258 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1259 ('hide (company-echo-hide))))
1260
1261 (provide 'company)
1262 ;;; company.el ends here