Install msysDTK-1.0.1
[msysgit.git] / share / guile / 1.6.0 / ice-9 / readline.scm
blob07af531ee7675255ac5a64bef9fb3ceac2009b92
1 ;;;; readline.scm --- support functions for command-line editing
2 ;;;;
3 ;;;;    Copyright (C) 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
4 ;;;; 
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.
9 ;;;; 
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.
14 ;;;; 
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
19 ;;;; 
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)
30   :no-backtrace)
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
43                #f
44                "readline is not provided in this Guile installation"
45                '()
46                '()))
50 ;;; Run-time options
52 (export
53  readline-options
54  readline-enable
55  readline-disable)
56 (export-syntax
57  readline-set!)
59 (define-option-interface
60   (readline-options-interface
61    (readline-options readline-enable readline-disable)
62    (readline-set!)))
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.
72 (define prompt "")
73 (define prompt2 "")
74 (define input-port (current-input-port))
75 (define output-port (current-output-port))
76 (define read-hook #f)
78 (define (make-readline-port)
79   (make-line-buffered-input-port (lambda (continuation?)
80                                    (let* ((prompt (if continuation?
81                                                       prompt2
82                                                       prompt))
83                                           (str (%readline (if (string? prompt)
84                                                               prompt
85                                                               (prompt))
86                                                           input-port
87                                                           output-port
88                                                           read-hook)))
89                                      (or (eof-object? str)
90                                          (string=? str "")
91                                          (add-history str))
92                                      str))))
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
98 ;;; isn't reentrant.
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)
108                            history-file))))))
109     (lambda ()
110       (if (not the-readline-port)
111           (begin
112             (do read-history) 
113             (set! the-readline-port (make-readline-port))
114             (add-hook! exit-hook (lambda () 
115                                    (do write-history)
116                                    (clear-history)))))
117       the-readline-port)))
119 ;;; The user might try to use readline in his programs.  It then
120 ;;; becomes very uncomfortable that the current-input-port is the
121 ;;; readline port...
123 ;;; Here, we detect this situation and replace it with the
124 ;;; underlying port.
126 ;;; %readline is the low-level readline procedure.
128 (define-public (readline . args)
129   (let ((prompt prompt)
130         (inp input-port))
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))))))
137     (apply %readline
138            prompt
139            (if (eq? inp the-readline-port)
140                input-port
141                inp)
142            args)))
144 (define-public (set-readline-prompt! p . rest)
145   (set! prompt p)
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))
153         ((port-closed? p)
154          (scm-error 'misc-error "set-readline-input-port!"
155                     "Port not open: ~S" (list p) #f))
156         (else
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))
163         ((port-closed? p)
164          (scm-error 'misc-error "set-readline-output-port!"
165                     "Port not open: ~S" (list p) #f))
166         (else
167          (set! output-port p))))
169 (define-public (set-readline-read-hook! h)
170   (set! read-hook h))
172 (if (feature? 'regex)
173     (begin
174       (define-public apropos-completion-function
175         (let ((completions '()))
176           (lambda (text cont?)
177             (if (not cont?)
178                 (set! completions
179                       (map symbol->string
180                            (apropos-internal
181                             (string-append "^" (regexp-quote text))))))
182             (if (null? completions)
183                 #f
184                 (let ((retval (car completions)))
185                   (begin (set! completions (cdr completions))
186                          retval))))))
188       (set! *readline-completion-function* apropos-completion-function)
189       ))
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*))
194     (dynamic-wind
195         (lambda ()
196           (set! *readline-completion-function* completer))
197         thunk
198         (lambda ()
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))
207         (set! repl-reader
208               (lambda (prompt)
209                 (dynamic-wind
210                     (lambda ()
211                       (set-buffered-input-continuation?! (readline-port) #f)
212                       (set-readline-prompt! prompt "... ")
213                       (set-readline-read-hook! read-hook))
214                     (lambda () (read))
215                     (lambda ()
216                       (set-readline-prompt! "" "")
217                       (set-readline-read-hook! #f)))))
218         (set! (using-readline?) #t))))