1 ;;;; readline.scm --- support functions for command-line editing
3 ;;;; Copyright (C) 1997, 1999, 2000, 2001 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 (feature? 'readline))
38 (dynamic-call "scm_init_readline"
39 (dynamic-link "libguilereadline-v-12")))
41 (if (not (feature? 'readline))
42 (scm-error 'misc-error
44 "readline is not provided in this Guile installation"
59 (define-option-interface
60 (readline-options-interface
61 (readline-options readline-enable readline-disable)
66 ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
67 ;;; There should probably be low-level support instead of this code.
69 ;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
70 ;;; guile will enter an endless loop or crash.
74 (define input-port (current-input-port))
75 (define output-port (current-output-port))
78 (define (make-readline-port)
79 (make-line-buffered-input-port (lambda (continuation?)
80 (let* ((prompt (if continuation?
83 (str (%readline (if (string? prompt)
94 ;;; We only create one readline port. There's no point in having
95 ;;; more, since they would all share the tty and history ---
96 ;;; everything except the prompt. And don't forget the
97 ;;; compile/load/run phase distinctions. Also, the readline library
99 (define the-readline-port #f)
101 (define history-variable "GUILE_HISTORY")
102 (define history-file (string-append (getenv "HOME") "/.guile_history"))
104 (define-public readline-port
105 (let ((do (lambda (r/w)
106 (if (memq 'history-file (readline-options-interface))
107 (r/w (or (getenv history-variable)
110 (if (not the-readline-port)
113 (set! the-readline-port (make-readline-port))
114 (add-hook! exit-hook (lambda ()
119 ;;; The user might try to use readline in his programs. It then
120 ;;; becomes very uncomfortable that the current-input-port is the
123 ;;; Here, we detect this situation and replace it with the
126 ;;; %readline is the low-level readline procedure.
128 (define-public (readline . args)
129 (let ((prompt prompt)
131 (cond ((not (null? args))
132 (set! prompt (car args))
133 (set! args (cdr args))
134 (cond ((not (null? args))
135 (set! inp (car args))
136 (set! args (cdr args))))))
139 (if (eq? inp the-readline-port)
144 (define-public (set-readline-prompt! p . rest)
146 (if (not (null? rest))
147 (set! prompt2 (car rest))))
149 (define-public (set-readline-input-port! p)
150 (cond ((or (not (file-port? p)) (not (input-port? p)))
151 (scm-error 'wrong-type-arg "set-readline-input-port!"
152 "Not a file input port: ~S" (list p) #f))
154 (scm-error 'misc-error "set-readline-input-port!"
155 "Port not open: ~S" (list p) #f))
157 (set! input-port p))))
159 (define-public (set-readline-output-port! p)
160 (cond ((or (not (file-port? p)) (not (output-port? p)))
161 (scm-error 'wrong-type-arg "set-readline-input-port!"
162 "Not a file output port: ~S" (list p) #f))
164 (scm-error 'misc-error "set-readline-output-port!"
165 "Port not open: ~S" (list p) #f))
167 (set! output-port p))))
169 (define-public (set-readline-read-hook! h)
172 (if (feature? 'regex)
174 (define-public apropos-completion-function
175 (let ((completions '()))
181 (string-append "^" (regexp-quote text))))))
182 (if (null? completions)
184 (let ((retval (car completions)))
185 (begin (set! completions (cdr completions))
188 (set! *readline-completion-function* apropos-completion-function)
191 (define-public (with-readline-completion-function completer thunk)
192 "With @var{completer} as readline completion function, call @var{thunk}."
193 (let ((old-completer *readline-completion-function*))
196 (set! *readline-completion-function* completer))
199 (set! *readline-completion-function* old-completer)))))
201 (define-public (activate-readline)
202 (if (and (isatty? (current-input-port))
203 (not (and (module-defined? the-root-module 'use-emacs-interface)
204 (module-ref the-root-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))))