-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-complete.el
889 lines (772 loc) · 28 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
;;; auto-complete.el --- Auto completion with popup menu
;; Copyright (C) 2008 MATSUYAMA Tomohiro
;; Author: MATSUYAMA Tomohiro <[email protected]>
;; Keywords: convenience
;; Version: 0.1.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 select a completion with
;; popup menu.
;;
;; I checked that this extension can work properly on GNU Emacs 22 or higher.
;; To use this extension, locate this file to load-path directory,
;; and add the following code to your .emacs.
;; ------------------------------
;; (require 'auto-complete)
;; (global-auto-complete-mode t)
;; ------------------------------
;; After installation, you can try this:
;;
;; 1. Switch to emacs-lisp-mode buffer such as .emacs
;; 2. Goto anywhere
;; 3. Type "def"
;; 4. You may see a pop menu after the cursor like:
;; def-!-
;; +-----------------+
;; |defun | <- highlight
;; |defvar |
;; |defmacro |
;; | ... |
;; +-----------------+
;; 5. You can complete by seleting the menu item
;; by pressing TAB, <down>, <up>, and RET.
;;; Tips:
;;
;; ================================
;; Use C-n/C-p to select candidates
;; ================================
;;
;; Add following code to your .emacs.
;;
;; ------------------------------
;; (define-key ac-complete-mode-map "\C-n" 'ac-next)
;; (define-key ac-complete-mode-map "\C-p" 'ac-previous)
;; ------------------------------
;;
;;
;; ====================================
;; Don't start completion automatically
;; ====================================
;;
;; Add following code to your .emacs.
;;
;; ------------------------------
;; (setq ac-auto-start nil)
;; (global-set-key "\M-/" 'ac-start)
;; ------------------------------
;;
;; Or
;;
;; ------------------------------
;; ;; start completion when entered 3 characters
;; (setq ac-auto-start 3)
;; ------------------------------
;;
;;
;; =================
;; Completion by TAB
;; =================
;;
;; Add following code to your .emacs.
;;
;; ------------------------------
;; (define-key ac-complete-mode-map "\t" 'ac-complete)
;; (define-key ac-complete-mode-map "\r" nil)
;; ------------------------------
;;
;;
;; ===================
;; Do What I Mean mode
;; ===================
;;
;; If DWIM (Do What I Mean) mode is enabled,
;; the following features is available:
;;
;; a. TAB (ac-expand) behave as completion (ac-complete)
;; when only one candidate is left
;; b. TAB (ac-expand) behave as completion (ac-complete)
;; after you select candidate
;;
;; DWIM mode is enabled by default.
;; You can disable this feature by
;; setting `ac-dwim' to nil.
;;
;;
;; ==================================
;; Change sources for particular mode
;; ==================================
;;
;; `ac-sources' is global variable, so you have to
;; make it local variable to change sources for particular mode like:
;;
;; ------------------------------
;; (add-hook 'emacs-lisp-mode-hook
;; (lambda ()
;; (make-local-variable 'ac-sources)
;; (setq ac-sources '(ac-source-words-in-buffer ac-source-symbols))))
;; ------------------------------
;; This extension is so simple that you can extend
;; how Emacs find a target and how Emacs enumerate
;; candidates.
;; I don't have intention to implement heavy functions :-)
;;
;; Enjoy!
;;; History:
;; 2008-11-26 MATSUYAMA Tomohiro <[email protected]>
;;
;; * auto-complete.el 0.1.0 released
;;
;; 2008-11-19 MATSUYAMA Tomohiro <[email protected]>
;;
;; * thanks for Taiki SUGAWARA <[email protected]>
;; * added source ac-source-abbrev
;; * added source ac-source-symbols
;; * added ac-expand-common to expand common part
;;
;; 2008-11-18 MATSUYAMA Tomohiro <[email protected]>
;;
;; * added ac-auto-start switch
;; * added ac-dwim switch
;; * changed menu popup behavior at end of window
;; * thanks rubikitch <[email protected]>, kazu-yamamoto.
;; * fixed canceler bug
;; * changed to use overriding-local-map instead of minor mode map
;; * changed default key bindings
;;
;; 2008-11-16 MATSUYAMA Tomohiro <[email protected]>
;;
;; * supported candidates by using sources
;; * added automatically start swtich
;; * fixed some bug
;; * added source ac-source-files-in-current-dir
;; * added source ac-source-words-in-buffer
;; * added source ac-source-yasnippet
;; * renamed ac-enum-candidates-function to ac-candidate-function
;; * renamed ac-find-target-function to ac-find-function
;; * ac-find-function and ac-candidate-function is not buffer local variable now
;; * made candidates visible when you are end of line
;;
;; 2008-11-11 MATSUYAMA Tomohiro <[email protected]>
;;
;; * by reporting from rubikitch <[email protected]>
;; * renamed hook name
;; * registered backward-delete-char as special command
;; * fixed code for creating candidates
;; * made auto-complete disabled when isearch-mode enabled
;; * added some major-mode into ac-modes
;;
;; 2008-11-09 MATSUYAMA Tomohiro <[email protected]>
;;
;; * auto-complete.el 0.0.1 released
;; * fixed double-width character displaying problem
;; * fixed menu position following tab character
;; * made candidates visible when you are end of window
;;; TODO:
;;
;; - performance issue (cache issue)
;; - single source mode
;; - fix narrowing bug (reported by Yuto Hayamizu <[email protected]>)
;; - care about undo (buffer-disable-undo)
;; - support scroll in menu
;; - use double candidate menu
;; - omni completion
;; - show description
;; - demo movie (YouTube)
;; - backward menu in tiny buffer
;; - dictionary
;; - semantic
;;; Code:
(defgroup auto-complete nil
"Auto completion with popup menu"
:group 'convenience
:prefix "auto-complete-")
(defcustom ac-candidate-menu-width 25
"Max width of candidate menu."
:type 'number
:group 'auto-complete)
(defcustom ac-candidate-menu-height 10
"Max height of candidate menu."
:type 'number
:group 'auto-complete)
(defcustom ac-candidate-max 10
"Max of number of candidates."
:type 'number
:group 'auto-complete)
(defcustom ac-modes
'(emacs-lisp-mode lisp-interaction-mode
c-mode cc-mode c++-mode java-mode
perl-mode cperl-mode python-mode ruby-mode
ecmascript-mode javascript-mode php-mode css-mode
makefile-mode sh-mode fortran-mode f90-mode ada-mode
xml-mode sgml-mode)
"Major modes `auto-complete-mode' can run on."
:type '(list symbol)
:group 'auto-complete)
(defcustom ac-auto-start t
"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."
:group 'auto-complete)
(defcustom ac-dwim t
"Non-nil means `auto-complete' works based on Do What I Mean."
:type 'boolean
:group 'auto-complete)
(defface ac-selection-face
'((t (:background "blue" :foreground "white")))
"Face for the selected candidate."
:group 'auto-complete)
(defface ac-menu-face
'((t (:background "lightgray" :foreground "black")))
"Face for candidate menu."
:group 'auto-complete)
(defvar auto-complete-mode-hook nil
"Hook for `auto-complete-mode'.")
(defvar ac-menu nil
"Menu instance.")
(defvar ac-menu-direction 1
"Positive integer means `ac-menu' grows forward.
Or, `ac-menu' grows backward.")
(defvar ac-menu-offset 0
"Offset to contents.")
(defvar ac-completing nil
"Non-nil means `auto-complete-mode' is now working on completion.")
(defvar ac-saved-window-start nil
"Saved window start value for restore.")
(defvar ac-saved-window-hscroll nil
"Saved window hscroll value for restore.")
(defvar ac-point nil
"Start point of target.")
(defvar ac-target nil
"Target string.")
(defvar ac-limit 0
"Limit of number of candidates.")
(defvar ac-candidates nil
"Current candidates.")
(defvar ac-selection nil
"Current candidate index.")
(defvar ac-dwim-enable nil
"Non-nil means DWIM completion will be allowed.")
(defvar ac-find-function 'ac-default-find
"When `auto-complete-mode' finds it can start completion
or update candidates, it will call this function to find a
start point of the completion target.
If this function returns an integer, `auto-complete-mode'
will set the substring between the point and current point to `ac-target'.
And also it will start completion or update candidates by using
the `ac-target'.
If this function returns `nil', `auto-complete-mode'
ignore starting completion or stop completing.")
(defvar ac-init-function 'ac-sources-init
"This function will be called when candidate menu is setupped.")
(defvar ac-candidate-function 'ac-sources-candidate
"This function can return candidates as list by
using the `TARGET' that is given as a first argument.")
(defvar ac-complete-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\t" 'ac-expand)
(define-key map "\r" 'ac-complete)
(define-key map [down] 'ac-next)
(define-key map [up] 'ac-previous)
map)
"Keymap for completion.")
(or (assq 'ac-completing minor-mode-map-alist)
(push (cons 'ac-completing ac-complete-mode-map) minor-mode-map-alist))
(defvar ac-saved-local-map nil
"Old keymap before `auto-complete' activated.")
;;;; Auto completion
(defun ac-setup (point)
"Setup popup menu."
(save-excursion
(goto-char point)
(let ((column (current-column))
(line (line-number-at-pos)))
(setq ac-saved-window-start (window-start))
(setq ac-saved-window-hscroll (window-hscroll))
(setq ac-menu-direction
(if (and (> line ac-candidate-max)
(> ac-candidate-max
(-
(max 1 (- (window-height)
(if mode-line-format 1 0)
(if header-line-format 1 0)))
(1+ (count-lines (window-start) (point))))))
-1
1))
(let ((window-width (window-width))
(right (- (+ column ac-candidate-menu-width)
(window-hscroll))))
(if (and (> right window-width)
(>= right ac-candidate-menu-width))
(setq column (- column ac-candidate-menu-width))))
(if (> ac-menu-direction 0)
(progn
(forward-line)
(if (eq line (line-number-at-pos))
(newline)
(forward-line -1))
(setq ac-menu (ac-menu-create (1+ line) column ac-candidate-menu-width ac-candidate-menu-height))
(setq ac-point point))
(setq ac-menu (ac-menu-create (- line ac-candidate-max) column ac-candidate-menu-width ac-candidate-menu-height))
(setq ac-point point)))))
(defun ac-cleanup ()
"Destroy popup menu."
(ac-deactivate-mode-map)
(when ac-menu
(ac-menu-delete ac-menu)
(set-window-start (selected-window) ac-saved-window-start)
(set-window-hscroll (selected-window) ac-saved-window-hscroll))
(setq ac-menu nil)
(setq ac-completing nil)
(setq ac-point nil)
(setq ac-candidates nil)
(setq ac-selection 0))
(defun ac-activate-mode-map ()
"Activate `ac-complete-mode-map'."
(setq ac-saved-local-map overriding-terminal-local-map)
(if (eq ac-saved-local-map ac-complete-mode-map)
;; maybe never reach here
(setq ac-saved-local-map nil))
(setq overriding-terminal-local-map ac-complete-mode-map))
(defun ac-deactivate-mode-map ()
"Deactivate `ac-complete-mode-map'."
(when (eq overriding-terminal-local-map ac-complete-mode-map)
(setq overriding-terminal-local-map ac-saved-local-map)
(setq ac-saved-local-map nil)))
(defun ac-next ()
"Select next candidate."
(interactive)
(if (interactive-p)
(setq ac-dwim-enable t))
(if ac-candidates
(ac-select-candidate
(let ((selection (1+ ac-selection)))
(if (= selection (+ ac-menu-offset (min ac-candidate-menu-height (length ac-candidates))))
ac-menu-offset
selection)))))
(defun ac-previous ()
"Select previous candidate."
(interactive)
(if (interactive-p)
(setq ac-dwim-enable t))
(if ac-candidates
(ac-select-candidate
(let ((selection (1- ac-selection)))
(if (< selection ac-menu-offset)
(1- (+ ac-menu-offset (min ac-candidate-menu-height (length ac-candidates))))
selection)))))
(defun ac-expand-1 ()
"Try expansion."
(let ((string (overlay-get (ac-menu-line-overlay ac-menu ac-selection) 'real-string)))
(delete-region ac-point (point))
(insert string)
(setq ac-target string)))
(defun ac-expand ()
"Try expansion but select next if expanded twice."
(interactive)
(if (and ac-dwim ac-dwim-enable)
(ac-complete)
(let ((target ac-target)
(string (ac-expand-1)))
(when (equal target string)
(ac-next)
(ac-expand-1)))))
(defun ac-expand-common ()
"Try expansion common part."
(interactive)
(let ((common (try-completion ac-target ac-candidates)))
(when (stringp common)
(delete-region ac-point (point))
(insert common)
(setq ac-target common))))
(defun ac-complete ()
"Try completion."
(interactive)
(let* ((string (overlay-get (ac-menu-line-overlay ac-menu ac-selection) 'real-string))
(source (get-text-property 0 'source string))
(complete-function (and source (cdr-safe (assq 'action source)))))
(ac-expand-1)
(if complete-function
(funcall complete-function))
(ac-abort)))
(defun ac-abort ()
"Abort completion."
(ac-cleanup))
(defun ac-update-candidates (candidates)
"Update candidates of popup menu."
(setq ac-menu-offset (if (> ac-menu-direction 0)
0
(- ac-candidate-menu-height
(min ac-candidate-menu-height
(length candidates)))))
(setq ac-selection ac-menu-offset)
(setq ac-candidates candidates)
(setq ac-dwim-enable (= (length candidates) 1))
(if candidates
(progn
(setq ac-completing t)
(ac-activate-mode-map))
(setq ac-completing nil)
(ac-deactivate-mode-map))
(let ((i ac-menu-offset))
;; show line and set string to the line
(mapcar
(lambda (candidate)
(when (< i ac-candidate-menu-height)
(ac-menu-show-line ac-menu i)
(ac-menu-set-line-string ac-menu i candidate (if (= i ac-selection) 'ac-selection-face))
(setq i (1+ i))))
candidates)
;; ensure lines visible
(if (and (> ac-menu-direction 0)
(> i (-
(max 1 (- (window-height)
(if mode-line-format 1 0)
(if header-line-format 1 0)))
(1+ (count-lines (window-start) (point))))))
(recenter (- (1+ i))))
(if (> i ac-menu-offset)
(let ((window-width (window-width))
(right (- (+ (ac-menu-column ac-menu) ac-candidate-menu-width)
(window-hscroll))))
(if (> right window-width)
(scroll-left (- right window-width)))))
;; hide remaining lines
(if (> ac-menu-direction 0)
(while (< i ac-candidate-menu-height)
(ac-menu-hide-line ac-menu i)
(setq i (1+ i)))
(dotimes (i ac-menu-offset)
(ac-menu-hide-line ac-menu i)))))
(defun ac-select-candidate (selection)
"Select candidate pointed by `SELECTION'."
(when ac-candidates
(ac-menu-set-line-string ac-menu ac-selection (nth (- ac-selection ac-menu-offset) ac-candidates))
(ac-menu-set-line-string ac-menu selection (nth (- selection ac-menu-offset) ac-candidates) 'ac-selection-face)
(setq ac-selection selection)))
(defun ac-default-find ()
"Default implemention for `ac-find-function'."
(require 'thingatpt)
(car-safe (bounds-of-thing-at-point 'symbol)))
(defun ac-start ()
"Start completion."
(interactive)
(let ((point (funcall ac-find-function)))
(if (or (null point)
(and ac-menu
(/= point ac-point)))
(ac-abort)
(when (null ac-menu)
(ac-setup point)
(funcall ac-init-function))
(setq ac-target (buffer-substring-no-properties point (point)))
(setq ac-limit ac-candidate-max)
(ac-update-candidates
(if (or ac-completing
(not (integerp ac-auto-start))
(>= (length ac-target) ac-auto-start))
(funcall ac-candidate-function))))))
(defun ac-trigger-command-p ()
"Return non-nil if `this-command' is a trigger command."
(or (eq this-command 'self-insert-command)
(and ac-completing
(memq this-command
'(delete-backward-char
backward-delete-char
backward-delete-char-untabify)))))
(defun ac-on-pre-command ()
(if (and (not (ac-trigger-command-p))
(or (not (symbolp this-command))
(not (string-match "^ac-" (symbol-name this-command)))))
(ac-abort)))
(defun ac-on-post-command ()
(if (and ac-auto-start
(not isearch-mode)
(ac-trigger-command-p))
(ac-start)))
(defun auto-complete-mode-maybe ()
"What buffer `auto-complete-mode' prefers."
(if (and (not (minibufferp (current-buffer)))
(memq major-mode ac-modes))
(auto-complete-mode 1)))
(require 'easy-mmode)
(define-minor-mode auto-complete-mode
"AutoComplete mode"
:lighter " AC"
:group 'auto-complete
(if auto-complete-mode
(progn
(add-hook 'post-command-hook 'ac-on-post-command nil t)
(add-hook 'pre-command-hook 'ac-on-pre-command nil t)
(run-hooks 'auto-complete-mode-hook))
(remove-hook 'post-command-hook 'ac-on-post-command t)
(remove-hook 'pre-command-hook 'ac-on-pre-command t)
(ac-abort)))
(if (fboundp 'define-global-minor-mode)
(define-global-minor-mode global-auto-complete-mode
auto-complete-mode auto-complete-mode-maybe
:group 'auto-complete))
;;;; Sources implementation
(defvar ac-sources '(ac-source-words-in-buffer)
"Sources for completion.
Source takes a form of alist:
(init . INIT-FUNC)
INIT-FUNC will be called before creating candidate every time.
(candidates . CANDIDATE-FUNC)
CANDIDATE-FUNC will return a list of string as candidates.
CANDIDATE-FUNC should care about `ac-limit' that is specified at limit for performance.
(action . ACTION-FUNC)
ACTION-FUNC will be called when `ac-complete' is called.
(limit . LIMIT-NUM)
A limit of candidates.
(requires . REQUIRES-NUM)
This source will be included when `ac-target' length is larger than REQUIRES-NUM.")
(defun ac-sources-init ()
"Implementation for `ac-init-function' by sources."
(dolist (source ac-sources)
(if (symbolp source)
(setq source (symbol-value source)))
(let ((init-function (cdr-safe (assq 'init source))))
(if init-function
(funcall init-function)))))
(defun ac-sources-candidate ()
"Implementation for `ac-cadidates-function' by sources."
(when (> (length ac-target) 0)
(let (candidates)
(dolist (source ac-sources)
(if (symbolp source)
(setq source (symbol-value source)))
(let* ((ac-limit (or (cdr-safe (assq 'limit source)) ac-limit))
(requires (cdr-safe (assq 'requires source)))
cand)
(if (or (null requires)
(>= (length ac-target) requires))
(setq cand
(delq nil
(mapcar (lambda (candidate)
(propertize candidate 'source source))
(funcall (cdr (assq 'candidates source)))))))
(if (and (> ac-limit 1)
(> (length cand) ac-limit))
(setcdr (nthcdr (1- ac-limit) cand) nil))
(setq candidates (append candidates cand))))
(delete-dups candidates))))
(defun ac-candidate-words-in-buffer ()
"Default implemention for `ac-candidate-function'."
(if (> (length ac-target) 0)
(let ((i 0)
candidate
candidates
(regexp (concat "\\b" (regexp-quote ac-target) "\\(\\s_\\|\\sw\\)*\\b")))
(save-excursion
;; search backward
(goto-char ac-point)
(while (and (< i ac-limit)
(re-search-backward regexp nil t))
(setq candidate (match-string-no-properties 0))
(unless (member candidate candidates)
(push candidate candidates)
(setq i (1+ i))))
;; search backward
(goto-char (+ ac-point (length ac-target)))
(while (and (< i ac-limit)
(re-search-forward regexp nil t))
(setq candidate (match-string-no-properties 0))
(unless (member candidate candidates)
(push candidate candidates)
(setq i (1+ i))))
(nreverse candidates)))))
(defvar ac-source-words-in-buffer
'((candidates . ac-candidate-words-in-buffer))
"Simple source like dabbrev.")
(defvar ac-source-symbols
'((candidates
. (lambda ()
(all-completions ac-target obarray))))
"Source for Emacs lisp symbols.")
(defvar ac-source-abbrev
`((candidates
. (lambda ()
(all-completions ac-target local-abbrev-table)))
(action
. expand-abbrev))
"Source for abbrev.")
(defvar ac-source-files-in-current-dir
'((candidates
. (lambda () (all-completions ac-target (directory-files default-directory)))))
"Source for listing files in current directory.")
(defvar ac-imenu-index nil
"Imenu index.")
(defun ac-imenu-candidate ()
(require 'imenu)
(let ((i 0)
(stack ac-imenu-index)
candidates
node)
(while (and stack
(< i ac-limit))
(setq node (pop stack))
(when (consp node)
(let ((car (car node))
(cdr (cdr node)))
(if (consp cdr)
(mapcar (lambda (child)
(push child stack))
cdr)
(when (and (stringp car)
(string-match (concat "^" (regexp-quote ac-target)) car))
(push car candidates)
(setq i (1+ i)))))))
(nreverse candidates)))
(defvar ac-source-imenu
'((init
. (lambda ()
(require 'imenu)
(setq ac-imenu-index
(condition-case nil
(imenu--make-index-alist)
(error nil)))))
(candidates . ac-imenu-candidate))
"Source for imenu.")
(defun ac-yasnippet-candidate-1 (table)
(let ((hashtab (yas/snippet-table-hash table))
(parent (yas/snippet-table-parent table))
candidates)
(maphash (lambda (key value)
(push key candidates))
hashtab)
(setq candidates (all-completions ac-target (nreverse candidates)))
(if parent
(setq candidates
(append candidates (ac-yasnippet-candidate-1 parent))))
candidates))
(defun ac-yasnippet-candidate ()
(require 'yasnippet)
(let ((table (yas/snippet-table major-mode)))
(if table
(ac-yasnippet-candidate-1 table))))
(defvar ac-source-yasnippet
'((candidates . ac-yasnippet-candidate)
(action . yas/expand)
(limit . 3))
"Source for Yasnippet.")
;;;; Popup menu
(defun ac-menu-line (menu)
"Line number of `MENU'."
(nth 0 menu))
(defun ac-menu-column (menu)
"Column of `MENU'."
(nth 1 menu))
(defun ac-menu-width (menu)
"Popup menu width of `MENU'."
(nth 2 menu))
(defun ac-menu-height (menu)
"Popup menu height of `MENU'."
(nth 3 menu))
(defun ac-menu-overlays (menu)
"Overlays that `MENU' contains."
(nth 4 menu))
(defun ac-menu-line-overlay (menu line)
"Return a overlay of `MENU' at `LINE'."
(aref (ac-menu-overlays menu) line))
(defun ac-menu-hide-line (menu line)
"Hide `LINE' in `MENU'."
(let ((overlay (ac-menu-line-overlay menu line)))
(overlay-put overlay 'invisible nil)
(overlay-put overlay 'after-string nil)))
(defun ac-menu-show-line (menu line)
"Show `LINE' in `MENU'."
(let ((overlay (ac-menu-line-overlay menu line)))
(overlay-put overlay 'invisible t)))
(defun ac-menu-set-line-string (menu line string &optional face)
"Set contents of `LINE' in `MENU'."
(let ((overlay (ac-menu-line-overlay menu line)))
(overlay-put overlay 'real-string string)
(funcall (overlay-get overlay 'set-string-function) overlay string face)))
(defun ac-menu-create-line-string (menu string)
"Adjust `STRING' into `MENU'."
(let ((length 0)
(width 0)
(menu-width (ac-menu-width menu))
(chars (append string nil)))
(while (and
chars
(<= (setq width (+ width (char-width (car chars)))) menu-width))
(setq length (1+ length))
(setq chars (cdr chars)))
(if (< length (length string))
(setq string (substring string 0 length)))
(let ((string-width (string-width string)))
(if (< string-width menu-width)
(setq string (concat string
(make-string (- menu-width string-width) ? )))))
string))
(defun ac-menu-hide (menu)
"Hide `MENU'."
(dotimes (i (ac-menu-height menu))
(ac-menu-hide-line menu i)))
(defun ac-menu-last-line-of-buffer ()
(save-excursion
(not (eq (forward-line) 0))))
(defun ac-menu-create (line column width height)
"Create popup menu."
(save-excursion
(let ((overlays (make-vector height nil))
(window (selected-window)))
(goto-line line)
(dotimes (i height)
(move-to-column column)
(let (overlay begin w current-column (prefix "") (postfix ""))
(setq current-column (current-column))
(cond
((> current-column column)
(backward-char)
(setq current-column (current-column))
(if (< current-column column)
(setq prefix (make-string (- column current-column) ? ))))
((< current-column column)
(setq prefix (make-string (- column current-column) ? ))))
(setq begin (point))
(setq w (+ width (length prefix)))
(while (and (not (eolp))
(> w 0))
(setq w (- w (char-width (char-after))))
(forward-char))
(if (< w 0)
(setq postfix (make-string (- w) ? )))
(if (ac-menu-last-line-of-buffer)
(setq postfix (concat postfix "\n")))
(setq overlay (make-overlay begin (point)))
(overlay-put overlay 'window window)
(overlay-put overlay 'prefix prefix)
(overlay-put overlay 'postfix postfix)
(overlay-put overlay 'width width)
(overlay-put overlay 'set-string-function
(lambda (overlay string &optional face)
(overlay-put overlay
'after-string
(concat (overlay-get overlay 'prefix)
(propertize (ac-menu-create-line-string menu string) 'face (or face 'ac-menu-face))
(overlay-get overlay 'postfix)))))
(aset overlays i overlay))
(forward-line))
(let ((i 100))
(mapcar (lambda (overlay)
(overlay-put overlay 'priority i)
(setq i (1+ i)))
(nreverse (append overlays nil))))
(list line column width height overlays))))
(defun ac-menu-delete (menu)
"Delete `MENU'."
(mapcar 'delete-overlay (ac-menu-overlays menu)))
(provide 'auto-complete)
;;; auto-complete.el ends here