Update define-ibuffer-op doc string
[emacs.git] / lisp / emacs-lisp / edebug.el
blobc283c168b5e18f39e90f317b029735c88c655ea1
1 ;;; edebug.el --- a source-level debugger for Emacs Lisp -*- lexical-binding: t -*-
3 ;; Copyright (C) 1988-1995, 1997, 1999-2016 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Daniel LaLiberte <liberte@holonexus.org>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: lisp, tools, maint
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This minor mode allows programmers to step through Emacs Lisp
28 ;; source code while executing functions. You can also set
29 ;; breakpoints, trace (stopping at each expression), evaluate
30 ;; expressions as if outside Edebug, reevaluate and display a list of
31 ;; expressions, trap errors normally caught by debug, and display a
32 ;; debug style backtrace.
34 ;;; Minimal Instructions
35 ;; =====================
37 ;; First evaluate a defun with C-M-x, then run the function. Step
38 ;; through the code with SPC, mark breakpoints with b, go until a
39 ;; breakpoint is reached with g, and quit execution with q. Use the
40 ;; "?" command in edebug to describe other commands.
41 ;; See the Emacs Lisp Reference Manual for more details.
43 ;; If you wish to change the default edebug global command prefix, change:
44 ;; (setq edebug-global-prefix "\C-xX")
46 ;; Edebug was written by
47 ;; Daniel LaLiberte
48 ;; GTE Labs
49 ;; 40 Sylvan Rd
50 ;; Waltham, MA 02254
51 ;; liberte@holonexus.org
53 ;;; Code:
55 (require 'macroexp)
56 (require 'cl-lib)
57 (eval-when-compile (require 'pcase))
59 ;;; Options
61 (defgroup edebug nil
62 "A source-level debugger for Emacs Lisp."
63 :group 'lisp)
66 (defcustom edebug-setup-hook nil
67 "Functions to call before edebug is used.
68 Each time it is set to a new value, Edebug will call those functions
69 once and then reset `edebug-setup-hook' to nil. You could use this
70 to load up Edebug specifications associated with a package you are
71 using, but only when you also use Edebug."
72 :type 'hook
73 :group 'edebug)
75 ;; edebug-all-defs and edebug-all-forms need to be autoloaded
76 ;; because the byte compiler binds them; as a result, if edebug
77 ;; is first loaded for a require in a compilation, they will be left unbound.
79 ;;;###autoload
80 (defcustom edebug-all-defs nil
81 "If non-nil, evaluating defining forms instruments for Edebug.
82 This applies to `eval-defun', `eval-region', `eval-buffer', and
83 `eval-current-buffer'. `eval-region' is also called by
84 `eval-last-sexp', and `eval-print-last-sexp'.
86 You can use the command `edebug-all-defs' to toggle the value of this
87 variable. You may wish to make it local to each buffer with
88 \(make-local-variable \\='edebug-all-defs) in your
89 `emacs-lisp-mode-hook'."
90 :type 'boolean
91 :group 'edebug)
93 ;; edebug-all-defs and edebug-all-forms need to be autoloaded
94 ;; because the byte compiler binds them; as a result, if edebug
95 ;; is first loaded for a require in a compilation, they will be left unbound.
97 ;;;###autoload
98 (defcustom edebug-all-forms nil
99 "Non-nil means evaluation of all forms will instrument for Edebug.
100 This doesn't apply to loading or evaluations in the minibuffer.
101 Use the command `edebug-all-forms' to toggle the value of this option."
102 :type 'boolean
103 :group 'edebug)
105 (defcustom edebug-eval-macro-args nil
106 "Non-nil means all macro call arguments may be evaluated.
107 If this variable is nil, the default, Edebug will *not* wrap
108 macro call arguments as if they will be evaluated.
109 For each macro, an `edebug-form-spec' overrides this option.
110 So to specify exceptions for macros that have some arguments evaluated
111 and some not, use `def-edebug-spec' to specify an `edebug-form-spec'."
112 :type 'boolean
113 :group 'edebug)
115 (defcustom edebug-save-windows t
116 "If non-nil, Edebug saves and restores the window configuration.
117 That takes some time, so if your program does not care what happens to
118 the window configurations, it is better to set this variable to nil.
120 If the value is a list, only the listed windows are saved and
121 restored.
123 `edebug-toggle-save-windows' may be used to change this variable."
124 :type '(choice boolean (repeat string))
125 :group 'edebug)
127 (defcustom edebug-save-displayed-buffer-points nil
128 "If non-nil, save and restore point in all displayed buffers.
130 Saving and restoring point in other buffers is necessary if you are
131 debugging code that changes the point of a buffer that is displayed
132 in a non-selected window. If Edebug or the user then selects the
133 window, the buffer's point will be changed to the window's point.
135 Saving and restoring point in all buffers is expensive, since it
136 requires selecting each window twice, so enable this only if you
137 need it."
138 :type 'boolean
139 :group 'edebug)
141 (defcustom edebug-initial-mode 'step
142 "Initial execution mode for Edebug, if non-nil.
143 If this variable is non-nil, it specifies the initial execution mode
144 for Edebug when it is first activated. Possible values are step, next,
145 go, Go-nonstop, trace, Trace-fast, continue, and Continue-fast."
146 :type '(choice (const step) (const next) (const go)
147 (const Go-nonstop) (const trace)
148 (const Trace-fast) (const continue)
149 (const Continue-fast))
150 :group 'edebug)
152 (defcustom edebug-trace nil
153 "Non-nil means display a trace of function entry and exit.
154 Tracing output is displayed in a buffer named `*edebug-trace*', one
155 function entry or exit per line, indented by the recursion level.
157 You can customize by replacing functions `edebug-print-trace-before'
158 and `edebug-print-trace-after'."
159 :type 'boolean
160 :group 'edebug)
162 (defcustom edebug-test-coverage nil
163 "If non-nil, Edebug tests coverage of all expressions debugged.
164 This is done by comparing the result of each expression with the
165 previous result. Coverage is considered OK if two different
166 results are found.
168 Use `edebug-display-freq-count' to display the frequency count and
169 coverage information for a definition."
170 :type 'boolean
171 :group 'edebug)
173 (defcustom edebug-continue-kbd-macro nil
174 "If non-nil, continue defining or executing any keyboard macro.
175 Use this with caution since it is not debugged."
176 :type 'boolean
177 :group 'edebug)
180 (defcustom edebug-print-length 50
181 "If non-nil, default value of `print-length' for printing results in Edebug."
182 :type 'integer
183 :group 'edebug)
184 (defcustom edebug-print-level 50
185 "If non-nil, default value of `print-level' for printing results in Edebug."
186 :type 'integer
187 :group 'edebug)
188 (defcustom edebug-print-circle t
189 "If non-nil, default value of `print-circle' for printing results in Edebug."
190 :type 'boolean
191 :group 'edebug)
193 (defcustom edebug-unwrap-results nil
194 "Non-nil if Edebug should unwrap results of expressions.
195 That is, Edebug will try to remove its own instrumentation from the result.
196 This is useful when debugging macros where the results of expressions
197 are instrumented expressions. But don't do this when results might be
198 circular or an infinite loop will result."
199 :type 'boolean
200 :group 'edebug)
202 (defcustom edebug-on-error t
203 "Value bound to `debug-on-error' while Edebug is active.
205 If `debug-on-error' is non-nil, that value is still used.
207 If the value is a list of signal names, Edebug will stop when any of
208 these errors are signaled from Lisp code whether or not the signal is
209 handled by a `condition-case'. This option is useful for debugging
210 signals that *are* handled since they would otherwise be missed.
211 After execution is resumed, the error is signaled again."
212 :type '(choice (const :tag "off")
213 (repeat :menu-tag "When"
214 :value (nil)
215 (symbol :format "%v"))
216 (const :tag "always" t))
217 :group 'edebug)
219 (defcustom edebug-on-quit t
220 "Value bound to `debug-on-quit' while Edebug is active."
221 :type 'boolean
222 :group 'edebug)
224 (defcustom edebug-global-break-condition nil
225 "If non-nil, an expression to test for at every stop point.
226 If the result is non-nil, then break. Errors are ignored."
227 :type 'sexp
228 :risky t
229 :group 'edebug)
231 (defcustom edebug-sit-for-seconds 1
232 "Number of seconds to pause when execution mode is `trace' or `continue'."
233 :type 'number
234 :group 'edebug)
236 (defcustom edebug-sit-on-break t
237 "Whether or not to pause for `edebug-sit-for-seconds' on reaching a break."
238 :type 'boolean
239 :group 'edebug
240 :version "25.2")
242 ;;; Form spec utilities.
244 (defun get-edebug-spec (symbol)
245 ;; Get the spec of symbol resolving all indirection.
246 (let ((spec nil)
247 (indirect symbol))
248 (while
249 (progn
250 (and (symbolp indirect)
251 (setq indirect
252 (function-get indirect 'edebug-form-spec 'macro))))
253 ;; (edebug-trace "indirection: %s" edebug-form-spec)
254 (setq spec indirect))
255 spec))
257 ;;;###autoload
258 (defun edebug-basic-spec (spec)
259 "Return t if SPEC uses only extant spec symbols.
260 An extant spec symbol is a symbol that is not a function and has a
261 `edebug-form-spec' property."
262 (cond ((listp spec)
263 (catch 'basic
264 (while spec
265 (unless (edebug-basic-spec (car spec)) (throw 'basic nil))
266 (setq spec (cdr spec)))
268 ((symbolp spec)
269 (unless (functionp spec) (function-get spec 'edebug-form-spec)))))
271 ;;; Utilities
273 (defun edebug-lambda-list-keywordp (object)
274 "Return t if OBJECT is a lambda list keyword.
275 A lambda list keyword is a symbol that starts with `&'."
276 (and (symbolp object)
277 (= ?& (aref (symbol-name object) 0))))
280 (defun edebug-last-sexp ()
281 ;; Return the last sexp before point in current buffer.
282 ;; Assumes Emacs Lisp syntax is active.
283 (car
284 (read-from-string
285 (buffer-substring
286 (save-excursion
287 (forward-sexp -1)
288 (point))
289 (point)))))
291 (defun edebug-window-list ()
292 "Return a list of windows, in order of `next-window'."
293 ;; This doesn't work for epoch.
294 (let (window-list)
295 (walk-windows (lambda (w) (push w window-list)))
296 (nreverse window-list)))
298 ;; Not used.
299 '(defun edebug-two-window-p ()
300 "Return t if there are two windows."
301 (and (not (one-window-p))
302 (eq (selected-window)
303 (next-window (next-window)))))
305 (defun edebug-sort-alist (alist function)
306 ;; Return the ALIST sorted with comparison function FUNCTION.
307 ;; This uses 'sort so the sorting is destructive.
308 (sort alist (function
309 (lambda (e1 e2)
310 (funcall function (car e1) (car e2))))))
312 ;; Not used.
313 '(defmacro edebug-save-restriction (&rest body)
314 "Evaluate BODY while saving the current buffers restriction.
315 BODY may change buffer outside of current restriction, unlike
316 save-restriction. BODY may change the current buffer,
317 and the restriction will be restored to the original buffer,
318 and the current buffer remains current.
319 Return the result of the last expression in BODY."
320 (declare (debug t))
321 `(let ((edebug:s-r-beg (point-min-marker))
322 (edebug:s-r-end (point-max-marker)))
323 (unwind-protect
324 (progn ,@body)
325 (with-current-buffer (marker-buffer edebug:s-r-beg)
326 (narrow-to-region edebug:s-r-beg edebug:s-r-end)))))
328 ;;; Display
330 (defconst edebug-trace-buffer "*edebug-trace*"
331 "Name of the buffer to put trace info in.")
333 (defun edebug-pop-to-buffer (buffer &optional window)
334 ;; Like pop-to-buffer, but select window where BUFFER was last shown.
335 ;; Select WINDOW if it is provided and still exists. Otherwise,
336 ;; if buffer is currently shown in several windows, choose one.
337 ;; Otherwise, find a new window, possibly splitting one.
338 ;; FIXME: We should probably just be using `pop-to-buffer'.
339 (setq window
340 (cond
341 ((and (edebug-window-live-p window)
342 (eq (window-buffer window) buffer))
343 window)
344 ((eq (window-buffer) buffer)
345 ;; Selected window already displays BUFFER.
346 (selected-window))
347 ((get-buffer-window buffer 0))
348 ((one-window-p 'nomini)
349 ;; When there's one window only, split it.
350 (split-window (minibuffer-selected-window)))
351 ((let ((trace-window (get-buffer-window edebug-trace-buffer)))
352 (catch 'found
353 (dolist (elt (window-list nil 'nomini))
354 (unless (or (eq elt (selected-window)) (eq elt trace-window)
355 (window-dedicated-p elt))
356 ;; Found a non-dedicated window not showing
357 ;; `edebug-trace-buffer', use it.
358 (throw 'found elt))))))
359 ;; All windows are dedicated or show `edebug-trace-buffer', split
360 ;; selected one.
361 (t (split-window (minibuffer-selected-window)))))
362 (set-window-buffer window buffer)
363 (select-window window)
364 (set-window-hscroll window 0)) ;; should this be??
366 (defun edebug-get-displayed-buffer-points ()
367 ;; Return a list of buffer point pairs, for all displayed buffers.
368 (let (list)
369 (walk-windows (lambda (w)
370 (unless (eq w (selected-window))
371 (push (cons (window-buffer w)
372 (window-point w))
373 list))))
374 list))
377 (defun edebug-set-buffer-points (buffer-points)
378 ;; Restore the buffer-points created by edebug-get-displayed-buffer-points.
379 (save-current-buffer
380 (mapcar (lambda (buf-point)
381 (when (buffer-live-p (car buf-point))
382 (set-buffer (car buf-point))
383 (goto-char (cdr buf-point))))
384 buffer-points)))
386 (defun edebug-current-windows (which-windows)
387 ;; Get either a full window configuration or some window information.
388 (if (listp which-windows)
389 (mapcar (function (lambda (window)
390 (if (edebug-window-live-p window)
391 (list window
392 (window-buffer window)
393 (window-point window)
394 (window-start window)
395 (window-hscroll window)))))
396 which-windows)
397 (current-window-configuration)))
399 (defun edebug-set-windows (window-info)
400 ;; Set either a full window configuration or some window information.
401 (if (listp window-info)
402 (mapcar (function
403 (lambda (one-window-info)
404 (if one-window-info
405 (apply (function
406 (lambda (window buffer point start hscroll)
407 (if (edebug-window-live-p window)
408 (progn
409 (set-window-buffer window buffer)
410 (set-window-point window point)
411 (set-window-start window start)
412 (set-window-hscroll window hscroll)))))
413 one-window-info))))
414 window-info)
415 (set-window-configuration window-info)))
417 ;;; Redefine read and eval functions
418 ;; read is redefined to maybe instrument forms.
419 ;; eval-defun is redefined to check edebug-all-forms and edebug-all-defs.
421 (defun edebug--read (orig &optional stream)
422 "Read one Lisp expression as text from STREAM, return as Lisp object.
423 If STREAM is nil, use the value of `standard-input' (which see).
424 STREAM or the value of `standard-input' may be:
425 a buffer (read from point and advance it)
426 a marker (read from where it points and advance it)
427 a function (call it with no arguments for each character,
428 call it with a char as argument to push a char back)
429 a string (takes text from string, starting at the beginning)
430 t (read text line using minibuffer and use it).
432 This version, from Edebug, maybe instruments the expression. But the
433 STREAM must be the current buffer to do so. Whether it instruments is
434 also dependent on the values of the option `edebug-all-defs' and
435 the option `edebug-all-forms'."
436 (or stream (setq stream standard-input))
437 (if (eq stream (current-buffer))
438 (edebug-read-and-maybe-wrap-form)
439 (funcall (or orig #'read) stream)))
441 (defvar edebug-result) ; The result of the function call returned by body.
443 ;; We should somehow arrange to be able to do this
444 ;; without actually replacing the eval-defun command.
445 (defun edebug-eval-defun (edebug-it)
446 "Evaluate the top-level form containing point, or after point.
448 If the current defun is actually a call to `defvar', then reset the
449 variable using its initial value expression even if the variable
450 already has some other value. (Normally `defvar' does not change the
451 variable's value if it already has a value.) Treat `defcustom'
452 similarly. Reinitialize the face according to `defface' specification.
454 With a prefix argument, instrument the code for Edebug.
456 Setting option `edebug-all-defs' to a non-nil value reverses the meaning
457 of the prefix argument. Code is then instrumented when this function is
458 invoked without a prefix argument.
460 If acting on a `defun' for FUNCTION, and the function was instrumented,
461 `Edebug: FUNCTION' is printed in the minibuffer. If not instrumented,
462 just FUNCTION is printed.
464 If not acting on a `defun', the result of evaluation is displayed in
465 the minibuffer."
466 (interactive "P")
467 (let* ((edebugging (not (eq (not edebug-it) (not edebug-all-defs))))
468 (edebug-result)
469 (form
470 (let ((edebug-all-forms edebugging)
471 (edebug-all-defs (eq edebug-all-defs (not edebug-it))))
472 (edebug-read-top-level-form))))
473 ;; This should be consistent with `eval-defun-1', but not the
474 ;; same, since that gets a macroexpanded form.
475 (cond ((and (eq (car form) 'defvar)
476 (cdr-safe (cdr-safe form)))
477 ;; Force variable to be bound.
478 (makunbound (nth 1 form)))
479 ((and (eq (car form) 'defcustom)
480 (default-boundp (nth 1 form)))
481 ;; Force variable to be bound.
482 ;; FIXME: Shouldn't this use the :setter or :initializer?
483 (set-default (nth 1 form) (eval (nth 2 form) lexical-binding)))
484 ((eq (car form) 'defface)
485 ;; Reset the face.
486 (setq face-new-frame-defaults
487 (assq-delete-all (nth 1 form) face-new-frame-defaults))
488 (put (nth 1 form) 'face-defface-spec nil)
489 (put (nth 1 form) 'face-documentation (nth 3 form))
490 ;; See comments in `eval-defun-1' for purpose of code below
491 (setq form (prog1 `(prog1 ,form
492 (put ',(nth 1 form) 'saved-face
493 ',(get (nth 1 form) 'saved-face))
494 (put ',(nth 1 form) 'customized-face
495 ,(nth 2 form)))
496 (put (nth 1 form) 'saved-face nil)))))
497 (setq edebug-result (eval (eval-sexp-add-defvars form) lexical-binding))
498 (if (not edebugging)
499 (prog1
500 (prin1 edebug-result)
501 (let ((str (eval-expression-print-format edebug-result)))
502 (if str (princ str))))
503 edebug-result)))
506 ;;;###autoload
507 (defalias 'edebug-defun 'edebug-eval-top-level-form)
509 ;;;###autoload
510 (defun edebug-eval-top-level-form ()
511 "Evaluate the top level form point is in, stepping through with Edebug.
512 This is like `eval-defun' except that it steps the code for Edebug
513 before evaluating it. It displays the value in the echo area
514 using `eval-expression' (which see).
516 If you do this on a function definition such as a defun or defmacro,
517 it defines the function and instruments its definition for Edebug,
518 so it will do Edebug stepping when called later. It displays
519 `Edebug: FUNCTION' in the echo area to indicate that FUNCTION is now
520 instrumented for Edebug.
522 If the current defun is actually a call to `defvar' or `defcustom',
523 evaluating it this way resets the variable using its initial value
524 expression even if the variable already has some other value.
525 \(Normally `defvar' and `defcustom' do not alter the value if there
526 already is one.)"
527 (interactive)
528 (eval-expression
529 ;; Bind edebug-all-forms only while reading, not while evalling
530 ;; but this causes problems while edebugging edebug.
531 (let ((edebug-all-forms t)
532 (edebug-all-defs t))
533 (eval-sexp-add-defvars
534 (edebug-read-top-level-form)))))
537 (defun edebug-read-top-level-form ()
538 (let ((starting-point (point)))
539 (end-of-defun)
540 (beginning-of-defun)
541 (prog1
542 (edebug-read-and-maybe-wrap-form)
543 ;; Recover point, but only if no error occurred.
544 (goto-char starting-point))))
547 ;; Compatibility with old versions.
548 (defalias 'edebug-all-defuns 'edebug-all-defs)
550 ;;;###autoload
551 (defun edebug-all-defs ()
552 "Toggle edebugging of all definitions."
553 (interactive)
554 (setq edebug-all-defs (not edebug-all-defs))
555 (message "Edebugging all definitions is %s."
556 (if edebug-all-defs "on" "off")))
559 ;;;###autoload
560 (defun edebug-all-forms ()
561 "Toggle edebugging of all forms."
562 (interactive)
563 (setq edebug-all-forms (not edebug-all-forms))
564 (message "Edebugging all forms is %s."
565 (if edebug-all-forms "on" "off")))
568 (defun edebug-install-read-eval-functions ()
569 (interactive)
570 (add-function :around load-read-function #'edebug--read)
571 (advice-add 'eval-defun :override #'edebug-eval-defun))
573 (defun edebug-uninstall-read-eval-functions ()
574 (interactive)
575 (remove-function load-read-function #'edebug--read)
576 (advice-remove 'eval-defun 'edebug-eval-defun))
578 ;;; Edebug internal data
580 ;; The internal data that is needed for edebugging is kept in the
581 ;; buffer-local variable `edebug-form-data'.
583 (defvar-local edebug-form-data nil
584 "A list of entries associating symbols with buffer regions.
585 Each entry is an `edebug--form-data' struct with fields:
586 SYMBOL, BEGIN-MARKER, and END-MARKER. The markers
587 are at the beginning and end of an entry level form and SYMBOL is
588 a symbol that holds all edebug related information for the form on its
589 property list.
591 In the future (haha!), the symbol will be irrelevant and edebug data will
592 be stored in the definitions themselves rather than in the property
593 list of a symbol.")
595 (cl-defstruct (edebug--form-data
596 ;; Some callers expect accessors to return nil when passed nil.
597 (:type list)
598 (:constructor edebug--make-form-data-entry (name begin end))
599 (:predicate nil) (:constructor nil) (:copier nil))
600 name begin end)
602 (defsubst edebug-set-form-data-entry (entry name begin end)
603 (setf (edebug--form-data-name entry) name) ;; In case name is changed.
604 (set-marker (edebug--form-data-begin entry) begin)
605 (set-marker (edebug--form-data-end entry) end))
607 (defun edebug-get-form-data-entry (pnt &optional end-point)
608 ;; Find the edebug form data entry which is closest to PNT.
609 ;; If END-POINT is supplied, match must be exact.
610 ;; Return nil if none found.
611 (let ((rest edebug-form-data)
612 closest-entry
613 (closest-dist 999999)) ;; Need maxint here.
614 (while (and rest (< 0 closest-dist))
615 (let* ((entry (car rest))
616 (begin (edebug--form-data-begin entry))
617 (dist (- pnt begin)))
618 (setq rest (cdr rest))
619 (if (and (<= 0 dist)
620 (< dist closest-dist)
621 (or (not end-point)
622 (= end-point (edebug--form-data-end entry)))
623 (<= pnt (edebug--form-data-end entry)))
624 (setq closest-dist dist
625 closest-entry entry))))
626 closest-entry))
628 ;; Also need to find all contained entries,
629 ;; and find an entry given a symbol, which should be just assq.
631 (defun edebug-form-data-symbol ()
632 "Return the edebug data symbol of the form where point is in.
633 If point is not inside a edebuggable form, cause error."
634 (or (edebug--form-data-name (edebug-get-form-data-entry (point)))
635 (error "Not inside instrumented form")))
637 (defun edebug-make-top-form-data-entry (new-entry)
638 ;; Make NEW-ENTRY the first element in the `edebug-form-data' list.
639 (edebug-clear-form-data-entry new-entry)
640 (push new-entry edebug-form-data))
642 (defun edebug-clear-form-data-entry (entry)
643 "If non-nil, clear ENTRY out of the form data.
644 Maybe clear the markers and delete the symbol's edebug property?"
645 (if entry
646 (progn
647 ;; Instead of this, we could just find all contained forms.
648 ;; (put (car entry) 'edebug nil) ;
649 ;; (mapcar 'edebug-clear-form-data-entry ; dangerous
650 ;; (get (car entry) 'edebug-dependents))
651 ;; (set-marker (nth 1 entry) nil)
652 ;; (set-marker (nth 2 entry) nil)
653 (setq edebug-form-data (delq entry edebug-form-data)))))
655 ;;; Parser utilities
657 (defun edebug-syntax-error (&rest args)
658 ;; Signal an invalid-read-syntax with ARGS.
659 (signal 'invalid-read-syntax args))
662 (defconst edebug-read-syntax-table
663 ;; Lookup table for significant characters indicating the class of the
664 ;; token that follows. This is not a \"real\" syntax table.
665 (let ((table (make-char-table 'syntax-table 'symbol))
666 (i 0))
667 (while (< i ?!)
668 (aset table i 'space)
669 (setq i (1+ i)))
670 (aset table ?\( 'lparen)
671 (aset table ?\) 'rparen)
672 (aset table ?\' 'quote)
673 (aset table ?\` 'backquote)
674 (aset table ?\, 'comma)
675 (aset table ?\" 'string)
676 (aset table ?\? 'char)
677 (aset table ?\[ 'lbracket)
678 (aset table ?\] 'rbracket)
679 (aset table ?\. 'dot)
680 (aset table ?\# 'hash)
681 ;; We treat numbers as symbols, because of confusion with -, -1, and 1-.
682 ;; We don't care about any other chars since they won't be seen.
683 table))
685 (defun edebug-next-token-class ()
686 ;; Move to the next token and return its class. We only care about
687 ;; lparen, rparen, dot, quote, backquote, comma, string, char, vector,
688 ;; or symbol.
689 (edebug-skip-whitespace)
690 (if (and (eq (following-char) ?.)
691 (save-excursion
692 (forward-char 1)
693 (or (and (eq (aref edebug-read-syntax-table (following-char))
694 'symbol)
695 (not (= (following-char) ?\;)))
696 (memq (following-char) '(?\, ?\.)))))
697 'symbol
698 (aref edebug-read-syntax-table (following-char))))
701 (defun edebug-skip-whitespace ()
702 ;; Leave point before the next token, skipping white space and comments.
703 (skip-chars-forward " \t\r\n\f")
704 (while (= (following-char) ?\;)
705 (skip-chars-forward "^\n") ; skip the comment
706 (skip-chars-forward " \t\r\n\f")))
709 ;; Mostly obsolete reader; still used in one case.
711 (defun edebug-read-sexp ()
712 ;; Read one sexp from the current buffer starting at point.
713 ;; Leave point immediately after it. A sexp can be a list or atom.
714 ;; An atom is a symbol (or number), character, string, or vector.
715 ;; This works for reading anything legitimate, but it
716 ;; is gummed up by parser inconsistencies (bugs?)
717 (let ((class (edebug-next-token-class)))
718 (cond
719 ;; read goes one too far if a (possibly quoted) string or symbol
720 ;; is immediately followed by non-whitespace.
721 ((eq class 'symbol) (read (current-buffer)))
722 ((eq class 'string) (read (current-buffer)))
723 ((eq class 'quote) (forward-char 1)
724 (list 'quote (edebug-read-sexp)))
725 ((eq class 'backquote)
726 (list '\` (edebug-read-sexp)))
727 ((eq class 'comma)
728 (list '\, (edebug-read-sexp)))
729 (t ; anything else, just read it.
730 (read (current-buffer))))))
732 ;;; Offsets for reader
734 ;; Define a structure to represent offset positions of expressions.
735 ;; Each offset structure looks like: (before . after) for constituents,
736 ;; or for structures that have elements: (before <subexpressions> . after)
737 ;; where the <subexpressions> are the offset structures for subexpressions
738 ;; including the head of a list.
739 (defvar edebug-offsets nil)
741 ;; Stack of offset structures in reverse order of the nesting.
742 ;; This is used to get back to previous levels.
743 (defvar edebug-offsets-stack nil)
744 (defvar edebug-current-offset nil) ; Top of the stack, for convenience.
746 ;; We must store whether we just read a list with a dotted form that
747 ;; is itself a list. This structure will be condensed, so the offsets
748 ;; must also be condensed.
749 (defvar edebug-read-dotted-list nil)
751 (defsubst edebug-initialize-offsets ()
752 ;; Reinitialize offset recording.
753 (setq edebug-current-offset nil))
755 (defun edebug-store-before-offset (point)
756 ;; Add a new offset pair with POINT as the before offset.
757 (let ((new-offset (list point)))
758 (if edebug-current-offset
759 (setcdr edebug-current-offset
760 (cons new-offset (cdr edebug-current-offset)))
761 ;; Otherwise, we are at the top level, so initialize.
762 (setq edebug-offsets new-offset
763 edebug-offsets-stack nil
764 edebug-read-dotted-list nil))
765 ;; Cons the new offset to the front of the stack.
766 (setq edebug-offsets-stack (cons new-offset edebug-offsets-stack)
767 edebug-current-offset new-offset)
770 (defun edebug-store-after-offset (point)
771 ;; Finalize the current offset struct by reversing it and
772 ;; store POINT as the after offset.
773 (if (not edebug-read-dotted-list)
774 ;; Just reverse the offsets of all subexpressions.
775 (setcdr edebug-current-offset (nreverse (cdr edebug-current-offset)))
777 ;; We just read a list after a dot, which will be abbreviated out.
778 (setq edebug-read-dotted-list nil)
779 ;; Drop the corresponding offset pair.
780 ;; That is, nconc the reverse of the rest of the offsets
781 ;; with the cdr of last offset.
782 (setcdr edebug-current-offset
783 (nconc (nreverse (cdr (cdr edebug-current-offset)))
784 (cdr (car (cdr edebug-current-offset))))))
786 ;; Now append the point using nconc.
787 (setq edebug-current-offset (nconc edebug-current-offset point))
788 ;; Pop the stack.
789 (setq edebug-offsets-stack (cdr edebug-offsets-stack)
790 edebug-current-offset (car edebug-offsets-stack)))
792 (defun edebug-ignore-offset ()
793 ;; Ignore the last created offset pair.
794 (setcdr edebug-current-offset (cdr (cdr edebug-current-offset))))
796 (defmacro edebug-storing-offsets (point &rest body)
797 (declare (debug (form body)) (indent 1))
798 `(unwind-protect
799 (progn
800 (edebug-store-before-offset ,point)
801 ,@body)
802 (edebug-store-after-offset (point))))
805 ;;; Reader for Emacs Lisp.
807 ;; Uses edebug-next-token-class (and edebug-skip-whitespace) above.
809 (defconst edebug-read-alist
810 '((symbol . edebug-read-symbol)
811 (lparen . edebug-read-list)
812 (string . edebug-read-string)
813 (quote . edebug-read-quote)
814 (backquote . edebug-read-backquote)
815 (comma . edebug-read-comma)
816 (lbracket . edebug-read-vector)
817 (hash . edebug-read-function)
820 (defun edebug-read-storing-offsets (stream)
821 (let (edebug-read-dotted-list) ; see edebug-store-after-offset
822 (edebug-storing-offsets (point)
823 (funcall
824 (or (cdr (assq (edebug-next-token-class) edebug-read-alist))
825 ;; anything else, just read it.
826 #'read)
827 stream))))
829 (defalias 'edebug-read-symbol #'read)
830 (defalias 'edebug-read-string #'read)
832 (defun edebug-read-quote (stream)
833 ;; Turn 'thing into (quote thing)
834 (forward-char 1)
835 (list
836 (edebug-storing-offsets (1- (point)) 'quote)
837 (edebug-read-storing-offsets stream)))
839 (defun edebug-read-backquote (stream)
840 ;; Turn `thing into (\` thing)
841 (forward-char 1)
842 (list
843 (edebug-storing-offsets (1- (point)) '\`)
844 (edebug-read-storing-offsets stream)))
846 (defun edebug-read-comma (stream)
847 ;; Turn ,thing into (\, thing). Handle ,@ and ,. also.
848 (let ((opoint (point)))
849 (forward-char 1)
850 (let ((symbol '\,))
851 (cond ((eq (following-char) ?\.)
852 (setq symbol '\,\.)
853 (forward-char 1))
854 ((eq (following-char) ?\@)
855 (setq symbol '\,@)
856 (forward-char 1)))
857 ;; Generate the same structure of offsets we would have
858 ;; if the resulting list appeared verbatim in the input text.
859 (list
860 (edebug-storing-offsets opoint symbol)
861 (edebug-read-storing-offsets stream)))))
863 (defun edebug-read-function (stream)
864 ;; Turn #'thing into (function thing)
865 (forward-char 1)
866 (cond ((eq ?\' (following-char))
867 (forward-char 1)
868 (list
869 (edebug-storing-offsets (- (point) 2) 'function)
870 (edebug-read-storing-offsets stream)))
871 ((memq (following-char) '(?: ?B ?O ?X ?b ?o ?x ?1 ?2 ?3 ?4 ?5 ?6
872 ?7 ?8 ?9 ?0))
873 (backward-char 1)
874 (read stream))
875 (t (edebug-syntax-error "Bad char after #"))))
877 (defun edebug-read-list (stream)
878 (forward-char 1) ; skip \(
879 (prog1
880 (let ((elements))
881 (while (not (memq (edebug-next-token-class) '(rparen dot)))
882 (push (edebug-read-storing-offsets stream) elements))
883 (setq elements (nreverse elements))
884 (if (eq 'dot (edebug-next-token-class))
885 (let (dotted-form)
886 (forward-char 1) ; skip \.
887 (setq dotted-form (edebug-read-storing-offsets stream))
888 elements (nconc elements dotted-form)
889 (if (not (eq (edebug-next-token-class) 'rparen))
890 (edebug-syntax-error "Expected `)'"))
891 (setq edebug-read-dotted-list (listp dotted-form))
893 elements)
894 (forward-char 1) ; skip \)
897 (defun edebug-read-vector (stream)
898 (forward-char 1) ; skip \[
899 (prog1
900 (let ((elements))
901 (while (not (eq 'rbracket (edebug-next-token-class)))
902 (push (edebug-read-storing-offsets stream) elements))
903 (apply 'vector (nreverse elements)))
904 (forward-char 1) ; skip \]
907 ;;; Cursors for traversal of list and vector elements with offsets.
909 (defvar edebug-dotted-spec nil)
911 (defun edebug-new-cursor (expressions offsets)
912 ;; Return a new cursor for EXPRESSIONS with OFFSETS.
913 (if (vectorp expressions)
914 (setq expressions (append expressions nil)))
915 (cons expressions offsets))
917 (defsubst edebug-set-cursor (cursor expressions offsets)
918 ;; Set the CURSOR's EXPRESSIONS and OFFSETS to the given.
919 ;; Return the cursor.
920 (setcar cursor expressions)
921 (setcdr cursor offsets)
922 cursor)
924 (defun edebug-copy-cursor (cursor)
925 ;; Copy the cursor using the same object and offsets.
926 (cons (car cursor) (cdr cursor)))
928 (defsubst edebug-cursor-expressions (cursor)
929 (car cursor))
930 (defsubst edebug-cursor-offsets (cursor)
931 (cdr cursor))
933 (defsubst edebug-empty-cursor (cursor)
934 ;; Return non-nil if CURSOR is empty - meaning no more elements.
935 (null (car cursor)))
937 (defsubst edebug-top-element (cursor)
938 ;; Return the top element at the cursor.
939 ;; Assumes not empty.
940 (car (car cursor)))
942 (defun edebug-top-element-required (cursor &rest error)
943 ;; Check if a dotted form is required.
944 (if edebug-dotted-spec (edebug-no-match cursor "Dot expected."))
945 ;; Check if there is at least one more argument.
946 (if (edebug-empty-cursor cursor) (apply 'edebug-no-match cursor error))
947 ;; Return that top element.
948 (edebug-top-element cursor))
950 (defsubst edebug-top-offset (cursor)
951 ;; Return the top offset pair corresponding to the top element.
952 (car (cdr cursor)))
954 (defun edebug-move-cursor (cursor)
955 ;; Advance and return the cursor to the next element and offset.
956 ;; throw no-match if empty before moving.
957 ;; This is a violation of the cursor encapsulation, but
958 ;; there is plenty of that going on while matching.
959 ;; The following test should always fail.
960 (if (edebug-empty-cursor cursor)
961 (edebug-no-match cursor "Not enough arguments."))
962 (setcar cursor (cdr (car cursor)))
963 (setcdr cursor (cdr (cdr cursor)))
964 cursor)
967 (defun edebug-before-offset (cursor)
968 ;; Return the before offset of the cursor.
969 ;; If there is nothing left in the offsets,
970 ;; return one less than the offset itself,
971 ;; which is the after offset for a list.
972 (let ((offset (edebug-cursor-offsets cursor)))
973 (if (consp offset)
974 (car (car offset))
975 (1- offset))))
977 (defun edebug-after-offset (cursor)
978 ;; Return the after offset of the cursor object.
979 (let ((offset (edebug-top-offset cursor)))
980 (while (consp offset)
981 (setq offset (cdr offset)))
982 offset))
984 ;;; The Parser
986 ;; The top level function for parsing forms is
987 ;; edebug-read-and-maybe-wrap-form; it calls all the rest. It checks the
988 ;; syntax a bit and leaves point at any error it finds, but otherwise
989 ;; should appear to work like eval-defun.
991 ;; The basic plan is to surround each expression with a call to
992 ;; the edebug debugger together with indexes into a table of positions of
993 ;; all expressions. Thus an expression "exp" becomes:
995 ;; (edebug-after (edebug-before 1) 2 exp)
997 ;; When this is evaluated, first point is moved to the beginning of
998 ;; exp at offset 1 of the current function. The expression is
999 ;; evaluated, which may cause more edebug calls, and then point is
1000 ;; moved to offset 2 after the end of exp.
1002 ;; The highest level expressions of the function are wrapped in a call to
1003 ;; edebug-enter, which supplies the function name and the actual
1004 ;; arguments to the function. See functions edebug-enter, edebug-before,
1005 ;; and edebug-after for more details.
1007 ;; Dynamically bound vars, left unbound, but globally declared.
1008 ;; This is to quiet the byte compiler.
1010 ;; Window data of the highest definition being wrapped.
1011 ;; This data is shared by all embedded definitions.
1012 (defvar edebug-top-window-data)
1014 (defvar edebug-&optional)
1015 (defvar edebug-&rest)
1016 (defvar edebug-gate nil) ;; whether no-match forces an error.
1018 (defvar edebug-def-name nil) ; name of definition, used by interactive-form
1019 (defvar edebug-old-def-name nil) ; previous name of containing definition.
1021 (defvar edebug-error-point nil)
1022 (defvar edebug-best-error nil)
1025 (defun edebug-read-and-maybe-wrap-form ()
1026 ;; Read a form and wrap it with edebug calls, if the conditions are right.
1027 ;; Here we just catch any no-match not caught below and signal an error.
1029 ;; Run the setup hook.
1030 ;; If it gets an error, make it nil.
1031 (let ((temp-hook edebug-setup-hook))
1032 (setq edebug-setup-hook nil)
1033 (if (functionp temp-hook) (funcall temp-hook)
1034 (mapc #'funcall temp-hook)))
1036 (let (result
1037 edebug-top-window-data
1038 edebug-def-name;; make sure it is locally nil
1039 ;; I don't like these here!!
1040 edebug-&optional
1041 edebug-&rest
1042 edebug-gate
1043 edebug-best-error
1044 edebug-error-point
1045 ;; Do this once here instead of several times.
1046 (max-lisp-eval-depth (+ 800 max-lisp-eval-depth))
1047 (max-specpdl-size (+ 2000 max-specpdl-size)))
1048 (let ((no-match
1049 (catch 'no-match
1050 (setq result (edebug-read-and-maybe-wrap-form1))
1051 nil)))
1052 (if no-match
1053 (apply 'edebug-syntax-error no-match)))
1054 result))
1057 (defun edebug-read-and-maybe-wrap-form1 ()
1058 (let (spec
1059 def-kind
1060 defining-form-p
1061 def-name
1062 ;; These offset things don't belong here, but to support recursive
1063 ;; calls to edebug-read, they need to be here.
1064 edebug-offsets
1065 edebug-offsets-stack
1066 edebug-current-offset ; reset to nil
1068 (save-excursion
1069 (if (and (eq 'lparen (edebug-next-token-class))
1070 (eq 'symbol (progn (forward-char 1) (edebug-next-token-class))))
1071 ;; Find out if this is a defining form from first symbol
1072 (setq def-kind (read (current-buffer))
1073 spec (and (symbolp def-kind) (get-edebug-spec def-kind))
1074 defining-form-p (and (listp spec)
1075 (eq '&define (car spec)))
1076 ;; This is incorrect in general!! But OK most of the time.
1077 def-name (if (and defining-form-p
1078 (eq 'name (car (cdr spec)))
1079 (eq 'symbol (edebug-next-token-class)))
1080 (read (current-buffer))))))
1081 ;;;(message "all defs: %s all forms: %s" edebug-all-defs edebug-all-forms)
1082 (cond
1083 (defining-form-p
1084 (if (or edebug-all-defs edebug-all-forms)
1085 ;; If it is a defining form and we are edebugging defs,
1086 ;; then let edebug-list-form start it.
1087 (let ((cursor (edebug-new-cursor
1088 (list (edebug-read-storing-offsets (current-buffer)))
1089 (list edebug-offsets))))
1090 (car
1091 (edebug-make-form-wrapper
1092 cursor
1093 (edebug-before-offset cursor)
1094 (1- (edebug-after-offset cursor))
1095 (list (cons (symbol-name def-kind) (cdr spec))))))
1097 ;; Not edebugging this form, so reset the symbol's edebug
1098 ;; property to be just a marker at the definition's source code.
1099 ;; This only works for defs with simple names.
1100 (put def-name 'edebug (point-marker))
1101 ;; Also nil out dependent defs.
1102 '(mapcar (function
1103 (lambda (def)
1104 (put def-name 'edebug nil)))
1105 (get def-name 'edebug-dependents))
1106 (edebug-read-sexp)))
1108 ;; If all forms are being edebugged, explicitly wrap it.
1109 (edebug-all-forms
1110 (let ((cursor (edebug-new-cursor
1111 (list (edebug-read-storing-offsets (current-buffer)))
1112 (list edebug-offsets))))
1113 (edebug-make-form-wrapper
1114 cursor
1115 (edebug-before-offset cursor)
1116 (edebug-after-offset cursor)
1117 nil)))
1119 ;; Not a defining form, and not edebugging.
1120 (t (edebug-read-sexp)))
1124 (defvar edebug-def-args) ; args of defining form.
1125 (defvar edebug-def-interactive) ; is it an emacs interactive function?
1126 (defvar edebug-inside-func) ;; whether code is inside function context.
1127 ;; Currently def-form sets this to nil; def-body sets it to t.
1129 (defun edebug-interactive-p-name ()
1130 ;; Return a unique symbol for the variable used to store the
1131 ;; status of interactive-p for this function.
1132 (intern (format "edebug-%s-interactive-p" edebug-def-name)))
1135 (defun edebug-wrap-def-body (forms)
1136 "Wrap the FORMS of a definition body."
1137 (if edebug-def-interactive
1138 `(let ((,(edebug-interactive-p-name)
1139 (interactive-p)))
1140 ,(edebug-make-enter-wrapper forms))
1141 (edebug-make-enter-wrapper forms)))
1144 (defun edebug-make-enter-wrapper (forms)
1145 ;; Generate the enter wrapper for some forms of a definition.
1146 ;; This is not to be used for the body of other forms, e.g. `while',
1147 ;; since it wraps the list of forms with a call to `edebug-enter'.
1148 ;; Uses the dynamically bound vars edebug-def-name and edebug-def-args.
1149 ;; Do this after parsing since that may find a name.
1150 (setq edebug-def-name
1151 (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
1152 `(edebug-enter
1153 (quote ,edebug-def-name)
1154 ,(if edebug-inside-func
1155 `(list
1156 ;; Doesn't work with more than one def-body!!
1157 ;; But the list will just be reversed.
1158 ,@(nreverse edebug-def-args))
1159 'nil)
1160 (function (lambda () ,@forms))
1164 (defvar edebug-form-begin-marker) ; the mark for def being instrumented
1166 (defvar edebug-offset-index) ; the next available offset index.
1167 (defvar edebug-offset-list) ; the list of offset positions.
1169 (defun edebug-inc-offset (offset)
1170 ;; Modifies edebug-offset-index and edebug-offset-list
1171 ;; accesses edebug-func-marc and buffer point.
1172 (prog1
1173 edebug-offset-index
1174 (setq edebug-offset-list (cons (- offset edebug-form-begin-marker)
1175 edebug-offset-list)
1176 edebug-offset-index (1+ edebug-offset-index))))
1179 (defun edebug-make-before-and-after-form (before-index form after-index)
1180 ;; Return the edebug form for the current function at offset BEFORE-INDEX
1181 ;; given FORM. Looks like:
1182 ;; (edebug-after (edebug-before BEFORE-INDEX) AFTER-INDEX FORM)
1183 ;; Also increment the offset index for subsequent use.
1184 `(edebug-after (edebug-before ,before-index) ,after-index ,form))
1186 (defun edebug-make-after-form (form after-index)
1187 ;; Like edebug-make-before-and-after-form, but only after.
1188 `(edebug-after 0 ,after-index ,form))
1191 (defun edebug-unwrap (sexp)
1192 "Return the unwrapped SEXP or return it as is if it is not wrapped.
1193 The SEXP might be the result of wrapping a body, which is a list of
1194 expressions; a `progn' form will be returned enclosing these forms."
1195 (if (consp sexp)
1196 (cond
1197 ((eq 'edebug-after (car sexp))
1198 (nth 3 sexp))
1199 ((eq 'edebug-enter (car sexp))
1200 (macroexp-progn (nthcdr 2 (nth 1 (nth 3 sexp)))))
1201 (t sexp);; otherwise it is not wrapped, so just return it.
1203 sexp))
1205 (defun edebug-unwrap* (sexp)
1206 "Return the SEXP recursively unwrapped."
1207 (let ((new-sexp (edebug-unwrap sexp)))
1208 (while (not (eq sexp new-sexp))
1209 (setq sexp new-sexp
1210 new-sexp (edebug-unwrap sexp)))
1211 (if (consp new-sexp)
1212 (mapcar 'edebug-unwrap* new-sexp)
1213 new-sexp)))
1216 (defun edebug-defining-form (cursor form-begin form-end speclist)
1217 ;; Process the defining form, starting outside the form.
1218 ;; The speclist is a generated list spec that looks like:
1219 ;; (("def-symbol" defining-form-spec-sans-&define))
1220 ;; Skip the first offset.
1221 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1222 (cdr (edebug-cursor-offsets cursor)))
1223 (edebug-make-form-wrapper
1224 cursor
1225 form-begin (1- form-end)
1226 speclist))
1228 (defun edebug-make-form-wrapper (cursor form-begin form-end
1229 &optional speclist)
1230 ;; Wrap a form, usually a defining form, but any evaluated one.
1231 ;; If speclist is non-nil, this is being called by edebug-defining-form.
1232 ;; Otherwise it is being called from edebug-read-and-maybe-wrap-form1.
1233 ;; This is a hack, but I haven't figured out a simpler way yet.
1234 (let* ((form-data-entry (edebug-get-form-data-entry form-begin form-end))
1235 ;; Set this marker before parsing.
1236 (edebug-form-begin-marker
1237 (if form-data-entry
1238 (edebug--form-data-begin form-data-entry)
1239 ;; Buffer must be current-buffer for this to work:
1240 (set-marker (make-marker) form-begin))))
1242 (let (edebug-offset-list
1243 (edebug-offset-index 0)
1244 result
1245 ;; For definitions.
1246 ;; (edebug-containing-def-name edebug-def-name)
1247 ;; Get name from form-data, if any.
1248 (edebug-old-def-name (edebug--form-data-name form-data-entry))
1249 edebug-def-name
1250 edebug-def-args
1251 edebug-def-interactive
1252 edebug-inside-func;; whether wrapped code executes inside a function.
1255 (setq result
1256 (if speclist
1257 (edebug-match cursor speclist)
1259 ;; else wrap as an enter-form.
1260 (edebug-make-enter-wrapper (list (edebug-form cursor)))))
1262 ;; Set the name here if it was not set by edebug-make-enter-wrapper.
1263 (setq edebug-def-name
1264 (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
1266 ;; Add this def as a dependent of containing def. Buggy.
1267 '(if (and edebug-containing-def-name
1268 (not (get edebug-containing-def-name 'edebug-dependents)))
1269 (put edebug-containing-def-name 'edebug-dependents
1270 (cons edebug-def-name
1271 (get edebug-containing-def-name
1272 'edebug-dependents))))
1274 ;; Create a form-data-entry or modify existing entry's markers.
1275 ;; In the latter case, pointers to the entry remain eq.
1276 (if (not form-data-entry)
1277 (setq form-data-entry
1278 (edebug--make-form-data-entry
1279 edebug-def-name
1280 edebug-form-begin-marker
1281 ;; Buffer must be current-buffer.
1282 (set-marker (make-marker) form-end)
1284 (edebug-set-form-data-entry
1285 form-data-entry edebug-def-name ;; in case name is changed
1286 form-begin form-end))
1288 ;; (message "defining: %s" edebug-def-name) (sit-for 2)
1289 (edebug-make-top-form-data-entry form-data-entry)
1290 (message "Edebug: %s" edebug-def-name)
1291 ;;(debug edebug-def-name)
1293 ;; Destructively reverse edebug-offset-list and make vector from it.
1294 (setq edebug-offset-list (vconcat (nreverse edebug-offset-list)))
1296 ;; Side effects on the property list of edebug-def-name.
1297 (edebug-clear-frequency-count edebug-def-name)
1298 (edebug-clear-coverage edebug-def-name)
1300 ;; Set up the initial window data.
1301 (if (not edebug-top-window-data) ;; if not already set, do it now.
1302 (let ((window ;; Find the best window for this buffer.
1303 (or (get-buffer-window (current-buffer))
1304 (selected-window))))
1305 (setq edebug-top-window-data
1306 (cons window (window-start window)))))
1308 ;; Store the edebug data in symbol's property list.
1309 (put edebug-def-name 'edebug
1310 ;; A struct or vector would be better here!!
1311 (list edebug-form-begin-marker
1312 nil ; clear breakpoints
1313 edebug-offset-list
1314 edebug-top-window-data
1316 result
1320 (defun edebug-clear-frequency-count (name)
1321 ;; Create initial frequency count vector.
1322 ;; For each stop point, the counter is incremented each time it is visited.
1323 (put name 'edebug-freq-count
1324 (make-vector (length edebug-offset-list) 0)))
1327 (defun edebug-clear-coverage (name)
1328 ;; Create initial coverage vector.
1329 ;; Only need one per expression, but it is simpler to use stop points.
1330 (put name 'edebug-coverage
1331 (make-vector (length edebug-offset-list) 'unknown)))
1334 (defun edebug-form (cursor)
1335 ;; Return the instrumented form for the following form.
1336 ;; Add the point offsets to the edebug-offset-list for the form.
1337 (let* ((form (edebug-top-element-required cursor "Expected form"))
1338 (offset (edebug-top-offset cursor)))
1339 (prog1
1340 (cond
1341 ((consp form)
1342 ;; The first offset for a list form is for the list form itself.
1343 (if (eq 'quote (car form))
1344 form
1345 (let* ((head (car form))
1346 (spec (and (symbolp head) (get-edebug-spec head)))
1347 (new-cursor (edebug-new-cursor form offset)))
1348 ;; Find out if this is a defining form from first symbol.
1349 ;; An indirect spec would not work here, yet.
1350 (if (and (consp spec) (eq '&define (car spec)))
1351 (edebug-defining-form
1352 new-cursor
1353 (car offset);; before the form
1354 (edebug-after-offset cursor)
1355 (cons (symbol-name head) (cdr spec)))
1356 ;; Wrap a regular form.
1357 (edebug-make-before-and-after-form
1358 (edebug-inc-offset (car offset))
1359 (edebug-list-form new-cursor)
1360 ;; After processing the list form, the new-cursor is left
1361 ;; with the offset after the form.
1362 (edebug-inc-offset (edebug-cursor-offsets new-cursor))))
1365 ((symbolp form)
1366 (cond
1367 ;; Check for constant symbols that don't get wrapped.
1368 ((or (memq form '(t nil))
1369 (keywordp form))
1370 form)
1372 (t ;; just a variable
1373 (edebug-make-after-form form (edebug-inc-offset (cdr offset))))))
1375 ;; Anything else is self-evaluating.
1376 (t form))
1377 (edebug-move-cursor cursor))))
1380 (defsubst edebug-forms (cursor) (edebug-match cursor '(&rest form)))
1381 (defsubst edebug-sexps (cursor) (edebug-match cursor '(&rest sexp)))
1383 (defsubst edebug-list-form-args (head cursor)
1384 ;; Process the arguments of a list form given that head of form is a symbol.
1385 ;; Helper for edebug-list-form
1386 (let ((spec (get-edebug-spec head)))
1387 (cond
1388 (spec
1389 (cond
1390 ((consp spec)
1391 ;; It is a speclist.
1392 (let (edebug-best-error
1393 edebug-error-point);; This may not be needed.
1394 (edebug-match-sublist cursor spec)))
1395 ((eq t spec) (edebug-forms cursor))
1396 ((eq 0 spec) (edebug-sexps cursor))
1397 ((symbolp spec) (funcall spec cursor));; Not used by edebug,
1398 ; but leave it in for compatibility.
1400 ;; No edebug-form-spec provided.
1401 ((macrop head)
1402 (if edebug-eval-macro-args
1403 (edebug-forms cursor)
1404 (edebug-sexps cursor)))
1405 (t ;; Otherwise it is a function call.
1406 (edebug-forms cursor)))))
1409 (defun edebug-list-form (cursor)
1410 ;; Return an instrumented form built from the list form.
1411 ;; The after offset will be left in the cursor after processing the form.
1412 (let ((head (edebug-top-element-required cursor "Expected elements"))
1413 ;; Prevent backtracking whenever instrumenting.
1414 (edebug-gate t)
1415 ;; A list form is never optional because it matches anything.
1416 (edebug-&optional nil)
1417 (edebug-&rest nil))
1418 ;; Skip the first offset.
1419 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1420 (cdr (edebug-cursor-offsets cursor)))
1421 (cond
1422 ((symbolp head)
1423 (cond
1424 ((null head) nil) ; () is valid.
1425 ((eq head 'interactive-p)
1426 ;; Special case: replace (interactive-p) with variable
1427 (setq edebug-def-interactive 'check-it)
1428 (edebug-move-cursor cursor)
1429 (edebug-interactive-p-name))
1431 (cons head (edebug-list-form-args
1432 head (edebug-move-cursor cursor))))))
1434 ((consp head)
1435 (if (eq (car head) '\,)
1436 ;; The head of a form should normally be a symbol or a lambda
1437 ;; expression but it can also be an unquote form to be filled
1438 ;; before evaluation. We evaluate the arguments anyway, on the
1439 ;; assumption that the unquote form will place a proper function
1440 ;; name (rather than a macro name).
1441 (edebug-match cursor '(("," def-form) body))
1442 ;; Process anonymous function and args.
1443 ;; This assumes no anonymous macros.
1444 (edebug-match-specs cursor '(lambda-expr body) 'edebug-match-specs)))
1446 (t (edebug-syntax-error
1447 "Head of list form must be a symbol or lambda expression")))
1450 ;;; Matching of specs.
1452 (defvar edebug-after-dotted-spec nil)
1454 (defvar edebug-matching-depth 0) ;; initial value
1455 (defconst edebug-max-depth 150) ;; maximum number of matching recursions.
1458 ;;; Failure to match
1460 ;; This throws to no-match, if there are higher alternatives.
1461 ;; Otherwise it signals an error. The place of the error is found
1462 ;; with the two before- and after-offset functions.
1464 (defun edebug-no-match (cursor &rest args)
1465 ;; Throw a no-match, or signal an error immediately if gate is active.
1466 ;; Remember this point in case we need to report this error.
1467 (setq edebug-error-point (or edebug-error-point
1468 (edebug-before-offset cursor))
1469 edebug-best-error (or edebug-best-error args))
1470 (if (and edebug-gate (not edebug-&optional))
1471 (progn
1472 (if edebug-error-point
1473 (goto-char edebug-error-point))
1474 (apply 'edebug-syntax-error args))
1475 (throw 'no-match args)))
1478 (defun edebug-match (cursor specs)
1479 ;; Top level spec matching function.
1480 ;; Used also at each lower level of specs.
1481 (let (edebug-&optional
1482 edebug-&rest
1483 edebug-best-error
1484 edebug-error-point
1485 (edebug-gate edebug-gate) ;; locally bound to limit effect
1487 (edebug-match-specs cursor specs 'edebug-match-specs)))
1490 (defun edebug-match-one-spec (cursor spec)
1491 ;; Match one spec, which is not a keyword &-spec.
1492 (cond
1493 ((symbolp spec) (edebug-match-symbol cursor spec))
1494 ((vectorp spec) (edebug-match cursor (append spec nil)))
1495 ((stringp spec) (edebug-match-string cursor spec))
1496 ((listp spec) (edebug-match-list cursor spec))
1500 (defun edebug-match-specs (cursor specs remainder-handler)
1501 ;; Append results of matching the list of specs.
1502 ;; The first spec is handled and the remainder-handler handles the rest.
1503 (let ((edebug-matching-depth
1504 (if (> edebug-matching-depth edebug-max-depth)
1505 (error "Too deep - perhaps infinite loop in spec?")
1506 (1+ edebug-matching-depth))))
1507 (cond
1508 ((null specs) nil)
1510 ;; Is the spec dotted?
1511 ((atom specs)
1512 (let ((edebug-dotted-spec t));; Containing spec list was dotted.
1513 (edebug-match-specs cursor (list specs) remainder-handler)))
1515 ;; Is the form dotted?
1516 ((not (listp (edebug-cursor-expressions cursor)));; allow nil
1517 (if (not edebug-dotted-spec)
1518 (edebug-no-match cursor "Dotted spec required."))
1519 ;; Cancel dotted spec and dotted form.
1520 (let ((edebug-dotted-spec)
1521 (this-form (edebug-cursor-expressions cursor))
1522 (this-offset (edebug-cursor-offsets cursor)))
1523 ;; Wrap the form in a list, (by changing the cursor??)...
1524 (edebug-set-cursor cursor (list this-form) this-offset)
1525 ;; and process normally, then unwrap the result.
1526 (car (edebug-match-specs cursor specs remainder-handler))))
1528 (t;; Process normally.
1529 (let* ((spec (car specs))
1530 (rest)
1531 (first-char (and (symbolp spec) (aref (symbol-name spec) 0))))
1532 ;;(message "spec = %s first char = %s" spec first-char) (sit-for 1)
1533 (nconc
1534 (cond
1535 ((eq ?& first-char);; "&" symbols take all following specs.
1536 (funcall (get-edebug-spec spec) cursor (cdr specs)))
1537 ((eq ?: first-char);; ":" symbols take one following spec.
1538 (setq rest (cdr (cdr specs)))
1539 (funcall (get-edebug-spec spec) cursor (car (cdr specs))))
1540 (t;; Any other normal spec.
1541 (setq rest (cdr specs))
1542 (edebug-match-one-spec cursor spec)))
1543 (funcall remainder-handler cursor rest remainder-handler)))))))
1546 ;; Define specs for all the symbol specs with functions used to process them.
1547 ;; Perhaps we shouldn't be doing this with edebug-form-specs since the
1548 ;; user may want to define macros or functions with the same names.
1549 ;; We could use an internal obarray for these primitive specs.
1551 (dolist (pair '((&optional . edebug-match-&optional)
1552 (&rest . edebug-match-&rest)
1553 (&or . edebug-match-&or)
1554 (form . edebug-match-form)
1555 (sexp . edebug-match-sexp)
1556 (body . edebug-match-body)
1557 (&define . edebug-match-&define)
1558 (name . edebug-match-name)
1559 (:name . edebug-match-colon-name)
1560 (arg . edebug-match-arg)
1561 (def-body . edebug-match-def-body)
1562 (def-form . edebug-match-def-form)
1563 ;; Less frequently used:
1564 ;; (function . edebug-match-function)
1565 (lambda-expr . edebug-match-lambda-expr)
1566 (&not . edebug-match-&not)
1567 (&key . edebug-match-&key)
1568 (place . edebug-match-place)
1569 (gate . edebug-match-gate)
1570 ;; (nil . edebug-match-nil) not this one - special case it.
1572 (put (car pair) 'edebug-form-spec (cdr pair)))
1574 (defun edebug-match-symbol (cursor symbol)
1575 ;; Match a symbol spec.
1576 (let* ((spec (get-edebug-spec symbol)))
1577 (cond
1578 (spec
1579 (if (consp spec)
1580 ;; It is an indirect spec.
1581 (edebug-match cursor spec)
1582 ;; Otherwise it should be the symbol name of a function.
1583 ;; There could be a bug here - maybe need to do edebug-match bindings.
1584 (funcall spec cursor)))
1586 ((null symbol) ;; special case this.
1587 (edebug-match-nil cursor))
1589 ((fboundp symbol) ; is it a predicate?
1590 (let ((sexp (edebug-top-element-required cursor "Expected" symbol)))
1591 ;; Special case for edebug-`.
1592 (if (and (listp sexp) (eq (car sexp) '\,))
1593 (edebug-match cursor '(("," def-form)))
1594 (if (not (funcall symbol sexp))
1595 (edebug-no-match cursor symbol "failed"))
1596 (edebug-move-cursor cursor)
1597 (list sexp))))
1598 (t (error "%s is not a form-spec or function" symbol))
1602 (defun edebug-match-sexp (cursor)
1603 (list (prog1 (edebug-top-element-required cursor "Expected sexp")
1604 (edebug-move-cursor cursor))))
1606 (defun edebug-match-form (cursor)
1607 (list (edebug-form cursor)))
1609 (defalias 'edebug-match-place 'edebug-match-form)
1610 ;; Currently identical to edebug-match-form.
1611 ;; This is for common lisp setf-style place arguments.
1613 (defsubst edebug-match-body (cursor) (edebug-forms cursor))
1615 (defun edebug-match-&optional (cursor specs)
1616 ;; Keep matching until one spec fails.
1617 (edebug-&optional-wrapper cursor specs 'edebug-&optional-wrapper))
1619 (defun edebug-&optional-wrapper (cursor specs remainder-handler)
1620 (let (result
1621 (edebug-&optional specs)
1622 (edebug-gate nil)
1623 (this-form (edebug-cursor-expressions cursor))
1624 (this-offset (edebug-cursor-offsets cursor)))
1625 (if (null (catch 'no-match
1626 (setq result
1627 (edebug-match-specs cursor specs remainder-handler))
1628 ;; Returning nil means no no-match was thrown.
1629 nil))
1630 result
1631 ;; no-match, but don't fail; just reset cursor and return nil.
1632 (edebug-set-cursor cursor this-form this-offset)
1633 nil)))
1636 (defun edebug-&rest-wrapper (cursor specs remainder-handler)
1637 (if (null specs) (setq specs edebug-&rest))
1638 ;; Reuse the &optional handler with this as the remainder handler.
1639 (edebug-&optional-wrapper cursor specs remainder-handler))
1641 (defun edebug-match-&rest (cursor specs)
1642 ;; Repeatedly use specs until failure.
1643 (let ((edebug-&rest specs) ;; remember these
1644 edebug-best-error
1645 edebug-error-point)
1646 (edebug-&rest-wrapper cursor specs 'edebug-&rest-wrapper)))
1649 (defun edebug-match-&or (cursor specs)
1650 ;; Keep matching until one spec succeeds, and return its results.
1651 ;; If none match, fail.
1652 ;; This needs to be optimized since most specs spend time here.
1653 (let ((original-specs specs)
1654 (this-form (edebug-cursor-expressions cursor))
1655 (this-offset (edebug-cursor-offsets cursor)))
1656 (catch 'matched
1657 (while specs
1658 (catch 'no-match
1659 (throw 'matched
1660 (let (edebug-gate ;; only while matching each spec
1661 edebug-best-error
1662 edebug-error-point)
1663 ;; Doesn't support e.g. &or symbolp &rest form
1664 (edebug-match-one-spec cursor (car specs)))))
1665 ;; Match failed, so reset and try again.
1666 (setq specs (cdr specs))
1667 ;; Reset the cursor for the next match.
1668 (edebug-set-cursor cursor this-form this-offset))
1669 ;; All failed.
1670 (apply 'edebug-no-match cursor "Expected one of" original-specs))
1674 (defun edebug-match-&not (cursor specs)
1675 ;; If any specs match, then fail
1676 (if (null (catch 'no-match
1677 (let ((edebug-gate nil))
1678 (save-excursion
1679 (edebug-match-&or cursor specs)))
1680 nil))
1681 ;; This means something matched, so it is a no match.
1682 (edebug-no-match cursor "Unexpected"))
1683 ;; This means nothing matched, so it is OK.
1684 nil) ;; So, return nothing
1687 (def-edebug-spec &key edebug-match-&key)
1689 (defun edebug-match-&key (cursor specs)
1690 ;; Following specs must look like (<name> <spec>) ...
1691 ;; where <name> is the name of a keyword, and spec is its spec.
1692 ;; This really doesn't save much over the expanded form and takes time.
1693 (edebug-match-&rest
1694 cursor
1695 (cons '&or
1696 (mapcar (function (lambda (pair)
1697 (vector (format ":%s" (car pair))
1698 (car (cdr pair)))))
1699 specs))))
1702 (defun edebug-match-gate (_cursor)
1703 ;; Simply set the gate to prevent backtracking at this level.
1704 (setq edebug-gate t)
1705 nil)
1708 (defun edebug-match-list (cursor specs)
1709 ;; The spec is a list, but what kind of list, and what context?
1710 (if edebug-dotted-spec
1711 ;; After dotted spec but form did not contain dot,
1712 ;; so match list spec elements as if spliced in.
1713 (prog1
1714 (let ((edebug-dotted-spec))
1715 (edebug-match-specs cursor specs 'edebug-match-specs))
1716 ;; If it matched, really clear the dotted-spec flag.
1717 (setq edebug-dotted-spec nil))
1718 (let ((spec (car specs))
1719 (form (edebug-top-element-required cursor "Expected" specs)))
1720 (cond
1721 ((eq 'quote spec)
1722 (let ((spec (car (cdr specs))))
1723 (cond
1724 ((symbolp spec)
1725 ;; Special case: spec quotes a symbol to match.
1726 ;; Change in future. Use "..." instead.
1727 (if (not (eq spec form))
1728 (edebug-no-match cursor "Expected" spec))
1729 (edebug-move-cursor cursor)
1730 (setq edebug-gate t)
1731 form)
1733 (error "Bad spec: %s" specs)))))
1735 ((eq 'vector spec)
1736 (if (vectorp form)
1737 ;; Special case: match a vector with the specs.
1738 (let ((result (edebug-match-sublist
1739 (edebug-new-cursor
1740 form (cdr (edebug-top-offset cursor)))
1741 (cdr specs))))
1742 (edebug-move-cursor cursor)
1743 (list (apply 'vector result)))
1744 (edebug-no-match cursor "Expected" specs)))
1746 ((listp form)
1747 (prog1
1748 (list (edebug-match-sublist
1749 ;; First offset is for the list form itself.
1750 ;; Treat nil as empty list.
1751 (edebug-new-cursor form (cdr (edebug-top-offset cursor)))
1752 specs))
1753 (edebug-move-cursor cursor)))
1755 (t (edebug-no-match cursor "Expected" specs)))
1759 (defun edebug-match-sublist (cursor specs)
1760 ;; Match a sublist of specs.
1761 (let (edebug-&optional
1762 ;;edebug-best-error
1763 ;;edebug-error-point
1765 (prog1
1766 ;; match with edebug-match-specs so edebug-best-error is not bound.
1767 (edebug-match-specs cursor specs 'edebug-match-specs)
1768 (if (not (edebug-empty-cursor cursor))
1769 (if edebug-best-error
1770 (apply 'edebug-no-match cursor edebug-best-error)
1771 ;; A failed &rest or &optional spec may leave some args.
1772 (edebug-no-match cursor "Failed matching" specs)
1773 )))))
1776 (defun edebug-match-string (cursor spec)
1777 (let ((sexp (edebug-top-element-required cursor "Expected" spec)))
1778 (if (not (eq (intern spec) sexp))
1779 (edebug-no-match cursor "Expected" spec)
1780 ;; Since it matched, failure means immediate error, unless &optional.
1781 (setq edebug-gate t)
1782 (edebug-move-cursor cursor)
1783 (list sexp)
1786 (defun edebug-match-nil (cursor)
1787 ;; There must be nothing left to match a nil.
1788 (if (not (edebug-empty-cursor cursor))
1789 (edebug-no-match cursor "Unmatched argument(s)")
1790 nil))
1793 (defun edebug-match-function (_cursor)
1794 (error "Use function-form instead of function in edebug spec"))
1796 (defun edebug-match-&define (cursor specs)
1797 ;; Match a defining form.
1798 ;; Normally, &define is interpreted specially other places.
1799 ;; This should only be called inside of a spec list to match the remainder
1800 ;; of the current list. e.g. ("lambda" &define args def-body)
1801 (edebug-make-form-wrapper
1802 cursor
1803 (edebug-before-offset cursor)
1804 ;; Find the last offset in the list.
1805 (let ((offsets (edebug-cursor-offsets cursor)))
1806 (while (consp offsets) (setq offsets (cdr offsets)))
1807 offsets)
1808 specs))
1810 (defun edebug-match-lambda-expr (cursor)
1811 ;; The expression must be a function.
1812 ;; This will match any list form that begins with a symbol
1813 ;; that has an edebug-form-spec beginning with &define. In
1814 ;; practice, only lambda expressions should be used.
1815 ;; I could add a &lambda specification to avoid confusion.
1816 (let* ((sexp (edebug-top-element-required
1817 cursor "Expected lambda expression"))
1818 (offset (edebug-top-offset cursor))
1819 (head (and (consp sexp) (car sexp)))
1820 (spec (and (symbolp head) (get-edebug-spec head)))
1821 (edebug-inside-func nil))
1822 ;; Find out if this is a defining form from first symbol.
1823 (if (and (consp spec) (eq '&define (car spec)))
1824 (prog1
1825 (list
1826 (edebug-defining-form
1827 (edebug-new-cursor sexp offset)
1828 (car offset);; before the sexp
1829 (edebug-after-offset cursor)
1830 (cons (symbol-name head) (cdr spec))))
1831 (edebug-move-cursor cursor))
1832 (edebug-no-match cursor "Expected lambda expression")
1836 (defun edebug-match-name (cursor)
1837 ;; Set the edebug-def-name bound in edebug-defining-form.
1838 (let ((name (edebug-top-element-required cursor "Expected name")))
1839 ;; Maybe strings and numbers could be used.
1840 (if (not (symbolp name))
1841 (edebug-no-match cursor "Symbol expected for name of definition"))
1842 (setq edebug-def-name
1843 (if edebug-def-name
1844 ;; Construct a new name by appending to previous name.
1845 (intern (format "%s@%s" edebug-def-name name))
1846 name))
1847 (edebug-move-cursor cursor)
1848 (list name)))
1850 (defun edebug-match-colon-name (_cursor spec)
1851 ;; Set the edebug-def-name to the spec.
1852 (setq edebug-def-name
1853 (if edebug-def-name
1854 ;; Construct a new name by appending to previous name.
1855 (intern (format "%s@%s" edebug-def-name spec))
1856 spec))
1857 nil)
1859 (defun edebug-match-arg (cursor)
1860 ;; set the def-args bound in edebug-defining-form
1861 (let ((edebug-arg (edebug-top-element-required cursor "Expected arg")))
1862 (if (or (not (symbolp edebug-arg))
1863 (edebug-lambda-list-keywordp edebug-arg))
1864 (edebug-no-match cursor "Bad argument:" edebug-arg))
1865 (edebug-move-cursor cursor)
1866 (setq edebug-def-args (cons edebug-arg edebug-def-args))
1867 (list edebug-arg)))
1869 (defun edebug-match-def-form (cursor)
1870 ;; Like form but the form is wrapped in edebug-enter form.
1871 ;; The form is assumed to be executing outside of the function context.
1872 ;; This is a hack for now, since a def-form might execute inside as well.
1873 ;; Not to be used otherwise.
1874 (let ((edebug-inside-func nil))
1875 (list (edebug-make-enter-wrapper (list (edebug-form cursor))))))
1877 (defun edebug-match-def-body (cursor)
1878 ;; Like body but body is wrapped in edebug-enter form.
1879 ;; The body is assumed to be executing inside of the function context.
1880 ;; Not to be used otherwise.
1881 (let* ((edebug-inside-func t)
1882 (forms (edebug-forms cursor)))
1883 ;; If there's no form, there's nothing to wrap!
1884 ;; This happens to handle bug#20281, tho maybe a better fix would be to
1885 ;; improve the `defun' spec.
1886 (when forms
1887 (list (edebug-wrap-def-body forms)))))
1890 ;;;; Edebug Form Specs
1891 ;;; ==========================================================
1893 ;;;;* Spec for def-edebug-spec
1894 ;;; Out of date.
1896 (defun edebug-spec-p (object)
1897 "Return non-nil if OBJECT is a symbol with an edebug-form-spec property."
1898 (and (symbolp object)
1899 (get object 'edebug-form-spec)))
1901 (def-edebug-spec def-edebug-spec
1902 ;; Top level is different from lower levels.
1903 (&define :name edebug-spec name
1904 &or "nil" edebug-spec-p "t" "0" (&rest edebug-spec)))
1906 (def-edebug-spec edebug-spec-list
1907 ;; A list must have something in it, or it is nil, a symbolp
1908 ((edebug-spec . [&or nil edebug-spec])))
1910 (def-edebug-spec edebug-spec
1911 (&or
1912 (vector &rest edebug-spec) ; matches a vector
1913 ("vector" &rest edebug-spec) ; matches a vector spec
1914 ("quote" symbolp)
1915 edebug-spec-list
1916 stringp
1917 [edebug-lambda-list-keywordp &rest edebug-spec]
1918 [keywordp gate edebug-spec]
1919 edebug-spec-p ;; Including all the special ones e.g. form.
1920 symbolp;; a predicate
1924 ;;;* Emacs special forms and some functions.
1926 ;; quote expects only one argument, although it allows any number.
1927 (def-edebug-spec quote sexp)
1929 ;; The standard defining forms.
1930 (def-edebug-spec defconst defvar)
1931 (def-edebug-spec defvar (symbolp &optional form stringp))
1933 (def-edebug-spec defun
1934 (&define name lambda-list
1935 [&optional stringp]
1936 [&optional ("interactive" interactive)]
1937 def-body))
1938 (def-edebug-spec defmacro
1939 ;; FIXME: Improve `declare' so we can Edebug gv-expander and
1940 ;; gv-setter declarations.
1941 (&define name lambda-list [&optional stringp]
1942 [&optional ("declare" &rest sexp)] def-body))
1944 (def-edebug-spec arglist lambda-list) ;; deprecated - use lambda-list.
1946 (def-edebug-spec lambda-list
1947 (([&rest arg]
1948 [&optional ["&optional" arg &rest arg]]
1949 &optional ["&rest" arg]
1952 (def-edebug-spec interactive
1953 (&optional &or stringp def-form))
1955 ;; A function-form is for an argument that may be a function or a form.
1956 ;; This specially recognizes anonymous functions quoted with quote.
1957 (def-edebug-spec function-form
1958 ;; form at the end could also handle "function",
1959 ;; but recognize it specially to avoid wrapping function forms.
1960 (&or ([&or "quote" "function"] &or symbolp lambda-expr) form))
1962 ;; function expects a symbol or a lambda or macro expression
1963 ;; A macro is allowed by Emacs.
1964 (def-edebug-spec function (&or symbolp lambda-expr))
1966 ;; A macro expression is a lambda expression with "macro" prepended.
1967 (def-edebug-spec macro (&define "lambda" lambda-list def-body))
1969 ;; (def-edebug-spec anonymous-form ((&or ["lambda" lambda] ["macro" macro])))
1971 ;; Standard functions that take function-forms arguments.
1973 ;; FIXME? The manual uses this form (maybe that's just for illustration?):
1974 ;; (def-edebug-spec let
1975 ;; ((&rest &or symbolp (gate symbolp &optional form))
1976 ;; body))
1977 (def-edebug-spec let
1978 ((&rest &or (symbolp &optional form) symbolp)
1979 body))
1981 (def-edebug-spec let* let)
1983 (def-edebug-spec setq (&rest symbolp form))
1984 (def-edebug-spec setq-default setq)
1986 (def-edebug-spec cond (&rest (&rest form)))
1988 (def-edebug-spec condition-case
1989 (symbolp
1990 form
1991 &rest ([&or symbolp (&rest symbolp)] body)))
1994 (def-edebug-spec \` (backquote-form))
1996 ;; Supports quotes inside backquotes,
1997 ;; but only at the top level inside unquotes.
1998 (def-edebug-spec backquote-form
1999 (&or
2000 ([&or "," ",@"] &or ("quote" backquote-form) form)
2001 ;; The simple version:
2002 ;; (backquote-form &rest backquote-form)
2003 ;; doesn't handle (a . ,b). The straightforward fix:
2004 ;; (backquote-form . [&or nil backquote-form])
2005 ;; uses up too much stack space.
2006 ;; Note that `(foo . ,@bar) is not valid, so we don't need to handle it.
2007 (backquote-form [&rest [&not ","] backquote-form]
2008 . [&or nil backquote-form])
2009 ;; If you use dotted forms in backquotes, replace the previous line
2010 ;; with the following. This takes quite a bit more stack space, however.
2011 ;; (backquote-form . [&or nil backquote-form])
2012 (vector &rest backquote-form)
2013 sexp))
2015 ;; Special version of backquote that instruments backquoted forms
2016 ;; destined to be evaluated, usually as the result of a
2017 ;; macroexpansion. Backquoted code can only have unquotes (, and ,@)
2018 ;; in places where list forms are allowed, and predicates. If the
2019 ;; backquote is used in a macro, unquoted code that come from
2020 ;; arguments must be instrumented, if at all, with def-form not def-body.
2022 ;; We could assume that all forms (not nested in other forms)
2023 ;; in arguments of macros should be def-forms, whether or not the macros
2024 ;; are defined with edebug-` but this would be expensive.
2026 ;; ,@ might have some problems.
2028 (defalias 'edebug-\` '\`) ;; same macro as regular backquote.
2029 (def-edebug-spec edebug-\` (def-form))
2031 ;; Assume immediate quote in unquotes mean backquote at next higher level.
2032 (def-edebug-spec \, (&or ("quote" edebug-\`) def-form))
2033 (def-edebug-spec \,@ (&define ;; so (,@ form) is never wrapped.
2034 &or ("quote" edebug-\`) def-form))
2036 ;; New byte compiler.
2038 (def-edebug-spec save-selected-window t)
2039 (def-edebug-spec save-current-buffer t)
2041 ;; Anything else?
2043 ;;; The debugger itself
2045 (defvar edebug-active nil) ;; Non-nil when edebug is active
2047 (defvar edebug-stack nil)
2048 ;; Stack of active functions evaluated via edebug.
2049 ;; Should be nil at the top level.
2051 (defvar edebug-stack-depth -1)
2052 ;; Index of last edebug-stack item.
2054 (defvar edebug-offset-indices nil)
2055 ;; Stack of offset indices of visited edebug sexps.
2056 ;; Should be nil at the top level.
2057 ;; Each function adds one cons. Top is modified with setcar.
2060 (defvar edebug-entered nil
2061 ;; Non-nil if edebug has already been entered at this recursive edit level.
2062 ;; This should stay nil at the top level.
2065 ;; Should these be options?
2066 (defconst edebug-debugger 'edebug
2067 ;; Name of function to use for debugging when error or quit occurs.
2068 ;; Set this to 'debug if you want to debug edebug.
2072 ;; Dynamically bound variables, declared globally but left unbound.
2073 (defvar edebug-function) ; the function being executed. change name!!
2074 (defvar edebug-data) ; the edebug data for the function
2075 (defvar edebug-def-mark) ; the mark for the definition
2076 (defvar edebug-freq-count) ; the count of expression visits.
2077 (defvar edebug-coverage) ; the coverage results of each expression of function.
2079 (defvar edebug-buffer) ; which buffer the function is in.
2081 (defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2082 (defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2084 (defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2085 (defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2087 ;;; Handling signals
2089 (defun edebug-signal (signal-name signal-data)
2090 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2091 A signal name is a symbol with an `error-conditions' property
2092 that is a list of condition names.
2093 A handler for any of those names will get to handle this signal.
2094 The symbol `error' should always be one of them.
2096 DATA should be a list. Its elements are printed as part of the error message.
2097 If the signal is handled, DATA is made available to the handler.
2098 See `condition-case'.
2100 This is the Edebug replacement for the standard `signal'. It should
2101 only be active while Edebug is. It checks `debug-on-error' to see
2102 whether it should call the debugger. When execution is resumed, the
2103 error is signaled again."
2104 (if (and (listp debug-on-error) (memq signal-name debug-on-error))
2105 (edebug 'error (cons signal-name signal-data)))
2106 ;; If we reach here without another non-local exit, then send signal again.
2107 ;; i.e. the signal is not continuable, yet.
2108 ;; Avoid infinite recursion.
2109 (let ((signal-hook-function nil))
2110 (signal signal-name signal-data)))
2112 ;;; Entering Edebug
2114 (defun edebug-enter (function args body)
2115 ;; Entering FUNC. The arguments are ARGS, and the body is BODY.
2116 ;; Setup edebug variables and evaluate BODY. This function is called
2117 ;; when a function evaluated with edebug-eval-top-level-form is entered.
2118 ;; Return the result of BODY.
2120 ;; Is this the first time we are entering edebug since
2121 ;; lower-level recursive-edit command?
2122 ;; More precisely, this tests whether Edebug is currently active.
2123 (let ((edebug-function function))
2124 (if (not edebug-entered)
2125 (let ((edebug-entered t)
2126 ;; Binding max-lisp-eval-depth here is OK,
2127 ;; but not inside an unwind-protect.
2128 ;; Doing it here also keeps it from growing too large.
2129 (max-lisp-eval-depth (+ 100 max-lisp-eval-depth)) ; too much??
2130 (max-specpdl-size (+ 200 max-specpdl-size))
2132 (debugger edebug-debugger) ; only while edebug is active.
2133 (edebug-outside-debug-on-error debug-on-error)
2134 (edebug-outside-debug-on-quit debug-on-quit)
2135 ;; Binding these may not be the right thing to do.
2136 ;; We want to allow the global values to be changed.
2137 (debug-on-error (or debug-on-error edebug-on-error))
2138 (debug-on-quit edebug-on-quit))
2139 (unwind-protect
2140 (let ((signal-hook-function 'edebug-signal))
2141 (setq edebug-execution-mode (or edebug-next-execution-mode
2142 edebug-initial-mode
2143 edebug-execution-mode)
2144 edebug-next-execution-mode nil)
2145 (edebug-enter function args body))))
2147 (let* ((edebug-data (get function 'edebug))
2148 (edebug-def-mark (car edebug-data)) ; mark at def start
2149 (edebug-freq-count (get function 'edebug-freq-count))
2150 (edebug-coverage (get function 'edebug-coverage))
2151 (edebug-buffer (marker-buffer edebug-def-mark))
2153 (edebug-stack (cons function edebug-stack))
2154 (edebug-offset-indices (cons 0 edebug-offset-indices))
2156 (if (get function 'edebug-on-entry)
2157 (progn
2158 (setq edebug-execution-mode 'step)
2159 (if (eq (get function 'edebug-on-entry) 'temp)
2160 (put function 'edebug-on-entry nil))))
2161 (if edebug-trace
2162 (edebug--enter-trace function args body)
2163 (funcall body))
2164 ))))
2166 (defun edebug-var-status (var)
2167 "Return a cons cell describing the status of VAR's current binding.
2168 The purpose of this function is so you can properly undo
2169 subsequent changes to the same binding, by passing the status
2170 cons cell to `edebug-restore-status'. The status cons cell
2171 has the form (LOCUS . VALUE), where LOCUS can be a buffer
2172 \(for a buffer-local binding), a frame (for a frame-local binding),
2173 or nil (if the default binding is current)."
2174 (cons (variable-binding-locus var)
2175 (symbol-value var)))
2177 (defun edebug-restore-status (var status)
2178 "Reset VAR based on STATUS.
2179 STATUS should be a list returned by `edebug-var-status'."
2180 (let ((locus (car status))
2181 (value (cdr status)))
2182 (cond ((bufferp locus)
2183 (if (buffer-live-p locus)
2184 (with-current-buffer locus
2185 (set var value))))
2186 ((framep locus)
2187 (modify-frame-parameters locus (list (cons var value))))
2189 (set var value)))))
2191 (defun edebug--enter-trace (function args body)
2192 (let ((edebug-stack-depth (1+ edebug-stack-depth))
2193 edebug-result)
2194 (edebug-print-trace-before
2195 (format "%s args: %s" function args))
2196 (prog1 (setq edebug-result (funcall body))
2197 (edebug-print-trace-after
2198 (format "%s result: %s" function edebug-result)))))
2200 (def-edebug-spec edebug-tracing (form body))
2202 (defmacro edebug-tracing (msg &rest body)
2203 "Print MSG in *edebug-trace* before and after evaluating BODY.
2204 The result of BODY is also printed."
2205 `(let ((edebug-stack-depth (1+ edebug-stack-depth))
2206 edebug-result)
2207 (edebug-print-trace-before ,msg)
2208 (prog1 (setq edebug-result (progn ,@body))
2209 (edebug-print-trace-after
2210 (format "%s result: %s" ,msg edebug-result)))))
2212 (defun edebug-print-trace-before (msg)
2213 "Function called to print trace info before expression evaluation.
2214 MSG is printed after `::::{ '."
2215 (edebug-trace-display
2216 edebug-trace-buffer "%s{ %s" (make-string edebug-stack-depth ?\:) msg))
2218 (defun edebug-print-trace-after (msg)
2219 "Function called to print trace info after expression evaluation.
2220 MSG is printed after `::::} '."
2221 (edebug-trace-display
2222 edebug-trace-buffer "%s} %s" (make-string edebug-stack-depth ?\:) msg))
2226 (defun edebug-slow-before (before-index)
2227 (unless edebug-active
2228 ;; Debug current function given BEFORE position.
2229 ;; Called from functions compiled with edebug-eval-top-level-form.
2230 ;; Return the before index.
2231 (setcar edebug-offset-indices before-index)
2233 ;; Increment frequency count
2234 (aset edebug-freq-count before-index
2235 (1+ (aref edebug-freq-count before-index)))
2237 (if (or (not (memq edebug-execution-mode '(Go-nonstop next)))
2238 (input-pending-p))
2239 (edebug-debugger before-index 'before nil)))
2240 before-index)
2242 (defun edebug-fast-before (_before-index)
2243 ;; Do nothing.
2246 (defun edebug-slow-after (_before-index after-index value)
2247 (if edebug-active
2248 value
2249 ;; Debug current function given AFTER position and VALUE.
2250 ;; Called from functions compiled with edebug-eval-top-level-form.
2251 ;; Return VALUE.
2252 (setcar edebug-offset-indices after-index)
2254 ;; Increment frequency count
2255 (aset edebug-freq-count after-index
2256 (1+ (aref edebug-freq-count after-index)))
2257 (if edebug-test-coverage (edebug--update-coverage after-index value))
2259 (if (and (eq edebug-execution-mode 'Go-nonstop)
2260 (not (input-pending-p)))
2261 ;; Just return result.
2262 value
2263 (edebug-debugger after-index 'after value)
2266 (defun edebug-fast-after (_before-index _after-index value)
2267 ;; Do nothing but return the value.
2268 value)
2270 (defun edebug-run-slow ()
2271 (defalias 'edebug-before 'edebug-slow-before)
2272 (defalias 'edebug-after 'edebug-slow-after))
2274 ;; This is not used, yet.
2275 (defun edebug-run-fast ()
2276 (defalias 'edebug-before 'edebug-fast-before)
2277 (defalias 'edebug-after 'edebug-fast-after))
2279 (edebug-run-slow)
2282 (defun edebug--update-coverage (after-index value)
2283 (let ((old-result (aref edebug-coverage after-index)))
2284 (cond
2285 ((eq 'ok-coverage old-result))
2286 ((eq 'unknown old-result)
2287 (aset edebug-coverage after-index value))
2288 ;; Test if a different result.
2289 ((not (eq value old-result))
2290 (aset edebug-coverage after-index 'ok-coverage)))))
2293 ;; Dynamically declared unbound variables.
2294 (defvar edebug-breakpoints)
2295 (defvar edebug-break-data) ; break data for current function.
2296 (defvar edebug-break) ; whether a break occurred.
2297 (defvar edebug-global-break) ; whether a global break occurred.
2298 (defvar edebug-break-condition) ; whether the breakpoint is conditional.
2300 (defvar edebug-break-result nil)
2301 (defvar edebug-global-break-result nil)
2304 (defun edebug-debugger (offset-index arg-mode value)
2305 (if inhibit-redisplay
2306 ;; Don't really try to enter edebug within an eval from redisplay.
2307 value
2308 ;; Check breakpoints and pending input.
2309 ;; If edebug display should be updated, call edebug--display.
2310 ;; Return value.
2311 (let* ( ;; This needs to be here since breakpoints may be changed.
2312 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
2313 (edebug-break-data (assq offset-index edebug-breakpoints))
2314 (edebug-break-condition (car (cdr edebug-break-data)))
2315 (edebug-global-break
2316 (if edebug-global-break-condition
2317 (condition-case nil
2318 (setq edebug-global-break-result
2319 (edebug-eval edebug-global-break-condition))
2320 (error nil))))
2321 (edebug-break))
2323 ;;(edebug-trace "exp: %s" value)
2324 ;; Test whether we should break.
2325 (setq edebug-break
2326 (or edebug-global-break
2327 (and edebug-break-data
2328 (or (not edebug-break-condition)
2329 (setq edebug-break-result
2330 (edebug-eval edebug-break-condition))))))
2331 (if (and edebug-break
2332 (nth 2 edebug-break-data)) ; is it temporary?
2333 ;; Delete the breakpoint.
2334 (setcdr edebug-data
2335 (cons (delq edebug-break-data edebug-breakpoints)
2336 (cdr (cdr edebug-data)))))
2338 ;; Display if mode is not go, continue, or Continue-fast
2339 ;; or break, or input is pending,
2340 (if (or (not (memq edebug-execution-mode '(go continue Continue-fast)))
2341 edebug-break
2342 (input-pending-p))
2343 (edebug--display value offset-index arg-mode)) ; <---------- display
2345 value)))
2348 ;; window-start now stored with each function.
2349 ;;(defvar edebug-window-start nil)
2350 ;; Remember where each buffers' window starts between edebug calls.
2351 ;; This is to avoid spurious recentering.
2352 ;; Does this still need to be buffer-local??
2353 ;;(setq-default edebug-window-start nil)
2354 ;;(make-variable-buffer-local 'edebug-window-start)
2357 ;; Dynamically declared unbound vars
2358 (defvar edebug-point) ; the point in edebug buffer
2359 (defvar edebug-outside-buffer) ; the current-buffer outside of edebug
2360 (defvar edebug-outside-point) ; the point outside of edebug
2361 (defvar edebug-outside-mark) ; the mark outside of edebug
2362 (defvar edebug-window-data) ; window and window-start for current function
2363 (defvar edebug-outside-windows) ; outside window configuration
2364 (defvar edebug-eval-buffer) ; for the evaluation list.
2365 (defvar edebug-outside-d-c-i-n-s-w) ; outside default-cursor-in-non-selected-windows
2367 (defvar edebug-eval-list nil) ;; List of expressions to evaluate.
2369 (defvar edebug-previous-result nil) ;; Last result returned.
2371 ;; Emacs 19 adds an arg to mark and mark-marker.
2372 (defalias 'edebug-mark-marker 'mark-marker)
2374 (defun edebug--display (value offset-index arg-mode)
2375 ;; edebug--display-1 is too big, we should split it. This function
2376 ;; here was just introduced to avoid making edebug--display-1
2377 ;; yet a bit deeper.
2378 (save-excursion (edebug--display-1 value offset-index arg-mode)))
2380 (defun edebug--display-1 (value offset-index arg-mode)
2381 (unless (marker-position edebug-def-mark)
2382 ;; The buffer holding the source has been killed.
2383 ;; Let's at least show a backtrace so the user can figure out
2384 ;; which function we're talking about.
2385 (debug))
2386 ;; Setup windows for edebug, determine mode, maybe enter recursive-edit.
2387 ;; Uses local variables of edebug-enter, edebug-before, edebug-after
2388 ;; and edebug-debugger.
2389 (let ((edebug-active t) ; For minor mode alist.
2390 (edebug-with-timeout-suspend (with-timeout-suspend))
2391 edebug-stop ; Should we enter recursive-edit?
2392 (edebug-point (+ edebug-def-mark
2393 (aref (nth 2 edebug-data) offset-index)))
2394 edebug-buffer-outside-point ; current point in edebug-buffer
2395 ;; window displaying edebug-buffer
2396 (edebug-window-data (nth 3 edebug-data))
2397 (edebug-outside-window (selected-window))
2398 (edebug-outside-buffer (current-buffer))
2399 (edebug-outside-point (point))
2400 (edebug-outside-mark (edebug-mark))
2401 edebug-outside-windows ; Window or screen configuration.
2402 edebug-buffer-points
2404 edebug-eval-buffer ; Declared here so we can kill it below.
2405 (eval-result-list (and edebug-eval-list
2406 (edebug-eval-result-list)))
2407 edebug-trace-window
2408 edebug-trace-window-start
2410 (edebug-outside-d-c-i-n-s-w
2411 (default-value 'cursor-in-non-selected-windows)))
2412 (unwind-protect
2413 (let ((cursor-in-echo-area nil)
2414 (unread-command-events nil)
2415 ;; any others??
2417 (setq-default cursor-in-non-selected-windows t)
2418 (if (not (buffer-name edebug-buffer))
2419 (user-error "Buffer defining %s not found" edebug-function))
2421 (if (eq 'after arg-mode)
2422 ;; Compute result string now before windows are modified.
2423 (edebug-compute-previous-result value))
2425 (if edebug-save-windows
2426 ;; Save windows now before we modify them.
2427 (setq edebug-outside-windows
2428 (edebug-current-windows edebug-save-windows)))
2430 (if edebug-save-displayed-buffer-points
2431 (setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
2433 ;; First move the edebug buffer point to edebug-point
2434 ;; so that window start doesn't get changed when we display it.
2435 ;; I don't know if this is going to help.
2436 ;;(set-buffer edebug-buffer)
2437 ;;(goto-char edebug-point)
2439 ;; If edebug-buffer is not currently displayed,
2440 ;; first find a window for it.
2441 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data))
2442 (setcar edebug-window-data (selected-window))
2444 ;; Now display eval list, if any.
2445 ;; This is done after the pop to edebug-buffer
2446 ;; so that buffer-window correspondence is correct after quitting.
2447 (edebug-eval-display eval-result-list)
2448 ;; The evaluation list better not have deleted edebug-window-data.
2449 (select-window (car edebug-window-data))
2450 (set-buffer edebug-buffer)
2452 (setq edebug-buffer-outside-point (point))
2453 (goto-char edebug-point)
2455 (if (eq 'before arg-mode)
2456 ;; Check whether positions are up-to-date.
2457 ;; This assumes point is never before symbol.
2458 (if (not (memq (following-char) '(?\( ?\# ?\` )))
2459 (user-error "Source has changed - reevaluate definition of %s"
2460 edebug-function)
2463 ;; Make sure we bind those in the right buffer (bug#16410).
2464 (let ((overlay-arrow-position overlay-arrow-position)
2465 (overlay-arrow-string overlay-arrow-string))
2466 ;; Now display arrow based on mode.
2467 (edebug-overlay-arrow)
2469 (cond
2470 ((eq 'error arg-mode)
2471 ;; Display error message
2472 (setq edebug-execution-mode 'step)
2473 (edebug-overlay-arrow)
2474 (beep)
2475 (if (eq 'quit (car value))
2476 (message "Quit")
2477 (edebug-report-error value)))
2478 (edebug-break
2479 (cond
2480 (edebug-global-break
2481 (message "Global Break: %s => %s"
2482 edebug-global-break-condition
2483 edebug-global-break-result))
2484 (edebug-break-condition
2485 (message "Break: %s => %s"
2486 edebug-break-condition
2487 edebug-break-result))
2488 ((not (eq edebug-execution-mode 'Continue-fast))
2489 (message "Break"))
2490 (t)))
2492 (t (message "")))
2494 (if (eq 'after arg-mode)
2495 (progn
2496 ;; Display result of previous evaluation.
2497 (if (and edebug-break
2498 edebug-sit-on-break
2499 (not (eq edebug-execution-mode 'Continue-fast)))
2500 (sit-for edebug-sit-for-seconds)) ; Show message.
2501 (edebug-previous-result)))
2503 (cond
2504 (edebug-break
2505 (cond
2506 ((eq edebug-execution-mode 'continue)
2507 (sit-for edebug-sit-for-seconds))
2508 ((eq edebug-execution-mode 'Continue-fast) (sit-for 0))
2509 (t (setq edebug-stop t))))
2510 ;; not edebug-break
2511 ((eq edebug-execution-mode 'trace)
2512 (sit-for edebug-sit-for-seconds)) ; Force update and pause.
2513 ((eq edebug-execution-mode 'Trace-fast)
2514 (sit-for 0))) ; Force update and continue.
2516 (when (input-pending-p)
2517 (setq edebug-stop t)
2518 (setq edebug-execution-mode 'step) ; for `edebug-overlay-arrow'
2519 (edebug-stop))
2521 (edebug-overlay-arrow)
2523 (unwind-protect
2524 (if (or edebug-stop
2525 (memq edebug-execution-mode '(step next))
2526 (eq arg-mode 'error))
2527 (edebug--recursive-edit arg-mode)) ; <--- Recursive edit
2529 ;; Reset the edebug-window-data to whatever it is now.
2530 (let ((window (if (eq (window-buffer) edebug-buffer)
2531 (selected-window)
2532 (get-buffer-window edebug-buffer))))
2533 ;; Remember window-start for edebug-buffer, if still displayed.
2534 (if window
2535 (progn
2536 (setcar edebug-window-data window)
2537 (setcdr edebug-window-data (window-start window)))))
2539 ;; Save trace window point before restoring outside windows.
2540 ;; Could generalize this for other buffers.
2541 (setq edebug-trace-window
2542 (get-buffer-window edebug-trace-buffer))
2543 (if edebug-trace-window
2544 (setq edebug-trace-window-start
2545 (and edebug-trace-window
2546 (window-start edebug-trace-window))))
2548 ;; Restore windows before continuing.
2549 (if edebug-save-windows
2550 (progn
2551 (edebug-set-windows edebug-outside-windows)
2553 ;; Restore displayed buffer points.
2554 ;; Needed even if restoring windows because
2555 ;; window-points are not restored. (should they be??)
2556 (if edebug-save-displayed-buffer-points
2557 (edebug-set-buffer-points edebug-buffer-points))
2559 ;; Unrestore trace window's window-point.
2560 (if edebug-trace-window
2561 (set-window-start edebug-trace-window
2562 edebug-trace-window-start))
2564 ;; Unrestore edebug-buffer's window-start, if displayed.
2565 (let ((window (car edebug-window-data)))
2566 (if (and (edebug-window-live-p window)
2567 (eq (window-buffer) edebug-buffer))
2568 (progn
2569 (set-window-start window (cdr edebug-window-data)
2570 'no-force)
2571 ;; Unrestore edebug-buffer's window-point.
2572 ;; Needed in addition to setting the buffer point
2573 ;; - otherwise quitting doesn't leave point as is.
2574 ;; But can this causes point to not be restored.
2575 ;; Also, it may not be a visible window.
2576 ;; (set-window-point window edebug-point)
2579 ;; Unrestore edebug-buffer's point. Rerestored below.
2580 ;; (goto-char edebug-point) ;; in edebug-buffer
2582 ;; Since we may be in a save-excursion, in case of quit,
2583 ;; reselect the outside window only.
2584 ;; Only needed if we are not recovering windows??
2585 (if (edebug-window-live-p edebug-outside-window)
2586 (select-window edebug-outside-window))
2587 ) ; if edebug-save-windows
2589 ;; Restore current buffer always, in case application needs it.
2590 (if (buffer-name edebug-outside-buffer)
2591 (set-buffer edebug-outside-buffer))
2592 ;; Restore point, and mark.
2593 ;; Needed even if restoring windows because
2594 ;; that doesn't restore point and mark in the current buffer.
2595 ;; But don't restore point if edebug-buffer is current buffer.
2596 (if (not (eq edebug-buffer edebug-outside-buffer))
2597 (goto-char edebug-outside-point))
2598 (if (marker-buffer (edebug-mark-marker))
2599 ;; Does zmacs-regions need to be nil while doing set-marker?
2600 (set-marker (edebug-mark-marker) edebug-outside-mark))
2601 )) ; unwind-protect
2602 ;; None of the following is done if quit or signal occurs.
2604 ;; Restore edebug-buffer's outside point.
2605 ;; (edebug-trace "restore edebug-buffer point: %s"
2606 ;; edebug-buffer-outside-point)
2607 (with-current-buffer edebug-buffer
2608 (goto-char edebug-buffer-outside-point))
2609 ;; ... nothing more.
2611 ;; Could be an option to keep eval display up.
2612 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
2613 (with-timeout-unsuspend edebug-with-timeout-suspend)
2614 ;; Reset global variables to outside values in case they were changed.
2615 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
2619 (defvar edebug-number-of-recursions 0)
2620 ;; Number of recursive edits started by edebug.
2621 ;; Should be 0 at the top level.
2623 (defvar edebug-recursion-depth 0)
2624 ;; Value of recursion-depth when edebug was called.
2626 ;; Dynamically declared unbound vars
2627 (defvar edebug-outside-match-data) ; match data outside of edebug
2628 (defvar edebug-backtrace-buffer) ; each recursive edit gets its own
2629 (defvar edebug-inside-windows)
2630 (defvar edebug-interactive-p)
2632 (defun edebug--recursive-edit (arg-mode)
2633 ;; Start up a recursive edit inside of edebug.
2634 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
2635 ;; Assume that none of the variables below are buffer-local.
2636 (let (;; match-data must be done in the outside buffer
2637 (edebug-outside-match-data
2638 (with-current-buffer edebug-outside-buffer ; in case match buffer different
2639 (match-data)))
2641 ;;(edebug-number-of-recursions (1+ edebug-number-of-recursions))
2642 (edebug-recursion-depth (recursion-depth))
2643 edebug-entered ; bind locally to nil
2644 (edebug-interactive-p nil) ; again non-interactive
2645 edebug-backtrace-buffer ; each recursive edit gets its own
2646 ;; The window configuration may be saved and restored
2647 ;; during a recursive-edit
2648 edebug-inside-windows
2651 (unwind-protect
2652 (let (
2653 ;; Declare global values local but using the same global value.
2654 ;; We could set these to the values for previous edebug call.
2655 (last-command last-command)
2656 (this-command this-command)
2657 (current-prefix-arg nil)
2659 ;; More for Emacs 19
2660 (last-input-event nil)
2661 (last-command-event nil)
2662 (last-event-frame nil)
2663 (last-nonmenu-event nil)
2664 (track-mouse nil)
2666 (standard-output t)
2667 (standard-input t)
2669 ;; Don't keep reading from an executing kbd macro
2670 ;; within edebug unless edebug-continue-kbd-macro is
2671 ;; non-nil. Again, local binding may not be best.
2672 (executing-kbd-macro
2673 (if edebug-continue-kbd-macro executing-kbd-macro))
2675 ;; Don't get confused by the user's keymap changes.
2676 (overriding-local-map nil)
2677 (overriding-terminal-local-map nil)
2679 ;; Bind again to outside values.
2680 (debug-on-error edebug-outside-debug-on-error)
2681 (debug-on-quit edebug-outside-debug-on-quit)
2683 ;; Don't keep defining a kbd macro.
2684 (defining-kbd-macro
2685 (if edebug-continue-kbd-macro defining-kbd-macro))
2687 ;; others??
2690 (if (and (eq edebug-execution-mode 'go)
2691 (not (memq arg-mode '(after error))))
2692 (message "Break"))
2694 (setq signal-hook-function nil)
2696 (edebug-mode 1)
2697 (unwind-protect
2698 (recursive-edit) ; <<<<<<<<<< Recursive edit
2700 ;; Do the following, even if quit occurs.
2701 (setq signal-hook-function 'edebug-signal)
2702 (if edebug-backtrace-buffer
2703 (kill-buffer edebug-backtrace-buffer))
2705 ;; Remember selected-window after recursive-edit.
2706 ;; (setq edebug-inside-window (selected-window))
2708 (set-match-data edebug-outside-match-data)
2710 ;; Recursive edit may have changed buffers,
2711 ;; so set it back before exiting let.
2712 (if (buffer-name edebug-buffer) ; if it still exists
2713 (progn
2714 (set-buffer edebug-buffer)
2715 (when (memq edebug-execution-mode '(go Go-nonstop))
2716 (edebug-overlay-arrow)
2717 (sit-for 0))
2718 (edebug-mode -1))
2719 ;; gotta have a buffer to let its buffer local variables be set
2720 (get-buffer-create " bogus edebug buffer"))
2721 ));; inner let
2725 ;;; Display related functions
2727 (defconst edebug-arrow-alist
2728 '((Continue-fast . "=")
2729 (Trace-fast . "-")
2730 (continue . ">")
2731 (trace . "->")
2732 (step . "=>")
2733 (next . "=>")
2734 (go . "<>")
2735 (Go-nonstop . "..")
2737 "Association list of arrows for each edebug mode.")
2739 (defun edebug-overlay-arrow ()
2740 ;; Set up the overlay arrow at beginning-of-line in current buffer.
2741 ;; The arrow string is derived from edebug-arrow-alist and
2742 ;; edebug-execution-mode.
2743 (let ((pos (line-beginning-position)))
2744 (setq overlay-arrow-string
2745 (cdr (assq edebug-execution-mode edebug-arrow-alist)))
2746 (setq overlay-arrow-position (make-marker))
2747 (set-marker overlay-arrow-position pos (current-buffer))))
2750 (defun edebug-toggle-save-all-windows ()
2751 "Toggle the saving and restoring of all windows.
2752 Also, each time you toggle it on, the inside and outside window
2753 configurations become the same as the current configuration."
2754 (interactive)
2755 (setq edebug-save-windows (not edebug-save-windows))
2756 (if edebug-save-windows
2757 (setq edebug-inside-windows
2758 (setq edebug-outside-windows
2759 (edebug-current-windows
2760 edebug-save-windows))))
2761 (message "Window saving is %s for all windows."
2762 (if edebug-save-windows "on" "off")))
2764 (defmacro edebug-changing-windows (&rest body)
2765 `(let ((window (selected-window)))
2766 (setq edebug-inside-windows (edebug-current-windows t))
2767 (edebug-set-windows edebug-outside-windows)
2768 ,@body;; Code to change edebug-save-windows
2769 (setq edebug-outside-windows (edebug-current-windows
2770 edebug-save-windows))
2771 ;; Problem: what about outside windows that are deleted inside?
2772 (edebug-set-windows edebug-inside-windows)))
2774 (defun edebug-toggle-save-selected-window ()
2775 "Toggle the saving and restoring of the selected window.
2776 Also, each time you toggle it on, the inside and outside window
2777 configurations become the same as the current configuration."
2778 (interactive)
2779 (cond
2780 ((eq t edebug-save-windows)
2781 ;; Save all outside windows except the selected one.
2782 ;; Remove (selected-window) from outside-windows.
2783 (edebug-changing-windows
2784 (setq edebug-save-windows (delq window (edebug-window-list)))))
2786 ((memq (selected-window) edebug-save-windows)
2787 (setq edebug-outside-windows
2788 (delq (assq (selected-window) edebug-outside-windows)
2789 edebug-outside-windows))
2790 (setq edebug-save-windows
2791 (delq (selected-window) edebug-save-windows)))
2792 (t ; Save a new window.
2793 (edebug-changing-windows
2794 (setq edebug-save-windows (cons window edebug-save-windows)))))
2796 (message "Window saving is %s for %s."
2797 (if (memq (selected-window) edebug-save-windows)
2798 "on" "off")
2799 (selected-window)))
2801 (defun edebug-toggle-save-windows (arg)
2802 "Toggle the saving and restoring of windows.
2803 With prefix, toggle for just the selected window.
2804 Otherwise, toggle for all windows."
2805 (interactive "P")
2806 (if arg
2807 (edebug-toggle-save-selected-window)
2808 (edebug-toggle-save-all-windows)))
2810 (defun edebug-where ()
2811 "Show the debug windows and where we stopped in the program."
2812 (interactive)
2813 (if (not edebug-active)
2814 (error "Edebug is not active"))
2815 ;; Restore the window configuration to what it last was inside.
2816 ;; But it is not always set. - experiment
2817 ;;(if edebug-inside-windows
2818 ;; (edebug-set-windows edebug-inside-windows))
2819 (edebug-pop-to-buffer edebug-buffer)
2820 (goto-char edebug-point))
2822 (defun edebug-view-outside ()
2823 "Change to the outside window configuration.
2824 Use `edebug-where' to return."
2825 (interactive)
2826 (if (not edebug-active)
2827 (error "Edebug is not active"))
2828 (setq edebug-inside-windows
2829 (edebug-current-windows edebug-save-windows))
2830 (edebug-set-windows edebug-outside-windows)
2831 (goto-char edebug-outside-point)
2832 (message "Window configuration outside of Edebug. Return with %s"
2833 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
2836 (defun edebug-bounce-point (arg)
2837 "Bounce the point in the outside current buffer.
2838 If prefix argument ARG is supplied, sit for that many seconds
2839 before returning. The default is one second."
2840 (interactive "p")
2841 (if (not edebug-active)
2842 (error "Edebug is not active"))
2843 (save-excursion
2844 ;; If the buffer's currently displayed, avoid set-window-configuration.
2845 (save-window-excursion
2846 (edebug-pop-to-buffer edebug-outside-buffer)
2847 (goto-char edebug-outside-point)
2848 (message "Current buffer: %s Point: %s Mark: %s"
2849 (current-buffer) (point)
2850 (if (marker-buffer (edebug-mark-marker))
2851 (marker-position (edebug-mark-marker)) "<not set>"))
2852 (sit-for arg)
2853 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data)))))
2856 ;; Joe Wells, here is a start at your idea of adding a buffer to the internal
2857 ;; display list. Still need to use this list in edebug--display.
2859 '(defvar edebug-display-buffer-list nil
2860 "List of buffers that edebug will display when it is active.")
2862 '(defun edebug-display-buffer (buffer)
2863 "Toggle display of a buffer inside of edebug."
2864 (interactive "bBuffer: ")
2865 (let ((already-displaying (memq buffer edebug-display-buffer-list)))
2866 (setq edebug-display-buffer-list
2867 (if already-displaying
2868 (delq buffer edebug-display-buffer-list)
2869 (cons buffer edebug-display-buffer-list)))
2870 (message "Displaying %s %s" buffer
2871 (if already-displaying "off" "on"))))
2873 ;;; Breakpoint related functions
2875 (defun edebug-find-stop-point ()
2876 ;; Return (function . index) of the nearest edebug stop point.
2877 (let* ((edebug-def-name (edebug-form-data-symbol))
2878 (edebug-data
2879 (let ((data (get edebug-def-name 'edebug)))
2880 (if (or (null data) (markerp data))
2881 (error "%s is not instrumented for Edebug" edebug-def-name))
2882 data)) ; we could do it automatically, if data is a marker.
2883 ;; pull out parts of edebug-data.
2884 (edebug-def-mark (car edebug-data))
2885 ;; (edebug-breakpoints (car (cdr edebug-data)))
2887 (offset-vector (nth 2 edebug-data))
2888 (offset (- (save-excursion
2889 (if (looking-at "[ \t]")
2890 ;; skip backwards until non-whitespace, or bol
2891 (skip-chars-backward " \t"))
2892 (point))
2893 edebug-def-mark))
2894 len i)
2895 ;; the offsets are in order so we can do a linear search
2896 (setq len (length offset-vector))
2897 (setq i 0)
2898 (while (and (< i len) (> offset (aref offset-vector i)))
2899 (setq i (1+ i)))
2900 (if (and (< i len)
2901 (<= offset (aref offset-vector i)))
2902 ;; return the relevant info
2903 (cons edebug-def-name i)
2904 (message "Point is not on an expression in %s."
2905 edebug-def-name)
2909 (defun edebug-next-breakpoint ()
2910 "Move point to the next breakpoint, or first if none past point."
2911 (interactive)
2912 (let ((edebug-stop-point (edebug-find-stop-point)))
2913 (if edebug-stop-point
2914 (let* ((edebug-def-name (car edebug-stop-point))
2915 (index (cdr edebug-stop-point))
2916 (edebug-data (get edebug-def-name 'edebug))
2918 ;; pull out parts of edebug-data
2919 (edebug-def-mark (car edebug-data))
2920 (edebug-breakpoints (car (cdr edebug-data)))
2921 (offset-vector (nth 2 edebug-data))
2922 breakpoint)
2923 (if (not edebug-breakpoints)
2924 (message "No breakpoints in this function.")
2925 (let ((breaks edebug-breakpoints))
2926 (while (and breaks
2927 (<= (car (car breaks)) index))
2928 (setq breaks (cdr breaks)))
2929 (setq breakpoint
2930 (if breaks
2931 (car breaks)
2932 ;; goto the first breakpoint
2933 (car edebug-breakpoints)))
2934 (goto-char (+ edebug-def-mark
2935 (aref offset-vector (car breakpoint))))
2937 (message "%s"
2938 (concat (if (nth 2 breakpoint)
2939 "Temporary " "")
2940 (if (car (cdr breakpoint))
2941 (format "Condition: %s"
2942 (edebug-safe-prin1-to-string
2943 (car (cdr breakpoint))))
2944 "")))
2945 ))))))
2948 (defun edebug-modify-breakpoint (flag &optional condition temporary)
2949 "Modify the breakpoint for the form at point or after it.
2950 Set it if FLAG is non-nil, clear it otherwise. Then move to that point.
2951 If CONDITION or TEMPORARY are non-nil, add those attributes to
2952 the breakpoint."
2953 (let ((edebug-stop-point (edebug-find-stop-point)))
2954 (if edebug-stop-point
2955 (let* ((edebug-def-name (car edebug-stop-point))
2956 (index (cdr edebug-stop-point))
2957 (edebug-data (get edebug-def-name 'edebug))
2959 ;; pull out parts of edebug-data
2960 (edebug-def-mark (car edebug-data))
2961 (edebug-breakpoints (car (cdr edebug-data)))
2962 (offset-vector (nth 2 edebug-data))
2963 present)
2964 ;; delete it either way
2965 (setq present (assq index edebug-breakpoints))
2966 (setq edebug-breakpoints (delq present edebug-breakpoints))
2967 (if flag
2968 (progn
2969 ;; add it to the list and resort
2970 (setq edebug-breakpoints
2971 (edebug-sort-alist
2972 (cons
2973 (list index condition temporary)
2974 edebug-breakpoints) '<))
2975 (if condition
2976 (message "Breakpoint set in %s with condition: %s"
2977 edebug-def-name condition)
2978 (message "Breakpoint set in %s" edebug-def-name)))
2979 (if present
2980 (message "Breakpoint unset in %s" edebug-def-name)
2981 (message "No breakpoint here")))
2983 (setcar (cdr edebug-data) edebug-breakpoints)
2984 (goto-char (+ edebug-def-mark (aref offset-vector index)))
2985 ))))
2987 (defun edebug-set-breakpoint (arg)
2988 "Set the breakpoint of nearest sexp.
2989 With prefix argument, make it a temporary breakpoint."
2990 (interactive "P")
2991 (edebug-modify-breakpoint t nil arg))
2993 (defun edebug-unset-breakpoint ()
2994 "Clear the breakpoint of nearest sexp."
2995 (interactive)
2996 (edebug-modify-breakpoint nil))
2999 (defun edebug-set-global-break-condition (expression)
3000 "Set `edebug-global-break-condition' to EXPRESSION."
3001 (interactive
3002 (list
3003 (let ((initial (and edebug-global-break-condition
3004 (format "%s" edebug-global-break-condition))))
3005 (read-from-minibuffer
3006 "Global Condition: " initial read-expression-map t
3007 (if (equal (car read-expression-history) initial)
3008 '(read-expression-history . 1)
3009 'read-expression-history)))))
3010 (setq edebug-global-break-condition expression))
3013 ;;; Mode switching functions
3015 (defun edebug-set-mode (mode shortmsg msg)
3016 ;; Set the edebug mode to MODE.
3017 ;; Display SHORTMSG, or MSG if not within edebug.
3018 (if (eq (1+ edebug-recursion-depth) (recursion-depth))
3019 (progn
3020 (setq edebug-execution-mode mode)
3021 (message "%s" shortmsg)
3022 ;; Continue execution
3023 (exit-recursive-edit))
3024 ;; This is not terribly useful!!
3025 (setq edebug-next-execution-mode mode)
3026 (message "%s" msg)))
3029 (defalias 'edebug-step-through-mode 'edebug-step-mode)
3031 (defun edebug-step-mode ()
3032 "Proceed to next stop point."
3033 (interactive)
3034 (edebug-set-mode 'step "" "Edebug will stop at next stop point."))
3036 (defun edebug-next-mode ()
3037 "Proceed to next `after' stop point."
3038 (interactive)
3039 (edebug-set-mode 'next "" "Edebug will stop after next eval."))
3041 (defun edebug-go-mode (arg)
3042 "Go, evaluating until break.
3043 With prefix ARG, set temporary break at current point and go."
3044 (interactive "P")
3045 (if arg
3046 (edebug-set-breakpoint t))
3047 (edebug-set-mode 'go "Go..." "Edebug will go until break."))
3049 (defun edebug-Go-nonstop-mode ()
3050 "Go, evaluating without debugging.
3051 You can use `edebug-stop', or any editing command, to stop."
3052 (interactive)
3053 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
3054 "Edebug will not stop at breaks."))
3057 (defun edebug-trace-mode ()
3058 "Begin trace mode.
3059 Pauses for `edebug-sit-for-seconds' at each stop point."
3060 (interactive)
3061 (edebug-set-mode 'trace "Tracing..." "Edebug will trace with pause."))
3063 (defun edebug-Trace-fast-mode ()
3064 "Trace with no wait at each step.
3065 Updates the display at each stop point, but does not pause."
3066 (interactive)
3067 (edebug-set-mode 'Trace-fast
3068 "Trace fast..." "Edebug will trace without pause."))
3070 (defun edebug-continue-mode ()
3071 "Begin continue mode.
3072 Pauses for `edebug-sit-for-seconds' at each break point."
3073 (interactive)
3074 (edebug-set-mode 'continue "Continue..."
3075 "Edebug will pause at breakpoints."))
3077 (defun edebug-Continue-fast-mode ()
3078 "Trace with no wait at each step.
3079 Updates the display at each break point, but does not pause."
3080 (interactive)
3081 (edebug-set-mode 'Continue-fast "Continue fast..."
3082 "Edebug will stop and go at breakpoints."))
3084 ;; ------------------------------------------------------------
3085 ;; The following use the mode changing commands and breakpoints.
3088 (defun edebug-goto-here ()
3089 "Proceed to first stop-point at or after current position of point."
3090 (interactive)
3091 (edebug-go-mode t))
3094 (defun edebug-stop ()
3095 "Stop execution and do not continue.
3096 Useful for exiting from trace or continue loop."
3097 (interactive)
3098 (message "Stop"))
3101 '(defun edebug-forward ()
3102 "Proceed to the exit of the next expression to be evaluated."
3103 (interactive)
3104 (edebug-set-mode
3105 'forward "Forward"
3106 "Edebug will stop after exiting the next expression."))
3109 (defun edebug-forward-sexp (arg)
3110 "Proceed from the current point to the end of the ARGth sexp ahead.
3111 If there are not ARG sexps ahead, then do `edebug-step-out'."
3112 (interactive "p")
3113 (condition-case nil
3114 (let ((parse-sexp-ignore-comments t))
3115 ;; Call forward-sexp repeatedly until done or failure.
3116 (forward-sexp arg)
3117 (edebug-go-mode t))
3118 (error
3119 (edebug-step-out)
3122 (defun edebug-step-out ()
3123 "Proceed from the current point to the end of the containing sexp.
3124 If there is no containing sexp that is not the top level defun,
3125 go to the end of the last sexp, or if that is the same point, then step."
3126 (interactive)
3127 (condition-case nil
3128 (let ((parse-sexp-ignore-comments t))
3129 (up-list 1)
3130 (save-excursion
3131 ;; Is there still a containing expression?
3132 (up-list 1))
3133 (edebug-go-mode t))
3134 (error
3135 ;; At top level - 1, so first check if there are more sexps at this level.
3136 (let ((start-point (point)))
3137 ;; (up-list 1)
3138 (down-list -1)
3139 (if (= (point) start-point)
3140 (edebug-step-mode) ; No more at this level, so step.
3141 (edebug-go-mode t)
3142 )))))
3144 (defun edebug-instrument-function (func)
3145 ;; Func should be a function symbol.
3146 ;; Return the function symbol, or nil if not instrumented.
3147 (let ((func-marker (get func 'edebug)))
3148 (cond
3149 ((and (markerp func-marker) (marker-buffer func-marker))
3150 ;; It is uninstrumented, so instrument it.
3151 (with-current-buffer (marker-buffer func-marker)
3152 (goto-char func-marker)
3153 (edebug-eval-top-level-form)
3154 func))
3155 ((consp func-marker)
3156 (message "%s is already instrumented." func)
3157 func)
3159 (let ((loc (find-function-noselect func t)))
3160 (unless (cdr loc)
3161 (error "Could not find the definition in its file"))
3162 (with-current-buffer (car loc)
3163 (goto-char (cdr loc))
3164 (edebug-eval-top-level-form)
3165 func))))))
3167 (defun edebug-instrument-callee ()
3168 "Instrument the definition of the function or macro about to be called.
3169 Do this when stopped before the form or it will be too late.
3170 One side effect of using this command is that the next time the
3171 function or macro is called, Edebug will be called there as well."
3172 (interactive)
3173 (if (not (looking-at "("))
3174 (error "You must be before a list form")
3175 (let ((func
3176 (save-excursion
3177 (down-list 1)
3178 (if (looking-at "(")
3179 (edebug--form-data-name
3180 (edebug-get-form-data-entry (point)))
3181 (read (current-buffer))))))
3182 (edebug-instrument-function func))))
3185 (defun edebug-step-in ()
3186 "Step into the definition of the function or macro about to be called.
3187 This first does `edebug-instrument-callee' to ensure that it is
3188 instrumented. Then it does `edebug-on-entry' and switches to `go' mode."
3189 (interactive)
3190 (let ((func (edebug-instrument-callee)))
3191 (if func
3192 (progn
3193 (edebug-on-entry func 'temp)
3194 (edebug-go-mode nil)))))
3196 (defun edebug-on-entry (function &optional flag)
3197 "Cause Edebug to stop when FUNCTION is called.
3198 With prefix argument, make this temporary so it is automatically
3199 canceled the first time the function is entered."
3200 (interactive "aEdebug on entry to: \nP")
3201 ;; Could store this in the edebug data instead.
3202 (put function 'edebug-on-entry (if flag 'temp t)))
3204 (defun cancel-edebug-on-entry (function)
3205 (interactive "aEdebug on entry to: ")
3206 (put function 'edebug-on-entry nil))
3209 '(advice-add 'debug-on-entry :around 'edebug--debug-on-entry) ;; Should we do this?
3210 ;; Also need edebug-cancel-debug-on-entry
3212 '(defun edebug--debug-on-entry (orig function)
3213 "If the function is instrumented for Edebug, call `edebug-on-entry'."
3214 (let ((func-data (get function 'edebug)))
3215 (if (or (null func-data) (markerp func-data))
3216 (funcall orig function)
3217 (edebug-on-entry function))))
3220 (defun edebug-top-level-nonstop ()
3221 "Set mode to Go-nonstop, and exit to top-level.
3222 This is useful for exiting even if `unwind-protect' code may be executed."
3223 (interactive)
3224 (setq edebug-execution-mode 'Go-nonstop)
3225 (top-level))
3227 ;;(defun edebug-exit-out ()
3228 ;; "Go until the current function exits."
3229 ;; (interactive)
3230 ;; (edebug-set-mode 'exiting "Exit..."))
3232 (defconst edebug-initial-mode-alist
3233 '((edebug-step-mode . step)
3234 (edebug-next-mode . next)
3235 (edebug-trace-mode . trace)
3236 (edebug-Trace-fast-mode . Trace-fast)
3237 (edebug-go-mode . go)
3238 (edebug-continue-mode . continue)
3239 (edebug-Continue-fast-mode . Continue-fast)
3240 (edebug-Go-nonstop-mode . Go-nonstop))
3241 "Association list between commands and the modes they set.")
3243 (defvar edebug-mode-map) ; will be defined fully later.
3245 (defun edebug-set-initial-mode ()
3246 "Set the initial execution mode of Edebug.
3247 The mode is requested via the key that would be used to set the mode in
3248 edebug-mode."
3249 (interactive)
3250 (let* ((old-mode edebug-initial-mode)
3251 (key (read-key-sequence
3252 (format
3253 "Change initial edebug mode from %s (%c) to (enter key): "
3254 old-mode
3255 (aref (where-is-internal
3256 (car (rassq old-mode edebug-initial-mode-alist))
3257 edebug-mode-map 'firstonly)
3258 0))))
3259 (mode (cdr (assq (lookup-key edebug-mode-map key)
3260 edebug-initial-mode-alist))))
3261 (if mode
3262 (progn
3263 (setq edebug-initial-mode mode)
3264 (message "Edebug's initial mode is now: %s" mode))
3265 (error "Key must map to one of the mode changing commands"))))
3267 ;;; Evaluation of expressions
3269 (defmacro edebug-outside-excursion (&rest body)
3270 "Evaluate an expression list in the outside context.
3271 Return the result of the last expression."
3272 ;; Only restores the non-variables context since all the variables let-bound
3273 ;; by Edebug will be properly reset to the appropriate context's value by
3274 ;; backtrace-eval.
3275 (declare (debug t))
3276 `(save-excursion ; of current-buffer
3277 (if edebug-save-windows
3278 (progn
3279 ;; After excursion, we will
3280 ;; restore to current window configuration.
3281 (setq edebug-inside-windows
3282 (edebug-current-windows edebug-save-windows))
3283 ;; Restore outside windows.
3284 (edebug-set-windows edebug-outside-windows)))
3286 (set-buffer edebug-buffer) ; why?
3287 (set-match-data edebug-outside-match-data)
3288 ;; Restore outside context.
3289 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
3290 (unwind-protect
3291 ;; FIXME: This restoring of edebug-outside-buffer and
3292 ;; edebug-outside-point is redundant now that backtrace-eval does it
3293 ;; for us.
3294 (with-current-buffer edebug-outside-buffer ; of edebug-buffer
3295 (goto-char edebug-outside-point)
3296 (if (marker-buffer (edebug-mark-marker))
3297 (set-marker (edebug-mark-marker) edebug-outside-mark))
3298 ,@body)
3300 ;; Back to edebug-buffer. Restore rest of inside context.
3301 ;; (use-local-map edebug-inside-map)
3302 (if edebug-save-windows
3303 ;; Restore inside windows.
3304 (edebug-set-windows edebug-inside-windows))
3306 ;; Save values that may have been changed.
3307 (setq edebug-outside-d-c-i-n-s-w
3308 (default-value 'cursor-in-non-selected-windows))
3310 ;; Restore the outside saved values; don't alter
3311 ;; the outside binding loci.
3312 (setq-default cursor-in-non-selected-windows t))))
3314 (defun edebug-eval (expr)
3315 (backtrace-eval expr 0 'edebug-after))
3317 (defun edebug-safe-eval (expr)
3318 ;; Evaluate EXPR safely.
3319 ;; If there is an error, a string is returned describing the error.
3320 (condition-case edebug-err
3321 (edebug-eval expr)
3322 (error (edebug-format "%s: %s" ;; could
3323 (get (car edebug-err) 'error-message)
3324 (car (cdr edebug-err))))))
3326 ;;; Printing
3329 (defun edebug-report-error (value)
3330 ;; Print an error message like command level does.
3331 ;; This also prints the error name if it has no error-message.
3332 (message "%s: %s"
3333 (or (get (car value) 'error-message)
3334 (format "peculiar error (%s)" (car value)))
3335 (mapconcat (function (lambda (edebug-arg)
3336 ;; continuing after an error may
3337 ;; complain about edebug-arg. why??
3338 (prin1-to-string edebug-arg)))
3339 (cdr value) ", ")))
3341 (defvar print-readably) ; defined by lemacs
3342 ;; Alternatively, we could change the definition of
3343 ;; edebug-safe-prin1-to-string to only use these if defined.
3345 (defun edebug-safe-prin1-to-string (value)
3346 (let ((print-escape-newlines t)
3347 (print-length (or edebug-print-length print-length))
3348 (print-level (or edebug-print-level print-level))
3349 (print-circle (or edebug-print-circle print-circle))
3350 (print-readably nil)) ; lemacs uses this.
3351 (edebug-prin1-to-string value)))
3353 (defun edebug-compute-previous-result (previous-value)
3354 (if edebug-unwrap-results
3355 (setq previous-value
3356 (edebug-unwrap* previous-value)))
3357 (setq edebug-previous-result
3358 (concat "Result: "
3359 (edebug-safe-prin1-to-string previous-value)
3360 (eval-expression-print-format previous-value))))
3362 (defun edebug-previous-result ()
3363 "Print the previous result."
3364 (interactive)
3365 (message "%s" edebug-previous-result))
3367 ;;; Read, Eval and Print
3369 (defalias 'edebug-prin1 'prin1)
3370 (defalias 'edebug-print 'print)
3371 (defalias 'edebug-prin1-to-string 'prin1-to-string)
3372 (defalias 'edebug-format 'format-message)
3373 (defalias 'edebug-message 'message)
3375 (defun edebug-eval-expression (expr)
3376 "Evaluate an expression in the outside environment.
3377 If interactive, prompt for the expression.
3378 Print result in minibuffer."
3379 (interactive (list (read-from-minibuffer
3380 "Eval: " nil read-expression-map t
3381 'read-expression-history)))
3382 (princ
3383 (edebug-outside-excursion
3384 (setq values (cons (edebug-eval expr) values))
3385 (concat (edebug-safe-prin1-to-string (car values))
3386 (eval-expression-print-format (car values))))))
3388 (defun edebug-eval-last-sexp ()
3389 "Evaluate sexp before point in the outside environment.
3390 Print value in minibuffer."
3391 (interactive)
3392 (edebug-eval-expression (edebug-last-sexp)))
3394 (defun edebug-eval-print-last-sexp ()
3395 "Evaluate sexp before point in outside environment; insert value.
3396 This prints the value into current buffer."
3397 (interactive)
3398 (let* ((form (edebug-last-sexp))
3399 (result-string
3400 (edebug-outside-excursion
3401 (edebug-safe-prin1-to-string (edebug-safe-eval form))))
3402 (standard-output (current-buffer)))
3403 (princ "\n")
3404 ;; princ the string to get rid of quotes.
3405 (princ result-string)
3406 (princ "\n")
3409 ;;; Edebug Minor Mode
3411 (defvar edebug-inhibit-emacs-lisp-mode-bindings nil
3412 "If non-nil, inhibit Edebug bindings on the C-x C-a key.
3413 By default, loading the `edebug' library causes these bindings to
3414 be installed in `emacs-lisp-mode-map'.")
3416 (define-obsolete-variable-alias 'gud-inhibit-global-bindings
3417 'edebug-inhibit-emacs-lisp-mode-bindings "24.3")
3419 ;; Global GUD bindings for all emacs-lisp-mode buffers.
3420 (unless edebug-inhibit-emacs-lisp-mode-bindings
3421 (define-key emacs-lisp-mode-map "\C-x\C-a\C-s" 'edebug-step-mode)
3422 (define-key emacs-lisp-mode-map "\C-x\C-a\C-n" 'edebug-next-mode)
3423 (define-key emacs-lisp-mode-map "\C-x\C-a\C-c" 'edebug-go-mode)
3424 (define-key emacs-lisp-mode-map "\C-x\C-a\C-l" 'edebug-where)
3425 ;; The following isn't a GUD binding.
3426 (define-key emacs-lisp-mode-map "\C-x\C-a\C-m" 'edebug-set-initial-mode))
3428 (defvar edebug-mode-map
3429 (let ((map (copy-keymap emacs-lisp-mode-map)))
3430 ;; control
3431 (define-key map " " 'edebug-step-mode)
3432 (define-key map "n" 'edebug-next-mode)
3433 (define-key map "g" 'edebug-go-mode)
3434 (define-key map "G" 'edebug-Go-nonstop-mode)
3435 (define-key map "t" 'edebug-trace-mode)
3436 (define-key map "T" 'edebug-Trace-fast-mode)
3437 (define-key map "c" 'edebug-continue-mode)
3438 (define-key map "C" 'edebug-Continue-fast-mode)
3440 ;;(define-key map "f" 'edebug-forward) not implemented
3441 (define-key map "f" 'edebug-forward-sexp)
3442 (define-key map "h" 'edebug-goto-here)
3444 (define-key map "I" 'edebug-instrument-callee)
3445 (define-key map "i" 'edebug-step-in)
3446 (define-key map "o" 'edebug-step-out)
3448 ;; quitting and stopping
3449 (define-key map "q" 'top-level)
3450 (define-key map "Q" 'edebug-top-level-nonstop)
3451 (define-key map "a" 'abort-recursive-edit)
3452 (define-key map "S" 'edebug-stop)
3454 ;; breakpoints
3455 (define-key map "b" 'edebug-set-breakpoint)
3456 (define-key map "u" 'edebug-unset-breakpoint)
3457 (define-key map "B" 'edebug-next-breakpoint)
3458 (define-key map "x" 'edebug-set-conditional-breakpoint)
3459 (define-key map "X" 'edebug-set-global-break-condition)
3461 ;; evaluation
3462 (define-key map "r" 'edebug-previous-result)
3463 (define-key map "e" 'edebug-eval-expression)
3464 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3465 (define-key map "E" 'edebug-visit-eval-list)
3467 ;; views
3468 (define-key map "w" 'edebug-where)
3469 (define-key map "v" 'edebug-view-outside) ;; maybe obsolete??
3470 (define-key map "p" 'edebug-bounce-point)
3471 (define-key map "P" 'edebug-view-outside) ;; same as v
3472 (define-key map "W" 'edebug-toggle-save-windows)
3474 ;; misc
3475 (define-key map "?" 'edebug-help)
3476 (define-key map "d" 'edebug-backtrace)
3478 (define-key map "-" 'negative-argument)
3480 ;; statistics
3481 (define-key map "=" 'edebug-temp-display-freq-count)
3483 ;; GUD bindings
3484 (define-key map "\C-c\C-s" 'edebug-step-mode)
3485 (define-key map "\C-c\C-n" 'edebug-next-mode)
3486 (define-key map "\C-c\C-c" 'edebug-go-mode)
3488 (define-key map "\C-x " 'edebug-set-breakpoint)
3489 (define-key map "\C-c\C-d" 'edebug-unset-breakpoint)
3490 (define-key map "\C-c\C-t"
3491 (lambda () (interactive) (edebug-set-breakpoint t)))
3492 (define-key map "\C-c\C-l" 'edebug-where)
3493 map))
3495 ;; Autoloading these global bindings doesn't make sense because
3496 ;; they cannot be used anyway unless Edebug is already loaded and active.
3498 (defvar global-edebug-prefix "\^XX"
3499 "Prefix key for global edebug commands, available from any buffer.")
3501 (defvar global-edebug-map
3502 (let ((map (make-sparse-keymap)))
3504 (define-key map " " 'edebug-step-mode)
3505 (define-key map "g" 'edebug-go-mode)
3506 (define-key map "G" 'edebug-Go-nonstop-mode)
3507 (define-key map "t" 'edebug-trace-mode)
3508 (define-key map "T" 'edebug-Trace-fast-mode)
3509 (define-key map "c" 'edebug-continue-mode)
3510 (define-key map "C" 'edebug-Continue-fast-mode)
3512 ;; breakpoints
3513 (define-key map "b" 'edebug-set-breakpoint)
3514 (define-key map "u" 'edebug-unset-breakpoint)
3515 (define-key map "x" 'edebug-set-conditional-breakpoint)
3516 (define-key map "X" 'edebug-set-global-break-condition)
3518 ;; views
3519 (define-key map "w" 'edebug-where)
3520 (define-key map "W" 'edebug-toggle-save-windows)
3522 ;; quitting
3523 (define-key map "q" 'top-level)
3524 (define-key map "Q" 'edebug-top-level-nonstop)
3525 (define-key map "a" 'abort-recursive-edit)
3527 ;; statistics
3528 (define-key map "=" 'edebug-display-freq-count)
3529 map)
3530 "Global map of edebug commands, available from any buffer.")
3532 (global-unset-key global-edebug-prefix)
3533 (global-set-key global-edebug-prefix global-edebug-map)
3536 (defun edebug-help ()
3537 "Describe `edebug-mode'."
3538 (interactive)
3539 (describe-function 'edebug-mode))
3541 (defvar edebug--mode-saved-vars nil)
3543 (define-minor-mode edebug-mode
3544 "Mode for Emacs Lisp buffers while in Edebug.
3546 In addition to all Emacs Lisp commands (except those that modify the
3547 buffer) there are local and global key bindings to several Edebug
3548 specific commands. E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
3549 in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3551 Also see bindings for the eval list buffer *edebug* in `edebug-eval-mode'.
3553 The edebug buffer commands:
3554 \\{edebug-mode-map}
3556 Global commands prefixed by `global-edebug-prefix':
3557 \\{global-edebug-map}
3559 Options:
3560 `edebug-setup-hook'
3561 `edebug-all-defs'
3562 `edebug-all-forms'
3563 `edebug-save-windows'
3564 `edebug-save-displayed-buffer-points'
3565 `edebug-initial-mode'
3566 `edebug-trace'
3567 `edebug-test-coverage'
3568 `edebug-continue-kbd-macro'
3569 `edebug-print-length'
3570 `edebug-print-level'
3571 `edebug-print-circle'
3572 `edebug-on-error'
3573 `edebug-on-quit'
3574 `edebug-on-signal'
3575 `edebug-unwrap-results'
3576 `edebug-global-break-condition'"
3577 :lighter " *Debugging*"
3578 :keymap edebug-mode-map
3579 ;; If the user kills the buffer in which edebug is currently active,
3580 ;; exit to top level, because the edebug command loop can't usefully
3581 ;; continue running in such a case.
3583 (if (not edebug-mode)
3584 (progn
3585 (while edebug--mode-saved-vars
3586 (let ((setting (pop edebug--mode-saved-vars)))
3587 (if (consp setting)
3588 (set (car setting) (cdr setting))
3589 (kill-local-variable setting))))
3590 (remove-hook 'kill-buffer-hook 'edebug-kill-buffer t))
3591 (pcase-dolist (`(,var . ,val) '((buffer-read-only . t)))
3592 (push
3593 (if (local-variable-p var) (cons var (symbol-value var)) var)
3594 edebug--mode-saved-vars)
3595 (set (make-local-variable var) val))
3596 ;; Append `edebug-kill-buffer' to the hook to avoid interfering with
3597 ;; other entries that are unguarded against deleted buffer.
3598 (add-hook 'kill-buffer-hook 'edebug-kill-buffer t t)))
3600 (defun edebug-kill-buffer ()
3601 "Used on `kill-buffer-hook' when Edebug is operating in a buffer of Lisp code."
3602 (run-with-timer 0 nil #'top-level))
3604 ;;; edebug eval list mode
3606 ;; A list of expressions and their evaluations is displayed in *edebug*.
3608 (defun edebug-eval-result-list ()
3609 "Return a list of evaluations of `edebug-eval-list'."
3610 ;; Assumes in outside environment.
3611 ;; Don't do any edebug things now.
3612 (let ((edebug-execution-mode 'Go-nonstop)
3613 (edebug-trace nil))
3614 (mapcar 'edebug-safe-eval edebug-eval-list)))
3616 (defun edebug-eval-display-list (eval-result-list)
3617 ;; Assumes edebug-eval-buffer exists.
3618 (let ((standard-output edebug-eval-buffer)
3619 (edebug-comment-line
3620 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
3621 (set-buffer edebug-eval-buffer)
3622 (erase-buffer)
3623 (dolist (exp edebug-eval-list)
3624 (prin1 exp) (terpri)
3625 (prin1 (pop eval-result-list)) (terpri)
3626 (princ edebug-comment-line))
3627 (edebug-pop-to-buffer edebug-eval-buffer)
3630 (defun edebug-create-eval-buffer ()
3631 (unless (and edebug-eval-buffer (buffer-name edebug-eval-buffer))
3632 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
3633 (edebug-eval-mode)))
3635 ;; Should generalize this to be callable outside of edebug
3636 ;; with calls in user functions, e.g. (edebug-eval-display)
3638 (defun edebug-eval-display (eval-result-list)
3639 "Display expressions and evaluations in EVAL-RESULT-LIST.
3640 It modifies the context by popping up the eval display."
3641 (when eval-result-list
3642 (edebug-create-eval-buffer)
3643 (edebug-eval-display-list eval-result-list)))
3645 (defun edebug-eval-redisplay ()
3646 "Redisplay eval list in outside environment.
3647 May only be called from within `edebug--recursive-edit'."
3648 (edebug-create-eval-buffer)
3649 (edebug-outside-excursion
3650 (edebug-eval-display-list (edebug-eval-result-list))
3653 (defun edebug-visit-eval-list ()
3654 "Switch to the evaluation list buffer \"*edebug*\"."
3655 (interactive)
3656 (edebug-eval-redisplay)
3657 (edebug-pop-to-buffer edebug-eval-buffer))
3660 (defun edebug-update-eval-list ()
3661 "Replace the evaluation list with the sexps now in the eval buffer."
3662 (interactive)
3663 (let ((starting-point (point))
3664 new-list)
3665 (goto-char (point-min))
3666 ;; get the first expression
3667 (edebug-skip-whitespace)
3668 (if (not (eobp))
3669 (progn
3670 (forward-sexp 1)
3671 (push (edebug-last-sexp) new-list)))
3673 (while (re-search-forward "^;" nil t)
3674 (forward-line 1)
3675 (skip-chars-forward " \t\n\r")
3676 (if (and (/= ?\; (following-char))
3677 (not (eobp)))
3678 (progn
3679 (forward-sexp 1)
3680 (push (edebug-last-sexp) new-list))))
3682 (setq edebug-eval-list (nreverse new-list))
3683 (edebug-eval-redisplay)
3684 (goto-char starting-point)))
3687 (defun edebug-delete-eval-item ()
3688 "Delete the item under point and redisplay."
3689 ;; could add arg to do repeatedly
3690 (interactive)
3691 (if (re-search-backward "^;" nil 'nofail)
3692 (forward-line 1))
3693 (delete-region
3694 (point) (progn (re-search-forward "^;" nil 'nofail)
3695 (beginning-of-line)
3696 (point)))
3697 (edebug-update-eval-list))
3701 (defvar edebug-eval-mode-map
3702 (let ((map (make-sparse-keymap)))
3703 (set-keymap-parent map lisp-interaction-mode-map)
3704 (define-key map "\C-c\C-w" 'edebug-where)
3705 (define-key map "\C-c\C-d" 'edebug-delete-eval-item)
3706 (define-key map "\C-c\C-u" 'edebug-update-eval-list)
3707 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3708 (define-key map "\C-j" 'edebug-eval-print-last-sexp)
3709 map)
3710 "Keymap for Edebug Eval mode. Superset of Lisp Interaction mode.")
3712 (put 'edebug-eval-mode 'mode-class 'special)
3714 (define-derived-mode edebug-eval-mode lisp-interaction-mode "Edebug Eval"
3715 "Mode for evaluation list buffer while in Edebug.
3717 In addition to all Interactive Emacs Lisp commands there are local and
3718 global key bindings to several Edebug specific commands. E.g.
3719 `edebug-step-mode' is bound to \\[edebug-step-mode] in the Edebug
3720 buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3722 Eval list buffer commands:
3723 \\{edebug-eval-mode-map}
3725 Global commands prefixed by `global-edebug-prefix':
3726 \\{global-edebug-map}")
3728 ;;; Interface with standard debugger.
3730 ;; (setq debugger 'edebug) ; to use the edebug debugger
3731 ;; (setq debugger 'debug) ; use the standard debugger
3733 ;; Note that debug and its utilities must be byte-compiled to work,
3734 ;; since they depend on the backtrace looking a certain way. But
3735 ;; edebug is not dependent on this, yet.
3737 (defun edebug (&optional arg-mode &rest args)
3738 "Replacement for `debug'.
3739 If we are running an edebugged function, show where we last were.
3740 Otherwise call `debug' normally."
3741 ;;(message "entered: %s depth: %s edebug-recursion-depth: %s"
3742 ;; edebug-entered (recursion-depth) edebug-recursion-depth) (sit-for 1)
3743 (if (and edebug-entered ; anything active?
3744 (eq (recursion-depth) edebug-recursion-depth))
3745 (let (;; Where were we before the error occurred?
3746 (offset-index (car edebug-offset-indices))
3747 (value (car args))
3748 ;; Bind variables required by edebug--display.
3749 edebug-breakpoints
3750 edebug-break-data
3751 edebug-break-condition
3752 edebug-global-break
3753 (edebug-break (null arg-mode)) ;; If called explicitly.
3755 (edebug--display value offset-index arg-mode)
3756 (if (eq arg-mode 'error)
3758 value))
3760 ;; Otherwise call debug normally.
3761 ;; Still need to remove extraneous edebug calls from stack.
3762 (apply 'debug arg-mode args)
3766 (defun edebug-backtrace ()
3767 "Display a non-working backtrace. Better than nothing..."
3768 (interactive)
3769 (if (or (not edebug-backtrace-buffer)
3770 (null (buffer-name edebug-backtrace-buffer)))
3771 (setq edebug-backtrace-buffer
3772 (generate-new-buffer "*Backtrace*"))
3773 ;; Else, could just display edebug-backtrace-buffer.
3775 (with-output-to-temp-buffer (buffer-name edebug-backtrace-buffer)
3776 (setq edebug-backtrace-buffer standard-output)
3777 (let ((print-escape-newlines t)
3778 (print-length 50) ; FIXME cf edebug-safe-prin1-to-string
3779 last-ok-point)
3780 (backtrace)
3782 ;; Clean up the backtrace.
3783 ;; Not quite right for current edebug scheme.
3784 (set-buffer edebug-backtrace-buffer)
3785 (setq truncate-lines t)
3786 (goto-char (point-min))
3787 (setq last-ok-point (point))
3788 (if t (progn
3790 ;; Delete interspersed edebug internals.
3791 (while (re-search-forward "^ (?edebug" nil t)
3792 (beginning-of-line)
3793 (cond
3794 ((looking-at "^ (edebug-after")
3795 ;; Previous lines may contain code, so just delete this line.
3796 (setq last-ok-point (point))
3797 (forward-line 1)
3798 (delete-region last-ok-point (point)))
3800 ((looking-at "^ edebug")
3801 (forward-line 1)
3802 (delete-region last-ok-point (point))
3804 )))))
3807 ;;; Trace display
3809 (defun edebug-trace-display (buf-name fmt &rest args)
3810 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
3811 The buffer is created if it does not exist.
3812 You must include newlines in FMT to break lines, but one newline is appended."
3813 ;; e.g.
3814 ;; (edebug-trace-display "*trace-point*"
3815 ;; "saving: point = %s window-start = %s"
3816 ;; (point) (window-start))
3817 (let* ((oldbuf (current-buffer))
3818 (selected-window (selected-window))
3819 (buffer (get-buffer-create buf-name))
3820 buf-window)
3821 ;; (message "before pop-to-buffer") (sit-for 1)
3822 (edebug-pop-to-buffer buffer)
3823 (setq truncate-lines t)
3824 (setq buf-window (selected-window))
3825 (goto-char (point-max))
3826 (insert (apply 'edebug-format fmt args) "\n")
3827 ;; Make it visible.
3828 (vertical-motion (- 1 (window-height)))
3829 (set-window-start buf-window (point))
3830 (goto-char (point-max))
3831 ;; (set-window-point buf-window (point))
3832 ;; (sit-for 0)
3833 (bury-buffer buffer)
3834 (select-window selected-window)
3835 (set-buffer oldbuf))
3836 buf-name)
3839 (defun edebug-trace (fmt &rest args)
3840 "Convenience call to `edebug-trace-display' using `edebug-trace-buffer'."
3841 (apply 'edebug-trace-display edebug-trace-buffer fmt args))
3844 ;;; Frequency count and coverage
3846 ;; FIXME should this use overlays instead?
3847 ;; Definitely, IMO. The current business with undo in
3848 ;; edebug-temp-display-freq-count is horrid.
3849 (defun edebug-display-freq-count ()
3850 "Display the frequency count data for each line of the current definition.
3851 The frequency counts are inserted as comment lines after each line,
3852 and you can undo all insertions with one `undo' command.
3854 The counts are inserted starting under the `(' before an expression
3855 or the `)' after an expression, or on the last char of a symbol.
3856 The counts are only displayed when they differ from previous counts on
3857 the same line.
3859 If coverage is being tested, whenever all known results of an expression
3860 are `eq', the char `=' will be appended after the count
3861 for that expression. Note that this is always the case for an
3862 expression only evaluated once.
3864 To clear the frequency count and coverage data for a definition,
3865 reinstrument it."
3866 (interactive)
3867 (let* ((function (edebug-form-data-symbol))
3868 (counts (get function 'edebug-freq-count))
3869 (coverages (get function 'edebug-coverage))
3870 (data (get function 'edebug))
3871 (def-mark (car data)) ; mark at def start
3872 (edebug-points (nth 2 data))
3873 (i (1- (length edebug-points)))
3874 (last-index)
3875 (first-index)
3876 (start-of-line)
3877 (start-of-count-line)
3878 (last-count)
3880 (save-excursion
3881 ;; Traverse in reverse order so offsets are correct.
3882 (while (<= 0 i)
3883 ;; Start at last expression in line.
3884 (goto-char (+ def-mark (aref edebug-points i)))
3885 (beginning-of-line)
3886 (setq start-of-line (- (point) def-mark)
3887 last-index i)
3889 ;; Find all indexes on same line.
3890 (while (and (<= 0 (setq i (1- i)))
3891 (<= start-of-line (aref edebug-points i))))
3892 ;; Insert all the indices for this line.
3893 (forward-line 1)
3894 (setq start-of-count-line (point)
3895 first-index i ; Really, last index for line above this one.
3896 last-count -1) ; Cause first count to always appear.
3897 (insert ";#")
3898 ;; i == first-index still
3899 (while (<= (setq i (1+ i)) last-index)
3900 (let ((count (aref counts i))
3901 (coverage (aref coverages i))
3902 (col (save-excursion
3903 (goto-char (+ (aref edebug-points i) def-mark))
3904 (- (current-column)
3905 (if (= ?\( (following-char)) 0 1)))))
3906 (insert (make-string
3907 (max 0 (- col (- (point) start-of-count-line))) ?\s)
3908 (if (and (< 0 count)
3909 (not (memq coverage
3910 '(unknown ok-coverage))))
3911 "=" "")
3912 (if (= count last-count) "" (int-to-string count))
3913 " ")
3914 (setq last-count count)))
3915 (insert "\n")
3916 (setq i first-index)))))
3918 ;; FIXME this does not work very well. Eg if you press an arrow key,
3919 ;; or make a mouse-click, it fails with "Non-character input-event".
3920 (defun edebug-temp-display-freq-count ()
3921 "Temporarily display the frequency count data for the current definition.
3922 It is removed when you hit any char."
3923 ;; This seems not to work with Emacs 18.59. It undoes too far.
3924 (interactive)
3925 (let ((inhibit-read-only t))
3926 (undo-boundary)
3927 (edebug-display-freq-count)
3928 (setq unread-command-events
3929 (append unread-command-events (list (read-event))))
3930 ;; Yuck! This doesn't seem to work at all for me.
3931 (undo)))
3934 ;;; Menus
3936 (defun edebug-toggle (variable)
3937 (set variable (not (symbol-value variable)))
3938 (message "%s: %s" variable (symbol-value variable)))
3940 ;; We have to require easymenu (even for Emacs 18) just so
3941 ;; the easy-menu-define macro call is compiled correctly.
3942 (require 'easymenu)
3944 (defconst edebug-mode-menus
3945 '("Edebug"
3946 ["Stop" edebug-stop t]
3947 ["Step" edebug-step-mode t]
3948 ["Next" edebug-next-mode t]
3949 ["Trace" edebug-trace-mode t]
3950 ["Trace Fast" edebug-Trace-fast-mode t]
3951 ["Continue" edebug-continue-mode t]
3952 ["Continue Fast" edebug-Continue-fast-mode t]
3953 ["Go" edebug-go-mode t]
3954 ["Go Nonstop" edebug-Go-nonstop-mode t]
3955 "----"
3956 ["Help" edebug-help t]
3957 ["Abort" abort-recursive-edit t]
3958 ["Quit to Top Level" top-level t]
3959 ["Quit Nonstop" edebug-top-level-nonstop t]
3960 "----"
3961 ("Jumps"
3962 ["Forward Sexp" edebug-forward-sexp t]
3963 ["Step In" edebug-step-in t]
3964 ["Step Out" edebug-step-out t]
3965 ["Goto Here" edebug-goto-here t])
3967 ("Breaks"
3968 ["Set Breakpoint" edebug-set-breakpoint t]
3969 ["Unset Breakpoint" edebug-unset-breakpoint t]
3970 ["Set Conditional Breakpoint" edebug-set-conditional-breakpoint t]
3971 ["Set Global Break Condition" edebug-set-global-break-condition t]
3972 ["Show Next Breakpoint" edebug-next-breakpoint t])
3974 ("Views"
3975 ["Where am I?" edebug-where t]
3976 ["Bounce to Current Point" edebug-bounce-point t]
3977 ["View Outside Windows" edebug-view-outside t]
3978 ["Previous Result" edebug-previous-result t]
3979 ["Show Backtrace" edebug-backtrace t]
3980 ["Display Freq Count" edebug-display-freq-count t])
3982 ("Eval"
3983 ["Expression" edebug-eval-expression t]
3984 ["Last Sexp" edebug-eval-last-sexp t]
3985 ["Visit Eval List" edebug-visit-eval-list t])
3987 ("Options"
3988 ["Edebug All Defs" edebug-all-defs
3989 :style toggle :selected edebug-all-defs]
3990 ["Edebug All Forms" edebug-all-forms
3991 :style toggle :selected edebug-all-forms]
3992 "----"
3993 ["Tracing" (edebug-toggle 'edebug-trace)
3994 :style toggle :selected edebug-trace]
3995 ["Test Coverage" (edebug-toggle 'edebug-test-coverage)
3996 :style toggle :selected edebug-test-coverage]
3997 ["Save Windows" edebug-toggle-save-windows
3998 :style toggle :selected edebug-save-windows]
3999 ["Save Point"
4000 (edebug-toggle 'edebug-save-displayed-buffer-points)
4001 :style toggle :selected edebug-save-displayed-buffer-points]
4003 "Menus for Edebug.")
4006 ;;; Emacs version specific code
4008 (defalias 'edebug-window-live-p 'window-live-p)
4010 (defun edebug-mark ()
4011 (mark t))
4013 (defun edebug-set-conditional-breakpoint (arg condition)
4014 "Set a conditional breakpoint at nearest sexp.
4015 The condition is evaluated in the outside context.
4016 With prefix argument, make it a temporary breakpoint."
4017 ;; (interactive "P\nxCondition: ")
4018 (interactive
4019 (list
4020 current-prefix-arg
4021 ;; Read condition as follows; getting previous condition is cumbersome:
4022 (let ((edebug-stop-point (edebug-find-stop-point)))
4023 (if edebug-stop-point
4024 (let* ((edebug-def-name (car edebug-stop-point))
4025 (index (cdr edebug-stop-point))
4026 (edebug-data (get edebug-def-name 'edebug))
4027 (edebug-breakpoints (car (cdr edebug-data)))
4028 (edebug-break-data (assq index edebug-breakpoints))
4029 (edebug-break-condition (car (cdr edebug-break-data)))
4030 (initial (and edebug-break-condition
4031 (format "%s" edebug-break-condition))))
4032 (read-from-minibuffer
4033 "Condition: " initial read-expression-map t
4034 (if (equal (car read-expression-history) initial)
4035 '(read-expression-history . 1)
4036 'read-expression-history)))))))
4037 (edebug-modify-breakpoint t condition arg))
4039 (easy-menu-define edebug-menu edebug-mode-map "Edebug menus" edebug-mode-menus)
4041 ;;; Autoloading of Edebug accessories
4043 ;; edebug-cl-read and cl-read are available from liberte@cs.uiuc.edu
4044 (defun edebug--require-cl-read ()
4045 (require 'edebug-cl-read))
4047 (if (featurep 'cl-read)
4048 (add-hook 'edebug-setup-hook #'edebug--require-cl-read)
4049 ;; The following causes edebug-cl-read to be loaded when you load cl-read.el.
4050 (add-hook 'cl-read-load-hooks #'edebug--require-cl-read))
4053 ;;; Finalize Loading
4055 ;; When edebugging a function, some of the sub-expressions are
4056 ;; wrapped in (edebug-enter (lambda () ..)), so we need to teach
4057 ;; called-interactively-p that calls within the inner lambda should refer to
4058 ;; the outside function.
4059 (add-hook 'called-interactively-p-functions
4060 #'edebug--called-interactively-skip)
4061 (defun edebug--called-interactively-skip (i frame1 frame2)
4062 (when (and (eq (car-safe (nth 1 frame1)) 'lambda)
4063 (eq (nth 1 (nth 1 frame1)) '())
4064 (eq (nth 1 frame2) 'edebug-enter))
4065 ;; `edebug-enter' calls itself on its first invocation.
4066 (if (eq (nth 1 (backtrace-frame i 'called-interactively-p))
4067 'edebug-enter)
4068 2 1)))
4070 ;; Finally, hook edebug into the rest of Emacs.
4071 ;; There are probably some other things that could go here.
4073 ;; Install edebug read and eval functions.
4074 (edebug-install-read-eval-functions)
4076 (defun edebug-unload-function ()
4077 "Unload the Edebug source level debugger."
4078 (when edebug-active
4079 (setq edebug-active nil)
4080 (unwind-protect
4081 (abort-recursive-edit)
4082 ;; We still want to run unload-feature to completion
4083 (run-with-idle-timer 0 nil #'(lambda () (unload-feature 'edebug)))))
4084 (remove-hook 'called-interactively-p-functions
4085 'edebug--called-interactively-skip)
4086 (remove-hook 'cl-read-load-hooks 'edebug--require-cl-read)
4087 (edebug-uninstall-read-eval-functions)
4088 ;; Continue standard unloading.
4089 nil)
4091 (provide 'edebug)
4092 ;;; edebug.el ends here