readline.scm 7.8 KB

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