ruby_learner 1.0.0

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