; Merge from origin/emacs-25
[emacs.git] / lisp / emacs-lisp / edebug.el
blob1a00c45447ca6544cb236d2209bfd740142f23a8
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 ("declare" &rest sexp)]
1937 [&optional ("interactive" interactive)]
1938 def-body))
1939 (def-edebug-spec defmacro
1940 ;; FIXME: Improve `declare' so we can Edebug gv-expander and
1941 ;; gv-setter declarations.
1942 (&define name lambda-list [&optional stringp]
1943 [&optional ("declare" &rest sexp)] def-body))
1945 (def-edebug-spec arglist lambda-list) ;; deprecated - use lambda-list.
1947 (def-edebug-spec lambda-list
1948 (([&rest arg]
1949 [&optional ["&optional" arg &rest arg]]
1950 &optional ["&rest" arg]
1953 (def-edebug-spec interactive
1954 (&optional &or stringp def-form))
1956 ;; A function-form is for an argument that may be a function or a form.
1957 ;; This specially recognizes anonymous functions quoted with quote.
1958 (def-edebug-spec function-form
1959 ;; form at the end could also handle "function",
1960 ;; but recognize it specially to avoid wrapping function forms.
1961 (&or ([&or "quote" "function"] &or symbolp lambda-expr) form))
1963 ;; function expects a symbol or a lambda or macro expression
1964 ;; A macro is allowed by Emacs.
1965 (def-edebug-spec function (&or symbolp lambda-expr))
1967 ;; A macro expression is a lambda expression with "macro" prepended.
1968 (def-edebug-spec macro (&define "lambda" lambda-list def-body))
1970 ;; (def-edebug-spec anonymous-form ((&or ["lambda" lambda] ["macro" macro])))
1972 ;; Standard functions that take function-forms arguments.
1974 ;; FIXME? The manual uses this form (maybe that's just for illustration?):
1975 ;; (def-edebug-spec let
1976 ;; ((&rest &or symbolp (gate symbolp &optional form))
1977 ;; body))
1978 (def-edebug-spec let
1979 ((&rest &or (symbolp &optional form) symbolp)
1980 body))
1982 (def-edebug-spec let* let)
1984 (def-edebug-spec setq (&rest symbolp form))
1985 (def-edebug-spec setq-default setq)
1987 (def-edebug-spec cond (&rest (&rest form)))
1989 (def-edebug-spec condition-case
1990 (symbolp
1991 form
1992 &rest ([&or symbolp (&rest symbolp)] body)))
1995 (def-edebug-spec \` (backquote-form))
1997 ;; Supports quotes inside backquotes,
1998 ;; but only at the top level inside unquotes.
1999 (def-edebug-spec backquote-form
2000 (&or
2001 ([&or "," ",@"] &or ("quote" backquote-form) form)
2002 ;; The simple version:
2003 ;; (backquote-form &rest backquote-form)
2004 ;; doesn't handle (a . ,b). The straightforward fix:
2005 ;; (backquote-form . [&or nil backquote-form])
2006 ;; uses up too much stack space.
2007 ;; Note that `(foo . ,@bar) is not valid, so we don't need to handle it.
2008 (backquote-form [&rest [&not ","] backquote-form]
2009 . [&or nil backquote-form])
2010 ;; If you use dotted forms in backquotes, replace the previous line
2011 ;; with the following. This takes quite a bit more stack space, however.
2012 ;; (backquote-form . [&or nil backquote-form])
2013 (vector &rest backquote-form)
2014 sexp))
2016 ;; Special version of backquote that instruments backquoted forms
2017 ;; destined to be evaluated, usually as the result of a
2018 ;; macroexpansion. Backquoted code can only have unquotes (, and ,@)
2019 ;; in places where list forms are allowed, and predicates. If the
2020 ;; backquote is used in a macro, unquoted code that come from
2021 ;; arguments must be instrumented, if at all, with def-form not def-body.
2023 ;; We could assume that all forms (not nested in other forms)
2024 ;; in arguments of macros should be def-forms, whether or not the macros
2025 ;; are defined with edebug-` but this would be expensive.
2027 ;; ,@ might have some problems.
2029 (defalias 'edebug-\` '\`) ;; same macro as regular backquote.
2030 (def-edebug-spec edebug-\` (def-form))
2032 ;; Assume immediate quote in unquotes mean backquote at next higher level.
2033 (def-edebug-spec \, (&or ("quote" edebug-\`) def-form))
2034 (def-edebug-spec \,@ (&define ;; so (,@ form) is never wrapped.
2035 &or ("quote" edebug-\`) def-form))
2037 ;; New byte compiler.
2039 (def-edebug-spec save-selected-window t)
2040 (def-edebug-spec save-current-buffer t)
2042 ;; Anything else?
2044 ;;; The debugger itself
2046 (defvar edebug-active nil) ;; Non-nil when edebug is active
2048 (defvar edebug-stack nil)
2049 ;; Stack of active functions evaluated via edebug.
2050 ;; Should be nil at the top level.
2052 (defvar edebug-stack-depth -1)
2053 ;; Index of last edebug-stack item.
2055 (defvar edebug-offset-indices nil)
2056 ;; Stack of offset indices of visited edebug sexps.
2057 ;; Should be nil at the top level.
2058 ;; Each function adds one cons. Top is modified with setcar.
2061 (defvar edebug-entered nil
2062 ;; Non-nil if edebug has already been entered at this recursive edit level.
2063 ;; This should stay nil at the top level.
2066 ;; Should these be options?
2067 (defconst edebug-debugger 'edebug
2068 ;; Name of function to use for debugging when error or quit occurs.
2069 ;; Set this to 'debug if you want to debug edebug.
2073 ;; Dynamically bound variables, declared globally but left unbound.
2074 (defvar edebug-function) ; the function being executed. change name!!
2075 (defvar edebug-data) ; the edebug data for the function
2076 (defvar edebug-def-mark) ; the mark for the definition
2077 (defvar edebug-freq-count) ; the count of expression visits.
2078 (defvar edebug-coverage) ; the coverage results of each expression of function.
2080 (defvar edebug-buffer) ; which buffer the function is in.
2082 (defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2083 (defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2085 (defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2086 (defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2088 ;;; Handling signals
2090 (defun edebug-signal (signal-name signal-data)
2091 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2092 A signal name is a symbol with an `error-conditions' property
2093 that is a list of condition names.
2094 A handler for any of those names will get to handle this signal.
2095 The symbol `error' should always be one of them.
2097 DATA should be a list. Its elements are printed as part of the error message.
2098 If the signal is handled, DATA is made available to the handler.
2099 See `condition-case'.
2101 This is the Edebug replacement for the standard `signal'. It should
2102 only be active while Edebug is. It checks `debug-on-error' to see
2103 whether it should call the debugger. When execution is resumed, the
2104 error is signaled again."
2105 (if (and (listp debug-on-error) (memq signal-name debug-on-error))
2106 (edebug 'error (cons signal-name signal-data)))
2107 ;; If we reach here without another non-local exit, then send signal again.
2108 ;; i.e. the signal is not continuable, yet.
2109 ;; Avoid infinite recursion.
2110 (let ((signal-hook-function nil))
2111 (signal signal-name signal-data)))
2113 ;;; Entering Edebug
2115 (defun edebug-enter (function args body)
2116 ;; Entering FUNC. The arguments are ARGS, and the body is BODY.
2117 ;; Setup edebug variables and evaluate BODY. This function is called
2118 ;; when a function evaluated with edebug-eval-top-level-form is entered.
2119 ;; Return the result of BODY.
2121 ;; Is this the first time we are entering edebug since
2122 ;; lower-level recursive-edit command?
2123 ;; More precisely, this tests whether Edebug is currently active.
2124 (let ((edebug-function function))
2125 (if (not edebug-entered)
2126 (let ((edebug-entered t)
2127 ;; Binding max-lisp-eval-depth here is OK,
2128 ;; but not inside an unwind-protect.
2129 ;; Doing it here also keeps it from growing too large.
2130 (max-lisp-eval-depth (+ 100 max-lisp-eval-depth)) ; too much??
2131 (max-specpdl-size (+ 200 max-specpdl-size))
2133 (debugger edebug-debugger) ; only while edebug is active.
2134 (edebug-outside-debug-on-error debug-on-error)
2135 (edebug-outside-debug-on-quit debug-on-quit)
2136 ;; Binding these may not be the right thing to do.
2137 ;; We want to allow the global values to be changed.
2138 (debug-on-error (or debug-on-error edebug-on-error))
2139 (debug-on-quit edebug-on-quit))
2140 (unwind-protect
2141 (let ((signal-hook-function 'edebug-signal))
2142 (setq edebug-execution-mode (or edebug-next-execution-mode
2143 edebug-initial-mode
2144 edebug-execution-mode)
2145 edebug-next-execution-mode nil)
2146 (edebug-enter function args body))))
2148 (let* ((edebug-data (get function 'edebug))
2149 (edebug-def-mark (car edebug-data)) ; mark at def start
2150 (edebug-freq-count (get function 'edebug-freq-count))
2151 (edebug-coverage (get function 'edebug-coverage))
2152 (edebug-buffer (marker-buffer edebug-def-mark))
2154 (edebug-stack (cons function edebug-stack))
2155 (edebug-offset-indices (cons 0 edebug-offset-indices))
2157 (if (get function 'edebug-on-entry)
2158 (progn
2159 (setq edebug-execution-mode 'step)
2160 (if (eq (get function 'edebug-on-entry) 'temp)
2161 (put function 'edebug-on-entry nil))))
2162 (if edebug-trace
2163 (edebug--enter-trace function args body)
2164 (funcall body))
2165 ))))
2167 (defun edebug-var-status (var)
2168 "Return a cons cell describing the status of VAR's current binding.
2169 The purpose of this function is so you can properly undo
2170 subsequent changes to the same binding, by passing the status
2171 cons cell to `edebug-restore-status'. The status cons cell
2172 has the form (LOCUS . VALUE), where LOCUS can be a buffer
2173 \(for a buffer-local binding), a frame (for a frame-local binding),
2174 or nil (if the default binding is current)."
2175 (cons (variable-binding-locus var)
2176 (symbol-value var)))
2178 (defun edebug-restore-status (var status)
2179 "Reset VAR based on STATUS.
2180 STATUS should be a list returned by `edebug-var-status'."
2181 (let ((locus (car status))
2182 (value (cdr status)))
2183 (cond ((bufferp locus)
2184 (if (buffer-live-p locus)
2185 (with-current-buffer locus
2186 (set var value))))
2187 ((framep locus)
2188 (modify-frame-parameters locus (list (cons var value))))
2190 (set var value)))))
2192 (defun edebug--enter-trace (function args body)
2193 (let ((edebug-stack-depth (1+ edebug-stack-depth))
2194 edebug-result)
2195 (edebug-print-trace-before
2196 (format "%s args: %s" function args))
2197 (prog1 (setq edebug-result (funcall body))
2198 (edebug-print-trace-after
2199 (format "%s result: %s" function edebug-result)))))
2201 (def-edebug-spec edebug-tracing (form body))
2203 (defmacro edebug-tracing (msg &rest body)
2204 "Print MSG in *edebug-trace* before and after evaluating BODY.
2205 The result of BODY is also printed."
2206 `(let ((edebug-stack-depth (1+ edebug-stack-depth))
2207 edebug-result)
2208 (edebug-print-trace-before ,msg)
2209 (prog1 (setq edebug-result (progn ,@body))
2210 (edebug-print-trace-after
2211 (format "%s result: %s" ,msg edebug-result)))))
2213 (defun edebug-print-trace-before (msg)
2214 "Function called to print trace info before expression evaluation.
2215 MSG is printed after `::::{ '."
2216 (edebug-trace-display
2217 edebug-trace-buffer "%s{ %s" (make-string edebug-stack-depth ?\:) msg))
2219 (defun edebug-print-trace-after (msg)
2220 "Function called to print trace info after expression evaluation.
2221 MSG is printed after `::::} '."
2222 (edebug-trace-display
2223 edebug-trace-buffer "%s} %s" (make-string edebug-stack-depth ?\:) msg))
2227 (defun edebug-slow-before (before-index)
2228 (unless edebug-active
2229 ;; Debug current function given BEFORE position.
2230 ;; Called from functions compiled with edebug-eval-top-level-form.
2231 ;; Return the before index.
2232 (setcar edebug-offset-indices before-index)
2234 ;; Increment frequency count
2235 (aset edebug-freq-count before-index
2236 (1+ (aref edebug-freq-count before-index)))
2238 (if (or (not (memq edebug-execution-mode '(Go-nonstop next)))
2239 (input-pending-p))
2240 (edebug-debugger before-index 'before nil)))
2241 before-index)
2243 (defun edebug-fast-before (_before-index)
2244 ;; Do nothing.
2247 (defun edebug-slow-after (_before-index after-index value)
2248 (if edebug-active
2249 value
2250 ;; Debug current function given AFTER position and VALUE.
2251 ;; Called from functions compiled with edebug-eval-top-level-form.
2252 ;; Return VALUE.
2253 (setcar edebug-offset-indices after-index)
2255 ;; Increment frequency count
2256 (aset edebug-freq-count after-index
2257 (1+ (aref edebug-freq-count after-index)))
2258 (if edebug-test-coverage (edebug--update-coverage after-index value))
2260 (if (and (eq edebug-execution-mode 'Go-nonstop)
2261 (not (input-pending-p)))
2262 ;; Just return result.
2263 value
2264 (edebug-debugger after-index 'after value)
2267 (defun edebug-fast-after (_before-index _after-index value)
2268 ;; Do nothing but return the value.
2269 value)
2271 (defun edebug-run-slow ()
2272 (defalias 'edebug-before 'edebug-slow-before)
2273 (defalias 'edebug-after 'edebug-slow-after))
2275 ;; This is not used, yet.
2276 (defun edebug-run-fast ()
2277 (defalias 'edebug-before 'edebug-fast-before)
2278 (defalias 'edebug-after 'edebug-fast-after))
2280 (edebug-run-slow)
2283 (defun edebug--update-coverage (after-index value)
2284 (let ((old-result (aref edebug-coverage after-index)))
2285 (cond
2286 ((eq 'ok-coverage old-result))
2287 ((eq 'unknown old-result)
2288 (aset edebug-coverage after-index value))
2289 ;; Test if a different result.
2290 ((not (eq value old-result))
2291 (aset edebug-coverage after-index 'ok-coverage)))))
2294 ;; Dynamically declared unbound variables.
2295 (defvar edebug-breakpoints)
2296 (defvar edebug-break-data) ; break data for current function.
2297 (defvar edebug-break) ; whether a break occurred.
2298 (defvar edebug-global-break) ; whether a global break occurred.
2299 (defvar edebug-break-condition) ; whether the breakpoint is conditional.
2301 (defvar edebug-break-result nil)
2302 (defvar edebug-global-break-result nil)
2305 (defun edebug-debugger (offset-index arg-mode value)
2306 (if inhibit-redisplay
2307 ;; Don't really try to enter edebug within an eval from redisplay.
2308 value
2309 ;; Check breakpoints and pending input.
2310 ;; If edebug display should be updated, call edebug--display.
2311 ;; Return value.
2312 (let* ( ;; This needs to be here since breakpoints may be changed.
2313 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
2314 (edebug-break-data (assq offset-index edebug-breakpoints))
2315 (edebug-break-condition (car (cdr edebug-break-data)))
2316 (edebug-global-break
2317 (if edebug-global-break-condition
2318 (condition-case nil
2319 (setq edebug-global-break-result
2320 (edebug-eval edebug-global-break-condition))
2321 (error nil))))
2322 (edebug-break))
2324 ;;(edebug-trace "exp: %s" value)
2325 ;; Test whether we should break.
2326 (setq edebug-break
2327 (or edebug-global-break
2328 (and edebug-break-data
2329 (or (not edebug-break-condition)
2330 (setq edebug-break-result
2331 (edebug-eval edebug-break-condition))))))
2332 (if (and edebug-break
2333 (nth 2 edebug-break-data)) ; is it temporary?
2334 ;; Delete the breakpoint.
2335 (setcdr edebug-data
2336 (cons (delq edebug-break-data edebug-breakpoints)
2337 (cdr (cdr edebug-data)))))
2339 ;; Display if mode is not go, continue, or Continue-fast
2340 ;; or break, or input is pending,
2341 (if (or (not (memq edebug-execution-mode '(go continue Continue-fast)))
2342 edebug-break
2343 (input-pending-p))
2344 (edebug--display value offset-index arg-mode)) ; <---------- display
2346 value)))
2349 ;; window-start now stored with each function.
2350 ;;(defvar edebug-window-start nil)
2351 ;; Remember where each buffers' window starts between edebug calls.
2352 ;; This is to avoid spurious recentering.
2353 ;; Does this still need to be buffer-local??
2354 ;;(setq-default edebug-window-start nil)
2355 ;;(make-variable-buffer-local 'edebug-window-start)
2358 ;; Dynamically declared unbound vars
2359 (defvar edebug-point) ; the point in edebug buffer
2360 (defvar edebug-outside-buffer) ; the current-buffer outside of edebug
2361 (defvar edebug-outside-point) ; the point outside of edebug
2362 (defvar edebug-outside-mark) ; the mark outside of edebug
2363 (defvar edebug-window-data) ; window and window-start for current function
2364 (defvar edebug-outside-windows) ; outside window configuration
2365 (defvar edebug-eval-buffer) ; for the evaluation list.
2366 (defvar edebug-outside-d-c-i-n-s-w) ; outside default-cursor-in-non-selected-windows
2368 (defvar edebug-eval-list nil) ;; List of expressions to evaluate.
2370 (defvar edebug-previous-result nil) ;; Last result returned.
2372 ;; Emacs 19 adds an arg to mark and mark-marker.
2373 (defalias 'edebug-mark-marker 'mark-marker)
2375 (defun edebug--display (value offset-index arg-mode)
2376 ;; edebug--display-1 is too big, we should split it. This function
2377 ;; here was just introduced to avoid making edebug--display-1
2378 ;; yet a bit deeper.
2379 (save-excursion (edebug--display-1 value offset-index arg-mode)))
2381 (defun edebug--display-1 (value offset-index arg-mode)
2382 (unless (marker-position edebug-def-mark)
2383 ;; The buffer holding the source has been killed.
2384 ;; Let's at least show a backtrace so the user can figure out
2385 ;; which function we're talking about.
2386 (debug))
2387 ;; Setup windows for edebug, determine mode, maybe enter recursive-edit.
2388 ;; Uses local variables of edebug-enter, edebug-before, edebug-after
2389 ;; and edebug-debugger.
2390 (let ((edebug-active t) ; For minor mode alist.
2391 (edebug-with-timeout-suspend (with-timeout-suspend))
2392 edebug-stop ; Should we enter recursive-edit?
2393 (edebug-point (+ edebug-def-mark
2394 (aref (nth 2 edebug-data) offset-index)))
2395 edebug-buffer-outside-point ; current point in edebug-buffer
2396 ;; window displaying edebug-buffer
2397 (edebug-window-data (nth 3 edebug-data))
2398 (edebug-outside-window (selected-window))
2399 (edebug-outside-buffer (current-buffer))
2400 (edebug-outside-point (point))
2401 (edebug-outside-mark (edebug-mark))
2402 edebug-outside-windows ; Window or screen configuration.
2403 edebug-buffer-points
2405 edebug-eval-buffer ; Declared here so we can kill it below.
2406 (eval-result-list (and edebug-eval-list
2407 (edebug-eval-result-list)))
2408 edebug-trace-window
2409 edebug-trace-window-start
2411 (edebug-outside-d-c-i-n-s-w
2412 (default-value 'cursor-in-non-selected-windows)))
2413 (unwind-protect
2414 (let ((cursor-in-echo-area nil)
2415 (unread-command-events nil)
2416 ;; any others??
2418 (setq-default cursor-in-non-selected-windows t)
2419 (if (not (buffer-name edebug-buffer))
2420 (user-error "Buffer defining %s not found" edebug-function))
2422 (if (eq 'after arg-mode)
2423 ;; Compute result string now before windows are modified.
2424 (edebug-compute-previous-result value))
2426 (if edebug-save-windows
2427 ;; Save windows now before we modify them.
2428 (setq edebug-outside-windows
2429 (edebug-current-windows edebug-save-windows)))
2431 (if edebug-save-displayed-buffer-points
2432 (setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
2434 ;; First move the edebug buffer point to edebug-point
2435 ;; so that window start doesn't get changed when we display it.
2436 ;; I don't know if this is going to help.
2437 ;;(set-buffer edebug-buffer)
2438 ;;(goto-char edebug-point)
2440 ;; If edebug-buffer is not currently displayed,
2441 ;; first find a window for it.
2442 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data))
2443 (setcar edebug-window-data (selected-window))
2445 ;; Now display eval list, if any.
2446 ;; This is done after the pop to edebug-buffer
2447 ;; so that buffer-window correspondence is correct after quitting.
2448 (edebug-eval-display eval-result-list)
2449 ;; The evaluation list better not have deleted edebug-window-data.
2450 (select-window (car edebug-window-data))
2451 (set-buffer edebug-buffer)
2453 (setq edebug-buffer-outside-point (point))
2454 (goto-char edebug-point)
2456 (if (eq 'before arg-mode)
2457 ;; Check whether positions are up-to-date.
2458 ;; This assumes point is never before symbol.
2459 (if (not (memq (following-char) '(?\( ?\# ?\` )))
2460 (user-error "Source has changed - reevaluate definition of %s"
2461 edebug-function)
2464 ;; Make sure we bind those in the right buffer (bug#16410).
2465 (let ((overlay-arrow-position overlay-arrow-position)
2466 (overlay-arrow-string overlay-arrow-string))
2467 ;; Now display arrow based on mode.
2468 (edebug-overlay-arrow)
2470 (cond
2471 ((eq 'error arg-mode)
2472 ;; Display error message
2473 (setq edebug-execution-mode 'step)
2474 (edebug-overlay-arrow)
2475 (beep)
2476 (if (eq 'quit (car value))
2477 (message "Quit")
2478 (edebug-report-error value)))
2479 (edebug-break
2480 (cond
2481 (edebug-global-break
2482 (message "Global Break: %s => %s"
2483 edebug-global-break-condition
2484 edebug-global-break-result))
2485 (edebug-break-condition
2486 (message "Break: %s => %s"
2487 edebug-break-condition
2488 edebug-break-result))
2489 ((not (eq edebug-execution-mode 'Continue-fast))
2490 (message "Break"))
2491 (t)))
2493 (t (message "")))
2495 (if (eq 'after arg-mode)
2496 (progn
2497 ;; Display result of previous evaluation.
2498 (if (and edebug-break
2499 edebug-sit-on-break
2500 (not (eq edebug-execution-mode 'Continue-fast)))
2501 (sit-for edebug-sit-for-seconds)) ; Show message.
2502 (edebug-previous-result)))
2504 (cond
2505 (edebug-break
2506 (cond
2507 ((eq edebug-execution-mode 'continue)
2508 (sit-for edebug-sit-for-seconds))
2509 ((eq edebug-execution-mode 'Continue-fast) (sit-for 0))
2510 (t (setq edebug-stop t))))
2511 ;; not edebug-break
2512 ((eq edebug-execution-mode 'trace)
2513 (sit-for edebug-sit-for-seconds)) ; Force update and pause.
2514 ((eq edebug-execution-mode 'Trace-fast)
2515 (sit-for 0))) ; Force update and continue.
2517 (when (input-pending-p)
2518 (setq edebug-stop t)
2519 (setq edebug-execution-mode 'step) ; for `edebug-overlay-arrow'
2520 (edebug-stop))
2522 (edebug-overlay-arrow)
2524 (unwind-protect
2525 (if (or edebug-stop
2526 (memq edebug-execution-mode '(step next))
2527 (eq arg-mode 'error))
2528 (edebug--recursive-edit arg-mode)) ; <--- Recursive edit
2530 ;; Reset the edebug-window-data to whatever it is now.
2531 (let ((window (if (eq (window-buffer) edebug-buffer)
2532 (selected-window)
2533 (get-buffer-window edebug-buffer))))
2534 ;; Remember window-start for edebug-buffer, if still displayed.
2535 (if window
2536 (progn
2537 (setcar edebug-window-data window)
2538 (setcdr edebug-window-data (window-start window)))))
2540 ;; Save trace window point before restoring outside windows.
2541 ;; Could generalize this for other buffers.
2542 (setq edebug-trace-window
2543 (get-buffer-window edebug-trace-buffer))
2544 (if edebug-trace-window
2545 (setq edebug-trace-window-start
2546 (and edebug-trace-window
2547 (window-start edebug-trace-window))))
2549 ;; Restore windows before continuing.
2550 (if edebug-save-windows
2551 (progn
2552 (edebug-set-windows edebug-outside-windows)
2554 ;; Restore displayed buffer points.
2555 ;; Needed even if restoring windows because
2556 ;; window-points are not restored. (should they be??)
2557 (if edebug-save-displayed-buffer-points
2558 (edebug-set-buffer-points edebug-buffer-points))
2560 ;; Unrestore trace window's window-point.
2561 (if edebug-trace-window
2562 (set-window-start edebug-trace-window
2563 edebug-trace-window-start))
2565 ;; Unrestore edebug-buffer's window-start, if displayed.
2566 (let ((window (car edebug-window-data)))
2567 (if (and (edebug-window-live-p window)
2568 (eq (window-buffer) edebug-buffer))
2569 (progn
2570 (set-window-start window (cdr edebug-window-data)
2571 'no-force)
2572 ;; Unrestore edebug-buffer's window-point.
2573 ;; Needed in addition to setting the buffer point
2574 ;; - otherwise quitting doesn't leave point as is.
2575 ;; But can this causes point to not be restored.
2576 ;; Also, it may not be a visible window.
2577 ;; (set-window-point window edebug-point)
2580 ;; Unrestore edebug-buffer's point. Rerestored below.
2581 ;; (goto-char edebug-point) ;; in edebug-buffer
2583 ;; Since we may be in a save-excursion, in case of quit,
2584 ;; reselect the outside window only.
2585 ;; Only needed if we are not recovering windows??
2586 (if (edebug-window-live-p edebug-outside-window)
2587 (select-window edebug-outside-window))
2588 ) ; if edebug-save-windows
2590 ;; Restore current buffer always, in case application needs it.
2591 (if (buffer-name edebug-outside-buffer)
2592 (set-buffer edebug-outside-buffer))
2593 ;; Restore point, and mark.
2594 ;; Needed even if restoring windows because
2595 ;; that doesn't restore point and mark in the current buffer.
2596 ;; But don't restore point if edebug-buffer is current buffer.
2597 (if (not (eq edebug-buffer edebug-outside-buffer))
2598 (goto-char edebug-outside-point))
2599 (if (marker-buffer (edebug-mark-marker))
2600 ;; Does zmacs-regions need to be nil while doing set-marker?
2601 (set-marker (edebug-mark-marker) edebug-outside-mark))
2602 )) ; unwind-protect
2603 ;; None of the following is done if quit or signal occurs.
2605 ;; Restore edebug-buffer's outside point.
2606 ;; (edebug-trace "restore edebug-buffer point: %s"
2607 ;; edebug-buffer-outside-point)
2608 (with-current-buffer edebug-buffer
2609 (goto-char edebug-buffer-outside-point))
2610 ;; ... nothing more.
2612 ;; Could be an option to keep eval display up.
2613 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
2614 (with-timeout-unsuspend edebug-with-timeout-suspend)
2615 ;; Reset global variables to outside values in case they were changed.
2616 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
2620 (defvar edebug-number-of-recursions 0)
2621 ;; Number of recursive edits started by edebug.
2622 ;; Should be 0 at the top level.
2624 (defvar edebug-recursion-depth 0)
2625 ;; Value of recursion-depth when edebug was called.
2627 ;; Dynamically declared unbound vars
2628 (defvar edebug-outside-match-data) ; match data outside of edebug
2629 (defvar edebug-backtrace-buffer) ; each recursive edit gets its own
2630 (defvar edebug-inside-windows)
2631 (defvar edebug-interactive-p)
2633 (defun edebug--recursive-edit (arg-mode)
2634 ;; Start up a recursive edit inside of edebug.
2635 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
2636 ;; Assume that none of the variables below are buffer-local.
2637 (let (;; match-data must be done in the outside buffer
2638 (edebug-outside-match-data
2639 (with-current-buffer edebug-outside-buffer ; in case match buffer different
2640 (match-data)))
2642 ;;(edebug-number-of-recursions (1+ edebug-number-of-recursions))
2643 (edebug-recursion-depth (recursion-depth))
2644 edebug-entered ; bind locally to nil
2645 (edebug-interactive-p nil) ; again non-interactive
2646 edebug-backtrace-buffer ; each recursive edit gets its own
2647 ;; The window configuration may be saved and restored
2648 ;; during a recursive-edit
2649 edebug-inside-windows
2652 (unwind-protect
2653 (let (
2654 ;; Declare global values local but using the same global value.
2655 ;; We could set these to the values for previous edebug call.
2656 (last-command last-command)
2657 (this-command this-command)
2658 (current-prefix-arg nil)
2660 ;; More for Emacs 19
2661 (last-input-event nil)
2662 (last-command-event nil)
2663 (last-event-frame nil)
2664 (last-nonmenu-event nil)
2665 (track-mouse nil)
2667 (standard-output t)
2668 (standard-input t)
2670 ;; Don't keep reading from an executing kbd macro
2671 ;; within edebug unless edebug-continue-kbd-macro is
2672 ;; non-nil. Again, local binding may not be best.
2673 (executing-kbd-macro
2674 (if edebug-continue-kbd-macro executing-kbd-macro))
2676 ;; Don't get confused by the user's keymap changes.
2677 (overriding-local-map nil)
2678 (overriding-terminal-local-map nil)
2680 ;; Bind again to outside values.
2681 (debug-on-error edebug-outside-debug-on-error)
2682 (debug-on-quit edebug-outside-debug-on-quit)
2684 ;; Don't keep defining a kbd macro.
2685 (defining-kbd-macro
2686 (if edebug-continue-kbd-macro defining-kbd-macro))
2688 ;; others??
2691 (if (and (eq edebug-execution-mode 'go)
2692 (not (memq arg-mode '(after error))))
2693 (message "Break"))
2695 (setq signal-hook-function nil)
2697 (edebug-mode 1)
2698 (unwind-protect
2699 (recursive-edit) ; <<<<<<<<<< Recursive edit
2701 ;; Do the following, even if quit occurs.
2702 (setq signal-hook-function 'edebug-signal)
2703 (if edebug-backtrace-buffer
2704 (kill-buffer edebug-backtrace-buffer))
2706 ;; Remember selected-window after recursive-edit.
2707 ;; (setq edebug-inside-window (selected-window))
2709 (set-match-data edebug-outside-match-data)
2711 ;; Recursive edit may have changed buffers,
2712 ;; so set it back before exiting let.
2713 (if (buffer-name edebug-buffer) ; if it still exists
2714 (progn
2715 (set-buffer edebug-buffer)
2716 (when (memq edebug-execution-mode '(go Go-nonstop))
2717 (edebug-overlay-arrow)
2718 (sit-for 0))
2719 (edebug-mode -1))
2720 ;; gotta have a buffer to let its buffer local variables be set
2721 (get-buffer-create " bogus edebug buffer"))
2722 ));; inner let
2726 ;;; Display related functions
2728 (defconst edebug-arrow-alist
2729 '((Continue-fast . "=")
2730 (Trace-fast . "-")
2731 (continue . ">")
2732 (trace . "->")
2733 (step . "=>")
2734 (next . "=>")
2735 (go . "<>")
2736 (Go-nonstop . "..")
2738 "Association list of arrows for each edebug mode.")
2740 (defun edebug-overlay-arrow ()
2741 ;; Set up the overlay arrow at beginning-of-line in current buffer.
2742 ;; The arrow string is derived from edebug-arrow-alist and
2743 ;; edebug-execution-mode.
2744 (let ((pos (line-beginning-position)))
2745 (setq overlay-arrow-string
2746 (cdr (assq edebug-execution-mode edebug-arrow-alist)))
2747 (setq overlay-arrow-position (make-marker))
2748 (set-marker overlay-arrow-position pos (current-buffer))))
2751 (defun edebug-toggle-save-all-windows ()
2752 "Toggle the saving and restoring of all windows.
2753 Also, each time you toggle it on, the inside and outside window
2754 configurations become the same as the current configuration."
2755 (interactive)
2756 (setq edebug-save-windows (not edebug-save-windows))
2757 (if edebug-save-windows
2758 (setq edebug-inside-windows
2759 (setq edebug-outside-windows
2760 (edebug-current-windows
2761 edebug-save-windows))))
2762 (message "Window saving is %s for all windows."
2763 (if edebug-save-windows "on" "off")))
2765 (defmacro edebug-changing-windows (&rest body)
2766 `(let ((window (selected-window)))
2767 (setq edebug-inside-windows (edebug-current-windows t))
2768 (edebug-set-windows edebug-outside-windows)
2769 ,@body;; Code to change edebug-save-windows
2770 (setq edebug-outside-windows (edebug-current-windows
2771 edebug-save-windows))
2772 ;; Problem: what about outside windows that are deleted inside?
2773 (edebug-set-windows edebug-inside-windows)))
2775 (defun edebug-toggle-save-selected-window ()
2776 "Toggle the saving and restoring of the selected window.
2777 Also, each time you toggle it on, the inside and outside window
2778 configurations become the same as the current configuration."
2779 (interactive)
2780 (cond
2781 ((eq t edebug-save-windows)
2782 ;; Save all outside windows except the selected one.
2783 ;; Remove (selected-window) from outside-windows.
2784 (edebug-changing-windows
2785 (setq edebug-save-windows (delq window (edebug-window-list)))))
2787 ((memq (selected-window) edebug-save-windows)
2788 (setq edebug-outside-windows
2789 (delq (assq (selected-window) edebug-outside-windows)
2790 edebug-outside-windows))
2791 (setq edebug-save-windows
2792 (delq (selected-window) edebug-save-windows)))
2793 (t ; Save a new window.
2794 (edebug-changing-windows
2795 (setq edebug-save-windows (cons window edebug-save-windows)))))
2797 (message "Window saving is %s for %s."
2798 (if (memq (selected-window) edebug-save-windows)
2799 "on" "off")
2800 (selected-window)))
2802 (defun edebug-toggle-save-windows (arg)
2803 "Toggle the saving and restoring of windows.
2804 With prefix, toggle for just the selected window.
2805 Otherwise, toggle for all windows."
2806 (interactive "P")
2807 (if arg
2808 (edebug-toggle-save-selected-window)
2809 (edebug-toggle-save-all-windows)))
2811 (defun edebug-where ()
2812 "Show the debug windows and where we stopped in the program."
2813 (interactive)
2814 (if (not edebug-active)
2815 (error "Edebug is not active"))
2816 ;; Restore the window configuration to what it last was inside.
2817 ;; But it is not always set. - experiment
2818 ;;(if edebug-inside-windows
2819 ;; (edebug-set-windows edebug-inside-windows))
2820 (edebug-pop-to-buffer edebug-buffer)
2821 (goto-char edebug-point))
2823 (defun edebug-view-outside ()
2824 "Change to the outside window configuration.
2825 Use `edebug-where' to return."
2826 (interactive)
2827 (if (not edebug-active)
2828 (error "Edebug is not active"))
2829 (setq edebug-inside-windows
2830 (edebug-current-windows edebug-save-windows))
2831 (edebug-set-windows edebug-outside-windows)
2832 (goto-char edebug-outside-point)
2833 (message "Window configuration outside of Edebug. Return with %s"
2834 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
2837 (defun edebug-bounce-point (arg)
2838 "Bounce the point in the outside current buffer.
2839 If prefix argument ARG is supplied, sit for that many seconds
2840 before returning. The default is one second."
2841 (interactive "p")
2842 (if (not edebug-active)
2843 (error "Edebug is not active"))
2844 (save-excursion
2845 ;; If the buffer's currently displayed, avoid set-window-configuration.
2846 (save-window-excursion
2847 (edebug-pop-to-buffer edebug-outside-buffer)
2848 (goto-char edebug-outside-point)
2849 (message "Current buffer: %s Point: %s Mark: %s"
2850 (current-buffer) (point)
2851 (if (marker-buffer (edebug-mark-marker))
2852 (marker-position (edebug-mark-marker)) "<not set>"))
2853 (sit-for arg)
2854 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data)))))
2857 ;; Joe Wells, here is a start at your idea of adding a buffer to the internal
2858 ;; display list. Still need to use this list in edebug--display.
2860 '(defvar edebug-display-buffer-list nil
2861 "List of buffers that edebug will display when it is active.")
2863 '(defun edebug-display-buffer (buffer)
2864 "Toggle display of a buffer inside of edebug."
2865 (interactive "bBuffer: ")
2866 (let ((already-displaying (memq buffer edebug-display-buffer-list)))
2867 (setq edebug-display-buffer-list
2868 (if already-displaying
2869 (delq buffer edebug-display-buffer-list)
2870 (cons buffer edebug-display-buffer-list)))
2871 (message "Displaying %s %s" buffer
2872 (if already-displaying "off" "on"))))
2874 ;;; Breakpoint related functions
2876 (defun edebug-find-stop-point ()
2877 ;; Return (function . index) of the nearest edebug stop point.
2878 (let* ((edebug-def-name (edebug-form-data-symbol))
2879 (edebug-data
2880 (let ((data (get edebug-def-name 'edebug)))
2881 (if (or (null data) (markerp data))
2882 (error "%s is not instrumented for Edebug" edebug-def-name))
2883 data)) ; we could do it automatically, if data is a marker.
2884 ;; pull out parts of edebug-data.
2885 (edebug-def-mark (car edebug-data))
2886 ;; (edebug-breakpoints (car (cdr edebug-data)))
2888 (offset-vector (nth 2 edebug-data))
2889 (offset (- (save-excursion
2890 (if (looking-at "[ \t]")
2891 ;; skip backwards until non-whitespace, or bol
2892 (skip-chars-backward " \t"))
2893 (point))
2894 edebug-def-mark))
2895 len i)
2896 ;; the offsets are in order so we can do a linear search
2897 (setq len (length offset-vector))
2898 (setq i 0)
2899 (while (and (< i len) (> offset (aref offset-vector i)))
2900 (setq i (1+ i)))
2901 (if (and (< i len)
2902 (<= offset (aref offset-vector i)))
2903 ;; return the relevant info
2904 (cons edebug-def-name i)
2905 (message "Point is not on an expression in %s."
2906 edebug-def-name)
2910 (defun edebug-next-breakpoint ()
2911 "Move point to the next breakpoint, or first if none past point."
2912 (interactive)
2913 (let ((edebug-stop-point (edebug-find-stop-point)))
2914 (if edebug-stop-point
2915 (let* ((edebug-def-name (car edebug-stop-point))
2916 (index (cdr edebug-stop-point))
2917 (edebug-data (get edebug-def-name 'edebug))
2919 ;; pull out parts of edebug-data
2920 (edebug-def-mark (car edebug-data))
2921 (edebug-breakpoints (car (cdr edebug-data)))
2922 (offset-vector (nth 2 edebug-data))
2923 breakpoint)
2924 (if (not edebug-breakpoints)
2925 (message "No breakpoints in this function.")
2926 (let ((breaks edebug-breakpoints))
2927 (while (and breaks
2928 (<= (car (car breaks)) index))
2929 (setq breaks (cdr breaks)))
2930 (setq breakpoint
2931 (if breaks
2932 (car breaks)
2933 ;; goto the first breakpoint
2934 (car edebug-breakpoints)))
2935 (goto-char (+ edebug-def-mark
2936 (aref offset-vector (car breakpoint))))
2938 (message "%s"
2939 (concat (if (nth 2 breakpoint)
2940 "Temporary " "")
2941 (if (car (cdr breakpoint))
2942 (format "Condition: %s"
2943 (edebug-safe-prin1-to-string
2944 (car (cdr breakpoint))))
2945 "")))
2946 ))))))
2949 (defun edebug-modify-breakpoint (flag &optional condition temporary)
2950 "Modify the breakpoint for the form at point or after it.
2951 Set it if FLAG is non-nil, clear it otherwise. Then move to that point.
2952 If CONDITION or TEMPORARY are non-nil, add those attributes to
2953 the breakpoint."
2954 (let ((edebug-stop-point (edebug-find-stop-point)))
2955 (if edebug-stop-point
2956 (let* ((edebug-def-name (car edebug-stop-point))
2957 (index (cdr edebug-stop-point))
2958 (edebug-data (get edebug-def-name 'edebug))
2960 ;; pull out parts of edebug-data
2961 (edebug-def-mark (car edebug-data))
2962 (edebug-breakpoints (car (cdr edebug-data)))
2963 (offset-vector (nth 2 edebug-data))
2964 present)
2965 ;; delete it either way
2966 (setq present (assq index edebug-breakpoints))
2967 (setq edebug-breakpoints (delq present edebug-breakpoints))
2968 (if flag
2969 (progn
2970 ;; add it to the list and resort
2971 (setq edebug-breakpoints
2972 (edebug-sort-alist
2973 (cons
2974 (list index condition temporary)
2975 edebug-breakpoints) '<))
2976 (if condition
2977 (message "Breakpoint set in %s with condition: %s"
2978 edebug-def-name condition)
2979 (message "Breakpoint set in %s" edebug-def-name)))
2980 (if present
2981 (message "Breakpoint unset in %s" edebug-def-name)
2982 (message "No breakpoint here")))
2984 (setcar (cdr edebug-data) edebug-breakpoints)
2985 (goto-char (+ edebug-def-mark (aref offset-vector index)))
2986 ))))
2988 (defun edebug-set-breakpoint (arg)
2989 "Set the breakpoint of nearest sexp.
2990 With prefix argument, make it a temporary breakpoint."
2991 (interactive "P")
2992 (edebug-modify-breakpoint t nil arg))
2994 (defun edebug-unset-breakpoint ()
2995 "Clear the breakpoint of nearest sexp."
2996 (interactive)
2997 (edebug-modify-breakpoint nil))
3000 (defun edebug-set-global-break-condition (expression)
3001 "Set `edebug-global-break-condition' to EXPRESSION."
3002 (interactive
3003 (list
3004 (let ((initial (and edebug-global-break-condition
3005 (format "%s" edebug-global-break-condition))))
3006 (read-from-minibuffer
3007 "Global Condition: " initial read-expression-map t
3008 (if (equal (car read-expression-history) initial)
3009 '(read-expression-history . 1)
3010 'read-expression-history)))))
3011 (setq edebug-global-break-condition expression))
3014 ;;; Mode switching functions
3016 (defun edebug-set-mode (mode shortmsg msg)
3017 ;; Set the edebug mode to MODE.
3018 ;; Display SHORTMSG, or MSG if not within edebug.
3019 (if (eq (1+ edebug-recursion-depth) (recursion-depth))
3020 (progn
3021 (setq edebug-execution-mode mode)
3022 (message "%s" shortmsg)
3023 ;; Continue execution
3024 (exit-recursive-edit))
3025 ;; This is not terribly useful!!
3026 (setq edebug-next-execution-mode mode)
3027 (message "%s" msg)))
3030 (defalias 'edebug-step-through-mode 'edebug-step-mode)
3032 (defun edebug-step-mode ()
3033 "Proceed to next stop point."
3034 (interactive)
3035 (edebug-set-mode 'step "" "Edebug will stop at next stop point."))
3037 (defun edebug-next-mode ()
3038 "Proceed to next `after' stop point."
3039 (interactive)
3040 (edebug-set-mode 'next "" "Edebug will stop after next eval."))
3042 (defun edebug-go-mode (arg)
3043 "Go, evaluating until break.
3044 With prefix ARG, set temporary break at current point and go."
3045 (interactive "P")
3046 (if arg
3047 (edebug-set-breakpoint t))
3048 (edebug-set-mode 'go "Go..." "Edebug will go until break."))
3050 (defun edebug-Go-nonstop-mode ()
3051 "Go, evaluating without debugging.
3052 You can use `edebug-stop', or any editing command, to stop."
3053 (interactive)
3054 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
3055 "Edebug will not stop at breaks."))
3058 (defun edebug-trace-mode ()
3059 "Begin trace mode.
3060 Pauses for `edebug-sit-for-seconds' at each stop point."
3061 (interactive)
3062 (edebug-set-mode 'trace "Tracing..." "Edebug will trace with pause."))
3064 (defun edebug-Trace-fast-mode ()
3065 "Trace with no wait at each step.
3066 Updates the display at each stop point, but does not pause."
3067 (interactive)
3068 (edebug-set-mode 'Trace-fast
3069 "Trace fast..." "Edebug will trace without pause."))
3071 (defun edebug-continue-mode ()
3072 "Begin continue mode.
3073 Pauses for `edebug-sit-for-seconds' at each break point."
3074 (interactive)
3075 (edebug-set-mode 'continue "Continue..."
3076 "Edebug will pause at breakpoints."))
3078 (defun edebug-Continue-fast-mode ()
3079 "Trace with no wait at each step.
3080 Updates the display at each break point, but does not pause."
3081 (interactive)
3082 (edebug-set-mode 'Continue-fast "Continue fast..."
3083 "Edebug will stop and go at breakpoints."))
3085 ;; ------------------------------------------------------------
3086 ;; The following use the mode changing commands and breakpoints.
3089 (defun edebug-goto-here ()
3090 "Proceed to first stop-point at or after current position of point."
3091 (interactive)
3092 (edebug-go-mode t))
3095 (defun edebug-stop ()
3096 "Stop execution and do not continue.
3097 Useful for exiting from trace or continue loop."
3098 (interactive)
3099 (message "Stop"))
3102 '(defun edebug-forward ()
3103 "Proceed to the exit of the next expression to be evaluated."
3104 (interactive)
3105 (edebug-set-mode
3106 'forward "Forward"
3107 "Edebug will stop after exiting the next expression."))
3110 (defun edebug-forward-sexp (arg)
3111 "Proceed from the current point to the end of the ARGth sexp ahead.
3112 If there are not ARG sexps ahead, then do `edebug-step-out'."
3113 (interactive "p")
3114 (condition-case nil
3115 (let ((parse-sexp-ignore-comments t))
3116 ;; Call forward-sexp repeatedly until done or failure.
3117 (forward-sexp arg)
3118 (edebug-go-mode t))
3119 (error
3120 (edebug-step-out)
3123 (defun edebug-step-out ()
3124 "Proceed from the current point to the end of the containing sexp.
3125 If there is no containing sexp that is not the top level defun,
3126 go to the end of the last sexp, or if that is the same point, then step."
3127 (interactive)
3128 (condition-case nil
3129 (let ((parse-sexp-ignore-comments t))
3130 (up-list 1)
3131 (save-excursion
3132 ;; Is there still a containing expression?
3133 (up-list 1))
3134 (edebug-go-mode t))
3135 (error
3136 ;; At top level - 1, so first check if there are more sexps at this level.
3137 (let ((start-point (point)))
3138 ;; (up-list 1)
3139 (down-list -1)
3140 (if (= (point) start-point)
3141 (edebug-step-mode) ; No more at this level, so step.
3142 (edebug-go-mode t)
3143 )))))
3145 (defun edebug-instrument-function (func)
3146 ;; Func should be a function symbol.
3147 ;; Return the function symbol, or nil if not instrumented.
3148 (let ((func-marker (get func 'edebug)))
3149 (cond
3150 ((and (markerp func-marker) (marker-buffer func-marker))
3151 ;; It is uninstrumented, so instrument it.
3152 (with-current-buffer (marker-buffer func-marker)
3153 (goto-char func-marker)
3154 (edebug-eval-top-level-form)
3155 func))
3156 ((consp func-marker)
3157 (message "%s is already instrumented." func)
3158 func)
3160 (let ((loc (find-function-noselect func t)))
3161 (unless (cdr loc)
3162 (error "Could not find the definition in its file"))
3163 (with-current-buffer (car loc)
3164 (goto-char (cdr loc))
3165 (edebug-eval-top-level-form)
3166 func))))))
3168 (defun edebug-instrument-callee ()
3169 "Instrument the definition of the function or macro about to be called.
3170 Do this when stopped before the form or it will be too late.
3171 One side effect of using this command is that the next time the
3172 function or macro is called, Edebug will be called there as well."
3173 (interactive)
3174 (if (not (looking-at "("))
3175 (error "You must be before a list form")
3176 (let ((func
3177 (save-excursion
3178 (down-list 1)
3179 (if (looking-at "(")
3180 (edebug--form-data-name
3181 (edebug-get-form-data-entry (point)))
3182 (read (current-buffer))))))
3183 (edebug-instrument-function func))))
3186 (defun edebug-step-in ()
3187 "Step into the definition of the function or macro about to be called.
3188 This first does `edebug-instrument-callee' to ensure that it is
3189 instrumented. Then it does `edebug-on-entry' and switches to `go' mode."
3190 (interactive)
3191 (let ((func (edebug-instrument-callee)))
3192 (if func
3193 (progn
3194 (edebug-on-entry func 'temp)
3195 (edebug-go-mode nil)))))
3197 (defun edebug-on-entry (function &optional flag)
3198 "Cause Edebug to stop when FUNCTION is called.
3199 With prefix argument, make this temporary so it is automatically
3200 canceled the first time the function is entered."
3201 (interactive "aEdebug on entry to: \nP")
3202 ;; Could store this in the edebug data instead.
3203 (put function 'edebug-on-entry (if flag 'temp t)))
3205 (defun cancel-edebug-on-entry (function)
3206 (interactive "aEdebug on entry to: ")
3207 (put function 'edebug-on-entry nil))
3210 '(advice-add 'debug-on-entry :around 'edebug--debug-on-entry) ;; Should we do this?
3211 ;; Also need edebug-cancel-debug-on-entry
3213 '(defun edebug--debug-on-entry (orig function)
3214 "If the function is instrumented for Edebug, call `edebug-on-entry'."
3215 (let ((func-data (get function 'edebug)))
3216 (if (or (null func-data) (markerp func-data))
3217 (funcall orig function)
3218 (edebug-on-entry function))))
3221 (defun edebug-top-level-nonstop ()
3222 "Set mode to Go-nonstop, and exit to top-level.
3223 This is useful for exiting even if `unwind-protect' code may be executed."
3224 (interactive)
3225 (setq edebug-execution-mode 'Go-nonstop)
3226 (top-level))
3228 ;;(defun edebug-exit-out ()
3229 ;; "Go until the current function exits."
3230 ;; (interactive)
3231 ;; (edebug-set-mode 'exiting "Exit..."))
3233 (defconst edebug-initial-mode-alist
3234 '((edebug-step-mode . step)
3235 (edebug-next-mode . next)
3236 (edebug-trace-mode . trace)
3237 (edebug-Trace-fast-mode . Trace-fast)
3238 (edebug-go-mode . go)
3239 (edebug-continue-mode . continue)
3240 (edebug-Continue-fast-mode . Continue-fast)
3241 (edebug-Go-nonstop-mode . Go-nonstop))
3242 "Association list between commands and the modes they set.")
3244 (defvar edebug-mode-map) ; will be defined fully later.
3246 (defun edebug-set-initial-mode ()
3247 "Set the initial execution mode of Edebug.
3248 The mode is requested via the key that would be used to set the mode in
3249 edebug-mode."
3250 (interactive)
3251 (let* ((old-mode edebug-initial-mode)
3252 (key (read-key-sequence
3253 (format
3254 "Change initial edebug mode from %s (%c) to (enter key): "
3255 old-mode
3256 (aref (where-is-internal
3257 (car (rassq old-mode edebug-initial-mode-alist))
3258 edebug-mode-map 'firstonly)
3259 0))))
3260 (mode (cdr (assq (lookup-key edebug-mode-map key)
3261 edebug-initial-mode-alist))))
3262 (if mode
3263 (progn
3264 (setq edebug-initial-mode mode)
3265 (message "Edebug's initial mode is now: %s" mode))
3266 (error "Key must map to one of the mode changing commands"))))
3268 ;;; Evaluation of expressions
3270 (defmacro edebug-outside-excursion (&rest body)
3271 "Evaluate an expression list in the outside context.
3272 Return the result of the last expression."
3273 ;; Only restores the non-variables context since all the variables let-bound
3274 ;; by Edebug will be properly reset to the appropriate context's value by
3275 ;; backtrace-eval.
3276 (declare (debug t))
3277 `(save-excursion ; of current-buffer
3278 (if edebug-save-windows
3279 (progn
3280 ;; After excursion, we will
3281 ;; restore to current window configuration.
3282 (setq edebug-inside-windows
3283 (edebug-current-windows edebug-save-windows))
3284 ;; Restore outside windows.
3285 (edebug-set-windows edebug-outside-windows)))
3287 (set-buffer edebug-buffer) ; why?
3288 (set-match-data edebug-outside-match-data)
3289 ;; Restore outside context.
3290 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
3291 (unwind-protect
3292 ;; FIXME: This restoring of edebug-outside-buffer and
3293 ;; edebug-outside-point is redundant now that backtrace-eval does it
3294 ;; for us.
3295 (with-current-buffer edebug-outside-buffer ; of edebug-buffer
3296 (goto-char edebug-outside-point)
3297 (if (marker-buffer (edebug-mark-marker))
3298 (set-marker (edebug-mark-marker) edebug-outside-mark))
3299 ,@body)
3301 ;; Back to edebug-buffer. Restore rest of inside context.
3302 ;; (use-local-map edebug-inside-map)
3303 (if edebug-save-windows
3304 ;; Restore inside windows.
3305 (edebug-set-windows edebug-inside-windows))
3307 ;; Save values that may have been changed.
3308 (setq edebug-outside-d-c-i-n-s-w
3309 (default-value 'cursor-in-non-selected-windows))
3311 ;; Restore the outside saved values; don't alter
3312 ;; the outside binding loci.
3313 (setq-default cursor-in-non-selected-windows t))))
3315 (defun edebug-eval (expr)
3316 (backtrace-eval expr 0 'edebug-after))
3318 (defun edebug-safe-eval (expr)
3319 ;; Evaluate EXPR safely.
3320 ;; If there is an error, a string is returned describing the error.
3321 (condition-case edebug-err
3322 (edebug-eval expr)
3323 (error (edebug-format "%s: %s" ;; could
3324 (get (car edebug-err) 'error-message)
3325 (car (cdr edebug-err))))))
3327 ;;; Printing
3330 (defun edebug-report-error (value)
3331 ;; Print an error message like command level does.
3332 ;; This also prints the error name if it has no error-message.
3333 (message "%s: %s"
3334 (or (get (car value) 'error-message)
3335 (format "peculiar error (%s)" (car value)))
3336 (mapconcat (function (lambda (edebug-arg)
3337 ;; continuing after an error may
3338 ;; complain about edebug-arg. why??
3339 (prin1-to-string edebug-arg)))
3340 (cdr value) ", ")))
3342 (defvar print-readably) ; defined by lemacs
3343 ;; Alternatively, we could change the definition of
3344 ;; edebug-safe-prin1-to-string to only use these if defined.
3346 (defun edebug-safe-prin1-to-string (value)
3347 (let ((print-escape-newlines t)
3348 (print-length (or edebug-print-length print-length))
3349 (print-level (or edebug-print-level print-level))
3350 (print-circle (or edebug-print-circle print-circle))
3351 (print-readably nil)) ; lemacs uses this.
3352 (edebug-prin1-to-string value)))
3354 (defun edebug-compute-previous-result (previous-value)
3355 (if edebug-unwrap-results
3356 (setq previous-value
3357 (edebug-unwrap* previous-value)))
3358 (setq edebug-previous-result
3359 (concat "Result: "
3360 (edebug-safe-prin1-to-string previous-value)
3361 (eval-expression-print-format previous-value))))
3363 (defun edebug-previous-result ()
3364 "Print the previous result."
3365 (interactive)
3366 (message "%s" edebug-previous-result))
3368 ;;; Read, Eval and Print
3370 (defalias 'edebug-prin1 'prin1)
3371 (defalias 'edebug-print 'print)
3372 (defalias 'edebug-prin1-to-string 'prin1-to-string)
3373 (defalias 'edebug-format 'format-message)
3374 (defalias 'edebug-message 'message)
3376 (defun edebug-eval-expression (expr)
3377 "Evaluate an expression in the outside environment.
3378 If interactive, prompt for the expression.
3379 Print result in minibuffer."
3380 (interactive (list (read-from-minibuffer
3381 "Eval: " nil read-expression-map t
3382 'read-expression-history)))
3383 (princ
3384 (edebug-outside-excursion
3385 (setq values (cons (edebug-eval expr) values))
3386 (concat (edebug-safe-prin1-to-string (car values))
3387 (eval-expression-print-format (car values))))))
3389 (defun edebug-eval-last-sexp ()
3390 "Evaluate sexp before point in the outside environment.
3391 Print value in minibuffer."
3392 (interactive)
3393 (edebug-eval-expression (edebug-last-sexp)))
3395 (defun edebug-eval-print-last-sexp ()
3396 "Evaluate sexp before point in outside environment; insert value.
3397 This prints the value into current buffer."
3398 (interactive)
3399 (let* ((form (edebug-last-sexp))
3400 (result-string
3401 (edebug-outside-excursion
3402 (edebug-safe-prin1-to-string (edebug-safe-eval form))))
3403 (standard-output (current-buffer)))
3404 (princ "\n")
3405 ;; princ the string to get rid of quotes.
3406 (princ result-string)
3407 (princ "\n")
3410 ;;; Edebug Minor Mode
3412 (defvar edebug-inhibit-emacs-lisp-mode-bindings nil
3413 "If non-nil, inhibit Edebug bindings on the C-x C-a key.
3414 By default, loading the `edebug' library causes these bindings to
3415 be installed in `emacs-lisp-mode-map'.")
3417 (define-obsolete-variable-alias 'gud-inhibit-global-bindings
3418 'edebug-inhibit-emacs-lisp-mode-bindings "24.3")
3420 ;; Global GUD bindings for all emacs-lisp-mode buffers.
3421 (unless edebug-inhibit-emacs-lisp-mode-bindings
3422 (define-key emacs-lisp-mode-map "\C-x\C-a\C-s" 'edebug-step-mode)
3423 (define-key emacs-lisp-mode-map "\C-x\C-a\C-n" 'edebug-next-mode)
3424 (define-key emacs-lisp-mode-map "\C-x\C-a\C-c" 'edebug-go-mode)
3425 (define-key emacs-lisp-mode-map "\C-x\C-a\C-l" 'edebug-where)
3426 ;; The following isn't a GUD binding.
3427 (define-key emacs-lisp-mode-map "\C-x\C-a\C-m" 'edebug-set-initial-mode))
3429 (defvar edebug-mode-map
3430 (let ((map (copy-keymap emacs-lisp-mode-map)))
3431 ;; control
3432 (define-key map " " 'edebug-step-mode)
3433 (define-key map "n" 'edebug-next-mode)
3434 (define-key map "g" 'edebug-go-mode)
3435 (define-key map "G" 'edebug-Go-nonstop-mode)
3436 (define-key map "t" 'edebug-trace-mode)
3437 (define-key map "T" 'edebug-Trace-fast-mode)
3438 (define-key map "c" 'edebug-continue-mode)
3439 (define-key map "C" 'edebug-Continue-fast-mode)
3441 ;;(define-key map "f" 'edebug-forward) not implemented
3442 (define-key map "f" 'edebug-forward-sexp)
3443 (define-key map "h" 'edebug-goto-here)
3445 (define-key map "I" 'edebug-instrument-callee)
3446 (define-key map "i" 'edebug-step-in)
3447 (define-key map "o" 'edebug-step-out)
3449 ;; quitting and stopping
3450 (define-key map "q" 'top-level)
3451 (define-key map "Q" 'edebug-top-level-nonstop)
3452 (define-key map "a" 'abort-recursive-edit)
3453 (define-key map "S" 'edebug-stop)
3455 ;; breakpoints
3456 (define-key map "b" 'edebug-set-breakpoint)
3457 (define-key map "u" 'edebug-unset-breakpoint)
3458 (define-key map "B" 'edebug-next-breakpoint)
3459 (define-key map "x" 'edebug-set-conditional-breakpoint)
3460 (define-key map "X" 'edebug-set-global-break-condition)
3462 ;; evaluation
3463 (define-key map "r" 'edebug-previous-result)
3464 (define-key map "e" 'edebug-eval-expression)
3465 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3466 (define-key map "E" 'edebug-visit-eval-list)
3468 ;; views
3469 (define-key map "w" 'edebug-where)
3470 (define-key map "v" 'edebug-view-outside) ;; maybe obsolete??
3471 (define-key map "p" 'edebug-bounce-point)
3472 (define-key map "P" 'edebug-view-outside) ;; same as v
3473 (define-key map "W" 'edebug-toggle-save-windows)
3475 ;; misc
3476 (define-key map "?" 'edebug-help)
3477 (define-key map "d" 'edebug-backtrace)
3479 (define-key map "-" 'negative-argument)
3481 ;; statistics
3482 (define-key map "=" 'edebug-temp-display-freq-count)
3484 ;; GUD bindings
3485 (define-key map "\C-c\C-s" 'edebug-step-mode)
3486 (define-key map "\C-c\C-n" 'edebug-next-mode)
3487 (define-key map "\C-c\C-c" 'edebug-go-mode)
3489 (define-key map "\C-x " 'edebug-set-breakpoint)
3490 (define-key map "\C-c\C-d" 'edebug-unset-breakpoint)
3491 (define-key map "\C-c\C-t"
3492 (lambda () (interactive) (edebug-set-breakpoint t)))
3493 (define-key map "\C-c\C-l" 'edebug-where)
3494 map))
3496 ;; Autoloading these global bindings doesn't make sense because
3497 ;; they cannot be used anyway unless Edebug is already loaded and active.
3499 (defvar global-edebug-prefix "\^XX"
3500 "Prefix key for global edebug commands, available from any buffer.")
3502 (defvar global-edebug-map
3503 (let ((map (make-sparse-keymap)))
3505 (define-key map " " 'edebug-step-mode)
3506 (define-key map "g" 'edebug-go-mode)
3507 (define-key map "G" 'edebug-Go-nonstop-mode)
3508 (define-key map "t" 'edebug-trace-mode)
3509 (define-key map "T" 'edebug-Trace-fast-mode)
3510 (define-key map "c" 'edebug-continue-mode)
3511 (define-key map "C" 'edebug-Continue-fast-mode)
3513 ;; breakpoints
3514 (define-key map "b" 'edebug-set-breakpoint)
3515 (define-key map "u" 'edebug-unset-breakpoint)
3516 (define-key map "x" 'edebug-set-conditional-breakpoint)
3517 (define-key map "X" 'edebug-set-global-break-condition)
3519 ;; views
3520 (define-key map "w" 'edebug-where)
3521 (define-key map "W" 'edebug-toggle-save-windows)
3523 ;; quitting
3524 (define-key map "q" 'top-level)
3525 (define-key map "Q" 'edebug-top-level-nonstop)
3526 (define-key map "a" 'abort-recursive-edit)
3528 ;; statistics
3529 (define-key map "=" 'edebug-display-freq-count)
3530 map)
3531 "Global map of edebug commands, available from any buffer.")
3533 (global-unset-key global-edebug-prefix)
3534 (global-set-key global-edebug-prefix global-edebug-map)
3537 (defun edebug-help ()
3538 "Describe `edebug-mode'."
3539 (interactive)
3540 (describe-function 'edebug-mode))
3542 (defvar edebug--mode-saved-vars nil)
3544 (define-minor-mode edebug-mode
3545 "Mode for Emacs Lisp buffers while in Edebug.
3547 In addition to all Emacs Lisp commands (except those that modify the
3548 buffer) there are local and global key bindings to several Edebug
3549 specific commands. E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
3550 in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3552 Also see bindings for the eval list buffer *edebug* in `edebug-eval-mode'.
3554 The edebug buffer commands:
3555 \\{edebug-mode-map}
3557 Global commands prefixed by `global-edebug-prefix':
3558 \\{global-edebug-map}
3560 Options:
3561 `edebug-setup-hook'
3562 `edebug-all-defs'
3563 `edebug-all-forms'
3564 `edebug-save-windows'
3565 `edebug-save-displayed-buffer-points'
3566 `edebug-initial-mode'
3567 `edebug-trace'
3568 `edebug-test-coverage'
3569 `edebug-continue-kbd-macro'
3570 `edebug-print-length'
3571 `edebug-print-level'
3572 `edebug-print-circle'
3573 `edebug-on-error'
3574 `edebug-on-quit'
3575 `edebug-on-signal'
3576 `edebug-unwrap-results'
3577 `edebug-global-break-condition'"
3578 :lighter " *Debugging*"
3579 :keymap edebug-mode-map
3580 ;; If the user kills the buffer in which edebug is currently active,
3581 ;; exit to top level, because the edebug command loop can't usefully
3582 ;; continue running in such a case.
3584 (if (not edebug-mode)
3585 (progn
3586 (while edebug--mode-saved-vars
3587 (let ((setting (pop edebug--mode-saved-vars)))
3588 (if (consp setting)
3589 (set (car setting) (cdr setting))
3590 (kill-local-variable setting))))
3591 (remove-hook 'kill-buffer-hook 'edebug-kill-buffer t))
3592 (pcase-dolist (`(,var . ,val) '((buffer-read-only . t)))
3593 (push
3594 (if (local-variable-p var) (cons var (symbol-value var)) var)
3595 edebug--mode-saved-vars)
3596 (set (make-local-variable var) val))
3597 ;; Append `edebug-kill-buffer' to the hook to avoid interfering with
3598 ;; other entries that are unguarded against deleted buffer.
3599 (add-hook 'kill-buffer-hook 'edebug-kill-buffer t t)))
3601 (defun edebug-kill-buffer ()
3602 "Used on `kill-buffer-hook' when Edebug is operating in a buffer of Lisp code."
3603 (run-with-timer 0 nil #'top-level))
3605 ;;; edebug eval list mode
3607 ;; A list of expressions and their evaluations is displayed in *edebug*.
3609 (defun edebug-eval-result-list ()
3610 "Return a list of evaluations of `edebug-eval-list'."
3611 ;; Assumes in outside environment.
3612 ;; Don't do any edebug things now.
3613 (let ((edebug-execution-mode 'Go-nonstop)
3614 (edebug-trace nil))
3615 (mapcar 'edebug-safe-eval edebug-eval-list)))
3617 (defun edebug-eval-display-list (eval-result-list)
3618 ;; Assumes edebug-eval-buffer exists.
3619 (let ((standard-output edebug-eval-buffer)
3620 (edebug-comment-line
3621 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
3622 (set-buffer edebug-eval-buffer)
3623 (erase-buffer)
3624 (dolist (exp edebug-eval-list)
3625 (prin1 exp) (terpri)
3626 (prin1 (pop eval-result-list)) (terpri)
3627 (princ edebug-comment-line))
3628 (edebug-pop-to-buffer edebug-eval-buffer)
3631 (defun edebug-create-eval-buffer ()
3632 (unless (and edebug-eval-buffer (buffer-name edebug-eval-buffer))
3633 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
3634 (edebug-eval-mode)))
3636 ;; Should generalize this to be callable outside of edebug
3637 ;; with calls in user functions, e.g. (edebug-eval-display)
3639 (defun edebug-eval-display (eval-result-list)
3640 "Display expressions and evaluations in EVAL-RESULT-LIST.
3641 It modifies the context by popping up the eval display."
3642 (when eval-result-list
3643 (edebug-create-eval-buffer)
3644 (edebug-eval-display-list eval-result-list)))
3646 (defun edebug-eval-redisplay ()
3647 "Redisplay eval list in outside environment.
3648 May only be called from within `edebug--recursive-edit'."
3649 (edebug-create-eval-buffer)
3650 (edebug-outside-excursion
3651 (edebug-eval-display-list (edebug-eval-result-list))
3654 (defun edebug-visit-eval-list ()
3655 "Switch to the evaluation list buffer \"*edebug*\"."
3656 (interactive)
3657 (edebug-eval-redisplay)
3658 (edebug-pop-to-buffer edebug-eval-buffer))
3661 (defun edebug-update-eval-list ()
3662 "Replace the evaluation list with the sexps now in the eval buffer."
3663 (interactive)
3664 (let ((starting-point (point))
3665 new-list)
3666 (goto-char (point-min))
3667 ;; get the first expression
3668 (edebug-skip-whitespace)
3669 (if (not (eobp))
3670 (progn
3671 (forward-sexp 1)
3672 (push (edebug-last-sexp) new-list)))
3674 (while (re-search-forward "^;" nil t)
3675 (forward-line 1)
3676 (skip-chars-forward " \t\n\r")
3677 (if (and (/= ?\; (following-char))
3678 (not (eobp)))
3679 (progn
3680 (forward-sexp 1)
3681 (push (edebug-last-sexp) new-list))))
3683 (setq edebug-eval-list (nreverse new-list))
3684 (edebug-eval-redisplay)
3685 (goto-char starting-point)))
3688 (defun edebug-delete-eval-item ()
3689 "Delete the item under point and redisplay."
3690 ;; could add arg to do repeatedly
3691 (interactive)
3692 (if (re-search-backward "^;" nil 'nofail)
3693 (forward-line 1))
3694 (delete-region
3695 (point) (progn (re-search-forward "^;" nil 'nofail)
3696 (beginning-of-line)
3697 (point)))
3698 (edebug-update-eval-list))
3702 (defvar edebug-eval-mode-map
3703 (let ((map (make-sparse-keymap)))
3704 (set-keymap-parent map lisp-interaction-mode-map)
3705 (define-key map "\C-c\C-w" 'edebug-where)
3706 (define-key map "\C-c\C-d" 'edebug-delete-eval-item)
3707 (define-key map "\C-c\C-u" 'edebug-update-eval-list)
3708 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3709 (define-key map "\C-j" 'edebug-eval-print-last-sexp)
3710 map)
3711 "Keymap for Edebug Eval mode. Superset of Lisp Interaction mode.")
3713 (put 'edebug-eval-mode 'mode-class 'special)
3715 (define-derived-mode edebug-eval-mode lisp-interaction-mode "Edebug Eval"
3716 "Mode for evaluation list buffer while in Edebug.
3718 In addition to all Interactive Emacs Lisp commands there are local and
3719 global key bindings to several Edebug specific commands. E.g.
3720 `edebug-step-mode' is bound to \\[edebug-step-mode] in the Edebug
3721 buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3723 Eval list buffer commands:
3724 \\{edebug-eval-mode-map}
3726 Global commands prefixed by `global-edebug-prefix':
3727 \\{global-edebug-map}")
3729 ;;; Interface with standard debugger.
3731 ;; (setq debugger 'edebug) ; to use the edebug debugger
3732 ;; (setq debugger 'debug) ; use the standard debugger
3734 ;; Note that debug and its utilities must be byte-compiled to work,
3735 ;; since they depend on the backtrace looking a certain way. But
3736 ;; edebug is not dependent on this, yet.
3738 (defun edebug (&optional arg-mode &rest args)
3739 "Replacement for `debug'.
3740 If we are running an edebugged function, show where we last were.
3741 Otherwise call `debug' normally."
3742 ;;(message "entered: %s depth: %s edebug-recursion-depth: %s"
3743 ;; edebug-entered (recursion-depth) edebug-recursion-depth) (sit-for 1)
3744 (if (and edebug-entered ; anything active?
3745 (eq (recursion-depth) edebug-recursion-depth))
3746 (let (;; Where were we before the error occurred?
3747 (offset-index (car edebug-offset-indices))
3748 (value (car args))
3749 ;; Bind variables required by edebug--display.
3750 edebug-breakpoints
3751 edebug-break-data
3752 edebug-break-condition
3753 edebug-global-break
3754 (edebug-break (null arg-mode)) ;; If called explicitly.
3756 (edebug--display value offset-index arg-mode)
3757 (if (eq arg-mode 'error)
3759 value))
3761 ;; Otherwise call debug normally.
3762 ;; Still need to remove extraneous edebug calls from stack.
3763 (apply 'debug arg-mode args)
3767 (defun edebug-backtrace ()
3768 "Display a non-working backtrace. Better than nothing..."
3769 (interactive)
3770 (if (or (not edebug-backtrace-buffer)
3771 (null (buffer-name edebug-backtrace-buffer)))
3772 (setq edebug-backtrace-buffer
3773 (generate-new-buffer "*Backtrace*"))
3774 ;; Else, could just display edebug-backtrace-buffer.
3776 (with-output-to-temp-buffer (buffer-name edebug-backtrace-buffer)
3777 (setq edebug-backtrace-buffer standard-output)
3778 (let ((print-escape-newlines t)
3779 (print-length 50) ; FIXME cf edebug-safe-prin1-to-string
3780 last-ok-point)
3781 (backtrace)
3783 ;; Clean up the backtrace.
3784 ;; Not quite right for current edebug scheme.
3785 (set-buffer edebug-backtrace-buffer)
3786 (setq truncate-lines t)
3787 (goto-char (point-min))
3788 (setq last-ok-point (point))
3789 (if t (progn
3791 ;; Delete interspersed edebug internals.
3792 (while (re-search-forward "^ (?edebug" nil t)
3793 (beginning-of-line)
3794 (cond
3795 ((looking-at "^ (edebug-after")
3796 ;; Previous lines may contain code, so just delete this line.
3797 (setq last-ok-point (point))
3798 (forward-line 1)
3799 (delete-region last-ok-point (point)))
3801 ((looking-at "^ edebug")
3802 (forward-line 1)
3803 (delete-region last-ok-point (point))
3805 )))))
3808 ;;; Trace display
3810 (defun edebug-trace-display (buf-name fmt &rest args)
3811 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
3812 The buffer is created if it does not exist.
3813 You must include newlines in FMT to break lines, but one newline is appended."
3814 ;; e.g.
3815 ;; (edebug-trace-display "*trace-point*"
3816 ;; "saving: point = %s window-start = %s"
3817 ;; (point) (window-start))
3818 (let* ((oldbuf (current-buffer))
3819 (selected-window (selected-window))
3820 (buffer (get-buffer-create buf-name))
3821 buf-window)
3822 ;; (message "before pop-to-buffer") (sit-for 1)
3823 (edebug-pop-to-buffer buffer)
3824 (setq truncate-lines t)
3825 (setq buf-window (selected-window))
3826 (goto-char (point-max))
3827 (insert (apply 'edebug-format fmt args) "\n")
3828 ;; Make it visible.
3829 (vertical-motion (- 1 (window-height)))
3830 (set-window-start buf-window (point))
3831 (goto-char (point-max))
3832 ;; (set-window-point buf-window (point))
3833 ;; (sit-for 0)
3834 (bury-buffer buffer)
3835 (select-window selected-window)
3836 (set-buffer oldbuf))
3837 buf-name)
3840 (defun edebug-trace (fmt &rest args)
3841 "Convenience call to `edebug-trace-display' using `edebug-trace-buffer'."
3842 (apply 'edebug-trace-display edebug-trace-buffer fmt args))
3845 ;;; Frequency count and coverage
3847 ;; FIXME should this use overlays instead?
3848 ;; Definitely, IMO. The current business with undo in
3849 ;; edebug-temp-display-freq-count is horrid.
3850 (defun edebug-display-freq-count ()
3851 "Display the frequency count data for each line of the current definition.
3852 The frequency counts are inserted as comment lines after each line,
3853 and you can undo all insertions with one `undo' command.
3855 The counts are inserted starting under the `(' before an expression
3856 or the `)' after an expression, or on the last char of a symbol.
3857 The counts are only displayed when they differ from previous counts on
3858 the same line.
3860 If coverage is being tested, whenever all known results of an expression
3861 are `eq', the char `=' will be appended after the count
3862 for that expression. Note that this is always the case for an
3863 expression only evaluated once.
3865 To clear the frequency count and coverage data for a definition,
3866 reinstrument it."
3867 (interactive)
3868 (let* ((function (edebug-form-data-symbol))
3869 (counts (get function 'edebug-freq-count))
3870 (coverages (get function 'edebug-coverage))
3871 (data (get function 'edebug))
3872 (def-mark (car data)) ; mark at def start
3873 (edebug-points (nth 2 data))
3874 (i (1- (length edebug-points)))
3875 (last-index)
3876 (first-index)
3877 (start-of-line)
3878 (start-of-count-line)
3879 (last-count)
3881 (save-excursion
3882 ;; Traverse in reverse order so offsets are correct.
3883 (while (<= 0 i)
3884 ;; Start at last expression in line.
3885 (goto-char (+ def-mark (aref edebug-points i)))
3886 (beginning-of-line)
3887 (setq start-of-line (- (point) def-mark)
3888 last-index i)
3890 ;; Find all indexes on same line.
3891 (while (and (<= 0 (setq i (1- i)))
3892 (<= start-of-line (aref edebug-points i))))
3893 ;; Insert all the indices for this line.
3894 (forward-line 1)
3895 (setq start-of-count-line (point)
3896 first-index i ; Really, last index for line above this one.
3897 last-count -1) ; Cause first count to always appear.
3898 (insert ";#")
3899 ;; i == first-index still
3900 (while (<= (setq i (1+ i)) last-index)
3901 (let ((count (aref counts i))
3902 (coverage (aref coverages i))
3903 (col (save-excursion
3904 (goto-char (+ (aref edebug-points i) def-mark))
3905 (- (current-column)
3906 (if (= ?\( (following-char)) 0 1)))))
3907 (insert (make-string
3908 (max 0 (- col (- (point) start-of-count-line))) ?\s)
3909 (if (and (< 0 count)
3910 (not (memq coverage
3911 '(unknown ok-coverage))))
3912 "=" "")
3913 (if (= count last-count) "" (int-to-string count))
3914 " ")
3915 (setq last-count count)))
3916 (insert "\n")
3917 (setq i first-index)))))
3919 ;; FIXME this does not work very well. Eg if you press an arrow key,
3920 ;; or make a mouse-click, it fails with "Non-character input-event".
3921 (defun edebug-temp-display-freq-count ()
3922 "Temporarily display the frequency count data for the current definition.
3923 It is removed when you hit any char."
3924 ;; This seems not to work with Emacs 18.59. It undoes too far.
3925 (interactive)
3926 (let ((inhibit-read-only t))
3927 (undo-boundary)
3928 (edebug-display-freq-count)
3929 (setq unread-command-events
3930 (append unread-command-events (list (read-event))))
3931 ;; Yuck! This doesn't seem to work at all for me.
3932 (undo)))
3935 ;;; Menus
3937 (defun edebug-toggle (variable)
3938 (set variable (not (symbol-value variable)))
3939 (message "%s: %s" variable (symbol-value variable)))
3941 ;; We have to require easymenu (even for Emacs 18) just so
3942 ;; the easy-menu-define macro call is compiled correctly.
3943 (require 'easymenu)
3945 (defconst edebug-mode-menus
3946 '("Edebug"
3947 ["Stop" edebug-stop t]
3948 ["Step" edebug-step-mode t]
3949 ["Next" edebug-next-mode t]
3950 ["Trace" edebug-trace-mode t]
3951 ["Trace Fast" edebug-Trace-fast-mode t]
3952 ["Continue" edebug-continue-mode t]
3953 ["Continue Fast" edebug-Continue-fast-mode t]
3954 ["Go" edebug-go-mode t]
3955 ["Go Nonstop" edebug-Go-nonstop-mode t]
3956 "----"
3957 ["Help" edebug-help t]
3958 ["Abort" abort-recursive-edit t]
3959 ["Quit to Top Level" top-level t]
3960 ["Quit Nonstop" edebug-top-level-nonstop t]
3961 "----"
3962 ("Jumps"
3963 ["Forward Sexp" edebug-forward-sexp t]
3964 ["Step In" edebug-step-in t]
3965 ["Step Out" edebug-step-out t]
3966 ["Goto Here" edebug-goto-here t])
3968 ("Breaks"
3969 ["Set Breakpoint" edebug-set-breakpoint t]
3970 ["Unset Breakpoint" edebug-unset-breakpoint t]
3971 ["Set Conditional Breakpoint" edebug-set-conditional-breakpoint t]
3972 ["Set Global Break Condition" edebug-set-global-break-condition t]
3973 ["Show Next Breakpoint" edebug-next-breakpoint t])
3975 ("Views"
3976 ["Where am I?" edebug-where t]
3977 ["Bounce to Current Point" edebug-bounce-point t]
3978 ["View Outside Windows" edebug-view-outside t]
3979 ["Previous Result" edebug-previous-result t]
3980 ["Show Backtrace" edebug-backtrace t]
3981 ["Display Freq Count" edebug-display-freq-count t])
3983 ("Eval"
3984 ["Expression" edebug-eval-expression t]
3985 ["Last Sexp" edebug-eval-last-sexp t]
3986 ["Visit Eval List" edebug-visit-eval-list t])
3988 ("Options"
3989 ["Edebug All Defs" edebug-all-defs
3990 :style toggle :selected edebug-all-defs]
3991 ["Edebug All Forms" edebug-all-forms
3992 :style toggle :selected edebug-all-forms]
3993 "----"
3994 ["Tracing" (edebug-toggle 'edebug-trace)
3995 :style toggle :selected edebug-trace]
3996 ["Test Coverage" (edebug-toggle 'edebug-test-coverage)
3997 :style toggle :selected edebug-test-coverage]
3998 ["Save Windows" edebug-toggle-save-windows
3999 :style toggle :selected edebug-save-windows]
4000 ["Save Point"
4001 (edebug-toggle 'edebug-save-displayed-buffer-points)
4002 :style toggle :selected edebug-save-displayed-buffer-points]
4004 "Menus for Edebug.")
4007 ;;; Emacs version specific code
4009 (defalias 'edebug-window-live-p 'window-live-p)
4011 (defun edebug-mark ()
4012 (mark t))
4014 (defun edebug-set-conditional-breakpoint (arg condition)
4015 "Set a conditional breakpoint at nearest sexp.
4016 The condition is evaluated in the outside context.
4017 With prefix argument, make it a temporary breakpoint."
4018 ;; (interactive "P\nxCondition: ")
4019 (interactive
4020 (list
4021 current-prefix-arg
4022 ;; Read condition as follows; getting previous condition is cumbersome:
4023 (let ((edebug-stop-point (edebug-find-stop-point)))
4024 (if edebug-stop-point
4025 (let* ((edebug-def-name (car edebug-stop-point))
4026 (index (cdr edebug-stop-point))
4027 (edebug-data (get edebug-def-name 'edebug))
4028 (edebug-breakpoints (car (cdr edebug-data)))
4029 (edebug-break-data (assq index edebug-breakpoints))
4030 (edebug-break-condition (car (cdr edebug-break-data)))
4031 (initial (and edebug-break-condition
4032 (format "%s" edebug-break-condition))))
4033 (read-from-minibuffer
4034 "Condition: " initial read-expression-map t
4035 (if (equal (car read-expression-history) initial)
4036 '(read-expression-history . 1)
4037 'read-expression-history)))))))
4038 (edebug-modify-breakpoint t condition arg))
4040 (easy-menu-define edebug-menu edebug-mode-map "Edebug menus" edebug-mode-menus)
4042 ;;; Autoloading of Edebug accessories
4044 ;; edebug-cl-read and cl-read are available from liberte@cs.uiuc.edu
4045 (defun edebug--require-cl-read ()
4046 (require 'edebug-cl-read))
4048 (if (featurep 'cl-read)
4049 (add-hook 'edebug-setup-hook #'edebug--require-cl-read)
4050 ;; The following causes edebug-cl-read to be loaded when you load cl-read.el.
4051 (add-hook 'cl-read-load-hooks #'edebug--require-cl-read))
4054 ;;; Finalize Loading
4056 ;; When edebugging a function, some of the sub-expressions are
4057 ;; wrapped in (edebug-enter (lambda () ..)), so we need to teach
4058 ;; called-interactively-p that calls within the inner lambda should refer to
4059 ;; the outside function.
4060 (add-hook 'called-interactively-p-functions
4061 #'edebug--called-interactively-skip)
4062 (defun edebug--called-interactively-skip (i frame1 frame2)
4063 (when (and (eq (car-safe (nth 1 frame1)) 'lambda)
4064 (eq (nth 1 (nth 1 frame1)) '())
4065 (eq (nth 1 frame2) 'edebug-enter))
4066 ;; `edebug-enter' calls itself on its first invocation.
4067 (if (eq (nth 1 (backtrace-frame i 'called-interactively-p))
4068 'edebug-enter)
4069 2 1)))
4071 ;; Finally, hook edebug into the rest of Emacs.
4072 ;; There are probably some other things that could go here.
4074 ;; Install edebug read and eval functions.
4075 (edebug-install-read-eval-functions)
4077 (defun edebug-unload-function ()
4078 "Unload the Edebug source level debugger."
4079 (when edebug-active
4080 (setq edebug-active nil)
4081 (unwind-protect
4082 (abort-recursive-edit)
4083 ;; We still want to run unload-feature to completion
4084 (run-with-idle-timer 0 nil #'(lambda () (unload-feature 'edebug)))))
4085 (remove-hook 'called-interactively-p-functions
4086 'edebug--called-interactively-skip)
4087 (remove-hook 'cl-read-load-hooks 'edebug--require-cl-read)
4088 (edebug-uninstall-read-eval-functions)
4089 ;; Continue standard unloading.
4090 nil)
4092 (provide 'edebug)
4093 ;;; edebug.el ends here