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