Improve documentation of Edebug and macros
[emacs.git] / doc / lispref / edebug.texi
blob62fd9f38cb63a97e35e2442a3b07313f2634763f
1 @comment -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1992-1994, 1998-1999, 2001-2017 Free Software
4 @c Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
7 @c This file can also be used by an independent Edebug User
8 @c Manual in which case the Edebug node below should be used
9 @c with the following links to the Bugs section and to the top level:
11 @c , Bugs and Todo List, Top, Top
13 @node Edebug
14 @section Edebug
15 @cindex Edebug debugging facility
17   Edebug is a source-level debugger for Emacs Lisp programs, with which
18 you can:
20 @itemize @bullet
21 @item
22 Step through evaluation, stopping before and after each expression.
24 @item
25 Set conditional or unconditional breakpoints.
27 @item
28 Stop when a specified condition is true (the global break event).
30 @item
31 Trace slow or fast, stopping briefly at each stop point, or
32 at each breakpoint.
34 @item
35 Display expression results and evaluate expressions as if outside of
36 Edebug.
38 @item
39 Automatically re-evaluate a list of expressions and
40 display their results each time Edebug updates the display.
42 @item
43 Output trace information on function calls and returns.
45 @item
46 Stop when an error occurs.
48 @item
49 Display a backtrace, omitting Edebug's own frames.
51 @item
52 Specify argument evaluation for macros and defining forms.
54 @item
55 Obtain rudimentary coverage testing and frequency counts.
56 @end itemize
58 The first three sections below should tell you enough about Edebug to
59 start using it.
61 @menu
62 * Using Edebug::                Introduction to use of Edebug.
63 * Instrumenting::               You must instrument your code
64                                   in order to debug it with Edebug.
65 * Modes: Edebug Execution Modes. Execution modes, stopping more or less often.
66 * Jumping::                     Commands to jump to a specified place.
67 * Misc: Edebug Misc.            Miscellaneous commands.
68 * Breaks::                      Setting breakpoints to make the program stop.
69 * Trapping Errors::             Trapping errors with Edebug.
70 * Views: Edebug Views.          Views inside and outside of Edebug.
71 * Eval: Edebug Eval.            Evaluating expressions within Edebug.
72 * Eval List::                   Expressions whose values are displayed
73                                   each time you enter Edebug.
74 * Printing in Edebug::          Customization of printing.
75 * Trace Buffer::                How to produce trace output in a buffer.
76 * Coverage Testing::            How to test evaluation coverage.
77 * The Outside Context::         Data that Edebug saves and restores.
78 * Edebug and Macros::           Specifying how to handle macro calls.
79 * Options: Edebug Options.      Option variables for customizing Edebug.
80 @end menu
82 @node Using Edebug
83 @subsection Using Edebug
85   To debug a Lisp program with Edebug, you must first @dfn{instrument}
86 the Lisp code that you want to debug.  A simple way to do this is to
87 first move point into the definition of a function or macro and then do
88 @kbd{C-u C-M-x} (@code{eval-defun} with a prefix argument).  See
89 @ref{Instrumenting}, for alternative ways to instrument code.
91   Once a function is instrumented, any call to the function activates
92 Edebug.  Depending on which Edebug execution mode you have selected,
93 activating Edebug may stop execution and let you step through the
94 function, or it may update the display and continue execution while
95 checking for debugging commands.  The default execution mode is step,
96 which stops execution.  @xref{Edebug Execution Modes}.
98   Within Edebug, you normally view an Emacs buffer showing the source of
99 the Lisp code you are debugging.  This is referred to as the @dfn{source
100 code buffer}, and it is temporarily read-only.
102   An arrow in the left fringe indicates the line where the function is
103 executing.  Point initially shows where within the line the function is
104 executing, but this ceases to be true if you move point yourself.
106   If you instrument the definition of @code{fac} (shown below) and then
107 execute @code{(fac 3)}, here is what you would normally see.  Point is
108 at the open-parenthesis before @code{if}.
110 @example
111 (defun fac (n)
112 =>@point{}(if (< 0 n)
113       (* n (fac (1- n)))
114     1))
115 @end example
117 @cindex stop points
118 The places within a function where Edebug can stop execution are called
119 @dfn{stop points}.  These occur both before and after each subexpression
120 that is a list, and also after each variable reference.
121 Here we use periods to show the stop points in the function
122 @code{fac}:
124 @example
125 (defun fac (n)
126   .(if .(< 0 n.).
127       .(* n. .(fac .(1- n.).).).
128     1).)
129 @end example
131 The special commands of Edebug are available in the source code buffer
132 in addition to the commands of Emacs Lisp mode.  For example, you can
133 type the Edebug command @key{SPC} to execute until the next stop point.
134 If you type @key{SPC} once after entry to @code{fac}, here is the
135 display you will see:
137 @example
138 (defun fac (n)
139 =>(if @point{}(< 0 n)
140       (* n (fac (1- n)))
141     1))
142 @end example
144 When Edebug stops execution after an expression, it displays the
145 expression's value in the echo area.
147 Other frequently used commands are @kbd{b} to set a breakpoint at a stop
148 point, @kbd{g} to execute until a breakpoint is reached, and @kbd{q} to
149 exit Edebug and return to the top-level command loop.  Type @kbd{?} to
150 display a list of all Edebug commands.
152 @node Instrumenting
153 @subsection Instrumenting for Edebug
154 @cindex instrumenting for Edebug
156   In order to use Edebug to debug Lisp code, you must first
157 @dfn{instrument} the code.  Instrumenting code inserts additional code
158 into it, to invoke Edebug at the proper places.
160 @kindex C-M-x
161 @findex eval-defun (Edebug)
162   When you invoke command @kbd{C-M-x} (@code{eval-defun}) with a
163 prefix argument on a function definition, it instruments the
164 definition before evaluating it.  (This does not modify the source
165 code itself.)  If the variable @code{edebug-all-defs} is
166 non-@code{nil}, that inverts the meaning of the prefix argument: in
167 this case, @kbd{C-M-x} instruments the definition @emph{unless} it has
168 a prefix argument.  The default value of @code{edebug-all-defs} is
169 @code{nil}.  The command @kbd{M-x edebug-all-defs} toggles the value
170 of the variable @code{edebug-all-defs}.
172 @findex eval-region @r{(Edebug)}
173 @findex eval-buffer @r{(Edebug)}
174 @findex eval-current-buffer @r{(Edebug)}
175   If @code{edebug-all-defs} is non-@code{nil}, then the commands
176 @code{eval-region}, @code{eval-current-buffer}, and @code{eval-buffer}
177 also instrument any definitions they evaluate.  Similarly,
178 @code{edebug-all-forms} controls whether @code{eval-region} should
179 instrument @emph{any} form, even non-defining forms.  This doesn't apply
180 to loading or evaluations in the minibuffer.  The command @kbd{M-x
181 edebug-all-forms} toggles this option.
183 @findex edebug-eval-top-level-form
184 @findex edebug-defun
185   Another command, @kbd{M-x edebug-eval-top-level-form}, is available to
186 instrument any top-level form regardless of the values of
187 @code{edebug-all-defs} and @code{edebug-all-forms}.
188 @code{edebug-defun} is an alias for @code{edebug-eval-top-level-form}.
190   While Edebug is active, the command @kbd{I}
191 (@code{edebug-instrument-callee}) instruments the definition of the
192 function or macro called by the list form after point, if it is not already
193 instrumented.  This is possible only if Edebug knows where to find the
194 source for that function; for this reason, after loading Edebug,
195 @code{eval-region} records the position of every definition it
196 evaluates, even if not instrumenting it.  See also the @kbd{i} command
197 (@pxref{Jumping}), which steps into the call after instrumenting the
198 function.
200   Edebug knows how to instrument all the standard special forms,
201 @code{interactive} forms with an expression argument, anonymous lambda
202 expressions, and other defining forms.  However, Edebug cannot determine
203 on its own what a user-defined macro will do with the arguments of a
204 macro call, so you must provide that information using Edebug
205 specifications; for details, @pxref{Edebug and Macros}.
207   When Edebug is about to instrument code for the first time in a
208 session, it runs the hook @code{edebug-setup-hook}, then sets it to
209 @code{nil}.  You can use this to load Edebug specifications
210 associated with a package you are using, but only when you use Edebug.
212 @findex eval-expression @r{(Edebug)}
213   To remove instrumentation from a definition, simply re-evaluate its
214 definition in a way that does not instrument.  There are two ways of
215 evaluating forms that never instrument them: from a file with
216 @code{load}, and from the minibuffer with @code{eval-expression}
217 (@kbd{M-:}).
219   If Edebug detects a syntax error while instrumenting, it leaves point
220 at the erroneous code and signals an @code{invalid-read-syntax} error.
221 @c FIXME?  I can't see that it "leaves point at the erroneous code".
223   @xref{Edebug Eval}, for other evaluation functions available
224 inside of Edebug.
226 @node Edebug Execution Modes
227 @subsection Edebug Execution Modes
229 @cindex Edebug execution modes
230 Edebug supports several execution modes for running the program you are
231 debugging.  We call these alternatives @dfn{Edebug execution modes}; do
232 not confuse them with major or minor modes.  The current Edebug execution mode
233 determines how far Edebug continues execution before stopping---whether
234 it stops at each stop point, or continues to the next breakpoint, for
235 example---and how much Edebug displays the progress of the evaluation
236 before it stops.
238 Normally, you specify the Edebug execution mode by typing a command to
239 continue the program in a certain mode.  Here is a table of these
240 commands; all except for @kbd{S} resume execution of the program, at
241 least for a certain distance.
243 @table @kbd
244 @item S
245 Stop: don't execute any more of the program, but wait for more
246 Edebug commands (@code{edebug-stop}).
247 @c FIXME Does not work. https://debbugs.gnu.org/9764
249 @item @key{SPC}
250 Step: stop at the next stop point encountered (@code{edebug-step-mode}).
252 @item n
253 Next: stop at the next stop point encountered after an expression
254 (@code{edebug-next-mode}).  Also see @code{edebug-forward-sexp} in
255 @ref{Jumping}.
257 @item t
258 Trace: pause (normally one second) at each Edebug stop point
259 (@code{edebug-trace-mode}).
261 @item T
262 Rapid trace: update the display at each stop point, but don't actually
263 pause (@code{edebug-Trace-fast-mode}).
265 @item g
266 Go: run until the next breakpoint (@code{edebug-go-mode}).  @xref{Breakpoints}.
268 @item c
269 Continue: pause one second at each breakpoint, and then continue
270 (@code{edebug-continue-mode}).
272 @item C
273 Rapid continue: move point to each breakpoint, but don't pause
274 (@code{edebug-Continue-fast-mode}).
276 @item G
277 Go non-stop: ignore breakpoints (@code{edebug-Go-nonstop-mode}).  You
278 can still stop the program by typing @kbd{S}, or any editing command.
279 @end table
281 In general, the execution modes earlier in the above list run the
282 program more slowly or stop sooner than the modes later in the list.
284 When you enter a new Edebug level, Edebug will normally stop at the
285 first instrumented function it encounters.  If you prefer to stop only
286 at a break point, or not at all (for example, when gathering coverage
287 data), change the value of @code{edebug-initial-mode} from its default
288 @code{step} to @code{go}, or @code{Go-nonstop}, or one of its other
289 values (@pxref{Edebug Options}).  You can do this readily with
290 @kbd{C-x C-a C-m} (@code{edebug-set-initial-mode}):
292 @deffn Command edebug-set-initial-mode
293 @kindex C-x C-a C-m
294 This command, bound to @kbd{C-x C-a C-m}, sets
295 @code{edebug-initial-mode}.  It prompts you for a key to indicate the
296 mode.  You should enter one of the eight keys listed above, which sets
297 the corresponding mode.
298 @end deffn
300 Note that you may reenter the same Edebug level several times if, for
301 example, an instrumented function is called several times from one
302 command.
304 While executing or tracing, you can interrupt the execution by typing
305 any Edebug command.  Edebug stops the program at the next stop point and
306 then executes the command you typed.  For example, typing @kbd{t} during
307 execution switches to trace mode at the next stop point.  You can use
308 @kbd{S} to stop execution without doing anything else.
310 If your function happens to read input, a character you type intending
311 to interrupt execution may be read by the function instead.  You can
312 avoid such unintended results by paying attention to when your program
313 wants input.
315 @cindex keyboard macros (Edebug)
316 Keyboard macros containing the commands in this section do not
317 completely work: exiting from Edebug, to resume the program, loses track
318 of the keyboard macro.  This is not easy to fix.  Also, defining or
319 executing a keyboard macro outside of Edebug does not affect commands
320 inside Edebug.  This is usually an advantage.  See also the
321 @code{edebug-continue-kbd-macro} option in @ref{Edebug Options}.
323 @defopt edebug-sit-for-seconds
324 This option specifies how many seconds to wait between execution steps
325 in trace mode or continue mode.  The default is 1 second.
326 @end defopt
328 @node Jumping
329 @subsection Jumping
331   The commands described in this section execute until they reach a
332 specified location.  All except @kbd{i} make a temporary breakpoint to
333 establish the place to stop, then switch to go mode.  Any other
334 breakpoint reached before the intended stop point will also stop
335 execution.  @xref{Breakpoints}, for the details on breakpoints.
337   These commands may fail to work as expected in case of nonlocal exit,
338 as that can bypass the temporary breakpoint where you expected the
339 program to stop.
341 @table @kbd
342 @item h
343 Proceed to the stop point near where point is (@code{edebug-goto-here}).
345 @item f
346 Run the program for one expression
347 (@code{edebug-forward-sexp}).
349 @item o
350 Run the program until the end of the containing sexp (@code{edebug-step-out}).
352 @item i
353 Step into the function or macro called by the form after point
354 (@code{edebug-step-in}).
355 @end table
357 The @kbd{h} command proceeds to the stop point at or after the current
358 location of point, using a temporary breakpoint.
360 The @kbd{f} command runs the program forward over one expression.  More
361 precisely, it sets a temporary breakpoint at the position that
362 @code{forward-sexp} would reach, then executes in go mode so that
363 the program will stop at breakpoints.
365 With a prefix argument @var{n}, the temporary breakpoint is placed
366 @var{n} sexps beyond point.  If the containing list ends before @var{n}
367 more elements, then the place to stop is after the containing
368 expression.
370 You must check that the position @code{forward-sexp} finds is a place
371 that the program will really get to.  In @code{cond}, for example,
372 this may not be true.
374 For flexibility, the @kbd{f} command does @code{forward-sexp} starting
375 at point, rather than at the stop point.  If you want to execute one
376 expression @emph{from the current stop point}, first type @kbd{w}
377 (@code{edebug-where}) to move point there, and then type @kbd{f}.
379 The @kbd{o} command continues out of an expression.  It places a
380 temporary breakpoint at the end of the sexp containing point.  If the
381 containing sexp is a function definition itself, @kbd{o} continues until
382 just before the last sexp in the definition.  If that is where you are
383 now, it returns from the function and then stops.  In other words, this
384 command does not exit the currently executing function unless you are
385 positioned after the last sexp.
387 Normally, the @kbd{h}, @kbd{f}, and @kbd{o} commands display ``Break''
388 and pause for @code{edebug-sit-for-seconds} before showing the result
389 of the form just evaluated.  You can avoid this pause by setting
390 @code{edebug-sit-on-break} to @code{nil}.  @xref{Edebug Options}.
392 The @kbd{i} command steps into the function or macro called by the list
393 form after point, and stops at its first stop point.  Note that the form
394 need not be the one about to be evaluated.  But if the form is a
395 function call about to be evaluated, remember to use this command before
396 any of the arguments are evaluated, since otherwise it will be too late.
398 The @kbd{i} command instruments the function or macro it's supposed to
399 step into, if it isn't instrumented already.  This is convenient, but keep
400 in mind that the function or macro remains instrumented unless you explicitly
401 arrange to deinstrument it.
403 @node Edebug Misc
404 @subsection Miscellaneous Edebug Commands
406   Some miscellaneous Edebug commands are described here.
408 @table @kbd
409 @item ?
410 Display the help message for Edebug (@code{edebug-help}).
412 @item C-]
413 Abort one level back to the previous command level
414 (@code{abort-recursive-edit}).
416 @item q
417 Return to the top level editor command loop (@code{top-level}).  This
418 exits all recursive editing levels, including all levels of Edebug
419 activity.  However, instrumented code protected with
420 @code{unwind-protect} or @code{condition-case} forms may resume
421 debugging.
423 @item Q
424 Like @kbd{q}, but don't stop even for protected code
425 (@code{edebug-top-level-nonstop}).
427 @item r
428 Redisplay the most recently known expression result in the echo area
429 (@code{edebug-previous-result}).
431 @item d
432 Display a backtrace, excluding Edebug's own functions for clarity
433 (@code{edebug-backtrace}).
435 You cannot use debugger commands in the backtrace buffer in Edebug as
436 you would in the standard debugger.
438 The backtrace buffer is killed automatically when you continue
439 execution.
440 @end table
442 You can invoke commands from Edebug that activate Edebug again
443 recursively.  Whenever Edebug is active, you can quit to the top level
444 with @kbd{q} or abort one recursive edit level with @kbd{C-]}.  You can
445 display a backtrace of all the pending evaluations with @kbd{d}.
447 @node Breaks
448 @subsection Breaks
450 Edebug's step mode stops execution when the next stop point is reached.
451 There are three other ways to stop Edebug execution once it has started:
452 breakpoints, the global break condition, and source breakpoints.
454 @menu
455 * Breakpoints::                 Breakpoints at stop points.
456 * Global Break Condition::      Breaking on an event.
457 * Source Breakpoints::          Embedding breakpoints in source code.
458 @end menu
460 @node Breakpoints
461 @subsubsection Edebug Breakpoints
463 @cindex breakpoints (Edebug)
464 While using Edebug, you can specify @dfn{breakpoints} in the program you
465 are testing: these are places where execution should stop.  You can set a
466 breakpoint at any stop point, as defined in @ref{Using Edebug}.  For
467 setting and unsetting breakpoints, the stop point that is affected is
468 the first one at or after point in the source code buffer.  Here are the
469 Edebug commands for breakpoints:
471 @table @kbd
472 @item b
473 Set a breakpoint at the stop point at or after point
474 (@code{edebug-set-breakpoint}).  If you use a prefix argument, the
475 breakpoint is temporary---it turns off the first time it stops the
476 program.
478 @item u
479 Unset the breakpoint (if any) at the stop point at or after
480 point (@code{edebug-unset-breakpoint}).
482 @item x @var{condition} @key{RET}
483 Set a conditional breakpoint which stops the program only if
484 evaluating @var{condition} produces a non-@code{nil} value
485 (@code{edebug-set-conditional-breakpoint}).  With a prefix argument,
486 the breakpoint is temporary.
488 @item B
489 Move point to the next breakpoint in the current definition
490 (@code{edebug-next-breakpoint}).
491 @end table
493 While in Edebug, you can set a breakpoint with @kbd{b} and unset one
494 with @kbd{u}.  First move point to the Edebug stop point of your choice,
495 then type @kbd{b} or @kbd{u} to set or unset a breakpoint there.
496 Unsetting a breakpoint where none has been set has no effect.
498 Re-evaluating or reinstrumenting a definition removes all of its
499 previous breakpoints.
501 A @dfn{conditional breakpoint} tests a condition each time the program
502 gets there.  Any errors that occur as a result of evaluating the
503 condition are ignored, as if the result were @code{nil}.  To set a
504 conditional breakpoint, use @kbd{x}, and specify the condition
505 expression in the minibuffer.  Setting a conditional breakpoint at a
506 stop point that has a previously established conditional breakpoint puts
507 the previous condition expression in the minibuffer so you can edit it.
509 You can make a conditional or unconditional breakpoint
510 @dfn{temporary} by using a prefix argument with the command to set the
511 breakpoint.  When a temporary breakpoint stops the program, it is
512 automatically unset.
514 Edebug always stops or pauses at a breakpoint, except when the Edebug
515 mode is Go-nonstop.  In that mode, it ignores breakpoints entirely.
517 To find out where your breakpoints are, use the @kbd{B} command, which
518 moves point to the next breakpoint following point, within the same
519 function, or to the first breakpoint if there are no following
520 breakpoints.  This command does not continue execution---it just moves
521 point in the buffer.
523 @node Global Break Condition
524 @subsubsection Global Break Condition
526 @cindex stopping on events
527 @cindex global break condition
528   A @dfn{global break condition} stops execution when a specified
529 condition is satisfied, no matter where that may occur.  Edebug
530 evaluates the global break condition at every stop point; if it
531 evaluates to a non-@code{nil} value, then execution stops or pauses
532 depending on the execution mode, as if a breakpoint had been hit.  If
533 evaluating the condition gets an error, execution does not stop.
535 @findex edebug-set-global-break-condition
536   The condition expression is stored in
537 @code{edebug-global-break-condition}.  You can specify a new expression
538 using the @kbd{X} command from the source code buffer while Edebug is
539 active, or using @kbd{C-x X X} from any buffer at any time, as long as
540 Edebug is loaded (@code{edebug-set-global-break-condition}).
542   The global break condition is the simplest way to find where in your
543 code some event occurs, but it makes code run much more slowly.  So you
544 should reset the condition to @code{nil} when not using it.
546 @node Source Breakpoints
547 @subsubsection Source Breakpoints
549 @findex edebug
550 @cindex source breakpoints
551   All breakpoints in a definition are forgotten each time you
552 reinstrument it.  If you wish to make a breakpoint that won't be
553 forgotten, you can write a @dfn{source breakpoint}, which is simply a
554 call to the function @code{edebug} in your source code.  You can, of
555 course, make such a call conditional.  For example, in the @code{fac}
556 function, you can insert the first line as shown below, to stop when the
557 argument reaches zero:
559 @example
560 (defun fac (n)
561   (if (= n 0) (edebug))
562   (if (< 0 n)
563       (* n (fac (1- n)))
564     1))
565 @end example
567   When the @code{fac} definition is instrumented and the function is
568 called, the call to @code{edebug} acts as a breakpoint.  Depending on
569 the execution mode, Edebug stops or pauses there.
571   If no instrumented code is being executed when @code{edebug} is called,
572 that function calls @code{debug}.
573 @c This may not be a good idea anymore.
575 @node Trapping Errors
576 @subsection Trapping Errors
578   Emacs normally displays an error message when an error is signaled and
579 not handled with @code{condition-case}.  While Edebug is active and
580 executing instrumented code, it normally responds to all unhandled
581 errors.  You can customize this with the options @code{edebug-on-error}
582 and @code{edebug-on-quit}; see @ref{Edebug Options}.
584   When Edebug responds to an error, it shows the last stop point
585 encountered before the error.  This may be the location of a call to a
586 function which was not instrumented, and within which the error actually
587 occurred.  For an unbound variable error, the last known stop point
588 might be quite distant from the offending variable reference.  In that
589 case, you might want to display a full backtrace (@pxref{Edebug Misc}).
591 @c Edebug should be changed for the following: -- dan
592   If you change @code{debug-on-error} or @code{debug-on-quit} while
593 Edebug is active, these changes will be forgotten when Edebug becomes
594 inactive.  Furthermore, during Edebug's recursive edit, these variables
595 are bound to the values they had outside of Edebug.
597 @node Edebug Views
598 @subsection Edebug Views
600   These Edebug commands let you view aspects of the buffer and window
601 status as they were before entry to Edebug.  The outside window
602 configuration is the collection of windows and contents that were in
603 effect outside of Edebug.
605 @table @kbd
606 @item v
607 Switch to viewing the outside window configuration
608 (@code{edebug-view-outside}).  Type @kbd{C-x X w} to return to Edebug.
610 @item p
611 Temporarily display the outside current buffer with point at its
612 outside position (@code{edebug-bounce-point}), pausing for one second
613 before returning to Edebug.  With a prefix argument @var{n}, pause for
614 @var{n} seconds instead.
616 @item w
617 Move point back to the current stop point in the source code buffer
618 (@code{edebug-where}).
620 If you use this command in a different window displaying the same
621 buffer, that window will be used instead to display the current
622 definition in the future.
624 @item W
625 @c Its function is not simply to forget the saved configuration -- dan
626 Toggle whether Edebug saves and restores the outside window
627 configuration (@code{edebug-toggle-save-windows}).
629 With a prefix argument, @code{W} only toggles saving and restoring of
630 the selected window.  To specify a window that is not displaying the
631 source code buffer, you must use @kbd{C-x X W} from the global keymap.
632 @end table
634   You can view the outside window configuration with @kbd{v} or just
635 bounce to the point in the current buffer with @kbd{p}, even if
636 it is not normally displayed.
638   After moving point, you may wish to jump back to the stop point.
639 You can do that with @kbd{w} from a source code buffer.  You can jump
640 back to the stop point in the source code buffer from any buffer using
641 @kbd{C-x X w}.
643   Each time you use @kbd{W} to turn saving @emph{off}, Edebug forgets the
644 saved outside window configuration---so that even if you turn saving
645 back @emph{on}, the current window configuration remains unchanged when
646 you next exit Edebug (by continuing the program).  However, the
647 automatic redisplay of @file{*edebug*} and @file{*edebug-trace*} may
648 conflict with the buffers you wish to see unless you have enough windows
649 open.
651 @node Edebug Eval
652 @subsection Evaluation
654   While within Edebug, you can evaluate expressions as if Edebug
655 were not running.  Edebug tries to be invisible to the expression's
656 evaluation and printing.  Evaluation of expressions that cause side
657 effects will work as expected, except for changes to data that Edebug
658 explicitly saves and restores.  @xref{The Outside Context}, for details
659 on this process.
661 @table @kbd
662 @item e @var{exp} @key{RET}
663 Evaluate expression @var{exp} in the context outside of Edebug
664 (@code{edebug-eval-expression}).  That is, Edebug tries to minimize its
665 interference with the evaluation.
667 @item M-: @var{exp} @key{RET}
668 Evaluate expression @var{exp} in the context of Edebug itself
669 (@code{eval-expression}).
671 @item C-x C-e
672 Evaluate the expression before point, in the context outside of Edebug
673 (@code{edebug-eval-last-sexp}).
674 @end table
676 @cindex lexical binding (Edebug)
677   Edebug supports evaluation of expressions containing references to
678 lexically bound symbols created by the following constructs in
679 @file{cl.el}: @code{lexical-let}, @code{macrolet}, and
680 @code{symbol-macrolet}.
681 @c FIXME?  What about lexical-binding = t?
683 @node Eval List
684 @subsection Evaluation List Buffer
686   You can use the @dfn{evaluation list buffer}, called @file{*edebug*}, to
687 evaluate expressions interactively.  You can also set up the
688 @dfn{evaluation list} of expressions to be evaluated automatically each
689 time Edebug updates the display.
691 @table @kbd
692 @item E
693 Switch to the evaluation list buffer @file{*edebug*}
694 (@code{edebug-visit-eval-list}).
695 @end table
697   In the @file{*edebug*} buffer you can use the commands of Lisp
698 Interaction mode (@pxref{Lisp Interaction,,, emacs, The GNU Emacs
699 Manual}) as well as these special commands:
701 @table @kbd
702 @item C-j
703 Evaluate the expression before point, in the outside context, and insert
704 the value in the buffer (@code{edebug-eval-print-last-sexp}).
706 @item C-x C-e
707 Evaluate the expression before point, in the context outside of Edebug
708 (@code{edebug-eval-last-sexp}).
710 @item C-c C-u
711 Build a new evaluation list from the contents of the buffer
712 (@code{edebug-update-eval-list}).
714 @item C-c C-d
715 Delete the evaluation list group that point is in
716 (@code{edebug-delete-eval-item}).
718 @item C-c C-w
719 Switch back to the source code buffer at the current stop point
720 (@code{edebug-where}).
721 @end table
723   You can evaluate expressions in the evaluation list window with
724 @kbd{C-j} or @kbd{C-x C-e}, just as you would in @file{*scratch*};
725 but they are evaluated in the context outside of Edebug.
727   The expressions you enter interactively (and their results) are lost
728 when you continue execution; but you can set up an @dfn{evaluation list}
729 consisting of expressions to be evaluated each time execution stops.
731 @cindex evaluation list group
732   To do this, write one or more @dfn{evaluation list groups} in the
733 evaluation list buffer.  An evaluation list group consists of one or
734 more Lisp expressions.  Groups are separated by comment lines.
736   The command @kbd{C-c C-u} (@code{edebug-update-eval-list}) rebuilds the
737 evaluation list, scanning the buffer and using the first expression of
738 each group.  (The idea is that the second expression of the group is the
739 value previously computed and displayed.)
741   Each entry to Edebug redisplays the evaluation list by inserting each
742 expression in the buffer, followed by its current value.  It also
743 inserts comment lines so that each expression becomes its own group.
744 Thus, if you type @kbd{C-c C-u} again without changing the buffer text,
745 the evaluation list is effectively unchanged.
747   If an error occurs during an evaluation from the evaluation list,
748 the error message is displayed in a string as if it were the result.
749 Therefore, expressions using variables that are not currently valid do
750 not interrupt your debugging.
752   Here is an example of what the evaluation list window looks like after
753 several expressions have been added to it:
755 @smallexample
756 (current-buffer)
757 #<buffer *scratch*>
758 ;---------------------------------------------------------------
759 (selected-window)
760 #<window 16 on *scratch*>
761 ;---------------------------------------------------------------
762 (point)
764 ;---------------------------------------------------------------
765 bad-var
766 "Symbol's value as variable is void: bad-var"
767 ;---------------------------------------------------------------
768 (recursion-depth)
770 ;---------------------------------------------------------------
771 this-command
772 eval-last-sexp
773 ;---------------------------------------------------------------
774 @end smallexample
776 To delete a group, move point into it and type @kbd{C-c C-d}, or simply
777 delete the text for the group and update the evaluation list with
778 @kbd{C-c C-u}.  To add a new expression to the evaluation list, insert
779 the expression at a suitable place, insert a new comment line, then type
780 @kbd{C-c C-u}.  You need not insert dashes in the comment line---its
781 contents don't matter.
783 After selecting @file{*edebug*}, you can return to the source code
784 buffer with @kbd{C-c C-w}.  The @file{*edebug*} buffer is killed when
785 you continue execution, and recreated next time it is needed.
787 @node Printing in Edebug
788 @subsection Printing in Edebug
790 @cindex printing (Edebug)
791 @cindex printing circular structures
792 @pindex cust-print
793   If an expression in your program produces a value containing circular
794 list structure, you may get an error when Edebug attempts to print it.
796   One way to cope with circular structure is to set @code{print-length}
797 or @code{print-level} to truncate the printing.  Edebug does this for
798 you; it binds @code{print-length} and @code{print-level} to the values
799 of the variables @code{edebug-print-length} and
800 @code{edebug-print-level} (so long as they have non-@code{nil}
801 values).  @xref{Output Variables}.
803 @defopt edebug-print-length
804 If non-@code{nil}, Edebug binds @code{print-length} to this value while
805 printing results.  The default value is @code{50}.
806 @end defopt
808 @defopt edebug-print-level
809 If non-@code{nil}, Edebug binds @code{print-level} to this value while
810 printing results.  The default value is @code{50}.
811 @end defopt
813   You can also print circular structures and structures that share
814 elements more informatively by binding @code{print-circle}
815 to a non-@code{nil} value.
817   Here is an example of code that creates a circular structure:
819 @example
820 (setq a '(x y))
821 (setcar a a)
822 @end example
824 @noindent
825 Custom printing prints this as @samp{Result: #1=(#1# y)}.  The
826 @samp{#1=} notation labels the structure that follows it with the label
827 @samp{1}, and the @samp{#1#} notation references the previously labeled
828 structure.  This notation is used for any shared elements of lists or
829 vectors.
831 @defopt edebug-print-circle
832 If non-@code{nil}, Edebug binds @code{print-circle} to this value while
833 printing results.  The default value is @code{t}.
834 @end defopt
836   Other programs can also use custom printing; see @file{cust-print.el}
837 for details.
839 @node Trace Buffer
840 @subsection Trace Buffer
841 @cindex trace buffer
843   Edebug can record an execution trace, storing it in a buffer named
844 @file{*edebug-trace*}.  This is a log of function calls and returns,
845 showing the function names and their arguments and values.  To enable
846 trace recording, set @code{edebug-trace} to a non-@code{nil} value.
848   Making a trace buffer is not the same thing as using trace execution
849 mode (@pxref{Edebug Execution Modes}).
851   When trace recording is enabled, each function entry and exit adds
852 lines to the trace buffer.  A function entry record consists of
853 @samp{::::@{}, followed by the function name and argument values.  A
854 function exit record consists of @samp{::::@}}, followed by the function
855 name and result of the function.
857   The number of @samp{:}s in an entry shows its recursion depth.  You
858 can use the braces in the trace buffer to find the matching beginning or
859 end of function calls.
861 @findex edebug-print-trace-before
862 @findex edebug-print-trace-after
863   You can customize trace recording for function entry and exit by
864 redefining the functions @code{edebug-print-trace-before} and
865 @code{edebug-print-trace-after}.
867 @defmac edebug-tracing string body@dots{}
868 This macro requests additional trace information around the execution
869 of the @var{body} forms.  The argument @var{string} specifies text
870 to put in the trace buffer, after the @samp{@{} or @samp{@}}.  All
871 the arguments are evaluated, and @code{edebug-tracing} returns the
872 value of the last form in @var{body}.
873 @end defmac
875 @defun edebug-trace format-string &rest format-args
876 This function inserts text in the trace buffer.  It computes the text
877 with @code{(apply 'format @var{format-string} @var{format-args})}.
878 It also appends a newline to separate entries.
879 @end defun
881   @code{edebug-tracing} and @code{edebug-trace} insert lines in the
882 trace buffer whenever they are called, even if Edebug is not active.
883 Adding text to the trace buffer also scrolls its window to show the last
884 lines inserted.
886 @node Coverage Testing
887 @subsection Coverage Testing
889 @cindex coverage testing (Edebug)
890 @cindex frequency counts
891 @cindex performance analysis
892   Edebug provides rudimentary coverage testing and display of execution
893 frequency.
895   Coverage testing works by comparing the result of each expression with
896 the previous result; each form in the program is considered covered
897 if it has returned two different values since you began testing coverage
898 in the current Emacs session.  Thus, to do coverage testing on your
899 program, execute it under various conditions and note whether it behaves
900 correctly; Edebug will tell you when you have tried enough different
901 conditions that each form has returned two different values.
903   Coverage testing makes execution slower, so it is only done if
904 @code{edebug-test-coverage} is non-@code{nil}.  Frequency counting is
905 performed for all executions of an instrumented function, even if the
906 execution mode is Go-nonstop, and regardless of whether coverage testing
907 is enabled.
909 @kindex C-x X =
910 @findex edebug-temp-display-freq-count
911   Use @kbd{C-x X =} (@code{edebug-display-freq-count}) to display both
912 the coverage information and the frequency counts for a definition.
913 Just @kbd{=} (@code{edebug-temp-display-freq-count}) displays the same
914 information temporarily, only until you type another key.
916 @deffn Command edebug-display-freq-count
917 This command displays the frequency count data for each line of the
918 current definition.
920 It inserts frequency counts as comment lines after each line of code.
921 You can undo all insertions with one @code{undo} command.  The counts
922 appear under the @samp{(} before an expression or the @samp{)} after
923 an expression, or on the last character of a variable.  To simplify
924 the display, a count is not shown if it is equal to the count of an
925 earlier expression on the same line.
927 The character @samp{=} following the count for an expression says that
928 the expression has returned the same value each time it was evaluated.
929 In other words, it is not yet covered for coverage testing purposes.
931 To clear the frequency count and coverage data for a definition,
932 simply reinstrument it with @code{eval-defun}.
933 @end deffn
935 For example, after evaluating @code{(fac 5)} with a source
936 breakpoint, and setting @code{edebug-test-coverage} to @code{t}, when
937 the breakpoint is reached, the frequency data looks like this:
939 @example
940 (defun fac (n)
941   (if (= n 0) (edebug))
942 ;#6           1      = =5
943   (if (< 0 n)
944 ;#5         =
945       (* n (fac (1- n)))
946 ;#    5               0
947     1))
948 ;#   0
949 @end example
951 The comment lines show that @code{fac} was called 6 times.  The
952 first @code{if} statement returned 5 times with the same result each
953 time; the same is true of the condition on the second @code{if}.
954 The recursive call of @code{fac} did not return at all.
957 @node The Outside Context
958 @subsection The Outside Context
960 Edebug tries to be transparent to the program you are debugging, but it
961 does not succeed completely.  Edebug also tries to be transparent when
962 you evaluate expressions with @kbd{e} or with the evaluation list
963 buffer, by temporarily restoring the outside context.  This section
964 explains precisely what context Edebug restores, and how Edebug fails to
965 be completely transparent.
967 @menu
968 * Checking Whether to Stop::    When Edebug decides what to do.
969 * Edebug Display Update::       When Edebug updates the display.
970 * Edebug Recursive Edit::       When Edebug stops execution.
971 @end menu
973 @node Checking Whether to Stop
974 @subsubsection Checking Whether to Stop
976 Whenever Edebug is entered, it needs to save and restore certain data
977 before even deciding whether to make trace information or stop the
978 program.
980 @itemize @bullet
981 @item
982 @vindex edebug-max-depth
983 @code{max-lisp-eval-depth} (@pxref{Eval}) and @code{max-specpdl-size}
984 (@pxref{Local Variables}) are both increased to reduce Edebug's impact
985 on the stack.  You could, however, still run out of stack space when
986 using Edebug.  You can also enlarge the value of
987 @code{edebug-max-depth} if Edebug reaches the limit of recursion depth
988 instrumenting code that contains very large quoted lists.
990 @item
991 The state of keyboard macro execution is saved and restored.  While
992 Edebug is active, @code{executing-kbd-macro} is bound to @code{nil}
993 unless @code{edebug-continue-kbd-macro} is non-@code{nil}.
994 @end itemize
997 @node Edebug Display Update
998 @subsubsection Edebug Display Update
1000 @c This paragraph is not filled, because LaLiberte's conversion script
1001 @c needs an xref to be on just one line.
1002 When Edebug needs to display something (e.g., in trace mode), it saves
1003 the current window configuration from outside Edebug
1004 (@pxref{Window Configurations}).  When you exit Edebug, it restores
1005 the previous window configuration.
1007 Emacs redisplays only when it pauses.  Usually, when you continue
1008 execution, the program re-enters Edebug at a breakpoint or after
1009 stepping, without pausing or reading input in between.  In such cases,
1010 Emacs never gets a chance to redisplay the outside configuration.
1011 Consequently, what you see is the same window configuration as the last
1012 time Edebug was active, with no interruption.
1014 Entry to Edebug for displaying something also saves and restores the
1015 following data (though some of them are deliberately not restored if an
1016 error or quit signal occurs).
1018 @itemize @bullet
1019 @item
1020 @cindex current buffer point and mark (Edebug)
1021 Which buffer is current, and the positions of point and the mark in the
1022 current buffer, are saved and restored.
1024 @item
1025 @cindex window configuration (Edebug)
1026 The outside window configuration is saved and restored if
1027 @code{edebug-save-windows} is non-@code{nil} (@pxref{Edebug Options}).
1029 The window configuration is not restored on error or quit, but the
1030 outside selected window @emph{is} reselected even on error or quit in
1031 case a @code{save-excursion} is active.  If the value of
1032 @code{edebug-save-windows} is a list, only the listed windows are saved
1033 and restored.
1035 The window start and horizontal scrolling of the source code buffer are
1036 not restored, however, so that the display remains coherent within Edebug.
1038 @item
1039 The value of point in each displayed buffer is saved and restored if
1040 @code{edebug-save-displayed-buffer-points} is non-@code{nil}.
1042 @item
1043 The variables @code{overlay-arrow-position} and
1044 @code{overlay-arrow-string} are saved and restored, so you can safely
1045 invoke Edebug from the recursive edit elsewhere in the same buffer.
1047 @item
1048 @code{cursor-in-echo-area} is locally bound to @code{nil} so that
1049 the cursor shows up in the window.
1050 @end itemize
1052 @node Edebug Recursive Edit
1053 @subsubsection Edebug Recursive Edit
1055 When Edebug is entered and actually reads commands from the user, it
1056 saves (and later restores) these additional data:
1058 @itemize @bullet
1059 @item
1060 The current match data.  @xref{Match Data}.
1062 @item
1063 The variables @code{last-command}, @code{this-command},
1064 @code{last-command-event}, @code{last-input-event},
1065 @code{last-event-frame}, @code{last-nonmenu-event}, and
1066 @code{track-mouse}.  Commands in Edebug do not affect these variables
1067 outside of Edebug.
1069 Executing commands within Edebug can change the key sequence that
1070 would be returned by @code{this-command-keys}, and there is no way to
1071 reset the key sequence from Lisp.
1073 Edebug cannot save and restore the value of
1074 @code{unread-command-events}.  Entering Edebug while this variable has a
1075 nontrivial value can interfere with execution of the program you are
1076 debugging.
1078 @item
1079 Complex commands executed while in Edebug are added to the variable
1080 @code{command-history}.  In rare cases this can alter execution.
1082 @item
1083 Within Edebug, the recursion depth appears one deeper than the recursion
1084 depth outside Edebug.  This is not true of the automatically updated
1085 evaluation list window.
1087 @item
1088 @code{standard-output} and @code{standard-input} are bound to @code{nil}
1089 by the @code{recursive-edit}, but Edebug temporarily restores them during
1090 evaluations.
1092 @item
1093 The state of keyboard macro definition is saved and restored.  While
1094 Edebug is active, @code{defining-kbd-macro} is bound to
1095 @code{edebug-continue-kbd-macro}.
1096 @end itemize
1098 @node Edebug and Macros
1099 @subsection Edebug and Macros
1101 To make Edebug properly instrument expressions that call macros, some
1102 extra care is needed.  This subsection explains the details.
1104 @menu
1105 * Instrumenting Macro Calls::   The basic problem.
1106 * Specification List::          How to specify complex patterns of evaluation.
1107 * Backtracking::                What Edebug does when matching fails.
1108 * Specification Examples::      To help understand specifications.
1109 @end menu
1111 @node Instrumenting Macro Calls
1112 @subsubsection Instrumenting Macro Calls
1114   When Edebug instruments an expression that calls a Lisp macro, it needs
1115 additional information about the macro to do the job properly.  This is
1116 because there is no a-priori way to tell which subexpressions of the
1117 macro call are forms to be evaluated.  (Evaluation may occur explicitly
1118 in the macro body, or when the resulting expansion is evaluated, or any
1119 time later.)
1121   Therefore, you must define an Edebug specification for each macro
1122 that Edebug will encounter, to explain the format of calls to that
1123 macro.  To do this, add a @code{debug} declaration to the macro
1124 definition.  Here is a simple example that shows the specification for
1125 the @code{for} example macro (@pxref{Argument Evaluation}).
1127 @smallexample
1128 (defmacro for (var from init to final do &rest body)
1129   "Execute a simple \"for\" loop.
1130 For example, (for i from 1 to 10 do (print i))."
1131   (declare (debug (symbolp "from" form "to" form "do" &rest form)))
1132   ...)
1133 @end smallexample
1135   The Edebug specification says which parts of a call to the macro are
1136 forms to be evaluated.  For simple macros, the specification
1137 often looks very similar to the formal argument list of the macro
1138 definition, but specifications are much more general than macro
1139 arguments.  @xref{Defining Macros}, for more explanation of
1140 the @code{declare} form.
1142 @c See, e.g., https://debbugs.gnu.org/10577
1143 @c FIXME  Maybe there should be an Edebug option to get it to
1144 @c automatically load the entire source file containing the function
1145 @c being instrumented.  That would avoid this.
1146   Take care to ensure that the specifications are known to Edebug when
1147 you instrument code.  If you are instrumenting a function which uses a
1148 macro defined in another file, you may first need to either evaluate
1149 the @code{require} forms in the file containing your function, or
1150 explicitly load the file containing the macro.  If the definition of a
1151 macro is wrapped by @code{eval-when-compile}, you may need to evaluate
1154   You can also define an edebug specification for a macro separately
1155 from the macro definition with @code{def-edebug-spec}.  Adding
1156 @code{debug} declarations is preferred, and more convenient, for macro
1157 definitions in Lisp, but @code{def-edebug-spec} makes it possible to
1158 define Edebug specifications for special forms implemented in C.
1160 @defmac def-edebug-spec macro specification
1161 Specify which expressions of a call to macro @var{macro} are forms to be
1162 evaluated.  @var{specification} should be the edebug specification.
1163 Neither argument is evaluated.
1165 The @var{macro} argument can actually be any symbol, not just a macro
1166 name.
1167 @end defmac
1169 Here is a table of the possibilities for @var{specification} and how each
1170 directs processing of arguments.
1172 @table @asis
1173 @item @code{t}
1174 All arguments are instrumented for evaluation.
1176 @item @code{0}
1177 None of the arguments is instrumented.
1179 @item a symbol
1180 The symbol must have an Edebug specification, which is used instead.
1181 This indirection is repeated until another kind of specification is
1182 found.  This allows you to inherit the specification from another macro.
1184 @item a list
1185 The elements of the list describe the types of the arguments of a
1186 calling form.  The possible elements of a specification list are
1187 described in the following sections.
1188 @end table
1190 If a macro has no Edebug specification, neither through a @code{debug}
1191 declaration nor through a @code{def-edebug-spec} call, the variable
1192 @code{edebug-eval-macro-args} comes into play.
1194 @defopt edebug-eval-macro-args
1195 This controls the way Edebug treats macro arguments with no explicit
1196 Edebug specification.  If it is @code{nil} (the default), none of the
1197 arguments is instrumented for evaluation.  Otherwise, all arguments
1198 are instrumented.
1199 @end defopt
1201 @node Specification List
1202 @subsubsection Specification List
1204 @cindex Edebug specification list
1205 A @dfn{specification list} is required for an Edebug specification if
1206 some arguments of a macro call are evaluated while others are not.  Some
1207 elements in a specification list match one or more arguments, but others
1208 modify the processing of all following elements.  The latter, called
1209 @dfn{specification keywords}, are symbols beginning with @samp{&} (such
1210 as @code{&optional}).
1212 A specification list may contain sublists, which match arguments that are
1213 themselves lists, or it may contain vectors used for grouping.  Sublists
1214 and groups thus subdivide the specification list into a hierarchy of
1215 levels.  Specification keywords apply only to the remainder of the
1216 sublist or group they are contained in.
1218 When a specification list involves alternatives or repetition, matching
1219 it against an actual macro call may require backtracking.  For more
1220 details, @pxref{Backtracking}.
1222 Edebug specifications provide the power of regular expression matching,
1223 plus some context-free grammar constructs: the matching of sublists with
1224 balanced parentheses, recursive processing of forms, and recursion via
1225 indirect specifications.
1227 Here's a table of the possible elements of a specification list, with
1228 their meanings (see @ref{Specification Examples}, for the referenced
1229 examples):
1231 @table @code
1232 @item sexp
1233 A single unevaluated Lisp object, which is not instrumented.
1234 @c an "expression" is not necessarily intended for evaluation.
1236 @item form
1237 A single evaluated expression, which is instrumented.  If your macro
1238 wraps the expression with @code{lambda} before it is evaluated, use
1239 @code{def-form} instead.  See @code{def-form} below.
1241 @item place
1242 A generalized variable.  @xref{Generalized Variables}.
1244 @item body
1245 Short for @code{&rest form}.  See @code{&rest} below.  If your macro
1246 wraps its body of code with @code{lambda} before it is evaluated, use
1247 @code{def-body} instead.  See @code{def-body} below.
1249 @item function-form
1250 A function form: either a quoted function symbol, a quoted lambda
1251 expression, or a form (that should evaluate to a function symbol or
1252 lambda expression).  This is useful when an argument that's a lambda
1253 expression might be quoted with @code{quote} rather than
1254 @code{function}, since it instruments the body of the lambda expression
1255 either way.
1257 @item lambda-expr
1258 A lambda expression with no quoting.
1260 @item &optional
1261 @c @kindex &optional @r{(Edebug)}
1262 All following elements in the specification list are optional; as soon
1263 as one does not match, Edebug stops matching at this level.
1265 To make just a few elements optional, followed by non-optional elements,
1266 use @code{[&optional @var{specs}@dots{}]}.  To specify that several
1267 elements must all match or none, use @code{&optional
1268 [@var{specs}@dots{}]}.  See the @code{defun} example.
1270 @item &rest
1271 @c @kindex &rest @r{(Edebug)}
1272 All following elements in the specification list are repeated zero or
1273 more times.  In the last repetition, however, it is not a problem if the
1274 expression runs out before matching all of the elements of the
1275 specification list.
1277 To repeat only a few elements, use @code{[&rest @var{specs}@dots{}]}.
1278 To specify several elements that must all match on every repetition, use
1279 @code{&rest [@var{specs}@dots{}]}.
1281 @item &or
1282 @c @kindex &or @r{(Edebug)}
1283 Each of the following elements in the specification list is an
1284 alternative.  One of the alternatives must match, or the @code{&or}
1285 specification fails.
1287 Each list element following @code{&or} is a single alternative.  To
1288 group two or more list elements as a single alternative, enclose them in
1289 @code{[@dots{}]}.
1291 @item &not
1292 @c @kindex &not @r{(Edebug)}
1293 Each of the following elements is matched as alternatives as if by using
1294 @code{&or}, but if any of them match, the specification fails.  If none
1295 of them match, nothing is matched, but the @code{&not} specification
1296 succeeds.
1298 @c FIXME &key?
1300 @item &define
1301 @c @kindex &define @r{(Edebug)}
1303 Indicates that the specification is for a defining form.  Edebug's
1304 definition of a defining form is a form containing one or more code
1305 forms which are saved and executed later, after the execution of the
1306 defining form.
1308 The defining form itself is not instrumented (that is, Edebug does not
1309 stop before and after the defining form), but forms inside it
1310 typically will be instrumented.  The @code{&define} keyword should be
1311 the first element in a list specification.
1313 @item nil
1314 This is successful when there are no more arguments to match at the
1315 current argument list level; otherwise it fails.  See sublist
1316 specifications and the backquote example.
1318 @item gate
1319 @cindex preventing backtracking
1320 No argument is matched but backtracking through the gate is disabled
1321 while matching the remainder of the specifications at this level.  This
1322 is primarily used to generate more specific syntax error messages.  See
1323 @ref{Backtracking}, for more details.  Also see the @code{let} example.
1325 @item @var{other-symbol}
1326 @cindex indirect specifications
1327 Any other symbol in a specification list may be a predicate or an
1328 indirect specification.
1330 If the symbol has an Edebug specification, this @dfn{indirect
1331 specification} should be either a list specification that is used in
1332 place of the symbol, or a function that is called to process the
1333 arguments.  The specification may be defined with @code{def-edebug-spec}
1334 just as for macros.  See the @code{defun} example.
1336 Otherwise, the symbol should be a predicate.  The predicate is called
1337 with the argument, and if the predicate returns @code{nil}, the
1338 specification fails and the argument is not instrumented.
1340 Some suitable predicates include @code{symbolp}, @code{integerp},
1341 @code{stringp}, @code{vectorp}, and @code{atom}.
1343 @item [@var{elements}@dots{}]
1344 @cindex [@dots{}] (Edebug)
1345 A vector of elements groups the elements into a single @dfn{group
1346 specification}.  Its meaning has nothing to do with vectors.
1348 @item "@var{string}"
1349 The argument should be a symbol named @var{string}.  This specification
1350 is equivalent to the quoted symbol, @code{'@var{symbol}}, where the name
1351 of @var{symbol} is the @var{string}, but the string form is preferred.
1353 @item (vector @var{elements}@dots{})
1354 The argument should be a vector whose elements must match the
1355 @var{elements} in the specification.  See the backquote example.
1357 @item (@var{elements}@dots{})
1358 Any other list is a @dfn{sublist specification} and the argument must be
1359 a list whose elements match the specification @var{elements}.
1361 @cindex dotted lists (Edebug)
1362 A sublist specification may be a dotted list and the corresponding list
1363 argument may then be a dotted list.  Alternatively, the last @sc{cdr} of a
1364 dotted list specification may be another sublist specification (via a
1365 grouping or an indirect specification, e.g., @code{(spec .  [(more
1366 specs@dots{})])}) whose elements match the non-dotted list arguments.
1367 This is useful in recursive specifications such as in the backquote
1368 example.  Also see the description of a @code{nil} specification
1369 above for terminating such recursion.
1371 Note that a sublist specification written as @code{(specs .  nil)}
1372 is equivalent to @code{(specs)}, and @code{(specs .
1373 (sublist-elements@dots{}))} is equivalent to @code{(specs
1374 sublist-elements@dots{})}.
1375 @end table
1377 @c Need to document extensions with &symbol and :symbol
1379 Here is a list of additional specifications that may appear only after
1380 @code{&define}.  See the @code{defun} example.
1382 @table @code
1383 @item name
1384 The argument, a symbol, is the name of the defining form.
1386 A defining form is not required to have a name field; and it may have
1387 multiple name fields.
1389 @item :name
1390 This construct does not actually match an argument.  The element
1391 following @code{:name} should be a symbol; it is used as an additional
1392 name component for the definition.  You can use this to add a unique,
1393 static component to the name of the definition.  It may be used more
1394 than once.
1396 @item arg
1397 The argument, a symbol, is the name of an argument of the defining form.
1398 However, lambda-list keywords (symbols starting with @samp{&})
1399 are not allowed.
1401 @item lambda-list
1402 @cindex lambda-list (Edebug)
1403 This matches a lambda list---the argument list of a lambda expression.
1405 @item def-body
1406 The argument is the body of code in a definition.  This is like
1407 @code{body}, described above, but a definition body must be instrumented
1408 with a different Edebug call that looks up information associated with
1409 the definition.  Use @code{def-body} for the highest level list of forms
1410 within the definition.
1412 @item def-form
1413 The argument is a single, highest-level form in a definition.  This is
1414 like @code{def-body}, except it is used to match a single form rather than
1415 a list of forms.  As a special case, @code{def-form} also means that
1416 tracing information is not output when the form is executed.  See the
1417 @code{interactive} example.
1418 @end table
1420 @node Backtracking
1421 @subsubsection Backtracking in Specifications
1423 @cindex backtracking
1424 @cindex syntax error (Edebug)
1425 If a specification fails to match at some point, this does not
1426 necessarily mean a syntax error will be signaled; instead,
1427 @dfn{backtracking} will take place until all alternatives have been
1428 exhausted.  Eventually every element of the argument list must be
1429 matched by some element in the specification, and every required element
1430 in the specification must match some argument.
1432 When a syntax error is detected, it might not be reported until much
1433 later, after higher-level alternatives have been exhausted, and with the
1434 point positioned further from the real error.  But if backtracking is
1435 disabled when an error occurs, it can be reported immediately.  Note
1436 that backtracking is also reenabled automatically in several situations;
1437 when a new alternative is established by @code{&optional},
1438 @code{&rest}, or @code{&or}, or at the start of processing a sublist,
1439 group, or indirect specification.  The effect of enabling or disabling
1440 backtracking is limited to the remainder of the level currently being
1441 processed and lower levels.
1443 Backtracking is disabled while matching any of the
1444 form specifications (that is, @code{form}, @code{body}, @code{def-form}, and
1445 @code{def-body}).  These specifications will match any form so any error
1446 must be in the form itself rather than at a higher level.
1448 Backtracking is also disabled after successfully matching a quoted
1449 symbol or string specification, since this usually indicates a
1450 recognized construct.  But if you have a set of alternative constructs that
1451 all begin with the same symbol, you can usually work around this
1452 constraint by factoring the symbol out of the alternatives, e.g.,
1453 @code{["foo" &or [first case] [second case] ...]}.
1455 Most needs are satisfied by these two ways that backtracking is
1456 automatically disabled, but occasionally it is useful to explicitly
1457 disable backtracking by using the @code{gate} specification.  This is
1458 useful when you know that no higher alternatives could apply.  See the
1459 example of the @code{let} specification.
1461 @node Specification Examples
1462 @subsubsection Specification Examples
1464 It may be easier to understand Edebug specifications by studying
1465 the examples provided here.
1467 A @code{let} special form has a sequence of bindings and a body.  Each
1468 of the bindings is either a symbol or a sublist with a symbol and
1469 optional expression.  In the specification below, notice the @code{gate}
1470 inside of the sublist to prevent backtracking once a sublist is found.
1472 @ignore
1473 @c FIXME?  The actual definition in edebug.el looks like this (and always
1474 @c has AFAICS).  In fact, nothing in edebug.el uses gate.  So maybe
1475 @c this is just an example for illustration?
1476 (def-edebug-spec let
1477   ((&rest
1478     &or (symbolp &optional form) symbolp)
1479    body))
1480 @end ignore
1481 @example
1482 (def-edebug-spec let
1483   ((&rest
1484     &or symbolp (gate symbolp &optional form))
1485    body))
1486 @end example
1488 Edebug uses the following specifications for @code{defun} and the
1489 associated argument list and @code{interactive} specifications.  It is
1490 necessary to handle interactive forms specially since an expression
1491 argument is actually evaluated outside of the function body.  (The
1492 specification for @code{defmacro} is very similar to that for
1493 @code{defun}, but allows for the @code{declare} statement.)
1495 @smallexample
1496 (def-edebug-spec defun
1497   (&define name lambda-list
1498            [&optional stringp]   ; @r{Match the doc string, if present.}
1499            [&optional ("interactive" interactive)]
1500            def-body))
1502 (def-edebug-spec lambda-list
1503   (([&rest arg]
1504     [&optional ["&optional" arg &rest arg]]
1505     &optional ["&rest" arg]
1506     )))
1508 (def-edebug-spec interactive
1509   (&optional &or stringp def-form))    ; @r{Notice: @code{def-form}}
1510 @end smallexample
1512 The specification for backquote below illustrates how to match
1513 dotted lists and use @code{nil} to terminate recursion.  It also
1514 illustrates how components of a vector may be matched.  (The actual
1515 specification defined by Edebug is a little different, and does not
1516 support dotted lists because doing so causes very deep recursion that
1517 could fail.)
1519 @smallexample
1520 (def-edebug-spec \` (backquote-form))   ; @r{Alias just for clarity.}
1522 (def-edebug-spec backquote-form
1523   (&or ([&or "," ",@@"] &or ("quote" backquote-form) form)
1524        (backquote-form . [&or nil backquote-form])
1525        (vector &rest backquote-form)
1526        sexp))
1527 @end smallexample
1530 @node Edebug Options
1531 @subsection Edebug Options
1533   These options affect the behavior of Edebug:
1534 @c Previously defopt'd:
1535 @c edebug-sit-for-seconds, edebug-print-length, edebug-print-level
1536 @c edebug-print-circle, edebug-eval-macro-args
1538 @defopt edebug-setup-hook
1539 Functions to call before Edebug is used.  Each time it is set to a new
1540 value, Edebug will call those functions once and then
1541 reset @code{edebug-setup-hook} to @code{nil}.  You could use this to
1542 load up Edebug specifications associated with a package you are using,
1543 but only when you also use Edebug.
1544 @xref{Instrumenting}.
1545 @end defopt
1547 @defopt edebug-all-defs
1548 If this is non-@code{nil}, normal evaluation of defining forms such as
1549 @code{defun} and @code{defmacro} instruments them for Edebug.  This
1550 applies to @code{eval-defun}, @code{eval-region}, @code{eval-buffer},
1551 and @code{eval-current-buffer}.
1553 Use the command @kbd{M-x edebug-all-defs} to toggle the value of this
1554 option.  @xref{Instrumenting}.
1555 @end defopt
1557 @defopt edebug-all-forms
1558 If this is non-@code{nil}, the commands @code{eval-defun},
1559 @code{eval-region}, @code{eval-buffer}, and @code{eval-current-buffer}
1560 instrument all forms, even those that don't define anything.
1561 This doesn't apply to loading or evaluations in the minibuffer.
1563 Use the command @kbd{M-x edebug-all-forms} to toggle the value of this
1564 option.  @xref{Instrumenting}.
1565 @end defopt
1567 @defopt edebug-eval-macro-args
1568 When this is non-@code{nil}, all macro arguments will be instrumented
1569 in the generated code.  For any macro, an @code{edebug-form-spec}
1570 overrides this option.  So to specify exceptions for macros that have
1571 some arguments evaluated and some not, use @code{def-edebug-spec} to
1572 specify an @code{edebug-form-spec}.
1573 @end defopt
1575 @defopt edebug-save-windows
1576 If this is non-@code{nil}, Edebug saves and restores the window
1577 configuration.  That takes some time, so if your program does not care
1578 what happens to the window configurations, it is better to set this
1579 variable to @code{nil}.
1581 If the value is a list, only the listed windows are saved and
1582 restored.
1584 You can use the @kbd{W} command in Edebug to change this variable
1585 interactively.  @xref{Edebug Display Update}.
1586 @end defopt
1588 @defopt edebug-save-displayed-buffer-points
1589 If this is non-@code{nil}, Edebug saves and restores point in all
1590 displayed buffers.
1592 Saving and restoring point in other buffers is necessary if you are
1593 debugging code that changes the point of a buffer that is displayed in
1594 a non-selected window.  If Edebug or the user then selects the window,
1595 point in that buffer will move to the window's value of point.
1597 Saving and restoring point in all buffers is expensive, since it
1598 requires selecting each window twice, so enable this only if you need
1599 it.  @xref{Edebug Display Update}.
1600 @end defopt
1602 @defopt edebug-initial-mode
1603 If this variable is non-@code{nil}, it specifies the initial execution
1604 mode for Edebug when it is first activated.  Possible values are
1605 @code{step}, @code{next}, @code{go}, @code{Go-nonstop}, @code{trace},
1606 @code{Trace-fast}, @code{continue}, and @code{Continue-fast}.
1608 The default value is @code{step}.  This variable can be set
1609 interactively with @kbd{C-x C-a C-m} (@code{edebug-set-initial-mode}).
1610 @xref{Edebug Execution Modes}.
1611 @end defopt
1613 @defopt edebug-trace
1614 If this is non-@code{nil}, trace each function entry and exit.
1615 Tracing output is displayed in a buffer named @file{*edebug-trace*}, one
1616 function entry or exit per line, indented by the recursion level.
1618 Also see @code{edebug-tracing}, in @ref{Trace Buffer}.
1619 @end defopt
1621 @defopt edebug-test-coverage
1622 If non-@code{nil}, Edebug tests coverage of all expressions debugged.
1623 @xref{Coverage Testing}.
1624 @end defopt
1626 @defopt edebug-continue-kbd-macro
1627 If non-@code{nil}, continue defining or executing any keyboard macro
1628 that is executing outside of Edebug.   Use this with caution since it is not
1629 debugged.
1630 @xref{Edebug Execution Modes}.
1631 @end defopt
1633 @defopt edebug-print-length
1634 If non-@code{nil}, the default value of @code{print-length} for
1635 printing results in Edebug.  @xref{Output Variables}.
1636 @end defopt
1638 @defopt edebug-print-level
1639 If non-@code{nil}, the default value of @code{print-level} for
1640 printing results in Edebug.  @xref{Output Variables}.
1641 @end defopt
1643 @defopt edebug-print-circle
1644 If non-@code{nil}, the default value of @code{print-circle} for
1645 printing results in Edebug.  @xref{Output Variables}.
1646 @end defopt
1648 @defopt edebug-unwrap-results
1649 If non-@code{nil}, Edebug tries to remove any of its own
1650 instrumentation when showing the results of expressions.  This is
1651 relevant when debugging macros where the results of expressions are
1652 themselves instrumented expressions.  As a very artificial example,
1653 suppose that the example function @code{fac} has been instrumented,
1654 and consider a macro of the form:
1656 @c FIXME find a less silly example.
1657 @smallexample
1658 (defmacro test () "Edebug example."
1659   (if (symbol-function 'fac)
1660       @dots{}))
1661 @end smallexample
1663 If you instrument the @code{test} macro and step through it, then by
1664 default the result of the @code{symbol-function} call has numerous
1665 @code{edebug-after} and @code{edebug-before} forms, which can make it
1666 difficult to see the actual result.  If
1667 @code{edebug-unwrap-results} is non-@code{nil}, Edebug tries to remove
1668 these forms from the result.
1669 @end defopt
1671 @defopt edebug-on-error
1672 Edebug binds @code{debug-on-error} to this value, if
1673 @code{debug-on-error} was previously @code{nil}.  @xref{Trapping
1674 Errors}.
1675 @end defopt
1677 @defopt edebug-on-quit
1678 Edebug binds @code{debug-on-quit} to this value, if
1679 @code{debug-on-quit} was previously @code{nil}.  @xref{Trapping
1680 Errors}.
1681 @end defopt
1683   If you change the values of @code{edebug-on-error} or
1684 @code{edebug-on-quit} while Edebug is active, their values won't be used
1685 until the @emph{next} time Edebug is invoked via a new command.
1686 @c Not necessarily a deeper command level.
1687 @c A new command is not precisely true, but that is close enough -- dan
1689 @defopt edebug-global-break-condition
1690 If non-@code{nil}, an expression to test for at every stop point.  If
1691 the result is non-@code{nil}, then break.  Errors are ignored.
1692 @xref{Global Break Condition}.
1693 @end defopt
1695 @defopt edebug-sit-for-seconds
1696 Number of seconds to pause when a breakpoint is reached and the execution
1697 mode is trace or continue.  @xref{Edebug Execution Modes}.
1698 @end defopt
1700 @defopt edebug-sit-on-break
1701 Whether or not to pause for @code{edebug-sit-for-seconds} on reaching
1702 a breakpoint.  Set to @code{nil} to prevent the pause, non-@code{nil}
1703 to allow it.
1704 @end defopt