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