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