1 ;;;; readline.scm --- support functions for command-line editing
3 ;;;; Copyright (C) 1997, 1999, 2000 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>.
24 (define-module (ice-9 readline)
25 :use-module (ice-9 session)
26 :use-module (ice-9 regex)
29 ;;; Dynamically link the glue code for accessing the readline library,
30 ;;; but only when it isn't already present.
32 (if (not (feature? 'readline))
33 (dynamic-call "scm_init_readline" (dynamic-link "libguilereadline.so")))
35 (if (not (feature? 'readline))
36 (scm-error 'misc-error
38 "readline is not provided in this Guile installation"
42 ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
43 ;;; There should probably be low-level support instead of this code.
47 (define input-port (current-input-port))
48 (define output-port (current-output-port))
51 (define (make-readline-port)
52 (let ((read-string "")
54 (letrec ((get-character
57 ((eof-object? read-string)
59 ((>= string-index (string-length read-string))
61 (set! string-index -1)
66 (%readline (if (string? prompt)
73 (if (not (eof-object? read-string))
75 (or (string=? read-string "")
77 (add-history read-string)
78 (set! prompt prompt2)))
82 (let ((res (string-ref read-string string-index)))
83 (set! string-index (+ 1 string-index))
86 (vector write-char display #f get-character #f)
89 ;;; We only create one readline port. There's no point in having
90 ;;; more, since they would all share the tty and history ---
91 ;;; everything except the prompt. And don't forget the
92 ;;; compile/load/run phase distinctions. Also, the readline library
94 (define the-readline-port #f)
96 (define history-variable "GUILE_HISTORY")
97 (define history-file (string-append (getenv "HOME") "/.guile_history"))
99 (define-public readline-port
100 (let ((do (lambda (r/w)
101 (if (memq 'history-file (readline-options-interface))
102 (r/w (or (getenv history-variable)
105 (if (not the-readline-port)
108 (set! the-readline-port (make-readline-port))
109 (add-hook! exit-hook (lambda () (do write-history)))))
112 ;;; The user might try to use readline in his programs. It then
113 ;;; becomes very uncomfortable that the current-input-port is the
116 ;;; Here, we detect this situation and replace it with the
119 ;;; %readline is the low-level readline procedure.
121 (define-public (readline . args)
122 (let ((prompt prompt)
124 (cond ((not (null? args))
125 (set! prompt (car args))
126 (set! args (cdr args))
127 (cond ((not (null? args))
128 (set! inp (car args))
129 (set! args (cdr args))))))
132 (if (eq? inp the-readline-port)
137 (define-public (set-readline-prompt! p . rest)
139 (if (not (null? rest))
140 (set! prompt2 (car rest))))
142 (define-public (set-readline-input-port! p)
145 (define-public (set-readline-output-port! p)
146 (set! output-port p))
148 (define-public (set-readline-read-hook! h)
151 (if (feature? 'regex)
153 (define-public apropos-completion-function
154 (let ((completions '()))
160 (string-append "^" (regexp-quote text))))))
161 (if (null? completions)
163 (let ((retval (car completions)))
164 (begin (set! completions (cdr completions))
167 (set! *readline-completion-function* apropos-completion-function)
170 (define-public (activate-readline)
171 (if (and (isatty? (current-input-port))
172 (not (and (module-defined? the-root-module
173 'use-emacs-interface)
174 use-emacs-interface)))
175 (let ((read-hook (lambda () (run-hook before-read-hook))))
176 (set-current-input-port (readline-port))
181 (set-readline-prompt! prompt "... ")
182 (set-readline-read-hook! read-hook))
185 (set-readline-prompt! "" "")
186 (set-readline-read-hook! #f)))))
187 (set! (using-readline?) #t))))