Remove unused symbols.
[bpt/guile.git] / doc / ref / scheme-using.texi
CommitLineData
46f7666d
NJ
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
3@c Copyright (C) 2006
4@c Free Software Foundation, Inc.
5@c See the file guile.texi for copying conditions.
6
7@node Using Guile Interactively
8@section Using Guile Interactively
9
10When you start up Guile by typing just @code{guile}, without a
11@code{-c} argument or the name of a script to execute, you get an
12interactive interpreter where you can enter Scheme expressions, and
13Guile will evaluate them and print the results for you. Here are some
14simple examples.
15
16@lisp
17guile> (+ 3 4 5)
1812
19guile> (display "Hello world!\n")
20Hello world!
21guile> (values 'a 'b)
22a
23b
24@end lisp
25
26@noindent
27This mode of use is called a @dfn{REPL}, which is short for
28``Read-Eval-Print Loop'', because the Guile interpreter first reads the
29expression that you have typed, then evaluates it, and then prints the
30result.
31
32@menu
33* Readline::
34* Value Historyx::
35* Error Handling::
36* Interactive Debugger:: Using the interactive debugger.
37@end menu
38
39
40@node Readline
41@subsection Readline
42
43To make it easier for you to repeat and vary previously entered
44expressions, or to edit the expression that you're typing in, Guile
45can use the GNU Readline library. This is not enabled by default
46because of licensing reasons, but all you need to activate Readline is
47the following pair of lines.
48
49@lisp
50guile> (use-modules (ice-9 readline))
51guile> (activate-readline)
52@end lisp
53
54It's a good idea to put these two lines (without the ``guile>''
55prompts) in your @file{.guile} file. Guile reads this file when it
56starts up interactively, so anything in this file has the same effect
57as if you type it in by hand at the ``guile>'' prompt.
58
59
60@node Value Historyx
61@subsection Value History
62
63Just as Readline helps you to reuse a previous input line, @dfn{value
64history} allows you to use the @emph{result} of a previous evaluation
65in a new expression. When value history is enabled, each evaluation
66result is automatically assigned to the next in the sequence of
67variables @code{$1}, @code{$2}, @dots{}, and you can then use these
68variables in subsequent expressions.
69
70@lisp
71guile> (iota 10)
72$1 = (0 1 2 3 4 5 6 7 8 9)
73guile> (apply * (cdr $1))
74$2 = 362880
75guile> (sqrt $2)
76$3 = 602.3952191045344
77guile> (cons $2 $1)
78$4 = (362880 0 1 2 3 4 5 6 7 8 9)
79@end lisp
80
81To enable value history, type @code{(use-modules (ice-9 history))} at
82the Guile prompt, or add this to your @file{.guile} file. (It is not
83enabled by default, to avoid the possibility of conflicting with some
84other use you may have for the variables @code{$1}, @code{$2},
85@dots{}, and also because it prevents the stored evaluation results
86from being garbage collected, which some people may not want.)
87
88
89@node Error Handling
90@subsection Error Handling
91
92When code being evaluated from the REPL hits an error, Guile remembers
93the execution context where the error occurred and can give you three
94levels of information about what the error was and exactly where it
95occurred.
96
ee6be719 97By default, Guile displays only the first level, which is the most
46f7666d
NJ
98immediate information about where and why the error occurred, for
99example:
100
101@lisp
102(make-string (* 4 (+ 3 #\s)) #\space)
103@print{}
104standard input:2:19: In procedure + in expression (+ 3 #\s):
105standard input:2:19: Wrong type argument: #\s
106ABORT: (wrong-type-arg)
107
ee6be719
NJ
108Type "(backtrace)" to get more information
109or "(debug)" to enter the debugger.
46f7666d
NJ
110@end lisp
111
112@noindent
113However, as the message above says, you can obtain more information
114about the context of the error by typing @code{(backtrace)} or
115@code{(debug)}.
116
117@code{(backtrace)} displays the Scheme call stack at the point where the
118error occurred:
119
120@lisp
121(backtrace)
122@print{}
123Backtrace:
124In standard input:
125 2: 0* [make-string ...
126 2: 1* [* 4 ...
127 2: 2* [+ 3 #\s]
128
129Type "(debug-enable 'backtrace)" if you would like a backtrace
130automatically if an error occurs in the future.
131@end lisp
132
133@noindent
134In a more complex scenario than this one, this can be extremely useful
135for understanding where and why the error occurred. You can make Guile
136show the backtrace automatically by adding @code{(debug-enable
137'backtrace)} to your @file{.guile}.
138
139@code{(debug)} takes you into Guile's interactive debugger, which
140provides commands that allow you to
141
142@itemize @bullet
143@item
144display the Scheme call stack at the point where the error occurred
145(the @code{backtrace} command --- see @ref{Display Backtrace})
146
147@item
148move up and down the call stack, to see in detail the expression being
149evaluated, or the procedure being applied, in each @dfn{frame} (the
150@code{up}, @code{down}, @code{frame}, @code{position}, @code{info args}
151and @code{info frame} commands --- see @ref{Frame Selection} and
152@ref{Frame Information})
153
154@item
155examine the values of variables and expressions in the context of each
156frame (the @code{evaluate} command --- see @ref{Frame Evaluation}).
157@end itemize
158
159@noindent
ee6be719 160The interactive debugger is documented further in the following section.
46f7666d
NJ
161
162
163@node Interactive Debugger
164@subsection Using the Interactive Debugger
165
24dbb5ed
NJ
166Guile's interactive debugger is a command line application that
167accepts commands from you for examining the stack and, if stopped at a
168trap, for continuing program execution in various ways. Unlike in the
169normal Guile REPL, commands are typed mostly without parentheses.
46f7666d
NJ
170
171When you first enter the debugger, it introduces itself with a message
172like this:
173
174@lisp
175This is the Guile debugger -- for help, type `help'.
176There are 3 frames on the stack.
177
178Frame 2 at standard input:36:19
179 [+ 3 #\s]
180debug>
181@end lisp
182
183@noindent
184``debug>'' is the debugger's prompt, and a reminder that you are not in
ee6be719
NJ
185the normal Guile REPL. In case you find yourself in the debugger by
186mistake, the @code{quit} command will return you to the REPL.
187
188@deffn {Debugger Command} quit
189Exit the debugger.
190@end deffn
191
192The other available commands are described in the following subsections.
46f7666d
NJ
193
194@menu
195* Display Backtrace:: backtrace.
196* Frame Selection:: up, down, frame.
197* Frame Information:: info args, info frame, position.
198* Frame Evaluation:: evaluate.
ee6be719 199* Stepping and Continuing:: step, next, (trace-)finish, continue.
46f7666d
NJ
200@end menu
201
202
203@node Display Backtrace
204@subsubsection Display Backtrace
205
206The @code{backtrace} command, which can also be invoked as @code{bt} or
207@code{where}, displays the call stack (aka backtrace) at the point where
208the debugger was entered:
209
210@lisp
211debug> bt
212In standard input:
213 36: 0* [make-string ...
214 36: 1* [* 4 ...
215 36: 2* [+ 3 #\s]
216@end lisp
217
218@deffn {Debugger Command} backtrace [count]
219@deffnx {Debugger Command} bt [count]
220@deffnx {Debugger Command} where [count]
221Print backtrace of all stack frames, or of the innermost @var{count}
222frames. With a negative argument, print the outermost -@var{count}
223frames. If the number of frames isn't explicitly given, the debug
224option @code{depth} determines the maximum number of frames printed.
225@end deffn
226
227The format of the displayed backtrace is the same as for the
ee6be719 228@code{display-backtrace} procedure (@pxref{Examining the Stack}).
46f7666d
NJ
229
230
231@node Frame Selection
232@subsubsection Frame Selection
233
234A call stack consists of a sequence of stack @dfn{frames}, with each
235frame describing one level of the nested evaluations and applications
236that the program was executing when it hit a breakpoint or an error.
237Frames are numbered such that frame 0 is the outermost --- i.e. the
238operation on the call stack that began least recently --- and frame N-1
239the innermost (where N is the total number of frames on the stack).
240
241When you enter the debugger, the innermost frame is selected, which
242means that the commands for getting information about the ``current''
243frame, or for evaluating expressions in the context of the current
244frame, will do so by default with respect to the innermost frame. To
245select a different frame, so that these operations will apply to it
246instead, use the @code{up}, @code{down} and @code{frame} commands like
247this:
248
249@lisp
250debug> up
251Frame 1 at standard input:36:14
252 [* 4 ...
253debug> frame 0
254Frame 0 at standard input:36:1
255 [make-string ...
256debug> down
257Frame 1 at standard input:36:14
258 [* 4 ...
259@end lisp
260
261@deffn {Debugger Command} up [n]
262Move @var{n} frames up the stack. For positive @var{n}, this
ee6be719 263advances toward the outermost frame, to lower frame numbers, to
46f7666d
NJ
264frames that have existed longer. @var{n} defaults to one.
265@end deffn
266
267@deffn {Debugger Command} down [n]
268Move @var{n} frames down the stack. For positive @var{n}, this
ee6be719 269advances toward the innermost frame, to higher frame numbers, to frames
46f7666d
NJ
270that were created more recently. @var{n} defaults to one.
271@end deffn
272
273@deffn {Debugger Command} frame [n]
274Select and print a stack frame. With no argument, print the selected
275stack frame. (See also ``info frame''.) An argument specifies the
276frame to select; it must be a stack-frame number.
277@end deffn
278
279
280@node Frame Information
281@subsubsection Frame Information
282
ee6be719
NJ
283The following commands return detailed information about the currently
284selected frame.
46f7666d
NJ
285
286@deffn {Debugger Command} {info frame}
ee6be719
NJ
287Display a verbose description of the selected frame. The information
288that this command provides is equivalent to what can be deduced from the
289one line summary for the frame that appears in a backtrace, but is
290presented and explained more clearly.
46f7666d
NJ
291@end deffn
292
293@deffn {Debugger Command} {info args}
ee6be719
NJ
294Display the argument variables of the current stack frame. Arguments
295can also be seen in the backtrace, but are presented more clearly by
296this command.
46f7666d
NJ
297@end deffn
298
299@deffn {Debugger Command} position
ee6be719
NJ
300Display the name of the source file that the current expression comes
301from, and the line and column number of the expression's opening
302parenthesis within that file. This information is only available when
303the @code{positions} read option is enabled (@pxref{Reader options}).
46f7666d
NJ
304@end deffn
305
306
307@node Frame Evaluation
308@subsubsection Frame Evaluation
309
ee6be719
NJ
310The @code{evaluate} command is most useful for querying the value of a
311variable, either global or local, in the environment of the selected
312stack frame, but it can be used more generally to evaluate any
313expression.
46f7666d
NJ
314
315@deffn {Debugger Command} evaluate expression
ee6be719
NJ
316Evaluate an expression in the environment of the selected stack frame.
317The expression must appear on the same line as the command, however it
318may be continued over multiple lines.
46f7666d
NJ
319@end deffn
320
321
ee6be719
NJ
322@node Stepping and Continuing
323@subsubsection Single Stepping and Continuing Execution
46f7666d 324
ee6be719
NJ
325The commands in this subsection all apply only when the stack is
326@dfn{continuable} --- in other words when it makes sense for the program
327that the stack comes from to continue running. Usually this means that
328the program stopped because of a trap or a breakpoint.
46f7666d
NJ
329
330@deffn {Debugger Command} step [n]
ee6be719
NJ
331Tell the debugged program to do @var{n} more steps from its current
332position. One @dfn{step} means executing until the next frame entry or
333exit of any kind. @var{n} defaults to 1.
46f7666d
NJ
334@end deffn
335
336@deffn {Debugger Command} next [n]
ee6be719
NJ
337Tell the debugged program to do @var{n} more steps from its current
338position, but only counting frame entries and exits where the
339corresponding source code comes from the same file as the current stack
340frame. (See @ref{Step Traps} for the details of how this works.) If
341the current stack frame has no source code, the effect of this command
342is the same as of @code{step}. @var{n} defaults to 1.
46f7666d
NJ
343@end deffn
344
46f7666d 345@deffn {Debugger Command} finish
ee6be719
NJ
346Tell the program being debugged to continue running until the completion
347of the current stack frame, and at that time to print the result and
348reenter the command line debugger.
46f7666d
NJ
349@end deffn
350
46f7666d 351@deffn {Debugger Command} continue
ee6be719
NJ
352Tell the program being debugged to continue running. (In fact this is
353the same as the @code{quit} command, because it exits the debugger
354command loop and so allows whatever code it was that invoked the
355debugger to continue.)
46f7666d
NJ
356@end deffn
357
358
359@node Using Guile in Emacs
360@section Using Guile in Emacs
361
b20ef3a6
NJ
362@cindex GDS
363@cindex Emacs
24dbb5ed
NJ
364There are several options for working on Guile Scheme code in Emacs.
365The simplest are to use Emacs's standard @code{scheme-mode} for
366editing code, and to run the interpreter when you need it by typing
367``guile'' at the prompt of a @code{*shell*} buffer, but there are
368Emacs libraries available which add various bells and whistles to
369this. The following diagram shows these libraries and how they relate
370to each other, with the arrows indicating ``builds on'' or
371``extends''. For example, the Quack library builds on cmuscheme,
372which in turn builds on the standard scheme mode.
01d2ee15 373
e163dd34
BG
374@iftex
375@center @image{scheme,5in}
376@end iftex
377@ifnottex
01d2ee15 378@example
e163dd34 379@verbatiminclude scheme.txt
01d2ee15 380@end example
e163dd34 381@end ifnottex
01d2ee15
NJ
382
383@dfn{scheme}, written by Bill Rozas and Dave Love, is Emacs's standard
384mode for Scheme code files. It provides Scheme-sensitive syntax
385highlighting, parenthesis matching, indentation and so on.
386
387@dfn{cmuscheme}, written by Olin Shivers, provides a comint-based Scheme
388interaction buffer, so that you can run an interpreter more directly
94a2c24a 389than with the @code{*shell*} buffer approach by typing @kbd{M-x
01d2ee15
NJ
390run-scheme}. It also extends @code{scheme-mode} so that there are key
391presses for sending selected bits of code from a Scheme buffer to this
392interpreter. This means that when you are writing some code and want to
393check what an expression evaluates to, you can easily select that code
394and send it to the interpreter for evaluation, then switch to the
395interpreter to see what the result is. cmuscheme is included in the
396standard Emacs distribution.
397
398@dfn{Quack}, written by Neil Van Dyke, adds a number of incremental
399improvements to the scheme/cmuscheme combination: convenient menu
400entries for looking up Scheme-related references (such as the SRFIs);
401enhanced indentation rules that are customized for particular Scheme
402interpreters, including Guile; an enhanced version of the
403@code{run-scheme} command that knows the names of the common Scheme
404interpreters and remembers which one you used last time; and so on.
405Quack is available from @uref{http://www.neilvandyke.org/quack}.
406
407@dfn{GDS}, written by Neil Jerram, also builds on the scheme/cmuscheme
24dbb5ed
NJ
408combination, but with a change to the way that Scheme code fragments
409are sent to the interpreter for evaluation. cmuscheme and Quack send
410code fragments to the interpreter's standard input, on the assumption
411that the interpreter is expecting to read Scheme expressions there,
412and then monitor the interpreter's standard output to infer what the
413result of the evaluation is. GDS doesn't use standard input and
01d2ee15
NJ
414output like this. Instead, it sets up a socket connection between the
415Scheme interpreter and Emacs, and sends and receives messages using a
416simple protocol through this socket. The messages include requests to
417evaluate Scheme code, and responses conveying the results of an
24dbb5ed
NJ
418evaluation, thus providing similar function to cmuscheme or Quack.
419They also include requests for stack exploration and debugging, which
420go beyond what cmuscheme or Quack can do. The price of this extra
421power, however, is that GDS is Guile-specific. GDS requires the
422Scheme interpreter to run some GDS-specific library code; currently
423this code is written as a Guile module and uses features that are
01d2ee15 424specific to Guile. GDS is now included in the Guile distribution; for
24dbb5ed 425previous Guile releases (1.8.4 and earlier) it can be obtained as part
01d2ee15
NJ
426of the @code{guile-debugging} package from
427@uref{http://www.ossau.uklinux.net/guile}.
428
94a2c24a
NJ
429Finally, @dfn{xscheme} is similar to cmuscheme --- in that it starts up
430a Scheme interaction process and sends commands to that process's
431standard input --- and to GDS --- in that it has support beyond
432cmuscheme or Quack for exploring the Scheme stack when an error has
433occurred --- but is implemented specifically for MIT/GNU Scheme. Hence
434it isn't really relevant to Guile work in Emacs, except as a reference
435for useful features that could be implemented in one of the other
436libraries mentioned here.
437
438In summary, the best current choice for working on Guile code in Emacs
439is either Quack or GDS, depending on which of these libraries' features
440you find most important. For more information on Quack, please see the
441website referenced above. GDS is documented further in the rest of this
442section.
01d2ee15
NJ
443
444@menu
445* GDS Introduction::
94a2c24a 446* GDS Architecture::
01d2ee15 447* GDS Getting Started::
72bcfa04 448* Working with GDS in Scheme Buffers::
01d2ee15
NJ
449* Displaying the Scheme Stack::
450* Continuing Execution::
01d2ee15
NJ
451* Associating Buffers with Clients::
452* An Example GDS Session::
01d2ee15
NJ
453@end menu
454
455
456@node GDS Introduction
457@subsection GDS Introduction
458
459GDS aims to allow you to work on Guile Scheme code in the same kind of
460way that Emacs allows you to work on Emacs Lisp code: providing easy
461access to help, evaluating arbitrary fragments of code, a nice debugging
94a2c24a 462interface, and so on. The thinking behind the GDS library is that you
01d2ee15 463will usually be doing one of two things.
46f7666d
NJ
464
465@enumerate
466@item
01d2ee15
NJ
467Writing or editing code. The code will be in a normal Emacs Scheme mode
468buffer, and GDS extends Scheme mode to add keystrokes and menu items for
469the things that are likely to be useful to you when working on code:
46f7666d
NJ
470
471@itemize
472@item
94a2c24a
NJ
473completing the identifier at point, with respect to the set of variable
474names that are known to the associated Guile process
46f7666d 475@item
94a2c24a 476accessing Guile's built in ``help'' and ``apropos'' commands
46f7666d 477@item
94a2c24a
NJ
478evaluating fragments of code to check what they do, with the results
479popping up in a temporary Emacs window.
46f7666d
NJ
480@end itemize
481
482@item
69986e21
NJ
483Debugging a Guile Scheme program. When your program hits an error or
484stops at a trap, GDS shows you the relevant code and the Scheme stack,
485and makes it easy to
46f7666d
NJ
486
487@itemize
488@item
489look at the values of local variables
490@item
491see what is happening at all levels of the Scheme stack
492@item
493continue execution, either normally or step by step.
494@end itemize
46f7666d 495
94a2c24a
NJ
496The presentation makes it very easy to move up and down the stack,
497showing whenever possible the source code for each frame in another
498Emacs buffer. It also provides convenient keystrokes for telling Guile
499what to do next; for example, you can select a stack frame and tell
500Guile to run until that frame completes, at which point GDS will display
501the frame's return value.
4f6e3015 502@end enumerate
94a2c24a 503
94a2c24a
NJ
504GDS can provide these facilities for any number of Guile Scheme programs
505(which we often refer to as ``clients'') at once, and these programs can
506be started either independently of GDS, including outside Emacs, or
507specifically @emph{by} GDS.
62ae9557 508
94a2c24a
NJ
509Communication between each Guile client program and GDS uses a TCP
510socket, which means that it is orthogonal to any other interfaces that
511the client program has. In particular GDS does not interfere with a
512program's standard input and output.
62ae9557 513
94a2c24a
NJ
514
515@node GDS Architecture
516@subsection GDS Architecture
517
518In order to understand the following documentation fully it will help to
519have a picture in mind of how GDS works, so we briefly describe that
520here. GDS consists of three components.
46f7666d 521
62ae9557
NJ
522@itemize
523@item
94a2c24a
NJ
524The GDS @dfn{interface} code is written in Emacs Lisp and runs inside
525Emacs. This code, consisting of the installed files @file{gds.el} and
526@file{gds-server.el}, is responsible for displaying information from
527Guile in Emacs windows, and for responding to Emacs commands and
528keystrokes by sending instructions back to the Guile program being
24dbb5ed 529worked on.
94a2c24a
NJ
530
531@item
532The GDS @dfn{server} code is written in Scheme and runs as an Emacs
533inferior process. It acts as a multiplexer between the (possibly
534multiple) Guile programs being debugged and the interface code running
535in Emacs. The server code is the installed file
536@file{gds-server.scm}.
62ae9557
NJ
537
538@item
94a2c24a
NJ
539The GDS @dfn{client} code is written in Scheme (installed file
540@file{gds-client.scm}), and must be loaded as a module by each Guile
541program that wants to use GDS in any way.
62ae9557
NJ
542@end itemize
543
544@noindent
94a2c24a
NJ
545The following diagram shows how these components are connected to each
546other.
62ae9557 547
e163dd34
BG
548@iftex
549@center @image{gds,5in}
550@end iftex
551@ifnottex
94a2c24a 552@example
e163dd34 553@verbatiminclude gds.txt
94a2c24a 554@end example
e163dd34 555@end ifnottex
62ae9557 556
94a2c24a
NJ
557@cindex TCP, use of
558The data exchanged between client and server components, and between
559server and interface, is a sequence of sexps (parenthesised expressions)
560that are designed so as to be directly readable by both Scheme and Emacs
561Lisp. The use of a TCP connection means that the server and Emacs
24dbb5ed
NJ
562interface can theoretically be on a different computer from the client
563programs, but in practice there are currently two problems with
94a2c24a
NJ
564this. Firstly the GDS API doesn't provide any way of specifying a
565non-local server to connect to, and secondly there is no security or
566authentication mechanism in the GDS protocol. These are issues that
24dbb5ed 567should be addressed in the future.
62ae9557 568
94a2c24a 569
01d2ee15
NJ
570@node GDS Getting Started
571@subsection Getting Started with GDS
62ae9557 572
94a2c24a 573To enable the use of GDS in your own Emacs sessions, simply add
62ae9557
NJ
574
575@lisp
576(require 'gds)
577@end lisp
578
579@noindent
94a2c24a
NJ
580somewhere in your @file{.emacs} file. This will cause Emacs to load the
581GDS Emacs Lisp code when starting up, and to start the inferior GDS
582server process so that it is ready and waiting for any Guile programs
583that want to use GDS.
62ae9557 584
94a2c24a
NJ
585(If GDS's Scheme code is not installed in one of the locations in
586Guile's load path, you may find that the server process fails to start.
587When this happens you will see an error message from Emacs:
62ae9557 588
94a2c24a
NJ
589@lisp
590error in process filter: Wrong type argument: listp, Backtrace:
591@end lisp
62ae9557 592
94a2c24a
NJ
593@noindent
594and the @code{gds-debug} buffer will contain a Scheme backtrace ending
595with the message:
62ae9557 596
94a2c24a 597@lisp
24dbb5ed 598no code for module (ice-9 gds-server)
94a2c24a 599@end lisp
62ae9557 600
94a2c24a
NJ
601@noindent
602The solution for this is to customize the Emacs variable
603@code{gds-scheme-directory} so that it specifies where the GDS Scheme
604code is installed. Then either restart Emacs or type @kbd{M-x
605gds-run-debug-server} to try starting the GDS server process again.)
606
607For evaluations, help and completion from Scheme code buffers that you
608are working on, this is all you need. The first time you do any of
609these things, GDS will automatically start a new Guile client program as
610an Emacs subprocess. This Guile program does nothing but wait for and
611act on instructions from GDS, and we refer to it as a @dfn{utility}
612Guile client. Over time this utility client will accumulate the code
613that you ask it to evaluate, and you can also tell it to load complete
614files or modules by sending it @code{load} or @code{use-modules}
69986e21 615expressions.
94a2c24a 616
24dbb5ed 617When you want to use GDS to work on an independent Guile
94a2c24a
NJ
618application, you need to add something to that application's Scheme code
619to cause it to connect to and interact with GDS at the right times. The
24dbb5ed 620following subsections describe the ways of doing this.
62ae9557 621
94a2c24a 622@subsubsection Invoking GDS when an Exception Occurs
62ae9557 623
69986e21 624One option is to use GDS to catch and display any exceptions that
1e1387ca
NJ
625are thrown by the application's code. If you already have a
626@code{lazy-catch} or @code{with-throw-handler} around the area of code
627that you want to monitor, you just need to add the following to the
628handler code:
629
630@lisp
631(gds-debug-trap (throw->trap-context key args))
632@end lisp
633
634@noindent
635where @code{key} and @code{args} are the first and rest arguments that
636Guile passes to the handler. (In other words, they assume the handler
637signature @code{(lambda (key . args) @dots{})}.) With Guile 1.8 or
638later, you can also do this with a @code{catch}, by adding this same
aeb9d8e0 639code to the catch's pre-unwind handler.
1e1387ca
NJ
640
641If you don't already have any of these, insert a whole
aeb9d8e0
NJ
642@code{with-throw-handler} expression (or @code{lazy-catch} if your Guile
643is pre-1.8) around the code of interest like this:
1e1387ca
NJ
644
645@lisp
646(with-throw-handler #t
647 (lambda ()
648 ;; Protected code here.
649 )
650 (lambda (key . args)
651 (gds-debug-trap (throw->trap-context key args))))
652@end lisp
653
24dbb5ed 654Either way, you will need to use the @code{(ice-9 gds-client)} and
1e1387ca
NJ
655@code{(ice-9 debugging traps)} modules.
656
657Two special cases of this are the lazy-catch that the Guile REPL code
658uses to catch exceptions in user code, and the lazy-catch inside the
659@code{stack-catch} utility procedure that is provided by the
660@code{(ice-9 stack-catch)} module. Both of these use a handler called
aeb9d8e0
NJ
661@code{lazy-handler-dispatch} (defined in @file{boot-9.scm}), which you
662can hook into such that it calls GDS to display the stack when an
663exception occurs. To do this, use the @code{on-lazy-handler-dispatch}
664procedure as follows.
1e1387ca 665
62ae9557 666@lisp
94a2c24a
NJ
667(use-modules (ice-9 gds-client)
668 (ice-9 debugging traps))
94a2c24a 669(on-lazy-handler-dispatch gds-debug-trap)
62ae9557
NJ
670@end lisp
671
1e1387ca
NJ
672@noindent
673After this the program will use GDS to display the stack whenever it
674hits an exception that is protected by a @code{lazy-catch} using
aeb9d8e0 675@code{lazy-handler-dispatch}.
62ae9557
NJ
676
677@subsubsection Accepting GDS Instructions at Any Time
678
69986e21
NJ
679In addition to setting an exception handler as described above, a
680Guile program can in principle set itself up to accept new
681instructions from GDS at any time, not just when it has stopped at an
682exception. This would allow the GDS user to evaluate code in the
683context of the running program, without having to wait for the program
684to stop first.
62ae9557
NJ
685
686@lisp
687(use-modules (ice-9 gds-client))
688(gds-accept-input #t)
689@end lisp
690
691@code{gds-accept-input} causes the calling program to loop processing
692instructions from GDS, until GDS sends the @code{continue} instruction.
693This blocks the thread that calls it, however, so it will normally be
694more practical for the program to set up a dedicated GDS thread and call
695@code{gds-accept-input} from that thread.
696
697For @code{select}-driven applications, an alternative approach would be
698for the GDS client code to provide an API which allowed the application
699to
700
701@itemize
702@item
703discover the file descriptors (or Scheme ports) that are used for
704receiving instruction from the GDS front end, so that it could include
705these in its @code{select} call
706
707@item
708call the GDS instruction handler when @code{select} indicated data
709available for reading on those descriptors/ports.
710@end itemize
711
712@noindent
713This approach is not yet implemented, though.
714
715@subsubsection Utility Guile Implementation
716
24dbb5ed
NJ
717The ``utility'' Guile client mentioned above is a simple combination
718of the mechanisms that we have just described. In fact the code for
719the utility Guile client is essentially just this:
62ae9557
NJ
720
721@lisp
722(use-modules (ice-9 gds-client))
62ae9557
NJ
723(named-module-use! '(guile-user) '(ice-9 session))
724(gds-accept-input #f))
725@end lisp
726
69986e21 727The @code{named-module-use!} line ensures that the client can process
1e1387ca 728@code{help} and @code{apropos} expressions, to implement lookups in
24dbb5ed
NJ
729Guile's online help. The @code{#f} parameter to
730@code{gds-accept-input} means that the @code{continue} instruction
731will not cause the instruction loop to exit, which makes sense here
732because the utility client has nothing to do except to process GDS
733instructions.
734
735The utility client does not use @code{on-lazy-handler-dispatch} at its
736top level, because it has its own mechanism for catching and reporting
737exceptions in the code that it is asked to evaluate. This mechanism
738summarizes the exception and gives the user a button they can click to
739see the full stack, so the end result is very similar to what
740@code{on-lazy-handler-dispatch} provides. Deep inside
741@code{gds-accept-input}, in the part that handles evaluating
742expressions from Emacs, the GDS client code uses
743@code{throw->trap-context} and @code{gds-debug-trap} to implement
744this.
62ae9557
NJ
745
746
72bcfa04
NJ
747@node Working with GDS in Scheme Buffers
748@subsection Working with GDS in Scheme Buffers
749
750The following subsections describe the facilities and key sequences that
751GDS provides for working on code in @code{scheme-mode} buffers.
752
753@menu
754* Access to Guile Help and Completion::
72bcfa04
NJ
755* Evaluating Scheme Code::
756@end menu
757
758
7e5a256c 759@node Access to Guile Help and Completion
72bcfa04 760@subsubsection Access to Guile Help and Completion
7e5a256c
NJ
761
762The following keystrokes provide fast and convenient access to Guile's
763built in help, and to completion with respect to the set of defined and
72bcfa04 764accessible symbols.
7e5a256c
NJ
765
766@table @kbd
767@item C-h g
768@findex gds-help-symbol
769Get Guile help for a particular symbol, with the same results as if
770you had typed @code{(help SYMBOL)} into the Guile REPL
771(@code{gds-help-symbol}). The symbol to query defaults to the word at
772or before the cursor but can also be entered or edited in the
773minibuffer. The available help is popped up in a temporary Emacs
774window.
775
24dbb5ed 776@item C-h G
7e5a256c
NJ
777@findex gds-apropos
778List all accessible Guile symbols matching a given regular expression,
779with the same results as if you had typed @code{(apropos REGEXP)} into
780the Guile REPL (@code{gds-apropos}). The regexp to query defaults to
781the word at or before the cursor but can also be entered or edited in
782the minibuffer. The list of matching symbols is popped up in a
783temporary Emacs window.
784
785@item M-@key{TAB}
786@findex gds-complete-symbol
787Try to complete the symbol at the cursor by matching it against the
788set of all defined and accessible bindings in the associated Guile
789process (@code{gds-complete-symbol}). If there are any extra
790characters that can be definitively added to the symbol at point, they
791are inserted. Otherwise, if there are any completions available, they
792are popped up in a temporary Emacs window, where one of them can be
793selected using either @kbd{@key{RET}} or the mouse.
794@end table
795
796
7e5a256c 797@node Evaluating Scheme Code
72bcfa04 798@subsubsection Evaluating Scheme Code
7e5a256c
NJ
799
800The following keystrokes and commands provide various ways of sending
801code to a Guile client process for evaluation.
802
803@table @kbd
804@item M-C-x
805@findex gds-eval-defun
806Evaluate the ``top level defun'' that the cursor is in, in other words
807the smallest balanced expression which includes the cursor and whose
808opening parenthesis is in column 0 (@code{gds-eval-defun}).
809
810@item C-x C-e
811@findex gds-eval-last-sexp
812Evaluate the expression that ends just before the cursor
813(@code{gds-eval-last-sexp}). This is designed so that it is easy to
814evaluate an expression that you have just finished typing.
815
816@item C-c C-e
817@findex gds-eval-expression
818Read a Scheme expression using the minibuffer, and evaluate that
819expression (@code{gds-eval-expression}).
820
821@item C-c C-r
822@findex gds-eval-region
823Evaluate the Scheme code in the marked region of the current buffer
824(@code{gds-eval-region}). Note that GDS does not check whether the
825region contains a balanced expression, or try to expand the region so
826that it does; it uses the region exactly as it is.
827@end table
828
091baf9e
NJ
829If you type @kbd{C-u} before one of these commands, GDS will
830immediately pop up a Scheme stack buffer, showing the requested
831evaluation, so that you can single step through it. (This is achieved
832by setting a @code{<source-trap>} trap at the start of the requested
833evaluation; see @ref{Source Traps} for more on how those work.) The
834Scheme stack display, and the options for continuing through the code,
835are described in the next two sections.
836
7e5a256c 837
62ae9557
NJ
838@node Displaying the Scheme Stack
839@subsection Displaying the Scheme Stack
840
69986e21
NJ
841When you specify @code{gds-debug-trap} as the behaviour for a trap and
842the Guile program concerned hits that trap, GDS displays the stack and
843the relevant Scheme source code in Emacs, allowing you to explore the
844state of the program and then decide what to do next. The same
845applies if the program calls @code{(on-lazy-handler-dispatch
846gds-debug-trap)} and then throws an exception that passes through
847@code{lazy-handler-dispatch}, except that in this case you can only
848explore; it isn't possible to continue normal execution after an
849exception.
62ae9557
NJ
850
851The following commands are available in the stack buffer for exploring
852the state of the program.
853
854@table @asis
855@item @kbd{u}, @kbd{C-p}, @kbd{@key{up}}
856@findex gds-up
857Select the stack frame one up from the currently selected frame
858(@code{gds-up}). GDS displays stack frames with the innermost at the
859top, so moving ``up'' means selecting a more ``inner'' frame.
860
861@item @kbd{d}, @kbd{C-n}, @kbd{@key{down}}
862@findex gds-down
863Select the stack frame one down from the currently selected frame
864(@code{gds-down}). GDS displays stack frames with the innermost at the
865top, so moving ``down'' means selecting a more ``outer'' frame.
866
867@item @kbd{@key{RET}}
868@findex gds-select-stack-frame
869Select the stack frame at point (@code{gds-select-stack-frame}). This
870is useful after clicking somewhere in the stack trace with the mouse.
871@end table
872
873Selecting a frame means that GDS will display the source code
874corresponding to that frame in the adjacent window, and that
875subsequent frame-sensitive commands, such as @code{gds-evaluate} (see
876below) and @code{gds-step-over} (@pxref{Continuing Execution}), will
877refer to that frame.
878
879@table @kbd
880@item e
881@findex gds-evaluate
882Evaluate a variable or expression in the local environment of the
883selected stack frame (@code{gds-evaluate}). The result is displayed in
884the echo area.
885
886@item I
887@findex gds-frame-info
888Show summary information about the selected stack frame
889(@code{gds-frame-info}). This includes what type of frame it is, the
890associated expression, and the frame's source location, if any.
891
892@item A
893@findex gds-frame-args
894For an application frame, display the frame's arguments
895(@code{gds-frame-args}).
896
897@item S
898@findex gds-proc-source
899For an application frame, show the Scheme source code of the procedure
900being called (@code{gds-proc-source}). The source code (where
901available) is displayed in the echo area.
902@end table
903
904@kbd{S} (@code{gds-proc-source}) is useful when the procedure being
905called was created by an anonymous @code{(lambda @dots{})} expression.
906Such procedures appear in the stack trace as @code{<procedure #f
907(@dots{})>}, which doesn't give you much clue as to what will happen
908next. @kbd{S} will show you the procedure's code, which is usually
909enough for you to identify it.
910
911
912@node Continuing Execution
913@subsection Continuing Execution
914
915If it makes sense to continue execution from the stack which is being
916displayed, GDS provides the following further commands in the stack
917buffer.
918
919@table @asis
920@item @kbd{g}, @kbd{c}, @kbd{q}
921@findex gds-go
922Tell the program to continue running (@code{gds-go}). It may of course
923stop again if it hits another trap, or another occurrence of the same
924trap.
925
926The multiple keystrokes reflect that you can think of this as ``going'',
927``continuing'' or ``quitting'' (in the sense of quitting the GDS
928display).
929
930@item @kbd{@key{SPC}}
931@findex gds-step-file
932Tell the program to do a single-step to the next entry or exit of a
933frame whose code comes from the same source file as the selected stack
934frame (@code{gds-step-file}).
935
936In other words, you can hit @kbd{@key{SPC}} repeatedly to step through
937the code in a given file, automatically stepping @emph{over} any
938evaluations or procedure calls that use code from other files (or from
939no file).
940
941If the selected stack frame has no source, the effect of this command is
942the same as that of @kbd{i}, described next.
943
944@item @kbd{i}
945@findex gds-step-into
946Tell the debugged program to do a single-step to the next frame entry or
947exit of any kind (@code{gds-step-into}). @kbd{i} therefore steps
948through code at the most detailed level possible.
949
950@item @kbd{o}
951@findex gds-step-over
952Tell the debugged program to continue running until the selected stack
953frame completes, and then to display its result (@code{gds-step-over}).
954Note that the program may stop before then if it hits another trap; in
955this case the trap telling it to stop when the marked frame completes
956remains in place and so will still fire at the appropriate point.
957@end table
958
959
62ae9557
NJ
960@node Associating Buffers with Clients
961@subsection Associating Buffers with Clients
962
963The first time that you use one of GDS's evaluation, help or completion
964commands from a given Scheme mode buffer, GDS will ask which Guile
965client program you want to use for the operation, or if you want to
966start up a new ``utility'' client. After that GDS considers the buffer
967to be ``associated'' with the selected client, and so sends all further
968requests to that client, but you can override this by explicitly
969associating the buffer with a different client, or by removing the
970default association.
971
972@table @kbd
973@item M-x gds-associate-buffer
974Associate (or re-associate) the current buffer with a particular Guile
975client program. The available clients are listed, and you can also
976choose to start up a new ``utility'' client for this buffer to associate
977with.
978
979@item M-x gds-dissociate-buffer
980Dissociate the current buffer from its client, if any. This means that
981the next time you use an evaluation, help or completion command, GDS
982will ask you again which client to send the request to.
983@end table
984
985When a buffer is associated with a client program, the buffer's modeline
986shows whether the client is currently able to accept instruction from
987GDS. This is done by adding one of the following suffixes to the
988``Scheme'' major mode indicator:
989
990@table @asis
991@item :ready
992The client program (or one of its threads, if multithreaded) is
993currently ready to accept instruction from GDS. In other words, if you
994send it a help or evaluation request, you should see the result pretty
995much immediately.
996
997@item :running
998The client program is not currently able to accept instruction from
999GDS. This means that it (or all of its threads, if multithreaded) is
1000busy, or waiting for input other than from GDS.
1001
1002@item :debug
1003The client program (or one of its threads, if multithreaded) is stopped
1004in ``debugging mode'' with GDS displaying the stack for a trap or
1005exception. It is waiting for instruction from GDS on what to do next.
1006@end table
1007
1008
1009@node An Example GDS Session
1010@subsection An Example GDS Session
1011
1012Create a file, @file{testgds.scm} say, for experimenting with GDS and
1013Scheme code, and type this into it:
1014
1015@lisp
1016(use-modules (ice-9 debugging traps)
1017 (ice-9 gds-client)
1018 (ice-9 debugging example-fns))
1019(install-trap (make <procedure-trap>
1020 #:behaviour gds-debug-trap
1021 #:procedure fact1))
1022@end lisp
1023
1024@noindent
1025Now select all of this code and type @kbd{C-c C-r} to send the selected
1026region to Guile for evaluation. GDS will ask you which Guile process to
1027use; unless you know that you already have another Guile application
1028running and connected to GDS, choose the ``Start a new Guile'' option,
94a2c24a
NJ
1029which starts one of the ``utility'' processes described in @ref{GDS
1030Getting Started}.
62ae9557
NJ
1031
1032The results of the evaluation pop up in a window like this:
1033
1034@lisp
1035(use-modules (ice-9 debugging traps)\n @dots{}
1036
1037;;; Evaluating subexpression 1 in current module (guile-user)
1038 @result{} no (or unspecified) value
1039
1040;;; Evaluating subexpression 2 in current module (guile-user)
1041 @result{} no (or unspecified) value
1042
1043--:** *Guile Evaluation* (Scheme:ready)--All------------
1044@end lisp
1045
1046@noindent
1047this tells you that the evaluation was successful but that the return
1048values were unspecified. Its effect was to load a module of example
1049functions and set a trap on one of these functions, @code{fact1}, that
1050calculates the factorial of its argument.
1051
1052If you now call @code{fact1}, you can see the trap and GDS's stack
1053display in action. To do this add
1054
1055@lisp
1056(fact1 4)
1057@end lisp
1058
1059@noindent
1060to your @file{testgds.scm} buffer and type @kbd{C-x C-e} (which
24dbb5ed
NJ
1061evaluates the expression that the cursor is just after the end of).
1062The result should be that a GDS stack window like the following
1063appears:
62ae9557
NJ
1064
1065@lisp
1066Calling procedure:
1067=> s [fact1 4]
1068 s [primitive-eval (fact1 4)]
1069
1070
1071--:** PID 28729 (Guile-Debug)--All------------
1072@end lisp
1073
24dbb5ed
NJ
1074This stack tells you that Guile is about to call the @code{fact1}
1075procedure, with argument 4, and you can step through this call in
1076detail by pressing @kbd{i} once and then @kbd{@key{SPC}}
1077(@pxref{Continuing Execution}).
62ae9557
NJ
1078
1079(@kbd{i} is needed as the first keystroke rather than @kbd{@key{SPC}},
1080because the aim here is to step through code in the @code{(ice-9
1081debugging example-fns)} module, whose source file is
1082@file{@dots{}/ice-9/debugging/example-fns.scm}, but the initial
1083@code{(fact1 4)} call comes from the Guile session, whose ``source
1084file'' Guile presents as @file{standard input}. If the user starts by
1085pressing @kbd{@key{SPC}} instead of @kbd{i}, the effect is that the
1086program runs until it hits the first recursive call @code{(fact1 (- n
10871))}, where it stops because of the trap on @code{fact1} firing again.
1088At this point, the source file @emph{is}
1089@file{@dots{}/ice-9/debugging/example-fns.scm}, because the recursive
1090@code{(fact1 (- n 1))} call comes from code in that file, so further
1091pressing of @kbd{@key{SPC}} successfully single-steps through this
1092file.)
1093
1094
46f7666d
NJ
1095@c Local Variables:
1096@c TeX-master: "guile.texi"
1097@c End: