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