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