1 ;;; company.el --- extensible inline text completion mechanism
3 ;; Copyright (C) 2009 Nikolaj Schumacher
5 ;; Author: Nikolaj Schumacher <bugs * nschum de>
7 ;; Keywords: abbrev, convenience, matchis
8 ;; URL: http://nschum.de/src/emacs/company/
9 ;; Compatibility: GNU Emacs 23.x
11 ;; This file is NOT part of GNU Emacs.
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.
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.
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/>.
34 (eval-when-compile (require 'cl))
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$")
48 (defface company-tooltip
49 '((t :background "yellow"
54 (defface company-tooltip-selection
55 '((t :background "orange1"
60 (defface company-tooltip-common
61 '((t :inherit company-tooltip
66 (defface company-tooltip-common-selection
67 '((t :inherit company-tooltip-selection
72 (defcustom company-tooltip-limit 10
77 (defface company-preview
78 '((t :background "blue4"
83 (defface company-preview-common
84 '((t :inherit company-preview
89 (defface company-echo nil
93 (defface company-echo-common
94 '((((background dark)) (:foreground "firebrick1"))
95 (((background light)) (:background "firebrick4")))
99 (defun company-frontends-set (variable value)
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))
115 (setq value (append (delq f value) (list f)))))
116 (set variable value))
118 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
119 company-preview-if-just-one-frontend
120 company-echo-metadata-frontend)
122 :set 'company-frontends-set
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))))
134 (defcustom company-backends '(company-elisp company-nxml company-css
135 company-semantic company-oddmuse
136 company-files company-dabbrev)
139 :type '(repeat (function :tag "function" nil)))
141 (defcustom company-minimum-prefix-length 3
144 :type '(integer :tag "prefix length"))
146 (defvar company-timer nil)
148 (defun company-timer-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))))
154 (defcustom company-idle-delay .7
156 :set 'company-timer-set
158 :type '(choice (const :tag "never (nil)" nil)
159 (const :tag "immediate (t)" t)
160 (number :tag "seconds")))
162 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
164 (defvar company-mode-map (make-sparse-keymap))
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 (define-key keymap "\C-s" 'company-search-candidates)
177 (define-minor-mode company-mode
179 nil " comp" company-mode-map
182 (add-hook 'pre-command-hook 'company-pre-command nil t)
183 (add-hook 'post-command-hook 'company-post-command nil t)
184 (company-timer-set 'company-idle-delay
186 (remove-hook 'pre-command-hook 'company-pre-command t)
187 (remove-hook 'post-command-hook 'company-post-command t)
189 (kill-local-variable 'company-point)))
191 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
193 (defvar company-overriding-keymap-bound nil)
194 (make-variable-buffer-local 'company-overriding-keymap-bound)
196 (defvar company-old-keymap nil)
197 (make-variable-buffer-local 'company-old-keymap)
199 (defvar company-my-keymap nil)
200 (make-variable-buffer-local 'company-my-keymap)
202 (defsubst company-enable-overriding-keymap (keymap)
203 (setq company-my-keymap keymap)
204 (when company-overriding-keymap-bound
205 (company-uninstall-map)))
207 (defun company-install-map ()
208 (unless (or company-overriding-keymap-bound
209 (null company-my-keymap))
210 (setq company-old-keymap overriding-terminal-local-map
211 overriding-terminal-local-map company-my-keymap
212 company-overriding-keymap-bound t)))
214 (defun company-uninstall-map ()
215 (when (and company-overriding-keymap-bound
216 (eq overriding-terminal-local-map company-my-keymap))
217 (setq overriding-terminal-local-map company-old-keymap
218 company-overriding-keymap-bound nil)))
221 ;; Emacs calculates the active keymaps before reading the event. That means we
222 ;; cannot change the keymap from a timer. So we send a bogus command.
223 (defun company-ignore ()
226 (global-set-key '[31415926] 'company-ignore)
228 (defun company-input-noop ()
229 (push 31415926 unread-command-events))
231 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
233 (defun company-grab (regexp &optional expression)
234 (when (looking-back regexp)
235 (or (match-string-no-properties (or expression 0)) "")))
237 (defun company-in-string-or-comment (&optional point)
238 (let ((pos (syntax-ppss)))
239 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
241 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243 (defvar company-backend nil)
244 (make-variable-buffer-local 'company-backend)
246 (defvar company-prefix nil)
247 (make-variable-buffer-local 'company-prefix)
249 (defvar company-candidates nil)
250 (make-variable-buffer-local 'company-candidates)
252 (defvar company-candidates-cache nil)
253 (make-variable-buffer-local 'company-candidates-cache)
255 (defvar company-candidates-predicate nil)
256 (make-variable-buffer-local 'company-candidates-predicate)
258 (defvar company-common nil)
259 (make-variable-buffer-local 'company-common)
261 (defvar company-selection 0)
262 (make-variable-buffer-local 'company-selection)
264 (defvar company-selection-changed nil)
265 (make-variable-buffer-local 'company-selection-changed)
267 (defvar company-point nil)
268 (make-variable-buffer-local 'company-point)
270 (defvar company-disabled-backends nil)
272 (defsubst company-strip-prefix (str)
273 (substring str (length company-prefix)))
275 (defsubst company-reformat (candidate)
276 ;; company-ispell needs this, because the results are always lower-case
277 ;; It's mory efficient to fix it only when they are displayed.
278 (concat company-prefix (substring candidate (length company-prefix))))
280 (defsubst company-should-complete (prefix)
281 (and (eq company-idle-delay t)
282 (>= (length prefix) company-minimum-prefix-length)))
284 (defsubst company-call-frontends (command)
285 (dolist (frontend company-frontends)
286 (funcall frontend command)))
288 (defsubst company-set-selection (selection &optional force-update)
289 (setq selection (max 0 (min (1- (length company-candidates)) selection)))
290 (when (or force-update (not (equal selection company-selection)))
291 (setq company-selection selection
292 company-selection-changed t)
293 (company-call-frontends 'update)))
295 (defun company-apply-predicate (candidates predicate)
297 (dolist (c candidates)
298 (when (funcall predicate c)
302 (defsubst company-calculate-candidates (prefix)
303 (or (setq company-candidates (cdr (assoc prefix company-candidates-cache)))
304 (let ((len (length prefix))
305 (completion-ignore-case (funcall company-backend 'ignore-case))
308 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
309 company-candidates-cache)))
310 (setq company-candidates (all-completions prefix prev))
313 (setq company-candidates (funcall company-backend 'candidates prefix))
314 (when company-candidates-predicate
315 (setq company-candidates
316 (company-apply-predicate company-candidates
317 company-candidates-predicate)))
318 (unless (funcall company-backend 'sorted)
319 (setq company-candidates (sort company-candidates 'string<)))))
320 (unless (assoc prefix company-candidates-cache)
321 (push (cons prefix company-candidates) company-candidates-cache))
322 (setq company-selection 0
323 company-prefix prefix)
324 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
325 (setq company-common (try-completion company-prefix company-candidates)))
326 (when (eq company-common t)
327 (setq company-candidates nil))
330 (defun company-idle-begin ()
332 (not company-candidates)
333 (not (equal (point) company-point))
334 (let ((company-idle-delay t))
336 (when company-candidates
338 (company-post-command)))))
340 (defun company-manual-begin ()
342 (not company-candidates)
343 (let ((company-idle-delay t)
344 (company-minimum-prefix-length 0))
346 ;; Return non-nil if active.
349 (defun company-continue ()
350 (when company-candidates
351 (when (funcall company-backend 'no-cache)
352 ;; Don't complete existing candidates, fetch new ones.
353 (setq company-candidates-cache nil))
354 (let ((new-prefix (funcall company-backend 'prefix)))
355 (unless (and (= (- (point) (length new-prefix))
356 (- company-point (length company-prefix)))
357 (or (equal company-prefix new-prefix)
358 (company-calculate-candidates new-prefix)))
359 (setq company-candidates nil)))))
361 (defun company-begin ()
362 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
363 ;; Don't complete in these cases.
364 (setq company-candidates nil)
366 (unless company-candidates
368 (dolist (backend company-backends)
369 (unless (fboundp backend)
370 (ignore-errors (require backend nil t)))
371 (if (fboundp backend)
372 (when (setq prefix (funcall backend 'prefix))
373 (when (company-should-complete prefix)
374 (setq company-backend backend)
375 (company-calculate-candidates prefix))
377 (unless (memq backend company-disabled-backends)
378 (push backend company-disabled-backends)
379 (message "Company back-end '%s' could not be initialized"
381 (if company-candidates
383 (setq company-point (point))
384 (company-enable-overriding-keymap company-active-map)
385 (company-call-frontends 'update))
388 (defun company-cancel ()
389 (setq company-backend nil
391 company-candidates nil
392 company-candidates-cache nil
393 company-candidates-predicate nil
396 company-selection-changed nil
398 (company-search-mode 0)
399 (company-call-frontends 'hide)
400 (company-enable-overriding-keymap nil))
402 (defun company-abort ()
404 ;; Don't start again, unless started manually.
405 (setq company-point (point)))
407 (defun company-pre-command ()
408 (unless (eq this-command 'company-show-doc-buffer)
410 (when company-candidates
411 (company-call-frontends 'pre-command))
412 (error (message "Company: An error occurred in pre-command")
413 (message "%s" (error-message-string err))
415 (company-uninstall-map))
417 (defun company-post-command ()
418 (unless (eq this-command 'company-show-doc-buffer)
421 (unless (equal (point) company-point)
423 (when company-candidates
424 (company-call-frontends 'post-command)))
425 (error (message "Company: An error occurred in post-command")
426 (message "%s" (error-message-string err))
428 (company-install-map))
430 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
432 (defvar company-search-string nil)
433 (make-variable-buffer-local 'company-search-string)
435 (defvar company-search-lighter " Search: \"\"")
436 (make-variable-buffer-local 'company-search-lighter)
438 (defvar company-search-old-map nil)
439 (make-variable-buffer-local 'company-search-old-map)
441 (defvar company-search-old-selection 0)
442 (make-variable-buffer-local 'company-search-old-selection)
444 (defun company-search (text lines)
445 (let ((quoted (regexp-quote text))
448 (when (string-match quoted line (length company-prefix))
452 (defun company-search-printing-char ()
454 (setq company-search-string
455 (concat (or company-search-string "") (string last-command-event))
456 company-search-lighter (concat " Search: \"" company-search-string
458 (let ((pos (company-search company-search-string
459 (nthcdr company-selection company-candidates))))
462 (company-set-selection (+ company-selection pos) t))))
464 (defun company-search-repeat-forward ()
466 (let ((pos (company-search company-search-string
467 (cdr (nthcdr company-selection
468 company-candidates)))))
471 (company-set-selection (+ company-selection pos 1) t))))
473 (defun company-search-repeat-backward ()
475 (let ((pos (company-search company-search-string
476 (nthcdr (- (length company-candidates)
478 (reverse company-candidates)))))
481 (company-set-selection (- company-selection pos 1) t))))
483 (defun company-search-kill-others ()
485 (let ((predicate `(lambda (candidate)
486 (string-match ,company-search-string candidate))))
488 (setq company-candidates-predicate predicate)
489 (company-manual-begin)))
491 (defun company-search-abort ()
493 (company-set-selection company-search-old-selection t)
494 (company-search-mode 0))
496 (defun company-search-other-char ()
498 (company-search-mode 0)
499 (when last-input-event
500 (clear-this-command-keys t)
501 (setq unread-command-events (list last-input-event))))
503 (defvar company-search-map
505 (keymap (make-keymap)))
506 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
507 'company-search-printing-char)
508 (define-key keymap [t] 'company-search-other-char)
510 (define-key keymap (make-string 1 i) 'company-search-other-char)
513 (define-key keymap (vector i) 'company-search-printing-char)
515 (let ((meta-map (make-sparse-keymap)))
516 (define-key keymap (char-to-string meta-prefix-char) meta-map)
517 (define-key keymap [escape] meta-map))
518 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
519 (define-key keymap "\e\e\e" 'company-search-other-char)
520 (define-key keymap [escape escape escape] 'company-search-other-char)
522 (define-key keymap "\C-g" 'company-search-abort)
523 (define-key keymap "\C-s" 'company-search-repeat-forward)
524 (define-key keymap "\C-r" 'company-search-repeat-backward)
525 (define-key keymap "\C-o" 'company-search-kill-others)
528 (define-minor-mode company-search-mode
530 nil company-search-lighter nil
531 (if company-search-mode
532 (if (company-manual-begin)
534 (setq company-search-old-selection company-selection)
535 (company-enable-overriding-keymap company-search-map)
536 (company-call-frontends 'update))
537 (setq company-search-mode nil))
538 (kill-local-variable 'company-search-string)
539 (kill-local-variable 'company-search-lighter)
540 (kill-local-variable 'company-search-old-selection)
541 (company-enable-overriding-keymap company-active-map)))
543 (defun company-search-candidates ()
545 (company-search-mode 1))
547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
549 (defun company-select-next ()
551 (when (company-manual-begin)
552 (company-set-selection (1+ company-selection))))
554 (defun company-select-previous ()
556 (when (company-manual-begin)
557 (company-set-selection (1- company-selection))))
559 (defun company-complete-selection ()
561 (when (company-manual-begin)
562 (insert (company-strip-prefix (nth company-selection company-candidates)))
565 (defun company-complete-common ()
567 (when (company-manual-begin)
568 (insert (company-strip-prefix company-common))))
570 (defun company-complete ()
572 (when (company-manual-begin)
573 (if (or company-selection-changed
574 (eq last-command 'company-complete-common))
575 (call-interactively 'company-complete-selection)
576 (call-interactively 'company-complete-common)
577 (setq this-command 'company-complete-common))))
579 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
581 (defconst company-space-strings-limit 100)
583 (defconst company-space-strings
585 (dotimes (i company-space-strings-limit)
586 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
587 (apply 'vector lst)))
589 (defsubst company-space-string (len)
590 (if (< len company-space-strings-limit)
591 (aref company-space-strings len)
592 (make-string len ?\ )))
594 (defsubst company-safe-substring (str from &optional to)
595 (let ((len (length str)))
598 (if (and to (> to len))
599 (concat (substring str from)
600 (company-space-string (- to len)))
601 (substring str from to)))))
603 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
605 (defvar company-last-metadata nil)
606 (make-variable-buffer-local 'company-last-metadata)
608 (defun company-fetch-metadata ()
609 (let ((selected (nth company-selection company-candidates)))
610 (unless (equal selected (car company-last-metadata))
611 (setq company-last-metadata
612 (cons selected (funcall company-backend 'meta selected))))
613 (cdr company-last-metadata)))
615 (defun company-doc-buffer (&optional string)
616 (with-current-buffer (get-buffer-create "*Company meta-data*")
620 (defun company-show-doc-buffer ()
622 (when company-candidates
623 (save-window-excursion
624 (let* ((height (window-height))
625 (row (cdr (posn-col-row (posn-at-point))))
626 (selected (nth company-selection company-candidates))
627 (buffer (funcall company-backend 'doc-buffer selected)))
629 (error "No documentation available.")
630 (display-buffer buffer)
631 (and (< (window-height) height)
632 (< (- (window-height) row 2) company-tooltip-limit)
633 (recenter (- (window-height) row 2)))
635 (when last-input-event
636 (clear-this-command-keys t)
637 (setq unread-command-events (list last-input-event))))))))
639 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
641 (defvar company-pseudo-tooltip-overlay nil)
642 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
644 (defvar company-tooltip-offset 0)
645 (make-variable-buffer-local 'company-tooltip-offset)
647 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
650 (setq company-tooltip-offset
651 (max (min selection company-tooltip-offset)
652 (- selection -1 limit)))
654 (when (<= company-tooltip-offset 1)
656 (setq company-tooltip-offset 0))
658 (when (>= company-tooltip-offset (- num-lines limit 1))
660 (when (= selection (1- num-lines))
661 (decf company-tooltip-offset)
662 (when (<= company-tooltip-offset 1)
663 (setq company-tooltip-offset 0)
670 (defun company-fill-propertize (line width selected)
671 (setq line (company-safe-substring line 0 width))
672 (add-text-properties 0 width (list 'face 'company-tooltip) line)
673 (add-text-properties 0 (length company-common)
674 (list 'face 'company-tooltip-common) line)
676 (if (and company-search-string
677 (string-match (regexp-quote company-search-string) line
678 (length company-prefix)))
680 (add-text-properties (match-beginning 0) (match-end 0)
681 '(face company-tooltip-selection) line)
682 (when (< (match-beginning 0) (length company-common))
683 (add-text-properties (match-beginning 0) (length company-common)
684 '(face company-tooltip-common-selection)
686 (add-text-properties 0 width '(face company-tooltip-selection) line)
687 (add-text-properties 0 (length company-common)
688 (list 'face 'company-tooltip-common-selection)
694 (defun company-buffer-lines (beg end)
696 (let ((row (cdr (posn-col-row (posn-at-point))))
698 (while (and (equal (move-to-window-line (incf row)) row)
700 (push (buffer-substring beg (min end (1- (point)))) lines)
703 (push (buffer-substring beg end) lines))
706 (defun company-modify-line (old new offset)
707 (concat (company-safe-substring old 0 offset)
709 (company-safe-substring old (+ offset (length new)))))
711 (defun company-replacement-string (old lines column nl)
713 ;; Inject into old lines.
715 (push (company-modify-line (pop old) (pop lines) column) new))
716 ;; Append whole new lines.
718 (push (company-modify-line "" (pop lines) column) new))
719 (concat (when nl "\n")
720 (mapconcat 'identity (nreverse new) "\n")
723 (defun company-create-lines (column lines selection limit)
725 (let ((len (length lines))
733 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
735 (when (> company-tooltip-offset 0)
736 (setq previous (format "...(%d)" company-tooltip-offset)))
738 (setq remainder (- len limit company-tooltip-offset)
739 remainder (when (> remainder 0)
740 (setq remainder (format "...(%d)" remainder))))
742 (decf selection company-tooltip-offset)
743 (setq width (min (length previous) (length remainder))
744 lines (nthcdr company-tooltip-offset lines)
745 len (min limit (length lines))
749 (setq width (max (length (pop lines-copy)) width)))
750 (setq width (min width (- (window-width) column)))
753 (push (propertize (company-safe-substring previous 0 width)
754 'face 'company-tooltip)
758 (push (company-fill-propertize (company-reformat (pop lines))
759 width (equal i selection))
763 (push (propertize (company-safe-substring remainder 0 width)
764 'face 'company-tooltip)
767 (setq lines (nreverse new))))
771 (defsubst company-pseudo-tooltip-height ()
772 "Calculate the appropriate tooltip height."
773 (max 3 (min company-tooltip-limit
774 (- (window-height) (cdr (posn-col-row (posn-at-point))) 2))))
776 (defun company-pseudo-tooltip-show (row column lines selection)
777 (company-pseudo-tooltip-hide)
778 (unless lines (error "No text provided"))
783 (let* ((height (company-pseudo-tooltip-height))
784 (lines (company-create-lines column lines selection height))
785 (nl (< (move-to-window-line row) row))
788 (move-to-window-line (+ row height))
790 (old-string (company-buffer-lines beg end))
793 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
795 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
796 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
797 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
798 (overlay-put company-pseudo-tooltip-overlay 'company-before
799 (company-replacement-string old-string lines column nl))
800 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
802 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
804 (defun company-pseudo-tooltip-show-at-point (pos)
805 (let ((col-row (posn-col-row (posn-at-point pos))))
806 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
807 company-candidates company-selection)))
809 (defun company-pseudo-tooltip-edit (lines selection)
810 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
811 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
812 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
813 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
814 (lines (company-create-lines column lines selection height)))
815 (overlay-put company-pseudo-tooltip-overlay 'company-before
816 (company-replacement-string old-string lines column nl))))
818 (defun company-pseudo-tooltip-hide ()
819 (when company-pseudo-tooltip-overlay
820 (delete-overlay company-pseudo-tooltip-overlay)
821 (setq company-pseudo-tooltip-overlay nil)))
823 (defun company-pseudo-tooltip-hide-temporarily ()
824 (when (overlayp company-pseudo-tooltip-overlay)
825 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
826 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
828 (defun company-pseudo-tooltip-unhide ()
829 (when company-pseudo-tooltip-overlay
830 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
831 (overlay-put company-pseudo-tooltip-overlay 'before-string
832 (overlay-get company-pseudo-tooltip-overlay 'company-before))))
834 (defun company-pseudo-tooltip-frontend (command)
836 ('pre-command (company-pseudo-tooltip-hide-temporarily))
838 (unless (and (overlayp company-pseudo-tooltip-overlay)
839 (equal (overlay-get company-pseudo-tooltip-overlay
841 (company-pseudo-tooltip-height)))
843 (company-pseudo-tooltip-show-at-point (- (point)
844 (length company-prefix))))
845 (company-pseudo-tooltip-unhide))
846 ('hide (company-pseudo-tooltip-hide)
847 (setq company-tooltip-offset 0))
848 ('update (when (overlayp company-pseudo-tooltip-overlay)
849 (company-pseudo-tooltip-edit company-candidates
850 company-selection)))))
852 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
853 (unless (and (eq command 'post-command)
854 (not (cdr company-candidates)))
855 (company-pseudo-tooltip-frontend command)))
857 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
859 (defvar company-preview-overlay nil)
860 (make-variable-buffer-local 'company-preview-overlay)
862 (defun company-preview-show-at-point (pos)
863 (company-preview-hide)
865 (setq company-preview-overlay (make-overlay pos pos))
867 (let ((completion (company-strip-prefix (nth company-selection
868 company-candidates))))
869 (and (equal pos (point))
870 (not (equal completion ""))
871 (add-text-properties 0 1 '(cursor t) completion))
873 (setq completion (propertize completion 'face 'company-preview))
874 (add-text-properties 0 (- (length company-common) (length company-prefix))
875 '(face company-preview-common) completion)
877 (overlay-put company-preview-overlay 'after-string completion)
878 (overlay-put company-preview-overlay 'window (selected-window))))
880 (defun company-preview-hide ()
881 (when company-preview-overlay
882 (delete-overlay company-preview-overlay)
883 (setq company-preview-overlay nil)))
885 (defun company-preview-frontend (command)
887 ('pre-command (company-preview-hide))
888 ('post-command (company-preview-show-at-point (point)))
889 ('hide (company-preview-hide))))
891 (defun company-preview-if-just-one-frontend (command)
892 (unless (and (eq command 'post-command)
893 (cdr company-candidates))
894 (company-preview-frontend command)))
896 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
898 (defvar company-echo-last-msg nil)
899 (make-variable-buffer-local 'company-echo-last-msg)
901 (defun company-echo-refresh ()
902 (let ((message-log-max nil))
903 (if company-echo-last-msg
904 (message "%s" company-echo-last-msg)
907 (defun company-echo-show (candidates)
909 ;; Roll to selection.
910 (setq candidates (nthcdr company-selection candidates))
912 (let ((limit (window-width (minibuffer-window)))
916 (setq comp (company-reformat (pop candidates))
917 len (+ len 1 (length comp)))
919 (setq candidates nil)
920 (setq comp (propertize comp 'face 'company-echo))
921 (add-text-properties 0 (length company-common)
922 '(face company-echo-common) comp)
925 (setq company-echo-last-msg (mapconcat 'identity (nreverse msg) " "))
926 (company-echo-refresh)))
928 (defun company-echo-frontend (command)
930 ('pre-command (company-echo-refresh))
931 ('post-command (company-echo-show company-candidates))
932 ('hide (setq company-echo-last-msg nil))))
934 (defun company-echo-metadata-frontend (command)
936 ('pre-command (company-echo-refresh))
937 ('post-command (setq company-echo-last-msg (company-fetch-metadata))
938 (company-echo-refresh))
939 ('hide (setq company-echo-last-msg nil))))
943 ;;; company.el ends here