Documentation warning
[geiser.git] / doc / repl.texi
blob282cff0a5aed86317a587583a61b72da1071d95b
1 @node The REPL
2 @chapter The REPL
3 @anchor{quick-start}
4 If you've followed the instructions in @ref{Installation}, 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 * Seeing is believing::
15 * Customization and tips::
16 @end menu
18 @node Starting the REPL, First aids, The REPL, The REPL
19 @section Starting the REPL
21 @cindex REPL
22 To start a Scheme REPL (meaning, a Scheme process offering you a
23 Read-Eval-Print Loop), Geiser provides the generic interactive command
24 @command{run-geiser}. If you invoke it (via, as is customary in Emacs,
25 @kbd{M-x run-geiser}), you'll be saluted by a prompt asking which one of
26 the supported implementations you want to launch---yes, you can stop the
27 asking, see
28 @altr{active-implementations,below,Customization and tips,.}
29 Tabbing for completion will offer you, as of this writing, @code{guile},
30 @code{racket}, @code{chicken}, @code{mit}, @code{chibi} and @code{chez}.
31 Just choose your poison, and a new REPL buffer will pop up (by default,
32 the REPL will appear in a new window: if that annoys you, just set
33 @code{geiser-repl-use-other-window} to @code{nil} and the current window
34 will be used).
36 @imgc{repls}
38 If all went according to plan, you'll be facing an
39 implementation-dependent banner, followed by an interactive prompt.
40 Going according to plan includes having the executable of the Scheme you
41 chose in your path.  If that's not the case, you can tell Emacs where it
42 is, as described in
43 @altr{impl-binary,a moment,Customization and tips,.}
44 Returning to our REPL, the first thing to notice is that the funny
45 prompt is telling you your current module: its name is the part just
46 after the @@ sign (in Guile, that means @code{guile-user}, while
47 Racket's and Chicken's top namespaces don't have a name;
48 cf. discussion in
49 @altr{Switching context,,Switching context,).}
50 Other than that, this is pretty much equivalent to having a
51 command-line interpreter in a terminal, with a bunch of add-ons that
52 we'll be reviewing below.  You can start typing sexps right there:
53 Geiser will only dispatch them for evaluation when they're complete,
54 and will indent new lines properly until then.  It will also keep
55 track of your input, maintaining a history file that will be reloaded
56 whenever you restart the REPL.
58 @cindex REPL, faces
59 @cindex faces, in the REPL
60 If you're not happy with the faces Geiser is using for the REPL's prompt
61 and evaluated input, you can customise
62 @code{geiser-font-lock-repl-prompt} and
63 @code{geiser-font-lock-repl-input} to better-looking faces.
65 @subsubheading Connecting to an external Scheme
66 @cindex remote REPL
67 @cindex connect to server
68 There's an alternative way of starting a Geiser REPL: you can connect to
69 an external Scheme process, provided it's running a REPL server at some
70 known port.  How to make that happen depends on the Scheme implementation.
72 @cindex Guile's REPL server
73 If you use Guile, you just need to start your Guile process (possibly
74 outside Emacs) passing to it the flag @code{--listen}.  This flag accepts
75 an optional port as argument (as in @code{--listen=1969}), if you don't
76 want to use the default.
78 @cindex Racket's REPL server
79 In Racket, you have to use the REPL server that comes with Geiser.  To
80 that end, put Geiser's Racket @file{scheme} directory in Racket's
81 collection search path and invoke @code{start-geiser} (a procedure in
82 the module @code{geiser/server}) somewhere in your program, passing it
83 the desired port and, if desired, network interface name.  This
84 procedure will start the REPL server in a separate thread.  For an
85 example of how to do that, see the script @file{bin/geiser-racket.sh} in
86 the source distribution, or, if you've compiled Geiser,
87 @file{bin/geiser-racket-noinst} in the build directory, or, if you've
88 installed Geiser, @file{geiser-racket} in
89 @file{<installation-prefix>/bin}.  These scripts start a new interactive
90 Racket that is also running a REPL server (they also load the errortrace
91 library to provide better diagnostics, but that's not strictly needed).
93 With your external Scheme process running and serving, come back to
94 Emacs and execute @kbd{M-x geiser-connect}, @kbd{M-x connect-to-guile}
95 or @kbd{M-x connect-to-racket}.  You'll be asked for a host and a port,
96 and, voila, you'll have a Geiser REPL that is served by the remote
97 Scheme process in a dedicated thread, meaning that your external program
98 can go on doing whatever it was doing while you tinker with it from
99 Emacs.  Note, however, that all Scheme threads share the heap, so that
100 you'll be able to interact with those other threads in the running
101 Scheme from Emacs in a variety of ways.  For starters, all your
102 (re)definitions will be visible everywhere.  That's dangerous, but will
103 come in handy when you need to debug your running web server.
105 @cindex remote connections
106 The connection between Emacs and the Scheme process goes over TCP, so it
107 can be as remote as you need, perhaps with the intervention of an SSH
108 tunnel.
110 @node First aids, Switching context, Starting the REPL, The REPL
111 @section First aids
113 @img{repl-menu, right}
114 @cindex REPL commands
115 A quick way of seeing what else Geiser's REPL can do for you, is to
116 display the corresponding entry up there in your menu bar.  No, i don't
117 normally use menus either; but they can come in handy until you've
118 memorized Geiser's commands, as a learning device.  And yes, i usually
119 run Emacs inside a terminal, but one can always use
120 @uref{http://www.emacswiki.org/emacs/LaCarte, La Carte} to access the
121 menus in a convenient enough fashion.
123 Or just press @kbd{C-h m} and be done with that.
125 Among the commands at your disposal, we find the familiar input
126 navigation keys, with a couple twists.  By default, @kbd{M-p} and
127 @kbd{M-n} are bound to @i{matching} items in your input history.  That
128 is, they'll find the previous or next sexp that starts with the current
129 input prefix (defined as the text between the end of the prompt and your
130 current position, a.k.a.  @dfn{point}, in the buffer).  For going up and
131 down the list unconditionally, just use @kbd{C-c M-p} and @kbd{C-c M-n}.
132 In addition, navigation is sexp-based rather than line-based.
134 There are also a few commands to twiddle with the Scheme process.
135 @kbd{C-c C-q} will gently ask it to quit, while @kbd{C-u C-c C-q} will
136 mercilessly kill the process (but not before stowing your history in the
137 file system).  Unless you're using a remote REPL, that is, in which case
138 both commands will just sever the connection and leave the remote
139 process alone.  If worse comes to worst and the process is dead, @kbd{C-c
140 C-z} will restart it.  However, the same shortcut, issued when the REPL is
141 alive, will bring you back to the buffer you came from, as explained
143 @altr{switching-repl-buff,this section,The source and the REPL,.}
145 The remaining commands are meatier, and deserve sections of their own.
147 @node Switching context, Completion and error handling, First aids, The REPL
148 @section Switching context
150 @cindex current module, in REPL
151 @cindex ,enter vs. enter!
152 In tune with Geiser's
153 @ifhtml
154 @ref{current-module,,modus operandi},
155 @end ifhtml
156 @ifnothtml
157 @i{modus operandi},
158 @end ifnothtml
159 evaluations in the REPL take place in the namespace of the current
160 module.  As noted above, the REPL's prompt tells you the name of the
161 current module.  To switch to a different one, you can use the command
162 @command{switch-to-geiser-module}, bound to @kbd{C-c C-m}.  You'll
163 notice that Geiser simply uses a couple of meta-commands provided by
164 the Scheme REPL (the stock @command{,m} in Guile and Chicken and the
165 (geiser-defined) @command{,enter} in Racket), and that it doesn't even
166 try to hide that fact.  That means that you can freely use said native
167 ways directly at the REPL, and Geiser will be happy to oblige.  In
168 Racket, @command{,enter} works like Racket's standard @code{enter!}
169 form, but you can also provide a path string as its argument (e.g.,
170 @command{,enter "/tmp/foo.rkt"} is equivalent to @command{,enter (file
171 "/tmp/foo.rkt")}).  Like @code{enter!}, @command{,enter} accepts also
172 module names (as in, say, @command{,enter geiser/main}).  As
173 mentioned, in Guile and Chicken, @command{,m} is used @i{as is}.
175 @cindex current module, change
176 Once you enter a new module, only those bindings visible in its
177 namespace will be available to your evaluations.  All Schemes supported
178 by Geiser provide a way to import new modules in the current namespace.
179 Again, there's a Geiser command, @command{geiser-repl-import-module}, to
180 invoke such functionality, bound this time to @kbd{C-c C-i}.  And, again,
181 you'll see Geiser just introducing the native incantation for you, and
182 you're free to use such incantations by hand whenever you want.
184 One convenience provided by these two Geiser commands is that completion
185 is available when introducing the new module name, using the
186 @kbd{@key{TAB}} key.  Pressing it at the command's prompt will offer you
187 a prefix-aware list of available module names.
189 @imgc{mod-completion}
191 Which brings me to the next group of REPL commands.
193 @node Completion and error handling, Autodoc and friends, Switching context, The REPL
194 @section Completion and error handling
196 @cindex completion, at the REPL
197 We've already seen Geiser completion of module names in action at the
198 minibuffer.  You won't be surprised to know that it's also available at
199 the REPL buffer itself.  There, you can use either @kbd{C-.} or
200 @kbd{M-`} to complete module names, and @kbd{@key{TAB}} or
201 @kbd{M-@key{TAB}} to complete identifiers.  Geiser will know what
202 identifiers are bound in the current module and show you a list of those
203 starting with the prefix at point.  Needless to say, this is not a static
204 list, and it will grow as you define or import new bindings in the
205 namespace at hand.  If no completion is found, @kbd{@key{TAB}} will try
206 to complete the prefix after point as a module name.
208 REPL buffers use Emacs' compilation mode to highlight errors reported by
209 the Scheme interpreter, and you can use the @command{next-error} command
210 (@kbd{M-g n}) to jump to their location.  By default, every time you
211 enter a new expression for evaluation old error messages are forgotten,
212 so that @kbd{M-g n} will always jump to errors related to the last
213 evaluation request, if any.  If you prefer a not-so-forgetful REPL, set
214 the customization variable @code{geiser-repl-forget-old-errors-p} to
215 @code{nil}.  Note, however, that even when that variable is left as
216 @kbd{t}, you can always jump to an old error by moving to its line at
217 the REPL and pressing @kbd{@key{RET}}.  When your cursor is away from
218 the last prompt, @kbd{@key{TAB}} will move to the next error in the
219 buffer, and you can use @kbd{@key{BACKTAB}} everywhere to go to the
220 previous one.
222 @subheading Caveat about completion & the REPL
223 @anchor{completion-caveat}
225 It is possible for Geiser to hang your Emacs process when trying to
226 complete symbols. This can happen in the REPL itself or even in a
227 Scheme buffer that is attached to the REPL process. If this happens,
228 you've probably entered a module that changes the REPL prompt from
229 what Geiser was expecting to see.
231 Unfortunately, there's no general solution for this issue right now (as
232 it is a daunting task to try to make a regexp that can encompass all
233 possible REPL prompts). The best solution for now is to fix this issue
234 on a case-by-case basis by adjusting your prompt regexp variable so
235 that it matches the default prompt as well as your Scheme module's
236 special prompt.
238 For example, XREPL is a Racket module that implements a better Racket
239 REPL. You might be interested in toying around with some of its
240 functions, but when you try to enter XREPL via, say, @kbd{C-c C-m
241 xrepl}, you'll notice that the REPL prompt has changed to something
242 like this:
244 @example
245 <pkgs>/xrepl-lib/xrepl/main>
246 @end example
248 If you start typing symbols, and then you try to auto-complete those
249 symbols, your Emacs process may hang. This is because Geiser expects
250 the REPL prompt to match this regexp (for Racket):
252 @example
253 "\\(mzscheme\\|racket\\)@@[^ ]*> "
254 @end example
256 Therefore, we can fix this issue by changing our default prompt regexp
257 like so:
259 @example
260 (setq geiser-racket--prompt-regexp "<pkgs>.*> \\|\\(mzscheme\\|racket\\)@@[^ ]*> ")
261 @end example
263 Note that you may have to run @kbd{M-x geiser-reload} after setting
264 this variable so that your changes will take effect.
266 Again, you'll have to change the regexp to fit every prompt that
267 causes this issue, but the only alternative (that we can think of
268 right now) is to create a regexp that will match every possible
269 prompt. Obviously, that is going to be more than a little
270 tricky. However, if you have a better solution than that, please share
271 it with the Geiser developers; we'll be more than happy to hear it.
273 @node Autodoc and friends, Seeing is believing, Completion and error handling, The REPL
274 @section Autodoc and friends
276 Oftentimes, there's more you'll want to know about an identifier
277 besides its name: What module does it belong to?  Is it a procedure and,
278 if so, what arguments does it take?  Geiser tries to help you answering
279 those questions too.
281 @cindex autodoc, in the REPL
282 Actually, if you've been playing with the REPL as you read, you might
283 have notice some frantic activity taking place in the echo area every
284 now and then.  That was Geiser trying to be helpful (while, hopefully,
285 not being clippy), or, more concretely, what i call, for want of a
286 better name, its @dfn{autodoc} mode.  Whenever it's active (did you
287 notice that @i{A} in the mode-line?), Geiser's gerbils will be scanning
288 what you type and showing (unless you silence them with @kbd{C-c C-d C-a})
289 information about the identifier nearest to point.
291 @imgc{repl-autodoc}
293 If that identifier corresponds to a variable visible in the current
294 namespace, you'll see the module it belongs to and its value.  For
295 procedures and macros, autodoc will display, instead of their value, the
296 argument names (or an underscore if Geiser cannot determine the name
297 used in the definition).  Optional arguments are surrounded by
298 parentheses.  When the optional argument has a default value, it's
299 represented by a list made up of its name and that value.  When the
300 argument is a keyword argument, its name has ``#:'' as a prefix.
302 @cindex help on identifier
303 If that's not enough documentation for you, @kbd{C-c C-d d} will open
304 a separate documentation buffer with help on the symbol at point.
305 This buffer will contain implementation-specific information about the
306 identifier (e.g., its docstring for Guile, or its contract, if any,
307 for Racket), and a handy button to open the corresponding manual entry
308 for the symbol, which will open an HTML page (for Racket and Chicken)
309 or the texinfo manual (for Guile). If you'd rather go directly to the
310 manual, try @kbd{C-c C-d i}, which invokes
311 @code{geiser-doc-look-up-manual} as the handy button does.
313 @cindex module exports
314 @anchor{repl-mod} Geiser can also produce for you a list, classified by
315 kind, of the identifiers exported by a given module: all you need to do
316 is press @kbd{C-c C-d m}, and type or complete the desired module's
317 name.
319 @imgc{repl-mod}
321 The list of exported bindings is shown, again, in a buffer belonging to
322 Geiser's documentation browser, where you have at your disposal a bunch
323 of navigation commands listed in
324 @altr{Documentation browser,our cheat-sheet,Documentation browser,.}
326 We'll have a bit more to say about the documentation browser in
327 @altr{doc-browser,a later section,Documentation helpers,.}
329 @cindex jump, at the REPL
330 If that's still not enough, Geiser can jump, via @kbd{M-.}, to the
331 symbol's definition.  A buffer with the corresponding file will pop up,
332 with its point resting upon the identifier's defining form.  When you're
333 done inspecting, @kbd{M-,} will bring you back to where you were.  As we
334 will see, these commands are also available in Scheme buffers.  @kbd{M-.}
335 also works for modules: if your point is on an unambiguous module name,
336 the file where it's defined will be opened for you.
338 @node Seeing is believing, Customization and tips, Autodoc and friends, The REPL
339 @section Seeing is believing
341 @cindex image support
342 In schemes that support images as values (currently, that means
343 Racket), the REPL will display them inline if you're using them in a
344 graphics-aware Emacs.
346 @imgc{repl-images}
348 @cindex external image viewer
349 @cindex image viewer
350 For the terminal, images will appear as buttons: press return on them to
351 invoke an external viewer (configurable via @code{geiser-image-viewer})
352 that will show you the image at hand.  You can also ask for the same
353 behaviour on all emacsen by customising
354 @code{geiser-repl-inline-images-p} to @code{nil}.
356 @cindex image cache
357 Geiser keeps a cache of the last displayed images in the directory
358 @code{geiser-image-cache-dir}, which defaults to the system's temp
359 directory, with up to @code{geiser-image-cache-keep-last} files.  You
360 can invoke the external image viewer on any of them with @command{M-x
361 geiser-view-last-image}, which takes a prefix argument to indicate which
362 image number you want, 0 corresponding to the newest one.
364 @node Customization and tips,  , Seeing is believing, The REPL
365 @section Customization and tips
367 @cindex REPL customization
368 The looks and ways of the REPL can be fine-tuned via a bunch of
369 customization variables.  You can see and modify them all in the
370 corresponding customization group (by using the menu entry or the good
371 old @kbd{M-x customize-group geiser-repl}), or by setting them in your
372 Emacs initialisation files (as a rule, all knobs in Geiser are tunable
373 this way: you don't need to use customization buffers if you don't like
374 them).
376 I'm documenting below a proper subset of those settings, together with
377 some related tips.
379 @subsubheading Choosing a Scheme implementation
380 @cindex scheme implementation, choosing
381 @anchor{choosing-impl}
382 Instead of using the generic @command{run-geiser} command, you can directly
383 start your Scheme of choice using any of the following commands:
384 @itemize @bullet
385 @item @command{run-racket}
386 @item @command{run-guile}
387 @item @command{run-chicken}
388 @item @command{run-mit}
389 @item @command{run-chibi}
390 @item @command{run-chez}
391 @end itemize
392   @anchor{active-implementations} In addition, the
393 variable @code{geiser-active-implementations} contains a list of those
394 Schemes Geiser should be aware of.  Thus, if you happen to be, say, a
395 racketeer not to be beguiled by other schemes, you can tell Geiser to
396 forget about the richness of the Scheme ecosystem with something like:
398 @example
399 (setq geiser-active-implementations '(racket))
400 @end example
402 @noindent
403 in your initialisation files.
405 @cindex scheme binary
406 @cindex scheme executable path
407 @anchor{impl-binary} When starting a new REPL, Geiser assumes, by
408 default, that the corresponding Scheme binary is in your path.  If that's
409 not the case, the variables to tweak are (depending on which Scheme you choose):
410 @itemize @bullet
411 @item @code{geiser-guile-binary}
412 @item @code{geiser-racket-binary}
413 @item @code{geiser-chicken-binary}
414 @item @code{geiser-mit-binary}
415 @item @code{geiser-chibi-binary}
416 @item @code{geiser-chez-binary}
417 @end itemize
418 They should be set to a string with the full path to the requisite binary.
420 @cindex Version checking
421 Before starting the REPL, Geiser will check wether the version of your
422 Scheme interpreter is good enough.  This means that it will spend a
423 couple tenths of a second launching and quickly discarding a Scheme
424 process, but also that the error message you'll get if you're on the
425 wrong Scheme version will be much more informative.  If you one to
426 avoid version checks, just check
427 @code{geiser-repl-skip-version-check-p} to @code{t} in your
428 configuration.
430 @cindex scheme load path
431 @cindex scheme init file
432 @cindex GUILE_LOAD_PATH
433 @cindex GUILE_LOAD_COMPILED_PATH
434 @cindex PLTCOLLECTS
435 You can also specify a couple more initialisation parameters.  For
436 Guile, @code{geiser-guile-load-path} is a list of paths to add to its
437 load path (and its compiled load path) when it's started, while
438 @code{geiser-guile-init-file} is the path to an initialisation file to
439 be loaded on start-up.  The equivalent variables for Racket are
440 @code{geiser-racket-collects} and @code{geiser-racket-init-file}.
442 Note, however, that specifying @code{geiser-guile-init-file} is @i{not}
443 equivalent to changing Guile's initialization file (@file{~/.guile}),
444 because the former is loaded using the @code{-l} flag, together with
445 @code{-q} to disable loading the second.  But there are subtle
446 differences in the way Guile loads the initialization file versus how it
447 loads a file specified via the @code{-l} flag.  If what you want is just
448 loading @file{~/.guile}, leave @code{geiser-guile-init-file} alone and
449 set @code{geiser-guile-load-init-file-p} to @code{t} instead.
451 These variables controlling your scheme's initialisation process are
452 good candidates for an entry in a project's @file{.dir-locals.el} file,
453 so that they are automatically set to a sensible value whenever you
454 start a REPL in the project's directory.
456 @subsubheading Racket startup time
458 @cindex startup timeout
459 @cindex timeout
460 When starting Racket in little computers, Geiser might have to wait a
461 bit more than it expects (which is ten seconds, or ten thousand
462 milliseconds, by default).  If you find that Geiser is giving up too
463 quickly and complaining that no prompt was found, try to increase the
464 value of @code{geiser-repl-startup-time} to, say, twenty seconds:
466 @example
467 (setq geiser-repl-startup-time 20000)
468 @end example
470 @noindent
471 If you prefer, you can use the customize interface to, well, customise
472 the above variable's value.
474 @subsubheading History
476 By default, Geiser won't record duplicates in your input history.  If you
477 prefer it did, just set @code{geiser-repl-history-no-dups-p} to
478 @code{nil}.  History entries are persistent across REPL sessions:
479 they're saved in implementation-specific files whose location is
480 controlled by the variable @code{geiser-repl-history-filename}.  For
481 example, my Geiser configuration includes the following line:
483 @example
484 (setq geiser-repl-history-filename "~/.emacs.d/geiser-history")
485 @end example
487 @noindent
488 which makes the files @file{geiser-history.guile} and
489 @file{geiser-history.racket} to live inside my home's @file{.emacs.d}
490 directory.
492 @subsubheading Autodoc
494 @cindex autodoc, disabling
495 @cindex peace and quiet
496 If you happen to love peace and quiet and prefer to keep your REPL's
497 echo area free from autodoc's noise, @code{geiser-repl-autodoc-p} is the
498 customization variable for you: set it to @code{nil} and autodoc will be
499 disabled by default in new REPLs.  You can always bring the fairies
500 back, on a per-REPL basis, using @kbd{C-c C-d C-a}.
502 @subsubheading Remote connections
504 @cindex port, default
505 @cindex host, default
506 When using any of the connection commands (e.g. @code{geiser-connect},
507 @code{connect-to-guile}, @code{connect-to-racket}, etc.) you'll be
508 prompted for a host and a port, defaulting to ``localhost'' and 37146.
509 You can change those defaults customizing
510 @code{geiser-repl-default-host} and @code{geiser-repl-default-port},
511 respectively.
513 @subsubheading Killing REPLs
515 @cindex ask on kill, don't
516 If you don't want Emacs to ask for confirmation when you're about to
517 kill a live REPL buffer (as will happen, for instance, if you're exiting
518 Emacs before closing all your REPLs), you can set the flag
519 @code{geiser-repl-query-on-kill-p} to @code{nil}.  On a related note,
520 the customizable variable @code{geiser-repl-query-on-exit-p} controls
521 whether Geiser should ask for confirmation when you exit the REPL
522 explicitly (via, say, @kbd{C-c C-q}, as opposed to killing the buffer),
523 and is set to @code{nil} by default.
525 @c Local Variables:
526 @c mode: texinfo
527 @c TeX-master: "geiser"
528 @c End: