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