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