1 ;;;; readline.scm --- support functions for command-line editing
3 ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2006, 2009, 2010, 2011, 2013 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 "guile-readline" "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-once new-input-prompt "")
77 (define-once continuation-prompt "")
78 (define-once input-port (current-input-port))
79 (define-once output-port (current-output-port))
80 (define-once read-hook #f)
82 (define (make-readline-port)
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
88 (if (and (not continuation?)
91 (add-history history-buffer)
92 (set! history-buffer #f)))
93 ;; Set up prompts and read a line.
94 (let* ((prompt (if continuation?
97 (str (%readline (if (string? prompt)
103 (or (eof-object? str)
107 (string-append history-buffer
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
118 (define-once the-readline-port #f)
120 (define-once history-variable "GUILE_HISTORY")
121 (define-once history-file (string-append (getenv "HOME") "/.guile_history"))
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)
129 (if (not the-readline-port)
132 (set! the-readline-port (make-readline-port))
133 (add-hook! exit-hook (lambda ()
138 ;;; The user might try to use readline in his programs. It then
139 ;;; becomes very uncomfortable that the current-input-port is the
142 ;;; Here, we detect this situation and replace it with the
145 ;;; %readline is the low-level readline procedure.
147 (define-public (readline . args)
148 (let ((prompt new-input-prompt)
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))))))
158 (if (eq? inp the-readline-port)
163 (define-public (set-readline-prompt! p . rest)
164 (set! new-input-prompt p)
165 (if (not (null? rest))
166 (set! continuation-prompt (car rest))))
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))
173 (scm-error 'misc-error "set-readline-input-port!"
174 "Port not open: ~S" (list p) #f))
176 (set! input-port p))))
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))
183 (scm-error 'misc-error "set-readline-output-port!"
184 "Port not open: ~S" (list p) #f))
186 (set! output-port p))))
188 (define-public (set-readline-read-hook! h)
191 (define-public apropos-completion-function
192 (let ((completions '()))
198 (string-append "^" (regexp-quote text))))))
199 (if (null? completions)
201 (let ((retval (car completions)))
202 (begin (set! completions (cdr completions))
205 (if (provided? 'regex)
206 (set! *readline-completion-function* apropos-completion-function))
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*))
213 (set! *readline-completion-function* completer))
216 (set! *readline-completion-function* old-completer)))))
218 (define-once 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))
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))
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))))))
239 (define-public (activate-readline)
240 (if (isatty? (current-input-port))
242 (set-current-input-port (readline-port))
243 (set! repl-reader readline-repl-reader)
244 (set! (using-readline?) #t))))
246 (define-public (make-completion-function strings)
247 "Construct and return a completion function for a list of strings.
248 The returned function is suitable for passing to
249 @code{with-readline-completion-function. The argument @var{strings}
250 should be a list of strings, where each string is one of the possible
254 (completer (lambda (text continue?)
258 (let ((str (car strs)))
259 (set! strs (cdr strs))
260 (if (string-match regexp str)
262 (completer text #t))))
266 (string-append "^" (regexp-quote text)))
267 (completer text #t))))))