forked from company-mode/company-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
company.el
3103 lines (2711 loc) · 121 KB
/
company.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; company.el --- Modular text completion framework -*- lexical-binding: t -*-
;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
;; Author: Nikolaj Schumacher
;; Maintainer: Dmitry Gutov <dgutov@yandex.ru>
;; URL: http://company-mode.github.io/
;; Version: 0.9.0
;; Keywords: abbrev, convenience, matching
;; Package-Requires: ((emacs "24.1") (cl-lib "0.5"))
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Company is a modular completion framework. Modules for retrieving completion
;; candidates are called backends, modules for displaying them are frontends.
;;
;; Company comes with many backends, e.g. `company-etags'. These are
;; distributed in separate files and can be used individually.
;;
;; Enable `company-mode' in all buffers with M-x global-company-mode. For
;; further information look at the documentation for `company-mode' (C-h f
;; company-mode RET).
;;
;; If you want to start a specific backend, call it interactively or use
;; `company-begin-backend'. For example:
;; M-x company-abbrev will prompt for and insert an abbrev.
;;
;; To write your own backend, look at the documentation for `company-backends'.
;; Here is a simple example completing "foo":
;;
;; (defun company-my-backend (command &optional arg &rest ignored)
;; (pcase command
;; (`prefix (company-grab-symbol))
;; (`candidates (list "foobar" "foobaz" "foobarbaz"))
;; (`meta (format "This value is named %s" arg))))
;;
;; Sometimes it is a good idea to mix several backends together, for example to
;; enrich gtags with dabbrev-code results (to emulate local variables). To do
;; this, add a list with both backends as an element in `company-backends'.
;;
;;; Change Log:
;;
;; See NEWS.md in the repository.
;;; Code:
(require 'cl-lib)
(require 'newcomment)
(require 'pcase)
;; FIXME: Use `user-error'.
(add-to-list 'debug-ignored-errors "^.* frontend cannot be used twice$")
(add-to-list 'debug-ignored-errors "^Echo area cannot be used twice$")
(add-to-list 'debug-ignored-errors "^No \\(document\\|loc\\)ation available$")
(add-to-list 'debug-ignored-errors "^Company not ")
(add-to-list 'debug-ignored-errors "^No candidate number ")
(add-to-list 'debug-ignored-errors "^Cannot complete at point$")
(add-to-list 'debug-ignored-errors "^No other backend$")
;;; Compatibility
(eval-and-compile
;; `defvar-local' for Emacs 24.2 and below
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL.
Like `defvar' but additionally marks the variable as being automatically
buffer-local wherever it is set."
(declare (debug defvar) (doc-string 3))
`(progn
(defvar ,var ,val ,docstring)
(make-variable-buffer-local ',var))))
(unless (fboundp 'string-suffix-p)
(defun string-suffix-p (suffix string &optional ignore-case)
"Return non-nil if SUFFIX is a suffix of STRING.
If IGNORE-CASE is non-nil, the comparison is done without paying
attention to case differences."
(let ((start-pos (- (length string) (length suffix))))
(and (>= start-pos 0)
(eq t (compare-strings suffix nil nil
string start-pos nil ignore-case)))))))
(defgroup company nil
"Extensible inline text completion mechanism"
:group 'abbrev
:group 'convenience
:group 'matching)
(defface company-tooltip
'((default :foreground "black")
(((class color) (min-colors 88) (background light))
(:background "cornsilk"))
(((class color) (min-colors 88) (background dark))
(:background "yellow")))
"Face used for the tooltip.")
(defface company-tooltip-selection
'((((class color) (min-colors 88) (background light))
(:background "light blue"))
(((class color) (min-colors 88) (background dark))
(:background "orange1"))
(t (:background "green")))
"Face used for the selection in the tooltip.")
(defface company-tooltip-search
'((default :inherit company-tooltip-selection))
"Face used for the search string in the tooltip.")
(defface company-tooltip-mouse
'((default :inherit highlight))
"Face used for the tooltip item under the mouse.")
(defface company-tooltip-common
'((((background light))
:foreground "darkred")
(((background dark))
:foreground "red"))
"Face used for the common completion in the tooltip.")
(defface company-tooltip-common-selection
'((default :inherit company-tooltip-common))
"Face used for the selected common completion in the tooltip.")
(defface company-tooltip-annotation
'((((background light))
:foreground "firebrick4")
(((background dark))
:foreground "red4"))
"Face used for the completion annotation in the tooltip.")
(defface company-tooltip-annotation-selection
'((default :inherit company-tooltip-annotation))
"Face used for the selected completion annotation in the tooltip.")
(defface company-scrollbar-fg
'((((background light))
:background "darkred")
(((background dark))
:background "red"))
"Face used for the tooltip scrollbar thumb.")
(defface company-scrollbar-bg
'((((background light))
:background "wheat")
(((background dark))
:background "gold"))
"Face used for the tooltip scrollbar background.")
(defface company-preview
'((((background light))
:inherit (company-tooltip-selection company-tooltip))
(((background dark))
:background "blue4"
:foreground "wheat"))
"Face used for the completion preview.")
(defface company-preview-common
'((((background light))
:inherit company-tooltip-common-selection)
(((background dark))
:inherit company-preview
:foreground "red"))
"Face used for the common part of the completion preview.")
(defface company-preview-search
'((((background light))
:inherit company-tooltip-common-selection)
(((background dark))
:inherit company-preview
:background "blue1"))
"Face used for the search string in the completion preview.")
(defface company-echo nil
"Face used for completions in the echo area.")
(defface company-echo-common
'((((background dark)) (:foreground "firebrick1"))
(((background light)) (:background "firebrick4")))
"Face used for the common part of completions in the echo area.")
(defun company-frontends-set (variable value)
;; Uniquify.
(let ((value (delete-dups (copy-sequence value))))
(and (or (and (memq 'company-pseudo-tooltip-unless-just-one-frontend value)
(memq 'company-pseudo-tooltip-frontend value))
(and (memq 'company-pseudo-tooltip-unless-just-one-frontend-with-delay value)
(memq 'company-pseudo-tooltip-frontend value))
(and (memq 'company-pseudo-tooltip-unless-just-one-frontend-with-delay value)
(memq 'company-pseudo-tooltip-unless-just-one-frontend value)))
(error "Pseudo tooltip frontend cannot be used more than once"))
(and (memq 'company-preview-if-just-one-frontend value)
(memq 'company-preview-frontend value)
(error "Preview frontend cannot be used twice"))
(and (memq 'company-echo value)
(memq 'company-echo-metadata-frontend value)
(error "Echo area cannot be used twice"))
;; Preview must come last.
(dolist (f '(company-preview-if-just-one-frontend company-preview-frontend))
(when (cdr (memq f value))
(setq value (append (delq f value) (list f)))))
(set variable value)))
(defcustom company-frontends '(company-pseudo-tooltip-unless-just-one-frontend
company-preview-if-just-one-frontend
company-echo-metadata-frontend)
"The list of active frontends (visualizations).
Each frontend is a function that takes one argument. It is called with
one of the following arguments:
`show': When the visualization should start.
`hide': When the visualization should end.
`update': When the data has been updated.
`pre-command': Before every command that is executed while the
visualization is active.
`post-command': After every command that is executed while the
visualization is active.
The visualized data is stored in `company-prefix', `company-candidates',
`company-common', `company-selection', `company-point' and
`company-search-string'."
:set 'company-frontends-set
:type '(repeat (choice (const :tag "echo" company-echo-frontend)
(const :tag "echo, strip common"
company-echo-strip-common-frontend)
(const :tag "show echo meta-data in echo"
company-echo-metadata-frontend)
(const :tag "pseudo tooltip"
company-pseudo-tooltip-frontend)
(const :tag "pseudo tooltip, multiple only"
company-pseudo-tooltip-unless-just-one-frontend)
(const :tag "pseudo tooltip, multiple only, delayed"
company-pseudo-tooltip-unless-just-one-frontend-with-delay)
(const :tag "preview" company-preview-frontend)
(const :tag "preview, unique only"
company-preview-if-just-one-frontend)
(function :tag "custom function" nil))))
(defcustom company-tooltip-limit 10
"The maximum number of candidates in the tooltip."
:type 'integer)
(defcustom company-tooltip-minimum 6
"The minimum height of the tooltip.
If this many lines are not available, prefer to display the tooltip above."
:type 'integer)
(defcustom company-tooltip-minimum-width 0
"The minimum width of the tooltip's inner area.
This doesn't include the margins and the scroll bar."
:type 'integer
:package-version '(company . "0.8.0"))
(defcustom company-tooltip-margin 1
"Width of margin columns to show around the toolip."
:type 'integer)
(defcustom company-tooltip-offset-display 'scrollbar
"Method using which the tooltip displays scrolling position.
`scrollbar' means draw a scrollbar to the right of the items.
`lines' means wrap items in lines with \"before\" and \"after\" counters."
:type '(choice (const :tag "Scrollbar" scrollbar)
(const :tag "Two lines" lines)))
(defcustom company-tooltip-align-annotations nil
"When non-nil, align annotations to the right tooltip border."
:type 'boolean
:package-version '(company . "0.7.1"))
(defcustom company-tooltip-flip-when-above nil
"Whether to flip the tooltip when it's above the current line."
:type 'boolean
:package-version '(company . "0.8.1"))
(defvar company-safe-backends
'((company-abbrev . "Abbrev")
(company-bbdb . "BBDB")
(company-capf . "completion-at-point-functions")
(company-clang . "Clang")
(company-cmake . "CMake")
(company-css . "CSS")
(company-dabbrev . "dabbrev for plain text")
(company-dabbrev-code . "dabbrev for code")
(company-eclim . "Eclim (an Eclipse interface)")
(company-elisp . "Emacs Lisp")
(company-etags . "etags")
(company-files . "Files")
(company-gtags . "GNU Global")
(company-ispell . "Ispell")
(company-keywords . "Programming language keywords")
(company-nxml . "nxml")
(company-oddmuse . "Oddmuse")
(company-semantic . "Semantic")
(company-tempo . "Tempo templates")
(company-xcode . "Xcode")))
(put 'company-safe-backends 'risky-local-variable t)
(defun company-safe-backends-p (backends)
(and (consp backends)
(not (cl-dolist (backend backends)
(unless (if (consp backend)
(company-safe-backends-p backend)
(assq backend company-safe-backends))
(cl-return t))))))
(defcustom company-backends `(,@(unless (version< "24.3.51" emacs-version)
(list 'company-elisp))
company-bbdb
company-nxml company-css
company-eclim company-semantic company-clang
company-xcode company-cmake
company-capf
company-files
(company-dabbrev-code company-gtags company-etags
company-keywords)
company-oddmuse company-dabbrev)
"The list of active backends (completion engines).
Only one backend is used at a time. The choice depends on the order of
the items in this list, and on the values they return in response to the
`prefix' command (see below). But a backend can also be a \"grouped\"
one (see below).
`company-begin-backend' can be used to start a specific backend,
`company-other-backend' will skip to the next matching backend in the list.
Each backend is a function that takes a variable number of arguments.
The first argument is the command requested from the backend. It is one
of the following:
`prefix': The backend should return the text to be completed. It must be
text immediately before point. Returning nil from this command passes
control to the next backend. The function should return `stop' if it
should complete but cannot (e.g. if it is in the middle of a string).
Instead of a string, the backend may return a cons (PREFIX . LENGTH)
where LENGTH is a number used in place of PREFIX's length when
comparing against `company-minimum-prefix-length'. LENGTH can also
be just t, and in the latter case the test automatically succeeds.
`candidates': The second argument is the prefix to be completed. The
return value should be a list of candidates that match the prefix.
Non-prefix matches are also supported (candidates that don't start with the
prefix, but match it in some backend-defined way). Backends that use this
feature must disable cache (return t to `no-cache') and might also want to
respond to `match'.
Optional commands
=================
`sorted': Return t here to indicate that the candidates are sorted and will
not need to be sorted again.
`duplicates': If non-nil, company will take care of removing duplicates
from the list.
`no-cache': Usually company doesn't ask for candidates again as completion
progresses, unless the backend returns t for this command. The second
argument is the latest prefix.
`ignore-case': Return t here if the backend returns case-insensitive
matches. This value is used to determine the longest common prefix (as
used in `company-complete-common'), and to filter completions when fetching
them from cache.
`meta': The second argument is a completion candidate. Return a (short)
documentation string for it.
`doc-buffer': The second argument is a completion candidate. Return a
buffer with documentation for it. Preferably use `company-doc-buffer'. If
not all buffer contents pertain to this candidate, return a cons of buffer
and window start position.
`location': The second argument is a completion candidate. Return a cons
of buffer and buffer location, or of file and line number where the
completion candidate was defined.
`annotation': The second argument is a completion candidate. Return a
string to be displayed inline with the candidate in the popup. If
duplicates are removed by company, candidates with equal string values will
be kept if they have different annotations. For that to work properly,
backends should store the related information on candidates using text
properties.
`match': The second argument is a completion candidate. Return the index
after the end of text matching `prefix' within the candidate string. It
will be used when rendering the popup. This command only makes sense for
backends that provide non-prefix completion.
`require-match': If this returns t, the user is not allowed to enter
anything not offered as a candidate. Please don't use that value in normal
backends. The default value nil gives the user that choice with
`company-require-match'. Return value `never' overrides that option the
other way around.
`init': Called once for each buffer. The backend can check for external
programs and files and load any required libraries. Raising an error here
will show up in message log once, and the backend will not be used for
completion.
`post-completion': Called after a completion candidate has been inserted
into the buffer. The second argument is the candidate. Can be used to
modify it, e.g. to expand a snippet.
The backend should return nil for all commands it does not support or
does not know about. It should also be callable interactively and use
`company-begin-backend' to start itself in that case.
Grouped backends
================
An element of `company-backends' can also be a list of backends. The
completions from backends in such groups are merged, but only from those
backends which return the same `prefix'.
If a backend command takes a candidate as an argument (e.g. `meta'), the
call is dispatched to the backend the candidate came from. In other
cases (except for `duplicates' and `sorted'), the first non-nil value among
all the backends is returned.
The group can also contain keywords. Currently, `:with' and `:separate'
keywords are defined. If the group contains keyword `:with', the backends
listed after this keyword are ignored for the purpose of the `prefix'
command. If the group contains keyword `:separate', the candidates that
come from different backends are sorted separately in the combined list.
Asynchronous backends
=====================
The return value of each command can also be a cons (:async . FETCHER)
where FETCHER is a function of one argument, CALLBACK. When the data
arrives, FETCHER must call CALLBACK and pass it the appropriate return
value, as described above. That call must happen in the same buffer as
where completion was initiated.
True asynchronous operation is only supported for command `candidates', and
only during idle completion. Other commands will block the user interface,
even if the backend uses the asynchronous calling convention."
:type `(repeat
(choice
:tag "backend"
,@(mapcar (lambda (b) `(const :tag ,(cdr b) ,(car b)))
company-safe-backends)
(symbol :tag "User defined")
(repeat :tag "Merged backends"
(choice :tag "backend"
,@(mapcar (lambda (b)
`(const :tag ,(cdr b) ,(car b)))
company-safe-backends)
(const :tag "With" :with)
(symbol :tag "User defined"))))))
(put 'company-backends 'safe-local-variable 'company-safe-backends-p)
(defcustom company-transformers nil
"Functions to change the list of candidates received from backends.
Each function gets called with the return value of the previous one.
The first one gets passed the list of candidates, already sorted and
without duplicates."
:type '(choice
(const :tag "None" nil)
(const :tag "Sort by occurrence" (company-sort-by-occurrence))
(const :tag "Sort by backend importance"
(company-sort-by-backend-importance))
(const :tag "Prefer case sensitive prefix"
(company-sort-prefer-same-case-prefix))
(repeat :tag "User defined" (function))))
(defcustom company-completion-started-hook nil
"Hook run when company starts completing.
The hook is called with one argument that is non-nil if the completion was
started manually."
:type 'hook)
(defcustom company-completion-cancelled-hook nil
"Hook run when company cancels completing.
The hook is called with one argument that is non-nil if the completion was
aborted manually."
:type 'hook)
(defcustom company-completion-finished-hook nil
"Hook run when company successfully completes.
The hook is called with the selected candidate as an argument.
If you indend to use it to post-process candidates from a specific
backend, consider using the `post-completion' command instead."
:type 'hook)
(defcustom company-minimum-prefix-length 3
"The minimum prefix length for idle completion."
:type '(integer :tag "prefix length"))
(defcustom company-abort-manual-when-too-short nil
"If enabled, cancel a manually started completion when the prefix gets
shorter than both `company-minimum-prefix-length' and the length of the
prefix it was started from."
:type 'boolean
:package-version '(company . "0.8.0"))
(defcustom company-require-match 'company-explicit-action-p
"If enabled, disallow non-matching input.
This can be a function do determine if a match is required.
This can be overridden by the backend, if it returns t or `never' to
`require-match'. `company-auto-complete' also takes precedence over this."
:type '(choice (const :tag "Off" nil)
(function :tag "Predicate function")
(const :tag "On, if user interaction took place"
'company-explicit-action-p)
(const :tag "On" t)))
(defcustom company-auto-complete nil
"Determines when to auto-complete.
If this is enabled, all characters from `company-auto-complete-chars'
trigger insertion of the selected completion candidate.
This can also be a function."
:type '(choice (const :tag "Off" nil)
(function :tag "Predicate function")
(const :tag "On, if user interaction took place"
'company-explicit-action-p)
(const :tag "On" t)))
(defcustom company-auto-complete-chars '(?\ ?\) ?.)
"Determines which characters trigger auto-completion.
See `company-auto-complete'. If this is a string, each string character
tiggers auto-completion. If it is a list of syntax description characters (see
`modify-syntax-entry'), all characters with that syntax auto-complete.
This can also be a function, which is called with the new input and should
return non-nil if company should auto-complete.
A character that is part of a valid candidate never triggers auto-completion."
:type '(choice (string :tag "Characters")
(set :tag "Syntax"
(const :tag "Whitespace" ?\ )
(const :tag "Symbol" ?_)
(const :tag "Opening parentheses" ?\()
(const :tag "Closing parentheses" ?\))
(const :tag "Word constituent" ?w)
(const :tag "Punctuation." ?.)
(const :tag "String quote." ?\")
(const :tag "Paired delimiter." ?$)
(const :tag "Expression quote or prefix operator." ?\')
(const :tag "Comment starter." ?<)
(const :tag "Comment ender." ?>)
(const :tag "Character-quote." ?/)
(const :tag "Generic string fence." ?|)
(const :tag "Generic comment fence." ?!))
(function :tag "Predicate function")))
(defcustom company-idle-delay .5
"The idle delay in seconds until completion starts automatically.
The prefix still has to satisfy `company-minimum-prefix-length' before that
happens. The value of nil means no idle completion."
:type '(choice (const :tag "never (nil)" nil)
(const :tag "immediate (0)" 0)
(number :tag "seconds")))
(defcustom company-tooltip-idle-delay .5
"The idle delay in seconds until tooltip is shown when using
`company-pseudo-tooltip-unless-just-one-frontend-with-delay'."
:type '(choice (const :tag "never (nil)" nil)
(const :tag "immediate (0)" 0)
(number :tag "seconds")))
(defcustom company-begin-commands '(self-insert-command
org-self-insert-command
orgtbl-self-insert-command
c-scope-operator
c-electric-colon
c-electric-lt-gt
c-electric-slash)
"A list of commands after which idle completion is allowed.
If this is t, it can show completions after any command except a few from a
pre-defined list. See `company-idle-delay'.
Alternatively, any command with a non-nil `company-begin' property is
treated as if it was on this list."
:type '(choice (const :tag "Any command" t)
(const :tag "Self insert command" '(self-insert-command))
(repeat :tag "Commands" function))
:package-version '(company . "0.8.4"))
(defcustom company-continue-commands '(not save-buffer save-some-buffers
save-buffers-kill-terminal
save-buffers-kill-emacs)
"A list of commands that are allowed during completion.
If this is t, or if `company-begin-commands' is t, any command is allowed.
Otherwise, the value must be a list of symbols. If it starts with `not',
the cdr is the list of commands that abort completion. Otherwise, all
commands except those in that list, or in `company-begin-commands', or
commands in the `company-' namespace, abort completion."
:type '(choice (const :tag "Any command" t)
(cons :tag "Any except"
(const not)
(repeat :tag "Commands" function))
(repeat :tag "Commands" function)))
(defcustom company-show-numbers nil
"If enabled, show quick-access numbers for the first ten candidates."
:type '(choice (const :tag "off" nil)
(const :tag "on" t)))
(defcustom company-selection-wrap-around nil
"If enabled, selecting item before first or after last wraps around."
:type '(choice (const :tag "off" nil)
(const :tag "on" t)))
(defvar company-async-wait 0.03
"Pause between checks to see if the value's been set when turning an
asynchronous call into synchronous.")
(defvar company-async-timeout 2
"Maximum wait time for a value to be set during asynchronous call.")
;;; mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar company-mode-map (make-sparse-keymap)
"Keymap used by `company-mode'.")
(defvar company-active-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap "\e\e\e" 'company-abort)
(define-key keymap "\C-g" 'company-abort)
(define-key keymap (kbd "M-n") 'company-select-next)
(define-key keymap (kbd "M-p") 'company-select-previous)
(define-key keymap (kbd "<down>") 'company-select-next-or-abort)
(define-key keymap (kbd "<up>") 'company-select-previous-or-abort)
(define-key keymap [remap scroll-up-command] 'company-next-page)
(define-key keymap [remap scroll-down-command] 'company-previous-page)
(define-key keymap [down-mouse-1] 'ignore)
(define-key keymap [down-mouse-3] 'ignore)
(define-key keymap [mouse-1] 'company-complete-mouse)
(define-key keymap [mouse-3] 'company-select-mouse)
(define-key keymap [up-mouse-1] 'ignore)
(define-key keymap [up-mouse-3] 'ignore)
(define-key keymap [return] 'company-complete-selection)
(define-key keymap (kbd "RET") 'company-complete-selection)
(define-key keymap [tab] 'company-complete-common)
(define-key keymap (kbd "TAB") 'company-complete-common)
(define-key keymap (kbd "<f1>") 'company-show-doc-buffer)
(define-key keymap (kbd "C-h") 'company-show-doc-buffer)
(define-key keymap "\C-w" 'company-show-location)
(define-key keymap "\C-s" 'company-search-candidates)
(define-key keymap "\C-\M-s" 'company-filter-candidates)
(dotimes (i 10)
(define-key keymap (read-kbd-macro (format "M-%d" i)) 'company-complete-number))
keymap)
"Keymap that is enabled during an active completion.")
(defvar company--disabled-backends nil)
(defun company-init-backend (backend)
(and (symbolp backend)
(not (fboundp backend))
(ignore-errors (require backend nil t)))
(cond
((symbolp backend)
(condition-case err
(progn
(funcall backend 'init)
(put backend 'company-init t))
(error
(put backend 'company-init 'failed)
(unless (memq backend company--disabled-backends)
(message "Company backend '%s' could not be initialized:\n%s"
backend (error-message-string err)))
(cl-pushnew backend company--disabled-backends)
nil)))
;; No initialization for lambdas.
((functionp backend) t)
(t ;; Must be a list.
(cl-dolist (b backend)
(unless (keywordp b)
(company-init-backend b))))))
(defcustom company-lighter-base "company"
"Base string to use for the `company-mode' lighter."
:type 'string
:package-version '(company . "0.8.10"))
(defvar company-lighter '(" "
(company-candidates
(:eval
(if (consp company-backend)
(company--group-lighter (nth company-selection
company-candidates)
company-lighter-base)
(symbol-name company-backend)))
company-lighter-base))
"Mode line lighter for Company.
The value of this variable is a mode line template as in
`mode-line-format'.")
(put 'company-lighter 'risky-local-variable t)
;;;###autoload
(define-minor-mode company-mode
"\"complete anything\"; is an in-buffer completion framework.
Completion starts automatically, depending on the values
`company-idle-delay' and `company-minimum-prefix-length'.
Completion can be controlled with the commands:
`company-complete-common', `company-complete-selection', `company-complete',
`company-select-next', `company-select-previous'. If these commands are
called before `company-idle-delay', completion will also start.
Completions can be searched with `company-search-candidates' or
`company-filter-candidates'. These can be used while completion is
inactive, as well.
The completion data is retrieved using `company-backends' and displayed
using `company-frontends'. If you want to start a specific backend, call
it interactively or use `company-begin-backend'.
By default, the completions list is sorted alphabetically, unless the
backend chooses otherwise, or `company-transformers' changes it later.
regular keymap (`company-mode-map'):
\\{company-mode-map}
keymap during active completions (`company-active-map'):
\\{company-active-map}"
nil company-lighter company-mode-map
(if company-mode
(progn
(add-hook 'pre-command-hook 'company-pre-command nil t)
(add-hook 'post-command-hook 'company-post-command nil t)
(mapc 'company-init-backend company-backends))
(remove-hook 'pre-command-hook 'company-pre-command t)
(remove-hook 'post-command-hook 'company-post-command t)
(company-cancel)
(kill-local-variable 'company-point)))
(defcustom company-global-modes t
"Modes for which `company-mode' mode is turned on by `global-company-mode'.
If nil, means no modes. If t, then all major modes have it turned on.
If a list, it should be a list of `major-mode' symbol names for which
`company-mode' should be automatically turned on. The sense of the list is
negated if it begins with `not'. For example:
(c-mode c++-mode)
means that `company-mode' is turned on for buffers in C and C++ modes only.
(not message-mode)
means that `company-mode' is always turned on except in `message-mode' buffers."
:type '(choice (const :tag "none" nil)
(const :tag "all" t)
(set :menu-tag "mode specific" :tag "modes"
:value (not)
(const :tag "Except" not)
(repeat :inline t (symbol :tag "mode")))))
;;;###autoload
(define-globalized-minor-mode global-company-mode company-mode company-mode-on)
(defun company-mode-on ()
(when (and (not (or noninteractive (eq (aref (buffer-name) 0) ?\s)))
(cond ((eq company-global-modes t)
t)
((eq (car-safe company-global-modes) 'not)
(not (memq major-mode (cdr company-global-modes))))
(t (memq major-mode company-global-modes))))
(company-mode 1)))
(defsubst company-assert-enabled ()
(unless company-mode
(company-uninstall-map)
(error "Company not enabled")))
;;; keymaps ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar-local company-my-keymap nil)
(defvar company-emulation-alist '((t . nil)))
(defsubst company-enable-overriding-keymap (keymap)
(company-uninstall-map)
(setq company-my-keymap keymap))
(defun company-ensure-emulation-alist ()
(unless (eq 'company-emulation-alist (car emulation-mode-map-alists))
(setq emulation-mode-map-alists
(cons 'company-emulation-alist
(delq 'company-emulation-alist emulation-mode-map-alists)))))
(defun company-install-map ()
(unless (or (cdar company-emulation-alist)
(null company-my-keymap))
(setf (cdar company-emulation-alist) company-my-keymap)))
(defun company-uninstall-map ()
(setf (cdar company-emulation-alist) nil))
;; Hack:
;; Emacs calculates the active keymaps before reading the event. That means we
;; cannot change the keymap from a timer. So we send a bogus command.
;; XXX: Even in Emacs 24.4, seems to be needed in the terminal.
(defun company-ignore ()
(interactive)
(setq this-command last-command))
(global-set-key '[company-dummy-event] 'company-ignore)
(defun company-input-noop ()
(push 'company-dummy-event unread-command-events))
(defun company--posn-col-row (posn)
(let ((col (car (posn-col-row posn)))
;; `posn-col-row' doesn't work well with lines of different height.
;; `posn-actual-col-row' doesn't handle multiple-width characters.
(row (cdr (or (posn-actual-col-row posn)
;; When position is non-visible for some reason.
(posn-col-row posn)))))
(when (and header-line-format (version< emacs-version "24.3.93.3"))
;; http://debbugs.gnu.org/18384
(cl-decf row))
(cons (+ col (window-hscroll)) row)))
(defun company--col-row (&optional pos)
(company--posn-col-row (posn-at-point pos)))
(defun company--row (&optional pos)
(cdr (company--col-row pos)))
;;; backends ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar-local company-backend nil)
(defun company-grab (regexp &optional expression limit)
(when (looking-back regexp limit)
(or (match-string-no-properties (or expression 0)) "")))
(defun company-grab-line (regexp &optional expression)
"Return a match string for REGEXP if it matches text before point.
If EXPRESSION is non-nil, return the match string for the respective
parenthesized expression in REGEXP.
Matching is limited to the current line."
(let ((inhibit-field-text-motion t))
(company-grab regexp expression (point-at-bol))))
(defun company-grab-symbol ()
"If point is at the end of a symbol, return it.
Otherwise, if point is not inside a symbol, return an empty string."
(if (looking-at "\\_>")
(buffer-substring (point) (save-excursion (skip-syntax-backward "w_")
(point)))
(unless (and (char-after) (memq (char-syntax (char-after)) '(?w ?_)))
"")))
(defun company-grab-word ()
"If point is at the end of a word, return it.
Otherwise, if point is not inside a symbol, return an empty string."
(if (looking-at "\\>")
(buffer-substring (point) (save-excursion (skip-syntax-backward "w")
(point)))
(unless (and (char-after) (eq (char-syntax (char-after)) ?w))
"")))
(defun company-grab-symbol-cons (idle-begin-after-re &optional max-len)
"Return a string SYMBOL or a cons (SYMBOL . t).
SYMBOL is as returned by `company-grab-symbol'. If the text before point
matches IDLE-BEGIN-AFTER-RE, return it wrapped in a cons."
(let ((symbol (company-grab-symbol)))
(when symbol
(save-excursion
(forward-char (- (length symbol)))
(if (looking-back idle-begin-after-re (if max-len
(- (point) max-len)
(line-beginning-position)))
(cons symbol t)
symbol)))))
(defun company-in-string-or-comment ()
"Return non-nil if point is within a string or comment."
(let ((ppss (syntax-ppss)))
(or (car (setq ppss (nthcdr 3 ppss)))
(car (setq ppss (cdr ppss)))
(nth 3 ppss))))
(defun company-call-backend (&rest args)
(company--force-sync #'company-call-backend-raw args company-backend))
(defun company--force-sync (fun args backend)
(let ((value (apply fun args)))
(if (not (eq (car-safe value) :async))
value
(let ((res 'trash)
(start (time-to-seconds)))
(funcall (cdr value)
(lambda (result) (setq res result)))
(while (eq res 'trash)
(if (> (- (time-to-seconds) start) company-async-timeout)
(error "Company: backend %s async timeout with args %s"
backend args)
(sleep-for company-async-wait)))
res))))
(defun company-call-backend-raw (&rest args)
(condition-case-unless-debug err
(if (functionp company-backend)
(apply company-backend args)
(apply #'company--multi-backend-adapter company-backend args))
(user-error (user-error
"Company: backend %s user-error: %s"
company-backend (error-message-string err)))
(error (error "Company: backend %s error \"%s\" with args %s"
company-backend (error-message-string err) args))))
(defun company--multi-backend-adapter (backends command &rest args)
(let ((backends (cl-loop for b in backends
when (not (and (symbolp b)
(eq 'failed (get b 'company-init))))
collect b))
(separate (memq :separate backends)))
(when (eq command 'prefix)
(setq backends (butlast backends (length (member :with backends)))))
(setq backends (cl-delete-if #'keywordp backends))
(pcase command
(`candidates
(company--multi-backend-adapter-candidates backends (car args) separate))
(`sorted separate)
(`duplicates (not separate))
((or `prefix `ignore-case `no-cache `require-match)
(let (value)
(cl-dolist (backend backends)
(when (setq value (company--force-sync
backend (cons command args) backend))
(cl-return value)))))
(_
(let ((arg (car args)))
(when (> (length arg) 0)
(let ((backend (or (get-text-property 0 'company-backend arg)
(car backends))))
(apply backend command args))))))))
(defun company--multi-backend-adapter-candidates (backends prefix separate)
(let ((pairs (cl-loop for backend in backends
when (equal (company--prefix-str
(funcall backend 'prefix))
prefix)
collect (cons (funcall backend 'candidates prefix)
(company--multi-candidates-mapper
backend
separate
;; Small perf optimization: don't tag the
;; candidates received from the first
;; backend in the group.
(not (eq backend (car backends))))))))
(company--merge-async pairs (lambda (values) (apply #'append values)))))
(defun company--multi-candidates-mapper (backend separate tag)
(lambda (candidates)
(when separate
(let ((company-backend backend))
(setq candidates
(company--preprocess-candidates candidates))))
(when tag
(setq candidates
(mapcar
(lambda (str)
(propertize str 'company-backend backend))
candidates)))
candidates))
(defun company--merge-async (pairs merger)
(let ((async (cl-loop for pair in pairs
thereis
(eq :async (car-safe (car pair))))))
(if (not async)
(funcall merger (cl-loop for (val . mapper) in pairs
collect (funcall mapper val)))
(cons
:async
(lambda (callback)
(let* (lst
(pending (mapcar #'car pairs))
(finisher (lambda ()