]> code.delx.au - gnu-emacs-elpa/blob - company.el
company--multi-backend-adapter: safe > fast
[gnu-emacs-elpa] / company.el
1 ;;; company.el --- Modular in-buffer completion framework
2
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6 ;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
7 ;; Version: 0.6
8 ;; Keywords: abbrev, convenience, matching
9 ;; URL: http://company-mode.github.com/
10 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x, GNU Emacs 24.x
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28 ;;
29 ;; Company is a modular completion mechanism. Modules for retrieving completion
30 ;; candidates are called back-ends, modules for displaying them are front-ends.
31 ;;
32 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
33 ;; distributed in separate files and can be used individually.
34 ;;
35 ;; Place company.el and the back-ends you want to use in a directory and add the
36 ;; following to your .emacs:
37 ;; (add-to-list 'load-path "/path/to/company")
38 ;; (autoload 'company-mode "company" nil t)
39 ;;
40 ;; Enable company-mode with M-x company-mode. For further information look at
41 ;; the documentation for `company-mode' (C-h f company-mode RET)
42 ;;
43 ;; If you want to start a specific back-end, call it interactively or use
44 ;; `company-begin-backend'. For example:
45 ;; M-x company-abbrev will prompt for and insert an abbrev.
46 ;;
47 ;; To write your own back-end, look at the documentation for `company-backends'.
48 ;; Here is a simple example completing "foo":
49 ;;
50 ;; (defun company-my-backend (command &optional arg &rest ignored)
51 ;; (case command
52 ;; (prefix (when (looking-back "foo\\>")
53 ;; (match-string 0)))
54 ;; (candidates (list "foobar" "foobaz" "foobarbaz"))
55 ;; (meta (format "This value is named %s" arg))))
56 ;;
57 ;; Sometimes it is a good idea to mix several back-ends together, for example to
58 ;; enrich gtags with dabbrev-code results (to emulate local variables).
59 ;; To do this, add a list with both back-ends as an element in company-backends.
60 ;;
61 ;; Known Issues:
62 ;; When point is at the very end of the buffer, the pseudo-tooltip appears very
63 ;; wrong, unless company is allowed to temporarily insert a fake newline.
64 ;; This behavior is enabled by `company-end-of-buffer-workaround'.
65 ;;
66 ;;; Change Log:
67 ;;
68 ;; See NEWS.md in the repository.
69
70 ;;; Code:
71
72 (eval-when-compile (require 'cl))
73
74 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
75 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
76 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
77 (add-to-list 'debug-ignored-errors "^Company not ")
78 (add-to-list 'debug-ignored-errors "^No candidate number ")
79 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
80 (add-to-list 'debug-ignored-errors "^No other back-end$")
81
82 (defgroup company nil
83 "Extensible inline text completion mechanism"
84 :group 'abbrev
85 :group 'convenience
86 :group 'matching)
87
88 (defface company-tooltip
89 '((t :background "yellow"
90 :foreground "black"))
91 "Face used for the tool tip."
92 :group 'company)
93
94 (defface company-tooltip-selection
95 '((default :inherit company-tooltip)
96 (((class color) (min-colors 88)) (:background "orange1"))
97 (t (:background "green")))
98 "Face used for the selection in the tool tip."
99 :group 'company)
100
101 (defface company-tooltip-mouse
102 '((default :inherit highlight))
103 "Face used for the tool tip item under the mouse."
104 :group 'company)
105
106 (defface company-tooltip-common
107 '((t :inherit company-tooltip
108 :foreground "red"))
109 "Face used for the common completion in the tool tip."
110 :group 'company)
111
112 (defface company-tooltip-common-selection
113 '((t :inherit company-tooltip-selection
114 :foreground "red"))
115 "Face used for the selected common completion in the tool tip."
116 :group 'company)
117
118 (defface company-preview
119 '((t :background "blue4"
120 :foreground "wheat"))
121 "Face used for the completion preview."
122 :group 'company)
123
124 (defface company-preview-common
125 '((t :inherit company-preview
126 :foreground "red"))
127 "Face used for the common part of the completion preview."
128 :group 'company)
129
130 (defface company-preview-search
131 '((t :inherit company-preview
132 :background "blue1"))
133 "Face used for the search string in the completion preview."
134 :group 'company)
135
136 (defface company-echo nil
137 "Face used for completions in the echo area."
138 :group 'company)
139
140 (defface company-echo-common
141 '((((background dark)) (:foreground "firebrick1"))
142 (((background light)) (:background "firebrick4")))
143 "Face used for the common part of completions in the echo area."
144 :group 'company)
145
146 (defun company-frontends-set (variable value)
147 ;; uniquify
148 (let ((remainder value))
149 (setcdr remainder (delq (car remainder) (cdr remainder))))
150 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
151 (memq 'company-pseudo-tooltip-frontend value)
152 (error "Pseudo tooltip frontend cannot be used twice"))
153 (and (memq 'company-preview-if-just-one-frontend value)
154 (memq 'company-preview-frontend value)
155 (error "Preview frontend cannot be used twice"))
156 (and (memq 'company-echo value)
157 (memq 'company-echo-metadata-frontend value)
158 (error "Echo area cannot be used twice"))
159 ;; preview must come last
160 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
161 (when (memq f value)
162 (setq value (append (delq f value) (list f)))))
163 (set variable value))
164
165 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
166 company-preview-if-just-one-frontend
167 company-echo-metadata-frontend)
168 "The list of active front-ends (visualizations).
169 Each front-end is a function that takes one argument. It is called with
170 one of the following arguments:
171
172 'show: When the visualization should start.
173
174 'hide: When the visualization should end.
175
176 'update: When the data has been updated.
177
178 'pre-command: Before every command that is executed while the
179 visualization is active.
180
181 'post-command: After every command that is executed while the
182 visualization is active.
183
184 The visualized data is stored in `company-prefix', `company-candidates',
185 `company-common', `company-selection', `company-point' and
186 `company-search-string'."
187 :set 'company-frontends-set
188 :group 'company
189 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
190 (const :tag "echo, strip common"
191 company-echo-strip-common-frontend)
192 (const :tag "show echo meta-data in echo"
193 company-echo-metadata-frontend)
194 (const :tag "pseudo tooltip"
195 company-pseudo-tooltip-frontend)
196 (const :tag "pseudo tooltip, multiple only"
197 company-pseudo-tooltip-unless-just-one-frontend)
198 (const :tag "preview" company-preview-frontend)
199 (const :tag "preview, unique only"
200 company-preview-if-just-one-frontend)
201 (function :tag "custom function" nil))))
202
203 (defcustom company-tooltip-limit 10
204 "The maximum number of candidates in the tool tip"
205 :group 'company
206 :type 'integer)
207
208 (defcustom company-tooltip-minimum 6
209 "The minimum height of the tool tip.
210 If this many lines are not available, prefer to display the tooltip above."
211 :group 'company
212 :type 'integer)
213
214 (defvar company-safe-backends
215 '((company-abbrev . "Abbrev")
216 (company-clang . "clang")
217 (company-css . "CSS")
218 (company-dabbrev . "dabbrev for plain text")
219 (company-dabbrev-code . "dabbrev for code")
220 (company-eclim . "eclim (an Eclipse interace)")
221 (company-elisp . "Emacs Lisp")
222 (company-etags . "etags")
223 (company-files . "Files")
224 (company-gtags . "GNU Global")
225 (company-ispell . "ispell")
226 (company-keywords . "Programming language keywords")
227 (company-nxml . "nxml")
228 (company-oddmuse . "Oddmuse")
229 (company-pysmell . "PySmell")
230 (company-ropemacs . "ropemacs")
231 (company-semantic . "CEDET Semantic")
232 (company-tempo . "Tempo templates")
233 (company-xcode . "Xcode")))
234 (put 'company-safe-backends 'risky-local-variable t)
235
236 (defun company-safe-backends-p (backends)
237 (and (consp backends)
238 (not (dolist (backend backends)
239 (unless (if (consp backend)
240 (company-safe-backends-p backend)
241 (assq backend company-safe-backends))
242 (return t))))))
243
244 (defun company-capf (command &optional arg &rest args)
245 "`company-mode' back-end using `completion-at-point-functions'.
246 Requires Emacs 24.1 or newer."
247 (interactive (list 'interactive))
248 (case command
249 (interactive (company-begin-backend 'company-capf))
250 (prefix
251 (let ((res (run-hook-wrapped 'completion-at-point-functions
252 ;; Ignore misbehaving functions.
253 #'completion--capf-wrapper 'optimist)))
254 (when (consp res)
255 (if (> (nth 2 res) (point))
256 'stop
257 (buffer-substring-no-properties (nth 1 res) (point))))))
258 (candidates
259 (let ((res (run-hook-wrapped 'completion-at-point-functions
260 ;; Ignore misbehaving functions.
261 #'completion--capf-wrapper 'optimist)))
262 (when (consp res)
263 (all-completions arg (nth 3 res)
264 (plist-get (nthcdr 4 res) :predicate)))))))
265
266 (defcustom company-backends '(company-elisp company-nxml company-css
267 company-clang company-semantic company-eclim
268 company-xcode company-ropemacs
269 (company-gtags company-etags company-dabbrev-code
270 company-keywords)
271 company-oddmuse company-files company-dabbrev)
272 "The list of active back-ends (completion engines).
273 Each list elements can itself be a list of back-ends. In that case their
274 completions are merged. Otherwise only the first matching back-end returns
275 results.
276
277 `company-begin-backend' can be used to start a specific back-end,
278 `company-other-backend' will skip to the next matching back-end in the list.
279
280 Each back-end is a function that takes a variable number of arguments.
281 The first argument is the command requested from the back-end. It is one
282 of the following:
283
284 `prefix': The back-end should return the text to be completed. It must be
285 text immediately before `point'. Returning nil passes control to the next
286 back-end. The function should return 'stop if it should complete but cannot
287 \(e.g. if it is in the middle of a string\). If the returned value is only
288 part of the prefix (e.g. the part after \"->\" in C), the back-end may return a
289 cons of prefix and prefix length, which is then used in the
290 `company-minimum-prefix-length' test.
291
292 `candidates': The second argument is the prefix to be completed. The
293 return value should be a list of candidates that start with the prefix.
294
295 Optional commands:
296
297 `sorted': The back-end may return t here to indicate that the candidates
298 are sorted and will not need to be sorted again.
299
300 `duplicates': If non-nil, company will take care of removing duplicates
301 from the list.
302
303 `no-cache': Usually company doesn't ask for candidates again as completion
304 progresses, unless the back-end returns t for this command. The second
305 argument is the latest prefix.
306
307 `meta': The second argument is a completion candidate. The back-end should
308 return a (short) documentation string for it.
309
310 `doc-buffer': The second argument is a completion candidate.
311 The back-end should create a buffer (preferably with `company-doc-buffer'),
312 fill it with documentation and return it.
313
314 `location': The second argument is a completion candidate. The back-end can
315 return the cons of buffer and buffer location, or of file and line
316 number where the completion candidate was defined.
317
318 `require-match': If this value is t, the user is not allowed to enter anything
319 not offering as a candidate. Use with care! The default value nil gives the
320 user that choice with `company-require-match'. Return value 'never overrides
321 that option the other way around.
322
323 `init': Called once for each buffer, the back-end can check for external
324 programs and files and load any required libraries. Raising an error here will
325 show up in message log once, and the backend will not be used for completion.
326
327 `post-completion': Called after a completion candidate has been inserted into
328 the buffer. The second argument is the candidate. Can be used to modify it,
329 e.g. to expand a snippet.
330
331 The back-end should return nil for all commands it does not support or
332 does not know about. It should also be callable interactively and use
333 `company-begin-backend' to start itself in that case."
334 :group 'company
335 :type `(repeat
336 (choice
337 :tag "Back-end"
338 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
339 company-safe-backends)
340 (symbol :tag "User defined")
341 (repeat :tag "Merged Back-ends"
342 (choice :tag "Back-end"
343 ,@(mapcar (lambda (b)
344 `(const :tag ,(cdr b) ,(car b)))
345 company-safe-backends)
346 (symbol :tag "User defined"))))))
347
348 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
349
350 (defcustom company-completion-started-hook nil
351 "Hook run when company starts completing.
352 The hook is called with one argument that is non-nil if the completion was
353 started manually."
354 :group 'company
355 :type 'hook)
356
357 (defcustom company-completion-cancelled-hook nil
358 "Hook run when company cancels completing.
359 The hook is called with one argument that is non-nil if the completion was
360 aborted manually."
361 :group 'company
362 :type 'hook)
363
364 (defcustom company-completion-finished-hook nil
365 "Hook run when company successfully completes.
366 The hook is called with the selected candidate as an argument."
367 :group 'company
368 :type 'hook)
369
370 (defcustom company-minimum-prefix-length 3
371 "The minimum prefix length for automatic completion."
372 :group 'company
373 :type '(integer :tag "prefix length"))
374
375 (defcustom company-require-match 'company-explicit-action-p
376 "If enabled, disallow non-matching input.
377 This can be a function do determine if a match is required.
378
379 This can be overridden by the back-end, if it returns t or 'never to
380 'require-match. `company-auto-complete' also takes precedence over this."
381 :group 'company
382 :type '(choice (const :tag "Off" nil)
383 (function :tag "Predicate function")
384 (const :tag "On, if user interaction took place"
385 'company-explicit-action-p)
386 (const :tag "On" t)))
387
388 (defcustom company-auto-complete 'company-explicit-action-p
389 "Determines when to auto-complete.
390 If this is enabled, all characters from `company-auto-complete-chars' complete
391 the selected completion. This can also be a function."
392 :group 'company
393 :type '(choice (const :tag "Off" nil)
394 (function :tag "Predicate function")
395 (const :tag "On, if user interaction took place"
396 'company-explicit-action-p)
397 (const :tag "On" t)))
398
399 (defcustom company-auto-complete-chars '(?\ ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
400 "Determines which characters trigger an automatic completion.
401 See `company-auto-complete'. If this is a string, each string character causes
402 completion. If it is a list of syntax description characters (see
403 `modify-syntax-entry'), all characters with that syntax auto-complete.
404
405 This can also be a function, which is called with the new input and should
406 return non-nil if company should auto-complete.
407
408 A character that is part of a valid candidate never starts auto-completion."
409 :group 'company
410 :type '(choice (string :tag "Characters")
411 (set :tag "Syntax"
412 (const :tag "Whitespace" ?\ )
413 (const :tag "Symbol" ?_)
414 (const :tag "Opening parentheses" ?\()
415 (const :tag "Closing parentheses" ?\))
416 (const :tag "Word constituent" ?w)
417 (const :tag "Punctuation." ?.)
418 (const :tag "String quote." ?\")
419 (const :tag "Paired delimiter." ?$)
420 (const :tag "Expression quote or prefix operator." ?\')
421 (const :tag "Comment starter." ?<)
422 (const :tag "Comment ender." ?>)
423 (const :tag "Character-quote." ?/)
424 (const :tag "Generic string fence." ?|)
425 (const :tag "Generic comment fence." ?!))
426 (function :tag "Predicate function")))
427
428 (defcustom company-idle-delay .7
429 "The idle delay in seconds until automatic completions starts.
430 A value of nil means never complete automatically, t means complete
431 immediately when a prefix of `company-minimum-prefix-length' is reached."
432 :group 'company
433 :type '(choice (const :tag "never (nil)" nil)
434 (const :tag "immediate (t)" t)
435 (number :tag "seconds")))
436
437 (defcustom company-begin-commands t
438 "A list of commands following which company will start completing.
439 If this is t, it will complete after any command. See `company-idle-delay'.
440
441 Alternatively any command with a non-nil 'company-begin property is treated as
442 if it was on this list."
443 :group 'company
444 :type '(choice (const :tag "Any command" t)
445 (const :tag "Self insert command" '(self-insert-command))
446 (repeat :tag "Commands" function)))
447
448 (defcustom company-show-numbers nil
449 "If enabled, show quick-access numbers for the first ten candidates."
450 :group 'company
451 :type '(choice (const :tag "off" nil)
452 (const :tag "on" t)))
453
454 (defvar company-end-of-buffer-workaround t
455 "Work around a visualization bug when completing at the end of the buffer.
456 The work-around consists of adding a newline.")
457
458 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
459
460 (defvar company-mode-map (make-sparse-keymap)
461 "Keymap used by `company-mode'.")
462
463 (defvar company-active-map
464 (let ((keymap (make-sparse-keymap)))
465 (define-key keymap "\e\e\e" 'company-abort)
466 (define-key keymap "\C-g" 'company-abort)
467 (define-key keymap (kbd "M-n") 'company-select-next)
468 (define-key keymap (kbd "M-p") 'company-select-previous)
469 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
470 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
471 (define-key keymap [down-mouse-1] 'ignore)
472 (define-key keymap [down-mouse-3] 'ignore)
473 (define-key keymap [mouse-1] 'company-complete-mouse)
474 (define-key keymap [mouse-3] 'company-select-mouse)
475 (define-key keymap [up-mouse-1] 'ignore)
476 (define-key keymap [up-mouse-3] 'ignore)
477 (define-key keymap [return] 'company-complete-selection)
478 (define-key keymap [tab] 'company-complete-common)
479 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
480 (define-key keymap "\C-w" 'company-show-location)
481 (define-key keymap "\C-s" 'company-search-candidates)
482 (define-key keymap "\C-\M-s" 'company-filter-candidates)
483 (dotimes (i 10)
484 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
485 `(lambda () (interactive) (company-complete-number ,i))))
486
487 keymap)
488 "Keymap that is enabled during an active completion.")
489
490 (defvar company--disabled-backends nil)
491
492 (defun company-init-backend (backend)
493 (and (symbolp backend)
494 (not (fboundp backend))
495 (ignore-errors (require backend nil t)))
496
497 (if (or (symbolp backend)
498 (functionp backend))
499 (condition-case err
500 (progn
501 (funcall backend 'init)
502 (put backend 'company-init t))
503 (error
504 (put backend 'company-init 'failed)
505 (unless (memq backend company--disabled-backends)
506 (message "Company back-end '%s' could not be initialized:\n%s"
507 backend (error-message-string err)))
508 (pushnew backend company--disabled-backends)
509 nil))
510 (mapc 'company-init-backend backend)))
511
512 (defvar company-default-lighter " company")
513
514 (defvar company-lighter company-default-lighter)
515 (make-variable-buffer-local 'company-lighter)
516
517 ;;;###autoload
518 (define-minor-mode company-mode
519 "\"complete anything\"; is an in-buffer completion framework.
520 Completion starts automatically, depending on the values
521 `company-idle-delay' and `company-minimum-prefix-length'.
522
523 Completion can be controlled with the commands:
524 `company-complete-common', `company-complete-selection', `company-complete',
525 `company-select-next', `company-select-previous'. If these commands are
526 called before `company-idle-delay', completion will also start.
527
528 Completions can be searched with `company-search-candidates' or
529 `company-filter-candidates'. These can be used while completion is
530 inactive, as well.
531
532 The completion data is retrieved using `company-backends' and displayed using
533 `company-frontends'. If you want to start a specific back-end, call it
534 interactively or use `company-begin-backend'.
535
536 regular keymap (`company-mode-map'):
537
538 \\{company-mode-map}
539 keymap during active completions (`company-active-map'):
540
541 \\{company-active-map}"
542 nil company-lighter company-mode-map
543 (if company-mode
544 (progn
545 (add-hook 'pre-command-hook 'company-pre-command nil t)
546 (add-hook 'post-command-hook 'company-post-command nil t)
547 (mapc 'company-init-backend company-backends))
548 (remove-hook 'pre-command-hook 'company-pre-command t)
549 (remove-hook 'post-command-hook 'company-post-command t)
550 (company-cancel)
551 (kill-local-variable 'company-point)))
552
553 (define-globalized-minor-mode global-company-mode company-mode
554 (lambda () (unless (or noninteractive (eq (aref (buffer-name) 0) ?\s))
555 (company-mode 1))))
556
557 (defsubst company-assert-enabled ()
558 (unless company-mode
559 (company-uninstall-map)
560 (error "Company not enabled")))
561
562 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
563
564 (defvar company-my-keymap nil)
565 (make-variable-buffer-local 'company-my-keymap)
566
567 (defvar company-emulation-alist '((t . nil)))
568
569 (defsubst company-enable-overriding-keymap (keymap)
570 (company-uninstall-map)
571 (setq company-my-keymap keymap))
572
573 (defun company-ensure-emulation-alist ()
574 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
575 (setq emulation-mode-map-alists
576 (cons 'company-emulation-alist
577 (delq 'company-emulation-alist emulation-mode-map-alists)))))
578
579 (defun company-install-map ()
580 (unless (or (cdar company-emulation-alist)
581 (null company-my-keymap))
582 (setf (cdar company-emulation-alist) company-my-keymap)))
583
584 (defun company-uninstall-map ()
585 (setf (cdar company-emulation-alist) nil))
586
587 ;; Hack:
588 ;; Emacs calculates the active keymaps before reading the event. That means we
589 ;; cannot change the keymap from a timer. So we send a bogus command.
590 (defun company-ignore ()
591 (interactive)
592 (setq this-command last-command))
593
594 (global-set-key '[31415926] 'company-ignore)
595
596 (defun company-input-noop ()
597 (push 31415926 unread-command-events))
598
599 ;; Hack:
600 ;; posn-col-row is incorrect in older Emacsen when line-spacing is set
601 (defun company--col-row (&optional pos)
602 (let ((posn (posn-at-point pos)))
603 (cons (car (posn-col-row posn)) (cdr (posn-actual-col-row posn)))))
604
605 (defsubst company--column (&optional pos)
606 (car (posn-col-row (posn-at-point pos))))
607
608 (defsubst company--row (&optional pos)
609 (cdr (posn-actual-col-row (posn-at-point pos))))
610
611 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
612
613 (defun company-grab (regexp &optional expression limit)
614 (when (looking-back regexp limit)
615 (or (match-string-no-properties (or expression 0)) "")))
616
617 (defun company-grab-line (regexp &optional expression)
618 (company-grab regexp expression (point-at-bol)))
619
620 (defun company-grab-symbol ()
621 (if (looking-at "\\_>")
622 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
623 (point)))
624 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
625 "")))
626
627 (defun company-grab-word ()
628 (if (looking-at "\\>")
629 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
630 (point)))
631 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
632 "")))
633
634 (defun company-in-string-or-comment ()
635 (let ((ppss (syntax-ppss)))
636 (or (car (setq ppss (nthcdr 3 ppss)))
637 (car (setq ppss (cdr ppss)))
638 (nth 3 ppss))))
639
640 (if (fboundp 'locate-dominating-file)
641 (defalias 'company-locate-dominating-file 'locate-dominating-file)
642 (defun company-locate-dominating-file (file name)
643 (catch 'root
644 (let ((dir (file-name-directory file))
645 (prev-dir nil))
646 (while (not (equal dir prev-dir))
647 (when (file-exists-p (expand-file-name name dir))
648 (throw 'root dir))
649 (setq prev-dir dir
650 dir (file-name-directory (directory-file-name dir))))))))
651
652 (defun company-call-backend (&rest args)
653 (if (functionp company-backend)
654 (apply company-backend args)
655 (apply 'company--multi-backend-adapter company-backend args)))
656
657 (defun company--multi-backend-adapter (backends command &rest args)
658 (let ((backends (remove-if (lambda (b)
659 (and (symbolp b)
660 (eq 'failed (get b 'company-init))))
661 backends)))
662 (case command
663 (candidates
664 (loop for backend in backends
665 when (equal (funcall backend 'prefix)
666 (car args))
667 append (apply backend 'candidates args)))
668 (sorted nil)
669 (duplicates t)
670 (otherwise
671 (let (value)
672 (dolist (backend backends)
673 (when (setq value (apply backend command args))
674 (return value))))))))
675
676 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
677
678 (defvar company-backend nil)
679 (make-variable-buffer-local 'company-backend)
680
681 (defvar company-prefix nil)
682 (make-variable-buffer-local 'company-prefix)
683
684 (defvar company-candidates nil)
685 (make-variable-buffer-local 'company-candidates)
686
687 (defvar company-candidates-length nil)
688 (make-variable-buffer-local 'company-candidates-length)
689
690 (defvar company-candidates-cache nil)
691 (make-variable-buffer-local 'company-candidates-cache)
692
693 (defvar company-candidates-predicate nil)
694 (make-variable-buffer-local 'company-candidates-predicate)
695
696 (defvar company-common nil)
697 (make-variable-buffer-local 'company-common)
698
699 (defvar company-selection 0)
700 (make-variable-buffer-local 'company-selection)
701
702 (defvar company-selection-changed nil)
703 (make-variable-buffer-local 'company-selection-changed)
704
705 (defvar company--explicit-action nil
706 "Non-nil, if explicit completion took place.")
707 (make-variable-buffer-local 'company--explicit-action)
708
709 (defvar company--auto-completion nil
710 "Non-nil when current candidate is being completed automatically.
711 Controlled by `company-auto-complete'.")
712
713 (defvar company--point-max nil)
714 (make-variable-buffer-local 'company--point-max)
715
716 (defvar company-point nil)
717 (make-variable-buffer-local 'company-point)
718
719 (defvar company-timer nil)
720
721 (defvar company-added-newline nil)
722 (make-variable-buffer-local 'company-added-newline)
723
724 (defsubst company-strip-prefix (str)
725 (substring str (length company-prefix)))
726
727 (defmacro company-with-candidate-inserted (candidate &rest body)
728 "Evaluate BODY with CANDIDATE temporarily inserted.
729 This is a tool for back-ends that need candidates inserted before they
730 can retrieve meta-data for them."
731 (declare (indent 1))
732 `(let ((inhibit-modification-hooks t)
733 (inhibit-point-motion-hooks t)
734 (modified-p (buffer-modified-p)))
735 (insert (company-strip-prefix ,candidate))
736 (unwind-protect
737 (progn ,@body)
738 (delete-region company-point (point)))))
739
740 (defun company-explicit-action-p ()
741 "Return whether explicit completion action was taken by the user."
742 (or company--explicit-action
743 company-selection-changed))
744
745 (defsubst company-reformat (candidate)
746 ;; company-ispell needs this, because the results are always lower-case
747 ;; It's mory efficient to fix it only when they are displayed.
748 (concat company-prefix (substring candidate (length company-prefix))))
749
750 (defun company--should-complete ()
751 (and (not (or buffer-read-only overriding-terminal-local-map
752 overriding-local-map
753 (minibufferp)))
754 ;; Check if in the middle of entering a key combination.
755 (or (equal (this-command-keys-vector) [])
756 (not (keymapp (key-binding (this-command-keys-vector)))))
757 (eq company-idle-delay t)
758 (or (eq t company-begin-commands)
759 (memq this-command company-begin-commands)
760 (and (symbolp this-command) (get this-command 'company-begin)))
761 (not (and transient-mark-mode mark-active))))
762
763 (defsubst company-call-frontends (command)
764 (dolist (frontend company-frontends)
765 (condition-case err
766 (funcall frontend command)
767 (error (error "Company: Front-end %s error \"%s\" on command %s"
768 frontend (error-message-string err) command)))))
769
770 (defsubst company-set-selection (selection &optional force-update)
771 (setq selection (max 0 (min (1- company-candidates-length) selection)))
772 (when (or force-update (not (equal selection company-selection)))
773 (setq company-selection selection
774 company-selection-changed t)
775 (company-call-frontends 'update)))
776
777 (defun company-apply-predicate (candidates predicate)
778 (let (new)
779 (dolist (c candidates)
780 (when (funcall predicate c)
781 (push c new)))
782 (nreverse new)))
783
784 (defun company-update-candidates (candidates)
785 (setq company-candidates-length (length candidates))
786 (if (> company-selection 0)
787 ;; Try to restore the selection
788 (let ((selected (nth company-selection company-candidates)))
789 (setq company-selection 0
790 company-candidates candidates)
791 (when selected
792 (while (and candidates (string< (pop candidates) selected))
793 (incf company-selection))
794 (unless candidates
795 ;; Make sure selection isn't out of bounds.
796 (setq company-selection (min (1- company-candidates-length)
797 company-selection)))))
798 (setq company-selection 0
799 company-candidates candidates))
800 ;; Save in cache:
801 (push (cons company-prefix company-candidates) company-candidates-cache)
802 ;; Calculate common.
803 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
804 (setq company-common (company--safe-candidate
805 (try-completion company-prefix company-candidates))))
806 (when (eq company-common t)
807 (setq company-candidates nil)))
808
809 (defun company--safe-candidate (str)
810 (or (company-call-backend 'crop str)
811 str))
812
813 (defun company-calculate-candidates (prefix)
814 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
815 (ignore-case (company-call-backend 'ignore-case)))
816 (or candidates
817 (when company-candidates-cache
818 (let ((len (length prefix))
819 (completion-ignore-case ignore-case)
820 prev)
821 (dotimes (i (1+ len))
822 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
823 company-candidates-cache)))
824 (setq candidates (all-completions prefix prev))
825 (return t)))))
826 ;; no cache match, call back-end
827 (progn
828 (setq candidates (company-call-backend 'candidates prefix))
829 (when company-candidates-predicate
830 (setq candidates
831 (company-apply-predicate candidates
832 company-candidates-predicate)))
833 (unless (company-call-backend 'sorted)
834 (setq candidates (sort candidates 'string<)))
835 (when (company-call-backend 'duplicates)
836 ;; strip duplicates
837 (let ((c2 candidates))
838 (while c2
839 (setcdr c2 (progn (while (equal (pop c2) (car c2)))
840 c2)))))))
841 (if (and candidates
842 (or (cdr candidates)
843 (not (eq t (compare-strings (car candidates) nil nil
844 prefix nil nil ignore-case)))))
845 candidates
846 ;; Already completed and unique; don't start.
847 ;; FIXME: Not the right place? maybe when setting?
848 (and company-candidates t))))
849
850 (defun company-idle-begin (buf win tick pos)
851 (and company-mode
852 (eq buf (current-buffer))
853 (eq win (selected-window))
854 (eq tick (buffer-chars-modified-tick))
855 (eq pos (point))
856 (not company-candidates)
857 (not (equal (point) company-point))
858 (let ((company-idle-delay t)
859 (company-begin-commands t))
860 (company-begin)
861 (when company-candidates
862 (company-input-noop)
863 (company-post-command)))))
864
865 (defun company-auto-begin ()
866 (company-assert-enabled)
867 (and company-mode
868 (not company-candidates)
869 (let ((company-idle-delay t)
870 (company-minimum-prefix-length 0)
871 (company-begin-commands t))
872 (company-begin)))
873 ;; Return non-nil if active.
874 company-candidates)
875
876 (defun company-manual-begin ()
877 (interactive)
878 (setq company--explicit-action t)
879 (company-auto-begin))
880
881 (defun company-other-backend (&optional backward)
882 (interactive (list current-prefix-arg))
883 (company-assert-enabled)
884 (if company-backend
885 (let* ((after (cdr (member company-backend company-backends)))
886 (before (cdr (member company-backend (reverse company-backends))))
887 (next (if backward
888 (append before (reverse after))
889 (append after (reverse before)))))
890 (company-cancel)
891 (dolist (backend next)
892 (when (ignore-errors (company-begin-backend backend))
893 (return t))))
894 (company-manual-begin))
895 (unless company-candidates
896 (error "No other back-end")))
897
898 (defun company-require-match-p ()
899 (let ((backend-value (company-call-backend 'require-match)))
900 (or (eq backend-value t)
901 (and (if (functionp company-require-match)
902 (funcall company-require-match)
903 (eq company-require-match t))
904 (not (eq backend-value 'never))))))
905
906 (defun company-punctuation-p (input)
907 "Return non-nil, if input starts with punctuation or parentheses."
908 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
909
910 (defun company-auto-complete-p (input)
911 "Return non-nil, if input starts with punctuation or parentheses."
912 (and (if (functionp company-auto-complete)
913 (funcall company-auto-complete)
914 company-auto-complete)
915 (if (functionp company-auto-complete-chars)
916 (funcall company-auto-complete-chars input)
917 (if (consp company-auto-complete-chars)
918 (memq (char-syntax (string-to-char input))
919 company-auto-complete-chars)
920 (string-match (substring input 0 1) company-auto-complete-chars)))))
921
922 (defun company--incremental-p ()
923 (and (> (point) company-point)
924 (> (point-max) company--point-max)
925 (not (eq this-command 'backward-delete-char-untabify))
926 (equal (buffer-substring (- company-point (length company-prefix))
927 company-point)
928 company-prefix)))
929
930 (defsubst company--string-incremental-p (old-prefix new-prefix)
931 (and (> (length new-prefix) (length old-prefix))
932 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
933
934 (defun company--continue-failed (new-prefix)
935 (when (company--incremental-p)
936 (let ((input (buffer-substring-no-properties (point) company-point)))
937 (cond
938 ((company-auto-complete-p input)
939 ;; auto-complete
940 (save-excursion
941 (goto-char company-point)
942 (let ((company--auto-completion t))
943 (company-complete-selection))
944 nil))
945 ((and (company--string-incremental-p company-prefix new-prefix)
946 (company-require-match-p))
947 ;; wrong incremental input, but required match
948 (backward-delete-char (length input))
949 (ding)
950 (message "Matching input is required")
951 company-candidates)
952 ((equal company-prefix (car company-candidates))
953 ;; last input was actually success
954 (company-cancel company-prefix)
955 nil)))))
956
957 (defun company--good-prefix-p (prefix)
958 (and (or (company-explicit-action-p)
959 (unless (eq prefix 'stop)
960 (>= (or (cdr-safe prefix) (length prefix))
961 company-minimum-prefix-length)))
962 (stringp (or (car-safe prefix) prefix))))
963
964 (defun company--continue ()
965 (when (company-call-backend 'no-cache company-prefix)
966 ;; Don't complete existing candidates, fetch new ones.
967 (setq company-candidates-cache nil))
968 (let* ((new-prefix (company-call-backend 'prefix))
969 (c (when (and (company--good-prefix-p new-prefix)
970 (setq new-prefix (or (car-safe new-prefix) new-prefix))
971 (= (- (point) (length new-prefix))
972 (- company-point (length company-prefix))))
973 (setq new-prefix (or (car-safe new-prefix) new-prefix))
974 (company-calculate-candidates new-prefix))))
975 (or (cond
976 ((eq c t)
977 ;; t means complete/unique.
978 (company-cancel new-prefix)
979 nil)
980 ((consp c)
981 ;; incremental match
982 (setq company-prefix new-prefix)
983 (company-update-candidates c)
984 c)
985 (t (company--continue-failed new-prefix)))
986 (company-cancel))))
987
988 (defun company--begin-new ()
989 (let (prefix c)
990 (dolist (backend (if company-backend
991 ;; prefer manual override
992 (list company-backend)
993 company-backends))
994 (setq prefix
995 (if (or (symbolp backend)
996 (functionp backend))
997 (when (or (not (symbolp backend))
998 (eq t (get backend 'company-init))
999 (unless (get backend 'company-init)
1000 (company-init-backend backend)))
1001 (funcall backend 'prefix))
1002 (company--multi-backend-adapter backend 'prefix)))
1003 (when prefix
1004 (when (company--good-prefix-p prefix)
1005 (setq prefix (or (car-safe prefix) prefix)
1006 company-backend backend
1007 c (company-calculate-candidates prefix))
1008 ;; t means complete/unique. We don't start, so no hooks.
1009 (if (not (consp c))
1010 (when company--explicit-action
1011 (message "No completion found"))
1012 (setq company-prefix prefix)
1013 (when (symbolp backend)
1014 (setq company-lighter (concat " " (symbol-name backend))))
1015 (company-update-candidates c)
1016 (run-hook-with-args 'company-completion-started-hook
1017 (company-explicit-action-p))
1018 (company-call-frontends 'show)))
1019 (return c)))))
1020
1021 (defun company-begin ()
1022 (or (and company-candidates (company--continue))
1023 (and (company--should-complete) (company--begin-new)))
1024 (when company-candidates
1025 (when (and company-end-of-buffer-workaround (eobp))
1026 (save-excursion (insert "\n"))
1027 (setq company-added-newline (buffer-chars-modified-tick)))
1028 (setq company-point (point)
1029 company--point-max (point-max))
1030 (company-ensure-emulation-alist)
1031 (company-enable-overriding-keymap company-active-map)
1032 (company-call-frontends 'update)))
1033
1034 (defun company-cancel (&optional result)
1035 (and company-added-newline
1036 (> (point-max) (point-min))
1037 (let ((tick (buffer-chars-modified-tick)))
1038 (delete-region (1- (point-max)) (point-max))
1039 (equal tick company-added-newline))
1040 ;; Only set unmodified when tick remained the same since insert.
1041 (set-buffer-modified-p nil))
1042 (when company-prefix
1043 (if (stringp result)
1044 (progn
1045 (company-call-backend 'pre-completion result)
1046 (run-hook-with-args 'company-completion-finished-hook result)
1047 (company-call-backend 'post-completion result))
1048 (run-hook-with-args 'company-completion-cancelled-hook result)))
1049 (setq company-added-newline nil
1050 company-backend nil
1051 company-prefix nil
1052 company-candidates nil
1053 company-candidates-length nil
1054 company-candidates-cache nil
1055 company-candidates-predicate nil
1056 company-common nil
1057 company-selection 0
1058 company-selection-changed nil
1059 company--explicit-action nil
1060 company-lighter company-default-lighter
1061 company--point-max nil
1062 company-point nil)
1063 (when company-timer
1064 (cancel-timer company-timer))
1065 (company-search-mode 0)
1066 (company-call-frontends 'hide)
1067 (company-enable-overriding-keymap nil))
1068
1069 (defun company-abort ()
1070 (interactive)
1071 (company-cancel t)
1072 ;; Don't start again, unless started manually.
1073 (setq company-point (point)))
1074
1075 (defun company-finish (result)
1076 (insert (company-strip-prefix result))
1077 (company-cancel result)
1078 ;; Don't start again, unless started manually.
1079 (setq company-point (point)))
1080
1081 (defsubst company-keep (command)
1082 (and (symbolp command) (get command 'company-keep)))
1083
1084 (defun company-pre-command ()
1085 (unless (company-keep this-command)
1086 (condition-case err
1087 (when company-candidates
1088 (company-call-frontends 'pre-command))
1089 (error (message "Company: An error occurred in pre-command")
1090 (message "%s" (error-message-string err))
1091 (company-cancel))))
1092 (when company-timer
1093 (cancel-timer company-timer)
1094 (setq company-timer nil))
1095 (company-uninstall-map))
1096
1097 (defun company-post-command ()
1098 (unless (company-keep this-command)
1099 (condition-case err
1100 (progn
1101 (unless (equal (point) company-point)
1102 (company-begin))
1103 (if company-candidates
1104 (company-call-frontends 'post-command)
1105 (and (numberp company-idle-delay)
1106 (or (eq t company-begin-commands)
1107 (memq this-command company-begin-commands))
1108 (setq company-timer
1109 (run-with-timer company-idle-delay nil
1110 'company-idle-begin
1111 (current-buffer) (selected-window)
1112 (buffer-chars-modified-tick) (point))))))
1113 (error (message "Company: An error occurred in post-command")
1114 (message "%s" (error-message-string err))
1115 (company-cancel))))
1116 (company-install-map))
1117
1118 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1119
1120 (defvar company-search-string nil)
1121 (make-variable-buffer-local 'company-search-string)
1122
1123 (defvar company-search-lighter " Search: \"\"")
1124 (make-variable-buffer-local 'company-search-lighter)
1125
1126 (defvar company-search-old-map nil)
1127 (make-variable-buffer-local 'company-search-old-map)
1128
1129 (defvar company-search-old-selection 0)
1130 (make-variable-buffer-local 'company-search-old-selection)
1131
1132 (defun company-search (text lines)
1133 (let ((quoted (regexp-quote text))
1134 (i 0))
1135 (dolist (line lines)
1136 (when (string-match quoted line (length company-prefix))
1137 (return i))
1138 (incf i))))
1139
1140 (defun company-search-printing-char ()
1141 (interactive)
1142 (company-search-assert-enabled)
1143 (setq company-search-string
1144 (concat (or company-search-string "") (string last-command-event))
1145 company-search-lighter (concat " Search: \"" company-search-string
1146 "\""))
1147 (let ((pos (company-search company-search-string
1148 (nthcdr company-selection company-candidates))))
1149 (if (null pos)
1150 (ding)
1151 (company-set-selection (+ company-selection pos) t))))
1152
1153 (defun company-search-repeat-forward ()
1154 "Repeat the incremental search in completion candidates forward."
1155 (interactive)
1156 (company-search-assert-enabled)
1157 (let ((pos (company-search company-search-string
1158 (cdr (nthcdr company-selection
1159 company-candidates)))))
1160 (if (null pos)
1161 (ding)
1162 (company-set-selection (+ company-selection pos 1) t))))
1163
1164 (defun company-search-repeat-backward ()
1165 "Repeat the incremental search in completion candidates backwards."
1166 (interactive)
1167 (company-search-assert-enabled)
1168 (let ((pos (company-search company-search-string
1169 (nthcdr (- company-candidates-length
1170 company-selection)
1171 (reverse company-candidates)))))
1172 (if (null pos)
1173 (ding)
1174 (company-set-selection (- company-selection pos 1) t))))
1175
1176 (defun company-create-match-predicate ()
1177 (setq company-candidates-predicate
1178 `(lambda (candidate)
1179 ,(if company-candidates-predicate
1180 `(and (string-match ,company-search-string candidate)
1181 (funcall ,company-candidates-predicate
1182 candidate))
1183 `(string-match ,company-search-string candidate))))
1184 (company-update-candidates
1185 (company-apply-predicate company-candidates company-candidates-predicate))
1186 ;; Invalidate cache.
1187 (setq company-candidates-cache (cons company-prefix company-candidates)))
1188
1189 (defun company-filter-printing-char ()
1190 (interactive)
1191 (company-search-assert-enabled)
1192 (company-search-printing-char)
1193 (company-create-match-predicate)
1194 (company-call-frontends 'update))
1195
1196 (defun company-search-kill-others ()
1197 "Limit the completion candidates to the ones matching the search string."
1198 (interactive)
1199 (company-search-assert-enabled)
1200 (company-create-match-predicate)
1201 (company-search-mode 0)
1202 (company-call-frontends 'update))
1203
1204 (defun company-search-abort ()
1205 "Abort searching the completion candidates."
1206 (interactive)
1207 (company-search-assert-enabled)
1208 (company-set-selection company-search-old-selection t)
1209 (company-search-mode 0))
1210
1211 (defun company-search-other-char ()
1212 (interactive)
1213 (company-search-assert-enabled)
1214 (company-search-mode 0)
1215 (company--unread-last-input))
1216
1217 (defvar company-search-map
1218 (let ((i 0)
1219 (keymap (make-keymap)))
1220 (if (fboundp 'max-char)
1221 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1222 'company-search-printing-char)
1223 (with-no-warnings
1224 ;; obsolete in Emacs 23
1225 (let ((l (generic-character-list))
1226 (table (nth 1 keymap)))
1227 (while l
1228 (set-char-table-default table (car l) 'company-search-printing-char)
1229 (setq l (cdr l))))))
1230 (define-key keymap [t] 'company-search-other-char)
1231 (while (< i ?\s)
1232 (define-key keymap (make-string 1 i) 'company-search-other-char)
1233 (incf i))
1234 (while (< i 256)
1235 (define-key keymap (vector i) 'company-search-printing-char)
1236 (incf i))
1237 (let ((meta-map (make-sparse-keymap)))
1238 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1239 (define-key keymap [escape] meta-map))
1240 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1241 (define-key keymap "\e\e\e" 'company-search-other-char)
1242 (define-key keymap [escape escape escape] 'company-search-other-char)
1243
1244 (define-key keymap "\C-g" 'company-search-abort)
1245 (define-key keymap "\C-s" 'company-search-repeat-forward)
1246 (define-key keymap "\C-r" 'company-search-repeat-backward)
1247 (define-key keymap "\C-o" 'company-search-kill-others)
1248 keymap)
1249 "Keymap used for incrementally searching the completion candidates.")
1250
1251 (define-minor-mode company-search-mode
1252 "Search mode for completion candidates.
1253 Don't start this directly, use `company-search-candidates' or
1254 `company-filter-candidates'."
1255 nil company-search-lighter nil
1256 (if company-search-mode
1257 (if (company-manual-begin)
1258 (progn
1259 (setq company-search-old-selection company-selection)
1260 (company-call-frontends 'update))
1261 (setq company-search-mode nil))
1262 (kill-local-variable 'company-search-string)
1263 (kill-local-variable 'company-search-lighter)
1264 (kill-local-variable 'company-search-old-selection)
1265 (company-enable-overriding-keymap company-active-map)))
1266
1267 (defsubst company-search-assert-enabled ()
1268 (company-assert-enabled)
1269 (unless company-search-mode
1270 (company-uninstall-map)
1271 (error "Company not in search mode")))
1272
1273 (defun company-search-candidates ()
1274 "Start searching the completion candidates incrementally.
1275
1276 \\<company-search-map>Search can be controlled with the commands:
1277 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1278 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1279 - `company-search-abort' (\\[company-search-abort])
1280
1281 Regular characters are appended to the search string.
1282
1283 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
1284 the search string to limit the completion candidates."
1285 (interactive)
1286 (company-search-mode 1)
1287 (company-enable-overriding-keymap company-search-map))
1288
1289 (defvar company-filter-map
1290 (let ((keymap (make-keymap)))
1291 (define-key keymap [remap company-search-printing-char]
1292 'company-filter-printing-char)
1293 (set-keymap-parent keymap company-search-map)
1294 keymap)
1295 "Keymap used for incrementally searching the completion candidates.")
1296
1297 (defun company-filter-candidates ()
1298 "Start filtering the completion candidates incrementally.
1299 This works the same way as `company-search-candidates' immediately
1300 followed by `company-search-kill-others' after each input."
1301 (interactive)
1302 (company-search-mode 1)
1303 (company-enable-overriding-keymap company-filter-map))
1304
1305 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1306
1307 (defun company-select-next ()
1308 "Select the next candidate in the list."
1309 (interactive)
1310 (when (company-manual-begin)
1311 (company-set-selection (1+ company-selection))))
1312
1313 (defun company-select-previous ()
1314 "Select the previous candidate in the list."
1315 (interactive)
1316 (when (company-manual-begin)
1317 (company-set-selection (1- company-selection))))
1318
1319 (defun company-select-next-or-abort ()
1320 "Select the next candidate if more than one, else abort
1321 and invoke the normal binding."
1322 (interactive)
1323 (if (> company-candidates-length 1)
1324 (company-select-next)
1325 (company-abort)
1326 (company--unread-last-input)))
1327
1328 (defun company-select-previous-or-abort ()
1329 "Select the previous candidate if more than one, else abort
1330 and invoke the normal binding."
1331 (interactive)
1332 (if (> company-candidates-length 1)
1333 (company-select-previous)
1334 (company-abort)
1335 (company--unread-last-input)))
1336
1337 (defun company-select-mouse (event)
1338 "Select the candidate picked by the mouse."
1339 (interactive "e")
1340 (when (nth 4 (event-start event))
1341 (company-set-selection (- (cdr (posn-actual-col-row (event-start event)))
1342 (company--row)
1343 1))
1344 t))
1345
1346 (defun company-complete-mouse (event)
1347 "Complete the candidate picked by the mouse."
1348 (interactive "e")
1349 (when (company-select-mouse event)
1350 (company-complete-selection)))
1351
1352 (defun company-complete-selection ()
1353 "Complete the selected candidate."
1354 (interactive)
1355 (when (company-manual-begin)
1356 (let ((result (nth company-selection company-candidates)))
1357 (when company--auto-completion
1358 (setq result (company--safe-candidate result)))
1359 (company-finish result))))
1360
1361 (defun company-complete-common ()
1362 "Complete the common part of all candidates."
1363 (interactive)
1364 (when (company-manual-begin)
1365 (if (and (not (cdr company-candidates))
1366 (equal company-common (car company-candidates)))
1367 (company-complete-selection)
1368 (insert (company-strip-prefix company-common)))))
1369
1370 (defun company-complete ()
1371 "Complete the common part of all candidates or the current selection.
1372 The first time this is called, the common part is completed, the second time, or
1373 when the selection has been changed, the selected candidate is completed."
1374 (interactive)
1375 (when (company-manual-begin)
1376 (if (or company-selection-changed
1377 (eq last-command 'company-complete-common))
1378 (call-interactively 'company-complete-selection)
1379 (call-interactively 'company-complete-common)
1380 (setq this-command 'company-complete-common))))
1381
1382 (defun company-complete-number (n)
1383 "Complete the Nth candidate.
1384 To show the number next to the candidates in some back-ends, enable
1385 `company-show-numbers'."
1386 (when (company-manual-begin)
1387 (and (< n 1) (> n company-candidates-length)
1388 (error "No candidate number %d" n))
1389 (decf n)
1390 (company-finish (nth n company-candidates))))
1391
1392 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1393
1394 (defconst company-space-strings-limit 100)
1395
1396 (defconst company-space-strings
1397 (let (lst)
1398 (dotimes (i company-space-strings-limit)
1399 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1400 (apply 'vector lst)))
1401
1402 (defsubst company-space-string (len)
1403 (if (< len company-space-strings-limit)
1404 (aref company-space-strings len)
1405 (make-string len ?\ )))
1406
1407 (defsubst company-safe-substring (str from &optional to)
1408 (if (> from (string-width str))
1409 ""
1410 (with-temp-buffer
1411 (insert str)
1412 (move-to-column from)
1413 (let ((beg (point)))
1414 (if to
1415 (progn
1416 (move-to-column to)
1417 (concat (buffer-substring beg (point))
1418 (let ((padding (- to (current-column))))
1419 (when (> padding 0)
1420 (company-space-string padding)))))
1421 (buffer-substring beg (point-max)))))))
1422
1423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1424
1425 (defvar company-last-metadata nil)
1426 (make-variable-buffer-local 'company-last-metadata)
1427
1428 (defun company-fetch-metadata ()
1429 (let ((selected (nth company-selection company-candidates)))
1430 (unless (equal selected (car company-last-metadata))
1431 (setq company-last-metadata
1432 (cons selected (company-call-backend 'meta selected))))
1433 (cdr company-last-metadata)))
1434
1435 (defun company-doc-buffer (&optional string)
1436 (with-current-buffer (get-buffer-create "*Company meta-data*")
1437 (erase-buffer)
1438 (current-buffer)))
1439
1440 (defvar company--electric-commands
1441 '(scroll-other-window scroll-other-window-down)
1442 "List of Commands that won't break out of electric commands.")
1443
1444 (defmacro company--electric-do (&rest body)
1445 (declare (indent 0) (debug t))
1446 `(when (company-manual-begin)
1447 (save-window-excursion
1448 (let ((height (window-height))
1449 (row (company--row))
1450 cmd)
1451 ,@body
1452 (and (< (window-height) height)
1453 (< (- (window-height) row 2) company-tooltip-limit)
1454 (recenter (- (window-height) row 2)))
1455 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1456 company--electric-commands)
1457 (call-interactively cmd))
1458 (company--unread-last-input)))))
1459
1460 (defun company--unread-last-input ()
1461 (when last-input-event
1462 (clear-this-command-keys t)
1463 (setq unread-command-events (list last-input-event))))
1464
1465 (defun company-show-doc-buffer ()
1466 "Temporarily show a buffer with the complete documentation for the selection."
1467 (interactive)
1468 (company--electric-do
1469 (let* ((selected (nth company-selection company-candidates))
1470 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1471 (error "No documentation available"))))
1472 (with-current-buffer doc-buffer
1473 (goto-char (point-min)))
1474 (display-buffer doc-buffer t))))
1475 (put 'company-show-doc-buffer 'company-keep t)
1476
1477 (defun company-show-location ()
1478 "Temporarily display a buffer showing the selected candidate in context."
1479 (interactive)
1480 (company--electric-do
1481 (let* ((selected (nth company-selection company-candidates))
1482 (location (company-call-backend 'location selected))
1483 (pos (or (cdr location) (error "No location available")))
1484 (buffer (or (and (bufferp (car location)) (car location))
1485 (find-file-noselect (car location) t))))
1486 (with-selected-window (display-buffer buffer t)
1487 (save-restriction
1488 (widen)
1489 (if (bufferp (car location))
1490 (goto-char pos)
1491 (goto-char (point-min))
1492 (forward-line (1- pos))))
1493 (set-window-start nil (point))))))
1494 (put 'company-show-location 'company-keep t)
1495
1496 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1497
1498 (defvar company-callback nil)
1499 (make-variable-buffer-local 'company-callback)
1500
1501 (defvar company-begin-with-marker nil)
1502 (make-variable-buffer-local 'company-begin-with-marker)
1503
1504 (defun company-remove-callback (&optional ignored)
1505 (remove-hook 'company-completion-finished-hook company-callback t)
1506 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1507 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1508 (when company-begin-with-marker
1509 (set-marker company-begin-with-marker nil)))
1510
1511 (defun company-begin-backend (backend &optional callback)
1512 "Start a completion at point using BACKEND."
1513 (interactive (let ((val (completing-read "Company back-end: "
1514 obarray
1515 'functionp nil "company-")))
1516 (when val
1517 (list (intern val)))))
1518 (when (setq company-callback callback)
1519 (add-hook 'company-completion-finished-hook company-callback nil t))
1520 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1521 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1522 (setq company-backend backend)
1523 ;; Return non-nil if active.
1524 (or (company-manual-begin)
1525 (progn
1526 (setq company-backend nil)
1527 (error "Cannot complete at point"))))
1528
1529 (defun company-begin-with (candidates
1530 &optional prefix-length require-match callback)
1531 "Start a completion at point.
1532 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
1533 the prefix that already is in the buffer before point. It defaults to 0.
1534
1535 CALLBACK is a function called with the selected result if the user successfully
1536 completes the input.
1537
1538 Example:
1539 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1540 (setq company-begin-with-marker (copy-marker (point) t))
1541 (company-begin-backend
1542 `(lambda (command &optional arg &rest ignored)
1543 (cond
1544 ((eq command 'prefix)
1545 (when (equal (point) (marker-position company-begin-with-marker))
1546 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1547 ((eq command 'candidates)
1548 (all-completions arg ',candidates))
1549 ((eq command 'require-match)
1550 ,require-match)))
1551 callback))
1552
1553 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1554
1555 (defvar company-pseudo-tooltip-overlay nil)
1556 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1557
1558 (defvar company-tooltip-offset 0)
1559 (make-variable-buffer-local 'company-tooltip-offset)
1560
1561 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1562
1563 (decf limit 2)
1564 (setq company-tooltip-offset
1565 (max (min selection company-tooltip-offset)
1566 (- selection -1 limit)))
1567
1568 (when (<= company-tooltip-offset 1)
1569 (incf limit)
1570 (setq company-tooltip-offset 0))
1571
1572 (when (>= company-tooltip-offset (- num-lines limit 1))
1573 (incf limit)
1574 (when (= selection (1- num-lines))
1575 (decf company-tooltip-offset)
1576 (when (<= company-tooltip-offset 1)
1577 (setq company-tooltip-offset 0)
1578 (incf limit))))
1579
1580 limit)
1581
1582 ;;; propertize
1583
1584 (defsubst company-round-tab (arg)
1585 (* (/ (+ arg tab-width) tab-width) tab-width))
1586
1587 (defun company-untabify (str)
1588 (let* ((pieces (split-string str "\t"))
1589 (copy pieces))
1590 (while (cdr copy)
1591 (setcar copy (company-safe-substring
1592 (car copy) 0 (company-round-tab (string-width (car copy)))))
1593 (pop copy))
1594 (apply 'concat pieces)))
1595
1596 (defun company-fill-propertize (line width selected)
1597 (setq line (company-safe-substring line 0 width))
1598 (add-text-properties 0 width '(face company-tooltip
1599 mouse-face company-tooltip-mouse)
1600 line)
1601 (add-text-properties 0 (length company-common)
1602 '(face company-tooltip-common
1603 mouse-face company-tooltip-mouse)
1604 line)
1605 (when selected
1606 (if (and company-search-string
1607 (string-match (regexp-quote company-search-string) line
1608 (length company-prefix)))
1609 (progn
1610 (add-text-properties (match-beginning 0) (match-end 0)
1611 '(face company-tooltip-selection)
1612 line)
1613 (when (< (match-beginning 0) (length company-common))
1614 (add-text-properties (match-beginning 0) (length company-common)
1615 '(face company-tooltip-common-selection)
1616 line)))
1617 (add-text-properties 0 width '(face company-tooltip-selection
1618 mouse-face company-tooltip-selection)
1619 line)
1620 (add-text-properties 0 (length company-common)
1621 '(face company-tooltip-common-selection
1622 mouse-face company-tooltip-selection)
1623 line)))
1624 line)
1625
1626 ;;; replace
1627
1628 (defun company-buffer-lines (beg end)
1629 (goto-char beg)
1630 (let (lines)
1631 (while (and (= 1 (vertical-motion 1))
1632 (<= (point) end))
1633 (push (buffer-substring beg (min end (1- (point)))) lines)
1634 (setq beg (point)))
1635 (unless (eq beg end)
1636 (push (buffer-substring beg end) lines))
1637 (nreverse lines)))
1638
1639 (defsubst company-modify-line (old new offset)
1640 (concat (company-safe-substring old 0 offset)
1641 new
1642 (company-safe-substring old (+ offset (length new)))))
1643
1644 (defsubst company--length-limit (lst limit)
1645 (if (nthcdr limit lst)
1646 limit
1647 (length lst)))
1648
1649 (defun company--replacement-string (lines old column nl &optional align-top)
1650
1651 (let ((width (length (car lines))))
1652 (when (> width (- (window-width) column))
1653 (setq column (max 0 (- (window-width) width)))))
1654
1655 (let (new)
1656 (when align-top
1657 ;; untouched lines first
1658 (dotimes (i (- (length old) (length lines)))
1659 (push (pop old) new)))
1660 ;; length into old lines.
1661 (while old
1662 (push (company-modify-line (pop old) (pop lines) column) new))
1663 ;; Append whole new lines.
1664 (while lines
1665 (push (concat (company-space-string column) (pop lines)) new))
1666
1667 (let ((str (concat (when nl "\n")
1668 (mapconcat 'identity (nreverse new) "\n")
1669 "\n")))
1670 (font-lock-append-text-property 0 (length str) 'face 'default str)
1671 str)))
1672
1673 (defun company--create-lines (selection limit)
1674
1675 (let ((len company-candidates-length)
1676 (numbered 99999)
1677 lines
1678 width
1679 lines-copy
1680 previous
1681 remainder
1682 new)
1683
1684 ;; Scroll to offset.
1685 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1686
1687 (when (> company-tooltip-offset 0)
1688 (setq previous (format "...(%d)" company-tooltip-offset)))
1689
1690 (setq remainder (- len limit company-tooltip-offset)
1691 remainder (when (> remainder 0)
1692 (setq remainder (format "...(%d)" remainder))))
1693
1694 (decf selection company-tooltip-offset)
1695 (setq width (max (length previous) (length remainder))
1696 lines (nthcdr company-tooltip-offset company-candidates)
1697 len (min limit len)
1698 lines-copy lines)
1699
1700 (dotimes (i len)
1701 (setq width (max (length (pop lines-copy)) width)))
1702 (setq width (min width (window-width)))
1703
1704 (setq lines-copy lines)
1705
1706 ;; number can make tooltip too long
1707 (when company-show-numbers
1708 (setq numbered company-tooltip-offset))
1709
1710 (when previous
1711 (push (propertize (company-safe-substring previous 0 width)
1712 'face 'company-tooltip)
1713 new))
1714
1715 (dotimes (i len)
1716 (push (company-fill-propertize
1717 (if (>= numbered 10)
1718 (company-reformat (pop lines))
1719 (incf numbered)
1720 (format "%s %d"
1721 (company-safe-substring (company-reformat (pop lines))
1722 0 (- width 2))
1723 (mod numbered 10)))
1724 width (equal i selection))
1725 new))
1726
1727 (when remainder
1728 (push (propertize (company-safe-substring remainder 0 width)
1729 'face 'company-tooltip)
1730 new))
1731
1732 (setq lines (nreverse new))))
1733
1734 ;; show
1735
1736 (defsubst company--window-inner-height ()
1737 (let ((edges (window-inside-edges (selected-window))))
1738 (- (nth 3 edges) (nth 1 edges))))
1739
1740 (defsubst company--pseudo-tooltip-height ()
1741 "Calculate the appropriate tooltip height.
1742 Returns a negative number if the tooltip should be displayed above point."
1743 (let* ((lines (company--row))
1744 (below (- (company--window-inner-height) 1 lines)))
1745 (if (and (< below (min company-tooltip-minimum company-candidates-length))
1746 (> lines below))
1747 (- (max 3 (min company-tooltip-limit lines)))
1748 (max 3 (min company-tooltip-limit below)))))
1749
1750 (defun company-pseudo-tooltip-show (row column selection)
1751 (company-pseudo-tooltip-hide)
1752 (save-excursion
1753
1754 (move-to-column 0)
1755
1756 (let* ((height (company--pseudo-tooltip-height))
1757 above)
1758
1759 (when (< height 0)
1760 (setq row (+ row height -1)
1761 above t))
1762
1763 (let* ((nl (< (move-to-window-line row) row))
1764 (beg (point))
1765 (end (save-excursion
1766 (move-to-window-line (+ row (abs height)))
1767 (point)))
1768 (ov (make-overlay beg end))
1769 (args (list (mapcar 'company-untabify
1770 (company-buffer-lines beg end))
1771 column nl above)))
1772
1773 (setq company-pseudo-tooltip-overlay ov)
1774 (overlay-put ov 'company-replacement-args args)
1775 (overlay-put ov 'company-before
1776 (apply 'company--replacement-string
1777 (company--create-lines selection (abs height))
1778 args))
1779
1780 (overlay-put ov 'company-column column)
1781 (overlay-put ov 'company-height (abs height))))))
1782
1783 (defun company-pseudo-tooltip-show-at-point (pos)
1784 (let ((col-row (company--col-row pos)))
1785 (when col-row
1786 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
1787 company-selection))))
1788
1789 (defun company-pseudo-tooltip-edit (lines selection)
1790 (let ((column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1791 (height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
1792 (overlay-put company-pseudo-tooltip-overlay 'company-before
1793 (apply 'company--replacement-string
1794 (company--create-lines selection height)
1795 (overlay-get company-pseudo-tooltip-overlay
1796 'company-replacement-args)))))
1797
1798 (defun company-pseudo-tooltip-hide ()
1799 (when company-pseudo-tooltip-overlay
1800 (delete-overlay company-pseudo-tooltip-overlay)
1801 (setq company-pseudo-tooltip-overlay nil)))
1802
1803 (defun company-pseudo-tooltip-hide-temporarily ()
1804 (when (overlayp company-pseudo-tooltip-overlay)
1805 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1806 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1807
1808 (defun company-pseudo-tooltip-unhide ()
1809 (when company-pseudo-tooltip-overlay
1810 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1811 (overlay-put company-pseudo-tooltip-overlay 'before-string
1812 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1813 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1814
1815 (defun company-pseudo-tooltip-frontend (command)
1816 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1817 (case command
1818 (pre-command (company-pseudo-tooltip-hide-temporarily))
1819 (post-command
1820 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
1821 (overlay-get company-pseudo-tooltip-overlay
1822 'company-height)
1823 0))
1824 (new-height (company--pseudo-tooltip-height)))
1825 (unless (and (>= (* old-height new-height) 0)
1826 (>= (abs old-height) (abs new-height)))
1827 ;; Redraw needed.
1828 (company-pseudo-tooltip-show-at-point (- (point)
1829 (length company-prefix)))))
1830 (company-pseudo-tooltip-unhide))
1831 (hide (company-pseudo-tooltip-hide)
1832 (setq company-tooltip-offset 0))
1833 (update (when (overlayp company-pseudo-tooltip-overlay)
1834 (company-pseudo-tooltip-edit company-candidates
1835 company-selection)))))
1836
1837 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1838 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1839 (unless (and (eq command 'post-command)
1840 (not (cdr company-candidates)))
1841 (company-pseudo-tooltip-frontend command)))
1842
1843 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1844
1845 (defvar company-preview-overlay nil)
1846 (make-variable-buffer-local 'company-preview-overlay)
1847
1848 (defun company-preview-show-at-point (pos)
1849 (company-preview-hide)
1850
1851 (setq company-preview-overlay (make-overlay pos pos))
1852
1853 (let ((completion(nth company-selection company-candidates)))
1854 (setq completion (propertize completion 'face 'company-preview))
1855 (add-text-properties 0 (length company-common)
1856 '(face company-preview-common) completion)
1857
1858 ;; Add search string
1859 (and company-search-string
1860 (string-match (regexp-quote company-search-string) completion)
1861 (add-text-properties (match-beginning 0)
1862 (match-end 0)
1863 '(face company-preview-search)
1864 completion))
1865
1866 (setq completion (company-strip-prefix completion))
1867
1868 (and (equal pos (point))
1869 (not (equal completion ""))
1870 (add-text-properties 0 1 '(cursor t) completion))
1871
1872 (overlay-put company-preview-overlay 'after-string completion)
1873 (overlay-put company-preview-overlay 'window (selected-window))))
1874
1875 (defun company-preview-hide ()
1876 (when company-preview-overlay
1877 (delete-overlay company-preview-overlay)
1878 (setq company-preview-overlay nil)))
1879
1880 (defun company-preview-frontend (command)
1881 "A `company-mode' front-end showing the selection as if it had been inserted."
1882 (case command
1883 (pre-command (company-preview-hide))
1884 (post-command (company-preview-show-at-point (point)))
1885 (hide (company-preview-hide))))
1886
1887 (defun company-preview-if-just-one-frontend (command)
1888 "`company-preview-frontend', but only shown for single candidates."
1889 (unless (and (eq command 'post-command)
1890 (cdr company-candidates))
1891 (company-preview-frontend command)))
1892
1893 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1894
1895 (defvar company-echo-last-msg nil)
1896 (make-variable-buffer-local 'company-echo-last-msg)
1897
1898 (defvar company-echo-timer nil)
1899
1900 (defvar company-echo-delay .01)
1901
1902 (defun company-echo-show (&optional getter)
1903 (when getter
1904 (setq company-echo-last-msg (funcall getter)))
1905 (let ((message-log-max nil))
1906 (if company-echo-last-msg
1907 (message "%s" company-echo-last-msg)
1908 (message ""))))
1909
1910 (defsubst company-echo-show-soon (&optional getter)
1911 (when company-echo-timer
1912 (cancel-timer company-echo-timer))
1913 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
1914
1915 (defsubst company-echo-show-when-idle (&optional getter)
1916 (when (sit-for .01)
1917 (company-echo-show getter)))
1918
1919 (defsubst company-echo-show-when-not-busy (&optional getter)
1920 "Run `company-echo-show' with arg GETTER once Emacs isn't busy."
1921 (when (sit-for company-echo-delay)
1922 (company-echo-show getter)))
1923
1924 (defun company-echo-format ()
1925
1926 (let ((limit (window-width (minibuffer-window)))
1927 (len -1)
1928 ;; Roll to selection.
1929 (candidates (nthcdr company-selection company-candidates))
1930 (i (if company-show-numbers company-selection 99999))
1931 comp msg)
1932
1933 (while candidates
1934 (setq comp (company-reformat (pop candidates))
1935 len (+ len 1 (length comp)))
1936 (if (< i 10)
1937 ;; Add number.
1938 (progn
1939 (setq comp (propertize (format "%d: %s" i comp)
1940 'face 'company-echo))
1941 (incf len 3)
1942 (incf i)
1943 (add-text-properties 3 (+ 3 (length company-common))
1944 '(face company-echo-common) comp))
1945 (setq comp (propertize comp 'face 'company-echo))
1946 (add-text-properties 0 (length company-common)
1947 '(face company-echo-common) comp))
1948 (if (>= len limit)
1949 (setq candidates nil)
1950 (push comp msg)))
1951
1952 (mapconcat 'identity (nreverse msg) " ")))
1953
1954 (defun company-echo-strip-common-format ()
1955
1956 (let ((limit (window-width (minibuffer-window)))
1957 (len (+ (length company-prefix) 2))
1958 ;; Roll to selection.
1959 (candidates (nthcdr company-selection company-candidates))
1960 (i (if company-show-numbers company-selection 99999))
1961 msg comp)
1962
1963 (while candidates
1964 (setq comp (company-strip-prefix (pop candidates))
1965 len (+ len 2 (length comp)))
1966 (when (< i 10)
1967 ;; Add number.
1968 (setq comp (format "%s (%d)" comp i))
1969 (incf len 4)
1970 (incf i))
1971 (if (>= len limit)
1972 (setq candidates nil)
1973 (push (propertize comp 'face 'company-echo) msg)))
1974
1975 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1976 (mapconcat 'identity (nreverse msg) ", ")
1977 "}")))
1978
1979 (defun company-echo-hide ()
1980 (unless (equal company-echo-last-msg "")
1981 (setq company-echo-last-msg "")
1982 (company-echo-show)))
1983
1984 (defun company-echo-frontend (command)
1985 "A `company-mode' front-end showing the candidates in the echo area."
1986 (case command
1987 (post-command (company-echo-show-soon 'company-echo-format))
1988 (hide (company-echo-hide))))
1989
1990 (defun company-echo-strip-common-frontend (command)
1991 "A `company-mode' front-end showing the candidates in the echo area."
1992 (case command
1993 (post-command (company-echo-show-soon 'company-echo-strip-common-format))
1994 (hide (company-echo-hide))))
1995
1996 (defun company-echo-metadata-frontend (command)
1997 "A `company-mode' front-end showing the documentation in the echo area."
1998 (case command
1999 (post-command (company-echo-show-when-idle 'company-fetch-metadata))
2000 (hide (company-echo-hide))))
2001
2002 ;; templates ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2003
2004 (autoload 'company-template-declare-template "company-template")
2005
2006 (provide 'company)
2007 ;;; company.el ends here