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
|
; -*- geiser-scheme-implementation:chibi; -*-
(define (all-environment-exports environment prefix)
(if environment
(append (filter (lambda (identifier)
(if (string=? prefix "")
#t
(string-contains identifier prefix)))
(map symbol->string (env-exports environment)))
(all-environment-exports (env-parent environment) prefix))
'()))
(define (geiser:completions prefix . rest)
rest
(sort (all-environment-exports (current-environment) prefix)
string-ci<?))
(define (write-to-string form)
(let ((out (open-output-string)))
(write form out)
(get-output-string out)))
(define (geiser:write/ss-to-string form)
(let ((out (open-output-string)))
(write/ss form out)
(get-output-string out)))
;;> Evaluate a \var{form} in the namespace of the \var{module}.
;;> The meaning of \var{rest} is unknown.
;;> Return the alist with the first field, \scheme{result}, holds
;;> the result of evaluation \scheme{(write)}'d and the second
;;> field, \scheme{output}, contains everyting that the evaluation
;;> would print to the standard output.
(define (geiser:eval module form . rest)
rest
(guard (err (else (write `((result ,(show #f err))))))
(let* ((output (open-output-string))
(result (parameterize ((current-output-port output))
(if module
(let ((mod (module-env (find-module module))))
(eval form mod))
(eval form))
)
))
(write `((result ,(write-to-string result))
(output . ,(get-output-string output))))))
(values))
; (display "debug:Hello\n")
(define (geiser:module-completions prefix . rest)
;; (available-modules) walks the directory tree and is too slow
(let ((modules (map car *modules*)))
(map write-to-string
(delete-duplicates
(filter (lambda (module)
(if (string=? "" prefix)
#t
(string-contains prefix (write-to-string module))))
modules)))))
(define (procedure-arglist id fun)
(let ((arglist (lambda-params (procedure-analysis fun))))
(if (pair? arglist)
(let loop ((arglist arglist)
(optionals? #f)
(required '())
(optional '()))
(cond ((null? arglist)
`(,id ("args" (("required" ,@(reverse required))
("optional" ,@(reverse optional))
("key")
("module" ,(let ((mod (containing-module fun))) (if mod (car mod) #f)))))))
((symbol? arglist)
(loop '()
#t
required
(cons "..." (cons arglist optional))))
(else
(loop
(cdr arglist)
optionals?
(if optionals? required (cons (car arglist) required))
(if optionals? (cons (car arglist) optional) optional)))))
'())))
(define (geiser:operator-arglist id)
(let ((binding (eval id)))
(cond ((procedure? binding)
(if (opcode? binding)
'()
(procedure-arglist id binding)))
(else
'()))))
(define (geiser:autodoc ids . rest)
rest
(cond ((null? ids) '())
((not (list? ids))
(geiser:autodoc (list ids)))
((not (symbol? (car ids)))
(geiser:autodoc (cdr ids)))
(else
(map (lambda (id)
(geiser:operator-arglist id))
ids))))
(define (geiser:no-values)
#f)
(define (geiser:newline)
#f)
;;> A chibi implementation of the standard geiser's location-making
;;> subroutine. \var{file} is a string representing file name with path,
;;> \var{line} is the line number starting from 0 (scheme way).
(define (make-location file line)
(list (cons "file"
(if (string? file)
(path-resolve
file
(current-directory))
'()))
(cons "line" (if (number? line) (+ 1 line) '())))
)
;TODO: (define (geiser:symbol-location)
; implement this method in order to make
; xref work better in Chibi. For reference, see [[geiser:module-location]]
; (analyze-module (caar (modules-exporting-identifier 'symbol-in-question)))
;(module-ast (analyze-module (caar (modules-exporting-identifier 'ckind))))
(define (geiser:symbol-location symbol-in-question . rest)
(let* (
(result (tree-walker
(module-ast
(analyze-module
(caar
(modules-exporting-identifier
symbol-in-question))))
symbol-in-question))
(location
(make-location
(car result)
(- (cdr result) 1))) ; Ehh... line numbering in 'make-location starts from 0
)
location
)
)
(define (tree-walker node . symbol-in-question)
; The reason this function used a (let), not a (begin) is that (begin)
; for some reason does not allow (display)s inside. It
; works in xfce4-terminal, but not here. I decided not to
; debug it, since (do) "just worked". TODO.
(if (pair? node)
(let ((result
(tree-walker
(car node)
(car symbol-in-question)))
)
(if result
result
(tree-walker (cdr node) (car symbol-in-question))
)
)
(let () ; we have leaf
(if (set? node)
(if (equal? (ref-name (set-var node)) (car symbol-in-question))
(let ((thingy (set-value node)))
(if (lambda? thingy)
(lambda->lcons thingy)
(set-node->lcons/dirty-trick node)
)
)
#f
)
#f
)
)
)
)
(define (lambda->lcons thingy)
(let* ((l-source (lambda-source thingy))
(l-location
(cons
(car l-source)
(cdr l-source))))
l-location
))
(define (set-node->lcons/dirty-trick node)
(let* ((exam2 (geiser:write/ss-to-string node))
(strl (string-length exam2))
(l-matches
(regexp-search
'(: "(\""
(-> filename (*? graphic) )
"\" . "
(-> lineno (+ num) )
")}")
exam2 ))
(l-filename
(regexp-match-submatch l-matches 'filename))
(l-lineno
(string->number
(regexp-match-submatch l-matches 'lineno)))
(l-location
(cons l-filename l-lineno)))
l-location))
;(geiser:symbol-location 'run-application)
;;> A function to find the file where the symbol
;;> \var{symbol-representing-module} is defined.
(define (geiser:module-location symbol-representing-module)
(make-location
(find-module-file
(module-name->file
(module-name
(find-module symbol-representing-module))))
0 )
)
|