More documentation
[geiser.git] / doc / fun.texi
blob0b6e215e7e57795fcb32e175346b872fdb225e5c
1 @node Fun between the parens
2 @chapter Fun between the parens
4 A good @repl{} is a must, but just about half the story of a good Scheme
5 hacking environment. Well, perhaps a bit more than a half; but, at any
6 rate, one surely needs also a pleasant way of editing source code. Don't
7 pay attention to naysayers: Emacs comes with an excellent editor
8 included for about any language on Earth, and just the best one when
9 that language is sexpy (specially if you use @ref{paredit,,Paredit}).
10 Geiser's support for writing Scheme code adds to Emacs'
11 @code{scheme-mode}, rather than supplanting it; and it does so by means
12 of a minor mode (unimaginatively dubbed @code{geiser-mode}) that defines
13 a bunch of new commands to try and, with the help of the same Scheme
14 process giving you the @repl{}, make those Scheme buffers come to life.
16 @menu
17 * Activating Geiser::
18 * The source and the REPL::
19 * Documentation helpers::
20 * To eval or not to eval::
21 * To err perchance to debug::
22 * Jumping around::
23 * Geiser writes for you::
24 @end menu
26 @node Activating Geiser, The source and the REPL, Fun between the parens, Fun between the parens
27 @section Activating Geiser
29 @cindex geiser-mode
30 @img{geiser-mode, right} With Geiser installed following any of the
31 procedures described in @ref{Setting it up}, Emacs will automatically
32 activate @i{geiser-mode} when opening a Scheme buffer. Geiser also
33 instructs Emacs to consider files with the extension @file{rkt} part of
34 the family, so that, in principle, there's nothing you need to do to
35 ensure that Geiser's extensions will be available, out of the box, when
36 you start editing Scheme code.
38 Indications that everything is working according to plan include the
39 'Geiser' minor mode indicator in your mode-line and the appearance of a
40 new entry for Geiser in the menu bar. If, moreover, the mode-line
41 indicator is the name of a Scheme implementation, you're indeed in a
42 perfect world; otherwise, don't despair and keep on reading: i'll tell
43 you how to fix that in a moment.
45 @cindex geiser-mode commands
46 The menu provides a good synopsis of everthing Geiser brings to the
47 party, including those keyboard shortcuts we Emacsers love. If you're
48 seeing the name of your favourite Scheme implementation in the
49 mode-line, have a running @repl{} and are comfortable with Emacs, you
50 can stop reading now and, instead, discover Geiser's joys by yourself.
51 I've tried to make Geiser as self-documenting as any self-respecting
52 Emacs package should be. If you follow this route, make sure to take a
53 look at Geiser's customization buffers (@kbd{M-x customize-group
54 @key{RET} geiser}): there's lot of fine tunning available there.
56 Since @i{geiser-mode} is a minor mode, you can toggle it with
57 @kbd{M-x geiser-mode}, and control its activation in hooks with the
58 functions @code{turn-on-geiser-mode} and @code{turn-off-geiser-mode}.
59 If, for some reason i cannot fathom, you prefer @i{geiser-mode} not
60 to be active by default, the following elisp incantation will do the
61 trick:
62 @example
63 (eval-after-load "geiser-mode"
64   (remove-hook 'scheme-mode-hook 'turn-on-geiser-mode))
65 @end example
66 @cindex scheme file extensions
67 And if you happen to use a funky extension for your Scheme files that is
68 not recognised as such by Emacs, just tell her about it with:
69 @example
70 (add-to-list 'auto-mode-alist '("\\.funky-extension\\'" . scheme-mode))
71 @end example
73 @cindex useless wretch
74 Now, @i{geiser-mode} is just a useless wretch unless there's a running
75 Scheme process backing it up. Meaning that virtually all the commands it
76 provides require a @repl{} up and running, preferably corresponding to
77 the correct Scheme implementation. In the following section, we'll see
78 how to make sure that that's actually the case.
80 @node The source and the REPL, Documentation helpers, Activating Geiser, Fun between the parens
81 @section The source and the REPL
83 As i've already mentioned a couple of times, @i{geiser-mode} needs a
84 running @repl{} to be operative. Thus, a common usage pattern will be
85 for you to first call @code{run-geiser} (or one of its variants, see
86 them described @ref{choosing-impl,,here}), and then open Scheme files;
87 but there's nothing wrong in first opening a couple Scheme buffers and
88 then starting the @repl{} (you can even find it more convenient, since
89 pressing @kbd{C-c C-z} in a Scheme buffer will start the @repl{} for
90 you). Since Geiser supports more than one Scheme implementation, though,
91 there's the problem of knowing which of them is to be associated with
92 each Scheme source file. Serviceable as it is, @i{geiser-mode} will try
93 to guess the correct implementation for you, according to the algorithm
94 described below. If you find that Geiser is already guessing right the
95 Scheme implementation, feel free to skip to the
96 @ref{switching-repl-buff,,next subsection}.
98 @subsubheading How Geiser associates a REPL to your Scheme buffer
99 @cindex scheme implementation, choosing
100 To determine what Scheme implementation corresponds to a given source
101 file, Geiser uses the following algorithm:
102 @enumerate
103 @item
104 If the file-local variable @code{geiser-scheme-implementation} is
105 defined, its value is used. A common way of setting buffer-local
106 variables is to put them in a comment near the beginning of the file,
107 surrounded by @code{-*-} marks, as in:
108 @example
109 ;; -*- geiser-scheme-implementation: guile -*-
110 @end example
111 @item
112 If you've customized @code{geiser-active-implementations} so that it's a
113 single-element list (as explained @ref{choosing-impl,,here}), that
114 element is used as the chosen implementation.
115 @item
116 The contents of the file is scanned for hints on its associated
117 implementation. For instance, files that contain a @code{#lang}
118 directive will be considered Racket source code, while those with a
119 @code{define-module} form in them will be assigned to a Guile @repl{}.
120 @item
121 The current buffer's file name is checked against the rules given in
122 @code{geiser-implementations-alist}, and the first match is applied. You
123 can provide your own rules by customizing this variable, as explained
124 below.
125 @item
126 If we haven't been lucky this far and you have customized
127 @code{geiser-default-implementation} to the name of a supported
128 implementation, we'll follow your lead.
129 @item
130 See? That's the problem of being a smart alec: one's always outsmarted
131 by people around. At this point, @i{geiser-mode} will humbly give up and
132 ask you to explicitly choose the Scheme implementation.
133 @end enumerate
134 As you can see in the list above, there are several ways to influence
135 Geiser's guessing by mean customizable variables. The most direct (and
136 most impoverishing) is probably limiting the active implementations to a
137 single one, while customizing @code{geiser-implementations-alist} is the
138 most flexible (and, unsurprisingly, also the most complex). Here's the
139 default value for the latter variable:
140 @example
141 (((regexp "\\.scm$") guile)
142  ((regexp "\\.ss$") racket)
143  ((regexp "\\.rkt$") racket))
144 @end example
145 which describes the simple heuristic that files with @file{.scm} as
146 extension are by default associated to a Guile @repl{} while those
147 ending in @file{.ss} or @file{.rkt} correspond to Racket's
148 implementation (with the caveat that these rules are applied only if the
149 previous heuristics have failed to detect the correct implementation,
150 and that they'll match only if the corresponding implementation is
151 active). You can add rules to @code{geiser-implementations-alist} (or
152 replace all of them) by customizing it. Besides regular expressions, you
153 can also use a directory name; for instance, the following snippet:
154 @example
155 (eval-after-load "geiser-impl"
156   '(add-to-list 'geiser-implementations-alist
157                 '((dir "/home/jao/prj/frob") guile)))
158 @end example
159 will add a new rule that says that any file inside my
160 @file{/home/jao/prj/frob} directory (or, recursively, any of its
161 children) is to be assigned to Guile. Since rules are first matched,
162 first served, this new rule will take precedence over the default ones.
164 @subsubheading Switching between source files and the REPL
165 @cindex switching to REPL
166 @cindex switching to source
167 @anchor{switching-repl-buff} Once you have a working @i{geiser-mode},
168 you can switch from Scheme source buffers to the @repl{} or @kbd{C-c
169 C-z}. Those shortcuts map to the interactive command
170 @code{switch-to-geiser}.
172 @cindex switching to module
173 If you use a numeric prefix, as in @kbd{C-u C-c C-z}, besides being
174 teleported to the @repl{}, the latter will switch to the namespace of
175 the Scheme source file (as if you had used @kbd{C-c C-m} in the @repl{},
176 with the source file's module as argument; cf. @ref{Switching context}).
177 This command is also bound to @kbd{C-c C-Z}, with a capital zed.
179 Once you're in the @repl{}, the same @kbd{C-c C-z} shortcut will bring
180 you back to the buffer you jumped from, provided you don't kill the
181 Scheme process in between. This is why the command is called
182 @i{switch-to-geiser} instead of @i{switch-to-repl}, and what makes it
183 really handy, if you ask me.
185 @cindex switching schemes
186 If for some reason you're not happy with the Scheme implementation that
187 Geiser has assigned to your file, you can change it with @kbd{C-c C-s},
188 and probably take a look at @ref{switching-repl-buff,,the previous
189 subsection} to make sure that Geiser doesn't get confused again.
191 @subsubheading A note about context
192 As explained before (@pxref{Modus operandi}), all Geiser activities take
193 place in the context of the @i{current namespace}, which, for Scheme
194 buffers, corresponds to the module that the Scheme implementation
195 associates to the source file at hand (for instance, in Racket, there's
196 a one to one correspondence between paths and modules, while Guile
197 relies on explicit @code{define-module} forms in the source file).
199 Now that we have @code{geiser-mode} happily alive in our Scheme buffers
200 and communicating with the right @repl{} instance, let us see what it
201 can do for us, besides jumping to and fro.
203 @node Documentation helpers, To eval or not to eval, The source and the REPL, Fun between the parens
204 @section Documentation helpers
206 @subsubheading Autodoc redux
208 @cindex autodoc, in scheme buffers
209 The first thing you will notice by moving around Scheme source is that,
210 every now and then, the echo area lightens up with the same autodoc
211 messages we know and love from our @repl{} forays. This happens every
212 time the Scheme process is able to recognise an identifier in the
213 buffer, and provide information either on its value (for variables) or
214 on its arity and the name of its formal arguments (for procedures and
215 macros). That information will only be available if the module the
216 identifier belongs to has been loaded in the running Scheme image. So it
217 can be the case that, at first, no autodoc is shown for identifiers
218 defined in the file you're editing. But as soon as you evaluate them
219 (either individually or collectively using any of the devices described
220 in @ref{To eval or not to eval}) their signatures will start appearing
221 in the echo area.
223 @cindex disabling autodoc
224 Autodoc activation is controlled by a minor mode, @code{geiser-autodoc},
225 which you can toggle with @kbd{M-x geiser-autodoc}, or its associated
226 keyboard shortcut, @kbd{C-c C-d a}. That @t{/A} indicator in the
227 mode-line is telling you that autodoc is active. If you prefer, for some
228 obscure reason, that it be inactive by default, just set
229 @code{geiser-mode-autodoc-p} to @code{nil} in your customization files.
231 @cindex autodoc explained
232 @img{autodoc-req, right} The way autodoc displays information deserves
233 some explanation. It will first show the name of the module where the
234 identifier at hand is defined, followed by a colon and the identifier
235 itself. If the latter corresponds to a procedure or macro, it will be
236 followed by a list of argument names, starting with the ones that are
237 required. Then there comes a square-bracketed list of optional argument
238 names, if any. When an optional argument has a default value (or a form
239 defining its default value), instead of a plain name, autodoc will
240 display a list with the name followed by its initialisation form. When
241 the optional arguments are keywords, their names are prefixed with a
242 colon. An ellipsis (@dots{}) servers as a marker of an indeterminated
243 number of parameters, as is the case with @i{rest} arguments or when
244 autodoc cannot fathom the exact number of arguments (this is often the
245 case with macros defined using @code{syntax-case}). Another way in which
246 autodoc displays its ignorance is by using and underscore to display
247 parameters whose name is beyond its powers.
249 @img{autodoc-multi, right} It can also be the case that a function or
250 macro has more than one signature (e.g., functions defined using
251 @code{case-lambda}, or some @code{syntax-rules} macros, for which Geiser
252 has often the black magic necessary to retrieve their actual arities).
253 In those cases, autodoc shows all known signatures (using the above
254 rules for each one) separated by a vertical bar (|). As you have already
255 noticed, the whole thing is enclosed in parenthesis. After all, we're
256 talking about Scheme here.
258 @cindex autodoc for variables
259 @img{autodoc-var, right} Finally, life is much easier when your cursor
260 is on a symbol corresponding to a plain variable: you'll see in the echo
261 area its name, preceded by the module where it's defined, and followed
262 by its value, with an intervening arrow for greater effect. This time,
263 there are no enclosing parenthesis (i hope you see the logic in my
264 madness).
266 @cindex autodoc customized
267 You can change the way Geiser displays the module/identifier combo by
268 customizing @code{geiser-autodoc-identifier-format}. For example, if you
269 wanted a tilde surrounded by spaces instead of a colon as a separator,
270 you would write something like
271 @example
272 (setq geiser-autodoc-identifier-format "%s ~ %s")
273 @end example
274 in your Emacs initialization files. There's also a face
275 (@code{geiser-font-lock-autodoc-identifier}) that you can customize (for
276 instance, with @kbd{M-x customize-face}) to change the appearance of the
277 text. And another one (@code{geiser-font-lock-autodoc-current-arg}) that
278 controls how the current argument position is highlighted.
280 @subsubheading Other documentation commands
282 Sometimes, autodoc won't provide enough information for you to
283 understand what a function does. In those cases, you can ask Geiser to
284 ask the running Scheme for further information on a given identifier or
285 module.
287 @cindex documentation for symbol
288 @cindex docstrings, maybe
289 For symbols, the incantation is @kbd{M-x geiser-doc-symbol-at-point}, or
290 @kbd{C-c C-d C-d} for short. If the associated scheme supports
291 docstrings (as, for instance, Guile does), you'll be teleported to a new
292 Emacs buffer displaying Geiser's documentation browser, filled with
293 information about the identifier, including its docstring (if any;
294 unfortunately, that an implementation supports docstrings doesn't mean
295 that they're used everywhere).
297 @image{img/docstring}
299 Pressing @kbd{q} in the documentation buffer will bring you back,
300 enlightened, to where you were. There's also a handful of other
301 navigation commands available in that buffer, which you can discover by
302 means of its menu or via the good old @kbd{C-h m} command.
304 For Racket, which does not support docstrings out of the box, this
305 command will invoke Racket's @code{help} procedure, thereby opening your
306 configured web browser with the corresponding manual page for you to
307 peruse.
309 You can also ask Geiser to display information about a module, in the
310 form of a list of its exported identifiers, using @kbd{C-c C-d C-m},
311 exactly as you would do @ref{repl-mod,,in the REPL}. This commands works
312 with all supported Schemes, no strings attached.
314 @node To eval or not to eval, To err perchance to debug, Documentation helpers, Fun between the parens
315 @section To eval or not to eval
317 @cindex philosophy
318 @cindex incremental development
319 One of Geiser's main goals is to facilitate incremental development. You
320 might have noticed that i've made a big fuss of Geiser's ability to
321 recognize context, by being aware of the namespace where its operations
322 happen.
324 That awareness is specially important when evaluating code in your
325 scheme buffers, using the commands described below. They allow you to
326 send code to the running Scheme with a granularity ranging from whole
327 files to single s-expressions. That code will be evaluated in the module
328 associated with the file you're editing, allowing you to redefine values
329 and procedures to your heart's (and other modules') content.
331 @cindex incremental development, evil
332 Macros are, of course, another kettle of fish: one needs to re-evaluate
333 uses of a macro after redefining it. That's not a limitation imposed by
334 Geiser, but a consequence of how macros work in Scheme (and other
335 Lisps). There's also the risk that you lose track of what's actually
336 defined and what's not during a given session. But,
337 @uref{http://programming-musings.org/2009/03/29/from-my-cold-prying-hands/,in
338 my opinion}, those are limitations we lispers are aware of, and they
339 don't force us to throw the baby with the bathwater and ditch
340 incremental evaluation. Some people disagree; if you happen to find
341 @uref{http://blog.racket-lang.org/2009/03/drscheme-repl-isnt-lisp.html,
342 their arguments} convincing, you don't have to throw away Geiser
343 together with the baby: @kbd{M-x geiser-restart-repl} will let you
344 restart the @repl{} as many times as you see fit.
346 @cindex evaluation
347 @cindex incremental development, not evil
348 For all of you bearded old lispers still with me, here are some of the
349 commands performing incremental evaluation in Geiser.
351 @code{geiser-eval-last-sexp}, bound to @kbd{C-x C-e}, will eval the
352 s-expression just before point.
354 @code{geiser-eval-definition}, bound to @kbd{C-M-x}, finds the topmost
355 definition containing point and sends it for evaluation. The variant
356 @code{geiser-eval-definition-and-go} (@kbd{C-c M-e}) works in the same
357 way, but it also teleports you to @repl{} after the evaluation.
359 @code{geiser-eval-region}, bound to @kbd{C-c C-r}, evals the current
360 region. Again, there's an @i{and go} version available,
361 @code{geiser-eval-region-and-go}, bound to @kbd{C-c M-r}.
363 For all the commands above, the result of the evaluation is displayed in
364 the minibuffer, unless it causes a (scheme-side) error (@pxref{To err
365 perchance to debug}).
367 At the risk of repeating myself, i'll remember you that all these
368 evaluations will take place in the namespace of the module corresponding
369 to the Scheme file from which you're sending your code, which, in
370 general, will be different from the @repl{}'s current module. And, if
371 all goes according to plan, (re)defined variables and procedures should
372 be immediately visible inside and, if exported, outside their module.
374 Besides evaluating expressions, definitions and regions, you can also
375 macro-expand them. The corresponding keybindings start with the prefix
376 @kbd{C-c C-m} and end, respectively, with @kbd{C-e}, @kbd{C-x} and
377 @kbd{C-r}. The result of the macro expansion always appears in a pop up
378 buffer.
380 @node To err perchance to debug, Jumping around, To eval or not to eval, Fun between the parens
381 @section To err: perchance to debug
383 @cindex to err is schemey
384 @cindex backtraces
385 When an error occurs during evaluation, it will be reported according to
386 the capabilities of the underlying Scheme REPL.
388 @cindex error buffer
389 In Racket, you'll be presented with a backtrace, in a new buffer where
390 file paths locating the origin of the error are clickable (you can
391 navigate them using the @key{TAB} key, and use @key{RET} or the mouse to
392 jump to the offending spot; or invoke Emacs' stock commands
393 @code{next-error} and @code{previous-error}, bound to @kbd{M-g n} and
394 @kbd{M-g p} by default).
396 @image{img/eval-error}
398 The Racket backtrace also highlights the exception type, making it
399 clickable. Following the link will open the documentation corresponding
400 to said exception type. Both the error and exception link faces are
401 customizable (@code{geiser-font-lock-error-link} and
402 @code{geiser-font-lock-doc-link}).
404 On the other hand, Guile's reaction to evaluation errors is different:
405 it enters the debugger in its REPL. Accordingly, the REPL buffer will
406 pop up if your evaluation fails in a Guile file, and the error message
407 and backtrace will be displayed in there, again clickable and all. But
408 there you have the debugger at your disposal, with the REPL's current
409 module set to that of the offender, and a host of special debugging
410 commands that are described in Guile's fine documentation.
412 @image{img/guile-eval-error}
414 In addition, Guile will sometimes report warnings for otherwise
415 successful evaluations. In those cases, it won't enter the debugger, and
416 Geiser will report the warnings in a debug buffer, as it does for
417 Racket. You can control how picky Guile is reporting warnings by
418 customizing the variable @code{geiser-guile-warning-level}, whose
419 detailed docstring (which see, using, e.g. @kbd{C-h v}) allows me to
420 offer no further explanation here. The customization group
421 @i{geiser-guile} is also worth a glance, for a couple of options to fine
422 tune how Geiser interacts with Guile's debugger (and more). Same thing
423 for racketeers and @i{geiser-racket}.
425 @node Jumping around, Geiser writes for you, To err perchance to debug, Fun between the parens
426 @section Jumping around
428 @cindex jumping in scheme buffers
429 This one feature is as sweet as easy to explain: @kbd{M-.}
430 (@code{geiser-edit-symbol-at-point}) will open the file where the
431 identifier around point is defined and land your point on its
432 definition. To return to where you were, press @kbd{M-,}
433 (@code{geiser-pop-symbol-stack}).
435 Sometimes, the underlying Scheme will tell Geiser only the file where
436 the symbol is defined, but Geiser will use some heuristics (read,
437 regular expressions) to locate the exact line and bring you there. Thus,
438 if you find Geiser systematically missing your definitions, send a
439 message to the mailing list and we'll try to make the algorithm smarter.
441 @cindex jumping customized
442 You can control how the destination buffer pops up by setting
443 @code{geiser-edit-symbol-method} to either @code{nil} (to open the file
444 in the current window), @code{'window} (other window in the same frame)
445 or @code{'frame} (in a new frame).
447 @node Geiser writes for you,  , Jumping around, Fun between the parens
448 @section Geiser writes for you
450 @cindex completion in scheme buffers
451 No self-respecting programming mode would be complete without
452 completion. In geiser-mode, identifier completion is bound to
453 @kbd{M-@key{TAB}}, and will offer all visible identifiers starting with
454 the prefix before point. Visible here means all symbols imported or
455 defined in the current namespace plus locally bound ones. E.g., if
456 you're at the end of the following partial expression:
458 @example
459 (let ((default 42))
460   (frob def
461 @end example
463 and press @kbd{M-@key{TAB}}, one of the possible completions will be
464 @code{definite}.
466 @cindex completion for module names
467 Geiser also knows how to complete module names, but it's not yet smart
468 enough to infer by context that that's what you want. Instead, you must
469 use a separate command, bound to @kbd{M-`} (that's a backtick).
471 @c Local Variables:
472 @c mode: texinfo
473 @c TeX-master: "geiser"
474 @c End: