Quote a few backticks in docstrings.
[emacs.git] / lisp / emacs-lisp / edebug.el
blob7e4d244f5e2124d93a543c383918c9dc18a021a9
1 ;;; edebug.el --- a source-level debugger for Emacs Lisp -*- lexical-binding: t -*-
3 ;; Copyright (C) 1988-1995, 1997, 1999-2018 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 <https://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-max-depth 150
116 "Maximum recursion depth when instrumenting code.
117 This limit is intended to stop recursion if an Edebug specification
118 contains an infinite loop. When Edebug is instrumenting code
119 containing very large quoted lists, it may reach this limit and give
120 the error message \"Too deep - perhaps infinite loop in spec?\".
121 Make this limit larger to countermand that, but you may also need to
122 increase `max-lisp-eval-depth' and `max-specpdl-size'."
123 :type 'integer
124 :group 'edebug
125 :version "26.1")
127 (defcustom edebug-save-windows t
128 "If non-nil, Edebug saves and restores the window configuration.
129 That takes some time, so if your program does not care what happens to
130 the window configurations, it is better to set this variable to nil.
132 If the value is a list, only the listed windows are saved and
133 restored.
135 `edebug-toggle-save-windows' may be used to change this variable."
136 :type '(choice boolean (repeat string))
137 :group 'edebug)
139 (defcustom edebug-save-displayed-buffer-points nil
140 "If non-nil, save and restore point in all displayed buffers.
142 Saving and restoring point in other buffers is necessary if you are
143 debugging code that changes the point of a buffer that is displayed
144 in a non-selected window. If Edebug or the user then selects the
145 window, the buffer's point will be changed to the window's point.
147 Saving and restoring point in all buffers is expensive, since it
148 requires selecting each window twice, so enable this only if you
149 need it."
150 :type 'boolean
151 :group 'edebug)
153 (defcustom edebug-initial-mode 'step
154 "Initial execution mode for Edebug, if non-nil.
155 If this variable is non-nil, it specifies the initial execution mode
156 for Edebug when it is first activated. Possible values are step, next,
157 go, Go-nonstop, trace, Trace-fast, continue, and Continue-fast."
158 :type '(choice (const step) (const next) (const go)
159 (const Go-nonstop) (const trace)
160 (const Trace-fast) (const continue)
161 (const Continue-fast))
162 :group 'edebug)
164 (defcustom edebug-trace nil
165 "Non-nil means display a trace of function entry and exit.
166 Tracing output is displayed in a buffer named `*edebug-trace*', one
167 function entry or exit per line, indented by the recursion level.
169 You can customize by replacing functions `edebug-print-trace-before'
170 and `edebug-print-trace-after'."
171 :type 'boolean
172 :group 'edebug)
174 (defcustom edebug-test-coverage nil
175 "If non-nil, Edebug tests coverage of all expressions debugged.
176 This is done by comparing the result of each expression with the
177 previous result. Coverage is considered OK if two different
178 results are found.
180 Use `edebug-display-freq-count' to display the frequency count and
181 coverage information for a definition."
182 :type 'boolean
183 :group 'edebug)
185 (defcustom edebug-continue-kbd-macro nil
186 "If non-nil, continue defining or executing any keyboard macro.
187 Use this with caution since it is not debugged."
188 :type 'boolean
189 :group 'edebug)
192 (defcustom edebug-print-length 50
193 "If non-nil, default value of `print-length' for printing results in Edebug."
194 :type 'integer
195 :group 'edebug)
196 (defcustom edebug-print-level 50
197 "If non-nil, default value of `print-level' for printing results in Edebug."
198 :type 'integer
199 :group 'edebug)
200 (defcustom edebug-print-circle t
201 "If non-nil, default value of `print-circle' for printing results in Edebug."
202 :type 'boolean
203 :group 'edebug)
205 (defcustom edebug-unwrap-results nil
206 "Non-nil if Edebug should unwrap results of expressions.
207 That is, Edebug will try to remove its own instrumentation from the result.
208 This is useful when debugging macros where the results of expressions
209 are instrumented expressions. But don't do this when results might be
210 circular or an infinite loop will result."
211 :type 'boolean
212 :group 'edebug)
214 (defcustom edebug-on-error t
215 "Value bound to `debug-on-error' while Edebug is active.
217 If `debug-on-error' is non-nil, that value is still used.
219 If the value is a list of signal names, Edebug will stop when any of
220 these errors are signaled from Lisp code whether or not the signal is
221 handled by a `condition-case'. This option is useful for debugging
222 signals that *are* handled since they would otherwise be missed.
223 After execution is resumed, the error is signaled again."
224 :type '(choice (const :tag "off")
225 (repeat :menu-tag "When"
226 :value (nil)
227 (symbol :format "%v"))
228 (const :tag "always" t))
229 :group 'edebug)
231 (defcustom edebug-on-quit t
232 "Value bound to `debug-on-quit' while Edebug is active."
233 :type 'boolean
234 :group 'edebug)
236 (defcustom edebug-global-break-condition nil
237 "If non-nil, an expression to test for at every stop point.
238 If the result is non-nil, then break. Errors are ignored."
239 :type 'sexp
240 :risky t
241 :group 'edebug)
243 (defcustom edebug-sit-for-seconds 1
244 "Number of seconds to pause when execution mode is `trace' or `continue'."
245 :type 'number
246 :group 'edebug)
248 (defcustom edebug-sit-on-break t
249 "Whether or not to pause for `edebug-sit-for-seconds' on reaching a break."
250 :type 'boolean
251 :group 'edebug
252 :version "26.1")
254 ;;; Form spec utilities.
256 (defun get-edebug-spec (symbol)
257 ;; Get the spec of symbol resolving all indirection.
258 (let ((spec nil)
259 (indirect symbol))
260 (while
261 (progn
262 (and (symbolp indirect)
263 (setq indirect
264 (function-get indirect 'edebug-form-spec 'macro))))
265 ;; (edebug-trace "indirection: %s" edebug-form-spec)
266 (setq spec indirect))
267 spec))
269 ;;;###autoload
270 (defun edebug-basic-spec (spec)
271 "Return t if SPEC uses only extant spec symbols.
272 An extant spec symbol is a symbol that is not a function and has a
273 `edebug-form-spec' property."
274 (cond ((listp spec)
275 (catch 'basic
276 (while spec
277 (unless (edebug-basic-spec (car spec)) (throw 'basic nil))
278 (setq spec (cdr spec)))
280 ((symbolp spec)
281 (unless (functionp spec) (function-get spec 'edebug-form-spec)))))
283 ;;; Utilities
285 (defun edebug-lambda-list-keywordp (object)
286 "Return t if OBJECT is a lambda list keyword.
287 A lambda list keyword is a symbol that starts with `&'."
288 (and (symbolp object)
289 (= ?& (aref (symbol-name object) 0))))
292 (defun edebug-last-sexp ()
293 ;; Return the last sexp before point in current buffer.
294 ;; Assumes Emacs Lisp syntax is active.
295 (car
296 (read-from-string
297 (buffer-substring
298 (save-excursion
299 (forward-sexp -1)
300 (point))
301 (point)))))
303 (defun edebug-window-list ()
304 "Return a list of windows, in order of `next-window'."
305 ;; This doesn't work for epoch.
306 (let (window-list)
307 (walk-windows (lambda (w) (push w window-list)))
308 (nreverse window-list)))
310 ;; Not used.
311 '(defun edebug-two-window-p ()
312 "Return t if there are two windows."
313 (and (not (one-window-p))
314 (eq (selected-window)
315 (next-window (next-window)))))
317 (defun edebug-sort-alist (alist function)
318 ;; Return the ALIST sorted with comparison function FUNCTION.
319 ;; This uses 'sort so the sorting is destructive.
320 (sort alist (function
321 (lambda (e1 e2)
322 (funcall function (car e1) (car e2))))))
324 ;; Not used.
325 '(defmacro edebug-save-restriction (&rest body)
326 "Evaluate BODY while saving the current buffers restriction.
327 BODY may change buffer outside of current restriction, unlike
328 save-restriction. BODY may change the current buffer,
329 and the restriction will be restored to the original buffer,
330 and the current buffer remains current.
331 Return the result of the last expression in BODY."
332 (declare (debug t))
333 `(let ((edebug:s-r-beg (point-min-marker))
334 (edebug:s-r-end (point-max-marker)))
335 (unwind-protect
336 (progn ,@body)
337 (with-current-buffer (marker-buffer edebug:s-r-beg)
338 (narrow-to-region edebug:s-r-beg edebug:s-r-end)))))
340 ;;; Display
342 (defconst edebug-trace-buffer "*edebug-trace*"
343 "Name of the buffer to put trace info in.")
345 (defun edebug-pop-to-buffer (buffer &optional window)
346 ;; Like pop-to-buffer, but select window where BUFFER was last shown.
347 ;; Select WINDOW if it is provided and still exists. Otherwise,
348 ;; if buffer is currently shown in several windows, choose one.
349 ;; Otherwise, find a new window, possibly splitting one.
350 ;; FIXME: We should probably just be using `pop-to-buffer'.
351 (setq window
352 (cond
353 ((and (edebug-window-live-p window)
354 (eq (window-buffer window) buffer))
355 window)
356 ((eq (window-buffer) buffer)
357 ;; Selected window already displays BUFFER.
358 (selected-window))
359 ((get-buffer-window buffer 0))
360 ((one-window-p 'nomini)
361 ;; When there's one window only, split it.
362 (split-window (minibuffer-selected-window)))
363 ((let ((trace-window (get-buffer-window edebug-trace-buffer)))
364 (catch 'found
365 (dolist (elt (window-list nil 'nomini))
366 (unless (or (eq elt (selected-window)) (eq elt trace-window)
367 (window-dedicated-p elt))
368 ;; Found a non-dedicated window not showing
369 ;; `edebug-trace-buffer', use it.
370 (throw 'found elt))))))
371 ;; All windows are dedicated or show `edebug-trace-buffer', split
372 ;; selected one.
373 (t (split-window (minibuffer-selected-window)))))
374 (set-window-buffer window buffer)
375 (select-window window)
376 (set-window-hscroll window 0)) ;; should this be??
378 (defun edebug-get-displayed-buffer-points ()
379 ;; Return a list of buffer point pairs, for all displayed buffers.
380 (let (list)
381 (walk-windows (lambda (w)
382 (unless (eq w (selected-window))
383 (push (cons (window-buffer w)
384 (window-point w))
385 list))))
386 list))
389 (defun edebug-set-buffer-points (buffer-points)
390 ;; Restore the buffer-points created by edebug-get-displayed-buffer-points.
391 (save-current-buffer
392 (mapcar (lambda (buf-point)
393 (when (buffer-live-p (car buf-point))
394 (set-buffer (car buf-point))
395 (goto-char (cdr buf-point))))
396 buffer-points)))
398 (defun edebug-current-windows (which-windows)
399 ;; Get either a full window configuration or some window information.
400 (if (listp which-windows)
401 (mapcar (lambda (window)
402 (if (edebug-window-live-p window)
403 (list window
404 (window-buffer window)
405 (window-point window)
406 (window-start window)
407 (window-hscroll window))))
408 which-windows)
409 (current-window-configuration)))
411 (defun edebug-set-windows (window-info)
412 ;; Set either a full window configuration or some window information.
413 (if (listp window-info)
414 (mapcar (lambda (one-window-info)
415 (if one-window-info
416 (apply (function
417 (lambda (window buffer point start hscroll)
418 (if (edebug-window-live-p window)
419 (progn
420 (set-window-buffer window buffer)
421 (set-window-point window point)
422 (set-window-start window start)
423 (set-window-hscroll window hscroll)))))
424 one-window-info)))
425 window-info)
426 (set-window-configuration window-info)))
428 ;;; Redefine read and eval functions
429 ;; read is redefined to maybe instrument forms.
430 ;; eval-defun is redefined to check edebug-all-forms and edebug-all-defs.
432 (defun edebug--read (orig &optional stream)
433 "Read one Lisp expression as text from STREAM, return as Lisp object.
434 If STREAM is nil, use the value of `standard-input' (which see).
435 STREAM or the value of `standard-input' may be:
436 a buffer (read from point and advance it)
437 a marker (read from where it points and advance it)
438 a function (call it with no arguments for each character,
439 call it with a char as argument to push a char back)
440 a string (takes text from string, starting at the beginning)
441 t (read text line using minibuffer and use it).
443 This version, from Edebug, maybe instruments the expression. But the
444 STREAM must be the current buffer to do so. Whether it instruments is
445 also dependent on the values of the option `edebug-all-defs' and
446 the option `edebug-all-forms'."
447 (or stream (setq stream standard-input))
448 (if (eq stream (current-buffer))
449 (edebug-read-and-maybe-wrap-form)
450 (funcall (or orig #'read) stream)))
452 (defvar edebug-result) ; The result of the function call returned by body.
454 ;; We should somehow arrange to be able to do this
455 ;; without actually replacing the eval-defun command.
456 (defun edebug-eval-defun (edebug-it)
457 "Evaluate the top-level form containing point, or after point.
459 If the current defun is actually a call to `defvar', then reset the
460 variable using its initial value expression even if the variable
461 already has some other value. (Normally `defvar' does not change the
462 variable's value if it already has a value.) Treat `defcustom'
463 similarly. Reinitialize the face according to `defface' specification.
465 With a prefix argument, instrument the code for Edebug.
467 Setting option `edebug-all-defs' to a non-nil value reverses the meaning
468 of the prefix argument. Code is then instrumented when this function is
469 invoked without a prefix argument.
471 If acting on a `defun' for FUNCTION, and the function was instrumented,
472 `Edebug: FUNCTION' is printed in the minibuffer. If not instrumented,
473 just FUNCTION is printed.
475 If not acting on a `defun', the result of evaluation is displayed in
476 the minibuffer."
477 (interactive "P")
478 (let* ((edebugging (not (eq (not edebug-it) (not edebug-all-defs))))
479 (edebug-result)
480 (form
481 (let ((edebug-all-forms edebugging)
482 (edebug-all-defs (eq edebug-all-defs (not edebug-it))))
483 (edebug-read-top-level-form))))
484 ;; This should be consistent with `eval-defun-1', but not the
485 ;; same, since that gets a macroexpanded form.
486 (cond ((and (eq (car form) 'defvar)
487 (cdr-safe (cdr-safe form)))
488 ;; Force variable to be bound.
489 (makunbound (nth 1 form)))
490 ((and (eq (car form) 'defcustom)
491 (default-boundp (nth 1 form)))
492 ;; Force variable to be bound.
493 ;; FIXME: Shouldn't this use the :setter or :initializer?
494 (set-default (nth 1 form) (eval (nth 2 form) lexical-binding)))
495 ((eq (car form) 'defface)
496 ;; Reset the face.
497 (setq face-new-frame-defaults
498 (assq-delete-all (nth 1 form) face-new-frame-defaults))
499 (put (nth 1 form) 'face-defface-spec nil)
500 (put (nth 1 form) 'face-documentation (nth 3 form))
501 ;; See comments in `eval-defun-1' for purpose of code below
502 (setq form (prog1 `(prog1 ,form
503 (put ',(nth 1 form) 'saved-face
504 ',(get (nth 1 form) 'saved-face))
505 (put ',(nth 1 form) 'customized-face
506 ,(nth 2 form)))
507 (put (nth 1 form) 'saved-face nil)))))
508 (setq edebug-result (eval (eval-sexp-add-defvars form) lexical-binding))
509 (if (not edebugging)
510 (prog1
511 (prin1 edebug-result)
512 (let ((str (eval-expression-print-format edebug-result)))
513 (if str (princ str))))
514 edebug-result)))
517 ;;;###autoload
518 (defalias 'edebug-defun 'edebug-eval-top-level-form)
520 ;;;###autoload
521 (defun edebug-eval-top-level-form ()
522 "Evaluate the top level form point is in, stepping through with Edebug.
523 This is like `eval-defun' except that it steps the code for Edebug
524 before evaluating it. It displays the value in the echo area
525 using `eval-expression' (which see).
527 If you do this on a function definition such as a defun or defmacro,
528 it defines the function and instruments its definition for Edebug,
529 so it will do Edebug stepping when called later. It displays
530 `Edebug: FUNCTION' in the echo area to indicate that FUNCTION is now
531 instrumented for Edebug.
533 If the current defun is actually a call to `defvar' or `defcustom',
534 evaluating it this way resets the variable using its initial value
535 expression even if the variable already has some other value.
536 \(Normally `defvar' and `defcustom' do not alter the value if there
537 already is one.)"
538 (interactive)
539 (eval-expression
540 ;; Bind edebug-all-forms only while reading, not while evalling
541 ;; but this causes problems while edebugging edebug.
542 (let ((edebug-all-forms t)
543 (edebug-all-defs t))
544 (eval-sexp-add-defvars
545 (edebug-read-top-level-form)))))
548 (defun edebug-read-top-level-form ()
549 (let ((starting-point (point)))
550 (end-of-defun)
551 (beginning-of-defun)
552 (prog1
553 (edebug-read-and-maybe-wrap-form)
554 ;; Recover point, but only if no error occurred.
555 (goto-char starting-point))))
558 ;; Compatibility with old versions.
559 (defalias 'edebug-all-defuns 'edebug-all-defs)
561 ;;;###autoload
562 (defun edebug-all-defs ()
563 "Toggle edebugging of all definitions."
564 (interactive)
565 (setq edebug-all-defs (not edebug-all-defs))
566 (message "Edebugging all definitions is %s."
567 (if edebug-all-defs "on" "off")))
570 ;;;###autoload
571 (defun edebug-all-forms ()
572 "Toggle edebugging of all forms."
573 (interactive)
574 (setq edebug-all-forms (not edebug-all-forms))
575 (message "Edebugging all forms is %s."
576 (if edebug-all-forms "on" "off")))
579 (defun edebug-install-read-eval-functions ()
580 (interactive)
581 (add-function :around load-read-function #'edebug--read)
582 (advice-add 'eval-defun :override #'edebug-eval-defun))
584 (defun edebug-uninstall-read-eval-functions ()
585 (interactive)
586 (remove-function load-read-function #'edebug--read)
587 (advice-remove 'eval-defun 'edebug-eval-defun))
589 ;;; Edebug internal data
591 ;; The internal data that is needed for edebugging is kept in the
592 ;; buffer-local variable `edebug-form-data'.
594 (defvar-local edebug-form-data nil
595 "A list of entries associating symbols with buffer regions.
596 Each entry is an `edebug--form-data' struct with fields:
597 SYMBOL, BEGIN-MARKER, and END-MARKER. The markers
598 are at the beginning and end of an entry level form and SYMBOL is
599 a symbol that holds all edebug related information for the form on its
600 property list.
602 In the future (haha!), the symbol will be irrelevant and edebug data will
603 be stored in the definitions themselves rather than in the property
604 list of a symbol.")
606 (cl-defstruct (edebug--form-data
607 ;; Some callers expect accessors to return nil when passed nil.
608 (:type list)
609 (:constructor edebug--make-form-data-entry (name begin end))
610 (:predicate nil) (:constructor nil) (:copier nil))
611 name begin end)
613 (defsubst edebug-set-form-data-entry (entry name begin end)
614 (setf (edebug--form-data-name entry) name) ;; In case name is changed.
615 (set-marker (edebug--form-data-begin entry) begin)
616 (set-marker (edebug--form-data-end entry) end))
618 (defun edebug-get-form-data-entry (pnt &optional end-point)
619 ;; Find the edebug form data entry which is closest to PNT.
620 ;; If END-POINT is supplied, match must be exact.
621 ;; Return nil if none found.
622 (let ((rest edebug-form-data)
623 closest-entry
624 (closest-dist 999999)) ;; Need maxint here.
625 (while (and rest (< 0 closest-dist))
626 (let* ((entry (car rest))
627 (begin (edebug--form-data-begin entry))
628 (dist (- pnt begin)))
629 (setq rest (cdr rest))
630 (if (and (<= 0 dist)
631 (< dist closest-dist)
632 (or (not end-point)
633 (= end-point (edebug--form-data-end entry)))
634 (<= pnt (edebug--form-data-end entry)))
635 (setq closest-dist dist
636 closest-entry entry))))
637 closest-entry))
639 ;; Also need to find all contained entries,
640 ;; and find an entry given a symbol, which should be just assq.
642 (defun edebug-form-data-symbol ()
643 "Return the edebug data symbol of the form where point is in.
644 If point is not inside an edebuggable form, signal an error."
645 (or (edebug--form-data-name (edebug-get-form-data-entry (point)))
646 (error "Not inside instrumented form")))
648 (defun edebug-make-top-form-data-entry (new-entry)
649 ;; Make NEW-ENTRY the first element in the `edebug-form-data' list.
650 (edebug-clear-form-data-entry new-entry)
651 (push new-entry edebug-form-data))
653 (defun edebug-clear-form-data-entry (entry)
654 "If non-nil, clear ENTRY out of the form data.
655 Maybe clear the markers and delete the symbol's edebug property?"
656 (if entry
657 (progn
658 ;; Instead of this, we could just find all contained forms.
659 ;; (put (car entry) 'edebug nil) ;
660 ;; (mapcar #'edebug-clear-form-data-entry ; dangerous
661 ;; (get (car entry) 'edebug-dependents))
662 ;; (set-marker (nth 1 entry) nil)
663 ;; (set-marker (nth 2 entry) nil)
664 (setq edebug-form-data (delq entry edebug-form-data)))))
666 ;;; Parser utilities
668 (defun edebug-syntax-error (&rest args)
669 ;; Signal an invalid-read-syntax with ARGS.
670 (signal 'invalid-read-syntax args))
673 (defconst edebug-read-syntax-table
674 ;; Lookup table for significant characters indicating the class of the
675 ;; token that follows. This is not a \"real\" syntax table.
676 (let ((table (make-char-table 'syntax-table 'symbol))
677 (i 0))
678 (while (< i ?!)
679 (aset table i 'space)
680 (setq i (1+ i)))
681 (aset table ?\( 'lparen)
682 (aset table ?\) 'rparen)
683 (aset table ?\' 'quote)
684 (aset table ?\` 'backquote)
685 (aset table ?\, 'comma)
686 (aset table ?\" 'string)
687 (aset table ?\? 'char)
688 (aset table ?\[ 'lbracket)
689 (aset table ?\] 'rbracket)
690 (aset table ?\. 'dot)
691 (aset table ?\# 'hash)
692 ;; We treat numbers as symbols, because of confusion with -, -1, and 1-.
693 ;; We don't care about any other chars since they won't be seen.
694 table))
696 (defun edebug-next-token-class ()
697 ;; Move to the next token and return its class. We only care about
698 ;; lparen, rparen, dot, quote, backquote, comma, string, char, vector,
699 ;; or symbol.
700 (edebug-skip-whitespace)
701 (if (and (eq (following-char) ?.)
702 (save-excursion
703 (forward-char 1)
704 (or (and (eq (aref edebug-read-syntax-table (following-char))
705 'symbol)
706 (not (= (following-char) ?\;)))
707 (memq (following-char) '(?\, ?\.)))))
708 'symbol
709 (aref edebug-read-syntax-table (following-char))))
712 (defun edebug-skip-whitespace ()
713 ;; Leave point before the next token, skipping white space and comments.
714 (skip-chars-forward " \t\r\n\f")
715 (while (= (following-char) ?\;)
716 (skip-chars-forward "^\n") ; skip the comment
717 (skip-chars-forward " \t\r\n\f")))
720 ;; Mostly obsolete reader; still used in one case.
722 (defun edebug-read-sexp ()
723 ;; Read one sexp from the current buffer starting at point.
724 ;; Leave point immediately after it. A sexp can be a list or atom.
725 ;; An atom is a symbol (or number), character, string, or vector.
726 ;; This works for reading anything legitimate, but it
727 ;; is gummed up by parser inconsistencies (bugs?)
728 (let ((class (edebug-next-token-class)))
729 (cond
730 ;; read goes one too far if a (possibly quoted) string or symbol
731 ;; is immediately followed by non-whitespace.
732 ((eq class 'symbol) (read (current-buffer)))
733 ((eq class 'string) (read (current-buffer)))
734 ((eq class 'quote) (forward-char 1)
735 (list 'quote (edebug-read-sexp)))
736 ((eq class 'backquote) (forward-char 1)
737 (list '\` (edebug-read-sexp)))
738 ((eq class 'comma) (forward-char 1)
739 (list '\, (edebug-read-sexp)))
740 (t ; anything else, just read it.
741 (read (current-buffer))))))
743 ;;; Offsets for reader
745 ;; Define a structure to represent offset positions of expressions.
746 ;; Each offset structure looks like: (before . after) for constituents,
747 ;; or for structures that have elements: (before <subexpressions> . after)
748 ;; where the <subexpressions> are the offset structures for subexpressions
749 ;; including the head of a list.
750 (defvar edebug-offsets nil)
752 ;; Stack of offset structures in reverse order of the nesting.
753 ;; This is used to get back to previous levels.
754 (defvar edebug-offsets-stack nil)
755 (defvar edebug-current-offset nil) ; Top of the stack, for convenience.
757 ;; The association list of objects read with the #n=object form.
758 ;; Each member of the list has the form (n . object), and is used to
759 ;; look up the object for the corresponding #n# construct.
760 (defvar edebug-read-objects nil)
762 ;; We must store whether we just read a list with a dotted form that
763 ;; is itself a list. This structure will be condensed, so the offsets
764 ;; must also be condensed.
765 (defvar edebug-read-dotted-list nil)
767 (defsubst edebug-initialize-offsets ()
768 ;; Reinitialize offset recording.
769 (setq edebug-current-offset nil))
771 (defun edebug-store-before-offset (point)
772 ;; Add a new offset pair with POINT as the before offset.
773 (let ((new-offset (list point)))
774 (if edebug-current-offset
775 (setcdr edebug-current-offset
776 (cons new-offset (cdr edebug-current-offset)))
777 ;; Otherwise, we are at the top level, so initialize.
778 (setq edebug-offsets new-offset
779 edebug-offsets-stack nil
780 edebug-read-dotted-list nil))
781 ;; Cons the new offset to the front of the stack.
782 (setq edebug-offsets-stack (cons new-offset edebug-offsets-stack)
783 edebug-current-offset new-offset)
786 (defun edebug-store-after-offset (point)
787 ;; Finalize the current offset struct by reversing it and
788 ;; store POINT as the after offset.
789 (if (not edebug-read-dotted-list)
790 ;; Just reverse the offsets of all subexpressions.
791 (setcdr edebug-current-offset (nreverse (cdr edebug-current-offset)))
793 ;; We just read a list after a dot, which will be abbreviated out.
794 (setq edebug-read-dotted-list nil)
795 ;; Drop the corresponding offset pair.
796 ;; That is, nconc the reverse of the rest of the offsets
797 ;; with the cdr of last offset.
798 (setcdr edebug-current-offset
799 (nconc (nreverse (cdr (cdr edebug-current-offset)))
800 (cdr (car (cdr edebug-current-offset))))))
802 ;; Now append the point using nconc.
803 (setq edebug-current-offset (nconc edebug-current-offset point))
804 ;; Pop the stack.
805 (setq edebug-offsets-stack (cdr edebug-offsets-stack)
806 edebug-current-offset (car edebug-offsets-stack)))
808 (defun edebug-ignore-offset ()
809 ;; Ignore the last created offset pair.
810 (setcdr edebug-current-offset (cdr (cdr edebug-current-offset))))
812 (defmacro edebug-storing-offsets (point &rest body)
813 (declare (debug (form body)) (indent 1))
814 `(unwind-protect
815 (progn
816 (edebug-store-before-offset ,point)
817 ,@body)
818 (edebug-store-after-offset (point))))
821 ;;; Reader for Emacs Lisp.
823 ;; Uses edebug-next-token-class (and edebug-skip-whitespace) above.
825 (defconst edebug-read-alist
826 '((symbol . edebug-read-symbol)
827 (lparen . edebug-read-list)
828 (string . edebug-read-string)
829 (quote . edebug-read-quote)
830 (backquote . edebug-read-backquote)
831 (comma . edebug-read-comma)
832 (lbracket . edebug-read-vector)
833 (hash . edebug-read-special)
836 (defun edebug-read-storing-offsets (stream)
837 (let (edebug-read-dotted-list) ; see edebug-store-after-offset
838 (edebug-storing-offsets (point)
839 (funcall
840 (or (cdr (assq (edebug-next-token-class) edebug-read-alist))
841 ;; anything else, just read it.
842 #'read)
843 stream))))
845 (defalias 'edebug-read-symbol #'read)
846 (defalias 'edebug-read-string #'read)
848 (defun edebug-read-quote (stream)
849 ;; Turn 'thing into (quote thing)
850 (forward-char 1)
851 (list
852 (edebug-storing-offsets (1- (point)) 'quote)
853 (edebug-read-storing-offsets stream)))
855 (defun edebug-read-backquote (stream)
856 ;; Turn `thing into (\` thing)
857 (forward-char 1)
858 (list
859 (edebug-storing-offsets (1- (point)) '\`)
860 (edebug-read-storing-offsets stream)))
862 (defun edebug-read-comma (stream)
863 ;; Turn ,thing into (\, thing). Handle ,@ and ,. also.
864 (let ((opoint (point)))
865 (forward-char 1)
866 (let ((symbol '\,))
867 (cond ((eq (following-char) ?\.)
868 (setq symbol '\,\.)
869 (forward-char 1))
870 ((eq (following-char) ?\@)
871 (setq symbol '\,@)
872 (forward-char 1)))
873 ;; Generate the same structure of offsets we would have
874 ;; if the resulting list appeared verbatim in the input text.
875 (list
876 (edebug-storing-offsets opoint symbol)
877 (edebug-read-storing-offsets stream)))))
879 (defun edebug-read-special (stream)
880 "Read from STREAM a Lisp object beginning with #.
881 Turn #'thing into (function thing) and handle the read syntax for
882 circular objects. Let `read' read everything else."
883 (catch 'return
884 (forward-char 1)
885 (let ((start (point)))
886 (cond
887 ((eq ?\' (following-char))
888 (forward-char 1)
889 (throw 'return
890 (list
891 (edebug-storing-offsets (- (point) 2) 'function)
892 (edebug-read-storing-offsets stream))))
893 ((and (>= (following-char) ?0) (<= (following-char) ?9))
894 (while (and (>= (following-char) ?0) (<= (following-char) ?9))
895 (forward-char 1))
896 (let ((n (string-to-number (buffer-substring start (point)))))
897 (when (and read-circle
898 (<= n most-positive-fixnum))
899 (cond
900 ((eq ?= (following-char))
901 ;; Make a placeholder for #n# to use temporarily.
902 (let* ((placeholder (cons nil nil))
903 (elem (cons n placeholder)))
904 (push elem edebug-read-objects)
905 ;; Read the object and then replace the placeholder
906 ;; with the object itself, wherever it occurs.
907 (forward-char 1)
908 (let ((obj (edebug-read-storing-offsets stream)))
909 (lread--substitute-object-in-subtree obj placeholder t)
910 (throw 'return (setf (cdr elem) obj)))))
911 ((eq ?# (following-char))
912 ;; #n# returns a previously read object.
913 (let ((elem (assq n edebug-read-objects)))
914 (when (consp elem)
915 (forward-char 1)
916 (throw 'return (cdr elem))))))))))
917 ;; Let read handle errors, radix notation, and anything else.
918 (goto-char (1- start))
919 (read stream))))
921 (defun edebug-read-list (stream)
922 (forward-char 1) ; skip \(
923 (prog1
924 (let ((elements))
925 (while (not (memq (edebug-next-token-class) '(rparen dot)))
926 (push (edebug-read-storing-offsets stream) elements))
927 (setq elements (nreverse elements))
928 (if (eq 'dot (edebug-next-token-class))
929 (let (dotted-form)
930 (forward-char 1) ; skip \.
931 (setq dotted-form (edebug-read-storing-offsets stream))
932 elements (nconc elements dotted-form)
933 (if (not (eq (edebug-next-token-class) 'rparen))
934 (edebug-syntax-error "Expected `)'"))
935 (setq edebug-read-dotted-list (listp dotted-form))
937 elements)
938 (forward-char 1) ; skip \)
941 (defun edebug-read-vector (stream)
942 (forward-char 1) ; skip \[
943 (prog1
944 (let ((elements))
945 (while (not (eq 'rbracket (edebug-next-token-class)))
946 (push (edebug-read-storing-offsets stream) elements))
947 (apply #'vector (nreverse elements)))
948 (forward-char 1) ; skip \]
951 ;;; Cursors for traversal of list and vector elements with offsets.
953 (defvar edebug-dotted-spec nil
954 "Set to t when matching after the dot in a dotted spec list.")
956 (defun edebug-new-cursor (expressions offsets)
957 ;; Return a new cursor for EXPRESSIONS with OFFSETS.
958 (if (vectorp expressions)
959 (setq expressions (append expressions nil)))
960 (cons expressions offsets))
962 (defsubst edebug-set-cursor (cursor expressions offsets)
963 ;; Set the CURSOR's EXPRESSIONS and OFFSETS to the given.
964 ;; Return the cursor.
965 (setcar cursor expressions)
966 (setcdr cursor offsets)
967 cursor)
969 (defun edebug-copy-cursor (cursor)
970 ;; Copy the cursor using the same object and offsets.
971 (cons (car cursor) (cdr cursor)))
973 (defsubst edebug-cursor-expressions (cursor)
974 (car cursor))
975 (defsubst edebug-cursor-offsets (cursor)
976 (cdr cursor))
978 (defsubst edebug-empty-cursor (cursor)
979 ;; Return non-nil if CURSOR is empty - meaning no more elements.
980 (null (car cursor)))
982 (defsubst edebug-top-element (cursor)
983 ;; Return the top element at the cursor.
984 ;; Assumes not empty.
985 (car (car cursor)))
987 (defun edebug-top-element-required (cursor &rest error)
988 ;; Check if a dotted form is required.
989 (if edebug-dotted-spec (edebug-no-match cursor "Dot expected."))
990 ;; Check if there is at least one more argument.
991 (if (edebug-empty-cursor cursor) (apply #'edebug-no-match cursor error))
992 ;; Return that top element.
993 (edebug-top-element cursor))
995 (defsubst edebug-top-offset (cursor)
996 ;; Return the top offset pair corresponding to the top element.
997 (car (cdr cursor)))
999 (defun edebug-move-cursor (cursor)
1000 ;; Advance and return the cursor to the next element and offset.
1001 ;; throw no-match if empty before moving.
1002 ;; This is a violation of the cursor encapsulation, but
1003 ;; there is plenty of that going on while matching.
1004 ;; The following test should always fail.
1005 (if (edebug-empty-cursor cursor)
1006 (edebug-no-match cursor "Not enough arguments."))
1007 (setcar cursor (cdr (car cursor)))
1008 (setcdr cursor (cdr (cdr cursor)))
1009 cursor)
1012 (defun edebug-before-offset (cursor)
1013 ;; Return the before offset of the cursor.
1014 ;; If there is nothing left in the offsets,
1015 ;; return one less than the offset itself,
1016 ;; which is the after offset for a list.
1017 (let ((offset (edebug-cursor-offsets cursor)))
1018 (if (consp offset)
1019 (car (car offset))
1020 (1- offset))))
1022 (defun edebug-after-offset (cursor)
1023 ;; Return the after offset of the cursor object.
1024 (let ((offset (edebug-top-offset cursor)))
1025 (while (consp offset)
1026 (setq offset (cdr offset)))
1027 offset))
1029 ;;; The Parser
1031 ;; The top level function for parsing forms is
1032 ;; edebug-read-and-maybe-wrap-form; it calls all the rest. It checks the
1033 ;; syntax a bit and leaves point at any error it finds, but otherwise
1034 ;; should appear to work like eval-defun.
1036 ;; The basic plan is to surround each expression with a call to
1037 ;; the edebug debugger together with indexes into a table of positions of
1038 ;; all expressions. Thus an expression "exp" becomes:
1040 ;; (edebug-after (edebug-before 1) 2 exp)
1042 ;; When this is evaluated, first point is moved to the beginning of
1043 ;; exp at offset 1 of the current function. The expression is
1044 ;; evaluated, which may cause more edebug calls, and then point is
1045 ;; moved to offset 2 after the end of exp.
1047 ;; The highest level expressions of the function are wrapped in a call to
1048 ;; edebug-enter, which supplies the function name and the actual
1049 ;; arguments to the function. See functions edebug-enter, edebug-before,
1050 ;; and edebug-after for more details.
1052 ;; Dynamically bound vars, left unbound, but globally declared.
1053 ;; This is to quiet the byte compiler.
1055 ;; Window data of the highest definition being wrapped.
1056 ;; This data is shared by all embedded definitions.
1057 (defvar edebug-top-window-data)
1059 (defvar edebug-&optional)
1060 (defvar edebug-&rest)
1061 (defvar edebug-gate nil) ;; whether no-match forces an error.
1063 (defvar edebug-def-name nil) ; name of definition, used by interactive-form
1064 (defvar edebug-old-def-name nil) ; previous name of containing definition.
1066 (defvar edebug-error-point nil)
1067 (defvar edebug-best-error nil)
1070 (defun edebug-read-and-maybe-wrap-form ()
1071 ;; Read a form and wrap it with edebug calls, if the conditions are right.
1072 ;; Here we just catch any no-match not caught below and signal an error.
1074 ;; Run the setup hook.
1075 ;; If it gets an error, make it nil.
1076 (let ((temp-hook edebug-setup-hook))
1077 (setq edebug-setup-hook nil)
1078 (if (functionp temp-hook) (funcall temp-hook)
1079 (mapc #'funcall temp-hook)))
1081 (let (result
1082 edebug-top-window-data
1083 edebug-def-name;; make sure it is locally nil
1084 ;; I don't like these here!!
1085 edebug-&optional
1086 edebug-&rest
1087 edebug-gate
1088 edebug-best-error
1089 edebug-error-point
1090 ;; Do this once here instead of several times.
1091 (max-lisp-eval-depth (+ 800 max-lisp-eval-depth))
1092 (max-specpdl-size (+ 2000 max-specpdl-size)))
1093 (let ((no-match
1094 (catch 'no-match
1095 (setq result (edebug-read-and-maybe-wrap-form1))
1096 nil)))
1097 (if no-match
1098 (apply #'edebug-syntax-error no-match)))
1099 result))
1102 (defun edebug-read-and-maybe-wrap-form1 ()
1103 (let (spec
1104 def-kind
1105 defining-form-p
1106 def-name
1107 ;; These offset things don't belong here, but to support recursive
1108 ;; calls to edebug-read, they need to be here.
1109 edebug-offsets
1110 edebug-offsets-stack
1111 edebug-current-offset ; reset to nil
1112 edebug-read-objects
1114 (save-excursion
1115 (if (and (eq 'lparen (edebug-next-token-class))
1116 (eq 'symbol (progn (forward-char 1) (edebug-next-token-class))))
1117 ;; Find out if this is a defining form from first symbol
1118 (setq def-kind (read (current-buffer))
1119 spec (and (symbolp def-kind) (get-edebug-spec def-kind))
1120 defining-form-p (and (listp spec)
1121 (eq '&define (car spec)))
1122 ;; This is incorrect in general!! But OK most of the time.
1123 def-name (if (and defining-form-p
1124 (eq 'name (car (cdr spec)))
1125 (eq 'symbol (edebug-next-token-class)))
1126 (read (current-buffer))))))
1127 ;;;(message "all defs: %s all forms: %s" edebug-all-defs edebug-all-forms)
1128 (cond
1129 (defining-form-p
1130 (if (or edebug-all-defs edebug-all-forms)
1131 ;; If it is a defining form and we are edebugging defs,
1132 ;; then let edebug-list-form start it.
1133 (let ((cursor (edebug-new-cursor
1134 (list (edebug-read-storing-offsets (current-buffer)))
1135 (list edebug-offsets))))
1136 (car
1137 (edebug-make-form-wrapper
1138 cursor
1139 (edebug-before-offset cursor)
1140 (1- (edebug-after-offset cursor))
1141 (list (cons (symbol-name def-kind) (cdr spec))))))
1143 ;; Not edebugging this form, so reset the symbol's edebug
1144 ;; property to be just a marker at the definition's source code.
1145 ;; This only works for defs with simple names.
1146 (put def-name 'edebug (point-marker))
1147 ;; Also nil out dependent defs.
1148 '(mapcar (function
1149 (lambda (def)
1150 (put def-name 'edebug nil)))
1151 (get def-name 'edebug-dependents))
1152 (edebug-read-sexp)))
1154 ;; If all forms are being edebugged, explicitly wrap it.
1155 (edebug-all-forms
1156 (let ((cursor (edebug-new-cursor
1157 (list (edebug-read-storing-offsets (current-buffer)))
1158 (list edebug-offsets))))
1159 (edebug-make-form-wrapper
1160 cursor
1161 (edebug-before-offset cursor)
1162 (edebug-after-offset cursor)
1163 nil)))
1165 ;; Not a defining form, and not edebugging.
1166 (t (edebug-read-sexp)))
1170 (defvar edebug-def-args) ; args of defining form.
1171 (defvar edebug-def-interactive) ; is it an emacs interactive function?
1172 (defvar edebug-inside-func) ;; whether code is inside function context.
1173 ;; Currently def-form sets this to nil; def-body sets it to t.
1175 (defun edebug-interactive-p-name ()
1176 ;; Return a unique symbol for the variable used to store the
1177 ;; status of interactive-p for this function.
1178 (intern (format "edebug-%s-interactive-p" edebug-def-name)))
1181 (defun edebug-wrap-def-body (forms)
1182 "Wrap the FORMS of a definition body."
1183 (if edebug-def-interactive
1184 `(let ((,(edebug-interactive-p-name)
1185 (interactive-p)))
1186 ,(edebug-make-enter-wrapper forms))
1187 (edebug-make-enter-wrapper forms)))
1190 (defun edebug-make-enter-wrapper (forms)
1191 ;; Generate the enter wrapper for some forms of a definition.
1192 ;; This is not to be used for the body of other forms, e.g. `while',
1193 ;; since it wraps the list of forms with a call to `edebug-enter'.
1194 ;; Uses the dynamically bound vars edebug-def-name and edebug-def-args.
1195 ;; Do this after parsing since that may find a name.
1196 (setq edebug-def-name
1197 (or edebug-def-name edebug-old-def-name (gensym "edebug-anon")))
1198 `(edebug-enter
1199 (quote ,edebug-def-name)
1200 ,(if edebug-inside-func
1201 `(list
1202 ;; Doesn't work with more than one def-body!!
1203 ;; But the list will just be reversed.
1204 ,@(nreverse edebug-def-args))
1205 'nil)
1206 (function (lambda () ,@forms))
1210 (defvar edebug-form-begin-marker) ; the mark for def being instrumented
1212 (defvar edebug-offset-index) ; the next available offset index.
1213 (defvar edebug-offset-list) ; the list of offset positions.
1215 (defun edebug-inc-offset (offset)
1216 ;; Modifies edebug-offset-index and edebug-offset-list
1217 ;; accesses edebug-func-marc and buffer point.
1218 (prog1
1219 edebug-offset-index
1220 (setq edebug-offset-list (cons (- offset edebug-form-begin-marker)
1221 edebug-offset-list)
1222 edebug-offset-index (1+ edebug-offset-index))))
1225 (defun edebug-make-before-and-after-form (before-index form after-index)
1226 ;; Return the edebug form for the current function at offset BEFORE-INDEX
1227 ;; given FORM. Looks like:
1228 ;; (edebug-after (edebug-before BEFORE-INDEX) AFTER-INDEX FORM)
1229 ;; Also increment the offset index for subsequent use.
1230 `(edebug-after (edebug-before ,before-index) ,after-index ,form))
1232 (defun edebug-make-after-form (form after-index)
1233 ;; Like edebug-make-before-and-after-form, but only after.
1234 `(edebug-after 0 ,after-index ,form))
1237 (defun edebug-unwrap (sexp)
1238 "Return the unwrapped SEXP or return it as is if it is not wrapped.
1239 The SEXP might be the result of wrapping a body, which is a list of
1240 expressions; a `progn' form will be returned enclosing these forms."
1241 (if (consp sexp)
1242 (cond
1243 ((eq 'edebug-after (car sexp))
1244 (nth 3 sexp))
1245 ((eq 'edebug-enter (car sexp))
1246 (macroexp-progn (nthcdr 2 (nth 1 (nth 3 sexp)))))
1247 (t sexp);; otherwise it is not wrapped, so just return it.
1249 sexp))
1251 (defun edebug-unwrap* (sexp)
1252 "Return the SEXP recursively unwrapped."
1253 (let ((new-sexp (edebug-unwrap sexp)))
1254 (while (not (eq sexp new-sexp))
1255 (setq sexp new-sexp
1256 new-sexp (edebug-unwrap sexp)))
1257 (if (consp new-sexp)
1258 (mapcar #'edebug-unwrap* new-sexp)
1259 new-sexp)))
1262 (defun edebug-defining-form (cursor form-begin form-end speclist)
1263 ;; Process the defining form, starting outside the form.
1264 ;; The speclist is a generated list spec that looks like:
1265 ;; (("def-symbol" defining-form-spec-sans-&define))
1266 ;; Skip the first offset.
1267 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1268 (cdr (edebug-cursor-offsets cursor)))
1269 (edebug-make-form-wrapper
1270 cursor
1271 form-begin (1- form-end)
1272 speclist))
1274 (defun edebug-make-form-wrapper (cursor form-begin form-end
1275 &optional speclist)
1276 ;; Wrap a form, usually a defining form, but any evaluated one.
1277 ;; If speclist is non-nil, this is being called by edebug-defining-form.
1278 ;; Otherwise it is being called from edebug-read-and-maybe-wrap-form1.
1279 ;; This is a hack, but I haven't figured out a simpler way yet.
1280 (let* ((form-data-entry (edebug-get-form-data-entry form-begin form-end))
1281 ;; Set this marker before parsing.
1282 (edebug-form-begin-marker
1283 (if form-data-entry
1284 (edebug--form-data-begin form-data-entry)
1285 ;; Buffer must be current-buffer for this to work:
1286 (set-marker (make-marker) form-begin))))
1288 (let (edebug-offset-list
1289 (edebug-offset-index 0)
1290 result
1291 ;; For definitions.
1292 ;; (edebug-containing-def-name edebug-def-name)
1293 ;; Get name from form-data, if any.
1294 (edebug-old-def-name (edebug--form-data-name form-data-entry))
1295 edebug-def-name
1296 edebug-def-args
1297 edebug-def-interactive
1298 edebug-inside-func;; whether wrapped code executes inside a function.
1301 (setq result
1302 (if speclist
1303 (edebug-match cursor speclist)
1305 ;; else wrap as an enter-form.
1306 (edebug-make-enter-wrapper (list (edebug-form cursor)))))
1308 ;; Set the name here if it was not set by edebug-make-enter-wrapper.
1309 (setq edebug-def-name
1310 (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
1312 ;; Add this def as a dependent of containing def. Buggy.
1313 '(if (and edebug-containing-def-name
1314 (not (get edebug-containing-def-name 'edebug-dependents)))
1315 (put edebug-containing-def-name 'edebug-dependents
1316 (cons edebug-def-name
1317 (get edebug-containing-def-name
1318 'edebug-dependents))))
1320 ;; Create a form-data-entry or modify existing entry's markers.
1321 ;; In the latter case, pointers to the entry remain eq.
1322 (if (not form-data-entry)
1323 (setq form-data-entry
1324 (edebug--make-form-data-entry
1325 edebug-def-name
1326 edebug-form-begin-marker
1327 ;; Buffer must be current-buffer.
1328 (set-marker (make-marker) form-end)
1330 (edebug-set-form-data-entry
1331 form-data-entry edebug-def-name ;; in case name is changed
1332 form-begin form-end))
1334 ;; (message "defining: %s" edebug-def-name) (sit-for 2)
1335 (edebug-make-top-form-data-entry form-data-entry)
1336 (message "Edebug: %s" edebug-def-name)
1337 ;;(debug edebug-def-name)
1339 ;; Destructively reverse edebug-offset-list and make vector from it.
1340 (setq edebug-offset-list (vconcat (nreverse edebug-offset-list)))
1342 ;; Side effects on the property list of edebug-def-name.
1343 (edebug-clear-frequency-count edebug-def-name)
1344 (edebug-clear-coverage edebug-def-name)
1346 ;; Set up the initial window data.
1347 (if (not edebug-top-window-data) ;; if not already set, do it now.
1348 (let ((window ;; Find the best window for this buffer.
1349 (or (get-buffer-window (current-buffer))
1350 (selected-window))))
1351 (setq edebug-top-window-data
1352 (cons window (window-start window)))))
1354 ;; Store the edebug data in symbol's property list.
1355 (put edebug-def-name 'edebug
1356 ;; A struct or vector would be better here!!
1357 (list edebug-form-begin-marker
1358 nil ; clear breakpoints
1359 edebug-offset-list
1360 edebug-top-window-data
1362 result
1366 (defun edebug-clear-frequency-count (name)
1367 ;; Create initial frequency count vector.
1368 ;; For each stop point, the counter is incremented each time it is visited.
1369 (put name 'edebug-freq-count
1370 (make-vector (length edebug-offset-list) 0)))
1373 (defun edebug-clear-coverage (name)
1374 ;; Create initial coverage vector.
1375 ;; Only need one per expression, but it is simpler to use stop points.
1376 (put name 'edebug-coverage
1377 (make-vector (length edebug-offset-list) 'unknown)))
1380 (defun edebug-form (cursor)
1381 ;; Return the instrumented form for the following form.
1382 ;; Add the point offsets to the edebug-offset-list for the form.
1383 (let* ((form (edebug-top-element-required cursor "Expected form"))
1384 (offset (edebug-top-offset cursor)))
1385 (prog1
1386 (cond
1387 ((consp form)
1388 ;; The first offset for a list form is for the list form itself.
1389 (if (eq 'quote (car form))
1390 form
1391 (let* ((head (car form))
1392 (spec (and (symbolp head) (get-edebug-spec head)))
1393 (new-cursor (edebug-new-cursor form offset)))
1394 ;; Find out if this is a defining form from first symbol.
1395 ;; An indirect spec would not work here, yet.
1396 (if (and (consp spec) (eq '&define (car spec)))
1397 (edebug-defining-form
1398 new-cursor
1399 (car offset);; before the form
1400 (edebug-after-offset cursor)
1401 (cons (symbol-name head) (cdr spec)))
1402 ;; Wrap a regular form.
1403 (edebug-make-before-and-after-form
1404 (edebug-inc-offset (car offset))
1405 (edebug-list-form new-cursor)
1406 ;; After processing the list form, the new-cursor is left
1407 ;; with the offset after the form.
1408 (edebug-inc-offset (edebug-cursor-offsets new-cursor))))
1411 ((symbolp form)
1412 (cond
1413 ;; Check for constant symbols that don't get wrapped.
1414 ((or (memq form '(t nil))
1415 (keywordp form))
1416 form)
1418 (t ;; just a variable
1419 (edebug-make-after-form form (edebug-inc-offset (cdr offset))))))
1421 ;; Anything else is self-evaluating.
1422 (t form))
1423 (edebug-move-cursor cursor))))
1426 (defsubst edebug-forms (cursor) (edebug-match cursor '(&rest form)))
1427 (defsubst edebug-sexps (cursor) (edebug-match cursor '(&rest sexp)))
1429 (defsubst edebug-list-form-args (head cursor)
1430 ;; Process the arguments of a list form given that head of form is a symbol.
1431 ;; Helper for edebug-list-form
1432 (let ((spec (get-edebug-spec head)))
1433 (cond
1434 (spec
1435 (cond
1436 ((consp spec)
1437 ;; It is a speclist.
1438 (let (edebug-best-error
1439 edebug-error-point);; This may not be needed.
1440 (edebug-match-sublist cursor spec)))
1441 ((eq t spec) (edebug-forms cursor))
1442 ((eq 0 spec) (edebug-sexps cursor))
1443 ((symbolp spec) (funcall spec cursor));; Not used by edebug,
1444 ; but leave it in for compatibility.
1446 ;; No edebug-form-spec provided.
1447 ((macrop head)
1448 (if edebug-eval-macro-args
1449 (edebug-forms cursor)
1450 (edebug-sexps cursor)))
1451 (t ;; Otherwise it is a function call.
1452 (edebug-forms cursor)))))
1455 (defun edebug-list-form (cursor)
1456 ;; Return an instrumented form built from the list form.
1457 ;; The after offset will be left in the cursor after processing the form.
1458 (let ((head (edebug-top-element-required cursor "Expected elements"))
1459 ;; Prevent backtracking whenever instrumenting.
1460 (edebug-gate t)
1461 ;; A list form is never optional because it matches anything.
1462 (edebug-&optional nil)
1463 (edebug-&rest nil))
1464 ;; Skip the first offset.
1465 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1466 (cdr (edebug-cursor-offsets cursor)))
1467 (cond
1468 ((symbolp head)
1469 (cond
1470 ((null head) nil) ; () is valid.
1471 ((eq head 'interactive-p)
1472 ;; Special case: replace (interactive-p) with variable
1473 (setq edebug-def-interactive 'check-it)
1474 (edebug-move-cursor cursor)
1475 (edebug-interactive-p-name))
1477 (cons head (edebug-list-form-args
1478 head (edebug-move-cursor cursor))))))
1480 ((consp head)
1481 (if (eq (car head) '\,)
1482 ;; The head of a form should normally be a symbol or a lambda
1483 ;; expression but it can also be an unquote form to be filled
1484 ;; before evaluation. We evaluate the arguments anyway, on the
1485 ;; assumption that the unquote form will place a proper function
1486 ;; name (rather than a macro name).
1487 (edebug-match cursor '(("," def-form) body))
1488 ;; Process anonymous function and args.
1489 ;; This assumes no anonymous macros.
1490 (edebug-match-specs cursor '(lambda-expr body) 'edebug-match-specs)))
1492 (t (edebug-syntax-error
1493 "Head of list form must be a symbol or lambda expression")))
1496 ;;; Matching of specs.
1498 (defvar edebug-matching-depth 0) ;; initial value
1501 ;;; Failure to match
1503 ;; This throws to no-match, if there are higher alternatives.
1504 ;; Otherwise it signals an error. The place of the error is found
1505 ;; with the two before- and after-offset functions.
1507 (defun edebug-no-match (cursor &rest args)
1508 ;; Throw a no-match, or signal an error immediately if gate is active.
1509 ;; Remember this point in case we need to report this error.
1510 (setq edebug-error-point (or edebug-error-point
1511 (edebug-before-offset cursor))
1512 edebug-best-error (or edebug-best-error args))
1513 (if (and edebug-gate (not edebug-&optional))
1514 (progn
1515 (if edebug-error-point
1516 (goto-char edebug-error-point))
1517 (apply #'edebug-syntax-error args))
1518 (throw 'no-match args)))
1521 (defun edebug-match (cursor specs)
1522 ;; Top level spec matching function.
1523 ;; Used also at each lower level of specs.
1524 (let (edebug-&optional
1525 edebug-&rest
1526 edebug-best-error
1527 edebug-error-point
1528 (edebug-gate edebug-gate) ;; locally bound to limit effect
1530 (edebug-match-specs cursor specs 'edebug-match-specs)))
1533 (defun edebug-match-one-spec (cursor spec)
1534 ;; Match one spec, which is not a keyword &-spec.
1535 (cond
1536 ((symbolp spec) (edebug-match-symbol cursor spec))
1537 ((vectorp spec) (edebug-match cursor (append spec nil)))
1538 ((stringp spec) (edebug-match-string cursor spec))
1539 ((listp spec) (edebug-match-list cursor spec))
1543 (defun edebug-match-specs (cursor specs remainder-handler)
1544 ;; Append results of matching the list of specs.
1545 ;; The first spec is handled and the remainder-handler handles the rest.
1546 (let ((edebug-matching-depth
1547 (if (> edebug-matching-depth edebug-max-depth)
1548 (error "Too deep - perhaps infinite loop in spec?")
1549 (1+ edebug-matching-depth))))
1550 (cond
1551 ((null specs) nil)
1553 ;; Is the spec dotted?
1554 ((atom specs)
1555 (let ((edebug-dotted-spec t));; Containing spec list was dotted.
1556 (edebug-match-specs cursor (list specs) remainder-handler)))
1558 ;; The reason for processing here &optional, &rest, and vectors
1559 ;; which might contain them even when the form is dotted is to
1560 ;; allow them to match nothing, so we can advance to the dotted
1561 ;; part of the spec.
1562 ((or (listp (edebug-cursor-expressions cursor))
1563 (vectorp (car specs))
1564 (memq (car specs) '(&optional &rest))) ; Process normally.
1565 ;; (message "%scursor=%s specs=%s"
1566 ;; (make-string edebug-matching-depth ?|) cursor (car specs))
1567 (let* ((spec (car specs))
1568 (rest)
1569 (first-char (and (symbolp spec) (aref (symbol-name spec) 0)))
1570 (match (cond
1571 ((eq ?& first-char);; "&" symbols take all following specs.
1572 (funcall (get-edebug-spec spec) cursor (cdr specs)))
1573 ((eq ?: first-char);; ":" symbols take one following spec.
1574 (setq rest (cdr (cdr specs)))
1575 (funcall (get-edebug-spec spec) cursor (car (cdr specs))))
1576 (t;; Any other normal spec.
1577 (setq rest (cdr specs))
1578 (edebug-match-one-spec cursor spec)))))
1579 ;; The first match result may not be a list, which can happen
1580 ;; when matching the tail of a dotted list. In that case
1581 ;; there is no remainder.
1582 (if (listp match)
1583 (nconc match
1584 (funcall remainder-handler cursor rest remainder-handler))
1585 match)))
1587 ;; Must be a dotted form, with no remaining &rest or &optional specs to
1588 ;; match.
1590 (if (not edebug-dotted-spec)
1591 (edebug-no-match cursor "Dotted spec required."))
1592 ;; Cancel dotted spec and dotted form.
1593 (let ((edebug-dotted-spec)
1594 (this-form (edebug-cursor-expressions cursor))
1595 (this-offset (edebug-cursor-offsets cursor)))
1596 ;; Wrap the form in a list, by changing the cursor.
1597 (edebug-set-cursor cursor (list this-form) this-offset)
1598 ;; Process normally, then unwrap the result.
1599 (car (edebug-match-specs cursor specs remainder-handler)))))))
1601 ;; Define specs for all the symbol specs with functions used to process them.
1602 ;; Perhaps we shouldn't be doing this with edebug-form-specs since the
1603 ;; user may want to define macros or functions with the same names.
1604 ;; We could use an internal obarray for these primitive specs.
1606 (dolist (pair '((&optional . edebug-match-&optional)
1607 (&rest . edebug-match-&rest)
1608 (&or . edebug-match-&or)
1609 (form . edebug-match-form)
1610 (sexp . edebug-match-sexp)
1611 (body . edebug-match-body)
1612 (&define . edebug-match-&define)
1613 (name . edebug-match-name)
1614 (:name . edebug-match-colon-name)
1615 (arg . edebug-match-arg)
1616 (def-body . edebug-match-def-body)
1617 (def-form . edebug-match-def-form)
1618 ;; Less frequently used:
1619 ;; (function . edebug-match-function)
1620 (lambda-expr . edebug-match-lambda-expr)
1621 (cl-generic-method-args . edebug-match-cl-generic-method-args)
1622 (&not . edebug-match-&not)
1623 (&key . edebug-match-&key)
1624 (place . edebug-match-place)
1625 (gate . edebug-match-gate)
1626 ;; (nil . edebug-match-nil) not this one - special case it.
1628 (put (car pair) 'edebug-form-spec (cdr pair)))
1630 (defun edebug-match-symbol (cursor symbol)
1631 ;; Match a symbol spec.
1632 (let* ((spec (get-edebug-spec symbol)))
1633 (cond
1634 (spec
1635 (if (consp spec)
1636 ;; It is an indirect spec.
1637 (edebug-match cursor spec)
1638 ;; Otherwise it should be the symbol name of a function.
1639 ;; There could be a bug here - maybe need to do edebug-match bindings.
1640 (funcall spec cursor)))
1642 ((null symbol) ;; special case this.
1643 (edebug-match-nil cursor))
1645 ((fboundp symbol) ; is it a predicate?
1646 (let ((sexp (edebug-top-element-required cursor "Expected" symbol)))
1647 ;; Special case for edebug-`.
1648 (if (and (listp sexp) (eq (car sexp) '\,))
1649 (edebug-match cursor '(("," def-form)))
1650 (if (not (funcall symbol sexp))
1651 (edebug-no-match cursor symbol "failed"))
1652 (edebug-move-cursor cursor)
1653 (list sexp))))
1654 (t (error "%s is not a form-spec or function" symbol))
1658 (defun edebug-match-sexp (cursor)
1659 (list (prog1 (edebug-top-element-required cursor "Expected sexp")
1660 (edebug-move-cursor cursor))))
1662 (defun edebug-match-form (cursor)
1663 (list (edebug-form cursor)))
1665 (defalias 'edebug-match-place 'edebug-match-form)
1666 ;; Currently identical to edebug-match-form.
1667 ;; This is for common lisp setf-style place arguments.
1669 (defsubst edebug-match-body (cursor) (edebug-forms cursor))
1671 (defun edebug-match-&optional (cursor specs)
1672 ;; Keep matching until one spec fails.
1673 (edebug-&optional-wrapper cursor specs 'edebug-&optional-wrapper))
1675 (defun edebug-&optional-wrapper (cursor specs remainder-handler)
1676 (let (result
1677 (edebug-&optional specs)
1678 (edebug-gate nil)
1679 (this-form (edebug-cursor-expressions cursor))
1680 (this-offset (edebug-cursor-offsets cursor)))
1681 (if (null (catch 'no-match
1682 (setq result
1683 (edebug-match-specs cursor specs remainder-handler))
1684 ;; Returning nil means no no-match was thrown.
1685 nil))
1686 result
1687 ;; no-match, but don't fail; just reset cursor and return nil.
1688 (edebug-set-cursor cursor this-form this-offset)
1689 nil)))
1692 (defun edebug-&rest-wrapper (cursor specs remainder-handler)
1693 (if (null specs) (setq specs edebug-&rest))
1694 ;; Reuse the &optional handler with this as the remainder handler.
1695 (edebug-&optional-wrapper cursor specs remainder-handler))
1697 (defun edebug-match-&rest (cursor specs)
1698 ;; Repeatedly use specs until failure.
1699 (let ((edebug-&rest specs) ;; remember these
1700 edebug-best-error
1701 edebug-error-point)
1702 (edebug-&rest-wrapper cursor specs 'edebug-&rest-wrapper)))
1705 (defun edebug-match-&or (cursor specs)
1706 ;; Keep matching until one spec succeeds, and return its results.
1707 ;; If none match, fail.
1708 ;; This needs to be optimized since most specs spend time here.
1709 (let ((original-specs specs)
1710 (this-form (edebug-cursor-expressions cursor))
1711 (this-offset (edebug-cursor-offsets cursor)))
1712 (catch 'matched
1713 (while specs
1714 (catch 'no-match
1715 (throw 'matched
1716 (let (edebug-gate ;; only while matching each spec
1717 edebug-best-error
1718 edebug-error-point)
1719 ;; Doesn't support e.g. &or symbolp &rest form
1720 (edebug-match-one-spec cursor (car specs)))))
1721 ;; Match failed, so reset and try again.
1722 (setq specs (cdr specs))
1723 ;; Reset the cursor for the next match.
1724 (edebug-set-cursor cursor this-form this-offset))
1725 ;; All failed.
1726 (apply #'edebug-no-match cursor "Expected one of" original-specs))
1730 (defun edebug-match-&not (cursor specs)
1731 ;; If any specs match, then fail
1732 (if (null (catch 'no-match
1733 (let ((edebug-gate nil))
1734 (save-excursion
1735 (edebug-match-&or cursor specs)))
1736 nil))
1737 ;; This means something matched, so it is a no match.
1738 (edebug-no-match cursor "Unexpected"))
1739 ;; This means nothing matched, so it is OK.
1740 nil) ;; So, return nothing
1743 (def-edebug-spec &key edebug-match-&key)
1745 (defun edebug-match-&key (cursor specs)
1746 ;; Following specs must look like (<name> <spec>) ...
1747 ;; where <name> is the name of a keyword, and spec is its spec.
1748 ;; This really doesn't save much over the expanded form and takes time.
1749 (edebug-match-&rest
1750 cursor
1751 (cons '&or
1752 (mapcar (lambda (pair)
1753 (vector (format ":%s" (car pair))
1754 (car (cdr pair))))
1755 specs))))
1758 (defun edebug-match-gate (_cursor)
1759 ;; Simply set the gate to prevent backtracking at this level.
1760 (setq edebug-gate t)
1761 nil)
1764 (defun edebug-match-list (cursor specs)
1765 ;; The spec is a list, but what kind of list, and what context?
1766 (if edebug-dotted-spec
1767 ;; After dotted spec but form did not contain dot,
1768 ;; so match list spec elements as if spliced in.
1769 (prog1
1770 (let ((edebug-dotted-spec))
1771 (edebug-match-specs cursor specs 'edebug-match-specs))
1772 ;; If it matched, really clear the dotted-spec flag.
1773 (setq edebug-dotted-spec nil))
1774 (let ((spec (car specs))
1775 (form (edebug-top-element-required cursor "Expected" specs)))
1776 (cond
1777 ((eq 'quote spec)
1778 (let ((spec (car (cdr specs))))
1779 (cond
1780 ((symbolp spec)
1781 ;; Special case: spec quotes a symbol to match.
1782 ;; Change in future. Use "..." instead.
1783 (if (not (eq spec form))
1784 (edebug-no-match cursor "Expected" spec))
1785 (edebug-move-cursor cursor)
1786 (setq edebug-gate t)
1787 form)
1789 (error "Bad spec: %s" specs)))))
1791 ((eq 'vector spec)
1792 (if (vectorp form)
1793 ;; Special case: match a vector with the specs.
1794 (let ((result (edebug-match-sublist
1795 (edebug-new-cursor
1796 form (cdr (edebug-top-offset cursor)))
1797 (cdr specs))))
1798 (edebug-move-cursor cursor)
1799 (list (apply #'vector result)))
1800 (edebug-no-match cursor "Expected" specs)))
1802 ((listp form)
1803 (prog1
1804 (list (edebug-match-sublist
1805 ;; First offset is for the list form itself.
1806 ;; Treat nil as empty list.
1807 (edebug-new-cursor form (cdr (edebug-top-offset cursor)))
1808 specs))
1809 (edebug-move-cursor cursor)))
1811 (t (edebug-no-match cursor "Expected" specs)))
1815 (defun edebug-match-sublist (cursor specs)
1816 ;; Match a sublist of specs.
1817 (let (edebug-&optional
1818 ;;edebug-best-error
1819 ;;edebug-error-point
1821 (prog1
1822 ;; match with edebug-match-specs so edebug-best-error is not bound.
1823 (edebug-match-specs cursor specs 'edebug-match-specs)
1824 (if (not (edebug-empty-cursor cursor))
1825 (if edebug-best-error
1826 (apply #'edebug-no-match cursor edebug-best-error)
1827 ;; A failed &rest or &optional spec may leave some args.
1828 (edebug-no-match cursor "Failed matching" specs)
1829 )))))
1832 (defun edebug-match-string (cursor spec)
1833 (let ((sexp (edebug-top-element-required cursor "Expected" spec)))
1834 (if (not (eq (intern spec) sexp))
1835 (edebug-no-match cursor "Expected" spec)
1836 ;; Since it matched, failure means immediate error, unless &optional.
1837 (setq edebug-gate t)
1838 (edebug-move-cursor cursor)
1839 (list sexp)
1842 (defun edebug-match-nil (cursor)
1843 ;; There must be nothing left to match a nil.
1844 (if (not (edebug-empty-cursor cursor))
1845 (edebug-no-match cursor "Unmatched argument(s)")
1846 nil))
1849 (defun edebug-match-function (_cursor)
1850 (error "Use function-form instead of function in edebug spec"))
1852 (defun edebug-match-&define (cursor specs)
1853 ;; Match a defining form.
1854 ;; Normally, &define is interpreted specially other places.
1855 ;; This should only be called inside of a spec list to match the remainder
1856 ;; of the current list. e.g. ("lambda" &define args def-body)
1857 (edebug-make-form-wrapper
1858 cursor
1859 (edebug-before-offset cursor)
1860 ;; Find the last offset in the list.
1861 (let ((offsets (edebug-cursor-offsets cursor)))
1862 (while (consp offsets) (setq offsets (cdr offsets)))
1863 offsets)
1864 specs))
1866 (defun edebug-match-lambda-expr (cursor)
1867 ;; The expression must be a function.
1868 ;; This will match any list form that begins with a symbol
1869 ;; that has an edebug-form-spec beginning with &define. In
1870 ;; practice, only lambda expressions should be used.
1871 ;; I could add a &lambda specification to avoid confusion.
1872 (let* ((sexp (edebug-top-element-required
1873 cursor "Expected lambda expression"))
1874 (offset (edebug-top-offset cursor))
1875 (head (and (consp sexp) (car sexp)))
1876 (spec (and (symbolp head) (get-edebug-spec head)))
1877 (edebug-inside-func nil))
1878 ;; Find out if this is a defining form from first symbol.
1879 (if (and (consp spec) (eq '&define (car spec)))
1880 (prog1
1881 (list
1882 (edebug-defining-form
1883 (edebug-new-cursor sexp offset)
1884 (car offset);; before the sexp
1885 (edebug-after-offset cursor)
1886 (cons (symbol-name head) (cdr spec))))
1887 (edebug-move-cursor cursor))
1888 (edebug-no-match cursor "Expected lambda expression")
1892 (defun edebug-match-name (cursor)
1893 ;; Set the edebug-def-name bound in edebug-defining-form.
1894 (let ((name (edebug-top-element-required cursor "Expected name")))
1895 ;; Maybe strings and numbers could be used.
1896 (if (not (symbolp name))
1897 (edebug-no-match cursor "Symbol expected for name of definition"))
1898 (setq edebug-def-name
1899 (if edebug-def-name
1900 ;; Construct a new name by appending to previous name.
1901 (intern (format "%s@%s" edebug-def-name name))
1902 name))
1903 (edebug-move-cursor cursor)
1904 (list name)))
1906 (defun edebug-match-colon-name (_cursor spec)
1907 ;; Set the edebug-def-name to the spec.
1908 (setq edebug-def-name
1909 (if edebug-def-name
1910 ;; Construct a new name by appending to previous name.
1911 (intern (format "%s@%s" edebug-def-name spec))
1912 spec))
1913 nil)
1915 (defun edebug-match-cl-generic-method-args (cursor)
1916 (let ((args (edebug-top-element-required cursor "Expected arguments")))
1917 (if (not (consp args))
1918 (edebug-no-match cursor "List expected"))
1919 ;; Append the arguments to edebug-def-name.
1920 (setq edebug-def-name
1921 (intern (format "%s %s" edebug-def-name args)))
1922 (edebug-move-cursor cursor)
1923 (list args)))
1925 (defun edebug-match-arg (cursor)
1926 ;; set the def-args bound in edebug-defining-form
1927 (let ((edebug-arg (edebug-top-element-required cursor "Expected arg")))
1928 (if (or (not (symbolp edebug-arg))
1929 (edebug-lambda-list-keywordp edebug-arg))
1930 (edebug-no-match cursor "Bad argument:" edebug-arg))
1931 (edebug-move-cursor cursor)
1932 (setq edebug-def-args (cons edebug-arg edebug-def-args))
1933 (list edebug-arg)))
1935 (defun edebug-match-def-form (cursor)
1936 ;; Like form but the form is wrapped in edebug-enter form.
1937 ;; The form is assumed to be executing outside of the function context.
1938 ;; This is a hack for now, since a def-form might execute inside as well.
1939 ;; Not to be used otherwise.
1940 (let ((edebug-inside-func nil))
1941 (list (edebug-make-enter-wrapper (list (edebug-form cursor))))))
1943 (defun edebug-match-def-body (cursor)
1944 ;; Like body but body is wrapped in edebug-enter form.
1945 ;; The body is assumed to be executing inside of the function context.
1946 ;; Not to be used otherwise.
1947 (let* ((edebug-inside-func t)
1948 (forms (edebug-forms cursor)))
1949 ;; If there's no form, there's nothing to wrap!
1950 ;; This happens to handle bug#20281, tho maybe a better fix would be to
1951 ;; improve the `defun' spec.
1952 (when forms
1953 (list (edebug-wrap-def-body forms)))))
1956 ;;;; Edebug Form Specs
1957 ;;; ==========================================================
1959 ;;;;* Spec for def-edebug-spec
1960 ;;; Out of date.
1962 (defun edebug-spec-p (object)
1963 "Return non-nil if OBJECT is a symbol with an edebug-form-spec property."
1964 (and (symbolp object)
1965 (get object 'edebug-form-spec)))
1967 (def-edebug-spec def-edebug-spec
1968 ;; Top level is different from lower levels.
1969 (&define :name edebug-spec name
1970 &or "nil" edebug-spec-p "t" "0" (&rest edebug-spec)))
1972 (def-edebug-spec edebug-spec-list
1973 ;; A list must have something in it, or it is nil, a symbolp
1974 ((edebug-spec . [&or nil edebug-spec])))
1976 (def-edebug-spec edebug-spec
1977 (&or
1978 (vector &rest edebug-spec) ; matches a vector
1979 ("vector" &rest edebug-spec) ; matches a vector spec
1980 ("quote" symbolp)
1981 edebug-spec-list
1982 stringp
1983 [edebug-lambda-list-keywordp &rest edebug-spec]
1984 [keywordp gate edebug-spec]
1985 edebug-spec-p ;; Including all the special ones e.g. form.
1986 symbolp;; a predicate
1990 ;;;* Emacs special forms and some functions.
1992 ;; quote expects only one argument, although it allows any number.
1993 (def-edebug-spec quote sexp)
1995 ;; The standard defining forms.
1996 (def-edebug-spec defconst defvar)
1997 (def-edebug-spec defvar (symbolp &optional form stringp))
1999 (def-edebug-spec defun
2000 (&define name lambda-list lambda-doc
2001 [&optional ("declare" &rest sexp)]
2002 [&optional ("interactive" interactive)]
2003 def-body))
2004 (def-edebug-spec defmacro
2005 ;; FIXME: Improve `declare' so we can Edebug gv-expander and
2006 ;; gv-setter declarations.
2007 (&define name lambda-list lambda-doc
2008 [&optional ("declare" &rest sexp)] def-body))
2010 (def-edebug-spec arglist lambda-list) ;; deprecated - use lambda-list.
2012 (def-edebug-spec lambda-list
2013 (([&rest arg]
2014 [&optional ["&optional" arg &rest arg]]
2015 &optional ["&rest" arg]
2018 (def-edebug-spec lambda-doc
2019 (&optional [&or stringp
2020 (&define ":documentation" def-form)]))
2022 (def-edebug-spec interactive
2023 (&optional &or stringp def-form))
2025 ;; A function-form is for an argument that may be a function or a form.
2026 ;; This specially recognizes anonymous functions quoted with quote.
2027 (def-edebug-spec function-form
2028 ;; form at the end could also handle "function",
2029 ;; but recognize it specially to avoid wrapping function forms.
2030 (&or ([&or "quote" "function"] &or symbolp lambda-expr) form))
2032 ;; function expects a symbol or a lambda or macro expression
2033 ;; A macro is allowed by Emacs.
2034 (def-edebug-spec function (&or symbolp lambda-expr))
2036 ;; A macro expression is a lambda expression with "macro" prepended.
2037 (def-edebug-spec macro (&define "lambda" lambda-list def-body))
2039 ;; (def-edebug-spec anonymous-form ((&or ["lambda" lambda] ["macro" macro])))
2041 ;; Standard functions that take function-forms arguments.
2043 ;; FIXME? The manual uses this form (maybe that's just for illustration?):
2044 ;; (def-edebug-spec let
2045 ;; ((&rest &or symbolp (gate symbolp &optional form))
2046 ;; body))
2047 (def-edebug-spec let
2048 ((&rest &or (symbolp &optional form) symbolp)
2049 body))
2051 (def-edebug-spec let* let)
2053 (def-edebug-spec setq (&rest symbolp form))
2054 (def-edebug-spec setq-default setq)
2056 (def-edebug-spec cond (&rest (&rest form)))
2058 (def-edebug-spec condition-case
2059 (symbolp
2060 form
2061 &rest ([&or symbolp (&rest symbolp)] body)))
2064 (def-edebug-spec \` (backquote-form))
2066 ;; Supports quotes inside backquotes,
2067 ;; but only at the top level inside unquotes.
2068 (def-edebug-spec backquote-form
2069 (&or
2070 ([&or "," ",@"] &or ("quote" backquote-form) form)
2071 ;; The simple version:
2072 ;; (backquote-form &rest backquote-form)
2073 ;; doesn't handle (a . ,b). The straightforward fix:
2074 ;; (backquote-form . [&or nil backquote-form])
2075 ;; uses up too much stack space.
2076 ;; Note that `(foo . ,@bar) is not valid, so we don't need to handle it.
2077 (backquote-form [&rest [&not ","] backquote-form]
2078 . [&or nil backquote-form])
2079 ;; If you use dotted forms in backquotes, replace the previous line
2080 ;; with the following. This takes quite a bit more stack space, however.
2081 ;; (backquote-form . [&or nil backquote-form])
2082 (vector &rest backquote-form)
2083 sexp))
2085 ;; Special version of backquote that instruments backquoted forms
2086 ;; destined to be evaluated, usually as the result of a
2087 ;; macroexpansion. Backquoted code can only have unquotes (, and ,@)
2088 ;; in places where list forms are allowed, and predicates. If the
2089 ;; backquote is used in a macro, unquoted code that come from
2090 ;; arguments must be instrumented, if at all, with def-form not def-body.
2092 ;; We could assume that all forms (not nested in other forms)
2093 ;; in arguments of macros should be def-forms, whether or not the macros
2094 ;; are defined with edebug-` but this would be expensive.
2096 ;; ,@ might have some problems.
2098 (defalias 'edebug-\` '\`) ;; same macro as regular backquote.
2099 (def-edebug-spec edebug-\` (def-form))
2101 ;; Assume immediate quote in unquotes mean backquote at next higher level.
2102 (def-edebug-spec \, (&or ("quote" edebug-\`) def-form))
2103 (def-edebug-spec \,@ (&define ;; so (,@ form) is never wrapped.
2104 &or ("quote" edebug-\`) def-form))
2106 ;; New byte compiler.
2108 (def-edebug-spec save-selected-window t)
2109 (def-edebug-spec save-current-buffer t)
2111 ;; Anything else?
2113 ;;; The debugger itself
2115 (defvar edebug-active nil) ;; Non-nil when edebug is active
2117 (defvar edebug-stack nil)
2118 ;; Stack of active functions evaluated via edebug.
2119 ;; Should be nil at the top level.
2121 (defvar edebug-stack-depth -1)
2122 ;; Index of last edebug-stack item.
2124 (defvar edebug-offset-indices nil)
2125 ;; Stack of offset indices of visited edebug sexps.
2126 ;; Should be nil at the top level.
2127 ;; Each function adds one cons. Top is modified with setcar.
2130 (defvar edebug-entered nil
2131 ;; Non-nil if edebug has already been entered at this recursive edit level.
2132 ;; This should stay nil at the top level.
2135 ;; Should these be options?
2136 (defconst edebug-debugger 'edebug
2137 ;; Name of function to use for debugging when error or quit occurs.
2138 ;; Set this to 'debug if you want to debug edebug.
2142 ;; Dynamically bound variables, declared globally but left unbound.
2143 (defvar edebug-function) ; the function being executed. change name!!
2144 (defvar edebug-data) ; the edebug data for the function
2145 (defvar edebug-def-mark) ; the mark for the definition
2146 (defvar edebug-freq-count) ; the count of expression visits.
2147 (defvar edebug-coverage) ; the coverage results of each expression of function.
2149 (defvar edebug-buffer) ; which buffer the function is in.
2151 (defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2152 (defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2154 (defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2155 (defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2157 ;;; Handling signals
2159 (defun edebug-signal (signal-name signal-data)
2160 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2161 A signal name is a symbol with an `error-conditions' property
2162 that is a list of condition names.
2163 A handler for any of those names will get to handle this signal.
2164 The symbol `error' should always be one of them.
2166 DATA should be a list. Its elements are printed as part of the error message.
2167 If the signal is handled, DATA is made available to the handler.
2168 See `condition-case'.
2170 This is the Edebug replacement for the standard `signal'. It should
2171 only be active while Edebug is. It checks `debug-on-error' to see
2172 whether it should call the debugger. When execution is resumed, the
2173 error is signaled again."
2174 (if (and (listp debug-on-error) (memq signal-name debug-on-error))
2175 (edebug 'error (cons signal-name signal-data)))
2176 ;; If we reach here without another non-local exit, then send signal again.
2177 ;; i.e. the signal is not continuable, yet.
2178 ;; Avoid infinite recursion.
2179 (let ((signal-hook-function nil))
2180 (signal signal-name signal-data)))
2182 ;;; Entering Edebug
2184 (defun edebug-enter (function args body)
2185 ;; Entering FUNC. The arguments are ARGS, and the body is BODY.
2186 ;; Setup edebug variables and evaluate BODY. This function is called
2187 ;; when a function evaluated with edebug-eval-top-level-form is entered.
2188 ;; Return the result of BODY.
2190 ;; Is this the first time we are entering edebug since
2191 ;; lower-level recursive-edit command?
2192 ;; More precisely, this tests whether Edebug is currently active.
2193 (let ((edebug-function function))
2194 (if (not edebug-entered)
2195 (let ((edebug-entered t)
2196 ;; Binding max-lisp-eval-depth here is OK,
2197 ;; but not inside an unwind-protect.
2198 ;; Doing it here also keeps it from growing too large.
2199 (max-lisp-eval-depth (+ 100 max-lisp-eval-depth)) ; too much??
2200 (max-specpdl-size (+ 200 max-specpdl-size))
2202 (debugger edebug-debugger) ; only while edebug is active.
2203 (edebug-outside-debug-on-error debug-on-error)
2204 (edebug-outside-debug-on-quit debug-on-quit)
2205 ;; Binding these may not be the right thing to do.
2206 ;; We want to allow the global values to be changed.
2207 (debug-on-error (or debug-on-error edebug-on-error))
2208 (debug-on-quit edebug-on-quit))
2209 (unwind-protect
2210 (let ((signal-hook-function 'edebug-signal))
2211 (setq edebug-execution-mode (or edebug-next-execution-mode
2212 edebug-initial-mode
2213 edebug-execution-mode)
2214 edebug-next-execution-mode nil)
2215 (edebug-enter function args body))))
2217 (let* ((edebug-data (get function 'edebug))
2218 (edebug-def-mark (car edebug-data)) ; mark at def start
2219 (edebug-freq-count (get function 'edebug-freq-count))
2220 (edebug-coverage (get function 'edebug-coverage))
2221 (edebug-buffer (marker-buffer edebug-def-mark))
2223 (edebug-stack (cons function edebug-stack))
2224 (edebug-offset-indices (cons 0 edebug-offset-indices))
2226 (if (get function 'edebug-on-entry)
2227 (progn
2228 (setq edebug-execution-mode 'step)
2229 (if (eq (get function 'edebug-on-entry) 'temp)
2230 (put function 'edebug-on-entry nil))))
2231 (if edebug-trace
2232 (edebug--enter-trace function args body)
2233 (funcall body))
2234 ))))
2236 (defun edebug-var-status (var)
2237 "Return a cons cell describing the status of VAR's current binding.
2238 The purpose of this function is so you can properly undo
2239 subsequent changes to the same binding, by passing the status
2240 cons cell to `edebug-restore-status'. The status cons cell
2241 has the form (LOCUS . VALUE), where LOCUS can be a buffer
2242 \(for a buffer-local binding), or nil (if the default binding is current)."
2243 (cons (variable-binding-locus var)
2244 (symbol-value var)))
2246 (defun edebug-restore-status (var status)
2247 "Reset VAR based on STATUS.
2248 STATUS should be a list returned by `edebug-var-status'."
2249 (let ((locus (car status))
2250 (value (cdr status)))
2251 (cond ((bufferp locus)
2252 (if (buffer-live-p locus)
2253 (with-current-buffer locus
2254 (set var value))))
2255 ((framep locus)
2256 (modify-frame-parameters locus (list (cons var value))))
2258 (set var value)))))
2260 (defun edebug--enter-trace (function args body)
2261 (let ((edebug-stack-depth (1+ edebug-stack-depth))
2262 edebug-result)
2263 (edebug-print-trace-before
2264 (format "%s args: %s" function args))
2265 (prog1 (setq edebug-result (funcall body))
2266 (edebug-print-trace-after
2267 (format "%s result: %s" function edebug-result)))))
2269 (def-edebug-spec edebug-tracing (form body))
2271 (defmacro edebug-tracing (msg &rest body)
2272 "Print MSG in *edebug-trace* before and after evaluating BODY.
2273 The result of BODY is also printed."
2274 `(let ((edebug-stack-depth (1+ edebug-stack-depth))
2275 edebug-result)
2276 (edebug-print-trace-before ,msg)
2277 (prog1 (setq edebug-result (progn ,@body))
2278 (edebug-print-trace-after
2279 (format "%s result: %s" ,msg edebug-result)))))
2281 (defun edebug-print-trace-before (msg)
2282 "Function called to print trace info before expression evaluation.
2283 MSG is printed after `::::{ '."
2284 (edebug-trace-display
2285 edebug-trace-buffer "%s{ %s" (make-string edebug-stack-depth ?\:) msg))
2287 (defun edebug-print-trace-after (msg)
2288 "Function called to print trace info after expression evaluation.
2289 MSG is printed after `::::} '."
2290 (edebug-trace-display
2291 edebug-trace-buffer "%s} %s" (make-string edebug-stack-depth ?\:) msg))
2295 (defun edebug-slow-before (before-index)
2296 (unless edebug-active
2297 ;; Debug current function given BEFORE position.
2298 ;; Called from functions compiled with edebug-eval-top-level-form.
2299 ;; Return the before index.
2300 (setcar edebug-offset-indices before-index)
2302 ;; Increment frequency count
2303 (aset edebug-freq-count before-index
2304 (1+ (aref edebug-freq-count before-index)))
2306 (if (or (not (memq edebug-execution-mode '(Go-nonstop next)))
2307 (input-pending-p))
2308 (edebug-debugger before-index 'before nil)))
2309 before-index)
2311 (defun edebug-fast-before (_before-index)
2312 ;; Do nothing.
2315 (defun edebug-slow-after (_before-index after-index value)
2316 (if edebug-active
2317 value
2318 ;; Debug current function given AFTER position and VALUE.
2319 ;; Called from functions compiled with edebug-eval-top-level-form.
2320 ;; Return VALUE.
2321 (setcar edebug-offset-indices after-index)
2323 ;; Increment frequency count
2324 (aset edebug-freq-count after-index
2325 (1+ (aref edebug-freq-count after-index)))
2326 (if edebug-test-coverage (edebug--update-coverage after-index value))
2328 (if (and (eq edebug-execution-mode 'Go-nonstop)
2329 (not (input-pending-p)))
2330 ;; Just return result.
2331 value
2332 (edebug-debugger after-index 'after value)
2335 (defun edebug-fast-after (_before-index _after-index value)
2336 ;; Do nothing but return the value.
2337 value)
2339 (defun edebug-run-slow ()
2340 (defalias 'edebug-before 'edebug-slow-before)
2341 (defalias 'edebug-after 'edebug-slow-after))
2343 ;; This is not used, yet.
2344 (defun edebug-run-fast ()
2345 (defalias 'edebug-before 'edebug-fast-before)
2346 (defalias 'edebug-after 'edebug-fast-after))
2348 (edebug-run-slow)
2351 (defun edebug--update-coverage (after-index value)
2352 (let ((old-result (aref edebug-coverage after-index)))
2353 (cond
2354 ((eq 'ok-coverage old-result))
2355 ((eq 'unknown old-result)
2356 (aset edebug-coverage after-index value))
2357 ;; Test if a different result.
2358 ((not (eq value old-result))
2359 (aset edebug-coverage after-index 'ok-coverage)))))
2362 ;; Dynamically declared unbound variables.
2363 (defvar edebug-breakpoints)
2364 (defvar edebug-break-data) ; break data for current function.
2365 (defvar edebug-break) ; whether a break occurred.
2366 (defvar edebug-global-break) ; whether a global break occurred.
2367 (defvar edebug-break-condition) ; whether the breakpoint is conditional.
2369 (defvar edebug-break-result nil)
2370 (defvar edebug-global-break-result nil)
2373 (defun edebug-debugger (offset-index arg-mode value)
2374 (if inhibit-redisplay
2375 ;; Don't really try to enter edebug within an eval from redisplay.
2376 value
2377 ;; Check breakpoints and pending input.
2378 ;; If edebug display should be updated, call edebug--display.
2379 ;; Return value.
2380 (let* ( ;; This needs to be here since breakpoints may be changed.
2381 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
2382 (edebug-break-data (assq offset-index edebug-breakpoints))
2383 (edebug-break-condition (car (cdr edebug-break-data)))
2384 (edebug-global-break
2385 (if edebug-global-break-condition
2386 (condition-case nil
2387 (setq edebug-global-break-result
2388 (edebug-eval edebug-global-break-condition))
2389 (error nil))))
2390 (edebug-break))
2392 ;;(edebug-trace "exp: %s" value)
2393 ;; Test whether we should break.
2394 (setq edebug-break
2395 (or edebug-global-break
2396 (and edebug-break-data
2397 (or (not edebug-break-condition)
2398 (setq edebug-break-result
2399 (edebug-eval edebug-break-condition))))))
2400 (if (and edebug-break
2401 (nth 2 edebug-break-data)) ; is it temporary?
2402 ;; Delete the breakpoint.
2403 (setcdr edebug-data
2404 (cons (delq edebug-break-data edebug-breakpoints)
2405 (cdr (cdr edebug-data)))))
2407 ;; Display if mode is not go, continue, or Continue-fast
2408 ;; or break, or input is pending,
2409 (if (or (not (memq edebug-execution-mode '(go continue Continue-fast)))
2410 edebug-break
2411 (input-pending-p))
2412 (edebug--display value offset-index arg-mode)) ; <---------- display
2414 value)))
2417 ;; window-start now stored with each function.
2418 ;;(defvar edebug-window-start nil)
2419 ;; Remember where each buffers' window starts between edebug calls.
2420 ;; This is to avoid spurious recentering.
2421 ;; Does this still need to be buffer-local??
2422 ;;(setq-default edebug-window-start nil)
2423 ;;(make-variable-buffer-local 'edebug-window-start)
2426 ;; Dynamically declared unbound vars
2427 (defvar edebug-point) ; the point in edebug buffer
2428 (defvar edebug-outside-buffer) ; the current-buffer outside of edebug
2429 (defvar edebug-outside-point) ; the point outside of edebug
2430 (defvar edebug-outside-mark) ; the mark outside of edebug
2431 (defvar edebug-window-data) ; window and window-start for current function
2432 (defvar edebug-outside-windows) ; outside window configuration
2433 (defvar edebug-eval-buffer) ; for the evaluation list.
2434 (defvar edebug-outside-d-c-i-n-s-w) ; outside default cursor-in-non-selected-windows
2436 (defvar edebug-eval-list nil) ;; List of expressions to evaluate.
2438 (defvar edebug-previous-result nil) ;; Last result returned.
2440 ;; Emacs 19 adds an arg to mark and mark-marker.
2441 (defalias 'edebug-mark-marker 'mark-marker)
2443 (defun edebug--display (value offset-index arg-mode)
2444 ;; edebug--display-1 is too big, we should split it. This function
2445 ;; here was just introduced to avoid making edebug--display-1
2446 ;; yet a bit deeper.
2447 (save-excursion (edebug--display-1 value offset-index arg-mode)))
2449 (defun edebug--display-1 (value offset-index arg-mode)
2450 (unless (marker-position edebug-def-mark)
2451 ;; The buffer holding the source has been killed.
2452 ;; Let's at least show a backtrace so the user can figure out
2453 ;; which function we're talking about.
2454 (debug))
2455 ;; Setup windows for edebug, determine mode, maybe enter recursive-edit.
2456 ;; Uses local variables of edebug-enter, edebug-before, edebug-after
2457 ;; and edebug-debugger.
2458 (let ((edebug-active t) ; For minor mode alist.
2459 (edebug-with-timeout-suspend (with-timeout-suspend))
2460 edebug-stop ; Should we enter recursive-edit?
2461 (edebug-point (+ edebug-def-mark
2462 (aref (nth 2 edebug-data) offset-index)))
2463 edebug-buffer-outside-point ; current point in edebug-buffer
2464 ;; window displaying edebug-buffer
2465 (edebug-window-data (nth 3 edebug-data))
2466 (edebug-outside-window (selected-window))
2467 (edebug-outside-buffer (current-buffer))
2468 (edebug-outside-point (point))
2469 (edebug-outside-mark (edebug-mark))
2470 edebug-outside-windows ; Window or screen configuration.
2471 edebug-buffer-points
2473 edebug-eval-buffer ; Declared here so we can kill it below.
2474 (eval-result-list (and edebug-eval-list
2475 (edebug-eval-result-list)))
2476 edebug-trace-window
2477 edebug-trace-window-start
2479 (edebug-outside-d-c-i-n-s-w
2480 (default-value 'cursor-in-non-selected-windows)))
2481 (unwind-protect
2482 (let ((cursor-in-echo-area nil)
2483 (unread-command-events nil)
2484 ;; any others??
2486 (setq-default cursor-in-non-selected-windows t)
2487 (if (not (buffer-name edebug-buffer))
2488 (user-error "Buffer defining %s not found" edebug-function))
2490 (if (eq 'after arg-mode)
2491 ;; Compute result string now before windows are modified.
2492 (edebug-compute-previous-result value))
2494 (if edebug-save-windows
2495 ;; Save windows now before we modify them.
2496 (setq edebug-outside-windows
2497 (edebug-current-windows edebug-save-windows)))
2499 (if edebug-save-displayed-buffer-points
2500 (setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
2502 ;; First move the edebug buffer point to edebug-point
2503 ;; so that window start doesn't get changed when we display it.
2504 ;; I don't know if this is going to help.
2505 ;;(set-buffer edebug-buffer)
2506 ;;(goto-char edebug-point)
2508 ;; If edebug-buffer is not currently displayed,
2509 ;; first find a window for it.
2510 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data))
2511 (setcar edebug-window-data (selected-window))
2513 ;; Now display eval list, if any.
2514 ;; This is done after the pop to edebug-buffer
2515 ;; so that buffer-window correspondence is correct after quitting.
2516 (edebug-eval-display eval-result-list)
2517 ;; The evaluation list better not have deleted edebug-window-data.
2518 (select-window (car edebug-window-data))
2519 (set-buffer edebug-buffer)
2521 (setq edebug-buffer-outside-point (point))
2522 (goto-char edebug-point)
2524 (if (eq 'before arg-mode)
2525 ;; Check whether positions are up-to-date.
2526 ;; This assumes point is never before symbol.
2527 (if (not (memq (following-char) '(?\( ?\# ?\` )))
2528 (user-error "Source has changed - reevaluate definition of %s"
2529 edebug-function)
2532 ;; Make sure we bind those in the right buffer (bug#16410).
2533 (let ((overlay-arrow-position overlay-arrow-position)
2534 (overlay-arrow-string overlay-arrow-string))
2535 ;; Now display arrow based on mode.
2536 (edebug-overlay-arrow)
2538 (cond
2539 ((eq 'error arg-mode)
2540 ;; Display error message
2541 (setq edebug-execution-mode 'step)
2542 (edebug-overlay-arrow)
2543 (beep)
2544 (if (eq 'quit (car value))
2545 (message "Quit")
2546 (edebug-report-error value)))
2547 (edebug-break
2548 (cond
2549 (edebug-global-break
2550 (message "Global Break: %s => %s"
2551 edebug-global-break-condition
2552 edebug-global-break-result))
2553 (edebug-break-condition
2554 (message "Break: %s => %s"
2555 edebug-break-condition
2556 edebug-break-result))
2557 ((not (eq edebug-execution-mode 'Continue-fast))
2558 (message "Break"))
2559 (t)))
2561 (t (message "")))
2563 (if (eq 'after arg-mode)
2564 (progn
2565 ;; Display result of previous evaluation.
2566 (if (and edebug-break
2567 edebug-sit-on-break
2568 (not (eq edebug-execution-mode 'Continue-fast)))
2569 (sit-for edebug-sit-for-seconds)) ; Show message.
2570 (edebug-previous-result)))
2572 (cond
2573 (edebug-break
2574 (cond
2575 ((eq edebug-execution-mode 'continue)
2576 (sit-for edebug-sit-for-seconds))
2577 ((eq edebug-execution-mode 'Continue-fast) (sit-for 0))
2578 (t (setq edebug-stop t))))
2579 ;; not edebug-break
2580 ((eq edebug-execution-mode 'trace)
2581 (sit-for edebug-sit-for-seconds)) ; Force update and pause.
2582 ((eq edebug-execution-mode 'Trace-fast)
2583 (sit-for 0))) ; Force update and continue.
2585 (when (input-pending-p)
2586 (setq edebug-stop t)
2587 (setq edebug-execution-mode 'step) ; for `edebug-overlay-arrow'
2588 (edebug-stop))
2590 (edebug-overlay-arrow)
2592 (unwind-protect
2593 (if (or edebug-stop
2594 (memq edebug-execution-mode '(step next))
2595 (eq arg-mode 'error))
2596 (edebug--recursive-edit arg-mode)) ; <--- Recursive edit
2598 ;; Reset the edebug-window-data to whatever it is now.
2599 (let ((window (if (eq (window-buffer) edebug-buffer)
2600 (selected-window)
2601 (get-buffer-window edebug-buffer))))
2602 ;; Remember window-start for edebug-buffer, if still displayed.
2603 (if window
2604 (progn
2605 (setcar edebug-window-data window)
2606 (setcdr edebug-window-data (window-start window)))))
2608 ;; Save trace window point before restoring outside windows.
2609 ;; Could generalize this for other buffers.
2610 (setq edebug-trace-window
2611 (get-buffer-window edebug-trace-buffer))
2612 (if edebug-trace-window
2613 (setq edebug-trace-window-start
2614 (and edebug-trace-window
2615 (window-start edebug-trace-window))))
2617 ;; Restore windows before continuing.
2618 (if edebug-save-windows
2619 (progn
2620 (edebug-set-windows edebug-outside-windows)
2622 ;; Restore displayed buffer points.
2623 ;; Needed even if restoring windows because
2624 ;; window-points are not restored. (should they be??)
2625 (if edebug-save-displayed-buffer-points
2626 (edebug-set-buffer-points edebug-buffer-points))
2628 ;; Unrestore trace window's window-point.
2629 (if edebug-trace-window
2630 (set-window-start edebug-trace-window
2631 edebug-trace-window-start))
2633 ;; Unrestore edebug-buffer's window-start, if displayed.
2634 (let ((window (car edebug-window-data)))
2635 (if (and (edebug-window-live-p window)
2636 (eq (window-buffer) edebug-buffer))
2637 (progn
2638 (set-window-start window (cdr edebug-window-data)
2639 'no-force)
2640 ;; Unrestore edebug-buffer's window-point.
2641 ;; Needed in addition to setting the buffer point
2642 ;; - otherwise quitting doesn't leave point as is.
2643 ;; But can this causes point to not be restored.
2644 ;; Also, it may not be a visible window.
2645 ;; (set-window-point window edebug-point)
2648 ;; Unrestore edebug-buffer's point. Rerestored below.
2649 ;; (goto-char edebug-point) ;; in edebug-buffer
2651 ;; Since we may be in a save-excursion, in case of quit,
2652 ;; reselect the outside window only.
2653 ;; Only needed if we are not recovering windows??
2654 (if (edebug-window-live-p edebug-outside-window)
2655 (select-window edebug-outside-window))
2656 ) ; if edebug-save-windows
2658 ;; Restore current buffer always, in case application needs it.
2659 (if (buffer-name edebug-outside-buffer)
2660 (set-buffer edebug-outside-buffer))
2661 ;; Restore point, and mark.
2662 ;; Needed even if restoring windows because
2663 ;; that doesn't restore point and mark in the current buffer.
2664 ;; But don't restore point if edebug-buffer is current buffer.
2665 (if (not (eq edebug-buffer edebug-outside-buffer))
2666 (goto-char edebug-outside-point))
2667 (if (marker-buffer (edebug-mark-marker))
2668 ;; Does zmacs-regions need to be nil while doing set-marker?
2669 (set-marker (edebug-mark-marker) edebug-outside-mark))
2670 )) ; unwind-protect
2671 ;; None of the following is done if quit or signal occurs.
2673 ;; Restore edebug-buffer's outside point.
2674 ;; (edebug-trace "restore edebug-buffer point: %s"
2675 ;; edebug-buffer-outside-point)
2676 (with-current-buffer edebug-buffer
2677 (goto-char edebug-buffer-outside-point))
2678 ;; ... nothing more.
2680 ;; Could be an option to keep eval display up.
2681 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
2682 (with-timeout-unsuspend edebug-with-timeout-suspend)
2683 ;; Reset global variables to outside values in case they were changed.
2684 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
2688 (defvar edebug-number-of-recursions 0)
2689 ;; Number of recursive edits started by edebug.
2690 ;; Should be 0 at the top level.
2692 (defvar edebug-recursion-depth 0)
2693 ;; Value of recursion-depth when edebug was called.
2695 ;; Dynamically declared unbound vars
2696 (defvar edebug-outside-match-data) ; match data outside of edebug
2697 (defvar edebug-backtrace-buffer) ; each recursive edit gets its own
2698 (defvar edebug-inside-windows)
2699 (defvar edebug-interactive-p)
2701 (defun edebug--recursive-edit (arg-mode)
2702 ;; Start up a recursive edit inside of edebug.
2703 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
2704 ;; Assume that none of the variables below are buffer-local.
2705 (let (;; match-data must be done in the outside buffer
2706 (edebug-outside-match-data
2707 (with-current-buffer edebug-outside-buffer ; in case match buffer different
2708 (match-data)))
2710 ;;(edebug-number-of-recursions (1+ edebug-number-of-recursions))
2711 (edebug-recursion-depth (recursion-depth))
2712 edebug-entered ; bind locally to nil
2713 (edebug-interactive-p nil) ; again non-interactive
2714 edebug-backtrace-buffer ; each recursive edit gets its own
2715 ;; The window configuration may be saved and restored
2716 ;; during a recursive-edit
2717 edebug-inside-windows
2720 (unwind-protect
2721 (let (
2722 ;; Declare global values local but using the same global value.
2723 ;; We could set these to the values for previous edebug call.
2724 (last-command last-command)
2725 (this-command this-command)
2726 (current-prefix-arg nil)
2728 ;; More for Emacs 19
2729 (last-input-event nil)
2730 (last-command-event nil)
2731 (last-event-frame nil)
2732 (last-nonmenu-event nil)
2733 (track-mouse nil)
2735 (standard-output t)
2736 (standard-input t)
2738 ;; Don't keep reading from an executing kbd macro
2739 ;; within edebug unless edebug-continue-kbd-macro is
2740 ;; non-nil. Again, local binding may not be best.
2741 (executing-kbd-macro
2742 (if edebug-continue-kbd-macro executing-kbd-macro))
2744 ;; Don't get confused by the user's keymap changes.
2745 (overriding-local-map nil)
2746 (overriding-terminal-local-map nil)
2748 ;; Bind again to outside values.
2749 (debug-on-error edebug-outside-debug-on-error)
2750 (debug-on-quit edebug-outside-debug-on-quit)
2752 ;; Don't keep defining a kbd macro.
2753 (defining-kbd-macro
2754 (if edebug-continue-kbd-macro defining-kbd-macro))
2756 ;; others??
2759 (if (and (eq edebug-execution-mode 'go)
2760 (not (memq arg-mode '(after error))))
2761 (message "Break"))
2763 (setq signal-hook-function nil)
2765 (edebug-mode 1)
2766 (unwind-protect
2767 (recursive-edit) ; <<<<<<<<<< Recursive edit
2769 ;; Do the following, even if quit occurs.
2770 (setq signal-hook-function 'edebug-signal)
2771 (if edebug-backtrace-buffer
2772 (kill-buffer edebug-backtrace-buffer))
2774 ;; Remember selected-window after recursive-edit.
2775 ;; (setq edebug-inside-window (selected-window))
2777 (set-match-data edebug-outside-match-data)
2779 ;; Recursive edit may have changed buffers,
2780 ;; so set it back before exiting let.
2781 (if (buffer-name edebug-buffer) ; if it still exists
2782 (progn
2783 (set-buffer edebug-buffer)
2784 (when (memq edebug-execution-mode '(go Go-nonstop))
2785 (edebug-overlay-arrow)
2786 (sit-for 0))
2787 (edebug-mode -1))
2788 ;; gotta have a buffer to let its buffer local variables be set
2789 (get-buffer-create " bogus edebug buffer"))
2790 ));; inner let
2794 ;;; Display related functions
2796 (defconst edebug-arrow-alist
2797 '((Continue-fast . "=")
2798 (Trace-fast . "-")
2799 (continue . ">")
2800 (trace . "->")
2801 (step . "=>")
2802 (next . "=>")
2803 (go . "<>")
2804 (Go-nonstop . "..")
2806 "Association list of arrows for each edebug mode.")
2808 (defun edebug-overlay-arrow ()
2809 ;; Set up the overlay arrow at beginning-of-line in current buffer.
2810 ;; The arrow string is derived from edebug-arrow-alist and
2811 ;; edebug-execution-mode.
2812 (let ((pos (line-beginning-position)))
2813 (setq overlay-arrow-string
2814 (cdr (assq edebug-execution-mode edebug-arrow-alist)))
2815 (setq overlay-arrow-position (make-marker))
2816 (set-marker overlay-arrow-position pos (current-buffer))))
2819 (defun edebug-toggle-save-all-windows ()
2820 "Toggle the saving and restoring of all windows.
2821 Also, each time you toggle it on, the inside and outside window
2822 configurations become the same as the current configuration."
2823 (interactive)
2824 (setq edebug-save-windows (not edebug-save-windows))
2825 (if edebug-save-windows
2826 (setq edebug-inside-windows
2827 (setq edebug-outside-windows
2828 (edebug-current-windows
2829 edebug-save-windows))))
2830 (message "Window saving is %s for all windows."
2831 (if edebug-save-windows "on" "off")))
2833 (defmacro edebug-changing-windows (&rest body)
2834 `(let ((window (selected-window)))
2835 (setq edebug-inside-windows (edebug-current-windows t))
2836 (edebug-set-windows edebug-outside-windows)
2837 ,@body;; Code to change edebug-save-windows
2838 (setq edebug-outside-windows (edebug-current-windows
2839 edebug-save-windows))
2840 ;; Problem: what about outside windows that are deleted inside?
2841 (edebug-set-windows edebug-inside-windows)))
2843 (defun edebug-toggle-save-selected-window ()
2844 "Toggle the saving and restoring of the selected window.
2845 Also, each time you toggle it on, the inside and outside window
2846 configurations become the same as the current configuration."
2847 (interactive)
2848 (cond
2849 ((eq t edebug-save-windows)
2850 ;; Save all outside windows except the selected one.
2851 ;; Remove (selected-window) from outside-windows.
2852 (edebug-changing-windows
2853 (setq edebug-save-windows (delq window (edebug-window-list)))))
2855 ((memq (selected-window) edebug-save-windows)
2856 (setq edebug-outside-windows
2857 (delq (assq (selected-window) edebug-outside-windows)
2858 edebug-outside-windows))
2859 (setq edebug-save-windows
2860 (delq (selected-window) edebug-save-windows)))
2861 (t ; Save a new window.
2862 (edebug-changing-windows
2863 (setq edebug-save-windows (cons window edebug-save-windows)))))
2865 (message "Window saving is %s for %s."
2866 (if (memq (selected-window) edebug-save-windows)
2867 "on" "off")
2868 (selected-window)))
2870 (defun edebug-toggle-save-windows (arg)
2871 "Toggle the saving and restoring of windows.
2872 With prefix, toggle for just the selected window.
2873 Otherwise, toggle for all windows."
2874 (interactive "P")
2875 (if arg
2876 (edebug-toggle-save-selected-window)
2877 (edebug-toggle-save-all-windows)))
2879 (defun edebug-where ()
2880 "Show the debug windows and where we stopped in the program."
2881 (interactive)
2882 (if (not edebug-active)
2883 (error "Edebug is not active"))
2884 ;; Restore the window configuration to what it last was inside.
2885 ;; But it is not always set. - experiment
2886 ;;(if edebug-inside-windows
2887 ;; (edebug-set-windows edebug-inside-windows))
2888 (edebug-pop-to-buffer edebug-buffer)
2889 (goto-char edebug-point))
2891 (defun edebug-view-outside ()
2892 "Change to the outside window configuration.
2893 Use `edebug-where' to return."
2894 (interactive)
2895 (if (not edebug-active)
2896 (error "Edebug is not active"))
2897 (setq edebug-inside-windows
2898 (edebug-current-windows edebug-save-windows))
2899 (edebug-set-windows edebug-outside-windows)
2900 (goto-char edebug-outside-point)
2901 (message "Window configuration outside of Edebug. Return with %s"
2902 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
2905 (defun edebug-bounce-point (arg)
2906 "Bounce the point in the outside current buffer.
2907 If prefix argument ARG is supplied, sit for that many seconds
2908 before returning. The default is one second."
2909 (interactive "p")
2910 (if (not edebug-active)
2911 (error "Edebug is not active"))
2912 (save-excursion
2913 ;; If the buffer's currently displayed, avoid set-window-configuration.
2914 (save-window-excursion
2915 (edebug-pop-to-buffer edebug-outside-buffer)
2916 (goto-char edebug-outside-point)
2917 (message "Current buffer: %s Point: %s Mark: %s"
2918 (current-buffer) (point)
2919 (if (marker-buffer (edebug-mark-marker))
2920 (marker-position (edebug-mark-marker)) "<not set>"))
2921 (sit-for arg)
2922 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data)))))
2925 ;; Joe Wells, here is a start at your idea of adding a buffer to the internal
2926 ;; display list. Still need to use this list in edebug--display.
2928 '(defvar edebug-display-buffer-list nil
2929 "List of buffers that edebug will display when it is active.")
2931 '(defun edebug-display-buffer (buffer)
2932 "Toggle display of a buffer inside of edebug."
2933 (interactive "bBuffer: ")
2934 (let ((already-displaying (memq buffer edebug-display-buffer-list)))
2935 (setq edebug-display-buffer-list
2936 (if already-displaying
2937 (delq buffer edebug-display-buffer-list)
2938 (cons buffer edebug-display-buffer-list)))
2939 (message "Displaying %s %s" buffer
2940 (if already-displaying "off" "on"))))
2942 ;;; Breakpoint related functions
2944 (defun edebug-find-stop-point ()
2945 ;; Return (function . index) of the nearest edebug stop point.
2946 (let* ((edebug-def-name (edebug-form-data-symbol))
2947 (edebug-data
2948 (let ((data (get edebug-def-name 'edebug)))
2949 (if (or (null data) (markerp data))
2950 (error "%s is not instrumented for Edebug" edebug-def-name))
2951 data)) ; we could do it automatically, if data is a marker.
2952 ;; pull out parts of edebug-data.
2953 (edebug-def-mark (car edebug-data))
2954 ;; (edebug-breakpoints (car (cdr edebug-data)))
2956 (offset-vector (nth 2 edebug-data))
2957 (offset (- (save-excursion
2958 (if (looking-at "[ \t]")
2959 ;; skip backwards until non-whitespace, or bol
2960 (skip-chars-backward " \t"))
2961 (point))
2962 edebug-def-mark))
2963 len i)
2964 ;; the offsets are in order so we can do a linear search
2965 (setq len (length offset-vector))
2966 (setq i 0)
2967 (while (and (< i len) (> offset (aref offset-vector i)))
2968 (setq i (1+ i)))
2969 (if (and (< i len)
2970 (<= offset (aref offset-vector i)))
2971 ;; return the relevant info
2972 (cons edebug-def-name i)
2973 (message "Point is not on an expression in %s."
2974 edebug-def-name)
2978 (defun edebug-next-breakpoint ()
2979 "Move point to the next breakpoint, or first if none past point."
2980 (interactive)
2981 (let ((edebug-stop-point (edebug-find-stop-point)))
2982 (if edebug-stop-point
2983 (let* ((edebug-def-name (car edebug-stop-point))
2984 (index (cdr edebug-stop-point))
2985 (edebug-data (get edebug-def-name 'edebug))
2987 ;; pull out parts of edebug-data
2988 (edebug-def-mark (car edebug-data))
2989 (edebug-breakpoints (car (cdr edebug-data)))
2990 (offset-vector (nth 2 edebug-data))
2991 breakpoint)
2992 (if (not edebug-breakpoints)
2993 (message "No breakpoints in this function.")
2994 (let ((breaks edebug-breakpoints))
2995 (while (and breaks
2996 (<= (car (car breaks)) index))
2997 (setq breaks (cdr breaks)))
2998 (setq breakpoint
2999 (if breaks
3000 (car breaks)
3001 ;; goto the first breakpoint
3002 (car edebug-breakpoints)))
3003 (goto-char (+ edebug-def-mark
3004 (aref offset-vector (car breakpoint))))
3006 (message "%s"
3007 (concat (if (nth 2 breakpoint)
3008 "Temporary " "")
3009 (if (car (cdr breakpoint))
3010 (format "Condition: %s"
3011 (edebug-safe-prin1-to-string
3012 (car (cdr breakpoint))))
3013 "")))
3014 ))))))
3017 (defun edebug-modify-breakpoint (flag &optional condition temporary)
3018 "Modify the breakpoint for the form at point or after it.
3019 Set it if FLAG is non-nil, clear it otherwise. Then move to that point.
3020 If CONDITION or TEMPORARY are non-nil, add those attributes to
3021 the breakpoint."
3022 (let ((edebug-stop-point (edebug-find-stop-point)))
3023 (if edebug-stop-point
3024 (let* ((edebug-def-name (car edebug-stop-point))
3025 (index (cdr edebug-stop-point))
3026 (edebug-data (get edebug-def-name 'edebug))
3028 ;; pull out parts of edebug-data
3029 (edebug-def-mark (car edebug-data))
3030 (edebug-breakpoints (car (cdr edebug-data)))
3031 (offset-vector (nth 2 edebug-data))
3032 present)
3033 ;; delete it either way
3034 (setq present (assq index edebug-breakpoints))
3035 (setq edebug-breakpoints (delq present edebug-breakpoints))
3036 (if flag
3037 (progn
3038 ;; add it to the list and resort
3039 (setq edebug-breakpoints
3040 (edebug-sort-alist
3041 (cons
3042 (list index condition temporary)
3043 edebug-breakpoints) '<))
3044 (if condition
3045 (message "Breakpoint set in %s with condition: %s"
3046 edebug-def-name condition)
3047 (message "Breakpoint set in %s" edebug-def-name)))
3048 (if present
3049 (message "Breakpoint unset in %s" edebug-def-name)
3050 (message "No breakpoint here")))
3052 (setcar (cdr edebug-data) edebug-breakpoints)
3053 (goto-char (+ edebug-def-mark (aref offset-vector index)))
3054 ))))
3056 (defun edebug-set-breakpoint (arg)
3057 "Set the breakpoint of nearest sexp.
3058 With prefix argument, make it a temporary breakpoint."
3059 (interactive "P")
3060 (edebug-modify-breakpoint t nil arg))
3062 (defun edebug-unset-breakpoint ()
3063 "Clear the breakpoint of nearest sexp."
3064 (interactive)
3065 (edebug-modify-breakpoint nil))
3068 (defun edebug-set-global-break-condition (expression)
3069 "Set `edebug-global-break-condition' to EXPRESSION."
3070 (interactive
3071 (list
3072 (let ((initial (and edebug-global-break-condition
3073 (format "%s" edebug-global-break-condition))))
3074 (read-from-minibuffer
3075 "Global Condition: " initial read-expression-map t
3076 (if (equal (car read-expression-history) initial)
3077 '(read-expression-history . 1)
3078 'read-expression-history)))))
3079 (setq edebug-global-break-condition expression))
3082 ;;; Mode switching functions
3084 (defun edebug-set-mode (mode shortmsg msg)
3085 ;; Set the edebug mode to MODE.
3086 ;; Display SHORTMSG, or MSG if not within edebug.
3087 (if (eq (1+ edebug-recursion-depth) (recursion-depth))
3088 (progn
3089 (setq edebug-execution-mode mode)
3090 (message "%s" shortmsg)
3091 ;; Continue execution
3092 (exit-recursive-edit))
3093 ;; This is not terribly useful!!
3094 (setq edebug-next-execution-mode mode)
3095 (message "%s" msg)))
3098 (defalias 'edebug-step-through-mode 'edebug-step-mode)
3100 (defun edebug-step-mode ()
3101 "Proceed to next stop point."
3102 (interactive)
3103 (edebug-set-mode 'step "" "Edebug will stop at next stop point."))
3105 (defun edebug-next-mode ()
3106 "Proceed to next `after' stop point."
3107 (interactive)
3108 (edebug-set-mode 'next "" "Edebug will stop after next eval."))
3110 (defun edebug-go-mode (arg)
3111 "Go, evaluating until break.
3112 With prefix ARG, set temporary break at current point and go."
3113 (interactive "P")
3114 (if arg
3115 (edebug-set-breakpoint t))
3116 (edebug-set-mode 'go "Go..." "Edebug will go until break."))
3118 (defun edebug-Go-nonstop-mode ()
3119 "Go, evaluating without debugging.
3120 You can use `edebug-stop', or any editing command, to stop."
3121 (interactive)
3122 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
3123 "Edebug will not stop at breaks."))
3126 (defun edebug-trace-mode ()
3127 "Begin trace mode.
3128 Pauses for `edebug-sit-for-seconds' at each stop point."
3129 (interactive)
3130 (edebug-set-mode 'trace "Tracing..." "Edebug will trace with pause."))
3132 (defun edebug-Trace-fast-mode ()
3133 "Trace with no wait at each step.
3134 Updates the display at each stop point, but does not pause."
3135 (interactive)
3136 (edebug-set-mode 'Trace-fast
3137 "Trace fast..." "Edebug will trace without pause."))
3139 (defun edebug-continue-mode ()
3140 "Begin continue mode.
3141 Pauses for `edebug-sit-for-seconds' at each break point."
3142 (interactive)
3143 (edebug-set-mode 'continue "Continue..."
3144 "Edebug will pause at breakpoints."))
3146 (defun edebug-Continue-fast-mode ()
3147 "Trace with no wait at each step.
3148 Updates the display at each break point, but does not pause."
3149 (interactive)
3150 (edebug-set-mode 'Continue-fast "Continue fast..."
3151 "Edebug will stop and go at breakpoints."))
3153 ;; ------------------------------------------------------------
3154 ;; The following use the mode changing commands and breakpoints.
3157 (defun edebug-goto-here ()
3158 "Proceed to first stop-point at or after current position of point."
3159 (interactive)
3160 (edebug-go-mode t))
3163 (defun edebug-stop ()
3164 "Stop execution and do not continue.
3165 Useful for exiting from trace or continue loop."
3166 (interactive)
3167 (message "Stop"))
3170 '(defun edebug-forward ()
3171 "Proceed to the exit of the next expression to be evaluated."
3172 (interactive)
3173 (edebug-set-mode
3174 'forward "Forward"
3175 "Edebug will stop after exiting the next expression."))
3178 (defun edebug-forward-sexp (arg)
3179 "Proceed from the current point to the end of the ARGth sexp ahead.
3180 If there are not ARG sexps ahead, then do `edebug-step-out'."
3181 (interactive "p")
3182 (condition-case nil
3183 (let ((parse-sexp-ignore-comments t))
3184 ;; Call forward-sexp repeatedly until done or failure.
3185 (forward-sexp arg)
3186 (edebug-go-mode t))
3187 (error
3188 (edebug-step-out)
3191 (defun edebug-step-out ()
3192 "Proceed from the current point to the end of the containing sexp.
3193 If there is no containing sexp that is not the top level defun,
3194 go to the end of the last sexp, or if that is the same point, then step."
3195 (interactive)
3196 (condition-case nil
3197 (let ((parse-sexp-ignore-comments t))
3198 (up-list 1)
3199 (save-excursion
3200 ;; Is there still a containing expression?
3201 (up-list 1))
3202 (edebug-go-mode t))
3203 (error
3204 ;; At top level - 1, so first check if there are more sexps at this level.
3205 (let ((start-point (point)))
3206 ;; (up-list 1)
3207 (down-list -1)
3208 (if (= (point) start-point)
3209 (edebug-step-mode) ; No more at this level, so step.
3210 (edebug-go-mode t)
3211 )))))
3213 (defun edebug-instrument-function (func)
3214 "Instrument the function or generic method FUNC.
3215 Return the list of function symbols which were instrumented.
3216 This may be simply (FUNC) for a normal function, or a list of
3217 generated symbols for methods. If a function or method to
3218 instrument cannot be found, signal an error."
3219 (let ((func-marker (get func 'edebug)))
3220 (cond
3221 ((cl-generic-p func)
3222 (let ((method-defs (cl--generic-method-files func))
3223 symbols)
3224 (unless method-defs
3225 (error "Could not find any method definitions for %s" func))
3226 (pcase-dolist (`(,file . ,spec) method-defs)
3227 (let* ((loc (find-function-search-for-symbol spec 'cl-defmethod file)))
3228 (unless (cdr loc)
3229 (error "Could not find the definition for %s in its file" spec))
3230 (with-current-buffer (car loc)
3231 (goto-char (cdr loc))
3232 (edebug-eval-top-level-form)
3233 (push (edebug-form-data-symbol) symbols))))
3234 symbols))
3235 ((and (markerp func-marker) (marker-buffer func-marker))
3236 ;; It is uninstrumented, so instrument it.
3237 (with-current-buffer (marker-buffer func-marker)
3238 (goto-char func-marker)
3239 (edebug-eval-top-level-form)
3240 (list func)))
3241 ((consp func-marker)
3242 (message "%s is already instrumented." func)
3243 (list func))
3245 (let ((loc (find-function-noselect func t)))
3246 (unless (cdr loc)
3247 (error "Could not find the definition in its file"))
3248 (with-current-buffer (car loc)
3249 (goto-char (cdr loc))
3250 (edebug-eval-top-level-form)
3251 (list func)))))))
3253 (defun edebug-instrument-callee ()
3254 "Instrument the definition of the function or macro about to be called.
3255 Do this when stopped before the form or it will be too late.
3256 One side effect of using this command is that the next time the
3257 function or macro is called, Edebug will be called there as well.
3258 If the callee is a generic function, Edebug will instrument all
3259 the methods, not just the one which is about to be called. Return
3260 the list of symbols which were instrumented."
3261 (interactive)
3262 (if (not (looking-at "("))
3263 (error "You must be before a list form")
3264 (let ((func
3265 (save-excursion
3266 (down-list 1)
3267 (if (looking-at "(")
3268 (edebug--form-data-name
3269 (edebug-get-form-data-entry (point)))
3270 (read (current-buffer))))))
3271 (edebug-instrument-function func))))
3274 (defun edebug-step-in ()
3275 "Step into the definition of the function, macro or method about to be called.
3276 This first does `edebug-instrument-callee' to ensure that it is
3277 instrumented. Then it does `edebug-on-entry' and switches to `go' mode."
3278 (interactive)
3279 (let ((funcs (edebug-instrument-callee)))
3280 (if funcs
3281 (progn
3282 (mapc (lambda (func) (edebug-on-entry func 'temp)) funcs)
3283 (edebug-go-mode nil)))))
3285 (defun edebug-on-entry (function &optional flag)
3286 "Cause Edebug to stop when FUNCTION is called.
3287 With prefix argument, make this temporary so it is automatically
3288 canceled the first time the function is entered."
3289 (interactive "aEdebug on entry to: \nP")
3290 ;; Could store this in the edebug data instead.
3291 (put function 'edebug-on-entry (if flag 'temp t)))
3293 (defun cancel-edebug-on-entry (function)
3294 (interactive "aEdebug on entry to: ")
3295 (put function 'edebug-on-entry nil))
3298 '(advice-add 'debug-on-entry :around 'edebug--debug-on-entry) ;; Should we do this?
3299 ;; Also need edebug-cancel-debug-on-entry
3301 '(defun edebug--debug-on-entry (orig function)
3302 "If the function is instrumented for Edebug, call `edebug-on-entry'."
3303 (let ((func-data (get function 'edebug)))
3304 (if (or (null func-data) (markerp func-data))
3305 (funcall orig function)
3306 (edebug-on-entry function))))
3309 (defun edebug-top-level-nonstop ()
3310 "Set mode to Go-nonstop, and exit to top-level.
3311 This is useful for exiting even if `unwind-protect' code may be executed."
3312 (interactive)
3313 (setq edebug-execution-mode 'Go-nonstop)
3314 (top-level))
3316 ;;(defun edebug-exit-out ()
3317 ;; "Go until the current function exits."
3318 ;; (interactive)
3319 ;; (edebug-set-mode 'exiting "Exit..."))
3321 (defconst edebug-initial-mode-alist
3322 '((edebug-step-mode . step)
3323 (edebug-next-mode . next)
3324 (edebug-trace-mode . trace)
3325 (edebug-Trace-fast-mode . Trace-fast)
3326 (edebug-go-mode . go)
3327 (edebug-continue-mode . continue)
3328 (edebug-Continue-fast-mode . Continue-fast)
3329 (edebug-Go-nonstop-mode . Go-nonstop))
3330 "Association list between commands and the modes they set.")
3332 (defvar edebug-mode-map) ; will be defined fully later.
3334 (defun edebug-set-initial-mode ()
3335 "Set the initial execution mode of Edebug.
3336 The mode is requested via the key that would be used to set the mode in
3337 edebug-mode."
3338 (interactive)
3339 (let* ((old-mode edebug-initial-mode)
3340 (key (read-key-sequence
3341 (format
3342 "Change initial edebug mode from %s (%c) to (enter key): "
3343 old-mode
3344 (aref (where-is-internal
3345 (car (rassq old-mode edebug-initial-mode-alist))
3346 edebug-mode-map 'firstonly)
3347 0))))
3348 (mode (cdr (assq (lookup-key edebug-mode-map key)
3349 edebug-initial-mode-alist))))
3350 (if mode
3351 (progn
3352 (setq edebug-initial-mode mode)
3353 (message "Edebug's initial mode is now: %s" mode))
3354 (error "Key must map to one of the mode changing commands"))))
3356 ;;; Evaluation of expressions
3358 (defmacro edebug-outside-excursion (&rest body)
3359 "Evaluate an expression list in the outside context.
3360 Return the result of the last expression."
3361 ;; Only restores the non-variables context since all the variables let-bound
3362 ;; by Edebug will be properly reset to the appropriate context's value by
3363 ;; backtrace-eval.
3364 (declare (debug t))
3365 `(save-excursion ; of current-buffer
3366 (if edebug-save-windows
3367 (progn
3368 ;; After excursion, we will
3369 ;; restore to current window configuration.
3370 (setq edebug-inside-windows
3371 (edebug-current-windows edebug-save-windows))
3372 ;; Restore outside windows.
3373 (edebug-set-windows edebug-outside-windows)))
3375 (set-buffer edebug-buffer) ; why?
3376 (set-match-data edebug-outside-match-data)
3377 ;; Restore outside context.
3378 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
3379 (unwind-protect
3380 ;; FIXME: This restoring of edebug-outside-buffer and
3381 ;; edebug-outside-point is redundant now that backtrace-eval does it
3382 ;; for us.
3383 (with-current-buffer edebug-outside-buffer ; of edebug-buffer
3384 (goto-char edebug-outside-point)
3385 (if (marker-buffer (edebug-mark-marker))
3386 (set-marker (edebug-mark-marker) edebug-outside-mark))
3387 ,@body)
3389 ;; Back to edebug-buffer. Restore rest of inside context.
3390 ;; (use-local-map edebug-inside-map)
3391 (if edebug-save-windows
3392 ;; Restore inside windows.
3393 (edebug-set-windows edebug-inside-windows))
3395 ;; Save values that may have been changed.
3396 (setq edebug-outside-d-c-i-n-s-w
3397 (default-value 'cursor-in-non-selected-windows))
3399 ;; Restore the outside saved values; don't alter
3400 ;; the outside binding loci.
3401 (setq-default cursor-in-non-selected-windows t))))
3403 (defun edebug-eval (expr)
3404 (backtrace-eval expr 0 'edebug-after))
3406 (defun edebug-safe-eval (expr)
3407 ;; Evaluate EXPR safely.
3408 ;; If there is an error, a string is returned describing the error.
3409 (condition-case edebug-err
3410 (edebug-eval expr)
3411 (error (edebug-format "%s: %s" ;; could
3412 (get (car edebug-err) 'error-message)
3413 (car (cdr edebug-err))))))
3415 ;;; Printing
3418 (defun edebug-report-error (value)
3419 ;; Print an error message like command level does.
3420 ;; This also prints the error name if it has no error-message.
3421 (message "%s: %s"
3422 (or (get (car value) 'error-message)
3423 (format "peculiar error (%s)" (car value)))
3424 (mapconcat (lambda (edebug-arg)
3425 ;; continuing after an error may
3426 ;; complain about edebug-arg. why??
3427 (prin1-to-string edebug-arg))
3428 (cdr value) ", ")))
3430 (defvar print-readably) ; defined by lemacs
3431 ;; Alternatively, we could change the definition of
3432 ;; edebug-safe-prin1-to-string to only use these if defined.
3434 (defun edebug-safe-prin1-to-string (value)
3435 (let ((print-escape-newlines t)
3436 (print-length (or edebug-print-length print-length))
3437 (print-level (or edebug-print-level print-level))
3438 (print-circle (or edebug-print-circle print-circle))
3439 (print-readably nil)) ; lemacs uses this.
3440 (edebug-prin1-to-string value)))
3442 (defun edebug-compute-previous-result (previous-value)
3443 (if edebug-unwrap-results
3444 (setq previous-value
3445 (edebug-unwrap* previous-value)))
3446 (setq edebug-previous-result
3447 (concat "Result: "
3448 (edebug-safe-prin1-to-string previous-value)
3449 (eval-expression-print-format previous-value))))
3451 (defun edebug-previous-result ()
3452 "Print the previous result."
3453 (interactive)
3454 (message "%s" edebug-previous-result))
3456 ;;; Read, Eval and Print
3458 (defalias 'edebug-prin1-to-string #'cl-prin1-to-string)
3459 (defalias 'edebug-format #'format-message)
3460 (defalias 'edebug-message #'message)
3462 (defun edebug-eval-expression (expr)
3463 "Evaluate an expression in the outside environment.
3464 If interactive, prompt for the expression.
3465 Print result in minibuffer."
3466 (interactive (list (read-from-minibuffer
3467 "Eval: " nil read-expression-map t
3468 'read-expression-history)))
3469 (princ
3470 (edebug-outside-excursion
3471 (setq values (cons (edebug-eval expr) values))
3472 (concat (edebug-safe-prin1-to-string (car values))
3473 (eval-expression-print-format (car values))))))
3475 (defun edebug-eval-last-sexp ()
3476 "Evaluate sexp before point in the outside environment.
3477 Print value in minibuffer."
3478 (interactive)
3479 (edebug-eval-expression (edebug-last-sexp)))
3481 (defun edebug-eval-print-last-sexp ()
3482 "Evaluate sexp before point in outside environment; insert value.
3483 This prints the value into current buffer."
3484 (interactive)
3485 (let* ((form (edebug-last-sexp))
3486 (result-string
3487 (edebug-outside-excursion
3488 (edebug-safe-prin1-to-string (edebug-safe-eval form))))
3489 (standard-output (current-buffer)))
3490 (princ "\n")
3491 ;; princ the string to get rid of quotes.
3492 (princ result-string)
3493 (princ "\n")
3496 ;;; Edebug Minor Mode
3498 (defvar edebug-inhibit-emacs-lisp-mode-bindings nil
3499 "If non-nil, inhibit Edebug bindings on the C-x C-a key.
3500 By default, loading the `edebug' library causes these bindings to
3501 be installed in `emacs-lisp-mode-map'.")
3503 (define-obsolete-variable-alias 'gud-inhibit-global-bindings
3504 'edebug-inhibit-emacs-lisp-mode-bindings "24.3")
3506 ;; Global GUD bindings for all emacs-lisp-mode buffers.
3507 (unless edebug-inhibit-emacs-lisp-mode-bindings
3508 (define-key emacs-lisp-mode-map "\C-x\C-a\C-s" 'edebug-step-mode)
3509 (define-key emacs-lisp-mode-map "\C-x\C-a\C-n" 'edebug-next-mode)
3510 (define-key emacs-lisp-mode-map "\C-x\C-a\C-c" 'edebug-go-mode)
3511 (define-key emacs-lisp-mode-map "\C-x\C-a\C-l" 'edebug-where)
3512 ;; The following isn't a GUD binding.
3513 (define-key emacs-lisp-mode-map "\C-x\C-a\C-m" 'edebug-set-initial-mode))
3515 (defvar edebug-mode-map
3516 (let ((map (copy-keymap emacs-lisp-mode-map)))
3517 ;; control
3518 (define-key map " " 'edebug-step-mode)
3519 (define-key map "n" 'edebug-next-mode)
3520 (define-key map "g" 'edebug-go-mode)
3521 (define-key map "G" 'edebug-Go-nonstop-mode)
3522 (define-key map "t" 'edebug-trace-mode)
3523 (define-key map "T" 'edebug-Trace-fast-mode)
3524 (define-key map "c" 'edebug-continue-mode)
3525 (define-key map "C" 'edebug-Continue-fast-mode)
3527 ;;(define-key map "f" 'edebug-forward) not implemented
3528 (define-key map "f" 'edebug-forward-sexp)
3529 (define-key map "h" 'edebug-goto-here)
3531 (define-key map "I" 'edebug-instrument-callee)
3532 (define-key map "i" 'edebug-step-in)
3533 (define-key map "o" 'edebug-step-out)
3535 ;; quitting and stopping
3536 (define-key map "q" 'top-level)
3537 (define-key map "Q" 'edebug-top-level-nonstop)
3538 (define-key map "a" 'abort-recursive-edit)
3539 (define-key map "S" 'edebug-stop)
3541 ;; breakpoints
3542 (define-key map "b" 'edebug-set-breakpoint)
3543 (define-key map "u" 'edebug-unset-breakpoint)
3544 (define-key map "B" 'edebug-next-breakpoint)
3545 (define-key map "x" 'edebug-set-conditional-breakpoint)
3546 (define-key map "X" 'edebug-set-global-break-condition)
3548 ;; evaluation
3549 (define-key map "r" 'edebug-previous-result)
3550 (define-key map "e" 'edebug-eval-expression)
3551 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3552 (define-key map "E" 'edebug-visit-eval-list)
3554 ;; views
3555 (define-key map "w" 'edebug-where)
3556 (define-key map "v" 'edebug-view-outside) ;; maybe obsolete??
3557 (define-key map "p" 'edebug-bounce-point)
3558 (define-key map "P" 'edebug-view-outside) ;; same as v
3559 (define-key map "W" 'edebug-toggle-save-windows)
3561 ;; misc
3562 (define-key map "?" 'edebug-help)
3563 (define-key map "d" 'edebug-backtrace)
3565 (define-key map "-" 'negative-argument)
3567 ;; statistics
3568 (define-key map "=" 'edebug-temp-display-freq-count)
3570 ;; GUD bindings
3571 (define-key map "\C-c\C-s" 'edebug-step-mode)
3572 (define-key map "\C-c\C-n" 'edebug-next-mode)
3573 (define-key map "\C-c\C-c" 'edebug-go-mode)
3575 (define-key map "\C-x " 'edebug-set-breakpoint)
3576 (define-key map "\C-c\C-d" 'edebug-unset-breakpoint)
3577 (define-key map "\C-c\C-t"
3578 (lambda () (interactive) (edebug-set-breakpoint t)))
3579 (define-key map "\C-c\C-l" 'edebug-where)
3580 map))
3582 ;; Autoloading these global bindings doesn't make sense because
3583 ;; they cannot be used anyway unless Edebug is already loaded and active.
3585 (defvar global-edebug-prefix "\^XX"
3586 "Prefix key for global edebug commands, available from any buffer.")
3588 (defvar global-edebug-map
3589 (let ((map (make-sparse-keymap)))
3591 (define-key map " " 'edebug-step-mode)
3592 (define-key map "g" 'edebug-go-mode)
3593 (define-key map "G" 'edebug-Go-nonstop-mode)
3594 (define-key map "t" 'edebug-trace-mode)
3595 (define-key map "T" 'edebug-Trace-fast-mode)
3596 (define-key map "c" 'edebug-continue-mode)
3597 (define-key map "C" 'edebug-Continue-fast-mode)
3599 ;; breakpoints
3600 (define-key map "b" 'edebug-set-breakpoint)
3601 (define-key map "u" 'edebug-unset-breakpoint)
3602 (define-key map "x" 'edebug-set-conditional-breakpoint)
3603 (define-key map "X" 'edebug-set-global-break-condition)
3605 ;; views
3606 (define-key map "w" 'edebug-where)
3607 (define-key map "W" 'edebug-toggle-save-windows)
3609 ;; quitting
3610 (define-key map "q" 'top-level)
3611 (define-key map "Q" 'edebug-top-level-nonstop)
3612 (define-key map "a" 'abort-recursive-edit)
3614 ;; statistics
3615 (define-key map "=" 'edebug-display-freq-count)
3616 map)
3617 "Global map of edebug commands, available from any buffer.")
3619 (global-unset-key global-edebug-prefix)
3620 (global-set-key global-edebug-prefix global-edebug-map)
3623 (defun edebug-help ()
3624 "Describe `edebug-mode'."
3625 (interactive)
3626 (describe-function 'edebug-mode))
3628 (defvar edebug--mode-saved-vars nil)
3630 (define-minor-mode edebug-mode
3631 "Mode for Emacs Lisp buffers while in Edebug.
3633 In addition to all Emacs Lisp commands (except those that modify the
3634 buffer) there are local and global key bindings to several Edebug
3635 specific commands. E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
3636 in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3638 Also see bindings for the eval list buffer *edebug* in `edebug-eval-mode'.
3640 The edebug buffer commands:
3641 \\{edebug-mode-map}
3643 Global commands prefixed by `global-edebug-prefix':
3644 \\{global-edebug-map}
3646 Options:
3647 `edebug-setup-hook'
3648 `edebug-all-defs'
3649 `edebug-all-forms'
3650 `edebug-save-windows'
3651 `edebug-save-displayed-buffer-points'
3652 `edebug-initial-mode'
3653 `edebug-trace'
3654 `edebug-test-coverage'
3655 `edebug-continue-kbd-macro'
3656 `edebug-print-length'
3657 `edebug-print-level'
3658 `edebug-print-circle'
3659 `edebug-on-error'
3660 `edebug-on-quit'
3661 `edebug-on-signal'
3662 `edebug-unwrap-results'
3663 `edebug-global-break-condition'"
3664 :lighter " *Debugging*"
3665 :keymap edebug-mode-map
3666 ;; If the user kills the buffer in which edebug is currently active,
3667 ;; exit to top level, because the edebug command loop can't usefully
3668 ;; continue running in such a case.
3670 (if (not edebug-mode)
3671 (progn
3672 (while edebug--mode-saved-vars
3673 (let ((setting (pop edebug--mode-saved-vars)))
3674 (if (consp setting)
3675 (set (car setting) (cdr setting))
3676 (kill-local-variable setting))))
3677 (remove-hook 'kill-buffer-hook 'edebug-kill-buffer t))
3678 (pcase-dolist (`(,var . ,val) '((buffer-read-only . t)))
3679 (push
3680 (if (local-variable-p var) (cons var (symbol-value var)) var)
3681 edebug--mode-saved-vars)
3682 (set (make-local-variable var) val))
3683 ;; Append `edebug-kill-buffer' to the hook to avoid interfering with
3684 ;; other entries that are unguarded against deleted buffer.
3685 (add-hook 'kill-buffer-hook 'edebug-kill-buffer t t)))
3687 (defun edebug-kill-buffer ()
3688 "Used on `kill-buffer-hook' when Edebug is operating in a buffer of Lisp code."
3689 (run-with-timer 0 nil #'top-level))
3691 ;;; edebug eval list mode
3693 ;; A list of expressions and their evaluations is displayed in *edebug*.
3695 (defun edebug-eval-result-list ()
3696 "Return a list of evaluations of `edebug-eval-list'."
3697 ;; Assumes in outside environment.
3698 ;; Don't do any edebug things now.
3699 (let ((edebug-execution-mode 'Go-nonstop)
3700 (edebug-trace nil))
3701 (mapcar #'edebug-safe-eval edebug-eval-list)))
3703 (defun edebug-eval-display-list (eval-result-list)
3704 ;; Assumes edebug-eval-buffer exists.
3705 (let ((standard-output edebug-eval-buffer)
3706 (edebug-comment-line
3707 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
3708 (set-buffer edebug-eval-buffer)
3709 (erase-buffer)
3710 (dolist (exp edebug-eval-list)
3711 (prin1 exp) (terpri)
3712 (prin1 (pop eval-result-list)) (terpri)
3713 (princ edebug-comment-line))
3714 (edebug-pop-to-buffer edebug-eval-buffer)
3717 (defun edebug-create-eval-buffer ()
3718 (unless (and edebug-eval-buffer (buffer-name edebug-eval-buffer))
3719 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
3720 (edebug-eval-mode)))
3722 ;; Should generalize this to be callable outside of edebug
3723 ;; with calls in user functions, e.g. (edebug-eval-display)
3725 (defun edebug-eval-display (eval-result-list)
3726 "Display expressions and evaluations in EVAL-RESULT-LIST.
3727 It modifies the context by popping up the eval display."
3728 (when eval-result-list
3729 (edebug-create-eval-buffer)
3730 (edebug-eval-display-list eval-result-list)))
3732 (defun edebug-eval-redisplay ()
3733 "Redisplay eval list in outside environment.
3734 May only be called from within `edebug--recursive-edit'."
3735 (edebug-create-eval-buffer)
3736 (edebug-outside-excursion
3737 (edebug-eval-display-list (edebug-eval-result-list))
3740 (defun edebug-visit-eval-list ()
3741 "Switch to the evaluation list buffer \"*edebug*\"."
3742 (interactive)
3743 (edebug-eval-redisplay)
3744 (edebug-pop-to-buffer edebug-eval-buffer))
3747 (defun edebug-update-eval-list ()
3748 "Replace the evaluation list with the sexps now in the eval buffer."
3749 (interactive)
3750 (let ((starting-point (point))
3751 new-list)
3752 (goto-char (point-min))
3753 ;; get the first expression
3754 (edebug-skip-whitespace)
3755 (if (not (eobp))
3756 (progn
3757 (forward-sexp 1)
3758 (push (edebug-last-sexp) new-list)))
3760 (while (re-search-forward "^;" nil t)
3761 (forward-line 1)
3762 (skip-chars-forward " \t\n\r")
3763 (if (and (/= ?\; (following-char))
3764 (not (eobp)))
3765 (progn
3766 (forward-sexp 1)
3767 (push (edebug-last-sexp) new-list))))
3769 (setq edebug-eval-list (nreverse new-list))
3770 (edebug-eval-redisplay)
3771 (goto-char starting-point)))
3774 (defun edebug-delete-eval-item ()
3775 "Delete the item under point and redisplay."
3776 ;; could add arg to do repeatedly
3777 (interactive)
3778 (if (re-search-backward "^;" nil 'nofail)
3779 (forward-line 1))
3780 (delete-region
3781 (point) (progn (re-search-forward "^;" nil 'nofail)
3782 (beginning-of-line)
3783 (point)))
3784 (edebug-update-eval-list))
3788 (defvar edebug-eval-mode-map
3789 (let ((map (make-sparse-keymap)))
3790 (set-keymap-parent map lisp-interaction-mode-map)
3791 (define-key map "\C-c\C-w" 'edebug-where)
3792 (define-key map "\C-c\C-d" 'edebug-delete-eval-item)
3793 (define-key map "\C-c\C-u" 'edebug-update-eval-list)
3794 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3795 (define-key map "\C-j" 'edebug-eval-print-last-sexp)
3796 map)
3797 "Keymap for Edebug Eval mode. Superset of Lisp Interaction mode.")
3799 (put 'edebug-eval-mode 'mode-class 'special)
3801 (define-derived-mode edebug-eval-mode lisp-interaction-mode "Edebug Eval"
3802 "Mode for evaluation list buffer while in Edebug.
3804 In addition to all Interactive Emacs Lisp commands there are local and
3805 global key bindings to several Edebug specific commands. E.g.
3806 `edebug-step-mode' is bound to \\[edebug-step-mode] in the Edebug
3807 buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3809 Eval list buffer commands:
3810 \\{edebug-eval-mode-map}
3812 Global commands prefixed by `global-edebug-prefix':
3813 \\{global-edebug-map}")
3815 ;;; Interface with standard debugger.
3817 ;; (setq debugger 'edebug) ; to use the edebug debugger
3818 ;; (setq debugger 'debug) ; use the standard debugger
3820 ;; Note that debug and its utilities must be byte-compiled to work,
3821 ;; since they depend on the backtrace looking a certain way. But
3822 ;; edebug is not dependent on this, yet.
3824 (defun edebug (&optional arg-mode &rest args)
3825 "Replacement for `debug'.
3826 If we are running an edebugged function, show where we last were.
3827 Otherwise call `debug' normally."
3828 ;;(message "entered: %s depth: %s edebug-recursion-depth: %s"
3829 ;; edebug-entered (recursion-depth) edebug-recursion-depth) (sit-for 1)
3830 (if (and edebug-entered ; anything active?
3831 (eq (recursion-depth) edebug-recursion-depth))
3832 (let (;; Where were we before the error occurred?
3833 (offset-index (car edebug-offset-indices))
3834 (value (car args))
3835 ;; Bind variables required by edebug--display.
3836 edebug-breakpoints
3837 edebug-break-data
3838 edebug-break-condition
3839 edebug-global-break
3840 (edebug-break (null arg-mode)) ;; If called explicitly.
3842 (edebug--display value offset-index arg-mode)
3843 (if (eq arg-mode 'error)
3845 value))
3847 ;; Otherwise call debug normally.
3848 ;; Still need to remove extraneous edebug calls from stack.
3849 (apply #'debug arg-mode args)
3853 (defun edebug-backtrace ()
3854 "Display a non-working backtrace. Better than nothing..."
3855 (interactive)
3856 (if (or (not edebug-backtrace-buffer)
3857 (null (buffer-name edebug-backtrace-buffer)))
3858 (setq edebug-backtrace-buffer
3859 (generate-new-buffer "*Backtrace*"))
3860 ;; Else, could just display edebug-backtrace-buffer.
3862 (with-output-to-temp-buffer (buffer-name edebug-backtrace-buffer)
3863 (setq edebug-backtrace-buffer standard-output)
3864 (let ((print-escape-newlines t)
3865 (print-length 50) ; FIXME cf edebug-safe-prin1-to-string
3866 last-ok-point)
3867 (backtrace)
3869 ;; Clean up the backtrace.
3870 ;; Not quite right for current edebug scheme.
3871 (set-buffer edebug-backtrace-buffer)
3872 (setq truncate-lines t)
3873 (goto-char (point-min))
3874 (setq last-ok-point (point))
3875 (if t (progn
3877 ;; Delete interspersed edebug internals.
3878 (while (re-search-forward "^ (?edebug" nil t)
3879 (beginning-of-line)
3880 (cond
3881 ((looking-at "^ (edebug-after")
3882 ;; Previous lines may contain code, so just delete this line.
3883 (setq last-ok-point (point))
3884 (forward-line 1)
3885 (delete-region last-ok-point (point)))
3887 ((looking-at (if debugger-stack-frame-as-list
3888 "^ (edebug"
3889 "^ edebug"))
3890 (forward-line 1)
3891 (delete-region last-ok-point (point))
3893 )))))
3896 ;;; Trace display
3898 (defun edebug-trace-display (buf-name fmt &rest args)
3899 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
3900 The buffer is created if it does not exist.
3901 You must include newlines in FMT to break lines, but one newline is appended."
3902 ;; e.g.
3903 ;; (edebug-trace-display "*trace-point*"
3904 ;; "saving: point = %s window-start = %s"
3905 ;; (point) (window-start))
3906 (let* ((oldbuf (current-buffer))
3907 (selected-window (selected-window))
3908 (buffer (get-buffer-create buf-name))
3909 buf-window)
3910 ;; (message "before pop-to-buffer") (sit-for 1)
3911 (edebug-pop-to-buffer buffer)
3912 (setq truncate-lines t)
3913 (setq buf-window (selected-window))
3914 (goto-char (point-max))
3915 (insert (apply #'edebug-format fmt args) "\n")
3916 ;; Make it visible.
3917 (vertical-motion (- 1 (window-height)))
3918 (set-window-start buf-window (point))
3919 (goto-char (point-max))
3920 ;; (set-window-point buf-window (point))
3921 ;; (sit-for 0)
3922 (bury-buffer buffer)
3923 (select-window selected-window)
3924 (set-buffer oldbuf))
3925 buf-name)
3928 (defun edebug-trace (fmt &rest args)
3929 "Convenience call to `edebug-trace-display' using `edebug-trace-buffer'."
3930 (apply #'edebug-trace-display edebug-trace-buffer fmt args))
3933 ;;; Frequency count and coverage
3935 ;; FIXME should this use overlays instead?
3936 ;; Definitely, IMO. The current business with undo in
3937 ;; edebug-temp-display-freq-count is horrid.
3938 (defun edebug-display-freq-count ()
3939 "Display the frequency count data for each line of the current definition.
3940 The frequency counts are inserted as comment lines after each line,
3941 and you can undo all insertions with one `undo' command.
3943 The counts are inserted starting under the `(' before an expression
3944 or the `)' after an expression, or on the last char of a symbol.
3945 The counts are only displayed when they differ from previous counts on
3946 the same line.
3948 If coverage is being tested, whenever all known results of an expression
3949 are `eq', the char `=' will be appended after the count
3950 for that expression. Note that this is always the case for an
3951 expression only evaluated once.
3953 To clear the frequency count and coverage data for a definition,
3954 reinstrument it."
3955 (interactive)
3956 (let* ((function (edebug-form-data-symbol))
3957 (counts (get function 'edebug-freq-count))
3958 (coverages (get function 'edebug-coverage))
3959 (data (get function 'edebug))
3960 (def-mark (car data)) ; mark at def start
3961 (edebug-points (nth 2 data))
3962 (i (1- (length edebug-points)))
3963 (last-index)
3964 (first-index)
3965 (start-of-line)
3966 (start-of-count-line)
3967 (last-count)
3969 (save-excursion
3970 ;; Traverse in reverse order so offsets are correct.
3971 (while (<= 0 i)
3972 ;; Start at last expression in line.
3973 (goto-char (+ def-mark (aref edebug-points i)))
3974 (beginning-of-line)
3975 (setq start-of-line (- (point) def-mark)
3976 last-index i)
3978 ;; Find all indexes on same line.
3979 (while (and (<= 0 (setq i (1- i)))
3980 (<= start-of-line (aref edebug-points i))))
3981 ;; Insert all the indices for this line.
3982 (forward-line 1)
3983 (setq start-of-count-line (point)
3984 first-index i ; Really, last index for line above this one.
3985 last-count -1) ; Cause first count to always appear.
3986 (insert ";#")
3987 ;; i == first-index still
3988 (while (<= (setq i (1+ i)) last-index)
3989 (let ((count (aref counts i))
3990 (coverage (aref coverages i))
3991 (col (save-excursion
3992 (goto-char (+ (aref edebug-points i) def-mark))
3993 (- (current-column)
3994 (if (= ?\( (following-char)) 0 1)))))
3995 (insert (make-string
3996 (max 0 (- col (- (point) start-of-count-line))) ?\s)
3997 (if (and (< 0 count)
3998 (not (memq coverage
3999 '(unknown ok-coverage))))
4000 "=" "")
4001 (if (= count last-count) "" (int-to-string count))
4002 " ")
4003 (setq last-count count)))
4004 (insert "\n")
4005 (setq i first-index)))))
4007 ;; FIXME this does not work very well. Eg if you press an arrow key,
4008 ;; or make a mouse-click, it fails with "Non-character input-event".
4009 (defun edebug-temp-display-freq-count ()
4010 "Temporarily display the frequency count data for the current definition.
4011 It is removed when you hit any char."
4012 ;; This seems not to work with Emacs 18.59. It undoes too far.
4013 (interactive)
4014 (let ((inhibit-read-only t))
4015 (undo-boundary)
4016 (edebug-display-freq-count)
4017 (setq unread-command-events
4018 (append unread-command-events (list (read-event))))
4019 ;; Yuck! This doesn't seem to work at all for me.
4020 (undo)))
4023 ;;; Menus
4025 (defun edebug-toggle (variable)
4026 (set variable (not (symbol-value variable)))
4027 (message "%s: %s" variable (symbol-value variable)))
4029 ;; We have to require easymenu (even for Emacs 18) just so
4030 ;; the easy-menu-define macro call is compiled correctly.
4031 (require 'easymenu)
4033 (defconst edebug-mode-menus
4034 '("Edebug"
4035 ["Stop" edebug-stop t]
4036 ["Step" edebug-step-mode t]
4037 ["Next" edebug-next-mode t]
4038 ["Trace" edebug-trace-mode t]
4039 ["Trace Fast" edebug-Trace-fast-mode t]
4040 ["Continue" edebug-continue-mode t]
4041 ["Continue Fast" edebug-Continue-fast-mode t]
4042 ["Go" edebug-go-mode t]
4043 ["Go Nonstop" edebug-Go-nonstop-mode t]
4044 "----"
4045 ["Help" edebug-help t]
4046 ["Abort" abort-recursive-edit t]
4047 ["Quit to Top Level" top-level t]
4048 ["Quit Nonstop" edebug-top-level-nonstop t]
4049 "----"
4050 ("Jumps"
4051 ["Forward Sexp" edebug-forward-sexp t]
4052 ["Step In" edebug-step-in t]
4053 ["Step Out" edebug-step-out t]
4054 ["Goto Here" edebug-goto-here t])
4056 ("Breaks"
4057 ["Set Breakpoint" edebug-set-breakpoint t]
4058 ["Unset Breakpoint" edebug-unset-breakpoint t]
4059 ["Set Conditional Breakpoint" edebug-set-conditional-breakpoint t]
4060 ["Set Global Break Condition" edebug-set-global-break-condition t]
4061 ["Show Next Breakpoint" edebug-next-breakpoint t])
4063 ("Views"
4064 ["Where am I?" edebug-where t]
4065 ["Bounce to Current Point" edebug-bounce-point t]
4066 ["View Outside Windows" edebug-view-outside t]
4067 ["Previous Result" edebug-previous-result t]
4068 ["Show Backtrace" edebug-backtrace t]
4069 ["Display Freq Count" edebug-display-freq-count t])
4071 ("Eval"
4072 ["Expression" edebug-eval-expression t]
4073 ["Last Sexp" edebug-eval-last-sexp t]
4074 ["Visit Eval List" edebug-visit-eval-list t])
4076 ("Options"
4077 ["Edebug All Defs" edebug-all-defs
4078 :style toggle :selected edebug-all-defs]
4079 ["Edebug All Forms" edebug-all-forms
4080 :style toggle :selected edebug-all-forms]
4081 "----"
4082 ["Tracing" (edebug-toggle 'edebug-trace)
4083 :style toggle :selected edebug-trace]
4084 ["Test Coverage" (edebug-toggle 'edebug-test-coverage)
4085 :style toggle :selected edebug-test-coverage]
4086 ["Save Windows" edebug-toggle-save-windows
4087 :style toggle :selected edebug-save-windows]
4088 ["Save Point"
4089 (edebug-toggle 'edebug-save-displayed-buffer-points)
4090 :style toggle :selected edebug-save-displayed-buffer-points]
4092 "Menus for Edebug.")
4095 ;;; Emacs version specific code
4097 (defalias 'edebug-window-live-p 'window-live-p)
4099 (defun edebug-mark ()
4100 (mark t))
4102 (defun edebug-set-conditional-breakpoint (arg condition)
4103 "Set a conditional breakpoint at nearest sexp.
4104 The condition is evaluated in the outside context.
4105 With prefix argument, make it a temporary breakpoint."
4106 ;; (interactive "P\nxCondition: ")
4107 (interactive
4108 (list
4109 current-prefix-arg
4110 ;; Read condition as follows; getting previous condition is cumbersome:
4111 (let ((edebug-stop-point (edebug-find-stop-point)))
4112 (if edebug-stop-point
4113 (let* ((edebug-def-name (car edebug-stop-point))
4114 (index (cdr edebug-stop-point))
4115 (edebug-data (get edebug-def-name 'edebug))
4116 (edebug-breakpoints (car (cdr edebug-data)))
4117 (edebug-break-data (assq index edebug-breakpoints))
4118 (edebug-break-condition (car (cdr edebug-break-data)))
4119 (initial (and edebug-break-condition
4120 (format "%s" edebug-break-condition))))
4121 (read-from-minibuffer
4122 "Condition: " initial read-expression-map t
4123 (if (equal (car read-expression-history) initial)
4124 '(read-expression-history . 1)
4125 'read-expression-history)))))))
4126 (edebug-modify-breakpoint t condition arg))
4128 (easy-menu-define edebug-menu edebug-mode-map "Edebug menus" edebug-mode-menus)
4130 ;;; Autoloading of Edebug accessories
4132 ;; edebug-cl-read and cl-read are available from liberte@cs.uiuc.edu
4133 (defun edebug--require-cl-read ()
4134 (require 'edebug-cl-read))
4136 (if (featurep 'cl-read)
4137 (add-hook 'edebug-setup-hook #'edebug--require-cl-read)
4138 ;; The following causes edebug-cl-read to be loaded when you load cl-read.el.
4139 (add-hook 'cl-read-load-hooks #'edebug--require-cl-read))
4142 ;;; Finalize Loading
4144 ;; When edebugging a function, some of the sub-expressions are
4145 ;; wrapped in (edebug-enter (lambda () ..)), so we need to teach
4146 ;; called-interactively-p that calls within the inner lambda should refer to
4147 ;; the outside function.
4148 (add-hook 'called-interactively-p-functions
4149 #'edebug--called-interactively-skip)
4150 (defun edebug--called-interactively-skip (i frame1 frame2)
4151 (when (and (eq (car-safe (nth 1 frame1)) 'lambda)
4152 (eq (nth 1 (nth 1 frame1)) '())
4153 (eq (nth 1 frame2) 'edebug-enter))
4154 ;; `edebug-enter' calls itself on its first invocation.
4155 (if (eq (nth 1 (backtrace-frame i 'called-interactively-p))
4156 'edebug-enter)
4157 2 1)))
4159 ;; Finally, hook edebug into the rest of Emacs.
4160 ;; There are probably some other things that could go here.
4162 ;; Install edebug read and eval functions.
4163 (edebug-install-read-eval-functions)
4165 (defun edebug-unload-function ()
4166 "Unload the Edebug source level debugger."
4167 (when edebug-active
4168 (setq edebug-active nil)
4169 (unwind-protect
4170 (abort-recursive-edit)
4171 ;; We still want to run unload-feature to completion
4172 (run-with-idle-timer 0 nil #'(lambda () (unload-feature 'edebug)))))
4173 (remove-hook 'called-interactively-p-functions
4174 'edebug--called-interactively-skip)
4175 (remove-hook 'cl-read-load-hooks 'edebug--require-cl-read)
4176 (edebug-uninstall-read-eval-functions)
4177 ;; Continue standard unloading.
4178 nil)
4180 (provide 'edebug)
4181 ;;; edebug.el ends here