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