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