(M-40132'): Unify GT-53970.
[chise/xemacs-chise.git-] / info / lispref.info-13
1 This is ../info/lispref.info, produced by makeinfo version 4.0b from
2 lispref/lispref.texi.
3
4 INFO-DIR-SECTION XEmacs Editor
5 START-INFO-DIR-ENTRY
6 * Lispref: (lispref).           XEmacs Lisp Reference Manual.
7 END-INFO-DIR-ENTRY
8
9    Edition History:
10
11    GNU Emacs Lisp Reference Manual Second Edition (v2.01), May 1993 GNU
12 Emacs Lisp Reference Manual Further Revised (v2.02), August 1993 Lucid
13 Emacs Lisp Reference Manual (for 19.10) First Edition, March 1994
14 XEmacs Lisp Programmer's Manual (for 19.12) Second Edition, April 1995
15 GNU Emacs Lisp Reference Manual v2.4, June 1995 XEmacs Lisp
16 Programmer's Manual (for 19.13) Third Edition, July 1995 XEmacs Lisp
17 Reference Manual (for 19.14 and 20.0) v3.1, March 1996 XEmacs Lisp
18 Reference Manual (for 19.15 and 20.1, 20.2, 20.3) v3.2, April, May,
19 November 1997 XEmacs Lisp Reference Manual (for 21.0) v3.3, April 1998
20
21    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995 Free Software
22 Foundation, Inc.  Copyright (C) 1994, 1995 Sun Microsystems, Inc.
23 Copyright (C) 1995, 1996 Ben Wing.
24
25    Permission is granted to make and distribute verbatim copies of this
26 manual provided the copyright notice and this permission notice are
27 preserved on all copies.
28
29    Permission is granted to copy and distribute modified versions of
30 this manual under the conditions for verbatim copying, provided that the
31 entire resulting derived work is distributed under the terms of a
32 permission notice identical to this one.
33
34    Permission is granted to copy and distribute translations of this
35 manual into another language, under the above conditions for modified
36 versions, except that this permission notice may be stated in a
37 translation approved by the Foundation.
38
39    Permission is granted to copy and distribute modified versions of
40 this manual under the conditions for verbatim copying, provided also
41 that the section entitled "GNU General Public License" is included
42 exactly as in the original, and provided that the entire resulting
43 derived work is distributed under the terms of a permission notice
44 identical to this one.
45
46    Permission is granted to copy and distribute translations of this
47 manual into another language, under the above conditions for modified
48 versions, except that the section entitled "GNU General Public License"
49 may be included in a translation approved by the Free Software
50 Foundation instead of in the original English.
51
52 \1f
53 File: lispref.info,  Node: Disassembly,  Next: Different Behavior,  Prev: Compiled-Function Objects,  Up: Byte Compilation
54
55 Disassembled Byte-Code
56 ======================
57
58    People do not write byte-code; that job is left to the byte compiler.
59 But we provide a disassembler to satisfy a cat-like curiosity.  The
60 disassembler converts the byte-compiled code into humanly readable form.
61
62    The byte-code interpreter is implemented as a simple stack machine.
63 It pushes values onto a stack of its own, then pops them off to use them
64 in calculations whose results are themselves pushed back on the stack.
65 When a byte-code function returns, it pops a value off the stack and
66 returns it as the value of the function.
67
68    In addition to the stack, byte-code functions can use, bind, and set
69 ordinary Lisp variables, by transferring values between variables and
70 the stack.
71
72  - Command: disassemble object &optional stream
73      This function prints the disassembled code for OBJECT.  If STREAM
74      is supplied, then output goes there.  Otherwise, the disassembled
75      code is printed to the stream `standard-output'.  The argument
76      OBJECT can be a function name or a lambda expression.
77
78      As a special exception, if this function is used interactively, it
79      outputs to a buffer named `*Disassemble*'.
80
81    Here are two examples of using the `disassemble' function.  We have
82 added explanatory comments to help you relate the byte-code to the Lisp
83 source; these do not appear in the output of `disassemble'.
84
85      (defun factorial (integer)
86        "Compute factorial of an integer."
87        (if (= 1 integer) 1
88          (* integer (factorial (1- integer)))))
89           => factorial
90      
91      (factorial 4)
92           => 24
93      
94      (disassemble 'factorial)
95           -| byte-code for factorial:
96       doc: Compute factorial of an integer.
97       args: (integer)
98      
99      0   varref   integer        ; Get value of `integer'
100                                  ;   from the environment
101                                  ;   and push the value
102                                  ;   onto the stack.
103      
104      1   constant 1              ; Push 1 onto stack.
105      
106      2   eqlsign                 ; Pop top two values off stack,
107                                  ;   compare them,
108                                  ;   and push result onto stack.
109      
110      3   goto-if-nil 1           ; Pop and test top of stack;
111                                  ;   if `nil',
112                                  ;   go to label 1 (which is also byte 7),
113                                  ;   else continue.
114      
115      5   constant 1              ; Push 1 onto top of stack.
116      
117      6   return                  ; Return the top element
118                                  ;   of the stack.
119      
120      7:1 varref   integer        ; Push value of `integer' onto stack.
121      
122      8   constant factorial      ; Push `factorial' onto stack.
123      
124      9   varref   integer        ; Push value of `integer' onto stack.
125      
126      10  sub1                    ; Pop `integer', decrement value,
127                                  ;   push new value onto stack.
128      
129                                  ; Stack now contains:
130                                  ;   - decremented value of `integer'
131                                  ;   - `factorial'
132                                  ;   - value of `integer'
133      
134      15  call     1              ; Call function `factorial' using
135                                  ;   the first (i.e., the top) element
136                                  ;   of the stack as the argument;
137                                  ;   push returned value onto stack.
138      
139                                  ; Stack now contains:
140                                  ;   - result of recursive
141                                  ;        call to `factorial'
142                                  ;   - value of `integer'
143      
144      12  mult                    ; Pop top two values off the stack,
145                                  ;   multiply them,
146                                  ;   pushing the result onto the stack.
147      
148      13  return                  ; Return the top element
149                                  ;   of the stack.
150           => nil
151
152    The `silly-loop' function is somewhat more complex:
153
154      (defun silly-loop (n)
155        "Return time before and after N iterations of a loop."
156        (let ((t1 (current-time-string)))
157          (while (> (setq n (1- n))
158                    0))
159          (list t1 (current-time-string))))
160           => silly-loop
161      
162      (disassemble 'silly-loop)
163           -| byte-code for silly-loop:
164       doc: Return time before and after N iterations of a loop.
165       args: (n)
166      
167      0   constant current-time-string  ; Push
168                                        ;   `current-time-string'
169                                        ;   onto top of stack.
170      
171      1   call     0              ; Call `current-time-string'
172                                  ;    with no argument,
173                                  ;    pushing result onto stack.
174      
175      2   varbind  t1             ; Pop stack and bind `t1'
176                                  ;   to popped value.
177      
178      3:1 varref   n              ; Get value of `n' from
179                                  ;   the environment and push
180                                  ;   the value onto the stack.
181      
182      4   sub1                    ; Subtract 1 from top of stack.
183      
184      5   dup                     ; Duplicate the top of the stack;
185                                  ;   i.e., copy the top of
186                                  ;   the stack and push the
187                                  ;   copy onto the stack.
188      
189      6   varset   n              ; Pop the top of the stack,
190                                  ;   and set `n' to the value.
191      
192                                  ; In effect, the sequence `dup varset'
193                                  ;   copies the top of the stack
194                                  ;   into the value of `n'
195                                  ;   without popping it.
196      
197      7   constant 0              ; Push 0 onto stack.
198      
199      8   gtr                     ; Pop top two values off stack,
200                                  ;   test if N is greater than 0
201                                  ;   and push result onto stack.
202      
203      9   goto-if-not-nil 1       ; Goto label 1 (byte 3) if `n' <= 0
204                                  ;   (this exits the while loop).
205                                  ;   else pop top of stack
206                                  ;   and continue
207      
208      11  varref   t1             ; Push value of `t1' onto stack.
209      
210      12  constant current-time-string  ; Push
211                                        ;   `current-time-string'
212                                        ;   onto top of stack.
213      
214      13  call     0              ; Call `current-time-string' again.
215      
216      14  unbind   1              ; Unbind `t1' in local environment.
217      
218      15  list2                   ; Pop top two elements off stack,
219                                  ;   create a list of them,
220                                  ;   and push list onto stack.
221      
222      16  return                  ; Return the top element of the stack.
223      
224           => nil
225
226 \1f
227 File: lispref.info,  Node: Different Behavior,  Prev: Disassembly,  Up: Byte Compilation
228
229 Different Behavior
230 ==================
231
232    The intent is that compiled byte-code and the corresponding code
233 executed by the Lisp interpreter produce identical results.  However,
234 there are some circumstances where the results will differ.
235
236    * Arithmetic operations may be rearranged for efficiency or
237      compile-time evaluation.  When floating point numbers are
238      involved, this may produce different values or an overflow.
239
240    * Some arithmetic operations may be optimized away.  For example, the
241      expression `(+ x)' may be optimized to simply `x'.  If the value
242      of `x' is a marker, then the value will be a marker instead of an
243      integer.  If the value of `x' is a cons cell, then the interpreter
244      will issue an error, while the bytecode will not.
245
246      If you're trying to use `(+ OBJECT 0)' to convert OBJECT to
247      integer, consider using an explicit conversion function, which is
248      clearer and guaranteed to work.  Instead of `(+ MARKER 0)', use
249      `(marker-position MARKER)'.  Instead of `(+ CHAR 0)', use
250      `(char-int CHAR)'.
251
252    For maximal equivalence between interpreted and compiled code, the
253 variables `byte-compile-delete-errors' and `byte-compile-optimize' can
254 be set to `nil', but this is not recommended.
255
256 \1f
257 File: lispref.info,  Node: Debugging,  Next: Read and Print,  Prev: Byte Compilation,  Up: Top
258
259 Debugging Lisp Programs
260 ***********************
261
262    There are three ways to investigate a problem in an XEmacs Lisp
263 program, depending on what you are doing with the program when the
264 problem appears.
265
266    * If the problem occurs when you run the program, you can use a Lisp
267      debugger (either the default debugger or Edebug) to investigate
268      what is happening during execution.
269
270    * If the problem is syntactic, so that Lisp cannot even read the
271      program, you can use the XEmacs facilities for editing Lisp to
272      localize it.
273
274    * If the problem occurs when trying to compile the program with the
275      byte compiler, you need to know how to examine the compiler's
276      input buffer.
277
278 * Menu:
279
280 * Debugger::            How the XEmacs Lisp debugger is implemented.
281 * Syntax Errors::       How to find syntax errors.
282 * Compilation Errors::  How to find errors that show up in byte compilation.
283 * Edebug::              A source-level XEmacs Lisp debugger.
284
285    Another useful debugging tool is the dribble file.  When a dribble
286 file is open, XEmacs copies all keyboard input characters to that file.
287 Afterward, you can examine the file to find out what input was used.
288 *Note Terminal Input::.
289
290    For debugging problems in terminal descriptions, the
291 `open-termscript' function can be useful.  *Note Terminal Output::.
292
293 \1f
294 File: lispref.info,  Node: Debugger,  Next: Syntax Errors,  Up: Debugging
295
296 The Lisp Debugger
297 =================
298
299    The "Lisp debugger" provides the ability to suspend evaluation of a
300 form.  While evaluation is suspended (a state that is commonly known as
301 a "break"), you may examine the run time stack, examine the values of
302 local or global variables, or change those values.  Since a break is a
303 recursive edit, all the usual editing facilities of XEmacs are
304 available; you can even run programs that will enter the debugger
305 recursively.  *Note Recursive Editing::.
306
307 * Menu:
308
309 * Error Debugging::       Entering the debugger when an error happens.
310 * Infinite Loops::        Stopping and debugging a program that doesn't exit.
311 * Function Debugging::    Entering it when a certain function is called.
312 * Explicit Debug::        Entering it at a certain point in the program.
313 * Using Debugger::        What the debugger does; what you see while in it.
314 * Debugger Commands::     Commands used while in the debugger.
315 * Invoking the Debugger:: How to call the function `debug'.
316 * Internals of Debugger:: Subroutines of the debugger, and global variables.
317
318 \1f
319 File: lispref.info,  Node: Error Debugging,  Next: Infinite Loops,  Up: Debugger
320
321 Entering the Debugger on an Error
322 ---------------------------------
323
324    The most important time to enter the debugger is when a Lisp error
325 happens.  This allows you to investigate the immediate causes of the
326 error.
327
328    However, entry to the debugger is not a normal consequence of an
329 error.  Many commands frequently get Lisp errors when invoked in
330 inappropriate contexts (such as `C-f' at the end of the buffer) and
331 during ordinary editing it would be very unpleasant to enter the
332 debugger each time this happens.  If you want errors to enter the
333 debugger, set the variable `debug-on-error' to non-`nil'.
334
335  - User Option: debug-on-error
336      This variable determines whether the debugger is called when an
337      error is signaled and not handled.  If `debug-on-error' is `t', all
338      errors call the debugger.  If it is `nil', none call the debugger.
339
340      The value can also be a list of error conditions that should call
341      the debugger.  For example, if you set it to the list
342      `(void-variable)', then only errors about a variable that has no
343      value invoke the debugger.
344
345      When this variable is non-`nil', Emacs does not catch errors that
346      happen in process filter functions and sentinels.  Therefore, these
347      errors also can invoke the debugger.  *Note Processes::.
348
349  - User Option: debug-on-signal
350      This variable is similar to `debug-on-error' but breaks whenever
351      an error is signalled, regardless of whether it would be handled.
352
353  - User Option: debug-ignored-errors
354      This variable specifies certain kinds of errors that should not
355      enter the debugger.  Its value is a list of error condition
356      symbols and/or regular expressions.  If the error has any of those
357      condition symbols, or if the error message matches any of the
358      regular expressions, then that error does not enter the debugger,
359      regardless of the value of `debug-on-error'.
360
361      The normal value of this variable lists several errors that happen
362      often during editing but rarely result from bugs in Lisp programs.
363
364    To debug an error that happens during loading of the `.emacs' file,
365 use the option `-debug-init', which binds `debug-on-error' to `t' while
366 `.emacs' is loaded and inhibits use of `condition-case' to catch init
367 file errors.
368
369    If your `.emacs' file sets `debug-on-error', the effect may not last
370 past the end of loading `.emacs'.  (This is an undesirable byproduct of
371 the code that implements the `-debug-init' command line option.)  The
372 best way to make `.emacs' set `debug-on-error' permanently is with
373 `after-init-hook', like this:
374
375      (add-hook 'after-init-hook
376                '(lambda () (setq debug-on-error t)))
377
378 \1f
379 File: lispref.info,  Node: Infinite Loops,  Next: Function Debugging,  Prev: Error Debugging,  Up: Debugger
380
381 Debugging Infinite Loops
382 ------------------------
383
384    When a program loops infinitely and fails to return, your first
385 problem is to stop the loop.  On most operating systems, you can do this
386 with `C-g', which causes quit.
387
388    Ordinary quitting gives no information about why the program was
389 looping.  To get more information, you can set the variable
390 `debug-on-quit' to non-`nil'.  Quitting with `C-g' is not considered an
391 error, and `debug-on-error' has no effect on the handling of `C-g'.
392 Likewise, `debug-on-quit' has no effect on errors.
393
394    Once you have the debugger running in the middle of the infinite
395 loop, you can proceed from the debugger using the stepping commands.
396 If you step through the entire loop, you will probably get enough
397 information to solve the problem.
398
399  - User Option: debug-on-quit
400      This variable determines whether the debugger is called when `quit'
401      is signaled and not handled.  If `debug-on-quit' is non-`nil',
402      then the debugger is called whenever you quit (that is, type
403      `C-g').  If `debug-on-quit' is `nil', then the debugger is not
404      called when you quit.  *Note Quitting::.
405
406 \1f
407 File: lispref.info,  Node: Function Debugging,  Next: Explicit Debug,  Prev: Infinite Loops,  Up: Debugger
408
409 Entering the Debugger on a Function Call
410 ----------------------------------------
411
412    To investigate a problem that happens in the middle of a program, one
413 useful technique is to enter the debugger whenever a certain function is
414 called.  You can do this to the function in which the problem occurs,
415 and then step through the function, or you can do this to a function
416 called shortly before the problem, step quickly over the call to that
417 function, and then step through its caller.
418
419  - Command: debug-on-entry function-name
420      This function requests FUNCTION-NAME to invoke the debugger each
421      time it is called.  It works by inserting the form `(debug
422      'debug)' into the function definition as the first form.
423
424      Any function defined as Lisp code may be set to break on entry,
425      regardless of whether it is interpreted code or compiled code.  If
426      the function is a command, it will enter the debugger when called
427      from Lisp and when called interactively (after the reading of the
428      arguments).  You can't debug primitive functions (i.e., those
429      written in C) this way.
430
431      When `debug-on-entry' is called interactively, it prompts for
432      FUNCTION-NAME in the minibuffer.
433
434      If the function is already set up to invoke the debugger on entry,
435      `debug-on-entry' does nothing.
436
437      *Please note:* if you redefine a function after using
438      `debug-on-entry' on it, the code to enter the debugger is lost.
439
440      `debug-on-entry' returns FUNCTION-NAME.
441
442           (defun fact (n)
443             (if (zerop n) 1
444                 (* n (fact (1- n)))))
445                => fact
446           (debug-on-entry 'fact)
447                => fact
448           (fact 3)
449           
450           ------ Buffer: *Backtrace* ------
451           Entering:
452           * fact(3)
453             eval-region(4870 4878 t)
454             byte-code("...")
455             eval-last-sexp(nil)
456             (let ...)
457             eval-insert-last-sexp(nil)
458           * call-interactively(eval-insert-last-sexp)
459           ------ Buffer: *Backtrace* ------
460           
461           (symbol-function 'fact)
462                => (lambda (n)
463                     (debug (quote debug))
464                     (if (zerop n) 1 (* n (fact (1- n)))))
465
466  - Command: cancel-debug-on-entry &optional function-name
467      This function undoes the effect of `debug-on-entry' on
468      FUNCTION-NAME.  When called interactively, it prompts for
469      FUNCTION-NAME in the minibuffer.  If FUNCTION-NAME is `nil' or the
470      empty string, it cancels debugging for all functions.
471
472      If `cancel-debug-on-entry' is called more than once on the same
473      function, the second call does nothing.  `cancel-debug-on-entry'
474      returns FUNCTION-NAME.
475
476 \1f
477 File: lispref.info,  Node: Explicit Debug,  Next: Using Debugger,  Prev: Function Debugging,  Up: Debugger
478
479 Explicit Entry to the Debugger
480 ------------------------------
481
482    You can cause the debugger to be called at a certain point in your
483 program by writing the expression `(debug)' at that point.  To do this,
484 visit the source file, insert the text `(debug)' at the proper place,
485 and type `C-M-x'.  Be sure to undo this insertion before you save the
486 file!
487
488    The place where you insert `(debug)' must be a place where an
489 additional form can be evaluated and its value ignored.  (If the value
490 of `(debug)' isn't ignored, it will alter the execution of the
491 program!)  The most common suitable places are inside a `progn' or an
492 implicit `progn' (*note Sequencing::).
493
494 \1f
495 File: lispref.info,  Node: Using Debugger,  Next: Debugger Commands,  Prev: Explicit Debug,  Up: Debugger
496
497 Using the Debugger
498 ------------------
499
500    When the debugger is entered, it displays the previously selected
501 buffer in one window and a buffer named `*Backtrace*' in another
502 window.  The backtrace buffer contains one line for each level of Lisp
503 function execution currently going on.  At the beginning of this buffer
504 is a message describing the reason that the debugger was invoked (such
505 as the error message and associated data, if it was invoked due to an
506 error).
507
508    The backtrace buffer is read-only and uses a special major mode,
509 Debugger mode, in which letters are defined as debugger commands.  The
510 usual XEmacs editing commands are available; thus, you can switch
511 windows to examine the buffer that was being edited at the time of the
512 error, switch buffers, visit files, or do any other sort of editing.
513 However, the debugger is a recursive editing level (*note Recursive
514 Editing::) and it is wise to go back to the backtrace buffer and exit
515 the debugger (with the `q' command) when you are finished with it.
516 Exiting the debugger gets out of the recursive edit and kills the
517 backtrace buffer.
518
519    The backtrace buffer shows you the functions that are executing and
520 their argument values.  It also allows you to specify a stack frame by
521 moving point to the line describing that frame.  (A stack frame is the
522 place where the Lisp interpreter records information about a particular
523 invocation of a function.)  The frame whose line point is on is
524 considered the "current frame".  Some of the debugger commands operate
525 on the current frame.
526
527    The debugger itself must be run byte-compiled, since it makes
528 assumptions about how many stack frames are used for the debugger
529 itself.  These assumptions are false if the debugger is running
530 interpreted.
531
532 \1f
533 File: lispref.info,  Node: Debugger Commands,  Next: Invoking the Debugger,  Prev: Using Debugger,  Up: Debugger
534
535 Debugger Commands
536 -----------------
537
538    Inside the debugger (in Debugger mode), these special commands are
539 available in addition to the usual cursor motion commands.  (Keep in
540 mind that all the usual facilities of XEmacs, such as switching windows
541 or buffers, are still available.)
542
543    The most important use of debugger commands is for stepping through
544 code, so that you can see how control flows.  The debugger can step
545 through the control structures of an interpreted function, but cannot do
546 so in a byte-compiled function.  If you would like to step through a
547 byte-compiled function, replace it with an interpreted definition of the
548 same function.  (To do this, visit the source file for the function and
549 type `C-M-x' on its definition.)
550
551    Here is a list of Debugger mode commands:
552
553 `c'
554      Exit the debugger and continue execution.  This resumes execution
555      of the program as if the debugger had never been entered (aside
556      from the effect of any variables or data structures you may have
557      changed while inside the debugger).
558
559      Continuing when an error or quit was signalled will cause the
560      normal action of the signalling to take place.  If you do not want
561      this to happen, but instead want the program execution to continue
562      as if the call to `signal' did not occur, use the `r' command.
563
564 `d'
565      Continue execution, but enter the debugger the next time any Lisp
566      function is called.  This allows you to step through the
567      subexpressions of an expression, seeing what values the
568      subexpressions compute, and what else they do.
569
570      The stack frame made for the function call which enters the
571      debugger in this way will be flagged automatically so that the
572      debugger will be called again when the frame is exited.  You can
573      use the `u' command to cancel this flag.
574
575 `b'
576      Flag the current frame so that the debugger will be entered when
577      the frame is exited.  Frames flagged in this way are marked with
578      stars in the backtrace buffer.
579
580 `u'
581      Don't enter the debugger when the current frame is exited.  This
582      cancels a `b' command on that frame.
583
584 `e'
585      Read a Lisp expression in the minibuffer, evaluate it, and print
586      the value in the echo area.  The debugger alters certain important
587      variables, and the current buffer, as part of its operation; `e'
588      temporarily restores their outside-the-debugger values so you can
589      examine them.  This makes the debugger more transparent.  By
590      contrast, `M-:' does nothing special in the debugger; it shows you
591      the variable values within the debugger.
592
593 `q'
594      Terminate the program being debugged; return to top-level XEmacs
595      command execution.
596
597      If the debugger was entered due to a `C-g' but you really want to
598      quit, and not debug, use the `q' command.
599
600 `r'
601      Return a value from the debugger.  The value is computed by
602      reading an expression with the minibuffer and evaluating it.
603
604      The `r' command is useful when the debugger was invoked due to exit
605      from a Lisp call frame (as requested with `b'); then the value
606      specified in the `r' command is used as the value of that frame.
607      It is also useful if you call `debug' and use its return value.
608
609      If the debugger was entered at the beginning of a function call,
610      `r' has the same effect as `c', and the specified return value
611      does not matter.
612
613      If the debugger was entered through a call to `signal' (i.e. as a
614      result of an error or quit), then returning a value will cause the
615      call to `signal' itself to return, rather than throwing to
616      top-level or invoking a handler, as is normal.  This allows you to
617      correct an error (e.g. the type of an argument was wrong) or
618      continue from a `debug-on-quit' as if it never happened.
619
620      Note that some errors (e.g. any error signalled using the `error'
621      function, and many errors signalled from a primitive function) are
622      not continuable.  If you return a value from them and continue
623      execution, then the error will immediately be signalled again.
624      Other errors (e.g. wrong-type-argument errors) will be continually
625      resignalled until the problem is corrected.
626
627 \1f
628 File: lispref.info,  Node: Invoking the Debugger,  Next: Internals of Debugger,  Prev: Debugger Commands,  Up: Debugger
629
630 Invoking the Debugger
631 ---------------------
632
633    Here we describe fully the function used to invoke the debugger.
634
635  - Function: debug &rest debugger-args
636      This function enters the debugger.  It switches buffers to a buffer
637      named `*Backtrace*' (or `*Backtrace*<2>' if it is the second
638      recursive entry to the debugger, etc.), and fills it with
639      information about the stack of Lisp function calls.  It then
640      enters a recursive edit, showing the backtrace buffer in Debugger
641      mode.
642
643      The Debugger mode `c' and `r' commands exit the recursive edit;
644      then `debug' switches back to the previous buffer and returns to
645      whatever called `debug'.  This is the only way the function
646      `debug' can return to its caller.
647
648      If the first of the DEBUGGER-ARGS passed to `debug' is `nil' (or
649      if it is not one of the special values in the table below), then
650      `debug' displays the rest of its arguments at the top of the
651      `*Backtrace*' buffer.  This mechanism is used to display a message
652      to the user.
653
654      However, if the first argument passed to `debug' is one of the
655      following special values, then it has special significance.
656      Normally, these values are passed to `debug' only by the internals
657      of XEmacs and the debugger, and not by programmers calling `debug'.
658
659      The special values are:
660
661     `lambda'
662           A first argument of `lambda' means `debug' was called because
663           of entry to a function when `debug-on-next-call' was
664           non-`nil'.  The debugger displays `Entering:' as a line of
665           text at the top of the buffer.
666
667     `debug'
668           `debug' as first argument indicates a call to `debug' because
669           of entry to a function that was set to debug on entry.  The
670           debugger displays `Entering:', just as in the `lambda' case.
671           It also marks the stack frame for that function so that it
672           will invoke the debugger when exited.
673
674     `t'
675           When the first argument is `t', this indicates a call to
676           `debug' due to evaluation of a list form when
677           `debug-on-next-call' is non-`nil'.  The debugger displays the
678           following as the top line in the buffer:
679
680                Beginning evaluation of function call form:
681
682     `exit'
683           When the first argument is `exit', it indicates the exit of a
684           stack frame previously marked to invoke the debugger on exit.
685           The second argument given to `debug' in this case is the
686           value being returned from the frame.  The debugger displays
687           `Return value:' on the top line of the buffer, followed by
688           the value being returned.
689
690     `error'
691           When the first argument is `error', the debugger indicates
692           that it is being entered because an error or `quit' was
693           signaled and not handled, by displaying `Signaling:' followed
694           by the error signaled and any arguments to `signal'.  For
695           example,
696
697                (let ((debug-on-error t))
698                  (/ 1 0))
699                
700                ------ Buffer: *Backtrace* ------
701                Signaling: (arith-error)
702                  /(1 0)
703                ...
704                ------ Buffer: *Backtrace* ------
705
706           If an error was signaled, presumably the variable
707           `debug-on-error' is non-`nil'.  If `quit' was signaled, then
708           presumably the variable `debug-on-quit' is non-`nil'.
709
710     `nil'
711           Use `nil' as the first of the DEBUGGER-ARGS when you want to
712           enter the debugger explicitly.  The rest of the DEBUGGER-ARGS
713           are printed on the top line of the buffer.  You can use this
714           feature to display messages--for example, to remind yourself
715           of the conditions under which `debug' is called.
716
717 \1f
718 File: lispref.info,  Node: Internals of Debugger,  Prev: Invoking the Debugger,  Up: Debugger
719
720 Internals of the Debugger
721 -------------------------
722
723    This section describes functions and variables used internally by the
724 debugger.
725
726  - Variable: debugger
727      The value of this variable is the function to call to invoke the
728      debugger.  Its value must be a function of any number of arguments
729      (or, more typically, the name of a function).  Presumably this
730      function will enter some kind of debugger.  The default value of
731      the variable is `debug'.
732
733      The first argument that Lisp hands to the function indicates why it
734      was called.  The convention for arguments is detailed in the
735      description of `debug'.
736
737  - Command: backtrace &optional stream detailed
738      This function prints a trace of Lisp function calls currently
739      active.  This is the function used by `debug' to fill up the
740      `*Backtrace*' buffer.  It is written in C, since it must have
741      access to the stack to determine which function calls are active.
742      The return value is always `nil'.
743
744      The backtrace is normally printed to `standard-output', but this
745      can be changed by specifying a value for STREAM.  If DETAILED is
746      non-`nil', the backtrace also shows places where currently active
747      variable bindings, catches, condition-cases, and unwind-protects
748      were made as well as function calls.
749
750      In the following example, a Lisp expression calls `backtrace'
751      explicitly.  This prints the backtrace to the stream
752      `standard-output': in this case, to the buffer `backtrace-output'.
753      Each line of the backtrace represents one function call.  The
754      line shows the values of the function's arguments if they are all
755      known.  If they are still being computed, the line says so.  The
756      arguments of special forms are elided.
757
758           (with-output-to-temp-buffer "backtrace-output"
759             (let ((var 1))
760               (save-excursion
761                 (setq var (eval '(progn
762                                    (1+ var)
763                                    (list 'testing (backtrace))))))))
764           
765                => nil
766           
767           ----------- Buffer: backtrace-output ------------
768             backtrace()
769             (list ...computing arguments...)
770             (progn ...)
771             eval((progn (1+ var) (list (quote testing) (backtrace))))
772             (setq ...)
773             (save-excursion ...)
774             (let ...)
775             (with-output-to-temp-buffer ...)
776             eval-region(1973 2142 #<buffer *scratch*>)
777             byte-code("...  for eval-print-last-sexp ...")
778             eval-print-last-sexp(nil)
779           * call-interactively(eval-print-last-sexp)
780           ----------- Buffer: backtrace-output ------------
781
782      The character `*' indicates a frame whose debug-on-exit flag is
783      set.
784
785  - Variable: debug-on-next-call
786      If this variable is non-`nil', it says to call the debugger before
787      the next `eval', `apply' or `funcall'.  Entering the debugger sets
788      `debug-on-next-call' to `nil'.
789
790      The `d' command in the debugger works by setting this variable.
791
792  - Function: backtrace-debug level flag
793      This function sets the debug-on-exit flag of the stack frame LEVEL
794      levels down the stack, giving it the value FLAG.  If FLAG is
795      non-`nil', this will cause the debugger to be entered when that
796      frame later exits.  Even a nonlocal exit through that frame will
797      enter the debugger.
798
799      This function is used only by the debugger.
800
801  - Variable: command-debug-status
802      This variable records the debugging status of the current
803      interactive command.  Each time a command is called interactively,
804      this variable is bound to `nil'.  The debugger can set this
805      variable to leave information for future debugger invocations
806      during the same command.
807
808      The advantage, for the debugger, of using this variable rather than
809      another global variable is that the data will never carry over to a
810      subsequent command invocation.
811
812  - Function: backtrace-frame frame-number
813      The function `backtrace-frame' is intended for use in Lisp
814      debuggers.  It returns information about what computation is
815      happening in the stack frame FRAME-NUMBER levels down.
816
817      If that frame has not evaluated the arguments yet (or is a special
818      form), the value is `(nil FUNCTION ARG-FORMS...)'.
819
820      If that frame has evaluated its arguments and called its function
821      already, the value is `(t FUNCTION ARG-VALUES...)'.
822
823      In the return value, FUNCTION is whatever was supplied as the CAR
824      of the evaluated list, or a `lambda' expression in the case of a
825      macro call.  If the function has a `&rest' argument, that is
826      represented as the tail of the list ARG-VALUES.
827
828      If FRAME-NUMBER is out of range, `backtrace-frame' returns `nil'.
829
830 \1f
831 File: lispref.info,  Node: Syntax Errors,  Next: Compilation Errors,  Prev: Debugger,  Up: Debugging
832
833 Debugging Invalid Lisp Syntax
834 =============================
835
836    The Lisp reader reports invalid syntax, but cannot say where the real
837 problem is.  For example, the error "End of file during parsing" in
838 evaluating an expression indicates an excess of open parentheses (or
839 square brackets).  The reader detects this imbalance at the end of the
840 file, but it cannot figure out where the close parenthesis should have
841 been.  Likewise, "Invalid read syntax: ")"" indicates an excess close
842 parenthesis or missing open parenthesis, but does not say where the
843 missing parenthesis belongs.  How, then, to find what to change?
844
845    If the problem is not simply an imbalance of parentheses, a useful
846 technique is to try `C-M-e' at the beginning of each defun, and see if
847 it goes to the place where that defun appears to end.  If it does not,
848 there is a problem in that defun.
849
850    However, unmatched parentheses are the most common syntax errors in
851 Lisp, and we can give further advice for those cases.
852
853 * Menu:
854
855 * Excess Open::     How to find a spurious open paren or missing close.
856 * Excess Close::    How to find a spurious close paren or missing open.
857
858 \1f
859 File: lispref.info,  Node: Excess Open,  Next: Excess Close,  Up: Syntax Errors
860
861 Excess Open Parentheses
862 -----------------------
863
864    The first step is to find the defun that is unbalanced.  If there is
865 an excess open parenthesis, the way to do this is to insert a close
866 parenthesis at the end of the file and type `C-M-b' (`backward-sexp').
867 This will move you to the beginning of the defun that is unbalanced.
868 (Then type `C-<SPC> C-_ C-u C-<SPC>' to set the mark there, undo the
869 insertion of the close parenthesis, and finally return to the mark.)
870
871    The next step is to determine precisely what is wrong.  There is no
872 way to be sure of this except to study the program, but often the
873 existing indentation is a clue to where the parentheses should have
874 been.  The easiest way to use this clue is to reindent with `C-M-q' and
875 see what moves.
876
877    Before you do this, make sure the defun has enough close parentheses.
878 Otherwise, `C-M-q' will get an error, or will reindent all the rest of
879 the file until the end.  So move to the end of the defun and insert a
880 close parenthesis there.  Don't use `C-M-e' to move there, since that
881 too will fail to work until the defun is balanced.
882
883    Now you can go to the beginning of the defun and type `C-M-q'.
884 Usually all the lines from a certain point to the end of the function
885 will shift to the right.  There is probably a missing close parenthesis,
886 or a superfluous open parenthesis, near that point.  (However, don't
887 assume this is true; study the code to make sure.)  Once you have found
888 the discrepancy, undo the `C-M-q' with `C-_', since the old indentation
889 is probably appropriate to the intended parentheses.
890
891    After you think you have fixed the problem, use `C-M-q' again.  If
892 the old indentation actually fit the intended nesting of parentheses,
893 and you have put back those parentheses, `C-M-q' should not change
894 anything.
895
896 \1f
897 File: lispref.info,  Node: Excess Close,  Prev: Excess Open,  Up: Syntax Errors
898
899 Excess Close Parentheses
900 ------------------------
901
902    To deal with an excess close parenthesis, first insert an open
903 parenthesis at the beginning of the file, back up over it, and type
904 `C-M-f' to find the end of the unbalanced defun.  (Then type `C-<SPC>
905 C-_ C-u C-<SPC>' to set the mark there, undo the insertion of the open
906 parenthesis, and finally return to the mark.)
907
908    Then find the actual matching close parenthesis by typing `C-M-f' at
909 the beginning of the defun.  This will leave you somewhere short of the
910 place where the defun ought to end.  It is possible that you will find
911 a spurious close parenthesis in that vicinity.
912
913    If you don't see a problem at that point, the next thing to do is to
914 type `C-M-q' at the beginning of the defun.  A range of lines will
915 probably shift left; if so, the missing open parenthesis or spurious
916 close parenthesis is probably near the first of those lines.  (However,
917 don't assume this is true; study the code to make sure.)  Once you have
918 found the discrepancy, undo the `C-M-q' with `C-_', since the old
919 indentation is probably appropriate to the intended parentheses.
920
921    After you think you have fixed the problem, use `C-M-q' again.  If
922 the old indentation actually fit the intended nesting of parentheses,
923 and you have put back those parentheses, `C-M-q' should not change
924 anything.
925
926 \1f
927 File: lispref.info,  Node: Compilation Errors,  Next: Edebug,  Prev: Syntax Errors,  Up: Debugging
928
929 Debugging Problems in Compilation
930 =================================
931
932    When an error happens during byte compilation, it is normally due to
933 invalid syntax in the program you are compiling.  The compiler prints a
934 suitable error message in the `*Compile-Log*' buffer, and then stops.
935 The message may state a function name in which the error was found, or
936 it may not.  Either way, here is how to find out where in the file the
937 error occurred.
938
939    What you should do is switch to the buffer ` *Compiler Input*'.
940 (Note that the buffer name starts with a space, so it does not show up
941 in `M-x list-buffers'.)  This buffer contains the program being
942 compiled, and point shows how far the byte compiler was able to read.
943
944    If the error was due to invalid Lisp syntax, point shows exactly
945 where the invalid syntax was _detected_.  The cause of the error is not
946 necessarily near by!  Use the techniques in the previous section to find
947 the error.
948
949    If the error was detected while compiling a form that had been read
950 successfully, then point is located at the end of the form.  In this
951 case, this technique can't localize the error precisely, but can still
952 show you which function to check.
953
954 \1f
955 File: lispref.info,  Node: Edebug,  Prev: Compilation Errors,  Up: Top
956
957 Edebug
958 ======
959
960    Edebug is a source-level debugger for XEmacs Lisp programs that
961 provides the following features:
962
963    * Step through evaluation, stopping before and after each expression.
964
965    * Set conditional or unconditional breakpoints, install embedded
966      breakpoints, or a global break event.
967
968    * Trace slow or fast stopping briefly at each stop point, or each
969      breakpoint.
970
971    * Display expression results and evaluate expressions as if outside
972      of Edebug.  Interface with the custom printing package for
973      printing circular structures.
974
975    * Automatically reevaluate a list of expressions and display their
976      results each time Edebug updates the display.
977
978    * Output trace info on function enter and exit.
979
980    * Errors stop before the source causing the error.
981
982    * Display backtrace without Edebug calls.
983
984    * Allow specification of argument evaluation for macros and defining
985      forms.
986
987    * Provide rudimentary coverage testing and display of frequency
988      counts.
989
990
991    The first three sections should tell you enough about Edebug to
992 enable you to use it.
993
994 * Menu:
995
996 * Using Edebug::                Introduction to use of Edebug.
997 * Instrumenting::               You must first instrument code.
998 * Edebug Execution Modes::      Execution modes, stopping more or less often.
999 * Jumping::                     Commands to jump to a specified place.
1000 * Edebug Misc::                 Miscellaneous commands.
1001 * Breakpoints::                 Setting breakpoints to make the program stop.
1002 * Trapping Errors::             trapping errors with Edebug.
1003 * Edebug Views::                Views inside and outside of Edebug.
1004 * Edebug Eval::                 Evaluating expressions within Edebug.
1005 * Eval List::                   Automatic expression evaluation.
1006 * Reading in Edebug::           Customization of reading.
1007 * Printing in Edebug::          Customization of printing.
1008 * Tracing::                     How to produce tracing output.
1009 * Coverage Testing::            How to test evaluation coverage.
1010 * The Outside Context::         Data that Edebug saves and restores.
1011 * Instrumenting Macro Calls::   Specifying how to handle macro calls.
1012 * Edebug Options::              Option variables for customizing Edebug.
1013
1014 \1f
1015 File: lispref.info,  Node: Using Edebug,  Next: Instrumenting,  Up: Edebug
1016
1017 Using Edebug
1018 ------------
1019
1020    To debug an XEmacs Lisp program with Edebug, you must first
1021 "instrument" the Lisp code that you want to debug.  If you want to just
1022 try it now, load `edebug.el', move point into a definition and do `C-u
1023 C-M-x' (`eval-defun' with a prefix argument).  See *Note
1024 Instrumenting:: for alternative ways to instrument code.
1025
1026    Once a function is instrumented, any call to the function activates
1027 Edebug.  Activating Edebug may stop execution and let you step through
1028 the function, or it may update the display and continue execution while
1029 checking for debugging commands, depending on the selected Edebug
1030 execution mode.  The initial execution mode is `step', by default,
1031 which does stop execution.  *Note Edebug Execution Modes::.
1032
1033    Within Edebug, you normally view an XEmacs buffer showing the source
1034 of the Lisp function you are debugging.  This is referred to as the
1035 "source code buffer"--but note that it is not always the same buffer
1036 depending on which function is currently being executed.
1037
1038    An arrow at the left margin indicates the line where the function is
1039 executing.  Point initially shows where within the line the function is
1040 executing, but you can move point yourself.
1041
1042    If you instrument the definition of `fac' (shown below) and then
1043 execute `(fac 3)', here is what you normally see.  Point is at the
1044 open-parenthesis before `if'.
1045
1046      (defun fac (n)
1047      =>-!-(if (< 0 n)
1048            (* n (fac (1- n)))
1049          1))
1050
1051    The places within a function where Edebug can stop execution are
1052 called "stop points".  These occur both before and after each
1053 subexpression that is a list, and also after each variable reference.
1054 Here we show with periods the stop points found in the function `fac':
1055
1056      (defun fac (n)
1057        .(if .(< 0 n.).
1058            .(* n. .(fac (1- n.).).).
1059          1).)
1060
1061    While the source code buffer is selected, the special commands of
1062 Edebug are available in it, in addition to the commands of XEmacs Lisp
1063 mode.  (The buffer is temporarily made read-only, however.)  For
1064 example, you can type the Edebug command <SPC> to execute until the
1065 next stop point.  If you type <SPC> once after entry to `fac', here is
1066 the display you will see:
1067
1068      (defun fac (n)
1069      =>(if -!-(< 0 n)
1070            (* n (fac (1- n)))
1071          1))
1072
1073    When Edebug stops execution after an expression, it displays the
1074 expression's value in the echo area.
1075
1076    Other frequently used commands are `b' to set a breakpoint at a stop
1077 point, `g' to execute until a breakpoint is reached, and `q' to exit to
1078 the top-level command loop.  Type `?' to display a list of all Edebug
1079 commands.
1080
1081 \1f
1082 File: lispref.info,  Node: Instrumenting,  Next: Edebug Execution Modes,  Prev: Using Edebug,  Up: Edebug
1083
1084 Instrumenting for Edebug
1085 ------------------------
1086
1087    In order to use Edebug to debug Lisp code, you must first
1088 "instrument" the code.  Instrumenting a form inserts additional code
1089 into it which invokes Edebug at the proper places.  Furthermore, if
1090 Edebug detects a syntax error while instrumenting, point is left at the
1091 erroneous code and an `invalid-read-syntax' error is signaled.
1092
1093    Once you have loaded Edebug, the command `C-M-x' (`eval-defun') is
1094 redefined so that when invoked with a prefix argument on a definition,
1095 it instruments the definition before evaluating it.  (The source code
1096 itself is not modified.)  If the variable `edebug-all-defs' is
1097 non-`nil', that inverts the meaning of the prefix argument: then
1098 `C-M-x' instruments the definition _unless_ it has a prefix argument.
1099 The default value of `edebug-all-defs' is `nil'.  The command `M-x
1100 edebug-all-defs' toggles the value of the variable `edebug-all-defs'.
1101
1102    If `edebug-all-defs' is non-`nil', then the commands `eval-region',
1103 `eval-current-buffer', and `eval-buffer' also instrument any
1104 definitions they evaluate.  Similarly, `edebug-all-forms' controls
1105 whether `eval-region' should instrument _any_ form, even non-defining
1106 forms.  This doesn't apply to loading or evaluations in the minibuffer.
1107 The command `M-x edebug-all-forms' toggles this option.
1108
1109    Another command, `M-x edebug-eval-top-level-form', is available to
1110 instrument any top-level form regardless of the value of
1111 `edebug-all-defs' or `edebug-all-forms'.
1112
1113    Just before Edebug instruments any code, it calls any functions in
1114 the variable `edebug-setup-hook' and resets its value to `nil'.  You
1115 could use this to load up Edebug specifications associated with a
1116 package you are using but only when you also use Edebug.  For example,
1117 `my-specs.el' may be loaded automatically when you use `my-package'
1118 with Edebug by including the following code in `my-package.el'.
1119
1120      (add-hook 'edebug-setup-hook
1121        (function (lambda () (require 'my-specs))))
1122
1123    While Edebug is active, the command `I' (`edebug-instrument-callee')
1124 instruments the definition of the function or macro called by the list
1125 form after point, if is not already instrumented.  If the location of
1126 the definition is not known to Edebug, this command cannot be used.
1127 After loading Edebug, `eval-region' records the position of every
1128 definition it evaluates, even if not instrumenting it.  Also see the
1129 command `i' (*Note Jumping::) which steps into the callee.
1130
1131    Edebug knows how to instrument all the standard special forms, an
1132 interactive form with an expression argument, anonymous lambda
1133 expressions, and other defining forms.  (Specifications for macros
1134 defined by `cl.el' (version 2.03) are provided in `cl-specs.el'.)
1135 Edebug cannot know what a user-defined macro will do with the arguments
1136 of a macro call so you must tell it.  See *Note Instrumenting Macro
1137 Calls:: for the details.
1138
1139    Note that a couple ways remain to evaluate expressions without
1140 instrumenting them.  Loading a file via the `load' subroutine does not
1141 instrument expressions for Edebug.  Evaluations in the minibuffer via
1142 `eval-expression' (`M-ESC') are not instrumented.
1143
1144    To remove instrumentation from a definition, simply reevaluate it
1145 with one of the non-instrumenting commands, or reload the file.
1146
1147    See *Note Edebug Eval:: for other evaluation functions available
1148 inside of Edebug.
1149