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