1 ;;; edebug.el --- a source-level debugger for Emacs Lispl
3 ;; Copyright (C) 1988,'89,'90,'91,'92,'93,'94 Free Software Foundation, Inc
5 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
6 ;; Keywords: lisp, tools, maint
9 ;; edebug|Daniel LaLiberte|liberte@cs.uiuc.edu
10 ;; |A source level debugger for Emacs Lisp.
11 ;; |$Date: 1994/04/05 01:21:06 $|$Revision: 3.5 $|~/modes/edebug.el|
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to
27 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 ;;; This minor mode allows programmers to step through Emacs Lisp
32 ;;; source code while executing functions. You can also set
33 ;;; breakpoints, trace (stopping at each expression), evaluate
34 ;;; expressions as if outside Edebug, reevaluate and display a list of
35 ;;; expressions, trap errors normally caught by debug, and display a
36 ;;; debug style backtrace.
41 ;;; Put edebug.el in some directory in your load-path and
42 ;;; byte-compile it. Also read the beginning of edebug-epoch.el,
43 ;;; cl-specs.el, and edebug-cl-read.el if they apply to you.
45 ;;; Unless you are using Emacs 19 which is already set up to use Edebug,
46 ;;; put the following forms in your .emacs file.
47 ;;; (define-key emacs-lisp-mode-map "\C-xx" 'edebug-eval-top-level-form)
48 ;;; (autoload 'edebug-eval-top-level-form "edebug")
50 ;;; If you wish to change the default edebug global command prefix, change:
51 ;;; (setq edebug-global-prefix "\C-xX")
53 ;;; Other options, are described in the manual.
55 ;;; In previous versions of Edebug, users were directed to set
56 ;;; `debugger' to `edebug-debug'. This is no longer necessary
57 ;;; since Edebug automatically sets it whenever Edebug is active.
59 ;;;; Minimal Instructions
60 ;;; =====================
62 ;;; First evaluate a defun with C-xx, then run the function. Step
63 ;;; through the code with SPC, mark breakpoints with b, go until a
64 ;;; breakpoint is reached with g, and quit execution with q. Use the
65 ;;; "?" command in edebug to describe other commands. See edebug.tex
66 ;;; or the Emacs 19 Lisp Reference Manual for more instructions.
68 ;;; Send me your enhancements, ideas, bugs, or fixes.
69 ;;; For bugs, you can call edebug-submit-bug-report if you have reporter.el.
70 ;;; There is an edebug mailing list if you want to keep up
71 ;;; with the latest developments. Requests to: edebug-request@cs.uiuc.edu
73 ;;; Daniel LaLiberte 217-398-4114
74 ;;; University of Illinois, Urbana-Champaign
75 ;;; Department of Computer Science
76 ;;; 1304 W Springfield
80 ;;; liberte@cs.uiuc.edu
82 ;;; ===============================
83 ;;; $Header: /import/kaplan/kaplan/liberte/Edebug/RCS/edebug.el,v 3.5 1994/04/08 21:39:52 liberte Exp liberte $
84 ;;; $Log: edebug.el,v $
85 ;;; Revision 3.5 1994/04/04 21:39:52 liberte
86 ;;; * Change "-emacs-" to "-original-" throughout.
87 ;;; * (edebug-last-sexp) Fix missing ";;"
88 ;;; * (edebug-read) Fix docstring, &optional arg,
89 ;;; and the check that stream is current-buffer.
90 ;;; * (edebug-install-read-eval-functions) Use (elisp-eval-region-install).
91 ;;; * (edebug-uninstall-read-eval-functions) Restore to symbol-function values.
92 ;;; * (edebug-eval-defun) Repair check for edebug-all-defs.
93 ;;; * (edebug-top-level-form) Set edebug-all-defs to t.
94 ;;; * (edebug-read-and-maybe-wrap-form1) Call edebug-original-read
96 ;;; * (edebug-enter) Bind pre-command-hook and post-command-hook to nil.
97 ;;; Reset these variables and executing-macro to outside values.
98 ;;; * (edebug-display) Reset global variables to outside values.
99 ;;; * (edebug-recursive-edit) Reset global variables to outside values.
100 ;;; * (edebug-outside-excursion) Set outside values of global variables
101 ;;; in case they were changed by side effect.
102 ;;; * (edebug-instrument-callee) Use edebug-original-read instaed of read.
103 ;;; * (edebug-eval-result-list) While evaluating evaluation list,
104 ;;; bind edebug-execution-mode and edebug-trace to nil, to avoid loop.
107 ;;; For the rest of the revision history, see edebug-history.
109 (defconst edebug-version
110 (let ((raw-version "$Revision: 3.5 $"))
111 (substring raw-version (string-match "[0-9.]*" raw-version)
116 ;; Emacs 18 doesnt have defalias.
118 (or (fboundp 'defalias) (fset 'defalias 'fset)))
124 (defconst edebug-maintainer-address "liberte@cs.uiuc.edu")
126 (defun edebug-submit-bug-report ()
127 "Submit, via mail, a bug report on edebug."
130 (and (y-or-n-p "Do you really want to submit a report on edebug? ")
131 (reporter-submit-bug-report
132 edebug-maintainer-address
133 (concat "edebug.el " edebug-version)
134 (list 'edebug-setup-hook
137 'edebug-eval-macro-args
138 'edebug-stop-before-symbols
140 'edebug-save-displayed-buffer-points
143 'edebug-test-coverage
144 'edebug-continue-kbd-macro
152 ;;; ===============================
154 (defvar edebug-setup-hook nil
155 "*Functions to call before edebug is used.
156 Each time it is set to a new value, Edebug will call those functions
157 once and then `edebug-setup-hook' is reset to nil. You could use this
158 to load up Edebug specifications associated with a package you are
159 using but only when you also use Edebug.")
161 (defvar edebug-all-defs nil
162 "*If non-nil, evaluation of any defining forms will instrument for Edebug.
163 This applies to `eval-defun', `eval-region', `eval-buffer', and
164 `eval-current-buffer'. `eval-region' is also called by
165 `eval-last-sexp', and `eval-print-last-sexp'.
167 You can use the command `edebug-all-defs' to toggle the value of this
168 variable. You may wish to make it local to each buffer with
169 (make-local-variable 'edebug-all-defs) in your
170 `emacs-lisp-mode-hook'.")
172 (defvar edebug-all-forms nil
173 "*Non-nil evaluation of all forms will instrument for Edebug.
174 This doesn't apply to loading or evaluations in the minibuffer.
175 Use the command edebug-all-forms to toggle the value of this option.")
177 (defvar edebug-eval-macro-args nil
178 "*Non-nil means all macro call arguments may be evaluated.
179 If this variable is nil, the default, edebug will *not* wrap
180 macro call arguments as if they will be evaluated.
181 For each macro, a edebug-form-spec overrides this option.
182 So to specify exceptions for macros that have some arguments evaluated
183 and some not, you should specify an edebug-form-spec.
185 This option is going away soon.")
187 (defvar edebug-stop-before-symbols nil
188 "*Non-nil causes Edebug to stop before symbols as well as after.
189 In any case, a breakpoint or interrupt may stop before a symbol.
191 This option is going away soon.")
193 (defvar edebug-save-windows t
194 "*If non-nil, Edebug saves and restores the window configuration.
195 That takes some time, so if your program does not care what happens to
196 the window configurations, it is better to set this variable to nil.
198 If the value is a list, only the listed windows are saved and
201 `edebug-toggle-save-windows' may be used to change this variable.")
203 (defvar edebug-save-displayed-buffer-points nil
204 "*If non-nil, save and restore point in all displayed buffers.
206 Saving and restoring point in other buffers is necessary if you are
207 debugging code that changes the point of a buffer which is displayed
208 in a non-selected window. If Edebug or the user then selects the
209 window, the buffer's point will be changed to the window's point.
211 Saving and restoring point in all buffers is expensive, since it
212 requires selecting each window twice, so enable this only if you need
215 (defvar edebug-initial-mode 'step
216 "*Initial execution mode for Edebug, if non-nil. If this variable
217 is non-@code{nil}, it specifies the initial execution mode for Edebug
218 when it is first activated. Possible values are step, next, go,
219 Go-nonstop, trace, Trace-fast, continue, and Continue-fast.")
221 (defvar edebug-trace nil
222 "*Non-nil means display a trace of function entry and exit.
223 Tracing output is displayed in a buffer named `*edebug-trace*', one
224 function entry or exit per line, indented by the recursion level.
226 You can customize by replacing functions `edebug-print-trace-before'
227 and `edebug-print-trace-after'.")
229 (defvar edebug-test-coverage nil
230 "*If non-nil, Edebug tests coverage of all expressions debugged.
231 This is done by comparing the result of each expression
232 with the previous result. Coverage is considered OK if two different
235 Use `edebug-display-freq-count' to display the frequency count and
236 coverage information for a definition.")
238 (defvar edebug-continue-kbd-macro nil
239 "*If non-nil, continue defining or executing any keyboard macro.
240 Use this with caution since it is not debugged.")
243 (defvar edebug-print-length 50
244 "*Default value of print-length to use while printing results in edebug.")
245 (defvar edebug-print-level 50
246 "*Default value of print-level to use while printing results in edebug.")
247 (defvar edebug-print-circle t
248 "*Default value of print-circle to use while printing results in edebug.")
250 (defvar edebug-unwrap-results nil
251 "*Non-nil if Edebug should unwrap results of expressions.
252 This is useful when debugging macros where the results of expressions
253 are instrumented expressions. But don't do this when results might be
254 circular or an infinite loop will result.")
256 (defvar edebug-on-error t
257 "*Value bound to `debug-on-error' while Edebug is active.
259 If `debug-on-error' is non-nil, that value is still used.
261 If the value is a list of signal names, Edebug will stop when any of
262 these errors are signaled from Lisp code whether or not the signal is
263 handled by a `condition-case'. This option is useful for debugging
264 signals that *are* handled since they would otherwise be missed.
265 After execution is resumed, the error is signaled again.")
267 (defvar edebug-on-quit t
268 "*Value bound to `debug-on-quit' while Edebug is active.")
270 (defvar edebug-global-break-condition nil
271 "*If non-nil, an expression to test for at every stop point.
272 If the result is non-nil, then break. Errors are ignored.")
275 ;;;; Form spec utilities.
276 ;;; ===============================
279 (defmacro def-edebug-spec (symbol spec)
280 "Set the edebug-form-spec property of SYMBOL according to SPEC.
281 Both SYMBOL and SPEC are unevaluated. The SPEC can be 0, t, a symbol
282 (naming a function), or a list."
283 (` (put (quote (, symbol)) 'edebug-form-spec (quote (, spec)))))
285 (defmacro def-edebug-form-spec (symbol spec-form)
286 "For compatibility with old version. Use def-edebug-spec instead."
287 (message "Obsolete: use def-edebug-spec instead.")
288 (def-edebug-spec symbol (eval spec-form)))
290 (defun get-edebug-spec (symbol)
291 ;; Get the spec of symbol resolving all indirection.
292 (let ((edebug-form-spec (get symbol 'edebug-form-spec))
294 (while (and (symbolp edebug-form-spec)
295 (setq indirect (get edebug-form-spec 'edebug-form-spec)))
296 ;; (edebug-trace "indirection: %s" edebug-form-spec)
297 (setq edebug-form-spec indirect))
303 ;;; ===============================
305 (if (not (fboundp 'gensym))
308 ;; Define gensym - from old cl.el
309 (defvar *gensym-index* 0
310 "Integer used by gensym to produce new names.")
312 (defun gensym (&optional prefix)
313 "Generate a fresh uninterned symbol.
314 There is an optional argument, PREFIX. PREFIX is the
315 string that begins the new name. Most people take just the default,
316 except when debugging needs suggest otherwise."
319 (let ((newsymbol nil)
321 (while (not newsymbol)
322 (setq newname (concat prefix *gensym-index*))
323 (setq *gensym-index* (+ *gensym-index* 1))
324 (if (not (intern-soft newname))
325 (setq newsymbol (make-symbol newname))))
329 ;; Only used by CL-like code.
330 '(if (not (fboundp 'keywordp))
331 (defun keywordp (object)
332 "Return t if OBJECT is a keyword.
333 A keyword is a symbol that starts with "":""."
334 (and (symbolp object)
335 (= ?: (aref (symbol-name object) 0)))))
337 (defun lambda-list-keywordp (object)
338 "Return t if OBJECT is a lambda list keyword.
339 A lambda list keyword is a symbol that starts with ""&""."
340 (and (symbolp object)
341 (= ?& (aref (symbol-name object) 0))))
344 (defun edebug-last-sexp ()
345 ;; Return the last sexp before point in current buffer.
346 ;; Assumes elisp syntax is active.
355 (defun edebug-window-list ()
356 "Return a list of windows, in order of next-window."
357 ;; This doesnt work for epoch.
358 (let* ((first-window (selected-window))
359 (window-list (list first-window))
360 (next (next-window first-window)))
361 (while (not (eq next first-window))
362 (setq window-list (cons next window-list))
363 (setq next (next-window next)))
364 (nreverse window-list)))
366 (defun edebug-window-live-p (window)
367 "Return non-nil if WINDOW is visible."
368 (let* ((first-window (selected-window))
369 (next (next-window first-window t)))
370 (while (not (or (eq next window)
371 (eq next first-window)))
372 (setq next (next-window next t)))
376 '(defun edebug-two-window-p ()
377 "Return t if there are two windows."
378 (and (not (one-window-p))
379 (eq (selected-window)
380 (next-window (next-window (selected-window))))))
382 (defsubst edebug-lookup-function (object)
383 (while (and (symbolp object) (fboundp object))
384 (setq object (symbol-function object)))
387 (defun edebug-macrop (object)
388 "Return the macro named by OBJECT, or nil if it is not a macro."
389 (setq object (edebug-lookup-function object))
390 (if (and (listp object)
391 (eq 'macro (car object))
392 (edebug-functionp (cdr object)))
395 (defun edebug-functionp (object)
396 "Returns the function named by OBJECT, or nil if it is not a function."
397 (setq object (edebug-lookup-function object))
398 (if (or (subrp object)
400 (eq (car object) 'lambda)
401 (listp (car (cdr object)))))
404 (defun edebug-sort-alist (alist function)
405 ;; Return the ALIST sorted with comparison function FUNCTION.
406 ;; This uses 'sort so the sorting is destructive.
407 (sort alist (function
409 (funcall function (car e1) (car e2))))))
411 ;;(def-edebug-spec edebug-save-restriction t)
413 ;; Not used. If it is used, def-edebug-spec must be defined before use.
414 '(defmacro edebug-save-restriction (&rest body)
415 "Evaluate BODY while saving the current buffers restriction.
416 BODY may change buffer outside of current restriction, unlike
417 save-restriction. BODY may change the current buffer,
418 and the restriction will be restored to the original buffer,
419 and the current buffer remains current.
420 Return the result of the last expression in BODY."
421 (` (let ((edebug:s-r-beg (point-min-marker))
422 (edebug:s-r-end (point-max-marker)))
426 (set-buffer (marker-buffer edebug:s-r-beg))
427 (narrow-to-region edebug:s-r-beg edebug:s-r-end))))))
432 (defconst edebug-trace-buffer "*edebug-trace*"
433 "Name of the buffer to put trace info in.")
435 (defun edebug-pop-to-buffer (buffer &optional window)
436 ;; Like pop-to-buffer, but select window where BUFFER was last shown.
437 ;; Select WINDOW if it provided and it still exists. Otherwise,
438 ;; if buffer is currently shown in several windows, choose one.
439 ;; Otherwise, find a new window, possibly splitting one.
440 (setq window (if (and (windowp window) (edebug-window-live-p window)
441 (eq (window-buffer window) buffer))
443 (if (eq (window-buffer (selected-window)) buffer)
445 (edebug-get-buffer-window buffer))))
447 (select-window window)
450 ;; (message "next window: %s" (next-window)) (sit-for 1)
451 (if (eq (get-buffer-window edebug-trace-buffer) (next-window))
452 ;; Dont select trace window
454 (select-window (next-window))))
455 (set-window-buffer (selected-window) buffer)
456 (set-window-hscroll (selected-window) 0);; should this be??
457 ;; Selecting the window does not set the buffer until command loop.
458 ;;(set-buffer buffer)
462 (defun edebug-get-displayed-buffer-points ()
463 ;; Return a list of buffer point pairs, for all displayed buffers.
465 (let* ((first-window (selected-window))
466 (next (next-window first-window))
467 (buffer-point-list nil)
469 (while (not (eq next first-window))
470 (set-buffer (setq buffer (window-buffer next)))
471 (setq buffer-point-list
472 (cons (cons buffer (point)) buffer-point-list))
473 (setq next (next-window next)))
477 (defun edebug-set-buffer-points (buffer-points)
478 ;; Restore the buffer-points created by edebug-get-displayed-buffer-points.
479 (let ((current-buffer (current-buffer)))
480 (mapcar (function (lambda (buf-point)
481 (if (buffer-name (car buf-point)) ; still exists
483 (set-buffer (car buf-point))
484 (goto-char (cdr buf-point))))))
486 (set-buffer current-buffer)))
488 (defun edebug-current-windows (which-windows)
489 ;; Get either a full window configuration or some window information.
490 (if (listp which-windows)
491 (mapcar (function (lambda (window)
492 (if (edebug-window-live-p window)
494 (window-buffer window)
495 (window-point window)
496 (window-start window)
497 (window-hscroll window)))))
499 (current-window-configuration)))
501 (defun edebug-set-windows (window-info)
502 ;; Set either a full window configuration or some window information.
503 (if (listp window-info)
505 (lambda (one-window-info)
508 (lambda (window buffer point start hscroll)
509 (if (edebug-window-live-p window)
511 (set-window-buffer window buffer)
512 (set-window-point window point)
513 (set-window-start window start)
514 (set-window-hscroll window hscroll)))))
517 (set-window-configuration window-info)))
519 (defalias 'edebug-get-buffer-window 'get-buffer-window)
520 (defalias 'edebug-sit-for 'sit-for)
521 (defalias 'edebug-input-pending-p 'input-pending-p)
524 ;;;; Redefine read and eval functions
525 ;;; =================================
526 ;;; read is redefined to maybe instrument forms.
527 ;;; eval-defun is redefined to check edebug-all-forms and edebug-all-defs.
529 ;;; Use the Lisp version of eval-region.
530 (require 'elisp-eval-region "eval-region")
532 ;; Save the original read function
533 (or (fboundp 'edebug-original-read)
534 (defalias 'edebug-original-read (symbol-function 'read)))
536 (defun edebug-read (&optional stream)
537 "Read one Lisp expression as text from STREAM, return as Lisp object.
538 If STREAM is nil, use the value of `standard-input' (which see).
539 STREAM or the value of `standard-input' may be:
540 a buffer (read from point and advance it)
541 a marker (read from where it points and advance it)
542 a function (call it with no arguments for each character,
543 call it with a char as argument to push a char back)
544 a string (takes text from string, starting at the beginning)
545 t (read text line using minibuffer and use it).
547 This version, from Edebug, maybe instruments the expression. But the
548 STREAM must be the current buffer to do so. Whether it instuments is
549 also dependent on the values of `edebug-all-defs' and
551 (or stream (setq stream standard-input))
552 (if (eq stream (current-buffer))
553 (edebug-read-and-maybe-wrap-form)
554 (edebug-original-read stream)))
556 (or (fboundp 'edebug-original-eval-defun)
557 (defalias 'edebug-original-eval-defun (symbol-function 'eval-defun)))
559 (defun edebug-eval-defun (edebug-it)
560 "Evaluate the top-level form containing point, or after point.
562 This version, from Edebug, has the following differences: With a
563 prefix argument instrument the code for Edebug. If `edebug-all-defs' is
564 non-nil, then the code is instrumented *unless* there is a prefix
565 argument. If instrumenting, it prints: \"Edebug: <function name>\".
566 Otherwise, it prints in the minibuffer."
568 (let ((edebugging (not (eq (not edebug-it) (not edebug-all-defs))))
572 (let ((edebug-all-forms edebugging)
573 (edebug-all-defs (eq edebug-all-defs (not edebug-it))))
574 (edebug-read-top-level-form))))
576 (princ edebug-result)
581 (defalias 'edebug-defun 'edebug-eval-top-level-form)
584 (defun edebug-eval-top-level-form ()
585 "Evaluate a top level form, such as a defun or defmacro.
586 This is like `eval-defun', but the code is always instrumented for Edebug.
587 Print its name in the minibuffer and leave point where it is,
588 or if an error occurs, leave point after it with mark at the original
592 ;; Bind edebug-all-forms only while reading, not while evaling
593 ;; but this causes problems while edebugging edebug.
594 (let ((edebug-all-forms t)
596 (edebug-read-top-level-form))))
599 (defun edebug-read-top-level-form ()
600 (let ((starting-point (point)))
604 (edebug-read-and-maybe-wrap-form)
605 ;; Recover point, but only if no error occurred.
606 (goto-char starting-point))))
609 ;; Compatibility with old versions.
610 (defalias 'edebug-all-defuns 'edebug-all-defs)
612 (defun edebug-all-defs ()
613 "Toggle edebugging of all definitions."
615 (setq edebug-all-defs (not edebug-all-defs))
616 (message "Edebugging all definitions is %s."
617 (if edebug-all-defs "on" "off")))
620 (defun edebug-all-forms ()
621 "Toggle edebugging of all forms."
623 (setq edebug-all-forms (not edebug-all-forms))
624 (message "Edebugging all forms is %s."
625 (if edebug-all-forms "on" "off")))
628 (defun edebug-install-read-eval-functions ()
630 ;; Don't install if already installed.
631 (if (eq 'read 'edebug-read) nil
632 (elisp-eval-region-install)
633 (defalias 'read 'edebug-read)
634 (defalias 'eval-defun 'edebug-eval-defun)))
636 (defun edebug-uninstall-read-eval-functions ()
638 (elisp-eval-region-uninstall)
639 (defalias 'read (symbol-function 'edebug-original-read))
640 (defalias 'eval-defun (symbol-function 'edebug-original-eval-defun)))
643 ;;;; Edebug internal data
644 ;;; ===============================
646 ;;; The internal data that is needed for edebugging is kept in the
647 ;;; buffer-local variable `edebug-form-data'.
649 (make-variable-buffer-local 'edebug-form-data)
651 (defconst edebug-form-data nil)
652 ;; A list of entries associating symbols with buffer regions.
653 ;; This is an automatic buffer local variable. Each entry looks like:
654 ;; @code{(@var{symbol} @var{begin-marker} @var{end-marker}). The markers
655 ;; are at the beginning and end of an entry level form and @var{symbol} is
656 ;; a symbol that holds all edebug related information for the form on its
659 ;; In the future, the symbol will be irrelevant and edebug data will
660 ;; be stored in the definitions themselves rather than in the property
663 (defun edebug-make-form-data-entry (symbol begin end)
664 (list symbol begin end))
666 (defsubst edebug-form-data-name (entry)
669 (defsubst edebug-form-data-begin (entry)
672 (defsubst edebug-form-data-end (entry)
675 (defsubst edebug-set-form-data-entry (entry name begin end)
676 (setcar entry name);; in case name is changed
677 (set-marker (nth 1 entry) begin)
678 (set-marker (nth 2 entry) end))
680 (defun edebug-get-form-data-entry (pnt &optional end-point)
681 ;; Find the edebug form data entry which is closest to PNT.
682 ;; If END-POINT is supplied, match must be exact.
683 ;; Return `nil' if none found.
684 (let ((rest edebug-form-data)
686 (closest-dist 999999)) ;; need maxint here
687 (while (and rest (< 0 closest-dist))
688 (let* ((entry (car rest))
689 (begin (edebug-form-data-begin entry))
690 (dist (- pnt begin)))
691 (setq rest (cdr rest))
693 (< dist closest-dist)
695 (= end-point (edebug-form-data-end entry)))
696 (<= pnt (edebug-form-data-end entry)))
697 (setq closest-dist dist
698 closest-entry entry))))
701 ;; Also need to find all contained entries,
702 ;; and find an entry given a symbol, which should be just assq.
704 (defun edebug-form-data-symbol ()
705 ;; Return the edebug data symbol of the form where point is in.
706 ;; If point is not inside a edebuggable form, cause error.
707 (or (edebug-form-data-name (edebug-get-form-data-entry (point)))
708 (error "Not inside instrumented form")))
710 (defun edebug-make-top-form-data-entry (new-entry)
711 ;; Make NEW-ENTRY the first element in the `edebug-form-data' list.
712 (edebug-clear-form-data-entry new-entry)
713 (setq edebug-form-data (cons new-entry edebug-form-data)))
715 (defun edebug-clear-form-data-entry (entry)
716 ;; If non-nil, clear ENTRY out of the form data.
717 ;; Maybe clear the markers and delete the symbol's edebug property?
720 ;; Instead of this, we could just find all contained forms.
721 ;; (put (car entry) 'edebug nil) ;
722 ;; (mapcar 'edebug-clear-form-data-entry ; dangerous
723 ;; (get (car entry) 'edebug-dependents))
724 ;; (set-marker (nth 1 entry) nil)
725 ;; (set-marker (nth 2 entry) nil)
726 (setq edebug-form-data (delq entry edebug-form-data)))))
729 ;;;; Parser utilities
730 ;;; ===============================
733 (defun edebug-syntax-error (&rest args)
734 ;; Signal an invalid-read-syntax with ARGS.
735 (signal 'invalid-read-syntax args))
738 (defconst edebug-read-syntax-table
739 ;; Lookup table for significant characters indicating the class of the
740 ;; token that follows. This is not a \"real\" syntax table.
741 (let ((table (make-vector 256 'symbol))
744 (aset table i 'space)
746 (aset table ?\( 'lparen)
747 (aset table ?\) 'rparen)
748 (aset table ?\' 'quote)
749 (aset table ?\" 'string)
750 (aset table ?\? 'char)
751 (aset table ?\[ 'lbracket)
752 (aset table ?\] 'rbracket)
753 (aset table ?\. 'dot)
754 (aset table ?\# 'hash)
755 ;; We treat numbers as symbols, because of confusion with -, -1, and 1-.
756 ;; We dont care about any other chars since they wont be seen.
759 (defun edebug-next-token-class ()
760 ;; Move to the next token and return its class. We only care about
761 ;; lparen, rparen, dot, quote, string, char, vector, or symbol.
762 (edebug-skip-whitespace)
763 (aref edebug-read-syntax-table (following-char)))
766 (defun edebug-skip-whitespace ()
767 ;; Leave point before the next token, skipping white space and comments.
768 (skip-chars-forward " \t\r\n\f")
769 (while (= (following-char) ?\;)
770 ;; \r is counted as a comment terminator to support selective display.
771 (skip-chars-forward "^\n\r") ; skip the comment
772 (skip-chars-forward " \t\r\n\f")))
775 ;; Mostly obsolete reader; still used in one case.
777 (defun edebug-read-sexp ()
778 ;; Read one sexp from the current buffer starting at point.
779 ;; Leave point immediately after it. A sexp can be a list or atom.
780 ;; An atom is a symbol (or number), character, string, or vector.
781 ;; This works for reading anything legitimate, but it
782 ;; is gummed up by parser inconsistencies (bugs?)
783 (let ((class (edebug-next-token-class)))
785 ;; read goes one too far if a (possibly quoted) string or symbol
786 ;; is immediately followed by non-whitespace.
787 ((eq class 'symbol) (prog1
788 (edebug-original-read (current-buffer))
789 (if (not (eq (aref edebug-read-syntax-table
790 (preceding-char)) 'symbol))
792 ((eq class 'string) (prog1
793 (edebug-original-read (current-buffer))
794 (if (/= (preceding-char) ?\")
796 ((eq class 'quote) (forward-char 1)
797 (list 'quote (edebug-read-sexp)))
798 (t ; anything else, just read it.
799 (edebug-original-read (current-buffer))))))
802 ;;;; Offsets for reader
803 ;;; ==============================
805 ;; Define a structure to represent offset positions of expressions.
806 ;; Each offset structure looks like: (before . after) for constituents,
807 ;; or for structures that have elements: (before <subexpressions> . after)
808 ;; where the <subexpressions> are the offset structures for subexpressions
809 ;; including the head of a list.
810 (defconst edebug-offsets nil)
812 ;; Stack of offset structures in reverse order of the nesting.
813 ;; This is used to get back to previous levels.
814 (defconst edebug-offsets-stack nil)
815 (defconst edebug-current-offset nil) ; Top of the stack, for convenience.
817 ;; We must store whether we just read a list with a dotted form that
818 ;; is itself a list. This structure will be condensed, so the offsets
819 ;; must also be condensed.
820 (defconst edebug-read-dotted-list nil)
822 (defsubst edebug-initialize-offsets ()
823 ;; Reinitialize offset recording.
824 (setq edebug-current-offset nil))
826 (defun edebug-store-before-offset (point)
827 ;; Add a new offset pair with POINT as the before offset.
828 (let ((new-offset (list point)))
829 (if edebug-current-offset
830 (setcdr edebug-current-offset
831 (cons new-offset (cdr edebug-current-offset)))
832 ;; Otherwise, we are at the top level, so initialize.
833 (setq edebug-offsets new-offset
834 edebug-offsets-stack nil
835 edebug-read-dotted-list nil))
836 ;; Cons the new offset to the front of the stack.
837 (setq edebug-offsets-stack (cons new-offset edebug-offsets-stack)
838 edebug-current-offset new-offset)
841 (defun edebug-store-after-offset (point)
842 ;; Finalize the current offset struct by reversing it and
843 ;; store POINT as the after offset.
844 (if (not edebug-read-dotted-list)
845 ;; Just reverse the offsets of all subexpressions.
846 (setcdr edebug-current-offset (nreverse (cdr edebug-current-offset)))
848 ;; We just read a list after a dot, which will be abbreviated out.
849 (setq edebug-read-dotted-list nil)
850 ;; Drop the corresponding offset pair.
851 ;; That is, nconc the reverse of the rest of the offsets
852 ;; with the cdr of last offset.
853 (setcdr edebug-current-offset
854 (nconc (nreverse (cdr (cdr edebug-current-offset)))
855 (cdr (car (cdr edebug-current-offset))))))
857 ;; Now append the point using nconc.
858 (setq edebug-current-offset (nconc edebug-current-offset point))
860 (setq edebug-offsets-stack (cdr edebug-offsets-stack)
861 edebug-current-offset (car edebug-offsets-stack)))
863 (defun edebug-ignore-offset ()
864 ;; Ignore the last created offset pair.
865 (setcdr edebug-current-offset (cdr (cdr edebug-current-offset))))
867 (def-edebug-spec edebug-storing-offsets (form body))
868 (put 'edebug-storing-offsets 'lisp-indent-hook 1)
870 (defmacro edebug-storing-offsets (point &rest body)
873 (edebug-store-before-offset (, point))
875 (edebug-store-after-offset (point)))))
878 ;;;; Reader for Emacs Lisp.
879 ;;; ==========================================
880 ;; Uses edebug-next-token-class (and edebug-skip-whitespace) above.
882 (defconst edebug-read-alist
883 '((symbol . edebug-read-symbol)
884 (lparen . edebug-read-list)
885 (string . edebug-read-string)
886 (quote . edebug-read-quote)
887 (lbracket . edebug-read-vector)
888 (hash . edebug-read-function)
891 (defun edebug-read-storing-offsets (stream)
892 (let ((class (edebug-next-token-class))
894 edebug-read-dotted-list) ; see edebug-store-after-offset
895 (edebug-storing-offsets (point)
896 (if (setq func (assq class edebug-read-alist))
897 (funcall (cdr func) stream)
898 ;; anything else, just read it.
899 (edebug-original-read stream))
902 (defun edebug-read-symbol (stream)
904 (edebug-original-read stream)
905 ;; loses for escaped chars
906 (if (not (eq (aref edebug-read-syntax-table
907 (preceding-char)) 'symbol))
910 (defun edebug-read-string (stream)
912 (edebug-original-read stream)
913 (if (/= (preceding-char) ?\")
916 (defun edebug-read-quote (stream)
917 ;; Turn 'thing into (quote thing)
920 (edebug-storing-offsets (point) 'quote)
921 (edebug-read-storing-offsets stream)))
923 (defun edebug-read-function (stream)
924 ;; Turn #'thing into (function thing)
926 (if (/= ?\' (following-char)) (edebug-syntax-error "Bad char"))
929 (edebug-storing-offsets (point)
930 (if (featurep 'cl) 'function* 'function))
931 (edebug-read-storing-offsets stream)))
933 (defun edebug-read-list (stream)
934 (forward-char 1) ; skip \(
937 (while (not (memq (edebug-next-token-class) '(rparen dot)))
938 (setq elements (cons (edebug-read-storing-offsets stream) elements)))
939 (setq elements (nreverse elements))
940 (if (eq 'dot (edebug-next-token-class))
942 (forward-char 1) ; skip \.
943 (setq dotted-form (edebug-read-storing-offsets stream))
944 elements (nconc elements dotted-form)
945 (if (not (eq (edebug-next-token-class) 'rparen))
946 (edebug-syntax-error "Expected `)'"))
947 (setq edebug-read-dotted-list (listp dotted-form))
950 (forward-char 1) ; skip \)
953 (defun edebug-read-vector (stream)
954 (forward-char 1) ; skip \[
957 (while (not (eq 'rbracket (edebug-next-token-class)))
958 (setq elements (cons (edebug-read-storing-offsets stream) elements)))
959 (apply 'vector (nreverse elements)))
960 (forward-char 1) ; skip \]
965 ;;;; Cursors for traversal of list and vector elements with offsets.
966 ;;;====================================================================
968 (defvar edebug-dotted-spec nil)
970 (defun edebug-new-cursor (expressions offsets)
971 ;; Return a new cursor for EXPRESSIONS with OFFSETS.
972 (if (vectorp expressions)
973 (setq expressions (append expressions nil)))
974 (cons expressions offsets))
976 (defsubst edebug-set-cursor (cursor expressions offsets)
977 ;; Set the CURSOR's EXPRESSIONS and OFFSETS to the given.
978 ;; Return the cursor.
979 (setcar cursor expressions)
980 (setcdr cursor offsets)
983 '(defun edebug-copy-cursor (cursor)
984 ;; Copy the cursor using the same object and offsets.
985 (cons (car cursor) (cdr cursor)))
987 (defsubst edebug-cursor-expressions (cursor)
989 (defsubst edebug-cursor-offsets (cursor)
992 (defsubst edebug-empty-cursor (cursor)
993 ;; Return non-nil if CURSOR is empty - meaning no more elements.
996 (defsubst edebug-top-element (cursor)
997 ;; Return the top element at the cursor.
998 ;; Assumes not empty.
1001 (defun edebug-top-element-required (cursor &rest error)
1002 ;; Check if a dotted form is required.
1003 (if edebug-dotted-spec (edebug-no-match cursor "Dot expected."))
1004 ;; Check if there is at least one more argument.
1005 (if (edebug-empty-cursor cursor) (apply 'edebug-no-match cursor error))
1006 ;; Return that top element.
1007 (edebug-top-element cursor))
1009 (defsubst edebug-top-offset (cursor)
1010 ;; Return the top offset pair corresponding to the top element.
1013 (defun edebug-move-cursor (cursor)
1014 ;; Advance and return the cursor to the next element and offset.
1015 ;; throw no-match if empty before moving.
1016 ;; This is a violation of the cursor encapsulation, but
1017 ;; there is plenty of that going on while matching.
1018 ;; The following test should always fail.
1019 (if (edebug-empty-cursor cursor)
1020 (edebug-no-match cursor "Not enough arguments."))
1021 (setcar cursor (cdr (car cursor)))
1022 (setcdr cursor (cdr (cdr cursor)))
1026 (defun edebug-before-offset (cursor)
1027 ;; Return the before offset of the cursor.
1028 ;; If there is nothing left in the offsets,
1029 ;; return one less than the offset itself,
1030 ;; which is the after offset for a list.
1031 (let ((offset (edebug-cursor-offsets cursor)))
1036 (defun edebug-after-offset (cursor)
1037 ;; Return the after offset of the cursor object.
1038 (let ((offset (edebug-top-offset cursor)))
1039 (while (consp offset)
1040 (setq offset (cdr offset)))
1044 ;;; ===============================
1046 ;;; The top level function for parsing forms is
1047 ;;; edebug-read-and-maybe-wrap-form; it calls all the rest. It checks the
1048 ;;; syntax a bit and leaves point at any error it finds, but otherwise
1049 ;;; should appear to work like eval-defun.
1051 ;;; The basic plan is to surround each expression with a call to
1052 ;;; the edebug debugger together with indexes into a table of positions of
1053 ;;; all expressions. Thus an expression "exp" becomes:
1055 ;;; (edebug-after (edebug-before 1) 2 exp)
1057 ;;; When this is evaluated, first point is moved to the beginning of
1058 ;;; exp at offset 1 of the current function. The expression is
1059 ;;; evaluated, which may cause more edebug calls, and then point is
1060 ;;; moved to offset 2 after the end of exp.
1062 ;;; The highest level expressions of the function are wrapped in a call to
1063 ;;; edebug-enter, which supplies the function name and the actual
1064 ;;; arguments to the function. See functions edebug-enter, edebug-before,
1065 ;;; and edebug-after for more details.
1067 ;; Dynamically bound vars, left unbound, but globally declared.
1068 ;; This is to quiet the byte compiler.
1070 ;; Window data of the highest definition being wrapped.
1071 ;; This data is shared by all embedded definitions.
1072 (defvar edebug-top-window-data)
1074 (defvar edebug-&optional)
1075 (defvar edebug-&rest)
1076 (defvar edebug-gate nil) ;; whether no-match forces an error.
1078 (defconst edebug-def-name nil) ; name of definition, used by interactive-form
1079 (defconst edebug-old-def-name nil) ; previous name of containing definition.
1081 (defconst edebug-error-point nil)
1082 (defconst edebug-best-error nil)
1085 (defun edebug-read-and-maybe-wrap-form ()
1086 ;; Read a form and wrap it with edebug calls, if the conditions are right.
1087 ;; Here we just catch any no-match not caught below and signal an error.
1089 ;; Run the setup hook.
1090 (run-hooks 'edebug-setup-hook)
1091 (setq edebug-setup-hook nil)
1094 edebug-top-window-data
1095 edebug-def-name;; make sure it is locally nil
1096 ;; I dont like these here!!
1103 ;; Do this once here instead of several times.
1104 (max-lisp-eval-depth (+ 800 max-lisp-eval-depth))
1105 (max-specpdl-size (+ 1200 max-specpdl-size)))
1108 (setq result (edebug-read-and-maybe-wrap-form1))
1111 (apply 'edebug-syntax-error no-match))
1115 (defun edebug-read-and-maybe-wrap-form1 ()
1120 ;; These offset things dont belong here, but to support recursive
1121 ;; calls to edebug-read, they need to be here.
1123 edebug-offsets-stack
1124 edebug-current-offset ; reset to nil
1127 (if (and (eq 'lparen (edebug-next-token-class))
1128 (eq 'symbol (progn (forward-char 1) (edebug-next-token-class))))
1129 ;; Find out if this is a defining form from first symbol
1130 (setq def-kind (edebug-original-read (current-buffer))
1131 spec (and (symbolp def-kind) (get-edebug-spec def-kind))
1132 defining-form-p (and (listp spec)
1133 (eq '&define (car spec)))
1134 ;; This is incorrect in general!! But OK most of the time.
1135 def-name (if (and defining-form-p
1136 (eq 'name (car (cdr spec)))
1137 (eq 'symbol (edebug-next-token-class)))
1138 (edebug-original-read (current-buffer))))))
1139 ;;;(message "all defs: %s all forms: %s" edebug-all-defs edebug-all-forms)
1142 (if (or edebug-all-defs edebug-all-forms)
1143 ;; If it is a defining form and we are edebugging defs,
1144 ;; then let edebug-list-form start it.
1145 (let ((cursor (edebug-new-cursor
1146 (list (edebug-read-storing-offsets (current-buffer)))
1147 (list edebug-offsets))))
1149 (edebug-make-form-wrapper
1151 (edebug-before-offset cursor)
1152 (1- (edebug-after-offset cursor))
1153 (list (cons (symbol-name def-kind) (cdr spec))))))
1155 ;; Not edebugging this form, so reset the symbol's edebug
1156 ;; property to be just a marker at the definition's source code.
1157 ;; This only works for defs with simple names.
1158 (put def-name 'edebug (point-marker))
1159 ;; Also nil out dependent defs.
1162 (put def-name 'edebug nil)))
1163 (get def-name 'edebug-dependents))
1164 (edebug-read-sexp)))
1166 ;; If all forms are being edebugged, explicitly wrap it.
1168 (let ((cursor (edebug-new-cursor
1169 (list (edebug-read-storing-offsets (current-buffer)))
1170 (list edebug-offsets))))
1171 (edebug-make-form-wrapper
1173 (edebug-before-offset cursor)
1174 (edebug-after-offset cursor)
1177 ;; Not a defining form, and not edebugging.
1178 (t (edebug-read-sexp)))
1182 (defvar edebug-def-args) ; args of defining form.
1183 (defvar edebug-def-interactive) ; is it an emacs interactive function?
1184 (defvar edebug-inside-func) ;; whether code is inside function context.
1185 ;; Currently def-form sets this to nil; def-body sets it to t.
1187 (defun edebug-interactive-p-name ()
1188 ;; Return a unique symbol for the variable used to store the
1189 ;; status of interactive-p for this function.
1190 (intern (format "edebug-%s-interactive-p" edebug-def-name)))
1193 (defun edebug-wrap-def-body (forms)
1194 "Wrap the FORMS of a definition body."
1195 (if edebug-def-interactive
1196 (` (let (((, (edebug-interactive-p-name))
1198 (, (edebug-make-enter-wrapper forms))))
1199 (edebug-make-enter-wrapper forms)))
1202 (defun edebug-make-enter-wrapper (forms)
1203 ;; Generate the enter wrapper for some forms of a definition.
1204 ;; This is not to be used for the body of other forms, e.g. `while',
1205 ;; since it wraps the list of forms with a call to `edebug-enter'.
1206 ;; Uses the dynamically bound vars edebug-def-name and edebug-def-args.
1207 ;; Do this after parsing since that may find a name.
1208 (setq edebug-def-name
1209 (or edebug-def-name edebug-old-def-name (gensym "edebug-anon")))
1211 (quote (, edebug-def-name))
1212 (, (if edebug-inside-func
1214 ;; Doesnt work with more than one def-body!!
1215 ;; But the list will just be reversed.
1216 (nreverse edebug-def-args))))
1218 (function (lambda () (,@ forms)))
1222 (defvar edebug-form-begin-marker) ; the mark for def being instrumented
1224 (defvar edebug-offset-index) ; the next available offset index.
1225 (defvar edebug-offset-list) ; the list of offset positions.
1227 (defun edebug-inc-offset (offset)
1228 ;; modifies edebug-offset-index and edebug-offset-list
1229 ;; accesses edebug-func-marc and buffer point
1232 (setq edebug-offset-list (cons (- offset edebug-form-begin-marker)
1234 edebug-offset-index (1+ edebug-offset-index))))
1237 (defun edebug-make-before-and-after-form (before-index form after-index)
1238 ;; Return the edebug form for the current function at offset BEFORE-INDEX
1239 ;; given FORM. Looks like:
1240 ;; (edebug-after (edebug-before BEFORE-INDEX) AFTER-INDEX FORM)
1241 ;; Also increment the offset index for subsequent use.
1242 ;; if (not edebug-stop-before-symbols) and form is a symbol,
1243 ;; then dont call edebug-before.
1245 (list 'edebug-before before-index)
1248 (defun edebug-make-after-form (form after-index)
1249 ;; Like edebug-make-before-and-after-form, but only after.
1250 (list 'edebug-after 0 after-index form))
1253 (defun edebug-unwrap (sexp)
1254 "Return the unwrapped SEXP or return it as is if it is not wrapped.
1255 The SEXP might be the result of wrapping a body, which is a list of
1256 expressions; a `progn' form will be returned enclosing these forms."
1259 ((eq 'edebug-after (car sexp))
1261 ((eq 'edebug-enter (car sexp))
1262 (let ((forms (nthcdr 2 (nth 1 (nth 3 sexp)))))
1263 (if (> (length forms) 1)
1264 (cons 'progn forms) ;; could return (values forms) instead.
1266 (t sexp);; otherwise it is not wrapped, so just return it.
1270 (defun edebug-unwrap* (sexp)
1271 "Return the sexp recursively unwrapped."
1272 (let ((new-sexp (edebug-unwrap sexp)))
1273 (while (not (eq sexp new-sexp))
1275 new-sexp (edebug-unwrap sexp)))
1276 (if (consp new-sexp)
1277 (mapcar 'edebug-unwrap* new-sexp)
1281 (defun edebug-defining-form (cursor form-begin form-end speclist)
1282 ;; Process the defining form, starting outside the form.
1283 ;; The speclist is a generated list spec that looks like:
1284 ;; (("def-symbol" defining-form-spec-sans-&define))
1285 ;; Skip the first offset.
1286 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1287 (cdr (edebug-cursor-offsets cursor)))
1288 (edebug-make-form-wrapper
1290 form-begin (1- form-end)
1293 (defun edebug-make-form-wrapper (cursor form-begin form-end
1295 ;; Wrap a form, usually a defining form, but any evaluated one.
1296 ;; If speclist is non-nil, this is being called by edebug-defining-form.
1297 ;; Otherwise it is being called from edebug-read-and-maybe-wrap-form1.
1298 ;; This is a hack, but I havent figured out a simpler way yet.
1299 (let* ((form-data-entry (edebug-get-form-data-entry form-begin form-end))
1300 ;; Set this marker before parsing.
1301 (edebug-form-begin-marker
1303 (edebug-form-data-begin form-data-entry)
1304 ;; Buffer must be current-buffer for this to work:
1305 (set-marker (make-marker) form-begin))))
1307 (let (edebug-offset-list
1308 (edebug-offset-index 0)
1311 ;; (edebug-containing-def-name edebug-def-name)
1312 ;; Get name from form-data, if any.
1313 (edebug-old-def-name (edebug-form-data-name form-data-entry))
1316 edebug-def-interactive
1317 edebug-inside-func;; whether wrapped code executes inside a function.
1322 (edebug-match cursor speclist)
1324 ;; else wrap as an enter-form.
1325 (edebug-make-enter-wrapper (list (edebug-form cursor)))))
1327 ;; Set the name here if it was not set by edebug-make-enter-wrapper.
1328 (setq edebug-def-name
1329 (or edebug-def-name edebug-old-def-name (gensym "edebug-anon")))
1331 ;; Add this def as a dependent of containing def. Buggy.
1332 '(if (and edebug-containing-def-name
1333 (not (get edebug-containing-def-name 'edebug-dependents)))
1334 (put edebug-containing-def-name 'edebug-dependents
1335 (cons edebug-def-name
1336 (get edebug-containing-def-name
1337 'edebug-dependents))))
1339 ;; Create a form-data-entry or modify existing entry's markers.
1340 ;; In the latter case, pointers to the entry remain eq.
1341 (if (not form-data-entry)
1342 (setq form-data-entry
1343 (edebug-make-form-data-entry
1345 edebug-form-begin-marker
1346 ;; Buffer must be current-buffer.
1347 (set-marker (make-marker) form-end)
1349 (edebug-set-form-data-entry
1350 form-data-entry edebug-def-name ;; in case name is changed
1351 form-begin form-end))
1353 ;; (message "defining: %s" edebug-def-name) (sit-for 2)
1354 (edebug-make-top-form-data-entry form-data-entry)
1355 (message "Edebug: %s" edebug-def-name)
1356 ;;(debug edebug-def-name)
1358 ;; Destructively reverse edebug-offset-list and make vector from it.
1359 (setq edebug-offset-list (vconcat (nreverse edebug-offset-list)))
1361 ;; Side effects on the property list of edebug-def-name.
1362 (edebug-clear-frequency-count edebug-def-name)
1363 (edebug-clear-coverage edebug-def-name)
1365 ;; Set up the initial window data.
1366 (if (not edebug-top-window-data) ;; if not already set, do it now.
1367 (let ((window ;; Find the best window for this buffer.
1368 (or (get-buffer-window (current-buffer))
1369 (selected-window))))
1370 (setq edebug-top-window-data
1371 (cons window (window-start window)))))
1373 ;; Store the edebug data in symbol's property list.
1374 (put edebug-def-name 'edebug
1375 ;; A struct or vector would be better here!!
1376 (list edebug-form-begin-marker
1377 nil ; clear breakpoints
1379 edebug-top-window-data
1385 (defun edebug-clear-frequency-count (name)
1386 ;; Create initial frequency count vector.
1387 ;; For each stop point, the counter is incremented each time it is visited.
1388 (put name 'edebug-freq-count
1389 (make-vector (length edebug-offset-list) 0)))
1392 (defun edebug-clear-coverage (name)
1393 ;; Create initial coverage vector.
1394 ;; Only need one per expression, but it is simpler to use stop points.
1395 (put name 'edebug-coverage
1396 (make-vector (length edebug-offset-list) 'unknown)))
1399 (defun edebug-form (cursor)
1400 ;; Return the instrumented form for the following form.
1401 ;; Add the point offsets to the edebug-offset-list for the form.
1402 (let* ((form (edebug-top-element-required cursor "Expected form"))
1403 (offset (edebug-top-offset cursor)))
1407 ;; The first offset for a list form is for the list form itself.
1408 (if (eq 'quote (car form))
1410 (let* ((head (car form))
1411 (spec (and (symbolp head) (get-edebug-spec head)))
1412 (new-cursor (edebug-new-cursor form offset)))
1413 ;; Find out if this is a defining form from first symbol.
1414 ;; An indirect spec would not work here, yet.
1415 (if (and (consp spec) (eq '&define (car spec)))
1416 (edebug-defining-form
1418 (car offset);; before the form
1419 (edebug-after-offset cursor)
1420 (cons (symbol-name head) (cdr spec)))
1421 ;; Wrap a regular form.
1422 (edebug-make-before-and-after-form
1423 (edebug-inc-offset (car offset))
1424 (edebug-list-form new-cursor)
1425 ;; After processing the list form, the new-cursor is left
1426 ;; with the offset after the form.
1427 (edebug-inc-offset (edebug-cursor-offsets new-cursor))))
1432 ;; Check for constant symbols that dont get wrapped.
1433 ((or (memq form '(t nil))
1434 (and (fboundp 'keywordp) (keywordp form)))
1437 ;; This option may go away.
1438 (edebug-stop-before-symbols
1439 (edebug-make-before-and-after-form
1440 (edebug-inc-offset (car offset))
1442 (edebug-inc-offset (cdr offset))
1445 (t ;; just a variable
1446 (edebug-make-after-form form (edebug-inc-offset (cdr offset))))))
1448 ;; Anything else is self-evaluating.
1450 (edebug-move-cursor cursor))))
1453 (defsubst edebug-forms (cursor) (edebug-match cursor '(&rest form)))
1454 (defsubst edebug-sexps (cursor) (edebug-match cursor '(&rest sexp)))
1456 (defsubst edebug-list-form-args (head cursor)
1457 ;; Process the arguments of a list form given that head of form is a symbol.
1458 ;; Helper for edebug-list-form
1459 (let ((spec (get-edebug-spec head)))
1464 ;; It is a speclist.
1465 (let (edebug-best-error
1466 edebug-error-point);; This may not be needed.
1467 (edebug-match-sublist cursor spec)))
1468 ((eq t spec) (edebug-forms cursor))
1469 ((eq 0 spec) (edebug-sexps cursor))
1470 ((symbolp spec) (funcall spec cursor));; Not used by edebug,
1471 ; but leave it in for compatibility.
1473 ;; No edebug-form-spec provided.
1474 ((edebug-macrop head)
1475 (if edebug-eval-macro-args
1476 (edebug-forms cursor)
1477 (edebug-sexps cursor)))
1478 (t ;; Otherwise it is a function call.
1479 (edebug-forms cursor)))))
1482 (defun edebug-list-form (cursor)
1483 ;; Return an instrumented form built from the list form.
1484 ;; The after offset will be left in the cursor after processing the form.
1485 (let ((head (edebug-top-element-required cursor "Expected elements"))
1486 ;; Prevent backtracking whenever instrumenting.
1488 ;; A list form is never optional because it matches anything.
1489 (edebug-&optional nil)
1491 ;; Skip the first offset.
1492 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1493 (cdr (edebug-cursor-offsets cursor)))
1495 ((null head) nil) ; () is legal.
1500 (edebug-syntax-error "nil head"))
1501 ((eq head 'interactive-p)
1502 ;; Special case: replace (interactive-p) with variable
1503 (setq edebug-def-interactive 'check-it)
1504 (edebug-move-cursor cursor)
1505 (edebug-interactive-p-name))
1507 (cons head (edebug-list-form-args
1508 head (edebug-move-cursor cursor))))))
1511 (if (and (listp head) (eq (car head) ',))
1512 (edebug-match cursor '(("," def-form) body))
1513 ;; Process anonymous function and args.
1514 ;; This assumes no anonymous macros.
1515 (edebug-match-specs cursor '(lambda-expr body) 'edebug-match-specs)))
1517 (t (edebug-syntax-error
1518 "Head of list form must be a symbol or lambda expression.")))
1522 ;;;; Matching of specs.
1523 ;;; ===================
1525 (defvar edebug-after-dotted-spec nil)
1527 (defvar edebug-matching-depth 0) ;; initial value
1528 (defconst edebug-max-depth 150) ;; maximum number of matching recursions.
1531 ;;;; Failure to match
1532 ;;; ==================
1533 ;; This throws to no-match, if there are higher alternatives.
1534 ;; Otherwise it signals an error. The place of the error is found
1535 ;; with the two before- and after-offset functions.
1537 (defun edebug-no-match (cursor &rest edebug-args)
1538 ;; Throw a no-match, or signal an error immediately if gate is active.
1539 ;; Remember this point in case we need to report this error.
1540 (setq edebug-error-point (or edebug-error-point
1541 (edebug-before-offset cursor))
1542 edebug-best-error (or edebug-best-error edebug-args))
1543 (if (and edebug-gate (not edebug-&optional))
1545 (if edebug-error-point
1546 (goto-char edebug-error-point))
1547 (apply 'edebug-syntax-error edebug-args))
1548 (funcall 'throw 'no-match edebug-args)))
1551 (defun edebug-match (cursor specs)
1552 ;; Top level spec matching function.
1553 ;; Used also at each lower level of specs.
1554 (let (edebug-&optional
1558 (edebug-gate edebug-gate) ;; locally bound to limit effect
1560 (edebug-match-specs cursor specs 'edebug-match-specs)))
1563 (defun edebug-match-one-spec (cursor spec)
1564 ;; Match one spec, which is not a keyword &-spec.
1566 ((symbolp spec) (edebug-match-symbol cursor spec))
1567 ((vectorp spec) (edebug-match cursor (append spec nil)))
1568 ((stringp spec) (edebug-match-string cursor spec))
1569 ((listp spec) (edebug-match-list cursor spec))
1573 (defun edebug-match-specs (cursor specs remainder-handler)
1574 ;; Append results of matching the list of specs.
1575 ;; The first spec is handled and the remainder-handler handles the rest.
1576 (let ((edebug-matching-depth
1577 (if (> edebug-matching-depth edebug-max-depth)
1578 (error "too deep - perhaps infinite loop in spec?")
1579 (1+ edebug-matching-depth))))
1583 ;; Is the spec dotted?
1585 (let ((edebug-dotted-spec t));; Containing spec list was dotted.
1586 (edebug-match-specs cursor (list specs) remainder-handler)))
1588 ;; Is the form dotted?
1589 ((not (listp (edebug-cursor-expressions cursor)));; allow nil
1590 (if (not edebug-dotted-spec)
1591 (edebug-no-match cursor "Dotted spec required."))
1592 ;; Cancel dotted spec and dotted form.
1593 (let ((edebug-dotted-spec)
1594 (this-form (edebug-cursor-expressions cursor))
1595 (this-offset (edebug-cursor-offsets cursor)))
1596 ;; Wrap the form in a list, (by changing the cursor??)...
1597 (edebug-set-cursor cursor (list this-form) this-offset)
1598 ;; and process normally, then unwrap the result.
1599 (car (edebug-match-specs cursor specs remainder-handler))))
1601 (t;; Process normally.
1602 (let* ((spec (car specs))
1604 (first-char (and (symbolp spec) (aref (symbol-name spec) 0))))
1605 ;;(message "spec = %s first char = %s" spec first-char) (sit-for 1)
1608 ((eq ?& first-char);; "&" symbols take all following specs.
1609 (funcall (get-edebug-spec spec) cursor (cdr specs)))
1610 ((eq ?: first-char);; ":" symbols take one following spec.
1611 (setq rest (cdr (cdr specs)))
1612 (funcall (get-edebug-spec spec) cursor (car (cdr specs))))
1613 (t;; Any other normal spec.
1614 (setq rest (cdr specs))
1615 (edebug-match-one-spec cursor spec)))
1616 (funcall remainder-handler cursor rest remainder-handler)))))))
1619 ;; Define specs for all the symbol specs with functions used to process them.
1620 ;; Perhaps we shouldnt be doing this with edebug-form-specs since the
1621 ;; user may want to define macros or functions with the same names.
1622 ;; We could use an internal obarray for these primitive specs.
1625 (function (lambda (pair)
1626 (put (car pair) 'edebug-form-spec (cdr pair))))
1627 '((&optional . edebug-match-&optional)
1628 (&rest . edebug-match-&rest)
1629 (&or . edebug-match-&or)
1630 (form . edebug-match-form)
1631 (sexp . edebug-match-sexp)
1632 (body . edebug-match-body)
1633 (&define . edebug-match-&define)
1634 (name . edebug-match-name)
1635 (:name . edebug-match-colon-name)
1636 (arg . edebug-match-arg)
1637 (def-body . edebug-match-def-body)
1638 (def-form . edebug-match-def-form)
1639 ;; Less frequently used:
1640 ;; (function . edebug-match-function)
1641 (lambda-expr . edebug-match-lambda-expr)
1642 ;; (keywordp . edebug-match-keywordp)
1643 (¬ . edebug-match-¬)
1644 (&key . edebug-match-&key)
1645 (place . edebug-match-place)
1646 (gate . edebug-match-gate)
1647 ;; (nil . edebug-match-nil) not this one - special case it.
1650 (defun edebug-match-symbol (cursor symbol)
1651 ;; Match a symbol spec.
1652 (let* ((spec (get-edebug-spec symbol)))
1656 ;; It is an indirect spec.
1657 (edebug-match cursor spec)
1658 ;; Otherwise it should be the symbol name of a function.
1659 ;; There could be a bug here - maybe need to do edebug-match bindings.
1660 (funcall spec cursor)))
1662 ((null symbol) ;; special case this.
1663 (edebug-match-nil cursor))
1665 ((fboundp symbol) ; is it a predicate?
1666 (let ((sexp (edebug-top-element-required cursor "Expected" symbol)))
1667 ;; Special case for edebug-`.
1668 (if (and (listp sexp) (eq (car sexp) ',))
1669 (edebug-match cursor '(("," def-form)))
1670 (if (not (funcall symbol sexp))
1671 (edebug-no-match cursor symbol "failed"))
1672 (edebug-move-cursor cursor)
1674 (t (error "%s is not a form-spec or function" symbol))
1678 (defun edebug-match-sexp (cursor)
1679 (list (prog1 (edebug-top-element-required cursor "Expected sexp")
1680 (edebug-move-cursor cursor))))
1682 (defun edebug-match-form (cursor)
1683 (list (edebug-form cursor)))
1685 (defalias 'edebug-match-place 'edebug-match-form)
1686 ;; Currently identical to edebug-match-form.
1687 ;; This is for common lisp setf-style place arguments.
1689 (defsubst edebug-match-body (cursor) (edebug-forms cursor))
1691 (defun edebug-match-&optional (cursor specs)
1692 ;; Keep matching until one spec fails.
1693 (edebug-&optional-wrapper cursor specs 'edebug-&optional-wrapper))
1695 (defun edebug-&optional-wrapper (cursor specs remainder-handler)
1697 (edebug-&optional specs)
1699 (this-form (edebug-cursor-expressions cursor))
1700 (this-offset (edebug-cursor-offsets cursor)))
1701 (if (null (catch 'no-match
1703 (edebug-match-specs cursor specs remainder-handler))
1704 ;; Returning nil means no no-match was thrown.
1707 ;; no-match, but don't fail; just reset cursor and return nil.
1708 (edebug-set-cursor cursor this-form this-offset)
1712 (defun edebug-&rest-wrapper (cursor specs remainder-handler)
1713 (if (null specs) (setq specs edebug-&rest))
1714 ;; Reuse the &optional handler with this as the remainder handler.
1715 (edebug-&optional-wrapper cursor specs remainder-handler))
1717 (defun edebug-match-&rest (cursor specs)
1718 ;; Repeatedly use specs until failure.
1719 (let ((edebug-&rest specs) ;; remember these
1722 (edebug-&rest-wrapper cursor specs 'edebug-&rest-wrapper)))
1725 (defun edebug-match-&or (cursor specs)
1726 ;; Keep matching until one spec succeeds, and return its results.
1727 ;; If none match, fail.
1728 ;; This needs to be optimized since most specs spend time here.
1729 (let ((original-specs specs)
1730 (this-form (edebug-cursor-expressions cursor))
1731 (this-offset (edebug-cursor-offsets cursor)))
1736 (let (edebug-gate ;; only while matching each spec
1739 ;; Doesnt support e.g. &or symbolp &rest form
1740 (edebug-match-one-spec cursor (car specs)))))
1741 ;; Match failed, so reset and try again.
1742 (setq specs (cdr specs))
1743 ;; Reset the cursor for the next match.
1744 (edebug-set-cursor cursor this-form this-offset))
1746 (apply 'edebug-no-match cursor "Expected one of" original-specs))
1750 (defun edebug-match-¬ (cursor specs)
1751 ;; If any specs match, then fail
1752 (if (null (catch 'no-match
1753 (let ((edebug-gate nil))
1755 (edebug-match-&or cursor specs)))
1757 ;; This means something matched, so it is a no match.
1758 (edebug-no-match cursor "Unexpected"))
1759 ;; This means nothing matched, so it is OK.
1760 nil) ;; So, return nothing
1763 (def-edebug-spec &key edebug-match-&key)
1765 (defun edebug-match-&key (cursor specs)
1766 ;; Following specs must look like (<name> <spec>) ...
1767 ;; where <name> is the name of a keyword, and spec is its spec.
1768 ;; This really doesnt save much over the expanded form and takes time.
1772 (mapcar (function (lambda (pair)
1773 (vector (format ":%s" (car pair))
1778 (defun edebug-match-gate (cursor)
1779 ;; Simply set the gate to prevent backtracking at this level.
1780 (setq edebug-gate t)
1784 (defun edebug-match-list (cursor specs)
1785 ;; The spec is a list, but what kind of list, and what context?
1786 (if edebug-dotted-spec
1787 ;; After dotted spec but form did not contain dot,
1788 ;; so match list spec elements as if spliced in.
1790 (let ((edebug-dotted-spec))
1791 (edebug-match-specs cursor specs 'edebug-match-specs))
1792 ;; If it matched, really clear the dotted-spec flag.
1793 (setq edebug-dotted-spec nil))
1794 (let ((spec (car specs))
1795 (form (edebug-top-element-required cursor "Expected" specs)))
1798 (let ((spec (car (cdr specs))))
1801 ;; Special case: spec quotes a symbol to match.
1802 ;; Change in future. Use "..." instead.
1803 (if (not (eq spec form))
1804 (edebug-no-match cursor "Expected" spec))
1805 (edebug-move-cursor cursor)
1806 (setq edebug-gate t)
1809 (error "Bad spec: %s" specs)))))
1813 (list (edebug-match-sublist
1814 ;; First offset is for the list form itself.
1815 ;; Treat nil as empty list.
1816 (edebug-new-cursor form (cdr (edebug-top-offset cursor)))
1818 (edebug-move-cursor cursor)))
1820 ((and (eq 'vector spec) (vectorp form))
1821 ;; Special case: match a vector with the specs.
1822 (let ((result (edebug-match-sublist
1824 form (cdr (edebug-top-offset cursor)))
1826 (edebug-move-cursor cursor)
1827 (list (apply 'vector result))))
1829 (t (edebug-no-match cursor "Expected" specs)))
1833 (defun edebug-match-sublist (cursor specs)
1834 ;; Match a sublist of specs.
1835 (let (edebug-&optional
1837 ;;edebug-error-point
1840 ;; match with edebug-match-specs so edebug-best-error is not bound.
1841 (edebug-match-specs cursor specs 'edebug-match-specs)
1842 (if (not (edebug-empty-cursor cursor))
1843 (if edebug-best-error
1844 (apply 'edebug-no-match cursor edebug-best-error)
1845 ;; A failed &rest or &optional spec may leave some args.
1846 (edebug-no-match cursor "Failed matching" specs)
1850 (defun edebug-match-string (cursor spec)
1851 (let ((sexp (edebug-top-element-required cursor "Expected" spec)))
1852 (if (not (eq (intern spec) sexp))
1853 (edebug-no-match cursor "Expected" spec)
1854 ;; Since it matched, failure means immediate error, unless &optional.
1855 (setq edebug-gate t)
1856 (edebug-move-cursor cursor)
1860 (defun edebug-match-nil (cursor)
1861 ;; There must be nothing left to match a nil.
1862 (if (not (edebug-empty-cursor cursor))
1863 (edebug-no-match cursor "Unmatched argument(s)")
1867 (defun edebug-match-function (cursor)
1868 (error "Use function-form instead of function in edebug spec"))
1870 (defun edebug-match-&define (cursor specs)
1871 ;; Match a defining form.
1872 ;; Normally, &define is interpretted specially other places.
1873 ;; This should only be called inside of a spec list to match the remainder
1874 ;; of the current list. e.g. ("lambda" &define args def-body)
1875 (edebug-make-form-wrapper
1877 (edebug-before-offset cursor)
1878 ;; Find the last offset in the list.
1879 (let ((offsets (edebug-cursor-offsets cursor)))
1880 (while (consp offsets) (setq offsets (cdr offsets)))
1884 (defun edebug-match-lambda-expr (cursor)
1885 ;; The expression must be a function.
1886 ;; This will match any list form that begins with a symbol
1887 ;; that has an edebug-form-spec beginning with &define. In
1888 ;; practice, only lambda expressions should be used.
1889 ;; I could add a &lambda specification to avoid confusion.
1890 (let* ((sexp (edebug-top-element-required
1891 cursor "Expected lambda expression"))
1892 (offset (edebug-top-offset cursor))
1893 (head (and (consp sexp) (car sexp)))
1894 (spec (and (symbolp head) (get-edebug-spec head)))
1895 (edebug-inside-func nil))
1896 ;; Find out if this is a defining form from first symbol.
1897 (if (and (consp spec) (eq '&define (car spec)))
1900 (edebug-defining-form
1901 (edebug-new-cursor sexp offset)
1902 (car offset);; before the sexp
1903 (edebug-after-offset cursor)
1904 (cons (symbol-name head) (cdr spec))))
1905 (edebug-move-cursor cursor))
1906 (edebug-no-match cursor "Expected lambda expression")
1910 ;; Not needed if the predicate exists.
1911 '(defun edebug-match-keywordp (cursor)
1912 ;; Match a common lisp style keyword symbol.
1913 (let ((sexp (edebug-top-element cursor)))
1917 (edebug-move-cursor cursor))
1918 (edebug-no-match cursor "Keyword expected"))))
1921 (defun edebug-match-name (cursor)
1922 ;; Set the edebug-def-name bound in edebug-defining-form.
1923 (let ((name (edebug-top-element-required cursor "Expected name")))
1924 ;; Maybe strings and numbers could be used.
1925 (if (not (symbolp name))
1926 (edebug-no-match cursor "Symbol expected for name of definition"))
1927 (setq edebug-def-name
1929 ;; Construct a new name by appending to previous name.
1930 (intern (format "%s@%s" edebug-def-name name))
1932 (edebug-move-cursor cursor)
1935 (defun edebug-match-colon-name (cursor spec)
1936 ;; Set the edebug-def-name to the spec.
1937 (setq edebug-def-name
1939 ;; Construct a new name by appending to previous name.
1940 (intern (format "%s@%s" edebug-def-name spec))
1944 (defun edebug-match-arg (cursor)
1945 ;; set the def-args bound in edebug-defining-form
1946 (let ((edebug-arg (edebug-top-element-required cursor "Expected arg")))
1947 (if (or (not (symbolp edebug-arg))
1948 (lambda-list-keywordp edebug-arg))
1949 (edebug-no-match cursor "Bad argument:" edebug-arg))
1950 (edebug-move-cursor cursor)
1951 (setq edebug-def-args (cons edebug-arg edebug-def-args))
1954 (defun edebug-match-def-form (cursor)
1955 ;; Like form but the form is wrapped in edebug-enter form.
1956 ;; The form is assumed to be executing outside of the function context.
1957 ;; This is a hack for now, since a def-form might execute inside as well.
1958 ;; Not to be used otherwise.
1959 (let ((edebug-inside-func nil))
1960 (list (edebug-make-enter-wrapper (list (edebug-form cursor))))))
1962 (defun edebug-match-def-body (cursor)
1963 ;; Like body but body is wrapped in edebug-enter form.
1964 ;; The body is assumed to be executing inside of the function context.
1965 ;; Not to be used otherwise.
1966 (let ((edebug-inside-func t))
1967 (list (edebug-wrap-def-body (edebug-forms cursor)))))
1970 ;;;; Edebug Form Specs
1971 ;;; ==========================================================
1972 ;;; See cl-specs.el for common lisp specs.
1974 ;;;;* Spec for def-edebug-spec
1977 (defun edebug-spec-p (object)
1978 "Return non-nil if OBJECT is a symbol with an edebug-form-spec property."
1979 (and (symbolp object)
1980 (get object 'edebug-form-spec)))
1982 (def-edebug-spec def-edebug-spec
1983 ;; Top level is different from lower levels.
1984 (&define :name edebug-spec name
1985 &or "nil" edebug-spec-p "t" "0" (&rest edebug-spec)))
1987 (def-edebug-spec edebug-spec-list
1988 ;; A list must have something in it, or it is nil, a symbolp
1989 ((edebug-spec . [&or nil edebug-spec])))
1991 (def-edebug-spec edebug-spec
1993 (vector &rest edebug-spec) ; matches a vector
1994 ("vector" &rest edebug-spec) ; matches a vector spec
1998 [lambda-list-keywordp &rest edebug-spec]
1999 ;; [keywordp gate edebug-spec] ;; need keywordp for this.
2000 edebug-spec-p ;; Including all the special ones e.g. form.
2001 symbolp;; a predicate
2005 ;;;;* Emacs special forms and some functions.
2007 ;; quote expects only one argument, although it allows any number.
2008 (def-edebug-spec quote sexp)
2010 ;; The standard defining forms.
2011 (def-edebug-spec defconst defvar)
2012 (def-edebug-spec defvar (symbolp &optional form stringp))
2014 (def-edebug-spec defun
2015 (&define name lambda-list
2017 [&optional ("interactive" interactive)]
2019 (def-edebug-spec defmacro
2020 (&define name lambda-list def-body))
2022 (def-edebug-spec arglist lambda-list) ;; denegrated - use lambda-list.
2024 (def-edebug-spec lambda-list
2026 [&optional ["&optional" arg &rest arg]]
2027 &optional ["&rest" arg]
2030 (def-edebug-spec interactive
2031 (&optional &or stringp def-form))
2033 ;; A function-form is for an argument that may be a function or a form.
2034 ;; This specially recognizes anonymous functions quoted with quote.
2035 (def-edebug-spec function-form
2036 ;; form at the end could also handle "function",
2037 ;; but recognize it specially to avoid wrapping function forms.
2038 (&or ([&or "quote" "function"] &or symbolp lambda-expr) form))
2040 ;; function expects a symbol or a lambda or macro expression
2041 ;; A macro is allowed by Emacs.
2042 (def-edebug-spec function (&or symbolp lambda-expr))
2044 ;; lambda is a macro in emacs 19.
2045 (def-edebug-spec lambda (&define lambda-list
2047 [&optional ("interactive" interactive)]
2050 ;; A macro expression is a lambda expression with "macro" prepended.
2051 (def-edebug-spec macro (&define "lambda" lambda-list def-body))
2053 ;; (def-edebug-spec anonymous-form ((&or ["lambda" lambda] ["macro" macro])))
2055 ;; Standard functions that take function-forms arguments.
2056 (def-edebug-spec mapcar (function-form form))
2057 (def-edebug-spec mapconcat (function-form form form))
2058 (def-edebug-spec mapatoms (function-form &optional form))
2059 (def-edebug-spec apply (function-form &rest form))
2060 (def-edebug-spec funcall (function-form &rest form))
2062 (def-edebug-spec let
2063 ((&rest &or (symbolp &optional form) symbolp)
2066 (def-edebug-spec let* let)
2068 (def-edebug-spec setq (&rest symbolp form))
2069 (def-edebug-spec setq-default setq)
2071 (def-edebug-spec cond (&rest (&rest form)))
2073 (def-edebug-spec condition-case
2076 &rest (symbolp body)))
2079 (def-edebug-spec ` (backquote-form))
2081 ;; Supports quotes inside backquotes,
2082 ;; but only at the top level inside unquotes.
2083 (def-edebug-spec backquote-form
2085 ([&or "," ",@"] &or ("quote" backquote-form) form)
2086 (backquote-form &rest backquote-form)
2087 ;; If you use dotted forms in backquotes, replace the previous line
2088 ;; with the following. This takes quite a bit more stack space, however.
2089 ;; (backquote-form . [&or nil backquote-form])
2090 (vector &rest backquote-form)
2093 ;; Special version of backquote that instruments backquoted forms
2094 ;; destined to be evaluated, usually as the result of a
2095 ;; macroexpansion. Backquoted code can only have unquotes (, and ,@)
2096 ;; in places where list forms are allowed, and predicates. If the
2097 ;; backquote is used in a macro, unquoted code that come from
2098 ;; arguments must be instrumented, if at all, with def-form not def-body.
2100 ;; We could assume that all forms (not nested in other forms)
2101 ;; in arguments of macros should be def-forms, whether or not the macros
2102 ;; are defined with edebug-` but this would be expensive.
2104 ;; ,@ might have some problems.
2106 (defalias 'edebug-` '`) ;; same macro as regular backquote.
2107 (def-edebug-spec edebug-` (def-form))
2109 ;; Assume immediate quote in unquotes mean backquote at next higher level.
2110 (def-edebug-spec , (&or ("quote" edebug-`) def-form))
2111 (def-edebug-spec ,@ (&define ;; so (,@ form) is never wrapped.
2112 &or ("quote" edebug-`) def-form))
2114 ;; New byte compiler.
2115 (def-edebug-spec defsubst defun)
2116 (def-edebug-spec dont-compile t)
2117 (def-edebug-spec eval-when-compile t)
2118 (def-edebug-spec eval-and-compile t)
2123 ;;====================
2124 ;; Some miscellaneous specs for macros in public packages.
2127 ;; advice.el by Hans Chalupsky (hans@cs.buffalo.edu)
2129 (def-edebug-spec ad-dolist ((symbolp form &optional form) body))
2130 (def-edebug-spec defadvice
2131 (&define name ;; thing being advised.
2132 (name ;; class is [&or "before" "around" "after"
2133 ;; "activation" "deactivation"]
2134 name ;; name of advice
2135 &rest sexp ;; optional position and flags
2138 [&optional ("interactive" interactive)]
2142 ;;;; The debugger itself
2143 ;;; ===============================
2145 (defvar edebug-active nil) ;; Non-nil when edebug is active
2147 ;;; add minor-mode-alist entry
2148 (or (assq 'edebug-active minor-mode-alist)
2149 (setq minor-mode-alist (cons (list 'edebug-active " *Debugging*")
2152 (defvar edebug-stack nil)
2153 ;; Stack of active functions evaluated via edebug.
2154 ;; Should be nil at the top level.
2156 (defvar edebug-stack-depth -1)
2157 ;; Index of last edebug-stack item.
2159 (defvar edebug-offset-indices nil)
2160 ;; Stack of offset indices of visited edebug sexps.
2161 ;; Should be nil at the top level.
2162 ;; Each function adds one cons. Top is modified with setcar.
2165 (defvar edebug-entered nil
2166 ;; Non-nil if edebug has already been entered at this recursive edit level.
2167 ;; This should stay nil at the top level.
2170 ;; Should these be options?
2171 (defconst edebug-debugger 'edebug
2172 ;; Name of function to use for debugging when error or quit occurs.
2173 ;; Set this to 'debug if you want to debug edebug.
2177 ;; Dynamically bound variables, declared globally but left unbound.
2178 (defvar edebug-function) ; the function being executed. change name!!
2179 (defvar edebug-args) ; the arguments of the function
2180 (defvar edebug-data) ; the edebug data for the function
2181 (defvar edebug-value) ; the result of the expression
2182 (defvar edebug-after-index)
2183 (defvar edebug-def-mark) ; the mark for the definition
2184 (defvar edebug-freq-count) ; the count of expression visits.
2185 (defvar edebug-coverage) ; the coverage results of each expression of function.
2187 (defvar edebug-buffer) ; which buffer the function is in.
2188 (defvar edebug-result) ; the result of the function call returned by body
2189 (defvar edebug-outside-executing-macro)
2190 (defvar edebug-outside-defining-kbd-macro)
2192 (defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2193 (defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2195 (defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2196 (defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2198 (defvar edebug-outside-pre-command-hook)
2199 (defvar edebug-outside-post-command-hook)
2202 (defvar pre-command-hook nil)
2203 (defvar post-command-hook nil)
2205 (defvar cl-lexical-debug) ;; Defined in cl.el
2207 ;;; Handling signals
2208 ;;; =================
2210 (if (not (fboundp 'edebug-original-signal))
2211 (defalias 'edebug-original-signal (symbol-function 'signal)))
2212 ;; We should use advise for this!!
2214 (defun edebug-signal (edebug-signal-name edebug-signal-data)
2215 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2216 A signal name is a symbol with an `error-conditions' property
2217 that is a list of condition names.
2218 A handler for any of those names will get to handle this signal.
2219 The symbol `error' should always be one of them.
2221 DATA should be a list. Its elements are printed as part of the error message.
2222 If the signal is handled, DATA is made available to the handler.
2223 See `condition-case'.
2225 This is the Edebug replacement for the standard `signal'. It should
2226 only be active while Edebug is. It checks `debug-on-error' to see
2227 whether it should call the debugger. When execution is resumed, the
2228 error is signaled again."
2229 (if (and (listp debug-on-error) (memq edebug-signal-name debug-on-error))
2230 (edebug 'error (cons edebug-signal-name edebug-signal-data)))
2231 ;; If we reach here without another non-local exit, then send signal again.
2232 ;; i.e. the signal is not continuable, yet.
2233 (edebug-original-signal edebug-signal-name edebug-signal-data))
2237 ;;; ==================
2239 (defun edebug-enter (edebug-function edebug-args edebug-body)
2240 ;; Entering FUNC. The arguments are ARGS, and the body is BODY.
2241 ;; Setup edebug variables and evaluate BODY. This function is called
2242 ;; when a function evaluated with edebug-eval-top-level-form is entered.
2243 ;; Return the result of BODY.
2245 ;; Is this the first time we are entering edebug since
2246 ;; lower-level recursive-edit command?
2247 ;; More precisely, this tests whether Edebug is currently active.
2248 (if (not edebug-entered)
2249 (let ((edebug-entered t)
2250 ;; Binding max-lisp-eval-depth here is OK,
2251 ;; but not inside an unwind-protect.
2252 ;; Doing it here also keeps it from growing too large.
2253 (max-lisp-eval-depth (+ 100 max-lisp-eval-depth)) ; too much??
2254 (max-specpdl-size (+ 200 max-specpdl-size))
2256 (debugger edebug-debugger) ; only while edebug is active.
2257 (edebug-outside-debug-on-error debug-on-error)
2258 (edebug-outside-debug-on-quit debug-on-quit)
2259 ;; Binding these may not be the right thing to do.
2260 ;; We want to allow the global values to be changed.
2261 (debug-on-error (or debug-on-error edebug-on-error))
2262 (debug-on-quit edebug-on-quit)
2264 ;; Lexical bindings must be uncompiled for this to work.
2265 (cl-lexical-debug t)
2267 ;; Save the outside value of executing macro. (here??)
2268 (edebug-outside-executing-macro executing-macro)
2269 (edebug-outside-pre-command-hook pre-command-hook)
2270 (edebug-outside-post-command-hook post-command-hook))
2272 (let (;; Don't keep reading from an executing kbd macro
2273 ;; within edebug unless edebug-continue-kbd-macro is
2274 ;; non-nil. Again, local binding may not be best.
2276 (if edebug-continue-kbd-macro executing-macro))
2278 ;; Disable command hooks. This is essential when
2279 ;; a hook function is instrumented - to avoid infinite loop.
2280 ;; This may be more than we need, however.
2281 (pre-command-hook nil)
2282 (post-command-hook nil))
2283 (setq edebug-execution-mode (or edebug-next-execution-mode
2285 edebug-execution-mode)
2286 edebug-next-execution-mode nil)
2287 ;; Bind signal to edebug-signal only while Edebug is active.
2288 (fset 'signal 'edebug-signal)
2290 (edebug-enter edebug-function edebug-args edebug-body)
2291 (fset 'signal (symbol-function 'edebug-original-signal))))
2292 ;; Reset global variables in case outside value was changed.
2293 (setq executing-macro edebug-outside-executing-macro
2294 edebug-outside-pre-command-hook pre-command-hook
2295 edebug-outside-post-command-hook post-command-hook
2298 (let* ((edebug-data (get edebug-function 'edebug))
2299 (edebug-def-mark (car edebug-data)) ; mark at def start
2300 (edebug-freq-count (get edebug-function 'edebug-freq-count))
2301 (edebug-coverage (get edebug-function 'edebug-coverage))
2302 (edebug-buffer (marker-buffer edebug-def-mark))
2304 (edebug-stack (cons edebug-function edebug-stack))
2305 (edebug-offset-indices (cons 0 edebug-offset-indices))
2307 (if (get edebug-function 'edebug-on-entry)
2309 (setq edebug-execution-mode 'step)
2310 (if (eq (get edebug-function 'edebug-on-entry) 'temp)
2311 (put edebug-function 'edebug-on-entry nil))))
2313 (edebug-enter-trace edebug-body)
2314 (funcall edebug-body))
2318 (defun edebug-enter-trace (edebug-body)
2319 (let ((edebug-stack-depth (1+ edebug-stack-depth))
2321 (edebug-print-trace-before
2322 (format "%s args: %s" edebug-function edebug-args))
2323 (prog1 (setq edebug-result (funcall edebug-body))
2324 (edebug-print-trace-after
2325 (format "%s result: %s" edebug-function edebug-result)))))
2327 (def-edebug-spec edebug-tracing (form body))
2329 (defmacro edebug-tracing (msg &rest body)
2330 "Print MSG in *edebug-trace* before and after evaluating BODY.
2331 The result of BODY is also printed."
2332 (` (let ((edebug-stack-depth (1+ edebug-stack-depth))
2334 (edebug-print-trace-before (, msg))
2335 (prog1 (setq edebug-result (progn (,@ body)))
2336 (edebug-print-trace-after
2337 (format "%s result: %s" (, msg) edebug-result))))))
2339 (defun edebug-print-trace-before (msg)
2340 "Function called to print trace info before expression evaluation.
2341 MSG is printed after `::::{ '."
2342 (edebug-trace-display
2343 edebug-trace-buffer "%s{ %s" (make-string edebug-stack-depth ?\:) msg))
2345 (defun edebug-print-trace-after (msg)
2346 "Function called to print trace info after expression evaluation.
2347 MSG is printed after `::::} '."
2348 (edebug-trace-display
2349 edebug-trace-buffer "%s} %s" (make-string edebug-stack-depth ?\:) msg))
2353 (defun edebug-slow-before (edebug-before-index)
2354 ;; Debug current function given BEFORE position.
2355 ;; Called from functions compiled with edebug-eval-top-level-form.
2356 ;; Return the before index.
2357 (setcar edebug-offset-indices edebug-before-index)
2359 ;; Increment frequency count
2360 (aset edebug-freq-count edebug-before-index
2361 (1+ (aref edebug-freq-count edebug-before-index)))
2363 (if (or (not (memq edebug-execution-mode '(Go-nonstop next)))
2364 (edebug-input-pending-p))
2365 (edebug-debugger edebug-before-index 'before nil))
2366 edebug-before-index)
2368 (defun edebug-fast-before (edebug-before-index)
2372 (defun edebug-slow-after (edebug-before-index edebug-after-index edebug-value)
2373 ;; Debug current function given AFTER position and VALUE.
2374 ;; Called from functions compiled with edebug-eval-top-level-form.
2376 (setcar edebug-offset-indices edebug-after-index)
2378 ;; Increment frequency count
2379 (aset edebug-freq-count edebug-after-index
2380 (1+ (aref edebug-freq-count edebug-after-index)))
2381 (if edebug-test-coverage (edebug-update-coverage))
2383 (if (and (eq edebug-execution-mode 'Go-nonstop)
2384 (not (edebug-input-pending-p)))
2385 ;; Just return result.
2387 (edebug-debugger edebug-after-index 'after edebug-value)
2390 (defun edebug-fast-after (edebug-before-index edebug-after-index edebug-value)
2391 ;; Do nothing but return the value.
2394 (defun edebug-run-slow ()
2395 (defalias 'edebug-before 'edebug-slow-before)
2396 (defalias 'edebug-after 'edebug-slow-after))
2398 ;; This is not used, yet.
2399 (defun edebug-run-fast ()
2400 (defalias 'edebug-before 'edebug-fast-before)
2401 (defalias 'edebug-after 'edebug-fast-after))
2406 (defun edebug-update-coverage ()
2407 (let ((old-result (aref edebug-coverage edebug-after-index)))
2409 ((eq 'ok-coverage old-result))
2410 ((eq 'unknown old-result)
2411 (aset edebug-coverage edebug-after-index edebug-value))
2412 ;; Test if a different result.
2413 ((not (eq edebug-value old-result))
2414 (aset edebug-coverage edebug-after-index 'ok-coverage)))))
2417 ;; Dynamically declared unbound variables.
2418 (defvar edebug-arg-mode) ; the mode, either before, after, or error
2419 (defvar edebug-breakpoints)
2420 (defvar edebug-break-data) ; break data for current function.
2421 (defvar edebug-break) ; whether a break occurred.
2422 (defvar edebug-global-break) ; whether a global break occurred.
2423 (defvar edebug-break-condition) ; whether the breakpoint is conditional.
2425 (defvar edebug-break-result nil)
2426 (defvar edebug-global-break-result nil)
2429 (defun edebug-debugger (edebug-offset-index edebug-arg-mode edebug-value)
2430 ;; Check breakpoints and pending input.
2431 ;; If edebug display should be updated, call edebug-display.
2432 ;; Return edebug-value.
2433 (let* (;; This needs to be here since breakpoints may be changed.
2434 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
2435 (edebug-break-data (assq edebug-offset-index edebug-breakpoints))
2436 (edebug-break-condition (car (cdr edebug-break-data)))
2437 (edebug-global-break
2438 (if edebug-global-break-condition
2440 (setq edebug-global-break-result
2441 (eval edebug-global-break-condition))
2445 ;;; (edebug-trace "exp: %s" edebug-value)
2446 ;; Test whether we should break.
2448 (or edebug-global-break
2449 (and edebug-break-data
2450 (or (not edebug-break-condition)
2451 (setq edebug-break-result
2452 (eval edebug-break-condition))))))
2453 (if (and edebug-break
2454 (nth 2 edebug-break-data)) ; is it temporary?
2455 ;; Delete the breakpoint.
2457 (cons (delq edebug-break-data edebug-breakpoints)
2458 (cdr (cdr edebug-data)))))
2460 ;; Display if mode is not go, continue, or Continue-fast
2461 ;; or break, or input is pending,
2462 (if (or (not (memq edebug-execution-mode '(go continue Continue-fast)))
2464 (edebug-input-pending-p))
2465 (edebug-display)) ; <--------------- display
2471 ;; window-start now stored with each function.
2472 ;;(defvar edebug-window-start nil)
2473 ;; Remember where each buffers' window starts between edebug calls.
2474 ;; This is to avoid spurious recentering.
2475 ;; Does this still need to be buffer-local??
2476 ;;(setq-default edebug-window-start nil)
2477 ;;(make-variable-buffer-local 'edebug-window-start)
2480 ;; Dynamically declared unbound vars
2481 (defvar edebug-point) ; the point in edebug buffer
2482 (defvar edebug-outside-buffer) ; the current-buffer outside of edebug
2483 (defvar edebug-outside-point) ; the point outside of edebug
2484 (defvar edebug-outside-mark) ; the mark outside of edebug
2485 (defvar edebug-window-data) ; window and window-start for current function
2486 (defvar edebug-outside-windows) ; outside window configuration
2487 (defvar edebug-eval-buffer) ; for the evaluation list.
2488 (defvar edebug-outside-o-a-p) ; outside overlay-arrow-position
2489 (defvar edebug-outside-o-a-s) ; outside overlay-arrow-string
2490 (defvar edebug-outside-c-i-e-a) ; outside cursor-in-echo-area
2492 (defvar edebug-eval-list nil) ;; List of expressions to evaluate.
2494 (defvar edebug-previous-result nil) ;; Last result returned.
2496 ;; Emacs 19 adds an arg to mark and mark-marker.
2497 (defalias 'edebug-mark 'mark)
2498 (defalias 'edebug-mark-marker 'mark-marker)
2501 (defun edebug-display ()
2502 ;; Setup windows for edebug, determine mode, maybe enter recursive-edit.
2503 ;; Uses local variables of edebug-enter, edebug-before, edebug-after
2504 ;; and edebug-debugger.
2505 (let ((edebug-active t) ; for minor mode alist
2506 edebug-stop ; should we enter recursive-edit
2507 (edebug-point (+ edebug-def-mark
2508 (aref (nth 2 edebug-data) edebug-offset-index)))
2509 edebug-buffer-outside-point ; current point in edebug-buffer
2510 ;; window displaying edebug-buffer
2511 (edebug-window-data (nth 3 edebug-data))
2512 (edebug-outside-window (selected-window))
2513 (edebug-outside-buffer (current-buffer))
2514 (edebug-outside-point (point))
2515 (edebug-outside-mark (edebug-mark))
2516 edebug-outside-windows ; window or screen configuration
2517 edebug-buffer-points
2519 edebug-eval-buffer ; declared here so we can kill it below
2520 (edebug-eval-result-list (and edebug-eval-list
2521 (edebug-eval-result-list)))
2523 edebug-trace-window-start
2525 (edebug-outside-o-a-p overlay-arrow-position)
2526 (edebug-outside-o-a-s overlay-arrow-string)
2527 (edebug-outside-c-i-e-a cursor-in-echo-area))
2529 (let ((overlay-arrow-position overlay-arrow-position)
2530 (overlay-arrow-string overlay-arrow-string)
2531 (cursor-in-echo-area nil)
2534 (if (not (buffer-name edebug-buffer))
2535 (let ((debug-on-error nil))
2536 (error "Buffer defining %s not found" edebug-function)))
2538 (if (eq 'after edebug-arg-mode)
2539 ;; Compute result string now before windows are modified.
2540 (edebug-compute-previous-result edebug-value))
2542 (if edebug-save-windows
2543 ;; Save windows now before we modify them.
2544 (setq edebug-outside-windows
2545 (edebug-current-windows edebug-save-windows)))
2547 (if edebug-save-displayed-buffer-points
2548 (setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
2550 ;; First move the edebug buffer point to edebug-point
2551 ;; so that window start doesnt get changed when we display it.
2552 ;; I dont know if this is going to help.
2553 ;;(set-buffer edebug-buffer)
2554 ;;(goto-char edebug-point)
2556 ;; If edebug-buffer is not currently displayed,
2557 ;; first find a window for it.
2558 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data))
2559 (setcar edebug-window-data (selected-window))
2561 ;; Now display eval list, if any.
2562 ;; This is done after the pop to edebug-buffer
2563 ;; so that buffer-window correspondence is correct after quitting.
2564 (edebug-eval-display edebug-eval-result-list)
2565 ;; The evaluation list better not have deleted edebug-window-data.
2566 (select-window (car edebug-window-data))
2567 (set-buffer edebug-buffer)
2569 (setq edebug-buffer-outside-point (point))
2570 (goto-char edebug-point)
2572 (if (eq 'before edebug-arg-mode)
2573 ;; Check whether positions are up-to-date.
2574 ;; This assumes point is never before symbol.
2575 (if (not (memq (following-char) '(?\( ?\# ?\` )))
2576 (let ((debug-on-error nil))
2577 (error "Source has changed - reevaluate definition of %s"
2581 (setcdr edebug-window-data
2582 (edebug-adjust-window (cdr edebug-window-data)))
2584 ;; Test if there is input, not including keyboard macros.
2585 (if (edebug-input-pending-p)
2587 (setq edebug-execution-mode 'step
2590 ;; (discard-input) ; is this unfriendly??
2592 ;; Now display arrow based on mode.
2593 (edebug-overlay-arrow)
2596 ((eq 'error edebug-arg-mode)
2597 ;; Display error message
2598 (setq edebug-execution-mode 'step)
2599 (edebug-overlay-arrow)
2601 (if (eq 'quit (car edebug-value))
2603 (edebug-report-error edebug-value)))
2606 (edebug-global-break
2607 (message "Global Break: %s => %s"
2608 edebug-global-break-condition
2609 edebug-global-break-result))
2610 (edebug-break-condition
2611 (message "Break: %s => %s"
2612 edebug-break-condition
2613 edebug-break-result))
2614 ((not (eq edebug-execution-mode 'Continue-fast))
2620 (if (eq 'after edebug-arg-mode)
2622 ;; Display result of previous evaluation.
2623 (if (and edebug-break
2624 (not (eq edebug-execution-mode 'Continue-fast)))
2625 (sit-for 1)) ; Show break message.
2626 (edebug-previous-result)))
2631 ((eq edebug-execution-mode 'continue) (edebug-sit-for 1))
2632 ((eq edebug-execution-mode 'Continue-fast) (edebug-sit-for 0))
2633 (t (setq edebug-stop t))))
2635 ((eq edebug-execution-mode 'trace)
2636 (edebug-sit-for 1)) ; Force update and pause.
2637 ((eq edebug-execution-mode 'Trace-fast)
2638 (edebug-sit-for 0)) ; Force update and continue.
2643 (memq edebug-execution-mode '(step next))
2644 (eq edebug-arg-mode 'error))
2646 ;; (setq edebug-execution-mode 'step)
2647 ;; (edebug-overlay-arrow) ; this doesnt always show up.
2648 (edebug-recursive-edit))) ; <---------- Recursive edit
2650 ;; Reset the edebug-window-data to whatever it is now.
2651 (let ((window (if (eq (window-buffer) edebug-buffer)
2653 (edebug-get-buffer-window edebug-buffer))))
2654 ;; Remember window-start for edebug-buffer, if still displayed.
2657 (setcar edebug-window-data window)
2658 (setcdr edebug-window-data (window-start window)))))
2660 ;; Save trace window point before restoring outside windows.
2661 ;; Could generalize this for other buffers.
2662 (setq edebug-trace-window (get-buffer-window edebug-trace-buffer))
2663 (if edebug-trace-window
2664 (setq edebug-trace-window-start
2665 (and edebug-trace-window
2666 (window-start edebug-trace-window))))
2668 ;; Restore windows before continuing.
2669 (if edebug-save-windows
2671 (edebug-set-windows edebug-outside-windows)
2673 ;; Restore displayed buffer points.
2674 ;; Needed even if restoring windows because
2675 ;; window-points are not restored. (should they be??)
2676 (if edebug-save-displayed-buffer-points
2677 (edebug-set-buffer-points edebug-buffer-points))
2679 ;; Unrestore trace window's window-point.
2680 (if edebug-trace-window
2681 (set-window-start edebug-trace-window
2682 edebug-trace-window-start))
2684 ;; Unrestore edebug-buffer's window-start, if displayed.
2685 (let ((window (car edebug-window-data)))
2686 (if (and window (edebug-window-live-p window)
2687 (eq (window-buffer) edebug-buffer))
2689 (set-window-start window (cdr edebug-window-data)
2691 ;; Unrestore edebug-buffer's window-point.
2692 ;; Needed in addition to setting the buffer point
2693 ;; - otherwise quitting doesnt leave point as is.
2694 ;; But this causes point to not be restored at times.
2695 ;; Also, it may not be a visible window.
2696 ;; (set-window-point window edebug-point)
2699 ;; Unrestore edebug-buffer's point. Rerestored below.
2700 ;; (goto-char edebug-point) ;; in edebug-buffer
2702 ;; Since we may be in a save-excursion, in case of quit,
2703 ;; reselect the outside window only.
2704 ;; Only needed if we are not recovering windows??
2705 (if (edebug-window-live-p edebug-outside-window)
2706 (select-window edebug-outside-window))
2707 ) ; if edebug-save-windows
2709 ;; Restore current buffer always, in case application needs it.
2710 (set-buffer edebug-outside-buffer)
2711 ;; Restore point, and mark.
2712 ;; Needed even if restoring windows because
2713 ;; that doesnt restore point and mark in the current buffer.
2714 ;; But dont restore point if edebug-buffer is current buffer.
2715 (if (not (eq edebug-buffer edebug-outside-buffer))
2716 (goto-char edebug-outside-point))
2717 (if (marker-buffer (edebug-mark-marker))
2718 ;; Does zmacs-regions need to be nil while doing set-marker?
2719 (set-marker (edebug-mark-marker) edebug-outside-mark))
2721 ;; None of the following is done if quit or signal occurs.
2723 ;; Restore edebug-buffer's outside point.
2724 ;; (edebug-trace "restore edebug-buffer point: %s"
2725 ;; edebug-buffer-outside-point)
2726 (let ((current-buffer (current-buffer)))
2727 (set-buffer edebug-buffer)
2728 (goto-char edebug-buffer-outside-point)
2729 (set-buffer current-buffer))
2730 ;; ... nothing more.
2732 ;; Reset global variables to outside values in case they were changed.
2734 overlay-arrow-position edebug-outside-o-a-p
2735 overlay-arrow-string edebug-outside-o-a-s
2736 cursor-in-echo-area edebug-outside-c-i-e-a)
2740 (defvar edebug-number-of-recursions 0)
2741 ;; Number of recursive edits started by edebug.
2742 ;; Should be 0 at the top level.
2744 (defvar edebug-recursion-depth 0)
2745 ;; Value of recursion-depth when edebug was called.
2747 ;; Dynamically declared unbound vars
2748 (defvar edebug-outside-match-data) ; match data outside of edebug
2749 (defvar edebug-backtrace-buffer) ; each recursive edit gets its own
2750 (defvar edebug-inside-windows)
2751 (defvar edebug-interactive-p)
2753 (defvar edebug-outside-map)
2754 (defvar edebug-outside-standard-output)
2755 (defvar edebug-outside-standard-input)
2756 (defvar edebug-outside-last-command-char)
2757 (defvar edebug-outside-last-command)
2758 (defvar edebug-outside-this-command)
2759 (defvar edebug-outside-last-input-char)
2761 ;; Note: here we have defvars for variables that are
2762 ;; built-in in certain versions.
2763 ;; Each defvar makes a difference
2764 ;; in versions where the variable is *not* built-in.
2767 (defvar edebug-outside-unread-command-char)
2768 (defvar unread-command-char -1)
2771 (defvar edebug-outside-unread-command-event) ;; like unread-command-events
2772 (defvar unread-command-event nil)
2775 (defvar edebug-outside-last-command-event)
2776 (defvar edebug-outside-unread-command-events)
2777 (defvar edebug-outside-last-input-event)
2778 (defvar edebug-outside-last-event-frame)
2779 (defvar edebug-outside-last-nonmenu-event)
2780 (defvar edebug-outside-track-mouse)
2782 ;; For Emacs 18, define vars defined by Emacs 19.
2783 (defvar last-input-event nil)
2784 (defvar last-command-event nil)
2785 (defvar unread-command-events nil)
2786 (defvar last-event-frame nil)
2787 (defvar last-nonmenu-event nil)
2788 (defvar track-mouse nil)
2790 ;; Disable byte compiler warnings about unread-command-char and -event
2791 ;; (maybe works with byte-compile-version 2.22 at least)
2792 (defvar edebug-unread-command-char-warning)
2793 (defvar edebug-unread-command-event-warning)
2795 (setq edebug-unread-command-char-warning
2796 (get 'unread-command-char 'byte-obsolete-variable))
2797 (put 'unread-command-char 'byte-obsolete-variable nil)
2798 (setq edebug-unread-command-event-warning
2799 (get 'unread-command-event 'byte-obsolete-variable))
2800 (put 'unread-command-event 'byte-obsolete-variable nil))
2802 (defun edebug-recursive-edit ()
2803 ;; Start up a recursive edit inside of edebug.
2804 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
2805 ;; Assume that none of the variables below are buffer-local.
2806 (let ((edebug-buffer-read-only buffer-read-only)
2807 ;; match-data must be done in the outside buffer
2808 (edebug-outside-match-data
2809 (save-excursion ; might be unnecessary now??
2810 (set-buffer edebug-outside-buffer) ; in case match buffer different
2813 ;;(edebug-number-of-recursions (1+ edebug-number-of-recursions))
2814 (edebug-recursion-depth (recursion-depth))
2815 edebug-entered ; bind locally to nil
2816 (edebug-interactive-p nil) ; again non-interactive
2817 edebug-backtrace-buffer ; each recursive edit gets its own
2818 ;; The window configuration may be saved and restored
2819 ;; during a recursive-edit
2820 edebug-inside-windows
2822 (edebug-outside-map (current-local-map))
2824 (edebug-outside-standard-output standard-output)
2825 (edebug-outside-standard-input standard-input)
2826 (edebug-outside-defining-kbd-macro defining-kbd-macro)
2828 (edebug-outside-last-command-char last-command-char)
2829 (edebug-outside-last-command last-command)
2830 (edebug-outside-this-command this-command)
2831 (edebug-outside-last-input-char last-input-char)
2833 (edebug-outside-unread-command-char unread-command-char)
2835 (edebug-outside-last-input-event last-input-event)
2836 (edebug-outside-last-command-event last-command-event)
2837 (edebug-outside-unread-command-event unread-command-event)
2838 (edebug-outside-unread-command-events unread-command-events)
2839 (edebug-outside-last-event-frame last-event-frame)
2840 (edebug-outside-last-nonmenu-event last-nonmenu-event)
2841 (edebug-outside-track-mouse track-mouse)
2846 ;; Declare global values local but using the same global value.
2847 ;; We could set these to the values for previous edebug call.
2848 (last-command-char last-command-char)
2849 (last-command last-command)
2850 (this-command this-command)
2851 (last-input-char last-input-char)
2853 ;; Assume no edebug command sets unread-command-char.
2854 (unread-command-char -1)
2856 ;; More for Emacs 19
2857 (last-input-event nil)
2858 (last-command-event nil)
2859 (unread-command-event nil);; lemacs
2860 (unread-command-events nil)
2861 (last-event-frame nil)
2862 (last-nonmenu-event nil)
2865 ;; Bind again to outside values.
2866 (debug-on-error edebug-outside-debug-on-error)
2867 (debug-on-quit edebug-outside-debug-on-quit)
2869 ;; Don't keep defining a kbd macro.
2871 (if edebug-continue-kbd-macro defining-kbd-macro))
2876 (if (fboundp 'zmacs-deactivate-region);; for lemacs
2877 (zmacs-deactivate-region))
2878 (if (and (eq edebug-execution-mode 'go)
2879 (not (memq edebug-arg-mode '(after error))))
2882 (setq buffer-read-only t)
2883 (fset 'signal (symbol-function 'edebug-original-signal))
2887 (recursive-edit) ; <<<<<<<<<< Recursive edit
2889 ;; Do the following, even if quit occurs.
2890 (fset 'signal 'edebug-signal)
2891 (if edebug-backtrace-buffer
2892 (kill-buffer edebug-backtrace-buffer))
2893 ;; Could be an option to keep eval display up.
2894 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
2896 ;; Remember selected-window after recursive-edit.
2897 ;; (setq edebug-inside-window (selected-window))
2899 (store-match-data edebug-outside-match-data)
2901 ;; Recursive edit may have changed buffers,
2902 ;; so set it back before exiting let.
2903 (if (buffer-name edebug-buffer) ; if it still exists
2905 (set-buffer edebug-buffer)
2906 (if (memq edebug-execution-mode '(go Go-nonstop))
2907 (edebug-overlay-arrow))
2908 (setq buffer-read-only edebug-buffer-read-only)
2909 (use-local-map edebug-outside-map)
2911 ;; gotta have a buffer to let its buffer local variables be set
2912 (get-buffer-create " bogus edebug buffer"))
2915 ;; Reset global vars to outside values, in case they have been changed.
2917 last-command-char edebug-outside-last-command-char
2918 last-command-event edebug-outside-last-command-event
2919 last-command edebug-outside-last-command
2920 this-command edebug-outside-this-command
2921 unread-command-char edebug-outside-unread-command-char
2922 unread-command-event edebug-outside-unread-command-event
2923 unread-command-events edebug-outside-unread-command-events
2924 last-input-char edebug-outside-last-input-char
2925 last-input-event edebug-outside-last-input-event
2926 last-event-frame edebug-outside-last-event-frame
2927 last-nonmenu-event edebug-outside-last-nonmenu-event
2928 track-mouse edebug-outside-track-mouse
2930 standard-output edebug-outside-standard-output
2931 standard-input edebug-outside-standard-input
2932 defining-kbd-macro edebug-outside-defining-kbd-macro
2937 ;;; Display related functions
2938 ;;; ===============================
2940 (defun edebug-adjust-window (old-start)
2941 ;; If pos is not visible, adjust current window to fit following context.
2942 ;;; (message "window: %s old-start: %s window-start: %s pos: %s"
2943 ;;; (selected-window) old-start (window-start) (point)) (sit-for 5)
2944 (if (not (pos-visible-in-window-p))
2946 ;; First try old-start
2948 (set-window-start (selected-window) old-start))
2949 (if (not (pos-visible-in-window-p))
2951 ;; (message "resetting window start") (sit-for 2)
2956 (if (< (point) (window-start)) -1 ; one line before if in back
2957 (- (/ (window-height) 2)) ; center the line moving forward
2965 (defconst edebug-arrow-alist
2966 '((Continue-fast . "=")
2973 (Go-nonstop . "..") ; not used
2975 "Association list of arrows for each edebug mode.")
2977 (defun edebug-overlay-arrow ()
2978 ;; Set up the overlay arrow at beginning-of-line in current buffer.
2979 ;; The arrow string is derived from edebug-arrow-alist and
2980 ;; edebug-execution-mode.
2981 (let ((pos (save-excursion (beginning-of-line) (point))))
2982 (setq overlay-arrow-string
2983 (cdr (assq edebug-execution-mode edebug-arrow-alist)))
2984 (setq overlay-arrow-position (make-marker))
2985 (set-marker overlay-arrow-position pos (current-buffer))))
2988 (defun edebug-toggle-save-all-windows ()
2989 "Toggle the saving and restoring of all windows.
2990 Also, each time you toggle it on, the inside and outside window
2991 configurations become the same as the current configuration."
2993 (setq edebug-save-windows (not edebug-save-windows))
2994 (if edebug-save-windows
2995 (setq edebug-inside-windows
2996 (setq edebug-outside-windows
2997 (edebug-current-windows
2998 edebug-save-windows))))
2999 (message "Window saving is %s for all windows."
3000 (if edebug-save-windows "on" "off")))
3002 (defmacro edebug-changing-windows (&rest body)
3003 (` (let ((window (selected-window)))
3004 (setq edebug-inside-windows (edebug-current-windows t))
3005 (edebug-set-windows edebug-outside-windows)
3006 (,@ body) ;; Code to change edebug-save-windows
3007 (setq edebug-outside-windows (edebug-current-windows
3008 edebug-save-windows))
3009 ;; Problem: what about outside windows that are deleted inside?
3010 (edebug-set-windows edebug-inside-windows))))
3012 (defun edebug-toggle-save-selected-window ()
3013 "Toggle the saving and restoring of the selected window.
3014 Also, each time you toggle it on, the inside and outside window
3015 configurations become the same as the current configuration."
3018 ((eq t edebug-save-windows)
3019 ;; Save all outside windows except the selected one.
3020 ;; Remove (selected-window) from outside-windows.
3021 (edebug-changing-windows
3022 (setq edebug-save-windows (delq window (edebug-window-list)))))
3024 ((memq (selected-window) edebug-save-windows)
3025 (setq edebug-outside-windows
3026 (delq (assq (selected-window) edebug-outside-windows)
3027 edebug-outside-windows))
3028 (setq edebug-save-windows
3029 (delq (selected-window) edebug-save-windows)))
3030 (t ; Save a new window.
3031 (edebug-changing-windows
3032 (setq edebug-save-windows (cons window edebug-save-windows)))))
3034 (message "Window saving is %s for %s."
3035 (if (memq (selected-window) edebug-save-windows)
3039 (defun edebug-toggle-save-windows (arg)
3040 "Toggle the saving and restoring of windows.
3041 With prefix, toggle for just the selected window.
3042 Otherwise, toggle for all windows."
3045 (edebug-toggle-save-selected-window)
3046 (edebug-toggle-save-all-windows)))
3049 (defun edebug-where ()
3050 "Show the debug windows and where we stopped in the program."
3052 (if (not edebug-active)
3053 (error "Edebug is not active"))
3054 ;; Restore the window configuration to what it last was inside.
3055 ;; But it is not always set. - experiment
3056 ;;(if edebug-inside-windows
3057 ;; (edebug-set-windows edebug-inside-windows))
3058 (edebug-pop-to-buffer edebug-buffer)
3059 (goto-char edebug-point))
3061 (defun edebug-view-outside ()
3062 "Change to the outside window configuration."
3064 (if (not edebug-active)
3065 (error "Edebug is not active"))
3066 (setq edebug-inside-windows
3067 (edebug-current-windows edebug-save-windows))
3068 (edebug-set-windows edebug-outside-windows)
3069 (goto-char edebug-outside-point)
3070 (message "Window configuration outside of Edebug. Return with %s"
3071 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
3074 (defun edebug-bounce-point (arg)
3075 "Bounce the point in the outside current buffer.
3076 If prefix arg is supplied, sit for that many seconds before returning.
3077 The default is one second."
3079 (if (not edebug-active)
3080 (error "Edebug is not active"))
3082 ;; If the buffer's currently displayed, avoid set-window-configuration.
3083 (save-window-excursion
3084 (edebug-pop-to-buffer edebug-outside-buffer)
3085 (goto-char edebug-outside-point)
3086 (message "Current buffer: %s Point: %s Mark: %s"
3087 (current-buffer) (point)
3088 (if (marker-buffer (edebug-mark-marker))
3089 (marker-position (edebug-mark-marker)) "<not set>"))
3090 (edebug-sit-for arg)
3091 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data)))))
3094 ;; Joe Wells, here is a start at your idea of adding a buffer to the internal
3095 ;; display list. Still need to use this list in edebug-display.
3097 '(defvar edebug-display-buffer-list nil
3098 "List of buffers that edebug will display when it is active.")
3100 '(defun edebug-display-buffer (buffer)
3101 "Toggle display of a buffer inside of edebug."
3102 (interactive "bBuffer: ")
3103 (let ((already-displaying (memq buffer edebug-display-buffer-list)))
3104 (setq edebug-display-buffer-list
3105 (if already-displaying
3106 (delq buffer edebug-display-buffer-list)
3107 (cons buffer edebug-display-buffer-list)))
3108 (message "Displaying %s %s" buffer
3109 (if already-displaying "off" "on"))))
3112 ;;; Breakpoint related functions
3113 ;;; ===============================
3115 (defun edebug-find-stop-point ()
3116 ;; Return (function . index) of the nearest edebug stop point.
3117 (let* ((edebug-def-name (edebug-form-data-symbol))
3119 (let ((data (get edebug-def-name 'edebug)))
3120 (if (or (null data) (markerp data))
3121 (error "%s is not instrumented for Edebug" edebug-def-name))
3122 data)) ; we could do it automatically, if data is a marker.
3123 ;; pull out parts of edebug-data.
3124 (edebug-def-mark (car edebug-data))
3125 ;; (edebug-breakpoints (car (cdr edebug-data)))
3127 (offset-vector (nth 2 edebug-data))
3128 (offset (- (save-excursion
3129 (if (looking-at "[ \t]")
3130 ;; skip backwards until non-whitespace, or bol
3131 (skip-chars-backward " \t"))
3135 ;; the offsets are in order so we can do a linear search
3136 (setq len (length offset-vector))
3138 (while (and (< i len) (> offset (aref offset-vector i)))
3141 (<= offset (aref offset-vector i)))
3142 ;; return the relevant info
3143 (cons edebug-def-name i)
3144 (message "Point is not on an expression in %s."
3149 (defun edebug-next-breakpoint ()
3150 "Move point to the next breakpoint, or first if none past point."
3152 (let ((edebug-stop-point (edebug-find-stop-point)))
3153 (if edebug-stop-point
3154 (let* ((edebug-def-name (car edebug-stop-point))
3155 (index (cdr edebug-stop-point))
3156 (edebug-data (get edebug-def-name 'edebug))
3158 ;; pull out parts of edebug-data
3159 (edebug-def-mark (car edebug-data))
3160 (edebug-breakpoints (car (cdr edebug-data)))
3161 (offset-vector (nth 2 edebug-data))
3163 (if (not edebug-breakpoints)
3164 (message "No breakpoints in this function.")
3165 (let ((breaks edebug-breakpoints))
3167 (<= (car (car breaks)) index))
3168 (setq breaks (cdr breaks)))
3172 ;; goto the first breakpoint
3173 (car edebug-breakpoints)))
3174 (goto-char (+ edebug-def-mark
3175 (aref offset-vector (car breakpoint))))
3177 (message (concat (if (nth 2 breakpoint)
3179 (if (car (cdr breakpoint))
3180 (format "Condition: %s"
3181 (edebug-safe-prin1-to-string
3182 (car (cdr breakpoint))))
3187 (defun edebug-modify-breakpoint (flag &optional condition temporary)
3188 "Modify the breakpoint for the form at point or after it according
3189 to FLAG: set if t, clear if nil. Then move to that point.
3190 If CONDITION or TEMPORARY are non-nil, add those attributes to
3192 (let ((edebug-stop-point (edebug-find-stop-point)))
3193 (if edebug-stop-point
3194 (let* ((edebug-def-name (car edebug-stop-point))
3195 (index (cdr edebug-stop-point))
3196 (edebug-data (get edebug-def-name 'edebug))
3198 ;; pull out parts of edebug-data
3199 (edebug-def-mark (car edebug-data))
3200 (edebug-breakpoints (car (cdr edebug-data)))
3201 (offset-vector (nth 2 edebug-data))
3203 ;; delete it either way
3204 (setq present (assq index edebug-breakpoints))
3205 (setq edebug-breakpoints (delq present edebug-breakpoints))
3208 ;; add it to the list and resort
3209 (setq edebug-breakpoints
3212 (list index condition temporary)
3213 edebug-breakpoints) '<))
3215 (message "Breakpoint set in %s with condition: %s"
3216 edebug-def-name condition)
3217 (message "Breakpoint set in %s" edebug-def-name)))
3219 (message "Breakpoint unset in %s" edebug-def-name)
3220 (message "No breakpoint here")))
3222 (setcar (cdr edebug-data) edebug-breakpoints)
3223 (goto-char (+ edebug-def-mark (aref offset-vector index)))
3226 (defun edebug-set-breakpoint (arg)
3227 "Set the breakpoint of nearest sexp.
3228 With prefix argument, make it a temporary breakpoint."
3230 (edebug-modify-breakpoint t nil arg))
3232 (defun edebug-unset-breakpoint ()
3233 "Clear the breakpoint of nearest sexp."
3235 (edebug-modify-breakpoint nil))
3238 ;; For emacs 18, no read-expression-history
3239 (defun edebug-set-conditional-breakpoint (arg condition)
3240 "Set a conditional breakpoint at nearest sexp.
3241 The condition is evaluated in the outside context.
3242 With prefix argument, make it a temporary breakpoint."
3243 ;; (interactive "P\nxCondition: ")
3247 ;; Edit previous condition as follows, but it is cumbersome:
3248 (let ((edebug-stop-point (edebug-find-stop-point)))
3249 (if edebug-stop-point
3250 (let* ((edebug-def-name (car edebug-stop-point))
3251 (index (cdr edebug-stop-point))
3252 (edebug-data (get edebug-def-name 'edebug))
3253 (edebug-breakpoints (car (cdr edebug-data)))
3254 (edebug-break-data (assq index edebug-breakpoints))
3255 (edebug-break-condition (car (cdr edebug-break-data))))
3257 (format "Condition in %s: " edebug-def-name)
3258 (if edebug-break-condition
3259 (format "%s" edebug-break-condition)
3261 (edebug-modify-breakpoint t condition arg))
3264 (defun edebug-set-global-break-condition (expression)
3265 (interactive (list (read-minibuffer
3266 "Global Condition: "
3267 (format "%s" edebug-global-break-condition))))
3268 (setq edebug-global-break-condition expression))
3271 ;;; Mode switching functions
3272 ;;; ===============================
3274 (defun edebug-set-mode (mode shortmsg msg)
3275 ;; Set the edebug mode to MODE.
3276 ;; Display SHORTMSG, or MSG if not within edebug.
3277 (if (eq (1+ edebug-recursion-depth) (recursion-depth))
3279 (setq edebug-execution-mode mode)
3281 ;; Continue execution
3282 (exit-recursive-edit))
3283 ;; This is not terribly useful!!
3284 (setq edebug-next-execution-mode mode)
3288 (defalias 'edebug-step-through-mode 'edebug-step-mode)
3290 (defun edebug-step-mode ()
3291 "Proceed to next stop point."
3293 (edebug-set-mode 'step "" "Edebug will stop at next stop point."))
3295 (defun edebug-next-mode ()
3296 "Proceed to next `after' stop point."
3298 (edebug-set-mode 'next "" "Edebug will stop after next eval."))
3300 (defun edebug-go-mode (arg)
3301 "Go, evaluating until break.
3302 With prefix ARG, set temporary break at current point and go."
3305 (edebug-set-breakpoint t))
3306 (edebug-set-mode 'go "Go..." "Edebug will go until break."))
3308 (defun edebug-Go-nonstop-mode ()
3309 "Go, evaluating without debugging."
3311 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
3312 "Edebug will not stop at breaks."))
3315 (defun edebug-trace-mode ()
3318 (edebug-set-mode 'trace "Tracing..." "Edebug will trace with pause."))
3320 (defun edebug-Trace-fast-mode ()
3321 "Trace with no wait at each step."
3323 (edebug-set-mode 'Trace-fast
3324 "Trace fast..." "Edebug will trace without pause."))
3326 (defun edebug-continue-mode ()
3327 "Begin continue mode."
3329 (edebug-set-mode 'continue "Continue..."
3330 "Edebug will pause at breakpoints."))
3332 (defun edebug-Continue-fast-mode ()
3333 "Trace with no wait at each step."
3335 (edebug-set-mode 'Continue-fast "Continue fast..."
3336 "Edebug will stop and go at breakpoints."))
3338 ;; ------------------------------------------------------------
3339 ;; The following use the mode changing commands and breakpoints.
3342 (defun edebug-goto-here ()
3343 "Proceed to this stop point."
3348 (defun edebug-stop ()
3349 "Stop execution and do not continue.
3350 Useful for exiting from trace or continue loop."
3355 '(defun edebug-forward ()
3356 "Proceed to the exit of the next expression to be evaluated."
3360 "Edebug will stop after exiting the next expression."))
3363 (defun edebug-forward-sexp (arg)
3364 "Proceed from the current point to the end of the ARGth sexp ahead.
3365 If there are not ARG sexps ahead, then do edebug-step-out."
3368 (let ((parse-sexp-ignore-comments t))
3369 ;; Call forward-sexp repeatedly until done or failure.
3376 (defun edebug-step-out ()
3377 "Proceed from the current point to the end of the containing sexp.
3378 If there is no containing sexp that is not the top level defun,
3379 go to the end of the last sexp, or if that is the same point, then step."
3382 (let ((parse-sexp-ignore-comments t))
3385 ;; Is there still a containing expression?
3389 ;; At top level - 1, so first check if there are more sexps at this level.
3390 (let ((start-point (point)))
3393 (if (= (point) start-point)
3394 (edebug-step-mode) ; No more at this level, so step.
3398 (defun edebug-instrument-function (func)
3399 ;; Func should be a function symbol.
3400 ;; Return the function symbol, or nil if not instrumented.
3401 (let ((func-marker))
3402 (setq func-marker (get func 'edebug))
3404 ((markerp func-marker)
3405 ;; It is uninstrumented, so instrument it.
3407 (set-buffer (marker-buffer func-marker))
3408 (goto-char func-marker)
3409 (edebug-eval-top-level-form)
3411 ((consp func-marker)
3412 (message "%s is already instrumented." func)
3415 ;; We could try harder, e.g. do a tags search.
3416 (error "Don't know where %s is defined" func)
3419 (defun edebug-instrument-callee ()
3420 "Instrument the definition of the function or macro about to be called.
3421 Do this when stopped before the form or it will be too late.
3422 One side effect of using this command is that the next time the
3423 function or macro is called, Edebug will be called there as well."
3425 (if (not (looking-at "\("))
3426 (error "You must be before a list form")
3430 (if (looking-at "\(")
3431 (edebug-form-data-name
3432 (edebug-get-form-data-entry (point)))
3433 (edebug-original-read (current-buffer))))))
3434 (edebug-instrument-function func))))
3437 (defun edebug-step-in ()
3438 "Step into the definition of the function or macro about to be called.
3439 This first does `edebug-instrument-callee' to ensure that it is
3440 instrumented. Then it does `edebug-on-entry' and switches to `go' mode."
3442 (let ((func (edebug-instrument-callee)))
3445 (edebug-on-entry func 'temp)
3446 (edebug-go-mode nil)))))
3448 (defun edebug-on-entry (function &optional flag)
3449 "Cause Edebug to stop when FUNCTION is called.
3450 With prefix argument, make this temporary so it is automatically
3451 cancelled the first time the function is entered."
3452 (interactive "aEdebug on entry to: \nP")
3453 ;; Could store this in the edebug data instead.
3454 (put function 'edebug-on-entry (if flag 'temp t)))
3456 (defun cancel-edebug-on-entry (function)
3457 (interactive "aEdebug on entry to: ")
3458 (put function 'edebug-on-entry nil))
3461 (if (not (fboundp 'edebug-original-debug-on-entry))
3462 (fset 'edebug-original-debug-on-entry (symbol-function 'debug-on-entry)))
3463 '(fset 'debug-on-entry 'edebug-debug-on-entry) ;; Should we do this?
3464 ;; Also need edebug-cancel-debug-on-entry
3466 '(defun edebug-debug-on-entry (function)
3467 "Request FUNCTION to invoke debugger each time it is called.
3468 If the user continues, FUNCTION's execution proceeds.
3469 Works by modifying the definition of FUNCTION,
3470 which must be written in Lisp, not predefined.
3471 Use `cancel-debug-on-entry' to cancel the effect of this command.
3472 Redefining FUNCTION also does that.
3474 This version is from Edebug. If the function is instrumented for
3475 Edebug, it calls `edebug-on-entry'"
3476 (interactive "aDebug on entry (to function): ")
3477 (let ((func-data (get function 'edebug)))
3478 (if (or (null func-data) (markerp func-data))
3479 (edebug-original-debug-on-entry function)
3480 (edebug-on-entry function))))
3483 (defun edebug-top-level-nonstop ()
3484 "Set mode to Go-nonstop, and exit to top-level.
3485 This is useful for exiting even if unwind-protect code may be executed."
3487 (setq edebug-execution-mode 'Go-nonstop)
3491 ;;(defun edebug-exit-out ()
3492 ;; "Go until the current function exits."
3494 ;; (edebug-set-mode 'exiting "Exit..."))
3497 ;;; -----------------------------------------------------------------
3498 ;;; The following initial mode setting definitions are not used yet.
3500 '(defconst edebug-initial-mode-alist
3501 '((edebug-Continue-fast . Continue-fast)
3502 (edebug-Trace-fast . Trace-fast)
3503 (edebug-continue . continue)
3504 (edebug-trace . trace)
3506 (edebug-step-through . step)
3507 (edebug-Go-nonstop . Go-nonstop)
3509 "Association list between commands and the modes they set.")
3512 '(defun edebug-set-initial-mode ()
3513 "Ask for the initial mode of the enclosing function.
3514 The mode is requested via the key that would be used to set the mode in
3517 (let* ((this-function (edebug-which-function))
3518 (keymap (if (eq edebug-mode-map (current-local-map))
3520 (old-mode (or (get this-function 'edebug-initial-mode)
3521 edebug-initial-mode))
3522 (key (read-key-sequence
3524 "Change initial edebug mode for %s from %s (%s) to (enter key): "
3528 (car (rassq old-mode edebug-initial-mode-alist))
3531 (mode (cdr (assq (key-binding key) edebug-initial-mode-alist)))
3534 (or (get this-function 'edebug-initial-mode)
3535 (not (eq mode edebug-initial-mode))))
3537 (put this-function 'edebug-initial-mode mode)
3538 (message "Initial mode for %s is now: %s"
3539 this-function mode))
3540 (error "Key must map to one of the mode changing commands")
3544 ;;; Evaluation of expressions
3545 ;;; ===============================
3547 (def-edebug-spec edebug-outside-excursion t)
3549 (defmacro edebug-outside-excursion (&rest body)
3550 "Evaluate an expression list in the outside context.
3551 Return the result of the last expression."
3552 (` (save-excursion ; of current-buffer
3553 (if edebug-save-windows
3555 ;; After excursion, we will
3556 ;; restore to current window configuration.
3557 (setq edebug-inside-windows
3558 (edebug-current-windows edebug-save-windows))
3559 ;; Restore outside windows.
3560 (edebug-set-windows edebug-outside-windows)))
3562 (set-buffer edebug-buffer) ; why?
3563 ;; (use-local-map edebug-outside-map)
3564 (store-match-data edebug-outside-match-data)
3565 ;; Restore outside context.
3566 (let (;; (edebug-inside-map (current-local-map)) ;; restore map??
3567 (last-command-char edebug-outside-last-command-char)
3568 (last-command-event edebug-outside-last-command-event)
3569 (last-command edebug-outside-last-command)
3570 (this-command edebug-outside-this-command)
3571 (unread-command-char edebug-outside-unread-command-char)
3572 (unread-command-event edebug-outside-unread-command-event)
3573 (unread-command-events edebug-outside-unread-command-events)
3574 (last-input-char edebug-outside-last-input-char)
3575 (last-input-event edebug-outside-last-input-event)
3576 (last-event-frame edebug-outside-last-event-frame)
3577 (last-nonmenu-event edebug-outside-last-nonmenu-event)
3578 (track-mouse edebug-outside-track-mouse)
3579 (standard-output edebug-outside-standard-output)
3580 (standard-input edebug-outside-standard-input)
3582 (executing-macro edebug-outside-executing-macro)
3583 (defining-kbd-macro edebug-outside-defining-kbd-macro)
3584 (pre-command-hook edebug-outside-pre-command-hook)
3585 (post-command-hook edebug-outside-post-command-hook)
3587 ;; See edebug-display
3588 (overlay-arrow-position edebug-outside-o-a-p)
3589 (overlay-arrow-string edebug-outside-o-a-s)
3590 (cursor-in-echo-area edebug-outside-c-i-e-a)
3593 (save-excursion ; of edebug-buffer
3594 (set-buffer edebug-outside-buffer)
3595 (goto-char edebug-outside-point)
3596 (if (marker-buffer (edebug-mark-marker))
3597 (set-marker (edebug-mark-marker) edebug-outside-mark))
3600 ;; Back to edebug-buffer. Restore rest of inside context.
3601 ;; (use-local-map edebug-inside-map)
3602 (if edebug-save-windows
3603 ;; Restore inside windows.
3604 (edebug-set-windows edebug-inside-windows))
3606 ;; Save values that may have been changed.
3608 edebug-outside-last-command-char last-command-char
3609 edebug-outside-last-command-event last-command-event
3610 edebug-outside-last-command last-command
3611 edebug-outside-this-command this-command
3612 edebug-outside-unread-command-char unread-command-char
3613 edebug-outside-unread-command-event unread-command-event
3614 edebug-outside-unread-command-events unread-command-events
3615 edebug-outside-last-input-char last-input-char
3616 edebug-outside-last-input-event last-input-event
3617 edebug-outside-last-event-frame last-event-frame
3618 edebug-outside-last-nonmenu-event last-nonmenu-event
3619 edebug-outside-track-mouse track-mouse
3620 edebug-outside-standard-output standard-output
3621 edebug-outside-standard-input standard-input
3623 edebug-outside-executing-macro executing-macro
3624 edebug-outside-defining-kbd-macro defining-kbd-macro
3625 edebug-outside-pre-command-hook pre-command-hook
3626 edebug-outside-post-command-hook post-command-hook
3628 edebug-outside-o-a-p overlay-arrow-position
3629 edebug-outside-o-a-s overlay-arrow-string
3630 edebug-outside-c-i-e-a cursor-in-echo-area
3634 (defvar cl-debug-env nil) ;; defined in cl; non-nil when lexical env used.
3636 (defun edebug-eval (edebug-expr)
3637 ;; Are there cl lexical variables active?
3639 (eval (cl-macroexpand-all edebug-expr cl-debug-env))
3640 (eval edebug-expr)))
3642 (defun edebug-safe-eval (edebug-expr)
3643 ;; Evaluate EXPR safely.
3644 ;; If there is an error, a string is returned describing the error.
3645 (condition-case edebug-err
3646 (edebug-eval edebug-expr)
3647 (error (edebug-format "%s: %s" ;; could
3648 (get (car edebug-err) 'error-message)
3649 (car (cdr edebug-err))))))
3653 ;; Replace printing functions.
3656 (defalias 'edebug-install-custom-print-funcs 'edebug-install-custom-print)
3657 (defalias 'edebug-reset-print-funcs 'edebug-uninstall-custom-print)
3658 (defalias 'edebug-uninstall-custom-print-funcs 'edebug-uninstall-custom-print)
3660 (defun edebug-install-custom-print ()
3661 "Replace print functions used by Edebug with custom versions."
3662 ;; Modifying the custom print functions, or changing print-length,
3663 ;; print-level, print-circle, custom-print-list or custom-print-vector
3664 ;; have immediate effect.
3666 (require 'cust-print)
3667 (defalias 'edebug-prin1 'custom-prin1)
3668 (defalias 'edebug-print 'custom-print)
3669 (defalias 'edebug-prin1-to-string 'custom-prin1-to-string)
3670 (defalias 'edebug-format 'custom-format)
3671 (defalias 'edebug-message 'custom-message)
3675 (defun edebug-uninstall-custom-print ()
3676 "Replace edebug custom print functions with internal versions."
3678 (defalias 'edebug-prin1 'prin1)
3679 (defalias 'edebug-print 'print)
3680 (defalias 'edebug-prin1-to-string 'prin1-to-string)
3681 (defalias 'edebug-format 'format)
3682 (defalias 'edebug-message 'message)
3685 ;; Default print functions are the same as Emacs'.
3686 (edebug-uninstall-custom-print))
3689 (defun edebug-report-error (edebug-value)
3690 ;; Print an error message like command level does.
3691 ;; This also prints the error name if it has no error-message.
3693 (or (get (car edebug-value) 'error-message)
3694 (format "peculiar error (%s)" (car edebug-value)))
3695 (mapconcat (function (lambda (edebug-arg)
3696 ;; continuing after an error may
3697 ;; complain about edebug-arg. why??
3698 (prin1-to-string edebug-arg)))
3699 (cdr edebug-value) ", ")))
3701 ;; Define here in case they are not already defined.
3702 (defvar print-level nil)
3703 (defvar print-circle nil)
3704 (defvar print-readably) ;; defined by lemacs
3705 ;; Alternatively, we could change the definition of
3706 ;; edebug-safe-prin1-to-string to only use these if defined.
3708 (defun edebug-safe-prin1-to-string (value)
3709 (let ((print-escape-newlines t)
3710 (print-length (or edebug-print-length print-length))
3711 (print-level (or edebug-print-level print-level))
3712 (print-circle (or edebug-print-circle print-circle))
3713 (print-readably nil)) ;; lemacs uses this.
3714 (edebug-prin1-to-string value)))
3716 (defun edebug-compute-previous-result (edebug-previous-value)
3717 (setq edebug-previous-result
3718 (if (and (numberp edebug-previous-value)
3719 (< edebug-previous-value 256)
3720 (>= edebug-previous-value 0))
3721 (format "Result: %s = %s" edebug-previous-value
3722 (single-key-description edebug-previous-value))
3723 (if edebug-unwrap-results
3724 (setq edebug-previous-value
3725 (edebug-unwrap* edebug-previous-value)))
3727 (edebug-safe-prin1-to-string edebug-previous-value)))))
3729 (defun edebug-previous-result ()
3730 "Print the previous result."
3732 (message "%s" edebug-previous-result))
3734 ;;;; Read, Eval and Print
3735 ;;; =====================
3737 (defun edebug-eval-expression (edebug-expr)
3738 "Evaluate an expression in the outside environment.
3739 If interactive, prompt for the expression.
3740 Print result in minibuffer."
3741 (interactive "xEval: ")
3743 (edebug-outside-excursion
3744 (setq values (cons (edebug-eval edebug-expr) values))
3745 (edebug-safe-prin1-to-string (car values)))))
3747 (defun edebug-eval-last-sexp ()
3748 "Evaluate sexp before point in the outside environment;
3749 print value in minibuffer."
3751 (edebug-eval-expression (edebug-last-sexp)))
3753 (defun edebug-eval-print-last-sexp ()
3754 "Evaluate sexp before point in the outside environment;
3755 print value into current buffer."
3757 (let* ((edebug-form (edebug-last-sexp))
3758 (edebug-result-string
3759 (edebug-outside-excursion
3760 (edebug-safe-prin1-to-string (edebug-safe-eval edebug-form))))
3761 (standard-output (current-buffer)))
3763 ;; princ the string to get rid of quotes.
3764 (princ edebug-result-string)
3769 ;;;; Edebug Minor Mode
3770 ;;; ===============================
3772 ;; Global GUD bindings for all emacs-lisp-mode buffers.
3773 (define-key emacs-lisp-mode-map "\C-x\C-a\C-s" 'edebug-step-mode)
3774 (define-key emacs-lisp-mode-map "\C-x\C-a\C-n" 'edebug-next-mode)
3775 (define-key emacs-lisp-mode-map "\C-x\C-a\C-c" 'edebug-go-mode)
3776 (define-key emacs-lisp-mode-map "\C-x\C-a\C-l" 'edebug-where)
3779 (defvar edebug-mode-map nil)
3783 (setq edebug-mode-map (copy-keymap emacs-lisp-mode-map))
3785 (define-key edebug-mode-map " " 'edebug-step-mode)
3786 (define-key edebug-mode-map "n" 'edebug-next-mode)
3787 (define-key edebug-mode-map "g" 'edebug-go-mode)
3788 (define-key edebug-mode-map "G" 'edebug-Go-nonstop-mode)
3789 (define-key edebug-mode-map "t" 'edebug-trace-mode)
3790 (define-key edebug-mode-map "T" 'edebug-Trace-fast-mode)
3791 (define-key edebug-mode-map "c" 'edebug-continue-mode)
3792 (define-key edebug-mode-map "C" 'edebug-Continue-fast-mode)
3794 ;;(define-key edebug-mode-map "f" 'edebug-forward) not implemented
3795 (define-key edebug-mode-map "f" 'edebug-forward-sexp)
3796 (define-key edebug-mode-map "h" 'edebug-goto-here)
3798 (define-key edebug-mode-map "I" 'edebug-instrument-callee)
3799 (define-key edebug-mode-map "i" 'edebug-step-in)
3800 (define-key edebug-mode-map "o" 'edebug-step-out)
3802 ;; quitting and stopping
3803 (define-key edebug-mode-map "q" 'top-level)
3804 (define-key edebug-mode-map "Q" 'edebug-top-level-nonstop)
3805 (define-key edebug-mode-map "a" 'abort-recursive-edit)
3806 (define-key edebug-mode-map "S" 'edebug-stop)
3809 (define-key edebug-mode-map "b" 'edebug-set-breakpoint)
3810 (define-key edebug-mode-map "u" 'edebug-unset-breakpoint)
3811 (define-key edebug-mode-map "B" 'edebug-next-breakpoint)
3812 (define-key edebug-mode-map "x" 'edebug-set-conditional-breakpoint)
3813 (define-key edebug-mode-map "X" 'edebug-set-global-break-condition)
3816 (define-key edebug-mode-map "r" 'edebug-previous-result)
3817 (define-key edebug-mode-map "e" 'edebug-eval-expression)
3818 (define-key edebug-mode-map "\C-x\C-e" 'edebug-eval-last-sexp)
3819 (define-key edebug-mode-map "E" 'edebug-visit-eval-list)
3822 (define-key edebug-mode-map "w" 'edebug-where)
3823 (define-key edebug-mode-map "v" 'edebug-view-outside) ;; maybe obsolete??
3824 (define-key edebug-mode-map "p" 'edebug-bounce-point)
3825 (define-key edebug-mode-map "P" 'edebug-view-outside) ;; same as v
3826 (define-key edebug-mode-map "W" 'edebug-toggle-save-windows)
3829 (define-key edebug-mode-map "?" 'edebug-help)
3830 (define-key edebug-mode-map "d" 'edebug-backtrace)
3832 (define-key edebug-mode-map "-" 'negative-argument)
3835 (define-key edebug-mode-map "=" 'edebug-temp-display-freq-count)
3838 (define-key edebug-mode-map "\C-c\C-s" 'edebug-step-mode)
3839 (define-key edebug-mode-map "\C-c\C-n" 'edebug-next-mode)
3840 (define-key edebug-mode-map "\C-c\C-c" 'edebug-go-mode)
3842 (define-key edebug-mode-map "\C-x " 'edebug-set-breakpoint)
3843 (define-key edebug-mode-map "\C-c\C-d" 'edebug-unset-breakpoint)
3844 (define-key edebug-mode-map "\C-c\C-t"
3845 (function (lambda () (edebug-set-breakpoint t))))
3846 (define-key edebug-mode-map "\C-c\C-l" 'edebug-where)
3849 ;; Autoloading these global bindings doesn't make sense because
3850 ;; they cannot be used anyway unless Edebug is already loaded and active.
3852 (defvar global-edebug-prefix "\^XX"
3853 "Prefix key for global edebug commands, available from any buffer.")
3855 (defvar global-edebug-map nil
3856 "Global map of edebug commands, available from any buffer.")
3858 (if global-edebug-map
3860 (setq global-edebug-map (make-sparse-keymap))
3862 (global-unset-key global-edebug-prefix)
3863 (global-set-key global-edebug-prefix global-edebug-map)
3865 (define-key global-edebug-map " " 'edebug-step-mode)
3866 (define-key global-edebug-map "g" 'edebug-go-mode)
3867 (define-key global-edebug-map "G" 'edebug-Go-nonstop-mode)
3868 (define-key global-edebug-map "t" 'edebug-trace-mode)
3869 (define-key global-edebug-map "T" 'edebug-Trace-fast-mode)
3870 (define-key global-edebug-map "c" 'edebug-continue-mode)
3871 (define-key global-edebug-map "C" 'edebug-Continue-fast-mode)
3874 (define-key global-edebug-map "b" 'edebug-set-breakpoint)
3875 (define-key global-edebug-map "u" 'edebug-unset-breakpoint)
3876 (define-key global-edebug-map "x" 'edebug-set-conditional-breakpoint)
3877 (define-key global-edebug-map "X" 'edebug-set-global-break-condition)
3880 (define-key global-edebug-map "w" 'edebug-where)
3881 (define-key global-edebug-map "W" 'edebug-toggle-save-windows)
3884 (define-key global-edebug-map "q" 'top-level)
3885 (define-key global-edebug-map "Q" 'edebug-top-level-nonstop)
3886 (define-key global-edebug-map "a" 'abort-recursive-edit)
3889 (define-key global-edebug-map "=" 'edebug-display-freq-count)
3892 (defun edebug-help ()
3894 (describe-function 'edebug-mode))
3896 (defun edebug-mode ()
3897 "Mode for Emacs Lisp buffers while in Edebug.
3899 In addition to all Emacs Lisp commands (except those that modify the
3900 buffer) there are local and global key bindings to several Edebug
3901 specific commands. E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
3902 in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3904 Also see bindings for the eval list buffer, *edebug*.
3906 The edebug buffer commands:
3909 Global commands prefixed by `global-edebug-prefix':
3910 \\{global-edebug-map}
3917 edebug-save-displayed-buffer-points
3920 edebug-test-coverage
3921 edebug-continue-kbd-macro
3928 edebug-unwrap-results
3929 edebug-global-break-condition
3931 (use-local-map edebug-mode-map))
3934 ;;;; edebug eval list mode
3935 ;;; ===============================================
3936 ;; A list of expressions and their evaluations is displayed in *edebug*.
3938 (defun edebug-eval-result-list ()
3939 "Return a list of evaluations of edebug-eval-list"
3940 ;; Assumes in outside environment.
3941 ;; Don't do any edebug things now.
3942 (let ((edebug-execution-mode 'Go-nonstop)
3944 (mapcar 'edebug-safe-eval edebug-eval-list)))
3946 (defun edebug-eval-display-list (edebug-eval-result-list)
3947 ;; Assumes edebug-eval-buffer exists.
3948 (let ((edebug-eval-list-temp edebug-eval-list)
3949 (standard-output edebug-eval-buffer)
3950 (edebug-comment-line
3951 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
3952 (set-buffer edebug-eval-buffer)
3954 (while edebug-eval-list-temp
3955 (prin1 (car edebug-eval-list-temp)) (terpri)
3956 (prin1 (car edebug-eval-result-list)) (terpri)
3957 (princ edebug-comment-line)
3958 (setq edebug-eval-list-temp (cdr edebug-eval-list-temp))
3959 (setq edebug-eval-result-list (cdr edebug-eval-result-list)))
3960 (edebug-pop-to-buffer edebug-eval-buffer)
3963 (defun edebug-create-eval-buffer ()
3964 (if (not (and edebug-eval-buffer (buffer-name edebug-eval-buffer)))
3966 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
3967 (edebug-eval-mode))))
3969 ;; Should generalize this to be callable outside of edebug
3970 ;; with calls in user functions, e.g. (edebug-eval-display)
3972 (defun edebug-eval-display (edebug-eval-result-list)
3973 "Display expressions and evaluations in EVAL-LIST.
3974 It modifies the context by popping up the eval display."
3975 (if edebug-eval-result-list
3977 (edebug-create-eval-buffer)
3978 (edebug-eval-display-list edebug-eval-result-list)
3981 (defun edebug-eval-redisplay ()
3982 "Redisplay eval list in outside environment.
3983 May only be called from within edebug-recursive-edit."
3984 (edebug-create-eval-buffer)
3985 (edebug-outside-excursion
3986 (edebug-eval-display-list (edebug-eval-result-list))
3989 (defun edebug-visit-eval-list ()
3991 (edebug-eval-redisplay)
3992 (edebug-pop-to-buffer edebug-eval-buffer))
3995 (defun edebug-update-eval-list ()
3996 "Replace the evaluation list with the sexps now in the eval buffer."
3998 (let ((starting-point (point))
4000 (goto-char (point-min))
4001 ;; get the first expression
4002 (edebug-skip-whitespace)
4006 (setq new-list (cons (edebug-last-sexp) new-list))))
4008 (while (re-search-forward "^;" nil t)
4010 (skip-chars-forward " \t\n\r")
4011 (if (and (/= ?\; (following-char))
4015 (setq new-list (cons (edebug-last-sexp) new-list)))))
4017 (setq edebug-eval-list (nreverse new-list))
4018 (edebug-eval-redisplay)
4019 (goto-char starting-point)))
4022 (defun edebug-delete-eval-item ()
4023 "Delete the item under point and redisplay."
4024 ;; could add arg to do repeatedly
4026 (if (re-search-backward "^;" nil 'nofail)
4029 (point) (progn (re-search-forward "^;" nil 'nofail)
4032 (edebug-update-eval-list))
4036 (defvar edebug-eval-mode-map nil
4037 "Keymap for edebug-eval-mode. Superset of lisp-interaction-mode.")
4039 (if edebug-eval-mode-map
4041 (setq edebug-eval-mode-map (copy-keymap lisp-interaction-mode-map))
4043 (define-key edebug-eval-mode-map "\C-c\C-w" 'edebug-where)
4044 (define-key edebug-eval-mode-map "\C-c\C-d" 'edebug-delete-eval-item)
4045 (define-key edebug-eval-mode-map "\C-c\C-u" 'edebug-update-eval-list)
4046 (define-key edebug-eval-mode-map "\C-x\C-e" 'edebug-eval-last-sexp)
4047 (define-key edebug-eval-mode-map "\C-j" 'edebug-eval-print-last-sexp)
4051 (defun edebug-eval-mode ()
4052 "Mode for evaluation list buffer while in Edebug.
4054 In addition to all Interactive Emacs Lisp commands there are local and
4055 global key bindings to several Edebug specific commands. E.g.
4056 `edebug-step-mode' is bound to \\[edebug-step-mode] in the Edebug
4057 buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
4059 Eval list buffer commands:
4060 \\{edebug-eval-mode-map}
4062 Global commands prefixed by global-edebug-prefix:
4063 \\{global-edebug-map}
4065 (lisp-interaction-mode)
4066 (setq major-mode 'edebug-eval-mode)
4067 (setq mode-name "Edebug-Eval")
4068 (use-local-map edebug-eval-mode-map))
4071 ;;;; Interface with standard debugger.
4072 ;;; ========================================
4074 ;; (setq debugger 'edebug) ; to use the edebug debugger
4075 ;; (setq debugger 'debug) ; use the standard debugger
4077 ;; Note that debug and its utilities must be byte-compiled to work,
4078 ;; since they depend on the backtrace looking a certain way. But
4079 ;; edebug is not dependent on this, yet.
4081 (defun edebug (&optional edebug-arg-mode &rest debugger-args)
4082 "Replacement for debug.
4083 If we are running an edebugged function,
4084 show where we last were. Otherwise call debug normally."
4085 ;; (message "entered: %s depth: %s edebug-recursion-depth: %s"
4086 ;; edebug-entered (recursion-depth) edebug-recursion-depth) (sit-for 1)
4087 (if (and edebug-entered ; anything active?
4088 (eq (recursion-depth) edebug-recursion-depth))
4089 (let (;; Where were we before the error occurred?
4090 (edebug-offset-index (car edebug-offset-indices))
4091 ;; Bind variables required by edebug-display
4092 (edebug-value (car debugger-args))
4095 edebug-break-condition
4097 (edebug-break (null edebug-arg-mode)) ;; if called explicitly
4100 (if (eq edebug-arg-mode 'error)
4104 ;; Otherwise call debug normally.
4105 ;; Still need to remove extraneous edebug calls from stack.
4106 (apply 'debug edebug-arg-mode debugger-args)
4110 (defun edebug-backtrace ()
4111 "Display a non-working backtrace. Better than nothing..."
4113 (if (or (not edebug-backtrace-buffer)
4114 (null (buffer-name edebug-backtrace-buffer)))
4115 (setq edebug-backtrace-buffer
4116 (generate-new-buffer "*Backtrace*"))
4117 ;; else, could just display edebug-backtrace-buffer
4119 (with-output-to-temp-buffer (buffer-name edebug-backtrace-buffer)
4120 (setq edebug-backtrace-buffer standard-output)
4121 (let ((print-escape-newlines t)
4126 ;; Clean up the backtrace.
4127 ;; Not quite right for current edebug scheme.
4128 (set-buffer edebug-backtrace-buffer)
4129 (setq truncate-lines t)
4130 (goto-char (point-min))
4131 (setq last-ok-point (point))
4134 ;; Delete interspersed edebug internals.
4135 (while (re-search-forward "^ \(?edebug" nil t)
4138 ((looking-at "^ \(edebug-after")
4139 ;; Previous lines may contain code, so just delete this line
4140 (setq last-ok-point (point))
4142 (delete-region last-ok-point (point)))
4144 ((looking-at "^ edebug")
4146 (delete-region last-ok-point (point))
4152 ;; ===============================
4154 (defun edebug-trace-display (buf-name fmt &rest args)
4155 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
4156 The buffer is created if it does not exist.
4157 You must include newlines in FMT to break lines, but one newline is appended."
4159 ;; (edebug-trace-display "*trace-point*"
4160 ;; "saving: point = %s window-start = %s"
4161 ;; (point) (window-start))
4162 (let* ((selected-window (selected-window))
4163 (buffer (get-buffer-create buf-name))
4165 ;; (message "before pop-to-buffer") (sit-for 1)
4166 (edebug-pop-to-buffer buffer)
4167 (setq truncate-lines t)
4168 (setq buf-window (selected-window))
4169 (goto-char (point-max))
4170 (insert (apply 'edebug-format fmt args) "\n")
4172 (vertical-motion (- 1 (window-height)))
4173 (set-window-start buf-window (point))
4174 (goto-char (point-max))
4175 ;; (set-window-point buf-window (point))
4176 ;; (edebug-sit-for 0)
4177 (bury-buffer buffer)
4178 (select-window selected-window))
4182 (defun edebug-trace (fmt &rest args)
4183 "Convenience call to edebug-trace-display using edebug-trace-buffer"
4184 (apply 'edebug-trace-display edebug-trace-buffer fmt args))
4187 ;;;; Frequency count and coverage
4188 ;;; ==============================
4190 (defun edebug-display-freq-count ()
4191 "Display the frequency count data for each line of the current
4192 definition. The frequency counts are inserted as comment lines after
4193 each line, and you can undo all insertions with one `undo' command.
4195 The counts are inserted starting under the `(' before an expression
4196 or the `)' after an expression, or on the last char of a symbol.
4197 The counts are only displayed when they differ from previous counts on
4200 If coverage is being tested, whenever all known results of an expression
4201 are `eq', the char `=' will be appended after the count
4202 for that expression. Note that this is always the case for an
4203 expression only evaluated once.
4205 To clear the frequency count and coverage data for a definition,
4208 (let* ((function (edebug-form-data-symbol))
4209 (counts (get function 'edebug-freq-count))
4210 (coverages (get function 'edebug-coverage))
4211 (data (get function 'edebug))
4212 (def-mark (car data)) ; mark at def start
4213 (edebug-points (nth 2 data))
4214 (i (1- (length edebug-points)))
4218 (start-of-count-line)
4222 ;; Traverse in reverse order so offsets are correct.
4224 ;; Start at last expression in line.
4225 (goto-char (+ def-mark (aref edebug-points i)))
4227 (setq start-of-line (- (point) def-mark)
4230 ;; Find all indexes on same line.
4231 (while (and (<= 0 (setq i (1- i)))
4232 (<= start-of-line (aref edebug-points i))))
4233 ;; Insert all the indices for this line.
4235 (setq start-of-count-line (point)
4236 first-index i ; really last index for line above this one.
4237 last-count -1) ; cause first count to always appear.
4239 ;; i == first-index still
4240 (while (<= (setq i (1+ i)) last-index)
4241 (let ((count (aref counts i))
4242 (coverage (aref coverages i))
4243 (col (save-excursion
4244 (goto-char (+ (aref edebug-points i) def-mark))
4246 (if (= ?\( (following-char)) 0 1)))))
4247 (insert (make-string
4248 (max 0 (- col (- (point) start-of-count-line))) ?\ )
4249 (if (and (< 0 count)
4251 '(unknown ok-coverage))))
4253 (if (= count last-count) "" (int-to-string count))
4255 (setq last-count count)))
4257 (setq i first-index)))))
4259 (defun edebug-temp-display-freq-count ()
4260 "Temporarily display the frequency count data for the current definition.
4261 It is removed when you hit any char."
4262 ;; This seems not to work with Emacs 18.59. It undoes too far.
4264 (let ((buffer-read-only nil))
4266 (edebug-display-freq-count)
4267 (setq unread-command-char (read-char))
4274 (defun edebug-toggle (variable)
4275 (set variable (not (eval variable)))
4276 (message "%s: %s" variable (eval variable)))
4278 ;; We have to require easymenu (even for Emacs 18) just so
4279 ;; the easy-menu-define macro call is compiled correctly.
4282 (defconst edebug-mode-menus
4285 ["Stop" edebug-stop t]
4286 ["Step" edebug-step-mode t]
4287 ["Next" edebug-next-mode t]
4288 ["Trace" edebug-trace-mode t]
4289 ["Trace Fast" edebug-Trace-fast-mode t]
4290 ["Continue" edebug-continue-mode t]
4291 ["Continue Fast" edebug-Continue-fast-mode t]
4292 ["Go" edebug-go-mode t]
4293 ["Go Nonstop" edebug-Go-nonstop-mode t]
4295 ["Help" edebug-help t]
4296 ["Abort" abort-recursive-edit t]
4297 ["Quit to Top Level" top-level t]
4298 ["Quit Nonstop" edebug-top-level-nonstop t]
4301 ["Forward Sexp" edebug-forward-sexp t]
4302 ["Step In" edebug-step-in t]
4303 ["Step Out" edebug-step-out t]
4304 ["Goto Here" edebug-goto-here t])
4307 ["Set Breakpoint" edebug-set-breakpoint t]
4308 ["Unset Breakpoint" edebug-unset-breakpoint t]
4309 ["Set Conditional Breakpoint" edebug-set-conditional-breakpoint t]
4310 ["Set Global Break Condition" edebug-set-global-break-condition t]
4311 ["Show Next Breakpoint" edebug-next-breakpoint t])
4314 ["Where am I?" edebug-where t]
4315 ["Bounce to Current Point" edebug-bounce-point t]
4316 ["View Outside Windows" edebug-view-outside t]
4317 ["Previous Result" edebug-previous-result t]
4318 ["Show Backtrace" edebug-backtrace t]
4319 ["Display Freq Count" edebug-display-freq-count t])
4322 ["Expression" edebug-eval-expression t]
4323 ["Last Sexp" edebug-eval-last-sexp t]
4324 ["Visit Eval List" edebug-visit-eval-list t])
4327 ["Edebug All Defs" edebug-all-defs t]
4328 ["Edebug All Forms" edebug-all-forms t]
4330 ["Toggle Tracing" (edebug-toggle 'edebug-trace) t]
4331 ["Toggle Coverage Testing" (edebug-toggle 'edebug-test-coverage) t]
4332 ["Toggle Window Saving" edebug-toggle-save-windows t]
4333 ["Toggle Point Saving"
4334 (edebug-toggle 'edebug-save-displayed-buffer-points) t]
4336 "Lemacs style menus for Edebug.")
4339 ;;;; Emacs version specific code
4340 ;;;=============================
4341 ;;; The default for all above is Emacs 18, because it is easier to compile
4342 ;;; Emacs 18 code in Emacs 19 than vice versa. This default will
4343 ;;; change once most people are using Emacs 19 or derivatives.
4345 ;; Epoch specific code is in a separate file: edebug-epoch.el.
4347 ;; The byte-compiler will complain about changes in number of arguments
4348 ;; to functions like mark and read-from-minibuffer. These warnings
4349 ;; may be ignored because the right call should always be made.
4351 (defun edebug-emacs-19-specific ()
4353 (defalias 'edebug-window-live-p 'window-live-p)
4355 ;; Mark takes an argument in Emacs 19.
4356 (defun edebug-mark ()
4357 (mark t));; Does this work for lemacs too?
4359 ;; Use minibuffer-history when reading expressions.
4360 (defvar read-expression-history) ;; hush bytecomp
4361 (defvar read-expression-map)
4363 (defun edebug-set-conditional-breakpoint (arg condition)
4364 "Set a conditional breakpoint at nearest sexp.
4365 The condition is evaluated in the outside context.
4366 With prefix argument, make it a temporary breakpoint."
4367 ;; (interactive "P\nxCondition: ")
4371 ;; Read condition as follows; getting previous condition is cumbersome:
4372 (let ((edebug-stop-point (edebug-find-stop-point)))
4373 (if edebug-stop-point
4374 (let* ((edebug-def-name (car edebug-stop-point))
4375 (index (cdr edebug-stop-point))
4376 (edebug-data (get edebug-def-name 'edebug))
4377 (edebug-breakpoints (car (cdr edebug-data)))
4378 (edebug-break-data (assq index edebug-breakpoints))
4379 (edebug-break-condition (car (cdr edebug-break-data)))
4380 (edebug-expression-history
4381 ;; Prepend the current condition, if any.
4382 (if edebug-break-condition
4383 (cons edebug-break-condition read-expression-history)
4384 read-expression-history)))
4386 (read-from-minibuffer
4387 "Condition: " nil read-expression-map t
4388 'edebug-expression-history)
4389 (setq read-expression-history edebug-expression-history)
4391 (edebug-modify-breakpoint t condition arg))
4393 (defun edebug-eval-expression (edebug-expr)
4394 "Evaluate an expression in the outside environment.
4395 If interactive, prompt for the expression.
4396 Print result in minibuffer."
4397 (interactive (list (read-from-minibuffer
4398 "Eval: " nil read-expression-map t
4399 'read-expression-history)))
4401 (edebug-outside-excursion
4402 (setq values (cons (edebug-eval edebug-expr) values))
4403 (edebug-safe-prin1-to-string (car values)))))
4405 (easy-menu-define 'edebug edebug-mode-map "Edebug menus" edebug-mode-menus)
4409 (defun edebug-lemacs-specific ()
4411 ;; We need to bind zmacs-regions to nil around all calls to `mark' and
4412 ;; `mark-marker' but don't bind it to nil before entering a recursive edit,
4413 ;; that is, don't interfere with the binding the user might see while
4414 ;; executing a command.
4416 (defvar zmacs-regions)
4418 (defun edebug-mark ()
4419 (let ((zmacs-regions nil))
4422 (defun edebug-mark-marker ()
4423 (let ((zmacs-regions nil));; for lemacs
4427 (defun edebug-mode-menu (event)
4428 (interactive "@event")
4429 (popup-menu edebug-mode-menus))
4431 (define-key edebug-mode-map 'button3 'edebug-mode-menu)
4434 (defun edebug-emacs-version-specific ()
4436 ((string-match "Lucid" emacs-version);; Lucid Emacs
4437 (edebug-lemacs-specific))
4439 ((and (boundp 'epoch::version) epoch::version)
4440 (require 'edebug-epoch))
4442 ((not (string-match "^18" emacs-version))
4443 (edebug-emacs-19-specific))))
4445 (edebug-emacs-version-specific)
4449 ;;; ====================
4450 ;; Extension for bytecomp to resolve undefined function references.
4451 ;; Requires new byte compiler.
4453 ;; Reenable byte compiler warnings about unread-command-char and -event.
4454 ;; Disabled before edebug-recursive-edit.
4456 (if edebug-unread-command-char-warning
4457 (put 'unread-command-char 'byte-obsolete-variable
4458 edebug-unread-command-char-warning))
4459 (if edebug-unread-command-event-warning
4460 (put 'unread-command-event 'byte-obsolete-variable
4461 edebug-unread-command-event-warning)))
4464 ;; The body of eval-when-compile seems to get evaluated with eval-defun.
4465 ;; We only want to evaluate when actually byte compiling.
4466 ;; But it is OK to evaluate as long as byte-compiler has been loaded.
4467 (if (featurep 'byte-compile) (progn
4469 (defun byte-compile-resolve-functions (funcs)
4470 "Say it is OK for the named functions to be unresolved."
4474 (setq byte-compile-unresolved-functions
4475 (delq (assq func byte-compile-unresolved-functions)
4476 byte-compile-unresolved-functions))))
4480 '(defun byte-compile-resolve-free-references (vars)
4481 "Say it is OK for the named variables to be referenced."
4485 (setq byte-compile-free-references
4486 (delq var byte-compile-free-references))))
4490 '(defun byte-compile-resolve-free-assignments (vars)
4491 "Say it is OK for the named variables to be assigned."
4495 (setq byte-compile-free-assignments
4496 (delq var byte-compile-free-assignments))))
4500 (byte-compile-resolve-functions
4501 '(reporter-submit-bug-report
4502 gensym ;; also in cl.el
4503 ;; Interfaces to standard functions.
4504 edebug-original-eval-defun
4505 edebug-original-read
4506 edebug-get-buffer-window
4509 edebug-input-pending-p
4511 edebug-prin1-to-string
4513 edebug-original-signal
4515 zmacs-deactivate-region
4519 ;; And believe it or not, the byte compiler doesnt know about:
4520 byte-compile-resolve-functions
4523 '(byte-compile-resolve-free-references
4524 '(read-expression-history
4525 read-expression-map))
4527 '(byte-compile-resolve-free-assignments
4528 '(read-expression-history))
4533 ;;;; Autoloading of Edebug accessories
4534 ;;;===================================
4537 (add-hook 'edebug-setup-hook
4538 (function (lambda () (require 'cl-specs))))
4539 ;; The following causes cl-specs to be loaded if you load cl.el.
4540 (add-hook 'cl-load-hook
4541 (function (lambda () (require 'cl-specs)))))
4543 (if (featurep 'cl-read)
4544 (add-hook 'edebug-setup-hook
4545 (function (lambda () (require 'edebug-cl-read))))
4546 ;; The following causes edebug-cl-read to be loaded when you load cl-read.el.
4547 (add-hook 'cl-read-load-hooks
4548 (function (lambda () (require 'edebug-cl-read)))))
4551 ;;;; Finalize Loading
4552 ;;;===================
4554 ;;; Finally, hook edebug into the rest of Emacs.
4555 ;;; There are probably some other things that could go here.
4557 ;; Install edebug read and eval functions.
4558 (edebug-install-read-eval-functions)
4562 ;;; edebug.el ends here