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