]> code.delx.au - gnu-emacs-elpa/blob - company.el
Added candidate search.
[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 (define-key keymap "\C-s" 'company-search-candidates)
174 keymap))
175
176 ;;;###autoload
177 (define-minor-mode company-mode
178 ""
179 nil " comp" company-mode-map
180 (if company-mode
181 (progn
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
185 company-idle-delay))
186 (remove-hook 'pre-command-hook 'company-pre-command t)
187 (remove-hook 'post-command-hook 'company-post-command t)
188 (company-cancel)
189 (kill-local-variable 'company-point)))
190
191 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
192
193 (defvar company-overriding-keymap-bound nil)
194 (make-variable-buffer-local 'company-overriding-keymap-bound)
195
196 (defvar company-old-keymap nil)
197 (make-variable-buffer-local 'company-old-keymap)
198
199 (defvar company-my-keymap nil)
200 (make-variable-buffer-local 'company-my-keymap)
201
202 (defsubst company-enable-overriding-keymap (keymap)
203 (setq company-my-keymap keymap)
204 (when company-overriding-keymap-bound
205 (company-uninstall-map)))
206
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)))
213
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)))
219
220 ;; Hack:
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 ()
224 (interactive))
225
226 (global-set-key '[31415926] 'company-ignore)
227
228 (defun company-input-noop ()
229 (push 31415926 unread-command-events))
230
231 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
232
233 (defun company-grab (regexp &optional expression)
234 (when (looking-back regexp)
235 (or (match-string-no-properties (or expression 0)) "")))
236
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))))
240
241 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
242
243 (defvar company-backend nil)
244 (make-variable-buffer-local 'company-backend)
245
246 (defvar company-prefix nil)
247 (make-variable-buffer-local 'company-prefix)
248
249 (defvar company-candidates nil)
250 (make-variable-buffer-local 'company-candidates)
251
252 (defvar company-candidates-cache nil)
253 (make-variable-buffer-local 'company-candidates-cache)
254
255 (defvar company-common nil)
256 (make-variable-buffer-local 'company-common)
257
258 (defvar company-selection 0)
259 (make-variable-buffer-local 'company-selection)
260
261 (defvar company-selection-changed nil)
262 (make-variable-buffer-local 'company-selection-changed)
263
264 (defvar company-point nil)
265 (make-variable-buffer-local 'company-point)
266
267 (defvar company-disabled-backends nil)
268
269 (defsubst company-strip-prefix (str)
270 (substring str (length company-prefix)))
271
272 (defsubst company-reformat (candidate)
273 ;; company-ispell needs this, because the results are always lower-case
274 ;; It's mory efficient to fix it only when they are displayed.
275 (concat company-prefix (substring candidate (length company-prefix))))
276
277 (defsubst company-should-complete (prefix)
278 (and (eq company-idle-delay t)
279 (>= (length prefix) company-minimum-prefix-length)))
280
281 (defsubst company-call-frontends (command)
282 (dolist (frontend company-frontends)
283 (funcall frontend command)))
284
285 (defsubst company-set-selection (selection &optional force-update)
286 (setq selection (max 0 (min (1- (length company-candidates)) 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 (defsubst company-calculate-candidates (prefix)
293 (or (setq company-candidates (cdr (assoc prefix company-candidates-cache)))
294 (let ((len (length prefix))
295 (completion-ignore-case (funcall company-backend 'ignore-case))
296 prev)
297 (dotimes (i len)
298 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
299 company-candidates-cache)))
300 (setq company-candidates (all-completions prefix prev))
301 (return t))))
302 (progn
303 (setq company-candidates (funcall company-backend 'candidates prefix))
304 (unless (funcall company-backend 'sorted)
305 (setq company-candidates (sort company-candidates 'string<)))))
306 (unless (assoc prefix company-candidates-cache)
307 (push (cons prefix company-candidates) company-candidates-cache))
308 (setq company-selection 0
309 company-prefix prefix)
310 (let ((completion-ignore-case (funcall company-backend 'ignore-case)))
311 (setq company-common (try-completion company-prefix company-candidates)))
312 (when (eq company-common t)
313 (setq company-candidates nil))
314 company-candidates)
315
316 (defun company-idle-begin ()
317 (and company-mode
318 (not company-candidates)
319 (not (equal (point) company-point))
320 (let ((company-idle-delay t))
321 (company-begin)
322 (when company-candidates
323 (company-input-noop)
324 (company-post-command)))))
325
326 (defun company-manual-begin ()
327 (and company-mode
328 (not company-candidates)
329 (let ((company-idle-delay t)
330 (company-minimum-prefix-length 0))
331 (company-begin)))
332 ;; Return non-nil if active.
333 company-candidates)
334
335 (defun company-continue ()
336 (when company-candidates
337 (let ((new-prefix (funcall company-backend 'prefix)))
338 (unless (and (= (- (point) (length new-prefix))
339 (- company-point (length company-prefix)))
340 (or (equal company-prefix new-prefix)
341 (company-calculate-candidates new-prefix)))
342 (setq company-candidates nil)))))
343
344 (defun company-begin ()
345 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
346 ;; Don't complete in these cases.
347 (setq company-candidates nil)
348 (company-continue)
349 (unless company-candidates
350 (let (prefix)
351 (dolist (backend company-backends)
352 (unless (fboundp backend)
353 (ignore-errors (require backend nil t)))
354 (if (fboundp backend)
355 (when (setq prefix (funcall backend 'prefix))
356 (when (company-should-complete prefix)
357 (setq company-backend backend)
358 (company-calculate-candidates prefix))
359 (return prefix))
360 (unless (memq backend company-disabled-backends)
361 (push backend company-disabled-backends)
362 (message "Company back-end '%s' could not be initialized"
363 backend)))))))
364 (if company-candidates
365 (progn
366 (setq company-point (point))
367 (company-enable-overriding-keymap company-active-map)
368 (company-call-frontends 'update))
369 (company-cancel)))
370
371 (defun company-cancel ()
372 (setq company-backend nil
373 company-prefix nil
374 company-candidates nil
375 company-candidates-cache nil
376 company-common nil
377 company-selection 0
378 company-selection-changed nil
379 company-point nil)
380 (company-search-mode 0)
381 (company-call-frontends 'hide)
382 (company-enable-overriding-keymap nil))
383
384 (defun company-abort ()
385 (company-cancel)
386 ;; Don't start again, unless started manually.
387 (setq company-point (point)))
388
389 (defun company-pre-command ()
390 (unless (eq this-command 'company-show-doc-buffer)
391 (condition-case err
392 (when company-candidates
393 (company-call-frontends 'pre-command))
394 (error (message "Company: An error occurred in pre-command")
395 (message "%s" (error-message-string err))
396 (company-cancel))))
397 (company-uninstall-map))
398
399 (defun company-post-command ()
400 (unless (eq this-command 'company-show-doc-buffer)
401 (condition-case err
402 (progn
403 (unless (equal (point) company-point)
404 (company-begin))
405 (when company-candidates
406 (company-call-frontends 'post-command)))
407 (error (message "Company: An error occurred in post-command")
408 (message "%s" (error-message-string err))
409 (company-cancel))))
410 (company-install-map))
411
412 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
413
414 (defvar company-search-string nil)
415 (make-variable-buffer-local 'company-search-string)
416
417 (defvar company-search-lighter " Search: \"\"")
418 (make-variable-buffer-local 'company-search-lighter)
419
420 (defvar company-search-old-map nil)
421 (make-variable-buffer-local 'company-search-old-map)
422
423 (defvar company-search-old-selection 0)
424 (make-variable-buffer-local 'company-search-old-selection)
425
426 (defun company-search (text lines)
427 (let ((quoted (regexp-quote text))
428 (i 0))
429 (dolist (line lines)
430 (when (string-match quoted line (length company-prefix))
431 (return i))
432 (incf i))))
433
434 (defun company-search-printing-char ()
435 (interactive)
436 (setq company-search-string
437 (concat (or company-search-string "") (string last-command-event))
438 company-search-lighter (concat " Search: \"" company-search-string
439 "\""))
440 (let ((pos (company-search company-search-string
441 (nthcdr company-selection company-candidates))))
442 (if (null pos)
443 (ding)
444 (company-set-selection (+ company-selection pos) t))))
445
446 (defun company-search-repeat-forward ()
447 (interactive)
448 (let ((pos (company-search company-search-string
449 (cdr (nthcdr company-selection
450 company-candidates)))))
451 (if (null pos)
452 (ding)
453 (company-set-selection (+ company-selection pos 1) t))))
454
455 (defun company-search-repeat-backward ()
456 (interactive)
457 (let ((pos (company-search company-search-string
458 (nthcdr (- (length company-candidates)
459 company-selection)
460 (reverse company-candidates)))))
461 (if (null pos)
462 (ding)
463 (company-set-selection (- company-selection pos 1) t))))
464
465 (defun company-search-abort ()
466 (interactive)
467 (company-set-selection company-search-old-selection t)
468 (company-search-mode 0))
469
470 (defun company-search-other-char ()
471 (interactive)
472 (company-search-mode 0)
473 (when last-input-event
474 (clear-this-command-keys t)
475 (setq unread-command-events (list last-input-event))))
476
477 (defvar company-search-map
478 (let ((i 0)
479 (keymap (make-keymap)))
480 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
481 'company-search-printing-char)
482 (define-key keymap [t] 'company-search-other-char)
483 (while (< i ?\s)
484 (define-key keymap (make-string 1 i) 'company-search-other-char)
485 (incf i))
486 (while (< i 256)
487 (define-key keymap (vector i) 'company-search-printing-char)
488 (incf i))
489 (let ((meta-map (make-sparse-keymap)))
490 (define-key keymap (char-to-string meta-prefix-char) meta-map)
491 (define-key keymap [escape] meta-map))
492 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
493 (define-key keymap "\e\e\e" 'company-search-other-char)
494 (define-key keymap [escape escape escape] 'company-search-other-char)
495
496 (define-key keymap "\C-g" 'company-search-abort)
497 (define-key keymap "\C-s" 'company-search-repeat-forward)
498 (define-key keymap "\C-r" 'company-search-repeat-backward)
499 keymap))
500
501 (define-minor-mode company-search-mode
502 ""
503 nil company-search-lighter nil
504 (if company-search-mode
505 (if (company-manual-begin)
506 (progn
507 (setq company-search-old-selection company-selection)
508 (company-enable-overriding-keymap company-search-map)
509 (company-call-frontends 'update))
510 (setq company-search-mode nil))
511 (kill-local-variable 'company-search-string)
512 (kill-local-variable 'company-search-lighter)
513 (kill-local-variable 'company-search-old-selection)
514 (company-enable-overriding-keymap company-active-map)))
515
516 (defun company-search-candidates ()
517 (interactive)
518 (company-search-mode 1))
519
520 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
521
522 (defun company-select-next ()
523 (interactive)
524 (when (company-manual-begin)
525 (company-set-selection (1+ company-selection))))
526
527 (defun company-select-previous ()
528 (interactive)
529 (when (company-manual-begin)
530 (company-set-selection (1- company-selection))))
531
532 (defun company-complete-selection ()
533 (interactive)
534 (when (company-manual-begin)
535 (insert (company-strip-prefix (nth company-selection company-candidates)))
536 (company-abort)))
537
538 (defun company-complete-common ()
539 (interactive)
540 (when (company-manual-begin)
541 (insert (company-strip-prefix company-common))))
542
543 (defun company-complete ()
544 (interactive)
545 (when (company-manual-begin)
546 (if (or company-selection-changed
547 (eq last-command 'company-complete-common))
548 (call-interactively 'company-complete-selection)
549 (call-interactively 'company-complete-common)
550 (setq this-command 'company-complete-common))))
551
552 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
553
554 (defconst company-space-strings-limit 100)
555
556 (defconst company-space-strings
557 (let (lst)
558 (dotimes (i company-space-strings-limit)
559 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
560 (apply 'vector lst)))
561
562 (defsubst company-space-string (len)
563 (if (< len company-space-strings-limit)
564 (aref company-space-strings len)
565 (make-string len ?\ )))
566
567 (defsubst company-safe-substring (str from &optional to)
568 (let ((len (length str)))
569 (if (> from len)
570 ""
571 (if (and to (> to len))
572 (concat (substring str from)
573 (company-space-string (- to len)))
574 (substring str from to)))))
575
576 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
577
578 (defvar company-last-metadata nil)
579 (make-variable-buffer-local 'company-last-metadata)
580
581 (defun company-fetch-metadata ()
582 (let ((selected (nth company-selection company-candidates)))
583 (unless (equal selected (car company-last-metadata))
584 (setq company-last-metadata
585 (cons selected (funcall company-backend 'meta selected))))
586 (cdr company-last-metadata)))
587
588 (defun company-doc-buffer (&optional string)
589 (with-current-buffer (get-buffer-create "*Company meta-data*")
590 (erase-buffer)
591 (current-buffer)))
592
593 (defun company-show-doc-buffer ()
594 (interactive)
595 (when company-candidates
596 (save-window-excursion
597 (let* ((height (window-height))
598 (row (cdr (posn-col-row (posn-at-point))))
599 (selected (nth company-selection company-candidates))
600 (buffer (funcall company-backend 'doc-buffer selected)))
601 (if (not buffer)
602 (error "No documentation available.")
603 (display-buffer buffer)
604 (and (< (window-height) height)
605 (< (- (window-height) row 2) company-tooltip-limit)
606 (recenter (- (window-height) row 2)))
607 (read-event)
608 (when last-input-event
609 (clear-this-command-keys t)
610 (setq unread-command-events (list last-input-event))))))))
611
612 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
613
614 (defvar company-pseudo-tooltip-overlay nil)
615 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
616
617 (defvar company-tooltip-offset 0)
618 (make-variable-buffer-local 'company-tooltip-offset)
619
620 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
621
622 (decf limit 2)
623 (setq company-tooltip-offset
624 (max (min selection company-tooltip-offset)
625 (- selection -1 limit)))
626
627 (when (<= company-tooltip-offset 1)
628 (incf limit)
629 (setq company-tooltip-offset 0))
630
631 (when (>= company-tooltip-offset (- num-lines limit 1))
632 (incf limit)
633 (when (= selection (1- num-lines))
634 (decf company-tooltip-offset)
635 (when (<= company-tooltip-offset 1)
636 (setq company-tooltip-offset 0)
637 (incf limit))))
638
639 limit)
640
641 ;;; propertize
642
643 (defun company-fill-propertize (line width selected)
644 (setq line (company-safe-substring line 0 width))
645 (add-text-properties 0 width (list 'face 'company-tooltip) line)
646 (add-text-properties 0 (length company-common)
647 (list 'face 'company-tooltip-common) line)
648 (when selected
649 (if (and company-search-string
650 (string-match (regexp-quote company-search-string) line
651 (length company-prefix)))
652 (progn
653 (add-text-properties (match-beginning 0) (match-end 0)
654 '(face company-tooltip-selection) line)
655 (when (< (match-beginning 0) (length company-common))
656 (add-text-properties (match-beginning 0) (length company-common)
657 '(face company-tooltip-common-selection)
658 line)))
659 (add-text-properties 0 width '(face company-tooltip-selection) line)
660 (add-text-properties 0 (length company-common)
661 (list 'face 'company-tooltip-common-selection)
662 line)))
663 line)
664
665 ;;; replace
666
667 (defun company-buffer-lines (beg end)
668 (goto-char beg)
669 (let ((row (cdr (posn-col-row (posn-at-point))))
670 lines)
671 (while (and (equal (move-to-window-line (incf row)) row)
672 (<= (point) end))
673 (push (buffer-substring beg (min end (1- (point)))) lines)
674 (setq beg (point)))
675 (unless (eq beg end)
676 (push (buffer-substring beg end) lines))
677 (nreverse lines)))
678
679 (defun company-modify-line (old new offset)
680 (concat (company-safe-substring old 0 offset)
681 new
682 (company-safe-substring old (+ offset (length new)))))
683
684 (defun company-replacement-string (old lines column nl)
685 (let (new)
686 ;; Inject into old lines.
687 (while old
688 (push (company-modify-line (pop old) (pop lines) column) new))
689 ;; Append whole new lines.
690 (while lines
691 (push (company-modify-line "" (pop lines) column) new))
692 (concat (when nl "\n")
693 (mapconcat 'identity (nreverse new) "\n")
694 "\n")))
695
696 (defun company-create-lines (column lines selection limit)
697
698 (let ((len (length lines))
699 width
700 lines-copy
701 previous
702 remainder
703 new)
704
705 ;; Scroll to offset.
706 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
707
708 (when (> company-tooltip-offset 0)
709 (setq previous (format "...(%d)" company-tooltip-offset)))
710
711 (setq remainder (- len limit company-tooltip-offset)
712 remainder (when (> remainder 0)
713 (setq remainder (format "...(%d)" remainder))))
714
715 (decf selection company-tooltip-offset)
716 (setq width (min (length previous) (length remainder))
717 lines (nthcdr company-tooltip-offset lines)
718 len (min limit (length lines))
719 lines-copy lines)
720
721 (dotimes (i len)
722 (setq width (max (length (pop lines-copy)) width)))
723 (setq width (min width (- (window-width) column)))
724
725 (when previous
726 (push (propertize (company-safe-substring previous 0 width)
727 'face 'company-tooltip)
728 new))
729
730 (dotimes (i len)
731 (push (company-fill-propertize (company-reformat (pop lines))
732 width (equal i selection))
733 new))
734
735 (when remainder
736 (push (propertize (company-safe-substring remainder 0 width)
737 'face 'company-tooltip)
738 new))
739
740 (setq lines (nreverse new))))
741
742 ;; show
743
744 (defsubst company-pseudo-tooltip-height ()
745 "Calculate the appropriate tooltip height."
746 (max 3 (min company-tooltip-limit
747 (- (window-height) (cdr (posn-col-row (posn-at-point))) 2))))
748
749 (defun company-pseudo-tooltip-show (row column lines selection)
750 (company-pseudo-tooltip-hide)
751 (unless lines (error "No text provided"))
752 (save-excursion
753
754 (move-to-column 0)
755
756 (let* ((height (company-pseudo-tooltip-height))
757 (lines (company-create-lines column lines selection height))
758 (nl (< (move-to-window-line row) row))
759 (beg (point))
760 (end (save-excursion
761 (move-to-window-line (+ row height))
762 (point)))
763 (old-string (company-buffer-lines beg end))
764 str)
765
766 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
767
768 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
769 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
770 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
771 (overlay-put company-pseudo-tooltip-overlay 'company-before
772 (company-replacement-string old-string lines column nl))
773 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
774
775 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
776
777 (defun company-pseudo-tooltip-show-at-point (pos)
778 (let ((col-row (posn-col-row (posn-at-point pos))))
779 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
780 company-candidates company-selection)))
781
782 (defun company-pseudo-tooltip-edit (lines selection)
783 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
784 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
785 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
786 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
787 (lines (company-create-lines column lines selection height)))
788 (overlay-put company-pseudo-tooltip-overlay 'company-before
789 (company-replacement-string old-string lines column nl))))
790
791 (defun company-pseudo-tooltip-hide ()
792 (when company-pseudo-tooltip-overlay
793 (delete-overlay company-pseudo-tooltip-overlay)
794 (setq company-pseudo-tooltip-overlay nil)))
795
796 (defun company-pseudo-tooltip-hide-temporarily ()
797 (when (overlayp company-pseudo-tooltip-overlay)
798 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
799 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
800
801 (defun company-pseudo-tooltip-unhide ()
802 (when company-pseudo-tooltip-overlay
803 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
804 (overlay-put company-pseudo-tooltip-overlay 'before-string
805 (overlay-get company-pseudo-tooltip-overlay 'company-before))))
806
807 (defun company-pseudo-tooltip-frontend (command)
808 (case command
809 ('pre-command (company-pseudo-tooltip-hide-temporarily))
810 ('post-command
811 (unless (and (overlayp company-pseudo-tooltip-overlay)
812 (equal (overlay-get company-pseudo-tooltip-overlay
813 'company-height)
814 (company-pseudo-tooltip-height)))
815 ;; Redraw needed.
816 (company-pseudo-tooltip-show-at-point (- (point)
817 (length company-prefix))))
818 (company-pseudo-tooltip-unhide))
819 ('hide (company-pseudo-tooltip-hide)
820 (setq company-tooltip-offset 0))
821 ('update (when (overlayp company-pseudo-tooltip-overlay)
822 (company-pseudo-tooltip-edit company-candidates
823 company-selection)))))
824
825 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
826 (unless (and (eq command 'post-command)
827 (not (cdr company-candidates)))
828 (company-pseudo-tooltip-frontend command)))
829
830 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
831
832 (defvar company-preview-overlay nil)
833 (make-variable-buffer-local 'company-preview-overlay)
834
835 (defun company-preview-show-at-point (pos)
836 (company-preview-hide)
837
838 (setq company-preview-overlay (make-overlay pos pos))
839
840 (let ((completion (company-strip-prefix (nth company-selection
841 company-candidates))))
842 (and (equal pos (point))
843 (not (equal completion ""))
844 (add-text-properties 0 1 '(cursor t) completion))
845
846 (setq completion (propertize completion 'face 'company-preview))
847 (add-text-properties 0 (- (length company-common) (length company-prefix))
848 '(face company-preview-common) completion)
849
850 (overlay-put company-preview-overlay 'after-string completion)
851 (overlay-put company-preview-overlay 'window (selected-window))))
852
853 (defun company-preview-hide ()
854 (when company-preview-overlay
855 (delete-overlay company-preview-overlay)
856 (setq company-preview-overlay nil)))
857
858 (defun company-preview-frontend (command)
859 (case command
860 ('pre-command (company-preview-hide))
861 ('post-command (company-preview-show-at-point (point)))
862 ('hide (company-preview-hide))))
863
864 (defun company-preview-if-just-one-frontend (command)
865 (unless (and (eq command 'post-command)
866 (cdr company-candidates))
867 (company-preview-frontend command)))
868
869 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
870
871 (defvar company-echo-last-msg nil)
872 (make-variable-buffer-local 'company-echo-last-msg)
873
874 (defun company-echo-refresh ()
875 (let ((message-log-max nil))
876 (if company-echo-last-msg
877 (message "%s" company-echo-last-msg)
878 (message ""))))
879
880 (defun company-echo-show (candidates)
881
882 ;; Roll to selection.
883 (setq candidates (nthcdr company-selection candidates))
884
885 (let ((limit (window-width (minibuffer-window)))
886 (len -1)
887 comp msg)
888 (while candidates
889 (setq comp (company-reformat (pop candidates))
890 len (+ len 1 (length comp)))
891 (if (>= len limit)
892 (setq candidates nil)
893 (setq comp (propertize comp 'face 'company-echo))
894 (add-text-properties 0 (length company-common)
895 '(face company-echo-common) comp)
896 (push comp msg)))
897
898 (setq company-echo-last-msg (mapconcat 'identity (nreverse msg) " "))
899 (company-echo-refresh)))
900
901 (defun company-echo-frontend (command)
902 (case command
903 ('pre-command (company-echo-refresh))
904 ('post-command (company-echo-show company-candidates))
905 ('hide (setq company-echo-last-msg nil))))
906
907 (defun company-echo-metadata-frontend (command)
908 (case command
909 ('pre-command (company-echo-refresh))
910 ('post-command (setq company-echo-last-msg (company-fetch-metadata))
911 (company-echo-refresh))
912 ('hide (setq company-echo-last-msg nil))))
913
914
915 (provide 'company)
916 ;;; company.el ends here