(ice9_sources): Add gap-buffer.scm.
[bpt/guile.git] / guile-readline / readline.scm
CommitLineData
b18c7b77
MV
1;;;; readline.scm --- support functions for command-line editing
2;;;;
9cc64c3e 3;;;; Copyright (C) 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
b18c7b77
MV
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
17;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18;;;; Boston, MA 02111-1307 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
e9bab9df
DH
24\f
25
b18c7b77
MV
26(define-module (ice-9 readline)
27 :use-module (ice-9 session)
c8fac0c3 28 :use-module (ice-9 regex)
9155e458 29 :use-module (ice-9 buffered-input)
c8fac0c3 30 :no-backtrace)
b18c7b77 31
e9bab9df
DH
32\f
33
acb0207f
MV
34;;; Dynamically link the glue code for accessing the readline library,
35;;; but only when it isn't already present.
b18c7b77 36
fbd5c452 37(if (not (provided? 'readline))
7e8ef316 38 (load-extension "libguilereadline" "scm_init_readline"))
b18c7b77 39
fbd5c452 40(if (not (provided? 'readline))
c8fac0c3
MD
41 (scm-error 'misc-error
42 #f
43 "readline is not provided in this Guile installation"
44 '()
45 '()))
46
e9bab9df
DH
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
b18c7b77
MV
65;;; MDJ 980513 <djurfeldt@nada.kth.se>:
66;;; There should probably be low-level support instead of this code.
67
e9bab9df
DH
68;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
69;;; guile will enter an endless loop or crash.
70
b18c7b77
MV
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)
9155e458
NJ
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))))
b18c7b77
MV
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))
8ed35a15
MV
113 (add-hook! exit-hook (lambda ()
114 (do write-history)
115 (clear-history)))))
b18c7b77
MV
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)
c4a9b7bb
DH
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))))
b18c7b77
MV
157
158(define-public (set-readline-output-port! p)
c4a9b7bb
DH
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))))
b18c7b77
MV
167
168(define-public (set-readline-read-hook! h)
169 (set! read-hook h))
170
fbd5c452 171(if (provided? 'regex)
ebfa2cd5
MD
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 ))
b18c7b77 189
bbd26b5a 190(define-public (with-readline-completion-function completer thunk)
ee034603
NJ
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
b18c7b77
MV
200(define-public (activate-readline)
201 (if (and (isatty? (current-input-port))
6b098fec
DH
202 (not (and (module-defined? the-root-module 'use-emacs-interface)
203 (module-ref the-root-module 'use-emacs-interface))))
b18c7b77
MV
204 (let ((read-hook (lambda () (run-hook before-read-hook))))
205 (set-current-input-port (readline-port))
206 (set! repl-reader
207 (lambda (prompt)
208 (dynamic-wind
209 (lambda ()
9155e458 210 (set-buffered-input-continuation?! (readline-port) #f)
b18c7b77
MV
211 (set-readline-prompt! prompt "... ")
212 (set-readline-read-hook! read-hook))
213 (lambda () (read))
214 (lambda ()
215 (set-readline-prompt! "" "")
6373eb6f
MD
216 (set-readline-read-hook! #f)))))
217 (set! (using-readline?) #t))))