]> code.delx.au - gnu-emacs-elpa/blob - company.el
Add package-version property to vars after in 0.7.0
[gnu-emacs-elpa] / company.el
1 ;;; company.el --- Modular text completion framework -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6 ;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
7 ;; URL: http://company-mode.github.io/
8 ;; Version: 0.8.1-cvs
9 ;; Keywords: abbrev, convenience, matching
10 ;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28 ;;
29 ;; Company is a modular completion mechanism. Modules for retrieving completion
30 ;; candidates are called back-ends, modules for displaying them are front-ends.
31 ;;
32 ;; Company comes with many back-ends, e.g. `company-elisp'. These are
33 ;; distributed in separate files and can be used individually.
34 ;;
35 ;; Place company.el and the back-ends you want to use in a directory and add the
36 ;; following to your .emacs:
37 ;; (add-to-list 'load-path "/path/to/company")
38 ;; (autoload 'company-mode "company" nil t)
39 ;;
40 ;; Enable company-mode with M-x company-mode. For further information look at
41 ;; the documentation for `company-mode' (C-h f company-mode RET)
42 ;;
43 ;; If you want to start a specific back-end, call it interactively or use
44 ;; `company-begin-backend'. For example:
45 ;; M-x company-abbrev will prompt for and insert an abbrev.
46 ;;
47 ;; To write your own back-end, look at the documentation for `company-backends'.
48 ;; Here is a simple example completing "foo":
49 ;;
50 ;; (defun company-my-backend (command &optional arg &rest ignored)
51 ;; (pcase command
52 ;; (`prefix (when (looking-back "foo\\>")
53 ;; (match-string 0)))
54 ;; (`candidates (list "foobar" "foobaz" "foobarbaz"))
55 ;; (`meta (format "This value is named %s" arg))))
56 ;;
57 ;; Sometimes it is a good idea to mix several back-ends together, for example to
58 ;; enrich gtags with dabbrev-code results (to emulate local variables).
59 ;; To do this, add a list with both back-ends as an element in 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 ;; See NEWS.md in the repository.
69
70 ;;; Code:
71
72 (require 'cl-lib)
73 (require 'newcomment)
74
75 ;; FIXME: Use `user-error'.
76 (add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
77 (add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
78 (add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
79 (add-to-list 'debug-ignored-errors "^Company not ")
80 (add-to-list 'debug-ignored-errors "^No candidate number ")
81 (add-to-list 'debug-ignored-errors "^Cannot complete at point$")
82 (add-to-list 'debug-ignored-errors "^No other back-end$")
83
84 ;;; Compatibility
85 (eval-and-compile
86 ;; `defvar-local' for Emacs 24.2 and below
87 (unless (fboundp 'defvar-local)
88 (defmacro defvar-local (var val &optional docstring)
89 "Define VAR as a buffer-local variable with default value VAL.
90 Like `defvar' but additionally marks the variable as being automatically
91 buffer-local wherever it is set."
92 (declare (debug defvar) (doc-string 3))
93 `(progn
94 (defvar ,var ,val ,docstring)
95 (make-variable-buffer-local ',var)))))
96
97 (defgroup company nil
98 "Extensible inline text completion mechanism"
99 :group 'abbrev
100 :group 'convenience
101 :group 'matching)
102
103 (defface company-tooltip
104 '((default :foreground "black")
105 (((class color) (min-colors 88) (background light))
106 (:background "cornsilk"))
107 (((class color) (min-colors 88) (background dark))
108 (:background "yellow")))
109 "Face used for the tooltip.")
110
111 (defface company-tooltip-selection
112 '((default :inherit company-tooltip)
113 (((class color) (min-colors 88) (background light))
114 (:background "light blue"))
115 (((class color) (min-colors 88) (background dark))
116 (:background "orange1"))
117 (t (:background "green")))
118 "Face used for the selection in the tooltip.")
119
120 (defface company-tooltip-mouse
121 '((default :inherit highlight))
122 "Face used for the tooltip item under the mouse.")
123
124 (defface company-tooltip-common
125 '((default :inherit company-tooltip)
126 (((background light))
127 :foreground "darkred")
128 (((background dark))
129 :foreground "red"))
130 "Face used for the common completion in the tooltip.")
131
132 (defface company-tooltip-common-selection
133 '((default :inherit company-tooltip-selection)
134 (((background light))
135 :foreground "darkred")
136 (((background dark))
137 :foreground "red"))
138 "Face used for the selected common completion in the tooltip.")
139
140 (defface company-tooltip-annotation
141 '((default :inherit company-tooltip)
142 (((background light))
143 :foreground "firebrick4")
144 (((background dark))
145 :foreground "red4"))
146 "Face used for the annotation in the tooltip.")
147
148 (defface company-scrollbar-fg
149 '((((background light))
150 :background "darkred")
151 (((background dark))
152 :background "red"))
153 "Face used for the tooltip scrollbar thumb.")
154
155 (defface company-scrollbar-bg
156 '((default :inherit company-tooltip)
157 (((background light))
158 :background "wheat")
159 (((background dark))
160 :background "gold"))
161 "Face used for the tooltip scrollbar background.")
162
163 (defface company-preview
164 '((((background light))
165 :inherit company-tooltip-selection)
166 (((background dark))
167 :background "blue4"
168 :foreground "wheat"))
169 "Face used for the completion preview.")
170
171 (defface company-preview-common
172 '((((background light))
173 :inherit company-tooltip-selection)
174 (((background dark))
175 :inherit company-preview
176 :foreground "red"))
177 "Face used for the common part of the completion preview.")
178
179 (defface company-preview-search
180 '((((background light))
181 :inherit company-tooltip-common-selection)
182 (((background dark))
183 :inherit company-preview
184 :background "blue1"))
185 "Face used for the search string in the completion preview.")
186
187 (defface company-echo nil
188 "Face used for completions in the echo area.")
189
190 (defface company-echo-common
191 '((((background dark)) (:foreground "firebrick1"))
192 (((background light)) (:background "firebrick4")))
193 "Face used for the common part of completions in the echo area.")
194
195 (defun company-frontends-set (variable value)
196 ;; uniquify
197 (let ((remainder value))
198 (setcdr remainder (delq (car remainder) (cdr remainder))))
199 (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
200 (memq 'company-pseudo-tooltip-frontend value)
201 (error "Pseudo tooltip frontend cannot be used twice"))
202 (and (memq 'company-preview-if-just-one-frontend value)
203 (memq 'company-preview-frontend value)
204 (error "Preview frontend cannot be used twice"))
205 (and (memq 'company-echo value)
206 (memq 'company-echo-metadata-frontend value)
207 (error "Echo area cannot be used twice"))
208 ;; preview must come last
209 (dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
210 (when (memq f value)
211 (setq value (append (delq f value) (list f)))))
212 (set variable value))
213
214 (defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
215 company-preview-if-just-one-frontend
216 company-echo-metadata-frontend)
217 "The list of active front-ends (visualizations).
218 Each front-end is a function that takes one argument. It is called with
219 one of the following arguments:
220
221 `show': When the visualization should start.
222
223 `hide': When the visualization should end.
224
225 `update': When the data has been updated.
226
227 `pre-command': Before every command that is executed while the
228 visualization is active.
229
230 `post-command': After every command that is executed while the
231 visualization is active.
232
233 The visualized data is stored in `company-prefix', `company-candidates',
234 `company-common', `company-selection', `company-point' and
235 `company-search-string'."
236 :set 'company-frontends-set
237 :type '(repeat (choice (const :tag "echo" company-echo-frontend)
238 (const :tag "echo, strip common"
239 company-echo-strip-common-frontend)
240 (const :tag "show echo meta-data in echo"
241 company-echo-metadata-frontend)
242 (const :tag "pseudo tooltip"
243 company-pseudo-tooltip-frontend)
244 (const :tag "pseudo tooltip, multiple only"
245 company-pseudo-tooltip-unless-just-one-frontend)
246 (const :tag "preview" company-preview-frontend)
247 (const :tag "preview, unique only"
248 company-preview-if-just-one-frontend)
249 (function :tag "custom function" nil))))
250
251 (defcustom company-tooltip-limit 10
252 "The maximum number of candidates in the tooltip."
253 :type 'integer)
254
255 (defcustom company-tooltip-minimum 6
256 "The minimum height of the tooltip.
257 If this many lines are not available, prefer to display the tooltip above."
258 :type 'integer)
259
260 (defcustom company-tooltip-minimum-width 0
261 "The minimum width of the tooltip's inner area.
262 This doesn't include the margins and the scroll bar."
263 :type 'integer
264 :package-version '(company . "0.8.0"))
265
266 (defcustom company-tooltip-margin 1
267 "Width of margin columns to show around the toolip."
268 :type 'integer)
269
270 (defcustom company-tooltip-offset-display 'scrollbar
271 "Method using which the tooltip displays scrolling position.
272 `scrollbar' means draw a scrollbar to the right of the items.
273 `lines' means wrap items in lines with \"before\" and \"after\" counters."
274 :type '(choice (const :tag "Scrollbar" scrollbar)
275 (const :tag "Two lines" lines)))
276
277 (defcustom company-tooltip-align-annotations nil
278 "When non-nil, align annotations to the right tooltip border."
279 :type 'boolean
280 :package-version '(company . "0.7.1"))
281
282 (defcustom company-tooltip-flip-when-above nil
283 "Whether to flip the tooltip when it's above the current line."
284 :type 'boolean
285 :package-version '(company . "0.8.1"))
286
287 (defvar company-safe-backends
288 '((company-abbrev . "Abbrev")
289 (company-bbdb . "BBDB")
290 (company-capf . "completion-at-point-functions")
291 (company-clang . "Clang")
292 (company-cmake . "CMake")
293 (company-css . "CSS")
294 (company-dabbrev . "dabbrev for plain text")
295 (company-dabbrev-code . "dabbrev for code")
296 (company-eclim . "Eclim (an Eclipse interface)")
297 (company-elisp . "Emacs Lisp")
298 (company-etags . "etags")
299 (company-files . "Files")
300 (company-gtags . "GNU Global")
301 (company-ispell . "Ispell")
302 (company-keywords . "Programming language keywords")
303 (company-nxml . "nxml")
304 (company-oddmuse . "Oddmuse")
305 (company-pysmell . "PySmell")
306 (company-ropemacs . "ropemacs")
307 (company-semantic . "Semantic")
308 (company-tempo . "Tempo templates")
309 (company-xcode . "Xcode")))
310 (put 'company-safe-backends 'risky-local-variable t)
311
312 (defun company-safe-backends-p (backends)
313 (and (consp backends)
314 (not (cl-dolist (backend backends)
315 (unless (if (consp backend)
316 (company-safe-backends-p backend)
317 (assq backend company-safe-backends))
318 (cl-return t))))))
319
320 (defcustom company-backends `(,@(unless (version< "24.3.50" emacs-version)
321 (list 'company-elisp))
322 company-bbdb
323 company-nxml company-css
324 company-eclim company-semantic company-clang
325 company-xcode company-ropemacs company-cmake
326 company-capf
327 (company-dabbrev-code company-gtags company-etags
328 company-keywords)
329 company-oddmuse company-files company-dabbrev)
330 "The list of active back-ends (completion engines).
331
332 `company-begin-backend' can be used to start a specific back-end,
333 `company-other-backend' will skip to the next matching back-end in the list.
334
335 Each back-end is a function that takes a variable number of arguments.
336 The first argument is the command requested from the back-end. It is one
337 of the following:
338
339 `prefix': The back-end should return the text to be completed. It must be
340 text immediately before point. Returning nil passes control to the next
341 back-end. The function should return `stop' if it should complete but
342 cannot \(e.g. if it is in the middle of a string\). Instead of a string,
343 the back-end may return a cons where car is the prefix and cdr is used in
344 `company-minimum-prefix-length' test. It must be either number or t, and
345 in the latter case the test automatically succeeds.
346
347 `candidates': The second argument is the prefix to be completed. The
348 return value should be a list of candidates that match the prefix.
349
350 Non-prefix matches are also supported (candidates that don't start with the
351 prefix, but match it in some backend-defined way). Backends that use this
352 feature must disable cache (return t to `no-cache') and should also respond
353 to `match'.
354
355 Optional commands:
356
357 `sorted': Return t here to indicate that the candidates are sorted and will
358 not need to be sorted again.
359
360 `duplicates': If non-nil, company will take care of removing duplicates
361 from the list.
362
363 `no-cache': Usually company doesn't ask for candidates again as completion
364 progresses, unless the back-end returns t for this command. The second
365 argument is the latest prefix.
366
367 `meta': The second argument is a completion candidate. Return a (short)
368 documentation string for it.
369
370 `doc-buffer': The second argument is a completion candidate. Return a
371 buffer with documentation for it. Preferably use `company-doc-buffer',
372
373 `location': The second argument is a completion candidate. Return the cons
374 of buffer and buffer location, or of file and line number where the
375 completion candidate was defined.
376
377 `annotation': The second argument is a completion candidate. Return a
378 string to be displayed inline with the candidate in the popup. If
379 duplicates are removed by company, candidates with equal string values will
380 be kept if they have different annotations. For that to work properly,
381 backends should store the related information on candidates using text
382 properties.
383
384 `match': The second argument is a completion candidate. Backends that
385 provide non-prefix completions should return the position of the end of
386 text in the candidate that matches `prefix'. It will be used when
387 rendering the popup.
388
389 `require-match': If this returns t, the user is not allowed to enter
390 anything not offered as a candidate. Use with care! The default value nil
391 gives the user that choice with `company-require-match'. Return value
392 `never' overrides that option the other way around.
393
394 `init': Called once for each buffer. The back-end can check for external
395 programs and files and load any required libraries. Raising an error here
396 will show up in message log once, and the back-end will not be used for
397 completion.
398
399 `post-completion': Called after a completion candidate has been inserted
400 into the buffer. The second argument is the candidate. Can be used to
401 modify it, e.g. to expand a snippet.
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
407 Grouped back-ends:
408
409 An element of `company-backends' can also itself be a list of back-ends,
410 then it's considered to be a \"grouped\" back-end.
411
412 When possible, commands taking a candidate as an argument are dispatched to
413 the back-end it came from. In other cases, the first non-nil value among
414 all the back-ends is returned.
415
416 The latter is the case for the `prefix' command. But if the group contains
417 the keyword `:with', the back-ends after it are ignored for this command.
418
419 The completions from back-ends in a group are merged (but only from those
420 that return the same `prefix').
421
422 Asynchronous back-ends:
423
424 The return value of each command can also be a cons (:async . FETCHER)
425 where FETCHER is a function of one argument, CALLBACK. When the data
426 arrives, FETCHER must call CALLBACK and pass it the appropriate return
427 value, as described above.
428
429 True asynchronous operation is only supported for command `candidates', and
430 only during idle completion. Other commands will block the user interface,
431 even if the back-end uses the asynchronous calling convention."
432 :type `(repeat
433 (choice
434 :tag "Back-end"
435 ,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
436 company-safe-backends)
437 (symbol :tag "User defined")
438 (repeat :tag "Merged Back-ends"
439 (choice :tag "Back-end"
440 ,@(mapcar (lambda (b)
441 `(const :tag ,(cdr b) ,(car b)))
442 company-safe-backends)
443 (const :tag "With" :with)
444 (symbol :tag "User defined"))))))
445
446 (put 'company-backends 'safe-local-variable 'company-safe-backends-p)
447
448 (defcustom company-transformers nil
449 "Functions to change the list of candidates received from backends,
450 after sorting and removal of duplicates (if appropriate).
451 Each function gets called with the return value of the previous one."
452 :type '(choice
453 (const :tag "None" nil)
454 (const :tag "Sort by occurrence" (company-sort-by-occurrence))
455 (repeat :tag "User defined" (function))))
456
457 (defcustom company-completion-started-hook nil
458 "Hook run when company starts completing.
459 The hook is called with one argument that is non-nil if the completion was
460 started manually."
461 :type 'hook)
462
463 (defcustom company-completion-cancelled-hook nil
464 "Hook run when company cancels completing.
465 The hook is called with one argument that is non-nil if the completion was
466 aborted manually."
467 :type 'hook)
468
469 (defcustom company-completion-finished-hook nil
470 "Hook run when company successfully completes.
471 The hook is called with the selected candidate as an argument.
472
473 If you indend to use it to post-process candidates from a specific
474 back-end, consider using the `post-completion' command instead."
475 :type 'hook)
476
477 (defcustom company-minimum-prefix-length 3
478 "The minimum prefix length for idle completion."
479 :type '(integer :tag "prefix length"))
480
481 (defcustom company-abort-manual-when-too-short nil
482 "If enabled, cancel a manually started completion when the prefix gets
483 shorter than both `company-minimum-prefix-length' and the length of the
484 prefix it was started from."
485 :type 'boolean
486 :package-version '(company . "0.8.0"))
487
488 (defcustom company-require-match 'company-explicit-action-p
489 "If enabled, disallow non-matching input.
490 This can be a function do determine if a match is required.
491
492 This can be overridden by the back-end, if it returns t or `never' to
493 `require-match'. `company-auto-complete' also takes precedence over this."
494 :type '(choice (const :tag "Off" nil)
495 (function :tag "Predicate function")
496 (const :tag "On, if user interaction took place"
497 'company-explicit-action-p)
498 (const :tag "On" t)))
499
500 (defcustom company-auto-complete nil
501 "Determines when to auto-complete.
502 If this is enabled, all characters from `company-auto-complete-chars'
503 trigger insertion of the selected completion candidate.
504 This can also be a function."
505 :type '(choice (const :tag "Off" nil)
506 (function :tag "Predicate function")
507 (const :tag "On, if user interaction took place"
508 'company-explicit-action-p)
509 (const :tag "On" t)))
510
511 (defcustom company-auto-complete-chars '(?\ ?\) ?.)
512 "Determines which characters trigger auto-completion.
513 See `company-auto-complete'. If this is a string, each string character
514 tiggers auto-completion. If it is a list of syntax description characters (see
515 `modify-syntax-entry'), all characters with that syntax auto-complete.
516
517 This can also be a function, which is called with the new input and should
518 return non-nil if company should auto-complete.
519
520 A character that is part of a valid candidate never triggers auto-completion."
521 :type '(choice (string :tag "Characters")
522 (set :tag "Syntax"
523 (const :tag "Whitespace" ?\ )
524 (const :tag "Symbol" ?_)
525 (const :tag "Opening parentheses" ?\()
526 (const :tag "Closing parentheses" ?\))
527 (const :tag "Word constituent" ?w)
528 (const :tag "Punctuation." ?.)
529 (const :tag "String quote." ?\")
530 (const :tag "Paired delimiter." ?$)
531 (const :tag "Expression quote or prefix operator." ?\')
532 (const :tag "Comment starter." ?<)
533 (const :tag "Comment ender." ?>)
534 (const :tag "Character-quote." ?/)
535 (const :tag "Generic string fence." ?|)
536 (const :tag "Generic comment fence." ?!))
537 (function :tag "Predicate function")))
538
539 (defcustom company-idle-delay .5
540 "The idle delay in seconds until completion starts automatically.
541 A value of nil means no idle completion, t means show candidates
542 immediately when a prefix of `company-minimum-prefix-length' is reached."
543 :type '(choice (const :tag "never (nil)" nil)
544 (const :tag "immediate (t)" t)
545 (number :tag "seconds")))
546
547 (defcustom company-begin-commands '(self-insert-command org-self-insert-command)
548 "A list of commands after which idle completion is allowed.
549 If this is t, it can show completions after any command. See
550 `company-idle-delay'.
551
552 Alternatively, any command with a non-nil `company-begin' property is
553 treated as if it was on this list."
554 :type '(choice (const :tag "Any command" t)
555 (const :tag "Self insert command" '(self-insert-command))
556 (repeat :tag "Commands" function)))
557
558 (defcustom company-continue-commands '(not save-buffer save-some-buffers
559 save-buffers-kill-terminal
560 save-buffers-kill-emacs)
561 "A list of commands that are allowed during completion.
562 If this is t, or if `company-begin-commands' is t, any command is allowed.
563 Otherwise, the value must be a list of symbols. If it starts with `not',
564 the cdr is the list of commands that abort completion. Otherwise, all
565 commands except those in that list, or in `company-begin-commands', or
566 commands in the `company-' namespace, abort completion."
567 :type '(choice (const :tag "Any command" t)
568 (cons :tag "Any except"
569 (const not)
570 (repeat :tag "Commands" function))
571 (repeat :tag "Commands" function)))
572
573 (defcustom company-show-numbers nil
574 "If enabled, show quick-access numbers for the first ten candidates."
575 :type '(choice (const :tag "off" nil)
576 (const :tag "on" t)))
577
578 (defcustom company-selection-wrap-around nil
579 "If enabled, selecting item before first or after last wraps around."
580 :type '(choice (const :tag "off" nil)
581 (const :tag "on" t)))
582
583 (defvar company-end-of-buffer-workaround t
584 "Work around a visualization bug when completing at the end of the buffer.
585 The work-around consists of adding a newline.")
586
587 (defvar company-async-wait 0.03
588 "Pause between checks to see if the value's been set when turning an
589 asynchronous call into synchronous.")
590
591 (defvar company-async-timeout 2
592 "Maximum wait time for a value to be set during asynchronous call.")
593
594 ;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
595
596 (defvar company-mode-map (make-sparse-keymap)
597 "Keymap used by `company-mode'.")
598
599 (defvar company-active-map
600 (let ((keymap (make-sparse-keymap)))
601 (define-key keymap "\e\e\e" 'company-abort)
602 (define-key keymap "\C-g" 'company-abort)
603 (define-key keymap (kbd "M-n") 'company-select-next)
604 (define-key keymap (kbd "M-p") 'company-select-previous)
605 (define-key keymap (kbd "<down>") 'company-select-next-or-abort)
606 (define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
607 (define-key keymap [down-mouse-1] 'ignore)
608 (define-key keymap [down-mouse-3] 'ignore)
609 (define-key keymap [mouse-1] 'company-complete-mouse)
610 (define-key keymap [mouse-3] 'company-select-mouse)
611 (define-key keymap [up-mouse-1] 'ignore)
612 (define-key keymap [up-mouse-3] 'ignore)
613 (define-key keymap [return] 'company-complete-selection)
614 (define-key keymap (kbd "RET") 'company-complete-selection)
615 (define-key keymap [tab] 'company-complete-common)
616 (define-key keymap (kbd "TAB") 'company-complete-common)
617 (define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
618 (define-key keymap "\C-w" 'company-show-location)
619 (define-key keymap "\C-s" 'company-search-candidates)
620 (define-key keymap "\C-\M-s" 'company-filter-candidates)
621 (dotimes (i 10)
622 (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
623 `(lambda ()
624 (interactive)
625 (company-complete-number ,(if (zerop i) 10 i)))))
626
627 keymap)
628 "Keymap that is enabled during an active completion.")
629
630 (defvar company--disabled-backends nil)
631
632 (defun company-init-backend (backend)
633 (and (symbolp backend)
634 (not (fboundp backend))
635 (ignore-errors (require backend nil t)))
636 (cond
637 ((symbolp backend)
638 (condition-case err
639 (progn
640 (funcall backend 'init)
641 (put backend 'company-init t))
642 (error
643 (put backend 'company-init 'failed)
644 (unless (memq backend company--disabled-backends)
645 (message "Company back-end '%s' could not be initialized:\n%s"
646 backend (error-message-string err)))
647 (cl-pushnew backend company--disabled-backends)
648 nil)))
649 ;; No initialization for lambdas.
650 ((functionp backend) t)
651 (t ;; Must be a list.
652 (cl-dolist (b backend)
653 (unless (keywordp b)
654 (company-init-backend b))))))
655
656 (defvar company-default-lighter " company")
657
658 (defvar-local company-lighter company-default-lighter)
659
660 ;;;###autoload
661 (define-minor-mode company-mode
662 "\"complete anything\"; is an in-buffer completion framework.
663 Completion starts automatically, depending on the values
664 `company-idle-delay' and `company-minimum-prefix-length'.
665
666 Completion can be controlled with the commands:
667 `company-complete-common', `company-complete-selection', `company-complete',
668 `company-select-next', `company-select-previous'. If these commands are
669 called before `company-idle-delay', completion will also start.
670
671 Completions can be searched with `company-search-candidates' or
672 `company-filter-candidates'. These can be used while completion is
673 inactive, as well.
674
675 The completion data is retrieved using `company-backends' and displayed
676 using `company-frontends'. If you want to start a specific back-end, call
677 it interactively or use `company-begin-backend'.
678
679 regular keymap (`company-mode-map'):
680
681 \\{company-mode-map}
682 keymap during active completions (`company-active-map'):
683
684 \\{company-active-map}"
685 nil company-lighter company-mode-map
686 (if company-mode
687 (progn
688 (add-hook 'pre-command-hook 'company-pre-command nil t)
689 (add-hook 'post-command-hook 'company-post-command nil t)
690 (mapc 'company-init-backend company-backends))
691 (remove-hook 'pre-command-hook 'company-pre-command t)
692 (remove-hook 'post-command-hook 'company-post-command t)
693 (company-cancel)
694 (kill-local-variable 'company-point)))
695
696 (defcustom company-global-modes t
697 "Modes for which `company-mode' mode is turned on by `global-company-mode'.
698 If nil, means no modes. If t, then all major modes have it turned on.
699 If a list, it should be a list of `major-mode' symbol names for which
700 `company-mode' should be automatically turned on. The sense of the list is
701 negated if it begins with `not'. For example:
702 (c-mode c++-mode)
703 means that `company-mode' is turned on for buffers in C and C++ modes only.
704 (not message-mode)
705 means that `company-mode' is always turned on except in `message-mode' buffers."
706 :type '(choice (const :tag "none" nil)
707 (const :tag "all" t)
708 (set :menu-tag "mode specific" :tag "modes"
709 :value (not)
710 (const :tag "Except" not)
711 (repeat :inline t (symbol :tag "mode")))))
712
713 ;;;###autoload
714 (define-globalized-minor-mode global-company-mode company-mode company-mode-on)
715
716 (defun company-mode-on ()
717 (when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s)))
718 (cond ((eq company-global-modes t)
719 t)
720 ((eq (car-safe company-global-modes) 'not)
721 (not (memq major-mode (cdr company-global-modes))))
722 (t (memq major-mode company-global-modes))))
723 (company-mode 1)))
724
725 (defsubst company-assert-enabled ()
726 (unless company-mode
727 (company-uninstall-map)
728 (error "Company not enabled")))
729
730 ;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
731
732 (defvar-local company-my-keymap nil)
733
734 (defvar company-emulation-alist '((t . nil)))
735
736 (defsubst company-enable-overriding-keymap (keymap)
737 (company-uninstall-map)
738 (setq company-my-keymap keymap))
739
740 (defun company-ensure-emulation-alist ()
741 (unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
742 (setq emulation-mode-map-alists
743 (cons 'company-emulation-alist
744 (delq 'company-emulation-alist emulation-mode-map-alists)))))
745
746 (defun company-install-map ()
747 (unless (or (cdar company-emulation-alist)
748 (null company-my-keymap))
749 (setf (cdar company-emulation-alist) company-my-keymap)))
750
751 (defun company-uninstall-map ()
752 (setf (cdar company-emulation-alist) nil))
753
754 ;; Hack:
755 ;; Emacs calculates the active keymaps before reading the event. That means we
756 ;; cannot change the keymap from a timer. So we send a bogus command.
757 ;; XXX: Seems not to be needed anymore in Emacs 24.4
758 (defun company-ignore ()
759 (interactive)
760 (setq this-command last-command))
761
762 (global-set-key '[31415926] 'company-ignore)
763
764 (defun company-input-noop ()
765 (push 31415926 unread-command-events))
766
767 (defun company--column (&optional pos)
768 (save-excursion
769 (when pos (goto-char pos))
770 (save-restriction
771 (+ (save-excursion
772 (vertical-motion 0)
773 (narrow-to-region (point) (point-max))
774 (let ((prefix (get-text-property (point) 'line-prefix)))
775 (if prefix (length prefix) 0)))
776 (current-column)))))
777
778 (defun company--row (&optional pos)
779 (save-excursion
780 (when pos (goto-char pos))
781 (count-screen-lines (window-start)
782 (progn (vertical-motion 0) (point)))))
783
784 ;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
785
786 (defvar-local company-backend nil)
787
788 (defun company-grab (regexp &optional expression limit)
789 (when (looking-back regexp limit)
790 (or (match-string-no-properties (or expression 0)) "")))
791
792 (defun company-grab-line (regexp &optional expression)
793 (company-grab regexp expression (point-at-bol)))
794
795 (defun company-grab-symbol ()
796 (if (looking-at "\\_>")
797 (buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
798 (point)))
799 (unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
800 "")))
801
802 (defun company-grab-word ()
803 (if (looking-at "\\>")
804 (buffer-substring (point) (save-excursion (skip-syntax-backward "w")
805 (point)))
806 (unless (and (char-after) (eq (char-syntax (char-after)) ?w))
807 "")))
808
809 (defun company-grab-symbol-cons (idle-begin-after-re &optional max-len)
810 (let ((symbol (company-grab-symbol)))
811 (when symbol
812 (save-excursion
813 (forward-char (- (length symbol)))
814 (if (looking-back idle-begin-after-re (if max-len
815 (- (point) max-len)
816 (line-beginning-position)))
817 (cons symbol t)
818 symbol)))))
819
820 (defun company-in-string-or-comment ()
821 (let ((ppss (syntax-ppss)))
822 (or (car (setq ppss (nthcdr 3 ppss)))
823 (car (setq ppss (cdr ppss)))
824 (nth 3 ppss))))
825
826 (defun company-call-backend (&rest args)
827 (company--force-sync #'company-call-backend-raw args company-backend))
828
829 (defun company--force-sync (fun args backend)
830 (let ((value (apply fun args)))
831 (if (not (eq (car-safe value) :async))
832 value
833 (let ((res 'trash)
834 (start (time-to-seconds)))
835 (funcall (cdr value)
836 (lambda (result) (setq res result)))
837 (while (eq res 'trash)
838 (if (> (- (time-to-seconds) start) company-async-timeout)
839 (error "Company: Back-end %s async timeout with args %s"
840 backend args)
841 (sleep-for company-async-wait)))
842 res))))
843
844 (defun company-call-backend-raw (&rest args)
845 (condition-case err
846 (if (functionp company-backend)
847 (apply company-backend args)
848 (apply #'company--multi-backend-adapter company-backend args))
849 (error (error "Company: Back-end %s error \"%s\" with args %s"
850 company-backend (error-message-string err) args))))
851
852 (defun company--multi-backend-adapter (backends command &rest args)
853 (let ((backends (cl-loop for b in backends
854 when (not (and (symbolp b)
855 (eq 'failed (get b 'company-init))))
856 collect b)))
857 (setq backends
858 (if (eq command 'prefix)
859 (butlast backends (length (member :with backends)))
860 (delq :with backends)))
861 (pcase command
862 (`candidates
863 (company--multi-backend-adapter-candidates backends (car args)))
864 (`sorted nil)
865 (`duplicates t)
866 ((or `prefix `ignore-case `no-cache `require-match)
867 (let (value)
868 (cl-dolist (backend backends)
869 (when (setq value (company--force-sync
870 backend (cons command args) backend))
871 (cl-return value)))))
872 (_
873 (let ((arg (car args)))
874 (when (> (length arg) 0)
875 (let ((backend (or (get-text-property 0 'company-backend arg)
876 (car backends))))
877 (apply backend command args))))))))
878
879 (defun company--multi-backend-adapter-candidates (backends prefix)
880 (let ((pairs (cl-loop for backend in (cdr backends)
881 when (equal (company--prefix-str
882 (funcall backend 'prefix))
883 prefix)
884 collect (cons (funcall backend 'candidates prefix)
885 (let ((b backend))
886 (lambda (candidates)
887 (mapcar
888 (lambda (str)
889 (propertize str 'company-backend b))
890 candidates)))))))
891 (when (equal (company--prefix-str (funcall (car backends) 'prefix)) prefix)
892 ;; Small perf optimization: don't tag the candidates received
893 ;; from the first backend in the group.
894 (push (cons (funcall (car backends) 'candidates prefix)
895 'identity)
896 pairs))
897 (company--merge-async pairs (lambda (values) (apply #'append values)))))
898
899 (defun company--merge-async (pairs merger)
900 (let ((async (cl-loop for pair in pairs
901 thereis
902 (eq :async (car-safe (car pair))))))
903 (if (not async)
904 (funcall merger (cl-loop for (val . mapper) in pairs
905 collect (funcall mapper val)))
906 (cons
907 :async
908 (lambda (callback)
909 (let* (lst pending
910 (finisher (lambda ()
911 (unless pending
912 (funcall callback
913 (funcall merger
914 (nreverse lst)))))))
915 (dolist (pair pairs)
916 (let ((val (car pair))
917 (mapper (cdr pair)))
918 (if (not (eq :async (car-safe val)))
919 (push (funcall mapper val) lst)
920 (push nil lst)
921 (let ((cell lst)
922 (fetcher (cdr val)))
923 (push fetcher pending)
924 (funcall fetcher
925 (lambda (res)
926 (setq pending (delq fetcher pending))
927 (setcar cell (funcall mapper res))
928 (funcall finisher)))))))))))))
929
930 (defun company--prefix-str (prefix)
931 (or (car-safe prefix) prefix))
932
933 ;;; completion mechanism ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
934
935 (defvar-local company-prefix nil)
936
937 (defvar-local company-candidates nil)
938
939 (defvar-local company-candidates-length nil)
940
941 (defvar-local company-candidates-cache nil)
942
943 (defvar-local company-candidates-predicate nil)
944
945 (defvar-local company-common nil)
946
947 (defvar-local company-selection 0)
948
949 (defvar-local company-selection-changed nil)
950
951 (defvar-local company--manual-action nil
952 "Non-nil, if manual completion took place.")
953
954 (defvar-local company--manual-prefix nil)
955
956 (defvar company--auto-completion nil
957 "Non-nil when current candidate is being inserted automatically.
958 Controlled by `company-auto-complete'.")
959
960 (defvar-local company--point-max nil)
961
962 (defvar-local company-point nil)
963
964 (defvar company-timer nil)
965
966 (defvar-local company-added-newline nil)
967
968 (defsubst company-strip-prefix (str)
969 (substring str (length company-prefix)))
970
971 (defun company--insert-candidate (candidate)
972 (setq candidate (substring-no-properties candidate))
973 ;; XXX: Return value we check here is subject to change.
974 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
975 (insert (company-strip-prefix candidate))
976 (delete-region (- (point) (length company-prefix)) (point))
977 (insert candidate)))
978
979 (defmacro company-with-candidate-inserted (candidate &rest body)
980 "Evaluate BODY with CANDIDATE temporarily inserted.
981 This is a tool for back-ends that need candidates inserted before they
982 can retrieve meta-data for them."
983 (declare (indent 1))
984 `(let ((inhibit-modification-hooks t)
985 (inhibit-point-motion-hooks t)
986 (modified-p (buffer-modified-p)))
987 (company--insert-candidate ,candidate)
988 (unwind-protect
989 (progn ,@body)
990 (delete-region company-point (point)))))
991
992 (defun company-explicit-action-p ()
993 "Return whether explicit completion action was taken by the user."
994 (or company--manual-action
995 company-selection-changed))
996
997 (defun company-reformat (candidate)
998 ;; company-ispell needs this, because the results are always lower-case
999 ;; It's mory efficient to fix it only when they are displayed.
1000 ;; FIXME: Adopt the current text's capitalization instead?
1001 (if (eq (company-call-backend 'ignore-case) 'keep-prefix)
1002 (concat company-prefix (substring candidate (length company-prefix)))
1003 candidate))
1004
1005 (defun company--should-complete ()
1006 (and (not (or buffer-read-only overriding-terminal-local-map
1007 overriding-local-map))
1008 ;; Check if in the middle of entering a key combination.
1009 (or (equal (this-command-keys-vector) [])
1010 (not (keymapp (key-binding (this-command-keys-vector)))))
1011 (eq company-idle-delay t)
1012 (or (eq t company-begin-commands)
1013 (memq this-command company-begin-commands)
1014 (and (symbolp this-command) (get this-command 'company-begin)))
1015 (not (and transient-mark-mode mark-active))))
1016
1017 (defun company--should-continue ()
1018 (or (eq t company-begin-commands)
1019 (eq t company-continue-commands)
1020 (if (eq 'not (car company-continue-commands))
1021 (not (memq this-command (cdr company-continue-commands)))
1022 (or (memq this-command company-begin-commands)
1023 (memq this-command company-continue-commands)
1024 (string-match-p "\\`company-" (symbol-name this-command))))))
1025
1026 (defun company-call-frontends (command)
1027 (dolist (frontend company-frontends)
1028 (condition-case err
1029 (funcall frontend command)
1030 (error (error "Company: Front-end %s error \"%s\" on command %s"
1031 frontend (error-message-string err) command)))))
1032
1033 (defun company-set-selection (selection &optional force-update)
1034 (setq selection
1035 (if company-selection-wrap-around
1036 (mod selection company-candidates-length)
1037 (max 0 (min (1- company-candidates-length) selection))))
1038 (when (or force-update (not (equal selection company-selection)))
1039 (setq company-selection selection
1040 company-selection-changed t)
1041 (company-call-frontends 'update)))
1042
1043 (defun company-apply-predicate (candidates predicate)
1044 (let (new)
1045 (dolist (c candidates)
1046 (when (funcall predicate c)
1047 (push c new)))
1048 (nreverse new)))
1049
1050 (defun company-update-candidates (candidates)
1051 (setq company-candidates-length (length candidates))
1052 (if (> company-selection 0)
1053 ;; Try to restore the selection
1054 (let ((selected (nth company-selection company-candidates)))
1055 (setq company-selection 0
1056 company-candidates candidates)
1057 (when selected
1058 (while (and candidates (string< (pop candidates) selected))
1059 (cl-incf company-selection))
1060 (unless candidates
1061 ;; Make sure selection isn't out of bounds.
1062 (setq company-selection (min (1- company-candidates-length)
1063 company-selection)))))
1064 (setq company-selection 0
1065 company-candidates candidates))
1066 ;; Save in cache:
1067 (push (cons company-prefix company-candidates) company-candidates-cache)
1068 ;; Calculate common.
1069 (let ((completion-ignore-case (company-call-backend 'ignore-case)))
1070 ;; We want to support non-prefix completion, so filtering is the
1071 ;; responsibility of each respective backend, not ours.
1072 ;; On the other hand, we don't want to replace non-prefix input in
1073 ;; `company-complete-common'.
1074 (setq company-common
1075 (if (cdr company-candidates)
1076 (let ((common (try-completion company-prefix company-candidates)))
1077 (if (eq common t)
1078 ;; Mulple equal strings, probably with different
1079 ;; annotations.
1080 company-prefix
1081 common))
1082 (car company-candidates)))))
1083
1084 (defun company-calculate-candidates (prefix)
1085 (let ((candidates (cdr (assoc prefix company-candidates-cache)))
1086 (ignore-case (company-call-backend 'ignore-case)))
1087 (or candidates
1088 (when company-candidates-cache
1089 (let ((len (length prefix))
1090 (completion-ignore-case ignore-case)
1091 prev)
1092 (cl-dotimes (i (1+ len))
1093 (when (setq prev (cdr (assoc (substring prefix 0 (- len i))
1094 company-candidates-cache)))
1095 (setq candidates (all-completions prefix prev))
1096 (cl-return t)))))
1097 ;; no cache match, call back-end
1098 (setq candidates
1099 (company--process-candidates
1100 (company--fetch-candidates prefix))))
1101 (setq candidates (company--transform-candidates candidates))
1102 (when candidates
1103 (if (or (cdr candidates)
1104 (not (eq t (compare-strings (car candidates) nil nil
1105 prefix nil nil ignore-case))))
1106 candidates
1107 ;; Already completed and unique; don't start.
1108 t))))
1109
1110 (defun company--fetch-candidates (prefix)
1111 (let ((c (if company--manual-action
1112 (company-call-backend 'candidates prefix)
1113 (company-call-backend-raw 'candidates prefix)))
1114 res)
1115 (if (not (eq (car c) :async))
1116 c
1117 (let ((buf (current-buffer))
1118 (win (selected-window))
1119 (tick (buffer-chars-modified-tick))
1120 (pt (point))
1121 (backend company-backend))
1122 (funcall
1123 (cdr c)
1124 (lambda (candidates)
1125 (if (not (and candidates (eq res 'done)))
1126 ;; Fetcher called us right back.
1127 (setq res candidates)
1128 (setq company-backend backend
1129 company-candidates-cache
1130 (list (cons prefix
1131 (company--process-candidates
1132 candidates))))
1133 (company-idle-begin buf win tick pt)))))
1134 ;; FIXME: Relying on the fact that the callers
1135 ;; will interpret nil as "do nothing" is shaky.
1136 ;; A throw-catch would be one possible improvement.
1137 (or res
1138 (progn (setq res 'done) nil)))))
1139
1140 (defun company--process-candidates (candidates)
1141 (when company-candidates-predicate
1142 (setq candidates
1143 (company-apply-predicate candidates
1144 company-candidates-predicate)))
1145 (unless (company-call-backend 'sorted)
1146 (setq candidates (sort candidates 'string<)))
1147 (when (company-call-backend 'duplicates)
1148 (company--strip-duplicates candidates))
1149 candidates)
1150
1151 (defun company--strip-duplicates (candidates)
1152 (let ((c2 candidates))
1153 (while c2
1154 (setcdr c2
1155 (let ((str (car c2))
1156 (anno 'unk))
1157 (pop c2)
1158 (while (let ((str2 (car c2)))
1159 (if (not (equal str str2))
1160 nil
1161 (when (eq anno 'unk)
1162 (setq anno (company-call-backend
1163 'annotation str)))
1164 (equal anno
1165 (company-call-backend
1166 'annotation str2))))
1167 (pop c2))
1168 c2)))))
1169
1170 (defun company--transform-candidates (candidates)
1171 (let ((c candidates))
1172 (dolist (tr company-transformers)
1173 (setq c (funcall tr c)))
1174 c))
1175
1176 (defun company-sort-by-occurrence (candidates)
1177 "Sort CANDIDATES according to their occurrences.
1178 Searches for each in the currently visible part of the current buffer and
1179 gives priority to the closest ones above point, then closest ones below
1180 point. The rest of the list is appended unchanged.
1181 Keywords and function definition names are ignored."
1182 (let* (occurs
1183 (noccurs
1184 (cl-delete-if
1185 (lambda (candidate)
1186 (when (or
1187 (save-excursion
1188 (progn (forward-char (- (length company-prefix)))
1189 (search-backward candidate (window-start) t)))
1190 (save-excursion
1191 (search-forward candidate (window-end) t)))
1192 (let ((beg (match-beginning 0))
1193 (end (match-end 0)))
1194 (when (save-excursion
1195 (goto-char end)
1196 (and (not (memq (get-text-property (point) 'face)
1197 '(font-lock-function-name-face
1198 font-lock-keyword-face)))
1199 (let ((prefix (company--prefix-str
1200 (company-call-backend 'prefix))))
1201 (and (stringp prefix)
1202 (= (length prefix) (- end beg))))))
1203 (push (cons candidate (if (< beg (point))
1204 (- (point) end)
1205 (- beg (window-start))))
1206 occurs)
1207 t))))
1208 candidates)))
1209 (nconc
1210 (mapcar #'car (sort occurs (lambda (e1 e2) (<= (cdr e1) (cdr e2)))))
1211 noccurs)))
1212
1213 (defun company-idle-begin (buf win tick pos)
1214 (and (eq buf (current-buffer))
1215 (eq win (selected-window))
1216 (eq tick (buffer-chars-modified-tick))
1217 (eq pos (point))
1218 (when (company-auto-begin)
1219 (when (version< emacs-version "24.3.50")
1220 (company-input-noop))
1221 (company-post-command))))
1222
1223 (defun company-auto-begin ()
1224 (and company-mode
1225 (not company-candidates)
1226 (let ((company-idle-delay t)
1227 (company-begin-commands t))
1228 (condition-case-unless-debug err
1229 (company-begin)
1230 (error (message "Company: An error occurred in auto-begin")
1231 (message "%s" (error-message-string err))
1232 (company-cancel))
1233 (quit (company-cancel)))))
1234 (unless company-candidates
1235 (setq company-backend nil))
1236 ;; Return non-nil if active.
1237 company-candidates)
1238
1239 (defun company-manual-begin ()
1240 (interactive)
1241 (company-assert-enabled)
1242 (setq company--manual-action t)
1243 (unwind-protect
1244 (let ((company-minimum-prefix-length 0))
1245 (company-auto-begin))
1246 (unless company-candidates
1247 (setq company--manual-action nil))))
1248
1249 (defun company-other-backend (&optional backward)
1250 (interactive (list current-prefix-arg))
1251 (company-assert-enabled)
1252 (let* ((after (if company-backend
1253 (cdr (member company-backend company-backends))
1254 company-backends))
1255 (before (cdr (member company-backend (reverse company-backends))))
1256 (next (if backward
1257 (append before (reverse after))
1258 (append after (reverse before)))))
1259 (company-cancel)
1260 (cl-dolist (backend next)
1261 (when (ignore-errors (company-begin-backend backend))
1262 (cl-return t))))
1263 (unless company-candidates
1264 (error "No other back-end")))
1265
1266 (defun company-require-match-p ()
1267 (let ((backend-value (company-call-backend 'require-match)))
1268 (or (eq backend-value t)
1269 (and (not (eq backend-value 'never))
1270 (if (functionp company-require-match)
1271 (funcall company-require-match)
1272 (eq company-require-match t))))))
1273
1274 (defun company-auto-complete-p (input)
1275 "Return non-nil, if input starts with punctuation or parentheses."
1276 (and (if (functionp company-auto-complete)
1277 (funcall company-auto-complete)
1278 company-auto-complete)
1279 (if (functionp company-auto-complete-chars)
1280 (funcall company-auto-complete-chars input)
1281 (if (consp company-auto-complete-chars)
1282 (memq (char-syntax (string-to-char input))
1283 company-auto-complete-chars)
1284 (string-match (substring input 0 1) company-auto-complete-chars)))))
1285
1286 (defun company--incremental-p ()
1287 (and (> (point) company-point)
1288 (> (point-max) company--point-max)
1289 (not (eq this-command 'backward-delete-char-untabify))
1290 (equal (buffer-substring (- company-point (length company-prefix))
1291 company-point)
1292 company-prefix)))
1293
1294 (defun company--continue-failed ()
1295 (let ((input (buffer-substring-no-properties (point) company-point)))
1296 (cond
1297 ((company-auto-complete-p input)
1298 ;; auto-complete
1299 (save-excursion
1300 (goto-char company-point)
1301 (let ((company--auto-completion t))
1302 (company-complete-selection))
1303 nil))
1304 ((company-require-match-p)
1305 ;; wrong incremental input, but required match
1306 (delete-char (- (length input)))
1307 (ding)
1308 (message "Matching input is required")
1309 company-candidates)
1310 ((equal company-prefix (car company-candidates))
1311 ;; last input was actually success
1312 (company-cancel company-prefix))
1313 (t (company-cancel)))))
1314
1315 (defun company--good-prefix-p (prefix)
1316 (and (stringp (company--prefix-str prefix)) ;excludes 'stop
1317 (or (eq (cdr-safe prefix) t)
1318 (let ((len (or (cdr-safe prefix) (length prefix))))
1319 (if company--manual-prefix
1320 (or (not company-abort-manual-when-too-short)
1321 ;; Must not be less than minimum or initial length.
1322 (>= len (min company-minimum-prefix-length
1323 (length company--manual-prefix))))
1324 (>= len company-minimum-prefix-length))))))
1325
1326 (defun company--continue ()
1327 (when (company-call-backend 'no-cache company-prefix)
1328 ;; Don't complete existing candidates, fetch new ones.
1329 (setq company-candidates-cache nil))
1330 (let* ((new-prefix (company-call-backend 'prefix))
1331 (c (when (and (company--good-prefix-p new-prefix)
1332 (setq new-prefix (company--prefix-str new-prefix))
1333 (= (- (point) (length new-prefix))
1334 (- company-point (length company-prefix))))
1335 (company-calculate-candidates new-prefix))))
1336 (cond
1337 ((eq c t)
1338 ;; t means complete/unique.
1339 ;; Handle it like completion was aborted, to differentiate from user
1340 ;; calling one of Company's commands to insert the candidate.
1341 (company-cancel 'unique))
1342 ((consp c)
1343 ;; incremental match
1344 (setq company-prefix new-prefix)
1345 (company-update-candidates c)
1346 c)
1347 ((not (company--incremental-p))
1348 (company-cancel))
1349 (t (company--continue-failed)))))
1350
1351 (defun company--begin-new ()
1352 (let (prefix c)
1353 (cl-dolist (backend (if company-backend
1354 ;; prefer manual override
1355 (list company-backend)
1356 company-backends))
1357 (setq prefix
1358 (if (or (symbolp backend)
1359 (functionp backend))
1360 (when (or (not (symbolp backend))
1361 (eq t (get backend 'company-init))
1362 (unless (get backend 'company-init)
1363 (company-init-backend backend)))
1364 (funcall backend 'prefix))
1365 (company--multi-backend-adapter backend 'prefix)))
1366 (when prefix
1367 (when (company--good-prefix-p prefix)
1368 (setq company-prefix (company--prefix-str prefix)
1369 company-backend backend
1370 c (company-calculate-candidates company-prefix))
1371 ;; t means complete/unique. We don't start, so no hooks.
1372 (if (not (consp c))
1373 (when company--manual-action
1374 (message "No completion found"))
1375 (when company--manual-action
1376 (setq company--manual-prefix prefix))
1377 (when (symbolp backend)
1378 (setq company-lighter (concat " " (symbol-name backend))))
1379 (company-update-candidates c)
1380 (run-hook-with-args 'company-completion-started-hook
1381 (company-explicit-action-p))
1382 (company-call-frontends 'show)))
1383 (cl-return c)))))
1384
1385 (defun company-begin ()
1386 (or (and company-candidates (company--continue))
1387 (and (company--should-complete) (company--begin-new)))
1388 (when company-candidates
1389 (let ((modified (buffer-modified-p)))
1390 (when (and company-end-of-buffer-workaround (eobp))
1391 (save-excursion (insert "\n"))
1392 (setq company-added-newline
1393 (or modified (buffer-chars-modified-tick)))))
1394 (setq company-point (point)
1395 company--point-max (point-max))
1396 (company-ensure-emulation-alist)
1397 (company-enable-overriding-keymap company-active-map)
1398 (company-call-frontends 'update)))
1399
1400 (defun company-cancel (&optional result)
1401 (and company-added-newline
1402 (> (point-max) (point-min))
1403 (let ((tick (buffer-chars-modified-tick)))
1404 (delete-region (1- (point-max)) (point-max))
1405 (equal tick company-added-newline))
1406 ;; Only set unmodified when tick remained the same since insert,
1407 ;; and the buffer wasn't modified before.
1408 (set-buffer-modified-p nil))
1409 (when company-prefix
1410 (if (stringp result)
1411 (progn
1412 (company-call-backend 'pre-completion result)
1413 (run-hook-with-args 'company-completion-finished-hook result)
1414 (company-call-backend 'post-completion result))
1415 (run-hook-with-args 'company-completion-cancelled-hook result)))
1416 (setq company-added-newline nil
1417 company-backend nil
1418 company-prefix nil
1419 company-candidates nil
1420 company-candidates-length nil
1421 company-candidates-cache nil
1422 company-candidates-predicate nil
1423 company-common nil
1424 company-selection 0
1425 company-selection-changed nil
1426 company--manual-action nil
1427 company--manual-prefix nil
1428 company-lighter company-default-lighter
1429 company--point-max nil
1430 company-point nil)
1431 (when company-timer
1432 (cancel-timer company-timer))
1433 (company-search-mode 0)
1434 (company-call-frontends 'hide)
1435 (company-enable-overriding-keymap nil)
1436 ;; Make return value explicit.
1437 nil)
1438
1439 (defun company-abort ()
1440 (interactive)
1441 (company-cancel t)
1442 ;; Don't start again, unless started manually.
1443 (setq company-point (point)))
1444
1445 (defun company-finish (result)
1446 (company--insert-candidate result)
1447 (company-cancel result)
1448 ;; Don't start again, unless started manually.
1449 (setq company-point (point)))
1450
1451 (defsubst company-keep (command)
1452 (and (symbolp command) (get command 'company-keep)))
1453
1454 (defun company-pre-command ()
1455 (unless (company-keep this-command)
1456 (condition-case err
1457 (when company-candidates
1458 (company-call-frontends 'pre-command)
1459 (unless (company--should-continue)
1460 (company-abort)))
1461 (error (message "Company: An error occurred in pre-command")
1462 (message "%s" (error-message-string err))
1463 (company-cancel))))
1464 (when company-timer
1465 (cancel-timer company-timer)
1466 (setq company-timer nil))
1467 (company-uninstall-map))
1468
1469 (defun company-post-command ()
1470 (unless (company-keep this-command)
1471 (condition-case err
1472 (progn
1473 (unless (equal (point) company-point)
1474 (company-begin))
1475 (if company-candidates
1476 (company-call-frontends 'post-command)
1477 (and (numberp company-idle-delay)
1478 (or (eq t company-begin-commands)
1479 (memq this-command company-begin-commands))
1480 (not (equal (point) company-point))
1481 (setq company-timer
1482 (run-with-timer company-idle-delay nil
1483 'company-idle-begin
1484 (current-buffer) (selected-window)
1485 (buffer-chars-modified-tick) (point))))))
1486 (error (message "Company: An error occurred in post-command")
1487 (message "%s" (error-message-string err))
1488 (company-cancel))))
1489 (company-install-map))
1490
1491 ;;; search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1492
1493 (defvar-local company-search-string nil)
1494
1495 (defvar-local company-search-lighter " Search: \"\"")
1496
1497 (defvar-local company-search-old-map nil)
1498
1499 (defvar-local company-search-old-selection 0)
1500
1501 (defun company-search (text lines)
1502 (let ((quoted (regexp-quote text))
1503 (i 0))
1504 (cl-dolist (line lines)
1505 (when (string-match quoted line (length company-prefix))
1506 (cl-return i))
1507 (cl-incf i))))
1508
1509 (defun company-search-printing-char ()
1510 (interactive)
1511 (company-search-assert-enabled)
1512 (setq company-search-string
1513 (concat (or company-search-string "") (string last-command-event))
1514 company-search-lighter (concat " Search: \"" company-search-string
1515 "\""))
1516 (let ((pos (company-search company-search-string
1517 (nthcdr company-selection company-candidates))))
1518 (if (null pos)
1519 (ding)
1520 (company-set-selection (+ company-selection pos) t))))
1521
1522 (defun company-search-repeat-forward ()
1523 "Repeat the incremental search in completion candidates forward."
1524 (interactive)
1525 (company-search-assert-enabled)
1526 (let ((pos (company-search company-search-string
1527 (cdr (nthcdr company-selection
1528 company-candidates)))))
1529 (if (null pos)
1530 (ding)
1531 (company-set-selection (+ company-selection pos 1) t))))
1532
1533 (defun company-search-repeat-backward ()
1534 "Repeat the incremental search in completion candidates backwards."
1535 (interactive)
1536 (company-search-assert-enabled)
1537 (let ((pos (company-search company-search-string
1538 (nthcdr (- company-candidates-length
1539 company-selection)
1540 (reverse company-candidates)))))
1541 (if (null pos)
1542 (ding)
1543 (company-set-selection (- company-selection pos 1) t))))
1544
1545 (defun company-create-match-predicate ()
1546 (setq company-candidates-predicate
1547 `(lambda (candidate)
1548 ,(if company-candidates-predicate
1549 `(and (string-match ,company-search-string candidate)
1550 (funcall ,company-candidates-predicate
1551 candidate))
1552 `(string-match ,company-search-string candidate))))
1553 (company-update-candidates
1554 (company-apply-predicate company-candidates company-candidates-predicate))
1555 ;; Invalidate cache.
1556 (setq company-candidates-cache (cons company-prefix company-candidates)))
1557
1558 (defun company-filter-printing-char ()
1559 (interactive)
1560 (company-search-assert-enabled)
1561 (company-search-printing-char)
1562 (company-create-match-predicate)
1563 (company-call-frontends 'update))
1564
1565 (defun company-search-kill-others ()
1566 "Limit the completion candidates to the ones matching the search string."
1567 (interactive)
1568 (company-search-assert-enabled)
1569 (company-create-match-predicate)
1570 (company-search-mode 0)
1571 (company-call-frontends 'update))
1572
1573 (defun company-search-abort ()
1574 "Abort searching the completion candidates."
1575 (interactive)
1576 (company-search-assert-enabled)
1577 (company-set-selection company-search-old-selection t)
1578 (company-search-mode 0))
1579
1580 (defun company-search-other-char ()
1581 (interactive)
1582 (company-search-assert-enabled)
1583 (company-search-mode 0)
1584 (company--unread-last-input))
1585
1586 (defvar company-search-map
1587 (let ((i 0)
1588 (keymap (make-keymap)))
1589 (if (fboundp 'max-char)
1590 (set-char-table-range (nth 1 keymap) (cons #x100 (max-char))
1591 'company-search-printing-char)
1592 (with-no-warnings
1593 ;; obsolete in Emacs 23
1594 (let ((l (generic-character-list))
1595 (table (nth 1 keymap)))
1596 (while l
1597 (set-char-table-default table (car l) 'company-search-printing-char)
1598 (setq l (cdr l))))))
1599 (define-key keymap [t] 'company-search-other-char)
1600 (while (< i ?\s)
1601 (define-key keymap (make-string 1 i) 'company-search-other-char)
1602 (cl-incf i))
1603 (while (< i 256)
1604 (define-key keymap (vector i) 'company-search-printing-char)
1605 (cl-incf i))
1606 (let ((meta-map (make-sparse-keymap)))
1607 (define-key keymap (char-to-string meta-prefix-char) meta-map)
1608 (define-key keymap [escape] meta-map))
1609 (define-key keymap (vector meta-prefix-char t) 'company-search-other-char)
1610 (define-key keymap "\e\e\e" 'company-search-other-char)
1611 (define-key keymap [escape escape escape] 'company-search-other-char)
1612 (define-key keymap (kbd "DEL") 'company-search-other-char)
1613
1614 (define-key keymap "\C-g" 'company-search-abort)
1615 (define-key keymap "\C-s" 'company-search-repeat-forward)
1616 (define-key keymap "\C-r" 'company-search-repeat-backward)
1617 (define-key keymap "\C-o" 'company-search-kill-others)
1618 keymap)
1619 "Keymap used for incrementally searching the completion candidates.")
1620
1621 (define-minor-mode company-search-mode
1622 "Search mode for completion candidates.
1623 Don't start this directly, use `company-search-candidates' or
1624 `company-filter-candidates'."
1625 nil company-search-lighter nil
1626 (if company-search-mode
1627 (if (company-manual-begin)
1628 (progn
1629 (setq company-search-old-selection company-selection)
1630 (company-call-frontends 'update))
1631 (setq company-search-mode nil))
1632 (kill-local-variable 'company-search-string)
1633 (kill-local-variable 'company-search-lighter)
1634 (kill-local-variable 'company-search-old-selection)
1635 (company-enable-overriding-keymap company-active-map)))
1636
1637 (defun company-search-assert-enabled ()
1638 (company-assert-enabled)
1639 (unless company-search-mode
1640 (company-uninstall-map)
1641 (error "Company not in search mode")))
1642
1643 (defun company-search-candidates ()
1644 "Start searching the completion candidates incrementally.
1645
1646 \\<company-search-map>Search can be controlled with the commands:
1647 - `company-search-repeat-forward' (\\[company-search-repeat-forward])
1648 - `company-search-repeat-backward' (\\[company-search-repeat-backward])
1649 - `company-search-abort' (\\[company-search-abort])
1650
1651 Regular characters are appended to the search string.
1652
1653 The command `company-search-kill-others' (\\[company-search-kill-others])
1654 uses the search string to limit the completion candidates."
1655 (interactive)
1656 (company-search-mode 1)
1657 (company-enable-overriding-keymap company-search-map))
1658
1659 (defvar company-filter-map
1660 (let ((keymap (make-keymap)))
1661 (define-key keymap [remap company-search-printing-char]
1662 'company-filter-printing-char)
1663 (set-keymap-parent keymap company-search-map)
1664 keymap)
1665 "Keymap used for incrementally searching the completion candidates.")
1666
1667 (defun company-filter-candidates ()
1668 "Start filtering the completion candidates incrementally.
1669 This works the same way as `company-search-candidates' immediately
1670 followed by `company-search-kill-others' after each input."
1671 (interactive)
1672 (company-search-mode 1)
1673 (company-enable-overriding-keymap company-filter-map))
1674
1675 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1676
1677 (defun company-select-next ()
1678 "Select the next candidate in the list."
1679 (interactive)
1680 (when (company-manual-begin)
1681 (company-set-selection (1+ company-selection))))
1682
1683 (defun company-select-previous ()
1684 "Select the previous candidate in the list."
1685 (interactive)
1686 (when (company-manual-begin)
1687 (company-set-selection (1- company-selection))))
1688
1689 (defun company-select-next-or-abort ()
1690 "Select the next candidate if more than one, else abort
1691 and invoke the normal binding."
1692 (interactive)
1693 (if (> company-candidates-length 1)
1694 (company-select-next)
1695 (company-abort)
1696 (company--unread-last-input)))
1697
1698 (defun company-select-previous-or-abort ()
1699 "Select the previous candidate if more than one, else abort
1700 and invoke the normal binding."
1701 (interactive)
1702 (if (> company-candidates-length 1)
1703 (company-select-previous)
1704 (company-abort)
1705 (company--unread-last-input)))
1706
1707 (defvar company-pseudo-tooltip-overlay)
1708
1709 (defvar company-tooltip-offset)
1710
1711 (defun company--inside-tooltip-p (event-col-row row height)
1712 (let* ((ovl company-pseudo-tooltip-overlay)
1713 (column (overlay-get ovl 'company-column))
1714 (width (overlay-get ovl 'company-width))
1715 (evt-col (car event-col-row))
1716 (evt-row (cdr event-col-row)))
1717 (and (>= evt-col column)
1718 (< evt-col (+ column width))
1719 (if (> height 0)
1720 (and (> evt-row row)
1721 (<= evt-row (+ row height) ))
1722 (and (< evt-row row)
1723 (>= evt-row (+ row height)))))))
1724
1725 (defun company--event-col-row (event)
1726 (let* ((col-row (posn-actual-col-row (event-start event)))
1727 (col (car col-row))
1728 (row (cdr col-row)))
1729 (cl-incf col (window-hscroll))
1730 (and header-line-format
1731 (version< "24" emacs-version)
1732 (cl-decf row))
1733 (cons col row)))
1734
1735 (defun company-select-mouse (event)
1736 "Select the candidate picked by the mouse."
1737 (interactive "e")
1738 (let ((event-col-row (company--event-col-row event))
1739 (ovl-row (company--row))
1740 (ovl-height (and company-pseudo-tooltip-overlay
1741 (min (overlay-get company-pseudo-tooltip-overlay
1742 'company-height)
1743 company-candidates-length))))
1744 (if (and ovl-height
1745 (company--inside-tooltip-p event-col-row ovl-row ovl-height))
1746 (progn
1747 (company-set-selection (+ (cdr event-col-row)
1748 (1- company-tooltip-offset)
1749 (if (and (eq company-tooltip-offset-display 'lines)
1750 (not (zerop company-tooltip-offset)))
1751 -1 0)
1752 (- ovl-row)
1753 (if (< ovl-height 0)
1754 (- 1 ovl-height)
1755 0)))
1756 t)
1757 (company-abort)
1758 (company--unread-last-input)
1759 nil)))
1760
1761 (defun company-complete-mouse (event)
1762 "Insert the candidate picked by the mouse."
1763 (interactive "e")
1764 (when (company-select-mouse event)
1765 (company-complete-selection)))
1766
1767 (defun company-complete-selection ()
1768 "Insert the selected candidate."
1769 (interactive)
1770 (when (company-manual-begin)
1771 (let ((result (nth company-selection company-candidates)))
1772 (company-finish result))))
1773
1774 (defun company-complete-common ()
1775 "Insert the common part of all candidates."
1776 (interactive)
1777 (when (company-manual-begin)
1778 (if (and (not (cdr company-candidates))
1779 (equal company-common (car company-candidates)))
1780 (company-complete-selection)
1781 (when company-common
1782 (company--insert-candidate company-common)))))
1783
1784 (defun company-complete ()
1785 "Insert the common part of all candidates or the current selection.
1786 The first time this is called, the common part is inserted, the second
1787 time, or when the selection has been changed, the selected candidate is
1788 inserted."
1789 (interactive)
1790 (when (company-manual-begin)
1791 (if (or company-selection-changed
1792 (eq last-command 'company-complete-common))
1793 (call-interactively 'company-complete-selection)
1794 (call-interactively 'company-complete-common)
1795 (setq this-command 'company-complete-common))))
1796
1797 (defun company-complete-number (n)
1798 "Insert the Nth candidate.
1799 To show the number next to the candidates in some back-ends, enable
1800 `company-show-numbers'."
1801 (when (company-manual-begin)
1802 (and (or (< n 1) (> n company-candidates-length))
1803 (error "No candidate number %d" n))
1804 (cl-decf n)
1805 (company-finish (nth n company-candidates))))
1806
1807 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1808
1809 (defconst company-space-strings-limit 100)
1810
1811 (defconst company-space-strings
1812 (let (lst)
1813 (dotimes (i company-space-strings-limit)
1814 (push (make-string (- company-space-strings-limit 1 i) ?\ ) lst))
1815 (apply 'vector lst)))
1816
1817 (defun company-space-string (len)
1818 (if (< len company-space-strings-limit)
1819 (aref company-space-strings len)
1820 (make-string len ?\ )))
1821
1822 (defun company-safe-substring (str from &optional to)
1823 (if (> from (string-width str))
1824 ""
1825 (with-temp-buffer
1826 (insert str)
1827 (move-to-column from)
1828 (let ((beg (point)))
1829 (if to
1830 (progn
1831 (move-to-column to)
1832 (concat (buffer-substring beg (point))
1833 (let ((padding (- to (current-column))))
1834 (when (> padding 0)
1835 (company-space-string padding)))))
1836 (buffer-substring beg (point-max)))))))
1837
1838 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1839
1840 (defvar-local company-last-metadata nil)
1841
1842 (defun company-fetch-metadata ()
1843 (let ((selected (nth company-selection company-candidates)))
1844 (unless (eq selected (car company-last-metadata))
1845 (setq company-last-metadata
1846 (cons selected (company-call-backend 'meta selected))))
1847 (cdr company-last-metadata)))
1848
1849 (defun company-doc-buffer (&optional string)
1850 (with-current-buffer (get-buffer-create "*company-documentation*")
1851 (erase-buffer)
1852 (when string
1853 (save-excursion
1854 (insert string)))
1855 (current-buffer)))
1856
1857 (defvar company--electric-commands
1858 '(scroll-other-window scroll-other-window-down)
1859 "List of Commands that won't break out of electric commands.")
1860
1861 (defmacro company--electric-do (&rest body)
1862 (declare (indent 0) (debug t))
1863 `(when (company-manual-begin)
1864 (save-window-excursion
1865 (let ((height (window-height))
1866 (row (company--row))
1867 cmd)
1868 ,@body
1869 (and (< (window-height) height)
1870 (< (- (window-height) row 2) company-tooltip-limit)
1871 (recenter (- (window-height) row 2)))
1872 (while (memq (setq cmd (key-binding (vector (list (read-event)))))
1873 company--electric-commands)
1874 (call-interactively cmd))
1875 (company--unread-last-input)))))
1876
1877 (defun company--unread-last-input ()
1878 (when last-input-event
1879 (clear-this-command-keys t)
1880 (setq unread-command-events (list last-input-event))))
1881
1882 (defun company-show-doc-buffer ()
1883 "Temporarily show the documentation buffer for the selection."
1884 (interactive)
1885 (company--electric-do
1886 (let* ((selected (nth company-selection company-candidates))
1887 (doc-buffer (or (company-call-backend 'doc-buffer selected)
1888 (error "No documentation available"))))
1889 (with-current-buffer doc-buffer
1890 (goto-char (point-min)))
1891 (display-buffer doc-buffer t))))
1892 (put 'company-show-doc-buffer 'company-keep t)
1893
1894 (defun company-show-location ()
1895 "Temporarily display a buffer showing the selected candidate in context."
1896 (interactive)
1897 (company--electric-do
1898 (let* ((selected (nth company-selection company-candidates))
1899 (location (company-call-backend 'location selected))
1900 (pos (or (cdr location) (error "No location available")))
1901 (buffer (or (and (bufferp (car location)) (car location))
1902 (find-file-noselect (car location) t))))
1903 (with-selected-window (display-buffer buffer t)
1904 (save-restriction
1905 (widen)
1906 (if (bufferp (car location))
1907 (goto-char pos)
1908 (goto-char (point-min))
1909 (forward-line (1- pos))))
1910 (set-window-start nil (point))))))
1911 (put 'company-show-location 'company-keep t)
1912
1913 ;;; package functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1914
1915 (defvar-local company-callback nil)
1916
1917 (defun company-remove-callback (&optional ignored)
1918 (remove-hook 'company-completion-finished-hook company-callback t)
1919 (remove-hook 'company-completion-cancelled-hook 'company-remove-callback t)
1920 (remove-hook 'company-completion-finished-hook 'company-remove-callback t))
1921
1922 (defun company-begin-backend (backend &optional callback)
1923 "Start a completion at point using BACKEND."
1924 (interactive (let ((val (completing-read "Company back-end: "
1925 obarray
1926 'functionp nil "company-")))
1927 (when val
1928 (list (intern val)))))
1929 (when (setq company-callback callback)
1930 (add-hook 'company-completion-finished-hook company-callback nil t))
1931 (add-hook 'company-completion-cancelled-hook 'company-remove-callback nil t)
1932 (add-hook 'company-completion-finished-hook 'company-remove-callback nil t)
1933 (setq company-backend backend)
1934 ;; Return non-nil if active.
1935 (or (company-manual-begin)
1936 (error "Cannot complete at point")))
1937
1938 (defun company-begin-with (candidates
1939 &optional prefix-length require-match callback)
1940 "Start a completion at point.
1941 CANDIDATES is the list of candidates to use and PREFIX-LENGTH is the length
1942 of the prefix that already is in the buffer before point.
1943 It defaults to 0.
1944
1945 CALLBACK is a function called with the selected result if the user
1946 successfully completes the input.
1947
1948 Example: \(company-begin-with '\(\"foo\" \"foobar\" \"foobarbaz\"\)\)"
1949 (let ((begin-marker (copy-marker (point) t)))
1950 (company-begin-backend
1951 (lambda (command &optional arg &rest ignored)
1952 (pcase command
1953 (`prefix
1954 (when (equal (point) (marker-position begin-marker))
1955 (buffer-substring (- (point) (or prefix-length 0)) (point))))
1956 (`candidates
1957 (all-completions arg candidates))
1958 (`require-match
1959 require-match)))
1960 callback)))
1961
1962 (defun company-version (&optional show-version)
1963 "Get the Company version as string.
1964
1965 If SHOW-VERSION is non-nil, show the version in the echo area."
1966 (interactive (list t))
1967 (with-temp-buffer
1968 (insert-file-contents (find-library-name "company"))
1969 (require 'lisp-mnt)
1970 (if show-version
1971 (message "Company version: %s" (lm-version))
1972 (lm-version))))
1973
1974 ;;; pseudo-tooltip ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1975
1976 (defvar-local company-pseudo-tooltip-overlay nil)
1977
1978 (defvar-local company-tooltip-offset 0)
1979
1980 (defun company-tooltip--lines-update-offset (selection num-lines limit)
1981 (cl-decf limit 2)
1982 (setq company-tooltip-offset
1983 (max (min selection company-tooltip-offset)
1984 (- selection -1 limit)))
1985
1986 (when (<= company-tooltip-offset 1)
1987 (cl-incf limit)
1988 (setq company-tooltip-offset 0))
1989
1990 (when (>= company-tooltip-offset (- num-lines limit 1))
1991 (cl-incf limit)
1992 (when (= selection (1- num-lines))
1993 (cl-decf company-tooltip-offset)
1994 (when (<= company-tooltip-offset 1)
1995 (setq company-tooltip-offset 0)
1996 (cl-incf limit))))
1997
1998 limit)
1999
2000 (defun company-tooltip--simple-update-offset (selection _num-lines limit)
2001 (setq company-tooltip-offset
2002 (if (< selection company-tooltip-offset)
2003 selection
2004 (max company-tooltip-offset
2005 (- selection limit -1)))))
2006
2007 ;;; propertize
2008
2009 (defsubst company-round-tab (arg)
2010 (* (/ (+ arg tab-width) tab-width) tab-width))
2011
2012 (defun company-plainify (str)
2013 (let ((prefix (get-text-property 0 'line-prefix str)))
2014 (when prefix ; Keep the original value unmodified, for no special reason.
2015 (setq str (concat prefix str))
2016 (remove-text-properties 0 (length str) '(line-prefix) str)))
2017 (let* ((pieces (split-string str "\t"))
2018 (copy pieces))
2019 (while (cdr copy)
2020 (setcar copy (company-safe-substring
2021 (car copy) 0 (company-round-tab (string-width (car copy)))))
2022 (pop copy))
2023 (apply 'concat pieces)))
2024
2025 (defun company-fill-propertize (value annotation width selected left right)
2026 (let* ((margin (length left))
2027 (common (+ (or (company-call-backend 'match value)
2028 (length company-common)) margin))
2029 (ann-ralign company-tooltip-align-annotations)
2030 (ann-truncate (< width
2031 (+ (length value) (length annotation)
2032 (if ann-ralign 1 0))))
2033 (ann-start (+ margin
2034 (if ann-ralign
2035 (if ann-truncate
2036 (1+ (length value))
2037 (- width (length annotation)))
2038 (length value))))
2039 (ann-end (min (+ ann-start (length annotation)) (+ margin width)))
2040 (line (concat left
2041 (if (or ann-truncate (not ann-ralign))
2042 (company-safe-substring
2043 (concat value
2044 (when (and annotation ann-ralign) " ")
2045 annotation)
2046 0 width)
2047 (concat
2048 (company-safe-substring value 0
2049 (- width (length annotation)))
2050 annotation))
2051 right)))
2052 (setq width (+ width margin (length right)))
2053
2054 (add-text-properties 0 width '(face company-tooltip
2055 mouse-face company-tooltip-mouse)
2056 line)
2057 (add-text-properties margin common
2058 '(face company-tooltip-common
2059 mouse-face company-tooltip-mouse)
2060 line)
2061 (when (< ann-start ann-end)
2062 (add-text-properties ann-start ann-end
2063 '(face company-tooltip-annotation
2064 mouse-face company-tooltip-mouse)
2065 line))
2066 (when selected
2067 (if (and company-search-string
2068 (string-match (regexp-quote company-search-string) value
2069 (length company-prefix)))
2070 (let ((beg (+ margin (match-beginning 0)))
2071 (end (+ margin (match-end 0))))
2072 (add-text-properties beg end '(face company-tooltip-selection)
2073 line)
2074 (when (< beg common)
2075 (add-text-properties beg common
2076 '(face company-tooltip-common-selection)
2077 line)))
2078 (add-text-properties 0 width '(face company-tooltip-selection
2079 mouse-face company-tooltip-selection)
2080 line)
2081 (add-text-properties margin common
2082 '(face company-tooltip-common-selection
2083 mouse-face company-tooltip-selection)
2084 line)))
2085 line))
2086
2087 ;;; replace
2088
2089 (defun company-buffer-lines (beg end)
2090 (goto-char beg)
2091 (let (lines)
2092 (while (and (= 1 (vertical-motion 1))
2093 (<= (point) end))
2094 (let ((bound (min end (1- (point)))))
2095 ;; A visual line can contain several physical lines (e.g. with outline's
2096 ;; folding overlay). Take only the first one.
2097 (push (buffer-substring beg
2098 (save-excursion
2099 (goto-char beg)
2100 (re-search-forward "$" bound 'move)
2101 (point)))
2102 lines))
2103 (setq beg (point)))
2104 (unless (eq beg end)
2105 (push (buffer-substring beg end) lines))
2106 (nreverse lines)))
2107
2108 (defun company-modify-line (old new offset)
2109 (concat (company-safe-substring old 0 offset)
2110 new
2111 (company-safe-substring old (+ offset (length new)))))
2112
2113 (defsubst company--length-limit (lst limit)
2114 (if (nthcdr limit lst)
2115 limit
2116 (length lst)))
2117
2118 (defun company--replacement-string (lines old column nl &optional align-top)
2119 (cl-decf column company-tooltip-margin)
2120
2121 (when (and align-top company-tooltip-flip-when-above)
2122 (setq lines (reverse lines)))
2123
2124 (let ((width (length (car lines)))
2125 (remaining-cols (- (+ (company--window-width) (window-hscroll))
2126 column)))
2127 (when (> width remaining-cols)
2128 (cl-decf column (- width remaining-cols))))
2129
2130 (let ((offset (and (< column 0) (- column)))
2131 new)
2132 (when offset
2133 (setq column 0))
2134 (when align-top
2135 ;; untouched lines first
2136 (dotimes (_ (- (length old) (length lines)))
2137 (push (pop old) new)))
2138 ;; length into old lines.
2139 (while old
2140 (push (company-modify-line (pop old)
2141 (company--offset-line (pop lines) offset)
2142 column) new))
2143 ;; Append whole new lines.
2144 (while lines
2145 (push (concat (company-space-string column)
2146 (company--offset-line (pop lines) offset))
2147 new))
2148
2149 (let ((str (concat (when nl "\n")
2150 (mapconcat 'identity (nreverse new) "\n")
2151 "\n")))
2152 (font-lock-append-text-property 0 (length str) 'face 'default str)
2153 str)))
2154
2155 (defun company--offset-line (line offset)
2156 (if (and offset line)
2157 (substring line offset)
2158 line))
2159
2160 (defun company--create-lines (selection limit)
2161 (let ((len company-candidates-length)
2162 (numbered 99999)
2163 (window-width (company--window-width))
2164 lines
2165 width
2166 lines-copy
2167 items
2168 previous
2169 remainder
2170 scrollbar-bounds)
2171
2172 ;; Maybe clear old offset.
2173 (when (< len (+ company-tooltip-offset limit))
2174 (setq company-tooltip-offset 0))
2175
2176 ;; Scroll to offset.
2177 (if (eq company-tooltip-offset-display 'lines)
2178 (setq limit (company-tooltip--lines-update-offset selection len limit))
2179 (company-tooltip--simple-update-offset selection len limit))
2180
2181 (cond
2182 ((eq company-tooltip-offset-display 'scrollbar)
2183 (setq scrollbar-bounds (company--scrollbar-bounds company-tooltip-offset
2184 limit len)))
2185 ((eq company-tooltip-offset-display 'lines)
2186 (when (> company-tooltip-offset 0)
2187 (setq previous (format "...(%d)" company-tooltip-offset)))
2188 (setq remainder (- len limit company-tooltip-offset)
2189 remainder (when (> remainder 0)
2190 (setq remainder (format "...(%d)" remainder))))))
2191
2192 (cl-decf selection company-tooltip-offset)
2193 (setq width (max (length previous) (length remainder))
2194 lines (nthcdr company-tooltip-offset company-candidates)
2195 len (min limit len)
2196 lines-copy lines)
2197
2198 (cl-decf window-width (* 2 company-tooltip-margin))
2199 (when scrollbar-bounds (cl-decf window-width))
2200
2201 (dotimes (_ len)
2202 (let* ((value (pop lines-copy))
2203 (annotation (company-call-backend 'annotation value)))
2204 (when (and annotation company-tooltip-align-annotations)
2205 ;; `lisp-completion-at-point' adds a space.
2206 (setq annotation (comment-string-strip annotation t nil)))
2207 (push (cons value annotation) items)
2208 (setq width (max (+ (length value)
2209 (if (and annotation company-tooltip-align-annotations)
2210 (1+ (length annotation))
2211 (length annotation)))
2212 width))))
2213
2214 (setq width (min window-width
2215 (max company-tooltip-minimum-width
2216 (if (and company-show-numbers
2217 (< company-tooltip-offset 10))
2218 (+ 2 width)
2219 width))))
2220
2221 ;; number can make tooltip too long
2222 (when company-show-numbers
2223 (setq numbered company-tooltip-offset))
2224
2225 (let ((items (nreverse items)) new)
2226 (when previous
2227 (push (company--scrollpos-line previous width) new))
2228
2229 (dotimes (i len)
2230 (let* ((item (pop items))
2231 (str (company-reformat (car item)))
2232 (annotation (cdr item))
2233 (right (company-space-string company-tooltip-margin))
2234 (width width))
2235 (when (< numbered 10)
2236 (cl-decf width 2)
2237 (cl-incf numbered)
2238 (setq right (concat (format " %d" (mod numbered 10)) right)))
2239 (push (concat
2240 (company-fill-propertize str annotation
2241 width (equal i selection)
2242 (company-space-string
2243 company-tooltip-margin)
2244 right)
2245 (when scrollbar-bounds
2246 (company--scrollbar i scrollbar-bounds)))
2247 new)))
2248
2249 (when remainder
2250 (push (company--scrollpos-line remainder width) new))
2251
2252 (nreverse new))))
2253
2254 (defun company--scrollbar-bounds (offset limit length)
2255 (when (> length limit)
2256 (let* ((size (ceiling (* limit (float limit)) length))
2257 (lower (floor (* limit (float offset)) length))
2258 (upper (+ lower size -1)))
2259 (cons lower upper))))
2260
2261 (defun company--scrollbar (i bounds)
2262 (propertize " " 'face
2263 (if (and (>= i (car bounds)) (<= i (cdr bounds)))
2264 'company-scrollbar-fg
2265 'company-scrollbar-bg)))
2266
2267 (defun company--scrollpos-line (text width)
2268 (propertize (concat (company-space-string company-tooltip-margin)
2269 (company-safe-substring text 0 width)
2270 (company-space-string company-tooltip-margin))
2271 'face 'company-tooltip))
2272
2273 ;; show
2274
2275 (defsubst company--window-inner-height ()
2276 (let ((edges (window-inside-edges)))
2277 (- (nth 3 edges) (nth 1 edges))))
2278
2279 (defsubst company--window-width ()
2280 (let ((ww (window-width)))
2281 ;; Account for the line continuation column.
2282 (when (zerop (cadr (window-fringes)))
2283 (cl-decf ww))
2284 (unless (or (display-graphic-p)
2285 (version< "24.3.1" emacs-version))
2286 ;; Emacs 24.3 and earlier included margins
2287 ;; in window-width when in TTY.
2288 (cl-decf ww
2289 (let ((margins (window-margins)))
2290 (+ (or (car margins) 0)
2291 (or (cdr margins) 0)))))
2292 ww))
2293
2294 (defun company--pseudo-tooltip-height ()
2295 "Calculate the appropriate tooltip height.
2296 Returns a negative number if the tooltip should be displayed above point."
2297 (let* ((lines (company--row))
2298 (below (- (company--window-inner-height) 1 lines)))
2299 (if (and (< below (min company-tooltip-minimum company-candidates-length))
2300 (> lines below))
2301 (- (max 3 (min company-tooltip-limit lines)))
2302 (max 3 (min company-tooltip-limit below)))))
2303
2304 (defun company-pseudo-tooltip-show (row column selection)
2305 (company-pseudo-tooltip-hide)
2306 (save-excursion
2307
2308 (let* ((height (company--pseudo-tooltip-height))
2309 above)
2310
2311 (when (< height 0)
2312 (setq row (+ row height -1)
2313 above t))
2314
2315 (let* ((nl (< (move-to-window-line row) row))
2316 (beg (point))
2317 (end (save-excursion
2318 (move-to-window-line (+ row (abs height)))
2319 (point)))
2320 (ov (make-overlay beg end))
2321 (args (list (mapcar 'company-plainify
2322 (company-buffer-lines beg end))
2323 column nl above)))
2324
2325 (setq company-pseudo-tooltip-overlay ov)
2326 (overlay-put ov 'company-replacement-args args)
2327
2328 (let ((lines (company--create-lines selection (abs height))))
2329 (overlay-put ov 'company-after
2330 (apply 'company--replacement-string lines args))
2331 (overlay-put ov 'company-width (string-width (car lines))))
2332
2333 (overlay-put ov 'company-column column)
2334 (overlay-put ov 'company-height height)))))
2335
2336 (defun company-pseudo-tooltip-show-at-point (pos)
2337 (let ((row (company--row pos))
2338 (col (company--column pos)))
2339 (company-pseudo-tooltip-show (1+ row) col company-selection)))
2340
2341 (defun company-pseudo-tooltip-edit (selection)
2342 (let ((height (overlay-get company-pseudo-tooltip-overlay 'company-height)))
2343 (overlay-put company-pseudo-tooltip-overlay 'company-after
2344 (apply 'company--replacement-string
2345 (company--create-lines selection (abs height))
2346 (overlay-get company-pseudo-tooltip-overlay
2347 'company-replacement-args)))))
2348
2349 (defun company-pseudo-tooltip-hide ()
2350 (when company-pseudo-tooltip-overlay
2351 (delete-overlay company-pseudo-tooltip-overlay)
2352 (setq company-pseudo-tooltip-overlay nil)))
2353
2354 (defun company-pseudo-tooltip-hide-temporarily ()
2355 (when (overlayp company-pseudo-tooltip-overlay)
2356 (overlay-put company-pseudo-tooltip-overlay 'invisible nil)
2357 (overlay-put company-pseudo-tooltip-overlay 'line-prefix nil)
2358 (overlay-put company-pseudo-tooltip-overlay 'after-string nil)))
2359
2360 (defun company-pseudo-tooltip-unhide ()
2361 (when company-pseudo-tooltip-overlay
2362 (overlay-put company-pseudo-tooltip-overlay 'invisible t)
2363 ;; Beat outline's folding overlays, at least.
2364 (overlay-put company-pseudo-tooltip-overlay 'priority 1)
2365 ;; No (extra) prefix for the first line.
2366 (overlay-put company-pseudo-tooltip-overlay 'line-prefix "")
2367 (overlay-put company-pseudo-tooltip-overlay 'after-string
2368 (overlay-get company-pseudo-tooltip-overlay 'company-after))
2369 (overlay-put company-pseudo-tooltip-overlay 'window (selected-window))))
2370
2371 (defun company-pseudo-tooltip-guard ()
2372 (buffer-substring-no-properties
2373 (point) (overlay-start company-pseudo-tooltip-overlay)))
2374
2375 (defun company-pseudo-tooltip-frontend (command)
2376 "`company-mode' front-end similar to a tooltip but based on overlays."
2377 (cl-case command
2378 (pre-command (company-pseudo-tooltip-hide-temporarily))
2379 (post-command
2380 (let ((old-height (if (overlayp company-pseudo-tooltip-overlay)
2381 (overlay-get company-pseudo-tooltip-overlay
2382 'company-height)
2383 0))
2384 (new-height (company--pseudo-tooltip-height)))
2385 (unless (and (>= (* old-height new-height) 0)
2386 (>= (abs old-height) (abs new-height))
2387 (equal (company-pseudo-tooltip-guard)
2388 (overlay-get company-pseudo-tooltip-overlay
2389 'company-guard)))
2390 ;; Redraw needed.
2391 (company-pseudo-tooltip-show-at-point (- (point)
2392 (length company-prefix)))
2393 (overlay-put company-pseudo-tooltip-overlay
2394 'company-guard (company-pseudo-tooltip-guard))))
2395 (company-pseudo-tooltip-unhide))
2396 (hide (company-pseudo-tooltip-hide)
2397 (setq company-tooltip-offset 0))
2398 (update (when (overlayp company-pseudo-tooltip-overlay)
2399 (company-pseudo-tooltip-edit company-selection)))))
2400
2401 (defun company-pseudo-tooltip-unless-just-one-frontend (command)
2402 "`company-pseudo-tooltip-frontend', but not shown for single candidates."
2403 (unless (and (eq command 'post-command)
2404 (company--show-inline-p))
2405 (company-pseudo-tooltip-frontend command)))
2406
2407 ;;; overlay ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2408
2409 (defvar-local company-preview-overlay nil)
2410
2411 (defun company-preview-show-at-point (pos)
2412 (company-preview-hide)
2413
2414 (setq company-preview-overlay (make-overlay pos (1+ pos)))
2415
2416 (let ((completion (nth company-selection company-candidates)))
2417 (setq completion (propertize completion 'face 'company-preview))
2418 (add-text-properties 0 (length company-common)
2419 '(face company-preview-common) completion)
2420
2421 ;; Add search string
2422 (and company-search-string
2423 (string-match (regexp-quote company-search-string) completion)
2424 (add-text-properties (match-beginning 0)
2425 (match-end 0)
2426 '(face company-preview-search)
2427 completion))
2428
2429 (setq completion (company-strip-prefix completion))
2430
2431 (and (equal pos (point))
2432 (not (equal completion ""))
2433 (add-text-properties 0 1 '(cursor t) completion))
2434
2435 (overlay-put company-preview-overlay 'display
2436 (concat completion (unless (eq pos (point-max))
2437 (buffer-substring pos (1+ pos)))))
2438 (overlay-put company-preview-overlay 'window (selected-window))))
2439
2440 (defun company-preview-hide ()
2441 (when company-preview-overlay
2442 (delete-overlay company-preview-overlay)
2443 (setq company-preview-overlay nil)))
2444
2445 (defun company-preview-frontend (command)
2446 "`company-mode' front-end showing the selection as if it had been inserted."
2447 (pcase command
2448 (`pre-command (company-preview-hide))
2449 (`post-command (company-preview-show-at-point (point)))
2450 (`hide (company-preview-hide))))
2451
2452 (defun company-preview-if-just-one-frontend (command)
2453 "`company-preview-frontend', but only shown for single candidates."
2454 (when (or (not (eq command 'post-command))
2455 (company--show-inline-p))
2456 (company-preview-frontend command)))
2457
2458 (defun company--show-inline-p ()
2459 (and (not (cdr company-candidates))
2460 company-common
2461 (string-prefix-p company-prefix company-common
2462 (company-call-backend 'ignore-case))))
2463
2464 ;;; echo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2465
2466 (defvar-local company-echo-last-msg nil)
2467
2468 (defvar company-echo-timer nil)
2469
2470 (defvar company-echo-delay .01)
2471
2472 (defun company-echo-show (&optional getter)
2473 (when getter
2474 (setq company-echo-last-msg (funcall getter)))
2475 (let ((message-log-max nil))
2476 (if company-echo-last-msg
2477 (message "%s" company-echo-last-msg)
2478 (message ""))))
2479
2480 (defun company-echo-show-soon (&optional getter)
2481 (when company-echo-timer
2482 (cancel-timer company-echo-timer))
2483 (setq company-echo-timer (run-with-timer 0 nil 'company-echo-show getter)))
2484
2485 (defsubst company-echo-show-when-idle (&optional getter)
2486 (when (sit-for company-echo-delay)
2487 (company-echo-show getter)))
2488
2489 (defun company-echo-format ()
2490
2491 (let ((limit (window-width (minibuffer-window)))
2492 (len -1)
2493 ;; Roll to selection.
2494 (candidates (nthcdr company-selection company-candidates))
2495 (i (if company-show-numbers company-selection 99999))
2496 comp msg)
2497
2498 (while candidates
2499 (setq comp (company-reformat (pop candidates))
2500 len (+ len 1 (length comp)))
2501 (if (< i 10)
2502 ;; Add number.
2503 (progn
2504 (setq comp (propertize (format "%d: %s" i comp)
2505 'face 'company-echo))
2506 (cl-incf len 3)
2507 (cl-incf i)
2508 (add-text-properties 3 (+ 3 (length company-common))
2509 '(face company-echo-common) comp))
2510 (setq comp (propertize comp 'face 'company-echo))
2511 (add-text-properties 0 (length company-common)
2512 '(face company-echo-common) comp))
2513 (if (>= len limit)
2514 (setq candidates nil)
2515 (push comp msg)))
2516
2517 (mapconcat 'identity (nreverse msg) " ")))
2518
2519 (defun company-echo-strip-common-format ()
2520
2521 (let ((limit (window-width (minibuffer-window)))
2522 (len (+ (length company-prefix) 2))
2523 ;; Roll to selection.
2524 (candidates (nthcdr company-selection company-candidates))
2525 (i (if company-show-numbers company-selection 99999))
2526 msg comp)
2527
2528 (while candidates
2529 (setq comp (company-strip-prefix (pop candidates))
2530 len (+ len 2 (length comp)))
2531 (when (< i 10)
2532 ;; Add number.
2533 (setq comp (format "%s (%d)" comp i))
2534 (cl-incf len 4)
2535 (cl-incf i))
2536 (if (>= len limit)
2537 (setq candidates nil)
2538 (push (propertize comp 'face 'company-echo) msg)))
2539
2540 (concat (propertize company-prefix 'face 'company-echo-common) "{"
2541 (mapconcat 'identity (nreverse msg) ", ")
2542 "}")))
2543
2544 (defun company-echo-hide ()
2545 (unless (equal company-echo-last-msg "")
2546 (setq company-echo-last-msg "")
2547 (company-echo-show)))
2548
2549 (defun company-echo-frontend (command)
2550 "`company-mode' front-end showing the candidates in the echo area."
2551 (pcase command
2552 (`post-command (company-echo-show-soon 'company-echo-format))
2553 (`hide (company-echo-hide))))
2554
2555 (defun company-echo-strip-common-frontend (command)
2556 "`company-mode' front-end showing the candidates in the echo area."
2557 (pcase command
2558 (`post-command (company-echo-show-soon 'company-echo-strip-common-format))
2559 (`hide (company-echo-hide))))
2560
2561 (defun company-echo-metadata-frontend (command)
2562 "`company-mode' front-end showing the documentation in the echo area."
2563 (pcase command
2564 (`post-command (company-echo-show-when-idle 'company-fetch-metadata))
2565 (`hide (company-echo-hide))))
2566
2567 (provide 'company)
2568 ;;; company.el ends here