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