1 (eval-when-compile (require 'cl))
3 (add-to-list 'debug-ignored-errors
4 "^Pseudo tooltip frontend cannot be used twice$")
5 (add-to-list 'debug-ignored-errors "^Preview frontend cannot be used twice$")
13 (defface company-tooltip
14 '((t :background "yellow"
19 (defface company-tooltip-selection
20 '((t :background "orange1"
25 (defface company-tooltip-common
26 '((t :inherit company-tooltip
31 (defface company-tooltip-common-selection
32 '((t :inherit company-tooltip-selection
37 (defcustom company-tooltip-limit 10
42 (defface company-preview
43 '((t :background "blue4"
48 (defface company-preview-common
49 '((t :inherit company-preview
54 (defface company-echo nil
58 (defface company-echo-common
59 '((((background dark)) (:foreground "firebrick1"))
60 (((background light)) (:background "firebrick4")))
64 (defun company-frontends-set (variable value)
66 (let ((remainder value))
67 (setcdr remainder (delq (car remainder) (cdr remainder))))
68 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
69 (memq 'company-pseudo-tooltip-frontend value)
70 (error "Pseudo tooltip frontend cannot be used twice"))
71 (and (memq 'company-preview-if-just-one-frontend value)
72 (memq 'company-preview-frontend value)
73 (error "Preview frontend cannot be used twice"))
74 ;; preview must come last
75 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
77 (setq value (append (delq f value) (list f)))))
80 (defcustom company-frontends '(company-echo-frontend
81 company-pseudo-tooltip-unless-just-one-frontend
82 company-preview-if-just-one-frontend)
84 :set 'company-frontends-set
86 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
87 (const :tag "pseudo tooltip"
88 company-pseudo-tooltip-frontend)
89 (const :tag "pseudo tooltip, multiple only"
90 company-pseudo-tooltip-unless-just-one-frontend)
91 (const :tag "preview" company-preview-frontend)
92 (const :tag "preview, unique only"
93 company-preview-if-just-one-frontend)
94 (function :tag "custom function" nil))))
96 (defcustom company-backends '(company-elisp company-nxml company-css
100 :type '(repeat (function :tag "function" nil)))
102 (defcustom company-minimum-prefix-length 3
105 :type '(integer :tag "prefix length"))
107 (defvar company-timer nil)
109 (defun company-timer-set (variable value)
111 (when company-timer (cancel-timer company-timer))
112 (when (numberp value)
113 (setq company-timer (run-with-idle-timer value t 'company-idle-begin))))
115 (defcustom company-idle-delay .7
117 :set 'company-timer-set
119 :type '(choice (const :tag "never (nil)" nil)
120 (const :tag "immediate (t)" t)
121 (number :tag "seconds")))
123 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 (defvar company-mode-map
126 (let ((keymap (make-sparse-keymap)))
127 (define-key keymap (kbd "M-n") 'company-select-next)
128 (define-key keymap (kbd "M-p") 'company-select-previous)
129 (define-key keymap (kbd "M-<return>") 'company-complete-selection)
130 (define-key keymap "\t" 'company-complete-common)
134 (define-minor-mode company-mode
136 nil " comp" company-mode-map
139 (add-hook 'pre-command-hook 'company-pre-command nil t)
140 (add-hook 'post-command-hook 'company-post-command nil t)
141 (company-timer-set 'company-idle-delay
143 (remove-hook 'pre-command-hook 'company-pre-command t)
144 (remove-hook 'post-command-hook 'company-post-command t)
146 (kill-local-variable 'company-point)))
148 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
150 (defun company-grab (regexp &optional expression)
151 (when (looking-back regexp)
152 (or (match-string-no-properties (or expression 0)) "")))
154 (defun company-in-string-or-comment (&optional point)
155 (let ((pos (syntax-ppss)))
156 (or (nth 3 pos) (nth 4 pos) (nth 7 pos))))
158 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 (defvar company-backend nil)
161 (make-variable-buffer-local 'company-backend)
163 (defvar company-prefix nil)
164 (make-variable-buffer-local 'company-prefix)
166 (defvar company-candidates nil)
167 (make-variable-buffer-local 'company-candidates)
169 (defvar company-common nil)
170 (make-variable-buffer-local 'company-common)
172 (defvar company-selection 0)
173 (make-variable-buffer-local 'company-selection)
175 (defvar company-selection-changed nil)
176 (make-variable-buffer-local 'company-selection-changed)
178 (defvar company-point nil)
179 (make-variable-buffer-local 'company-point)
181 (defvar company-disabled-backends nil)
183 (defsubst company-strip-prefix (str)
184 (substring str (length company-prefix)))
186 (defsubst company-offset (display-limit)
187 (let ((offset (- company-selection display-limit -1)))
190 (defsubst company-reformat (candidate)
191 ;; company-ispell needs this, because the results are always lower-case
192 ;; It's mory efficient to fix it only when they are displayed.
193 (concat company-prefix (substring candidate (length company-prefix))))
195 (defsubst company-should-complete (prefix)
196 (and (eq company-idle-delay t)
197 (>= (length prefix) company-minimum-prefix-length)))
199 (defsubst company-call-frontends (command)
200 (dolist (frontend company-frontends)
201 (funcall frontend command)))
203 (defun company-idle-begin ()
205 (not company-candidates)
206 (not (equal (point) company-point))
207 (let ((company-idle-delay t))
209 (company-post-command))))
211 (defun company-manual-begin ()
213 (not company-candidates)
214 (let ((company-idle-delay t)
215 (company-minimum-prefix-length 0))
217 ;; Return non-nil if active.
220 (defun company-continue-or-cancel ()
221 (when company-candidates
222 (let ((old-point (- company-point (length company-prefix)))
223 (company-idle-delay t)
224 (company-minimum-prefix-length 0))
225 ;; TODO: Make more efficient.
226 (setq company-candidates nil)
228 (unless (and company-candidates
229 (equal old-point (- company-point (length company-prefix))))
231 company-candidates)))
233 (defun company-begin ()
234 (or (company-continue-or-cancel)
236 (dolist (backend company-backends)
237 (unless (fboundp backend)
238 (ignore-errors (require backend nil t)))
239 (if (fboundp backend)
240 (when (setq prefix (funcall backend 'prefix))
241 (when (company-should-complete prefix)
242 (setq company-backend backend
243 company-prefix prefix
245 (funcall company-backend 'candidates prefix)
247 (let ((completion-ignore-case (funcall backend
249 (try-completion prefix company-candidates))
251 company-point (point))
252 (unless (funcall company-backend 'sorted)
253 (setq company-candidates
254 (sort company-candidates 'string<)))
255 (company-call-frontends 'update))
257 (unless (memq backend company-disabled-backends)
258 (push backend company-disabled-backends)
259 (message "Company back-end '%s' could not be initialized"
261 (unless (and company-candidates
262 (not (eq t company-common)))
265 (defun company-cancel ()
266 (setq company-backend nil
268 company-candidates nil
271 company-selection-changed nil
273 (company-call-frontends 'hide))
275 (defun company-abort ()
277 ;; Don't start again, unless started manually.
278 (setq company-point (point)))
280 (defun company-pre-command ()
281 (when company-candidates
282 (company-call-frontends 'pre-command)))
284 (defun company-post-command ()
285 (unless (equal (point) company-point)
287 (when company-candidates
288 (company-call-frontends 'post-command)))
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
292 (defun company-select-next ()
294 (when (company-manual-begin)
295 (setq company-selection (min (1- (length company-candidates))
296 (1+ company-selection))
297 company-selection-changed t)))
299 (defun company-select-previous ()
301 (when (company-manual-begin)
302 (setq company-selection (max 0 (1- company-selection))
303 company-selection-changed t)))
305 (defun company-complete-selection ()
307 (when (company-manual-begin)
308 (insert (company-strip-prefix (nth company-selection company-candidates)))
311 (defun company-complete-common ()
313 (when (company-manual-begin)
314 (insert (company-strip-prefix company-common))))
316 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
318 (defconst company-space-strings-limit 100)
320 (defconst company-space-strings
322 (dotimes (i company-space-strings-limit)
323 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
324 (apply 'vector lst)))
326 (defsubst company-space-string (len)
327 (if (< len company-space-strings-limit)
328 (aref company-space-strings len)
329 (make-string len ?\ )))
331 (defsubst company-safe-substring (str from &optional to)
332 (let ((len (length str)))
335 (if (and to (> to len))
336 (concat (substring str from)
337 (company-space-string (- to len)))
338 (substring str from to)))))
340 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
342 (defvar company-pseudo-tooltip-overlay nil)
343 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
347 (defun company-fill-propertize (line width selected)
348 (setq line (company-safe-substring line 0 width))
349 (add-text-properties 0 width
350 (list 'face (if selected
351 'company-tooltip-selection
352 'company-tooltip)) line)
353 (add-text-properties 0 (length company-common)
354 (list 'face (if selected
355 'company-tooltip-common-selection
356 'company-tooltip-common)) line)
359 (defun company-fill-propertize-lines (column lines selection)
362 (len (min company-tooltip-limit (length lines)))
365 (setq width (max (length (pop lines-copy)) width)))
366 (setq width (min width (- (window-width) column)))
368 (push (company-fill-propertize (company-reformat (pop lines))
369 width (equal i selection))
375 (defun company-buffer-lines (beg end)
377 (let ((row (cdr (posn-col-row (posn-at-point))))
379 (while (< (point) end)
380 (move-to-window-line (incf row))
381 (push (buffer-substring beg (min end (1- (point)))) lines)
385 (defun company-modify-line (old new offset)
386 (concat (company-safe-substring old 0 offset)
388 (company-safe-substring old (+ offset (length new)))))
390 (defun company-modified-substring (beg end lines column nl)
391 (let ((old (company-buffer-lines beg end))
393 ;; Inject into old lines.
395 (push (company-modify-line (pop old) (pop lines) column) new))
396 ;; Append whole new lines.
398 (push (company-modify-line "" (pop lines) column) new))
399 (concat (when nl "\n")
400 (mapconcat 'identity (nreverse new) "\n")
405 (defun company-pseudo-tooltip-show (row column lines selection)
406 (company-pseudo-tooltip-hide)
407 (unless lines (error "No text provided"))
411 (let ((offset (company-offset company-tooltip-limit)))
412 (setq lines (nthcdr offset lines))
413 (decf selection offset))
415 (setq lines (company-fill-propertize-lines column lines selection))
420 (let ((nl (< (move-to-window-line row) row))
423 (move-to-window-line (min (window-height)
424 (+ row company-tooltip-limit)))
428 (setq company-pseudo-tooltip-overlay (make-overlay beg end))
430 (overlay-put company-pseudo-tooltip-overlay 'before-string
431 (company-modified-substring beg end lines column nl))
432 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
433 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window)))))
435 (defun company-pseudo-tooltip-show-at-point (pos)
436 (let ((col-row (posn-col-row (posn-at-point pos))))
437 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
438 company-candidates company-selection)))
440 (defun company-pseudo-tooltip-hide ()
441 (when company-pseudo-tooltip-overlay
442 (delete-overlay company-pseudo-tooltip-overlay)
443 (setq company-pseudo-tooltip-overlay nil)))
445 (defun company-pseudo-tooltip-frontend (command)
447 ('pre-command (company-pseudo-tooltip-hide))
448 ('post-command (company-pseudo-tooltip-show-at-point
449 (- (point) (length company-prefix))))
450 ('hide (company-pseudo-tooltip-hide))))
452 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
453 (unless (and (eq command 'post-command)
454 (not (cdr company-candidates)))
455 (company-pseudo-tooltip-frontend command)))
457 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
459 (defvar company-preview-overlay nil)
460 (make-variable-buffer-local 'company-preview-overlay)
462 (defun company-preview-show-at-point (pos)
463 (company-preview-hide)
465 (setq company-preview-overlay (make-overlay pos pos))
467 (let ((completion (company-strip-prefix (nth company-selection
468 company-candidates))))
469 (and (equal pos (point))
470 (not (equal completion ""))
471 (add-text-properties 0 1 '(cursor t) completion))
473 (setq completion (propertize completion 'face 'company-preview))
474 (add-text-properties 0 (- (length company-common) (length company-prefix))
475 '(face company-preview-common) completion)
477 (overlay-put company-preview-overlay 'after-string completion)
478 (overlay-put company-preview-overlay 'window (selected-window))))
480 (defun company-preview-hide ()
481 (when company-preview-overlay
482 (delete-overlay company-preview-overlay)
483 (setq company-preview-overlay nil)))
485 (defun company-preview-frontend (command)
487 ('pre-command (company-preview-hide))
488 ('post-command (company-preview-show-at-point (point)))
489 ('hide (company-preview-hide))))
491 (defun company-preview-if-just-one-frontend (command)
492 (unless (and (eq command 'post-command)
493 (cdr company-candidates))
494 (company-preview-frontend command)))
496 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
498 (defvar company-echo-last-msg nil)
499 (make-variable-buffer-local 'company-echo-last-msg)
501 (defun company-echo-refresh ()
502 (let ((message-log-max nil))
503 (if company-echo-last-msg
504 (message "%s" company-echo-last-msg)
507 (defun company-echo-show (candidates)
509 ;; Roll to selection.
510 (setq candidates (nthcdr company-selection candidates))
512 (let ((limit (window-width (minibuffer-window)))
516 (setq comp (company-reformat (pop candidates))
517 len (+ len 1 (length comp)))
519 (setq candidates nil)
520 (setq comp (propertize comp 'face 'company-echo))
521 (add-text-properties 0 (length company-common)
522 '(face company-echo-common) comp)
525 (setq company-echo-last-msg (mapconcat 'identity (nreverse msg) " "))
526 (company-echo-refresh)))
528 (defun company-echo-frontend (command)
530 ('pre-command (company-echo-refresh))
531 ('post-command (company-echo-show company-candidates))
532 ('hide (setq company-echo-last-msg nil))))
535 ;;; company.el ends here