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