]> code.delx.au - gnu-emacs-elpa/blob - company.el
Replaced call to removed semantic-ia-get-completions.
[gnu-emacs-elpa] / company.el
1 ;;; company.el --- extensible inline text completion mechanism
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; Author: Nikolaj Schumacher <bugs * nschum de>
6 ;; Version: 0.4.3
7 ;; Keywords: abbrev, convenience, matching
8 ;; URL: http://nschum.de/src/emacs/company/
9 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.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 ;; `company-ropemacs' now provides location and docs. (Fernando H. Silva)
69 ;; Added `company-with-candidate-inserted' macro.
70 ;; Added `company-clang' back-end.
71 ;; Added new mechanism for non-consecutive insertion.
72 ;; (So far only used by clang for ObjC.)
73 ;; The semantic back-end now shows meta information for local symbols.
74 ;; Added compatibility for CEDET in Emacs 23.2 and from CVS. (Oleg Andreev)
75 ;;
76 ;; 2009-05-07 (0.4.3)
77 ;; Added `company-other-backend'.
78 ;; Idle completion no longer interrupts multi-key command input.
79 ;; Added `company-ropemacs' and `company-pysmell' back-ends.
80 ;;
81 ;; 2009-04-25 (0.4.2)
82 ;; In C modes . and -> now count towards `company-minimum-prefix-length'.
83 ;; Reverted default front-end back to `company-preview-if-just-one-frontend'.
84 ;; The pseudo tooltip will no longer be clipped at the right window edge.
85 ;; Added `company-tooltip-minimum'.
86 ;; Windows compatibility fixes.
87 ;;
88 ;; 2009-04-19 (0.4.1)
89 ;; Added `global-company-mode'.
90 ;; Performance enhancements.
91 ;; Added `company-eclim' back-end.
92 ;; Added safer workaround for Emacs `posn-col-row' bug.
93 ;;
94 ;; 2009-04-18 (0.4)
95 ;; Automatic completion is now aborted if the prefix gets too short.
96 ;; Added option `company-dabbrev-time-limit'.
97 ;; `company-backends' now supports merging back-ends.
98 ;; Added back-end `company-dabbrev-code' for generic code.
99 ;; Fixed `company-begin-with'.
100 ;;
101 ;; 2009-04-15 (0.3.1)
102 ;; Added 'stop prefix to prevent dabbrev from completing inside of symbols.
103 ;; Fixed issues with tabbar-mode and line-spacing.
104 ;; Performance enhancements.
105 ;;
106 ;; 2009-04-12 (0.3)
107 ;; Added `company-begin-commands' option.
108 ;; Added abbrev, tempo and Xcode back-ends.
109 ;; Back-ends are now interactive. You can start them with M-x backend-name.
110 ;; Added `company-begin-with' for starting company from elisp-code.
111 ;; Added hooks.
112 ;; Added `company-require-match' and `company-auto-complete' options.
113 ;;
114 ;; 2009-04-05 (0.2.1)
115 ;; Improved Emacs Lisp back-end behavior for local variables.
116 ;; Added `company-elisp-detect-function-context' option.
117 ;; The mouse can now be used for selection.
118 ;;
119 ;; 2009-03-22 (0.2)
120 ;; Added `company-show-location'.
121 ;; Added etags back-end.
122 ;; Added work-around for end-of-buffer bug.
123 ;; Added `company-filter-candidates'.
124 ;; More local Lisp variables are now included in the candidates.
125 ;;
126 ;; 2009-03-21 (0.1.5)
127 ;; Fixed elisp documentation buffer always showing the same doc.
128 ;; Added `company-echo-strip-common-frontend'.
129 ;; Added `company-show-numbers' option and M-0 ... M-9 default bindings.
130 ;; Don't hide the echo message if it isn't shown.
131 ;;
132 ;; 2009-03-20 (0.1)
133 ;; Initial release.
134 ;;
135 ;;; Code:
136
137 (eval-when-compile (require 'cl))
138
139 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
140 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
141 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
142 (add-to-list 'debug-ignored-errors "^Company not ")
143 (add-to-list 'debug-ignored-errors "^No candidate number ")
144 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
145 (add-to-list 'debug-ignored-errors "^No other back-end$")
146
147 (defgroup company nil
148 "Extensible inline text completion mechanism"
149 :group 'abbrev
150 :group 'convenience
151 :group 'matching)
152
153 (defface company-tooltip
154 '((t :background "yellow"
155 :foreground "black"))
156 "*Face used for the tool tip."
157 :group 'company)
158
159 (defface company-tooltip-selection
160 '((default :inherit company-tooltip)
161 (((class color) (min-colors 88)) (:background "orange1"))
162 (t (:background "green")))
163 "*Face used for the selection in the tool tip."
164 :group 'company)
165
166 (defface company-tooltip-mouse
167 '((default :inherit highlight))
168 "*Face used for the tool tip item under the mouse."
169 :group 'company)
170
171 (defface company-tooltip-common
172 '((t :inherit company-tooltip
173 :foreground "red"))
174 "*Face used for the common completion in the tool tip."
175 :group 'company)
176
177 (defface company-tooltip-common-selection
178 '((t :inherit company-tooltip-selection
179 :foreground "red"))
180 "*Face used for the selected common completion in the tool tip."
181 :group 'company)
182
183 (defface company-preview
184 '((t :background "blue4"
185 :foreground "wheat"))
186 "*Face used for the completion preview."
187 :group 'company)
188
189 (defface company-preview-common
190 '((t :inherit company-preview
191 :foreground "red"))
192 "*Face used for the common part of the completion preview."
193 :group 'company)
194
195 (defface company-preview-search
196 '((t :inherit company-preview
197 :background "blue1"))
198 "*Face used for the search string in the completion preview."
199 :group 'company)
200
201 (defface company-echo nil
202 "*Face used for completions in the echo area."
203 :group 'company)
204
205 (defface company-echo-common
206 '((((background dark)) (:foreground "firebrick1"))
207 (((background light)) (:background "firebrick4")))
208 "*Face used for the common part of completions in the echo area."
209 :group 'company)
210
211 (defun company-frontends-set (variable value)
212 ;; uniquify
213 (let ((remainder value))
214 (setcdr remainder (delq (car remainder) (cdr remainder))))
215 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
216 (memq 'company-pseudo-tooltip-frontend value)
217 (error "Pseudo tooltip frontend cannot be used twice"))
218 (and (memq 'company-preview-if-just-one-frontend value)
219 (memq 'company-preview-frontend value)
220 (error "Preview frontend cannot be used twice"))
221 (and (memq 'company-echo value)
222 (memq 'company-echo-metadata-frontend value)
223 (error "Echo area cannot be used twice"))
224 ;; preview must come last
225 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
226 (when (memq f value)
227 (setq value (append (delq f value) (list f)))))
228 (set variable value))
229
230 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
231 company-preview-if-just-one-frontend
232 company-echo-metadata-frontend)
233 "*The list of active front-ends (visualizations).
234 Each front-end is a function that takes one argument. It is called with
235 one of the following arguments:
236
237 'show: When the visualization should start.
238
239 'hide: When the visualization should end.
240
241 'update: When the data has been updated.
242
243 'pre-command: Before every command that is executed while the
244 visualization is active.
245
246 'post-command: After every command that is executed while the
247 visualization is active.
248
249 The visualized data is stored in `company-prefix', `company-candidates',
250 `company-common', `company-selection', `company-point' and
251 `company-search-string'."
252 :set 'company-frontends-set
253 :group 'company
254 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
255 (const :tag "echo, strip common"
256 company-echo-strip-common-frontend)
257 (const :tag "show echo meta-data in echo"
258 company-echo-metadata-frontend)
259 (const :tag "pseudo tooltip"
260 company-pseudo-tooltip-frontend)
261 (const :tag "pseudo tooltip, multiple only"
262 company-pseudo-tooltip-unless-just-one-frontend)
263 (const :tag "preview" company-preview-frontend)
264 (const :tag "preview, unique only"
265 company-preview-if-just-one-frontend)
266 (function :tag "custom function" nil))))
267
268 (defcustom company-tooltip-limit 10
269 "*The maximum number of candidates in the tool tip"
270 :group 'company
271 :type 'integer)
272
273 (defcustom company-tooltip-minimum 6
274 "*The minimum height of the tool tip.
275 If this many lines are not available, prefer to display the tooltip above."
276 :group 'company
277 :type 'integer)
278
279 (defvar company-safe-backends
280 '((company-abbrev . "Abbrev")
281 (company-clang . "clang")
282 (company-css . "CSS")
283 (company-dabbrev . "dabbrev for plain text")
284 (company-dabbrev-code . "dabbrev for code")
285 (company-eclim . "eclim (an Eclipse interace)")
286 (company-elisp . "Emacs Lisp")
287 (company-etags . "etags")
288 (company-files . "Files")
289 (company-gtags . "GNU Global")
290 (company-ispell . "ispell")
291 (company-keywords . "Programming language keywords")
292 (company-nxml . "nxml")
293 (company-oddmuse . "Oddmuse")
294 (company-pysmell . "PySmell")
295 (company-ropemacs . "ropemacs")
296 (company-semantic . "CEDET Semantic")
297 (company-tempo . "Tempo templates")
298 (company-xcode . "Xcode")))
299 (put 'company-safe-backends 'risky-local-variable t)
300
301 (defun company-safe-backends-p (backends)
302 (and (consp backends)
303 (not (dolist (backend backends)
304 (unless (if (consp backend)
305 (company-safe-backends-p backend)
306 (assq backend company-safe-backends))
307 (return t))))))
308
309 (defcustom company-backends '(company-elisp company-nxml company-css
310 company-eclim company-semantic company-clang
311 company-xcode company-ropemacs
312 (company-gtags company-etags company-dabbrev-code
313 company-pysmell company-keywords)
314 company-oddmuse company-files company-dabbrev)
315 "*The list of active back-ends (completion engines).
316 Each list elements can itself be a list of back-ends. In that case their
317 completions are merged. Otherwise only the first matching back-end returns
318 results.
319
320 `company-begin-backend' can be used to start a specific back-end,
321 `company-other-backend' will skip to the next matching back-end in the list.
322
323 Each back-end is a function that takes a variable number of arguments.
324 The first argument is the command requested from the back-end. It is one
325 of the following:
326
327 'prefix: The back-end should return the text to be completed. It must be
328 text immediately before `point'. Returning nil passes control to the next
329 back-end. The function should return 'stop if it should complete but cannot
330 \(e.g. if it is in the middle of a string\). If the returned value is only
331 part of the prefix (e.g. the part after \"->\" in C), the back-end may return a
332 cons of prefix and prefix length, which is then used in the
333 `company-minimum-prefix-length' test.
334
335 'candidates: The second argument is the prefix to be completed. The
336 return value should be a list of candidates that start with the prefix.
337
338 Optional commands:
339
340 'sorted: The back-end may return t here to indicate that the candidates
341 are sorted and will not need to be sorted again.
342
343 'duplicates: If non-nil, company will take care of removing duplicates
344 from the list.
345
346 'no-cache: Usually company doesn't ask for candidates again as completion
347 progresses, unless the back-end returns t for this command. The second
348 argument is the latest prefix.
349
350 'meta: The second argument is a completion candidate. The back-end should
351 return a (short) documentation string for it.
352
353 'doc-buffer: The second argument is a completion candidate. The back-end should
354 create a buffer (preferably with `company-doc-buffer'), fill it with
355 documentation and return it.
356
357 'location: The second argument is a completion candidate. The back-end can
358 return the cons of buffer and buffer location, or of file and line
359 number where the completion candidate was defined.
360
361 'require-match: If this value is t, the user is not allowed to enter anything
362 not offering as a candidate. Use with care! The default value nil gives the
363 user that choice with `company-require-match'. Return value 'never overrides
364 that option the other way around.
365
366 The back-end should return nil for all commands it does not support or
367 does not know about. It should also be callable interactively and use
368 `company-begin-backend' to start itself in that case."
369 :group 'company
370 :type `(repeat
371 (choice
372 :tag "Back-end"
373 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
374 company-safe-backends)
375 (symbol :tag "User defined")
376 (repeat :tag "Merged Back-ends"
377 (choice :tag "Back-end"
378 ,@(mapcar (lambda (b)
379 `(const :tag ,(cdr b) ,(car b)))
380 company-safe-backends)
381 (symbol :tag "User defined"))))))
382
383 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
384
385 (defcustom company-completion-started-hook nil
386 "*Hook run when company starts completing.
387 The hook is called with one argument that is non-nil if the completion was
388 started manually."
389 :group 'company
390 :type 'hook)
391
392 (defcustom company-completion-cancelled-hook nil
393 "*Hook run when company cancels completing.
394 The hook is called with one argument that is non-nil if the completion was
395 aborted manually."
396 :group 'company
397 :type 'hook)
398
399 (defcustom company-completion-finished-hook nil
400 "*Hook run when company successfully completes.
401 The hook is called with the selected candidate as an argument."
402 :group 'company
403 :type 'hook)
404
405 (defcustom company-minimum-prefix-length 3
406 "*The minimum prefix length for automatic completion."
407 :group 'company
408 :type '(integer :tag "prefix length"))
409
410 (defcustom company-require-match 'company-explicit-action-p
411 "*If enabled, disallow non-matching input.
412 This can be a function do determine if a match is required.
413
414 This can be overridden by the back-end, if it returns t or 'never to
415 'require-match. `company-auto-complete' also takes precedence over this."
416 :group 'company
417 :type '(choice (const :tag "Off" nil)
418 (function :tag "Predicate function")
419 (const :tag "On, if user interaction took place"
420 'company-explicit-action-p)
421 (const :tag "On" t)))
422
423 (defcustom company-auto-complete 'company-explicit-action-p
424 "Determines when to auto-complete.
425 If this is enabled, all characters from `company-auto-complete-chars' complete
426 the selected completion. This can also be a function."
427 :group 'company
428 :type '(choice (const :tag "Off" nil)
429 (function :tag "Predicate function")
430 (const :tag "On, if user interaction took place"
431 'company-explicit-action-p)
432 (const :tag "On" t)))
433
434 (defcustom company-auto-complete-chars '(?\ ?\( ?\) ?. ?\" ?$ ?\' ?< ?| ?!)
435 "Determines which characters trigger an automatic completion.
436 See `company-auto-complete'. If this is a string, each string character causes
437 completion. If it is a list of syntax description characters (see
438 `modify-syntax-entry'), all characters with that syntax auto-complete.
439
440 This can also be a function, which is called with the new input and should
441 return non-nil if company should auto-complete.
442
443 A character that is part of a valid candidate never starts auto-completion."
444 :group 'company
445 :type '(choice (string :tag "Characters")
446 (set :tag "Syntax"
447 (const :tag "Whitespace" ?\ )
448 (const :tag "Symbol" ?_)
449 (const :tag "Opening parentheses" ?\()
450 (const :tag "Closing parentheses" ?\))
451 (const :tag "Word constituent" ?w)
452 (const :tag "Punctuation." ?.)
453 (const :tag "String quote." ?\")
454 (const :tag "Paired delimiter." ?$)
455 (const :tag "Expression quote or prefix operator." ?\')
456 (const :tag "Comment starter." ?<)
457 (const :tag "Comment ender." ?>)
458 (const :tag "Character-quote." ?/)
459 (const :tag "Generic string fence." ?|)
460 (const :tag "Generic comment fence." ?!))
461 (function :tag "Predicate function")))
462
463 (defcustom company-idle-delay .7
464 "*The idle delay in seconds until automatic completions starts.
465 A value of nil means never complete automatically, t means complete
466 immediately when a prefix of `company-minimum-prefix-length' is reached."
467 :group 'company
468 :type '(choice (const :tag "never (nil)" nil)
469 (const :tag "immediate (t)" t)
470 (number :tag "seconds")))
471
472 (defcustom company-begin-commands t
473 "*A list of commands following which company will start completing.
474 If this is t, it will complete after any command. See `company-idle-delay'.
475
476 Alternatively any command with a non-nil 'company-begin property is treated as
477 if it was on this list."
478 :group 'company
479 :type '(choice (const :tag "Any command" t)
480 (const :tag "Self insert command" '(self-insert-command))
481 (repeat :tag "Commands" function)))
482
483 (defcustom company-show-numbers nil
484 "*If enabled, show quick-access numbers for the first ten candidates."
485 :group 'company
486 :type '(choice (const :tag "off" nil)
487 (const :tag "on" t)))
488
489 (defvar company-end-of-buffer-workaround t
490 "*Work around a visualization bug when completing at the end of the buffer.
491 The work-around consists of adding a newline.")
492
493 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
494
495 (defvar company-mode-map (make-sparse-keymap)
496 "Keymap used by `company-mode'.")
497
498 (defvar company-active-map
499 (let ((keymap (make-sparse-keymap)))
500 (define-key keymap "\e\e\e" 'company-abort)
501 (define-key keymap "\C-g" 'company-abort)
502 (define-key keymap (kbd "M-n") 'company-select-next)
503 (define-key keymap (kbd "M-p") 'company-select-previous)
504 (define-key keymap (kbd "<down>") 'company-select-next)
505 (define-key keymap (kbd "<up>") 'company-select-previous)
506 (define-key keymap [down-mouse-1] 'ignore)
507 (define-key keymap [down-mouse-3] 'ignore)
508 (define-key keymap [mouse-1] 'company-complete-mouse)
509 (define-key keymap [mouse-3] 'company-select-mouse)
510 (define-key keymap [up-mouse-1] 'ignore)
511 (define-key keymap [up-mouse-3] 'ignore)
512 (define-key keymap "\C-m" 'company-complete-selection)
513 (define-key keymap "\t" 'company-complete-common)
514 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
515 (define-key keymap "\C-w" 'company-show-location)
516 (define-key keymap "\C-s" 'company-search-candidates)
517 (define-key keymap "\C-\M-s" 'company-filter-candidates)
518 (dotimes (i 10)
519 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
520 `(lambda () (interactive) (company-complete-number ,i))))
521
522 keymap)
523 "Keymap that is enabled during an active completion.")
524
525 (defvar company--disabled-backends nil)
526
527 (defun company-init-backend (backend)
528 (and (symbolp backend)
529 (not (fboundp backend))
530 (ignore-errors (require backend nil t)))
531
532 (if (or (symbolp backend)
533 (functionp backend))
534 (condition-case err
535 (progn
536 (funcall backend 'init)
537 (put backend 'company-init t))
538 (error
539 (put backend 'company-init 'failed)
540 (unless (memq backend company--disabled-backends)
541 (message "Company back-end '%s' could not be initialized:\n%s"
542 backend (error-message-string err)))
543 (push backend company--disabled-backends)
544 nil))
545 (mapc 'company-init-backend backend)))
546
547 (defvar company-default-lighter " company")
548
549 (defvar company-lighter company-default-lighter)
550 (make-variable-buffer-local 'company-lighter)
551
552 ;;;###autoload
553 (define-minor-mode company-mode
554 "\"complete anything\"; in in-buffer completion framework.
555 Completion starts automatically, depending on the values
556 `company-idle-delay' and `company-minimum-prefix-length'.
557
558 Completion can be controlled with the commands:
559 `company-complete-common', `company-complete-selection', `company-complete',
560 `company-select-next', `company-select-previous'. If these commands are
561 called before `company-idle-delay', completion will also start.
562
563 Completions can be searched with `company-search-candidates' or
564 `company-filter-candidates'. These can be used while completion is
565 inactive, as well.
566
567 The completion data is retrieved using `company-backends' and displayed using
568 `company-frontends'. If you want to start a specific back-end, call it
569 interactively or use `company-begin-backend'.
570
571 regular keymap (`company-mode-map'):
572
573 \\{company-mode-map}
574 keymap during active completions (`company-active-map'):
575
576 \\{company-active-map}"
577 nil company-lighter company-mode-map
578 (if company-mode
579 (progn
580 (add-hook 'pre-command-hook 'company-pre-command nil t)
581 (add-hook 'post-command-hook 'company-post-command nil t)
582 (mapc 'company-init-backend company-backends))
583 (remove-hook 'pre-command-hook 'company-pre-command t)
584 (remove-hook 'post-command-hook 'company-post-command t)
585 (company-cancel)
586 (kill-local-variable 'company-point)))
587
588 (define-globalized-minor-mode global-company-mode company-mode
589 (lambda () (company-mode 1)))
590
591 (defsubst company-assert-enabled ()
592 (unless company-mode
593 (company-uninstall-map)
594 (error "Company not enabled")))
595
596 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
597
598 (defvar company-overriding-keymap-bound nil)
599 (make-variable-buffer-local 'company-overriding-keymap-bound)
600
601 (defvar company-old-keymap nil)
602 (make-variable-buffer-local 'company-old-keymap)
603
604 (defvar company-my-keymap nil)
605 (make-variable-buffer-local 'company-my-keymap)
606
607 (defsubst company-enable-overriding-keymap (keymap)
608 (setq company-my-keymap keymap)
609 (when company-overriding-keymap-bound
610 (company-uninstall-map)))
611
612 (defun company-install-map ()
613 (unless (or company-overriding-keymap-bound
614 (null company-my-keymap))
615 (setq company-old-keymap overriding-terminal-local-map
616 overriding-terminal-local-map company-my-keymap
617 company-overriding-keymap-bound t)))
618
619 (defun company-uninstall-map ()
620 (when (eq overriding-terminal-local-map company-my-keymap)
621 (setq overriding-terminal-local-map company-old-keymap
622 company-overriding-keymap-bound 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 (setq company-candidates
1039 (or (and company-candidates (company--continue))
1040 (and (company--should-complete) (company--begin-new))))
1041 (when company-candidates
1042 (when (and company-end-of-buffer-workaround (eobp))
1043 (save-excursion (insert "\n"))
1044 (setq company-added-newline (buffer-chars-modified-tick)))
1045 (setq company-point (point)
1046 company--point-max (point-max))
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 (defvar company-echo-timer nil)
1883
1884 (defvar company-echo-delay .01)
1885
1886 (defun company-echo-show (&optional getter)
1887 (when getter
1888 (setq company-echo-last-msg (funcall getter)))
1889 (let ((message-log-max nil))
1890 (if company-echo-last-msg
1891 (message "%s" company-echo-last-msg)
1892 (message ""))))
1893
1894 (defsubst company-echo-show-soon (&optional getter)
1895 (when company-echo-timer
1896 (cancel-timer company-echo-timer))
1897 (setq company-echo-timer (run-with-timer company-echo-delay nil
1898 'company-echo-show getter)))
1899
1900 (defun company-echo-format ()
1901
1902 (let ((limit (window-width (minibuffer-window)))
1903 (len -1)
1904 ;; Roll to selection.
1905 (candidates (nthcdr company-selection company-candidates))
1906 (i (if company-show-numbers company-selection 99999))
1907 comp msg)
1908
1909 (while candidates
1910 (setq comp (company-reformat (pop candidates))
1911 len (+ len 1 (length comp)))
1912 (if (< i 10)
1913 ;; Add number.
1914 (progn
1915 (setq comp (propertize (format "%d: %s" i comp)
1916 'face 'company-echo))
1917 (incf len 3)
1918 (incf i)
1919 (add-text-properties 3 (+ 3 (length company-common))
1920 '(face company-echo-common) comp))
1921 (setq comp (propertize comp 'face 'company-echo))
1922 (add-text-properties 0 (length company-common)
1923 '(face company-echo-common) comp))
1924 (if (>= len limit)
1925 (setq candidates nil)
1926 (push comp msg)))
1927
1928 (mapconcat 'identity (nreverse msg) " ")))
1929
1930 (defun company-echo-strip-common-format ()
1931
1932 (let ((limit (window-width (minibuffer-window)))
1933 (len (+ (length company-prefix) 2))
1934 ;; Roll to selection.
1935 (candidates (nthcdr company-selection company-candidates))
1936 (i (if company-show-numbers company-selection 99999))
1937 msg comp)
1938
1939 (while candidates
1940 (setq comp (company-strip-prefix (pop candidates))
1941 len (+ len 2 (length comp)))
1942 (when (< i 10)
1943 ;; Add number.
1944 (setq comp (format "%s (%d)" comp i))
1945 (incf len 4)
1946 (incf i))
1947 (if (>= len limit)
1948 (setq candidates nil)
1949 (push (propertize comp 'face 'company-echo) msg)))
1950
1951 (concat (propertize company-prefix 'face 'company-echo-common) "{"
1952 (mapconcat 'identity (nreverse msg) ", ")
1953 "}")))
1954
1955 (defun company-echo-hide ()
1956 (when company-echo-timer
1957 (cancel-timer company-echo-timer))
1958 (unless (equal company-echo-last-msg "")
1959 (setq company-echo-last-msg "")
1960 (company-echo-show)))
1961
1962 (defun company-echo-frontend (command)
1963 "A `company-mode' front-end showing the candidates in the echo area."
1964 (case command
1965 ('pre-command (company-echo-show-soon))
1966 ('post-command (company-echo-show-soon 'company-echo-format))
1967 ('hide (company-echo-hide))))
1968
1969 (defun company-echo-strip-common-frontend (command)
1970 "A `company-mode' front-end showing the candidates in the echo area."
1971 (case command
1972 ('pre-command (company-echo-show-soon))
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 ('pre-command (company-echo-show-soon))
1980 ('post-command (company-echo-show-soon 'company-fetch-metadata))
1981 ('hide (company-echo-hide))))
1982
1983 ;; templates ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1984
1985 (autoload 'company-template-declare-template "company-template")
1986
1987 (provide 'company)
1988 ;;; company.el ends here