1 ;;; edebug.el --- a source-level debugger for emacs lisp
3 ;; Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc
5 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
6 ;; Keywords: lisp, tools
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
27 ;;; This minor mode allows programmers to step through Emacs Lisp source
28 ;;; code while executing, set breakpoints, etc. See the texinfo
29 ;;; document (being constructed...) for more detailed instructions
30 ;;; than contained here. Send me your enhancement, ideas, bugs, or
33 ;;; Daniel LaLiberte 217-244-0785
34 ;;; University of Illinois, Urbana-Champaign
35 ;;; Department of Computer Science
36 ;;; 1304 W Springfield
40 ;;; liberte@cs.uiuc.edu
52 ;;;================================================================
56 ;;; $Header: /import/kaplan/kaplan/liberte/Edebug/RCS/edebug.el,v 2.5 91/07/25 13:32:53 liberte Exp Locker: liberte $
57 ;;; $Log: edebug.el,v $
58 ;;; Revision 2.5 91/07/25 13:32:53 liberte
59 ;;; Doc string cleanup.
60 ;;; If edebug-form-hook is t, evaluate all arguments.
61 ;;; If edebug-form-hook is 0, evaluate no arguments.
62 ;;; If edebug-form-hook is nil, evaluate macro args according
63 ;;; to edebug-eval-macro-args.
64 ;;; Save the outside value of executing macro.
65 ;;; Save and restore the outside restriction.
66 ;;; Dont force update for go and Go-nonstop.
67 ;;; Save and restore last-command-char, last-command,
68 ;;; this-command, last-input-char.
69 ;;; For epoch, do epoch::dispatch-events before sit-for
70 ;;; and input-pending-p since X events could interfere.
71 ;;; Warn about unsetting non-existent breakpoint.
72 ;;; Fix edebug-forward-sexp with prefix arg.
73 ;;; Add edebug-step-out to exit from current sexp.
75 ;;; Revision 2.4 91/03/18 12:35:44 liberte
76 ;;; Force update after go or Go-nonstop modes, so overlay arrow is correct.
77 ;;; Support debug-on-quit. Remove edebug-on-error.
78 ;;; Fix edebug-anonymous. Bug found by jackr@wpd.sgi.com (Jack Repenning).
79 ;;; Don't discard-input anymore. Easier to change modes this way.
80 ;;; Fix max-lisp-eval-depth and max-specpdl-size incrementing.
81 ;;; Save and restore points in all buffers, if
82 ;;; edebug-save-buffer-points is non-nil. Expensive!
83 ;;; Bug caught by wolfgang@wsrcc.com (Wolfgang S. Rupprecht)
84 ;;; Save standard-output and standard-input in edebug-recursive-edit
85 ;;; so that edebug-outside-excursion can restore them.
86 ;;; Call set-buffer in edebug-pop-to-buffer since
87 ;;; select-window does not do that.
88 ;;; Fix edebug's eval-defun to remember current buffer inside evaluations
89 ;;; and to evaluate top-level forms. Found by Jamie Zawinski.
90 ;;; Add edebug-interactive-entry to support interactive forms with
91 ;;; non-string arg. Bug found by Jack Repenning.
92 ;;; Simplify edebug-restore-match-data to just store-match-data.
93 ;;; Motivated by linus@lysator.liu.se.
94 ;;; Move the match-data call to before the outside
95 ;;; buffer is changed, since it assumes that.
97 ;;; Revision 2.3 91/01/17 20:55:14 liberte
98 ;;; Fix bug found by hollen@megatek.uucp.
99 ;;; Current buffer was not being restored.
100 ;;; Call edebug with (edebug begin end 'exp)
101 ;;; and add additional wrapper around body of functions:
102 ;;; (edebug-enter function body).
103 ;;; Make &optional only apply to immediate next arg
104 ;;; in edebug-form-parser (was edebug-macro-parser).
105 ;;; Catch debug errors with edebug. Yeah!
106 ;;; Reset edebug-mode on first function entry. Yeah!
107 ;;; Motivated by Dion Hollenbeck.
108 ;;; Add the missing bindings to the global-edebug-map.
109 ;;; eval-current-buffer now uses eval-region.
110 ;;; eval-region now does not narrow region.
111 ;;; Narrowing was the cause of the window-start being set wrong.
112 ;;; Reset edebug-mode only on
113 ;;; first entry of any function at each recursive-edit level.
114 ;;; Add edebug-backtrace, to generate cleaned up
115 ;;; backtrace. It doesnt "work" like the debug backtrace, however.
116 ;;; Require reselecting outside window even if
117 ;;; quit occurs, otherwise save-excursions may restore
118 ;;; buffer to the wrong window.
120 ;;; Revision 2.2 90/11/26 21:14:22 liberte
121 ;;; Shadow eval-defun and eval-region. Toggle
122 ;;; edebugging with edebug-all-defuns.
123 ;;; Call edebug with (edebug 'function begin end 'exp)
124 ;;; Suggested by Jamie Zawinski <jwz@lucid.com>.
125 ;;; Add edebug-form-parser to process macro args.
126 ;;; Motivated by Darryl Okahata darrylo@hpnmxx.hp.com.
127 ;;; Fix by Roland McGrath <roland@ai.mit.edu>
128 ;;; to wrap body of edebug-save-restriction in progn.
129 ;;; Fix by Darryl Okahata <darrylo%hpnmd@hpcea.hp.com>
130 ;;; to add (set-window-hscroll (selected-window) 0) to
131 ;;; edebug-pop-to-buffer.
133 ;;; Revision 2.1 90/11/16 21:55:35 liberte
135 ;;; Add edebug-form-hook to edebug macro calls. Thanks to Joe Wells.
136 ;;; edebug-forward-sexp uses step mode if no forward-sexp.
138 ;;; Revision 2.0 90/11/14 22:30:54 liberte
139 ;;; Handle lambda forms, function, interactive evals, defmacro.
140 ;;; Clean up display for Epoch - save and restore screen configurations.
141 ;;; Note: epoch 3.2 broke set-window-configuration.
142 ;;; Also, sit-for pauses do not always work in epoch.
143 ;;; Display evaluations window.
144 ;;; Display result after expression evaluation.
145 ;;; Thanks to discussions with Shinichirou Sugou.
146 ;;; Conditional and temporary breakpoints.
147 ;;; Change "continue" to "go" mode and add different "continue" mode.
148 ;;; Option to stop before symbols.
150 ;;; Fix by: Glen Ditchfield gjditchfield@violet.uwaterloo.ca
151 ;;; to handle ?# type chars.
153 ;;; Revision 1.5 89/05/10 02:39:27 liberte
154 ;;; Fix condition-case expression lists.
155 ;;; Reorganize edebug.
157 ;;; Revision 1.4 89/02/14 22:58:34 liberte
158 ;;; Fix broken breakpointing.
159 ;;; Temporarily widen Emacs Lisp buffer during edebug.
161 ;;; Revision 1.3 89/01/30 00:26:09 liberte
162 ;;; More bug fixes for cond and let.
163 ;;; Another parsing fix backquote.
164 ;;; Fix for lambda forms inside defuns.
165 ;;; Leave point at syntax error, mark at starting position.
167 ;;; Revision 1.2 88/11/28 12:14:15 liberte
168 ;;; Bug fixes: cond construct didnt execute.
169 ;;; () in sexp list didnt parse
170 ;;; () as variable in condition-case didnt parse.
172 ;;; Revision 1.1 88/11/28 12:11:27 liberte
179 ;; Put edebug.el in some directory in your load-path and byte-compile it.
181 ;; Put the following forms in your .emacs file.
182 ;; (define-key emacs-lisp-mode-map "\^Xx" 'edebug-defun)
183 ;; (autoload 'edebug-defun "edebug")
184 ;; (autoload 'edebug-debug "edebug")
185 ;; (setq debugger 'edebug-debug)
186 ;; ... other options, described in the next section.
188 ;; Evaluate a defun for edebug with edebug-defun.
189 ;; Evaluate your function normally.
190 ;; Use the "?" command in edebug to describe other commands.
191 ;; See edebug.texinfo for more instructions.
199 (defvar edebug-all-defuns nil
200 "*If non-nil, all defuns and defmacros evaluated will use edebug.
201 eval-defun without prefix arg and eval-region will use edebug-defun.
203 If nil, eval-region evaluates normally, but eval-defun with prefix arg
204 uses edebug-defun. eval-region is called by eval-defun, eval-last-sexp,
205 and eval-print-last-sexp.
207 You may wish to make this variable local to each Emacs Lisp buffer by calling
208 (make-local-variable 'edebug-all-defuns) in your emacs-lisp-mode-hook.
209 You can use the function edebug-all-defuns to toggle its value.")
212 (defvar edebug-eval-macro-args nil
213 "*If non-nil, edebug will assume that all macro call arguments for
214 macros that have no edebug-form-hook may be evaluated, otherwise it
215 will not. To specify exceptions for macros that have some arguments
216 evaluated and some not, you should specify an edebug-form-hook")
218 (defvar edebug-stop-before-symbols nil
219 "*Non-nil causes edebug to stop before symbols as well as after.
220 In any case, it is possible to stop before a symbol with a breakpoint or
223 (defvar edebug-save-windows t
224 "*If non-nil, save and restore window configuration on edebug calls.
225 It takes some time to save and restore, so if your program does not care
226 what happens to the window configurations, it is better to set this
229 (defvar edebug-save-point t
230 "*If non-nil, save and restore the point and mark in source code buffers.")
232 (defvar edebug-save-buffer-points nil
233 "*If non-nil, save and restore the points of all buffers, displayed or not.
235 Saving and restoring buffer points is necessary if you are debugging
236 code that changes the point of a buffer which is displayed in a
237 non-selected window. If edebug or the user then selects the
238 window, the buffer's point will be changed to the window's point.
240 Saving and restoring all the points is an expensive operation since it
241 visits each buffer twice for each edebug call, so it is best to avoid
244 (defvar edebug-initial-mode
'step
245 "*Global initial mode for edebug, if non-nil.
246 This is used when edebug is first entered for each recursive-edit level.
247 Possible values are nil (meaning keep using edebug-mode), step, go,
248 Go-nonstop, trace, Trace-fast, continue, and Continue-fast.")
250 (defvar edebug-trace nil
251 "*Non-nil if edebug should show a trace of function entry and exit.
252 Tracing output is displayed in a buffer named *edebug-trace*, one
253 function entry or exit per line, indented by the recursion level. You
254 can customize by replacing functions edebug-print-trace-entry and
255 edebug-print-trace-exit.")
259 ;;;========================================================================
263 (defun edebug-which-function ()
264 "Return the symbol of the function we are in"
269 (if (not (memq (read (current-buffer)) '(defun defmacro)))
270 (error "Not in defun or defmacro."))
271 (read (current-buffer))))
273 (defun edebug-last-sexp ()
274 "Return the last sexp before point in current buffer.
275 Assumes Emacs Lisp syntax is active."
284 (defun edebug-window-list ()
285 "Return a list of windows, in order of next-window."
286 ;; This doesnt work for epoch.
287 (let* ((first-window (selected-window))
288 (window-list (list first-window
))
289 (next (next-window first-window
)))
290 (while (not (eq next first-window
))
291 (setq window-list
(cons next window-list
))
292 (setq next
(next-window next
)))
293 (nreverse window-list
)))
295 (defun edebug-get-buffer-points ()
296 "Return a list of buffer point pairs, for all buffers."
298 (mapcar (function (lambda (buf)
303 (defun edebug-set-buffer-points ()
304 "Restore the buffer-points given by edebug-get-buffer-points."
305 (mapcar (function (lambda (buf-point)
306 (if (buffer-name (car buf-point
)) ; still exists
308 (set-buffer (car buf-point
))
309 (goto-char (cdr buf-point
))))))
310 edebug-buffer-points
))
312 (defun edebug-two-window-p ()
313 "Return t if there are two windows."
314 (and (not (one-window-p))
315 (eq (selected-window)
316 (next-window (next-window (selected-window))))))
318 (defun edebug-macrop (object)
319 "Return the macro named by OBJECT, or nil if it is not a macro."
320 (while (and (symbolp object
) (fboundp object
))
321 (setq object
(symbol-function object
)))
322 (if (and (listp object
)
323 (eq 'macro
(car object
))
324 (edebug-functionp (cdr object
)))
327 (defun edebug-functionp (object)
328 "Returns the function named by OBJECT, or nil if it is not a function."
329 (while (and (symbolp object
) (fboundp object
))
330 (setq object
(symbol-function object
)))
331 (if (or (subrp object
)
333 (eq (car object
) 'lambda
)
334 (listp (car (cdr object
)))))
337 (defun edebug-sort-alist (alist function
)
338 "Return the ALIST sorted with comparison function FUNCTION.
339 This uses 'sort so the sorting is destructive."
340 (sort alist
(function
342 (funcall function
(car e1
) (car e2
))))))
344 (put 'edebug-save-restriction
'edebug-form-hook
347 (defmacro edebug-save-restriction
(&rest body
)
348 "Evaluate BODY while saving the current buffers restriction.
349 BODY may change buffer outside of current restriction, unlike
350 save-restriction. BODY may change the current buffer,
351 and the restriction will be restored to the original buffer,
352 and the current buffer remains current.
353 Return the result of the last expression in BODY."
354 (` (let ((edebug:s-r-beg
(point-min-marker))
355 (edebug:s-r-end
(point-max-marker)))
359 (set-buffer (marker-buffer edebug
:s-r-beg
))
360 (narrow-to-region edebug
:s-r-beg edebug
:s-r-end
))))))
363 ;;;=============================================================
364 ;;; Redefine eval-defun, eval-region, and eval-current-buffer.
365 ;;; -----------------------------------------------------------
367 (defun edebug-all-defuns ()
368 "Toggle edebugging of all defuns and defmacros,
369 not including those evaluated in the minibuffer, or during load."
371 (setq edebug-all-defuns
(not edebug-all-defuns
))
372 (message "Edebugging is %s." (if edebug-all-defuns
"on" "off")))
375 (if (not (fboundp 'edebug-emacs-eval-defun
))
376 (fset 'edebug-emacs-eval-defun
(symbol-function 'eval-defun
)))
377 ;;(fset 'eval-defun (symbol-function 'edebug-emacs-eval-defun))
379 (defun eval-defun (edebug-debug)
380 "Edebug replacement for eval-defun. Print value in the minibuffer.
381 Evaluate the top-level form that point is in or before. Note:
382 eval-defun normally evaluates any top-level form, not just defuns.
384 Here are the differences from the standard eval-defun. If the prefix
385 argument is the same as edebug-all-defuns (nil or non-nil), evaluate
386 normally; otherwise edebug-defun is called to wrap edebug calls around
387 evaluatable expressions in the defun or defmacro body. Also, the
388 value printed by edebug-defun is not just the function name."
390 (let ((edebug-all-defuns
391 (not (eq (not edebug-debug
) (not edebug-all-defuns
)))))
392 (edebug-emacs-eval-defun nil)
396 (if (not (fboundp 'edebug-emacs-eval-region
))
397 (fset 'edebug-emacs-eval-region
(symbol-function 'eval-region
)))
398 ;; (fset 'eval-region (symbol-function 'edebug-emacs-eval-region))
400 (defun eval-region (edebug-e-r-start edebug-e-r-end
401 &optional edebug-e-r-output
)
402 "Edebug replacement for eval-defun.
403 Like eval-region, but call edebug-defun for defuns or defmacros.
404 Also, this eval-region does not narrow to the region and
405 if an error occurs, point is left at the error."
406 ;; One other piddling difference concerns whitespace after the expression.
408 (let ((standard-output (or edebug-e-r-output
'symbolp
))
409 (edebug-e-r-pnt (point))
410 (edebug-e-r-buf (current-buffer))
411 (edebug-e-r-inside-buf (current-buffer))
412 ;; Mark the end because it may move.
413 (edebug-e-r-end-marker (set-marker (make-marker) edebug-e-r-end
))
416 (goto-char edebug-e-r-start
)
417 (edebug-skip-whitespace)
418 (while (< (point) edebug-e-r-end-marker
)
419 (if (and edebug-all-defuns
420 (eq 'lparen
(edebug-next-token-class))
422 (forward-char 1) ; skip \(
423 (memq (edebug-read-sexp) '(defun defmacro))))
426 ;; Potential problem: edebug-defun always prints name.
427 (forward-sexp 1) ; skip the defun
429 (if (and (eq 'lparen
(edebug-next-token-class))
431 (forward-char 1) ; skip \(
432 (memq (edebug-read-sexp) '(defun defmacro))))
433 ;; If it's a defun or defmacro, but not edebug-all-defuns
434 ;; reset the symbols edebug property to be just a marker at
435 ;; the definitions source code.
436 (put (edebug-which-function) 'edebug
(point-marker)))
438 ;; Evaluate normally - after restoring the current-buffer.
439 (setq edebug-e-r-val
(edebug-read-sexp))
441 (set-buffer edebug-e-r-inside-buf
)
442 (setq edebug-e-r-val
(eval edebug-e-r-val
))
443 ;; Remember current buffer for next time.
444 (setq edebug-e-r-inside-buf
(current-buffer)))
446 (if edebug-e-r-output
448 (setq values
(cons edebug-e-r-val values
))
449 (if (eq standard-output t
)
450 (prin1 edebug-e-r-val
)
451 (print edebug-e-r-val
))))
454 (min (max edebug-e-r-end-marker
(point))
455 (progn (edebug-skip-whitespace) (point))))
457 (if (null edebug-e-r-output
)
458 ;; do the save-excursion recovery
460 ;; but mark is not restored
461 (set-buffer edebug-e-r-buf
)
462 (goto-char edebug-e-r-pnt
)))
467 (defun edebug-eval-current-buffer (&optional edebug-e-c-b-output
)
468 "Call eval-region on the whole buffer."
470 (eval-region (point-min) (point-max) edebug-e-c-b-output
))
472 (defun edebug-eval-buffer (&optional buffer edebug-e-c-b-output
)
473 "Call eval-region on the whole buffer."
474 (interactive "bEval buffer: ")
477 (eval-region (point-min) (point-max) edebug-e-c-b-output
)))
479 ;; The standard eval-current-buffer doesn't use eval-region.
480 (if (and (fboundp 'eval-current-buffer
)
481 (not (fboundp 'edebug-emacs-eval-current-buffer
)))
483 (fset 'edebug-emacs-eval-current-buffer
484 (symbol-function 'eval-current-buffer
))
485 (fset 'eval-current-buffer
'edebug-eval-current-buffer
)))
486 (if (and (fboundp 'eval-buffer
)
487 (not (fboundp 'edebug-emacs-eval-buffer
)))
489 (fset 'edebug-emacs-eval-buffer
490 (symbol-function 'eval-buffer
))
491 (fset 'eval-buffer
'edebug-eval-buffer
)))
495 ;;;======================================================================
499 ;;; The top level function for parsing defuns is edebug-defun; it
500 ;;; calls all the rest. It checks the syntax a bit and leaves point
501 ;;; at any error it finds, but otherwise should appear to work like
504 ;;; The basic plan is to surround each expression with a call to the
505 ;;; function edebug together with indexes into a table of positions of
506 ;;; all expressions. Thus an expression "exp" in function foo
509 ;;; (edebug 1 2 'exp)
511 ;;; First point moved to to the beginning of exp (offset 1 of the
512 ;;; current function). Then the expression is evaluated and point is
513 ;;; moved to offset 2, at the end of exp.
515 ;;; The top level expressions of the function are wrapped in a call to
516 ;;; edebug-enter, which supplies the function name and the actual
517 ;;; arguments to the function. See functions edebug and edebug-enter
518 ;;; for more details.
522 (defun edebug-defun ()
523 "Evaluate defun or defmacro, like eval-defun, but with edebug calls.
524 Print its name in the minibuffer and leave point after any error it finds,
525 with mark at the original point."
527 (let (def-kind ; whether defmacro or defun
532 (edebug-offset-index 0)
535 (starting-point (point))
537 (parse-sexp-ignore-comments t
))
545 (setq edebug-func-mark
(point-marker))
546 (if (not (eq 'defun
(setq def-kind
(edebug-read-sexp))))
547 (if (not (eq 'defmacro def-kind
))
548 (edebug-syntax-error "%s is not a defun or defmacro."
550 (setq def-name
(edebug-read-sexp))
551 (if (not (symbolp def-name
))
552 (edebug-syntax-error "Bad defun name: %s" def-name
))
553 (setq def-args
(edebug-read-sexp))
554 (if (not (listp def-args
))
555 (edebug-syntax-error "Bad defun arg list: %s" def-args
))
557 ;; look for doc string
558 (setq tmp-point
(point))
559 (if (eq 'string
(edebug-next-token-class))
561 (setq def-docstring
(edebug-read-sexp))
562 (setq tmp-point
(point))))
564 ;; look for interactive form
565 (if (eq 'lparen
(edebug-next-token-class))
567 (forward-char 1) ; skip \(
568 (if (eq 'interactive
(edebug-read-sexp))
570 (setq defun-interactive
571 (cons 'interactive
(edebug-interactive)))
572 (forward-char 1) ; skip \)
573 (setq tmp-point
(point))
576 (goto-char tmp-point
)
578 ;; build the new definition
579 (fset def-name
(` (lambda
582 (, defun-interactive
)
583 ;; the remainder is a list of sexps
588 (,@ (edebug-sexp-list t
)))))
590 ;; if it is a defmacro, prepend 'macro
591 (if (eq 'defmacro def-kind
)
592 (fset def-name
(cons 'macro
(symbol-function def-name
))))
594 ;; recover point, like save-excursion but only if no error occurs
595 (goto-char starting-point
)
597 ;; store the offset list in functions property list
598 (put def-name
'edebug
599 (list edebug-func-mark
600 nil
; clear breakpoints
601 (vconcat (nreverse edebug-offset-list
))))
602 (message "edebug: %s" def-name
)
606 ;; Set mark at starting-point so user can return.
607 ;; Leave point at error.
609 (goto-char starting-point
)
610 (set-mark-command nil
))
611 (message "Syntax error: %s" (cdr err
))
612 ;; (signal 'invalid-read-syntax (cdr err)) ; pass it on, to who?
619 (defun edebug-sexp-list (debuggable)
620 "Return an edebug form built from the sexp list following point in the
621 current buffer. If DEBUGGABLE then wrap edebug calls around each sexp.
622 The sexp list does not start with a left paren; we are already in the list.
623 Leave point at (before) the trailing right paren."
625 (while (not (eq 'rparen
(edebug-next-token-class)))
626 (setq sexp-list
(cons (if debuggable
630 (nreverse sexp-list
)))
633 (defun edebug-increment-offset ()
634 ;; accesses edebug-offset-index and edebug-offset-list
635 (setq edebug-offset-index
(1+ edebug-offset-index
))
636 (setq edebug-offset-list
(cons (- (point) edebug-func-mark
)
637 edebug-offset-list
)))
640 (defun edebug-make-edebug-form (index form
)
641 "Return the edebug form for the current function at offset INDEX given FORM.
642 Looks like: (edebug def-name INDEX edebug-offset-index 'FORM).
643 Also increment the offset index."
649 (edebug-increment-offset)
653 (defun edebug-form ()
654 "Return the debug form for the following form. Add the point offset
655 to the edebug-offset-list for the function and move point to
656 immediately after the form."
657 (let* ((index edebug-offset-index
)
659 ;; The point must be added to the offset list now
660 ;; because edebug-list will add more offsets indirectly.
661 (edebug-skip-whitespace)
662 (edebug-increment-offset)
663 (setq class
(edebug-next-token-class))
666 (edebug-make-edebug-form index
(edebug-list)))
669 (if (and (not (memq (setq form
(edebug-read-sexp)) '(nil t
)))
670 ;; note: symbol includes numbers, see parsing utilities
671 (not (numberp form
)))
672 (edebug-make-edebug-form index form
)
674 (t (edebug-read-sexp)))))
677 (defun edebug-list ()
678 "Return an edebug form built from the list form that follows point.
679 Insert debug calls as appropriate to the form. Start with point at
680 the left paren. Leave point after the right paren."
681 (let ((beginning (point))
685 (forward-char 1) ; skip \(
686 (setq class
(edebug-next-token-class))
689 (setq head
(edebug-read-sexp)))
691 (setq head
(edebug-anonymous)))
694 (t (edebug-syntax-error
695 "Head of list must be a symbol or lambda expression.")))
702 ;; None of the edebug-form-hooks defined below are used, for speed.
703 ;; They are included for documentation, though the hook would not
704 ;; necessarily behave the same as the function it is replacing.
706 ;;; Using the edebug-form-hooks should work, but would take more time.
708 ;;; (let ((form (get head 'edebug-form-hook)))
710 ;;; (edebug-form-parser form)
711 ;;; (if (edebug-macrop head)
712 ;;; (if edebug-eval-macro-args
713 ;;; (edebug-sexp-list t)
714 ;;; (edebug-sexp-list nil))
715 ;;; ;; assume it is a function
716 ;;; (edebug-sexp-list t)))))
718 ;; handle all special-forms with unevaluated arguments
719 ((memq head
'(let let
*)) (edebug-let))
720 ((memq head
'(setq setq-default
)) (edebug-setq))
721 ((eq head
'cond
) (edebug-cond))
722 ((eq head
'condition-case
) (edebug-condition-case))
724 ((memq head
'(quote ; permits more than one arg
725 defun
defvar defconst defmacro
))
726 (edebug-sexp-list nil
))
729 (if (eq 'lparen
(edebug-next-token-class))
731 (edebug-read-sexp) ; should be just a symbol
734 ;; is it a lisp macro?
735 ((edebug-macrop head
)
736 (or (and (symbolp head
)
737 (let ((form (get head
'edebug-form-hook
)))
742 (edebug-sexp-list nil
)
743 (edebug-form-parser form
))))))
744 (edebug-sexp-list edebug-eval-macro-args
)))
746 ((eq head
'interactive
)
747 (edebug-syntax-error "interactive not expected here."))
749 ;; otherwise it is a function call
750 (t (edebug-sexp-list t
))
753 (if (eq 'rparen
(edebug-next-token-class))
754 (forward-char 1) ; skip \)
755 (edebug-syntax-error "Too many arguments."))
759 (defun edebug-form-parser (args)
760 "Parse the macro arguments that follow based on ARGS.
761 ARGS describes the types of the arguments of a list form. Each of the ARGS
762 is processed left to right, in the same order as the arguments of the
763 list form. See the edebug documentation for more details. The ARGS
764 may be one of the following:
766 symbolp - an unevaluated symbol
767 integerp - an unevaluated number
768 stringp - an unevaluated string
769 vectorp - an unevaluated vector
770 atom - an unevaluated number, string, symbol, or vector
772 sexp - an unevaluated sexp (atom or list); may not be empty
773 form - an evaluated sexp; may not be empty
775 foo - any other symbol should be the name of a function; this
776 function is called on the argument as a predicate and an error
777 is signaled if the predicate fails.
779 &optional - one following arg in the list may or may not appear.
780 &rest - all following args are repeated zero or more times as a group.
781 This is an extension of the normal meaning of &rest.
782 &or - each of the following args are alternatives, processed left to
783 right until one succeeds. There is no way to group
784 more than one list element as one alternative.
786 (...) - a sublist, of the same format as the top level, processed recursively.
787 Special case: if the car of the list is quote, the argument must match
788 the quoted sexp (see example below of 'for macro).
792 arg form form-list class
795 (not (eq 'rparen
(setq class
(edebug-next-token-class)))))
797 (setq arg
(car arglist
))
798 (setq arglist
(cdr arglist
))
799 (if (and &rest
(null arglist
))
800 (setq arglist
&rest
))
803 ((memq arg
'(&optional
&rest
&or
))
804 ;; remember arglist at this point
806 (throw 'no-match nil
))
809 (setq form
(edebug-form)))
812 (setq form
(edebug-read-sexp)))
815 (if (eq 'quote
(car arg
))
816 ;; special case, match the quoted symbol
818 (setq arg
(car (cdr arg
)))
819 (if (not (eq arg
(setq form
(edebug-read-sexp))))
820 (edebug-form-parser-error)
822 (if (eq class
'lparen
)
824 (forward-char 1) ; skip \(
825 (setq form
(edebug-form-parser arg
))
826 (forward-char 1) ; skip \)
830 (pred (if (fboundp arg
) (symbol-function arg
))))
832 (not (funcall pred
(setq form
(edebug-read-sexp))))
833 (edebug-form-parser-error)
835 (t (throw 'no-match nil
))
837 (setq &optional nil
) ; only lasts for one match
838 (setq form-list
(cons form form-list
)) ; skipped by no-match throw
841 (if (and arglist
(not (or &optional
&rest
842 (memq (car arglist
) '(&optional
&rest
)))))
843 (edebug-syntax-error "Not enough arguments."))
844 (if (not (eq 'rparen
(edebug-next-token-class)))
846 (edebug-syntax-error "Unrecognized argument.")
847 (edebug-syntax-error "Too many arguments.")))
848 (nreverse form-list
)))
851 (defun edebug-form-parser-error ()
854 (throw 'no-match nil
)
857 (setq &optional nil
) ; only lasts for one failed match not in &or
858 (throw 'no-match nil
))
859 (edebug-syntax-error "%s is not %s" form arg
))))
861 ;; for loop defined in Emacs Lisp manual
862 (put 'for
'edebug-form-hook
863 '(symbolp 'from form
'to form
'do
&rest form
))
865 ;; case and do defined in cl.el
866 (put 'case
'edebug-form-hook
867 '(form &rest
(sexp form
)))
869 (put 'do
'edebug-form-hook
872 (symbolp &optional form
877 (put 'defvar
'edebug-form-hook
878 (put 'defconst
'edebug-form-hook
879 '(symbolp &optional form
&optional stringp
)))
881 (put 'defun
'edebug-form-hook
882 (put 'defmacro
'edebug-form-hook
883 '(symbolp (&rest symbolp
)
885 &optional
('interactive
&or stringp form
)
888 (put 'anonymous
'edebug-form-hook
889 '(&optional
'macro
'lambda
(&rest symbolp
) &rest form
))
891 (defun edebug-anonymous ()
892 "Return the edebug form for an anonymous lambda or macro.
893 Point starts before the left paren and ends after it."
894 (forward-char 1) ; skip \(
896 (let ((head (edebug-read-sexp)))
901 (if (not (eq 'lambda
(edebug-read-sexp)))
902 (edebug-syntax-error "lambda expected."))
903 (cons 'macro
(edebug-lambda)))
904 (t (edebug-syntax-error "Anonymous lambda or macro expected."))))
905 (forward-char 1) ; skip \)
909 (defun edebug-lambda ()
910 "Return the edebug form for the lambda form that follows.
911 Point starts after the lambda symbol and is moved to before the right paren."
913 (list 'lambda
(edebug-read-sexp)) ; the args
914 (edebug-sexp-list t
))) ; the body
918 (put 'let
'edebug-form-hook
919 (put 'let
* 'edebug-form-hook
921 &or
(symbolp &optional form
)
926 "Return the edebug form of the let or let* form.
927 Leave point before the right paren."
932 ;; first process the var/value list
933 (if (not (eq 'lparen
(edebug-next-token-class)))
934 (if (setq token
(edebug-read-sexp))
935 (edebug-syntax-error "Bad var list in let.") ; should be nil
939 (forward-char 1) ; lparen
940 (while (not (eq 'rparen
(setq class
(edebug-next-token-class))))
943 (if (not (eq 'lparen class
))
945 (forward-char 1) ; lparen
948 (if (not (eq 'rparen
(edebug-next-token-class)))
949 (edebug-syntax-error "Right paren expected in let.")
950 (forward-char 1) ; rparen
953 (forward-char 1) ; rparen
954 (nreverse var-value-list
))
956 ;; now process the expression list
957 (edebug-sexp-list t
))))
960 (defun edebug-var-value ()
961 "Return the edebug form of the var and optional value that follow point.
962 Leave point after the value, if there is one."
964 (edebug-read-sexp) ; the variable
965 (and (not (eq 'rparen
(edebug-next-token-class)))
969 (put 'setq
'edebug-form-hook
970 (put 'setq-default
'edebug-form-hook
971 '(&rest symbolp form
)))
973 (defun edebug-setq ()
974 "Return the edebug form of the setq or setq-default var-value list."
975 (let (var-value-list)
976 (while (not (eq 'rparen
(edebug-next-token-class)))
978 (append var-value-list
979 (edebug-var-value))))
983 (put 'interactive
'edebug-form-hook
984 '(&optional
&or stringp form
))
986 (defun edebug-interactive ()
987 "Return the edebug form of the interactive form."
989 (if (not (eq 'rparen
(edebug-next-token-class)))
990 (if (eq 'string
(edebug-next-token-class))
993 (` (edebug-interactive-entry
995 (quote ((,@ (edebug-form))))))
996 (if (not (eq 'rparen
(edebug-next-token-class)))
998 "Only first expression used in interactive form.")))))))
1001 (put 'cond
'edebug-form-hook
1002 '(&rest
(form &rest form
)))
1004 (defun edebug-cond ()
1005 "Return the edebug form of the cond form."
1006 (let (value-value-list
1008 (while (not (eq 'rparen
(setq class
(edebug-next-token-class))))
1009 (setq value-value-list
1011 (if (not (eq 'lparen class
))
1012 (let ((thing (edebug-read-sexp)))
1014 (edebug-syntax-error "Condition expected in cond")
1016 (forward-char 1) ; \(
1020 (if (eq 'rparen
(edebug-next-token-class))
1022 (edebug-sexp-list t
)))
1023 (if (not (eq 'rparen
(edebug-next-token-class)))
1024 (edebug-syntax-error "Right paren expected in cond"))
1025 (forward-char 1) ; \)
1028 (nreverse value-value-list
)))
1031 ;; Bug: this doesnt support condition name lists
1032 (put 'condition-case
'edebug-form-hook
1035 &rest
(symbolp &optional form
)))
1037 (defun edebug-condition-case ()
1038 "Return the edebug form of the condition-case form."
1041 ;; read the variable or nil
1042 (setq token
(edebug-read-sexp))
1043 (if (not (symbolp token
))
1044 (edebug-syntax-error
1045 "Variable or nil required for condition-case; found: %s" token
))
1049 (edebug-form) ; the form
1052 (let (symb-sexp-list
1054 (while (not (eq 'rparen
(setq class
(edebug-next-token-class))))
1055 (setq symb-sexp-list
1057 (if (not (eq 'lparen class
))
1058 (edebug-syntax-error "Bad handler in condition-case.")
1059 (forward-char 1) ; \(
1062 (edebug-read-sexp) ; the error-condition
1063 (and (not (eq 'rparen
(edebug-next-token-class)))
1064 (edebug-sexp-list t
)))
1065 (forward-char 1) ; \)
1068 (nreverse symb-sexp-list
)))))
1072 ;;------------------------------------------------
1075 (defun edebug-syntax-error (msg &rest args
)
1076 "Signal an invalid-read-syntax with MSG and ARGS.
1077 This is caught by edebug-defun."
1078 (signal 'invalid-read-syntax
(apply 'format msg args
)))
1081 (defun edebug-skip-whitespace ()
1082 "Leave point before the next token, skipping white space and comments."
1083 (skip-chars-forward " \t\r\n\f")
1084 (while (= (following-char) ?\
;)
1085 (skip-chars-forward "^\n\r") ; skip the comment
1086 (skip-chars-forward " \t\r\n\f")))
1088 (defun edebug-read-sexp ()
1089 "Read one sexp from the current buffer starting at point.
1090 Leave point immediately after it. A sexp can be a list or atom.
1091 An atom is a symbol (or number), character, string, or vector."
1092 ;; This is gummed up by parser inconsistencies (bugs?)
1094 (edebug-skip-whitespace)
1095 (if (or (= (following-char) ?\
[) (= (following-char) ??
))
1096 ;; scan-sexps doesn't read vectors or character literals correctly,
1098 (setq token
(read (current-buffer)))
1100 (min ; use the lesser of the read and scan-sexps motion
1101 ;; read goes one too far if (quoted) string or symbol
1102 ;; is immediately followed by non-whitespace
1104 (setq token
(read (current-buffer)))
1106 ;; scan-sexps reads too far if a quoting character is read
1107 (scan-sexps (point) 1))))
1110 (defconst edebug-syntax-table
1111 (let ((table (make-vector 256 'symbol
)))
1112 ;; Treat numbers as symbols, because of confusion with -, -1, and 1-.
1113 (aset table ?\
( 'lparen
)
1114 (aset table ?\
) 'rparen
)
1115 (aset table ?
\' 'quote
)
1116 (aset table ?
\" 'string
)
1117 (aset table ?
\? 'char
)
1118 (aset table ?\
[ 'vector
)
1119 (aset table ?\.
'dot
)
1120 ;; We dont care about any other chars since they wont be seen.
1122 "Lookup table for the token class of each character.")
1124 (defun edebug-next-token-class ()
1125 "Move to the next token and return its class. We only care about
1126 lparen, rparen, dot, quote, string, char, vector, or symbol."
1127 (edebug-skip-whitespace)
1128 (aref edebug-syntax-table
(following-char)))
1131 ;;;=================================================================
1132 ;;; The debugger itself
1133 ;;; -------------------
1136 (defvar edebug-active nil
1137 "Non-nil when edebug is active")
1140 ;;; add minor-mode-alist entry
1141 (or (assq 'edebug-active minor-mode-alist
)
1142 (setq minor-mode-alist
(cons (list 'edebug-active
" *Debugging*")
1145 (defvar edebug-backtrace nil
1146 "Stack of active functions evaluated via edebug.
1147 Should be nil at the top level.")
1149 (defvar edebug-offset-indices nil
; not used yet.
1150 "Stack of offset indices of visited edebug sexps.
1151 Should be nil at the top level.")
1153 (defvar edebug-entered nil
1154 "Non-nil if edebug has already been entered at this recursive edit level.")
1157 (defun edebug-enter (edebug-func edebug-args edebug-body
)
1158 "Entering FUNC. The arguments are ARGS, and the body is BODY.
1159 Setup edebug variables and evaluate BODY. This function is called
1160 when a function evaluated with edebug-defun is entered. Return the
1163 ;; Is this the first time we are entering edebug since
1164 ;; lower-level recursive-edit command?
1165 (if (and (not edebug-entered
)
1166 edebug-initial-mode
)
1167 ;; Reset edebug-mode to the initial mode.
1168 (setq edebug-mode edebug-initial-mode
))
1169 (let* ((edebug-entered t
)
1170 (edebug-data (get edebug-func
'edebug
))
1171 ;; pull out parts of the edebug-data
1172 (edebug-func-mark (car edebug-data
)) ; mark at function start
1174 (edebug-buffer (marker-buffer edebug-func-mark
))
1175 (edebug-backtrace (cons edebug-func edebug-backtrace
))
1176 (max-lisp-eval-depth (+ 6 max-lisp-eval-depth
)) ; too much??
1177 (max-specpdl-size (+ 10 max-specpdl-size
)) ; the args and these vars
1180 (let ((edebug-stack-depth (1- (length edebug-backtrace
)))
1182 (edebug-print-trace-entry
1183 "*edebug-trace*" edebug-func edebug-args edebug-stack-depth
)
1184 (setq edebug-result
(eval edebug-body
))
1185 (edebug-print-trace-exit
1186 "*edebug-trace*" edebug-func edebug-result edebug-stack-depth
)
1191 (defun edebug-interactive-entry (edebug-func edebug-args
)
1192 "Evaluating FUNCs non-string argument of interactive form ARGS."
1193 (if (and (not edebug-entered
)
1194 edebug-initial-mode
)
1195 ;; Reset edebug-mode to the initial mode.
1196 (setq edebug-mode edebug-initial-mode
))
1197 (let* ((edebug-entered t
)
1198 (edebug-data (get edebug-func
'edebug
))
1199 ;; pull out parts of the edebug-data
1200 (edebug-func-mark (car edebug-data
)) ; mark at function start
1202 (edebug-buffer (marker-buffer edebug-func-mark
))
1203 ;; (edebug-backtrace (cons edebug-func edebug-backtrace))
1205 (eval edebug-args
)))
1208 (defun edebug-print-trace-entry
1209 (edebug-stream edebug-function edebug-args edebug-stack-depth
)
1210 (edebug-trace-display
1212 "%sEnter: %s\n" (make-string edebug-stack-depth ?\
) edebug-function
)
1215 (defun edebug-print-trace-exit
1216 (edebug-stream edebug-function edebug-result edebug-stack-depth
)
1217 (edebug-trace-display
1219 "%sExit: %s\n" (make-string edebug-stack-depth ?\
) edebug-function
)
1223 (defun edebug (edebug-before-index edebug-after-index edebug-exp
)
1224 "Debug current function given BEFORE and AFTER positions around EXP.
1225 BEFORE and AFTER are indexes into the position offset vector in the
1226 functions 'edebug property. edebug is called from functions compiled
1228 (let ((max-lisp-eval-depth (+ 5 max-lisp-eval-depth
)) ; enough??
1229 (max-specpdl-size (+ 7 max-specpdl-size
)) ; the args and these vars
1230 (edebug-offset-indices
1231 (cons edebug-before-index edebug-offset-indices
))
1232 ;; Save the outside value of executing macro.
1233 (edebug-outside-executing-macro executing-macro
)
1234 ;; Don't keep reading from an executing kbd macro within edebug!
1235 (executing-macro nil
)
1237 (if (and (eq edebug-mode
'Go-nonstop
)
1238 (not (edebug-input-pending-p)))
1239 ;; Just return evalled expression.
1241 (edebug-debugger edebug-before-index
'enter edebug-exp
)
1242 (edebug-debugger edebug-after-index
'exit
(eval edebug-exp
))
1246 (defun edebug-debugger (edebug-offset-index edebug-arg-mode edebug-exp
)
1247 "Determine if edebug display should be updated."
1249 ;; This needs to be here since breakpoints may be changed.
1250 (edebug-breakpoints (car (cdr edebug-data
))) ; list of breakpoints
1251 (edebug-break-data (assq edebug-offset-index edebug-breakpoints
))
1253 (if edebug-break-data
1254 (let ((edebug-break-condition
1255 (car (cdr edebug-break-data
))))
1256 (or (not edebug-break-condition
)
1257 (eval edebug-break-condition
)))))
1259 (if (and edebug-break
1260 (car (cdr (cdr edebug-break-data
)))) ; is it temporary?
1261 ;; Delete the breakpoint.
1263 (cons (delq edebug-break-data edebug-breakpoints
)
1264 (cdr (cdr edebug-data
)))))
1266 ;; Dont do anything if mode is go, continue, or Continue-fast
1267 ;; and no break, and no input.
1268 (if (or (and (not (memq edebug-mode
'(go continue Continue-fast
)))
1269 (or edebug-stop-before-symbols
1270 (not (and (eq edebug-arg-mode
'enter
)
1271 (symbolp edebug-exp
)))))
1272 (edebug-input-pending-p)
1280 (defvar edebug-window-start
0
1281 "Remember where each buffers' window starts between edebug calls.
1282 This is to avoid spurious recentering.")
1284 (setq-default edebug-window-start
0)
1285 (make-variable-buffer-local 'edebug-window-start
)
1287 (defun edebug-display ()
1288 "Setup windows for edebug, determine mode, maybe enter recursive-edit."
1289 ;; uses local variables of edebug-enter, edebug, and edebug-debugger.
1290 (let ((edebug-active t
) ; for minor mode alist
1291 edebug-stop
; should we enter recursive-edit
1292 (edebug-point (+ edebug-func-mark
1293 (aref (car (cdr (cdr edebug-data
)))
1294 edebug-offset-index
)))
1295 (edebug-buffer-points
1296 (if edebug-save-buffer-points
(edebug-get-buffer-points)))
1297 edebug-window
; window displaying edebug-buffer
1298 edebug-inside-window
; window displayed after recursive edit
1299 (edebug-outside-window (selected-window))
1300 (edebug-outside-buffer (current-buffer))
1301 (edebug-outside-point (point))
1302 (edebug-outside-mark (mark))
1303 edebug-outside-windows
; window or screen configuration
1304 edebug-outside-edebug-point
; old point in edebug buffer
1305 edebug-outside-edebug-mark
1307 edebug-eval-buffer
; declared here so we can kill it below
1308 (edebug-eval-result-list (and edebug-eval-list
1309 (edebug-eval-result-list)))
1310 (edebug-outside-o-a-p overlay-arrow-position
)
1311 (edebug-outside-o-a-s overlay-arrow-string
)
1312 (edebug-outside-c-i-e-a cursor-in-echo-area
)
1314 edebug-outside-point-min
1315 edebug-outside-point-max
1317 overlay-arrow-position
1318 overlay-arrow-string
1319 (cursor-in-echo-area nil
)
1322 (if (not (buffer-name edebug-buffer
))
1323 (let (debug-on-error nil
)
1324 (error "Buffer defining %s not found." edebug-func
)))
1326 ;; Save windows now before we modify them.
1327 (if edebug-save-windows
1328 (setq edebug-outside-windows
1329 (edebug-current-window-configuration)))
1331 ;; If edebug-buffer is not currently displayed,
1332 ;; first find a window for it.
1333 (edebug-pop-to-buffer edebug-buffer
)
1334 (setq edebug-window
(selected-window))
1336 ;; Now display eval list, if any.
1337 ;; This is done after the pop to edebug-buffer
1338 ;; so that buffer-window correspondence is correct after quit.
1339 (edebug-eval-display edebug-eval-result-list
)
1340 (select-window edebug-window
)
1342 (if edebug-save-point
1344 (setq edebug-outside-edebug-point
(point))
1345 (setq edebug-outside-edebug-mark
(mark))))
1347 (edebug-save-restriction
1348 (setq edebug-outside-point-min
(point-min))
1349 (setq edebug-outside-point-max
(point-max))
1351 (goto-char edebug-point
)
1353 (setq edebug-window-start
1354 (edebug-adjust-window edebug-window-start
))
1356 (if (edebug-input-pending-p) ; not including keyboard macros
1358 (setq edebug-mode
'step
)
1359 (setq edebug-stop t
)
1361 ;; (discard-input) ; is this unfriendly??
1363 (edebug-overlay-arrow)
1366 ((eq 'exit edebug-arg-mode
)
1367 ;; Display result of previous evaluation.
1368 (setq edebug-previous-result edebug-exp
)
1369 (edebug-previous-result))
1371 ((eq 'error edebug-arg-mode
)
1372 ;; Display error message
1374 (if (eq 'quit
(car edebug-exp
))
1377 (get (car edebug-exp
) 'error-message
)
1378 (car (cdr edebug-exp
)))))
1385 (if (not (memq edebug-mode
'(continue Continue-fast
)))
1386 (setq edebug-stop t
)
1387 (if (eq edebug-mode
'continue
)
1389 (edebug-sit-for 0)))
1391 (if (eq edebug-mode
'trace
)
1392 (edebug-sit-for 1) ; Force update and pause.
1393 (if (eq edebug-mode
'Trace-fast
)
1394 (edebug-sit-for 0) ; Force update and continue.
1399 (eq edebug-mode
'step
)
1400 (eq edebug-arg-mode
'error
))
1402 (setq edebug-mode
'step
)
1403 (edebug-overlay-arrow) ; this doesnt always show up.
1404 (edebug-recursive-edit));; <<<<<< Recursive edit
1407 (if edebug-save-buffer-points
1408 (edebug-set-buffer-points))
1409 ;; Since we may be in a save-excursion, in case of quit
1410 ;; restore the outside window only.
1411 (select-window edebug-outside-window
)
1414 ;; None of the following is done if quit or signal occurs.
1415 (if edebug-save-point
1416 ;; Restore point and mark in edebug-buffer.
1417 ;; This does the save-excursion recovery only if no quit.
1418 ;; If edebug-buffer == edebug-outside-buffer,
1419 ;; then this is redundant with outside save-excursion.
1421 (set-buffer edebug-buffer
)
1422 (goto-char edebug-outside-edebug-point
)
1424 (set-marker (mark-marker) edebug-outside-edebug-mark
))
1426 ) ; edebug-save-restriction
1428 ;; Restore windows, buffer, point, and mark.
1429 (if edebug-save-windows
1430 ;; Restore windows before continuing.
1431 (edebug-set-window-configuration edebug-outside-windows
))
1432 (set-buffer edebug-outside-buffer
)
1433 (goto-char edebug-outside-point
)
1435 (set-marker (mark-marker) edebug-outside-mark
))
1436 ;; The following is not sufficient, and sometimes annoying.
1437 ;; (if (memq edebug-mode '(go Go-nonstop))
1438 ;; (edebug-sit-for 0))
1442 (defvar edebug-depth
0
1443 "Number of recursive edits started by edebug.
1444 Should be 0 at the top level.")
1446 (defvar edebug-recursion-depth
0
1447 "Value of recursion-depth when edebug was called.")
1450 (defun edebug-recursive-edit ()
1451 "Start up a recursive edit inside of edebug."
1452 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
1453 (let ((edebug-buffer-read-only buffer-read-only
)
1454 ;; match-data must be done in the outside buffer
1455 (edebug-outside-match-data
1457 (set-buffer edebug-outside-buffer
)
1460 (edebug-depth (1+ edebug-depth
))
1461 (edebug-recursion-depth (recursion-depth))
1462 edebug-entered
; bind locally to nil
1463 edebug-backtrace-buffer
; each recursive edit gets its own
1464 ;; The window configuration may be saved and restored
1465 ;; during a recursive-edit
1466 edebug-inside-windows
1468 (edebug-outside-map (current-local-map))
1469 (edebug-outside-standard-output standard-output
)
1470 (edebug-outside-standard-input standard-input
)
1472 (edebug-outside-last-command-char last-command-char
)
1473 (edebug-outside-last-command last-command
)
1474 (edebug-outside-this-command this-command
)
1475 (edebug-outside-last-input-char last-input-char
)
1476 ;; (edebug-outside-unread-command-char unread-command-char)
1478 ;; Declare the following local variables to protect global values.
1479 ;; We could set these to the values for previous edebug call.
1480 ;; But instead make it local, but use global value.
1481 (last-command-char last-command-char
)
1482 (last-command last-command
)
1483 (this-command this-command
)
1484 (last-input-char last-input-char
)
1485 ;; Assume no edebug command sets unread-command-char.
1486 ;; (unread-command-char -1)
1488 (debug-on-error debug-on-error
)
1493 (if (and (eq edebug-mode
'go
)
1494 (not (memq edebug-arg-mode
'(exit error
))))
1497 (if (boundp 'edebug-outside-debug-on-error
)
1498 (setq debug-on-error edebug-outside-debug-on-error
))
1500 (setq buffer-read-only t
)
1502 (recursive-edit) ; <<<<<<<<<< Recursive edit
1504 ;; Do the following, even if quit occurs.
1505 (if edebug-backtrace-buffer
1506 (kill-buffer edebug-backtrace-buffer
))
1507 ;; Could be an option to keep eval display up.
1508 (if edebug-eval-buffer
(kill-buffer edebug-eval-buffer
))
1510 ;; Remember selected-window after recursive-edit.
1511 (setq edebug-inside-window
(selected-window))
1513 (store-match-data edebug-outside-match-data
)
1515 ;; Recursive edit may have changed buffers,
1516 ;; so set it back before exiting let.
1517 (if (buffer-name edebug-buffer
) ; if it still exists
1519 (set-buffer edebug-buffer
)
1520 (if (memq edebug-mode
'(go Go-nonstop
))
1521 (edebug-overlay-arrow))
1522 (setq buffer-read-only edebug-buffer-read-only
)
1523 (use-local-map edebug-outside-map
)
1524 ;; Remember current window-start for next visit.
1525 (select-window edebug-window
)
1526 (if (eq edebug-buffer
(window-buffer edebug-window
))
1527 (setq edebug-window-start
(window-start)))
1528 (select-window edebug-inside-window
)
1533 ;;--------------------------
1534 ;; Display related functions
1536 (defun edebug-adjust-window (old-start)
1537 "Adjust window to fit as much as possible following point.
1538 The display should prefer to start at OLD-START if point is not visible.
1539 Return the new window-start."
1540 (if (not (pos-visible-in-window-p))
1542 (set-window-start (selected-window) old-start
)
1543 (if (not (pos-visible-in-window-p))
1544 (let ((start (window-start))
1550 (if (< pnt start
) -
1 ; one line before
1551 (- (/ (window-height) 2)) ; center the line
1558 (defconst edebug-arrow-alist
1559 '((Continue-fast .
">")
1565 (Go-nonstop .
"..") ; not used
1567 "Association list of arrows for each edebug mode.
1568 If you come up with arrows that make more sense, let me know.")
1570 (defun edebug-overlay-arrow ()
1571 "Set up the overlay arrow at beginning-of-line in current buffer.
1572 The arrow string is derived from edebug-arrow-alist and edebug-mode."
1577 (setq overlay-arrow-string
1578 (cdr (assq edebug-mode edebug-arrow-alist
)))
1579 (setq overlay-arrow-position
(make-marker))
1580 (set-marker overlay-arrow-position pos
(current-buffer))))
1583 (put 'edebug-outside-excursion
'edebug-form-hook
1586 (defmacro edebug-outside-excursion
(&rest body
)
1587 "Evaluate an expression list in the outside context.
1588 Return the result of the last expression."
1589 (` (save-excursion ; of current-buffer
1590 (if edebug-save-windows
1592 ;; After excursion, we will
1593 ;; restore to current window configuration.
1594 (setq edebug-inside-windows
1595 (edebug-current-window-configuration))
1596 ;; Restore outside windows.
1597 (edebug-set-window-configuration edebug-outside-windows
)))
1599 (set-buffer edebug-buffer
)
1600 ;; Restore outside context.
1601 (let ((edebug-inside-map (current-local-map))
1602 (last-command-char edebug-outside-last-command-char
)
1603 (last-command edebug-outside-last-command
)
1604 (this-command edebug-outside-this-command
)
1605 ;; (unread-command-char edebug-outside-unread-command-char)
1606 (last-input-char edebug-outside-last-input-char
)
1607 (overlay-arrow-position edebug-outside-o-a-p
)
1608 (overlay-arrow-string edebug-outside-o-a-s
)
1609 (cursor-in-echo-area edebug-outside-c-i-e-a
)
1610 (standard-output edebug-outside-standard-output
)
1611 (standard-input edebug-outside-standard-input
)
1612 (executing-macro edebug-outside-executing-macro
)
1616 (narrow-to-region edebug-outside-point-min
1617 edebug-outside-point-max
)
1618 (save-excursion ; of edebug-buffer
1619 (if edebug-save-point
1621 (goto-char edebug-outside-edebug-point
)
1623 (set-marker (mark-marker)
1624 edebug-outside-edebug-mark
))
1626 (use-local-map edebug-outside-map
)
1627 (store-match-data edebug-outside-match-data
)
1628 (select-window edebug-outside-window
)
1629 (set-buffer edebug-outside-buffer
)
1630 (goto-char edebug-outside-point
)
1633 ) ; save-restriction
1634 ;; Back to edebug-buffer. Restore rest of inside context.
1635 (use-local-map edebug-inside-map
)
1636 (if edebug-save-windows
1637 ;; Restore inside windows.
1638 (edebug-set-window-configuration edebug-inside-windows
))
1643 (defun edebug-toggle-save-windows ()
1644 "Toggle the edebug-save-windows variable.
1645 Each time you toggle it, the inside and outside window configurations
1646 become the same as the current configuration."
1648 (if (setq edebug-save-windows
(not edebug-save-windows
))
1649 (setq edebug-inside-windows
1650 (setq edebug-outside-windows
1651 (edebug-current-window-configuration))))
1652 (message "Window saving is %s."
1653 (if edebug-save-windows
"on" "off")))
1656 (defun edebug-where ()
1657 "Show the debug windows and where we stopped in the program."
1659 (if (not edebug-active
)
1660 (error "edebug is not active."))
1661 (edebug-pop-to-buffer edebug-buffer
)
1662 (goto-char edebug-point
) ; from edebug
1665 (defun edebug-view-outside ()
1666 "Change to the outside window configuration."
1668 (if (not edebug-active
)
1669 (error "edebug is not active."))
1670 (setq edebug-inside-windows
(edebug-current-window-configuration))
1671 (edebug-set-window-configuration edebug-outside-windows
)
1672 (goto-char edebug-outside-point
)
1673 (message "Window configuration outside of edebug. Return with %s"
1674 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
1677 (defun edebug-bounce-point ()
1678 "Bounce the point in the outside current buffer."
1680 (if (not edebug-active
)
1681 (error "edebug is not active."))
1683 ;; If the buffer's currently displayed, avoid the set-window-configuration.
1684 (save-window-excursion
1685 (edebug-pop-to-buffer edebug-outside-buffer
)
1686 ;; (edebug-sit-for 1) ; this shouldnt be necessary
1687 (goto-char edebug-outside-point
)
1688 ;; (message "current buffer: %s" (current-buffer))
1690 (edebug-pop-to-buffer edebug-buffer
))))
1694 ;;--------------------------
1695 ;; epoch related things
1697 (defvar edebug-epoch-running
(and (boundp 'epoch
::version
) epoch
::version
)
1698 "non-nil if epoch is running.
1699 Windows are handled a little differently under epoch.")
1702 (defun edebug-current-window-configuration ()
1703 "Return the current window or frame configuration."
1704 (if edebug-epoch-running
1705 (edebug-current-screen-configuration)
1706 (current-window-configuration)))
1709 (defun edebug-set-window-configuration (conf)
1710 "Set the window or frame configuration to CONF."
1711 (if edebug-epoch-running
1712 (edebug-set-screen-configuration conf
)
1713 (set-window-configuration conf
)))
1716 (defun edebug-get-buffer-window (buffer)
1717 (if edebug-epoch-running
1718 (epoch::get-buffer-window buffer
)
1719 (get-buffer-window buffer
)))
1722 (defun edebug-pop-to-buffer (buffer)
1723 "Like pop-to-buffer, but select a frame that buffer was shown in."
1724 (let ((edebug-window (edebug-get-buffer-window buffer
)))
1726 (select-window edebug-window
)
1727 ;; It is not currently displayed, so find some place to display it.
1728 (if edebug-epoch-running
1729 ;; Select a screen that the buffer has been displayed in before
1730 ;; or the current screen otherwise.
1732 ;; allowed-screens in epoch 3.2, was called screens before that
1733 (or (car (symbol-buffer-value 'allowed-screens buffer
))
1734 (epoch::current-screen
))))
1737 (select-window (next-window))
1738 (set-window-buffer (selected-window) buffer
)
1739 (set-window-hscroll (selected-window) 0)
1741 ;; Selecting the window does not set the buffer.
1746 (defun edebug-current-screen-configuration ()
1747 "Return an object recording the current configuration of Epoch screen-list.
1748 The object is a list of pairs of the form (SCREEN . CONFIGURATION)
1749 where SCREEN has window-configuration CONFIGURATION. The current
1750 screen is the head of the list."
1751 (let ((screen-list (epoch::screen-list
'unmapped
))
1752 (current-screen (epoch::get-screen
))
1753 (current-buffer (current-buffer))
1755 ;; put current screen first
1756 (setq screen-list
(cons current-screen
(delq current-screen screen-list
)))
1762 (epoch::select-screen screen
)
1763 (current-window-configuration)))))
1765 (epoch::select-screen current-screen
)
1766 (set-buffer current-buffer
)
1769 (defun edebug-set-screen-configuration (sc)
1770 "Set the window-configuration for all the screens in SC.
1771 Set the current screen to be the head of SC."
1773 (lambda (screen-conf)
1774 (if (epoch::screen-p
(car screen-conf
)) ; still exist?
1776 (epoch::select-screen
(car screen-conf
))
1777 (set-window-configuration (cdr screen-conf
))))))
1779 (if (epoch::screen-p
(car (car sc
)))
1780 (epoch::select-screen
(car (car sc
))))
1784 (defun edebug-sit-for (arg)
1785 (if edebug-epoch-running
1786 (epoch::dispatch-events
))
1790 (defun edebug-input-pending-p ()
1791 (if edebug-epoch-running
1792 (epoch::dispatch-events
))
1798 ;;--------------------------
1799 ;; breakpoint related functions
1801 (defun edebug-find-stop-point ()
1802 "Return (function . index) of the nearest edebug stop point."
1803 (let* ((def-name (edebug-which-function))
1805 (or (get def-name
'edebug
)
1807 "%s must first be evaluated with edebug-defun." def-name
)))
1808 ;; pull out parts of edebug-data.
1809 (edebug-func-mark (car edebug-data
))
1810 (edebug-breakpoints (car (cdr edebug-data
)))
1812 (offset-vector (car (cdr (cdr edebug-data
))))
1813 (offset (- (save-excursion
1814 (if (looking-at "[ \t]")
1815 ;; skip backwards until non-whitespace, or bol
1816 (skip-chars-backward " \t"))
1820 ;; the offsets are in order so we can do a linear search
1821 (setq len
(length offset-vector
))
1823 (while (and (< i len
) (> offset
(aref offset-vector i
)))
1826 (<= offset
(aref offset-vector i
)))
1827 ;; return the relevant info
1829 (message "Point is not on an expression in %s."
1834 (defun edebug-next-breakpoint ()
1835 "Move point to the next breakpoint, or first if none past point."
1837 (let ((edebug-stop-point (edebug-find-stop-point)))
1838 (if edebug-stop-point
1839 (let* ((def-name (car edebug-stop-point
))
1840 (index (cdr edebug-stop-point
))
1841 (edebug-data (get def-name
'edebug
))
1843 ;; pull out parts of edebug-data
1844 (edebug-func-mark (car edebug-data
))
1845 (edebug-breakpoints (car (cdr edebug-data
)))
1846 (offset-vector (car (cdr (cdr edebug-data
))))
1848 (if (not edebug-breakpoints
)
1849 (message "No breakpoints in this function.")
1850 (let ((breaks edebug-breakpoints
))
1852 (<= (car (car breaks
)) index
))
1853 (setq breaks
(cdr breaks
)))
1857 ;; goto the first breakpoint
1858 (car edebug-breakpoints
)))
1859 (goto-char (+ edebug-func-mark
1860 (aref offset-vector
(car breakpoint
))))
1862 (message (concat (if (car (cdr (cdr breakpoint
)))
1864 (if (car (cdr breakpoint
))
1865 (format "Condition: %s"
1867 (car (cdr breakpoint
))))
1872 (defun edebug-modify-breakpoint (flag &optional condition temporary
)
1873 "Modify the breakpoint for the form at point or after it according
1874 to FLAG: set if t, clear if nil. Then move to that point.
1875 If CONDITION or TEMPORARY are non-nil, add those attributes to
1877 (let ((edebug-stop-point (edebug-find-stop-point)))
1878 (if edebug-stop-point
1879 (let* ((def-name (car edebug-stop-point
))
1880 (index (cdr edebug-stop-point
))
1881 (edebug-data (get def-name
'edebug
))
1883 ;; pull out parts of edebug-data
1884 (edebug-func-mark (car edebug-data
))
1885 (edebug-breakpoints (car (cdr edebug-data
)))
1886 (offset-vector (car (cdr (cdr edebug-data
))))
1888 ;; delete it either way
1889 (setq present
(assq index edebug-breakpoints
))
1890 (setq edebug-breakpoints
(delq present edebug-breakpoints
))
1893 ;; add it to the list and resort
1894 (setq edebug-breakpoints
1897 (list index condition temporary
)
1898 edebug-breakpoints
) '<))
1899 (message "Breakpoint set in %s." def-name
))
1901 (message "Breakpoint unset in %s." def-name
)
1902 (message "No breakpoint here.")))
1905 (cons edebug-breakpoints
(cdr (cdr edebug-data
))))
1906 (goto-char (+ edebug-func-mark
(aref offset-vector index
)))
1909 (defun edebug-set-breakpoint (arg)
1910 "Set the breakpoint of nearest sexp.
1911 With prefix argument, make it a temporary breakpoint."
1913 (edebug-modify-breakpoint t nil arg
))
1915 (defun edebug-unset-breakpoint ()
1916 "Clear the breakpoint of nearest sexp."
1918 (edebug-modify-breakpoint nil
))
1920 (defun edebug-set-conditional-breakpoint (arg condition
)
1921 "Set a conditional breakpoint at nearest sexp.
1922 The condition is evaluated in the outside context.
1923 With prefix argument, make it a temporary breakpoint."
1924 (interactive "P\nxCondition: ")
1925 (edebug-modify-breakpoint t condition arg
))
1928 ;;--------------------------
1929 ;; Mode switching functions
1931 (defun edebug-set-mode (mode shortmsg msg
)
1932 "Set the edebug mode to MODE.
1933 Display SHORTMSG, or MSG if not within edebug."
1935 (setq edebug-mode mode
)
1936 (if (< 0 edebug-depth
)
1937 (if (eq (current-buffer) edebug-buffer
)
1940 (exit-recursive-edit)))
1944 (defun edebug-step-through ()
1945 "Proceed to next debug step."
1947 (edebug-set-mode 'step
"" "edebug will stop before next eval."))
1949 (defun edebug-go (arg)
1950 "Go, evaluating until break.
1951 With ARG set temporary break at stop point and go."
1954 (edebug-set-breakpoint t
))
1955 (edebug-set-mode 'go
"Go..." "edebug will go until break."))
1957 (defun edebug-Go-nonstop ()
1958 "Go, evaluating without debugging."
1960 (edebug-set-mode 'Go-nonstop
"Go-Nonstop..."
1961 "edebug will not stop at breaks."))
1963 (defun edebug-forward-sexp (arg)
1964 "Proceed from the current point to the end of the ARGth sexp ahead.
1965 If there are not ARG sexps ahead, then do edebug-step-out."
1968 (let ((parse-sexp-ignore-comments t
))
1969 ;; Call forward-sexp repeatedly until done or failure.
1976 (defun edebug-step-out ()
1977 "Proceed from the current point to the end of the containing sexp.
1978 If there is no containing sexp that is not the top level defun,
1979 go to the end of the last sexp, or if that is the same point, then step."
1982 (let ((parse-sexp-ignore-comments t
))
1985 ;; Is there still a containing expression?
1989 ;; At top level - 1, so first check if there are more sexps at this level.
1990 (let ((start-point (point)))
1993 (if (= (point) start-point
)
1994 (edebug-step-through) ; No more at this level, so step.
1999 (defun edebug-goto-here ()
2000 "Proceed to this stop point."
2005 (defun edebug-trace ()
2008 (edebug-set-mode 'trace
"Tracing..." "edebug will trace with pause."))
2010 (defun edebug-Trace-fast ()
2011 "Trace with no wait at each step."
2013 (edebug-set-mode 'Trace-fast
2014 "Trace fast..." "edebug will trace without pause."))
2016 (defun edebug-continue ()
2017 "Begin continue mode."
2019 (edebug-set-mode 'continue
"Continue..."
2020 "edebug will pause at breakpoints."))
2022 (defun edebug-Continue-fast ()
2023 "Trace with no wait at each step."
2025 (edebug-set-mode 'Continue-fast
"Continue fast..."
2026 "edebug will stop and go at breakpoints."))
2029 (defun edebug-step-in ()
2030 "Step into the function about to be called.
2031 Do this before the arguments are evaluated since otherwise it will be
2032 too late. One side effect of using edebug-step-in is that the next
2033 time the function is called, edebug will be called there as well."
2035 (if (not (eq 'enter edebug-arg-mode
))
2036 (error "You must be in front of a function or macro call."))
2037 (let* ((func (car edebug-exp
))
2038 (func-marker (get func
'edebug
)))
2040 ((markerp func-marker
)
2042 (set-buffer (marker-buffer func-marker
))
2043 (goto-char func-marker
)
2045 ((listp func-marker
)
2046 ;; its already been evaluated for edebug
2048 (t (error "You must first evaluate %s in a buffer." func
))))
2049 (exit-recursive-edit))
2052 ;;(defun edebug-exit-out ()
2053 ;; "Go until the current function exits."
2055 ;; (edebug-set-mode 'exiting "Exit..."))
2058 (defun edebug-stop ()
2059 "Useful for exiting from trace loop."
2064 ;;; The following initial mode setting definitions are not used yet.
2066 (defconst edebug-initial-mode-alist
2067 '((edebug-Continue-fast . Continue-fast
)
2068 (edebug-Trace-fast . Trace-fast
)
2069 (edebug-continue . continue
)
2070 (edebug-trace . trace
)
2072 (edebug-step-through . step
)
2073 (edebug-Go-nonstop . Go-nonstop
)
2075 "Association list between commands and the modes they set.")
2078 (defun edebug-set-initial-mode ()
2079 "Ask for the initial mode of the enclosing function.
2080 The mode is requested via the key that would be used to set the mode in
2083 (let* ((this-function (edebug-which-function))
2084 (keymap (if (eq edebug-mode-map
(current-local-map))
2086 (old-mode (or (get this-function
'edebug-initial-mode
)
2087 edebug-initial-mode
))
2088 (key (read-key-sequence
2090 "Change initial edebug mode for %s from %s (%s) to (enter key): "
2094 (car (rassq old-mode edebug-initial-mode-alist
))
2097 (mode (cdr (assq (key-binding key
) edebug-initial-mode-alist
)))
2100 (or (get this-function
'edebug-initial-mode
)
2101 (not (eq mode edebug-initial-mode
))))
2103 (put this-function
'edebug-initial-mode mode
)
2104 (message "Initial mode for %s is now: %s"
2105 this-function mode
))
2106 (error "Key must map to one of the mode changing commands.")
2111 ;;--------------------------
2112 ;; Evaluation of expressions
2114 (defvar edebug-previous-result nil
2115 "Last result returned from an expression.")
2117 (defun edebug-previous-result ()
2118 "Return the previous result."
2120 (let ((print-escape-newlines t
)
2122 (message "Result: %s" (prin1-to-string edebug-previous-result
))))
2125 (defun edebug-eval (expr)
2126 "Evaluate EXPR in the outside environment."
2127 (if (not edebug-active
)
2128 (error "edebug is not active."))
2129 (edebug-outside-excursion
2132 (defun edebug-eval-expression (expr)
2133 "Prompt and evaluate an expression in the outside environment.
2134 Print result in minibuffer."
2135 (interactive "xEval: ")
2136 (prin1 (edebug-eval expr
)))
2138 (defun edebug-eval-last-sexp ()
2139 "Evaluate sexp before point in the outside environment;
2140 print value in minibuffer."
2142 (prin1 (edebug-eval (edebug-last-sexp))))
2144 (defun edebug-eval-print-last-sexp ()
2145 "Evaluate sexp before point in the outside environment;
2146 print value into current buffer."
2148 (let ((standard-output (current-buffer)))
2151 (edebug-eval (edebug-last-sexp))
2152 (error (format "%s: %s"
2153 (get (car err
) 'error-message
)
2154 (car (cdr err
))))))))
2156 ;;;---------------------------------
2157 ;;; edebug minor mode initialization
2159 (defvar edebug-mode
'step
2160 "Current edebug mode set by user.")
2162 (defvar edebug-mode-map nil
)
2166 (setq edebug-mode-map
(copy-keymap emacs-lisp-mode-map
))
2168 (define-key edebug-mode-map
" " 'edebug-step-through
)
2169 (define-key edebug-mode-map
"g" 'edebug-go
)
2170 (define-key edebug-mode-map
"G" 'edebug-Go-nonstop
)
2171 (define-key edebug-mode-map
"t" 'edebug-trace
)
2172 (define-key edebug-mode-map
"T" 'edebug-Trace-fast
)
2173 (define-key edebug-mode-map
"c" 'edebug-continue
)
2174 (define-key edebug-mode-map
"C" 'edebug-Continue-fast
)
2176 (define-key edebug-mode-map
"f" 'edebug-forward-sexp
)
2177 (define-key edebug-mode-map
"h" 'edebug-goto-here
)
2179 (define-key edebug-mode-map
"r" 'edebug-previous-result
)
2181 (define-key edebug-mode-map
"i" 'edebug-step-in
)
2182 (define-key edebug-mode-map
"o" 'edebug-step-out
)
2184 ;; (define-key edebug-mode-map "m" 'edebug-set-initial-mode)
2186 (define-key edebug-mode-map
"q" 'top-level
)
2187 (define-key edebug-mode-map
"a" 'abort-recursive-edit
)
2188 (define-key edebug-mode-map
"S" 'edebug-stop
)
2191 (define-key edebug-mode-map
"b" 'edebug-set-breakpoint
)
2192 (define-key edebug-mode-map
"u" 'edebug-unset-breakpoint
)
2193 (define-key edebug-mode-map
"B" 'edebug-next-breakpoint
)
2194 (define-key edebug-mode-map
"x" 'edebug-set-conditional-breakpoint
)
2197 (define-key edebug-mode-map
"e" 'edebug-eval-expression
)
2198 (define-key edebug-mode-map
"\C-x\C-e" 'edebug-eval-last-sexp
)
2199 (define-key edebug-mode-map
"E" 'edebug-visit-eval-list
)
2202 (define-key edebug-mode-map
"w" 'edebug-where
)
2203 (define-key edebug-mode-map
"v" 'edebug-view-outside
)
2204 (define-key edebug-mode-map
"p" 'edebug-bounce-point
)
2205 (define-key edebug-mode-map
"W" 'edebug-toggle-save-windows
)
2208 (define-key edebug-mode-map
"?" 'edebug-help
)
2209 (define-key edebug-mode-map
"d" 'edebug-backtrace
)
2211 (define-key edebug-mode-map
"-" 'negative-argument
)
2215 (defvar global-edebug-prefix
"\^XX"
2216 "Prefix key for global edebug commands, available from any buffer.")
2218 (defvar global-edebug-map nil
2219 "Global map of edebug commands, available from any buffer.")
2221 (if global-edebug-map
2223 (setq global-edebug-map
(make-sparse-keymap))
2225 (global-unset-key global-edebug-prefix
)
2226 (global-set-key global-edebug-prefix global-edebug-map
)
2228 ;; (define-key global-edebug-map "X" 'edebug-step-through)
2229 (define-key global-edebug-map
" " 'edebug-step-through
)
2230 (define-key global-edebug-map
"g" 'edebug-go
)
2231 (define-key global-edebug-map
"G" 'edebug-Go-nonstop
)
2232 (define-key global-edebug-map
"t" 'edebug-trace
)
2233 (define-key global-edebug-map
"T" 'edebug-Trace-fast
)
2234 (define-key global-edebug-map
"c" 'edebug-continue
)
2235 (define-key global-edebug-map
"C" 'edebug-Continue-fast
)
2237 ;; (define-key global-edebug-map "m" 'edebug-set-initial-mode)
2238 (define-key global-edebug-map
"b" 'edebug-set-breakpoint
)
2239 (define-key global-edebug-map
"x" 'edebug-set-conditional-breakpoint
)
2240 (define-key global-edebug-map
"u" 'edebug-unset-breakpoint
)
2241 (define-key global-edebug-map
"w" 'edebug-where
)
2242 (define-key global-edebug-map
"q" 'top-level
)
2246 (defun edebug-help ()
2248 (describe-function 'edebug-mode
))
2251 (defun edebug-mode ()
2252 "Mode for Emacs Lisp buffers while in edebug. Under construction.
2254 There are both buffer local and global key bindings to several
2255 functions. E.g. edebug-step-through is bound to
2256 \\[edebug-step-through] in the debug buffer and
2257 \\<global-map>\\[edebug-step-through] in any buffer.
2259 edebug buffer commands:
2262 Global commands prefixed by global-edbug-prefix:
2263 \\{global-edebug-map}
2267 edebug-eval-macro-args
2268 edebug-stop-before-symbols
2271 edebug-save-buffer-points
2275 (use-local-map edebug-mode-map
))
2279 ;;===============================================
2280 ;; edebug eval list mode
2281 ;; A list of expressions and their evaluations is displayed
2282 ;; in edebug-eval-buffer
2284 (defvar edebug-eval-list nil
2285 "List of expressions to evaluate.")
2287 ;;(defvar edebug-eval-buffer "*edebug*"
2288 ;; "*Declared globally so edebug-eval-display can be called independent
2289 ;;of edebug (not implemented yet).")
2292 (defun edebug-eval-result-list ()
2293 "Return a list of evaluations of edebug-eval-list"
2294 ;; Assumes in outside environment.
2299 (error (format "%s: %s"
2300 (get (car err
) 'error-message
)
2305 (defun edebug-eval-display-list (edebug-eval-result-list)
2306 ;; Assumes edebug-eval-buffer exists.
2307 (let ((edebug-eval-list-temp edebug-eval-list
)
2308 (standard-output edebug-eval-buffer
)
2309 (edebug-display-line
2310 (format ";%s\n" (make-string (- (window-width) 2) ?-
))))
2311 (edebug-pop-to-buffer edebug-eval-buffer
)
2313 (while edebug-eval-list-temp
2314 (prin1 (car edebug-eval-list-temp
)) (terpri)
2315 (prin1 (car edebug-eval-result-list
)) (terpri)
2316 (princ edebug-display-line
)
2317 (setq edebug-eval-list-temp
(cdr edebug-eval-list-temp
))
2318 (setq edebug-eval-result-list
(cdr edebug-eval-result-list
)))
2321 (defun edebug-create-eval-buffer ()
2322 (if (not (and edebug-eval-buffer
(buffer-name edebug-eval-buffer
)))
2324 (set-buffer (setq edebug-eval-buffer
(get-buffer-create "*edebug*")))
2325 (edebug-eval-mode))))
2327 ;; Should generalize this to be callable outside of edebug
2328 ;; with calls in user functions, e.g. (edebug-eval-display)
2330 (defun edebug-eval-display (edebug-eval-result-list)
2331 "Display expressions and evaluations in EVAL-LIST.
2332 It modifies the context by popping up the eval display."
2333 (if edebug-eval-result-list
2335 (edebug-create-eval-buffer)
2336 (edebug-pop-to-buffer edebug-eval-buffer
)
2337 (edebug-eval-display-list edebug-eval-result-list
)
2340 (defun edebug-eval-redisplay ()
2341 "Redisplay eval list in outside environment.
2342 May only be called from within edebug-recursive-edit."
2343 (edebug-create-eval-buffer)
2344 (edebug-pop-to-buffer edebug-eval-buffer
)
2345 (edebug-outside-excursion
2346 (edebug-eval-display-list (edebug-eval-result-list))
2349 (defun edebug-visit-eval-list ()
2351 (edebug-eval-redisplay)
2352 (edebug-pop-to-buffer edebug-eval-buffer
))
2355 (defun edebug-update-eval-list ()
2356 "Replace the evaluation list with the sexps now in the eval buffer."
2358 (let ((starting-point (point))
2360 (goto-char (point-min))
2361 ;; get the first expression
2362 (edebug-skip-whitespace)
2366 (setq new-list
(cons (edebug-last-sexp) new-list
))))
2368 (while (re-search-forward "^;" nil t
)
2370 (skip-chars-forward " \t\n\r")
2371 (if (and (/= ?\
; (following-char))
2375 (setq new-list
(cons (edebug-last-sexp) new-list
)))))
2377 (setq edebug-eval-list
(nreverse new-list
))
2378 (edebug-eval-redisplay)
2379 (goto-char starting-point
)))
2382 (defun edebug-delete-eval-item ()
2383 "Delete the item under point and redisplay."
2384 ;; could add arg to do repeatedly
2386 (if (re-search-backward "^;" nil
'nofail
)
2389 (point) (progn (re-search-forward "^;" nil
'nofail
)
2392 (edebug-update-eval-list))
2396 (defvar edebug-eval-mode-map nil
2397 "Keymap for edebug-eval-mode. Superset of lisp-interaction-mode.")
2399 (if edebug-eval-mode-map
2401 (setq edebug-eval-mode-map
(copy-keymap lisp-interaction-mode-map
))
2403 (define-key edebug-eval-mode-map
"\C-c\C-w" 'edebug-where
)
2404 (define-key edebug-eval-mode-map
"\C-c\C-d" 'edebug-delete-eval-item
)
2405 (define-key edebug-eval-mode-map
"\C-c\C-u" 'edebug-update-eval-list
)
2406 (define-key edebug-eval-mode-map
"\C-x\C-e" 'edebug-eval-last-sexp
)
2407 (define-key edebug-eval-mode-map
"\C-j" 'edebug-eval-print-last-sexp
)
2411 (defun edebug-eval-mode ()
2412 "Mode for data display buffer while in edebug. Under construction.
2413 ... ignore the following...
2414 There are both buffer local and global key bindings to several
2415 functions. E.g. edebug-step-through is bound to
2416 \\[edebug-step-through] in the debug buffer and
2417 \\<global-map>\\[edebug-step-through] in any buffer.
2419 Eval list buffer commands:
2420 \\{edebug-eval-mode-map}
2422 Global commands prefixed by global-edbug-prefix:
2423 \\{global-edebug-map}
2425 (lisp-interaction-mode)
2426 (setq major-mode
'edebug-eval-mode
)
2427 (setq mode-name
"Edebug-Eval")
2428 (use-local-map edebug-eval-mode-map
))
2431 ;;========================================
2432 ;; Interface with standard debugger.
2434 (setq debugger
'edebug-debug
)
2435 ;; (setq debugger 'debug) ; use the default
2437 ;; Note that debug and its utilities must be byte-compiled to work, since
2438 ;; they depend on the backtrace looking a certain way.
2441 (defun edebug-debug (&rest debugger-args
)
2442 "Replacement for debug.
2443 If an error or quit occurred and we are running an edebugged function,
2444 show where we last were. Otherwise call debug normally."
2445 (if (and edebug-backtrace
; anything active?
2446 (eq (recursion-depth) edebug-recursion-depth
)
2449 ;; Where were we before the error occurred?
2450 (let ((edebug-offset-index (car edebug-offset-indices
))
2451 (edebug-arg-mode (car debugger-args
))
2452 (edebug-exp (car (cdr debugger-args
)))
2455 (edebug-outside-debug-on-eror debug-on-error
)
2456 (debug-on-error nil
))
2460 ;; Otherwise call debug normally.
2461 ;; Still need to remove extraneous edebug calls from stack.
2462 (apply 'debug debugger-args
)
2466 (defun edebug-backtrace ()
2467 "Display a non-working backtrace. Better than nothing..."
2469 (let ((old-buf (current-buffer)))
2470 (if (not edebug-backtrace-buffer
)
2471 (setq edebug-backtrace-buffer
2472 (let ((default-major-mode 'fundamental-mode
))
2473 (generate-new-buffer "*Backtrace*"))))
2474 (edebug-pop-to-buffer edebug-backtrace-buffer
)
2476 (let ((standard-output (current-buffer))
2477 (print-escape-newlines t
)
2481 (setq truncate-lines t
)
2484 ;; Clean up the backtrace.
2485 (goto-char (point-min))
2489 ;; Everything up to the first edebug is internal.
2490 (re-search-forward "^ edebug(")
2494 (setq last-ok-point
(point))
2496 ;; Delete interspersed edebug internals.
2497 (while (re-search-forward "^ edebug" nil t
)
2498 (if (looking-at "-enter")
2499 ;; delete extraneous progn at top level of function body
2501 (goto-char last-ok-point
)
2503 (setq last-ok-point
(point))))
2505 (delete-region last-ok-point
(point))
2506 (forward-line 1) ; skip past the good line
2507 (setq last-ok-point
(point))
2510 (edebug-pop-to-buffer old-buf
)
2514 ;;========================================================================
2515 ;; Trace display - append text to a buffer, and update display.
2517 ;;; (edebug-trace-display
2519 ;;; "saving: point = %s window-start = %s\n"
2520 ;;; (point) (window-start))
2522 (defun edebug-trace-display (buf-name fmt
&rest args
)
2523 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
2524 The buffer is created if it does not exist.
2525 You must include newlines in FMT to break lines."
2526 (let* ((selected-window (selected-window))
2527 (buffer (get-buffer-create buf-name
))
2529 (edebug-pop-to-buffer buffer
)
2531 (setq buf-window
(selected-window))
2533 (goto-char (point-max))
2534 (insert (apply 'format fmt args
))
2535 (set-window-point buf-window
(point))
2536 (forward-line (- 1 (window-height buf-window
)))
2537 (set-window-start buf-window
(point))
2538 ;; (edebug-sit-for 1)
2539 (bury-buffer buffer
)
2541 (select-window selected-window
)))
2543 ;;; edebug.el ends here