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