1 ;;;; readline.scm --- support functions for command-line editing
3 ;;;; Copyright (C) 1997 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, 675 Mass Ave, Cambridge, MA 02139, USA.
19 ;;;; Contributed by Daniel Risacher <risacher@worldnet.att.net>.
20 ;;;; Extensions based upon code by
21 ;;;; Andrew Archibald <aarchiba@undergrad.math.uwaterloo.ca>.
23 (define-module (ice-9 readline)
24 :use-module (ice-9 session)
25 :use-module (ice-9 regex))
27 ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
28 ;;; There should probably be low-level support instead of this code.
31 (define input-port (current-input-port))
32 (define output-port (current-output-port))
35 (define (make-readline-port)
36 (let ((read-string "")
38 (letrec ((get-character
41 ((eof-object? read-string)
43 ((>= string-index (string-length read-string))
45 (set! string-index -1)
50 (%readline (if (string? prompt)
57 (if (not (eof-object? read-string))
59 (or (string=? read-string "")
61 (add-history read-string)
62 (set! prompt "... ")))
66 (let ((res (string-ref read-string string-index)))
67 (set! string-index (+ 1 string-index))
70 (vector write-char display #f get-character #f)
73 ;;; We only create one readline port. There's no point in having
74 ;;; more, since they would all share the tty and history ---
75 ;;; everything except the prompt. And don't forget the
76 ;;; compile/load/run phase distinctions. Also, the readline library
78 (define the-readline-port #f)
80 (define history-variable "GUILE_HISTORY")
81 (define history-file (string-append (getenv "HOME") "/.guile_history"))
83 (define-public readline-port
84 (let ((do (lambda (r/w)
85 (if (memq 'history-file (readline-options-interface))
86 (r/w (or (getenv history-variable)
89 (if (not the-readline-port)
92 (set! the-readline-port (make-readline-port))
93 (add-hook! exit-hook (lambda () (do write-history)))))
96 ;;; The user might try to use readline in his programs. It then
97 ;;; becomes very uncomfortable that the current-input-port is the
100 ;;; Here, we detect this situation and replace it with the
103 ;;; %readline is the orginal readline procedure.
104 (if (not (defined? '%readline))
106 (define-public %readline readline)
107 (variable-set! (builtin-variable 'readline)
109 (let ((prompt prompt)
111 (cond ((not (null? args))
112 (set! prompt (car args))
113 (set! args (cdr args))
114 (cond ((not (null? args))
115 (set! inp (car args))
116 (set! args (cdr args))))))
119 (if (eq? inp the-readline-port)
124 (define-public (set-readline-prompt! p)
127 (define-public (set-readline-input-port! p)
130 (define-public (set-readline-output-port! p)
131 (set! output-port p))
133 (define-public (set-readline-read-hook! h)
136 (define-public apropos-completion-function
137 (let ((completions '()))
142 (apropos-internal (string-append "^"
143 (regexp-quote text))))))
144 (if (null? completions)
146 (let ((retval (car completions)))
147 (begin (set! completions (cdr completions))
150 (set! *readline-completion-function* apropos-completion-function)