* readline.scm: Typo in regex module name.
[bpt/guile.git] / ice-9 / readline.scm
CommitLineData
a3cd2b98
JB
1;;;; readline.scm --- support functions for command-line editing
2;;;;
3;;;; Copyright (C) 1997 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, 675 Mass Ave, Cambridge, MA 02139, USA.
18;;;;
19;;;; Contributed by Daniel Risacher <risacher@worldnet.att.net>.
f246e585
MD
20;;;; Extensions based upon code by
21;;;; Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>.
a3cd2b98 22
f246e585 23(define-module (ice-9 readline)
edd9ac21 24 :use-module (ice-9 session)
4ffd142c 25 :use-module (ice-9 regex))
a3cd2b98 26
b05d0b94
MD
27;;; MDJ 980513 <djurfeldt@nada.kth.se>:
28;;; There should probably be low-level support instead of this code.
29
a3cd2b98 30(define prompt "")
b05d0b94
MD
31(define input-port (current-input-port))
32(define output-port (current-output-port))
33(define read-hook #f)
a3cd2b98
JB
34
35(define (make-readline-port)
36 (let ((read-string "")
37 (string-index -1))
38 (letrec ((get-character
39 (lambda ()
40 (cond
41 ((eof-object? read-string)
42 read-string)
43 ((>= string-index (string-length read-string))
44 (begin
45 (set! string-index -1)
46 #\nl))
47 ((= string-index -1)
48 (begin
b05d0b94
MD
49 (set! read-string
50 (%readline (if (string? prompt)
51 prompt
52 (prompt))
53 input-port
54 output-port
55 read-hook))
a3cd2b98
JB
56 (set! string-index 0)
57 (if (not (eof-object? read-string))
58 (begin
59 (or (string=? read-string "")
60 (add-history read-string))
61 (get-character))
62 read-string)))
63 (else
64 (let ((res (string-ref read-string string-index)))
65 (set! string-index (+ 1 string-index))
66 res))))))
67 (make-soft-port
68 (vector write-char display #f get-character #f)
69 "rw"))))
70
71;;; We only create one readline port. There's no point in having
72;;; more, since they would all share the tty and history ---
73;;; everything except the prompt. And don't forget the
b05d0b94
MD
74;;; compile/load/run phase distinctions. Also, the readline library
75;;; isn't reentrant.
a3cd2b98
JB
76(define the-readline-port #f)
77
78(define-public (readline-port)
79 (if (not the-readline-port)
80 (set! the-readline-port (make-readline-port)))
81 the-readline-port)
82
b05d0b94
MD
83;;; The user might try to use readline in his programs. It then
84;;; becomes very uncomfortable that the current-input-port is the
85;;; readline port...
86;;;
87;;; Here, we detect this situation and replace it with the
88;;; underlying port.
89;;;
90;;; %readline is the orginal readline procedure.
91(if (not (defined? '%readline))
92 (begin
93 (define-public %readline readline)
94 (variable-set! (builtin-variable 'readline)
95 (lambda args
96 (let ((prompt prompt)
97 (inp input-port))
98 (cond ((not (null? args))
99 (set! prompt (car args))
100 (set! args (cdr args))
101 (cond ((not (null? args))
102 (set! inp (car args))
103 (set! args (cdr args))))))
104 (apply %readline
105 prompt
106 (if (eq? inp the-readline-port)
107 input-port
108 inp)
109 args))))))
110
a3cd2b98
JB
111(define-public (set-readline-prompt! p)
112 (set! prompt p))
113
b05d0b94
MD
114(define-public (set-readline-input-port! p)
115 (set! input-port p))
116
117(define-public (set-readline-output-port! p)
118 (set! output-port p))
119
120(define-public (set-readline-read-hook! h)
121 (set! read-hook h))
122
f246e585
MD
123(define-public apropos-completion-function
124 (let ((completions '()))
125 (lambda (text cont?)
126 (if (not cont?)
127 (set! completions
128 (map symbol->string
edd9ac21
MD
129 (apropos-internal (string-append "^"
130 (regexp-quote text))))))
f246e585
MD
131 (if (null? completions)
132 #f
133 (let ((retval (car completions)))
134 (begin (set! completions (cdr completions))
135 retval))))))
136
137(set! *readline-completion-function* apropos-completion-function)