REPL: new customizable faces for input and prompt
[geiser.git] / doc / repl.texi
blob99b928cadc56771abbfd8afbeda6d50e143bc614
1 @node The REPL, Between the parens, Installation, Top
2 @chapter The REPL
3 @anchor{quick-start}
4 If you've followed the instructions in @ref{Setting it up}, your Emacs is
5 now ready to start playing.  Otherwise, i'll wait for you: when you're
6 ready, just come back here and proceed to the following sections.
8 @menu
9 * Starting the REPL::
10 * First aids::
11 * Switching context::
12 * Completion and error handling::
13 * Autodoc and friends::
14 * Customization and tips::
15 @end menu
17 @node Starting the REPL, First aids, The REPL, The REPL
18 @section Starting the REPL
20 @cindex REPL
21 To start a Scheme REPL (meaning, a Scheme process offering you a
22 Read-Eval-Print Loop), Geiser provides the generic interactive command
23 @command{run-geiser}.  If you invoke it (via, as is customary in Emacs,
24 @kbd{M-x run-geiser}), you'll be saluted by a prompt asking which one of
25 the supported implementations you want to launch---yes, you can stop the
26 asking, see @altr{active-implementations,below,Customization and tips,.}
27 Tabbing for completion will offer you, as of this writing, @code{guile}
28 and @code{racket}.  Just choose your poison, and a new REPL buffer will
29 pop up (by default, the REPL will appear in a new window: if that annoys
30 you, just set @code{geiser-repl-use-other-window} to @code{nil} and the
31 current window will be used).
33 @imgc{repls}
35 If all went according to plan, you'll be facing an
36 implementation-dependent banner, followed by an interactive prompt.
37 Going according to plan includes having the executable of the Scheme you
38 chose in your path.  If that's not the case, you can tell Emacs where it
39 is, as described in @altr{impl-binary,a moment,Customization and tips,.}
40 Returning to our REPL, the first thing to notice is that the funny
41 prompt is telling you your current module: its name is the part just
42 after the @@ sign (in Guile, that means @code{guile-user}, while
43 Racket's top namespace doesn't have a name; cf. discussion in
44 @altr{Switching context,,Switching context,).} Other than that, this is
45 pretty much equivalent to having a command-line interpreter in a
46 terminal, with a bunch of add-ons that we'll be reviewing below.  You
47 can start typing sexps right there: Geiser will only dispatch them for
48 evaluation when they're complete, and will indent new lines properly
49 until then.  It will also keep track of your input, maintaining a
50 history file that will be reloaded whenever you restart the REPL.
52 @cindex REPL, faces
53 @cindex faces, in the REPL
54 If you're not happy with the faces Geiser is using for the REPL's prompt
55 and evaluated input, you can customise
56 @code{geiser-font-lock-repl-prompt} and
57 @code{geiser-font-lock-repl-input} to better looking faces.
59 @subsubheading Connecting to an external Scheme
60 @cindex remote REPL
61 @cindex connect to server
62 There's an alternative way of starting a Geiser REPL: you can connect to
63 an external Scheme process, provided it's running a REPL server at some
64 known port.  How to make that happen depends on the Scheme implementation.
66 @cindex Guile's REPL server
67 If you use Guile, you just need to start your Guile process (possibly
68 outside Emacs) passing to it the flag @code{--listen}.  This flag accepts
69 an optional port as argument (as in @code{--listen=1969}), if you don't
70 want to use the default.
72 @cindex Racket's REPL server
73 In Racket, you have to use the REPL server that comes with Geiser.  To
74 that end, put Geiser's Racket @file{scheme} directory in Racket's
75 collection search path and invoke @code{start-geiser} (a procedure in
76 the module @code{geiser/server}) somewhere in your program, passing it
77 the desired port.  This procedure will start the REPL server in a
78 separate thread.  For an example of how to do that, see the script
79 @file{bin/geiser-racket.sh} in the source distribution, or, if you've
80 compiled Geiser, @file{bin/geiser-racket-noinst} in the build directory,
81 or, if you've installed Geiser, @file{geiser-racket} in
82 @file{<installation-prefix>/bin}.  These scripts start a new interactive
83 Racket that is also running a REPL server (they also load the errortrace
84 library to provide better diagnostics, but that's not strictly needed).
86 With your external Scheme process running and serving, come back to
87 Emacs and execute @kbd{M-x geiser-connect}, @kbd{M-x connect-to-guile}
88 or @kbd{M-x connect-to-racket}.  You'll be asked for a host and a port,
89 and, voila, you'll have a Geiser REPL that is served by the remote
90 Scheme process in a dedicated thread, meaning that your external program
91 can go on doing whatever it was doing while you tinker with it from
92 Emacs.  Note, however, that all Scheme threads share the heap, so that
93 you'll be able to interact with those other threads in the running
94 Scheme from Emacs in a variety of ways.  For starters, all your
95 (re)definitions will be visible everywhere.  That's dangerous, but will
96 come in handy when you need to debug your running web server.
98 @cindex remote connections
99 The connection between Emacs and the Scheme process goes over TCP, so it
100 can be as remote as you need, perhaps with the intervention of an SSH
101 tunnel.
103 @node First aids, Switching context, Starting the REPL, The REPL
104 @section First aids
106 @img{repl-menu, right}
107 @cindex REPL commands
108 A quick way of seeing what else Geiser's REPL can do for you, is to
109 display the corresponding entry up there in your menu bar.  No, i don't
110 normally use menus either; but they can come in handy until you've
111 memorized Geiser's commands, as a learning device.  And yes, i usually
112 run Emacs inside a terminal, but one can always use
113 @uref{http://www.emacswiki.org/emacs/LaCarte, La Carte} to access the
114 menus in a convenient enough fashion.
116 Or just press @kbd{C-h m} and be done with that.
118 Among the commands at your disposal, we find the familiar input
119 navigation keys, with a couple twists.  By default, @kbd{M-p} and
120 @kbd{M-n} are bound to @i{matching} items in your input history.  That
121 is, they'll find the previous or next sexp that starts with the current
122 input prefix (defined as the text between the end of the prompt and your
123 current position, a.k.a.  @dfn{point}, in the buffer).  For going up and
124 down the list unconditionally, just use @kbd{C-c M-p} and @kbd{C-c M-n}.
125 In addition, navigation is sexp-based rather than line-based.
127 There are also a few commands to twiddle with the Scheme process.
128 @kbd{C-c C-q} will gently ask it to quit, while @kbd{C-u C-c C-q} will
129 mercilessly kill the process (but not before stowing your history in the
130 file system).  Unless you're using a remote REPL, that is, in which case
131 both commands will just sever the connection and leave the remote
132 process alone.  If worse comes to worst and the process is dead, @kbd{C-c
133 C-z} will restart it.  However, the same shortcut, issued when the REPL is
134 alive, will bring you back to the buffer you came from, as explained
135 in @altr{switching-repl-buff,this section,The source and the REPL,.}
137 The remaining commands are meatier, and deserve sections of their own.
139 @node Switching context, Completion and error handling, First aids, The REPL
140 @section Switching context
142 @cindex current module, in REPL
143 @cindex ,enter vs. enter!
144 In tune with Geiser's
145 @ifhtml
146 @ref{current-module,,modus operandi},
147 @end ifhtml
148 @ifnothtml
149 @i{modus operandi},
150 @end ifnothtml
151 evaluations in the REPL take place in the namespace of the
152 current module.  As noted above, the REPL's prompt tells you the name of
153 the current module.  To switch to a different one, you can use the
154 command @command{switch-to-geiser-module}, bound to @kbd{C-c C-m}.
155 You'll notice that Geiser simply uses a couple of meta-commands provided
156 by the Scheme REPL (the stock @command{,m} in Guile and the
157 (geiser-defined) @command{,enter} in Racket), and that it doesn't even
158 try to hide that fact.  That means that you can freely use said native
159 ways directly at the REPL, and Geiser will be happy to oblige.  In
160 Racket, @command{,enter} works like Racket's standard @code{enter!}
161 form, but you can also provide a path string as its argument (e.g.,
162 @command{,enter "/tmp/foo.rkt"} is equivalent to @command{,enter (file
163 "/tmp/foo.rkt")}).  Like @code{enter!}, @command{,enter} accepts also
164 module names (as in, say, @command{,enter geiser/main}).  As mentioned,
165 Guile's @command{,m} is used @i{as is}.
167 @cindex current module, change
168 Once you enter a new module, only those bindings visible in its
169 namespace will be available to your evaluations.  All Schemes supported
170 by Geiser provide a way to import new modules in the current namespace.
171 Again, there's a Geiser command, @command{geiser-repl-import-module}, to
172 invoke such functionality, bound this time to @kbd{C-c C-i}.  And, again,
173 you'll see Geiser just introducing the native incantation for you, and
174 you're free to use such incantations by hand whenever you want.
176 One convenience provided by these two Geiser commands is that completion
177 is available when introducing the new module name, using the
178 @kbd{@key{TAB}} key.  Pressing it at the command's prompt will offer you
179 a prefix-aware list of available module names.
181 @imgc{mod-completion}
183 Which brings me to the next group of REPL commands.
185 @node Completion and error handling, Autodoc and friends, Switching context, The REPL
186 @section Completion and error handling
188 @cindex completion, at the REPL
189 We've already seen Geiser completion of module names in action at the
190 minibuffer.  You won't be surprised to know that it's also available at
191 the REPL buffer itself.  There, you can use either @kbd{C-.} or
192 @kbd{M-`} to complete module names, and @kbd{@key{TAB}} or
193 @kbd{M-@key{TAB}} to complete identifiers.  Geiser will know what
194 identifiers are bound in the current module and show you a list of those
195 starting with the prefix at point.  Needless to say, this is not a static
196 list, and it will grow as you define or import new bindings in the
197 namespace at hand.  If no completion is found, @kbd{@key{TAB}} will try
198 to complete the prefix after point as a module name.
200 REPL buffers use Emacs' compilation mode to highlight errors reported by
201 the Scheme interpreter, and you can use the @command{next-error} command
202 (@kbd{M-g n}) to jump to their location.  By default, every time you
203 enter a new expression for evaluation old error messages are forgotten,
204 so that @kbd{M-g n} will always jump to errors related to the last
205 evaluation request, if any.  If you prefer a not-so-forgetful REPL, set
206 the customization variable @code{geiser-repl-forget-old-errors-p} to
207 @code{nil}.  Note, however, that even when that variable is left as
208 @kbd{t}, you can always jump to an old error by moving to its line at
209 the REPL and pressing @kbd{@key{RET}}.  When your cursor is away from
210 the last prompt, @kbd{@key{TAB}} will move to the next error in the
211 buffer, and you can use @kbd{@key{BACKTAB}} everywhere to go to the
212 previous one.
214 @node Autodoc and friends, Customization and tips, Completion and error handling, The REPL
215 @section Autodoc and friends
217 Oftentimes, there's more you'll want to know about an identifier
218 besides its name: What module does it belong to?  Is it a procedure and,
219 if so, what arguments does it take?  Geiser tries to help you answering
220 those questions too.
222 @cindex autodoc, in the REPL
223 Actually, if you've been playing with the REPL as you read, you might
224 have notice some frantic activity taking place in the echo area every
225 now and then.  That was Geiser trying to be helpful (while, hopefully,
226 not being clippy), or, more concretely, what i call, for want of a
227 better name, its @dfn{autodoc} mode.  Whenever it's active (did you
228 notice that @i{A} in the mode-line?), Geiser's gerbils will be scanning
229 what you type and showing (unless you silence them with @kbd{C-c C-d C-a})
230 information about the identifier nearest to point.
232 @imgc{repl-autodoc}
234 If that identifier corresponds to a variable visible in the current
235 namespace, you'll see the module it belongs to and its value.  For
236 procedures and macros, autodoc will display, instead of their value, the
237 argument names (or an underscore if Geiser cannot determine the name
238 used in the definition).  Optional arguments are surrounded by
239 parentheses.  When the optional argument has a default value, it's
240 represented by a list made up of its name and that value.  When the
241 argument is a keyword argument, its name has ``#:'' as a prefix.
243 @cindex help on identifier
244 If that's not enough documentation for you, @kbd{C-c C-d d} will open a
245 separate documentation buffer with help on the symbol at point.  This
246 buffer will contain implementation-specific information about the
247 identifier (e.g., its docstring for Guile, or its contract, if any, for
248 Racket), and a handy button to open the corresponding manual entry for
249 the symbol, which will open an HTML page (for Racket) or the texinfo
250 manual (for Guile).
252 @cindex module exports
253 @anchor{repl-mod} Geiser can also produce for you a list, classified by
254 kind, of the identifiers exported by a given module: all you need to do
255 is press @kbd{C-c C-d m}, and type or complete the desired module's
256 name.
258 @imgc{repl-mod}
260 The list of exported bindings is shown, again, in a buffer belonging to
261 Geiser's documentation browser, where you have at your disposal a bunch
262 of navigation commands listed in @altr{Documentation browser,our
263 cheat-sheet,Documentation browser,.}
265 We'll have a bit more to say about the documentation browser in
266 @altr{doc-browser,a later section,Documentation helpers,.}
268 @cindex jump, at the REPL
269 If that's still not enough, Geiser can jump, via @kbd{M-.}, to the
270 symbol's definition.  A buffer with the corresponding file will pop up,
271 with its point resting upon the identifier's defining form.  When you're
272 done inspecting, @kbd{M-,} will bring you back to where you were.  As we
273 will see, these commands are also available in Scheme buffers.  @kbd{M-.}
274 also works for modules: if your point is on an unambiguous module name,
275 the file where it's defined will be opened for you.
277 @node Customization and tips,  , Autodoc and friends, The REPL
278 @section Customization and tips
280 @cindex REPL customization
281 The looks and ways of the REPL can be fine-tuned via a bunch of
282 customization variables.  You can see and modify them all in the
283 corresponding customization group (by using the menu entry or the good
284 old @kbd{M-x customize-group geiser-repl}), or by setting them in your
285 Emacs initialisation files (as a rule, all knobs in Geiser are tunable
286 this way: you don't need to use customization buffers if you don't like
287 them).
289 I'm documenting below a proper subset of those settings, together with
290 some related tips.
292 @subsubheading Choosing a Scheme implementation
293 @cindex scheme implementation, choosing
294 @anchor{choosing-impl}
295 Instead of using the generic @command{run-geiser} command, you can start
296 directly your Scheme of choice via @command{run-racket} or
297 @command{run-guile}.  @anchor{active-implementations} In addition, the
298 variable @code{geiser-active-implementations} contains a list of those
299 Schemes Geiser should be aware of.  Thus, if you happen to be, say, a
300 racketeer not to be beguiled by other schemes, you can tell Geiser to
301 forget about the richness of the Scheme ecosystem with something like:
303 @example
304 (setq geiser-active-implementations '(racket))
305 @end example
307 @noindent
308 in your initialisation files.
310 @cindex scheme binary
311 @cindex scheme executable path
312 @anchor{impl-binary} When starting a new REPL, Geiser assumes, by
313 default, that the corresponding Scheme binary is in your path.  If that's
314 not the case, the variables to tweak are @code{geiser-guile-binary} and
315 @code{geiser-racket-binary}, which should be set to a string with the
316 full path to the requisite binary.
318 @cindex scheme load path
319 @cindex scheme init file
320 @cindex GUILE_LOAD_PATH
321 @cindex PLTCOLLECTS
322 You can also specify a couple more initialisation parameters.  For Guile,
323 @code{geiser-guile-load-path} is a list of paths to add to its load path
324 when it's started, while @code{geiser-guile-init-file} is the path to an
325 initialisation file to be loaded on start-up.  The equivalent variables
326 for Racket are @code{geiser-racket-collects} and
327 @code{geiser-racket-init-file}.
329 Note, however, that specifying @code{geiser-guile-init-file} is @i{not}
330 equivalent to changing Guile's initialization file (@file{~/.guile}),
331 because the former is loaded using the @code{-l} flag, together with
332 @code{-q} to disable loading the second.  But there are subtle
333 differences in the way Guile loads the initialization file versus how it
334 loads a file specified via the @code{-l} flag.  If what you want is just
335 loading @file{~/.guile}, leave @code{geiser-guile-init-file} alone and
336 set @code{geiser-guile-load-init-file-p} to @code{t} instead.
338 @subsubheading Racket startup time
340 @cindex startup timeout
341 @cindex timeout
342 When starting Racket in little computers, Geiser might have to wait a
343 bit more than it expects (which is ten seconds, or ten thousand
344 milliseconds, by default).  If you find that Geiser is giving up too
345 quickly and complaining that no prompt was found, try to increase the
346 value of @code{geiser-repl-startup-time} to, say, twenty seconds:
348 @example
349 (setq geiser-repl-startup-time 20000)
350 @end example
352 @noindent
353 If you prefer, you can use the customize interface to, well, customise
354 the above variable's value.
356 @subsubheading History
358 By default, Geiser won't record duplicates in your input history.  If you
359 prefer it did, just set @code{geiser-repl-history-no-dups-p} to
360 @code{nil}.  History entries are persistent across REPL sessions:
361 they're saved in implementation-specific files whose location is
362 controlled by the variable @code{geiser-repl-history-filename}.  For
363 example, my Geiser configuration includes the following line:
365 @example
366 (setq geiser-repl-history-filename "~/.emacs.d/geiser-history")
367 @end example
369 @noindent
370 which makes the files @file{geiser-history.guile} and
371 @file{geiser-history.racket} to live inside my home's @file{.emacs.d}
372 directory.
374 @subsubheading Autodoc
376 @cindex autodoc, disabling
377 @cindex peace and quiet
378 If you happen to love peace and quiet and prefer to keep your REPL's
379 echo area free from autodoc's noise, @code{geiser-repl-autodoc-p} is the
380 customization variable for you: set it to @code{nil} and autodoc will be
381 disabled by default in new REPLs.  You can always bring the fairies
382 back, on a per-REPL basis, using @kbd{C-c C-d C-a}.
384 @subsubheading Remote connections
386 @cindex port, default
387 @cindex host, default
388 When using @code{connect-to-guile}, @code{connect-to-racket} or
389 @code{geiser-connect}, you'll be prompted for a host and a port,
390 defaulting to ``localhost'' and 37146.  You can change those defaults
391 customizing @code{geiser-repl-default-host} and
392 @code{geiser-repl-default-port}, respectively.
394 @c Local Variables:
395 @c mode: texinfo
396 @c TeX-master: "geiser"
397 @c End: