repl-reader accepts optional "read" argument
[bpt/guile.git] / guile-readline / ice-9 / readline.scm
1 ;;;; readline.scm --- support functions for command-line editing
2 ;;;;
3 ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2006, 2009 Free Software Foundation, Inc.
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
7 ;;;; the Free Software Foundation; either version 3, or (at your option)
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
17 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;;;; Boston, MA 02110-1301 USA
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)
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))
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))
43 (load-extension "libguilereadline-v-18" "scm_init_readline"))
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
76 (define new-input-prompt "")
77 (define continuation-prompt "")
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?
85 continuation-prompt
86 new-input-prompt))
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)
133 (let ((prompt new-input-prompt)
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)
149 (set! new-input-prompt p)
150 (if (not (null? rest))
151 (set! continuation-prompt (car rest))))
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
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
190 (if (provided? 'regex)
191 (set! *readline-completion-function* apropos-completion-function))
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
203 (define-public (activate-readline)
204 (if (and (isatty? (current-input-port))
205 (not (let ((guile-user-module (resolve-module '(guile-user))))
206 (and (module-defined? guile-user-module 'use-emacs-interface)
207 (module-ref guile-user-module 'use-emacs-interface)))))
208 (let ((repl-read-hook (lambda () (run-hook before-read-hook))))
209 (set-current-input-port (readline-port))
210 (set! repl-reader
211 (lambda (repl-prompt . reader)
212 (let ((outer-new-input-prompt new-input-prompt)
213 (outer-continuation-prompt continuation-prompt)
214 (outer-read-hook read-hook))
215 (dynamic-wind
216 (lambda ()
217 (set-buffered-input-continuation?! (readline-port) #f)
218 (set-readline-prompt! repl-prompt "... ")
219 (set-readline-read-hook! repl-read-hook))
220 (lambda () ((or (and (pair? reader) (car reader))
221 (fluid-ref current-reader)
222 read)))
223 (lambda ()
224 (set-readline-prompt! outer-new-input-prompt outer-continuation-prompt)
225 (set-readline-read-hook! outer-read-hook))))))
226 (set! (using-readline?) #t))))
227
228 (define-public (make-completion-function strings)
229 "Construct and return a completion function for a list of strings.
230 The returned function is suitable for passing to
231 @code{with-readline-completion-function. The argument @var{strings}
232 should be a list of strings, where each string is one of the possible
233 completions."
234 (letrec ((strs '())
235 (regexp #f)
236 (completer (lambda (text continue?)
237 (if continue?
238 (if (null? strs)
239 #f
240 (let ((str (car strs)))
241 (set! strs (cdr strs))
242 (if (string-match regexp str)
243 str
244 (completer text #t))))
245 (begin
246 (set! strs strings)
247 (set! regexp
248 (string-append "^" (regexp-quote text)))
249 (completer text #t))))))
250 completer))