Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
marvel
GitHub Repository: marvel/qnf
Path: blob/master/elisp/auto-complete-config.el
987 views
1
;;; auto-complete-config.el --- auto-complete additional configuations
2
3
;; Copyright (C) 2009, 2010 Tomohiro Matsuyama
4
5
;; Author: Tomohiro Matsuyama <[email protected]>
6
;; Keywords: convenience
7
;; Version: 1.3
8
9
;; This program is free software; you can redistribute it and/or modify
10
;; it under the terms of the GNU General Public License as published by
11
;; the Free Software Foundation, either version 3 of the License, or
12
;; (at your option) any later version.
13
14
;; This program is distributed in the hope that it will be useful,
15
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
;; GNU General Public License for more details.
18
19
;; You should have received a copy of the GNU General Public License
20
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22
;;; Commentary:
23
24
;;
25
26
;;; Code:
27
28
(eval-when-compile
29
(require 'cl))
30
31
(require 'auto-complete)
32
33
34
35
;;;; Additional sources
36
37
;; imenu
38
39
(defvar ac-imenu-index nil)
40
41
(ac-clear-variable-every-10-minutes 'ac-imenu-index)
42
43
(defun ac-imenu-candidates ()
44
(loop with i = 0
45
with stack = (progn
46
(unless (local-variable-p 'ac-imenu-index)
47
(make-local-variable 'ac-imenu-index))
48
(or ac-imenu-index
49
(setq ac-imenu-index
50
(ignore-errors
51
(with-no-warnings
52
(imenu--make-index-alist))))))
53
with result
54
while (and stack (or (not (integerp ac-limit))
55
(< i ac-limit)))
56
for node = (pop stack)
57
if (consp node)
58
do
59
(let ((car (car node))
60
(cdr (cdr node)))
61
(if (consp cdr)
62
(mapc (lambda (child)
63
(push child stack))
64
cdr)
65
(when (and (stringp car)
66
(string-match (concat "^" (regexp-quote ac-prefix)) car))
67
;; Remove extra characters
68
(if (string-match "^.*\\(()\\|=\\|<>\\)$" car)
69
(setq car (substring car 0 (match-beginning 1))))
70
(push car result)
71
(incf i))))
72
finally return (nreverse result)))
73
74
(ac-define-source imenu
75
'((depends imenu)
76
(candidates . ac-imenu-candidates)
77
(symbol . "s")))
78
79
;; gtags
80
81
(defface ac-gtags-candidate-face
82
'((t (:background "lightgray" :foreground "navy")))
83
"Face for gtags candidate"
84
:group 'auto-complete)
85
86
(defface ac-gtags-selection-face
87
'((t (:background "navy" :foreground "white")))
88
"Face for the gtags selected candidate."
89
:group 'auto-complete)
90
91
(defun ac-gtags-candidate ()
92
(ignore-errors
93
(split-string (shell-command-to-string (format "global -ci %s" ac-prefix)) "\n")))
94
95
(ac-define-source gtags
96
'((candidates . ac-gtags-candidate)
97
(candidate-face . ac-gtags-candidate-face)
98
(selection-face . ac-gtags-selection-face)
99
(requires . 3)
100
(symbol . "s")))
101
102
;; yasnippet
103
104
(defface ac-yasnippet-candidate-face
105
'((t (:background "sandybrown" :foreground "black")))
106
"Face for yasnippet candidate."
107
:group 'auto-complete)
108
109
(defface ac-yasnippet-selection-face
110
'((t (:background "coral3" :foreground "white")))
111
"Face for the yasnippet selected candidate."
112
:group 'auto-complete)
113
114
(defun ac-yasnippet-table-hash (table)
115
(cond
116
((fboundp 'yas/snippet-table-hash)
117
(yas/snippet-table-hash table))
118
((fboundp 'yas/table-hash)
119
(yas/table-hash table))))
120
121
(defun ac-yasnippet-table-parent (table)
122
(cond
123
((fboundp 'yas/snippet-table-parent)
124
(yas/snippet-table-parent table))
125
((fboundp 'yas/table-parent)
126
(yas/table-parent table))))
127
128
(defun ac-yasnippet-candidate-1 (table)
129
(with-no-warnings
130
(let ((hashtab (ac-yasnippet-table-hash table))
131
(parent (ac-yasnippet-table-parent table))
132
candidates)
133
(maphash (lambda (key value)
134
(push key candidates))
135
hashtab)
136
(setq candidates (all-completions ac-prefix (nreverse candidates)))
137
(if parent
138
(setq candidates
139
(append candidates (ac-yasnippet-candidate-1 parent))))
140
candidates)))
141
142
(defun ac-yasnippet-candidates ()
143
(with-no-warnings
144
(if (fboundp 'yas/get-snippet-tables)
145
;; >0.6.0
146
(apply 'append (mapcar 'ac-yasnippet-candidate-1 (yas/get-snippet-tables major-mode)))
147
(let ((table
148
(if (fboundp 'yas/snippet-table)
149
;; <0.6.0
150
(yas/snippet-table major-mode)
151
;; 0.6.0
152
(yas/current-snippet-table))))
153
(if table
154
(ac-yasnippet-candidate-1 table))))))
155
156
(ac-define-source yasnippet
157
'((depends yasnippet)
158
(candidates . ac-yasnippet-candidates)
159
(action . yas/expand)
160
(candidate-face . ac-yasnippet-candidate-face)
161
(selection-face . ac-yasnippet-selection-face)
162
(symbol . "a")))
163
164
;; semantic
165
166
(defun ac-semantic-candidates (prefix)
167
(with-no-warnings
168
(delete "" ; semantic sometimes returns an empty string
169
(mapcar 'semantic-tag-name
170
(ignore-errors
171
(or (semantic-analyze-possible-completions
172
(semantic-analyze-current-context))
173
(senator-find-tag-for-completion prefix)))))))
174
175
(ac-define-source semantic
176
'((available . (or (require 'semantic-ia nil t)
177
(require 'semantic/ia nil t)))
178
(candidates . (ac-semantic-candidates ac-prefix))
179
(prefix . c-dot-ref)
180
(requires . 0)
181
(symbol . "m")))
182
183
(ac-define-source semantic-raw
184
'((available . (or (require 'semantic-ia nil t)
185
(require 'semantic/ia nil t)))
186
(candidates . (ac-semantic-candidates ac-prefix))
187
(symbol . "s")))
188
189
;; eclim
190
191
(defun ac-eclim-candidates ()
192
(with-no-warnings
193
(loop for c in (eclim/java-complete)
194
collect (nth 1 c))))
195
196
(ac-define-source eclim
197
'((candidates . ac-eclim-candidates)
198
(prefix . c-dot)
199
(requires . 0)
200
(symbol . "f")))
201
202
;; css
203
204
;; Copied from company-css.el
205
(defconst ac-css-property-alist
206
;; see http://www.w3.org/TR/CSS21/propidx.html
207
'(("azimuth" angle "left-side" "far-left" "left" "center-left" "center"
208
"center-right" "right" "far-right" "right-side" "behind" "leftwards"
209
"rightwards")
210
("background" background-color background-image background-repeat
211
background-attachment background-position)
212
("background-attachment" "scroll" "fixed")
213
("background-color" color "transparent")
214
("background-image" uri "none")
215
("background-position" percentage length "left" "center" "right" percentage
216
length "top" "center" "bottom" "left" "center" "right" "top" "center"
217
"bottom")
218
("background-repeat" "repeat" "repeat-x" "repeat-y" "no-repeat")
219
("border" border-width border-style border-color)
220
("border-bottom" border)
221
("border-bottom-color" border-color)
222
("border-bottom-style" border-style)
223
("border-bottom-width" border-width)
224
("border-collapse" "collapse" "separate")
225
("border-color" color "transparent")
226
("border-left" border)
227
("border-left-color" border-color)
228
("border-left-style" border-style)
229
("border-left-width" border-width)
230
("border-right" border)
231
("border-right-color" border-color)
232
("border-right-style" border-style)
233
("border-right-width" border-width)
234
("border-spacing" length length)
235
("border-style" border-style)
236
("border-top" border)
237
("border-top-color" border-color)
238
("border-top-style" border-style)
239
("border-top-width" border-width)
240
("border-width" border-width)
241
("bottom" length percentage "auto")
242
("caption-side" "top" "bottom")
243
("clear" "none" "left" "right" "both")
244
("clip" shape "auto")
245
("color" color)
246
("content" "normal" "none" string uri counter "attr()" "open-quote"
247
"close-quote" "no-open-quote" "no-close-quote")
248
("counter-increment" identifier integer "none")
249
("counter-reset" identifier integer "none")
250
("cue" cue-before cue-after)
251
("cue-after" uri "none")
252
("cue-before" uri "none")
253
("cursor" uri "*" "auto" "crosshair" "default" "pointer" "move" "e-resize"
254
"ne-resize" "nw-resize" "n-resize" "se-resize" "sw-resize" "s-resize"
255
"w-resize" "text" "wait" "help" "progress")
256
("direction" "ltr" "rtl")
257
("display" "inline" "block" "list-item" "run-in" "inline-block" "table"
258
"inline-table" "table-row-group" "table-header-group" "table-footer-group"
259
"table-row" "table-column-group" "table-column" "table-cell"
260
"table-caption" "none")
261
("elevation" angle "below" "level" "above" "higher" "lower")
262
("empty-cells" "show" "hide")
263
("float" "left" "right" "none")
264
("font" font-style font-variant font-weight font-size "/" line-height
265
font-family "caption" "icon" "menu" "message-box" "small-caption"
266
"status-bar")
267
("font-family" family-name generic-family)
268
("font-size" absolute-size relative-size length percentage)
269
("font-style" "normal" "italic" "oblique")
270
("font-variant" "normal" "small-caps")
271
("font-weight" "normal" "bold" "bolder" "lighter" "100" "200" "300" "400"
272
"500" "600" "700" "800" "900")
273
("height" length percentage "auto")
274
("left" length percentage "auto")
275
("letter-spacing" "normal" length)
276
("line-height" "normal" number length percentage)
277
("list-style" list-style-type list-style-position list-style-image)
278
("list-style-image" uri "none")
279
("list-style-position" "inside" "outside")
280
("list-style-type" "disc" "circle" "square" "decimal" "decimal-leading-zero"
281
"lower-roman" "upper-roman" "lower-greek" "lower-latin" "upper-latin"
282
"armenian" "georgian" "lower-alpha" "upper-alpha" "none")
283
("margin" margin-width)
284
("margin-bottom" margin-width)
285
("margin-left" margin-width)
286
("margin-right" margin-width)
287
("margin-top" margin-width)
288
("max-height" length percentage "none")
289
("max-width" length percentage "none")
290
("min-height" length percentage)
291
("min-width" length percentage)
292
("orphans" integer)
293
("outline" outline-color outline-style outline-width)
294
("outline-color" color "invert")
295
("outline-style" border-style)
296
("outline-width" border-width)
297
("overflow" "visible" "hidden" "scroll" "auto")
298
("padding" padding-width)
299
("padding-bottom" padding-width)
300
("padding-left" padding-width)
301
("padding-right" padding-width)
302
("padding-top" padding-width)
303
("page-break-after" "auto" "always" "avoid" "left" "right")
304
("page-break-before" "auto" "always" "avoid" "left" "right")
305
("page-break-inside" "avoid" "auto")
306
("pause" time percentage)
307
("pause-after" time percentage)
308
("pause-before" time percentage)
309
("pitch" frequency "x-low" "low" "medium" "high" "x-high")
310
("pitch-range" number)
311
("play-during" uri "mix" "repeat" "auto" "none")
312
("position" "static" "relative" "absolute" "fixed")
313
("quotes" string string "none")
314
("richness" number)
315
("right" length percentage "auto")
316
("speak" "normal" "none" "spell-out")
317
("speak-header" "once" "always")
318
("speak-numeral" "digits" "continuous")
319
("speak-punctuation" "code" "none")
320
("speech-rate" number "x-slow" "slow" "medium" "fast" "x-fast" "faster"
321
"slower")
322
("stress" number)
323
("table-layout" "auto" "fixed")
324
("text-align" "left" "right" "center" "justify")
325
("text-decoration" "none" "underline" "overline" "line-through" "blink")
326
("text-indent" length percentage)
327
("text-transform" "capitalize" "uppercase" "lowercase" "none")
328
("top" length percentage "auto")
329
("unicode-bidi" "normal" "embed" "bidi-override")
330
("vertical-align" "baseline" "sub" "super" "top" "text-top" "middle"
331
"bottom" "text-bottom" percentage length)
332
("visibility" "visible" "hidden" "collapse")
333
("voice-family" specific-voice generic-voice "*" specific-voice
334
generic-voice)
335
("volume" number percentage "silent" "x-soft" "soft" "medium" "loud"
336
"x-loud")
337
("white-space" "normal" "pre" "nowrap" "pre-wrap" "pre-line")
338
("widows" integer)
339
("width" length percentage "auto")
340
("word-spacing" "normal" length)
341
("z-index" "auto" integer))
342
"A list of CSS properties and their possible values.")
343
344
(defconst ac-css-value-classes
345
'((absolute-size "xx-small" "x-small" "small" "medium" "large" "x-large"
346
"xx-large")
347
(border-style "none" "hidden" "dotted" "dashed" "solid" "double" "groove"
348
"ridge" "inset" "outset")
349
(color "aqua" "black" "blue" "fuchsia" "gray" "green" "lime" "maroon" "navy"
350
"olive" "orange" "purple" "red" "silver" "teal" "white" "yellow"
351
"rgb")
352
(counter "counter")
353
(family-name "Courier" "Helvetica" "Times")
354
(generic-family "serif" "sans-serif" "cursive" "fantasy" "monospace")
355
(generic-voice "male" "female" "child")
356
(margin-width "auto") ;; length percentage
357
(relative-size "larger" "smaller")
358
(shape "rect")
359
(uri "url"))
360
"A list of CSS property value classes and their contents.")
361
362
(defconst ac-css-pseudo-classes
363
'("active" "after" "before" "first" "first-child" "first-letter" "first-line"
364
"focus" "hover" "lang" "left" "link" "right" "visited")
365
"Identifiers for CSS pseudo-elements and pseudo-classes.")
366
367
(defvar ac-css-property nil
368
"Current editing property.")
369
370
(defun ac-css-prefix ()
371
(when (save-excursion (re-search-backward "\\_<\\(.+?\\)\\_>\\s *:.*\\=" nil t))
372
(setq ac-css-property (match-string 1))
373
(or (ac-prefix-symbol) (point))))
374
375
(defun ac-css-property-candidates ()
376
(or (loop with list = (assoc-default ac-css-property ac-css-property-alist)
377
with value
378
while (setq value (pop list))
379
if (symbolp value)
380
do (setq list
381
(append list
382
(or (assoc-default value ac-css-value-classes)
383
(assoc-default (symbol-name value) ac-css-property-alist))))
384
else collect value)
385
ac-css-pseudo-classes))
386
387
(defvar ac-source-css-property
388
'((candidates . ac-css-property-candidates)
389
(prefix . ac-css-prefix)
390
(requires . 0)))
391
392
393
394
;;;; Not maintained sources
395
396
;; ropemacs
397
398
(defvar ac-ropemacs-loaded nil)
399
(defun ac-ropemacs-require ()
400
(with-no-warnings
401
(unless ac-ropemacs-loaded
402
(pymacs-load "ropemacs" "rope-")
403
(if (boundp 'ropemacs-enable-autoimport)
404
(setq ropemacs-enable-autoimport t))
405
(setq ac-ropemacs-loaded t))))
406
407
(defun ac-ropemacs-setup ()
408
(ac-ropemacs-require)
409
;(setq ac-sources (append (list 'ac-source-ropemacs) ac-sources))
410
(setq ac-omni-completion-sources '(("\\." ac-source-ropemacs))))
411
412
(defun ac-ropemacs-initialize ()
413
(autoload 'pymacs-apply "pymacs")
414
(autoload 'pymacs-call "pymacs")
415
(autoload 'pymacs-eval "pymacs" nil t)
416
(autoload 'pymacs-exec "pymacs" nil t)
417
(autoload 'pymacs-load "pymacs" nil t)
418
(add-hook 'python-mode-hook 'ac-ropemacs-setup)
419
t)
420
421
(defvar ac-ropemacs-completions-cache nil)
422
(defvar ac-source-ropemacs
423
'((init
424
. (lambda ()
425
(setq ac-ropemacs-completions-cache
426
(mapcar
427
(lambda (completion)
428
(concat ac-prefix completion))
429
(ignore-errors
430
(rope-completions))))))
431
(candidates . ac-ropemacs-completions-cache)))
432
433
;; rcodetools
434
435
(defvar ac-source-rcodetools
436
'((init . (lambda ()
437
(require 'rcodetools)
438
(condition-case x
439
(save-excursion
440
(rct-exec-and-eval rct-complete-command-name "--completion-emacs-icicles"))
441
(error) (setq rct-method-completion-table nil))))
442
(candidates . (lambda ()
443
(all-completions
444
ac-prefix
445
(mapcar
446
(lambda (completion)
447
(replace-regexp-in-string "\t.*$" "" (car completion)))
448
rct-method-completion-table))))))
449
450
451
452
;;;; Default settings
453
454
(defun ac-common-setup ()
455
(add-to-list 'ac-sources 'ac-source-filename))
456
457
(defun ac-emacs-lisp-mode-setup ()
458
(setq ac-sources (append '(ac-source-features ac-source-functions ac-source-yasnippet ac-source-variables ac-source-symbols) ac-sources)))
459
460
(defun ac-cc-mode-setup ()
461
(setq ac-sources (append '(ac-source-yasnippet ac-source-gtags) ac-sources)))
462
463
(defun ac-ruby-mode-setup ()
464
(make-local-variable 'ac-ignores)
465
(add-to-list 'ac-ignores "end"))
466
467
(defun ac-css-mode-setup ()
468
(setq ac-sources (append '(ac-source-css-property) ac-sources)))
469
470
(defun ac-config-default ()
471
(setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
472
(add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
473
(add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
474
(add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)
475
(add-hook 'css-mode-hook 'ac-css-mode-setup)
476
(add-hook 'auto-complete-mode-hook 'ac-common-setup)
477
(global-auto-complete-mode t))
478
479
(provide 'auto-complete-config)
480
;;; auto-complete-config.el ends here
481
482