-
Notifications
You must be signed in to change notification settings - Fork 5
/
distel-completion-lib.el
258 lines (221 loc) · 8.98 KB
/
distel-completion-lib.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
;;; distel-completion-lib.el --- Completion library for Erlang/Distel
;; Copyright (C) 2012 Sebastian Weddmark Olsson
;; Author: Sebastian Weddmark Olsson
;; URL: github.com/sebastiw/distel-completion
;; Version: 1.0.0
;; Keywords: Erlang Distel completion
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Completion library for Erlang using Distel as backend. If you are interested
;; to see how it can be used you can checkout the packages
;; `auto-complete-distel' or `company-distel'
;; (github.com/sebastiw/distel-completion).
;;
;; This library can find documentation and do Distel request. Use it to build
;; new completion modules.
;;; Code:
(require 'distel)
(defcustom distel-completion-get-doc-from-internet t
"Try to find the documentation from erlang.org"
:group 'distel-completion)
(defcustom distel-completion-valid-syntax "a-zA-Z:_-"
"Which syntax to skip backwards to find start of word."
:group 'distel-completion)
;;;;;;;;;;;;;;;;;;;;;;;;;
;;; docs funs ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;
(defun distel-completion-get-doc-buffer (args)
"Returns a buffer with the documentation for ARGS."
(with-current-buffer (company-doc-buffer)
(insert (distel-completion-get-doc-string args))
(current-buffer)))
(defun distel-completion-get-doc-string (args)
"Returns the documentation string for ARGS."
(interactive)
(let* ((isok (string-match ":" args))
(mod (substring args 0 isok))
(fun (and isok (substring args (+ isok 1))))
(doc (and fun (distel-completion-local-docs mod fun)))
(edocs (when (and distel-completion-get-doc-from-internet
mod
(or (not doc) (string= doc "")))
(distel-completion-get-docs-from-internet-p mod fun)))
(met (and fun (erl-format-arglists (distel-completion-get-metadoc mod fun))))
(to-show (or (and (not (string= doc "")) doc)
(and (not (string= edocs "")) edocs)
(and met (concat mod ":" fun met))
(format "Couldn't find any help for %s." args))))
to-show))
(defun distel-completion-get-docs-from-internet-p (mod fun) ;; maybe version?
"Download the documentation from internet."
(let ((str
(with-current-buffer
(url-retrieve-synchronously (format "http://www.erlang.org/doc/man/%s.html" mod))
(goto-char (point-min))
;; find <p> containing <a name="`FUN'"> then
;; find <div class="REFBODY">
(let* ((m (re-search-forward (or (and fun (format "<p>.*?<a name=\"%s.*?\">" fun))
"<h3>DESCRIPTION</h3>") nil t))
(beg (and m (match-end 0)))
(end (and m (progn (re-search-forward "</p>.*?</div>" nil t)
(match-end 0)))))
(and beg end (buffer-substring beg end))))))
(and str
(distel-completion-html-to-string str))))
(defun distel-completion-html-to-string (string)
"Removes html-tags from `STRING'."
(let ((replaces '(("</?p>" . "\n")
("<br>" . "\n")
("<[^>]*>" . "")
("[[:space:]|\n]$" . "")
("^[[:space:]]+" . "")
("^\n[\n]+" . "")
(">" . ">")
("<" . "<"))))
(dolist (tagpair replaces string)
(setq string (replace-regexp-in-string (car tagpair) (cdr tagpair) string)))))
;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Distel funs ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;
(defun distel-completion-complete (search-string buf)
"Complete `search-string' as a external module or function in current buffer."
(let* (;; Checks if there exists a ":" in the search-string
(isok (string-match ":" search-string))
;; module (from start of string to ":")
(mod (and isok (substring search-string 0 isok)))
;; function (from ":" to end of string)
(fun (and isok (substring search-string (+ isok 1))))
;; node
(node erl-nodename-cache))
;; Let distel complete the function or module, depending if ":" is part of
;; the search-string
(if isok
(distel-completion-complete-function mod fun)
(distel-completion-complete-module search-string))
;; The call to distel is asynchronous, lets wait some
(sleep-for 0.1)
;; Add the module if needed
(mapcar (lambda (item) (concat mod (when mod ":") item))
distel-completion-try-erl-complete-cache)))
(defvar distel-completion-try-erl-args-cache '()
"Store the functions arguments.")
(defvar distel-completion-try-erl-desc-cache ""
"Store of the description.")
(defvar distel-completion-try-erl-complete-cache '()
"Completion candidates cache.")
(defun distel-completion-get-metadoc (mod fun)
"Get the arguments for a function."
(let ((node erl-nodename-cache))
(distel-completion-args mod fun))
(sleep-for 0.1)
distel-completion-try-erl-args-cache)
(defun distel-completion-local-docs (mod fun)
"Get local documentation for a function."
(distel-completion-get-metadoc mod fun)
(let ((node erl-nodename-cache))
(setq distel-completion-try-erl-desc-cache "")
(dolist (args distel-completion-try-erl-args-cache)
(distel-completion-describe mod fun args)))
(sleep-for 0.1)
distel-completion-try-erl-desc-cache)
(defun distel-completion-describe (mod fun args)
"Get the documentation of a function."
(erl-spawn
(erl-send-rpc node 'distel 'describe (list (intern mod)
(intern fun)
(length args)))
(&distel-completion-receive-describe args)))
(defun &distel-completion-receive-describe (args)
(erl-receive (args)
((['rex ['ok desc]]
(let ((descr (format "%s:%s/%s\n%s\n\n"
(elt (car desc) 0)
(elt (car desc) 1)
(elt (car desc) 2)
(elt (car desc) 3))))
(when desc
(setq distel-completion-try-erl-desc-cache
(concat descr distel-completion-try-erl-desc-cache)))))
(else
(message "fail: %s" else)))))
(defun distel-completion-args (mod fun)
"Find the arguments to a function `FUN' in a module `MOD'"
(erl-spawn
(erl-send-rpc node 'distel 'get_arglists (list mod fun))
(&distel-completion-receive-args)))
(defun &distel-completion-receive-args ()
(erl-receive ()
((['rex 'error])
(['rex docs]
(setq distel-completion-try-erl-args-cache docs))
(else
(message "fail: %s" else)
(setq distel-completion-try-erl-args-cache '())))))
(defun distel-completion-complete-module (module)
"Get list of modulenames starting with `MODULE'."
(erl-spawn
(erl-send-rpc node 'distel 'modules (list module))
(&distel-completion-receive-completions)))
(defun distel-completion-complete-function (module function)
"Get list of function names starting with `FUNCTION'"
(erl-spawn
(erl-send-rpc node 'distel 'functions (list module function))
(&distel-completion-receive-completions)))
(defun &distel-completion-receive-completions ()
(erl-receive ()
((['rex ['ok completions]]
(setq distel-completion-try-erl-complete-cache completions))
(other
(message "Unexpected reply: %s" other)))))
;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Local buffer funs ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;
(defun distel-completion-get-dabbrevs (args &optional times limit)
"Use `dabbrev' to find last used commands beginning with ARGS
in current buffer only. TIMES is how many times it will match,
defaults to 5. LIMIT is how far from point it should search,
defaults to the start of the previous defun."
(when (require 'dabbrev nil t)
(dabbrev--reset-global-variables)
(setq dabbrev-check-other-buffers nil
dabbrev--last-expansion-location (point))
(let ((times (or times 5))
(dabbrev-limit (or limit (- (point) (save-excursion
(backward-char (length args))
(backward-paragraph)
(point)))))
(ret-list '())
str)
(while (and (> times 0) (dabbrev--find-expansion args 1 nil)))
dabbrev--last-table)))
(defun distel-completion-get-functions (args)
"Get all function definitions beginning with ARGS in the current buffer."
(let ((case-fold-search nil)
(funs '())
fun-end)
(save-excursion
(goto-char (point-min))
;; Search for functions in the current-buffer starting with `args'
(while (setq fun-end (re-search-forward (format "^%s.*?(" args) nil t))
;; add found functions as completion candidates
(add-to-list 'funs (buffer-substring (match-beginning 0) (1- fun-end)))))
funs))
(defun distel-completion-grab-word ()
"Grab the current Erlang mod/fun/word."
(interactive)
(buffer-substring (point) (save-excursion
(skip-chars-backward distel-completion-valid-syntax)
(point))))
(defun distel-completion-is-comment-or-cite-p (&optional poin)
"Returns t if point is inside a comment or a cite."
(save-excursion
(let ((po (or poin (point))))
(goto-char po)
(beginning-of-line)
(re-search-forward "[%\|\"|\']" po t)
(or (eql (char-before) ?%)
(and (or (eql (char-before) ?\")
(eql (char-before) ?\'))
(not (re-search-forward "[\"\|\']" po t)))))))
(provide 'distel-completion-lib)
;;; distel-completion-lib.el ends here