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 ;;; Revision 2.5 91/07/25 13:32:53 liberte
57 ;;; Doc string cleanup.
58 ;;; If edebug-form-hook is t, evaluate all arguments.
59 ;;; If edebug-form-hook is 0, evaluate no arguments.
60 ;;; If edebug-form-hook is nil, evaluate macro args according
61 ;;; to edebug-eval-macro-args.
62 ;;; Save the outside value of executing macro.
63 ;;; Save and restore the outside restriction.
64 ;;; Dont force update for go and Go-nonstop.
65 ;;; Save and restore last-command-char, last-command,
66 ;;; this-command, last-input-char.
67 ;;; For epoch, do epoch::dispatch-events before sit-for
68 ;;; and input-pending-p since X events could interfere.
69 ;;; Warn about unsetting non-existent breakpoint.
70 ;;; Fix edebug-forward-sexp with prefix arg.
71 ;;; Add edebug-step-out to exit from current sexp.
73 ;;; Revision 2.4 91/03/18 12:35:44 liberte
74 ;;; Force update after go or Go-nonstop modes, so overlay arrow is correct.
75 ;;; Support debug-on-quit. Remove edebug-on-error.
76 ;;; Fix edebug-anonymous. Bug found by jackr@wpd.sgi.com (Jack Repenning).
77 ;;; Don't discard-input anymore. Easier to change modes this way.
78 ;;; Fix max-lisp-eval-depth and max-specpdl-size incrementing.
79 ;;; Save and restore points in all buffers, if
80 ;;; edebug-save-buffer-points is non-nil. Expensive!
81 ;;; Bug caught by wolfgang@wsrcc.com (Wolfgang S. Rupprecht)
82 ;;; Save standard-output and standard-input in edebug-recursive-edit
83 ;;; so that edebug-outside-excursion can restore them.
84 ;;; Call set-buffer in edebug-pop-to-buffer since
85 ;;; select-window does not do that.
86 ;;; Fix edebug's eval-defun to remember current buffer inside evaluations
87 ;;; and to evaluate top-level forms. Found by Jamie Zawinski.
88 ;;; Add edebug-interactive-entry to support interactive forms with
89 ;;; non-string arg. Bug found by Jack Repenning.
90 ;;; Simplify edebug-restore-match-data to just store-match-data.
91 ;;; Motivated by linus@lysator.liu.se.
92 ;;; Move the match-data call to before the outside
93 ;;; buffer is changed, since it assumes that.
95 ;;; Revision 2.3 91/01/17 20:55:14 liberte
96 ;;; Fix bug found by hollen@megatek.uucp.
97 ;;; Current buffer was not being restored.
98 ;;; Call edebug with (edebug begin end 'exp)
99 ;;; and add additional wrapper around body of functions:
100 ;;; (edebug-enter function body).
101 ;;; Make &optional only apply to immediate next arg
102 ;;; in edebug-form-parser (was edebug-macro-parser).
103 ;;; Catch debug errors with edebug. Yeah!
104 ;;; Reset edebug-mode on first function entry. Yeah!
105 ;;; Motivated by Dion Hollenbeck.
106 ;;; Add the missing bindings to the global-edebug-map.
107 ;;; eval-current-buffer now uses eval-region.
108 ;;; eval-region now does not narrow region.
109 ;;; Narrowing was the cause of the window-start being set wrong.
110 ;;; Reset edebug-mode only on
111 ;;; first entry of any function at each recursive-edit level.
112 ;;; Add edebug-backtrace, to generate cleaned up
113 ;;; backtrace. It doesnt "work" like the debug backtrace, however.
114 ;;; Require reselecting outside window even if
115 ;;; quit occurs, otherwise save-excursions may restore
116 ;;; buffer to the wrong window.
118 ;;; Revision 2.2 90/11/26 21:14:22 liberte
119 ;;; Shadow eval-defun and eval-region. Toggle
120 ;;; edebugging with edebug-all-defuns.
121 ;;; Call edebug with (edebug 'function begin end 'exp)
122 ;;; Suggested by Jamie Zawinski <jwz@lucid.com>.
123 ;;; Add edebug-form-parser to process macro args.
124 ;;; Motivated by Darryl Okahata darrylo@hpnmxx.hp.com.
125 ;;; Fix by Roland McGrath <roland@ai.mit.edu>
126 ;;; to wrap body of edebug-save-restriction in progn.
127 ;;; Fix by Darryl Okahata <darrylo%hpnmd@hpcea.hp.com>
128 ;;; to add (set-window-hscroll (selected-window) 0) to
129 ;;; edebug-pop-to-buffer.
131 ;;; Revision 2.1 90/11/16 21:55:35 liberte
133 ;;; Add edebug-form-hook to edebug macro calls. Thanks to Joe Wells.
134 ;;; edebug-forward-sexp uses step mode if no forward-sexp.
136 ;;; Revision 2.0 90/11/14 22:30:54 liberte
137 ;;; Handle lambda forms, function, interactive evals, defmacro.
138 ;;; Clean up display for Epoch - save and restore screen configurations.
139 ;;; Note: epoch 3.2 broke set-window-configuration.
140 ;;; Also, sit-for pauses do not always work in epoch.
141 ;;; Display evaluations window.
142 ;;; Display result after expression evaluation.
143 ;;; Thanks to discussions with Shinichirou Sugou.
144 ;;; Conditional and temporary breakpoints.
145 ;;; Change "continue" to "go" mode and add different "continue" mode.
146 ;;; Option to stop before symbols.
148 ;;; Fix by: Glen Ditchfield gjditchfield@violet.uwaterloo.ca
149 ;;; to handle ?# type chars.
151 ;;; Revision 1.5 89/05/10 02:39:27 liberte
152 ;;; Fix condition-case expression lists.
153 ;;; Reorganize edebug.
155 ;;; Revision 1.4 89/02/14 22:58:34 liberte
156 ;;; Fix broken breakpointing.
157 ;;; Temporarily widen Emacs Lisp buffer during edebug.
159 ;;; Revision 1.3 89/01/30 00:26:09 liberte
160 ;;; More bug fixes for cond and let.
161 ;;; Another parsing fix backquote.
162 ;;; Fix for lambda forms inside defuns.
163 ;;; Leave point at syntax error, mark at starting position.
165 ;;; Revision 1.2 88/11/28 12:14:15 liberte
166 ;;; Bug fixes: cond construct didnt execute.
167 ;;; () in sexp list didnt parse
168 ;;; () as variable in condition-case didnt parse.
170 ;;; Revision 1.1 88/11/28 12:11:27 liberte
177 ;; Put edebug.el in some directory in your load-path and byte-compile it.
179 ;; Put the following forms in your .emacs file.
180 ;; (define-key emacs-lisp-mode-map "\^Xx" 'edebug-defun)
181 ;; (autoload 'edebug-defun "edebug")
182 ;; (autoload 'edebug-debug "edebug")
183 ;; (setq debugger 'edebug-debug)
184 ;; ... other options, described in the next section.
186 ;; Evaluate a defun for edebug with edebug-defun.
187 ;; Evaluate your function normally.
188 ;; Use the "?" command in edebug to describe other commands.
189 ;; See edebug.texinfo for more instructions.
197 (defvar edebug-all-defuns nil
198 "*If non-nil, all defuns and defmacros evaluated will use edebug.
199 eval-defun without prefix arg and eval-region will use edebug-defun.
201 If nil, eval-region evaluates normally, but eval-defun with prefix arg
202 uses edebug-defun. eval-region is called by eval-defun, eval-last-sexp,
203 and eval-print-last-sexp.
205 You may wish to make this variable local to each Emacs Lisp buffer by calling
206 (make-local-variable 'edebug-all-defuns) in your emacs-lisp-mode-hook.
207 You can use the function edebug-all-defuns to toggle its value.")
210 (defvar edebug-eval-macro-args nil
211 "*If non-nil, edebug will assume that all macro call arguments for
212 macros that have no edebug-form-hook may be evaluated, otherwise it
213 will not. To specify exceptions for macros that have some arguments
214 evaluated and some not, you should specify an edebug-form-hook")
216 (defvar edebug-stop-before-symbols nil
217 "*Non-nil causes edebug to stop before symbols as well as after.
218 In any case, it is possible to stop before a symbol with a breakpoint or
221 (defvar edebug-save-windows t
222 "*If non-nil, save and restore window configuration on edebug calls.
223 It takes some time to save and restore, so if your program does not care
224 what happens to the window configurations, it is better to set this
227 (defvar edebug-save-point t
228 "*If non-nil, save and restore the point and mark in source code buffers.")
230 (defvar edebug-save-buffer-points nil
231 "*If non-nil, save and restore the points of all buffers, displayed or not.
233 Saving and restoring buffer points is necessary if you are debugging
234 code that changes the point of a buffer which is displayed in a
235 non-selected window. If edebug or the user then selects the
236 window, the buffer's point will be changed to the window's point.
238 Saving and restoring all the points is an expensive operation since it
239 visits each buffer twice for each edebug call, so it is best to avoid
242 (defvar edebug-initial-mode 'step
243 "*Global initial mode for edebug, if non-nil.
244 This is used when edebug is first entered for each recursive-edit level.
245 Possible values are nil (meaning keep using edebug-mode), step, go,
246 Go-nonstop, trace, Trace-fast, continue, and Continue-fast.")
248 (defvar edebug-trace nil
249 "*Non-nil if edebug should show a trace of function entry and exit.
250 Tracing output is displayed in a buffer named *edebug-trace*, one
251 function entry or exit per line, indented by the recursion level. You
252 can customize by replacing functions edebug-print-trace-entry and
253 edebug-print-trace-exit.")
257 ;;;========================================================================
261 (defun edebug-which-function ()
262 "Return the symbol of the function we are in"
267 (if (not (memq (read (current-buffer)) '(defun defmacro)))
268 (error "Not in defun or defmacro."))
269 (read (current-buffer))))
271 (defun edebug-last-sexp ()
272 "Return the last sexp before point in current buffer.
273 Assumes Emacs Lisp syntax is active."
282 (defun edebug-window-list ()
283 "Return a list of windows, in order of next-window."
284 ;; This doesnt work for epoch.
285 (let* ((first-window (selected-window))
286 (window-list (list first-window))
287 (next (next-window first-window)))
288 (while (not (eq next first-window))
289 (setq window-list (cons next window-list))
290 (setq next (next-window next)))
291 (nreverse window-list)))
293 (defun edebug-get-buffer-points ()
294 "Return a list of buffer point pairs, for all buffers."
296 (mapcar (function (lambda (buf)
301 (defun edebug-set-buffer-points ()
302 "Restore the buffer-points given by edebug-get-buffer-points."
303 (mapcar (function (lambda (buf-point)
304 (if (buffer-name (car buf-point)) ; still exists
306 (set-buffer (car buf-point))
307 (goto-char (cdr buf-point))))))
308 edebug-buffer-points))
310 (defun edebug-two-window-p ()
311 "Return t if there are two windows."
312 (and (not (one-window-p))
313 (eq (selected-window)
314 (next-window (next-window (selected-window))))))
316 (defun edebug-macrop (object)
317 "Return the macro named by OBJECT, or nil if it is not a macro."
318 (while (and (symbolp object) (fboundp object))
319 (setq object (symbol-function object)))
320 (if (and (listp object)
321 (eq 'macro (car object))
322 (edebug-functionp (cdr object)))
325 (defun edebug-functionp (object)
326 "Returns the function named by OBJECT, or nil if it is not a function."
327 (while (and (symbolp object) (fboundp object))
328 (setq object (symbol-function object)))
329 (if (or (subrp object)
331 (eq (car object) 'lambda)
332 (listp (car (cdr object)))))
335 (defun edebug-sort-alist (alist function)
336 "Return the ALIST sorted with comparison function FUNCTION.
337 This uses 'sort so the sorting is destructive."
338 (sort alist (function
340 (funcall function (car e1) (car e2))))))
342 (put 'edebug-save-restriction 'edebug-form-hook
345 (defmacro edebug-save-restriction (&rest body)
346 "Evaluate BODY while saving the current buffers restriction.
347 BODY may change buffer outside of current restriction, unlike
348 save-restriction. BODY may change the current buffer,
349 and the restriction will be restored to the original buffer,
350 and the current buffer remains current.
351 Return the result of the last expression in BODY."
352 (` (let ((edebug:s-r-beg (point-min-marker))
353 (edebug:s-r-end (point-max-marker)))
357 (set-buffer (marker-buffer edebug:s-r-beg))
358 (narrow-to-region edebug:s-r-beg edebug:s-r-end))))))
361 ;;;=============================================================
362 ;;; Redefine eval-defun, eval-region, and eval-current-buffer.
363 ;;; -----------------------------------------------------------
365 (defun edebug-all-defuns ()
366 "Toggle edebugging of all defuns and defmacros,
367 not including those evaluated in the minibuffer, or during load."
369 (setq edebug-all-defuns (not edebug-all-defuns))
370 (message "Edebugging is %s." (if edebug-all-defuns "on" "off")))
373 (if (not (fboundp 'edebug-emacs-eval-defun))
374 (fset 'edebug-emacs-eval-defun (symbol-function 'eval-defun)))
375 ;;(fset 'eval-defun (symbol-function 'edebug-emacs-eval-defun))
377 (defun eval-defun (edebug-debug)
378 "Edebug replacement for eval-defun. Print value in the minibuffer.
379 Evaluate the top-level form that point is in or before. Note:
380 eval-defun normally evaluates any top-level form, not just defuns.
382 Here are the differences from the standard eval-defun. If the prefix
383 argument is the same as edebug-all-defuns (nil or non-nil), evaluate
384 normally; otherwise edebug-defun is called to wrap edebug calls around
385 evaluatable expressions in the defun or defmacro body. Also, the
386 value printed by edebug-defun is not just the function name."
388 (let ((edebug-all-defuns
389 (not (eq (not edebug-debug) (not edebug-all-defuns)))))
390 (edebug-emacs-eval-defun nil)
394 (if (not (fboundp 'edebug-emacs-eval-region))
395 (fset 'edebug-emacs-eval-region (symbol-function 'eval-region)))
396 ;; (fset 'eval-region (symbol-function 'edebug-emacs-eval-region))
398 (defun eval-region (edebug-e-r-start edebug-e-r-end
399 &optional edebug-e-r-output)
400 "Edebug replacement for eval-defun.
401 Like eval-region, but call edebug-defun for defuns or defmacros.
402 Also, this eval-region does not narrow to the region and
403 if an error occurs, point is left at the error."
404 ;; One other piddling difference concerns whitespace after the expression.
406 (let ((standard-output (or edebug-e-r-output 'symbolp))
407 (edebug-e-r-pnt (point))
408 (edebug-e-r-buf (current-buffer))
409 (edebug-e-r-inside-buf (current-buffer))
410 ;; Mark the end because it may move.
411 (edebug-e-r-end-marker (set-marker (make-marker) edebug-e-r-end))
414 (goto-char edebug-e-r-start)
415 (edebug-skip-whitespace)
416 (while (< (point) edebug-e-r-end-marker)
417 (if (and edebug-all-defuns
418 (eq 'lparen (edebug-next-token-class))
420 (forward-char 1) ; skip \(
421 (memq (edebug-read-sexp) '(defun defmacro))))
424 ;; Potential problem: edebug-defun always prints name.
425 (forward-sexp 1) ; skip the defun
427 (if (and (eq 'lparen (edebug-next-token-class))
429 (forward-char 1) ; skip \(
430 (memq (edebug-read-sexp) '(defun defmacro))))
431 ;; If it's a defun or defmacro, but not edebug-all-defuns
432 ;; reset the symbols edebug property to be just a marker at
433 ;; the definitions source code.
434 (put (edebug-which-function) 'edebug (point-marker)))
436 ;; Evaluate normally - after restoring the current-buffer.
437 (setq edebug-e-r-val (edebug-read-sexp))
439 (set-buffer edebug-e-r-inside-buf)
440 (setq edebug-e-r-val (eval edebug-e-r-val))
441 ;; Remember current buffer for next time.
442 (setq edebug-e-r-inside-buf (current-buffer)))
444 (if edebug-e-r-output
446 (setq values (cons edebug-e-r-val values))
447 (if (eq standard-output t)
448 (prin1 edebug-e-r-val)
449 (print edebug-e-r-val))))
452 (min (max edebug-e-r-end-marker (point))
453 (progn (edebug-skip-whitespace) (point))))
455 (if (null edebug-e-r-output)
456 ;; do the save-excursion recovery
458 ;; but mark is not restored
459 (set-buffer edebug-e-r-buf)
460 (goto-char edebug-e-r-pnt)))
465 (defun edebug-eval-current-buffer (&optional edebug-e-c-b-output)
466 "Call eval-region on the whole buffer."
468 (eval-region (point-min) (point-max) edebug-e-c-b-output))
470 (defun edebug-eval-buffer (&optional buffer edebug-e-c-b-output)
471 "Call eval-region on the whole buffer."
472 (interactive "bEval buffer: ")
475 (eval-region (point-min) (point-max) edebug-e-c-b-output)))
477 ;; The standard eval-current-buffer doesn't use eval-region.
478 (if (and (fboundp 'eval-current-buffer)
479 (not (fboundp 'edebug-emacs-eval-current-buffer)))
481 (fset 'edebug-emacs-eval-current-buffer
482 (symbol-function 'eval-current-buffer))
483 (fset 'eval-current-buffer 'edebug-eval-current-buffer)))
484 (if (and (fboundp 'eval-buffer)
485 (not (fboundp 'edebug-emacs-eval-buffer)))
487 (fset 'edebug-emacs-eval-buffer
488 (symbol-function 'eval-buffer))
489 (fset 'eval-buffer 'edebug-eval-buffer)))
493 ;;;======================================================================
497 ;;; The top level function for parsing defuns is edebug-defun; it
498 ;;; calls all the rest. It checks the syntax a bit and leaves point
499 ;;; at any error it finds, but otherwise should appear to work like
502 ;;; The basic plan is to surround each expression with a call to the
503 ;;; function edebug together with indexes into a table of positions of
504 ;;; all expressions. Thus an expression "exp" in function foo
507 ;;; (edebug 1 2 'exp)
509 ;;; First point moved to to the beginning of exp (offset 1 of the
510 ;;; current function). Then the expression is evaluated and point is
511 ;;; moved to offset 2, at the end of exp.
513 ;;; The top level expressions of the function are wrapped in a call to
514 ;;; edebug-enter, which supplies the function name and the actual
515 ;;; arguments to the function. See functions edebug and edebug-enter
516 ;;; for more details.
520 (defun edebug-defun ()
521 "Evaluate defun or defmacro, like eval-defun, but with edebug calls.
522 Print its name in the minibuffer and leave point after any error it finds,
523 with mark at the original point."
525 (let (def-kind ; whether defmacro or defun
530 (edebug-offset-index 0)
533 (starting-point (point))
535 (parse-sexp-ignore-comments t))
543 (setq edebug-func-mark (point-marker))
544 (if (not (eq 'defun (setq def-kind (edebug-read-sexp))))
545 (if (not (eq 'defmacro def-kind))
546 (edebug-syntax-error "%s is not a defun or defmacro."
548 (setq def-name (edebug-read-sexp))
549 (if (not (symbolp def-name))
550 (edebug-syntax-error "Bad defun name: %s" def-name))
551 (setq def-args (edebug-read-sexp))
552 (if (not (listp def-args))
553 (edebug-syntax-error "Bad defun arg list: %s" def-args))
555 ;; look for doc string
556 (setq tmp-point (point))
557 (if (eq 'string (edebug-next-token-class))
559 (setq def-docstring (edebug-read-sexp))
560 (setq tmp-point (point))))
562 ;; look for interactive form
563 (if (eq 'lparen (edebug-next-token-class))
565 (forward-char 1) ; skip \(
566 (if (eq 'interactive (edebug-read-sexp))
568 (setq defun-interactive
569 (cons 'interactive (edebug-interactive)))
570 (forward-char 1) ; skip \)
571 (setq tmp-point (point))
574 (goto-char tmp-point)
576 ;; build the new definition
577 (fset def-name (` (lambda
580 (, defun-interactive)
581 ;; the remainder is a list of sexps
586 (,@ (edebug-sexp-list t)))))
588 ;; if it is a defmacro, prepend 'macro
589 (if (eq 'defmacro def-kind)
590 (fset def-name (cons 'macro (symbol-function def-name))))
592 ;; recover point, like save-excursion but only if no error occurs
593 (goto-char starting-point)
595 ;; store the offset list in functions property list
596 (put def-name 'edebug
597 (list edebug-func-mark
598 nil ; clear breakpoints
599 (vconcat (nreverse edebug-offset-list))))
600 (message "edebug: %s" def-name)
604 ;; Set mark at starting-point so user can return.
605 ;; Leave point at error.
607 (goto-char starting-point)
608 (set-mark-command nil))
609 (message "Syntax error: %s" (cdr err))
610 ;; (signal 'invalid-read-syntax (cdr err)) ; pass it on, to who?
617 (defun edebug-sexp-list (debuggable)
618 "Return an edebug form built from the sexp list following point in the
619 current buffer. If DEBUGGABLE then wrap edebug calls around each sexp.
620 The sexp list does not start with a left paren; we are already in the list.
621 Leave point at (before) the trailing right paren."
623 (while (not (eq 'rparen (edebug-next-token-class)))
624 (setq sexp-list (cons (if debuggable
628 (nreverse sexp-list)))
631 (defun edebug-increment-offset ()
632 ;; accesses edebug-offset-index and edebug-offset-list
633 (setq edebug-offset-index (1+ edebug-offset-index))
634 (setq edebug-offset-list (cons (- (point) edebug-func-mark)
635 edebug-offset-list)))
638 (defun edebug-make-edebug-form (index form)
639 "Return the edebug form for the current function at offset INDEX given FORM.
640 Looks like: (edebug def-name INDEX edebug-offset-index 'FORM).
641 Also increment the offset index."
647 (edebug-increment-offset)
651 (defun edebug-form ()
652 "Return the debug form for the following form. Add the point offset
653 to the edebug-offset-list for the function and move point to
654 immediately after the form."
655 (let* ((index edebug-offset-index)
657 ;; The point must be added to the offset list now
658 ;; because edebug-list will add more offsets indirectly.
659 (edebug-skip-whitespace)
660 (edebug-increment-offset)
661 (setq class (edebug-next-token-class))
664 (edebug-make-edebug-form index (edebug-list)))
667 (if (and (not (memq (setq form (edebug-read-sexp)) '(nil t)))
668 ;; note: symbol includes numbers, see parsing utilities
669 (not (numberp form)))
670 (edebug-make-edebug-form index form)
672 (t (edebug-read-sexp)))))
675 (defun edebug-list ()
676 "Return an edebug form built from the list form that follows point.
677 Insert debug calls as appropriate to the form. Start with point at
678 the left paren. Leave point after the right paren."
679 (let ((beginning (point))
683 (forward-char 1) ; skip \(
684 (setq class (edebug-next-token-class))
687 (setq head (edebug-read-sexp)))
689 (setq head (edebug-anonymous)))
692 (t (edebug-syntax-error
693 "Head of list must be a symbol or lambda expression.")))
700 ;; None of the edebug-form-hooks defined below are used, for speed.
701 ;; They are included for documentation, though the hook would not
702 ;; necessarily behave the same as the function it is replacing.
704 ;;; Using the edebug-form-hooks should work, but would take more time.
706 ;;; (let ((form (get head 'edebug-form-hook)))
708 ;;; (edebug-form-parser form)
709 ;;; (if (edebug-macrop head)
710 ;;; (if edebug-eval-macro-args
711 ;;; (edebug-sexp-list t)
712 ;;; (edebug-sexp-list nil))
713 ;;; ;; assume it is a function
714 ;;; (edebug-sexp-list t)))))
716 ;; handle all special-forms with unevaluated arguments
717 ((memq head '(let let*)) (edebug-let))
718 ((memq head '(setq setq-default)) (edebug-setq))
719 ((eq head 'cond) (edebug-cond))
720 ((eq head 'condition-case) (edebug-condition-case))
722 ((memq head '(quote ; permits more than one arg
723 defun defvar defconst defmacro))
724 (edebug-sexp-list nil))
727 (if (eq 'lparen (edebug-next-token-class))
729 (edebug-read-sexp) ; should be just a symbol
732 ;; is it a lisp macro?
733 ((edebug-macrop head)
734 (or (and (symbolp head)
735 (let ((form (get head 'edebug-form-hook)))
740 (edebug-sexp-list nil)
741 (edebug-form-parser form))))))
742 (edebug-sexp-list edebug-eval-macro-args)))
744 ((eq head 'interactive)
745 (edebug-syntax-error "interactive not expected here."))
747 ;; otherwise it is a function call
748 (t (edebug-sexp-list t))
751 (if (eq 'rparen (edebug-next-token-class))
752 (forward-char 1) ; skip \)
753 (edebug-syntax-error "Too many arguments."))
757 (defun edebug-form-parser (args)
758 "Parse the macro arguments that follow based on ARGS.
759 ARGS describes the types of the arguments of a list form. Each of the ARGS
760 is processed left to right, in the same order as the arguments of the
761 list form. See the edebug documentation for more details. The ARGS
762 may be one of the following:
764 symbolp - an unevaluated symbol
765 integerp - an unevaluated number
766 stringp - an unevaluated string
767 vectorp - an unevaluated vector
768 atom - an unevaluated number, string, symbol, or vector
770 sexp - an unevaluated sexp (atom or list); may not be empty
771 form - an evaluated sexp; may not be empty
773 foo - any other symbol should be the name of a function; this
774 function is called on the argument as a predicate and an error
775 is signaled if the predicate fails.
777 &optional - one following arg in the list may or may not appear.
778 &rest - all following args are repeated zero or more times as a group.
779 This is an extension of the normal meaning of &rest.
780 &or - each of the following args are alternatives, processed left to
781 right until one succeeds. There is no way to group
782 more than one list element as one alternative.
784 (...) - a sublist, of the same format as the top level, processed recursively.
785 Special case: if the car of the list is quote, the argument must match
786 the quoted sexp (see example below of 'for macro).
790 arg form form-list class
793 (not (eq 'rparen (setq class (edebug-next-token-class)))))
795 (setq arg (car arglist))
796 (setq arglist (cdr arglist))
797 (if (and &rest (null arglist))
798 (setq arglist &rest))
801 ((memq arg '(&optional &rest &or))
802 ;; remember arglist at this point
804 (throw 'no-match nil))
807 (setq form (edebug-form)))
810 (setq form (edebug-read-sexp)))
813 (if (eq 'quote (car arg))
814 ;; special case, match the quoted symbol
816 (setq arg (car (cdr arg)))
817 (if (not (eq arg (setq form (edebug-read-sexp))))
818 (edebug-form-parser-error)
820 (if (eq class 'lparen)
822 (forward-char 1) ; skip \(
823 (setq form (edebug-form-parser arg))
824 (forward-char 1) ; skip \)
828 (pred (if (fboundp arg) (symbol-function arg))))
830 (not (funcall pred (setq form (edebug-read-sexp))))
831 (edebug-form-parser-error)
833 (t (throw 'no-match nil))
835 (setq &optional nil) ; only lasts for one match
836 (setq form-list (cons form form-list)) ; skipped by no-match throw
839 (if (and arglist (not (or &optional &rest
840 (memq (car arglist) '(&optional &rest)))))
841 (edebug-syntax-error "Not enough arguments."))
842 (if (not (eq 'rparen (edebug-next-token-class)))
844 (edebug-syntax-error "Unrecognized argument.")
845 (edebug-syntax-error "Too many arguments.")))
846 (nreverse form-list)))
849 (defun edebug-form-parser-error ()
852 (throw 'no-match nil)
855 (setq &optional nil) ; only lasts for one failed match not in &or
856 (throw 'no-match nil))
857 (edebug-syntax-error "%s is not %s" form arg))))
859 ;; for loop defined in Emacs Lisp manual
860 (put 'for 'edebug-form-hook
861 '(symbolp 'from form 'to form 'do &rest form))
863 ;; case and do defined in cl.el
864 (put 'case 'edebug-form-hook
865 '(form &rest (sexp form)))
867 (put 'do 'edebug-form-hook
870 (symbolp &optional form
875 (put 'defvar 'edebug-form-hook
876 (put 'defconst 'edebug-form-hook
877 '(symbolp &optional form &optional stringp)))
879 (put 'defun 'edebug-form-hook
880 (put 'defmacro 'edebug-form-hook
881 '(symbolp (&rest symbolp)
883 &optional ('interactive &or stringp form)
886 (put 'anonymous 'edebug-form-hook
887 '(&optional 'macro 'lambda (&rest symbolp) &rest form))
889 (defun edebug-anonymous ()
890 "Return the edebug form for an anonymous lambda or macro.
891 Point starts before the left paren and ends after it."
892 (forward-char 1) ; skip \(
894 (let ((head (edebug-read-sexp)))
899 (if (not (eq 'lambda (edebug-read-sexp)))
900 (edebug-syntax-error "lambda expected."))
901 (cons 'macro (edebug-lambda)))
902 (t (edebug-syntax-error "Anonymous lambda or macro expected."))))
903 (forward-char 1) ; skip \)
907 (defun edebug-lambda ()
908 "Return the edebug form for the lambda form that follows.
909 Point starts after the lambda symbol and is moved to before the right paren."
911 (list 'lambda (edebug-read-sexp)) ; the args
912 (edebug-sexp-list t))) ; the body
916 (put 'let 'edebug-form-hook
917 (put 'let* 'edebug-form-hook
919 &or (symbolp &optional form)
924 "Return the edebug form of the let or let* form.
925 Leave point before the right paren."
930 ;; first process the var/value list
931 (if (not (eq 'lparen (edebug-next-token-class)))
932 (if (setq token (edebug-read-sexp))
933 (edebug-syntax-error "Bad var list in let.") ; should be nil
937 (forward-char 1) ; lparen
938 (while (not (eq 'rparen (setq class (edebug-next-token-class))))
941 (if (not (eq 'lparen class))
943 (forward-char 1) ; lparen
946 (if (not (eq 'rparen (edebug-next-token-class)))
947 (edebug-syntax-error "Right paren expected in let.")
948 (forward-char 1) ; rparen
951 (forward-char 1) ; rparen
952 (nreverse var-value-list))
954 ;; now process the expression list
955 (edebug-sexp-list t))))
958 (defun edebug-var-value ()
959 "Return the edebug form of the var and optional value that follow point.
960 Leave point after the value, if there is one."
962 (edebug-read-sexp) ; the variable
963 (and (not (eq 'rparen (edebug-next-token-class)))
967 (put 'setq 'edebug-form-hook
968 (put 'setq-default 'edebug-form-hook
969 '(&rest symbolp form)))
971 (defun edebug-setq ()
972 "Return the edebug form of the setq or setq-default var-value list."
973 (let (var-value-list)
974 (while (not (eq 'rparen (edebug-next-token-class)))
976 (append var-value-list
977 (edebug-var-value))))
981 (put 'interactive 'edebug-form-hook
982 '(&optional &or stringp form))
984 (defun edebug-interactive ()
985 "Return the edebug form of the interactive form."
987 (if (not (eq 'rparen (edebug-next-token-class)))
988 (if (eq 'string (edebug-next-token-class))
991 (` (edebug-interactive-entry
993 (quote ((,@ (edebug-form))))))
994 (if (not (eq 'rparen (edebug-next-token-class)))
996 "Only first expression used in interactive form.")))))))
999 (put 'cond 'edebug-form-hook
1000 '(&rest (form &rest form)))
1002 (defun edebug-cond ()
1003 "Return the edebug form of the cond form."
1004 (let (value-value-list
1006 (while (not (eq 'rparen (setq class (edebug-next-token-class))))
1007 (setq value-value-list
1009 (if (not (eq 'lparen class))
1010 (let ((thing (edebug-read-sexp)))
1012 (edebug-syntax-error "Condition expected in cond")
1014 (forward-char 1) ; \(
1018 (if (eq 'rparen (edebug-next-token-class))
1020 (edebug-sexp-list t)))
1021 (if (not (eq 'rparen (edebug-next-token-class)))
1022 (edebug-syntax-error "Right paren expected in cond"))
1023 (forward-char 1) ; \)
1026 (nreverse value-value-list)))
1029 ;; Bug: this doesnt support condition name lists
1030 (put 'condition-case 'edebug-form-hook
1033 &rest (symbolp &optional form)))
1035 (defun edebug-condition-case ()
1036 "Return the edebug form of the condition-case form."
1039 ;; read the variable or nil
1040 (setq token (edebug-read-sexp))
1041 (if (not (symbolp token))
1042 (edebug-syntax-error
1043 "Variable or nil required for condition-case; found: %s" token))
1047 (edebug-form) ; the form
1050 (let (symb-sexp-list
1052 (while (not (eq 'rparen (setq class (edebug-next-token-class))))
1053 (setq symb-sexp-list
1055 (if (not (eq 'lparen class))
1056 (edebug-syntax-error "Bad handler in condition-case.")
1057 (forward-char 1) ; \(
1060 (edebug-read-sexp) ; the error-condition
1061 (and (not (eq 'rparen (edebug-next-token-class)))
1062 (edebug-sexp-list t)))
1063 (forward-char 1) ; \)
1066 (nreverse symb-sexp-list)))))
1070 ;;------------------------------------------------
1073 (defun edebug-syntax-error (msg &rest args)
1074 "Signal an invalid-read-syntax with MSG and ARGS.
1075 This is caught by edebug-defun."
1076 (signal 'invalid-read-syntax (apply 'format msg args)))
1079 (defun edebug-skip-whitespace ()
1080 "Leave point before the next token, skipping white space and comments."
1081 (skip-chars-forward " \t\r\n\f")
1082 (while (= (following-char) ?\;)
1083 (skip-chars-forward "^\n\r") ; skip the comment
1084 (skip-chars-forward " \t\r\n\f")))
1086 (defun edebug-read-sexp ()
1087 "Read one sexp from the current buffer starting at point.
1088 Leave point immediately after it. A sexp can be a list or atom.
1089 An atom is a symbol (or number), character, string, or vector."
1090 ;; This is gummed up by parser inconsistencies (bugs?)
1092 (edebug-skip-whitespace)
1093 (if (or (= (following-char) ?\[) (= (following-char) ??))
1094 ;; scan-sexps doesn't read vectors or character literals correctly,
1096 (setq token (read (current-buffer)))
1098 (min ; use the lesser of the read and scan-sexps motion
1099 ;; read goes one too far if (quoted) string or symbol
1100 ;; is immediately followed by non-whitespace
1102 (setq token (read (current-buffer)))
1104 ;; scan-sexps reads too far if a quoting character is read
1105 (scan-sexps (point) 1))))
1108 (defconst edebug-syntax-table
1109 (let ((table (make-vector 256 'symbol)))
1110 ;; Treat numbers as symbols, because of confusion with -, -1, and 1-.
1111 (aset table ?\( 'lparen)
1112 (aset table ?\) 'rparen)
1113 (aset table ?\' 'quote)
1114 (aset table ?\" 'string)
1115 (aset table ?\? 'char)
1116 (aset table ?\[ 'vector)
1117 (aset table ?\. 'dot)
1118 ;; We dont care about any other chars since they wont be seen.
1120 "Lookup table for the token class of each character.")
1122 (defun edebug-next-token-class ()
1123 "Move to the next token and return its class. We only care about
1124 lparen, rparen, dot, quote, string, char, vector, or symbol."
1125 (edebug-skip-whitespace)
1126 (aref edebug-syntax-table (following-char)))
1129 ;;;=================================================================
1130 ;;; The debugger itself
1131 ;;; -------------------
1134 (defvar edebug-active nil
1135 "Non-nil when edebug is active")
1138 ;;; add minor-mode-alist entry
1139 (or (assq 'edebug-active minor-mode-alist)
1140 (setq minor-mode-alist (cons (list 'edebug-active " *Debugging*")
1143 (defvar edebug-backtrace nil
1144 "Stack of active functions evaluated via edebug.
1145 Should be nil at the top level.")
1147 (defvar edebug-offset-indices nil ; not used yet.
1148 "Stack of offset indices of visited edebug sexps.
1149 Should be nil at the top level.")
1151 (defvar edebug-entered nil
1152 "Non-nil if edebug has already been entered at this recursive edit level.")
1155 (defun edebug-enter (edebug-func edebug-args edebug-body)
1156 "Entering FUNC. The arguments are ARGS, and the body is BODY.
1157 Setup edebug variables and evaluate BODY. This function is called
1158 when a function evaluated with edebug-defun is entered. Return the
1161 ;; Is this the first time we are entering edebug since
1162 ;; lower-level recursive-edit command?
1163 (if (and (not edebug-entered)
1164 edebug-initial-mode)
1165 ;; Reset edebug-mode to the initial mode.
1166 (setq edebug-mode edebug-initial-mode))
1167 (let* ((edebug-entered t)
1168 (edebug-data (get edebug-func 'edebug))
1169 ;; pull out parts of the edebug-data
1170 (edebug-func-mark (car edebug-data)) ; mark at function start
1172 (edebug-buffer (marker-buffer edebug-func-mark))
1173 (edebug-backtrace (cons edebug-func edebug-backtrace))
1174 (max-lisp-eval-depth (+ 6 max-lisp-eval-depth)) ; too much??
1175 (max-specpdl-size (+ 10 max-specpdl-size)) ; the args and these vars
1178 (let ((edebug-stack-depth (1- (length edebug-backtrace)))
1180 (edebug-print-trace-entry
1181 "*edebug-trace*" edebug-func edebug-args edebug-stack-depth)
1182 (setq edebug-result (eval edebug-body))
1183 (edebug-print-trace-exit
1184 "*edebug-trace*" edebug-func edebug-result edebug-stack-depth)
1189 (defun edebug-interactive-entry (edebug-func edebug-args)
1190 "Evaluating FUNCs non-string argument of interactive form ARGS."
1191 (if (and (not edebug-entered)
1192 edebug-initial-mode)
1193 ;; Reset edebug-mode to the initial mode.
1194 (setq edebug-mode edebug-initial-mode))
1195 (let* ((edebug-entered t)
1196 (edebug-data (get edebug-func 'edebug))
1197 ;; pull out parts of the edebug-data
1198 (edebug-func-mark (car edebug-data)) ; mark at function start
1200 (edebug-buffer (marker-buffer edebug-func-mark))
1201 ;; (edebug-backtrace (cons edebug-func edebug-backtrace))
1203 (eval edebug-args)))
1206 (defun edebug-print-trace-entry
1207 (edebug-stream edebug-function edebug-args edebug-stack-depth)
1208 (edebug-trace-display
1210 "%sEnter: %s\n" (make-string edebug-stack-depth ?\ ) edebug-function)
1213 (defun edebug-print-trace-exit
1214 (edebug-stream edebug-function edebug-result edebug-stack-depth)
1215 (edebug-trace-display
1217 "%sExit: %s\n" (make-string edebug-stack-depth ?\ ) edebug-function)
1221 (defun edebug (edebug-before-index edebug-after-index edebug-exp)
1222 "Debug current function given BEFORE and AFTER positions around EXP.
1223 BEFORE and AFTER are indexes into the position offset vector in the
1224 functions 'edebug property. edebug is called from functions compiled
1226 (let ((max-lisp-eval-depth (+ 5 max-lisp-eval-depth)) ; enough??
1227 (max-specpdl-size (+ 7 max-specpdl-size)) ; the args and these vars
1228 (edebug-offset-indices
1229 (cons edebug-before-index edebug-offset-indices))
1230 ;; Save the outside value of executing macro.
1231 (edebug-outside-executing-macro executing-macro)
1232 ;; Don't keep reading from an executing kbd macro within edebug!
1233 (executing-macro nil)
1235 (if (and (eq edebug-mode 'Go-nonstop)
1236 (not (edebug-input-pending-p)))
1237 ;; Just return evalled expression.
1239 (edebug-debugger edebug-before-index 'enter edebug-exp)
1240 (edebug-debugger edebug-after-index 'exit (eval edebug-exp))
1244 (defun edebug-debugger (edebug-offset-index edebug-arg-mode edebug-exp)
1245 "Determine if edebug display should be updated."
1247 ;; This needs to be here since breakpoints may be changed.
1248 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
1249 (edebug-break-data (assq edebug-offset-index edebug-breakpoints))
1251 (if edebug-break-data
1252 (let ((edebug-break-condition
1253 (car (cdr edebug-break-data))))
1254 (or (not edebug-break-condition)
1255 (eval edebug-break-condition)))))
1257 (if (and edebug-break
1258 (car (cdr (cdr edebug-break-data)))) ; is it temporary?
1259 ;; Delete the breakpoint.
1261 (cons (delq edebug-break-data edebug-breakpoints)
1262 (cdr (cdr edebug-data)))))
1264 ;; Dont do anything if mode is go, continue, or Continue-fast
1265 ;; and no break, and no input.
1266 (if (or (and (not (memq edebug-mode '(go continue Continue-fast)))
1267 (or edebug-stop-before-symbols
1268 (not (and (eq edebug-arg-mode 'enter)
1269 (symbolp edebug-exp)))))
1270 (edebug-input-pending-p)
1278 (defvar edebug-window-start 0
1279 "Remember where each buffers' window starts between edebug calls.
1280 This is to avoid spurious recentering.")
1282 (setq-default edebug-window-start 0)
1283 (make-variable-buffer-local 'edebug-window-start)
1285 (defun edebug-display ()
1286 "Setup windows for edebug, determine mode, maybe enter recursive-edit."
1287 ;; uses local variables of edebug-enter, edebug, and edebug-debugger.
1288 (let ((edebug-active t) ; for minor mode alist
1289 edebug-stop ; should we enter recursive-edit
1290 (edebug-point (+ edebug-func-mark
1291 (aref (car (cdr (cdr edebug-data)))
1292 edebug-offset-index)))
1293 (edebug-buffer-points
1294 (if edebug-save-buffer-points (edebug-get-buffer-points)))
1295 edebug-window ; window displaying edebug-buffer
1296 edebug-inside-window ; window displayed after recursive edit
1297 (edebug-outside-window (selected-window))
1298 (edebug-outside-buffer (current-buffer))
1299 (edebug-outside-point (point))
1300 (edebug-outside-mark (mark))
1301 edebug-outside-windows ; window or screen configuration
1302 edebug-outside-edebug-point ; old point in edebug buffer
1303 edebug-outside-edebug-mark
1305 edebug-eval-buffer ; declared here so we can kill it below
1306 (edebug-eval-result-list (and edebug-eval-list
1307 (edebug-eval-result-list)))
1308 (edebug-outside-o-a-p overlay-arrow-position)
1309 (edebug-outside-o-a-s overlay-arrow-string)
1310 (edebug-outside-c-i-e-a cursor-in-echo-area)
1312 edebug-outside-point-min
1313 edebug-outside-point-max
1315 overlay-arrow-position
1316 overlay-arrow-string
1317 (cursor-in-echo-area nil)
1320 (if (not (buffer-name edebug-buffer))
1321 (let (debug-on-error nil)
1322 (error "Buffer defining %s not found." edebug-func)))
1324 ;; Save windows now before we modify them.
1325 (if edebug-save-windows
1326 (setq edebug-outside-windows
1327 (edebug-current-window-configuration)))
1329 ;; If edebug-buffer is not currently displayed,
1330 ;; first find a window for it.
1331 (edebug-pop-to-buffer edebug-buffer)
1332 (setq edebug-window (selected-window))
1334 ;; Now display eval list, if any.
1335 ;; This is done after the pop to edebug-buffer
1336 ;; so that buffer-window correspondence is correct after quit.
1337 (edebug-eval-display edebug-eval-result-list)
1338 (select-window edebug-window)
1340 (if edebug-save-point
1342 (setq edebug-outside-edebug-point (point))
1343 (setq edebug-outside-edebug-mark (mark))))
1345 (edebug-save-restriction
1346 (setq edebug-outside-point-min (point-min))
1347 (setq edebug-outside-point-max (point-max))
1349 (goto-char edebug-point)
1351 (setq edebug-window-start
1352 (edebug-adjust-window edebug-window-start))
1354 (if (edebug-input-pending-p) ; not including keyboard macros
1356 (setq edebug-mode 'step)
1357 (setq edebug-stop t)
1359 ;; (discard-input) ; is this unfriendly??
1361 (edebug-overlay-arrow)
1364 ((eq 'exit edebug-arg-mode)
1365 ;; Display result of previous evaluation.
1366 (setq edebug-previous-result edebug-exp)
1367 (edebug-previous-result))
1369 ((eq 'error edebug-arg-mode)
1370 ;; Display error message
1372 (if (eq 'quit (car edebug-exp))
1375 (get (car edebug-exp) 'error-message)
1376 (car (cdr edebug-exp)))))
1383 (if (not (memq edebug-mode '(continue Continue-fast)))
1384 (setq edebug-stop t)
1385 (if (eq edebug-mode 'continue)
1387 (edebug-sit-for 0)))
1389 (if (eq edebug-mode 'trace)
1390 (edebug-sit-for 1) ; Force update and pause.
1391 (if (eq edebug-mode 'Trace-fast)
1392 (edebug-sit-for 0) ; Force update and continue.
1397 (eq edebug-mode 'step)
1398 (eq edebug-arg-mode 'error))
1400 (setq edebug-mode 'step)
1401 (edebug-overlay-arrow) ; this doesnt always show up.
1402 (edebug-recursive-edit));; <<<<<< Recursive edit
1405 (if edebug-save-buffer-points
1406 (edebug-set-buffer-points))
1407 ;; Since we may be in a save-excursion, in case of quit
1408 ;; restore the outside window only.
1409 (select-window edebug-outside-window)
1412 ;; None of the following is done if quit or signal occurs.
1413 (if edebug-save-point
1414 ;; Restore point and mark in edebug-buffer.
1415 ;; This does the save-excursion recovery only if no quit.
1416 ;; If edebug-buffer == edebug-outside-buffer,
1417 ;; then this is redundant with outside save-excursion.
1419 (set-buffer edebug-buffer)
1420 (goto-char edebug-outside-edebug-point)
1422 (set-marker (mark-marker) edebug-outside-edebug-mark))
1424 ) ; edebug-save-restriction
1426 ;; Restore windows, buffer, point, and mark.
1427 (if edebug-save-windows
1428 ;; Restore windows before continuing.
1429 (edebug-set-window-configuration edebug-outside-windows))
1430 (set-buffer edebug-outside-buffer)
1431 (goto-char edebug-outside-point)
1433 (set-marker (mark-marker) edebug-outside-mark))
1434 ;; The following is not sufficient, and sometimes annoying.
1435 ;; (if (memq edebug-mode '(go Go-nonstop))
1436 ;; (edebug-sit-for 0))
1440 (defvar edebug-depth 0
1441 "Number of recursive edits started by edebug.
1442 Should be 0 at the top level.")
1444 (defvar edebug-recursion-depth 0
1445 "Value of recursion-depth when edebug was called.")
1448 (defun edebug-recursive-edit ()
1449 "Start up a recursive edit inside of edebug."
1450 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
1451 (let ((edebug-buffer-read-only buffer-read-only)
1452 ;; match-data must be done in the outside buffer
1453 (edebug-outside-match-data
1455 (set-buffer edebug-outside-buffer)
1458 (edebug-depth (1+ edebug-depth))
1459 (edebug-recursion-depth (recursion-depth))
1460 edebug-entered ; bind locally to nil
1461 edebug-backtrace-buffer ; each recursive edit gets its own
1462 ;; The window configuration may be saved and restored
1463 ;; during a recursive-edit
1464 edebug-inside-windows
1466 (edebug-outside-map (current-local-map))
1467 (edebug-outside-standard-output standard-output)
1468 (edebug-outside-standard-input standard-input)
1470 (edebug-outside-last-command-char last-command-char)
1471 (edebug-outside-last-command last-command)
1472 (edebug-outside-this-command this-command)
1473 (edebug-outside-last-input-char last-input-char)
1474 ;; (edebug-outside-unread-command-char unread-command-char)
1476 ;; Declare the following local variables to protect global values.
1477 ;; We could set these to the values for previous edebug call.
1478 ;; But instead make it local, but use global value.
1479 (last-command-char last-command-char)
1480 (last-command last-command)
1481 (this-command this-command)
1482 (last-input-char last-input-char)
1483 ;; Assume no edebug command sets unread-command-char.
1484 ;; (unread-command-char -1)
1486 (debug-on-error debug-on-error)
1491 (if (and (eq edebug-mode 'go)
1492 (not (memq edebug-arg-mode '(exit error))))
1495 (if (boundp 'edebug-outside-debug-on-error)
1496 (setq debug-on-error edebug-outside-debug-on-error))
1498 (setq buffer-read-only t)
1500 (recursive-edit) ; <<<<<<<<<< Recursive edit
1502 ;; Do the following, even if quit occurs.
1503 (if edebug-backtrace-buffer
1504 (kill-buffer edebug-backtrace-buffer))
1505 ;; Could be an option to keep eval display up.
1506 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
1508 ;; Remember selected-window after recursive-edit.
1509 (setq edebug-inside-window (selected-window))
1511 (store-match-data edebug-outside-match-data)
1513 ;; Recursive edit may have changed buffers,
1514 ;; so set it back before exiting let.
1515 (if (buffer-name edebug-buffer) ; if it still exists
1517 (set-buffer edebug-buffer)
1518 (if (memq edebug-mode '(go Go-nonstop))
1519 (edebug-overlay-arrow))
1520 (setq buffer-read-only edebug-buffer-read-only)
1521 (use-local-map edebug-outside-map)
1522 ;; Remember current window-start for next visit.
1523 (select-window edebug-window)
1524 (if (eq edebug-buffer (window-buffer edebug-window))
1525 (setq edebug-window-start (window-start)))
1526 (select-window edebug-inside-window)
1531 ;;--------------------------
1532 ;; Display related functions
1534 (defun edebug-adjust-window (old-start)
1535 "Adjust window to fit as much as possible following point.
1536 The display should prefer to start at OLD-START if point is not visible.
1537 Return the new window-start."
1538 (if (not (pos-visible-in-window-p))
1540 (set-window-start (selected-window) old-start)
1541 (if (not (pos-visible-in-window-p))
1542 (let ((start (window-start))
1548 (if (< pnt start) -1 ; one line before
1549 (- (/ (window-height) 2)) ; center the line
1556 (defconst edebug-arrow-alist
1557 '((Continue-fast . ">")
1563 (Go-nonstop . "..") ; not used
1565 "Association list of arrows for each edebug mode.
1566 If you come up with arrows that make more sense, let me know.")
1568 (defun edebug-overlay-arrow ()
1569 "Set up the overlay arrow at beginning-of-line in current buffer.
1570 The arrow string is derived from edebug-arrow-alist and edebug-mode."
1575 (setq overlay-arrow-string
1576 (cdr (assq edebug-mode edebug-arrow-alist)))
1577 (setq overlay-arrow-position (make-marker))
1578 (set-marker overlay-arrow-position pos (current-buffer))))
1581 (put 'edebug-outside-excursion 'edebug-form-hook
1584 (defmacro edebug-outside-excursion (&rest body)
1585 "Evaluate an expression list in the outside context.
1586 Return the result of the last expression."
1587 (` (save-excursion ; of current-buffer
1588 (if edebug-save-windows
1590 ;; After excursion, we will
1591 ;; restore to current window configuration.
1592 (setq edebug-inside-windows
1593 (edebug-current-window-configuration))
1594 ;; Restore outside windows.
1595 (edebug-set-window-configuration edebug-outside-windows)))
1597 (set-buffer edebug-buffer)
1598 ;; Restore outside context.
1599 (let ((edebug-inside-map (current-local-map))
1600 (last-command-char edebug-outside-last-command-char)
1601 (last-command edebug-outside-last-command)
1602 (this-command edebug-outside-this-command)
1603 ;; (unread-command-char edebug-outside-unread-command-char)
1604 (last-input-char edebug-outside-last-input-char)
1605 (overlay-arrow-position edebug-outside-o-a-p)
1606 (overlay-arrow-string edebug-outside-o-a-s)
1607 (cursor-in-echo-area edebug-outside-c-i-e-a)
1608 (standard-output edebug-outside-standard-output)
1609 (standard-input edebug-outside-standard-input)
1610 (executing-macro edebug-outside-executing-macro)
1614 (narrow-to-region edebug-outside-point-min
1615 edebug-outside-point-max)
1616 (save-excursion ; of edebug-buffer
1617 (if edebug-save-point
1619 (goto-char edebug-outside-edebug-point)
1621 (set-marker (mark-marker)
1622 edebug-outside-edebug-mark))
1624 (use-local-map edebug-outside-map)
1625 (store-match-data edebug-outside-match-data)
1626 (select-window edebug-outside-window)
1627 (set-buffer edebug-outside-buffer)
1628 (goto-char edebug-outside-point)
1631 ) ; save-restriction
1632 ;; Back to edebug-buffer. Restore rest of inside context.
1633 (use-local-map edebug-inside-map)
1634 (if edebug-save-windows
1635 ;; Restore inside windows.
1636 (edebug-set-window-configuration edebug-inside-windows))
1641 (defun edebug-toggle-save-windows ()
1642 "Toggle the edebug-save-windows variable.
1643 Each time you toggle it, the inside and outside window configurations
1644 become the same as the current configuration."
1646 (if (setq edebug-save-windows (not edebug-save-windows))
1647 (setq edebug-inside-windows
1648 (setq edebug-outside-windows
1649 (edebug-current-window-configuration))))
1650 (message "Window saving is %s."
1651 (if edebug-save-windows "on" "off")))
1654 (defun edebug-where ()
1655 "Show the debug windows and where we stopped in the program."
1657 (if (not edebug-active)
1658 (error "edebug is not active."))
1659 (edebug-pop-to-buffer edebug-buffer)
1660 (goto-char edebug-point) ; from edebug
1663 (defun edebug-view-outside ()
1664 "Change to the outside window configuration."
1666 (if (not edebug-active)
1667 (error "edebug is not active."))
1668 (setq edebug-inside-windows (edebug-current-window-configuration))
1669 (edebug-set-window-configuration edebug-outside-windows)
1670 (goto-char edebug-outside-point)
1671 (message "Window configuration outside of edebug. Return with %s"
1672 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
1675 (defun edebug-bounce-point ()
1676 "Bounce the point in the outside current buffer."
1678 (if (not edebug-active)
1679 (error "edebug is not active."))
1681 ;; If the buffer's currently displayed, avoid the set-window-configuration.
1682 (save-window-excursion
1683 (edebug-pop-to-buffer edebug-outside-buffer)
1684 ;; (edebug-sit-for 1) ; this shouldnt be necessary
1685 (goto-char edebug-outside-point)
1686 ;; (message "current buffer: %s" (current-buffer))
1688 (edebug-pop-to-buffer edebug-buffer))))
1692 ;;--------------------------
1693 ;; epoch related things
1695 (defvar edebug-epoch-running (and (boundp 'epoch::version) epoch::version)
1696 "non-nil if epoch is running.
1697 Windows are handled a little differently under epoch.")
1700 (defun edebug-current-window-configuration ()
1701 "Return the current window or frame configuration."
1702 (if edebug-epoch-running
1703 (edebug-current-screen-configuration)
1704 (current-window-configuration)))
1707 (defun edebug-set-window-configuration (conf)
1708 "Set the window or frame configuration to CONF."
1709 (if edebug-epoch-running
1710 (edebug-set-screen-configuration conf)
1711 (set-window-configuration conf)))
1714 (defun edebug-get-buffer-window (buffer)
1715 (if edebug-epoch-running
1716 (epoch::get-buffer-window buffer)
1717 (get-buffer-window buffer)))
1720 (defun edebug-pop-to-buffer (buffer)
1721 "Like pop-to-buffer, but select a frame that buffer was shown in."
1722 (let ((edebug-window (edebug-get-buffer-window buffer)))
1724 (select-window edebug-window)
1725 ;; It is not currently displayed, so find some place to display it.
1726 (if edebug-epoch-running
1727 ;; Select a screen that the buffer has been displayed in before
1728 ;; or the current screen otherwise.
1730 ;; allowed-screens in epoch 3.2, was called screens before that
1731 (or (car (symbol-buffer-value 'allowed-screens buffer))
1732 (epoch::current-screen))))
1735 (select-window (next-window))
1736 (set-window-buffer (selected-window) buffer)
1737 (set-window-hscroll (selected-window) 0)
1739 ;; Selecting the window does not set the buffer.
1744 (defun edebug-current-screen-configuration ()
1745 "Return an object recording the current configuration of Epoch screen-list.
1746 The object is a list of pairs of the form (SCREEN . CONFIGURATION)
1747 where SCREEN has window-configuration CONFIGURATION. The current
1748 screen is the head of the list."
1749 (let ((screen-list (epoch::screen-list 'unmapped))
1750 (current-screen (epoch::get-screen))
1751 (current-buffer (current-buffer))
1753 ;; put current screen first
1754 (setq screen-list (cons current-screen (delq current-screen screen-list)))
1760 (epoch::select-screen screen)
1761 (current-window-configuration)))))
1763 (epoch::select-screen current-screen)
1764 (set-buffer current-buffer)
1767 (defun edebug-set-screen-configuration (sc)
1768 "Set the window-configuration for all the screens in SC.
1769 Set the current screen to be the head of SC."
1771 (lambda (screen-conf)
1772 (if (epoch::screen-p (car screen-conf)) ; still exist?
1774 (epoch::select-screen (car screen-conf))
1775 (set-window-configuration (cdr screen-conf))))))
1777 (if (epoch::screen-p (car (car sc)))
1778 (epoch::select-screen (car (car sc))))
1782 (defun edebug-sit-for (arg)
1783 (if edebug-epoch-running
1784 (epoch::dispatch-events))
1788 (defun edebug-input-pending-p ()
1789 (if edebug-epoch-running
1790 (epoch::dispatch-events))
1796 ;;--------------------------
1797 ;; breakpoint related functions
1799 (defun edebug-find-stop-point ()
1800 "Return (function . index) of the nearest edebug stop point."
1801 (let* ((def-name (edebug-which-function))
1803 (or (get def-name 'edebug)
1805 "%s must first be evaluated with edebug-defun." def-name)))
1806 ;; pull out parts of edebug-data.
1807 (edebug-func-mark (car edebug-data))
1808 (edebug-breakpoints (car (cdr edebug-data)))
1810 (offset-vector (car (cdr (cdr edebug-data))))
1811 (offset (- (save-excursion
1812 (if (looking-at "[ \t]")
1813 ;; skip backwards until non-whitespace, or bol
1814 (skip-chars-backward " \t"))
1818 ;; the offsets are in order so we can do a linear search
1819 (setq len (length offset-vector))
1821 (while (and (< i len) (> offset (aref offset-vector i)))
1824 (<= offset (aref offset-vector i)))
1825 ;; return the relevant info
1827 (message "Point is not on an expression in %s."
1832 (defun edebug-next-breakpoint ()
1833 "Move point to the next breakpoint, or first if none past point."
1835 (let ((edebug-stop-point (edebug-find-stop-point)))
1836 (if edebug-stop-point
1837 (let* ((def-name (car edebug-stop-point))
1838 (index (cdr edebug-stop-point))
1839 (edebug-data (get def-name 'edebug))
1841 ;; pull out parts of edebug-data
1842 (edebug-func-mark (car edebug-data))
1843 (edebug-breakpoints (car (cdr edebug-data)))
1844 (offset-vector (car (cdr (cdr edebug-data))))
1846 (if (not edebug-breakpoints)
1847 (message "No breakpoints in this function.")
1848 (let ((breaks edebug-breakpoints))
1850 (<= (car (car breaks)) index))
1851 (setq breaks (cdr breaks)))
1855 ;; goto the first breakpoint
1856 (car edebug-breakpoints)))
1857 (goto-char (+ edebug-func-mark
1858 (aref offset-vector (car breakpoint))))
1860 (message (concat (if (car (cdr (cdr breakpoint)))
1862 (if (car (cdr breakpoint))
1863 (format "Condition: %s"
1865 (car (cdr breakpoint))))
1870 (defun edebug-modify-breakpoint (flag &optional condition temporary)
1871 "Modify the breakpoint for the form at point or after it according
1872 to FLAG: set if t, clear if nil. Then move to that point.
1873 If CONDITION or TEMPORARY are non-nil, add those attributes to
1875 (let ((edebug-stop-point (edebug-find-stop-point)))
1876 (if edebug-stop-point
1877 (let* ((def-name (car edebug-stop-point))
1878 (index (cdr edebug-stop-point))
1879 (edebug-data (get def-name 'edebug))
1881 ;; pull out parts of edebug-data
1882 (edebug-func-mark (car edebug-data))
1883 (edebug-breakpoints (car (cdr edebug-data)))
1884 (offset-vector (car (cdr (cdr edebug-data))))
1886 ;; delete it either way
1887 (setq present (assq index edebug-breakpoints))
1888 (setq edebug-breakpoints (delq present edebug-breakpoints))
1891 ;; add it to the list and resort
1892 (setq edebug-breakpoints
1895 (list index condition temporary)
1896 edebug-breakpoints) '<))
1897 (message "Breakpoint set in %s." def-name))
1899 (message "Breakpoint unset in %s." def-name)
1900 (message "No breakpoint here.")))
1903 (cons edebug-breakpoints (cdr (cdr edebug-data))))
1904 (goto-char (+ edebug-func-mark (aref offset-vector index)))
1907 (defun edebug-set-breakpoint (arg)
1908 "Set the breakpoint of nearest sexp.
1909 With prefix argument, make it a temporary breakpoint."
1911 (edebug-modify-breakpoint t nil arg))
1913 (defun edebug-unset-breakpoint ()
1914 "Clear the breakpoint of nearest sexp."
1916 (edebug-modify-breakpoint nil))
1918 (defun edebug-set-conditional-breakpoint (arg condition)
1919 "Set a conditional breakpoint at nearest sexp.
1920 The condition is evaluated in the outside context.
1921 With prefix argument, make it a temporary breakpoint."
1922 (interactive "P\nxCondition: ")
1923 (edebug-modify-breakpoint t condition arg))
1926 ;;--------------------------
1927 ;; Mode switching functions
1929 (defun edebug-set-mode (mode shortmsg msg)
1930 "Set the edebug mode to MODE.
1931 Display SHORTMSG, or MSG if not within edebug."
1933 (setq edebug-mode mode)
1934 (if (< 0 edebug-depth)
1935 (if (eq (current-buffer) edebug-buffer)
1938 (exit-recursive-edit)))
1942 (defun edebug-step-through ()
1943 "Proceed to next debug step."
1945 (edebug-set-mode 'step "" "edebug will stop before next eval."))
1947 (defun edebug-go (arg)
1948 "Go, evaluating until break.
1949 With ARG set temporary break at stop point and go."
1952 (edebug-set-breakpoint t))
1953 (edebug-set-mode 'go "Go..." "edebug will go until break."))
1955 (defun edebug-Go-nonstop ()
1956 "Go, evaluating without debugging."
1958 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
1959 "edebug will not stop at breaks."))
1961 (defun edebug-forward-sexp (arg)
1962 "Proceed from the current point to the end of the ARGth sexp ahead.
1963 If there are not ARG sexps ahead, then do edebug-step-out."
1966 (let ((parse-sexp-ignore-comments t))
1967 ;; Call forward-sexp repeatedly until done or failure.
1974 (defun edebug-step-out ()
1975 "Proceed from the current point to the end of the containing sexp.
1976 If there is no containing sexp that is not the top level defun,
1977 go to the end of the last sexp, or if that is the same point, then step."
1980 (let ((parse-sexp-ignore-comments t))
1983 ;; Is there still a containing expression?
1987 ;; At top level - 1, so first check if there are more sexps at this level.
1988 (let ((start-point (point)))
1991 (if (= (point) start-point)
1992 (edebug-step-through) ; No more at this level, so step.
1997 (defun edebug-goto-here ()
1998 "Proceed to this stop point."
2003 (defun edebug-trace ()
2006 (edebug-set-mode 'trace "Tracing..." "edebug will trace with pause."))
2008 (defun edebug-Trace-fast ()
2009 "Trace with no wait at each step."
2011 (edebug-set-mode 'Trace-fast
2012 "Trace fast..." "edebug will trace without pause."))
2014 (defun edebug-continue ()
2015 "Begin continue mode."
2017 (edebug-set-mode 'continue "Continue..."
2018 "edebug will pause at breakpoints."))
2020 (defun edebug-Continue-fast ()
2021 "Trace with no wait at each step."
2023 (edebug-set-mode 'Continue-fast "Continue fast..."
2024 "edebug will stop and go at breakpoints."))
2027 (defun edebug-step-in ()
2028 "Step into the function about to be called.
2029 Do this before the arguments are evaluated since otherwise it will be
2030 too late. One side effect of using edebug-step-in is that the next
2031 time the function is called, edebug will be called there as well."
2033 (if (not (eq 'enter edebug-arg-mode))
2034 (error "You must be in front of a function or macro call."))
2035 (let* ((func (car edebug-exp))
2036 (func-marker (get func 'edebug)))
2038 ((markerp func-marker)
2040 (set-buffer (marker-buffer func-marker))
2041 (goto-char func-marker)
2043 ((listp func-marker)
2044 ;; its already been evaluated for edebug
2046 (t (error "You must first evaluate %s in a buffer." func))))
2047 (exit-recursive-edit))
2050 ;;(defun edebug-exit-out ()
2051 ;; "Go until the current function exits."
2053 ;; (edebug-set-mode 'exiting "Exit..."))
2056 (defun edebug-stop ()
2057 "Useful for exiting from trace loop."
2062 ;;; The following initial mode setting definitions are not used yet.
2064 (defconst edebug-initial-mode-alist
2065 '((edebug-Continue-fast . Continue-fast)
2066 (edebug-Trace-fast . Trace-fast)
2067 (edebug-continue . continue)
2068 (edebug-trace . trace)
2070 (edebug-step-through . step)
2071 (edebug-Go-nonstop . Go-nonstop)
2073 "Association list between commands and the modes they set.")
2076 (defun edebug-set-initial-mode ()
2077 "Ask for the initial mode of the enclosing function.
2078 The mode is requested via the key that would be used to set the mode in
2081 (let* ((this-function (edebug-which-function))
2082 (keymap (if (eq edebug-mode-map (current-local-map))
2084 (old-mode (or (get this-function 'edebug-initial-mode)
2085 edebug-initial-mode))
2086 (key (read-key-sequence
2088 "Change initial edebug mode for %s from %s (%s) to (enter key): "
2092 (car (rassq old-mode edebug-initial-mode-alist))
2095 (mode (cdr (assq (key-binding key) edebug-initial-mode-alist)))
2098 (or (get this-function 'edebug-initial-mode)
2099 (not (eq mode edebug-initial-mode))))
2101 (put this-function 'edebug-initial-mode mode)
2102 (message "Initial mode for %s is now: %s"
2103 this-function mode))
2104 (error "Key must map to one of the mode changing commands.")
2109 ;;--------------------------
2110 ;; Evaluation of expressions
2112 (defvar edebug-previous-result nil
2113 "Last result returned from an expression.")
2115 (defun edebug-previous-result ()
2116 "Return the previous result."
2118 (let ((print-escape-newlines t)
2120 (message "Result: %s" (prin1-to-string edebug-previous-result))))
2123 (defun edebug-eval (expr)
2124 "Evaluate EXPR in the outside environment."
2125 (if (not edebug-active)
2126 (error "edebug is not active."))
2127 (edebug-outside-excursion
2130 (defun edebug-eval-expression (expr)
2131 "Prompt and evaluate an expression in the outside environment.
2132 Print result in minibuffer."
2133 (interactive "xEval: ")
2134 (prin1 (edebug-eval expr)))
2136 (defun edebug-eval-last-sexp ()
2137 "Evaluate sexp before point in the outside environment;
2138 print value in minibuffer."
2140 (prin1 (edebug-eval (edebug-last-sexp))))
2142 (defun edebug-eval-print-last-sexp ()
2143 "Evaluate sexp before point in the outside environment;
2144 print value into current buffer."
2146 (let ((standard-output (current-buffer)))
2149 (edebug-eval (edebug-last-sexp))
2150 (error (format "%s: %s"
2151 (get (car err) 'error-message)
2152 (car (cdr err))))))))
2154 ;;;---------------------------------
2155 ;;; edebug minor mode initialization
2157 (defvar edebug-mode 'step
2158 "Current edebug mode set by user.")
2160 (defvar edebug-mode-map nil)
2164 (setq edebug-mode-map (copy-keymap emacs-lisp-mode-map))
2166 (define-key edebug-mode-map " " 'edebug-step-through)
2167 (define-key edebug-mode-map "g" 'edebug-go)
2168 (define-key edebug-mode-map "G" 'edebug-Go-nonstop)
2169 (define-key edebug-mode-map "t" 'edebug-trace)
2170 (define-key edebug-mode-map "T" 'edebug-Trace-fast)
2171 (define-key edebug-mode-map "c" 'edebug-continue)
2172 (define-key edebug-mode-map "C" 'edebug-Continue-fast)
2174 (define-key edebug-mode-map "f" 'edebug-forward-sexp)
2175 (define-key edebug-mode-map "h" 'edebug-goto-here)
2177 (define-key edebug-mode-map "r" 'edebug-previous-result)
2179 (define-key edebug-mode-map "i" 'edebug-step-in)
2180 (define-key edebug-mode-map "o" 'edebug-step-out)
2182 ;; (define-key edebug-mode-map "m" 'edebug-set-initial-mode)
2184 (define-key edebug-mode-map "q" 'top-level)
2185 (define-key edebug-mode-map "a" 'abort-recursive-edit)
2186 (define-key edebug-mode-map "S" 'edebug-stop)
2189 (define-key edebug-mode-map "b" 'edebug-set-breakpoint)
2190 (define-key edebug-mode-map "u" 'edebug-unset-breakpoint)
2191 (define-key edebug-mode-map "B" 'edebug-next-breakpoint)
2192 (define-key edebug-mode-map "x" 'edebug-set-conditional-breakpoint)
2195 (define-key edebug-mode-map "e" 'edebug-eval-expression)
2196 (define-key edebug-mode-map "\C-x\C-e" 'edebug-eval-last-sexp)
2197 (define-key edebug-mode-map "E" 'edebug-visit-eval-list)
2200 (define-key edebug-mode-map "w" 'edebug-where)
2201 (define-key edebug-mode-map "v" 'edebug-view-outside)
2202 (define-key edebug-mode-map "p" 'edebug-bounce-point)
2203 (define-key edebug-mode-map "W" 'edebug-toggle-save-windows)
2206 (define-key edebug-mode-map "?" 'edebug-help)
2207 (define-key edebug-mode-map "d" 'edebug-backtrace)
2209 (define-key edebug-mode-map "-" 'negative-argument)
2213 (defvar global-edebug-prefix "\^XX"
2214 "Prefix key for global edebug commands, available from any buffer.")
2216 (defvar global-edebug-map nil
2217 "Global map of edebug commands, available from any buffer.")
2219 (if global-edebug-map
2221 (setq global-edebug-map (make-sparse-keymap))
2223 (global-unset-key global-edebug-prefix)
2224 (global-set-key global-edebug-prefix global-edebug-map)
2226 ;; (define-key global-edebug-map "X" 'edebug-step-through)
2227 (define-key global-edebug-map " " 'edebug-step-through)
2228 (define-key global-edebug-map "g" 'edebug-go)
2229 (define-key global-edebug-map "G" 'edebug-Go-nonstop)
2230 (define-key global-edebug-map "t" 'edebug-trace)
2231 (define-key global-edebug-map "T" 'edebug-Trace-fast)
2232 (define-key global-edebug-map "c" 'edebug-continue)
2233 (define-key global-edebug-map "C" 'edebug-Continue-fast)
2235 ;; (define-key global-edebug-map "m" 'edebug-set-initial-mode)
2236 (define-key global-edebug-map "b" 'edebug-set-breakpoint)
2237 (define-key global-edebug-map "x" 'edebug-set-conditional-breakpoint)
2238 (define-key global-edebug-map "u" 'edebug-unset-breakpoint)
2239 (define-key global-edebug-map "w" 'edebug-where)
2240 (define-key global-edebug-map "q" 'top-level)
2244 (defun edebug-help ()
2246 (describe-function 'edebug-mode))
2249 (defun edebug-mode ()
2250 "Mode for Emacs Lisp buffers while in edebug. Under construction.
2252 There are both buffer local and global key bindings to several
2253 functions. E.g. edebug-step-through is bound to
2254 \\[edebug-step-through] in the debug buffer and
2255 \\<global-map>\\[edebug-step-through] in any buffer.
2257 edebug buffer commands:
2260 Global commands prefixed by global-edbug-prefix:
2261 \\{global-edebug-map}
2265 edebug-eval-macro-args
2266 edebug-stop-before-symbols
2269 edebug-save-buffer-points
2273 (use-local-map edebug-mode-map))
2277 ;;===============================================
2278 ;; edebug eval list mode
2279 ;; A list of expressions and their evaluations is displayed
2280 ;; in edebug-eval-buffer
2282 (defvar edebug-eval-list nil
2283 "List of expressions to evaluate.")
2285 ;;(defvar edebug-eval-buffer "*edebug*"
2286 ;; "*Declared globally so edebug-eval-display can be called independent
2287 ;;of edebug (not implemented yet).")
2290 (defun edebug-eval-result-list ()
2291 "Return a list of evaluations of edebug-eval-list"
2292 ;; Assumes in outside environment.
2297 (error (format "%s: %s"
2298 (get (car err) 'error-message)
2303 (defun edebug-eval-display-list (edebug-eval-result-list)
2304 ;; Assumes edebug-eval-buffer exists.
2305 (let ((edebug-eval-list-temp edebug-eval-list)
2306 (standard-output edebug-eval-buffer)
2307 (edebug-display-line
2308 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
2309 (edebug-pop-to-buffer edebug-eval-buffer)
2311 (while edebug-eval-list-temp
2312 (prin1 (car edebug-eval-list-temp)) (terpri)
2313 (prin1 (car edebug-eval-result-list)) (terpri)
2314 (princ edebug-display-line)
2315 (setq edebug-eval-list-temp (cdr edebug-eval-list-temp))
2316 (setq edebug-eval-result-list (cdr edebug-eval-result-list)))
2319 (defun edebug-create-eval-buffer ()
2320 (if (not (and edebug-eval-buffer (buffer-name edebug-eval-buffer)))
2322 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
2323 (edebug-eval-mode))))
2325 ;; Should generalize this to be callable outside of edebug
2326 ;; with calls in user functions, e.g. (edebug-eval-display)
2328 (defun edebug-eval-display (edebug-eval-result-list)
2329 "Display expressions and evaluations in EVAL-LIST.
2330 It modifies the context by popping up the eval display."
2331 (if edebug-eval-result-list
2333 (edebug-create-eval-buffer)
2334 (edebug-pop-to-buffer edebug-eval-buffer)
2335 (edebug-eval-display-list edebug-eval-result-list)
2338 (defun edebug-eval-redisplay ()
2339 "Redisplay eval list in outside environment.
2340 May only be called from within edebug-recursive-edit."
2341 (edebug-create-eval-buffer)
2342 (edebug-pop-to-buffer edebug-eval-buffer)
2343 (edebug-outside-excursion
2344 (edebug-eval-display-list (edebug-eval-result-list))
2347 (defun edebug-visit-eval-list ()
2349 (edebug-eval-redisplay)
2350 (edebug-pop-to-buffer edebug-eval-buffer))
2353 (defun edebug-update-eval-list ()
2354 "Replace the evaluation list with the sexps now in the eval buffer."
2356 (let ((starting-point (point))
2358 (goto-char (point-min))
2359 ;; get the first expression
2360 (edebug-skip-whitespace)
2364 (setq new-list (cons (edebug-last-sexp) new-list))))
2366 (while (re-search-forward "^;" nil t)
2368 (skip-chars-forward " \t\n\r")
2369 (if (and (/= ?\; (following-char))
2373 (setq new-list (cons (edebug-last-sexp) new-list)))))
2375 (setq edebug-eval-list (nreverse new-list))
2376 (edebug-eval-redisplay)
2377 (goto-char starting-point)))
2380 (defun edebug-delete-eval-item ()
2381 "Delete the item under point and redisplay."
2382 ;; could add arg to do repeatedly
2384 (if (re-search-backward "^;" nil 'nofail)
2387 (point) (progn (re-search-forward "^;" nil 'nofail)
2390 (edebug-update-eval-list))
2394 (defvar edebug-eval-mode-map nil
2395 "Keymap for edebug-eval-mode. Superset of lisp-interaction-mode.")
2397 (if edebug-eval-mode-map
2399 (setq edebug-eval-mode-map (copy-keymap lisp-interaction-mode-map))
2401 (define-key edebug-eval-mode-map "\C-c\C-w" 'edebug-where)
2402 (define-key edebug-eval-mode-map "\C-c\C-d" 'edebug-delete-eval-item)
2403 (define-key edebug-eval-mode-map "\C-c\C-u" 'edebug-update-eval-list)
2404 (define-key edebug-eval-mode-map "\C-x\C-e" 'edebug-eval-last-sexp)
2405 (define-key edebug-eval-mode-map "\C-j" 'edebug-eval-print-last-sexp)
2409 (defun edebug-eval-mode ()
2410 "Mode for data display buffer while in edebug. Under construction.
2411 ... ignore the following...
2412 There are both buffer local and global key bindings to several
2413 functions. E.g. edebug-step-through is bound to
2414 \\[edebug-step-through] in the debug buffer and
2415 \\<global-map>\\[edebug-step-through] in any buffer.
2417 Eval list buffer commands:
2418 \\{edebug-eval-mode-map}
2420 Global commands prefixed by global-edbug-prefix:
2421 \\{global-edebug-map}
2423 (lisp-interaction-mode)
2424 (setq major-mode 'edebug-eval-mode)
2425 (setq mode-name "Edebug-Eval")
2426 (use-local-map edebug-eval-mode-map))
2429 ;;========================================
2430 ;; Interface with standard debugger.
2432 (setq debugger 'edebug-debug)
2433 ;; (setq debugger 'debug) ; use the default
2435 ;; Note that debug and its utilities must be byte-compiled to work, since
2436 ;; they depend on the backtrace looking a certain way.
2439 (defun edebug-debug (&rest debugger-args)
2440 "Replacement for debug.
2441 If an error or quit occurred and we are running an edebugged function,
2442 show where we last were. Otherwise call debug normally."
2443 (if (and edebug-backtrace ; anything active?
2444 (eq (recursion-depth) edebug-recursion-depth)
2447 ;; Where were we before the error occurred?
2448 (let ((edebug-offset-index (car edebug-offset-indices))
2449 (edebug-arg-mode (car debugger-args))
2450 (edebug-exp (car (cdr debugger-args)))
2453 (edebug-outside-debug-on-eror debug-on-error)
2454 (debug-on-error nil))
2458 ;; Otherwise call debug normally.
2459 ;; Still need to remove extraneous edebug calls from stack.
2460 (apply 'debug debugger-args)
2464 (defun edebug-backtrace ()
2465 "Display a non-working backtrace. Better than nothing..."
2467 (let ((old-buf (current-buffer)))
2468 (if (not edebug-backtrace-buffer)
2469 (setq edebug-backtrace-buffer
2470 (let ((default-major-mode 'fundamental-mode))
2471 (generate-new-buffer "*Backtrace*"))))
2472 (edebug-pop-to-buffer edebug-backtrace-buffer)
2474 (let ((standard-output (current-buffer))
2475 (print-escape-newlines t)
2479 (setq truncate-lines t)
2482 ;; Clean up the backtrace.
2483 (goto-char (point-min))
2487 ;; Everything up to the first edebug is internal.
2488 (re-search-forward "^ edebug(")
2492 (setq last-ok-point (point))
2494 ;; Delete interspersed edebug internals.
2495 (while (re-search-forward "^ edebug" nil t)
2496 (if (looking-at "-enter")
2497 ;; delete extraneous progn at top level of function body
2499 (goto-char last-ok-point)
2501 (setq last-ok-point (point))))
2503 (delete-region last-ok-point (point))
2504 (forward-line 1) ; skip past the good line
2505 (setq last-ok-point (point))
2508 (edebug-pop-to-buffer old-buf)
2512 ;;========================================================================
2513 ;; Trace display - append text to a buffer, and update display.
2515 ;;; (edebug-trace-display
2517 ;;; "saving: point = %s window-start = %s\n"
2518 ;;; (point) (window-start))
2520 (defun edebug-trace-display (buf-name fmt &rest args)
2521 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
2522 The buffer is created if it does not exist.
2523 You must include newlines in FMT to break lines."
2524 (let* ((selected-window (selected-window))
2525 (buffer (get-buffer-create buf-name))
2527 (edebug-pop-to-buffer buffer)
2529 (setq buf-window (selected-window))
2531 (goto-char (point-max))
2532 (insert (apply 'format fmt args))
2533 (set-window-point buf-window (point))
2534 (forward-line (- 1 (window-height buf-window)))
2535 (set-window-start buf-window (point))
2536 ;; (edebug-sit-for 1)
2537 (bury-buffer buffer)
2539 (select-window selected-window)))
2541 ;;; edebug.el ends here