lotsa changes and inclusion of elpy
[emacs.git] / .emacs.d / elisp / auto-complete / auto-complete.el
1 ;;; auto-complete.el --- Auto Completion for GNU Emacs
2
3 ;; Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Tomohiro Matsuyama
4
5 ;; Author: Tomohiro Matsuyama <m2ym.pub@gmail.com>
6 ;; URL: http://cx4a.org/software/auto-complete
7 ;; Keywords: completion, convenience
8 ;; Version: 1.4.0
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24 ;;
25 ;; This extension provides a way to complete with popup menu like:
26 ;;
27 ;; def-!-
28 ;; +-----------------+
29 ;; |defun::::::::::::|
30 ;; |defvar |
31 ;; |defmacro |
32 ;; | ... |
33 ;; +-----------------+
34 ;;
35 ;; You can complete by typing and selecting menu.
36 ;;
37 ;; Entire documents are located in doc/ directory.
38 ;; Take a look for information.
39 ;;
40 ;; Enjoy!
41
42 ;;; Code:
43
44 \f
45
46 (defconst ac-version "1.4.0")
47
48 (eval-when-compile
49 (require 'cl))
50
51 (require 'popup)
52
53 ;;;; Global stuff
54
55 (defun ac-error (&optional var)
56 "Report an error and disable `auto-complete-mode'."
57 (ignore-errors
58 (message "auto-complete error: %s" var)
59 (auto-complete-mode -1)
60 var))
61
62 \f
63
64 ;;;; Customization
65
66 (defgroup auto-complete nil
67 "Auto completion."
68 :group 'completion
69 :prefix "ac-")
70
71 (defcustom ac-delay 0.1
72 "Delay to completions will be available."
73 :type 'float
74 :group 'auto-complete)
75
76 (defcustom ac-auto-show-menu 0.8
77 "Non-nil means completion menu will be automatically shown."
78 :type '(choice (const :tag "Yes" t)
79 (const :tag "Never" nil)
80 (float :tag "Timer"))
81 :group 'auto-complete)
82
83 (defcustom ac-show-menu-immediately-on-auto-complete t
84 "Non-nil means menu will be showed immediately on `auto-complete'."
85 :type 'boolean
86 :group 'auto-complete)
87
88 (defcustom ac-expand-on-auto-complete t
89 "Non-nil means expand whole common part on first time `auto-complete'."
90 :type 'boolean
91 :group 'auto-complete)
92
93 (defcustom ac-disable-faces '(font-lock-comment-face font-lock-string-face font-lock-doc-face)
94 "Non-nil means disable automatic completion on specified faces."
95 :type '(repeat symbol)
96 :group 'auto-complete)
97
98 (defcustom ac-stop-flymake-on-completing t
99 "Non-nil means disble flymake temporarily on completing."
100 :type 'boolean
101 :group 'auto-complete)
102
103 (defcustom ac-use-fuzzy (and (locate-library "fuzzy") t)
104 "Non-nil means use fuzzy matching."
105 :type 'boolean
106 :group 'auto-complete)
107
108 (defcustom ac-fuzzy-cursor-color "red"
109 "Cursor color in fuzzy mode."
110 :type 'string
111 :group 'auto-complete)
112
113 (defcustom ac-use-comphist t
114 "Non-nil means use intelligent completion history."
115 :type 'boolean
116 :group 'auto-complete)
117
118 (defcustom ac-comphist-threshold 0.7
119 "Percentage of ignoring low scored candidates."
120 :type 'float
121 :group 'auto-complete)
122
123 (defcustom ac-comphist-file
124 (expand-file-name (concat (if (boundp 'user-emacs-directory)
125 user-emacs-directory
126 "~/.emacs.d/")
127 "/ac-comphist.dat"))
128 "Completion history file name."
129 :type 'string
130 :group 'auto-complete)
131
132 (defcustom ac-user-dictionary nil
133 "User defined dictionary"
134 :type '(repeat string)
135 :group 'auto-complete)
136
137 (defcustom ac-dictionary-files '("~/.dict")
138 "Dictionary files."
139 :type '(repeat string)
140 :group 'auto-complete)
141 (defvaralias 'ac-user-dictionary-files 'ac-dictionary-files)
142
143 (defcustom ac-dictionary-directories
144 (ignore-errors
145 (when load-file-name
146 (let ((installed-dir (file-name-directory load-file-name)))
147 (loop for name in '("ac-dict" "dict")
148 for dir = (concat installed-dir name)
149 if (file-directory-p dir)
150 collect dir))))
151 "Dictionary directories."
152 :type '(repeat string)
153 :group 'auto-complete)
154
155 (defcustom ac-use-quick-help t
156 "Non-nil means use quick help."
157 :type 'boolean
158 :group 'auto-complete)
159
160 (defcustom ac-quick-help-delay 1.5
161 "Delay to show quick help."
162 :type 'float
163 :group 'auto-complete)
164
165 (defcustom ac-menu-height 10
166 "Max height of candidate menu."
167 :type 'integer
168 :group 'auto-complete)
169 (defvaralias 'ac-candidate-menu-height 'ac-menu-height)
170
171 (defcustom ac-quick-help-height 20
172 "Max height of quick help."
173 :type 'integer
174 :group 'auto-complete)
175
176 (defcustom ac-quick-help-prefer-pos-tip t
177 "Prefer native tooltip with pos-tip than overlay popup for displaying quick help."
178 :type 'boolean
179 :group 'auto-complete)
180 (defvaralias 'ac-quick-help-prefer-x 'ac-quick-help-prefer-pos-tip)
181
182 (defcustom ac-candidate-limit nil
183 "Limit number of candidates. Non-integer means no limit."
184 :type 'integer
185 :group 'auto-complete)
186 (defvaralias 'ac-candidate-max 'ac-candidate-limit)
187
188 (defcustom ac-modes
189 '(emacs-lisp-mode lisp-mode lisp-interaction-mode
190 slime-repl-mode
191 c-mode cc-mode c++-mode go-mode
192 java-mode malabar-mode clojure-mode clojurescript-mode scala-mode
193 scheme-mode
194 ocaml-mode tuareg-mode coq-mode haskell-mode agda-mode agda2-mode
195 perl-mode cperl-mode python-mode ruby-mode lua-mode
196 ecmascript-mode javascript-mode js-mode js2-mode php-mode css-mode
197 makefile-mode sh-mode fortran-mode f90-mode ada-mode
198 xml-mode sgml-mode
199 ts-mode
200 sclang-mode
201 verilog-mode)
202 "Major modes `auto-complete-mode' can run on."
203 :type '(repeat symbol)
204 :group 'auto-complete)
205
206 (defcustom ac-compatible-packages-regexp
207 "^ac-"
208 "Regexp to indicate what packages can work with auto-complete."
209 :type 'string
210 :group 'auto-complete)
211
212 (defcustom ac-non-trigger-commands
213 '(*table--cell-self-insert-command
214 electric-buffer-list)
215 "Commands that can't be used as triggers of `auto-complete'."
216 :type '(repeat symbol)
217 :group 'auto-complete)
218
219 (defcustom ac-trigger-commands
220 '(self-insert-command)
221 "Trigger commands that specify whether `auto-complete' should start or not."
222 :type '(repeat symbol)
223 :group 'auto-complete)
224
225 (defcustom ac-trigger-commands-on-completing
226 '(delete-backward-char
227 backward-delete-char
228 backward-delete-char-untabify
229 ;; autopair
230 autopair-backspace
231 ;; paredit
232 paredit-backward-delete
233 paredit-backward-delete-word)
234 "Trigger commands that specify whether `auto-complete' should continue or not."
235 :type '(repeat symbol)
236 :group 'auto-complete)
237
238 (defcustom ac-trigger-key nil
239 "Non-nil means `auto-complete' will start by typing this key.
240 If you specify this TAB, for example, `auto-complete' will start by typing TAB,
241 and if there is no completions, an original command will be fallbacked."
242 :type '(choice (const :tag "None" nil)
243 (string :tag "Key"))
244 :group 'auto-complete
245 :set (lambda (symbol value)
246 (set-default symbol value)
247 (when (and value
248 (fboundp 'ac-set-trigger-key))
249 (ac-set-trigger-key value))))
250
251 (defcustom ac-auto-start 2
252 "Non-nil means completion will be started automatically.
253 Positive integer means if a length of a word you entered is larger than the value,
254 completion will be started automatically.
255 If you specify `nil', never be started automatically."
256 :type '(choice (const :tag "Yes" t)
257 (const :tag "Never" nil)
258 (integer :tag "Require"))
259 :group 'auto-complete)
260
261 (defcustom ac-stop-words nil
262 "List of string to stop completion."
263 :type '(repeat string)
264 :group 'auto-complete)
265 (defvaralias 'ac-ignores 'ac-stop-words)
266
267 (defcustom ac-use-dictionary-as-stop-words t
268 "Non-nil means a buffer related dictionary will be thought of as stop words."
269 :type 'boolean
270 :group 'auto-complete)
271
272 (defcustom ac-ignore-case 'smart
273 "Non-nil means auto-complete ignores case.
274 If this value is `smart', auto-complete ignores case only when
275 a prefix doen't contain any upper case letters."
276 :type '(choice (const :tag "Yes" t)
277 (const :tag "Smart" smart)
278 (const :tag "No" nil))
279 :group 'auto-complete)
280
281 (defcustom ac-dwim t
282 "Non-nil means `auto-complete' works based on Do What I Mean."
283 :type 'boolean
284 :group 'auto-complete)
285
286 (defcustom ac-use-menu-map nil
287 "Non-nil means a special keymap `ac-menu-map' on completing menu will be used."
288 :type 'boolean
289 :group 'auto-complete)
290
291 (defcustom ac-use-overriding-local-map nil
292 "Non-nil means `overriding-local-map' will be used to hack for overriding key events on auto-completion."
293 :type 'boolean
294 :group 'auto-complete)
295
296 (defcustom ac-disable-inline nil
297 "Non-nil disable inline completion visibility"
298 :type 'boolean
299 :group 'auto-complete)
300
301 (defcustom ac-candidate-menu-min 1
302 "Number of candidates required to display menu"
303 :type 'integer
304 :group 'auto-complete)
305
306 (defcustom ac-max-width nil
307 "Maximum width for auto-complete menu to have"
308 :type '(choice (const :tag "No limit" nil)
309 (const :tag "Character Limit" 25)
310 (const :tag "Window Ratio Limit" 0.5))
311 :group 'auto-complete)
312
313 (defface ac-completion-face
314 '((t (:foreground "darkgray" :underline t)))
315 "Face for inline completion"
316 :group 'auto-complete)
317
318 (defface ac-candidate-face
319 '((t (:inherit popup-face)))
320 "Face for candidate."
321 :group 'auto-complete)
322
323 (defface ac-candidate-mouse-face
324 '((t (:inherit popup-mouse-face)))
325 "Mouse face for candidate."
326 :group 'auto-complete)
327
328 (defface ac-selection-face
329 '((t (:inherit popup-menu-selection-face)))
330 "Face for selected candidate."
331 :group 'auto-complete)
332
333 (defvar auto-complete-mode-hook nil
334 "Hook for `auto-complete-mode'.")
335
336 \f
337
338 ;;;; Internal variables
339
340 (defvar auto-complete-mode nil
341 "Dummy variable to suppress compiler warnings.")
342
343 (defvar ac-cursor-color nil
344 "Old cursor color.")
345
346 (defvar ac-inline nil
347 "Inline completion instance.")
348
349 (defvar ac-menu nil
350 "Menu instance.")
351
352 (defvar ac-show-menu nil
353 "Flag to show menu on timer tick.")
354
355 (defvar ac-last-completion nil
356 "Cons of prefix marker and selected item of last completion.")
357
358 (defvar ac-quick-help nil
359 "Quick help instance")
360
361 (defvar ac-completing nil
362 "Non-nil means `auto-complete-mode' is now working on completion.")
363
364 (defvar ac-buffer nil
365 "Buffer where auto-complete is started.")
366
367 (defvar ac-point nil
368 "Start point of prefix.")
369
370 (defvar ac-last-point nil
371 "Last point of updating pattern.")
372
373 (defvar ac-prefix nil
374 "Prefix string.")
375 (defvaralias 'ac-target 'ac-prefix)
376
377 (defvar ac-selected-candidate nil
378 "Last selected candidate.")
379
380 (defvar ac-common-part nil
381 "Common part string of meaningful candidates.
382 If there is no common part, this will be nil.")
383
384 (defvar ac-whole-common-part nil
385 "Common part string of whole candidates.
386 If there is no common part, this will be nil.")
387
388 (defvar ac-prefix-overlay nil
389 "Overlay for prefix string.")
390
391 (defvar ac-timer nil
392 "Completion idle timer.")
393
394 (defvar ac-show-menu-timer nil
395 "Show menu idle timer.")
396
397 (defvar ac-quick-help-timer nil
398 "Quick help idle timer.")
399
400 (defvar ac-triggered nil
401 "Flag to update.")
402
403 (defvar ac-limit nil
404 "Limit number of candidates for each sources.")
405
406 (defvar ac-candidates nil
407 "Current candidates.")
408
409 (defvar ac-candidates-cache nil
410 "Candidates cache for individual sources.")
411
412 (defvar ac-fuzzy-enable nil
413 "Non-nil means fuzzy matching is enabled.")
414
415 (defvar ac-dwim-enable nil
416 "Non-nil means DWIM completion will be allowed.")
417
418 (defvar ac-mode-map (make-sparse-keymap)
419 "Auto-complete mode map. It is also used for trigger key command. See also `ac-trigger-key'.")
420
421 (defvar ac-completing-map
422 (let ((map (make-sparse-keymap)))
423 (define-key map "\t" 'ac-expand)
424 (define-key map [tab] 'ac-expand)
425 (define-key map "\r" 'ac-complete)
426 (define-key map [return] 'ac-complete)
427 (define-key map (kbd "M-TAB") 'auto-complete)
428
429 (define-key map "\M-n" 'ac-next)
430 (define-key map "\M-p" 'ac-previous)
431 (define-key map [down] 'ac-next)
432 (define-key map [up] 'ac-previous)
433
434 (define-key map [f1] 'ac-help)
435 (define-key map [M-f1] 'ac-persist-help)
436 (define-key map (kbd "C-?") 'ac-help)
437 (define-key map (kbd "C-M-?") 'ac-persist-help)
438
439 (define-key map [C-down] 'ac-quick-help-scroll-down)
440 (define-key map [C-up] 'ac-quick-help-scroll-up)
441 (define-key map "\C-\M-n" 'ac-quick-help-scroll-down)
442 (define-key map "\C-\M-p" 'ac-quick-help-scroll-up)
443
444 (dotimes (i 9)
445 (let ((symbol (intern (format "ac-complete-select-%d" (1+ i)))))
446 (fset symbol
447 `(lambda ()
448 (interactive)
449 (when (and (ac-menu-live-p) (popup-select ac-menu ,i))
450 (ac-complete))))
451 (define-key map (read-kbd-macro (format "M-%s" (1+ i))) symbol)))
452
453 map)
454 "Keymap for completion.")
455 (defvaralias 'ac-complete-mode-map 'ac-completing-map)
456
457 (defvar ac-menu-map
458 (let ((map (make-sparse-keymap)))
459 (set-keymap-parent map ac-completing-map)
460 (define-key map "\C-n" 'ac-next)
461 (define-key map "\C-p" 'ac-previous)
462 (define-key map "\C-s" 'ac-isearch)
463 (define-key map [mouse-1] 'ac-mouse-1)
464 (define-key map [down-mouse-1] 'ac-ignore)
465 (define-key map [mouse-4] 'ac-mouse-4)
466 (define-key map [mouse-5] 'ac-mouse-5)
467 map)
468 "Keymap for completion on completing menu.")
469
470 (defvar ac-current-map
471 (let ((map (make-sparse-keymap)))
472 (set-keymap-parent map ac-completing-map)
473 map))
474
475 (defvar ac-match-function 'all-completions
476 "Default match function.")
477
478 (defvar ac-prefix-definitions
479 '((symbol . ac-prefix-symbol)
480 (file . ac-prefix-file)
481 (valid-file . ac-prefix-valid-file)
482 (c-dot . ac-prefix-c-dot)
483 (c-dot-ref . ac-prefix-c-dot-ref)
484 (cc-member . ac-prefix-cc-member))
485 "Prefix definitions for common use.")
486
487 (defvar ac-sources '(ac-source-words-in-same-mode-buffers)
488 "Sources for completion.")
489 (make-variable-buffer-local 'ac-sources)
490
491 (defvar ac-compiled-sources nil
492 "Compiled source of `ac-sources'.")
493
494 (defvar ac-current-sources nil
495 "Current working sources. This is sublist of `ac-compiled-sources'.")
496
497 (defvar ac-omni-completion-sources nil
498 "Do not use this anymore.")
499
500 (defvar ac-current-prefix-def nil)
501
502 (defvar ac-ignoring-prefix-def nil)
503
504 \f
505
506 ;;;; Intelligent completion history
507
508 (defvar ac-comphist nil
509 "Database of completion history.")
510
511 (defsubst ac-comphist-make-tab ()
512 (make-hash-table :test 'equal))
513
514 (defsubst ac-comphist-tab (db)
515 (nth 0 db))
516
517 (defsubst ac-comphist-cache (db)
518 (nth 1 db))
519
520 (defun ac-comphist-make (&optional tab)
521 (list (or tab (ac-comphist-make-tab)) (make-hash-table :test 'equal :weakness t)))
522
523 (defun ac-comphist-get (db string &optional create)
524 (let* ((tab (ac-comphist-tab db))
525 (index (gethash string tab)))
526 (when (and create (null index))
527 (setq index (make-vector (length string) 0))
528 (puthash string index tab))
529 index))
530
531 (defun ac-comphist-add (db string prefix)
532 (setq prefix (min prefix (1- (length string))))
533 (when (<= 0 prefix)
534 (setq string (substring-no-properties string))
535 (let ((stat (ac-comphist-get db string t)))
536 (incf (aref stat prefix))
537 (remhash string (ac-comphist-cache db)))))
538
539 (defun ac-comphist-score (db string prefix)
540 (setq prefix (min prefix (1- (length string))))
541 (if (<= 0 prefix)
542 (let ((cache (gethash string (ac-comphist-cache db))))
543 (or (and cache (aref cache prefix))
544 (let ((stat (ac-comphist-get db string))
545 (score 0.0))
546 (when stat
547 (loop for p from 0 below (length string)
548 ;; sigmoid function
549 with a = 5
550 with b = (/ 700.0 a) ; bounds for avoiding range error in `exp'
551 with d = (/ 6.0 a)
552 for x = (max (- b) (min b (- d (abs (- prefix p)))))
553 for r = (/ 1.0 (1+ (exp (* (- a) x))))
554 do
555 (incf score (* (aref stat p) r))))
556 ;; Weight by distance
557 (incf score (max 0.0 (- 0.3 (/ (- (length string) prefix) 100.0))))
558 (unless cache
559 (setq cache (make-vector (length string) nil))
560 (puthash string cache (ac-comphist-cache db)))
561 (aset cache prefix score)
562 score)))
563 0.0))
564
565 (defun ac-comphist-sort (db collection prefix &optional threshold)
566 (let (result
567 (n 0)
568 (total 0)
569 (cur 0))
570 (setq result (mapcar (lambda (a)
571 (when (and cur threshold)
572 (if (>= cur (* total threshold))
573 (setq cur nil)
574 (incf n)
575 (incf cur (cdr a))))
576 (car a))
577 (sort (mapcar (lambda (string)
578 (let ((score (ac-comphist-score db string prefix)))
579 (incf total score)
580 (cons string score)))
581 collection)
582 (lambda (a b) (< (cdr b) (cdr a))))))
583 (if threshold
584 (cons n result)
585 result)))
586
587 (defun ac-comphist-serialize (db)
588 (let (alist)
589 (maphash (lambda (k v)
590 (push (cons k v) alist))
591 (ac-comphist-tab db))
592 (list alist)))
593
594 (defun ac-comphist-deserialize (sexp)
595 (condition-case nil
596 (ac-comphist-make (let ((tab (ac-comphist-make-tab)))
597 (mapc (lambda (cons)
598 (puthash (car cons) (cdr cons) tab))
599 (nth 0 sexp))
600 tab))
601 (error (message "Invalid comphist db.") nil)))
602
603 (defun ac-comphist-init ()
604 (ac-comphist-load)
605 (add-hook 'kill-emacs-hook 'ac-comphist-save))
606
607 (defun ac-comphist-load ()
608 (interactive)
609 (let ((db (if (file-exists-p ac-comphist-file)
610 (ignore-errors
611 (with-temp-buffer
612 (insert-file-contents ac-comphist-file)
613 (goto-char (point-min))
614 (ac-comphist-deserialize (read (current-buffer))))))))
615 (setq ac-comphist (or db (ac-comphist-make)))))
616
617 (defun ac-comphist-save ()
618 (interactive)
619 (require 'pp)
620 (ignore-errors
621 (with-temp-buffer
622 (pp (ac-comphist-serialize ac-comphist) (current-buffer))
623 (write-region (point-min) (point-max) ac-comphist-file))))
624
625 \f
626
627 ;;;; Dictionary
628 (defvar ac-buffer-dictionary nil)
629 (defvar ac-file-dictionary (make-hash-table :test 'equal))
630
631 (defun ac-clear-dictionary-cache ()
632 (interactive)
633 (dolist (buffer (buffer-list))
634 (with-current-buffer buffer
635 (if (local-variable-p 'ac-buffer-dictionary)
636 (kill-local-variable 'ac-buffer-dictionary))))
637 (clrhash ac-file-dictionary))
638
639 (defun ac-file-dictionary (filename)
640 (let ((cache (gethash filename ac-file-dictionary 'none)))
641 (if (and cache (not (eq cache 'none)))
642 cache
643 (let (result)
644 (ignore-errors
645 (with-temp-buffer
646 (insert-file-contents filename)
647 (setq result (split-string (buffer-string) "\n" t))))
648 (puthash filename result ac-file-dictionary)
649 result))))
650
651 (defun ac-mode-dictionary (mode)
652 (loop for name in (cons (symbol-name mode)
653 (ignore-errors (list (file-name-extension (buffer-file-name)))))
654 append (loop for dir in ac-dictionary-directories
655 for file = (concat dir "/" name)
656 if (file-exists-p file)
657 append (ac-file-dictionary file))))
658
659 (defun ac-buffer-dictionary (&optional buffer)
660 (with-current-buffer (or buffer (current-buffer))
661 (if (local-variable-p 'ac-buffer-dictionary)
662 ac-buffer-dictionary
663 (make-local-variable 'ac-buffer-dictionary)
664 (setq ac-buffer-dictionary
665 (apply 'append
666 ac-user-dictionary
667 (ac-mode-dictionary major-mode)
668 (mapcar 'ac-file-dictionary ac-dictionary-files))))))
669
670 \f
671
672 ;;;; Auto completion internals
673
674 (defun ac-menu-at-wrapper-line-p ()
675 "Return non-nil if current line is long and wrapped to next visual line."
676 (and (not truncate-lines)
677 (eq (line-beginning-position)
678 (save-excursion
679 (vertical-motion 1)
680 (line-beginning-position)))))
681
682 (defun ac-stop-word-p (word)
683 (or (member word ac-stop-words)
684 (if ac-use-dictionary-as-stop-words
685 (member word (ac-buffer-dictionary)))))
686
687 (defun ac-prefix-default ()
688 "Same as `ac-prefix-symbol' but ignore a number prefix."
689 (let ((start (ac-prefix-symbol)))
690 (when start
691 (loop with end = (point)
692 for pos from start below end
693 for c = (char-after pos)
694 if (not (and (<= ?0 c) (<= c ?9)))
695 return start))))
696
697 (defun ac-prefix-symbol ()
698 "Default prefix definition function."
699 (require 'thingatpt)
700 (car-safe (bounds-of-thing-at-point 'symbol)))
701
702 (defun ac-prefix-file ()
703 "File prefix."
704 (let ((point (re-search-backward "[\"<>' \t\r\n]" nil t)))
705 (if point (1+ point))))
706
707 (defun ac-prefix-valid-file ()
708 "Existed (or to be existed) file prefix."
709 (let* ((line-beg (line-beginning-position))
710 (end (point))
711 (start (or (let ((point (re-search-backward "[\"<>'= \t\r\n]" line-beg t)))
712 (if point (1+ point)))
713 line-beg))
714 (file (buffer-substring start end)))
715 (if (and file (or (string-match "^/" file)
716 (and (setq file (and (string-match "^[^/]*/" file)
717 (match-string 0 file)))
718 (file-directory-p file))))
719 start)))
720
721 (defun ac-prefix-c-dot ()
722 "C-like languages dot(.) prefix."
723 (if (re-search-backward "\\.\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
724 (match-beginning 1)))
725
726 (defun ac-prefix-c-dot-ref ()
727 "C-like languages dot(.) and reference(->) prefix."
728 (if (re-search-backward "\\(?:\\.\\|->\\)\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
729 (match-beginning 1)))
730
731 (defun ac-prefix-cc-member ()
732 "C-like languages member(.)(->)(::) prefix."
733 (when (re-search-backward "\\(?:\\.\\|->\\|::\\)\\(\\(?:[a-zA-Z0-9][_a-zA-Z0-9]*\\)?\\)\\=" nil t)
734 (match-beginning 1)))
735
736 (defun ac-define-prefix (name prefix)
737 "Define new prefix definition.
738 You can not use it in source definition like (prefix . `NAME')."
739 (push (cons name prefix) ac-prefix-definitions))
740
741 (defun ac-match-substring (prefix candidates)
742 (loop with regexp = (regexp-quote prefix)
743 for candidate in candidates
744 if (string-match regexp candidate)
745 collect candidate))
746
747 (defsubst ac-source-entity (source)
748 (if (symbolp source)
749 (symbol-value source)
750 source))
751
752 (defun ac-source-available-p (source)
753 (if (and (symbolp source)
754 (get source 'available))
755 (eq (get source 'available) t)
756 (let* ((src (ac-source-entity source))
757 (avail-pair (assq 'available src))
758 (avail-cond (cdr avail-pair))
759 (available (and (if avail-pair
760 (cond
761 ((symbolp avail-cond)
762 (funcall avail-cond))
763 ((listp avail-cond)
764 (eval avail-cond)))
765 t)
766 (loop for feature in (assoc-default 'depends src)
767 unless (require feature nil t) return nil
768 finally return t))))
769 (if (symbolp source)
770 (put source 'available (if available t 'no)))
771 available)))
772
773 (defun ac-compile-sources (sources)
774 "Compiled `SOURCES' into expanded sources style."
775 (loop for source in sources
776 if (ac-source-available-p source)
777 do
778 (setq source (ac-source-entity source))
779 ;; prefix
780 (let* ((prefix (assoc 'prefix source))
781 (real (assoc-default (cdr prefix) ac-prefix-definitions)))
782 (cond
783 (real
784 (add-to-list 'source (cons 'prefix real)))
785 ((null prefix)
786 (add-to-list 'source (cons 'prefix 'ac-prefix-default)))))
787 ;; match
788 (let ((match (assq 'match source)))
789 (cond
790 ((eq (cdr match) 'substring)
791 (setcdr match 'ac-match-substring))))
792 and collect source))
793
794 (defun ac-compiled-sources ()
795 (or ac-compiled-sources
796 (setq ac-compiled-sources
797 (ac-compile-sources ac-sources))))
798
799 (defsubst ac-menu-live-p ()
800 (popup-live-p ac-menu))
801
802 (defun ac-menu-create (point width height)
803 (setq ac-menu
804 (popup-create point width height
805 :around t
806 :face 'ac-candidate-face
807 :max-width ac-max-width
808 :mouse-face 'ac-candidate-mouse-face
809 :selection-face 'ac-selection-face
810 :symbol t
811 :scroll-bar t
812 :margin-left 1
813 :keymap ac-menu-map
814 )))
815
816 (defun ac-menu-delete ()
817 (when ac-menu
818 (popup-delete ac-menu)
819 (setq ac-menu)))
820
821 (defsubst ac-inline-overlay ()
822 (nth 0 ac-inline))
823
824 (defsubst ac-inline-live-p ()
825 (and ac-inline (ac-inline-overlay) t))
826
827 (defun ac-inline-show (point string)
828 (unless ac-inline
829 (setq ac-inline (list nil)))
830 (save-excursion
831 (let ((overlay (ac-inline-overlay))
832 (width 0)
833 (string-width (string-width string))
834 (length 0)
835 (original-string string))
836 ;; Calculate string space to show completion
837 (goto-char point)
838 (let (c)
839 (while (and (not (eolp))
840 (< width string-width)
841 (setq c (char-after))
842 (not (eq c ?\t))) ; special case for tab
843 (incf width (char-width c))
844 (incf length)
845 (forward-char)))
846
847 ;; Show completion
848 (goto-char point)
849 (cond
850 ((= width 0)
851 ;; End-of-line
852 ;; Do nothing
853 )
854 ((<= width string-width)
855 ;; No space to show
856 ;; Do nothing
857 )
858 ((> width string-width)
859 ;; Need to fill space
860 (setq string (concat string (make-string (- width string-width) ? )))))
861 (setq string (propertize string 'face 'ac-completion-face))
862 (if overlay
863 (progn
864 (move-overlay overlay point (+ point length))
865 (overlay-put overlay 'invisible nil))
866 (setq overlay (make-overlay point (+ point length)))
867 (setf (nth 0 ac-inline) overlay)
868 (overlay-put overlay 'priority 9999)
869 ;; Help prefix-overlay in some cases
870 (overlay-put overlay 'keymap ac-current-map))
871 ;; TODO no width but char
872 (if (eq length 0)
873 ;; Case: End-of-line
874 (progn
875 (put-text-property 0 1 'cursor t string)
876 (overlay-put overlay 'after-string string))
877 (let ((display (substring string 0 1))
878 (after-string (substring string 1)))
879 (overlay-put overlay 'display display)
880 (overlay-put overlay 'after-string after-string)))
881 (overlay-put overlay 'string original-string))))
882
883 (defun ac-inline-delete ()
884 (when (ac-inline-live-p)
885 (ac-inline-hide)
886 (delete-overlay (ac-inline-overlay))
887 (setq ac-inline nil)))
888
889 (defun ac-inline-hide ()
890 (when (ac-inline-live-p)
891 (let ((overlay (ac-inline-overlay))
892 (buffer-undo-list t))
893 (when overlay
894 (move-overlay overlay (point-min) (point-min))
895 (overlay-put overlay 'invisible t)
896 (overlay-put overlay 'display nil)
897 (overlay-put overlay 'after-string nil)))))
898
899 (defun ac-inline-update ()
900 (if (and ac-completing ac-prefix (stringp ac-common-part))
901 (let ((common-part-length (length ac-common-part))
902 (prefix-length (length ac-prefix)))
903 (if (> common-part-length prefix-length)
904 (progn
905 (ac-inline-hide)
906 (ac-inline-show (point) (substring ac-common-part prefix-length)))
907 (ac-inline-delete)))
908 (ac-inline-delete)))
909
910 (defun ac-put-prefix-overlay ()
911 (unless ac-prefix-overlay
912 (let (newline)
913 ;; Insert newline to make sure that cursor always on the overlay
914 (when (eobp)
915 (popup-save-buffer-state
916 (insert "\n"))
917 (setq newline t))
918 (setq ac-prefix-overlay (make-overlay ac-point (1+ (point)) nil t t))
919 (overlay-put ac-prefix-overlay 'priority 9999)
920 (overlay-put ac-prefix-overlay 'keymap (make-sparse-keymap))
921 (overlay-put ac-prefix-overlay 'newline newline))))
922
923 (defun ac-remove-prefix-overlay ()
924 (when ac-prefix-overlay
925 (when (overlay-get ac-prefix-overlay 'newline)
926 ;; Remove inserted newline
927 (popup-save-buffer-state
928 (goto-char (point-max))
929 (if (eq (char-before) ?\n)
930 (delete-char -1))))
931 (delete-overlay ac-prefix-overlay)))
932
933 (defun ac-activate-completing-map ()
934 (if (and ac-show-menu ac-use-menu-map)
935 (set-keymap-parent ac-current-map ac-menu-map))
936 (when (and ac-use-overriding-local-map
937 (null overriding-terminal-local-map))
938 (setq overriding-terminal-local-map ac-current-map))
939 (when ac-prefix-overlay
940 (set-keymap-parent (overlay-get ac-prefix-overlay 'keymap) ac-current-map)))
941
942 (defun ac-deactivate-completing-map ()
943 (set-keymap-parent ac-current-map ac-completing-map)
944 (when (and ac-use-overriding-local-map
945 (eq overriding-terminal-local-map ac-current-map))
946 (setq overriding-terminal-local-map nil))
947 (when ac-prefix-overlay
948 (set-keymap-parent (overlay-get ac-prefix-overlay 'keymap) nil)))
949
950 (defsubst ac-selected-candidate ()
951 (if ac-menu
952 (popup-selected-item ac-menu)))
953
954 (defun ac-prefix (requires ignore-list)
955 (loop with current = (point)
956 with point
957 with prefix-def
958 with sources
959 for source in (ac-compiled-sources)
960 for prefix = (assoc-default 'prefix source)
961 for req = (or (assoc-default 'requires source) requires 1)
962
963 if (null prefix-def)
964 do
965 (unless (member prefix ignore-list)
966 (save-excursion
967 (setq point (cond
968 ((symbolp prefix)
969 (funcall prefix))
970 ((stringp prefix)
971 (and (re-search-backward (concat prefix "\\=") nil t)
972 (or (match-beginning 1) (match-beginning 0))))
973 ((stringp (car-safe prefix))
974 (let ((regexp (nth 0 prefix))
975 (end (nth 1 prefix))
976 (group (nth 2 prefix)))
977 (and (re-search-backward (concat regexp "\\=") nil t)
978 (funcall (if end 'match-end 'match-beginning)
979 (or group 0)))))
980 (t
981 (eval prefix))))
982 (if (and point
983 (integerp req)
984 (< (- current point) req))
985 (setq point nil))
986 (if point
987 (setq prefix-def prefix))))
988
989 if (equal prefix prefix-def) do (push source sources)
990
991 finally return
992 (and point (list prefix-def point (nreverse sources)))))
993
994 (defun ac-init ()
995 "Initialize current sources to start completion."
996 (setq ac-candidates-cache nil)
997 (loop for source in ac-current-sources
998 for function = (assoc-default 'init source)
999 if function do
1000 (save-excursion
1001 (cond
1002 ((functionp function)
1003 (funcall function))
1004 (t
1005 (eval function))))))
1006
1007 (defun ac-candidates-1 (source)
1008 (let* ((do-cache (assq 'cache source))
1009 (function (assoc-default 'candidates source))
1010 (action (assoc-default 'action source))
1011 (document (assoc-default 'document source))
1012 (symbol (assoc-default 'symbol source))
1013 (ac-limit (or (assoc-default 'limit source) ac-limit))
1014 (face (or (assoc-default 'face source) (assoc-default 'candidate-face source)))
1015 (selection-face (assoc-default 'selection-face source))
1016 (cache (and do-cache (assq source ac-candidates-cache)))
1017 (candidates (cdr cache)))
1018 (unless cache
1019 (setq candidates (save-excursion
1020 (cond
1021 ((functionp function)
1022 (funcall function))
1023 (t
1024 (eval function)))))
1025 ;; Convert (name value) format candidates into name with text properties.
1026 (setq candidates (mapcar (lambda (candidate)
1027 (if (consp candidate)
1028 (propertize (car candidate) 'value (cdr candidate))
1029 candidate))
1030 candidates))
1031 (when do-cache
1032 (push (cons source candidates) ac-candidates-cache)))
1033 (setq candidates (funcall (or (assoc-default 'match source)
1034 ac-match-function)
1035 ac-prefix candidates))
1036 ;; Remove extra items regarding to ac-limit
1037 (if (and (integerp ac-limit) (> ac-limit 1) (> (length candidates) ac-limit))
1038 (setcdr (nthcdr (1- ac-limit) candidates) nil))
1039 ;; Put candidate properties
1040 (setq candidates (mapcar (lambda (candidate)
1041 (popup-item-propertize candidate
1042 'action action
1043 'symbol symbol
1044 'document document
1045 'popup-face face
1046 'selection-face selection-face))
1047 candidates))
1048 candidates))
1049
1050 (defun ac-candidates ()
1051 "Produce candidates for current sources."
1052 (loop with completion-ignore-case = (or (eq ac-ignore-case t)
1053 (and (eq ac-ignore-case 'smart)
1054 (let ((case-fold-search nil)) (not (string-match "[[:upper:]]" ac-prefix)))))
1055 with case-fold-search = completion-ignore-case
1056 with prefix-len = (length ac-prefix)
1057 for source in ac-current-sources
1058 append (ac-candidates-1 source) into candidates
1059 finally return
1060 (progn
1061 (delete-dups candidates)
1062 (if (and ac-use-comphist ac-comphist)
1063 (if ac-show-menu
1064 (let* ((pair (ac-comphist-sort ac-comphist candidates prefix-len ac-comphist-threshold))
1065 (n (car pair))
1066 (result (cdr pair))
1067 (cons (if (> n 0) (nthcdr (1- n) result)))
1068 (cdr (cdr cons)))
1069 (if cons (setcdr cons nil))
1070 (setq ac-common-part (try-completion ac-prefix result))
1071 (setq ac-whole-common-part (try-completion ac-prefix candidates))
1072 (if cons (setcdr cons cdr))
1073 result)
1074 (setq candidates (ac-comphist-sort ac-comphist candidates prefix-len))
1075 (setq ac-common-part (if candidates (popup-x-to-string (car candidates))))
1076 (setq ac-whole-common-part (try-completion ac-prefix candidates))
1077 candidates)
1078 (setq ac-common-part (try-completion ac-prefix candidates))
1079 (setq ac-whole-common-part ac-common-part)
1080 candidates))))
1081
1082 (defun ac-update-candidates (cursor scroll-top)
1083 "Update candidates of menu to `ac-candidates' and redraw it."
1084 (setf (popup-cursor ac-menu) cursor
1085 (popup-scroll-top ac-menu) scroll-top)
1086 (setq ac-dwim-enable (= (length ac-candidates) 1))
1087 (if ac-candidates
1088 (progn
1089 (setq ac-completing t)
1090 (ac-activate-completing-map))
1091 (setq ac-completing nil)
1092 (ac-deactivate-completing-map))
1093 (unless ac-disable-inline
1094 (ac-inline-update))
1095 (popup-set-list ac-menu ac-candidates)
1096 (if (and (not ac-fuzzy-enable)
1097 (<= (length ac-candidates) ac-candidate-menu-min))
1098 (popup-hide ac-menu)
1099 (if ac-show-menu
1100 (popup-draw ac-menu))))
1101
1102 (defun ac-reposition ()
1103 "Force to redraw candidate menu with current `ac-candidates'."
1104 (let ((cursor (popup-cursor ac-menu))
1105 (scroll-top (popup-scroll-top ac-menu))
1106 (height (popup-height ac-menu)))
1107 (ac-menu-delete)
1108 (ac-menu-create ac-point (popup-preferred-width ac-candidates) height)
1109 (ac-update-candidates cursor scroll-top)))
1110
1111 (defun ac-cleanup ()
1112 "Cleanup auto completion."
1113 (if ac-cursor-color
1114 (set-cursor-color ac-cursor-color))
1115 (when (and ac-use-comphist ac-comphist)
1116 (when (and (null ac-selected-candidate)
1117 (member ac-prefix ac-candidates))
1118 ;; Assume candidate is selected by just typing
1119 (setq ac-selected-candidate ac-prefix)
1120 (setq ac-last-point ac-point))
1121 (when ac-selected-candidate
1122 (ac-comphist-add ac-comphist
1123 ac-selected-candidate
1124 (if ac-last-point
1125 (- ac-last-point ac-point)
1126 (length ac-prefix)))))
1127 (ac-deactivate-completing-map)
1128 (ac-remove-prefix-overlay)
1129 (ac-remove-quick-help)
1130 (ac-inline-delete)
1131 (ac-menu-delete)
1132 (ac-cancel-timer)
1133 (ac-cancel-show-menu-timer)
1134 (ac-cancel-quick-help-timer)
1135 (setq ac-cursor-color nil
1136 ac-inline nil
1137 ac-show-menu nil
1138 ac-menu nil
1139 ac-completing nil
1140 ac-point nil
1141 ac-last-point nil
1142 ac-prefix nil
1143 ac-prefix-overlay nil
1144 ac-selected-candidate nil
1145 ac-common-part nil
1146 ac-whole-common-part nil
1147 ac-triggered nil
1148 ac-limit nil
1149 ac-candidates nil
1150 ac-candidates-cache nil
1151 ac-fuzzy-enable nil
1152 ac-dwim-enable nil
1153 ac-compiled-sources nil
1154 ac-current-sources nil
1155 ac-current-prefix-def nil
1156 ac-ignoring-prefix-def nil))
1157
1158 (defsubst ac-abort ()
1159 "Abort completion."
1160 (ac-cleanup))
1161
1162 (defun ac-expand-string (string &optional remove-undo-boundary)
1163 "Expand `STRING' into the buffer and update `ac-prefix' to `STRING'.
1164 This function records deletion and insertion sequences by `undo-boundary'.
1165 If `remove-undo-boundary' is non-nil, this function also removes `undo-boundary'
1166 that have been made before in this function. When `buffer-undo-list' is
1167 `t', `remove-undo-boundary' has no effect."
1168 (when (eq buffer-undo-list t)
1169 (setq remove-undo-boundary nil))
1170 (when (not (equal string (buffer-substring ac-point (point))))
1171 (undo-boundary)
1172 ;; We can't use primitive-undo since it undoes by
1173 ;; groups, divided by boundaries.
1174 ;; We don't want boundary between deletion and insertion.
1175 ;; So do it manually.
1176 ;; Delete region silently for undo:
1177 (if remove-undo-boundary
1178 (progn
1179 (let (buffer-undo-list)
1180 (save-excursion
1181 (delete-region ac-point (point))))
1182 (setq buffer-undo-list
1183 (nthcdr 2 buffer-undo-list)))
1184 (delete-region ac-point (point)))
1185 (insert (substring-no-properties string))
1186 ;; Sometimes, possible when omni-completion used, (insert) added
1187 ;; to buffer-undo-list strange record about position changes.
1188 ;; Delete it here:
1189 (when (and remove-undo-boundary
1190 (integerp (cadr buffer-undo-list)))
1191 (setcdr buffer-undo-list (nthcdr 2 buffer-undo-list)))
1192 (undo-boundary)
1193 (setq ac-selected-candidate string)
1194 (setq ac-prefix string)))
1195
1196 (defun ac-set-trigger-key (key)
1197 "Set `ac-trigger-key' to `KEY'. It is recommemded to use this function instead of calling `setq'."
1198 ;; Remove old mapping
1199 (when ac-trigger-key
1200 (define-key ac-mode-map (read-kbd-macro ac-trigger-key) nil))
1201
1202 ;; Make new mapping
1203 (setq ac-trigger-key key)
1204 (when key
1205 (define-key ac-mode-map (read-kbd-macro key) 'ac-trigger-key-command)))
1206
1207 (defun ac-set-timer ()
1208 (unless ac-timer
1209 (setq ac-timer (run-with-idle-timer ac-delay ac-delay 'ac-update-greedy))))
1210
1211 (defun ac-cancel-timer ()
1212 (when (timerp ac-timer)
1213 (cancel-timer ac-timer)
1214 (setq ac-timer nil)))
1215
1216 (defun ac-update (&optional force)
1217 (when (and auto-complete-mode
1218 ac-prefix
1219 (or ac-triggered
1220 force)
1221 (not isearch-mode))
1222 (ac-put-prefix-overlay)
1223 (setq ac-candidates (ac-candidates))
1224 (let ((preferred-width (popup-preferred-width ac-candidates)))
1225 ;; Reposition if needed
1226 (when (or (null ac-menu)
1227 (>= (popup-width ac-menu) preferred-width)
1228 (<= (popup-width ac-menu) (- preferred-width 10))
1229 (and (> (popup-direction ac-menu) 0)
1230 (ac-menu-at-wrapper-line-p)))
1231 (ac-inline-hide) ; Hide overlay to calculate correct column
1232 (ac-remove-quick-help)
1233 (ac-menu-delete)
1234 (ac-menu-create ac-point preferred-width ac-menu-height)))
1235 (ac-update-candidates 0 0)
1236 t))
1237
1238 (defun ac-update-greedy (&optional force)
1239 (let (result)
1240 (while (when (and (setq result (ac-update force))
1241 (null ac-candidates))
1242 (add-to-list 'ac-ignoring-prefix-def ac-current-prefix-def)
1243 (ac-start :force-init t)
1244 ac-current-prefix-def))
1245 result))
1246
1247 (defun ac-set-show-menu-timer ()
1248 (when (and (or (integerp ac-auto-show-menu) (floatp ac-auto-show-menu))
1249 (null ac-show-menu-timer))
1250 (setq ac-show-menu-timer (run-with-idle-timer ac-auto-show-menu ac-auto-show-menu 'ac-show-menu))))
1251
1252 (defun ac-cancel-show-menu-timer ()
1253 (when (timerp ac-show-menu-timer)
1254 (cancel-timer ac-show-menu-timer)
1255 (setq ac-show-menu-timer nil)))
1256
1257 (defun ac-show-menu ()
1258 (when (not (eq ac-show-menu t))
1259 (setq ac-show-menu t)
1260 (ac-inline-hide)
1261 (ac-remove-quick-help)
1262 (ac-update t)))
1263
1264 (defun ac-help (&optional persist)
1265 (interactive "P")
1266 (when ac-menu
1267 (popup-menu-show-help ac-menu persist)))
1268
1269 (defun ac-persist-help ()
1270 (interactive)
1271 (ac-help t))
1272
1273 (defun ac-last-help (&optional persist)
1274 (interactive "P")
1275 (when ac-last-completion
1276 (popup-item-show-help (cdr ac-last-completion) persist)))
1277
1278 (defun ac-last-persist-help ()
1279 (interactive)
1280 (ac-last-help t))
1281
1282 (defun ac-set-quick-help-timer ()
1283 (when (and ac-use-quick-help
1284 (null ac-quick-help-timer))
1285 (setq ac-quick-help-timer (run-with-idle-timer ac-quick-help-delay ac-quick-help-delay 'ac-quick-help))))
1286
1287 (defun ac-cancel-quick-help-timer ()
1288 (when (timerp ac-quick-help-timer)
1289 (cancel-timer ac-quick-help-timer)
1290 (setq ac-quick-help-timer nil)))
1291
1292 (defun ac-pos-tip-show-quick-help (menu &optional item &rest args)
1293 (let* ((point (plist-get args :point))
1294 (around nil)
1295 (parent-offset (popup-offset menu))
1296 (doc (popup-menu-documentation menu item)))
1297 (when (stringp doc)
1298 (if (popup-hidden-p menu)
1299 (setq around t)
1300 (setq point nil))
1301 (with-no-warnings
1302 (pos-tip-show doc
1303 'popup-tip-face
1304 (or point
1305 (and menu
1306 (popup-child-point menu parent-offset))
1307 (point))
1308 nil 300
1309 popup-tip-max-width
1310 nil nil
1311 (and (not around) 0))
1312 (unless (plist-get args :nowait)
1313 (clear-this-command-keys)
1314 (unwind-protect
1315 (push (read-event (plist-get args :prompt)) unread-command-events)
1316 (pos-tip-hide))
1317 t)))))
1318
1319 (defun ac-quick-help-use-pos-tip-p ()
1320 (and ac-quick-help-prefer-pos-tip
1321 window-system
1322 (featurep 'pos-tip)))
1323
1324 (defun ac-quick-help (&optional force)
1325 (interactive)
1326 ;; TODO don't use FORCE
1327 (when (and (or force
1328 (with-no-warnings
1329 ;; called-interactively-p can take no args
1330 (called-interactively-p))
1331 ;; ac-isearch'ing
1332 (null this-command))
1333 (ac-menu-live-p)
1334 (null ac-quick-help))
1335 (setq ac-quick-help
1336 (funcall (if (ac-quick-help-use-pos-tip-p)
1337 'ac-pos-tip-show-quick-help
1338 'popup-menu-show-quick-help)
1339 ac-menu nil
1340 :point ac-point
1341 :height ac-quick-help-height
1342 :nowait t))))
1343
1344 (defun ac-remove-quick-help ()
1345 (when (ac-quick-help-use-pos-tip-p)
1346 (with-no-warnings
1347 (pos-tip-hide)))
1348 (when ac-quick-help
1349 (popup-delete ac-quick-help)
1350 (setq ac-quick-help nil)))
1351
1352 (defun ac-last-quick-help ()
1353 (interactive)
1354 (when (and ac-last-completion
1355 (eq (marker-buffer (car ac-last-completion))
1356 (current-buffer)))
1357 (let ((doc (popup-item-documentation (cdr ac-last-completion)))
1358 (point (marker-position (car ac-last-completion))))
1359 (when (stringp doc)
1360 (if (ac-quick-help-use-pos-tip-p)
1361 (with-no-warnings (pos-tip-show doc nil point nil 300))
1362 (popup-tip doc
1363 :point point
1364 :around t
1365 :scroll-bar t
1366 :margin t))))))
1367
1368 (defmacro ac-define-quick-help-command (name arglist &rest body)
1369 (declare (indent 2))
1370 `(progn
1371 (defun ,name ,arglist ,@body)
1372 (put ',name 'ac-quick-help-command t)))
1373
1374 (ac-define-quick-help-command ac-quick-help-scroll-down ()
1375 (interactive)
1376 (when ac-quick-help
1377 (popup-scroll-down ac-quick-help)))
1378
1379 (ac-define-quick-help-command ac-quick-help-scroll-up ()
1380 (interactive)
1381 (when ac-quick-help
1382 (popup-scroll-up ac-quick-help)))
1383
1384 \f
1385
1386 ;;;; Auto completion isearch
1387
1388 (defun ac-isearch-callback (list)
1389 (setq ac-dwim-enable (eq (length list) 1)))
1390
1391 (defun ac-isearch ()
1392 (interactive)
1393 (when (ac-menu-live-p)
1394 (ac-cancel-show-menu-timer)
1395 (ac-show-menu)
1396 (if ac-use-quick-help
1397 (let ((popup-menu-show-quick-help-function
1398 (if (ac-quick-help-use-pos-tip-p)
1399 'ac-pos-tip-show-quick-help
1400 'popup-menu-show-quick-help)))
1401 (popup-isearch ac-menu
1402 :callback 'ac-isearch-callback
1403 :help-delay ac-quick-help-delay))
1404 (popup-isearch ac-menu :callback 'ac-isearch-callback))))
1405
1406 \f
1407
1408 ;;;; Auto completion commands
1409
1410 (defun* auto-complete-1 (&key sources (triggered 'command))
1411 (let ((menu-live (ac-menu-live-p))
1412 (inline-live (ac-inline-live-p))
1413 started)
1414 (ac-abort)
1415 (let ((ac-sources (or sources ac-sources)))
1416 (if (or ac-show-menu-immediately-on-auto-complete
1417 inline-live)
1418 (setq ac-show-menu t))
1419 (setq started (ac-start :triggered triggered)))
1420 (when (ac-update-greedy t)
1421 ;; TODO Not to cause inline completion to be disrupted.
1422 (if (ac-inline-live-p)
1423 (ac-inline-hide))
1424 ;; Not to expand when it is first time to complete
1425 (when (and (or (and (not ac-expand-on-auto-complete)
1426 (> (length ac-candidates) 1)
1427 (not menu-live))
1428 (not (let ((ac-common-part ac-whole-common-part))
1429 (ac-expand-common))))
1430 ac-use-fuzzy
1431 (null ac-candidates))
1432 (ac-fuzzy-complete)))
1433 started))
1434
1435 ;;;###autoload
1436 (defun auto-complete (&optional sources)
1437 "Start auto-completion at current point."
1438 (interactive)
1439 (auto-complete-1 :sources sources))
1440
1441 (defun ac-fuzzy-complete ()
1442 "Start fuzzy completion at current point."
1443 (interactive)
1444 (if (not (require 'fuzzy nil t))
1445 (message "Please install fuzzy.el if you use fuzzy completion")
1446 (unless (ac-menu-live-p)
1447 (ac-start))
1448 (let ((ac-match-function 'fuzzy-all-completions))
1449 (unless ac-cursor-color
1450 (setq ac-cursor-color (frame-parameter (selected-frame) 'cursor-color)))
1451 (if ac-fuzzy-cursor-color
1452 (set-cursor-color ac-fuzzy-cursor-color))
1453 (setq ac-show-menu t)
1454 (setq ac-fuzzy-enable t)
1455 (setq ac-triggered nil)
1456 (ac-update t)))
1457 t)
1458
1459 (defun ac-next ()
1460 "Select next candidate."
1461 (interactive)
1462 (when (ac-menu-live-p)
1463 (when (popup-hidden-p ac-menu)
1464 (ac-show-menu))
1465 (popup-next ac-menu)
1466 (if (eq this-command 'ac-next)
1467 (setq ac-dwim-enable t))))
1468
1469 (defun ac-previous ()
1470 "Select previous candidate."
1471 (interactive)
1472 (when (ac-menu-live-p)
1473 (when (popup-hidden-p ac-menu)
1474 (ac-show-menu))
1475 (popup-previous ac-menu)
1476 (if (eq this-command 'ac-previous)
1477 (setq ac-dwim-enable t))))
1478
1479 (defun ac-expand ()
1480 "Try expand, and if expanded twice, select next candidate."
1481 (interactive)
1482 (unless (ac-expand-common)
1483 (let ((string (ac-selected-candidate)))
1484 (when string
1485 (when (equal ac-prefix string)
1486 (ac-next)
1487 (setq string (ac-selected-candidate)))
1488 (ac-expand-string string (eq last-command this-command))
1489 ;; Do reposition if menu at long line
1490 (if (and (> (popup-direction ac-menu) 0)
1491 (ac-menu-at-wrapper-line-p))
1492 (ac-reposition))
1493 (setq ac-show-menu t)
1494 string))))
1495
1496 (defun ac-expand-common ()
1497 "Try to expand meaningful common part."
1498 (interactive)
1499 (if (and ac-dwim ac-dwim-enable)
1500 (ac-complete)
1501 (when (and (ac-inline-live-p)
1502 ac-common-part)
1503 (ac-inline-hide)
1504 (ac-expand-string ac-common-part (eq last-command this-command))
1505 (setq ac-common-part nil)
1506 t)))
1507
1508 (defun ac-complete-1 (candidate)
1509 (let ((action (popup-item-property candidate 'action))
1510 (fallback nil))
1511 (when candidate
1512 (unless (ac-expand-string candidate)
1513 (setq fallback t))
1514 ;; Remember to show help later
1515 (when (and ac-point candidate)
1516 (unless ac-last-completion
1517 (setq ac-last-completion (cons (make-marker) nil)))
1518 (set-marker (car ac-last-completion) ac-point ac-buffer)
1519 (setcdr ac-last-completion candidate)))
1520 (ac-abort)
1521 (cond
1522 (action
1523 (funcall action))
1524 (fallback
1525 (ac-fallback-command)))
1526 candidate))
1527
1528 (defun ac-complete ()
1529 "Try complete."
1530 (interactive)
1531 (ac-complete-1 (ac-selected-candidate)))
1532
1533 (defun* ac-start (&key
1534 requires
1535 force-init
1536 (triggered (or ac-triggered t)))
1537 "Start completion."
1538 (interactive)
1539 (if (not auto-complete-mode)
1540 (message "auto-complete-mode is not enabled")
1541 (let* ((info (ac-prefix requires ac-ignoring-prefix-def))
1542 (prefix-def (nth 0 info))
1543 (point (nth 1 info))
1544 (sources (nth 2 info))
1545 prefix
1546 (init (or force-init (not (eq ac-point point)))))
1547 (if (or (null point)
1548 (progn
1549 (setq prefix (buffer-substring-no-properties point (point)))
1550 (and (not (eq triggered 'command))
1551 (ac-stop-word-p prefix))))
1552 (prog1 nil
1553 (ac-abort))
1554 (unless ac-cursor-color
1555 (setq ac-cursor-color (frame-parameter (selected-frame) 'cursor-color)))
1556 (setq ac-show-menu (or ac-show-menu (if (eq ac-auto-show-menu t) t))
1557 ac-current-sources sources
1558 ac-buffer (current-buffer)
1559 ac-point point
1560 ac-prefix prefix
1561 ac-limit ac-candidate-limit
1562 ac-triggered triggered
1563 ac-current-prefix-def prefix-def)
1564 (when (or init (null ac-prefix-overlay))
1565 (ac-init))
1566 (ac-set-timer)
1567 (ac-set-show-menu-timer)
1568 (ac-set-quick-help-timer)
1569 (ac-put-prefix-overlay)
1570 t))))
1571
1572 (defun ac-stop ()
1573 "Stop completiong."
1574 (interactive)
1575 (setq ac-selected-candidate nil)
1576 (ac-abort))
1577
1578 (defun ac-ignore (&rest ignore)
1579 "Same as `ignore'."
1580 (interactive))
1581
1582 (defun ac-mouse-1 (event)
1583 (interactive "e")
1584 (popup-awhen (popup-menu-item-of-mouse-event event)
1585 (ac-complete-1 it)))
1586
1587 (defun ac-mouse-4 (event)
1588 (interactive "e")
1589 (ac-previous))
1590
1591 (defun ac-mouse-5 (event)
1592 (interactive "e")
1593 (ac-next))
1594
1595 (defun ac-trigger-key-command (&optional force)
1596 (interactive "P")
1597 (let (started)
1598 (when (or force (ac-trigger-command-p last-command))
1599 (setq started (auto-complete-1 :triggered 'trigger-key)))
1600 (unless started
1601 (ac-fallback-command 'ac-trigger-key-command))))
1602
1603 \f
1604
1605 ;;;; Basic cache facility
1606
1607 (defvar ac-clear-variables-every-minute-timer nil)
1608 (defvar ac-clear-variables-after-save nil)
1609 (defvar ac-clear-variables-every-minute nil)
1610 (defvar ac-minutes-counter 0)
1611
1612 (defun ac-clear-variable-after-save (variable &optional pred)
1613 (add-to-list 'ac-clear-variables-after-save (cons variable pred)))
1614
1615 (defun ac-clear-variables-after-save ()
1616 (dolist (pair ac-clear-variables-after-save)
1617 (if (or (null (cdr pair))
1618 (funcall (cdr pair)))
1619 (set (car pair) nil))))
1620
1621 (defun ac-clear-variable-every-minutes (variable minutes)
1622 (add-to-list 'ac-clear-variables-every-minute (cons variable minutes)))
1623
1624 (defun ac-clear-variable-every-minute (variable)
1625 (ac-clear-variable-every-minutes variable 1))
1626
1627 (defun ac-clear-variable-every-10-minutes (variable)
1628 (ac-clear-variable-every-minutes variable 10))
1629
1630 (defun ac-clear-variables-every-minute ()
1631 (incf ac-minutes-counter)
1632 (dolist (pair ac-clear-variables-every-minute)
1633 (if (eq (% ac-minutes-counter (cdr pair)) 0)
1634 (set (car pair) nil))))
1635
1636 \f
1637
1638 ;;;; Auto complete mode
1639
1640 (defun ac-cursor-on-diable-face-p (&optional point)
1641 (memq (get-text-property (or point (point)) 'face) ac-disable-faces))
1642
1643 (defun ac-trigger-command-p (command)
1644 "Return non-nil if `COMMAND' is a trigger command."
1645 (and (symbolp command)
1646 (not (memq command ac-non-trigger-commands))
1647 (or (memq command ac-trigger-commands)
1648 (string-match "self-insert-command" (symbol-name command))
1649 (string-match "electric" (symbol-name command)))))
1650
1651 (defun ac-fallback-key-sequence ()
1652 (setq unread-command-events
1653 (append (this-single-command-raw-keys)
1654 unread-command-events))
1655 (read-key-sequence-vector ""))
1656
1657 (defun ac-fallback-command (&optional except-command)
1658 (let* ((auto-complete-mode nil)
1659 (keys (ac-fallback-key-sequence))
1660 (command (and keys (key-binding keys))))
1661 (when (and (commandp command)
1662 (not (eq command except-command)))
1663 (setq this-command command)
1664 (call-interactively command))))
1665
1666 (defun ac-compatible-package-command-p (command)
1667 "Return non-nil if `COMMAND' is compatible with auto-complete."
1668 (and (symbolp command)
1669 (string-match ac-compatible-packages-regexp (symbol-name command))))
1670
1671 (defun ac-handle-pre-command ()
1672 (condition-case var
1673 (if (or (setq ac-triggered (and (not ac-fuzzy-enable) ; ignore key storkes in fuzzy mode
1674 (or (eq this-command 'auto-complete) ; special case
1675 (ac-trigger-command-p this-command)
1676 (and ac-completing
1677 (memq this-command ac-trigger-commands-on-completing)))
1678 (not (ac-cursor-on-diable-face-p))))
1679 (ac-compatible-package-command-p this-command))
1680 (progn
1681 (if (or (not (symbolp this-command))
1682 (not (get this-command 'ac-quick-help-command)))
1683 (ac-remove-quick-help))
1684 ;; Not to cause inline completion to be disrupted.
1685 (ac-inline-hide))
1686 (ac-abort))
1687 (error (ac-error var))))
1688
1689 (defun ac-handle-post-command ()
1690 (condition-case var
1691 (when (and ac-triggered
1692 (or ac-auto-start
1693 ac-completing)
1694 (not isearch-mode))
1695 (setq ac-last-point (point))
1696 (ac-start :requires (unless ac-completing ac-auto-start))
1697 (unless ac-disable-inline
1698 (ac-inline-update)))
1699 (error (ac-error var))))
1700
1701 (defun ac-setup ()
1702 (if ac-trigger-key
1703 (ac-set-trigger-key ac-trigger-key))
1704 (if ac-use-comphist
1705 (ac-comphist-init))
1706 (unless ac-clear-variables-every-minute-timer
1707 (setq ac-clear-variables-every-minute-timer (run-with-timer 60 60 'ac-clear-variables-every-minute)))
1708 (if ac-stop-flymake-on-completing
1709 (defadvice flymake-on-timer-event (around ac-flymake-stop-advice activate)
1710 (unless ac-completing
1711 ad-do-it))
1712 (ad-disable-advice 'flymake-on-timer-event 'around 'ac-flymake-stop-advice)))
1713
1714 ;;;###autoload
1715 (define-minor-mode auto-complete-mode
1716 "AutoComplete mode"
1717 :lighter " AC"
1718 :keymap ac-mode-map
1719 :group 'auto-complete
1720 (if auto-complete-mode
1721 (progn
1722 (ac-setup)
1723 (add-hook 'pre-command-hook 'ac-handle-pre-command nil t)
1724 (add-hook 'post-command-hook 'ac-handle-post-command nil t)
1725 (add-hook 'after-save-hook 'ac-clear-variables-after-save nil t)
1726 (run-hooks 'auto-complete-mode-hook))
1727 (remove-hook 'pre-command-hook 'ac-handle-pre-command t)
1728 (remove-hook 'post-command-hook 'ac-handle-post-command t)
1729 (remove-hook 'after-save-hook 'ac-clear-variables-after-save t)
1730 (ac-abort)))
1731
1732 (defun auto-complete-mode-maybe ()
1733 "What buffer `auto-complete-mode' prefers."
1734 (if (and (not (minibufferp (current-buffer)))
1735 (memq major-mode ac-modes))
1736 (auto-complete-mode 1)))
1737
1738 ;;;###autoload
1739 (define-global-minor-mode global-auto-complete-mode
1740 auto-complete-mode auto-complete-mode-maybe
1741 :group 'auto-complete)
1742
1743 \f
1744
1745 ;;;; Compatibilities with other extensions
1746
1747 (defun ac-flyspell-workaround ()
1748 "Flyspell uses `sit-for' for delaying its process. Unfortunatelly,
1749 it stops auto completion which is trigger with `run-with-idle-timer'.
1750 This workaround avoid flyspell processes when auto completion is being started."
1751 (interactive)
1752 (defadvice flyspell-post-command-hook (around ac-flyspell-workaround activate)
1753 (unless ac-triggered
1754 ad-do-it)))
1755
1756 (defun ac-linum-workaround ()
1757 "linum-mode tries to display the line numbers even for the
1758 completion menu. This workaround stops that annoying behavior."
1759 (interactive)
1760 (defadvice linum-update (around ac-linum-update-workaround activate)
1761 (unless ac-completing
1762 ad-do-it)))
1763
1764 \f
1765
1766 ;;;; Standard sources
1767
1768 (defmacro ac-define-source (name source)
1769 "Source definition macro. It defines a complete command also."
1770 (declare (indent 1))
1771 `(progn
1772 (defvar ,(intern (format "ac-source-%s" name)))
1773 ;; Use `setq' to reset ac-source-NAME every time
1774 ;; `ac-define-source' is called. This is useful, for example
1775 ;; when evaluating `ac-define-source' using C-M-x (`eval-defun').
1776 (setq ,(intern (format "ac-source-%s" name)) ,source)
1777 (defun ,(intern (format "ac-complete-%s" name)) ()
1778 (interactive)
1779 (auto-complete '(,(intern (format "ac-source-%s" name)))))))
1780
1781 ;; Words in buffer source
1782 (defvar ac-word-index nil)
1783
1784 (defun ac-candidate-words-in-buffer (point prefix limit)
1785 (let ((i 0)
1786 candidate
1787 candidates
1788 (regexp (concat "\\_<" (regexp-quote prefix) "\\(\\sw\\|\\s_\\)+\\_>")))
1789 (save-excursion
1790 ;; Search backward
1791 (goto-char point)
1792 (while (and (or (not (integerp limit)) (< i limit))
1793 (re-search-backward regexp nil t))
1794 (setq candidate (match-string-no-properties 0))
1795 (unless (member candidate candidates)
1796 (push candidate candidates)
1797 (incf i)))
1798 ;; Search backward
1799 (goto-char (+ point (length prefix)))
1800 (while (and (or (not (integerp limit)) (< i limit))
1801 (re-search-forward regexp nil t))
1802 (setq candidate (match-string-no-properties 0))
1803 (unless (member candidate candidates)
1804 (push candidate candidates)
1805 (incf i)))
1806 (nreverse candidates))))
1807
1808 (defun ac-incremental-update-word-index ()
1809 (unless (local-variable-p 'ac-word-index)
1810 (make-local-variable 'ac-word-index))
1811 (if (null ac-word-index)
1812 (setq ac-word-index (cons nil nil)))
1813 ;; Mark incomplete
1814 (if (car ac-word-index)
1815 (setcar ac-word-index nil))
1816 (let ((index (cdr ac-word-index))
1817 (words (ac-candidate-words-in-buffer ac-point ac-prefix (or (and (integerp ac-limit) ac-limit) 10))))
1818 (dolist (word words)
1819 (unless (member word index)
1820 (push word index)
1821 (setcdr ac-word-index index)))))
1822
1823 (defun ac-update-word-index-1 ()
1824 (unless (local-variable-p 'ac-word-index)
1825 (make-local-variable 'ac-word-index))
1826 (when (and (not (car ac-word-index))
1827 (< (buffer-size) 1048576))
1828 ;; Complete index
1829 (setq ac-word-index
1830 (cons t
1831 (split-string (buffer-substring-no-properties (point-min) (point-max))
1832 "\\(?:^\\|\\_>\\).*?\\(?:\\_<\\|$\\)")))))
1833
1834 (defun ac-update-word-index ()
1835 (dolist (buffer (buffer-list))
1836 (when (or ac-fuzzy-enable
1837 (not (eq buffer (current-buffer))))
1838 (with-current-buffer buffer
1839 (ac-update-word-index-1)))))
1840
1841 (defun ac-word-candidates (&optional buffer-pred)
1842 (loop initially (unless ac-fuzzy-enable (ac-incremental-update-word-index))
1843 for buffer in (buffer-list)
1844 if (and (or (not (integerp ac-limit)) (< (length candidates) ac-limit))
1845 (if buffer-pred (funcall buffer-pred buffer) t))
1846 append (funcall ac-match-function
1847 ac-prefix
1848 (and (local-variable-p 'ac-word-index buffer)
1849 (cdr (buffer-local-value 'ac-word-index buffer))))
1850 into candidates
1851 finally return candidates))
1852
1853 (ac-define-source words-in-buffer
1854 '((candidates . ac-word-candidates)))
1855
1856 (ac-define-source words-in-all-buffer
1857 '((init . ac-update-word-index)
1858 (candidates . ac-word-candidates)))
1859
1860 (ac-define-source words-in-same-mode-buffers
1861 '((init . ac-update-word-index)
1862 (candidates . (ac-word-candidates
1863 (lambda (buffer)
1864 (derived-mode-p (buffer-local-value 'major-mode buffer)))))))
1865
1866 ;; Lisp symbols source
1867 (defvar ac-symbols-cache nil)
1868 (ac-clear-variable-every-10-minutes 'ac-symbols-cache)
1869
1870 (defun ac-symbol-file (symbol type)
1871 (if (fboundp 'find-lisp-object-file-name)
1872 (find-lisp-object-file-name symbol type)
1873 (let ((file-name (with-no-warnings
1874 (describe-simplify-lib-file-name
1875 (symbol-file symbol type)))))
1876 (when (equal file-name "loaddefs.el")
1877 ;; Find the real def site of the preloaded object.
1878 (let ((location (condition-case nil
1879 (if (eq type 'defun)
1880 (find-function-search-for-symbol symbol nil
1881 "loaddefs.el")
1882 (find-variable-noselect symbol file-name))
1883 (error nil))))
1884 (when location
1885 (with-current-buffer (car location)
1886 (when (cdr location)
1887 (goto-char (cdr location)))
1888 (when (re-search-backward
1889 "^;;; Generated autoloads from \\(.*\\)" nil t)
1890 (setq file-name (match-string 1)))))))
1891 (if (and (null file-name)
1892 (or (eq type 'defun)
1893 (integerp (get symbol 'variable-documentation))))
1894 ;; It's a object not defined in Elisp but in C.
1895 (if (get-buffer " *DOC*")
1896 (if (eq type 'defun)
1897 (help-C-file-name (symbol-function symbol) 'subr)
1898 (help-C-file-name symbol 'var))
1899 'C-source)
1900 file-name))))
1901
1902 (defun ac-symbol-documentation (symbol)
1903 (if (stringp symbol)
1904 (setq symbol (intern-soft symbol)))
1905 (ignore-errors
1906 (with-temp-buffer
1907 (let ((standard-output (current-buffer)))
1908 (prin1 symbol)
1909 (princ " is ")
1910 (cond
1911 ((fboundp symbol)
1912 ;; import help-xref-following
1913 (require 'help-mode)
1914 (let ((help-xref-following t)
1915 (major-mode 'help-mode)) ; avoid error in Emacs 24
1916 (describe-function-1 symbol))
1917 (buffer-string))
1918 ((boundp symbol)
1919 (let ((file-name (ac-symbol-file symbol 'defvar)))
1920 (princ "a variable")
1921 (when file-name
1922 (princ " defined in `")
1923 (princ (if (eq file-name 'C-source)
1924 "C source code"
1925 (file-name-nondirectory file-name))))
1926 (princ "'.\n\n")
1927 (princ (or (documentation-property symbol 'variable-documentation t)
1928 "Not documented."))
1929 (buffer-string)))
1930 ((facep symbol)
1931 (let ((file-name (ac-symbol-file symbol 'defface)))
1932 (princ "a face")
1933 (when file-name
1934 (princ " defined in `")
1935 (princ (if (eq file-name 'C-source)
1936 "C source code"
1937 (file-name-nondirectory file-name))))
1938 (princ "'.\n\n")
1939 (princ (or (documentation-property symbol 'face-documentation t)
1940 "Not documented."))
1941 (buffer-string)))
1942 (t
1943 (let ((doc (documentation-property symbol 'group-documentation t)))
1944 (when doc
1945 (princ "a group.\n\n")
1946 (princ doc)
1947 (buffer-string)))))))))
1948
1949 (defun ac-symbol-candidates ()
1950 (or ac-symbols-cache
1951 (setq ac-symbols-cache
1952 (loop for x being the symbols
1953 if (or (fboundp x)
1954 (boundp x)
1955 (symbol-plist x))
1956 collect (symbol-name x)))))
1957
1958 (ac-define-source symbols
1959 '((candidates . ac-symbol-candidates)
1960 (document . ac-symbol-documentation)
1961 (symbol . "s")
1962 (cache)))
1963
1964 ;; Lisp functions source
1965 (defvar ac-functions-cache nil)
1966 (ac-clear-variable-every-10-minutes 'ac-functions-cache)
1967
1968 (defun ac-function-candidates ()
1969 (or ac-functions-cache
1970 (setq ac-functions-cache
1971 (loop for x being the symbols
1972 if (fboundp x)
1973 collect (symbol-name x)))))
1974
1975 (ac-define-source functions
1976 '((candidates . ac-function-candidates)
1977 (document . ac-symbol-documentation)
1978 (symbol . "f")
1979 (prefix . "(\\(\\(?:\\sw\\|\\s_\\)+\\)")
1980 (cache)))
1981
1982 ;; Lisp variables source
1983 (defvar ac-variables-cache nil)
1984 (ac-clear-variable-every-10-minutes 'ac-variables-cache)
1985
1986 (defun ac-variable-candidates ()
1987 (or ac-variables-cache
1988 (setq ac-variables-cache
1989 (loop for x being the symbols
1990 if (boundp x)
1991 collect (symbol-name x)))))
1992
1993 (ac-define-source variables
1994 '((candidates . ac-variable-candidates)
1995 (document . ac-symbol-documentation)
1996 (symbol . "v")
1997 (cache)))
1998
1999 ;; Lisp features source
2000 (defvar ac-emacs-lisp-features nil)
2001 (ac-clear-variable-every-10-minutes 'ac-emacs-lisp-features)
2002
2003 (defun ac-emacs-lisp-feature-candidates ()
2004 (or ac-emacs-lisp-features
2005 (if (fboundp 'find-library-suffixes)
2006 (let ((suffix (concat (regexp-opt (find-library-suffixes) t) "\\'")))
2007 (setq ac-emacs-lisp-features
2008 (append (mapcar 'prin1-to-string features)
2009 (loop for dir in load-path
2010 if (file-directory-p dir)
2011 append (loop for file in (directory-files dir)
2012 if (string-match suffix file)
2013 collect (substring file 0 (match-beginning 0))))))))))
2014
2015 (ac-define-source features
2016 '((depends find-func)
2017 (candidates . ac-emacs-lisp-feature-candidates)
2018 (prefix . "require +'\\(\\(?:\\sw\\|\\s_\\)*\\)")
2019 (requires . 0)))
2020
2021 (defvaralias 'ac-source-emacs-lisp-features 'ac-source-features)
2022
2023 ;; Abbrev source
2024 (ac-define-source abbrev
2025 '((candidates . (mapcar 'popup-x-to-string (append (vconcat local-abbrev-table global-abbrev-table) nil)))
2026 (action . expand-abbrev)
2027 (symbol . "a")
2028 (cache)))
2029
2030 ;; Files in current directory source
2031 (ac-define-source files-in-current-dir
2032 '((candidates . (directory-files default-directory))
2033 (cache)))
2034
2035 ;; Filename source
2036 (defvar ac-filename-cache nil)
2037
2038 (defun ac-filename-candidate ()
2039 (let (file-name-handler-alist)
2040 (unless (or (and comment-start-skip
2041 (string-match comment-start-skip ac-prefix))
2042 (file-regular-p ac-prefix))
2043 (ignore-errors
2044 (loop with dir = (file-name-directory ac-prefix)
2045 with files = (or (assoc-default dir ac-filename-cache)
2046 (let ((files (directory-files dir nil "^[^.]")))
2047 (push (cons dir files) ac-filename-cache)
2048 files))
2049 for file in files
2050 for path = (concat dir file)
2051 collect (if (file-directory-p path)
2052 (concat path "/")
2053 path))))))
2054
2055 (ac-define-source filename
2056 '((init . (setq ac-filename-cache nil))
2057 (candidates . ac-filename-candidate)
2058 (prefix . valid-file)
2059 (requires . 0)
2060 (action . ac-start)
2061 (limit . nil)))
2062
2063 ;; Dictionary source
2064 (ac-define-source dictionary
2065 '((candidates . ac-buffer-dictionary)
2066 (symbol . "d")))
2067
2068 (provide 'auto-complete)
2069 ;;; auto-complete.el ends here