]> code.delx.au - gnu-emacs-elpa/blob - company.el
Autoload global-company-mode
[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.1
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 ;;;###autoload
554 (define-globalized-minor-mode global-company-mode company-mode
555 (lambda () (unless (or noninteractive (eq (aref (buffer-name) 0) ?\s))
556 (company-mode 1))))
557
558 (defsubst company-assert-enabled ()
559 (unless company-mode
560 (company-uninstall-map)
561 (error "Company not enabled")))
562
563 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
564
565 (defvar company-my-keymap nil)
566 (make-variable-buffer-local 'company-my-keymap)
567
568 (defvar company-emulation-alist '((t . nil)))
569
570 (defsubst company-enable-overriding-keymap (keymap)
571 (company-uninstall-map)
572 (setq company-my-keymap keymap))
573
574 (defun company-ensure-emulation-alist ()
575 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
576 (setq emulation-mode-map-alists
577 (cons 'company-emulation-alist
578 (delq 'company-emulation-alist emulation-mode-map-alists)))))
579
580 (defun company-install-map ()
581 (unless (or (cdar company-emulation-alist)
582 (null company-my-keymap))
583 (setf (cdar company-emulation-alist) company-my-keymap)))
584
585 (defun company-uninstall-map ()
586 (setf (cdar company-emulation-alist) nil))
587
588 ;; Hack:
589 ;; Emacs calculates the active keymaps before reading the event. That means we
590 ;; cannot change the keymap from a timer. So we send a bogus command.
591 (defun company-ignore ()
592 (interactive)
593 (setq this-command last-command))
594
595 (global-set-key '[31415926] 'company-ignore)
596
597 (defun company-input-noop ()
598 (push 31415926 unread-command-events))
599
600 ;; Hack:
601 ;; posn-col-row is incorrect in older Emacsen when line-spacing is set
602 (defun company--col-row (&optional pos)
603 (let ((posn (posn-at-point pos)))
604 (cons (car (posn-col-row posn)) (cdr (posn-actual-col-row posn)))))
605
606 (defsubst company--column (&optional pos)
607 (car (posn-col-row (posn-at-point pos))))
608
609 (defsubst company--row (&optional pos)
610 (cdr (posn-actual-col-row (posn-at-point pos))))
611
612 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
613
614 (defun company-grab (regexp &optional expression limit)
615 (when (looking-back regexp limit)
616 (or (match-string-no-properties (or expression 0)) "")))
617
618 (defun company-grab-line (regexp &optional expression)
619 (company-grab regexp expression (point-at-bol)))
620
621 (defun company-grab-symbol ()
622 (if (looking-at "\\_>")
623 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
624 (point)))
625 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
626 "")))
627
628 (defun company-grab-word ()
629 (if (looking-at "\\>")
630 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
631 (point)))
632 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
633 "")))
634
635 (defun company-in-string-or-comment ()
636 (let ((ppss (syntax-ppss)))
637 (or (car (setq ppss (nthcdr 3 ppss)))
638 (car (setq ppss (cdr ppss)))
639 (nth 3 ppss))))
640
641 (if (fboundp 'locate-dominating-file)
642 (defalias 'company-locate-dominating-file 'locate-dominating-file)
643 (defun company-locate-dominating-file (file name)
644 (catch 'root
645 (let ((dir (file-name-directory file))
646 (prev-dir nil))
647 (while (not (equal dir prev-dir))
648 (when (file-exists-p (expand-file-name name dir))
649 (throw 'root dir))
650 (setq prev-dir dir
651 dir (file-name-directory (directory-file-name dir))))))))
652
653 (defun company-call-backend (&rest args)
654 (if (functionp company-backend)
655 (apply company-backend args)
656 (apply 'company--multi-backend-adapter company-backend args)))
657
658 (defun company--multi-backend-adapter (backends command &rest args)
659 (let ((backends (remove-if (lambda (b)
660 (and (symbolp b)
661 (eq 'failed (get b 'company-init))))
662 backends)))
663 (case command
664 (candidates
665 (loop for backend in backends
666 when (equal (funcall backend 'prefix)
667 (car args))
668 append (apply backend 'candidates args)))
669 (sorted nil)
670 (duplicates t)
671 (otherwise
672 (let (value)
673 (dolist (backend backends)
674 (when (setq value (apply backend command args))
675 (return value))))))))
676
677 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
678
679 (defvar company-backend nil)
680 (make-variable-buffer-local 'company-backend)
681
682 (defvar company-prefix nil)
683 (make-variable-buffer-local 'company-prefix)
684
685 (defvar company-candidates nil)
686 (make-variable-buffer-local 'company-candidates)
687
688 (defvar company-candidates-length nil)
689 (make-variable-buffer-local 'company-candidates-length)
690
691 (defvar company-candidates-cache nil)
692 (make-variable-buffer-local 'company-candidates-cache)
693
694 (defvar company-candidates-predicate nil)
695 (make-variable-buffer-local 'company-candidates-predicate)
696
697 (defvar company-common nil)
698 (make-variable-buffer-local 'company-common)
699
700 (defvar company-selection 0)
701 (make-variable-buffer-local 'company-selection)
702
703 (defvar company-selection-changed nil)
704 (make-variable-buffer-local 'company-selection-changed)
705
706 (defvar company--explicit-action nil
707 "Non-nil, if explicit completion took place.")
708 (make-variable-buffer-local 'company--explicit-action)
709
710 (defvar company--auto-completion nil
711 "Non-nil when current candidate is being completed automatically.
712 Controlled by `company-auto-complete'.")
713
714 (defvar company--point-max nil)
715 (make-variable-buffer-local 'company--point-max)
716
717 (defvar company-point nil)
718 (make-variable-buffer-local 'company-point)
719
720 (defvar company-timer nil)
721
722 (defvar company-added-newline nil)
723 (make-variable-buffer-local 'company-added-newline)
724
725 (defsubst company-strip-prefix (str)
726 (substring str (length company-prefix)))
727
728 (defmacro company-with-candidate-inserted (candidate &rest body)
729 "Evaluate BODY with CANDIDATE temporarily inserted.
730 This is a tool for back-ends that need candidates inserted before they
731 can retrieve meta-data for them."
732 (declare (indent 1))
733 `(let ((inhibit-modification-hooks t)
734 (inhibit-point-motion-hooks t)
735 (modified-p (buffer-modified-p)))
736 (insert (company-strip-prefix ,candidate))
737 (unwind-protect
738 (progn ,@body)
739 (delete-region company-point (point)))))
740
741 (defun company-explicit-action-p ()
742 "Return whether explicit completion action was taken by the user."
743 (or company--explicit-action
744 company-selection-changed))
745
746 (defsubst company-reformat (candidate)
747 ;; company-ispell needs this, because the results are always lower-case
748 ;; It's mory efficient to fix it only when they are displayed.
749 (concat company-prefix (substring candidate (length company-prefix))))
750
751 (defun company--should-complete ()
752 (and (not (or buffer-read-only overriding-terminal-local-map
753 overriding-local-map
754 (minibufferp)))
755 ;; Check if in the middle of entering a key combination.
756 (or (equal (this-command-keys-vector) [])
757 (not (keymapp (key-binding (this-command-keys-vector)))))
758 (eq company-idle-delay t)
759 (or (eq t company-begin-commands)
760 (memq this-command company-begin-commands)
761 (and (symbolp this-command) (get this-command 'company-begin)))
762 (not (and transient-mark-mode mark-active))))
763
764 (defsubst company-call-frontends (command)
765 (dolist (frontend company-frontends)
766 (condition-case err
767 (funcall frontend command)
768 (error (error "Company: Front-end %s error \"%s\" on command %s"
769 frontend (error-message-string err) command)))))
770
771 (defsubst company-set-selection (selection &optional force-update)
772 (setq selection (max 0 (min (1- company-candidates-length) selection)))
773 (when (or force-update (not (equal selection company-selection)))
774 (setq company-selection selection
775 company-selection-changed t)
776 (company-call-frontends 'update)))
777
778 (defun company-apply-predicate (candidates predicate)
779 (let (new)
780 (dolist (c candidates)
781 (when (funcall predicate c)
782 (push c new)))
783 (nreverse new)))
784
785 (defun company-update-candidates (candidates)
786 (setq company-candidates-length (length candidates))
787 (if (> company-selection 0)
788 ;; Try to restore the selection
789 (let ((selected (nth company-selection company-candidates)))
790 (setq company-selection 0
791 company-candidates candidates)
792 (when selected
793 (while (and candidates (string< (pop candidates) selected))
794 (incf company-selection))
795 (unless candidates
796 ;; Make sure selection isn't out of bounds.
797 (setq company-selection (min (1- company-candidates-length)
798 company-selection)))))
799 (setq company-selection 0
800 company-candidates candidates))
801 ;; Save in cache:
802 (push (cons company-prefix company-candidates) company-candidates-cache)
803 ;; Calculate common.
804 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
805 (setq company-common (company--safe-candidate
806 (try-completion company-prefix company-candidates))))
807 (when (eq company-common t)
808 (setq company-candidates nil)))
809
810 (defun company--safe-candidate (str)
811 (or (company-call-backend 'crop str)
812 str))
813
814 (defun company-calculate-candidates (prefix)
815 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
816 (ignore-case (company-call-backend 'ignore-case)))
817 (or candidates
818 (when company-candidates-cache
819 (let ((len (length prefix))
820 (completion-ignore-case ignore-case)
821 prev)
822 (dotimes (i (1+ len))
823 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
824 company-candidates-cache)))
825 (setq candidates (all-completions prefix prev))
826 (return t)))))
827 ;; no cache match, call back-end
828 (progn
829 (setq candidates (company-call-backend 'candidates prefix))
830 (when company-candidates-predicate
831 (setq candidates
832 (company-apply-predicate candidates
833 company-candidates-predicate)))
834 (unless (company-call-backend 'sorted)
835 (setq candidates (sort candidates 'string<)))
836 (when (company-call-backend 'duplicates)
837 ;; strip duplicates
838 (let ((c2 candidates))
839 (while c2
840 (setcdr c2 (progn (while (equal (pop c2) (car c2)))
841 c2)))))))
842 (if (and candidates
843 (or (cdr candidates)
844 (not (eq t (compare-strings (car candidates) nil nil
845 prefix nil nil ignore-case)))))
846 candidates
847 ;; Already completed and unique; don't start.
848 ;; FIXME: Not the right place? maybe when setting?
849 (and company-candidates t))))
850
851 (defun company-idle-begin (buf win tick pos)
852 (and company-mode
853 (eq buf (current-buffer))
854 (eq win (selected-window))
855 (eq tick (buffer-chars-modified-tick))
856 (eq pos (point))
857 (not company-candidates)
858 (not (equal (point) company-point))
859 (let ((company-idle-delay t)
860 (company-begin-commands t))
861 (company-begin)
862 (when company-candidates
863 (company-input-noop)
864 (company-post-command)))))
865
866 (defun company-auto-begin ()
867 (company-assert-enabled)
868 (and company-mode
869 (not company-candidates)
870 (let ((company-idle-delay t)
871 (company-minimum-prefix-length 0)
872 (company-begin-commands t))
873 (company-begin)))
874 ;; Return non-nil if active.
875 company-candidates)
876
877 (defun company-manual-begin ()
878 (interactive)
879 (setq company--explicit-action t)
880 (company-auto-begin))
881
882 (defun company-other-backend (&optional backward)
883 (interactive (list current-prefix-arg))
884 (company-assert-enabled)
885 (if company-backend
886 (let* ((after (cdr (member company-backend company-backends)))
887 (before (cdr (member company-backend (reverse company-backends))))
888 (next (if backward
889 (append before (reverse after))
890 (append after (reverse before)))))
891 (company-cancel)
892 (dolist (backend next)
893 (when (ignore-errors (company-begin-backend backend))
894 (return t))))
895 (company-manual-begin))
896 (unless company-candidates
897 (error "No other back-end")))
898
899 (defun company-require-match-p ()
900 (let ((backend-value (company-call-backend 'require-match)))
901 (or (eq backend-value t)
902 (and (if (functionp company-require-match)
903 (funcall company-require-match)
904 (eq company-require-match t))
905 (not (eq backend-value 'never))))))
906
907 (defun company-punctuation-p (input)
908 "Return non-nil, if input starts with punctuation or parentheses."
909 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
910
911 (defun company-auto-complete-p (input)
912 "Return non-nil, if input starts with punctuation or parentheses."
913 (and (if (functionp company-auto-complete)
914 (funcall company-auto-complete)
915 company-auto-complete)
916 (if (functionp company-auto-complete-chars)
917 (funcall company-auto-complete-chars input)
918 (if (consp company-auto-complete-chars)
919 (memq (char-syntax (string-to-char input))
920 company-auto-complete-chars)
921 (string-match (substring input 0 1) company-auto-complete-chars)))))
922
923 (defun company--incremental-p ()
924 (and (> (point) company-point)
925 (> (point-max) company--point-max)
926 (not (eq this-command 'backward-delete-char-untabify))
927 (equal (buffer-substring (- company-point (length company-prefix))
928 company-point)
929 company-prefix)))
930
931 (defsubst company--string-incremental-p (old-prefix new-prefix)
932 (and (> (length new-prefix) (length old-prefix))
933 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
934
935 (defun company--continue-failed (new-prefix)
936 (when (company--incremental-p)
937 (let ((input (buffer-substring-no-properties (point) company-point)))
938 (cond
939 ((company-auto-complete-p input)
940 ;; auto-complete
941 (save-excursion
942 (goto-char company-point)
943 (let ((company--auto-completion t))
944 (company-complete-selection))
945 nil))
946 ((and (company--string-incremental-p company-prefix new-prefix)
947 (company-require-match-p))
948 ;; wrong incremental input, but required match
949 (backward-delete-char (length input))
950 (ding)
951 (message "Matching input is required")
952 company-candidates)
953 ((equal company-prefix (car company-candidates))
954 ;; last input was actually success
955 (company-cancel company-prefix)
956 nil)))))
957
958 (defun company--good-prefix-p (prefix)
959 (and (or (company-explicit-action-p)
960 (unless (eq prefix 'stop)
961 (>= (or (cdr-safe prefix) (length prefix))
962 company-minimum-prefix-length)))
963 (stringp (or (car-safe prefix) prefix))))
964
965 (defun company--continue ()
966 (when (company-call-backend 'no-cache company-prefix)
967 ;; Don't complete existing candidates, fetch new ones.
968 (setq company-candidates-cache nil))
969 (let* ((new-prefix (company-call-backend 'prefix))
970 (c (when (and (company--good-prefix-p new-prefix)
971 (setq new-prefix (or (car-safe new-prefix) new-prefix))
972 (= (- (point) (length new-prefix))
973 (- company-point (length company-prefix))))
974 (setq new-prefix (or (car-safe new-prefix) new-prefix))
975 (company-calculate-candidates new-prefix))))
976 (or (cond
977 ((eq c t)
978 ;; t means complete/unique.
979 (company-cancel new-prefix)
980 nil)
981 ((consp c)
982 ;; incremental match
983 (setq company-prefix new-prefix)
984 (company-update-candidates c)
985 c)
986 (t (company--continue-failed new-prefix)))
987 (company-cancel))))
988
989 (defun company--begin-new ()
990 (let (prefix c)
991 (dolist (backend (if company-backend
992 ;; prefer manual override
993 (list company-backend)
994 company-backends))
995 (setq prefix
996 (if (or (symbolp backend)
997 (functionp backend))
998 (when (or (not (symbolp backend))
999 (eq t (get backend 'company-init))
1000 (unless (get backend 'company-init)
1001 (company-init-backend backend)))
1002 (funcall backend 'prefix))
1003 (company--multi-backend-adapter backend 'prefix)))
1004 (when prefix
1005 (when (company--good-prefix-p prefix)
1006 (setq prefix (or (car-safe prefix) prefix)
1007 company-backend backend
1008 c (company-calculate-candidates prefix))
1009 ;; t means complete/unique. We don't start, so no hooks.
1010 (if (not (consp c))
1011 (when company--explicit-action
1012 (message "No completion found"))
1013 (setq company-prefix prefix)
1014 (when (symbolp backend)
1015 (setq company-lighter (concat " " (symbol-name backend))))
1016 (company-update-candidates c)
1017 (run-hook-with-args 'company-completion-started-hook
1018 (company-explicit-action-p))
1019 (company-call-frontends 'show)))
1020 (return c)))))
1021
1022 (defun company-begin ()
1023 (or (and company-candidates (company--continue))
1024 (and (company--should-complete) (company--begin-new)))
1025 (when company-candidates
1026 (when (and company-end-of-buffer-workaround (eobp))
1027 (save-excursion (insert "\n"))
1028 (setq company-added-newline (buffer-chars-modified-tick)))
1029 (setq company-point (point)
1030 company--point-max (point-max))
1031 (company-ensure-emulation-alist)
1032 (company-enable-overriding-keymap company-active-map)
1033 (company-call-frontends 'update)))
1034
1035 (defun company-cancel (&optional result)
1036 (and company-added-newline
1037 (> (point-max) (point-min))
1038 (let ((tick (buffer-chars-modified-tick)))
1039 (delete-region (1- (point-max)) (point-max))
1040 (equal tick company-added-newline))
1041 ;; Only set unmodified when tick remained the same since insert.
1042 (set-buffer-modified-p nil))
1043 (when company-prefix
1044 (if (stringp result)
1045 (progn
1046 (company-call-backend 'pre-completion result)
1047 (run-hook-with-args 'company-completion-finished-hook result)
1048 (company-call-backend 'post-completion result))
1049 (run-hook-with-args 'company-completion-cancelled-hook result)))
1050 (setq company-added-newline nil
1051 company-backend nil
1052 company-prefix nil
1053 company-candidates nil
1054 company-candidates-length nil
1055 company-candidates-cache nil
1056 company-candidates-predicate nil
1057 company-common nil
1058 company-selection 0
1059 company-selection-changed nil
1060 company--explicit-action nil
1061 company-lighter company-default-lighter
1062 company--point-max nil
1063 company-point nil)
1064 (when company-timer
1065 (cancel-timer company-timer))
1066 (company-search-mode 0)
1067 (company-call-frontends 'hide)
1068 (company-enable-overriding-keymap nil))
1069
1070 (defun company-abort ()
1071 (interactive)
1072 (company-cancel t)
1073 ;; Don't start again, unless started manually.
1074 (setq company-point (point)))
1075
1076 (defun company-finish (result)
1077 (insert (company-strip-prefix result))
1078 (company-cancel result)
1079 ;; Don't start again, unless started manually.
1080 (setq company-point (point)))
1081
1082 (defsubst company-keep (command)
1083 (and (symbolp command) (get command 'company-keep)))
1084
1085 (defun company-pre-command ()
1086 (unless (company-keep this-command)
1087 (condition-case err
1088 (when company-candidates
1089 (company-call-frontends 'pre-command))
1090 (error (message "Company: An error occurred in pre-command")
1091 (message "%s" (error-message-string err))
1092 (company-cancel))))
1093 (when company-timer
1094 (cancel-timer company-timer)
1095 (setq company-timer nil))
1096 (company-uninstall-map))
1097
1098 (defun company-post-command ()
1099 (unless (company-keep this-command)
1100 (condition-case err
1101 (progn
1102 (unless (equal (point) company-point)
1103 (company-begin))
1104 (if company-candidates
1105 (company-call-frontends 'post-command)
1106 (and (numberp company-idle-delay)
1107 (or (eq t company-begin-commands)
1108 (memq this-command company-begin-commands))
1109 (setq company-timer
1110 (run-with-timer company-idle-delay nil
1111 'company-idle-begin
1112 (current-buffer) (selected-window)
1113 (buffer-chars-modified-tick) (point))))))
1114 (error (message "Company: An error occurred in post-command")
1115 (message "%s" (error-message-string err))
1116 (company-cancel))))
1117 (company-install-map))
1118
1119 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1120
1121 (defvar company-search-string nil)
1122 (make-variable-buffer-local 'company-search-string)
1123
1124 (defvar company-search-lighter " Search: \"\"")
1125 (make-variable-buffer-local 'company-search-lighter)
1126
1127 (defvar company-search-old-map nil)
1128 (make-variable-buffer-local 'company-search-old-map)
1129
1130 (defvar company-search-old-selection 0)
1131 (make-variable-buffer-local 'company-search-old-selection)
1132
1133 (defun company-search (text lines)
1134 (let ((quoted (regexp-quote text))
1135 (i 0))
1136 (dolist (line lines)
1137 (when (string-match quoted line (length company-prefix))
1138 (return i))
1139 (incf i))))
1140
1141 (defun company-search-printing-char ()
1142 (interactive)
1143 (company-search-assert-enabled)
1144 (setq company-search-string
1145 (concat (or company-search-string "") (string last-command-event))
1146 company-search-lighter (concat " Search: \"" company-search-string
1147 "\""))
1148 (let ((pos (company-search company-search-string
1149 (nthcdr company-selection company-candidates))))
1150 (if (null pos)
1151 (ding)
1152 (company-set-selection (+ company-selection pos) t))))
1153
1154 (defun company-search-repeat-forward ()
1155 "Repeat the incremental search in completion candidates forward."
1156 (interactive)
1157 (company-search-assert-enabled)
1158 (let ((pos (company-search company-search-string
1159 (cdr (nthcdr company-selection
1160 company-candidates)))))
1161 (if (null pos)
1162 (ding)
1163 (company-set-selection (+ company-selection pos 1) t))))
1164
1165 (defun company-search-repeat-backward ()
1166 "Repeat the incremental search in completion candidates backwards."
1167 (interactive)
1168 (company-search-assert-enabled)
1169 (let ((pos (company-search company-search-string
1170 (nthcdr (- company-candidates-length
1171 company-selection)
1172 (reverse company-candidates)))))
1173 (if (null pos)
1174 (ding)
1175 (company-set-selection (- company-selection pos 1) t))))
1176
1177 (defun company-create-match-predicate ()
1178 (setq company-candidates-predicate
1179 `(lambda (candidate)
1180 ,(if company-candidates-predicate
1181 `(and (string-match ,company-search-string candidate)
1182 (funcall ,company-candidates-predicate
1183 candidate))
1184 `(string-match ,company-search-string candidate))))
1185 (company-update-candidates
1186 (company-apply-predicate company-candidates company-candidates-predicate))
1187 ;; Invalidate cache.
1188 (setq company-candidates-cache (cons company-prefix company-candidates)))
1189
1190 (defun company-filter-printing-char ()
1191 (interactive)
1192 (company-search-assert-enabled)
1193 (company-search-printing-char)
1194 (company-create-match-predicate)
1195 (company-call-frontends 'update))
1196
1197 (defun company-search-kill-others ()
1198 "Limit the completion candidates to the ones matching the search string."
1199 (interactive)
1200 (company-search-assert-enabled)
1201 (company-create-match-predicate)
1202 (company-search-mode 0)
1203 (company-call-frontends 'update))
1204
1205 (defun company-search-abort ()
1206 "Abort searching the completion candidates."
1207 (interactive)
1208 (company-search-assert-enabled)
1209 (company-set-selection company-search-old-selection t)
1210 (company-search-mode 0))
1211
1212 (defun company-search-other-char ()
1213 (interactive)
1214 (company-search-assert-enabled)
1215 (company-search-mode 0)
1216 (company--unread-last-input))
1217
1218 (defvar company-search-map
1219 (let ((i 0)
1220 (keymap (make-keymap)))
1221 (if (fboundp 'max-char)
1222 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1223 'company-search-printing-char)
1224 (with-no-warnings
1225 ;; obsolete in Emacs 23
1226 (let ((l (generic-character-list))
1227 (table (nth 1 keymap)))
1228 (while l
1229 (set-char-table-default table (car l) 'company-search-printing-char)
1230 (setq l (cdr l))))))
1231 (define-key keymap [t] 'company-search-other-char)
1232 (while (< i ?\s)
1233 (define-key keymap (make-string 1 i) 'company-search-other-char)
1234 (incf i))
1235 (while (< i 256)
1236 (define-key keymap (vector i) 'company-search-printing-char)
1237 (incf i))
1238 (let ((meta-map (make-sparse-keymap)))
1239 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1240 (define-key keymap [escape] meta-map))
1241 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1242 (define-key keymap "\e\e\e" 'company-search-other-char)
1243 (define-key keymap [escape escape escape] 'company-search-other-char)
1244
1245 (define-key keymap "\C-g" 'company-search-abort)
1246 (define-key keymap "\C-s" 'company-search-repeat-forward)
1247 (define-key keymap "\C-r" 'company-search-repeat-backward)
1248 (define-key keymap "\C-o" 'company-search-kill-others)
1249 keymap)
1250 "Keymap used for incrementally searching the completion candidates.")
1251
1252 (define-minor-mode company-search-mode
1253 "Search mode for completion candidates.
1254 Don't start this directly, use `company-search-candidates' or
1255 `company-filter-candidates'."
1256 nil company-search-lighter nil
1257 (if company-search-mode
1258 (if (company-manual-begin)
1259 (progn
1260 (setq company-search-old-selection company-selection)
1261 (company-call-frontends 'update))
1262 (setq company-search-mode nil))
1263 (kill-local-variable 'company-search-string)
1264 (kill-local-variable 'company-search-lighter)
1265 (kill-local-variable 'company-search-old-selection)
1266 (company-enable-overriding-keymap company-active-map)))
1267
1268 (defsubst company-search-assert-enabled ()
1269 (company-assert-enabled)
1270 (unless company-search-mode
1271 (company-uninstall-map)
1272 (error "Company not in search mode")))
1273
1274 (defun company-search-candidates ()
1275 "Start searching the completion candidates incrementally.
1276
1277 \\<company-search-map>Search can be controlled with the commands:
1278 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1279 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1280 - `company-search-abort' (\\[company-search-abort])
1281
1282 Regular characters are appended to the search string.
1283
1284 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
1285 the search string to limit the completion candidates."
1286 (interactive)
1287 (company-search-mode 1)
1288 (company-enable-overriding-keymap company-search-map))
1289
1290 (defvar company-filter-map
1291 (let ((keymap (make-keymap)))
1292 (define-key keymap [remap company-search-printing-char]
1293 'company-filter-printing-char)
1294 (set-keymap-parent keymap company-search-map)
1295 keymap)
1296 "Keymap used for incrementally searching the completion candidates.")
1297
1298 (defun company-filter-candidates ()
1299 "Start filtering the completion candidates incrementally.
1300 This works the same way as `company-search-candidates' immediately
1301 followed by `company-search-kill-others' after each input."
1302 (interactive)
1303 (company-search-mode 1)
1304 (company-enable-overriding-keymap company-filter-map))
1305
1306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1307
1308 (defun company-select-next ()
1309 "Select the next candidate in the list."
1310 (interactive)
1311 (when (company-manual-begin)
1312 (company-set-selection (1+ company-selection))))
1313
1314 (defun company-select-previous ()
1315 "Select the previous candidate in the list."
1316 (interactive)
1317 (when (company-manual-begin)
1318 (company-set-selection (1- company-selection))))
1319
1320 (defun company-select-next-or-abort ()
1321 "Select the next candidate if more than one, else abort
1322 and invoke the normal binding."
1323 (interactive)
1324 (if (> company-candidates-length 1)
1325 (company-select-next)
1326 (company-abort)
1327 (company--unread-last-input)))
1328
1329 (defun company-select-previous-or-abort ()
1330 "Select the previous candidate if more than one, else abort
1331 and invoke the normal binding."
1332 (interactive)
1333 (if (> company-candidates-length 1)
1334 (company-select-previous)
1335 (company-abort)
1336 (company--unread-last-input)))
1337
1338 (defun company-select-mouse (event)
1339 "Select the candidate picked by the mouse."
1340 (interactive "e")
1341 (when (nth 4 (event-start event))
1342 (company-set-selection (- (cdr (posn-actual-col-row (event-start event)))
1343 (company--row)
1344 1))
1345 t))
1346
1347 (defun company-complete-mouse (event)
1348 "Complete the candidate picked by the mouse."
1349 (interactive "e")
1350 (when (company-select-mouse event)
1351 (company-complete-selection)))
1352
1353 (defun company-complete-selection ()
1354 "Complete the selected candidate."
1355 (interactive)
1356 (when (company-manual-begin)
1357 (let ((result (nth company-selection company-candidates)))
1358 (when company--auto-completion
1359 (setq result (company--safe-candidate result)))
1360 (company-finish result))))
1361
1362 (defun company-complete-common ()
1363 "Complete the common part of all candidates."
1364 (interactive)
1365 (when (company-manual-begin)
1366 (if (and (not (cdr company-candidates))
1367 (equal company-common (car company-candidates)))
1368 (company-complete-selection)
1369 (insert (company-strip-prefix company-common)))))
1370
1371 (defun company-complete ()
1372 "Complete the common part of all candidates or the current selection.
1373 The first time this is called, the common part is completed, the second time, or
1374 when the selection has been changed, the selected candidate is completed."
1375 (interactive)
1376 (when (company-manual-begin)
1377 (if (or company-selection-changed
1378 (eq last-command 'company-complete-common))
1379 (call-interactively 'company-complete-selection)
1380 (call-interactively 'company-complete-common)
1381 (setq this-command 'company-complete-common))))
1382
1383 (defun company-complete-number (n)
1384 "Complete the Nth candidate.
1385 To show the number next to the candidates in some back-ends, enable
1386 `company-show-numbers'."
1387 (when (company-manual-begin)
1388 (and (< n 1) (> n company-candidates-length)
1389 (error "No candidate number %d" n))
1390 (decf n)
1391 (company-finish (nth n company-candidates))))
1392
1393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1394
1395 (defconst company-space-strings-limit 100)
1396
1397 (defconst company-space-strings
1398 (let (lst)
1399 (dotimes (i company-space-strings-limit)
1400 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1401 (apply 'vector lst)))
1402
1403 (defsubst company-space-string (len)
1404 (if (< len company-space-strings-limit)
1405 (aref company-space-strings len)
1406 (make-string len ?\ )))
1407
1408 (defsubst company-safe-substring (str from &optional to)
1409 (if (> from (string-width str))
1410 ""
1411 (with-temp-buffer
1412 (insert str)
1413 (move-to-column from)
1414 (let ((beg (point)))
1415 (if to
1416 (progn
1417 (move-to-column to)
1418 (concat (buffer-substring beg (point))
1419 (let ((padding (- to (current-column))))
1420 (when (> padding 0)
1421 (company-space-string padding)))))
1422 (buffer-substring beg (point-max)))))))
1423
1424 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1425
1426 (defvar company-last-metadata nil)
1427 (make-variable-buffer-local 'company-last-metadata)
1428
1429 (defun company-fetch-metadata ()
1430 (let ((selected (nth company-selection company-candidates)))
1431 (unless (equal selected (car company-last-metadata))
1432 (setq company-last-metadata
1433 (cons selected (company-call-backend 'meta selected))))
1434 (cdr company-last-metadata)))
1435
1436 (defun company-doc-buffer (&optional string)
1437 (with-current-buffer (get-buffer-create "*Company meta-data*")
1438 (erase-buffer)
1439 (current-buffer)))
1440
1441 (defvar company--electric-commands
1442 '(scroll-other-window scroll-other-window-down)
1443 "List of Commands that won't break out of electric commands.")
1444
1445 (defmacro company--electric-do (&rest body)
1446 (declare (indent 0) (debug t))
1447 `(when (company-manual-begin)
1448 (save-window-excursion
1449 (let ((height (window-height))
1450 (row (company--row))
1451 cmd)
1452 ,@body
1453 (and (< (window-height) height)
1454 (< (- (window-height) row 2) company-tooltip-limit)
1455 (recenter (- (window-height) row 2)))
1456 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1457 company--electric-commands)
1458 (call-interactively cmd))
1459 (company--unread-last-input)))))
1460
1461 (defun company--unread-last-input ()
1462 (when last-input-event
1463 (clear-this-command-keys t)
1464 (setq unread-command-events (list last-input-event))))
1465
1466 (defun company-show-doc-buffer ()
1467 "Temporarily show a buffer with the complete documentation for the selection."
1468 (interactive)
1469 (company--electric-do
1470 (let* ((selected (nth company-selection company-candidates))
1471 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1472 (error "No documentation available"))))
1473 (with-current-buffer doc-buffer
1474 (goto-char (point-min)))
1475 (display-buffer doc-buffer t))))
1476 (put 'company-show-doc-buffer 'company-keep t)
1477
1478 (defun company-show-location ()
1479 "Temporarily display a buffer showing the selected candidate in context."
1480 (interactive)
1481 (company--electric-do
1482 (let* ((selected (nth company-selection company-candidates))
1483 (location (company-call-backend 'location selected))
1484 (pos (or (cdr location) (error "No location available")))
1485 (buffer (or (and (bufferp (car location)) (car location))
1486 (find-file-noselect (car location) t))))
1487 (with-selected-window (display-buffer buffer t)
1488 (save-restriction
1489 (widen)
1490 (if (bufferp (car location))
1491 (goto-char pos)
1492 (goto-char (point-min))
1493 (forward-line (1- pos))))
1494 (set-window-start nil (point))))))
1495 (put 'company-show-location 'company-keep t)
1496
1497 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1498
1499 (defvar company-callback nil)
1500 (make-variable-buffer-local 'company-callback)
1501
1502 (defvar company-begin-with-marker nil)
1503 (make-variable-buffer-local 'company-begin-with-marker)
1504
1505 (defun company-remove-callback (&optional ignored)
1506 (remove-hook 'company-completion-finished-hook company-callback t)
1507 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1508 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1509 (when company-begin-with-marker
1510 (set-marker company-begin-with-marker nil)))
1511
1512 (defun company-begin-backend (backend &optional callback)
1513 "Start a completion at point using BACKEND."
1514 (interactive (let ((val (completing-read "Company back-end: "
1515 obarray
1516 'functionp nil "company-")))
1517 (when val
1518 (list (intern val)))))
1519 (when (setq company-callback callback)
1520 (add-hook 'company-completion-finished-hook company-callback nil t))
1521 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1522 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1523 (setq company-backend backend)
1524 ;; Return non-nil if active.
1525 (or (company-manual-begin)
1526 (progn
1527 (setq company-backend nil)
1528 (error "Cannot complete at point"))))
1529
1530 (defun company-begin-with (candidates
1531 &optional prefix-length require-match callback)
1532 "Start a completion at point.
1533 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
1534 the prefix that already is in the buffer before point. It defaults to 0.
1535
1536 CALLBACK is a function called with the selected result if the user successfully
1537 completes the input.
1538
1539 Example:
1540 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1541 (setq company-begin-with-marker (copy-marker (point) t))
1542 (company-begin-backend
1543 `(lambda (command &optional arg &rest ignored)
1544 (cond
1545 ((eq command 'prefix)
1546 (when (equal (point) (marker-position company-begin-with-marker))
1547 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1548 ((eq command 'candidates)
1549 (all-completions arg ',candidates))
1550 ((eq command 'require-match)
1551 ,require-match)))
1552 callback))
1553
1554 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1555
1556 (defvar company-pseudo-tooltip-overlay nil)
1557 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1558
1559 (defvar company-tooltip-offset 0)
1560 (make-variable-buffer-local 'company-tooltip-offset)
1561
1562 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1563
1564 (decf limit 2)
1565 (setq company-tooltip-offset
1566 (max (min selection company-tooltip-offset)
1567 (- selection -1 limit)))
1568
1569 (when (<= company-tooltip-offset 1)
1570 (incf limit)
1571 (setq company-tooltip-offset 0))
1572
1573 (when (>= company-tooltip-offset (- num-lines limit 1))
1574 (incf limit)
1575 (when (= selection (1- num-lines))
1576 (decf company-tooltip-offset)
1577 (when (<= company-tooltip-offset 1)
1578 (setq company-tooltip-offset 0)
1579 (incf limit))))
1580
1581 limit)
1582
1583 ;;; propertize
1584
1585 (defsubst company-round-tab (arg)
1586 (* (/ (+ arg tab-width) tab-width) tab-width))
1587
1588 (defun company-untabify (str)
1589 (let* ((pieces (split-string str "\t"))
1590 (copy pieces))
1591 (while (cdr copy)
1592 (setcar copy (company-safe-substring
1593 (car copy) 0 (company-round-tab (string-width (car copy)))))
1594 (pop copy))
1595 (apply 'concat pieces)))
1596
1597 (defun company-fill-propertize (line width selected)
1598 (setq line (company-safe-substring line 0 width))
1599 (add-text-properties 0 width '(face company-tooltip
1600 mouse-face company-tooltip-mouse)
1601 line)
1602 (add-text-properties 0 (length company-common)
1603 '(face company-tooltip-common
1604 mouse-face company-tooltip-mouse)
1605 line)
1606 (when selected
1607 (if (and company-search-string
1608 (string-match (regexp-quote company-search-string) line
1609 (length company-prefix)))
1610 (progn
1611 (add-text-properties (match-beginning 0) (match-end 0)
1612 '(face company-tooltip-selection)
1613 line)
1614 (when (< (match-beginning 0) (length company-common))
1615 (add-text-properties (match-beginning 0) (length company-common)
1616 '(face company-tooltip-common-selection)
1617 line)))
1618 (add-text-properties 0 width '(face company-tooltip-selection
1619 mouse-face company-tooltip-selection)
1620 line)
1621 (add-text-properties 0 (length company-common)
1622 '(face company-tooltip-common-selection
1623 mouse-face company-tooltip-selection)
1624 line)))
1625 line)
1626
1627 ;;; replace
1628
1629 (defun company-buffer-lines (beg end)
1630 (goto-char beg)
1631 (let (lines)
1632 (while (and (= 1 (vertical-motion 1))
1633 (<= (point) end))
1634 (push (buffer-substring beg (min end (1- (point)))) lines)
1635 (setq beg (point)))
1636 (unless (eq beg end)
1637 (push (buffer-substring beg end) lines))
1638 (nreverse lines)))
1639
1640 (defsubst company-modify-line (old new offset)
1641 (concat (company-safe-substring old 0 offset)
1642 new
1643 (company-safe-substring old (+ offset (length new)))))
1644
1645 (defsubst company--length-limit (lst limit)
1646 (if (nthcdr limit lst)
1647 limit
1648 (length lst)))
1649
1650 (defun company--replacement-string (lines old column nl &optional align-top)
1651
1652 (let ((width (length (car lines))))
1653 (when (> width (- (window-width) column))
1654 (setq column (max 0 (- (window-width) width)))))
1655
1656 (let (new)
1657 (when align-top
1658 ;; untouched lines first
1659 (dotimes (i (- (length old) (length lines)))
1660 (push (pop old) new)))
1661 ;; length into old lines.
1662 (while old
1663 (push (company-modify-line (pop old) (pop lines) column) new))
1664 ;; Append whole new lines.
1665 (while lines
1666 (push (concat (company-space-string column) (pop lines)) new))
1667
1668 (let ((str (concat (when nl "\n")
1669 (mapconcat 'identity (nreverse new) "\n")
1670 "\n")))
1671 (font-lock-append-text-property 0 (length str) 'face 'default str)
1672 str)))
1673
1674 (defun company--create-lines (selection limit)
1675
1676 (let ((len company-candidates-length)
1677 (numbered 99999)
1678 lines
1679 width
1680 lines-copy
1681 previous
1682 remainder
1683 new)
1684
1685 ;; Scroll to offset.
1686 (setq limit (company-pseudo-tooltip-update-offset selection len limit))
1687
1688 (when (> company-tooltip-offset 0)
1689 (setq previous (format "...(%d)" company-tooltip-offset)))
1690
1691 (setq remainder (- len limit company-tooltip-offset)
1692 remainder (when (> remainder 0)
1693 (setq remainder (format "...(%d)" remainder))))
1694
1695 (decf selection company-tooltip-offset)
1696 (setq width (max (length previous) (length remainder))
1697 lines (nthcdr company-tooltip-offset company-candidates)
1698 len (min limit len)
1699 lines-copy lines)
1700
1701 (dotimes (i len)
1702 (setq width (max (length (pop lines-copy)) width)))
1703 (setq width (min width (window-width)))
1704
1705 (setq lines-copy lines)
1706
1707 ;; number can make tooltip too long
1708 (when company-show-numbers
1709 (setq numbered company-tooltip-offset))
1710
1711 (when previous
1712 (push (propertize (company-safe-substring previous 0 width)
1713 'face 'company-tooltip)
1714 new))
1715
1716 (dotimes (i len)
1717 (push (company-fill-propertize
1718 (if (>= numbered 10)
1719 (company-reformat (pop lines))
1720 (incf numbered)
1721 (format "%s %d"
1722 (company-safe-substring (company-reformat (pop lines))
1723 0 (- width 2))
1724 (mod numbered 10)))
1725 width (equal i selection))
1726 new))
1727
1728 (when remainder
1729 (push (propertize (company-safe-substring remainder 0 width)
1730 'face 'company-tooltip)
1731 new))
1732
1733 (setq lines (nreverse new))))
1734
1735 ;; show
1736
1737 (defsubst company--window-inner-height ()
1738 (let ((edges (window-inside-edges (selected-window))))
1739 (- (nth 3 edges) (nth 1 edges))))
1740
1741 (defsubst company--pseudo-tooltip-height ()
1742 "Calculate the appropriate tooltip height.
1743 Returns a negative number if the tooltip should be displayed above point."
1744 (let* ((lines (company--row))
1745 (below (- (company--window-inner-height) 1 lines)))
1746 (if (and (< below (min company-tooltip-minimum company-candidates-length))
1747 (> lines below))
1748 (- (max 3 (min company-tooltip-limit lines)))
1749 (max 3 (min company-tooltip-limit below)))))
1750
1751 (defun company-pseudo-tooltip-show (row column selection)
1752 (company-pseudo-tooltip-hide)
1753 (save-excursion
1754
1755 (move-to-column 0)
1756
1757 (let* ((height (company--pseudo-tooltip-height))
1758 above)
1759
1760 (when (< height 0)
1761 (setq row (+ row height -1)
1762 above t))
1763
1764 (let* ((nl (< (move-to-window-line row) row))
1765 (beg (point))
1766 (end (save-excursion
1767 (move-to-window-line (+ row (abs height)))
1768 (point)))
1769 (ov (make-overlay beg end))
1770 (args (list (mapcar 'company-untabify
1771 (company-buffer-lines beg end))
1772 column nl above)))
1773
1774 (setq company-pseudo-tooltip-overlay ov)
1775 (overlay-put ov 'company-replacement-args args)
1776 (overlay-put ov 'company-before
1777 (apply 'company--replacement-string
1778 (company--create-lines selection (abs height))
1779 args))
1780
1781 (overlay-put ov 'company-column column)
1782 (overlay-put ov 'company-height (abs height))))))
1783
1784 (defun company-pseudo-tooltip-show-at-point (pos)
1785 (let ((col-row (company--col-row pos)))
1786 (when col-row
1787 (company-pseudo-tooltip-show (1+ (cdr col-row)) (car col-row)
1788 company-selection))))
1789
1790 (defun company-pseudo-tooltip-edit (lines selection)
1791 (let ((column (overlay-get company-pseudo-tooltip-overlay 'company-column))
1792 (height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
1793 (overlay-put company-pseudo-tooltip-overlay 'company-before
1794 (apply 'company--replacement-string
1795 (company--create-lines selection height)
1796 (overlay-get company-pseudo-tooltip-overlay
1797 'company-replacement-args)))))
1798
1799 (defun company-pseudo-tooltip-hide ()
1800 (when company-pseudo-tooltip-overlay
1801 (delete-overlay company-pseudo-tooltip-overlay)
1802 (setq company-pseudo-tooltip-overlay nil)))
1803
1804 (defun company-pseudo-tooltip-hide-temporarily ()
1805 (when (overlayp company-pseudo-tooltip-overlay)
1806 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
1807 (overlay-put company-pseudo-tooltip-overlay 'before-string nil)))
1808
1809 (defun company-pseudo-tooltip-unhide ()
1810 (when company-pseudo-tooltip-overlay
1811 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
1812 (overlay-put company-pseudo-tooltip-overlay 'before-string
1813 (overlay-get company-pseudo-tooltip-overlay 'company-before))
1814 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
1815
1816 (defun company-pseudo-tooltip-frontend (command)
1817 "A `company-mode' front-end similar to a tool-tip but based on overlays."
1818 (case command
1819 (pre-command (company-pseudo-tooltip-hide-temporarily))
1820 (post-command
1821 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
1822 (overlay-get company-pseudo-tooltip-overlay
1823 'company-height)
1824 0))
1825 (new-height (company--pseudo-tooltip-height)))
1826 (unless (and (>= (* old-height new-height) 0)
1827 (>= (abs old-height) (abs new-height)))
1828 ;; Redraw needed.
1829 (company-pseudo-tooltip-show-at-point (- (point)
1830 (length company-prefix)))))
1831 (company-pseudo-tooltip-unhide))
1832 (hide (company-pseudo-tooltip-hide)
1833 (setq company-tooltip-offset 0))
1834 (update (when (overlayp company-pseudo-tooltip-overlay)
1835 (company-pseudo-tooltip-edit company-candidates
1836 company-selection)))))
1837
1838 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
1839 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
1840 (unless (and (eq command 'post-command)
1841 (not (cdr company-candidates)))
1842 (company-pseudo-tooltip-frontend command)))
1843
1844 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1845
1846 (defvar company-preview-overlay nil)
1847 (make-variable-buffer-local 'company-preview-overlay)
1848
1849 (defun company-preview-show-at-point (pos)
1850 (company-preview-hide)
1851
1852 (setq company-preview-overlay (make-overlay pos pos))
1853
1854 (let ((completion(nth company-selection company-candidates)))
1855 (setq completion (propertize completion 'face 'company-preview))
1856 (add-text-properties 0 (length company-common)
1857 '(face company-preview-common) completion)
1858
1859 ;; Add search string
1860 (and company-search-string
1861 (string-match (regexp-quote company-search-string) completion)
1862 (add-text-properties (match-beginning 0)
1863 (match-end 0)
1864 '(face company-preview-search)
1865 completion))
1866
1867 (setq completion (company-strip-prefix completion))
1868
1869 (and (equal pos (point))
1870 (not (equal completion ""))
1871 (add-text-properties 0 1 '(cursor t) completion))
1872
1873 (overlay-put company-preview-overlay 'after-string completion)
1874 (overlay-put company-preview-overlay 'window (selected-window))))
1875
1876 (defun company-preview-hide ()
1877 (when company-preview-overlay
1878 (delete-overlay company-preview-overlay)
1879 (setq company-preview-overlay nil)))
1880
1881 (defun company-preview-frontend (command)
1882 "A `company-mode' front-end showing the selection as if it had been inserted."
1883 (case command
1884 (pre-command (company-preview-hide))
1885 (post-command (company-preview-show-at-point (point)))
1886 (hide (company-preview-hide))))
1887
1888 (defun company-preview-if-just-one-frontend (command)
1889 "`company-preview-frontend', but only shown for single candidates."
1890 (unless (and (eq command 'post-command)
1891 (cdr company-candidates))
1892 (company-preview-frontend command)))
1893
1894 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1895
1896 (defvar company-echo-last-msg nil)
1897 (make-variable-buffer-local 'company-echo-last-msg)
1898
1899 (defvar company-echo-timer nil)
1900
1901 (defvar company-echo-delay .01)
1902
1903 (defun company-echo-show (&optional getter)
1904 (when getter
1905 (setq company-echo-last-msg (funcall getter)))
1906 (let ((message-log-max nil))
1907 (if company-echo-last-msg
1908 (message "%s" company-echo-last-msg)
1909 (message ""))))
1910
1911 (defsubst company-echo-show-soon (&optional getter)
1912 (when company-echo-timer
1913 (cancel-timer company-echo-timer))
1914 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
1915
1916 (defsubst company-echo-show-when-idle (&optional getter)
1917 (when (sit-for .01)
1918 (company-echo-show getter)))
1919
1920 (defsubst company-echo-show-when-not-busy (&optional getter)
1921 "Run `company-echo-show' with arg GETTER once Emacs isn't busy."
1922 (when (sit-for company-echo-delay)
1923 (company-echo-show getter)))
1924
1925 (defun company-echo-format ()
1926
1927 (let ((limit (window-width (minibuffer-window)))
1928 (len -1)
1929 ;; Roll to selection.
1930 (candidates (nthcdr company-selection company-candidates))
1931 (i (if company-show-numbers company-selection 99999))
1932 comp msg)
1933
1934 (while candidates
1935 (setq comp (company-reformat (pop candidates))
1936 len (+ len 1 (length comp)))
1937 (if (< i 10)
1938 ;; Add number.
1939 (progn
1940 (setq comp (propertize (format "%d: %s" i comp)
1941 'face 'company-echo))
1942 (incf len 3)
1943 (incf i)
1944 (add-text-properties 3 (+ 3 (length company-common))
1945 '(face company-echo-common) comp))
1946 (setq comp (propertize comp 'face 'company-echo))
1947 (add-text-properties 0 (length company-common)
1948 '(face company-echo-common) comp))
1949 (if (>= len limit)
1950 (setq candidates nil)
1951 (push comp msg)))
1952
1953 (mapconcat 'identity (nreverse msg) " ")))
1954
1955 (defun company-echo-strip-common-format ()
1956
1957 (let ((limit (window-width (minibuffer-window)))
1958 (len (+ (length company-prefix) 2))
1959 ;; Roll to selection.
1960 (candidates (nthcdr company-selection company-candidates))
1961 (i (if company-show-numbers company-selection 99999))
1962 msg comp)
1963
1964 (while candidates
1965 (setq comp (company-strip-prefix (pop candidates))
1966 len (+ len 2 (length comp)))
1967 (when (< i 10)
1968 ;; Add number.
1969 (setq comp (format "%s (%d)" comp i))
1970 (incf len 4)
1971 (incf i))
1972 (if (>= len limit)
1973 (setq candidates nil)
1974 (push (propertize comp 'face 'company-echo) msg)))
1975
1976 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1977 (mapconcat 'identity (nreverse msg) ", ")
1978 "}")))
1979
1980 (defun company-echo-hide ()
1981 (unless (equal company-echo-last-msg "")
1982 (setq company-echo-last-msg "")
1983 (company-echo-show)))
1984
1985 (defun company-echo-frontend (command)
1986 "A `company-mode' front-end showing the candidates in the echo area."
1987 (case command
1988 (post-command (company-echo-show-soon 'company-echo-format))
1989 (hide (company-echo-hide))))
1990
1991 (defun company-echo-strip-common-frontend (command)
1992 "A `company-mode' front-end showing the candidates in the echo area."
1993 (case command
1994 (post-command (company-echo-show-soon 'company-echo-strip-common-format))
1995 (hide (company-echo-hide))))
1996
1997 (defun company-echo-metadata-frontend (command)
1998 "A `company-mode' front-end showing the documentation in the echo area."
1999 (case command
2000 (post-command (company-echo-show-when-idle 'company-fetch-metadata))
2001 (hide (company-echo-hide))))
2002
2003 ;; templates ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2004
2005 (autoload 'company-template-declare-template "company-template")
2006
2007 (provide 'company)
2008 ;;; company.el ends here