flesh out (web server)'s sanitize-response
[bpt/guile.git] / guile-readline / ice-9 / readline.scm
CommitLineData
74c88f53
RB
1;;;; readline.scm --- support functions for command-line editing
2;;;;
adb825b6 3;;;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2006, 2009, 2010 Free Software Foundation, Inc.
74c88f53
RB
4;;;;
5;;;; This program is free software; you can redistribute it and/or modify
6;;;; it under the terms of the GNU General Public License as published by
b82a8b48 7;;;; the Free Software Foundation; either version 3, or (at your option)
74c88f53
RB
8;;;; any later version.
9;;;;
10;;;; This program is distributed in the hope that it will be useful,
11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13;;;; GNU General Public License for more details.
14;;;;
15;;;; You should have received a copy of the GNU General Public License
16;;;; along with this software; see the file COPYING. If not, write to
92205699
MV
17;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18;;;; Boston, MA 02110-1301 USA
74c88f53
RB
19;;;;
20;;;; Contributed by Daniel Risacher <risacher@worldnet.att.net>.
21;;;; Extensions based upon code by
22;;;; Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>.
23
24\f
25
26(define-module (ice-9 readline)
f84c500d
NJ
27 #:use-module (ice-9 session)
28 #:use-module (ice-9 regex)
29 #:use-module (ice-9 buffered-input)
30 #:no-backtrace
31 #:export (filename-completion-function
32 add-history
33 read-history
34 write-history
35 clear-history))
74c88f53
RB
36
37\f
38
39;;; Dynamically link the glue code for accessing the readline library,
40;;; but only when it isn't already present.
41
42(if (not (provided? 'readline))
6cecdff7 43 (load-extension "libguilereadline-v-18" "scm_init_readline"))
74c88f53
RB
44
45(if (not (provided? 'readline))
46 (scm-error 'misc-error
47 #f
48 "readline is not provided in this Guile installation"
49 '()
50 '()))
51
52\f
53
54;;; Run-time options
55
56(export
57 readline-options
58 readline-enable
59 readline-disable)
60(export-syntax
61 readline-set!)
62
63(define-option-interface
64 (readline-options-interface
65 (readline-options readline-enable readline-disable)
66 (readline-set!)))
67
68\f
69
70;;; MDJ 980513 <djurfeldt@nada.kth.se>:
71;;; There should probably be low-level support instead of this code.
72
73;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
74;;; guile will enter an endless loop or crash.
75
3bff1789
NJ
76(define new-input-prompt "")
77(define continuation-prompt "")
74c88f53
RB
78(define input-port (current-input-port))
79(define output-port (current-output-port))
80(define read-hook #f)
81
82(define (make-readline-port)
83 (make-line-buffered-input-port (lambda (continuation?)
84 (let* ((prompt (if continuation?
3bff1789
NJ
85 continuation-prompt
86 new-input-prompt))
74c88f53
RB
87 (str (%readline (if (string? prompt)
88 prompt
89 (prompt))
90 input-port
91 output-port
92 read-hook)))
93 (or (eof-object? str)
94 (string=? str "")
95 (add-history str))
96 str))))
97
98;;; We only create one readline port. There's no point in having
99;;; more, since they would all share the tty and history ---
100;;; everything except the prompt. And don't forget the
101;;; compile/load/run phase distinctions. Also, the readline library
102;;; isn't reentrant.
103(define the-readline-port #f)
104
105(define history-variable "GUILE_HISTORY")
106(define history-file (string-append (getenv "HOME") "/.guile_history"))
107
108(define-public readline-port
109 (let ((do (lambda (r/w)
110 (if (memq 'history-file (readline-options-interface))
111 (r/w (or (getenv history-variable)
112 history-file))))))
113 (lambda ()
114 (if (not the-readline-port)
115 (begin
116 (do read-history)
117 (set! the-readline-port (make-readline-port))
118 (add-hook! exit-hook (lambda ()
119 (do write-history)
120 (clear-history)))))
121 the-readline-port)))
122
123;;; The user might try to use readline in his programs. It then
124;;; becomes very uncomfortable that the current-input-port is the
125;;; readline port...
126;;;
127;;; Here, we detect this situation and replace it with the
128;;; underlying port.
129;;;
130;;; %readline is the low-level readline procedure.
131
132(define-public (readline . args)
3bff1789 133 (let ((prompt new-input-prompt)
74c88f53
RB
134 (inp input-port))
135 (cond ((not (null? args))
136 (set! prompt (car args))
137 (set! args (cdr args))
138 (cond ((not (null? args))
139 (set! inp (car args))
140 (set! args (cdr args))))))
141 (apply %readline
142 prompt
143 (if (eq? inp the-readline-port)
144 input-port
145 inp)
146 args)))
147
148(define-public (set-readline-prompt! p . rest)
3bff1789 149 (set! new-input-prompt p)
74c88f53 150 (if (not (null? rest))
3bff1789 151 (set! continuation-prompt (car rest))))
74c88f53
RB
152
153(define-public (set-readline-input-port! p)
154 (cond ((or (not (file-port? p)) (not (input-port? p)))
155 (scm-error 'wrong-type-arg "set-readline-input-port!"
156 "Not a file input port: ~S" (list p) #f))
157 ((port-closed? p)
158 (scm-error 'misc-error "set-readline-input-port!"
159 "Port not open: ~S" (list p) #f))
160 (else
161 (set! input-port p))))
162
163(define-public (set-readline-output-port! p)
164 (cond ((or (not (file-port? p)) (not (output-port? p)))
165 (scm-error 'wrong-type-arg "set-readline-input-port!"
166 "Not a file output port: ~S" (list p) #f))
167 ((port-closed? p)
168 (scm-error 'misc-error "set-readline-output-port!"
169 "Port not open: ~S" (list p) #f))
170 (else
171 (set! output-port p))))
172
173(define-public (set-readline-read-hook! h)
174 (set! read-hook h))
175
64e5d08d
AW
176(define-public apropos-completion-function
177 (let ((completions '()))
178 (lambda (text cont?)
179 (if (not cont?)
180 (set! completions
181 (map symbol->string
182 (apropos-internal
183 (string-append "^" (regexp-quote text))))))
184 (if (null? completions)
185 #f
186 (let ((retval (car completions)))
187 (begin (set! completions (cdr completions))
188 retval))))))
189
74c88f53 190(if (provided? 'regex)
64e5d08d 191 (set! *readline-completion-function* apropos-completion-function))
74c88f53
RB
192
193(define-public (with-readline-completion-function completer thunk)
194 "With @var{completer} as readline completion function, call @var{thunk}."
195 (let ((old-completer *readline-completion-function*))
196 (dynamic-wind
197 (lambda ()
198 (set! *readline-completion-function* completer))
199 thunk
200 (lambda ()
201 (set! *readline-completion-function* old-completer)))))
202
1924145d
AW
203(define readline-repl-reader
204 (let ((boot-9-repl-reader repl-reader))
205 (lambda* (repl-prompt #:optional (reader (fluid-ref current-reader)))
206 (let ((port (current-input-port)))
207 (if (eq? port (readline-port))
208 (let ((outer-new-input-prompt new-input-prompt)
209 (outer-continuation-prompt continuation-prompt)
210 (outer-read-hook read-hook))
211 (dynamic-wind
212 (lambda ()
213 (set-buffered-input-continuation?! port #f)
214 (set-readline-prompt! repl-prompt "... ")
215 (set-readline-read-hook! (lambda ()
216 (run-hook before-read-hook))))
217 (lambda () ((or reader read) port))
218 (lambda ()
219 (set-readline-prompt! outer-new-input-prompt
220 outer-continuation-prompt)
221 (set-readline-read-hook! outer-read-hook))))
222 (boot-9-repl-reader repl-prompt reader))))))
223
74c88f53 224(define-public (activate-readline)
adb825b6 225 (if (isatty? (current-input-port))
1924145d
AW
226 (begin
227 (set-current-input-port (readline-port))
228 (set! repl-reader readline-repl-reader)
229 (set! (using-readline?) #t))))
1b09b607
KR
230
231(define-public (make-completion-function strings)
232 "Construct and return a completion function for a list of strings.
233The returned function is suitable for passing to
234@code{with-readline-completion-function. The argument @var{strings}
235should be a list of strings, where each string is one of the possible
236completions."
237 (letrec ((strs '())
238 (regexp #f)
239 (completer (lambda (text continue?)
240 (if continue?
241 (if (null? strs)
242 #f
243 (let ((str (car strs)))
244 (set! strs (cdr strs))
245 (if (string-match regexp str)
246 str
247 (completer text #t))))
248 (begin
249 (set! strs strings)
250 (set! regexp
251 (string-append "^" (regexp-quote text)))
252 (completer text #t))))))
253 completer))