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