1 ;;;; readline.scm --- support functions for command-line editing
3 ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2006, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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
20 ;;;; Contributed by Daniel Risacher <risacher@worldnet.att.net>.
21 ;;;; Extensions based upon code by
22 ;;;; Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>.
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)
31 #:export (filename-completion-function
39 ;;; Dynamically link the glue code for accessing the readline library,
40 ;;; but only when it isn't already present.
42 (if (not (provided? 'readline))
43 (load-extension "libguilereadline-v-18" "scm_init_readline"))
45 (if (not (provided? 'readline))
46 (scm-error 'misc-error
48 "readline is not provided in this Guile installation"
63 (define-option-interface
64 (readline-options-interface
65 (readline-options readline-enable readline-disable)
70 ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
71 ;;; There should probably be low-level support instead of this code.
73 ;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
74 ;;; guile will enter an endless loop or crash.
76 (define new-input-prompt "")
77 (define continuation-prompt "")
78 (define input-port (current-input-port))
79 (define output-port (current-output-port))
82 (define (make-readline-port)
83 (make-line-buffered-input-port (lambda (continuation?)
84 (let* ((prompt (if continuation?
87 (str (%readline (if (string? prompt)
98 ;;; We only create one readline port. There's no point in having
99 ;;; more, since they would all share the tty and history ---
100 ;;; everything except the prompt. And don't forget the
101 ;;; compile/load/run phase distinctions. Also, the readline library
103 (define the-readline-port #f)
105 (define history-variable "GUILE_HISTORY")
106 (define history-file (string-append (getenv "HOME") "/.guile_history"))
108 (define-public readline-port
109 (let ((do (lambda (r/w)
110 (if (memq 'history-file (readline-options-interface))
111 (r/w (or (getenv history-variable)
114 (if (not the-readline-port)
117 (set! the-readline-port (make-readline-port))
118 (add-hook! exit-hook (lambda ()
123 ;;; The user might try to use readline in his programs. It then
124 ;;; becomes very uncomfortable that the current-input-port is the
127 ;;; Here, we detect this situation and replace it with the
130 ;;; %readline is the low-level readline procedure.
132 (define-public (readline . args)
133 (let ((prompt new-input-prompt)
135 (cond ((not (null? args))
136 (set! prompt (car args))
137 (set! args (cdr args))
138 (cond ((not (null? args))
139 (set! inp (car args))
140 (set! args (cdr args))))))
143 (if (eq? inp the-readline-port)
148 (define-public (set-readline-prompt! p . rest)
149 (set! new-input-prompt p)
150 (if (not (null? rest))
151 (set! continuation-prompt (car rest))))
153 (define-public (set-readline-input-port! p)
154 (cond ((or (not (file-port? p)) (not (input-port? p)))
155 (scm-error 'wrong-type-arg "set-readline-input-port!"
156 "Not a file input port: ~S" (list p) #f))
158 (scm-error 'misc-error "set-readline-input-port!"
159 "Port not open: ~S" (list p) #f))
161 (set! input-port p))))
163 (define-public (set-readline-output-port! p)
164 (cond ((or (not (file-port? p)) (not (output-port? p)))
165 (scm-error 'wrong-type-arg "set-readline-input-port!"
166 "Not a file output port: ~S" (list p) #f))
168 (scm-error 'misc-error "set-readline-output-port!"
169 "Port not open: ~S" (list p) #f))
171 (set! output-port p))))
173 (define-public (set-readline-read-hook! h)
176 (define-public apropos-completion-function
177 (let ((completions '()))
183 (string-append "^" (regexp-quote text))))))
184 (if (null? completions)
186 (let ((retval (car completions)))
187 (begin (set! completions (cdr completions))
190 (if (provided? 'regex)
191 (set! *readline-completion-function* apropos-completion-function))
193 (define-public (with-readline-completion-function completer thunk)
194 "With @var{completer} as readline completion function, call @var{thunk}."
195 (let ((old-completer *readline-completion-function*))
198 (set! *readline-completion-function* completer))
201 (set! *readline-completion-function* old-completer)))))
203 (define-public (activate-readline)
204 (if (isatty? (current-input-port))
205 (let ((repl-read-hook (lambda () (run-hook before-read-hook))))
206 (set-current-input-port (readline-port))
208 (lambda* (repl-prompt
209 #:optional (reader (fluid-ref current-reader)))
210 (let ((outer-new-input-prompt new-input-prompt)
211 (outer-continuation-prompt continuation-prompt)
212 (outer-read-hook read-hook))
215 (set-buffered-input-continuation?! (readline-port) #f)
216 (set-readline-prompt! repl-prompt "... ")
217 (set-readline-read-hook! repl-read-hook))
218 (lambda () ((or reader read) (current-input-port)))
220 (set-readline-prompt! outer-new-input-prompt outer-continuation-prompt)
221 (set-readline-read-hook! outer-read-hook))))))
222 (set! (using-readline?) #t))))
224 (define-public (make-completion-function strings)
225 "Construct and return a completion function for a list of strings.
226 The returned function is suitable for passing to
227 @code{with-readline-completion-function. The argument @var{strings}
228 should be a list of strings, where each string is one of the possible
232 (completer (lambda (text continue?)
236 (let ((str (car strs)))
237 (set! strs (cdr strs))
238 (if (string-match regexp str)
240 (completer text #t))))
244 (string-append "^" (regexp-quote text)))
245 (completer text #t))))))