home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / guile / 1.6 / ice-9 / readline.scm < prev    next >
Encoding:
Text File  |  2006-06-19  |  7.1 KB  |  221 lines

  1. ;;;; readline.scm --- support functions for command-line editing
  2. ;;;;
  3. ;;;;     Copyright (C) 1997, 1999, 2000, 2001, 2002, 2005 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., 51 Franklin Street, Fifth Floor,
  18. ;;;; Boston, MA 02110-1301 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>.
  23.  
  24.  
  25.  
  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)
  31.  
  32.  
  33.  
  34. ;;; Dynamically link the glue code for accessing the readline library,
  35. ;;; but only when it isn't already present.
  36.  
  37. (if (not (provided? 'readline))
  38.     (dynamic-call "scm_init_readline"
  39.                   (dynamic-link "libguilereadline-v-12")))
  40.  
  41. (if (not (provided? 'readline))
  42.     (scm-error 'misc-error
  43.            #f
  44.            "readline is not provided in this Guile installation"
  45.            '()
  46.            '()))
  47.  
  48.  
  49.  
  50. ;;; Run-time options
  51.  
  52. (export
  53.  readline-options-interface ;; for readline-set! macro
  54.  readline-options
  55.  readline-enable
  56.  readline-disable)
  57. (export-syntax
  58.  readline-set!)
  59.  
  60. (define-option-interface
  61.   (readline-options-interface
  62.    (readline-options readline-enable readline-disable)
  63.    (readline-set!)))
  64.  
  65.  
  66.  
  67. ;;; MDJ 980513 <djurfeldt@nada.kth.se>:
  68. ;;; There should probably be low-level support instead of this code.
  69.  
  70. ;;; Dirk:FIXME:: If the-readline-port, input-port or output-port are closed,
  71. ;;; guile will enter an endless loop or crash.
  72.  
  73. (define prompt "")
  74. (define prompt2 "")
  75. (define input-port (current-input-port))
  76. (define output-port (current-output-port))
  77. (define read-hook #f)
  78.  
  79. (define (make-readline-port)
  80.   (make-line-buffered-input-port (lambda (continuation?)
  81.                                    (let* ((prompt (if continuation?
  82.                                                       prompt2
  83.                                                       prompt))
  84.                                           (str (%readline (if (string? prompt)
  85.                                                               prompt
  86.                                                               (prompt))
  87.                                                           input-port
  88.                                                           output-port
  89.                                                           read-hook)))
  90.                                      (or (eof-object? str)
  91.                                          (string=? str "")
  92.                                          (add-history str))
  93.                                      str))))
  94.  
  95. ;;; We only create one readline port.  There's no point in having
  96. ;;; more, since they would all share the tty and history ---
  97. ;;; everything except the prompt.  And don't forget the
  98. ;;; compile/load/run phase distinctions.  Also, the readline library
  99. ;;; isn't reentrant.
  100. (define the-readline-port #f)
  101.  
  102. (define history-variable "GUILE_HISTORY")
  103. (define history-file (string-append (getenv "HOME") "/.guile_history"))
  104.  
  105. (define-public readline-port
  106.   (let ((do (lambda (r/w)
  107.           (if (memq 'history-file (readline-options-interface))
  108.           (r/w (or (getenv history-variable)
  109.                history-file))))))
  110.     (lambda ()
  111.       (if (not the-readline-port)
  112.       (begin
  113.         (do read-history) 
  114.         (set! the-readline-port (make-readline-port))
  115.         (add-hook! exit-hook (lambda () 
  116.                    (do write-history)
  117.                    (clear-history)))))
  118.       the-readline-port)))
  119.  
  120. ;;; The user might try to use readline in his programs.  It then
  121. ;;; becomes very uncomfortable that the current-input-port is the
  122. ;;; readline port...
  123. ;;;
  124. ;;; Here, we detect this situation and replace it with the
  125. ;;; underlying port.
  126. ;;;
  127. ;;; %readline is the low-level readline procedure.
  128.  
  129. (define-public (readline . args)
  130.   (let ((prompt prompt)
  131.     (inp input-port))
  132.     (cond ((not (null? args))
  133.        (set! prompt (car args))
  134.        (set! args (cdr args))
  135.        (cond ((not (null? args))
  136.           (set! inp (car args))
  137.           (set! args (cdr args))))))
  138.     (apply %readline
  139.        prompt
  140.        (if (eq? inp the-readline-port)
  141.            input-port
  142.            inp)
  143.        args)))
  144.  
  145. (define-public (set-readline-prompt! p . rest)
  146.   (set! prompt p)
  147.   (if (not (null? rest))
  148.       (set! prompt2 (car rest))))
  149.  
  150. (define-public (set-readline-input-port! p)
  151.   (cond ((or (not (file-port? p)) (not (input-port? p)))
  152.      (scm-error 'wrong-type-arg "set-readline-input-port!"
  153.             "Not a file input port: ~S" (list p) #f))
  154.     ((port-closed? p)
  155.      (scm-error 'misc-error "set-readline-input-port!"
  156.             "Port not open: ~S" (list p) #f))
  157.     (else
  158.      (set! input-port p))))
  159.  
  160. (define-public (set-readline-output-port! p)
  161.   (cond ((or (not (file-port? p)) (not (output-port? p)))
  162.      (scm-error 'wrong-type-arg "set-readline-input-port!"
  163.             "Not a file output port: ~S" (list p) #f))
  164.     ((port-closed? p)
  165.      (scm-error 'misc-error "set-readline-output-port!"
  166.             "Port not open: ~S" (list p) #f))
  167.     (else
  168.      (set! output-port p))))
  169.  
  170. (define-public (set-readline-read-hook! h)
  171.   (set! read-hook h))
  172.  
  173. (if (provided? 'regex)
  174.     (begin
  175.       (define-public apropos-completion-function
  176.     (let ((completions '()))
  177.       (lambda (text cont?)
  178.         (if (not cont?)
  179.         (set! completions
  180.               (map symbol->string
  181.                (apropos-internal
  182.                 (string-append "^" (regexp-quote text))))))
  183.         (if (null? completions)
  184.         #f
  185.         (let ((retval (car completions)))
  186.           (begin (set! completions (cdr completions))
  187.              retval))))))
  188.  
  189.       (set! *readline-completion-function* apropos-completion-function)
  190.       ))
  191.  
  192. (define-public (with-readline-completion-function completer thunk)
  193.   "With @var{completer} as readline completion function, call @var{thunk}."
  194.   (let ((old-completer *readline-completion-function*))
  195.     (dynamic-wind
  196.     (lambda ()
  197.       (set! *readline-completion-function* completer))
  198.     thunk
  199.     (lambda ()
  200.       (set! *readline-completion-function* old-completer)))))
  201.  
  202. (define-public (activate-readline)
  203.   (if (and (isatty? (current-input-port))
  204.        (not (let ((guile-user-module (resolve-module '(guile-user))))
  205.           (and (module-defined? guile-user-module 'use-emacs-interface)
  206.                (module-ref guile-user-module 'use-emacs-interface)))))
  207.       (let ((read-hook (lambda () (run-hook before-read-hook))))
  208.     (set-current-input-port (readline-port))
  209.     (set! repl-reader
  210.           (lambda (prompt)
  211.         (dynamic-wind
  212.             (lambda ()
  213.                       (set-buffered-input-continuation?! (readline-port) #f)
  214.               (set-readline-prompt! prompt "... ")
  215.               (set-readline-read-hook! read-hook))
  216.             (lambda () (read))
  217.             (lambda ()
  218.               (set-readline-prompt! "" "")
  219.               (set-readline-read-hook! #f)))))
  220.     (set! (using-readline?) #t))))
  221.