forked from auto-complete/auto-complete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-complete.el
2058 lines (1775 loc) · 68.1 KB
/
auto-complete.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
;;; auto-complete.el --- Auto Completion for GNU Emacs
;; Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Tomohiro Matsuyama
;; Author: Tomohiro Matsuyama <[email protected]>
;; URL: http://cx4a.org/software/auto-complete
;; Keywords: completion, convenience
;; Version: 1.4.0
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This extension provides a way to complete with popup menu like:
;;
;; def-!-
;; +-----------------+
;; |defun::::::::::::|
;; |defvar |
;; |defmacro |
;; | ... |
;; +-----------------+
;;
;; You can complete by typing and selecting menu.
;;
;; Entire documents are located in doc/ directory.
;; Take a look for information.
;;
;; Enjoy!
;;; Code:
(defconst ac-version "1.4.0")
(eval-when-compile
(require 'cl))
(require 'popup)
;;;; Global stuff
(defun ac-error (&optional var)
"Report an error and disable `auto-complete-mode'."
(ignore-errors
(message "auto-complete error: %s" var)
(auto-complete-mode -1)
var))
;;;; Customization
(defgroup auto-complete nil
"Auto completion."
:group 'completion
:prefix "ac-")
(defcustom ac-delay 0.1
"Delay to completions will be available."
:type 'float
:group 'auto-complete)
(defcustom ac-auto-show-menu 0.8
"Non-nil means completion menu will be automatically shown."
:type '(choice (const :tag "Yes" t)
(const :tag "Never" nil)
(float :tag "Timer"))
:group 'auto-complete)
(defcustom ac-show-menu-immediately-on-auto-complete t
"Non-nil means menu will be showed immediately on `auto-complete'."
:type 'boolean
:group 'auto-complete)
(defcustom ac-expand-on-auto-complete t
"Non-nil means expand whole common part on first time `auto-complete'."
:type 'boolean
:group 'auto-complete)
(defcustom ac-disable-faces '(font-lock-comment-face font-lock-string-face font-lock-doc-face)
"Non-nil means disable automatic completion on specified faces."
:type '(repeat symbol)
:group 'auto-complete)
(defcustom ac-stop-flymake-on-completing t
"Non-nil means disble flymake temporarily on completing."
:type 'boolean
:group 'auto-complete)
(defcustom ac-use-fuzzy (and (locate-library "fuzzy") t)
"Non-nil means use fuzzy matching."
:type 'boolean
:group 'auto-complete)
(defcustom ac-fuzzy-cursor-color "red"
"Cursor color in fuzzy mode."
:type 'string
:group 'auto-complete)
(defcustom ac-use-comphist t
"Non-nil means use intelligent completion history."
:type 'boolean
:group 'auto-complete)
(defcustom ac-comphist-threshold 0.7
"Percentage of ignoring low scored candidates."
:type 'float
:group 'auto-complete)
(defcustom ac-comphist-file
(expand-file-name (concat (if (boundp 'user-emacs-directory)
user-emacs-directory
"~/.emacs.d/")
"/ac-comphist.dat"))
"Completion history file name."
:type 'string
:group 'auto-complete)
(defcustom ac-user-dictionary nil
"User defined dictionary"
:type '(repeat string)
:group 'auto-complete)
(defcustom ac-dictionary-files '("~/.dict")
"Dictionary files."
:type '(repeat string)
:group 'auto-complete)
(defvaralias 'ac-user-dictionary-files 'ac-dictionary-files)
(defcustom ac-dictionary-directories
(ignore-errors
(when load-file-name
(let ((installed-dir (file-name-directory load-file-name)))
(loop for name in '("ac-dict" "dict")
for dir = (concat installed-dir name)
if (file-directory-p dir)
collect dir))))
"Dictionary directories."
:type '(repeat string)
:group 'auto-complete)
(defcustom ac-use-quick-help t
"Non-nil means use quick help."
:type 'boolean
:group 'auto-complete)
(defcustom ac-quick-help-delay 1.5
"Delay to show quick help."
:type 'float
:group 'auto-complete)
(defcustom ac-menu-height 10
"Max height of candidate menu."
:type 'integer
:group 'auto-complete)
(defvaralias 'ac-candidate-menu-height 'ac-menu-height)
(defcustom ac-quick-help-height 20
"Max height of quick help."
:type 'integer
:group 'auto-complete)
(defcustom ac-quick-help-prefer-pos-tip t
"Prefer native tooltip with pos-tip than overlay popup for displaying quick help."
:type 'boolean
:group 'auto-complete)
(defvaralias 'ac-quick-help-prefer-x 'ac-quick-help-prefer-pos-tip)
(defcustom ac-candidate-limit nil
"Limit number of candidates. Non-integer means no limit."
:type 'integer
:group 'auto-complete)
(defvaralias 'ac-candidate-max 'ac-candidate-limit)
(defcustom ac-modes
'(emacs-lisp-mode lisp-mode lisp-interaction-mode
slime-repl-mode
c-mode cc-mode c++-mode go-mode
java-mode malabar-mode clojure-mode clojurescript-mode scala-mode
scheme-mode
ocaml-mode tuareg-mode coq-mode haskell-mode agda-mode agda2-mode
perl-mode cperl-mode python-mode ruby-mode lua-mode
ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode
makefile-mode sh-mode fortran-mode f90-mode ada-mode
xml-mode sgml-mode
ts-mode
sclang-mode
verilog-mode)
"Major modes `auto-complete-mode' can run on."
:type '(repeat symbol)
:group 'auto-complete)
(defcustom ac-compatible-packages-regexp
"^ac-"
"Regexp to indicate what packages can work with auto-complete."
:type 'string
:group 'auto-complete)
(defcustom ac-non-trigger-commands
'(*table--cell-self-insert-command
electric-buffer-list)
"Commands that can't be used as triggers of `auto-complete'."
:type '(repeat symbol)
:group 'auto-complete)
(defcustom ac-trigger-commands
'(self-insert-command)
"Trigger commands that specify whether `auto-complete' should start or not."
:type '(repeat symbol)
:group 'auto-complete)
(defcustom ac-trigger-commands-on-completing
'(delete-backward-char
backward-delete-char
backward-delete-char-untabify
;; autopair
autopair-backspace
;; paredit
paredit-backward-delete
paredit-backward-delete-word)
"Trigger commands that specify whether `auto-complete' should continue or not."
:type '(repeat symbol)
:group 'auto-complete)
(defcustom ac-trigger-key nil
"Non-nil means `auto-complete' will start by typing this key.
If you specify this TAB, for example, `auto-complete' will start by typing TAB,
and if there is no completions, an original command will be fallbacked."
:type '(choice (const :tag "None" nil)
(string :tag "Key"))
:group 'auto-complete
:set (lambda (symbol value)
(set-default symbol value)
(when (and value
(fboundp 'ac-set-trigger-key))
(ac-set-trigger-key value))))
(defcustom ac-auto-start 2
"Non-nil means completion will be started automatically.
Positive integer means if a length of a word you entered is larger than the value,
completion will be started automatically.
If you specify `nil', never be started automatically."
:type '(choice (const :tag "Yes" t)
(const :tag "Never" nil)
(integer :tag "Require"))
:group 'auto-complete)
(defcustom ac-stop-words nil
"List of string to stop completion."
:type '(repeat string)
:group 'auto-complete)
(defvaralias 'ac-ignores 'ac-stop-words)
(defcustom ac-use-dictionary-as-stop-words t
"Non-nil means a buffer related dictionary will be thought of as stop words."
:type 'boolean
:group 'auto-complete)
(defcustom ac-ignore-case 'smart
"Non-nil means auto-complete ignores case.
If this value is `smart', auto-complete ignores case only when
a prefix doen't contain any upper case letters."
:type '(choice (const :tag "Yes" t)
(const :tag "Smart" smart)
(const :tag "No" nil))
:group 'auto-complete)
(defcustom ac-dwim t
"Non-nil means `auto-complete' works based on Do What I Mean."
:type 'boolean
:group 'auto-complete)
(defcustom ac-use-menu-map nil
"Non-nil means a special keymap `ac-menu-map' on completing menu will be used."
:type 'boolean
:group 'auto-complete)
(defcustom ac-use-overriding-local-map nil
"Non-nil means `overriding-local-map' will be used to hack for overriding key events on auto-completion."
:type 'boolean
:group 'auto-complete)
(defcustom ac-disable-inline nil
"Non-nil disable inline completion visibility"
:type 'boolean
:group 'auto-complete)
(defcustom ac-candidate-menu-min 1
"Number of candidates required to display menu"
:type 'integer
:group 'auto-complete)
(defface ac-completion-face
'((t (:foreground "darkgray" :underline t)))
"Face for inline completion"
:group 'auto-complete)
(defface ac-candidate-face
'((t (:inherit popup-face)))
"Face for candidate."
:group 'auto-complete)
(defface ac-candidate-mouse-face
'((t (:inherit popup-mouse-face)))
"Mouse face for candidate."
:group 'auto-complete)
(defface ac-selection-face
'((t (:inherit popup-menu-selection-face)))
"Face for selected candidate."
:group 'auto-complete)
(defvar auto-complete-mode-hook nil
"Hook for `auto-complete-mode'.")
;;;; Internal variables
(defvar auto-complete-mode nil
"Dummy variable to suppress compiler warnings.")
(defvar ac-cursor-color nil
"Old cursor color.")
(defvar ac-inline nil
"Inline completion instance.")
(defvar ac-menu nil
"Menu instance.")
(defvar ac-show-menu nil
"Flag to show menu on timer tick.")
(defvar ac-last-completion nil
"Cons of prefix marker and selected item of last completion.")
(defvar ac-quick-help nil
"Quick help instance")
(defvar ac-completing nil
"Non-nil means `auto-complete-mode' is now working on completion.")
(defvar ac-buffer nil
"Buffer where auto-complete is started.")
(defvar ac-point nil
"Start point of prefix.")
(defvar ac-last-point nil
"Last point of updating pattern.")
(defvar ac-prefix nil
"Prefix string.")
(defvaralias 'ac-target 'ac-prefix)
(defvar ac-selected-candidate nil
"Last selected candidate.")
(defvar ac-common-part nil
"Common part string of meaningful candidates.
If there is no common part, this will be nil.")
(defvar ac-whole-common-part nil
"Common part string of whole candidates.
If there is no common part, this will be nil.")
(defvar ac-prefix-overlay nil
"Overlay for prefix string.")
(defvar ac-timer nil
"Completion idle timer.")
(defvar ac-show-menu-timer nil
"Show menu idle timer.")
(defvar ac-quick-help-timer nil
"Quick help idle timer.")
(defvar ac-triggered nil
"Flag to update.")
(defvar ac-limit nil
"Limit number of candidates for each sources.")
(defvar ac-candidates nil
"Current candidates.")
(defvar ac-candidates-cache nil
"Candidates cache for individual sources.")
(defvar ac-fuzzy-enable nil
"Non-nil means fuzzy matching is enabled.")
(defvar ac-dwim-enable nil
"Non-nil means DWIM completion will be allowed.")
(defvar ac-mode-map (make-sparse-keymap)
"Auto-complete mode map. It is also used for trigger key command. See also `ac-trigger-key'.")
(defvar ac-completing-map
(let ((map (make-sparse-keymap)))
(define-key map "\t" 'ac-expand)
(define-key map [tab] 'ac-expand)
(define-key map "\r" 'ac-complete)
(define-key map [return] 'ac-complete)
(define-key map (kbd "M-TAB") 'auto-complete)
(define-key map "\M-n" 'ac-next)
(define-key map "\M-p" 'ac-previous)
(define-key map [down] 'ac-next)
(define-key map [up] 'ac-previous)
(define-key map [f1] 'ac-help)
(define-key map [M-f1] 'ac-persist-help)
(define-key map (kbd "C-?") 'ac-help)
(define-key map (kbd "C-M-?") 'ac-persist-help)
(define-key map [C-down] 'ac-quick-help-scroll-down)
(define-key map [C-up] 'ac-quick-help-scroll-up)
(define-key map "\C-\M-n" 'ac-quick-help-scroll-down)
(define-key map "\C-\M-p" 'ac-quick-help-scroll-up)
(dotimes (i 9)
(let ((symbol (intern (format "ac-complete-select-%d" (1+ i)))))
(fset symbol
`(lambda ()
(interactive)
(when (and (ac-menu-live-p) (popup-select ac-menu ,i))
(ac-complete))))
(define-key map (read-kbd-macro (format "M-%s" (1+ i))) symbol)))
map)
"Keymap for completion.")
(defvaralias 'ac-complete-mode-map 'ac-completing-map)
(defvar ac-menu-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map ac-completing-map)
(define-key map "\C-n" 'ac-next)
(define-key map "\C-p" 'ac-previous)
(define-key map "\C-s" 'ac-isearch)
(define-key map [mouse-1] 'ac-mouse-1)
(define-key map [down-mouse-1] 'ac-ignore)
(define-key map [mouse-4] 'ac-mouse-4)
(define-key map [mouse-5] 'ac-mouse-5)
map)
"Keymap for completion on completing menu.")
(defvar ac-current-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map ac-completing-map)
map))
(defvar ac-match-function 'all-completions
"Default match function.")
(defvar ac-prefix-definitions
'((symbol . ac-prefix-symbol)
(file . ac-prefix-file)
(valid-file . ac-prefix-valid-file)
(c-dot . ac-prefix-c-dot)
(c-dot-ref . ac-prefix-c-dot-ref)
(cc-member . ac-prefix-cc-member))
"Prefix definitions for common use.")
(defvar ac-sources '(ac-source-words-in-same-mode-buffers)
"Sources for completion.")
(make-variable-buffer-local 'ac-sources)
(defvar ac-compiled-sources nil
"Compiled source of `ac-sources'.")
(defvar ac-current-sources nil
"Current working sources. This is sublist of `ac-compiled-sources'.")
(defvar ac-omni-completion-sources nil
"Do not use this anymore.")
(defvar ac-current-prefix-def nil)
(defvar ac-ignoring-prefix-def nil)
;;;; Intelligent completion history
(defvar ac-comphist nil
"Database of completion history.")
(defsubst ac-comphist-make-tab ()
(make-hash-table :test 'equal))
(defsubst ac-comphist-tab (db)
(nth 0 db))
(defsubst ac-comphist-cache (db)
(nth 1 db))
(defun ac-comphist-make (&optional tab)
(list (or tab (ac-comphist-make-tab)) (make-hash-table :test 'equal :weakness t)))
(defun ac-comphist-get (db string &optional create)
(let* ((tab (ac-comphist-tab db))
(index (gethash string tab)))
(when (and create (null index))
(setq index (make-vector (length string) 0))
(puthash string index tab))
index))
(defun ac-comphist-add (db string prefix)
(setq prefix (min prefix (1- (length string))))
(when (<= 0 prefix)
(setq string (substring-no-properties string))
(let ((stat (ac-comphist-get db string t)))
(incf (aref stat prefix))
(remhash string (ac-comphist-cache db)))))
(defun ac-comphist-score (db string prefix)
(setq prefix (min prefix (1- (length string))))
(if (<= 0 prefix)
(let ((cache (gethash string (ac-comphist-cache db))))
(or (and cache (aref cache prefix))
(let ((stat (ac-comphist-get db string))
(score 0.0))
(when stat
(loop for p from 0 below (length string)
;; sigmoid function
with a = 5
with b = (/ 700.0 a) ; bounds for avoiding range error in `exp'
with d = (/ 6.0 a)
for x = (max (- b) (min b (- d (abs (- prefix p)))))
for r = (/ 1.0 (1+ (exp (* (- a) x))))
do
(incf score (* (aref stat p) r))))
;; Weight by distance
(incf score (max 0.0 (- 0.3 (/ (- (length string) prefix) 100.0))))
(unless cache
(setq cache (make-vector (length string) nil))
(puthash string cache (ac-comphist-cache db)))
(aset cache prefix score)
score)))
0.0))
(defun ac-comphist-sort (db collection prefix &optional threshold)
(let (result
(n 0)
(total 0)
(cur 0))
(setq result (mapcar (lambda (a)
(when (and cur threshold)
(if (>= cur (* total threshold))
(setq cur nil)
(incf n)
(incf cur (cdr a))))
(car a))
(sort (mapcar (lambda (string)
(let ((score (ac-comphist-score db string prefix)))
(incf total score)
(cons string score)))
collection)
(lambda (a b) (< (cdr b) (cdr a))))))
(if threshold
(cons n result)
result)))
(defun ac-comphist-serialize (db)
(let (alist)
(maphash (lambda (k v)
(push (cons k v) alist))
(ac-comphist-tab db))
(list alist)))
(defun ac-comphist-deserialize (sexp)
(condition-case nil
(ac-comphist-make (let ((tab (ac-comphist-make-tab)))
(mapc (lambda (cons)
(puthash (car cons) (cdr cons) tab))
(nth 0 sexp))
tab))
(error (message "Invalid comphist db.") nil)))
(defun ac-comphist-init ()
(ac-comphist-load)
(add-hook 'kill-emacs-hook 'ac-comphist-save))
(defun ac-comphist-load ()
(interactive)
(let ((db (if (file-exists-p ac-comphist-file)
(ignore-errors
(with-temp-buffer
(insert-file-contents ac-comphist-file)
(goto-char (point-min))
(ac-comphist-deserialize (read (current-buffer))))))))
(setq ac-comphist (or db (ac-comphist-make)))))
(defun ac-comphist-save ()
(interactive)
(require 'pp)
(ignore-errors
(with-temp-buffer
(pp (ac-comphist-serialize ac-comphist) (current-buffer))
(write-region (point-min) (point-max) ac-comphist-file))))
;;;; Dictionary
(defvar ac-buffer-dictionary nil)
(defvar ac-file-dictionary (make-hash-table :test 'equal))
(defun ac-clear-dictionary-cache ()
(interactive)
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(if (local-variable-p 'ac-buffer-dictionary)
(kill-local-variable 'ac-buffer-dictionary))))
(clrhash ac-file-dictionary))
(defun ac-file-dictionary (filename)
(let ((cache (gethash filename ac-file-dictionary 'none)))
(if (and cache (not (eq cache 'none)))
cache
(let (result)
(ignore-errors
(with-temp-buffer
(insert-file-contents filename)
(setq result (split-string (buffer-string) "\n" t))))
(puthash filename result ac-file-dictionary)
result))))
(defun ac-mode-dictionary (mode)
(loop for name in (cons (symbol-name mode)
(ignore-errors (list (file-name-extension (buffer-file-name)))))
append (loop for dir in ac-dictionary-directories
for file = (concat dir "/" name)
if (file-exists-p file)
append (ac-file-dictionary file))))
(defun ac-buffer-dictionary (&optional buffer)
(with-current-buffer (or buffer (current-buffer))
(if (local-variable-p 'ac-buffer-dictionary)
ac-buffer-dictionary
(make-local-variable 'ac-buffer-dictionary)
(setq ac-buffer-dictionary
(apply 'append
ac-user-dictionary
(ac-mode-dictionary major-mode)
(mapcar 'ac-file-dictionary ac-dictionary-files))))))
;;;; Auto completion internals
(defun ac-menu-at-wrapper-line-p ()
"Return non-nil if current line is long and wrapped to next visual line."
(and (not truncate-lines)
(eq (line-beginning-position)
(save-excursion
(vertical-motion 1)
(line-beginning-position)))))
(defun ac-stop-word-p (word)
(or (member word ac-stop-words)
(if ac-use-dictionary-as-stop-words
(member word (ac-buffer-dictionary)))))
(defun ac-prefix-default ()
"Same as `ac-prefix-symbol' but ignore a number prefix."
(let ((start (ac-prefix-symbol)))
(when start
(loop with end = (point)
for pos from start below end
for c = (char-after pos)
if (not (and (<= ?0 c) (<= c ?9)))
return start))))
(defun ac-prefix-symbol ()
"Default prefix definition function."
(require 'thingatpt)
(car-safe (bounds-of-thing-at-point 'symbol)))
(defun ac-prefix-file ()
"File prefix."
(let ((point (re-search-backward "[\"<>' \t\r\n]" nil t)))
(if point (1+ point))))
(defun ac-prefix-valid-file ()
"Existed (or to be existed) file prefix."
(let* ((line-beg (line-beginning-position))
(end (point))
(start (or (let ((point (re-search-backward "[\"<>'= \t\r\n]" line-beg t)))
(if point (1+ point)))
line-beg))
(file (buffer-substring start end)))
(if (and file (or (string-match "^/" file)
(and (setq file (and (string-match "^[^/]*/" file)
(match-string 0 file)))
(file-directory-p file))))
start)))
(defun ac-prefix-c-dot ()
"C-like languages dot(.) prefix."
(if (re-search-backward "\\.\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
(match-beginning 1)))
(defun ac-prefix-c-dot-ref ()
"C-like languages dot(.) and reference(->) prefix."
(if (re-search-backward "\\(?:\\.\\|->\\)\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
(match-beginning 1)))
(defun ac-prefix-cc-member ()
"C-like languages member(.)(->)(::) prefix."
(when (re-search-backward "\\(?:\\.\\|->\\|::\\)\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
(match-beginning 1)))
(defun ac-define-prefix (name prefix)
"Define new prefix definition.
You can not use it in source definition like (prefix . `NAME')."
(push (cons name prefix) ac-prefix-definitions))
(defun ac-match-substring (prefix candidates)
(loop with regexp = (regexp-quote prefix)
for candidate in candidates
if (string-match regexp candidate)
collect candidate))
(defsubst ac-source-entity (source)
(if (symbolp source)
(symbol-value source)
source))
(defun ac-source-available-p (source)
(if (and (symbolp source)
(get source 'available))
(eq (get source 'available) t)
(let* ((src (ac-source-entity source))
(avail-pair (assq 'available src))
(avail-cond (cdr avail-pair))
(available (and (if avail-pair
(cond
((symbolp avail-cond)
(funcall avail-cond))
((listp avail-cond)
(eval avail-cond)))
t)
(loop for feature in (assoc-default 'depends src)
unless (require feature nil t) return nil
finally return t))))
(if (symbolp source)
(put source 'available (if available t 'no)))
available)))
(defun ac-compile-sources (sources)
"Compiled `SOURCES' into expanded sources style."
(loop for source in sources
if (ac-source-available-p source)
do
(setq source (ac-source-entity source))
;; prefix
(let* ((prefix (assoc 'prefix source))
(real (assoc-default (cdr prefix) ac-prefix-definitions)))
(cond
(real
(add-to-list 'source (cons 'prefix real)))
((null prefix)
(add-to-list 'source (cons 'prefix 'ac-prefix-default)))))
;; match
(let ((match (assq 'match source)))
(cond
((eq (cdr match) 'substring)
(setcdr match 'ac-match-substring))))
and collect source))
(defun ac-compiled-sources ()
(or ac-compiled-sources
(setq ac-compiled-sources
(ac-compile-sources ac-sources))))
(defsubst ac-menu-live-p ()
(popup-live-p ac-menu))
(defun ac-menu-create (point width height)
(setq ac-menu
(popup-create point width height
:around t
:face 'ac-candidate-face
:mouse-face 'ac-candidate-mouse-face
:selection-face 'ac-selection-face
:symbol t
:scroll-bar t
:margin-left 1
:keymap ac-menu-map
)))
(defun ac-menu-delete ()
(when ac-menu
(popup-delete ac-menu)
(setq ac-menu)))
(defsubst ac-inline-overlay ()
(nth 0 ac-inline))
(defsubst ac-inline-live-p ()
(and ac-inline (ac-inline-overlay) t))
(defun ac-inline-show (point string)
(unless ac-inline
(setq ac-inline (list nil)))
(save-excursion
(let ((overlay (ac-inline-overlay))
(width 0)
(string-width (string-width string))
(length 0)
(original-string string))
;; Calculate string space to show completion
(goto-char point)
(let (c)
(while (and (not (eolp))
(< width string-width)
(setq c (char-after))
(not (eq c ?\t))) ; special case for tab
(incf width (char-width c))
(incf length)
(forward-char)))
;; Show completion
(goto-char point)
(cond
((= width 0)
;; End-of-line
;; Do nothing
)
((<= width string-width)
;; No space to show
;; Do nothing
)
((> width string-width)
;; Need to fill space
(setq string (concat string (make-string (- width string-width) ? )))))
(setq string (propertize string 'face 'ac-completion-face))
(if overlay
(progn
(move-overlay overlay point (+ point length))
(overlay-put overlay 'invisible nil))
(setq overlay (make-overlay point (+ point length)))
(setf (nth 0 ac-inline) overlay)
(overlay-put overlay 'priority 9999)
;; Help prefix-overlay in some cases
(overlay-put overlay 'keymap ac-current-map))
;; TODO no width but char
(if (eq length 0)
;; Case: End-of-line
(progn
(put-text-property 0 1 'cursor t string)
(overlay-put overlay 'after-string string))
(let ((display (substring string 0 1))
(after-string (substring string 1)))
(overlay-put overlay 'display display)
(overlay-put overlay 'after-string after-string)))
(overlay-put overlay 'string original-string))))
(defun ac-inline-delete ()
(when (ac-inline-live-p)
(ac-inline-hide)
(delete-overlay (ac-inline-overlay))
(setq ac-inline nil)))
(defun ac-inline-hide ()
(when (ac-inline-live-p)
(let ((overlay (ac-inline-overlay))
(buffer-undo-list t))
(when overlay
(move-overlay overlay (point-min) (point-min))
(overlay-put overlay 'invisible t)
(overlay-put overlay 'display nil)
(overlay-put overlay 'after-string nil)))))
(defun ac-inline-update ()
(if (and ac-completing ac-prefix (stringp ac-common-part))
(let ((common-part-length (length ac-common-part))
(prefix-length (length ac-prefix)))
(if (> common-part-length prefix-length)
(progn
(ac-inline-hide)
(ac-inline-show (point) (substring ac-common-part prefix-length)))
(ac-inline-delete)))
(ac-inline-delete)))
(defun ac-put-prefix-overlay ()
(unless ac-prefix-overlay
(let (newline)
;; Insert newline to make sure that cursor always on the overlay
(when (eobp)
(popup-save-buffer-state
(insert "\n"))
(setq newline t))
(setq ac-prefix-overlay (make-overlay ac-point (1+ (point)) nil t t))
(overlay-put ac-prefix-overlay 'priority 9999)
(overlay-put ac-prefix-overlay 'keymap (make-sparse-keymap))
(overlay-put ac-prefix-overlay 'newline newline))))
(defun ac-remove-prefix-overlay ()
(when ac-prefix-overlay
(when (overlay-get ac-prefix-overlay 'newline)
;; Remove inserted newline
(popup-save-buffer-state
(goto-char (point-max))
(if (eq (char-before) ?\n)
(delete-char -1))))
(delete-overlay ac-prefix-overlay)))
(defun ac-activate-completing-map ()
(if (and ac-show-menu ac-use-menu-map)
(set-keymap-parent ac-current-map ac-menu-map))
(when (and ac-use-overriding-local-map
(null overriding-terminal-local-map))
(setq overriding-terminal-local-map ac-current-map))
(when ac-prefix-overlay
(set-keymap-parent (overlay-get ac-prefix-overlay 'keymap) ac-current-map)))
(defun ac-deactivate-completing-map ()
(set-keymap-parent ac-current-map ac-completing-map)
(when (and ac-use-overriding-local-map
(eq overriding-terminal-local-map ac-current-map))
(setq overriding-terminal-local-map nil))
(when ac-prefix-overlay
(set-keymap-parent (overlay-get ac-prefix-overlay 'keymap) nil)))
(defsubst ac-selected-candidate ()
(if ac-menu
(popup-selected-item ac-menu)))
(defun ac-prefix (requires ignore-list)
(loop with current = (point)
with point
with prefix-def
with sources
for source in (ac-compiled-sources)
for prefix = (assoc-default 'prefix source)
for req = (or (assoc-default 'requires source) requires 1)
if (null prefix-def)
do
(unless (member prefix ignore-list)
(save-excursion
(setq point (cond
((symbolp prefix)
(funcall prefix))
((stringp prefix)
(and (re-search-backward (concat prefix "\\=") nil t)
(or (match-beginning 1) (match-beginning 0))))
((stringp (car-safe prefix))
(let ((regexp (nth 0 prefix))
(end (nth 1 prefix))
(group (nth 2 prefix)))
(and (re-search-backward (concat regexp "\\=") nil t)
(funcall (if end 'match-end 'match-beginning)
(or group 0)))))
(t
(eval prefix))))
(if (and point
(integerp req)
(< (- current point) req))
(setq point nil))
(if point
(setq prefix-def prefix))))
if (equal prefix prefix-def) do (push source sources)
finally return
(and point (list prefix-def point (nreverse sources)))))
(defun ac-init ()
"Initialize current sources to start completion."
(setq ac-candidates-cache nil)
(loop for source in ac-current-sources
for function = (assoc-default 'init source)
if function do
(save-excursion
(cond
((functionp function)
(funcall function))
(t
(eval function))))))
(defun ac-candidates-1 (source)
(let* ((do-cache (assq 'cache source))