1 ;;;; readline.scm --- support functions for command-line editing
3 ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2002, 2006, 2009, 2010, 2011,
4 ;;;; 2013, 2014 Free Software Foundation, Inc.
6 ;;;; This program is free software; you can redistribute it and/or modify
7 ;;;; it under the terms of the GNU General Public License as published by
8 ;;;; the Free Software Foundation; either version 3, or (at your option)
9 ;;;; any later version.
11 ;;;; This program is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;;; GNU General Public License for more details.
16 ;;;; You should have received a copy of the GNU General Public License
17 ;;;; along with this software; see the file COPYING. If not, write to
18 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 ;;;; Boston, MA 02110-1301 USA
21 ;;;; Contributed by Daniel Risacher <risacher@worldnet.att.net>.
22 ;;;; Extensions based upon code by
23 ;;;; Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>.
27 (define-module (ice-9 readline)
28 #:use-module (ice-9 session)
29 #:use-module (ice-9 regex)
30 #:use-module (ice-9 buffered-input)
32 #:export (filename-completion-function
40 ;;; Dynamically link the glue code for accessing the readline library,
41 ;;; but only when it isn't already present.
43 (if (not (provided? 'readline))
44 (load-extension "guile-readline" "scm_init_readline"))
46 (if (not (provided? 'readline))
47 (scm-error 'misc-error
49 "readline is not provided in this Guile installation"
64 (define-option-interface
65 (readline-options-interface
66 (readline-options readline-enable readline-disable)
71 ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
72 ;;; There should probably be low-level support instead of this code.
74 ;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
75 ;;; guile will enter an endless loop or crash.
77 (define-once new-input-prompt "")
78 (define-once continuation-prompt "")
79 (define-once input-port (current-input-port))
80 (define-once output-port (current-output-port))
81 (define-once read-hook #f)
83 (define (make-readline-port)
84 (let ((history-buffer #f))
85 (make-line-buffered-input-port (lambda (continuation?)
86 ;; When starting a new read, add
87 ;; the previously read expression
89 (if (and (not continuation?)
92 (add-history history-buffer)
93 (set! history-buffer #f)))
94 ;; Set up prompts and read a line.
95 (let* ((prompt (if continuation?
98 (str (%readline (if (string? prompt)
104 (or (eof-object? str)
108 (string-append history-buffer
114 ;;; We only create one readline port. There's no point in having
115 ;;; more, since they would all share the tty and history ---
116 ;;; everything except the prompt. And don't forget the
117 ;;; compile/load/run phase distinctions. Also, the readline library
119 (define-once the-readline-port #f)
121 (define-once history-variable "GUILE_HISTORY")
122 (define-once history-file
123 (string-append (or (getenv "HOME") ".") "/.guile_history"))
125 (define-public readline-port
126 (let ((do (lambda (r/w)
127 (if (memq 'history-file (readline-options-interface))
128 (r/w (or (getenv history-variable)
131 (if (not the-readline-port)
134 (set! the-readline-port (make-readline-port))
135 (add-hook! exit-hook (lambda ()
140 ;;; The user might try to use readline in his programs. It then
141 ;;; becomes very uncomfortable that the current-input-port is the
144 ;;; Here, we detect this situation and replace it with the
147 ;;; %readline is the low-level readline procedure.
149 (define-public (readline . args)
150 (let ((prompt new-input-prompt)
152 (cond ((not (null? args))
153 (set! prompt (car args))
154 (set! args (cdr args))
155 (cond ((not (null? args))
156 (set! inp (car args))
157 (set! args (cdr args))))))
160 (if (eq? inp the-readline-port)
165 (define-public (set-readline-prompt! p . rest)
166 (set! new-input-prompt p)
167 (if (not (null? rest))
168 (set! continuation-prompt (car rest))))
170 (define-public (set-readline-input-port! p)
171 (cond ((or (not (file-port? p)) (not (input-port? p)))
172 (scm-error 'wrong-type-arg "set-readline-input-port!"
173 "Not a file input port: ~S" (list p) #f))
175 (scm-error 'misc-error "set-readline-input-port!"
176 "Port not open: ~S" (list p) #f))
178 (set! input-port p))))
180 (define-public (set-readline-output-port! p)
181 (cond ((or (not (file-port? p)) (not (output-port? p)))
182 (scm-error 'wrong-type-arg "set-readline-input-port!"
183 "Not a file output port: ~S" (list p) #f))
185 (scm-error 'misc-error "set-readline-output-port!"
186 "Port not open: ~S" (list p) #f))
188 (set! output-port p))))
190 (define-public (set-readline-read-hook! h)
193 (define-public apropos-completion-function
194 (let ((completions '()))
200 (string-append "^" (regexp-quote text))))))
201 (if (null? completions)
203 (let ((retval (car completions)))
204 (begin (set! completions (cdr completions))
207 (if (provided? 'regex)
208 (set! *readline-completion-function* apropos-completion-function))
210 (define-public (with-readline-completion-function completer thunk)
211 "With @var{completer} as readline completion function, call @var{thunk}."
212 (let ((old-completer *readline-completion-function*))
215 (set! *readline-completion-function* completer))
218 (set! *readline-completion-function* old-completer)))))
220 (define-once readline-repl-reader
221 (let ((boot-9-repl-reader repl-reader))
222 (lambda* (repl-prompt #:optional (reader (fluid-ref current-reader)))
223 (let ((port (current-input-port)))
224 (if (eq? port (readline-port))
225 (let ((outer-new-input-prompt new-input-prompt)
226 (outer-continuation-prompt continuation-prompt)
227 (outer-read-hook read-hook))
230 (set-buffered-input-continuation?! port #f)
231 (set-readline-prompt! repl-prompt "... ")
232 (set-readline-read-hook! (lambda ()
233 (run-hook before-read-hook))))
234 (lambda () ((or reader read) port))
236 (set-readline-prompt! outer-new-input-prompt
237 outer-continuation-prompt)
238 (set-readline-read-hook! outer-read-hook))))
239 (boot-9-repl-reader repl-prompt reader))))))
241 (define-public (activate-readline)
242 (if (isatty? (current-input-port))
244 (set-current-input-port (readline-port))
245 (set! repl-reader readline-repl-reader)
246 (set! (using-readline?) #t))))
248 (define-public (make-completion-function strings)
249 "Construct and return a completion function for a list of strings.
250 The returned function is suitable for passing to
251 @code{with-readline-completion-function. The argument @var{strings}
252 should be a list of strings, where each string is one of the possible
256 (completer (lambda (text continue?)
260 (let ((str (car strs)))
261 (set! strs (cdr strs))
262 (if (string-match regexp str)
264 (completer text #t))))
268 (string-append "^" (regexp-quote text)))
269 (completer text #t))))))