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