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