Documentation updates and nits for Freija
[geiser.git] / doc / repl.texi
blob7fa395bf17485a775d83a60d525ec299679dc81e
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 and @code{racket}.  Just choose your poison, and a new REPL buffer will
31 pop up (by default, the REPL will appear in a new window: if that annoys
32 you, just set @code{geiser-repl-use-other-window} to @code{nil} and the
33 current window 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 @altr{Switching context,,Switching context,).} Other
48 than that, this is pretty much equivalent to having a command-line
49 interpreter in a terminal, with a bunch of add-ons that we'll be
50 reviewing below.  You can start typing sexps right there: Geiser will
51 only dispatch them for evaluation when they're complete, and will
52 indent new lines properly until then.  It will also keep track of your
53 input, maintaining a history file that will be reloaded whenever you
54 restart the REPL.
56 @cindex REPL, faces
57 @cindex faces, in the REPL
58 If you're not happy with the faces Geiser is using for the REPL's prompt
59 and evaluated input, you can customise
60 @code{geiser-font-lock-repl-prompt} and
61 @code{geiser-font-lock-repl-input} to better looking faces.
63 @subsubheading Connecting to an external Scheme
64 @cindex remote REPL
65 @cindex connect to server
66 There's an alternative way of starting a Geiser REPL: you can connect to
67 an external Scheme process, provided it's running a REPL server at some
68 known port.  How to make that happen depends on the Scheme implementation.
70 @cindex Guile's REPL server
71 If you use Guile, you just need to start your Guile process (possibly
72 outside Emacs) passing to it the flag @code{--listen}.  This flag accepts
73 an optional port as argument (as in @code{--listen=1969}), if you don't
74 want to use the default.
76 @cindex Racket's REPL server
77 In Racket, you have to use the REPL server that comes with Geiser.  To
78 that end, put Geiser's Racket @file{scheme} directory in Racket's
79 collection search path and invoke @code{start-geiser} (a procedure in
80 the module @code{geiser/server}) somewhere in your program, passing it
81 the desired port and, if desired, network interface name.  This
82 procedure will start the REPL server in a separate thread.  For an
83 example of how to do that, see the script @file{bin/geiser-racket.sh} in
84 the source distribution, or, if you've compiled Geiser,
85 @file{bin/geiser-racket-noinst} in the build directory, or, if you've
86 installed Geiser, @file{geiser-racket} in
87 @file{<installation-prefix>/bin}.  These scripts start a new interactive
88 Racket that is also running a REPL server (they also load the errortrace
89 library to provide better diagnostics, but that's not strictly needed).
91 With your external Scheme process running and serving, come back to
92 Emacs and execute @kbd{M-x geiser-connect}, @kbd{M-x connect-to-guile}
93 or @kbd{M-x connect-to-racket}.  You'll be asked for a host and a port,
94 and, voila, you'll have a Geiser REPL that is served by the remote
95 Scheme process in a dedicated thread, meaning that your external program
96 can go on doing whatever it was doing while you tinker with it from
97 Emacs.  Note, however, that all Scheme threads share the heap, so that
98 you'll be able to interact with those other threads in the running
99 Scheme from Emacs in a variety of ways.  For starters, all your
100 (re)definitions will be visible everywhere.  That's dangerous, but will
101 come in handy when you need to debug your running web server.
103 @cindex remote connections
104 The connection between Emacs and the Scheme process goes over TCP, so it
105 can be as remote as you need, perhaps with the intervention of an SSH
106 tunnel.
108 @node First aids, Switching context, Starting the REPL, The REPL
109 @section First aids
111 @img{repl-menu, right}
112 @cindex REPL commands
113 A quick way of seeing what else Geiser's REPL can do for you, is to
114 display the corresponding entry up there in your menu bar.  No, i don't
115 normally use menus either; but they can come in handy until you've
116 memorized Geiser's commands, as a learning device.  And yes, i usually
117 run Emacs inside a terminal, but one can always use
118 @uref{http://www.emacswiki.org/emacs/LaCarte, La Carte} to access the
119 menus in a convenient enough fashion.
121 Or just press @kbd{C-h m} and be done with that.
123 Among the commands at your disposal, we find the familiar input
124 navigation keys, with a couple twists.  By default, @kbd{M-p} and
125 @kbd{M-n} are bound to @i{matching} items in your input history.  That
126 is, they'll find the previous or next sexp that starts with the current
127 input prefix (defined as the text between the end of the prompt and your
128 current position, a.k.a.  @dfn{point}, in the buffer).  For going up and
129 down the list unconditionally, just use @kbd{C-c M-p} and @kbd{C-c M-n}.
130 In addition, navigation is sexp-based rather than line-based.
132 There are also a few commands to twiddle with the Scheme process.
133 @kbd{C-c C-q} will gently ask it to quit, while @kbd{C-u C-c C-q} will
134 mercilessly kill the process (but not before stowing your history in the
135 file system).  Unless you're using a remote REPL, that is, in which case
136 both commands will just sever the connection and leave the remote
137 process alone.  If worse comes to worst and the process is dead, @kbd{C-c
138 C-z} will restart it.  However, the same shortcut, issued when the REPL is
139 alive, will bring you back to the buffer you came from, as explained
141 @altr{switching-repl-buff,this section,The source and the REPL,.}
143 The remaining commands are meatier, and deserve sections of their own.
145 @node Switching context, Completion and error handling, First aids, The REPL
146 @section Switching context
148 @cindex current module, in REPL
149 @cindex ,enter vs. enter!
150 In tune with Geiser's
151 @ifhtml
152 @ref{current-module,,modus operandi},
153 @end ifhtml
154 @ifnothtml
155 @i{modus operandi},
156 @end ifnothtml
157 evaluations in the REPL take place in the namespace of the current
158 module.  As noted above, the REPL's prompt tells you the name of the
159 current module.  To switch to a different one, you can use the command
160 @command{switch-to-geiser-module}, bound to @kbd{C-c C-m}.  You'll
161 notice that Geiser simply uses a couple of meta-commands provided by
162 the Scheme REPL (the stock @command{,m} in Guile and Chicken and the
163 (geiser-defined) @command{,enter} in Racket), and that it doesn't even
164 try to hide that fact.  That means that you can freely use said native
165 ways directly at the REPL, and Geiser will be happy to oblige.  In
166 Racket, @command{,enter} works like Racket's standard @code{enter!}
167 form, but you can also provide a path string as its argument (e.g.,
168 @command{,enter "/tmp/foo.rkt"} is equivalent to @command{,enter (file
169 "/tmp/foo.rkt")}).  Like @code{enter!}, @command{,enter} accepts also
170 module names (as in, say, @command{,enter geiser/main}).  As
171 mentioned, in Guile and Chicken, @command{,m} is used @i{as is}.
173 @cindex current module, change
174 Once you enter a new module, only those bindings visible in its
175 namespace will be available to your evaluations.  All Schemes supported
176 by Geiser provide a way to import new modules in the current namespace.
177 Again, there's a Geiser command, @command{geiser-repl-import-module}, to
178 invoke such functionality, bound this time to @kbd{C-c C-i}.  And, again,
179 you'll see Geiser just introducing the native incantation for you, and
180 you're free to use such incantations by hand whenever you want.
182 One convenience provided by these two Geiser commands is that completion
183 is available when introducing the new module name, using the
184 @kbd{@key{TAB}} key.  Pressing it at the command's prompt will offer you
185 a prefix-aware list of available module names.
187 @imgc{mod-completion}
189 Which brings me to the next group of REPL commands.
191 @node Completion and error handling, Autodoc and friends, Switching context, The REPL
192 @section Completion and error handling
194 @cindex completion, at the REPL
195 We've already seen Geiser completion of module names in action at the
196 minibuffer.  You won't be surprised to know that it's also available at
197 the REPL buffer itself.  There, you can use either @kbd{C-.} or
198 @kbd{M-`} to complete module names, and @kbd{@key{TAB}} or
199 @kbd{M-@key{TAB}} to complete identifiers.  Geiser will know what
200 identifiers are bound in the current module and show you a list of those
201 starting with the prefix at point.  Needless to say, this is not a static
202 list, and it will grow as you define or import new bindings in the
203 namespace at hand.  If no completion is found, @kbd{@key{TAB}} will try
204 to complete the prefix after point as a module name.
206 REPL buffers use Emacs' compilation mode to highlight errors reported by
207 the Scheme interpreter, and you can use the @command{next-error} command
208 (@kbd{M-g n}) to jump to their location.  By default, every time you
209 enter a new expression for evaluation old error messages are forgotten,
210 so that @kbd{M-g n} will always jump to errors related to the last
211 evaluation request, if any.  If you prefer a not-so-forgetful REPL, set
212 the customization variable @code{geiser-repl-forget-old-errors-p} to
213 @code{nil}.  Note, however, that even when that variable is left as
214 @kbd{t}, you can always jump to an old error by moving to its line at
215 the REPL and pressing @kbd{@key{RET}}.  When your cursor is away from
216 the last prompt, @kbd{@key{TAB}} will move to the next error in the
217 buffer, and you can use @kbd{@key{BACKTAB}} everywhere to go to the
218 previous one.
220 @node Autodoc and friends, Seeing is believing, Completion and error handling, The REPL
221 @section Autodoc and friends
223 Oftentimes, there's more you'll want to know about an identifier
224 besides its name: What module does it belong to?  Is it a procedure and,
225 if so, what arguments does it take?  Geiser tries to help you answering
226 those questions too.
228 @cindex autodoc, in the REPL
229 Actually, if you've been playing with the REPL as you read, you might
230 have notice some frantic activity taking place in the echo area every
231 now and then.  That was Geiser trying to be helpful (while, hopefully,
232 not being clippy), or, more concretely, what i call, for want of a
233 better name, its @dfn{autodoc} mode.  Whenever it's active (did you
234 notice that @i{A} in the mode-line?), Geiser's gerbils will be scanning
235 what you type and showing (unless you silence them with @kbd{C-c C-d C-a})
236 information about the identifier nearest to point.
238 @imgc{repl-autodoc}
240 If that identifier corresponds to a variable visible in the current
241 namespace, you'll see the module it belongs to and its value.  For
242 procedures and macros, autodoc will display, instead of their value, the
243 argument names (or an underscore if Geiser cannot determine the name
244 used in the definition).  Optional arguments are surrounded by
245 parentheses.  When the optional argument has a default value, it's
246 represented by a list made up of its name and that value.  When the
247 argument is a keyword argument, its name has ``#:'' as a prefix.
249 @cindex help on identifier
250 If that's not enough documentation for you, @kbd{C-c C-d d} will open
251 a separate documentation buffer with help on the symbol at point.
252 This buffer will contain implementation-specific information about the
253 identifier (e.g., its docstring for Guile, or its contract, if any,
254 for Racket), and a handy button to open the corresponding manual entry
255 for the symbol, which will open an HTML page (for Racket and Chicken)
256 or the texinfo manual (for Guile). If you'd rather go directly to the
257 manual, try @kbd{C-c C-d i}, which invokes
258 @code{geiser-doc-look-up-manual} as the handy button does.
260 @cindex module exports
261 @anchor{repl-mod} Geiser can also produce for you a list, classified by
262 kind, of the identifiers exported by a given module: all you need to do
263 is press @kbd{C-c C-d m}, and type or complete the desired module's
264 name.
266 @imgc{repl-mod}
268 The list of exported bindings is shown, again, in a buffer belonging to
269 Geiser's documentation browser, where you have at your disposal a bunch
270 of navigation commands listed in
271 @altr{Documentation browser,our cheat-sheet,Documentation browser,.}
273 We'll have a bit more to say about the documentation browser in
274 @altr{doc-browser,a later section,Documentation helpers,.}
276 @cindex jump, at the REPL
277 If that's still not enough, Geiser can jump, via @kbd{M-.}, to the
278 symbol's definition.  A buffer with the corresponding file will pop up,
279 with its point resting upon the identifier's defining form.  When you're
280 done inspecting, @kbd{M-,} will bring you back to where you were.  As we
281 will see, these commands are also available in Scheme buffers.  @kbd{M-.}
282 also works for modules: if your point is on an unambiguous module name,
283 the file where it's defined will be opened for you.
285 @node Seeing is believing, Customization and tips, Autodoc and friends, The REPL
286 @section Seeing is believing
288 @cindex image support
289 In schemes that support images as values (currently, that means
290 Racket), the REPL will display them inline if you're using them in a
291 graphics-aware Emacs.
293 @imgc{repl-images}
295 @cindex external image viewer
296 @cindex image viewer
297 For the terminal, images will appear as buttons: press return on them to
298 invoke an external viewer (configurable via @code{geiser-image-viewer})
299 that will show you the image at hand.  You can also ask for the same
300 behaviour on all emacsen by customising
301 @code{geiser-repl-inline-images-p} to @code{nil}.
303 @cindex image cache
304 Geiser keeps a cache of the last displayed images in the directory
305 @code{geiser-image-cache-dir}, which defaults to the system's temp
306 directory, with up to @code{geiser-image-cache-keep-last} files.  You
307 can invoke the external image viewer on any of them with @command{M-x
308 geiser-view-last-image}, which takes a prefix argument to indicate which
309 image number you want, 0 corresponding to the newest one.
311 @node Customization and tips,  , Seeing is believing, The REPL
312 @section Customization and tips
314 @cindex REPL customization
315 The looks and ways of the REPL can be fine-tuned via a bunch of
316 customization variables.  You can see and modify them all in the
317 corresponding customization group (by using the menu entry or the good
318 old @kbd{M-x customize-group geiser-repl}), or by setting them in your
319 Emacs initialisation files (as a rule, all knobs in Geiser are tunable
320 this way: you don't need to use customization buffers if you don't like
321 them).
323 I'm documenting below a proper subset of those settings, together with
324 some related tips.
326 @subsubheading Choosing a Scheme implementation
327 @cindex scheme implementation, choosing
328 @anchor{choosing-impl}
329 Instead of using the generic @command{run-geiser} command, you can start
330 directly your Scheme of choice via @command{run-racket} or
331 @command{run-guile}.  @anchor{active-implementations} In addition, the
332 variable @code{geiser-active-implementations} contains a list of those
333 Schemes Geiser should be aware of.  Thus, if you happen to be, say, a
334 racketeer not to be beguiled by other schemes, you can tell Geiser to
335 forget about the richness of the Scheme ecosystem with something like:
337 @example
338 (setq geiser-active-implementations '(racket))
339 @end example
341 @noindent
342 in your initialisation files.
344 @cindex scheme binary
345 @cindex scheme executable path
346 @anchor{impl-binary} When starting a new REPL, Geiser assumes, by
347 default, that the corresponding Scheme binary is in your path.  If that's
348 not the case, the variables to tweak are @code{geiser-guile-binary} and
349 @code{geiser-racket-binary}, which should be set to a string with the
350 full path to the requisite binary.
352 @cindex Version checking
353 Before starting the REPL, Geiser will check wether the version of your
354 Scheme interpreter is good enough.  This means that it will spend a
355 couple tenths of a second launching and quickly discarding a Scheme
356 process, but also that the error message you'll get if you're on the
357 wrong Scheme version will be much more informative.  If you one to
358 avoid version checks, just check
359 @code{geiser-repl-skip-version-check-p} to @code{t} in your
360 configuration.
362 @cindex scheme load path
363 @cindex scheme init file
364 @cindex GUILE_LOAD_PATH
365 @cindex GUILE_LOAD_COMPILED_PATH
366 @cindex PLTCOLLECTS
367 You can also specify a couple more initialisation parameters.  For
368 Guile, @code{geiser-guile-load-path} is a list of paths to add to its
369 load path (and its compiled load path) when it's started, while
370 @code{geiser-guile-init-file} is the path to an initialisation file to
371 be loaded on start-up.  The equivalent variables for Racket are
372 @code{geiser-racket-collects} and @code{geiser-racket-init-file}.
374 Note, however, that specifying @code{geiser-guile-init-file} is @i{not}
375 equivalent to changing Guile's initialization file (@file{~/.guile}),
376 because the former is loaded using the @code{-l} flag, together with
377 @code{-q} to disable loading the second.  But there are subtle
378 differences in the way Guile loads the initialization file versus how it
379 loads a file specified via the @code{-l} flag.  If what you want is just
380 loading @file{~/.guile}, leave @code{geiser-guile-init-file} alone and
381 set @code{geiser-guile-load-init-file-p} to @code{t} instead.
383 @subsubheading Racket startup time
385 @cindex startup timeout
386 @cindex timeout
387 When starting Racket in little computers, Geiser might have to wait a
388 bit more than it expects (which is ten seconds, or ten thousand
389 milliseconds, by default).  If you find that Geiser is giving up too
390 quickly and complaining that no prompt was found, try to increase the
391 value of @code{geiser-repl-startup-time} to, say, twenty seconds:
393 @example
394 (setq geiser-repl-startup-time 20000)
395 @end example
397 @noindent
398 If you prefer, you can use the customize interface to, well, customise
399 the above variable's value.
401 @subsubheading History
403 By default, Geiser won't record duplicates in your input history.  If you
404 prefer it did, just set @code{geiser-repl-history-no-dups-p} to
405 @code{nil}.  History entries are persistent across REPL sessions:
406 they're saved in implementation-specific files whose location is
407 controlled by the variable @code{geiser-repl-history-filename}.  For
408 example, my Geiser configuration includes the following line:
410 @example
411 (setq geiser-repl-history-filename "~/.emacs.d/geiser-history")
412 @end example
414 @noindent
415 which makes the files @file{geiser-history.guile} and
416 @file{geiser-history.racket} to live inside my home's @file{.emacs.d}
417 directory.
419 @subsubheading Autodoc
421 @cindex autodoc, disabling
422 @cindex peace and quiet
423 If you happen to love peace and quiet and prefer to keep your REPL's
424 echo area free from autodoc's noise, @code{geiser-repl-autodoc-p} is the
425 customization variable for you: set it to @code{nil} and autodoc will be
426 disabled by default in new REPLs.  You can always bring the fairies
427 back, on a per-REPL basis, using @kbd{C-c C-d C-a}.
429 @subsubheading Remote connections
431 @cindex port, default
432 @cindex host, default
433 When using @code{connect-to-guile}, @code{connect-to-racket} or
434 @code{geiser-connect}, you'll be prompted for a host and a port,
435 defaulting to ``localhost'' and 37146.  You can change those defaults
436 customizing @code{geiser-repl-default-host} and
437 @code{geiser-repl-default-port}, respectively.
439 @subsubheading Killing REPLs
441 @cindex ask on kill, don't
442 If you don't want Emacs to ask for confirmation when you're about to
443 kill a live REPL buffer (as will happen, for instance, if you're exiting
444 Emacs before closing all your REPLs), you can set the flag
445 @code{geiser-repl-query-on-kill-p} to @code{nil}.  On a related note,
446 the customizable variable @code{geiser-repl-query-on-exit-p} controls
447 whether Geiser should ask for confirmation when you exit the REPL
448 explicitly (via, say, @kbd{C-c C-q}, as opposed to killing the buffer),
449 and is set to @code{nil} by default.
451 @c Local Variables:
452 @c mode: texinfo
453 @c TeX-master: "geiser"
454 @c End: