]> code.delx.au - gnu-emacs-elpa/blob - company.el
Speed up echo by not showing it immediately.
[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:
7 ;; Keywords: abbrev, convenience, matchis
8 ;; URL: http://nschum.de/src/emacs/company/
9 ;; Compatibility: 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 ;;; Change Log:
29 ;;
30 ;; Initial release.
31 ;;
32 ;;; Code:
33
34 (eval-when-compile (require 'cl))
35
36 (add-to-list 'debug-ignored-errors
37 "^Pseudo tooltip frontend cannot be used twice$")
38 (add-to-list 'debug-ignored-errors "^Preview frontend cannot be used twice$")
39 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
40 (add-to-list 'debug-ignored-errors "^No documentation available$")
41
42 (defgroup company nil
43 ""
44 :group 'abbrev
45 :group 'convenience
46 :group 'maching)
47
48 (defface company-tooltip
49 '((t :background "yellow"
50 :foreground "black"))
51 "*"
52 :group 'company)
53
54 (defface company-tooltip-selection
55 '((t :background "orange1"
56 :foreground "black"))
57 "*"
58 :group 'company)
59
60 (defface company-tooltip-common
61 '((t :inherit company-tooltip
62 :foreground "red"))
63 "*"
64 :group 'company)
65
66 (defface company-tooltip-common-selection
67 '((t :inherit company-tooltip-selection
68 :foreground "red"))
69 "*"
70 :group 'company)
71
72 (defcustom company-tooltip-limit 10
73 "*"
74 :group 'company
75 :type 'integer)
76
77 (defface company-preview
78 '((t :background "blue4"
79 :foreground "wheat"))
80 "*"
81 :group 'company)
82
83 (defface company-preview-common
84 '((t :inherit company-preview
85 :foreground "red"))
86 "*"
87 :group 'company)
88
89 (defface company-echo nil
90 "*"
91 :group 'company)
92
93 (defface company-echo-common
94 '((((background dark)) (:foreground "firebrick1"))
95 (((background light)) (:background "firebrick4")))
96 "*"
97 :group 'company)
98
99 (defun company-frontends-set (variable value)
100 ;; uniquify
101 (let ((remainder value))
102 (setcdr remainder (delq (car remainder) (cdr remainder))))
103 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
104 (memq 'company-pseudo-tooltip-frontend value)
105 (error "Pseudo tooltip frontend cannot be used twice"))
106 (and (memq 'company-preview-if-just-one-frontend value)
107 (memq 'company-preview-frontend value)
108 (error "Preview frontend cannot be used twice"))
109 (and (memq 'company-echo value)
110 (memq 'company-echo-metadata-frontend value)
111 (error "Echo area cannot be used twice"))
112 ;; preview must come last
113 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
114 (when (memq f value)
115 (setq value (append (delq f value) (list f)))))
116 (set variable value))
117
118 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
119 company-preview-if-just-one-frontend
120 company-echo-metadata-frontend)
121 "*"
122 :set 'company-frontends-set
123 :group 'company
124 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
125 (const :tag "pseudo tooltip"
126 company-pseudo-tooltip-frontend)
127 (const :tag "pseudo tooltip, multiple only"
128 company-pseudo-tooltip-unless-just-one-frontend)
129 (const :tag "preview" company-preview-frontend)
130 (const :tag "preview, unique only"
131 company-preview-if-just-one-frontend)
132 (function :tag "custom function" nil))))
133
134 (defcustom company-backends '(company-elisp company-nxml company-css
135 company-semantic company-oddmuse
136 company-files company-dabbrev)
137 "*"
138 :group 'company
139 :type '(repeat (function :tag "function" nil)))
140
141 (defcustom company-minimum-prefix-length 3
142 "*"
143 :group 'company
144 :type '(integer :tag "prefix length"))
145
146 (defcustom company-idle-delay .7
147 "*"
148 :group 'company
149 :type '(choice (const :tag "never (nil)" nil)
150 (const :tag "immediate (t)" t)
151 (number :tag "seconds")))
152
153 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
154
155 (defvar company-mode-map (make-sparse-keymap))
156
157 (defvar company-active-map
158 (let ((keymap (make-sparse-keymap)))
159 (define-key keymap (kbd "M-n") 'company-select-next)
160 (define-key keymap (kbd "M-p") 'company-select-previous)
161 (define-key keymap "\C-m" 'company-complete-selection)
162 (define-key keymap "\t" 'company-complete-common)
163 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
164 (define-key keymap "\C-s" 'company-search-candidates)
165 keymap))
166
167 ;;;###autoload
168 (define-minor-mode company-mode
169 ""
170 nil " comp" company-mode-map
171 (if company-mode
172 (progn
173 (add-hook 'pre-command-hook 'company-pre-command nil t)
174 (add-hook 'post-command-hook 'company-post-command nil t))
175 (remove-hook 'pre-command-hook 'company-pre-command t)
176 (remove-hook 'post-command-hook 'company-post-command t)
177 (company-cancel)
178 (kill-local-variable 'company-point)))
179
180 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
181
182 (defvar company-overriding-keymap-bound nil)
183 (make-variable-buffer-local 'company-overriding-keymap-bound)
184
185 (defvar company-old-keymap nil)
186 (make-variable-buffer-local 'company-old-keymap)
187
188 (defvar company-my-keymap nil)
189 (make-variable-buffer-local 'company-my-keymap)
190
191 (defsubst company-enable-overriding-keymap (keymap)
192 (setq company-my-keymap keymap)
193 (when company-overriding-keymap-bound
194 (company-uninstall-map)))
195
196 (defun company-install-map ()
197 (unless (or company-overriding-keymap-bound
198 (null company-my-keymap))
199 (setq company-old-keymap overriding-terminal-local-map
200 overriding-terminal-local-map company-my-keymap
201 company-overriding-keymap-bound t)))
202
203 (defun company-uninstall-map ()
204 (when (and company-overriding-keymap-bound
205 (eq overriding-terminal-local-map company-my-keymap))
206 (setq overriding-terminal-local-map company-old-keymap
207 company-overriding-keymap-bound nil)))
208
209 ;; Hack:
210 ;; Emacs calculates the active keymaps before reading the event. That means we
211 ;; cannot change the keymap from a timer. So we send a bogus command.
212 (defun company-ignore ()
213 (interactive))
214
215 (global-set-key '[31415926] 'company-ignore)
216
217 (defun company-input-noop ()
218 (push 31415926 unread-command-events))
219
220 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221
222 (defun company-grab (regexp &optional expression)
223 (when (looking-back regexp)
224 (or (match-string-no-properties (or expression 0)) "")))
225
226 (defun company-in-string-or-comment (&optional point)
227 (let ((pos (syntax-ppss)))
228 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
229
230 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231
232 (defvar company-backend nil)
233 (make-variable-buffer-local 'company-backend)
234
235 (defvar company-prefix nil)
236 (make-variable-buffer-local 'company-prefix)
237
238 (defvar company-candidates nil)
239 (make-variable-buffer-local 'company-candidates)
240
241 (defvar company-candidates-length nil)
242 (make-variable-buffer-local 'company-candidates-length)
243
244 (defvar company-candidates-cache nil)
245 (make-variable-buffer-local 'company-candidates-cache)
246
247 (defvar company-candidates-predicate nil)
248 (make-variable-buffer-local 'company-candidates-predicate)
249
250 (defvar company-common nil)
251 (make-variable-buffer-local 'company-common)
252
253 (defvar company-selection 0)
254 (make-variable-buffer-local 'company-selection)
255
256 (defvar company-selection-changed nil)
257 (make-variable-buffer-local 'company-selection-changed)
258
259 (defvar company-point nil)
260 (make-variable-buffer-local 'company-point)
261
262 (defvar company-timer nil)
263
264 (defvar company-disabled-backends nil)
265
266 (defsubst company-strip-prefix (str)
267 (substring str (length company-prefix)))
268
269 (defsubst company-reformat (candidate)
270 ;; company-ispell needs this, because the results are always lower-case
271 ;; It's mory efficient to fix it only when they are displayed.
272 (concat company-prefix (substring candidate (length company-prefix))))
273
274 (defsubst company-should-complete (prefix)
275 (and (eq company-idle-delay t)
276 (>= (length prefix) company-minimum-prefix-length)))
277
278 (defsubst company-call-frontends (command)
279 (dolist (frontend company-frontends)
280 (condition-case err
281 (funcall frontend command)
282 (error (error "Company: Front-end %s error \"%s\" on command %s"
283 frontend (error-message-string err) command)))))
284
285 (defsubst company-set-selection (selection &optional force-update)
286 (setq selection (max 0 (min (1- company-candidates-length) selection)))
287 (when (or force-update (not (equal selection company-selection)))
288 (setq company-selection selection
289 company-selection-changed t)
290 (company-call-frontends 'update)))
291
292 (defun company-apply-predicate (candidates predicate)
293 (let (new)
294 (dolist (c candidates)
295 (when (funcall predicate c)
296 (push c new)))
297 (nreverse new)))
298
299 (defun company-update-candidates (candidates)
300 (setq company-candidates-length (length candidates))
301 (if (> company-selection 0)
302 ;; Try to restore the selection
303 (let ((selected (nth company-selection company-candidates)))
304 (setq company-selection 0
305 company-candidates candidates)
306 (when selected
307 (while (and candidates (string< (pop candidates) selected))
308 (incf company-selection))
309 (unless candidates
310 ;; Make sure selection isn't out of bounds.
311 (setq company-selection (min (1- company-candidates-length)
312 company-selection)))))
313 (setq company-selection 0
314 company-candidates candidates))
315 ;; Calculate common.
316 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
317 (setq company-common (try-completion company-prefix company-candidates)))
318 (when (eq company-common t)
319 (setq company-candidates nil)))
320
321 (defsubst company-calculate-candidates (prefix)
322 (setq company-prefix prefix)
323 (company-update-candidates
324 (or (cdr (assoc prefix company-candidates-cache))
325 (let ((len (length prefix))
326 (completion-ignore-case (funcall company-backend 'ignore-case))
327 prev)
328 (dotimes (i len)
329 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
330 company-candidates-cache)))
331 (return (all-completions prefix prev)))))
332 (let ((candidates (funcall company-backend 'candidates prefix)))
333 (and company-candidates-predicate
334 (setq candidates
335 (company-apply-predicate candidates
336 company-candidates-predicate)))
337 (unless (funcall company-backend 'sorted)
338 (setq candidates (sort candidates 'string<)))
339 candidates)))
340 (unless (assoc prefix company-candidates-cache)
341 (push (cons prefix company-candidates) company-candidates-cache))
342 company-candidates)
343
344 (defun company-idle-begin (buf win tick pos)
345 (and company-mode
346 (eq buf (current-buffer))
347 (eq win (selected-window))
348 (eq tick (buffer-chars-modified-tick))
349 (eq pos (point))
350 (not company-candidates)
351 (not (equal (point) company-point))
352 (let ((company-idle-delay t))
353 (company-begin)
354 (when company-candidates
355 (company-input-noop)
356 (company-post-command)))))
357
358 (defun company-manual-begin ()
359 (and company-mode
360 (not company-candidates)
361 (let ((company-idle-delay t)
362 (company-minimum-prefix-length 0))
363 (company-begin)))
364 ;; Return non-nil if active.
365 company-candidates)
366
367 (defun company-continue ()
368 (when company-candidates
369 (when (funcall company-backend 'no-cache company-prefix)
370 ;; Don't complete existing candidates, fetch new ones.
371 (setq company-candidates-cache nil))
372 (let ((new-prefix (funcall company-backend 'prefix)))
373 (unless (and (= (- (point) (length new-prefix))
374 (- company-point (length company-prefix)))
375 (or (equal company-prefix new-prefix)
376 (company-calculate-candidates new-prefix)))
377 (setq company-candidates nil)))))
378
379 (defun company-begin ()
380 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
381 ;; Don't complete in these cases.
382 (setq company-candidates nil)
383 (company-continue)
384 (unless company-candidates
385 (let (prefix)
386 (dolist (backend company-backends)
387 (unless (fboundp backend)
388 (ignore-errors (require backend nil t)))
389 (if (fboundp backend)
390 (when (setq prefix (funcall backend 'prefix))
391 (when (company-should-complete prefix)
392 (setq company-backend backend)
393 (company-calculate-candidates prefix))
394 (return prefix))
395 (unless (memq backend company-disabled-backends)
396 (push backend company-disabled-backends)
397 (message "Company back-end '%s' could not be initialized"
398 backend)))))))
399 (if company-candidates
400 (progn
401 (setq company-point (point))
402 (company-enable-overriding-keymap company-active-map)
403 (company-call-frontends 'update))
404 (company-cancel)))
405
406 (defun company-cancel ()
407 (setq company-backend nil
408 company-prefix nil
409 company-candidates nil
410 company-candidates-length nil
411 company-candidates-cache nil
412 company-candidates-predicate nil
413 company-common nil
414 company-selection 0
415 company-selection-changed nil
416 company-point nil)
417 (when company-timer
418 (cancel-timer company-timer))
419 (company-search-mode 0)
420 (company-call-frontends 'hide)
421 (company-enable-overriding-keymap nil))
422
423 (defun company-abort ()
424 (company-cancel)
425 ;; Don't start again, unless started manually.
426 (setq company-point (point)))
427
428 (defun company-pre-command ()
429 (unless (eq this-command 'company-show-doc-buffer)
430 (condition-case err
431 (when company-candidates
432 (company-call-frontends 'pre-command))
433 (error (message "Company: An error occurred in pre-command")
434 (message "%s" (error-message-string err))
435 (company-cancel))))
436 (when company-timer
437 (cancel-timer company-timer))
438 (company-uninstall-map))
439
440 (defun company-post-command ()
441 (unless (eq this-command 'company-show-doc-buffer)
442 (condition-case err
443 (progn
444 (unless (equal (point) company-point)
445 (company-begin))
446 (when company-candidates
447 (company-call-frontends 'post-command))
448 (when (numberp company-idle-delay)
449 (setq company-timer
450 (run-with-timer company-idle-delay nil 'company-idle-begin
451 (current-buffer) (selected-window)
452 (buffer-chars-modified-tick) (point)))))
453 (error (message "Company: An error occurred in post-command")
454 (message "%s" (error-message-string err))
455 (company-cancel))))
456 (company-install-map))
457
458 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
459
460 (defvar company-search-string nil)
461 (make-variable-buffer-local 'company-search-string)
462
463 (defvar company-search-lighter " Search: \"\"")
464 (make-variable-buffer-local 'company-search-lighter)
465
466 (defvar company-search-old-map nil)
467 (make-variable-buffer-local 'company-search-old-map)
468
469 (defvar company-search-old-selection 0)
470 (make-variable-buffer-local 'company-search-old-selection)
471
472 (defun company-search (text lines)
473 (let ((quoted (regexp-quote text))
474 (i 0))
475 (dolist (line lines)
476 (when (string-match quoted line (length company-prefix))
477 (return i))
478 (incf i))))
479
480 (defun company-search-printing-char ()
481 (interactive)
482 (setq company-search-string
483 (concat (or company-search-string "") (string last-command-event))
484 company-search-lighter (concat " Search: \"" company-search-string
485 "\""))
486 (let ((pos (company-search company-search-string
487 (nthcdr company-selection company-candidates))))
488 (if (null pos)
489 (ding)
490 (company-set-selection (+ company-selection pos) t))))
491
492 (defun company-search-repeat-forward ()
493 (interactive)
494 (let ((pos (company-search company-search-string
495 (cdr (nthcdr company-selection
496 company-candidates)))))
497 (if (null pos)
498 (ding)
499 (company-set-selection (+ company-selection pos 1) t))))
500
501 (defun company-search-repeat-backward ()
502 (interactive)
503 (let ((pos (company-search company-search-string
504 (nthcdr (- company-candidates-length
505 company-selection)
506 (reverse company-candidates)))))
507 (if (null pos)
508 (ding)
509 (company-set-selection (- company-selection pos 1) t))))
510
511 (defun company-search-kill-others ()
512 (interactive)
513 (let ((predicate `(lambda (candidate)
514 (string-match ,company-search-string candidate))))
515 (setq company-candidates-predicate predicate)
516 (company-update-candidates (company-apply-predicate company-candidates
517 predicate))
518 (company-search-mode 0)
519 (company-call-frontends 'update)))
520
521 (defun company-search-abort ()
522 (interactive)
523 (company-set-selection company-search-old-selection t)
524 (company-search-mode 0))
525
526 (defun company-search-other-char ()
527 (interactive)
528 (company-search-mode 0)
529 (when last-input-event
530 (clear-this-command-keys t)
531 (setq unread-command-events (list last-input-event))))
532
533 (defvar company-search-map
534 (let ((i 0)
535 (keymap (make-keymap)))
536 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
537 'company-search-printing-char)
538 (define-key keymap [t] 'company-search-other-char)
539 (while (< i ?\s)
540 (define-key keymap (make-string 1 i) 'company-search-other-char)
541 (incf i))
542 (while (< i 256)
543 (define-key keymap (vector i) 'company-search-printing-char)
544 (incf i))
545 (let ((meta-map (make-sparse-keymap)))
546 (define-key keymap (char-to-string meta-prefix-char) meta-map)
547 (define-key keymap [escape] meta-map))
548 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
549 (define-key keymap "\e\e\e" 'company-search-other-char)
550 (define-key keymap [escape escape escape] 'company-search-other-char)
551
552 (define-key keymap "\C-g" 'company-search-abort)
553 (define-key keymap "\C-s" 'company-search-repeat-forward)
554 (define-key keymap "\C-r" 'company-search-repeat-backward)
555 (define-key keymap "\C-o" 'company-search-kill-others)
556 keymap))
557
558 (define-minor-mode company-search-mode
559 ""
560 nil company-search-lighter nil
561 (if company-search-mode
562 (if (company-manual-begin)
563 (progn
564 (setq company-search-old-selection company-selection)
565 (company-enable-overriding-keymap company-search-map)
566 (company-call-frontends 'update))
567 (setq company-search-mode nil))
568 (kill-local-variable 'company-search-string)
569 (kill-local-variable 'company-search-lighter)
570 (kill-local-variable 'company-search-old-selection)
571 (company-enable-overriding-keymap company-active-map)))
572
573 (defun company-search-candidates ()
574 (interactive)
575 (company-search-mode 1))
576
577 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
578
579 (defun company-select-next ()
580 (interactive)
581 (when (company-manual-begin)
582 (company-set-selection (1+ company-selection))))
583
584 (defun company-select-previous ()
585 (interactive)
586 (when (company-manual-begin)
587 (company-set-selection (1- company-selection))))
588
589 (defun company-complete-selection ()
590 (interactive)
591 (when (company-manual-begin)
592 (insert (company-strip-prefix (nth company-selection company-candidates)))
593 (company-abort)))
594
595 (defun company-complete-common ()
596 (interactive)
597 (when (company-manual-begin)
598 (insert (company-strip-prefix company-common))))
599
600 (defun company-complete ()
601 (interactive)
602 (when (company-manual-begin)
603 (if (or company-selection-changed
604 (eq last-command 'company-complete-common))
605 (call-interactively 'company-complete-selection)
606 (call-interactively 'company-complete-common)
607 (setq this-command 'company-complete-common))))
608
609 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
610
611 (defconst company-space-strings-limit 100)
612
613 (defconst company-space-strings
614 (let (lst)
615 (dotimes (i company-space-strings-limit)
616 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
617 (apply 'vector lst)))
618
619 (defsubst company-space-string (len)
620 (if (< len company-space-strings-limit)
621 (aref company-space-strings len)
622 (make-string len ?\ )))
623
624 (defsubst company-safe-substring (str from &optional to)
625 (let ((len (length str)))
626 (if (> from len)
627 ""
628 (if (and to (> to len))
629 (concat (substring str from)
630 (company-space-string (- to len)))
631 (substring str from to)))))
632
633 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
634
635 (defvar company-last-metadata nil)
636 (make-variable-buffer-local 'company-last-metadata)
637
638 (defun company-fetch-metadata ()
639 (let ((selected (nth company-selection company-candidates)))
640 (unless (equal selected (car company-last-metadata))
641 (setq company-last-metadata
642 (cons selected (funcall company-backend 'meta selected))))
643 (cdr company-last-metadata)))
644
645 (defun company-doc-buffer (&optional string)
646 (with-current-buffer (get-buffer-create "*Company meta-data*")
647 (erase-buffer)
648 (current-buffer)))
649
650 (defun company-show-doc-buffer ()
651 (interactive)
652 (when company-candidates
653 (save-window-excursion
654 (let* ((height (window-height))
655 (row (cdr (posn-col-row (posn-at-point))))
656 (selected (nth company-selection company-candidates))
657 (buffer (funcall company-backend 'doc-buffer selected)))
658 (if (not buffer)
659 (error "No documentation available.")
660 (display-buffer buffer)
661 (and (< (window-height) height)
662 (< (- (window-height) row 2) company-tooltip-limit)
663 (recenter (- (window-height) row 2)))
664 (while (eq 'scroll-other-window
665 (key-binding (vector (list (read-event)))))
666 (scroll-other-window))
667 (when last-input-event
668 (clear-this-command-keys t)
669 (setq unread-command-events (list last-input-event))))))))
670
671 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
672
673 (defvar company-pseudo-tooltip-overlay nil)
674 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
675
676 (defvar company-tooltip-offset 0)
677 (make-variable-buffer-local 'company-tooltip-offset)
678
679 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
680
681 (decf limit 2)
682 (setq company-tooltip-offset
683 (max (min selection company-tooltip-offset)
684 (- selection -1 limit)))
685
686 (when (<= company-tooltip-offset 1)
687 (incf limit)
688 (setq company-tooltip-offset 0))
689
690 (when (>= company-tooltip-offset (- num-lines limit 1))
691 (incf limit)
692 (when (= selection (1- num-lines))
693 (decf company-tooltip-offset)
694 (when (<= company-tooltip-offset 1)
695 (setq company-tooltip-offset 0)
696 (incf limit))))
697
698 limit)
699
700 ;;; propertize
701
702 (defsubst company-round-tab (arg)
703 (* (/ (+ arg tab-width) tab-width) tab-width))
704
705 (defun company-untabify (str)
706 (let* ((pieces (split-string str "\t"))
707 (copy pieces))
708 (while (cdr copy)
709 (setcar copy (company-safe-substring
710 (car copy) 0 (company-round-tab (string-width (car copy)))))
711 (pop copy))
712 (apply 'concat pieces)))
713
714 (defun company-fill-propertize (line width selected)
715 (setq line (company-safe-substring line 0 width))
716 (add-text-properties 0 width (list 'face 'company-tooltip) line)
717 (add-text-properties 0 (length company-common)
718 (list 'face 'company-tooltip-common) line)
719 (when selected
720 (if (and company-search-string
721 (string-match (regexp-quote company-search-string) line
722 (length company-prefix)))
723 (progn
724 (add-text-properties (match-beginning 0) (match-end 0)
725 '(face company-tooltip-selection) line)
726 (when (< (match-beginning 0) (length company-common))
727 (add-text-properties (match-beginning 0) (length company-common)
728 '(face company-tooltip-common-selection)
729 line)))
730 (add-text-properties 0 width '(face company-tooltip-selection) line)
731 (add-text-properties 0 (length company-common)
732 (list 'face 'company-tooltip-common-selection)
733 line)))
734 line)
735
736 ;;; replace
737
738 (defun company-buffer-lines (beg end)
739 (goto-char beg)
740 (let ((row (cdr (posn-col-row (posn-at-point))))
741 lines)
742 (while (and (equal (move-to-window-line (incf row)) row)
743 (<= (point) end))
744 (push (buffer-substring beg (min end (1- (point)))) lines)
745 (setq beg (point)))
746 (unless (eq beg end)
747 (push (buffer-substring beg end) lines))
748 (nreverse lines)))
749
750 (defun company-modify-line (old new offset)
751 (concat (company-safe-substring old 0 offset)
752 new
753 (company-safe-substring old (+ offset (length new)))))
754
755 (defun company-replacement-string (old lines column nl)
756 (let (new)
757 ;; Inject into old lines.
758 (while old
759 (push (company-modify-line (pop old) (pop lines) column) new))
760 ;; Append whole new lines.
761 (while lines
762 (push (company-modify-line "" (pop lines) column) new))
763 (concat (when nl "\n")
764 (mapconcat 'identity (nreverse new) "\n")
765 "\n")))
766
767 (defun company-create-lines (column selection limit)
768
769 (let ((len company-candidates-length)
770 lines
771 width
772 lines-copy
773 previous
774 remainder
775 new)
776
777 ;; Scroll to offset.
778 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
779
780 (when (> company-tooltip-offset 0)
781 (setq previous (format "...(%d)" company-tooltip-offset)))
782
783 (setq remainder (- len limit company-tooltip-offset)
784 remainder (when (> remainder 0)
785 (setq remainder (format "...(%d)" remainder))))
786
787 (decf selection company-tooltip-offset)
788 (setq width (min (length previous) (length remainder))
789 lines (nthcdr company-tooltip-offset company-candidates)
790 len (min limit len)
791 lines-copy lines)
792
793 (dotimes (i len)
794 (setq width (max (length (pop lines-copy)) width)))
795 (setq width (min width (- (window-width) column)))
796
797 (when previous
798 (push (propertize (company-safe-substring previous 0 width)
799 'face 'company-tooltip)
800 new))
801
802 (dotimes (i len)
803 (push (company-fill-propertize (company-reformat (pop lines))
804 width (equal i selection))
805 new))
806
807 (when remainder
808 (push (propertize (company-safe-substring remainder 0 width)
809 'face 'company-tooltip)
810 new))
811
812 (setq lines (nreverse new))))
813
814 ;; show
815
816 (defsubst company-pseudo-tooltip-height ()
817 "Calculate the appropriate tooltip height."
818 (max 3 (min company-tooltip-limit
819 (- (window-height) (cdr (posn-col-row (posn-at-point))) 2))))
820
821 (defun company-pseudo-tooltip-show (row column selection)
822 (company-pseudo-tooltip-hide)
823 (save-excursion
824
825 (move-to-column 0)
826
827 (let* ((height (company-pseudo-tooltip-height))
828 (lines (company-create-lines column selection height))
829 (nl (< (move-to-window-line row) row))
830 (beg (point))
831 (end (save-excursion
832 (move-to-window-line (+ row height))
833 (point)))
834 (old-string
835 (mapcar 'company-untabify (company-buffer-lines beg end)))
836 str)
837
838 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
839
840 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
841 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
842 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
843 (overlay-put company-pseudo-tooltip-overlay 'company-before
844 (company-replacement-string old-string lines column nl))
845 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
846
847 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
848
849 (defun company-pseudo-tooltip-show-at-point (pos)
850 (let ((col-row (posn-col-row (posn-at-point pos))))
851 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
852
853 (defun company-pseudo-tooltip-edit (lines selection)
854 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
855 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
856 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
857 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
858 (lines (company-create-lines column selection height)))
859 (overlay-put company-pseudo-tooltip-overlay 'company-before
860 (company-replacement-string old-string lines column nl))))
861
862 (defun company-pseudo-tooltip-hide ()
863 (when company-pseudo-tooltip-overlay
864 (delete-overlay company-pseudo-tooltip-overlay)
865 (setq company-pseudo-tooltip-overlay nil)))
866
867 (defun company-pseudo-tooltip-hide-temporarily ()
868 (when (overlayp company-pseudo-tooltip-overlay)
869 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
870 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
871
872 (defun company-pseudo-tooltip-unhide ()
873 (when company-pseudo-tooltip-overlay
874 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
875 (overlay-put company-pseudo-tooltip-overlay 'before-string
876 (overlay-get company-pseudo-tooltip-overlay 'company-before))))
877
878 (defun company-pseudo-tooltip-frontend (command)
879 (case command
880 ('pre-command (company-pseudo-tooltip-hide-temporarily))
881 ('post-command
882 (unless (and (overlayp company-pseudo-tooltip-overlay)
883 (equal (overlay-get company-pseudo-tooltip-overlay
884 'company-height)
885 (company-pseudo-tooltip-height)))
886 ;; Redraw needed.
887 (company-pseudo-tooltip-show-at-point (- (point)
888 (length company-prefix))))
889 (company-pseudo-tooltip-unhide))
890 ('hide (company-pseudo-tooltip-hide)
891 (setq company-tooltip-offset 0))
892 ('update (when (overlayp company-pseudo-tooltip-overlay)
893 (company-pseudo-tooltip-edit company-candidates
894 company-selection)))))
895
896 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
897 (unless (and (eq command 'post-command)
898 (not (cdr company-candidates)))
899 (company-pseudo-tooltip-frontend command)))
900
901 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
902
903 (defvar company-preview-overlay nil)
904 (make-variable-buffer-local 'company-preview-overlay)
905
906 (defun company-preview-show-at-point (pos)
907 (company-preview-hide)
908
909 (setq company-preview-overlay (make-overlay pos pos))
910
911 (let ((completion (company-strip-prefix (nth company-selection
912 company-candidates))))
913 (and (equal pos (point))
914 (not (equal completion ""))
915 (add-text-properties 0 1 '(cursor t) completion))
916
917 (setq completion (propertize completion 'face 'company-preview))
918 (add-text-properties 0 (- (length company-common) (length company-prefix))
919 '(face company-preview-common) completion)
920
921 (overlay-put company-preview-overlay 'after-string completion)
922 (overlay-put company-preview-overlay 'window (selected-window))))
923
924 (defun company-preview-hide ()
925 (when company-preview-overlay
926 (delete-overlay company-preview-overlay)
927 (setq company-preview-overlay nil)))
928
929 (defun company-preview-frontend (command)
930 (case command
931 ('pre-command (company-preview-hide))
932 ('post-command (company-preview-show-at-point (point)))
933 ('hide (company-preview-hide))))
934
935 (defun company-preview-if-just-one-frontend (command)
936 (unless (and (eq command 'post-command)
937 (cdr company-candidates))
938 (company-preview-frontend command)))
939
940 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
941
942 (defvar company-echo-last-msg nil)
943 (make-variable-buffer-local 'company-echo-last-msg)
944
945 (defvar company-echo-timer nil)
946
947 (defvar company-echo-delay .1)
948
949 (defun company-echo-show (&optional getter)
950 (when getter
951 (setq company-echo-last-msg (funcall getter)))
952 (let ((message-log-max nil))
953 (if company-echo-last-msg
954 (message "%s" company-echo-last-msg)
955 (message ""))))
956
957 (defsubst company-echo-show-soon (&optional getter)
958 (when company-echo-timer
959 (cancel-timer company-echo-timer))
960 (setq company-echo-timer (run-with-timer company-echo-delay nil
961 'company-echo-show getter)))
962
963 (defun company-echo-format ()
964
965 (let ((limit (window-width (minibuffer-window)))
966 (len -1)
967 ;; Roll to selection.
968 (candidates (nthcdr company-selection company-candidates))
969 comp msg)
970
971 (while candidates
972 (setq comp (company-reformat (pop candidates))
973 len (+ len 1 (length comp)))
974 (if (>= len limit)
975 (setq candidates nil)
976 (setq comp (propertize comp 'face 'company-echo))
977 (add-text-properties 0 (length company-common)
978 '(face company-echo-common) comp)
979 (push comp msg)))
980
981 (mapconcat 'identity (nreverse msg) " ")))
982
983 (defun company-echo-hide ()
984 (when company-echo-timer
985 (cancel-timer company-echo-timer))
986 (setq company-echo-last-msg "")
987 (company-echo-show))
988
989 (defun company-echo-frontend (command)
990 (case command
991 ('pre-command (company-echo-show-soon))
992 ('post-command (company-echo-show-soon 'company-echo-format))
993 ('hide (company-echo-hide))))
994
995 (defun company-echo-metadata-frontend (command)
996 (case command
997 ('pre-command (company-echo-show-soon))
998 ('post-command (company-echo-show-soon 'company-fetch-metadata))
999 ('hide (company-echo-hide))))
1000
1001 (provide 'company)
1002 ;;; company.el ends here