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