1 ;;;; readline.scm --- support functions for command-line editing
3 ;;;; Copyright (C) 1997, 1999, 2000, 2001, 2002 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 2, 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., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 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)
34 ;;; Dynamically link the glue code for accessing the readline library,
35 ;;; but only when it isn't already present.
37 (if (not (provided? 'readline))
38 (load-extension "libguilereadline-v-16" "scm_init_readline"))
40 (if (not (provided? 'readline))
41 (scm-error 'misc-error
43 "readline is not provided in this Guile installation"
58 (define-option-interface
59 (readline-options-interface
60 (readline-options readline-enable readline-disable)
65 ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
66 ;;; There should probably be low-level support instead of this code.
68 ;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
69 ;;; guile will enter an endless loop or crash.
73 (define input-port (current-input-port))
74 (define output-port (current-output-port))
77 (define (make-readline-port)
78 (make-line-buffered-input-port (lambda (continuation?)
79 (let* ((prompt (if continuation?
82 (str (%readline (if (string? prompt)
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
98 (define the-readline-port #f)
100 (define history-variable "GUILE_HISTORY")
101 (define history-file (string-append (getenv "HOME") "/.guile_history"))
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)
109 (if (not the-readline-port)
112 (set! the-readline-port (make-readline-port))
113 (add-hook! exit-hook (lambda ()
118 ;;; The user might try to use readline in his programs. It then
119 ;;; becomes very uncomfortable that the current-input-port is the
122 ;;; Here, we detect this situation and replace it with the
125 ;;; %readline is the low-level readline procedure.
127 (define-public (readline . args)
128 (let ((prompt prompt)
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))))))
138 (if (eq? inp the-readline-port)
143 (define-public (set-readline-prompt! p . rest)
145 (if (not (null? rest))
146 (set! prompt2 (car rest))))
148 (define-public (set-readline-input-port! p)
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))
153 (scm-error 'misc-error "set-readline-input-port!"
154 "Port not open: ~S" (list p) #f))
156 (set! input-port p))))
158 (define-public (set-readline-output-port! p)
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))
163 (scm-error 'misc-error "set-readline-output-port!"
164 "Port not open: ~S" (list p) #f))
166 (set! output-port p))))
168 (define-public (set-readline-read-hook! h)
171 (if (provided? 'regex)
173 (define-public apropos-completion-function
174 (let ((completions '()))
180 (string-append "^" (regexp-quote text))))))
181 (if (null? completions)
183 (let ((retval (car completions)))
184 (begin (set! completions (cdr completions))
187 (set! *readline-completion-function* apropos-completion-function)
190 (define-public (with-readline-completion-function completer thunk)
191 "With @var{completer} as readline completion function, call @var{thunk}."
192 (let ((old-completer *readline-completion-function*))
195 (set! *readline-completion-function* completer))
198 (set! *readline-completion-function* old-completer)))))
200 (define-public (activate-readline)
201 (if (and (isatty? (current-input-port))
202 (not (let ((guile-user-module (resolve-module '(guile-user))))
203 (and (module-defined? guile-user-module 'use-emacs-interface)
204 (module-ref guile-user-module 'use-emacs-interface)))))
205 (let ((read-hook (lambda () (run-hook before-read-hook))))
206 (set-current-input-port (readline-port))
211 (set-buffered-input-continuation?! (readline-port) #f)
212 (set-readline-prompt! prompt "... ")
213 (set-readline-read-hook! read-hook))
216 (set-readline-prompt! "" "")
217 (set-readline-read-hook! #f)))))
218 (set! (using-readline?) #t))))