Lookup is a name, look up, a verb
[geiser.git] / doc / parens.texi
bloba902b271fc5764bb9cc587f9500d9c91fb23f15b
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 @anchor{doc-browser}Sometimes, autodoc won't provide enough information
282 for you to understand what a function does. In those cases, you can ask
283 Geiser to ask the running Scheme for further information on a given
284 identifier or 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 provide less information, but the documentation browser
305 will display the corresponding contract when it's available.
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}.
311 In both cases, the documentation browser will show a couple of buttons
312 giving you access to further documentation. First, you'll see a button
313 named @i{source}: pressing it you'll jump to the symbol's definition.
314 The second button, dubbed @i{manual}, will open the scheme
315 implementation's manual page for the symbol at hand. For Racket, that
316 will open your web browser displaying the corresponding reference's page
317 (using Emacs' @code{browser-url} command), while in Guile a lookup will
318 be performed in the texinfo manual.
320 @cindex opening manual pages
321 You can also jump directly to the manual page for the symbol at point
322 with the command @code{geiser-doc-look-up-manual}, bound to @kbd{C-c C-d
325 See also our @xref{Documentation browser,,cheat-sheet} for a list of
326 navigation commands available in the documentation browser.
328 @node To eval or not to eval, To err perchance to debug, Documentation helpers, Between the parens
329 @section To eval or not to eval
331 @cindex philosophy
332 @cindex incremental development
333 One of Geiser's main goals is to facilitate incremental development. You
334 might have noticed that i've made a big fuss of Geiser's ability to
335 recognize context, by being aware of the namespace where its operations
336 happen.
338 That awareness is specially important when evaluating code in your
339 scheme buffers, using the commands described below. They allow you to
340 send code to the running Scheme with a granularity ranging from whole
341 files to single s-expressions. That code will be evaluated in the module
342 associated with the file you're editing, allowing you to redefine values
343 and procedures to your heart's (and other modules') content.
345 @cindex incremental development, evil
346 Macros are, of course, another kettle of fish: one needs to re-evaluate
347 uses of a macro after redefining it. That's not a limitation imposed by
348 Geiser, but a consequence of how macros work in Scheme (and other
349 Lisps). There's also the risk that you lose track of what's actually
350 defined and what's not during a given session. But,
351 @uref{http://programming-musings.org/2009/03/29/from-my-cold-prying-hands/,in
352 my opinion}, those are limitations we lispers are aware of, and they
353 don't force us to throw the baby with the bathwater and ditch
354 incremental evaluation. Some people disagree; if you happen to find
355 @uref{http://blog.racket-lang.org/2009/03/drscheme-repl-isnt-lisp.html,
356 their arguments} convincing, you don't have to throw away Geiser
357 together with the baby: @kbd{M-x geiser-restart-repl} will let you
358 restart the REPL as many times as you see fit.
360 @cindex evaluation
361 @cindex incremental development, not evil
362 For all of you bearded old lispers still with me, here are some of the
363 commands performing incremental evaluation in Geiser.
365 @code{geiser-eval-last-sexp}, bound to @kbd{C-x C-e}, will eval the
366 s-expression just before point.
368 @code{geiser-eval-definition}, bound to @kbd{C-M-x}, finds the topmost
369 definition containing point and sends it for evaluation. The variant
370 @code{geiser-eval-definition-and-go} (@kbd{C-c M-e}) works in the same
371 way, but it also teleports you to REPL after the evaluation.
373 @code{geiser-eval-region}, bound to @kbd{C-c C-r}, evals the current
374 region. Again, there's an @i{and go} version available,
375 @code{geiser-eval-region-and-go}, bound to @kbd{C-c M-r}.
377 For all the commands above, the result of the evaluation is displayed in
378 the minibuffer, unless it causes a (scheme-side) error (@pxref{To err
379 perchance to debug}).
381 At the risk of repeating myself, i'll remember you that all these
382 evaluations will take place in the namespace of the module corresponding
383 to the Scheme file from which you're sending your code, which, in
384 general, will be different from the REPL's current module. And, if
385 all goes according to plan, (re)defined variables and procedures should
386 be immediately visible inside and, if exported, outside their module.
388 Besides evaluating expressions, definitions and regions, you can also
389 macro-expand them. The corresponding keybindings start with the prefix
390 @kbd{C-c C-m} and end, respectively, with @kbd{C-e}, @kbd{C-x} and
391 @kbd{C-r}. The result of the macro expansion always appears in a pop up
392 buffer.
394 @node To err perchance to debug, Jumping around, To eval or not to eval, Between the parens
395 @section To err: perchance to debug
397 @cindex to err is schemey
398 @cindex backtraces
399 When an error occurs during evaluation, it will be reported according to
400 the capabilities of the underlying Scheme REPL.
402 @cindex error buffer
403 In Racket, you'll be presented with a backtrace, in a new buffer where
404 file paths locating the origin of the error are clickable (you can
405 navigate them using the @key{TAB} key, and use @key{RET} or the mouse to
406 jump to the offending spot; or invoke Emacs' stock commands
407 @code{next-error} and @code{previous-error}, bound to @kbd{M-g n} and
408 @kbd{M-g p} by default).
410 @imgc{eval-error}
412 The Racket backtrace also highlights the exception type, making it
413 clickable. Following the link will open the documentation corresponding
414 to said exception type. Both the error and exception link faces are
415 customizable (@code{geiser-font-lock-error-link} and
416 @code{geiser-font-lock-doc-link}).
418 On the other hand, Guile's reaction to evaluation errors is different:
419 it enters the debugger in its REPL. Accordingly, the REPL buffer will
420 pop up if your evaluation fails in a Guile file, and the error message
421 and backtrace will be displayed in there, again clickable and all. But
422 there you have the debugger at your disposal, with the REPL's current
423 module set to that of the offender, and a host of special debugging
424 commands that are described in Guile's fine documentation.
426 @imgc{guile-eval-error}
428 In addition, Guile will sometimes report warnings for otherwise
429 successful evaluations. In those cases, it won't enter the debugger, and
430 Geiser will report the warnings in a debug buffer, as it does for
431 Racket. You can control how picky Guile is reporting warnings by
432 customizing the variable @code{geiser-guile-warning-level}, whose
433 detailed docstring (which see, using, e.g. @kbd{C-h v}) allows me to
434 offer no further explanation here. The customization group
435 @i{geiser-guile} is also worth a glance, for a couple of options to fine
436 tune how Geiser interacts with Guile's debugger (and more). Same thing
437 for racketeers and @i{geiser-racket}.
439 @node Jumping around, Geiser writes for you, To err perchance to debug, Between the parens
440 @section Jumping around
442 @cindex jumping in scheme buffers
443 This one feature is as sweet as easy to explain: @kbd{M-.}
444 (@code{geiser-edit-symbol-at-point}) will open the file where the
445 identifier around point is defined and land your point on its
446 definition. To return to where you were, press @kbd{M-,}
447 (@code{geiser-pop-symbol-stack}). This command works also for module
448 names: Geiser first tries to locate a definition for the identifier at
449 point and, if that fails, a module with that name; if the latter
450 succeeds, the file where the module is defined will pop up.
452 Sometimes, the underlying Scheme will tell Geiser only the file where
453 the symbol is defined, but Geiser will use some heuristics (read,
454 regular expressions) to locate the exact line and bring you there. Thus,
455 if you find Geiser systematically missing your definitions, send a
456 message to the mailing list and we'll try to make the algorithm smarter.
458 @cindex jumping customized
459 You can control how the destination buffer pops up by setting
460 @code{geiser-edit-symbol-method} to either @code{nil} (to open the file
461 in the current window), @code{'window} (other window in the same frame)
462 or @code{'frame} (in a new frame).
464 @node Geiser writes for you,  , Jumping around, Between the parens
465 @section Geiser writes for you
467 @cindex completion in scheme buffers
468 No self-respecting programming mode would be complete without
469 completion. In geiser-mode, identifier completion is bound to
470 @kbd{M-@key{TAB}}, and will offer all visible identifiers starting with
471 the prefix before point. Visible here means all symbols imported or
472 defined in the current namespace plus locally bound ones. E.g., if
473 you're at the end of the following partial expression:
475 @example
476 (let ((default 42))
477   (frob def
478 @end example
480 and press @kbd{M-@key{TAB}}, one of the possible completions will be
481 @code{default}.
483 @cindex smart tabs
484 If you find the @kbd{M} modifier annoying, you always have the option to
485 activate @code{geiser-smart-tab-mode}, which will make the @key{TAB} key
486 double duty as the regular Emacs indentation command (when the cursor is
487 not near a symbol) and Geiser's completion function. If you want this
488 smarty pants mode always on in Scheme buffers, customize
489 @code{geiser-mode-smart-tab-p} to @code{t}.
491 @cindex completion for module names
492 Geiser also knows how to complete module names: if no completion for the
493 prefix at point is found among the currently visible bindings, it will
494 try to find a module name that matches it. You can also request
495 explicitly completion only over module names using @kbd{M-`} (that's a
496 backtick).
498 There's also this little command, @code{geiser-squarify}, which will
499 toggle the delimiters of the innermost list around point between round
500 and square brackets. It is bound to @kbd{C-c C-e [}. With a numeric
501 prefix (as in, say, @kbd{M-2 C-c C-e [}), it will perform that many
502 toggles, forward for positive values and backward for negative ones.
504 @c Local Variables:
505 @c mode: texinfo
506 @c TeX-master: "geiser"
507 @c End: