]> code.delx.au - gnu-emacs-elpa/blob - company.el
company-buffer-lines: Speed up
[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 ;; Improved tooltip performance.
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 (push 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 () (company-mode 1)))
626
627 (defsubst company-assert-enabled ()
628 (unless company-mode
629 (company-uninstall-map)
630 (error "Company not enabled")))
631
632 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
633
634 (defvar company-my-keymap nil)
635 (make-variable-buffer-local 'company-my-keymap)
636
637 (defvar company-emulation-alist '((t . nil)))
638
639 (defsubst company-enable-overriding-keymap (keymap)
640 (company-uninstall-map)
641 (setq company-my-keymap keymap))
642
643 (defun company-ensure-emulation-alist ()
644 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
645 (setq emulation-mode-map-alists
646 (cons 'company-emulation-alist
647 (delq 'company-emulation-alist emulation-mode-map-alists)))))
648
649 (defun company-install-map ()
650 (unless (or (cdar company-emulation-alist)
651 (null company-my-keymap))
652 (setf (cdar company-emulation-alist) company-my-keymap)))
653
654 (defun company-uninstall-map ()
655 (setf (cdar company-emulation-alist) nil))
656
657 ;; Hack:
658 ;; Emacs calculates the active keymaps before reading the event. That means we
659 ;; cannot change the keymap from a timer. So we send a bogus command.
660 (defun company-ignore ()
661 (interactive)
662 (setq this-command last-command))
663
664 (global-set-key '[31415926] 'company-ignore)
665
666 (defun company-input-noop ()
667 (push 31415926 unread-command-events))
668
669 ;; Hack:
670 ;; posn-col-row is incorrect in older Emacsen when line-spacing is set
671 (defun company--col-row (&optional pos)
672 (let ((posn (posn-at-point pos)))
673 (cons (car (posn-col-row posn)) (cdr (posn-actual-col-row posn)))))
674
675 (defsubst company--column (&optional pos)
676 (car (posn-col-row (posn-at-point pos))))
677
678 (defsubst company--row (&optional pos)
679 (cdr (posn-actual-col-row (posn-at-point pos))))
680
681 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
682
683 (defun company-grab (regexp &optional expression limit)
684 (when (looking-back regexp limit)
685 (or (match-string-no-properties (or expression 0)) "")))
686
687 (defun company-grab-line (regexp &optional expression)
688 (company-grab regexp expression (point-at-bol)))
689
690 (defun company-grab-symbol ()
691 (if (looking-at "\\_>")
692 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
693 (point)))
694 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
695 "")))
696
697 (defun company-grab-word ()
698 (if (looking-at "\\>")
699 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
700 (point)))
701 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
702 "")))
703
704 (defun company-in-string-or-comment ()
705 (let ((ppss (syntax-ppss)))
706 (or (car (setq ppss (nthcdr 3 ppss)))
707 (car (setq ppss (cdr ppss)))
708 (nth 3 ppss))))
709
710 (if (fboundp 'locate-dominating-file)
711 (defalias 'company-locate-dominating-file 'locate-dominating-file)
712 (defun company-locate-dominating-file (file name)
713 (catch 'root
714 (let ((dir (file-name-directory file))
715 (prev-dir nil))
716 (while (not (equal dir prev-dir))
717 (when (file-exists-p (expand-file-name name dir))
718 (throw 'root dir))
719 (setq prev-dir dir
720 dir (file-name-directory (directory-file-name dir))))))))
721
722 (defun company-call-backend (&rest args)
723 (if (functionp company-backend)
724 (apply company-backend args)
725 (apply 'company--multi-backend-adapter company-backend args)))
726
727 (defun company--multi-backend-adapter (backends command &rest args)
728 (case command
729 (candidates
730 (apply 'append (mapcar (lambda (backend) (apply backend command args))
731 backends)))
732 (sorted nil)
733 (duplicates t)
734 (otherwise
735 (let (value)
736 (dolist (backend backends)
737 (when (setq value (apply backend command args))
738 (return value)))))))
739
740 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
741
742 (defvar company-backend nil)
743 (make-variable-buffer-local 'company-backend)
744
745 (defvar company-prefix nil)
746 (make-variable-buffer-local 'company-prefix)
747
748 (defvar company-candidates nil)
749 (make-variable-buffer-local 'company-candidates)
750
751 (defvar company-candidates-length nil)
752 (make-variable-buffer-local 'company-candidates-length)
753
754 (defvar company-candidates-cache nil)
755 (make-variable-buffer-local 'company-candidates-cache)
756
757 (defvar company-candidates-predicate nil)
758 (make-variable-buffer-local 'company-candidates-predicate)
759
760 (defvar company-common nil)
761 (make-variable-buffer-local 'company-common)
762
763 (defvar company-selection 0)
764 (make-variable-buffer-local 'company-selection)
765
766 (defvar company-selection-changed nil)
767 (make-variable-buffer-local 'company-selection-changed)
768
769 (defvar company--explicit-action nil
770 "Non-nil, if explicit completion took place.")
771 (make-variable-buffer-local 'company--explicit-action)
772
773 (defvar company--point-max nil)
774 (make-variable-buffer-local 'company--point-max)
775
776 (defvar company-point nil)
777 (make-variable-buffer-local 'company-point)
778
779 (defvar company-timer nil)
780
781 (defvar company-added-newline nil)
782 (make-variable-buffer-local 'company-added-newline)
783
784 (defsubst company-strip-prefix (str)
785 (substring str (length company-prefix)))
786
787 (defmacro company-with-candidate-inserted (candidate &rest body)
788 "Evaluate BODY with CANDIDATE temporarily inserted.
789 This is a tool for back-ends that need candidates inserted before they
790 can retrieve meta-data for them."
791 (declare (indent 1))
792 `(let ((inhibit-modification-hooks t)
793 (inhibit-point-motion-hooks t)
794 (modified-p (buffer-modified-p)))
795 (insert (company-strip-prefix ,candidate))
796 (unwind-protect
797 (progn ,@body)
798 (delete-region company-point (point)))))
799
800 (defun company-explicit-action-p ()
801 "Return whether explicit completion action was taken by the user."
802 (or company--explicit-action
803 company-selection-changed))
804
805 (defsubst company-reformat (candidate)
806 ;; company-ispell needs this, because the results are always lower-case
807 ;; It's mory efficient to fix it only when they are displayed.
808 (concat company-prefix (substring candidate (length company-prefix))))
809
810 (defun company--should-complete ()
811 (and (not (or buffer-read-only overriding-terminal-local-map
812 overriding-local-map
813 (minibufferp)))
814 ;; Check if in the middle of entering a key combination.
815 (or (equal (this-command-keys-vector) [])
816 (not (keymapp (key-binding (this-command-keys-vector)))))
817 (eq company-idle-delay t)
818 (or (eq t company-begin-commands)
819 (memq this-command company-begin-commands)
820 (and (symbolp this-command) (get this-command 'company-begin)))
821 (not (and transient-mark-mode mark-active))))
822
823 (defsubst company-call-frontends (command)
824 (dolist (frontend company-frontends)
825 (condition-case err
826 (funcall frontend command)
827 (error (error "Company: Front-end %s error \"%s\" on command %s"
828 frontend (error-message-string err) command)))))
829
830 (defsubst company-set-selection (selection &optional force-update)
831 (setq selection (max 0 (min (1- company-candidates-length) selection)))
832 (when (or force-update (not (equal selection company-selection)))
833 (setq company-selection selection
834 company-selection-changed t)
835 (company-call-frontends 'update)))
836
837 (defun company-apply-predicate (candidates predicate)
838 (let (new)
839 (dolist (c candidates)
840 (when (funcall predicate c)
841 (push c new)))
842 (nreverse new)))
843
844 (defun company-update-candidates (candidates)
845 (setq company-candidates-length (length candidates))
846 (if (> company-selection 0)
847 ;; Try to restore the selection
848 (let ((selected (nth company-selection company-candidates)))
849 (setq company-selection 0
850 company-candidates candidates)
851 (when selected
852 (while (and candidates (string< (pop candidates) selected))
853 (incf company-selection))
854 (unless candidates
855 ;; Make sure selection isn't out of bounds.
856 (setq company-selection (min (1- company-candidates-length)
857 company-selection)))))
858 (setq company-selection 0
859 company-candidates candidates))
860 ;; Save in cache:
861 (push (cons company-prefix company-candidates) company-candidates-cache)
862 ;; Calculate common.
863 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
864 (setq company-common (try-completion company-prefix company-candidates)))
865 (when (eq company-common t)
866 (setq company-candidates nil)))
867
868 (defun company-calculate-candidates (prefix)
869 (let ((candidates (cdr (assoc prefix company-candidates-cache))))
870 (or candidates
871 (when company-candidates-cache
872 (let ((len (length prefix))
873 (completion-ignore-case (company-call-backend 'ignore-case))
874 prev)
875 (dotimes (i (1+ len))
876 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
877 company-candidates-cache)))
878 (setq candidates (all-completions prefix prev))
879 (return t)))))
880 ;; no cache match, call back-end
881 (progn
882 (setq candidates (company-call-backend 'candidates prefix))
883 (when company-candidates-predicate
884 (setq candidates
885 (company-apply-predicate candidates
886 company-candidates-predicate)))
887 (unless (company-call-backend 'sorted)
888 (setq candidates (sort candidates 'string<)))
889 (when (company-call-backend 'duplicates)
890 ;; strip duplicates
891 (let ((c2 candidates))
892 (while c2
893 (setcdr c2 (progn (while (equal (pop c2) (car c2)))
894 c2)))))))
895 (if (or (cdr candidates)
896 (not (equal (car candidates) prefix)))
897 ;; Don't start when already completed and unique.
898 candidates
899 ;; Not the right place? maybe when setting?
900 (and company-candidates t))))
901
902 (defun company-idle-begin (buf win tick pos)
903 (and company-mode
904 (eq buf (current-buffer))
905 (eq win (selected-window))
906 (eq tick (buffer-chars-modified-tick))
907 (eq pos (point))
908 (not company-candidates)
909 (not (equal (point) company-point))
910 (let ((company-idle-delay t)
911 (company-begin-commands t))
912 (company-begin)
913 (when company-candidates
914 (company-input-noop)
915 (company-post-command)))))
916
917 (defun company-auto-begin ()
918 (company-assert-enabled)
919 (and company-mode
920 (not company-candidates)
921 (let ((company-idle-delay t)
922 (company-minimum-prefix-length 0)
923 (company-begin-commands t))
924 (company-begin)))
925 ;; Return non-nil if active.
926 company-candidates)
927
928 (defun company-manual-begin ()
929 (interactive)
930 (setq company--explicit-action t)
931 (company-auto-begin))
932
933 (defun company-other-backend (&optional backward)
934 (interactive (list current-prefix-arg))
935 (company-assert-enabled)
936 (if company-backend
937 (let* ((after (cdr (member company-backend company-backends)))
938 (before (cdr (member company-backend (reverse company-backends))))
939 (next (if backward
940 (append before (reverse after))
941 (append after (reverse before)))))
942 (company-cancel)
943 (dolist (backend next)
944 (when (ignore-errors (company-begin-backend backend))
945 (return t))))
946 (company-manual-begin))
947 (unless company-candidates
948 (error "No other back-end")))
949
950 (defun company-require-match-p ()
951 (let ((backend-value (company-call-backend 'require-match)))
952 (or (eq backend-value t)
953 (and (if (functionp company-require-match)
954 (funcall company-require-match)
955 (eq company-require-match t))
956 (not (eq backend-value 'never))))))
957
958 (defun company-punctuation-p (input)
959 "Return non-nil, if input starts with punctuation or parentheses."
960 (memq (char-syntax (string-to-char input)) '(?. ?\( ?\))))
961
962 (defun company-auto-complete-p (input)
963 "Return non-nil, if input starts with punctuation or parentheses."
964 (and (if (functionp company-auto-complete)
965 (funcall company-auto-complete)
966 company-auto-complete)
967 (if (functionp company-auto-complete-chars)
968 (funcall company-auto-complete-chars input)
969 (if (consp company-auto-complete-chars)
970 (memq (char-syntax (string-to-char input))
971 company-auto-complete-chars)
972 (string-match (substring input 0 1) company-auto-complete-chars)))))
973
974 (defun company--incremental-p ()
975 (and (> (point) company-point)
976 (> (point-max) company--point-max)
977 (not (eq this-command 'backward-delete-char-untabify))
978 (equal (buffer-substring (- company-point (length company-prefix))
979 company-point)
980 company-prefix)))
981
982 (defsubst company--string-incremental-p (old-prefix new-prefix)
983 (and (> (length new-prefix) (length old-prefix))
984 (equal old-prefix (substring new-prefix 0 (length old-prefix)))))
985
986 (defun company--continue-failed (new-prefix)
987 (when (company--incremental-p)
988 (let ((input (buffer-substring-no-properties (point) company-point)))
989 (cond
990 ((company-auto-complete-p input)
991 ;; auto-complete
992 (save-excursion
993 (goto-char company-point)
994 (company-complete-selection)
995 nil))
996 ((and (company--string-incremental-p company-prefix new-prefix)
997 (company-require-match-p))
998 ;; wrong incremental input, but required match
999 (backward-delete-char (length input))
1000 (ding)
1001 (message "Matching input is required")
1002 company-candidates)
1003 ((equal company-prefix (car company-candidates))
1004 ;; last input was actually success
1005 (company-cancel company-prefix)
1006 nil)))))
1007
1008 (defun company--good-prefix-p (prefix)
1009 (and (or (company-explicit-action-p)
1010 (>= (or (cdr-safe prefix) (length prefix))
1011 company-minimum-prefix-length))
1012 (stringp (or (car-safe prefix) prefix))))
1013
1014 (defun company--continue ()
1015 (when (company-call-backend 'no-cache company-prefix)
1016 ;; Don't complete existing candidates, fetch new ones.
1017 (setq company-candidates-cache nil))
1018 (let* ((new-prefix (company-call-backend 'prefix))
1019 (c (when (and (company--good-prefix-p new-prefix)
1020 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1021 (= (- (point) (length new-prefix))
1022 (- company-point (length company-prefix))))
1023 (setq new-prefix (or (car-safe new-prefix) new-prefix))
1024 (company-calculate-candidates new-prefix))))
1025 (or (cond
1026 ((eq c t)
1027 ;; t means complete/unique.
1028 (company-cancel new-prefix)
1029 nil)
1030 ((consp c)
1031 ;; incremental match
1032 (setq company-prefix new-prefix)
1033 (company-update-candidates c)
1034 c)
1035 (t (company--continue-failed new-prefix)))
1036 (company-cancel))))
1037
1038 (defun company--begin-new ()
1039 (let (prefix c)
1040 (dolist (backend (if company-backend
1041 ;; prefer manual override
1042 (list company-backend)
1043 company-backends))
1044 (setq prefix
1045 (if (or (symbolp backend)
1046 (functionp backend))
1047 (when (or (not (symbolp backend))
1048 (eq t (get backend 'company-init))
1049 (unless (get backend 'company-init)
1050 (company-init-backend backend)))
1051 (funcall backend 'prefix))
1052 (company--multi-backend-adapter backend 'prefix)))
1053 (when prefix
1054 (when (company--good-prefix-p prefix)
1055 (setq prefix (or (car-safe prefix) prefix)
1056 company-backend backend
1057 c (company-calculate-candidates prefix))
1058 ;; t means complete/unique. We don't start, so no hooks.
1059 (if (not (consp c))
1060 (when company--explicit-action
1061 (message "No completion found"))
1062 (setq company-prefix prefix)
1063 (when (symbolp backend)
1064 (setq company-lighter (concat " " (symbol-name backend))))
1065 (company-update-candidates c)
1066 (run-hook-with-args 'company-completion-started-hook
1067 (company-explicit-action-p))
1068 (company-call-frontends 'show)))
1069 (return c)))))
1070
1071 (defun company-begin ()
1072 (or (and company-candidates (company--continue))
1073 (and (company--should-complete) (company--begin-new)))
1074 (when company-candidates
1075 (when (and company-end-of-buffer-workaround (eobp))
1076 (save-excursion (insert "\n"))
1077 (setq company-added-newline (buffer-chars-modified-tick)))
1078 (setq company-point (point)
1079 company--point-max (point-max))
1080 (company-ensure-emulation-alist)
1081 (company-enable-overriding-keymap company-active-map)
1082 (company-call-frontends 'update)))
1083
1084 (defun company-cancel (&optional result)
1085 (and company-added-newline
1086 (> (point-max) (point-min))
1087 (let ((tick (buffer-chars-modified-tick)))
1088 (delete-region (1- (point-max)) (point-max))
1089 (equal tick company-added-newline))
1090 ;; Only set unmodified when tick remained the same since insert.
1091 (set-buffer-modified-p nil))
1092 (when company-prefix
1093 (if (stringp result)
1094 (progn
1095 (company-call-backend 'pre-completion result)
1096 (run-hook-with-args 'company-completion-finished-hook result)
1097 (company-call-backend 'post-completion result))
1098 (run-hook-with-args 'company-completion-cancelled-hook result)))
1099 (setq company-added-newline nil
1100 company-backend nil
1101 company-prefix nil
1102 company-candidates nil
1103 company-candidates-length nil
1104 company-candidates-cache nil
1105 company-candidates-predicate nil
1106 company-common nil
1107 company-selection 0
1108 company-selection-changed nil
1109 company--explicit-action nil
1110 company-lighter company-default-lighter
1111 company--point-max nil
1112 company-point nil)
1113 (when company-timer
1114 (cancel-timer company-timer))
1115 (company-search-mode 0)
1116 (company-call-frontends 'hide)
1117 (company-enable-overriding-keymap nil))
1118
1119 (defun company-abort ()
1120 (interactive)
1121 (company-cancel t)
1122 ;; Don't start again, unless started manually.
1123 (setq company-point (point)))
1124
1125 (defun company-finish (result)
1126 (insert (company-strip-prefix result))
1127 (company-cancel result)
1128 ;; Don't start again, unless started manually.
1129 (setq company-point (point)))
1130
1131 (defsubst company-keep (command)
1132 (and (symbolp command) (get command 'company-keep)))
1133
1134 (defun company-pre-command ()
1135 (unless (company-keep this-command)
1136 (condition-case err
1137 (when company-candidates
1138 (company-call-frontends 'pre-command))
1139 (error (message "Company: An error occurred in pre-command")
1140 (message "%s" (error-message-string err))
1141 (company-cancel))))
1142 (when company-timer
1143 (cancel-timer company-timer)
1144 (setq company-timer nil))
1145 (company-uninstall-map))
1146
1147 (defun company-post-command ()
1148 (unless (company-keep this-command)
1149 (condition-case err
1150 (progn
1151 (unless (equal (point) company-point)
1152 (company-begin))
1153 (if company-candidates
1154 (company-call-frontends 'post-command)
1155 (and (numberp company-idle-delay)
1156 (or (eq t company-begin-commands)
1157 (memq this-command company-begin-commands))
1158 (setq company-timer
1159 (run-with-timer company-idle-delay nil
1160 'company-idle-begin
1161 (current-buffer) (selected-window)
1162 (buffer-chars-modified-tick) (point))))))
1163 (error (message "Company: An error occurred in post-command")
1164 (message "%s" (error-message-string err))
1165 (company-cancel))))
1166 (company-install-map))
1167
1168 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1169
1170 (defvar company-search-string nil)
1171 (make-variable-buffer-local 'company-search-string)
1172
1173 (defvar company-search-lighter " Search: \"\"")
1174 (make-variable-buffer-local 'company-search-lighter)
1175
1176 (defvar company-search-old-map nil)
1177 (make-variable-buffer-local 'company-search-old-map)
1178
1179 (defvar company-search-old-selection 0)
1180 (make-variable-buffer-local 'company-search-old-selection)
1181
1182 (defun company-search (text lines)
1183 (let ((quoted (regexp-quote text))
1184 (i 0))
1185 (dolist (line lines)
1186 (when (string-match quoted line (length company-prefix))
1187 (return i))
1188 (incf i))))
1189
1190 (defun company-search-printing-char ()
1191 (interactive)
1192 (company-search-assert-enabled)
1193 (setq company-search-string
1194 (concat (or company-search-string "") (string last-command-event))
1195 company-search-lighter (concat " Search: \"" company-search-string
1196 "\""))
1197 (let ((pos (company-search company-search-string
1198 (nthcdr company-selection company-candidates))))
1199 (if (null pos)
1200 (ding)
1201 (company-set-selection (+ company-selection pos) t))))
1202
1203 (defun company-search-repeat-forward ()
1204 "Repeat the incremental search in completion candidates forward."
1205 (interactive)
1206 (company-search-assert-enabled)
1207 (let ((pos (company-search company-search-string
1208 (cdr (nthcdr company-selection
1209 company-candidates)))))
1210 (if (null pos)
1211 (ding)
1212 (company-set-selection (+ company-selection pos 1) t))))
1213
1214 (defun company-search-repeat-backward ()
1215 "Repeat the incremental search in completion candidates backwards."
1216 (interactive)
1217 (company-search-assert-enabled)
1218 (let ((pos (company-search company-search-string
1219 (nthcdr (- company-candidates-length
1220 company-selection)
1221 (reverse company-candidates)))))
1222 (if (null pos)
1223 (ding)
1224 (company-set-selection (- company-selection pos 1) t))))
1225
1226 (defun company-create-match-predicate ()
1227 (setq company-candidates-predicate
1228 `(lambda (candidate)
1229 ,(if company-candidates-predicate
1230 `(and (string-match ,company-search-string candidate)
1231 (funcall ,company-candidates-predicate
1232 candidate))
1233 `(string-match ,company-search-string candidate))))
1234 (company-update-candidates
1235 (company-apply-predicate company-candidates company-candidates-predicate))
1236 ;; Invalidate cache.
1237 (setq company-candidates-cache (cons company-prefix company-candidates)))
1238
1239 (defun company-filter-printing-char ()
1240 (interactive)
1241 (company-search-assert-enabled)
1242 (company-search-printing-char)
1243 (company-create-match-predicate)
1244 (company-call-frontends 'update))
1245
1246 (defun company-search-kill-others ()
1247 "Limit the completion candidates to the ones matching the search string."
1248 (interactive)
1249 (company-search-assert-enabled)
1250 (company-create-match-predicate)
1251 (company-search-mode 0)
1252 (company-call-frontends 'update))
1253
1254 (defun company-search-abort ()
1255 "Abort searching the completion candidates."
1256 (interactive)
1257 (company-search-assert-enabled)
1258 (company-set-selection company-search-old-selection t)
1259 (company-search-mode 0))
1260
1261 (defun company-search-other-char ()
1262 (interactive)
1263 (company-search-assert-enabled)
1264 (company-search-mode 0)
1265 (when last-input-event
1266 (clear-this-command-keys t)
1267 (setq unread-command-events (list last-input-event))))
1268
1269 (defvar company-search-map
1270 (let ((i 0)
1271 (keymap (make-keymap)))
1272 (if (fboundp 'max-char)
1273 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1274 'company-search-printing-char)
1275 (with-no-warnings
1276 ;; obselete in Emacs 23
1277 (let ((l (generic-character-list))
1278 (table (nth 1 keymap)))
1279 (while l
1280 (set-char-table-default table (car l) 'company-search-printing-char)
1281 (setq l (cdr l))))))
1282 (define-key keymap [t] 'company-search-other-char)
1283 (while (< i ?\s)
1284 (define-key keymap (make-string 1 i) 'company-search-other-char)
1285 (incf i))
1286 (while (< i 256)
1287 (define-key keymap (vector i) 'company-search-printing-char)
1288 (incf i))
1289 (let ((meta-map (make-sparse-keymap)))
1290 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1291 (define-key keymap [escape] meta-map))
1292 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1293 (define-key keymap "\e\e\e" 'company-search-other-char)
1294 (define-key keymap [escape escape escape] 'company-search-other-char)
1295
1296 (define-key keymap "\C-g" 'company-search-abort)
1297 (define-key keymap "\C-s" 'company-search-repeat-forward)
1298 (define-key keymap "\C-r" 'company-search-repeat-backward)
1299 (define-key keymap "\C-o" 'company-search-kill-others)
1300 keymap)
1301 "Keymap used for incrementally searching the completion candidates.")
1302
1303 (define-minor-mode company-search-mode
1304 "Search mode for completion candidates.
1305 Don't start this directly, use `company-search-candidates' or
1306 `company-filter-candidates'."
1307 nil company-search-lighter nil
1308 (if company-search-mode
1309 (if (company-manual-begin)
1310 (progn
1311 (setq company-search-old-selection company-selection)
1312 (company-call-frontends 'update))
1313 (setq company-search-mode nil))
1314 (kill-local-variable 'company-search-string)
1315 (kill-local-variable 'company-search-lighter)
1316 (kill-local-variable 'company-search-old-selection)
1317 (company-enable-overriding-keymap company-active-map)))
1318
1319 (defsubst company-search-assert-enabled ()
1320 (company-assert-enabled)
1321 (unless company-search-mode
1322 (company-uninstall-map)
1323 (error "Company not in search mode")))
1324
1325 (defun company-search-candidates ()
1326 "Start searching the completion candidates incrementally.
1327
1328 \\<company-search-map>Search can be controlled with the commands:
1329 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1330 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1331 - `company-search-abort' (\\[company-search-abort])
1332
1333 Regular characters are appended to the search string.
1334
1335 The command `company-search-kill-others' (\\[company-search-kill-others]) uses
1336 the search string to limit the completion candidates."
1337 (interactive)
1338 (company-search-mode 1)
1339 (company-enable-overriding-keymap company-search-map))
1340
1341 (defvar company-filter-map
1342 (let ((keymap (make-keymap)))
1343 (define-key keymap [remap company-search-printing-char]
1344 'company-filter-printing-char)
1345 (set-keymap-parent keymap company-search-map)
1346 keymap)
1347 "Keymap used for incrementally searching the completion candidates.")
1348
1349 (defun company-filter-candidates ()
1350 "Start filtering the completion candidates incrementally.
1351 This works the same way as `company-search-candidates' immediately
1352 followed by `company-search-kill-others' after each input."
1353 (interactive)
1354 (company-search-mode 1)
1355 (company-enable-overriding-keymap company-filter-map))
1356
1357 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1358
1359 (defun company-select-next ()
1360 "Select the next candidate in the list."
1361 (interactive)
1362 (when (company-manual-begin)
1363 (company-set-selection (1+ company-selection))))
1364
1365 (defun company-select-previous ()
1366 "Select the previous candidate in the list."
1367 (interactive)
1368 (when (company-manual-begin)
1369 (company-set-selection (1- company-selection))))
1370
1371 (defun company-select-mouse (event)
1372 "Select the candidate picked by the mouse."
1373 (interactive "e")
1374 (when (nth 4 (event-start event))
1375 (company-set-selection (- (cdr (posn-actual-col-row (event-start event)))
1376 (company--row)
1377 1))
1378 t))
1379
1380 (defun company-complete-mouse (event)
1381 "Complete the candidate picked by the mouse."
1382 (interactive "e")
1383 (when (company-select-mouse event)
1384 (company-complete-selection)))
1385
1386 (defun company-complete-selection ()
1387 "Complete the selected candidate."
1388 (interactive)
1389 (when (company-manual-begin)
1390 (company-finish (nth company-selection company-candidates))))
1391
1392 (defun company-complete-common ()
1393 "Complete the common part of all candidates."
1394 (interactive)
1395 (when (company-manual-begin)
1396 (if (and (not (cdr company-candidates))
1397 (equal company-common (car company-candidates)))
1398 (company-complete-selection)
1399 (insert (company-strip-prefix company-common)))))
1400
1401 (defun company-complete ()
1402 "Complete the common part of all candidates or the current selection.
1403 The first time this is called, the common part is completed, the second time, or
1404 when the selection has been changed, the selected candidate is completed."
1405 (interactive)
1406 (when (company-manual-begin)
1407 (if (or company-selection-changed
1408 (eq last-command 'company-complete-common))
1409 (call-interactively 'company-complete-selection)
1410 (call-interactively 'company-complete-common)
1411 (setq this-command 'company-complete-common))))
1412
1413 (defun company-complete-number (n)
1414 "Complete the Nth candidate.
1415 To show the number next to the candidates in some back-ends, enable
1416 `company-show-numbers'."
1417 (when (company-manual-begin)
1418 (and (< n 1) (> n company-candidates-length)
1419 (error "No candidate number %d" n))
1420 (decf n)
1421 (company-finish (nth n company-candidates))))
1422
1423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1424
1425 (defconst company-space-strings-limit 100)
1426
1427 (defconst company-space-strings
1428 (let (lst)
1429 (dotimes (i company-space-strings-limit)
1430 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1431 (apply 'vector lst)))
1432
1433 (defsubst company-space-string (len)
1434 (if (< len company-space-strings-limit)
1435 (aref company-space-strings len)
1436 (make-string len ?\ )))
1437
1438 (defsubst company-safe-substring (str from &optional to)
1439 (if (> from (string-width str))
1440 ""
1441 (with-temp-buffer
1442 (insert str)
1443 (move-to-column from)
1444 (let ((beg (point)))
1445 (if to
1446 (progn
1447 (move-to-column to)
1448 (concat (buffer-substring beg (point))
1449 (let ((padding (- to (current-column))))
1450 (when (> padding 0)
1451 (company-space-string padding)))))
1452 (buffer-substring beg (point-max)))))))
1453
1454 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1455
1456 (defvar company-last-metadata nil)
1457 (make-variable-buffer-local 'company-last-metadata)
1458
1459 (defun company-fetch-metadata ()
1460 (let ((selected (nth company-selection company-candidates)))
1461 (unless (equal selected (car company-last-metadata))
1462 (setq company-last-metadata
1463 (cons selected (company-call-backend 'meta selected))))
1464 (cdr company-last-metadata)))
1465
1466 (defun company-doc-buffer (&optional string)
1467 (with-current-buffer (get-buffer-create "*Company meta-data*")
1468 (erase-buffer)
1469 (current-buffer)))
1470
1471 (defvar company--electric-commands
1472 '(scroll-other-window scroll-other-window-down)
1473 "List of Commands that won't break out of electric commands.")
1474
1475 (defmacro company--electric-do (&rest body)
1476 (declare (indent 0) (debug t))
1477 `(when (company-manual-begin)
1478 (save-window-excursion
1479 (let ((height (window-height))
1480 (row (company--row))
1481 cmd)
1482 ,@body
1483 (and (< (window-height) height)
1484 (< (- (window-height) row 2) company-tooltip-limit)
1485 (recenter (- (window-height) row 2)))
1486 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1487 company--electric-commands)
1488 (call-interactively cmd))
1489 (when last-input-event
1490 (clear-this-command-keys t)
1491 (setq unread-command-events (list last-input-event)))))))
1492
1493 (defun company-show-doc-buffer ()
1494 "Temporarily show a buffer with the complete documentation for the selection."
1495 (interactive)
1496 (company--electric-do
1497 (let* ((selected (nth company-selection company-candidates))
1498 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1499 (error "No documentation available"))))
1500 (with-current-buffer doc-buffer
1501 (goto-char (point-min)))
1502 (display-buffer doc-buffer t))))
1503 (put 'company-show-doc-buffer 'company-keep t)
1504
1505 (defun company-show-location ()
1506 "Temporarily display a buffer showing the selected candidate in context."
1507 (interactive)
1508 (company--electric-do
1509 (let* ((selected (nth company-selection company-candidates))
1510 (location (company-call-backend 'location selected))
1511 (pos (or (cdr location) (error "No location available")))
1512 (buffer (or (and (bufferp (car location)) (car location))
1513 (find-file-noselect (car location) t))))
1514 (with-selected-window (display-buffer buffer t)
1515 (save-restriction
1516 (widen)
1517 (if (bufferp (car location))
1518 (goto-char pos)
1519 (goto-char (point-min))
1520 (forward-line (1- pos))))
1521 (set-window-start nil (point))))))
1522 (put 'company-show-location 'company-keep t)
1523
1524 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1525
1526 (defvar company-callback nil)
1527 (make-variable-buffer-local 'company-callback)
1528
1529 (defvar company-begin-with-marker nil)
1530 (make-variable-buffer-local 'company-begin-with-marker)
1531
1532 (defun company-remove-callback (&optional ignored)
1533 (remove-hook 'company-completion-finished-hook company-callback t)
1534 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1535 (remove-hook 'company-completion-finished-hook 'company-remove-callback t)
1536 (when company-begin-with-marker
1537 (set-marker company-begin-with-marker nil)))
1538
1539 (defun company-begin-backend (backend &optional callback)
1540 "Start a completion at point using BACKEND."
1541 (interactive (let ((val (completing-read "Company back-end: "
1542 obarray
1543 'functionp nil "company-")))
1544 (when val
1545 (list (intern val)))))
1546 (when (setq company-callback callback)
1547 (add-hook 'company-completion-finished-hook company-callback nil t))
1548 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1549 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1550 (setq company-backend backend)
1551 ;; Return non-nil if active.
1552 (or (company-manual-begin)
1553 (error "Cannot complete at point")))
1554
1555 (defun company-begin-with (candidates
1556 &optional prefix-length require-match callback)
1557 "Start a completion at point.
1558 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length of
1559 the prefix that already is in the buffer before point. It defaults to 0.
1560
1561 CALLBACK is a function called with the selected result if the user successfully
1562 completes the input.
1563
1564 Example:
1565 \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1566 (setq company-begin-with-marker (copy-marker (point) t))
1567 (company-begin-backend
1568 `(lambda (command &optional arg &rest ignored)
1569 (cond
1570 ((eq command 'prefix)
1571 (when (equal (point) (marker-position company-begin-with-marker))
1572 (buffer-substring ,(- (point) (or prefix-length 0)) (point))))
1573 ((eq command 'candidates)
1574 (all-completions arg ',candidates))
1575 ((eq command 'require-match)
1576 ,require-match)))
1577 callback))
1578
1579 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1580
1581 (defvar company-pseudo-tooltip-overlay nil)
1582 (make-variable-buffer-local 'company-pseudo-tooltip-overlay)
1583
1584 (defvar company-tooltip-offset 0)
1585 (make-variable-buffer-local 'company-tooltip-offset)
1586
1587 (defun company-pseudo-tooltip-update-offset (selection num-lines limit)
1588
1589 (decf limit 2)
1590 (setq company-tooltip-offset
1591 (max (min selection company-tooltip-offset)
1592 (- selection -1 limit)))
1593
1594 (when (<= company-tooltip-offset 1)
1595 (incf limit)
1596 (setq company-tooltip-offset 0))
1597
1598 (when (>= company-tooltip-offset (- num-lines limit 1))
1599 (incf limit)
1600 (when (= selection (1- num-lines))
1601 (decf company-tooltip-offset)
1602 (when (<= company-tooltip-offset 1)
1603 (setq company-tooltip-offset 0)
1604 (incf limit))))
1605
1606 limit)
1607
1608 ;;; propertize
1609
1610 (defsubst company-round-tab (arg)
1611 (* (/ (+ arg tab-width) tab-width) tab-width))
1612
1613 (defun company-untabify (str)
1614 (let* ((pieces (split-string str "\t"))
1615 (copy pieces))
1616 (while (cdr copy)
1617 (setcar copy (company-safe-substring
1618 (car copy) 0 (company-round-tab (string-width (car copy)))))
1619 (pop copy))
1620 (apply 'concat pieces)))
1621
1622 (defun company-fill-propertize (line width selected)
1623 (setq line (company-safe-substring line 0 width))
1624 (add-text-properties 0 width '(face company-tooltip
1625 mouse-face company-tooltip-mouse)
1626 line)
1627 (add-text-properties 0 (length company-common)
1628 '(face company-tooltip-common
1629 mouse-face company-tooltip-mouse)
1630 line)
1631 (when selected
1632 (if (and company-search-string
1633 (string-match (regexp-quote company-search-string) line
1634 (length company-prefix)))
1635 (progn
1636 (add-text-properties (match-beginning 0) (match-end 0)
1637 '(face company-tooltip-selection)
1638 line)
1639 (when (< (match-beginning 0) (length company-common))
1640 (add-text-properties (match-beginning 0) (length company-common)
1641 '(face company-tooltip-common-selection)
1642 line)))
1643 (add-text-properties 0 width '(face company-tooltip-selection
1644 mouse-face company-tooltip-selection)
1645 line)
1646 (add-text-properties 0 (length company-common)
1647 '(face company-tooltip-common-selection
1648 mouse-face company-tooltip-selection)
1649 line)))
1650 line)
1651
1652 ;;; replace
1653
1654 (defun company-buffer-lines (beg end)
1655 (goto-char beg)
1656 (let (lines)
1657 (while (and (zerop (forward-line 1))
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