repl-modules.texi 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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
  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. @c FIXME::martin: Review me!
  71. @cindex readline options
  72. The readline interface module can be configured in several ways to
  73. better suit the user's needs. Configuration is done via the readline
  74. module's options interface, in a similar way to the evaluator and
  75. debugging options (@pxref{Runtime Options}).
  76. @findex readline-options
  77. @findex readline-enable
  78. @findex readline-disable
  79. @findex readline-set!
  80. Here is the list of readline options generated by typing
  81. @code{(readline-options 'help)} in Guile. You can also see the
  82. default values.
  83. @smalllisp
  84. history-file yes Use history file.
  85. history-length 200 History length.
  86. bounce-parens 500 Time (ms) to show matching opening parenthesis
  87. (0 = off).
  88. @end smalllisp
  89. The history length specifies how many input lines will be remembered.
  90. If the history contains that many lines and additional lines are
  91. entered, the oldest lines will be lost. You can switch on/off the
  92. usage of the history file using the following call.
  93. @lisp
  94. (readline-disable 'history)
  95. @end lisp
  96. The readline options interface can only be used @emph{after} loading
  97. the readline module, because it is defined in that module.
  98. @node Readline Functions
  99. @subsection Readline Functions
  100. The following functions are provided by
  101. @example
  102. (use-modules (ice-9 readline))
  103. @end example
  104. There are two ways to use readline from Scheme code, either make calls
  105. to @code{readline} directly to get line by line input, or use the
  106. readline port below with all the usual reading functions.
  107. @defun readline [prompt]
  108. Read a line of input from the user and return it as a string (without
  109. a newline at the end). @var{prompt} is the prompt to show, or the
  110. default is the string set in @code{set-readline-prompt!} below.
  111. @example
  112. (readline "Type something: ") @result{} "hello"
  113. @end example
  114. @end defun
  115. @defun set-readline-input-port! port
  116. @defunx set-readline-output-port! port
  117. Set the input and output port the readline function should read from
  118. and write to. @var{port} must be a file port (@pxref{File Ports}),
  119. and should usually be a terminal.
  120. The default is the @code{current-input-port} and
  121. @code{current-output-port} (@pxref{Default Ports}) when @code{(ice-9
  122. readline)} loads, which in an interactive user session means the Unix
  123. ``standard input'' and ``standard output''.
  124. @end defun
  125. @subsubsection Readline Port
  126. @defun readline-port
  127. Return a buffered input port (@pxref{Buffered Input}) which calls the
  128. @code{readline} function above to get input. This port can be used
  129. with all the usual reading functions (@code{read}, @code{read-char},
  130. etc), and the user gets the interactive editing features of readline.
  131. There's only a single readline port created. @code{readline-port}
  132. creates it when first called, and on subsequent calls just returns
  133. what it previously made.
  134. @end defun
  135. @defun activate-readline
  136. If the @code{current-input-port} is a terminal (@pxref{Terminals and
  137. Ptys,, @code{isatty?}}) then enable readline for all reading from
  138. @code{current-input-port} (@pxref{Default Ports}) and enable readline
  139. features in the interactive REPL (@pxref{The REPL}).
  140. @example
  141. (activate-readline)
  142. (read-char)
  143. @end example
  144. @code{activate-readline} enables readline on @code{current-input-port}
  145. simply by a @code{set-current-input-port} to the @code{readline-port}
  146. above. An application can do that directly if the extra REPL features
  147. that @code{activate-readline} adds are not wanted.
  148. @end defun
  149. @defun set-readline-prompt! prompt1 [prompt2]
  150. Set the prompt string to print when reading input. This is used when
  151. reading through @code{readline-port}, and is also the default prompt
  152. for the @code{readline} function above.
  153. @var{prompt1} is the initial prompt shown. If a user might enter an
  154. expression across multiple lines, then @var{prompt2} is a different
  155. prompt to show further input required. In the Guile REPL for instance
  156. this is an ellipsis (@samp{...}).
  157. See @code{set-buffered-input-continuation?!} (@pxref{Buffered Input})
  158. for an application to indicate the boundaries of logical expressions
  159. (assuming of course an application has such a notion).
  160. @end defun
  161. @subsubsection Completion
  162. @defun with-readline-completion-function completer thunk
  163. Call @code{(@var{thunk})} with @var{completer} as the readline tab
  164. completion function to be used in any readline calls within that
  165. @var{thunk}. @var{completer} can be @code{#f} for no completion.
  166. @var{completer} will be called as @code{(@var{completer} text state)},
  167. as described in (@pxref{How Completing Works,,, readline, GNU Readline
  168. Library}). @var{text} is a partial word to be completed, and each
  169. @var{completer} call should return a possible completion string or
  170. @code{#f} when no more. @var{state} is @code{#f} for the first call
  171. asking about a new @var{text} then @code{#t} while getting further
  172. completions of that @var{text}.
  173. Here's an example @var{completer} for user login names from the
  174. password file (@pxref{User Information}), much like readline's own
  175. @code{rl_username_completion_function},
  176. @example
  177. (define (username-completer-function text state)
  178. (if (not state)
  179. (setpwent)) ;; new, go to start of database
  180. (let more ((pw (getpwent)))
  181. (if pw
  182. (if (string-prefix? text (passwd:name pw))
  183. (passwd:name pw) ;; this name matches, return it
  184. (more (getpwent))) ;; doesn't match, look at next
  185. (begin
  186. ;; end of database, close it and return #f
  187. (endpwent)
  188. #f))))
  189. @end example
  190. @end defun
  191. @defun apropos-completion-function text state
  192. A completion function offering completions for Guile functions and
  193. variables (all @code{define}s). This is the default completion
  194. function.
  195. @c
  196. @c FIXME: Cross reference the ``apropos'' stuff when it's documented.
  197. @c
  198. @end defun
  199. @defun filename-completion-function text state
  200. A completion function offering filename completions. This is
  201. readline's @code{rl_filename_completion_function} (@pxref{Completion
  202. Functions,,, readline, GNU Readline Library}).
  203. @end defun
  204. @defun make-completion-function string-list
  205. Return a completion function which offers completions from the
  206. possibilities in @var{string-list}. Matching is case-sensitive.
  207. @end defun
  208. @c Local Variables:
  209. @c TeX-master: "guile.texi"
  210. @c End: