Better geiser-implementation-help (for Geiser hackers)
[geiser.git] / doc / parens.texi
blob8068d5113ee7130936b81dc139f8e57a02791091
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-req, 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 square-bracketed list of optional argument
236 names, if any. When an optional argument has a default value (or a form
237 defining its default value), instead of a plain name, autodoc will
238 display a list with the name followed by its initialisation form. When
239 the optional arguments are keywords, their names are prefixed with a
240 colon. An ellipsis (@dots{}) servers as a marker of an indeterminated
241 number of parameters, as is the case with @i{rest} arguments or when
242 autodoc cannot fathom the exact number of arguments (this is often the
243 case with macros defined using @code{syntax-case}). Another way in which
244 autodoc displays its ignorance is by using and underscore to display
245 parameters 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 (|). As you have already
253 noticed, the whole thing is enclosed in parenthesis. After all, we're
254 talking about Scheme here.
256 @cindex autodoc for variables
257 @img{autodoc-var, right} Finally, life is much easier when your cursor
258 is on a symbol corresponding to a plain variable: you'll see in the echo
259 area its name, preceded by the module where it's defined, and followed
260 by its value, with an intervening arrow for greater effect. This time,
261 there are no enclosing parenthesis (i hope you see the logic in my
262 madness).
264 @cindex autodoc customized
265 You can change the way Geiser displays the module/identifier combo by
266 customizing @code{geiser-autodoc-identifier-format}. For example, if you
267 wanted a tilde surrounded by spaces instead of a colon as a separator,
268 you would write something like
269 @example
270 (setq geiser-autodoc-identifier-format "%s ~ %s")
271 @end example
272 in your Emacs initialization files. There's also a face
273 (@code{geiser-font-lock-autodoc-identifier}) that you can customize (for
274 instance, with @kbd{M-x customize-face}) to change the appearance of the
275 text. And another one (@code{geiser-font-lock-autodoc-current-arg}) that
276 controls how the current argument position is highlighted.
278 @subsubheading Other documentation commands
280 Sometimes, autodoc won't provide enough information for you to
281 understand what a function does. In those cases, you can ask Geiser to
282 ask the running Scheme for further information on a given identifier or
283 module.
285 @cindex documentation for symbol
286 @cindex docstrings, maybe
287 For symbols, the incantation is @kbd{M-x geiser-doc-symbol-at-point}, or
288 @kbd{C-c C-d C-d} for short. If the associated scheme supports
289 docstrings (as, for instance, Guile does), you'll be teleported to a new
290 Emacs buffer displaying Geiser's documentation browser, filled with
291 information about the identifier, including its docstring (if any;
292 unfortunately, that an implementation supports docstrings doesn't mean
293 that they're used everywhere).
295 @imgc{docstring}
297 Pressing @kbd{q} in the documentation buffer will bring you back,
298 enlightened, to where you were. There's also a handful of other
299 navigation commands available in that buffer, which you can discover by
300 means of its menu or via the good old @kbd{C-h m} command.
302 For Racket, which does not support docstrings out of the box, this
303 command will invoke Racket's @code{help} procedure, thereby opening your
304 configured web browser with the corresponding manual page for you to
305 peruse.
307 You can also ask Geiser to display information about a module, in the
308 form of a list of its exported identifiers, using @kbd{C-c C-d C-m},
309 exactly as you would do @ref{repl-mod,,in the REPL}. This commands works
310 with all supported Schemes, no strings attached.
312 @node To eval or not to eval, To err perchance to debug, Documentation helpers, Between the parens
313 @section To eval or not to eval
315 @cindex philosophy
316 @cindex incremental development
317 One of Geiser's main goals is to facilitate incremental development. You
318 might have noticed that i've made a big fuss of Geiser's ability to
319 recognize context, by being aware of the namespace where its operations
320 happen.
322 That awareness is specially important when evaluating code in your
323 scheme buffers, using the commands described below. They allow you to
324 send code to the running Scheme with a granularity ranging from whole
325 files to single s-expressions. That code will be evaluated in the module
326 associated with the file you're editing, allowing you to redefine values
327 and procedures to your heart's (and other modules') content.
329 @cindex incremental development, evil
330 Macros are, of course, another kettle of fish: one needs to re-evaluate
331 uses of a macro after redefining it. That's not a limitation imposed by
332 Geiser, but a consequence of how macros work in Scheme (and other
333 Lisps). There's also the risk that you lose track of what's actually
334 defined and what's not during a given session. But,
335 @uref{http://programming-musings.org/2009/03/29/from-my-cold-prying-hands/,in
336 my opinion}, those are limitations we lispers are aware of, and they
337 don't force us to throw the baby with the bathwater and ditch
338 incremental evaluation. Some people disagree; if you happen to find
339 @uref{http://blog.racket-lang.org/2009/03/drscheme-repl-isnt-lisp.html,
340 their arguments} convincing, you don't have to throw away Geiser
341 together with the baby: @kbd{M-x geiser-restart-repl} will let you
342 restart the REPL as many times as you see fit.
344 @cindex evaluation
345 @cindex incremental development, not evil
346 For all of you bearded old lispers still with me, here are some of the
347 commands performing incremental evaluation in Geiser.
349 @code{geiser-eval-last-sexp}, bound to @kbd{C-x C-e}, will eval the
350 s-expression just before point.
352 @code{geiser-eval-definition}, bound to @kbd{C-M-x}, finds the topmost
353 definition containing point and sends it for evaluation. The variant
354 @code{geiser-eval-definition-and-go} (@kbd{C-c M-e}) works in the same
355 way, but it also teleports you to REPL after the evaluation.
357 @code{geiser-eval-region}, bound to @kbd{C-c C-r}, evals the current
358 region. Again, there's an @i{and go} version available,
359 @code{geiser-eval-region-and-go}, bound to @kbd{C-c M-r}.
361 For all the commands above, the result of the evaluation is displayed in
362 the minibuffer, unless it causes a (scheme-side) error (@pxref{To err
363 perchance to debug}).
365 At the risk of repeating myself, i'll remember you that all these
366 evaluations will take place in the namespace of the module corresponding
367 to the Scheme file from which you're sending your code, which, in
368 general, will be different from the REPL's current module. And, if
369 all goes according to plan, (re)defined variables and procedures should
370 be immediately visible inside and, if exported, outside their module.
372 Besides evaluating expressions, definitions and regions, you can also
373 macro-expand them. The corresponding keybindings start with the prefix
374 @kbd{C-c C-m} and end, respectively, with @kbd{C-e}, @kbd{C-x} and
375 @kbd{C-r}. The result of the macro expansion always appears in a pop up
376 buffer.
378 @node To err perchance to debug, Jumping around, To eval or not to eval, Between the parens
379 @section To err: perchance to debug
381 @cindex to err is schemey
382 @cindex backtraces
383 When an error occurs during evaluation, it will be reported according to
384 the capabilities of the underlying Scheme REPL.
386 @cindex error buffer
387 In Racket, you'll be presented with a backtrace, in a new buffer where
388 file paths locating the origin of the error are clickable (you can
389 navigate them using the @key{TAB} key, and use @key{RET} or the mouse to
390 jump to the offending spot; or invoke Emacs' stock commands
391 @code{next-error} and @code{previous-error}, bound to @kbd{M-g n} and
392 @kbd{M-g p} by default).
394 @imgc{eval-error}
396 The Racket backtrace also highlights the exception type, making it
397 clickable. Following the link will open the documentation corresponding
398 to said exception type. Both the error and exception link faces are
399 customizable (@code{geiser-font-lock-error-link} and
400 @code{geiser-font-lock-doc-link}).
402 On the other hand, Guile's reaction to evaluation errors is different:
403 it enters the debugger in its REPL. Accordingly, the REPL buffer will
404 pop up if your evaluation fails in a Guile file, and the error message
405 and backtrace will be displayed in there, again clickable and all. But
406 there you have the debugger at your disposal, with the REPL's current
407 module set to that of the offender, and a host of special debugging
408 commands that are described in Guile's fine documentation.
410 @imgc{guile-eval-error}
412 In addition, Guile will sometimes report warnings for otherwise
413 successful evaluations. In those cases, it won't enter the debugger, and
414 Geiser will report the warnings in a debug buffer, as it does for
415 Racket. You can control how picky Guile is reporting warnings by
416 customizing the variable @code{geiser-guile-warning-level}, whose
417 detailed docstring (which see, using, e.g. @kbd{C-h v}) allows me to
418 offer no further explanation here. The customization group
419 @i{geiser-guile} is also worth a glance, for a couple of options to fine
420 tune how Geiser interacts with Guile's debugger (and more). Same thing
421 for racketeers and @i{geiser-racket}.
423 @node Jumping around, Geiser writes for you, To err perchance to debug, Between the parens
424 @section Jumping around
426 @cindex jumping in scheme buffers
427 This one feature is as sweet as easy to explain: @kbd{M-.}
428 (@code{geiser-edit-symbol-at-point}) will open the file where the
429 identifier around point is defined and land your point on its
430 definition. To return to where you were, press @kbd{M-,}
431 (@code{geiser-pop-symbol-stack}). This command works also for module
432 names: Geiser first tries to locate a definition for the identifier at
433 point and, if that fails, a module with that name; if the latter
434 succeeds, the file where the module is defined will pop up.
436 Sometimes, the underlying Scheme will tell Geiser only the file where
437 the symbol is defined, but Geiser will use some heuristics (read,
438 regular expressions) to locate the exact line and bring you there. Thus,
439 if you find Geiser systematically missing your definitions, send a
440 message to the mailing list and we'll try to make the algorithm smarter.
442 @cindex jumping customized
443 You can control how the destination buffer pops up by setting
444 @code{geiser-edit-symbol-method} to either @code{nil} (to open the file
445 in the current window), @code{'window} (other window in the same frame)
446 or @code{'frame} (in a new frame).
448 @node Geiser writes for you,  , Jumping around, Between the parens
449 @section Geiser writes for you
451 @cindex completion in scheme buffers
452 No self-respecting programming mode would be complete without
453 completion. In geiser-mode, identifier completion is bound to
454 @kbd{M-@key{TAB}}, and will offer all visible identifiers starting with
455 the prefix before point. Visible here means all symbols imported or
456 defined in the current namespace plus locally bound ones. E.g., if
457 you're at the end of the following partial expression:
459 @example
460 (let ((default 42))
461   (frob def
462 @end example
464 and press @kbd{M-@key{TAB}}, one of the possible completions will be
465 @code{default}.
467 @cindex smart tabs
468 If you find the @kbd{M} modifier annoying, you always have the option to
469 activate @code{geiser-smart-tab-mode}, which will make the @key{TAB} key
470 double duty as the regular Emacs indentation command (when the cursor is
471 not near a symbol) and Geiser's completion function. If you want this
472 smarty pants mode always on in Scheme buffers, customize
473 @code{geiser-mode-smart-tab-p} to @code{t}.
475 @cindex completion for module names
476 Geiser also knows how to complete module names: if no completion for the
477 prefix at point is found among the currently visible bindings, it will
478 try to find a module name that matches it. You can also request
479 explicitly completion only over module names using @kbd{M-`} (that's a
480 backtick).
482 There's also this little command, @code{geiser-squarify}, which will
483 toggle the delimiters of the innermost list around point between round
484 and square brackets. It is bound to @kbd{C-c C-e [}. With a numeric
485 prefix (as in, say, @kbd{M-2 C-c C-e [}), it will perform that many
486 toggles, forward for positive values and backward for negative ones.
488 @c Local Variables:
489 @c mode: texinfo
490 @c TeX-master: "geiser"
491 @c End: