Merge commit 'cce8b2ce93703aff953750fb40cb53176ea66504' into vm-check
[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)
afc4ccd4
KR
30 :no-backtrace
31 :export (filename-completion-function))
74c88f53
RB
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))
6cecdff7 39 (load-extension "libguilereadline-v-18" "scm_init_readline"))
74c88f53
RB
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
3bff1789
NJ
72(define new-input-prompt "")
73(define continuation-prompt "")
74c88f53
RB
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?
3bff1789
NJ
81 continuation-prompt
82 new-input-prompt))
74c88f53
RB
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)
3bff1789 129 (let ((prompt new-input-prompt)
74c88f53
RB
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)
3bff1789 145 (set! new-input-prompt p)
74c88f53 146 (if (not (null? rest))
3bff1789 147 (set! continuation-prompt (car rest))))
74c88f53
RB
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(if (provided? 'regex)
173 (begin
174 (define-public apropos-completion-function
175 (let ((completions '()))
176 (lambda (text cont?)
177 (if (not cont?)
178 (set! completions
179 (map symbol->string
180 (apropos-internal
181 (string-append "^" (regexp-quote text))))))
182 (if (null? completions)
183 #f
184 (let ((retval (car completions)))
185 (begin (set! completions (cdr completions))
186 retval))))))
187
188 (set! *readline-completion-function* apropos-completion-function)
189 ))
190
191(define-public (with-readline-completion-function completer thunk)
192 "With @var{completer} as readline completion function, call @var{thunk}."
193 (let ((old-completer *readline-completion-function*))
194 (dynamic-wind
195 (lambda ()
196 (set! *readline-completion-function* completer))
197 thunk
198 (lambda ()
199 (set! *readline-completion-function* old-completer)))))
200
201(define-public (activate-readline)
202 (if (and (isatty? (current-input-port))
203 (not (let ((guile-user-module (resolve-module '(guile-user))))
204 (and (module-defined? guile-user-module 'use-emacs-interface)
205 (module-ref guile-user-module 'use-emacs-interface)))))
3bff1789 206 (let ((repl-read-hook (lambda () (run-hook before-read-hook))))
74c88f53
RB
207 (set-current-input-port (readline-port))
208 (set! repl-reader
3bff1789
NJ
209 (lambda (repl-prompt)
210 (let ((outer-new-input-prompt new-input-prompt)
211 (outer-continuation-prompt continuation-prompt)
212 (outer-read-hook read-hook))
213 (dynamic-wind
214 (lambda ()
215 (set-buffered-input-continuation?! (readline-port) #f)
216 (set-readline-prompt! repl-prompt "... ")
217 (set-readline-read-hook! repl-read-hook))
d28d5993 218 (lambda () ((or (fluid-ref current-reader) read)))
3bff1789
NJ
219 (lambda ()
220 (set-readline-prompt! outer-new-input-prompt outer-continuation-prompt)
221 (set-readline-read-hook! outer-read-hook))))))
74c88f53 222 (set! (using-readline?) #t))))
1b09b607
KR
223
224(define-public (make-completion-function strings)
225 "Construct and return a completion function for a list of strings.
226The returned function is suitable for passing to
227@code{with-readline-completion-function. The argument @var{strings}
228should be a list of strings, where each string is one of the possible
229completions."
230 (letrec ((strs '())
231 (regexp #f)
232 (completer (lambda (text continue?)
233 (if continue?
234 (if (null? strs)
235 #f
236 (let ((str (car strs)))
237 (set! strs (cdr strs))
238 (if (string-match regexp str)
239 str
240 (completer text #t))))
241 (begin
242 (set! strs strings)
243 (set! regexp
244 (string-append "^" (regexp-quote text)))
245 (completer text #t))))))
246 completer))