-
Notifications
You must be signed in to change notification settings - Fork 1
/
ca2+sources.el
252 lines (208 loc) · 8.05 KB
/
ca2+sources.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
;;;;;;;;;;;;;;;;;;;; CompleteAnything^2+ sources ;;;;;;;;;;;;;;;;;;;;;;
;;
;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;; based on company-mode.el, auto-complete.el and completion methods
;; found on emacswiki
;; Simple Example Source
;; (defvar ca-source-lisp
;; '((name . "elisp")
;; (candidates . '(lambda (prefix)
;; (all-completions
;; prefix obarray)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; dabbrev ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar ca-source-dabbrev-candidates-max 40)
;; taken from emacswiki CompletionUI
(defun ca-source-dabbrev-candidates (prefix)
"A wrapper for dabbrev that returns a list of expansion of
PREFIX ordered in the same way dabbrev-expand find expansions.
First, expansions from the current point and up to the beginning
of the buffer is listed. Second, the expansions from the current
point and down to the bottom of the buffer is listed. Last,
expansions in other buffer are listed top-down. The returned
list has at most MAXNUM elements."
(dabbrev--reset-global-variables)
(let ((all-expansions nil)
(i 0)
(j 0)
(ignore-case nil)
expansion)
;; Search backward until we hit another buffer or reach max num
(save-excursion
(while (and (< i (/ ca-source-dabbrev-candidates-max 2))
(setq expansion
(dabbrev--find-expansion prefix 1 ignore-case))
(not dabbrev--last-buffer))
(setq all-expansions (nconc all-expansions (list expansion)))
(setq i (+ i 1))))
;; If last expansion was found in another buffer, remove of it from the
;; dabbrev-internal list of found expansions so we can find it when we
;; are supposed to search other buffers.
(when (and expansion dabbrev--last-buffer)
(setq dabbrev--last-table (delete expansion dabbrev--last-table)))
;; Reset to prepeare for a new search
(let ((table dabbrev--last-table))
(dabbrev--reset-global-variables)
(setq dabbrev--last-table table))
;; Search forward in current buffer and after that in other buffers
(save-excursion
(while (and (< j (/ ca-source-dabbrev-candidates-max 2))
(setq expansion
(dabbrev--find-expansion prefix -1 ignore-case)))
(setq all-expansions (nconc all-expansions (list expansion)))
(setq j (+ i j))))
all-expansions))
(defvar ca-source-dabbrev
'((candidates . ca-source-dabbrev-candidates)
(limit . 1)
(sorted . t)
(name . "dabbrev"))
"ca2+ dabbrev source")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; filename ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; added '+'
(setq thing-at-point-file-name-chars "-~/[:alnum:]_.${}#+%,:")
(defun ca-source-filename-candidates (prefix)
(let (strip-dash)
(unless (string-match "\\(\./\\|\.\./\\|~/\\)" prefix)
(setq strip-dash t)
(setq prefix (concat "./" prefix)))
(let ((dir (file-name-directory prefix)))
(ignore-errors
(mapcar (lambda (file)
(concat (if strip-dash (substring dir 2) dir) file))
(remove-if (lambda (file) (or (equal file "../")
(equal file "./")))
(file-name-all-completions
(file-name-nondirectory prefix) dir)))))))
(defvar ca-source-filename
'((candidates . ca-source-filename-candidates)
(decider . filename) ;; TODO use function that checks content of
;; current dir for name completion without ./
(limit . 1) ;; minimum prefix length to find completion
(separator . "/") ;; truncate candidates shown in popup
;; before last position of separator
(continue . ca-source-filename-candidates) ;; find new completions
;;(sorted . t)
(name . "filename"))
"ca2+ filename source")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; lisp symbols ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ca-source-lisp-candidates (prefix)
(delete-if-not
'(lambda (cand)
(or (boundp (intern cand))
(fboundp (intern cand))))
(all-completions (regexp-quote prefix) obarray)))
(defun ca-source-lisp-describe (candidate)
(let ((symbol (intern candidate)))
(cond ((fboundp symbol)
(describe-function symbol))
((symbolp symbol)
(describe-variable symbol))
(t
(message "no description")))))
(defvar ca-source-lisp
'((name . "elisp")
(candidates . ca-source-lisp-candidates)
(limit . 1)
(describe . ca-source-lisp-describe)
(sorted . t)
;;(separator . "-") ;; use this to strip common-prefix from tooltip
(sort-by-occurrence . t)
(common-prefix . t)) ;; candidates have common prefixes,
;; this is used to reduce the number
;; of visible candidates, instead
;; the prefixes are shown.
"ca2+ lisp symbol source")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; gtags ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; from auto-complete-extension.el, by Andy Stewart
(defun ca-source-gtags-candidates (prefix)
(all-completions prefix
(let ((option "-c")
all-expansions
expansion)
(with-temp-buffer
(call-process "global" nil t nil option prefix)
(goto-char (point-min))
(while (looking-at gtags-symbol-regexp)
(setq expansion (gtags-match-string 0))
(setq all-expansions (cons expansion all-expansions))
(forward-line)))
all-expansions)))
(defvar ca-source-gtags
'((candidates . ca-source-gtags-candidates)
(limit . 1)
(sorted . nil)
(sort-by-occurrence . t)
(common-prefix . t)
(name . "gtags"))
"ca2+ gtags source")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; yasnippet ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; taken from auto-complete.el
(defun ca-source-yasnippet-candidates-1 (table)
(let ((hashtab (yas/snippet-table-hash table))
(parent (yas/snippet-table-parent table))
(regex (concat "^" prefix))
cands)
(maphash (lambda (key value)
(if (string-match regex key)
(push (cons key (yas/template-name (cdar value)))
cands)))
hashtab)
(if parent
(append cands (ca-source-yasnippet-candidates-1 parent))
cands)))
(defun ca-source-yasnippet-candidates (prefix)
(let ((table (yas/snippet-table major-mode)))
(if table
(ca-source-yasnippet-candidates-1 table))))
(defvar ca-source-yasnippet
'((candidates . ca-source-yasnippet-candidates)
(decider . word)
(action . (lambda(candidate) (yas/expand)))
(limit . 1)
(sorted . t)
(name . "yasnippet"))
"ca2+ yasnippet source")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; python rope ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ca-source-rope-candidates (prefix)
(let (cands)
(dolist (cand (rope-completions))
(setq cands (cons (format "%s%s" prefix cand) cands)))
cands))
(defun ca-source-rope-decider ()
(let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))
(if (null symbol)
(when (looking-back "\.")
(point))
symbol)))
(defun ca-source-rope-continue (candidate)
(setq ca-prefix "")
(setq ca-initial-prefix "")
(insert ".")
(let ((cands (ca-source-rope-candidates "")))
(or (nreverse cands)
(delete-char -1))))
(defvar ca-source-python-rope
'((candidates . ca-source-rope-candidates)
(decider . ca-source-rope-decider)
(continue . ca-source-rope-continue)
(filter . t)
(sorted . nil)
(sort-by-occurrence . t)
(common-prefix . t)
(name . "python"))
"ca2+ rope powered python source")
(provide 'ca2+sources)