]> code.delx.au - gnu-emacs-elpa/blob - company.el
Added command to filter based on 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-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 (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-candidates-predicate nil)
256 (make-variable-buffer-local 'company-candidates-predicate)
257
258 (defvar company-common nil)
259 (make-variable-buffer-local 'company-common)
260
261 (defvar company-selection 0)
262 (make-variable-buffer-local 'company-selection)
263
264 (defvar company-selection-changed nil)
265 (make-variable-buffer-local 'company-selection-changed)
266
267 (defvar company-point nil)
268 (make-variable-buffer-local 'company-point)
269
270 (defvar company-disabled-backends nil)
271
272 (defsubst company-strip-prefix (str)
273 (substring str (length company-prefix)))
274
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))))
279
280 (defsubst company-should-complete (prefix)
281 (and (eq company-idle-delay t)
282 (>= (length prefix) company-minimum-prefix-length)))
283
284 (defsubst company-call-frontends (command)
285 (dolist (frontend company-frontends)
286 (funcall frontend command)))
287
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)))
294
295 (defun company-apply-predicate (candidates predicate)
296 (let (new)
297 (dolist (c candidates)
298 (when (funcall predicate c)
299 (push c new)))
300 (nreverse new)))
301
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))
306 prev)
307 (dotimes (i len)
308 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
309 company-candidates-cache)))
310 (setq company-candidates (all-completions prefix prev))
311 (return t))))
312 (progn
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))
328 company-candidates)
329
330 (defun company-idle-begin ()
331 (and company-mode
332 (not company-candidates)
333 (not (equal (point) company-point))
334 (let ((company-idle-delay t))
335 (company-begin)
336 (when company-candidates
337 (company-input-noop)
338 (company-post-command)))))
339
340 (defun company-manual-begin ()
341 (and company-mode
342 (not company-candidates)
343 (let ((company-idle-delay t)
344 (company-minimum-prefix-length 0))
345 (company-begin)))
346 ;; Return non-nil if active.
347 company-candidates)
348
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)))))
360
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)
365 (company-continue)
366 (unless company-candidates
367 (let (prefix)
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))
376 (return prefix))
377 (unless (memq backend company-disabled-backends)
378 (push backend company-disabled-backends)
379 (message "Company back-end '%s' could not be initialized"
380 backend)))))))
381 (if company-candidates
382 (progn
383 (setq company-point (point))
384 (company-enable-overriding-keymap company-active-map)
385 (company-call-frontends 'update))
386 (company-cancel)))
387
388 (defun company-cancel ()
389 (setq company-backend nil
390 company-prefix nil
391 company-candidates nil
392 company-candidates-cache nil
393 company-candidates-predicate nil
394 company-common nil
395 company-selection 0
396 company-selection-changed nil
397 company-point nil)
398 (company-search-mode 0)
399 (company-call-frontends 'hide)
400 (company-enable-overriding-keymap nil))
401
402 (defun company-abort ()
403 (company-cancel)
404 ;; Don't start again, unless started manually.
405 (setq company-point (point)))
406
407 (defun company-pre-command ()
408 (unless (eq this-command 'company-show-doc-buffer)
409 (condition-case err
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))
414 (company-cancel))))
415 (company-uninstall-map))
416
417 (defun company-post-command ()
418 (unless (eq this-command 'company-show-doc-buffer)
419 (condition-case err
420 (progn
421 (unless (equal (point) company-point)
422 (company-begin))
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))
427 (company-cancel))))
428 (company-install-map))
429
430 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
431
432 (defvar company-search-string nil)
433 (make-variable-buffer-local 'company-search-string)
434
435 (defvar company-search-lighter " Search: \"\"")
436 (make-variable-buffer-local 'company-search-lighter)
437
438 (defvar company-search-old-map nil)
439 (make-variable-buffer-local 'company-search-old-map)
440
441 (defvar company-search-old-selection 0)
442 (make-variable-buffer-local 'company-search-old-selection)
443
444 (defun company-search (text lines)
445 (let ((quoted (regexp-quote text))
446 (i 0))
447 (dolist (line lines)
448 (when (string-match quoted line (length company-prefix))
449 (return i))
450 (incf i))))
451
452 (defun company-search-printing-char ()
453 (interactive)
454 (setq company-search-string
455 (concat (or company-search-string "") (string last-command-event))
456 company-search-lighter (concat " Search: \"" company-search-string
457 "\""))
458 (let ((pos (company-search company-search-string
459 (nthcdr company-selection company-candidates))))
460 (if (null pos)
461 (ding)
462 (company-set-selection (+ company-selection pos) t))))
463
464 (defun company-search-repeat-forward ()
465 (interactive)
466 (let ((pos (company-search company-search-string
467 (cdr (nthcdr company-selection
468 company-candidates)))))
469 (if (null pos)
470 (ding)
471 (company-set-selection (+ company-selection pos 1) t))))
472
473 (defun company-search-repeat-backward ()
474 (interactive)
475 (let ((pos (company-search company-search-string
476 (nthcdr (- (length company-candidates)
477 company-selection)
478 (reverse company-candidates)))))
479 (if (null pos)
480 (ding)
481 (company-set-selection (- company-selection pos 1) t))))
482
483 (defun company-search-kill-others ()
484 (interactive)
485 (let ((predicate `(lambda (candidate)
486 (string-match ,company-search-string candidate))))
487 (company-cancel)
488 (setq company-candidates-predicate predicate)
489 (company-manual-begin)))
490
491 (defun company-search-abort ()
492 (interactive)
493 (company-set-selection company-search-old-selection t)
494 (company-search-mode 0))
495
496 (defun company-search-other-char ()
497 (interactive)
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))))
502
503 (defvar company-search-map
504 (let ((i 0)
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)
509 (while (< i ?\s)
510 (define-key keymap (make-string 1 i) 'company-search-other-char)
511 (incf i))
512 (while (< i 256)
513 (define-key keymap (vector i) 'company-search-printing-char)
514 (incf i))
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)
521
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)
526 keymap))
527
528 (define-minor-mode company-search-mode
529 ""
530 nil company-search-lighter nil
531 (if company-search-mode
532 (if (company-manual-begin)
533 (progn
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)))
542
543 (defun company-search-candidates ()
544 (interactive)
545 (company-search-mode 1))
546
547 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
548
549 (defun company-select-next ()
550 (interactive)
551 (when (company-manual-begin)
552 (company-set-selection (1+ company-selection))))
553
554 (defun company-select-previous ()
555 (interactive)
556 (when (company-manual-begin)
557 (company-set-selection (1- company-selection))))
558
559 (defun company-complete-selection ()
560 (interactive)
561 (when (company-manual-begin)
562 (insert (company-strip-prefix (nth company-selection company-candidates)))
563 (company-abort)))
564
565 (defun company-complete-common ()
566 (interactive)
567 (when (company-manual-begin)
568 (insert (company-strip-prefix company-common))))
569
570 (defun company-complete ()
571 (interactive)
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))))
578
579 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
580
581 (defconst company-space-strings-limit 100)
582
583 (defconst company-space-strings
584 (let (lst)
585 (dotimes (i company-space-strings-limit)
586 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
587 (apply 'vector lst)))
588
589 (defsubst company-space-string (len)
590 (if (< len company-space-strings-limit)
591 (aref company-space-strings len)
592 (make-string len ?\ )))
593
594 (defsubst company-safe-substring (str from &optional to)
595 (let ((len (length str)))
596 (if (> from len)
597 ""
598 (if (and to (> to len))
599 (concat (substring str from)
600 (company-space-string (- to len)))
601 (substring str from to)))))
602
603 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
604
605 (defvar company-last-metadata nil)
606 (make-variable-buffer-local 'company-last-metadata)
607
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)))
614
615 (defun company-doc-buffer (&optional string)
616 (with-current-buffer (get-buffer-create "*Company meta-data*")
617 (erase-buffer)
618 (current-buffer)))
619
620 (defun company-show-doc-buffer ()
621 (interactive)
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)))
628 (if (not buffer)
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)))
634 (read-event)
635 (when last-input-event
636 (clear-this-command-keys t)
637 (setq unread-command-events (list last-input-event))))))))
638
639 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
640
641 (defvar company-pseudo-tooltip-overlay nil)
642 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
643
644 (defvar company-tooltip-offset 0)
645 (make-variable-buffer-local 'company-tooltip-offset)
646
647 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
648
649 (decf limit 2)
650 (setq company-tooltip-offset
651 (max (min selection company-tooltip-offset)
652 (- selection -1 limit)))
653
654 (when (<= company-tooltip-offset 1)
655 (incf limit)
656 (setq company-tooltip-offset 0))
657
658 (when (>= company-tooltip-offset (- num-lines limit 1))
659 (incf limit)
660 (when (= selection (1- num-lines))
661 (decf company-tooltip-offset)
662 (when (<= company-tooltip-offset 1)
663 (setq company-tooltip-offset 0)
664 (incf limit))))
665
666 limit)
667
668 ;;; propertize
669
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)
675 (when selected
676 (if (and company-search-string
677 (string-match (regexp-quote company-search-string) line
678 (length company-prefix)))
679 (progn
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)
685 line)))
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)
689 line)))
690 line)
691
692 ;;; replace
693
694 (defun company-buffer-lines (beg end)
695 (goto-char beg)
696 (let ((row (cdr (posn-col-row (posn-at-point))))
697 lines)
698 (while (and (equal (move-to-window-line (incf row)) row)
699 (<= (point) end))
700 (push (buffer-substring beg (min end (1- (point)))) lines)
701 (setq beg (point)))
702 (unless (eq beg end)
703 (push (buffer-substring beg end) lines))
704 (nreverse lines)))
705
706 (defun company-modify-line (old new offset)
707 (concat (company-safe-substring old 0 offset)
708 new
709 (company-safe-substring old (+ offset (length new)))))
710
711 (defun company-replacement-string (old lines column nl)
712 (let (new)
713 ;; Inject into old lines.
714 (while old
715 (push (company-modify-line (pop old) (pop lines) column) new))
716 ;; Append whole new lines.
717 (while lines
718 (push (company-modify-line "" (pop lines) column) new))
719 (concat (when nl "\n")
720 (mapconcat 'identity (nreverse new) "\n")
721 "\n")))
722
723 (defun company-create-lines (column lines selection limit)
724
725 (let ((len (length lines))
726 width
727 lines-copy
728 previous
729 remainder
730 new)
731
732 ;; Scroll to offset.
733 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
734
735 (when (> company-tooltip-offset 0)
736 (setq previous (format "...(%d)" company-tooltip-offset)))
737
738 (setq remainder (- len limit company-tooltip-offset)
739 remainder (when (> remainder 0)
740 (setq remainder (format "...(%d)" remainder))))
741
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))
746 lines-copy lines)
747
748 (dotimes (i len)
749 (setq width (max (length (pop lines-copy)) width)))
750 (setq width (min width (- (window-width) column)))
751
752 (when previous
753 (push (propertize (company-safe-substring previous 0 width)
754 'face 'company-tooltip)
755 new))
756
757 (dotimes (i len)
758 (push (company-fill-propertize (company-reformat (pop lines))
759 width (equal i selection))
760 new))
761
762 (when remainder
763 (push (propertize (company-safe-substring remainder 0 width)
764 'face 'company-tooltip)
765 new))
766
767 (setq lines (nreverse new))))
768
769 ;; show
770
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))))
775
776 (defun company-pseudo-tooltip-show (row column lines selection)
777 (company-pseudo-tooltip-hide)
778 (unless lines (error "No text provided"))
779 (save-excursion
780
781 (move-to-column 0)
782
783 (let* ((height (company-pseudo-tooltip-height))
784 (lines (company-create-lines column lines selection height))
785 (nl (< (move-to-window-line row) row))
786 (beg (point))
787 (end (save-excursion
788 (move-to-window-line (+ row height))
789 (point)))
790 (old-string (company-buffer-lines beg end))
791 str)
792
793 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
794
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)
801
802 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
803
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)))
808
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))))
817
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)))
822
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)))
827
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))))
833
834 (defun company-pseudo-tooltip-frontend (command)
835 (case command
836 ('pre-command (company-pseudo-tooltip-hide-temporarily))
837 ('post-command
838 (unless (and (overlayp company-pseudo-tooltip-overlay)
839 (equal (overlay-get company-pseudo-tooltip-overlay
840 'company-height)
841 (company-pseudo-tooltip-height)))
842 ;; Redraw needed.
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)))))
851
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)))
856
857 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
858
859 (defvar company-preview-overlay nil)
860 (make-variable-buffer-local 'company-preview-overlay)
861
862 (defun company-preview-show-at-point (pos)
863 (company-preview-hide)
864
865 (setq company-preview-overlay (make-overlay pos pos))
866
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))
872
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)
876
877 (overlay-put company-preview-overlay 'after-string completion)
878 (overlay-put company-preview-overlay 'window (selected-window))))
879
880 (defun company-preview-hide ()
881 (when company-preview-overlay
882 (delete-overlay company-preview-overlay)
883 (setq company-preview-overlay nil)))
884
885 (defun company-preview-frontend (command)
886 (case command
887 ('pre-command (company-preview-hide))
888 ('post-command (company-preview-show-at-point (point)))
889 ('hide (company-preview-hide))))
890
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)))
895
896 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
897
898 (defvar company-echo-last-msg nil)
899 (make-variable-buffer-local 'company-echo-last-msg)
900
901 (defun company-echo-refresh ()
902 (let ((message-log-max nil))
903 (if company-echo-last-msg
904 (message "%s" company-echo-last-msg)
905 (message ""))))
906
907 (defun company-echo-show (candidates)
908
909 ;; Roll to selection.
910 (setq candidates (nthcdr company-selection candidates))
911
912 (let ((limit (window-width (minibuffer-window)))
913 (len -1)
914 comp msg)
915 (while candidates
916 (setq comp (company-reformat (pop candidates))
917 len (+ len 1 (length comp)))
918 (if (>= len limit)
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)
923 (push comp msg)))
924
925 (setq company-echo-last-msg (mapconcat 'identity (nreverse msg) " "))
926 (company-echo-refresh)))
927
928 (defun company-echo-frontend (command)
929 (case command
930 ('pre-command (company-echo-refresh))
931 ('post-command (company-echo-show company-candidates))
932 ('hide (setq company-echo-last-msg nil))))
933
934 (defun company-echo-metadata-frontend (command)
935 (case 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))))
940
941
942 (provide 'company)
943 ;;; company.el ends here