repl-modules.texi 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2010, 2011
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Readline Support
  7. @section Readline Support
  8. @c FIXME::martin: Review me!
  9. @cindex readline
  10. @cindex command line history
  11. Guile comes with an interface module to the readline library
  12. (@pxref{Top,,, readline, GNU Readline Library}). This
  13. makes interactive use much more convenient, because of the command-line
  14. editing features of readline. Using @code{(ice-9 readline)}, you can
  15. navigate through the current input line with the cursor keys, retrieve
  16. older command lines from the input history and even search through the
  17. history entries.
  18. @menu
  19. * Loading Readline Support:: How to load readline support into Guile.
  20. * Readline Options:: How to modify readline's behaviour.
  21. * Readline Functions:: Programming with readline.
  22. @end menu
  23. @node Loading Readline Support
  24. @subsection Loading Readline Support
  25. The module is not loaded by default and so has to be loaded and
  26. activated explicitly. This is done with two simple lines of code:
  27. @lisp
  28. (use-modules (ice-9 readline))
  29. (activate-readline)
  30. @end lisp
  31. @c FIXME::martin: Review me!
  32. The first line will load the necessary code, and the second will
  33. activate readline's features for the REPL. If you plan to use this
  34. module often, you should save these to lines to your @file{.guile}
  35. personal startup file.
  36. You will notice that the REPL's behaviour changes a bit when you have
  37. loaded the readline module. For example, when you press Enter before
  38. typing in the closing parentheses of a list, you will see the
  39. @dfn{continuation} prompt, three dots: @code{...} This gives you a nice
  40. visual feedback when trying to match parentheses. To make this even
  41. easier, @dfn{bouncing parentheses} are implemented. That means that
  42. when you type in a closing parentheses, the cursor will jump to the
  43. corresponding opening parenthesis for a short time, making it trivial to make
  44. them match.
  45. Once the readline module is activated, all lines entered interactively
  46. will be stored in a history and can be recalled later using the
  47. cursor-up and -down keys. Readline also understands the Emacs keys for
  48. navigating through the command line and history.
  49. @cindex @file{.guile_history}
  50. When you quit your Guile session by evaluating @code{(quit)} or pressing
  51. Ctrl-D, the history will be saved to the file @file{.guile_history} and
  52. read in when you start Guile for the next time. Thus you can start a
  53. new Guile session and still have the (probably long-winded) definition
  54. expressions available.
  55. @cindex @env{GUILE_HISTORY}
  56. @cindex @file{.inputrc}
  57. You can specify a different history file by setting the environment
  58. variable @env{GUILE_HISTORY}. And you can make Guile specific
  59. customizations to your @file{.inputrc} by testing for application
  60. @samp{Guile} (@pxref{Conditional Init Constructs,,, readline, GNU
  61. Readline Library}). For instance to define a key inserting a matched
  62. pair of parentheses,
  63. @example
  64. $if Guile
  65. "\C-o": "()\C-b"
  66. $endif
  67. @end example
  68. @node Readline Options
  69. @subsection Readline Options
  70. @cindex readline options
  71. The readline interface module can be tweaked in a few ways to better
  72. suit the user's needs. Configuration is done via the readline module's
  73. options interface, in a similar way to the evaluator and debugging
  74. options (@pxref{Runtime Options}).
  75. @deffn {Scheme Procedure} readline-options
  76. @deffnx {Scheme Procedure} readline-enable option-name
  77. @deffnx {Scheme Procedure} readline-disable option-name
  78. @deffnx {Scheme Syntax} readline-set! option-name value
  79. Accessors for the readline options. Note that unlike the enable/disable
  80. procedures, @code{readline-set!} is syntax, which expects an unquoted
  81. option name.
  82. @end deffn
  83. Here is the list of readline options generated by typing
  84. @code{(readline-options 'help)} in Guile. You can also see the
  85. default values.
  86. @smalllisp
  87. history-file yes Use history file.
  88. history-length 200 History length.
  89. bounce-parens 500 Time (ms) to show matching opening parenthesis
  90. (0 = off).
  91. bracketed-paste yes Disable interpretation of control characters
  92. in pastes.
  93. @end smalllisp
  94. The readline options interface can only be used @emph{after} loading
  95. the readline module, because it is defined in that module.
  96. @node Readline Functions
  97. @subsection Readline Functions
  98. The following functions are provided by
  99. @example
  100. (use-modules (ice-9 readline))
  101. @end example
  102. There are two ways to use readline from Scheme code, either make calls
  103. to @code{readline} directly to get line by line input, or use the
  104. readline port below with all the usual reading functions.
  105. @defun readline [prompt]
  106. Read a line of input from the user and return it as a string (without
  107. a newline at the end). @var{prompt} is the prompt to show, or the
  108. default is the string set in @code{set-readline-prompt!} below.
  109. @example
  110. (readline "Type something: ") @result{} "hello"
  111. @end example
  112. @end defun
  113. @defun set-readline-input-port! port
  114. @defunx set-readline-output-port! port
  115. Set the input and output port the readline function should read from
  116. and write to. @var{port} must be a file port (@pxref{File Ports}),
  117. and should usually be a terminal.
  118. The default is the @code{current-input-port} and
  119. @code{current-output-port} (@pxref{Default Ports}) when @code{(ice-9
  120. readline)} loads, which in an interactive user session means the Unix
  121. ``standard input'' and ``standard output''.
  122. @end defun
  123. @subsubsection Readline Port
  124. @defun readline-port
  125. Return a buffered input port (@pxref{Buffered Input}) which calls the
  126. @code{readline} function above to get input. This port can be used
  127. with all the usual reading functions (@code{read}, @code{read-char},
  128. etc), and the user gets the interactive editing features of readline.
  129. There's only a single readline port created. @code{readline-port}
  130. creates it when first called, and on subsequent calls just returns
  131. what it previously made.
  132. @end defun
  133. @defun activate-readline
  134. If the @code{current-input-port} is a terminal (@pxref{Terminals and
  135. Ptys,, @code{isatty?}}) then enable readline for all reading from
  136. @code{current-input-port} (@pxref{Default Ports}) and enable readline
  137. features in the interactive REPL (@pxref{The REPL}).
  138. @example
  139. (activate-readline)
  140. (read-char)
  141. @end example
  142. @code{activate-readline} enables readline on @code{current-input-port}
  143. simply by a @code{set-current-input-port} to the @code{readline-port}
  144. above. An application can do that directly if the extra REPL features
  145. that @code{activate-readline} adds are not wanted.
  146. @end defun
  147. @defun set-readline-prompt! prompt1 [prompt2]
  148. Set the prompt string to print when reading input. This is used when
  149. reading through @code{readline-port}, and is also the default prompt
  150. for the @code{readline} function above.
  151. @var{prompt1} is the initial prompt shown. If a user might enter an
  152. expression across multiple lines, then @var{prompt2} is a different
  153. prompt to show further input required. In the Guile REPL for instance
  154. this is an ellipsis (@samp{...}).
  155. See @code{set-buffered-input-continuation?!} (@pxref{Buffered Input})
  156. for an application to indicate the boundaries of logical expressions
  157. (assuming of course an application has such a notion).
  158. @end defun
  159. @subsubsection Completion
  160. @defun with-readline-completion-function completer thunk
  161. Call @code{(@var{thunk})} with @var{completer} as the readline tab
  162. completion function to be used in any readline calls within that
  163. @var{thunk}. @var{completer} can be @code{#f} for no completion.
  164. @var{completer} will be called as @code{(@var{completer} text state)},
  165. as described in (@pxref{How Completing Works,,, readline, GNU Readline
  166. Library}). @var{text} is a partial word to be completed, and each
  167. @var{completer} call should return a possible completion string or
  168. @code{#f} when no more. @var{state} is @code{#f} for the first call
  169. asking about a new @var{text} then @code{#t} while getting further
  170. completions of that @var{text}.
  171. Here's an example @var{completer} for user login names from the
  172. password file (@pxref{User Information}), much like readline's own
  173. @code{rl_username_completion_function},
  174. @example
  175. (define (username-completer-function text state)
  176. (if (not state)
  177. (setpwent)) ;; new, go to start of database
  178. (let more ((pw (getpwent)))
  179. (if pw
  180. (if (string-prefix? text (passwd:name pw))
  181. (passwd:name pw) ;; this name matches, return it
  182. (more (getpwent))) ;; doesn't match, look at next
  183. (begin
  184. ;; end of database, close it and return #f
  185. (endpwent)
  186. #f))))
  187. @end example
  188. @end defun
  189. @defun apropos-completion-function text state
  190. A completion function offering completions for Guile functions and
  191. variables (all @code{define}s). This is the default completion
  192. function.
  193. @c
  194. @c FIXME: Cross reference the ``apropos'' stuff when it's documented.
  195. @c
  196. @end defun
  197. @defun filename-completion-function text state
  198. A completion function offering filename completions. This is
  199. readline's @code{rl_filename_completion_function} (@pxref{Completion
  200. Functions,,, readline, GNU Readline Library}).
  201. @end defun
  202. @defun make-completion-function string-list
  203. Return a completion function which offers completions from the
  204. possibilities in @var{string-list}. Matching is case-sensitive.
  205. @end defun
  206. @c Local Variables:
  207. @c TeX-master: "guile.texi"
  208. @c End: