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