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, 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))
28 ;;; Dynamically link the glue code for accessing the readline library
30 (dynamic-call "scm_init_readline" (dynamic-link "libguilereadline.so"))
32 ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
33 ;;; There should probably be low-level support instead of this code.
37 (define input-port (current-input-port))
38 (define output-port (current-output-port))
41 (define (make-readline-port)
42 (let ((read-string "")
44 (letrec ((get-character
47 ((eof-object? read-string)
49 ((>= string-index (string-length read-string))
51 (set! string-index -1)
56 (%readline (if (string? prompt)
63 (if (not (eof-object? read-string))
65 (or (string=? read-string "")
67 (add-history read-string)
68 (set! prompt prompt2)))
72 (let ((res (string-ref read-string string-index)))
73 (set! string-index (+ 1 string-index))
76 (vector write-char display #f get-character #f)
79 ;;; We only create one readline port. There's no point in having
80 ;;; more, since they would all share the tty and history ---
81 ;;; everything except the prompt. And don't forget the
82 ;;; compile/load/run phase distinctions. Also, the readline library
84 (define the-readline-port #f)
86 (define history-variable "GUILE_HISTORY")
87 (define history-file (string-append (getenv "HOME") "/.guile_history"))
89 (define-public readline-port
90 (let ((do (lambda (r/w)
91 (if (memq 'history-file (readline-options-interface))
92 (r/w (or (getenv history-variable)
95 (if (not the-readline-port)
98 (set! the-readline-port (make-readline-port))
99 (add-hook! exit-hook (lambda () (do write-history)))))
102 ;;; The user might try to use readline in his programs. It then
103 ;;; becomes very uncomfortable that the current-input-port is the
106 ;;; Here, we detect this situation and replace it with the
109 ;;; %readline is the low-level readline procedure.
111 (define-public (readline . args)
112 (let ((prompt prompt)
114 (cond ((not (null? args))
115 (set! prompt (car args))
116 (set! args (cdr args))
117 (cond ((not (null? args))
118 (set! inp (car args))
119 (set! args (cdr args))))))
122 (if (eq? inp the-readline-port)
127 (define-public (set-readline-prompt! p . rest)
129 (if (not (null? rest))
130 (set! prompt2 (car rest))))
132 (define-public (set-readline-input-port! p)
135 (define-public (set-readline-output-port! p)
136 (set! output-port p))
138 (define-public (set-readline-read-hook! h)
141 (define-public apropos-completion-function
142 (let ((completions '()))
147 (apropos-internal (string-append "^"
148 (regexp-quote text))))))
149 (if (null? completions)
151 (let ((retval (car completions)))
152 (begin (set! completions (cdr completions))
155 (set! *readline-completion-function* apropos-completion-function)
157 (define-public (activate-readline)
158 (if (and (isatty? (current-input-port))
159 (not (and (module-defined? the-root-module
160 'use-emacs-interface)
161 use-emacs-interface)))
162 (let ((read-hook (lambda () (run-hook before-read-hook))))
163 (set-current-input-port (readline-port))
168 (set-readline-prompt! prompt "... ")
169 (set-readline-read-hook! read-hook))
172 (set-readline-prompt! "" "")
173 (set-readline-read-hook! #f))))))))