add vm-abort-continuation-hook, vm-restore-continuation-hook
[bpt/guile.git] / emacs / gds-tutorial.txt
1
2 ;; Welcome to the GDS tutorial!
3
4 ;; This tutorial teaches the use of GDS by leading you through a set
5 ;; of examples where you actually use GDS, in Emacs, along the way.
6 ;; To get maximum benefit, therefore, you should be reading this
7 ;; tutorial in Emacs.
8
9 ;; ** GDS setup
10
11 ;; The first thing to do, if you haven't already, is to load the GDS
12 ;; library into Emacs. The Emacs Lisp expression for this is:
13
14 (require 'gds)
15
16 ;; So, if you don't already have this in your .emacs, either add it
17 ;; and then restart Emacs, or evaluate it just for this Emacs session
18 ;; by moving the cursor to just after the closing parenthesis and
19 ;; typing `C-x C-e'.
20
21 ;; (Note that if you _have_ already loaded GDS, and you type `C-x C-e'
22 ;; after this expression, you will see a *Guile Evaluation* window
23 ;; telling you that the evaluation failed because `require' is
24 ;; unbound. Don't worry; this is not a problem, and the rest of the
25 ;; tutorial should still work just fine.)
26
27 ;; ** Help
28
29 ;; GDS makes it easy to access the Guile help system when working on a
30 ;; Scheme program in Emacs. For example, suppose that you are writing
31 ;; code that uses list-ref, and need to remind yourself about
32 ;; list-ref's arguments ...
33
34 (define (penultimate l)
35 (list-ref
36
37 ;; Just place the cursor on the word "list-ref" and type `C-h g RET'.
38 ;; Try it now!
39
40 ;; If GDS is working correctly, a window should have popped up above
41 ;; or below showing the Guile help for list-ref.
42
43 ;; You can also do an "apropos" search through Guile's help. If you
44 ;; couldn't remember the name list-ref, for example, you could search
45 ;; for anything matching "list" by typing `C-h C-g' and entering
46 ;; "list" at the minibuffer prompt. Try doing this now: you should
47 ;; see a longish list of Guile definitions whose names include "list".
48 ;; As usual in Emacs, you can use `M-PageUp' and `M-PageDown' to
49 ;; conveniently scroll the other window without having to select it.
50
51 ;; The functions called by `C-h g' and `C-h C-g' are gds-help-symbol
52 ;; and gds-apropos. They both look up the symbol or word at point by
53 ;; default, but that default can be overidden by typing something else
54 ;; at the minibuffer prompt.
55
56 ;; ** Completion
57
58 ;; As you are typing Scheme code, you can ask GDS to complete the
59 ;; symbol before point for you, by typing `ESC TAB'. GDS selects
60 ;; possible completions by matching the text so far against all
61 ;; definitions in the Guile environment. (This may be contrasted with
62 ;; the "dabbrev" completion performed by `M-/', which selects possible
63 ;; completions from the contents of Emacs buffers. So, if you are
64 ;; trying to complete "with-ou", to get "with-output-to-string", for
65 ;; example, `ESC TAB' will always work, because with-output-to-string
66 ;; is always defined in Guile's default environment, whereas `M-/'
67 ;; will only work if one of Emacs's buffers happens to contain the
68 ;; full name "with-output-to-string".)
69
70 ;; To illustrate the idea, here are some partial names that you can
71 ;; try completing. For each one, move the cursor to the end of the
72 ;; line and type `ESC TAB' to try to complete it.
73
74 list-
75 with-ou
76 with-output-to-s
77 mkst
78
79 ;; (If you are not familiar with any of the completed definitions,
80 ;; feel free to use `C-h g' to find out about them!)
81
82 ;; ** Evaluation
83
84 ;; GDS provides several ways for you to evaluate Scheme code from
85 ;; within Emacs.
86
87 ;; Just like in Emacs Lisp, a single expression in a buffer can be
88 ;; evaluated using `C-x C-e' or `C-M-x'. For `C-x C-e', the
89 ;; expression is that which ends immediately before point (so that it
90 ;; is useful for evaluating something just after you have typed it).
91 ;; For `C-M-x', the expression is the "top level defun" around point;
92 ;; this means the balanced chunk of code around point whose opening
93 ;; parenthesis is in column 0.
94
95 ;; Take this code fragment as an example:
96
97 (let ((x 1) (y 2))
98 (let ((z (atan x y)))
99 (display "Arctangent is: ")
100 (display z)
101 (newline)
102 z))
103
104 ;; If you move the cursor to the end of the (display z) line and type
105 ;; `C-x C-e', the code evaluated is just "(display z)", which normally
106 ;; produces an error, because z is not defined in the usual Guile
107 ;; environment. If, however, you type `C-M-x' with the cursor in the
108 ;; same place, the code evaluated is the whole "(let ((x 1) (y 2))
109 ;; ...)" kaboodle, because that is the most recent expression before
110 ;; point that starts in column 0.
111
112 ;; Try these now. The Guile Evaluation window should pop up again,
113 ;; and show you:
114 ;; - the expression that was evaluated (probably abbreviated)
115 ;; - the module that it was evaluated in
116 ;; - anything that the code wrote to its standard output
117 ;; - the return value(s) of the evaluation.
118 ;; Following the convention of the Emacs Lisp and Guile manuals,
119 ;; return values are indicated by the symbol "=>".
120
121 ;; To see what happens when an expression has multiple return values,
122 ;; try evaluating this one:
123
124 (values 'a (begin (display "hello world\n") 'b) 'c)
125
126 ;; You can also evaluate a region of a buffer using `C-c C-r'. If the
127 ;; code in the region consists of multiple expressions, GDS evaluates
128 ;; them sequentially. For example, try selecting the following three
129 ;; lines and typing `C-c C-r'.
130
131 (display "Arctangent is: ")
132 (display z)
133 (newline)
134
135 ;; If the code in the region evaluated isn't syntactically balanced,
136 ;; GDS will indicate a read error, for example for this code:
137
138 (let ((z (atan x y)))
139 (display "Arctangent is: ")
140 (display z)
141 (newline)
142
143 ;; Finally, if you want to evaluate something quickly that is not in a
144 ;; buffer, you can use `C-c C-e' and type the code to evaluate at the
145 ;; minibuffer prompt. The results are popped up in the same way as
146 ;; for code from a buffer.
147
148 ;; ** Breakpoints
149
150 ;; Before evaluating Scheme code from an Emacs buffer, you may want to
151 ;; set some breakpoints in it. With GDS you can set breakpoints in
152 ;; Scheme code by typing `C-x SPC'.
153 ;;
154 ;; To see how this works, select the second line of the following code
155 ;; (the `(format ...)' line) and type `C-x SPC'.
156
157 (for-each (lambda (x)
158 (format #t "~A cubed is ~A\n" x (* x x x)))
159 (iota 6))
160
161 ;; The two opening parentheses in that line should now be highlighted
162 ;; in red, to show that breakpoints have been set at the start of the
163 ;; `(format ...)' and `(* x x x)' expressions. Then evaluate the
164 ;; whole for-each expression by typing `C-M-x' ...
165 ;;
166 ;; In the upper half of your Emacs, a buffer appears showing you the
167 ;; Scheme stack.
168 ;;
169 ;; In the lower half, the `(format ...)' expression is highlighted.
170 ;;
171 ;; What has happened is that Guile started evaluating the for-each
172 ;; code, but then hit the breakpoint that you set on the start of the
173 ;; format expression. Guile therefore pauses the evaluation at that
174 ;; point and passes the stack (which encapsulates everything that is
175 ;; interesting about the state of Guile at that point) to GDS. You
176 ;; can then explore the stack and decide how to tell Guile to
177 ;; continue.
178 ;;
179 ;; - If you move your mouse over any of the identifiers in the
180 ;; highlighted code, a help echo (or tooltip) will appear to tell
181 ;; you that identifier's current value. (Note though that this only
182 ;; works when the stack buffer is selected. So if you have switched
183 ;; to this buffer in order to scroll down and read these lines, you
184 ;; will need to switch back to the stack buffer before trying this
185 ;; out.)
186 ;;
187 ;; - In the stack buffer, the "=>" on the left shows you that the top
188 ;; frame is currently selected. You can move up and down the stack
189 ;; by pressing the up and down arrows (or `u' and `d'). As you do
190 ;; this, GDS will change the highlight in the lower window to show
191 ;; the code that corresponds to the selected stack frame.
192 ;;
193 ;; - You can evaluate an arbitrary expression in the local environment
194 ;; of the selected stack frame by typing `e' followed by the
195 ;; expression.
196 ;;
197 ;; - You can show various bits of information about the selected frame
198 ;; by typing `I', `A' and `S'. Feel free to try these now, to see
199 ;; what they do.
200 ;;
201 ;; You also have control over the continuing evaluation of this code.
202 ;; Here are some of the things you can do - please try them as you
203 ;; read.
204 ;;
205 ;; - `g' tells Guile to continue execution normally. In this case
206 ;; that means that evaluation will continue until it hits the next
207 ;; breakpoint, which is on the `(* x x x)' expression.
208 ;;
209 ;; - `SPC' tells Guile to continue until the next significant event in
210 ;; the same source file as the selected frame. A "significant
211 ;; event" means either beginning to evaluate an expression in the
212 ;; relevant file, or completing such an evaluation, in which case
213 ;; GDS tells you the value that it is returning. Pressing `SPC'
214 ;; repeatedly is a nice way to step through all the details of the
215 ;; code in a given file, but stepping over calls that involve code
216 ;; from other files.
217 ;;
218 ;; - `o' tells Guile to continue execution until the selected stack
219 ;; frame completes, and then to show its return value.
220
221 ;; Local Variables:
222 ;; mode: scheme
223 ;; End: