blob: 478b9afca3d12a7446b0802cd79c5bc931cfdd5c (
plain)
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
|
(library (geiser)
(export geiser:eval
geiser:completions
geiser:module-completions
geiser:autodoc
geiser:no-values
geiser:load-file
geiser:newline
geiser:macroexpand)
(import (chezscheme))
(define (last-index-of str-list char idx last-idx)
(if (null? str-list)
last-idx
(last-index-of (cdr str-list) char (+ 1 idx) (if (char=? char (car str-list)) idx last-idx))))
(define (obj-file-name name)
(let ((idx (last-index-of (string->list name) #\. 0 -1)))
(if (= idx -1)
(string-append name ".so")
(string-append (substring name 0 idx) ".so"))))
(define (geiser:load-file filename)
(let ((output-filename (obj-file-name filename)))
(maybe-compile-file filename output-filename)
(load output-filename)))
(define string-prefix?
(lambda (x y)
(let ([n (string-length x)])
(and (fx<= n (string-length y))
(let prefix? ([i 0])
(or (fx= i n)
(and (char=? (string-ref x i) (string-ref y i))
(prefix? (fx+ i 1)))))))))
(define (geiser:completions prefix . rest)
rest
(sort string-ci<?
(filter (lambda (el)
(string-prefix? prefix el))
(map write-to-string (environment-symbols (interaction-environment))))))
(define (write-to-string x)
(with-output-to-string
(lambda ()
(write x))))
(define (geiser:eval module form . rest)
rest
(let ((output-string (open-output-string)))
(write
(call/cc
(lambda (k)
(with-exception-handler
(lambda (e)
(k `((result "")
(output . ,(with-output-to-string
(lambda ()
(display-condition e))))
(error (key . chez-error-message)))))
(lambda ()
(call-with-values
;; evaluate form, allow for multiple return values,
;; and capture output in output-string.
(lambda ()
(parameterize ((current-output-port output-string))
(if module
(eval form (environment module))
(eval form))))
(lambda result
`((result ,(with-output-to-string
(lambda ()
(pretty-print
(if (null? (cdr result)) (car result) result)))))
(output . ,(get-output-string output-string))))))))))
(newline)
(close-output-port output-string)))
(define (geiser:module-completions prefix . rest)
(define (substring? s1 s2)
(let ([n1 (string-length s1)] [n2 (string-length s2)])
(let loop2 ([i2 0])
(let loop1 ([i1 0] [j i2])
(if (fx= i1 n1)
i2
(and (not (fx= j n2))
(if (char=? (string-ref s1 i1) (string-ref s2 j))
(loop1 (fx+ i1 1) (fx+ j 1))
(loop2 (fx+ i2 1)))))))))
(filter (lambda (el)
(substring? prefix el))
(map write-to-string (library-list))))
(define (procedure-parameter-list p)
;; same as (inspect object), then hitting c
(let ((s (((inspect/object p) 'code) 'source)))
(if s
(let ((form (s 'value)))
(if (and (list? form)
(> (length form) 2))
(case (car form)
[(lambda) (list (cadr form))]
[(case-lambda) (map car (cdr form))]
[else #f])
#f))
#f)))
(define (operator-arglist operator)
(define (make-autodoc-arglist arglist)
(let loop ([arglist arglist]
[optionals? #f]
[required '()]
[optional '()])
(cond ((null? arglist)
`(("required" ,@(reverse required))
("optional" ,@(reverse optional))
("key")
;; ("module" ,module)
))
((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))))))
(let ([binding (eval operator)])
(if binding
(let ([arglists (procedure-parameter-list binding)])
`(,operator ("args" ,@(map make-autodoc-arglist arglists))))
'())))
(define (geiser:autodoc ids . rest)
(cond ((null? ids) '())
((not (list? ids))
(geiser:autodoc (list ids)))
((not (symbol? (car ids)))
(geiser:autodoc (cdr ids)))
(else
(map (lambda (id)
(operator-arglist id))
ids))))
(define (geiser:no-values)
#f)
(define (geiser:newline)
#f)
(define (geiser:macroexpand form . rest)
(with-output-to-string
(lambda ()
(pretty-print
(syntax->datum (expand form)))))))
|