readline.scm 9.5 KB

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