]> code.delx.au - gnu-emacs-elpa/blob - company.el
Added support for chained predicates.
[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-gtags 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 (when company-candidates-cache
326 (let ((len (length prefix))
327 (completion-ignore-case (funcall company-backend 'ignore-case))
328 prev)
329 (dotimes (i len)
330 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
331 company-candidates-cache)))
332 (return (all-completions prefix prev))))))
333 (let ((candidates (funcall company-backend 'candidates prefix)))
334 (when company-candidates-predicate
335 (setq candidates
336 (company-apply-predicate candidates
337 company-candidates-predicate)))
338 (unless (funcall company-backend 'sorted)
339 (setq candidates (sort candidates 'string<)))
340 candidates)))
341 (unless (assoc prefix company-candidates-cache)
342 (push (cons prefix company-candidates) company-candidates-cache))
343 company-candidates)
344
345 (defun company-idle-begin (buf win tick pos)
346 (and company-mode
347 (eq buf (current-buffer))
348 (eq win (selected-window))
349 (eq tick (buffer-chars-modified-tick))
350 (eq pos (point))
351 (not company-candidates)
352 (not (equal (point) company-point))
353 (let ((company-idle-delay t))
354 (company-begin)
355 (when company-candidates
356 (company-input-noop)
357 (company-post-command)))))
358
359 (defun company-manual-begin ()
360 (and company-mode
361 (not company-candidates)
362 (let ((company-idle-delay t)
363 (company-minimum-prefix-length 0))
364 (company-begin)))
365 ;; Return non-nil if active.
366 company-candidates)
367
368 (defun company-continue ()
369 (when company-candidates
370 (when (funcall company-backend 'no-cache company-prefix)
371 ;; Don't complete existing candidates, fetch new ones.
372 (setq company-candidates-cache nil))
373 (let ((new-prefix (funcall company-backend 'prefix)))
374 (unless (and (= (- (point) (length new-prefix))
375 (- company-point (length company-prefix)))
376 (or (equal company-prefix new-prefix)
377 (company-calculate-candidates new-prefix)))
378 (setq company-candidates nil)))))
379
380 (defun company-begin ()
381 (if (or buffer-read-only overriding-terminal-local-map overriding-local-map)
382 ;; Don't complete in these cases.
383 (setq company-candidates nil)
384 (company-continue)
385 (unless company-candidates
386 (let (prefix)
387 (dolist (backend company-backends)
388 (unless (fboundp backend)
389 (ignore-errors (require backend nil t)))
390 (if (fboundp backend)
391 (when (setq prefix (funcall backend 'prefix))
392 (when (company-should-complete prefix)
393 (setq company-backend backend)
394 (company-calculate-candidates prefix))
395 (return prefix))
396 (unless (memq backend company-disabled-backends)
397 (push backend company-disabled-backends)
398 (message "Company back-end '%s' could not be initialized"
399 backend)))))))
400 (if company-candidates
401 (progn
402 (setq company-point (point))
403 (company-enable-overriding-keymap company-active-map)
404 (company-call-frontends 'update))
405 (company-cancel)))
406
407 (defun company-cancel ()
408 (setq company-backend nil
409 company-prefix nil
410 company-candidates nil
411 company-candidates-length nil
412 company-candidates-cache nil
413 company-candidates-predicate nil
414 company-common nil
415 company-selection 0
416 company-selection-changed nil
417 company-point nil)
418 (when company-timer
419 (cancel-timer company-timer))
420 (company-search-mode 0)
421 (company-call-frontends 'hide)
422 (company-enable-overriding-keymap nil))
423
424 (defun company-abort ()
425 (company-cancel)
426 ;; Don't start again, unless started manually.
427 (setq company-point (point)))
428
429 (defun company-pre-command ()
430 (unless (eq this-command 'company-show-doc-buffer)
431 (condition-case err
432 (when company-candidates
433 (company-call-frontends 'pre-command))
434 (error (message "Company: An error occurred in pre-command")
435 (message "%s" (error-message-string err))
436 (company-cancel))))
437 (when company-timer
438 (cancel-timer company-timer))
439 (company-uninstall-map))
440
441 (defun company-post-command ()
442 (unless (eq this-command 'company-show-doc-buffer)
443 (condition-case err
444 (progn
445 (unless (equal (point) company-point)
446 (company-begin))
447 (when company-candidates
448 (company-call-frontends 'post-command))
449 (when (numberp company-idle-delay)
450 (setq company-timer
451 (run-with-timer company-idle-delay nil 'company-idle-begin
452 (current-buffer) (selected-window)
453 (buffer-chars-modified-tick) (point)))))
454 (error (message "Company: An error occurred in post-command")
455 (message "%s" (error-message-string err))
456 (company-cancel))))
457 (company-install-map))
458
459 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
460
461 (defvar company-search-string nil)
462 (make-variable-buffer-local 'company-search-string)
463
464 (defvar company-search-lighter " Search: \"\"")
465 (make-variable-buffer-local 'company-search-lighter)
466
467 (defvar company-search-old-map nil)
468 (make-variable-buffer-local 'company-search-old-map)
469
470 (defvar company-search-old-selection 0)
471 (make-variable-buffer-local 'company-search-old-selection)
472
473 (defun company-search (text lines)
474 (let ((quoted (regexp-quote text))
475 (i 0))
476 (dolist (line lines)
477 (when (string-match quoted line (length company-prefix))
478 (return i))
479 (incf i))))
480
481 (defun company-search-printing-char ()
482 (interactive)
483 (setq company-search-string
484 (concat (or company-search-string "") (string last-command-event))
485 company-search-lighter (concat " Search: \"" company-search-string
486 "\""))
487 (let ((pos (company-search company-search-string
488 (nthcdr company-selection company-candidates))))
489 (if (null pos)
490 (ding)
491 (company-set-selection (+ company-selection pos) t))))
492
493 (defun company-search-repeat-forward ()
494 (interactive)
495 (let ((pos (company-search company-search-string
496 (cdr (nthcdr company-selection
497 company-candidates)))))
498 (if (null pos)
499 (ding)
500 (company-set-selection (+ company-selection pos 1) t))))
501
502 (defun company-search-repeat-backward ()
503 (interactive)
504 (let ((pos (company-search company-search-string
505 (nthcdr (- company-candidates-length
506 company-selection)
507 (reverse company-candidates)))))
508 (if (null pos)
509 (ding)
510 (company-set-selection (- company-selection pos 1) t))))
511
512 (defsubst company-create-match-predicate (search-string)
513 `(lambda (candidate)
514 ,(if company-candidates-predicate
515 `(and (string-match ,search-string candidate)
516 (funcall ,company-candidates-predicate candidate))
517 `(string-match ,company-search-string candidate))))
518
519 (defun company-search-kill-others ()
520 (interactive)
521 (let ((predicate (company-create-match-predicate company-search-string)))
522 (setq company-candidates-predicate predicate)
523 (company-update-candidates (company-apply-predicate company-candidates
524 predicate))
525 (company-search-mode 0)
526 (company-call-frontends 'update)))
527
528 (defun company-search-abort ()
529 (interactive)
530 (company-set-selection company-search-old-selection t)
531 (company-search-mode 0))
532
533 (defun company-search-other-char ()
534 (interactive)
535 (company-search-mode 0)
536 (when last-input-event
537 (clear-this-command-keys t)
538 (setq unread-command-events (list last-input-event))))
539
540 (defvar company-search-map
541 (let ((i 0)
542 (keymap (make-keymap)))
543 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
544 'company-search-printing-char)
545 (define-key keymap [t] 'company-search-other-char)
546 (while (< i ?\s)
547 (define-key keymap (make-string 1 i) 'company-search-other-char)
548 (incf i))
549 (while (< i 256)
550 (define-key keymap (vector i) 'company-search-printing-char)
551 (incf i))
552 (let ((meta-map (make-sparse-keymap)))
553 (define-key keymap (char-to-string meta-prefix-char) meta-map)
554 (define-key keymap [escape] meta-map))
555 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
556 (define-key keymap "\e\e\e" 'company-search-other-char)
557 (define-key keymap [escape escape escape] 'company-search-other-char)
558
559 (define-key keymap "\C-g" 'company-search-abort)
560 (define-key keymap "\C-s" 'company-search-repeat-forward)
561 (define-key keymap "\C-r" 'company-search-repeat-backward)
562 (define-key keymap "\C-o" 'company-search-kill-others)
563 keymap))
564
565 (define-minor-mode company-search-mode
566 ""
567 nil company-search-lighter nil
568 (if company-search-mode
569 (if (company-manual-begin)
570 (progn
571 (setq company-search-old-selection company-selection)
572 (company-enable-overriding-keymap company-search-map)
573 (company-call-frontends 'update))
574 (setq company-search-mode nil))
575 (kill-local-variable 'company-search-string)
576 (kill-local-variable 'company-search-lighter)
577 (kill-local-variable 'company-search-old-selection)
578 (company-enable-overriding-keymap company-active-map)))
579
580 (defun company-search-candidates ()
581 (interactive)
582 (company-search-mode 1))
583
584 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
585
586 (defun company-select-next ()
587 (interactive)
588 (when (company-manual-begin)
589 (company-set-selection (1+ company-selection))))
590
591 (defun company-select-previous ()
592 (interactive)
593 (when (company-manual-begin)
594 (company-set-selection (1- company-selection))))
595
596 (defun company-complete-selection ()
597 (interactive)
598 (when (company-manual-begin)
599 (insert (company-strip-prefix (nth company-selection company-candidates)))
600 (company-abort)))
601
602 (defun company-complete-common ()
603 (interactive)
604 (when (company-manual-begin)
605 (insert (company-strip-prefix company-common))))
606
607 (defun company-complete ()
608 (interactive)
609 (when (company-manual-begin)
610 (if (or company-selection-changed
611 (eq last-command 'company-complete-common))
612 (call-interactively 'company-complete-selection)
613 (call-interactively 'company-complete-common)
614 (setq this-command 'company-complete-common))))
615
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
617
618 (defconst company-space-strings-limit 100)
619
620 (defconst company-space-strings
621 (let (lst)
622 (dotimes (i company-space-strings-limit)
623 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
624 (apply 'vector lst)))
625
626 (defsubst company-space-string (len)
627 (if (< len company-space-strings-limit)
628 (aref company-space-strings len)
629 (make-string len ?\ )))
630
631 (defsubst company-safe-substring (str from &optional to)
632 (let ((len (length str)))
633 (if (> from len)
634 ""
635 (if (and to (> to len))
636 (concat (substring str from)
637 (company-space-string (- to len)))
638 (substring str from to)))))
639
640 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
641
642 (defvar company-last-metadata nil)
643 (make-variable-buffer-local 'company-last-metadata)
644
645 (defun company-fetch-metadata ()
646 (let ((selected (nth company-selection company-candidates)))
647 (unless (equal selected (car company-last-metadata))
648 (setq company-last-metadata
649 (cons selected (funcall company-backend 'meta selected))))
650 (cdr company-last-metadata)))
651
652 (defun company-doc-buffer (&optional string)
653 (with-current-buffer (get-buffer-create "*Company meta-data*")
654 (erase-buffer)
655 (current-buffer)))
656
657 (defun company-show-doc-buffer ()
658 (interactive)
659 (when company-candidates
660 (save-window-excursion
661 (let* ((height (window-height))
662 (row (cdr (posn-col-row (posn-at-point))))
663 (selected (nth company-selection company-candidates))
664 (buffer (funcall company-backend 'doc-buffer selected)))
665 (if (not buffer)
666 (error "No documentation available.")
667 (display-buffer buffer)
668 (and (< (window-height) height)
669 (< (- (window-height) row 2) company-tooltip-limit)
670 (recenter (- (window-height) row 2)))
671 (while (eq 'scroll-other-window
672 (key-binding (vector (list (read-event)))))
673 (scroll-other-window))
674 (when last-input-event
675 (clear-this-command-keys t)
676 (setq unread-command-events (list last-input-event))))))))
677
678 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
679
680 (defvar company-pseudo-tooltip-overlay nil)
681 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
682
683 (defvar company-tooltip-offset 0)
684 (make-variable-buffer-local 'company-tooltip-offset)
685
686 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
687
688 (decf limit 2)
689 (setq company-tooltip-offset
690 (max (min selection company-tooltip-offset)
691 (- selection -1 limit)))
692
693 (when (<= company-tooltip-offset 1)
694 (incf limit)
695 (setq company-tooltip-offset 0))
696
697 (when (>= company-tooltip-offset (- num-lines limit 1))
698 (incf limit)
699 (when (= selection (1- num-lines))
700 (decf company-tooltip-offset)
701 (when (<= company-tooltip-offset 1)
702 (setq company-tooltip-offset 0)
703 (incf limit))))
704
705 limit)
706
707 ;;; propertize
708
709 (defsubst company-round-tab (arg)
710 (* (/ (+ arg tab-width) tab-width) tab-width))
711
712 (defun company-untabify (str)
713 (let* ((pieces (split-string str "\t"))
714 (copy pieces))
715 (while (cdr copy)
716 (setcar copy (company-safe-substring
717 (car copy) 0 (company-round-tab (string-width (car copy)))))
718 (pop copy))
719 (apply 'concat pieces)))
720
721 (defun company-fill-propertize (line width selected)
722 (setq line (company-safe-substring line 0 width))
723 (add-text-properties 0 width (list 'face 'company-tooltip) line)
724 (add-text-properties 0 (length company-common)
725 (list 'face 'company-tooltip-common) line)
726 (when selected
727 (if (and company-search-string
728 (string-match (regexp-quote company-search-string) line
729 (length company-prefix)))
730 (progn
731 (add-text-properties (match-beginning 0) (match-end 0)
732 '(face company-tooltip-selection) line)
733 (when (< (match-beginning 0) (length company-common))
734 (add-text-properties (match-beginning 0) (length company-common)
735 '(face company-tooltip-common-selection)
736 line)))
737 (add-text-properties 0 width '(face company-tooltip-selection) line)
738 (add-text-properties 0 (length company-common)
739 (list 'face 'company-tooltip-common-selection)
740 line)))
741 line)
742
743 ;;; replace
744
745 (defun company-buffer-lines (beg end)
746 (goto-char beg)
747 (let ((row (cdr (posn-col-row (posn-at-point))))
748 lines)
749 (while (and (equal (move-to-window-line (incf row)) row)
750 (<= (point) end))
751 (push (buffer-substring beg (min end (1- (point)))) lines)
752 (setq beg (point)))
753 (unless (eq beg end)
754 (push (buffer-substring beg end) lines))
755 (nreverse lines)))
756
757 (defsubst company-modify-line (old new offset)
758 (concat (company-safe-substring old 0 offset)
759 new
760 (company-safe-substring old (+ offset (length new)))))
761
762 (defun company-replacement-string (old lines column nl)
763 (let (new)
764 ;; Inject into old lines.
765 (while old
766 (push (company-modify-line (pop old) (pop lines) column) new))
767 ;; Append whole new lines.
768 (while lines
769 (push (concat (company-space-string column) (pop lines)) new))
770 (concat (when nl "\n")
771 (mapconcat 'identity (nreverse new) "\n")
772 "\n")))
773
774 (defun company-create-lines (column selection limit)
775
776 (let ((len company-candidates-length)
777 lines
778 width
779 lines-copy
780 previous
781 remainder
782 new)
783
784 ;; Scroll to offset.
785 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
786
787 (when (> company-tooltip-offset 0)
788 (setq previous (format "...(%d)" company-tooltip-offset)))
789
790 (setq remainder (- len limit company-tooltip-offset)
791 remainder (when (> remainder 0)
792 (setq remainder (format "...(%d)" remainder))))
793
794 (decf selection company-tooltip-offset)
795 (setq width (min (length previous) (length remainder))
796 lines (nthcdr company-tooltip-offset company-candidates)
797 len (min limit len)
798 lines-copy lines)
799
800 (dotimes (i len)
801 (setq width (max (length (pop lines-copy)) width)))
802 (setq width (min width (- (window-width) column)))
803
804 (when previous
805 (push (propertize (company-safe-substring previous 0 width)
806 'face 'company-tooltip)
807 new))
808
809 (dotimes (i len)
810 (push (company-fill-propertize (company-reformat (pop lines))
811 width (equal i selection))
812 new))
813
814 (when remainder
815 (push (propertize (company-safe-substring remainder 0 width)
816 'face 'company-tooltip)
817 new))
818
819 (setq lines (nreverse new))))
820
821 ;; show
822
823 (defsubst company-pseudo-tooltip-height ()
824 "Calculate the appropriate tooltip height."
825 (max 3 (min company-tooltip-limit
826 (- (window-height) 2
827 (count-lines (window-start) (point-at-bol))))))
828
829 (defun company-pseudo-tooltip-show (row column selection)
830 (company-pseudo-tooltip-hide)
831 (save-excursion
832
833 (move-to-column 0)
834
835 (let* ((height (company-pseudo-tooltip-height))
836 (lines (company-create-lines column selection height))
837 (nl (< (move-to-window-line row) row))
838 (beg (point))
839 (end (save-excursion
840 (move-to-window-line (+ row height))
841 (point)))
842 (old-string
843 (mapcar 'company-untabify (company-buffer-lines beg end)))
844 str)
845
846 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
847
848 (overlay-put company-pseudo-tooltip-overlay 'company-old old-string)
849 (overlay-put company-pseudo-tooltip-overlay 'company-column column)
850 (overlay-put company-pseudo-tooltip-overlay 'company-nl nl)
851 (overlay-put company-pseudo-tooltip-overlay 'company-before
852 (company-replacement-string old-string lines column nl))
853 (overlay-put company-pseudo-tooltip-overlay 'company-height height)
854
855 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
856
857 (defun company-pseudo-tooltip-show-at-point (pos)
858 (let ((col-row (posn-col-row (posn-at-point pos))))
859 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row) company-selection)))
860
861 (defun company-pseudo-tooltip-edit (lines selection)
862 (let* ((old-string (overlay-get company-pseudo-tooltip-overlay 'company-old))
863 (column (overlay-get company-pseudo-tooltip-overlay 'company-column))
864 (nl (overlay-get company-pseudo-tooltip-overlay 'company-nl))
865 (height (overlay-get company-pseudo-tooltip-overlay 'company-height))
866 (lines (company-create-lines column selection height)))
867 (overlay-put company-pseudo-tooltip-overlay 'company-before
868 (company-replacement-string old-string lines column nl))))
869
870 (defun company-pseudo-tooltip-hide ()
871 (when company-pseudo-tooltip-overlay
872 (delete-overlay company-pseudo-tooltip-overlay)
873 (setq company-pseudo-tooltip-overlay nil)))
874
875 (defun company-pseudo-tooltip-hide-temporarily ()
876 (when (overlayp company-pseudo-tooltip-overlay)
877 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
878 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
879
880 (defun company-pseudo-tooltip-unhide ()
881 (when company-pseudo-tooltip-overlay
882 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
883 (overlay-put company-pseudo-tooltip-overlay 'before-string
884 (overlay-get company-pseudo-tooltip-overlay 'company-before))))
885
886 (defun company-pseudo-tooltip-frontend (command)
887 (case command
888 ('pre-command (company-pseudo-tooltip-hide-temporarily))
889 ('post-command
890 (unless (and (overlayp company-pseudo-tooltip-overlay)
891 (equal (overlay-get company-pseudo-tooltip-overlay
892 'company-height)
893 (company-pseudo-tooltip-height)))
894 ;; Redraw needed.
895 (company-pseudo-tooltip-show-at-point (- (point)
896 (length company-prefix))))
897 (company-pseudo-tooltip-unhide))
898 ('hide (company-pseudo-tooltip-hide)
899 (setq company-tooltip-offset 0))
900 ('update (when (overlayp company-pseudo-tooltip-overlay)
901 (company-pseudo-tooltip-edit company-candidates
902 company-selection)))))
903
904 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
905 (unless (and (eq command 'post-command)
906 (not (cdr company-candidates)))
907 (company-pseudo-tooltip-frontend command)))
908
909 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
910
911 (defvar company-preview-overlay nil)
912 (make-variable-buffer-local 'company-preview-overlay)
913
914 (defun company-preview-show-at-point (pos)
915 (company-preview-hide)
916
917 (setq company-preview-overlay (make-overlay pos pos))
918
919 (let ((completion (company-strip-prefix (nth company-selection
920 company-candidates))))
921 (and (equal pos (point))
922 (not (equal completion ""))
923 (add-text-properties 0 1 '(cursor t) completion))
924
925 (setq completion (propertize completion 'face 'company-preview))
926 (add-text-properties 0 (- (length company-common) (length company-prefix))
927 '(face company-preview-common) completion)
928
929 (overlay-put company-preview-overlay 'after-string completion)
930 (overlay-put company-preview-overlay 'window (selected-window))))
931
932 (defun company-preview-hide ()
933 (when company-preview-overlay
934 (delete-overlay company-preview-overlay)
935 (setq company-preview-overlay nil)))
936
937 (defun company-preview-frontend (command)
938 (case command
939 ('pre-command (company-preview-hide))
940 ('post-command (company-preview-show-at-point (point)))
941 ('hide (company-preview-hide))))
942
943 (defun company-preview-if-just-one-frontend (command)
944 (unless (and (eq command 'post-command)
945 (cdr company-candidates))
946 (company-preview-frontend command)))
947
948 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
949
950 (defvar company-echo-last-msg nil)
951 (make-variable-buffer-local 'company-echo-last-msg)
952
953 (defvar company-echo-timer nil)
954
955 (defvar company-echo-delay .1)
956
957 (defun company-echo-show (&optional getter)
958 (when getter
959 (setq company-echo-last-msg (funcall getter)))
960 (let ((message-log-max nil))
961 (if company-echo-last-msg
962 (message "%s" company-echo-last-msg)
963 (message ""))))
964
965 (defsubst company-echo-show-soon (&optional getter)
966 (when company-echo-timer
967 (cancel-timer company-echo-timer))
968 (setq company-echo-timer (run-with-timer company-echo-delay nil
969 'company-echo-show getter)))
970
971 (defun company-echo-format ()
972
973 (let ((limit (window-width (minibuffer-window)))
974 (len -1)
975 ;; Roll to selection.
976 (candidates (nthcdr company-selection company-candidates))
977 comp msg)
978
979 (while candidates
980 (setq comp (company-reformat (pop candidates))
981 len (+ len 1 (length comp)))
982 (if (>= len limit)
983 (setq candidates nil)
984 (setq comp (propertize comp 'face 'company-echo))
985 (add-text-properties 0 (length company-common)
986 '(face company-echo-common) comp)
987 (push comp msg)))
988
989 (mapconcat 'identity (nreverse msg) " ")))
990
991 (defun company-echo-hide ()
992 (when company-echo-timer
993 (cancel-timer company-echo-timer))
994 (setq company-echo-last-msg "")
995 (company-echo-show))
996
997 (defun company-echo-frontend (command)
998 (case command
999 ('pre-command (company-echo-show-soon))
1000 ('post-command (company-echo-show-soon 'company-echo-format))
1001 ('hide (company-echo-hide))))
1002
1003 (defun company-echo-metadata-frontend (command)
1004 (case command
1005 ('pre-command (company-echo-show-soon))
1006 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1007 ('hide (company-echo-hide))))
1008
1009 (provide 'company)
1010 ;;; company.el ends here