Interruptible connection waiting
[geiser.git] / doc / repl.texi
blobe707e618462d30c95d29989f32d833ec4f4f1ad3
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 @cindex remote REPL
49 @cindex connect to server
50 If you use Guile, there's an alternative way of starting a Geiser REPL:
51 you can connect to a remote Guile process, provided the latter is
52 running a REPL server. For that to happen, you just need to start your
53 Guile process (outside Emacs) passing to it the flag @code{--listen}.
54 Then, come back to Emacs and execute @kbd{M-x connect-to-guile}. You'll
55 be asked for a host and a port, with suitable default values (Guile's
56 @code{--listen} flag accepts an optional port as argument (as in
57 @code{--listen=1969}), if you don't want to use the default). And voila,
58 you'll have a Geiser REPL that is served by the remote Guile process in
59 a dedicated thread, meaning that your Guile can go on doing whatever it
60 was doing while you tinker with it from Emacs. Note, however,
61 that all Guile threads share the heap, so that you'll be able to
62 interact with those other threads in the running scheme from Emacs in a
63 variety of ways. For starters, all you (re)defintions will be visible
64 everywhere. That's dangerous, but will come in handy when you need to
65 debug your running webserver.
67 Nothing that fanciful this far, but there's more to Geiser's REPL. On
68 to the next section!
70 @node First aids, Switching context, Starting the REPL, The REPL
71 @section First aids
73 @img{repl-menu, right}
74 @cindex REPL commands
75 A quick way of seeing what else Geiser's REPL can do for you, is to
76 display the corresponding entry up there in your menu bar. No, i don't
77 normally use menus either; but they can come in handy until you've
78 memorized Geiser's commands, as a learning device. And yes, i usually
79 run Emacs inside a terminal, but one can always use
80 @uref{http://www.emacswiki.org/emacs/LaCarte, La Carte} to access the
81 menus in a convenient enough fashion.
83 Or just press @kbd{C-h m} and be done with that.
85 Among the commands at your disposal, we find the familiar input
86 navigation keys, with a couple twists. By default, @kbd{M-p} and
87 @kbd{M-n} are bound to @i{matching} items in your input history. That
88 is, they'll find the previous or next sexp that starts with the current
89 input prefix (defined as the text between the end of the prompt and your
90 current position, a.k.a. @dfn{point}, in the buffer). For going up and
91 down the list unconditionally, just use @kbd{C-c M-p} and @kbd{C-c M-n}.
92 In addition, navigation is sexp- rather than line-based.
94 There are also a few commands to twiddle with the Scheme process.
95 @kbd{C-c C-q} will gently ask it to quit, while @kbd{C-u C-c C-q} will
96 mercilessly kill the process (but not before stowing your history in the
97 file system). Unless you're using a remote REPL, that is, in which case
98 both commands will just sever the connection and leave the remote
99 process alone. A softer nuke is performed by @kbd{C-c C-k}: some (rare,
100 i promise) times, Geiser's REPL can get confused by the input
101 received from then underlying Scheme (specially if you have multiple
102 threads writing to the standard ports), and become irresponsive; you can
103 try this command to try to revive it without killing the process or
104 closing your connection. Finally, if worse comes to worst and the
105 process is dead, @kbd{C-c C-z} will restart it (but the same shortcut,
106 issued when the REPL is alive, will bring you back to the buffer you
107 came from, as explained @ref{switching-repl-buff,,here}).
109 The remaining commands are meatier, and deserve sections of their own.
111 @node Switching context, Completion and error handling, First aids, The REPL
112 @section Switching context
114 @cindex current module, in REPL
115 In tune with Geiser's @ref{current-module,,modus operandi}, evaluations
116 in the REPL take place in the namespace of the current module. As
117 noted above, the REPL's prompt tells you the name of the current
118 module. To switch to a different one, you can use the command
119 @command{switch-to-geiser-module}, bound to @kbd{C-c C-m}. You'll notice
120 that Geiser simply uses a couple of meta-commands provided by the Scheme
121 REPL (the stock @command{,m} in Guile and @command{,enter} in
122 Racket), and that it doesn't even try to hide that fact. That means that
123 you can freely use said native ways directly at the REPL, and Geiser
124 will be happy to oblige.
126 @cindex current module, change
127 Once you enter a new module, only those bindings visible in its
128 namespace will be available to your evaluations. All schemes supported
129 by Geiser provide a way to import new modules in the current namespace.
130 Again, there's a Geiser command, @command{geiser-repl-import-module}, to
131 invoke such functionality, bound this time to @kbd{C-c C-i}. And, again,
132 you'll see Geiser just introducing the native incantation for you, and
133 you're free to use such incantations by hand whenever you want.
135 One convenience provided by these two Geiser commands is that completion
136 is available when introducing the new module name, using the
137 @kbd{@key{TAB}} key. Pressing it at the command's prompt will offer you
138 a prefix-aware list of available module names.
140 @imgc{mod-completion}
142 Which brings me to the next group of REPL commands.
144 @node Completion and error handling, Autodoc and friends, Switching context, The REPL
145 @section Completion and error handling
147 @cindex completion, module name
148 We've already seen Geiser completion of module names in action at the
149 mini-buffer. You won't be surprised to know that it's also available at
150 the REPL buffer itself. There, you can use either @kbd{C-.} or
151 @kbd{M-`} to complete module names, and @kbd{@key{TAB}} or
152 @kbd{M-@key{TAB}} to complete identifiers. Geiser will know what
153 identifiers are bound in the current module and show you a list of those
154 starting with the prefix at point. Needless to say, this is not a static
155 list, and it will grow as you define or import new bindings in the
156 namespace at hand. If no completion is found, @kbd{@key{TAB}} will try
157 to complete the prefix after point as a module name.
159 REPL buffers use Emacs' compilation mode to highlight errors reported by
160 the Scheme interpreter, and you can use the @command{next-error} command
161 (@kbd{M-g n}) to jump to their location. By default, every time you
162 enter a new expression for evaluation old error messages are forgotten,
163 so that @kbd{M-g n} will always jump to errors related to the last
164 evaluation request, if any. If you prefer a not so forgetful REPL, set
165 the customization variable @code{geiser-repl-forget-old-errors-p} to
166 @code{nil}. Note, however, that even when that variable is left as
167 @kbd{t}, you can always jump to an old error by moving to its line at
168 the REPL and pressing @kbd{RET}. When your cursor is away from the last
169 prompt, @kbd{TAB} will move to the next error in the buffer, and you can
170 use @kbd{BACKTAB} everywhere to go to the previous one.
172 @node Autodoc and friends, Customization and tips, Completion and error handling, The REPL
173 @section Autodoc and friends
175 Oftentimes, there's more you'll want to know about an identifier
176 besides its name: what module does it belong to? is it a procedure and,
177 if so, what arguments does it take? Geiser tries to help you answering
178 those questions too.
180 @cindex autodoc, in the REPL
181 Actually, if you've been playing with the REPL as you read, you might
182 have notice some frantic activity taking place in the minibuffer every
183 now and then. That was Geiser trying to be helpful (while, hopefully,
184 not being clippy), or, more concretely, what i call, for want of a
185 better name, its @dfn{autodoc} mode. Whenever it's active (did you
186 notice that @i{A} in the mode-line?), Geiser's gerbils will be scanning
187 what you type and showing (unless you silent them with @kbd{C-c C-a})
188 information about the identifier nearest to point.
190 @imgc{repl-autodoc}
192 If that identifier corresponds to a variable visible in the current
193 namespace, you'll see the module it belongs to and its value. For
194 procedures and macros, autodoc will display, instead of their value, the
195 argument names (or an underscore if Geiser cannot determine the name
196 used in the definition). Optional arguments are surrounded by square
197 brackets, and, when the optional argument has a default value, it's
198 represented by a list made up of its name and that value. When the
199 argument is a keyword argument, its name is preceded by a colon.
201 @cindex help on identifier
202 If that's not enough documentation for you, @kbd{C-c C-d d} will open a
203 separate documentation buffer with help on the symbol at point. For some
204 implementations (e.g. Racket), this separate buffer will actually be a
205 web page displaying the corresponding page in the manual, while for
206 implementations supporting docstrings (e.g. (you guessed it) Guile)
207 it'll be a real Emacs buffer displaying that information.
209 @cindex jump, at the REPL
210 If that's still not enough, Geiser can jump, via @kbd{M-.}, to the
211 symbol's definition. A buffer with the corresponding file will pop up,
212 with its point resting upon the identifier's defining form. When you're
213 done inspecting, @kbd{M-,} will bring you back to where you were. As we
214 will see, these commands are also available in scheme buffers. @kbd{M-.}
215 also works for modules: if your point is on an unambiguous module name,
216 the file where it's defined will be opened for you.
218 @cindex module exports
219 @anchor{repl-mod}
220 Finally, Geiser can produce for you a list, classified by kind, of the
221 identifiers exported by a given module: all you need to do is press
222 @kbd{C-c C-d m}, and type or complete the desired module's name.
224 @imgc{repl-mod}
226 The list of exported bindings is shown in a buffer belonging to Geiser's
227 documentation browser, of which more details are given in forthcoming
228 sections (but just perusing its associated key bindings, by any of the
229 methods we've already mentioned, will give you enough information to use
230 it). Racketeers will be pleased (i hope) to note that contracts are part
231 of the information displayed.
233 @node Customization and tips,  , Autodoc and friends, The REPL
234 @section Customization and tips
236 @cindex REPL customization
237 The looks and ways of the REPL can be fine-tuned via a bunch of
238 customization variables. You can see and modify them all in the
239 corresponding customization group (by using the menu entry or the good
240 old @kbd{M-x customize-group geiser-repl}), or by setting them in your
241 Emacs initialization files (as a rule, all knobs in Geiser are turnable
242 this way: you don't need to use customization buffers if you don't like
243 them).
245 I'm documenting below a proper subset of those settings, together with
246 some related tips.
248 @subsubheading Choosing a Scheme implementation
249 @cindex scheme implementation, choosing
250 @anchor{choosing-impl}
251 Instead of using the generic @command{run-geiser} command, you can start
252 directly your Scheme of choice via @command{run-racket} or
253 @command{run-guile}. @anchor{active-implementations} In addition, the
254 variable @code{geiser-active-implementations} contains a list of those
255 Schemes Geiser should be aware of. Thus, if you happen to be, say, a
256 racketeer not to be beguiled by other schemes, you can tell Geiser to
257 forget about the richness of the Scheme ecosystem with something like
258 @example
259 (setq geiser-active-implementations '(racket))
260 @end example
261 @noindent in your initialisation files.
263 @cindex scheme binary
264 @cindex scheme executable path
265 @anchor{impl-binary} When starting a new REPL, Geiser assumes, by
266 default, that the corresponding Scheme binary is in your path. If that's
267 not the case, the variables to tweak are @code{geiser-guile-binary} and
268 @code{geiser-racket-binary}, which should be set to a string with the
269 full path to the requisite binary.
271 @cindex scheme load path
272 @cindex scheme init file
273 @cindex GUILE_LOAD_PATH
274 @cindex PLTCOLLECTS
275 You can also specify a couple more initialisation parameters. For Guile,
276 @code{geiser-guile-load-path} is a list of paths to add to its load path
277 when it's started, while @code{geiser-guile-init-file} is the path to an
278 initialisation file to be loaded on startup. The equivalent variables
279 for Racket are @code{geiser-racket-collects} and
280 @code{geiser-racket-init-file}.
282 @subsubheading History
284 By default, Geiser won't record duplicates in your input history. If you
285 prefer it did, just set @code{geiser-repl-history-no-dups-p} to
286 @code{nil}. History entries are persistent across REPL sessions:
287 they're saved in implementation-specific files whose location is
288 controlled by the variable @code{geiser-repl-history-filename}. For
289 example, my Geiser configuration includes the following line:
290 @example
291 (setq geiser-repl-history-filename "~/.emacs.d/geiser-history")
292 @end example
293 @noindent which makes the files @file{geiser-history.guile} and
294 @file{geiser-history.racket} to live inside my home's @file{.emacs.d}
295 directory.
297 @subsubheading Autodoc
299 @cindex autodoc, disabling
300 @cindex peace and quiet
301 If you happen to love peace and quiet and prefer to keep your REPL's
302 echo area free from autodoc's noise, @code{geiser-repl-autodoc-p} is the
303 customization variable for you: set it to @code{nil} and autodoc will be
304 disabled by default in new REPLs. You can always bring the fairies
305 back, on a per REPL basis, using @kbd{C-c C-a}.
307 @subsubheading Remote connections
309 @cindex port, default
310 @cindex host, default
311 When using @code{connect-to-guile} or @code{geiser-connect}, you'll be
312 prompted for a host and a port, defaulting to localhost and 37146. You
313 can change those defaults customizing @code{geiser-repl-default-host}
314 and @code{geiser-repl-default-port}, respectfully.
316 @c Local Variables:
317 @c mode: texinfo
318 @c TeX-master: "geiser"
319 @c End: