]> code.delx.au - gnu-emacs-elpa/blob - company.el
Use overriding active map.
[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-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 (defvar company-timer nil)
147
148 (defun company-timer-set (variable value)
149 (set variable value)
150 (when company-timer (cancel-timer company-timer))
151 (when (numberp value)
152 (setq company-timer (run-with-idle-timer value t 'company-idle-begin))))
153
154 (defcustom company-idle-delay .7
155 "*"
156 :set 'company-timer-set
157 :group 'company
158 :type '(choice (const :tag "never (nil)" nil)
159 (const :tag "immediate (t)" t)
160 (number :tag "seconds")))
161
162 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
163
164 (defvar company-mode-map (make-sparse-keymap))
165
166 (defvar company-active-map
167 (let ((keymap (make-sparse-keymap)))
168 (define-key keymap (kbd "M-n") 'company-select-next)
169 (define-key keymap (kbd "M-p") 'company-select-previous)
170 (define-key keymap "\C-m" 'company-complete-selection)
171 (define-key keymap "\t" 'company-complete-common)
172 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
173 keymap))
174
175 ;;;###autoload
176 (define-minor-mode company-mode
177 ""
178 nil " comp" company-mode-map
179 (if company-mode
180 (progn
181 (add-hook 'pre-command-hook 'company-pre-command nil t)
182 (add-hook 'post-command-hook 'company-post-command nil t)
183 (company-timer-set 'company-idle-delay
184 company-idle-delay))
185 (remove-hook 'pre-command-hook 'company-pre-command t)
186 (remove-hook 'post-command-hook 'company-post-command t)
187 (company-cancel)
188 (kill-local-variable 'company-point)))
189
190 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
191
192 (defvar company-overriding-keymap-bound nil)
193 (make-variable-buffer-local 'company-overriding-keymap-bound)
194
195 (defvar company-old-keymap nil)
196 (make-variable-buffer-local 'company-old-keymap)
197
198 (defvar company-my-keymap nil)
199 (make-variable-buffer-local 'company-my-keymap)
200
201 (defsubst company-enable-overriding-keymap (keymap)
202 (setq company-my-keymap keymap)
203 (when company-overriding-keymap-bound
204 (company-uninstall-map)))
205
206 (defun company-install-map ()
207 (unless (or company-overriding-keymap-bound
208 (null company-my-keymap))
209 (setq company-old-keymap overriding-terminal-local-map
210 overriding-terminal-local-map company-my-keymap
211 company-overriding-keymap-bound t)))
212
213 (defun company-uninstall-map ()
214 (when (and company-overriding-keymap-bound
215 (eq overriding-terminal-local-map company-my-keymap))
216 (setq overriding-terminal-local-map company-old-keymap
217 company-overriding-keymap-bound nil)))
218
219 ;; Hack:
220 ;; Emacs calculates the active keymaps before reading the event. That means we
221 ;; cannot change the keymap from a timer. So we send a bogus command.
222 (defun company-ignore ()
223 (interactive))
224
225 (global-set-key '[31415926] 'company-ignore)
226
227 (defun company-input-noop ()
228 (push 31415926 unread-command-events))
229
230 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231
232 (defun company-grab (regexp &optional expression)
233 (when (looking-back regexp)
234 (or (match-string-no-properties (or expression 0)) "")))
235
236 (defun company-in-string-or-comment (&optional point)
237 (let ((pos (syntax-ppss)))
238 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
239
240 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
241
242 (defvar company-backend nil)
243 (make-variable-buffer-local 'company-backend)
244
245 (defvar company-prefix nil)
246 (make-variable-buffer-local 'company-prefix)
247
248 (defvar company-candidates nil)
249 (make-variable-buffer-local 'company-candidates)
250
251 (defvar company-candidates-cache nil)
252 (make-variable-buffer-local 'company-candidates-cache)
253
254 (defvar company-common nil)
255 (make-variable-buffer-local 'company-common)
256
257 (defvar company-selection 0)
258 (make-variable-buffer-local 'company-selection)
259
260 (defvar company-selection-changed nil)
261 (make-variable-buffer-local 'company-selection-changed)
262
263 (defvar company-point nil)
264 (make-variable-buffer-local 'company-point)
265
266 (defvar company-disabled-backends nil)
267
268 (defsubst company-strip-prefix (str)
269 (substring str (length company-prefix)))
270
271 (defsubst company-reformat (candidate)
272 ;; company-ispell needs this, because the results are always lower-case
273 ;; It's mory efficient to fix it only when they are displayed.
274 (concat company-prefix (substring candidate (length company-prefix))))
275
276 (defsubst company-should-complete (prefix)
277 (and (eq company-idle-delay t)
278 (>= (length prefix) company-minimum-prefix-length)))
279
280 (defsubst company-call-frontends (command)
281 (dolist (frontend company-frontends)
282 (funcall frontend command)))
283
284 (defsubst company-calculate-candidates (prefix)
285 (or (setq company-candidates (cdr (assoc prefix company-candidates-cache)))
286 (let ((len (length prefix))
287 (completion-ignore-case (funcall company-backend 'ignore-case))
288 prev)
289 (dotimes (i len)
290 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
291 company-candidates-cache)))
292 (setq company-candidates (all-completions prefix prev))
293 (return t))))
294 (progn
295 (setq company-candidates (funcall company-backend 'candidates prefix))
296 (unless (funcall company-backend 'sorted)
297 (setq company-candidates (sort company-candidates 'string<)))))
298 (unless (assoc prefix company-candidates-cache)
299 (push (cons prefix company-candidates) company-candidates-cache))
300 (setq company-selection 0
301 company-prefix prefix)
302 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
303 (setq company-common (try-completion company-prefix company-candidates)))
304 (when (eq company-common t)
305 (setq company-candidates nil))
306 company-candidates)
307
308 (defun company-idle-begin ()
309 (and company-mode
310 (not company-candidates)
311 (not (equal (point) company-point))
312 (let ((company-idle-delay t))
313 (company-begin)
314 (when company-candidates
315 (company-input-noop)
316 (company-post-command)))))
317
318 (defun company-manual-begin ()
319 (and company-mode
320 (not company-candidates)
321 (let ((company-idle-delay t)
322 (company-minimum-prefix-length 0))
323 (company-begin)))
324 ;; Return non-nil if active.
325 company-candidates)
326
327 (defun company-continue ()
328 (when company-candidates
329 (let ((new-prefix (funcall company-backend 'prefix)))
330 (unless (and (= (- (point) (length new-prefix))
331 (- company-point (length company-prefix)))
332 (or (equal company-prefix new-prefix)
333 (company-calculate-candidates new-prefix)))
334 (setq company-candidates nil)))))
335
336 (defun company-begin ()
337 (company-continue)
338 (unless company-candidates
339 (let (prefix)
340 (dolist (backend company-backends)
341 (unless (fboundp backend)
342 (ignore-errors (require backend nil t)))
343 (if (fboundp backend)
344 (when (setq prefix (funcall backend 'prefix))
345 (when (company-should-complete prefix)
346 (setq company-backend backend)
347 (company-calculate-candidates prefix))
348 (return prefix))
349 (unless (memq backend company-disabled-backends)
350 (push backend company-disabled-backends)
351 (message "Company back-end '%s' could not be initialized"
352 backend))))))
353 (if company-candidates
354 (progn
355 (setq company-point (point))
356 (company-enable-overriding-keymap company-active-map)
357 (company-call-frontends 'update))
358 (company-cancel)))
359
360 (defun company-cancel ()
361 (setq company-backend nil
362 company-prefix nil
363 company-candidates nil
364 company-candidates-cache nil
365 company-common nil
366 company-selection 0
367 company-selection-changed nil
368 company-point nil)
369 (company-call-frontends 'hide)
370 (company-enable-overriding-keymap nil))
371
372 (defun company-abort ()
373 (company-cancel)
374 ;; Don't start again, unless started manually.
375 (setq company-point (point)))
376
377 (defun company-pre-command ()
378 (unless (eq this-command 'company-show-doc-buffer)
379 (condition-case err
380 (when company-candidates
381 (company-call-frontends 'pre-command))
382 (error (message "Company: An error occurred in pre-command")
383 (message "%s" (error-message-string err))
384 (company-cancel))))
385 (company-uninstall-map))
386
387 (defun company-post-command ()
388 (unless (eq this-command 'company-show-doc-buffer)
389 (condition-case err
390 (progn
391 (unless (equal (point) company-point)
392 (company-begin))
393 (when company-candidates
394 (company-call-frontends 'post-command)))
395 (error (message "Company: An error occurred in post-command")
396 (message "%s" (error-message-string err))
397 (company-cancel))))
398 (company-install-map))
399
400 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
401
402 (defun company-select-next ()
403 (interactive)
404 (when (company-manual-begin)
405 (setq company-selection (min (1- (length company-candidates))
406 (1+ company-selection))
407 company-selection-changed t))
408 (company-call-frontends 'update))
409
410 (defun company-select-previous ()
411 (interactive)
412 (when (company-manual-begin)
413 (setq company-selection (max 0 (1- company-selection))
414 company-selection-changed t))
415 (company-call-frontends 'update))
416
417 (defun company-complete-selection ()
418 (interactive)
419 (when (company-manual-begin)
420 (insert (company-strip-prefix (nth company-selection company-candidates)))
421 (company-abort)))
422
423 (defun company-complete-common ()
424 (interactive)
425 (when (company-manual-begin)
426 (insert (company-strip-prefix company-common))))
427
428 (defun company-complete ()
429 (interactive)
430 (when (company-manual-begin)
431 (if (or company-selection-changed
432 (eq last-command 'company-complete-common))
433 (call-interactively 'company-complete-selection)
434 (call-interactively 'company-complete-common)
435 (setq this-command 'company-complete-common))))
436
437 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
438
439 (defconst company-space-strings-limit 100)
440
441 (defconst company-space-strings
442 (let (lst)
443 (dotimes (i company-space-strings-limit)
444 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
445 (apply 'vector lst)))
446
447 (defsubst company-space-string (len)
448 (if (< len company-space-strings-limit)
449 (aref company-space-strings len)
450 (make-string len ?\ )))
451
452 (defsubst company-safe-substring (str from &optional to)
453 (let ((len (length str)))
454 (if (> from len)
455 ""
456 (if (and to (> to len))
457 (concat (substring str from)
458 (company-space-string (- to len)))
459 (substring str from to)))))
460
461 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
462
463 (defvar company-last-metadata nil)
464 (make-variable-buffer-local 'company-last-metadata)
465
466 (defun company-fetch-metadata ()
467 (let ((selected (nth company-selection company-candidates)))
468 (unless (equal selected (car company-last-metadata))
469 (setq company-last-metadata
470 (cons selected (funcall company-backend 'meta selected))))
471 (cdr company-last-metadata)))
472
473 (defun company-doc-buffer (&optional string)
474 (with-current-buffer (get-buffer-create "*Company meta-data*")
475 (erase-buffer)
476 (current-buffer)))
477
478 (defun company-show-doc-buffer ()
479 (interactive)
480 (when company-candidates
481 (save-window-excursion
482 (let* ((selected (nth company-selection company-candidates))
483 (buffer (funcall company-backend 'doc-buffer selected)))
484 (if (not buffer)
485 (error "No documentation available.")
486 (display-buffer buffer)
487 (read-event)
488 (when last-input-event
489 (clear-this-command-keys t)
490 (setq unread-command-events (list last-input-event))))))))
491
492 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
493
494 (defvar company-pseudo-tooltip-overlay nil)
495 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
496
497 (defvar company-tooltip-offset 0)
498 (make-variable-buffer-local 'company-tooltip-offset)
499
500 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
501
502 (decf limit 2)
503 (setq company-tooltip-offset
504 (max (min selection company-tooltip-offset)
505 (- selection -1 limit)))
506
507 (when (<= company-tooltip-offset 1)
508 (incf limit)
509 (setq company-tooltip-offset 0))
510
511 (when (>= company-tooltip-offset (- num-lines limit 1))
512 (incf limit)
513 (when (= selection (1- num-lines))
514 (setq company-tooltip-offset (max (1- company-tooltip-offset) 0))))
515
516 limit)
517
518 ;;; propertize
519
520 (defun company-fill-propertize (line width selected)
521 (setq line (company-safe-substring line 0 width))
522 (add-text-properties 0 width
523 (list 'face (if selected
524 'company-tooltip-selection
525 'company-tooltip)) line)
526 (add-text-properties 0 (length company-common)
527 (list 'face (if selected
528 'company-tooltip-common-selection
529 'company-tooltip-common)) line)
530 line)
531
532 ;;; replace
533
534 (defun company-buffer-lines (beg end)
535 (goto-char beg)
536 (let ((row (cdr (posn-col-row (posn-at-point))))
537 lines)
538 (while (and (equal (move-to-window-line (incf row)) row)
539 (<= (point) end))
540 (push (buffer-substring beg (min end (1- (point)))) lines)
541 (setq beg (point)))
542 (unless (eq beg end)
543 (push (buffer-substring beg end) lines))
544 (nreverse lines)))
545
546 (defun company-modify-line (old new offset)
547 (concat (company-safe-substring old 0 offset)
548 new
549 (company-safe-substring old (+ offset (length new)))))
550
551 (defun company-replacement-string (old lines column nl)
552 (let (new)
553 ;; Inject into old lines.
554 (while old
555 (push (company-modify-line (pop old) (pop lines) column) new))
556 ;; Append whole new lines.
557 (while lines
558 (push (company-modify-line "" (pop lines) column) new))
559 (concat (when nl "\n")
560 (mapconcat 'identity (nreverse new) "\n")
561 "\n")))
562
563 (defun company-create-lines (column lines selection)
564
565 (let ((limit (max company-tooltip-limit 3))
566 (len (length lines))
567 width
568 lines-copy
569 previous
570 remainder
571 new)
572
573 ;; Scroll to offset.
574 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
575
576 (when (> company-tooltip-offset 0)
577 (setq previous (format "...(%d)" company-tooltip-offset)))
578
579 (setq remainder (- len limit company-tooltip-offset)
580 remainder (when (> remainder 0)
581 (setq remainder (format "...(%d)" remainder))))
582
583 (decf selection company-tooltip-offset)
584 (setq width (min (length previous) (length remainder))
585 lines (nthcdr company-tooltip-offset lines)
586 len (min limit (length lines))
587 lines-copy lines)
588
589 (dotimes (i len)
590 (setq width (max (length (pop lines-copy)) width)))
591 (setq width (min width (- (window-width) column)))
592
593 (when previous
594 (push (propertize (company-safe-substring previous 0 width)
595 'face 'company-tooltip)
596 new))
597
598 (dotimes (i len)
599 (push (company-fill-propertize (company-reformat (pop lines))
600 width (equal i selection))
601 new))
602
603 (when remainder
604 (push (propertize (company-safe-substring remainder 0 width)
605 'face 'company-tooltip)
606 new))
607
608 (setq lines (nreverse new))))
609
610 ;; show
611
612 (defun company-pseudo-tooltip-show (row column lines selection)
613 (company-pseudo-tooltip-hide)
614 (unless lines (error "No text provided"))
615 (save-excursion
616
617 (move-to-column 0)
618
619 (let* ((lines (company-create-lines column lines selection))
620 (nl (< (move-to-window-line row) row))
621 (beg (point))
622 (end (save-excursion
623 (move-to-window-line (min (window-height)
624 (+ row company-tooltip-limit)))
625 (point)))
626 (old-string (company-buffer-lines beg end))
627 str)
628
629 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
630
631 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
632 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
633 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
634 (overlay-put company-pseudo-tooltip-overlay 'company-before
635 (company-replacement-string old-string lines column nl))
636
637 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
638
639 (defun company-pseudo-tooltip-show-at-point (pos)
640 (let ((col-row (posn-col-row (posn-at-point pos))))
641 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
642 company-candidates company-selection)))
643
644 (defun company-pseudo-tooltip-edit (lines selection)
645 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
646 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
647 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
648 (lines (company-create-lines column lines selection)))
649 (overlay-put company-pseudo-tooltip-overlay 'company-before
650 (company-replacement-string old-string lines column nl))))
651
652 (defun company-pseudo-tooltip-hide ()
653 (when company-pseudo-tooltip-overlay
654 (delete-overlay company-pseudo-tooltip-overlay)
655 (setq company-pseudo-tooltip-overlay nil)))
656
657 (defun company-pseudo-tooltip-hide-temporarily ()
658 (when (overlayp company-pseudo-tooltip-overlay)
659 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
660 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
661
662 (defun company-pseudo-tooltip-unhide ()
663 (when company-pseudo-tooltip-overlay
664 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
665 (overlay-put company-pseudo-tooltip-overlay 'before-string
666 (overlay-get company-pseudo-tooltip-overlay 'company-before))))
667
668 (defun company-pseudo-tooltip-frontend (command)
669 (case command
670 ('pre-command (company-pseudo-tooltip-hide-temporarily))
671 ('post-command
672 (unless (overlayp company-pseudo-tooltip-overlay)
673 (company-pseudo-tooltip-show-at-point (- (point)
674 (length company-prefix))))
675 (company-pseudo-tooltip-unhide))
676 ('hide (company-pseudo-tooltip-hide)
677 (setq company-tooltip-offset 0))
678 ('update (when (overlayp company-pseudo-tooltip-overlay)
679 (company-pseudo-tooltip-edit company-candidates
680 company-selection)))))
681
682 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
683 (unless (and (eq command 'post-command)
684 (not (cdr company-candidates)))
685 (company-pseudo-tooltip-frontend command)))
686
687 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
688
689 (defvar company-preview-overlay nil)
690 (make-variable-buffer-local 'company-preview-overlay)
691
692 (defun company-preview-show-at-point (pos)
693 (company-preview-hide)
694
695 (setq company-preview-overlay (make-overlay pos pos))
696
697 (let ((completion (company-strip-prefix (nth company-selection
698 company-candidates))))
699 (and (equal pos (point))
700 (not (equal completion ""))
701 (add-text-properties 0 1 '(cursor t) completion))
702
703 (setq completion (propertize completion 'face 'company-preview))
704 (add-text-properties 0 (- (length company-common) (length company-prefix))
705 '(face company-preview-common) completion)
706
707 (overlay-put company-preview-overlay 'after-string completion)
708 (overlay-put company-preview-overlay 'window (selected-window))))
709
710 (defun company-preview-hide ()
711 (when company-preview-overlay
712 (delete-overlay company-preview-overlay)
713 (setq company-preview-overlay nil)))
714
715 (defun company-preview-frontend (command)
716 (case command
717 ('pre-command (company-preview-hide))
718 ('post-command (company-preview-show-at-point (point)))
719 ('hide (company-preview-hide))))
720
721 (defun company-preview-if-just-one-frontend (command)
722 (unless (and (eq command 'post-command)
723 (cdr company-candidates))
724 (company-preview-frontend command)))
725
726 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
727
728 (defvar company-echo-last-msg nil)
729 (make-variable-buffer-local 'company-echo-last-msg)
730
731 (defun company-echo-refresh ()
732 (let ((message-log-max nil))
733 (if company-echo-last-msg
734 (message "%s" company-echo-last-msg)
735 (message ""))))
736
737 (defun company-echo-show (candidates)
738
739 ;; Roll to selection.
740 (setq candidates (nthcdr company-selection candidates))
741
742 (let ((limit (window-width (minibuffer-window)))
743 (len -1)
744 comp msg)
745 (while candidates
746 (setq comp (company-reformat (pop candidates))
747 len (+ len 1 (length comp)))
748 (if (>= len limit)
749 (setq candidates nil)
750 (setq comp (propertize comp 'face 'company-echo))
751 (add-text-properties 0 (length company-common)
752 '(face company-echo-common) comp)
753 (push comp msg)))
754
755 (setq company-echo-last-msg (mapconcat 'identity (nreverse msg) " "))
756 (company-echo-refresh)))
757
758 (defun company-echo-frontend (command)
759 (case command
760 ('pre-command (company-echo-refresh))
761 ('post-command (company-echo-show company-candidates))
762 ('hide (setq company-echo-last-msg nil))))
763
764 (defun company-echo-metadata-frontend (command)
765 (case command
766 ('pre-command (company-echo-refresh))
767 ('post-command (setq company-echo-last-msg (company-fetch-metadata))
768 (company-echo-refresh))
769 ('hide (setq company-echo-last-msg nil))))
770
771
772 (provide 'company)
773 ;;; company.el ends here