Merge branch 'master' into boehm-demers-weiser-gc
[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 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
33 \f
34
35 ;;; Dynamically link the glue code for accessing the readline library,
36 ;;; but only when it isn't already present.
37
38 (if (not (provided? 'readline))
39 (load-extension "libguilereadline-v-18" "scm_init_readline"))
40
41 (if (not (provided? 'readline))
42 (scm-error 'misc-error
43 #f
44 "readline is not provided in this Guile installation"
45 '()
46 '()))
47
48 \f
49
50 ;;; Run-time options
51
52 (export
53 readline-options
54 readline-enable
55 readline-disable)
56 (export-syntax
57 readline-set!)
58
59 (define-option-interface
60 (readline-options-interface
61 (readline-options readline-enable readline-disable)
62 (readline-set!)))
63
64 \f
65
66 ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
67 ;;; There should probably be low-level support instead of this code.
68
69 ;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
70 ;;; guile will enter an endless loop or crash.
71
72 (define new-input-prompt "")
73 (define continuation-prompt "")
74 (define input-port (current-input-port))
75 (define output-port (current-output-port))
76 (define read-hook #f)
77
78 (define (make-readline-port)
79 (make-line-buffered-input-port (lambda (continuation?)
80 (let* ((prompt (if continuation?
81 continuation-prompt
82 new-input-prompt))
83 (str (%readline (if (string? prompt)
84 prompt
85 (prompt))
86 input-port
87 output-port
88 read-hook)))
89 (or (eof-object? str)
90 (string=? str "")
91 (add-history str))
92 str))))
93
94 ;;; We only create one readline port. There's no point in having
95 ;;; more, since they would all share the tty and history ---
96 ;;; everything except the prompt. And don't forget the
97 ;;; compile/load/run phase distinctions. Also, the readline library
98 ;;; isn't reentrant.
99 (define the-readline-port #f)
100
101 (define history-variable "GUILE_HISTORY")
102 (define history-file (string-append (getenv "HOME") "/.guile_history"))
103
104 (define-public readline-port
105 (let ((do (lambda (r/w)
106 (if (memq 'history-file (readline-options-interface))
107 (r/w (or (getenv history-variable)
108 history-file))))))
109 (lambda ()
110 (if (not the-readline-port)
111 (begin
112 (do read-history)
113 (set! the-readline-port (make-readline-port))
114 (add-hook! exit-hook (lambda ()
115 (do write-history)
116 (clear-history)))))
117 the-readline-port)))
118
119 ;;; The user might try to use readline in his programs. It then
120 ;;; becomes very uncomfortable that the current-input-port is the
121 ;;; readline port...
122 ;;;
123 ;;; Here, we detect this situation and replace it with the
124 ;;; underlying port.
125 ;;;
126 ;;; %readline is the low-level readline procedure.
127
128 (define-public (readline . args)
129 (let ((prompt new-input-prompt)
130 (inp input-port))
131 (cond ((not (null? args))
132 (set! prompt (car args))
133 (set! args (cdr args))
134 (cond ((not (null? args))
135 (set! inp (car args))
136 (set! args (cdr args))))))
137 (apply %readline
138 prompt
139 (if (eq? inp the-readline-port)
140 input-port
141 inp)
142 args)))
143
144 (define-public (set-readline-prompt! p . rest)
145 (set! new-input-prompt p)
146 (if (not (null? rest))
147 (set! continuation-prompt (car rest))))
148
149 (define-public (set-readline-input-port! p)
150 (cond ((or (not (file-port? p)) (not (input-port? p)))
151 (scm-error 'wrong-type-arg "set-readline-input-port!"
152 "Not a file input port: ~S" (list p) #f))
153 ((port-closed? p)
154 (scm-error 'misc-error "set-readline-input-port!"
155 "Port not open: ~S" (list p) #f))
156 (else
157 (set! input-port p))))
158
159 (define-public (set-readline-output-port! p)
160 (cond ((or (not (file-port? p)) (not (output-port? p)))
161 (scm-error 'wrong-type-arg "set-readline-input-port!"
162 "Not a file output port: ~S" (list p) #f))
163 ((port-closed? p)
164 (scm-error 'misc-error "set-readline-output-port!"
165 "Port not open: ~S" (list p) #f))
166 (else
167 (set! output-port p))))
168
169 (define-public (set-readline-read-hook! h)
170 (set! read-hook h))
171
172 (define-public apropos-completion-function
173 (let ((completions '()))
174 (lambda (text cont?)
175 (if (not cont?)
176 (set! completions
177 (map symbol->string
178 (apropos-internal
179 (string-append "^" (regexp-quote text))))))
180 (if (null? completions)
181 #f
182 (let ((retval (car completions)))
183 (begin (set! completions (cdr completions))
184 retval))))))
185
186 (if (provided? 'regex)
187 (set! *readline-completion-function* apropos-completion-function))
188
189 (define-public (with-readline-completion-function completer thunk)
190 "With @var{completer} as readline completion function, call @var{thunk}."
191 (let ((old-completer *readline-completion-function*))
192 (dynamic-wind
193 (lambda ()
194 (set! *readline-completion-function* completer))
195 thunk
196 (lambda ()
197 (set! *readline-completion-function* old-completer)))))
198
199 (define-public (activate-readline)
200 (if (and (isatty? (current-input-port))
201 (not (let ((guile-user-module (resolve-module '(guile-user))))
202 (and (module-defined? guile-user-module 'use-emacs-interface)
203 (module-ref guile-user-module 'use-emacs-interface)))))
204 (let ((repl-read-hook (lambda () (run-hook before-read-hook))))
205 (set-current-input-port (readline-port))
206 (set! repl-reader
207 (lambda (repl-prompt)
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?! (readline-port) #f)
214 (set-readline-prompt! repl-prompt "... ")
215 (set-readline-read-hook! repl-read-hook))
216 (lambda () ((or (fluid-ref current-reader) read)))
217 (lambda ()
218 (set-readline-prompt! outer-new-input-prompt outer-continuation-prompt)
219 (set-readline-read-hook! outer-read-hook))))))
220 (set! (using-readline?) #t))))
221
222 (define-public (make-completion-function strings)
223 "Construct and return a completion function for a list of strings.
224 The returned function is suitable for passing to
225 @code{with-readline-completion-function. The argument @var{strings}
226 should be a list of strings, where each string is one of the possible
227 completions."
228 (letrec ((strs '())
229 (regexp #f)
230 (completer (lambda (text continue?)
231 (if continue?
232 (if (null? strs)
233 #f
234 (let ((str (car strs)))
235 (set! strs (cdr strs))
236 (if (string-match regexp str)
237 str
238 (completer text #t))))
239 (begin
240 (set! strs strings)
241 (set! regexp
242 (string-append "^" (regexp-quote text)))
243 (completer text #t))))))
244 completer))