Expression-oriented readline history
[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)
8b755a75
NJ
83 (let ((history-buffer #f))
84 (make-line-buffered-input-port (lambda (continuation?)
85 ;; When starting a new read, add
86 ;; the previously read expression
87 ;; to the history.
88 (if (and (not continuation?)
89 history-buffer)
90 (begin
91 (add-history history-buffer)
92 (set! history-buffer #f)))
93 ;; Set up prompts and read a line.
94 (let* ((prompt (if continuation?
95 continuation-prompt
96 new-input-prompt))
97 (str (%readline (if (string? prompt)
98 prompt
99 (prompt))
100 input-port
101 output-port
102 read-hook)))
103 (or (eof-object? str)
104 (string=? str "")
105 (set! history-buffer
106 (if history-buffer
107 (string-append history-buffer
108 " "
109 str)
110 str)))
111 str)))))
74c88f53
RB
112
113;;; We only create one readline port. There's no point in having
114;;; more, since they would all share the tty and history ---
115;;; everything except the prompt. And don't forget the
116;;; compile/load/run phase distinctions. Also, the readline library
117;;; isn't reentrant.
118(define the-readline-port #f)
119
120(define history-variable "GUILE_HISTORY")
121(define history-file (string-append (getenv "HOME") "/.guile_history"))
122
123(define-public readline-port
124 (let ((do (lambda (r/w)
125 (if (memq 'history-file (readline-options-interface))
126 (r/w (or (getenv history-variable)
127 history-file))))))
128 (lambda ()
129 (if (not the-readline-port)
130 (begin
131 (do read-history)
132 (set! the-readline-port (make-readline-port))
133 (add-hook! exit-hook (lambda ()
134 (do write-history)
135 (clear-history)))))
136 the-readline-port)))
137
138;;; The user might try to use readline in his programs. It then
139;;; becomes very uncomfortable that the current-input-port is the
140;;; readline port...
141;;;
142;;; Here, we detect this situation and replace it with the
143;;; underlying port.
144;;;
145;;; %readline is the low-level readline procedure.
146
147(define-public (readline . args)
3bff1789 148 (let ((prompt new-input-prompt)
74c88f53
RB
149 (inp input-port))
150 (cond ((not (null? args))
151 (set! prompt (car args))
152 (set! args (cdr args))
153 (cond ((not (null? args))
154 (set! inp (car args))
155 (set! args (cdr args))))))
156 (apply %readline
157 prompt
158 (if (eq? inp the-readline-port)
159 input-port
160 inp)
161 args)))
162
163(define-public (set-readline-prompt! p . rest)
3bff1789 164 (set! new-input-prompt p)
74c88f53 165 (if (not (null? rest))
3bff1789 166 (set! continuation-prompt (car rest))))
74c88f53
RB
167
168(define-public (set-readline-input-port! p)
169 (cond ((or (not (file-port? p)) (not (input-port? p)))
170 (scm-error 'wrong-type-arg "set-readline-input-port!"
171 "Not a file input port: ~S" (list p) #f))
172 ((port-closed? p)
173 (scm-error 'misc-error "set-readline-input-port!"
174 "Port not open: ~S" (list p) #f))
175 (else
176 (set! input-port p))))
177
178(define-public (set-readline-output-port! p)
179 (cond ((or (not (file-port? p)) (not (output-port? p)))
180 (scm-error 'wrong-type-arg "set-readline-input-port!"
181 "Not a file output port: ~S" (list p) #f))
182 ((port-closed? p)
183 (scm-error 'misc-error "set-readline-output-port!"
184 "Port not open: ~S" (list p) #f))
185 (else
186 (set! output-port p))))
187
188(define-public (set-readline-read-hook! h)
189 (set! read-hook h))
190
64e5d08d
AW
191(define-public apropos-completion-function
192 (let ((completions '()))
193 (lambda (text cont?)
194 (if (not cont?)
195 (set! completions
196 (map symbol->string
197 (apropos-internal
198 (string-append "^" (regexp-quote text))))))
199 (if (null? completions)
200 #f
201 (let ((retval (car completions)))
202 (begin (set! completions (cdr completions))
203 retval))))))
204
74c88f53 205(if (provided? 'regex)
64e5d08d 206 (set! *readline-completion-function* apropos-completion-function))
74c88f53
RB
207
208(define-public (with-readline-completion-function completer thunk)
209 "With @var{completer} as readline completion function, call @var{thunk}."
210 (let ((old-completer *readline-completion-function*))
211 (dynamic-wind
212 (lambda ()
213 (set! *readline-completion-function* completer))
214 thunk
215 (lambda ()
216 (set! *readline-completion-function* old-completer)))))
217
1924145d
AW
218(define readline-repl-reader
219 (let ((boot-9-repl-reader repl-reader))
220 (lambda* (repl-prompt #:optional (reader (fluid-ref current-reader)))
221 (let ((port (current-input-port)))
222 (if (eq? port (readline-port))
223 (let ((outer-new-input-prompt new-input-prompt)
224 (outer-continuation-prompt continuation-prompt)
225 (outer-read-hook read-hook))
226 (dynamic-wind
227 (lambda ()
228 (set-buffered-input-continuation?! port #f)
229 (set-readline-prompt! repl-prompt "... ")
230 (set-readline-read-hook! (lambda ()
231 (run-hook before-read-hook))))
232 (lambda () ((or reader read) port))
233 (lambda ()
234 (set-readline-prompt! outer-new-input-prompt
235 outer-continuation-prompt)
236 (set-readline-read-hook! outer-read-hook))))
237 (boot-9-repl-reader repl-prompt reader))))))
238
74c88f53 239(define-public (activate-readline)
adb825b6 240 (if (isatty? (current-input-port))
1924145d
AW
241 (begin
242 (set-current-input-port (readline-port))
243 (set! repl-reader readline-repl-reader)
244 (set! (using-readline?) #t))))
1b09b607
KR
245
246(define-public (make-completion-function strings)
247 "Construct and return a completion function for a list of strings.
248The returned function is suitable for passing to
249@code{with-readline-completion-function. The argument @var{strings}
250should be a list of strings, where each string is one of the possible
251completions."
252 (letrec ((strs '())
253 (regexp #f)
254 (completer (lambda (text continue?)
255 (if continue?
256 (if (null? strs)
257 #f
258 (let ((str (car strs)))
259 (set! strs (cdr strs))
260 (if (string-match regexp str)
261 str
262 (completer text #t))))
263 (begin
264 (set! strs strings)
265 (set! regexp
266 (string-append "^" (regexp-quote text)))
267 (completer text #t))))))
268 completer))