Port thread.c to OpenBSD ARM
[emacs.git] / lisp / emacs-lisp / edebug.el
blobd00b14e803e7f9b34e02184b9177621c9a70b30c
1 ;;; edebug.el --- a source-level debugger for Emacs Lisp -*- lexical-binding: t -*-
3 ;; Copyright (C) 1988-1995, 1997, 1999-2017 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 a edebuggable form, cause 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)
955 (defun edebug-new-cursor (expressions offsets)
956 ;; Return a new cursor for EXPRESSIONS with OFFSETS.
957 (if (vectorp expressions)
958 (setq expressions (append expressions nil)))
959 (cons expressions offsets))
961 (defsubst edebug-set-cursor (cursor expressions offsets)
962 ;; Set the CURSOR's EXPRESSIONS and OFFSETS to the given.
963 ;; Return the cursor.
964 (setcar cursor expressions)
965 (setcdr cursor offsets)
966 cursor)
968 (defun edebug-copy-cursor (cursor)
969 ;; Copy the cursor using the same object and offsets.
970 (cons (car cursor) (cdr cursor)))
972 (defsubst edebug-cursor-expressions (cursor)
973 (car cursor))
974 (defsubst edebug-cursor-offsets (cursor)
975 (cdr cursor))
977 (defsubst edebug-empty-cursor (cursor)
978 ;; Return non-nil if CURSOR is empty - meaning no more elements.
979 (null (car cursor)))
981 (defsubst edebug-top-element (cursor)
982 ;; Return the top element at the cursor.
983 ;; Assumes not empty.
984 (car (car cursor)))
986 (defun edebug-top-element-required (cursor &rest error)
987 ;; Check if a dotted form is required.
988 (if edebug-dotted-spec (edebug-no-match cursor "Dot expected."))
989 ;; Check if there is at least one more argument.
990 (if (edebug-empty-cursor cursor) (apply #'edebug-no-match cursor error))
991 ;; Return that top element.
992 (edebug-top-element cursor))
994 (defsubst edebug-top-offset (cursor)
995 ;; Return the top offset pair corresponding to the top element.
996 (car (cdr cursor)))
998 (defun edebug-move-cursor (cursor)
999 ;; Advance and return the cursor to the next element and offset.
1000 ;; throw no-match if empty before moving.
1001 ;; This is a violation of the cursor encapsulation, but
1002 ;; there is plenty of that going on while matching.
1003 ;; The following test should always fail.
1004 (if (edebug-empty-cursor cursor)
1005 (edebug-no-match cursor "Not enough arguments."))
1006 (setcar cursor (cdr (car cursor)))
1007 (setcdr cursor (cdr (cdr cursor)))
1008 cursor)
1011 (defun edebug-before-offset (cursor)
1012 ;; Return the before offset of the cursor.
1013 ;; If there is nothing left in the offsets,
1014 ;; return one less than the offset itself,
1015 ;; which is the after offset for a list.
1016 (let ((offset (edebug-cursor-offsets cursor)))
1017 (if (consp offset)
1018 (car (car offset))
1019 (1- offset))))
1021 (defun edebug-after-offset (cursor)
1022 ;; Return the after offset of the cursor object.
1023 (let ((offset (edebug-top-offset cursor)))
1024 (while (consp offset)
1025 (setq offset (cdr offset)))
1026 offset))
1028 ;;; The Parser
1030 ;; The top level function for parsing forms is
1031 ;; edebug-read-and-maybe-wrap-form; it calls all the rest. It checks the
1032 ;; syntax a bit and leaves point at any error it finds, but otherwise
1033 ;; should appear to work like eval-defun.
1035 ;; The basic plan is to surround each expression with a call to
1036 ;; the edebug debugger together with indexes into a table of positions of
1037 ;; all expressions. Thus an expression "exp" becomes:
1039 ;; (edebug-after (edebug-before 1) 2 exp)
1041 ;; When this is evaluated, first point is moved to the beginning of
1042 ;; exp at offset 1 of the current function. The expression is
1043 ;; evaluated, which may cause more edebug calls, and then point is
1044 ;; moved to offset 2 after the end of exp.
1046 ;; The highest level expressions of the function are wrapped in a call to
1047 ;; edebug-enter, which supplies the function name and the actual
1048 ;; arguments to the function. See functions edebug-enter, edebug-before,
1049 ;; and edebug-after for more details.
1051 ;; Dynamically bound vars, left unbound, but globally declared.
1052 ;; This is to quiet the byte compiler.
1054 ;; Window data of the highest definition being wrapped.
1055 ;; This data is shared by all embedded definitions.
1056 (defvar edebug-top-window-data)
1058 (defvar edebug-&optional)
1059 (defvar edebug-&rest)
1060 (defvar edebug-gate nil) ;; whether no-match forces an error.
1062 (defvar edebug-def-name nil) ; name of definition, used by interactive-form
1063 (defvar edebug-old-def-name nil) ; previous name of containing definition.
1065 (defvar edebug-error-point nil)
1066 (defvar edebug-best-error nil)
1069 (defun edebug-read-and-maybe-wrap-form ()
1070 ;; Read a form and wrap it with edebug calls, if the conditions are right.
1071 ;; Here we just catch any no-match not caught below and signal an error.
1073 ;; Run the setup hook.
1074 ;; If it gets an error, make it nil.
1075 (let ((temp-hook edebug-setup-hook))
1076 (setq edebug-setup-hook nil)
1077 (if (functionp temp-hook) (funcall temp-hook)
1078 (mapc #'funcall temp-hook)))
1080 (let (result
1081 edebug-top-window-data
1082 edebug-def-name;; make sure it is locally nil
1083 ;; I don't like these here!!
1084 edebug-&optional
1085 edebug-&rest
1086 edebug-gate
1087 edebug-best-error
1088 edebug-error-point
1089 ;; Do this once here instead of several times.
1090 (max-lisp-eval-depth (+ 800 max-lisp-eval-depth))
1091 (max-specpdl-size (+ 2000 max-specpdl-size)))
1092 (let ((no-match
1093 (catch 'no-match
1094 (setq result (edebug-read-and-maybe-wrap-form1))
1095 nil)))
1096 (if no-match
1097 (apply #'edebug-syntax-error no-match)))
1098 result))
1101 (defun edebug-read-and-maybe-wrap-form1 ()
1102 (let (spec
1103 def-kind
1104 defining-form-p
1105 def-name
1106 ;; These offset things don't belong here, but to support recursive
1107 ;; calls to edebug-read, they need to be here.
1108 edebug-offsets
1109 edebug-offsets-stack
1110 edebug-current-offset ; reset to nil
1111 edebug-read-objects
1113 (save-excursion
1114 (if (and (eq 'lparen (edebug-next-token-class))
1115 (eq 'symbol (progn (forward-char 1) (edebug-next-token-class))))
1116 ;; Find out if this is a defining form from first symbol
1117 (setq def-kind (read (current-buffer))
1118 spec (and (symbolp def-kind) (get-edebug-spec def-kind))
1119 defining-form-p (and (listp spec)
1120 (eq '&define (car spec)))
1121 ;; This is incorrect in general!! But OK most of the time.
1122 def-name (if (and defining-form-p
1123 (eq 'name (car (cdr spec)))
1124 (eq 'symbol (edebug-next-token-class)))
1125 (read (current-buffer))))))
1126 ;;;(message "all defs: %s all forms: %s" edebug-all-defs edebug-all-forms)
1127 (cond
1128 (defining-form-p
1129 (if (or edebug-all-defs edebug-all-forms)
1130 ;; If it is a defining form and we are edebugging defs,
1131 ;; then let edebug-list-form start it.
1132 (let ((cursor (edebug-new-cursor
1133 (list (edebug-read-storing-offsets (current-buffer)))
1134 (list edebug-offsets))))
1135 (car
1136 (edebug-make-form-wrapper
1137 cursor
1138 (edebug-before-offset cursor)
1139 (1- (edebug-after-offset cursor))
1140 (list (cons (symbol-name def-kind) (cdr spec))))))
1142 ;; Not edebugging this form, so reset the symbol's edebug
1143 ;; property to be just a marker at the definition's source code.
1144 ;; This only works for defs with simple names.
1145 (put def-name 'edebug (point-marker))
1146 ;; Also nil out dependent defs.
1147 '(mapcar (function
1148 (lambda (def)
1149 (put def-name 'edebug nil)))
1150 (get def-name 'edebug-dependents))
1151 (edebug-read-sexp)))
1153 ;; If all forms are being edebugged, explicitly wrap it.
1154 (edebug-all-forms
1155 (let ((cursor (edebug-new-cursor
1156 (list (edebug-read-storing-offsets (current-buffer)))
1157 (list edebug-offsets))))
1158 (edebug-make-form-wrapper
1159 cursor
1160 (edebug-before-offset cursor)
1161 (edebug-after-offset cursor)
1162 nil)))
1164 ;; Not a defining form, and not edebugging.
1165 (t (edebug-read-sexp)))
1169 (defvar edebug-def-args) ; args of defining form.
1170 (defvar edebug-def-interactive) ; is it an emacs interactive function?
1171 (defvar edebug-inside-func) ;; whether code is inside function context.
1172 ;; Currently def-form sets this to nil; def-body sets it to t.
1174 (defun edebug-interactive-p-name ()
1175 ;; Return a unique symbol for the variable used to store the
1176 ;; status of interactive-p for this function.
1177 (intern (format "edebug-%s-interactive-p" edebug-def-name)))
1180 (defun edebug-wrap-def-body (forms)
1181 "Wrap the FORMS of a definition body."
1182 (if edebug-def-interactive
1183 `(let ((,(edebug-interactive-p-name)
1184 (interactive-p)))
1185 ,(edebug-make-enter-wrapper forms))
1186 (edebug-make-enter-wrapper forms)))
1189 (defun edebug-make-enter-wrapper (forms)
1190 ;; Generate the enter wrapper for some forms of a definition.
1191 ;; This is not to be used for the body of other forms, e.g. `while',
1192 ;; since it wraps the list of forms with a call to `edebug-enter'.
1193 ;; Uses the dynamically bound vars edebug-def-name and edebug-def-args.
1194 ;; Do this after parsing since that may find a name.
1195 (setq edebug-def-name
1196 (or edebug-def-name edebug-old-def-name (gensym "edebug-anon")))
1197 `(edebug-enter
1198 (quote ,edebug-def-name)
1199 ,(if edebug-inside-func
1200 `(list
1201 ;; Doesn't work with more than one def-body!!
1202 ;; But the list will just be reversed.
1203 ,@(nreverse edebug-def-args))
1204 'nil)
1205 (function (lambda () ,@forms))
1209 (defvar edebug-form-begin-marker) ; the mark for def being instrumented
1211 (defvar edebug-offset-index) ; the next available offset index.
1212 (defvar edebug-offset-list) ; the list of offset positions.
1214 (defun edebug-inc-offset (offset)
1215 ;; Modifies edebug-offset-index and edebug-offset-list
1216 ;; accesses edebug-func-marc and buffer point.
1217 (prog1
1218 edebug-offset-index
1219 (setq edebug-offset-list (cons (- offset edebug-form-begin-marker)
1220 edebug-offset-list)
1221 edebug-offset-index (1+ edebug-offset-index))))
1224 (defun edebug-make-before-and-after-form (before-index form after-index)
1225 ;; Return the edebug form for the current function at offset BEFORE-INDEX
1226 ;; given FORM. Looks like:
1227 ;; (edebug-after (edebug-before BEFORE-INDEX) AFTER-INDEX FORM)
1228 ;; Also increment the offset index for subsequent use.
1229 `(edebug-after (edebug-before ,before-index) ,after-index ,form))
1231 (defun edebug-make-after-form (form after-index)
1232 ;; Like edebug-make-before-and-after-form, but only after.
1233 `(edebug-after 0 ,after-index ,form))
1236 (defun edebug-unwrap (sexp)
1237 "Return the unwrapped SEXP or return it as is if it is not wrapped.
1238 The SEXP might be the result of wrapping a body, which is a list of
1239 expressions; a `progn' form will be returned enclosing these forms."
1240 (if (consp sexp)
1241 (cond
1242 ((eq 'edebug-after (car sexp))
1243 (nth 3 sexp))
1244 ((eq 'edebug-enter (car sexp))
1245 (macroexp-progn (nthcdr 2 (nth 1 (nth 3 sexp)))))
1246 (t sexp);; otherwise it is not wrapped, so just return it.
1248 sexp))
1250 (defun edebug-unwrap* (sexp)
1251 "Return the SEXP recursively unwrapped."
1252 (let ((new-sexp (edebug-unwrap sexp)))
1253 (while (not (eq sexp new-sexp))
1254 (setq sexp new-sexp
1255 new-sexp (edebug-unwrap sexp)))
1256 (if (consp new-sexp)
1257 (mapcar #'edebug-unwrap* new-sexp)
1258 new-sexp)))
1261 (defun edebug-defining-form (cursor form-begin form-end speclist)
1262 ;; Process the defining form, starting outside the form.
1263 ;; The speclist is a generated list spec that looks like:
1264 ;; (("def-symbol" defining-form-spec-sans-&define))
1265 ;; Skip the first offset.
1266 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1267 (cdr (edebug-cursor-offsets cursor)))
1268 (edebug-make-form-wrapper
1269 cursor
1270 form-begin (1- form-end)
1271 speclist))
1273 (defun edebug-make-form-wrapper (cursor form-begin form-end
1274 &optional speclist)
1275 ;; Wrap a form, usually a defining form, but any evaluated one.
1276 ;; If speclist is non-nil, this is being called by edebug-defining-form.
1277 ;; Otherwise it is being called from edebug-read-and-maybe-wrap-form1.
1278 ;; This is a hack, but I haven't figured out a simpler way yet.
1279 (let* ((form-data-entry (edebug-get-form-data-entry form-begin form-end))
1280 ;; Set this marker before parsing.
1281 (edebug-form-begin-marker
1282 (if form-data-entry
1283 (edebug--form-data-begin form-data-entry)
1284 ;; Buffer must be current-buffer for this to work:
1285 (set-marker (make-marker) form-begin))))
1287 (let (edebug-offset-list
1288 (edebug-offset-index 0)
1289 result
1290 ;; For definitions.
1291 ;; (edebug-containing-def-name edebug-def-name)
1292 ;; Get name from form-data, if any.
1293 (edebug-old-def-name (edebug--form-data-name form-data-entry))
1294 edebug-def-name
1295 edebug-def-args
1296 edebug-def-interactive
1297 edebug-inside-func;; whether wrapped code executes inside a function.
1300 (setq result
1301 (if speclist
1302 (edebug-match cursor speclist)
1304 ;; else wrap as an enter-form.
1305 (edebug-make-enter-wrapper (list (edebug-form cursor)))))
1307 ;; Set the name here if it was not set by edebug-make-enter-wrapper.
1308 (setq edebug-def-name
1309 (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
1311 ;; Add this def as a dependent of containing def. Buggy.
1312 '(if (and edebug-containing-def-name
1313 (not (get edebug-containing-def-name 'edebug-dependents)))
1314 (put edebug-containing-def-name 'edebug-dependents
1315 (cons edebug-def-name
1316 (get edebug-containing-def-name
1317 'edebug-dependents))))
1319 ;; Create a form-data-entry or modify existing entry's markers.
1320 ;; In the latter case, pointers to the entry remain eq.
1321 (if (not form-data-entry)
1322 (setq form-data-entry
1323 (edebug--make-form-data-entry
1324 edebug-def-name
1325 edebug-form-begin-marker
1326 ;; Buffer must be current-buffer.
1327 (set-marker (make-marker) form-end)
1329 (edebug-set-form-data-entry
1330 form-data-entry edebug-def-name ;; in case name is changed
1331 form-begin form-end))
1333 ;; (message "defining: %s" edebug-def-name) (sit-for 2)
1334 (edebug-make-top-form-data-entry form-data-entry)
1335 (message "Edebug: %s" edebug-def-name)
1336 ;;(debug edebug-def-name)
1338 ;; Destructively reverse edebug-offset-list and make vector from it.
1339 (setq edebug-offset-list (vconcat (nreverse edebug-offset-list)))
1341 ;; Side effects on the property list of edebug-def-name.
1342 (edebug-clear-frequency-count edebug-def-name)
1343 (edebug-clear-coverage edebug-def-name)
1345 ;; Set up the initial window data.
1346 (if (not edebug-top-window-data) ;; if not already set, do it now.
1347 (let ((window ;; Find the best window for this buffer.
1348 (or (get-buffer-window (current-buffer))
1349 (selected-window))))
1350 (setq edebug-top-window-data
1351 (cons window (window-start window)))))
1353 ;; Store the edebug data in symbol's property list.
1354 (put edebug-def-name 'edebug
1355 ;; A struct or vector would be better here!!
1356 (list edebug-form-begin-marker
1357 nil ; clear breakpoints
1358 edebug-offset-list
1359 edebug-top-window-data
1361 result
1365 (defun edebug-clear-frequency-count (name)
1366 ;; Create initial frequency count vector.
1367 ;; For each stop point, the counter is incremented each time it is visited.
1368 (put name 'edebug-freq-count
1369 (make-vector (length edebug-offset-list) 0)))
1372 (defun edebug-clear-coverage (name)
1373 ;; Create initial coverage vector.
1374 ;; Only need one per expression, but it is simpler to use stop points.
1375 (put name 'edebug-coverage
1376 (make-vector (length edebug-offset-list) 'unknown)))
1379 (defun edebug-form (cursor)
1380 ;; Return the instrumented form for the following form.
1381 ;; Add the point offsets to the edebug-offset-list for the form.
1382 (let* ((form (edebug-top-element-required cursor "Expected form"))
1383 (offset (edebug-top-offset cursor)))
1384 (prog1
1385 (cond
1386 ((consp form)
1387 ;; The first offset for a list form is for the list form itself.
1388 (if (eq 'quote (car form))
1389 form
1390 (let* ((head (car form))
1391 (spec (and (symbolp head) (get-edebug-spec head)))
1392 (new-cursor (edebug-new-cursor form offset)))
1393 ;; Find out if this is a defining form from first symbol.
1394 ;; An indirect spec would not work here, yet.
1395 (if (and (consp spec) (eq '&define (car spec)))
1396 (edebug-defining-form
1397 new-cursor
1398 (car offset);; before the form
1399 (edebug-after-offset cursor)
1400 (cons (symbol-name head) (cdr spec)))
1401 ;; Wrap a regular form.
1402 (edebug-make-before-and-after-form
1403 (edebug-inc-offset (car offset))
1404 (edebug-list-form new-cursor)
1405 ;; After processing the list form, the new-cursor is left
1406 ;; with the offset after the form.
1407 (edebug-inc-offset (edebug-cursor-offsets new-cursor))))
1410 ((symbolp form)
1411 (cond
1412 ;; Check for constant symbols that don't get wrapped.
1413 ((or (memq form '(t nil))
1414 (keywordp form))
1415 form)
1417 (t ;; just a variable
1418 (edebug-make-after-form form (edebug-inc-offset (cdr offset))))))
1420 ;; Anything else is self-evaluating.
1421 (t form))
1422 (edebug-move-cursor cursor))))
1425 (defsubst edebug-forms (cursor) (edebug-match cursor '(&rest form)))
1426 (defsubst edebug-sexps (cursor) (edebug-match cursor '(&rest sexp)))
1428 (defsubst edebug-list-form-args (head cursor)
1429 ;; Process the arguments of a list form given that head of form is a symbol.
1430 ;; Helper for edebug-list-form
1431 (let ((spec (get-edebug-spec head)))
1432 (cond
1433 (spec
1434 (cond
1435 ((consp spec)
1436 ;; It is a speclist.
1437 (let (edebug-best-error
1438 edebug-error-point);; This may not be needed.
1439 (edebug-match-sublist cursor spec)))
1440 ((eq t spec) (edebug-forms cursor))
1441 ((eq 0 spec) (edebug-sexps cursor))
1442 ((symbolp spec) (funcall spec cursor));; Not used by edebug,
1443 ; but leave it in for compatibility.
1445 ;; No edebug-form-spec provided.
1446 ((macrop head)
1447 (if edebug-eval-macro-args
1448 (edebug-forms cursor)
1449 (edebug-sexps cursor)))
1450 (t ;; Otherwise it is a function call.
1451 (edebug-forms cursor)))))
1454 (defun edebug-list-form (cursor)
1455 ;; Return an instrumented form built from the list form.
1456 ;; The after offset will be left in the cursor after processing the form.
1457 (let ((head (edebug-top-element-required cursor "Expected elements"))
1458 ;; Prevent backtracking whenever instrumenting.
1459 (edebug-gate t)
1460 ;; A list form is never optional because it matches anything.
1461 (edebug-&optional nil)
1462 (edebug-&rest nil))
1463 ;; Skip the first offset.
1464 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1465 (cdr (edebug-cursor-offsets cursor)))
1466 (cond
1467 ((symbolp head)
1468 (cond
1469 ((null head) nil) ; () is valid.
1470 ((eq head 'interactive-p)
1471 ;; Special case: replace (interactive-p) with variable
1472 (setq edebug-def-interactive 'check-it)
1473 (edebug-move-cursor cursor)
1474 (edebug-interactive-p-name))
1476 (cons head (edebug-list-form-args
1477 head (edebug-move-cursor cursor))))))
1479 ((consp head)
1480 (if (eq (car head) '\,)
1481 ;; The head of a form should normally be a symbol or a lambda
1482 ;; expression but it can also be an unquote form to be filled
1483 ;; before evaluation. We evaluate the arguments anyway, on the
1484 ;; assumption that the unquote form will place a proper function
1485 ;; name (rather than a macro name).
1486 (edebug-match cursor '(("," def-form) body))
1487 ;; Process anonymous function and args.
1488 ;; This assumes no anonymous macros.
1489 (edebug-match-specs cursor '(lambda-expr body) 'edebug-match-specs)))
1491 (t (edebug-syntax-error
1492 "Head of list form must be a symbol or lambda expression")))
1495 ;;; Matching of specs.
1497 (defvar edebug-after-dotted-spec nil)
1499 (defvar edebug-matching-depth 0) ;; initial value
1502 ;;; Failure to match
1504 ;; This throws to no-match, if there are higher alternatives.
1505 ;; Otherwise it signals an error. The place of the error is found
1506 ;; with the two before- and after-offset functions.
1508 (defun edebug-no-match (cursor &rest args)
1509 ;; Throw a no-match, or signal an error immediately if gate is active.
1510 ;; Remember this point in case we need to report this error.
1511 (setq edebug-error-point (or edebug-error-point
1512 (edebug-before-offset cursor))
1513 edebug-best-error (or edebug-best-error args))
1514 (if (and edebug-gate (not edebug-&optional))
1515 (progn
1516 (if edebug-error-point
1517 (goto-char edebug-error-point))
1518 (apply #'edebug-syntax-error args))
1519 (throw 'no-match args)))
1522 (defun edebug-match (cursor specs)
1523 ;; Top level spec matching function.
1524 ;; Used also at each lower level of specs.
1525 (let (edebug-&optional
1526 edebug-&rest
1527 edebug-best-error
1528 edebug-error-point
1529 (edebug-gate edebug-gate) ;; locally bound to limit effect
1531 (edebug-match-specs cursor specs 'edebug-match-specs)))
1534 (defun edebug-match-one-spec (cursor spec)
1535 ;; Match one spec, which is not a keyword &-spec.
1536 (cond
1537 ((symbolp spec) (edebug-match-symbol cursor spec))
1538 ((vectorp spec) (edebug-match cursor (append spec nil)))
1539 ((stringp spec) (edebug-match-string cursor spec))
1540 ((listp spec) (edebug-match-list cursor spec))
1544 (defun edebug-match-specs (cursor specs remainder-handler)
1545 ;; Append results of matching the list of specs.
1546 ;; The first spec is handled and the remainder-handler handles the rest.
1547 (let ((edebug-matching-depth
1548 (if (> edebug-matching-depth edebug-max-depth)
1549 (error "Too deep - perhaps infinite loop in spec?")
1550 (1+ edebug-matching-depth))))
1551 (cond
1552 ((null specs) nil)
1554 ;; Is the spec dotted?
1555 ((atom specs)
1556 (let ((edebug-dotted-spec t));; Containing spec list was dotted.
1557 (edebug-match-specs cursor (list specs) remainder-handler)))
1559 ;; Is the form dotted?
1560 ((not (listp (edebug-cursor-expressions cursor)));; allow nil
1561 (if (not edebug-dotted-spec)
1562 (edebug-no-match cursor "Dotted spec required."))
1563 ;; Cancel dotted spec and dotted form.
1564 (let ((edebug-dotted-spec)
1565 (this-form (edebug-cursor-expressions cursor))
1566 (this-offset (edebug-cursor-offsets cursor)))
1567 ;; Wrap the form in a list, (by changing the cursor??)...
1568 (edebug-set-cursor cursor (list this-form) this-offset)
1569 ;; and process normally, then unwrap the result.
1570 (car (edebug-match-specs cursor specs remainder-handler))))
1572 (t;; Process normally.
1573 (let* ((spec (car specs))
1574 (rest)
1575 (first-char (and (symbolp spec) (aref (symbol-name spec) 0))))
1576 ;;(message "spec = %s first char = %s" spec first-char) (sit-for 1)
1577 (nconc
1578 (cond
1579 ((eq ?& first-char);; "&" symbols take all following specs.
1580 (funcall (get-edebug-spec spec) cursor (cdr specs)))
1581 ((eq ?: first-char);; ":" symbols take one following spec.
1582 (setq rest (cdr (cdr specs)))
1583 (funcall (get-edebug-spec spec) cursor (car (cdr specs))))
1584 (t;; Any other normal spec.
1585 (setq rest (cdr specs))
1586 (edebug-match-one-spec cursor spec)))
1587 (funcall remainder-handler cursor rest remainder-handler)))))))
1590 ;; Define specs for all the symbol specs with functions used to process them.
1591 ;; Perhaps we shouldn't be doing this with edebug-form-specs since the
1592 ;; user may want to define macros or functions with the same names.
1593 ;; We could use an internal obarray for these primitive specs.
1595 (dolist (pair '((&optional . edebug-match-&optional)
1596 (&rest . edebug-match-&rest)
1597 (&or . edebug-match-&or)
1598 (form . edebug-match-form)
1599 (sexp . edebug-match-sexp)
1600 (body . edebug-match-body)
1601 (&define . edebug-match-&define)
1602 (name . edebug-match-name)
1603 (:name . edebug-match-colon-name)
1604 (arg . edebug-match-arg)
1605 (def-body . edebug-match-def-body)
1606 (def-form . edebug-match-def-form)
1607 ;; Less frequently used:
1608 ;; (function . edebug-match-function)
1609 (lambda-expr . edebug-match-lambda-expr)
1610 (cl-generic-method-args . edebug-match-cl-generic-method-args)
1611 (&not . edebug-match-&not)
1612 (&key . edebug-match-&key)
1613 (place . edebug-match-place)
1614 (gate . edebug-match-gate)
1615 ;; (nil . edebug-match-nil) not this one - special case it.
1617 (put (car pair) 'edebug-form-spec (cdr pair)))
1619 (defun edebug-match-symbol (cursor symbol)
1620 ;; Match a symbol spec.
1621 (let* ((spec (get-edebug-spec symbol)))
1622 (cond
1623 (spec
1624 (if (consp spec)
1625 ;; It is an indirect spec.
1626 (edebug-match cursor spec)
1627 ;; Otherwise it should be the symbol name of a function.
1628 ;; There could be a bug here - maybe need to do edebug-match bindings.
1629 (funcall spec cursor)))
1631 ((null symbol) ;; special case this.
1632 (edebug-match-nil cursor))
1634 ((fboundp symbol) ; is it a predicate?
1635 (let ((sexp (edebug-top-element-required cursor "Expected" symbol)))
1636 ;; Special case for edebug-`.
1637 (if (and (listp sexp) (eq (car sexp) '\,))
1638 (edebug-match cursor '(("," def-form)))
1639 (if (not (funcall symbol sexp))
1640 (edebug-no-match cursor symbol "failed"))
1641 (edebug-move-cursor cursor)
1642 (list sexp))))
1643 (t (error "%s is not a form-spec or function" symbol))
1647 (defun edebug-match-sexp (cursor)
1648 (list (prog1 (edebug-top-element-required cursor "Expected sexp")
1649 (edebug-move-cursor cursor))))
1651 (defun edebug-match-form (cursor)
1652 (list (edebug-form cursor)))
1654 (defalias 'edebug-match-place 'edebug-match-form)
1655 ;; Currently identical to edebug-match-form.
1656 ;; This is for common lisp setf-style place arguments.
1658 (defsubst edebug-match-body (cursor) (edebug-forms cursor))
1660 (defun edebug-match-&optional (cursor specs)
1661 ;; Keep matching until one spec fails.
1662 (edebug-&optional-wrapper cursor specs 'edebug-&optional-wrapper))
1664 (defun edebug-&optional-wrapper (cursor specs remainder-handler)
1665 (let (result
1666 (edebug-&optional specs)
1667 (edebug-gate nil)
1668 (this-form (edebug-cursor-expressions cursor))
1669 (this-offset (edebug-cursor-offsets cursor)))
1670 (if (null (catch 'no-match
1671 (setq result
1672 (edebug-match-specs cursor specs remainder-handler))
1673 ;; Returning nil means no no-match was thrown.
1674 nil))
1675 result
1676 ;; no-match, but don't fail; just reset cursor and return nil.
1677 (edebug-set-cursor cursor this-form this-offset)
1678 nil)))
1681 (defun edebug-&rest-wrapper (cursor specs remainder-handler)
1682 (if (null specs) (setq specs edebug-&rest))
1683 ;; Reuse the &optional handler with this as the remainder handler.
1684 (edebug-&optional-wrapper cursor specs remainder-handler))
1686 (defun edebug-match-&rest (cursor specs)
1687 ;; Repeatedly use specs until failure.
1688 (let ((edebug-&rest specs) ;; remember these
1689 edebug-best-error
1690 edebug-error-point)
1691 (edebug-&rest-wrapper cursor specs 'edebug-&rest-wrapper)))
1694 (defun edebug-match-&or (cursor specs)
1695 ;; Keep matching until one spec succeeds, and return its results.
1696 ;; If none match, fail.
1697 ;; This needs to be optimized since most specs spend time here.
1698 (let ((original-specs specs)
1699 (this-form (edebug-cursor-expressions cursor))
1700 (this-offset (edebug-cursor-offsets cursor)))
1701 (catch 'matched
1702 (while specs
1703 (catch 'no-match
1704 (throw 'matched
1705 (let (edebug-gate ;; only while matching each spec
1706 edebug-best-error
1707 edebug-error-point)
1708 ;; Doesn't support e.g. &or symbolp &rest form
1709 (edebug-match-one-spec cursor (car specs)))))
1710 ;; Match failed, so reset and try again.
1711 (setq specs (cdr specs))
1712 ;; Reset the cursor for the next match.
1713 (edebug-set-cursor cursor this-form this-offset))
1714 ;; All failed.
1715 (apply #'edebug-no-match cursor "Expected one of" original-specs))
1719 (defun edebug-match-&not (cursor specs)
1720 ;; If any specs match, then fail
1721 (if (null (catch 'no-match
1722 (let ((edebug-gate nil))
1723 (save-excursion
1724 (edebug-match-&or cursor specs)))
1725 nil))
1726 ;; This means something matched, so it is a no match.
1727 (edebug-no-match cursor "Unexpected"))
1728 ;; This means nothing matched, so it is OK.
1729 nil) ;; So, return nothing
1732 (def-edebug-spec &key edebug-match-&key)
1734 (defun edebug-match-&key (cursor specs)
1735 ;; Following specs must look like (<name> <spec>) ...
1736 ;; where <name> is the name of a keyword, and spec is its spec.
1737 ;; This really doesn't save much over the expanded form and takes time.
1738 (edebug-match-&rest
1739 cursor
1740 (cons '&or
1741 (mapcar (lambda (pair)
1742 (vector (format ":%s" (car pair))
1743 (car (cdr pair))))
1744 specs))))
1747 (defun edebug-match-gate (_cursor)
1748 ;; Simply set the gate to prevent backtracking at this level.
1749 (setq edebug-gate t)
1750 nil)
1753 (defun edebug-match-list (cursor specs)
1754 ;; The spec is a list, but what kind of list, and what context?
1755 (if edebug-dotted-spec
1756 ;; After dotted spec but form did not contain dot,
1757 ;; so match list spec elements as if spliced in.
1758 (prog1
1759 (let ((edebug-dotted-spec))
1760 (edebug-match-specs cursor specs 'edebug-match-specs))
1761 ;; If it matched, really clear the dotted-spec flag.
1762 (setq edebug-dotted-spec nil))
1763 (let ((spec (car specs))
1764 (form (edebug-top-element-required cursor "Expected" specs)))
1765 (cond
1766 ((eq 'quote spec)
1767 (let ((spec (car (cdr specs))))
1768 (cond
1769 ((symbolp spec)
1770 ;; Special case: spec quotes a symbol to match.
1771 ;; Change in future. Use "..." instead.
1772 (if (not (eq spec form))
1773 (edebug-no-match cursor "Expected" spec))
1774 (edebug-move-cursor cursor)
1775 (setq edebug-gate t)
1776 form)
1778 (error "Bad spec: %s" specs)))))
1780 ((eq 'vector spec)
1781 (if (vectorp form)
1782 ;; Special case: match a vector with the specs.
1783 (let ((result (edebug-match-sublist
1784 (edebug-new-cursor
1785 form (cdr (edebug-top-offset cursor)))
1786 (cdr specs))))
1787 (edebug-move-cursor cursor)
1788 (list (apply #'vector result)))
1789 (edebug-no-match cursor "Expected" specs)))
1791 ((listp form)
1792 (prog1
1793 (list (edebug-match-sublist
1794 ;; First offset is for the list form itself.
1795 ;; Treat nil as empty list.
1796 (edebug-new-cursor form (cdr (edebug-top-offset cursor)))
1797 specs))
1798 (edebug-move-cursor cursor)))
1800 (t (edebug-no-match cursor "Expected" specs)))
1804 (defun edebug-match-sublist (cursor specs)
1805 ;; Match a sublist of specs.
1806 (let (edebug-&optional
1807 ;;edebug-best-error
1808 ;;edebug-error-point
1810 (prog1
1811 ;; match with edebug-match-specs so edebug-best-error is not bound.
1812 (edebug-match-specs cursor specs 'edebug-match-specs)
1813 (if (not (edebug-empty-cursor cursor))
1814 (if edebug-best-error
1815 (apply #'edebug-no-match cursor edebug-best-error)
1816 ;; A failed &rest or &optional spec may leave some args.
1817 (edebug-no-match cursor "Failed matching" specs)
1818 )))))
1821 (defun edebug-match-string (cursor spec)
1822 (let ((sexp (edebug-top-element-required cursor "Expected" spec)))
1823 (if (not (eq (intern spec) sexp))
1824 (edebug-no-match cursor "Expected" spec)
1825 ;; Since it matched, failure means immediate error, unless &optional.
1826 (setq edebug-gate t)
1827 (edebug-move-cursor cursor)
1828 (list sexp)
1831 (defun edebug-match-nil (cursor)
1832 ;; There must be nothing left to match a nil.
1833 (if (not (edebug-empty-cursor cursor))
1834 (edebug-no-match cursor "Unmatched argument(s)")
1835 nil))
1838 (defun edebug-match-function (_cursor)
1839 (error "Use function-form instead of function in edebug spec"))
1841 (defun edebug-match-&define (cursor specs)
1842 ;; Match a defining form.
1843 ;; Normally, &define is interpreted specially other places.
1844 ;; This should only be called inside of a spec list to match the remainder
1845 ;; of the current list. e.g. ("lambda" &define args def-body)
1846 (edebug-make-form-wrapper
1847 cursor
1848 (edebug-before-offset cursor)
1849 ;; Find the last offset in the list.
1850 (let ((offsets (edebug-cursor-offsets cursor)))
1851 (while (consp offsets) (setq offsets (cdr offsets)))
1852 offsets)
1853 specs))
1855 (defun edebug-match-lambda-expr (cursor)
1856 ;; The expression must be a function.
1857 ;; This will match any list form that begins with a symbol
1858 ;; that has an edebug-form-spec beginning with &define. In
1859 ;; practice, only lambda expressions should be used.
1860 ;; I could add a &lambda specification to avoid confusion.
1861 (let* ((sexp (edebug-top-element-required
1862 cursor "Expected lambda expression"))
1863 (offset (edebug-top-offset cursor))
1864 (head (and (consp sexp) (car sexp)))
1865 (spec (and (symbolp head) (get-edebug-spec head)))
1866 (edebug-inside-func nil))
1867 ;; Find out if this is a defining form from first symbol.
1868 (if (and (consp spec) (eq '&define (car spec)))
1869 (prog1
1870 (list
1871 (edebug-defining-form
1872 (edebug-new-cursor sexp offset)
1873 (car offset);; before the sexp
1874 (edebug-after-offset cursor)
1875 (cons (symbol-name head) (cdr spec))))
1876 (edebug-move-cursor cursor))
1877 (edebug-no-match cursor "Expected lambda expression")
1881 (defun edebug-match-name (cursor)
1882 ;; Set the edebug-def-name bound in edebug-defining-form.
1883 (let ((name (edebug-top-element-required cursor "Expected name")))
1884 ;; Maybe strings and numbers could be used.
1885 (if (not (symbolp name))
1886 (edebug-no-match cursor "Symbol expected for name of definition"))
1887 (setq edebug-def-name
1888 (if edebug-def-name
1889 ;; Construct a new name by appending to previous name.
1890 (intern (format "%s@%s" edebug-def-name name))
1891 name))
1892 (edebug-move-cursor cursor)
1893 (list name)))
1895 (defun edebug-match-colon-name (_cursor spec)
1896 ;; Set the edebug-def-name to the spec.
1897 (setq edebug-def-name
1898 (if edebug-def-name
1899 ;; Construct a new name by appending to previous name.
1900 (intern (format "%s@%s" edebug-def-name spec))
1901 spec))
1902 nil)
1904 (defun edebug-match-cl-generic-method-args (cursor)
1905 (let ((args (edebug-top-element-required cursor "Expected arguments")))
1906 (if (not (consp args))
1907 (edebug-no-match cursor "List expected"))
1908 ;; Append the arguments to edebug-def-name.
1909 (setq edebug-def-name
1910 (intern (format "%s %s" edebug-def-name args)))
1911 (edebug-move-cursor cursor)
1912 (list args)))
1914 (defun edebug-match-arg (cursor)
1915 ;; set the def-args bound in edebug-defining-form
1916 (let ((edebug-arg (edebug-top-element-required cursor "Expected arg")))
1917 (if (or (not (symbolp edebug-arg))
1918 (edebug-lambda-list-keywordp edebug-arg))
1919 (edebug-no-match cursor "Bad argument:" edebug-arg))
1920 (edebug-move-cursor cursor)
1921 (setq edebug-def-args (cons edebug-arg edebug-def-args))
1922 (list edebug-arg)))
1924 (defun edebug-match-def-form (cursor)
1925 ;; Like form but the form is wrapped in edebug-enter form.
1926 ;; The form is assumed to be executing outside of the function context.
1927 ;; This is a hack for now, since a def-form might execute inside as well.
1928 ;; Not to be used otherwise.
1929 (let ((edebug-inside-func nil))
1930 (list (edebug-make-enter-wrapper (list (edebug-form cursor))))))
1932 (defun edebug-match-def-body (cursor)
1933 ;; Like body but body is wrapped in edebug-enter form.
1934 ;; The body is assumed to be executing inside of the function context.
1935 ;; Not to be used otherwise.
1936 (let* ((edebug-inside-func t)
1937 (forms (edebug-forms cursor)))
1938 ;; If there's no form, there's nothing to wrap!
1939 ;; This happens to handle bug#20281, tho maybe a better fix would be to
1940 ;; improve the `defun' spec.
1941 (when forms
1942 (list (edebug-wrap-def-body forms)))))
1945 ;;;; Edebug Form Specs
1946 ;;; ==========================================================
1948 ;;;;* Spec for def-edebug-spec
1949 ;;; Out of date.
1951 (defun edebug-spec-p (object)
1952 "Return non-nil if OBJECT is a symbol with an edebug-form-spec property."
1953 (and (symbolp object)
1954 (get object 'edebug-form-spec)))
1956 (def-edebug-spec def-edebug-spec
1957 ;; Top level is different from lower levels.
1958 (&define :name edebug-spec name
1959 &or "nil" edebug-spec-p "t" "0" (&rest edebug-spec)))
1961 (def-edebug-spec edebug-spec-list
1962 ;; A list must have something in it, or it is nil, a symbolp
1963 ((edebug-spec . [&or nil edebug-spec])))
1965 (def-edebug-spec edebug-spec
1966 (&or
1967 (vector &rest edebug-spec) ; matches a vector
1968 ("vector" &rest edebug-spec) ; matches a vector spec
1969 ("quote" symbolp)
1970 edebug-spec-list
1971 stringp
1972 [edebug-lambda-list-keywordp &rest edebug-spec]
1973 [keywordp gate edebug-spec]
1974 edebug-spec-p ;; Including all the special ones e.g. form.
1975 symbolp;; a predicate
1979 ;;;* Emacs special forms and some functions.
1981 ;; quote expects only one argument, although it allows any number.
1982 (def-edebug-spec quote sexp)
1984 ;; The standard defining forms.
1985 (def-edebug-spec defconst defvar)
1986 (def-edebug-spec defvar (symbolp &optional form stringp))
1988 (def-edebug-spec defun
1989 (&define name lambda-list lambda-doc
1990 [&optional ("declare" &rest sexp)]
1991 [&optional ("interactive" interactive)]
1992 def-body))
1993 (def-edebug-spec defmacro
1994 ;; FIXME: Improve `declare' so we can Edebug gv-expander and
1995 ;; gv-setter declarations.
1996 (&define name lambda-list lambda-doc
1997 [&optional ("declare" &rest sexp)] def-body))
1999 (def-edebug-spec arglist lambda-list) ;; deprecated - use lambda-list.
2001 (def-edebug-spec lambda-list
2002 (([&rest arg]
2003 [&optional ["&optional" arg &rest arg]]
2004 &optional ["&rest" arg]
2007 (def-edebug-spec lambda-doc
2008 (&optional [&or stringp
2009 (&define ":documentation" def-form)]))
2011 (def-edebug-spec interactive
2012 (&optional &or stringp def-form))
2014 ;; A function-form is for an argument that may be a function or a form.
2015 ;; This specially recognizes anonymous functions quoted with quote.
2016 (def-edebug-spec function-form
2017 ;; form at the end could also handle "function",
2018 ;; but recognize it specially to avoid wrapping function forms.
2019 (&or ([&or "quote" "function"] &or symbolp lambda-expr) form))
2021 ;; function expects a symbol or a lambda or macro expression
2022 ;; A macro is allowed by Emacs.
2023 (def-edebug-spec function (&or symbolp lambda-expr))
2025 ;; A macro expression is a lambda expression with "macro" prepended.
2026 (def-edebug-spec macro (&define "lambda" lambda-list def-body))
2028 ;; (def-edebug-spec anonymous-form ((&or ["lambda" lambda] ["macro" macro])))
2030 ;; Standard functions that take function-forms arguments.
2032 ;; FIXME? The manual uses this form (maybe that's just for illustration?):
2033 ;; (def-edebug-spec let
2034 ;; ((&rest &or symbolp (gate symbolp &optional form))
2035 ;; body))
2036 (def-edebug-spec let
2037 ((&rest &or (symbolp &optional form) symbolp)
2038 body))
2040 (def-edebug-spec let* let)
2042 (def-edebug-spec setq (&rest symbolp form))
2043 (def-edebug-spec setq-default setq)
2045 (def-edebug-spec cond (&rest (&rest form)))
2047 (def-edebug-spec condition-case
2048 (symbolp
2049 form
2050 &rest ([&or symbolp (&rest symbolp)] body)))
2053 (def-edebug-spec \` (backquote-form))
2055 ;; Supports quotes inside backquotes,
2056 ;; but only at the top level inside unquotes.
2057 (def-edebug-spec backquote-form
2058 (&or
2059 ([&or "," ",@"] &or ("quote" backquote-form) form)
2060 ;; The simple version:
2061 ;; (backquote-form &rest backquote-form)
2062 ;; doesn't handle (a . ,b). The straightforward fix:
2063 ;; (backquote-form . [&or nil backquote-form])
2064 ;; uses up too much stack space.
2065 ;; Note that `(foo . ,@bar) is not valid, so we don't need to handle it.
2066 (backquote-form [&rest [&not ","] backquote-form]
2067 . [&or nil backquote-form])
2068 ;; If you use dotted forms in backquotes, replace the previous line
2069 ;; with the following. This takes quite a bit more stack space, however.
2070 ;; (backquote-form . [&or nil backquote-form])
2071 (vector &rest backquote-form)
2072 sexp))
2074 ;; Special version of backquote that instruments backquoted forms
2075 ;; destined to be evaluated, usually as the result of a
2076 ;; macroexpansion. Backquoted code can only have unquotes (, and ,@)
2077 ;; in places where list forms are allowed, and predicates. If the
2078 ;; backquote is used in a macro, unquoted code that come from
2079 ;; arguments must be instrumented, if at all, with def-form not def-body.
2081 ;; We could assume that all forms (not nested in other forms)
2082 ;; in arguments of macros should be def-forms, whether or not the macros
2083 ;; are defined with edebug-` but this would be expensive.
2085 ;; ,@ might have some problems.
2087 (defalias 'edebug-\` '\`) ;; same macro as regular backquote.
2088 (def-edebug-spec edebug-\` (def-form))
2090 ;; Assume immediate quote in unquotes mean backquote at next higher level.
2091 (def-edebug-spec \, (&or ("quote" edebug-\`) def-form))
2092 (def-edebug-spec \,@ (&define ;; so (,@ form) is never wrapped.
2093 &or ("quote" edebug-\`) def-form))
2095 ;; New byte compiler.
2097 (def-edebug-spec save-selected-window t)
2098 (def-edebug-spec save-current-buffer t)
2100 ;; Anything else?
2102 ;;; The debugger itself
2104 (defvar edebug-active nil) ;; Non-nil when edebug is active
2106 (defvar edebug-stack nil)
2107 ;; Stack of active functions evaluated via edebug.
2108 ;; Should be nil at the top level.
2110 (defvar edebug-stack-depth -1)
2111 ;; Index of last edebug-stack item.
2113 (defvar edebug-offset-indices nil)
2114 ;; Stack of offset indices of visited edebug sexps.
2115 ;; Should be nil at the top level.
2116 ;; Each function adds one cons. Top is modified with setcar.
2119 (defvar edebug-entered nil
2120 ;; Non-nil if edebug has already been entered at this recursive edit level.
2121 ;; This should stay nil at the top level.
2124 ;; Should these be options?
2125 (defconst edebug-debugger 'edebug
2126 ;; Name of function to use for debugging when error or quit occurs.
2127 ;; Set this to 'debug if you want to debug edebug.
2131 ;; Dynamically bound variables, declared globally but left unbound.
2132 (defvar edebug-function) ; the function being executed. change name!!
2133 (defvar edebug-data) ; the edebug data for the function
2134 (defvar edebug-def-mark) ; the mark for the definition
2135 (defvar edebug-freq-count) ; the count of expression visits.
2136 (defvar edebug-coverage) ; the coverage results of each expression of function.
2138 (defvar edebug-buffer) ; which buffer the function is in.
2140 (defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2141 (defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2143 (defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2144 (defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2146 ;;; Handling signals
2148 (defun edebug-signal (signal-name signal-data)
2149 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2150 A signal name is a symbol with an `error-conditions' property
2151 that is a list of condition names.
2152 A handler for any of those names will get to handle this signal.
2153 The symbol `error' should always be one of them.
2155 DATA should be a list. Its elements are printed as part of the error message.
2156 If the signal is handled, DATA is made available to the handler.
2157 See `condition-case'.
2159 This is the Edebug replacement for the standard `signal'. It should
2160 only be active while Edebug is. It checks `debug-on-error' to see
2161 whether it should call the debugger. When execution is resumed, the
2162 error is signaled again."
2163 (if (and (listp debug-on-error) (memq signal-name debug-on-error))
2164 (edebug 'error (cons signal-name signal-data)))
2165 ;; If we reach here without another non-local exit, then send signal again.
2166 ;; i.e. the signal is not continuable, yet.
2167 ;; Avoid infinite recursion.
2168 (let ((signal-hook-function nil))
2169 (signal signal-name signal-data)))
2171 ;;; Entering Edebug
2173 (defun edebug-enter (function args body)
2174 ;; Entering FUNC. The arguments are ARGS, and the body is BODY.
2175 ;; Setup edebug variables and evaluate BODY. This function is called
2176 ;; when a function evaluated with edebug-eval-top-level-form is entered.
2177 ;; Return the result of BODY.
2179 ;; Is this the first time we are entering edebug since
2180 ;; lower-level recursive-edit command?
2181 ;; More precisely, this tests whether Edebug is currently active.
2182 (let ((edebug-function function))
2183 (if (not edebug-entered)
2184 (let ((edebug-entered t)
2185 ;; Binding max-lisp-eval-depth here is OK,
2186 ;; but not inside an unwind-protect.
2187 ;; Doing it here also keeps it from growing too large.
2188 (max-lisp-eval-depth (+ 100 max-lisp-eval-depth)) ; too much??
2189 (max-specpdl-size (+ 200 max-specpdl-size))
2191 (debugger edebug-debugger) ; only while edebug is active.
2192 (edebug-outside-debug-on-error debug-on-error)
2193 (edebug-outside-debug-on-quit debug-on-quit)
2194 ;; Binding these may not be the right thing to do.
2195 ;; We want to allow the global values to be changed.
2196 (debug-on-error (or debug-on-error edebug-on-error))
2197 (debug-on-quit edebug-on-quit))
2198 (unwind-protect
2199 (let ((signal-hook-function 'edebug-signal))
2200 (setq edebug-execution-mode (or edebug-next-execution-mode
2201 edebug-initial-mode
2202 edebug-execution-mode)
2203 edebug-next-execution-mode nil)
2204 (edebug-enter function args body))))
2206 (let* ((edebug-data (get function 'edebug))
2207 (edebug-def-mark (car edebug-data)) ; mark at def start
2208 (edebug-freq-count (get function 'edebug-freq-count))
2209 (edebug-coverage (get function 'edebug-coverage))
2210 (edebug-buffer (marker-buffer edebug-def-mark))
2212 (edebug-stack (cons function edebug-stack))
2213 (edebug-offset-indices (cons 0 edebug-offset-indices))
2215 (if (get function 'edebug-on-entry)
2216 (progn
2217 (setq edebug-execution-mode 'step)
2218 (if (eq (get function 'edebug-on-entry) 'temp)
2219 (put function 'edebug-on-entry nil))))
2220 (if edebug-trace
2221 (edebug--enter-trace function args body)
2222 (funcall body))
2223 ))))
2225 (defun edebug-var-status (var)
2226 "Return a cons cell describing the status of VAR's current binding.
2227 The purpose of this function is so you can properly undo
2228 subsequent changes to the same binding, by passing the status
2229 cons cell to `edebug-restore-status'. The status cons cell
2230 has the form (LOCUS . VALUE), where LOCUS can be a buffer
2231 \(for a buffer-local binding), or nil (if the default binding is current)."
2232 (cons (variable-binding-locus var)
2233 (symbol-value var)))
2235 (defun edebug-restore-status (var status)
2236 "Reset VAR based on STATUS.
2237 STATUS should be a list returned by `edebug-var-status'."
2238 (let ((locus (car status))
2239 (value (cdr status)))
2240 (cond ((bufferp locus)
2241 (if (buffer-live-p locus)
2242 (with-current-buffer locus
2243 (set var value))))
2244 ((framep locus)
2245 (modify-frame-parameters locus (list (cons var value))))
2247 (set var value)))))
2249 (defun edebug--enter-trace (function args body)
2250 (let ((edebug-stack-depth (1+ edebug-stack-depth))
2251 edebug-result)
2252 (edebug-print-trace-before
2253 (format "%s args: %s" function args))
2254 (prog1 (setq edebug-result (funcall body))
2255 (edebug-print-trace-after
2256 (format "%s result: %s" function edebug-result)))))
2258 (def-edebug-spec edebug-tracing (form body))
2260 (defmacro edebug-tracing (msg &rest body)
2261 "Print MSG in *edebug-trace* before and after evaluating BODY.
2262 The result of BODY is also printed."
2263 `(let ((edebug-stack-depth (1+ edebug-stack-depth))
2264 edebug-result)
2265 (edebug-print-trace-before ,msg)
2266 (prog1 (setq edebug-result (progn ,@body))
2267 (edebug-print-trace-after
2268 (format "%s result: %s" ,msg edebug-result)))))
2270 (defun edebug-print-trace-before (msg)
2271 "Function called to print trace info before expression evaluation.
2272 MSG is printed after `::::{ '."
2273 (edebug-trace-display
2274 edebug-trace-buffer "%s{ %s" (make-string edebug-stack-depth ?\:) msg))
2276 (defun edebug-print-trace-after (msg)
2277 "Function called to print trace info after expression evaluation.
2278 MSG is printed after `::::} '."
2279 (edebug-trace-display
2280 edebug-trace-buffer "%s} %s" (make-string edebug-stack-depth ?\:) msg))
2284 (defun edebug-slow-before (before-index)
2285 (unless edebug-active
2286 ;; Debug current function given BEFORE position.
2287 ;; Called from functions compiled with edebug-eval-top-level-form.
2288 ;; Return the before index.
2289 (setcar edebug-offset-indices before-index)
2291 ;; Increment frequency count
2292 (aset edebug-freq-count before-index
2293 (1+ (aref edebug-freq-count before-index)))
2295 (if (or (not (memq edebug-execution-mode '(Go-nonstop next)))
2296 (input-pending-p))
2297 (edebug-debugger before-index 'before nil)))
2298 before-index)
2300 (defun edebug-fast-before (_before-index)
2301 ;; Do nothing.
2304 (defun edebug-slow-after (_before-index after-index value)
2305 (if edebug-active
2306 value
2307 ;; Debug current function given AFTER position and VALUE.
2308 ;; Called from functions compiled with edebug-eval-top-level-form.
2309 ;; Return VALUE.
2310 (setcar edebug-offset-indices after-index)
2312 ;; Increment frequency count
2313 (aset edebug-freq-count after-index
2314 (1+ (aref edebug-freq-count after-index)))
2315 (if edebug-test-coverage (edebug--update-coverage after-index value))
2317 (if (and (eq edebug-execution-mode 'Go-nonstop)
2318 (not (input-pending-p)))
2319 ;; Just return result.
2320 value
2321 (edebug-debugger after-index 'after value)
2324 (defun edebug-fast-after (_before-index _after-index value)
2325 ;; Do nothing but return the value.
2326 value)
2328 (defun edebug-run-slow ()
2329 (defalias 'edebug-before 'edebug-slow-before)
2330 (defalias 'edebug-after 'edebug-slow-after))
2332 ;; This is not used, yet.
2333 (defun edebug-run-fast ()
2334 (defalias 'edebug-before 'edebug-fast-before)
2335 (defalias 'edebug-after 'edebug-fast-after))
2337 (edebug-run-slow)
2340 (defun edebug--update-coverage (after-index value)
2341 (let ((old-result (aref edebug-coverage after-index)))
2342 (cond
2343 ((eq 'ok-coverage old-result))
2344 ((eq 'unknown old-result)
2345 (aset edebug-coverage after-index value))
2346 ;; Test if a different result.
2347 ((not (eq value old-result))
2348 (aset edebug-coverage after-index 'ok-coverage)))))
2351 ;; Dynamically declared unbound variables.
2352 (defvar edebug-breakpoints)
2353 (defvar edebug-break-data) ; break data for current function.
2354 (defvar edebug-break) ; whether a break occurred.
2355 (defvar edebug-global-break) ; whether a global break occurred.
2356 (defvar edebug-break-condition) ; whether the breakpoint is conditional.
2358 (defvar edebug-break-result nil)
2359 (defvar edebug-global-break-result nil)
2362 (defun edebug-debugger (offset-index arg-mode value)
2363 (if inhibit-redisplay
2364 ;; Don't really try to enter edebug within an eval from redisplay.
2365 value
2366 ;; Check breakpoints and pending input.
2367 ;; If edebug display should be updated, call edebug--display.
2368 ;; Return value.
2369 (let* ( ;; This needs to be here since breakpoints may be changed.
2370 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
2371 (edebug-break-data (assq offset-index edebug-breakpoints))
2372 (edebug-break-condition (car (cdr edebug-break-data)))
2373 (edebug-global-break
2374 (if edebug-global-break-condition
2375 (condition-case nil
2376 (setq edebug-global-break-result
2377 (edebug-eval edebug-global-break-condition))
2378 (error nil))))
2379 (edebug-break))
2381 ;;(edebug-trace "exp: %s" value)
2382 ;; Test whether we should break.
2383 (setq edebug-break
2384 (or edebug-global-break
2385 (and edebug-break-data
2386 (or (not edebug-break-condition)
2387 (setq edebug-break-result
2388 (edebug-eval edebug-break-condition))))))
2389 (if (and edebug-break
2390 (nth 2 edebug-break-data)) ; is it temporary?
2391 ;; Delete the breakpoint.
2392 (setcdr edebug-data
2393 (cons (delq edebug-break-data edebug-breakpoints)
2394 (cdr (cdr edebug-data)))))
2396 ;; Display if mode is not go, continue, or Continue-fast
2397 ;; or break, or input is pending,
2398 (if (or (not (memq edebug-execution-mode '(go continue Continue-fast)))
2399 edebug-break
2400 (input-pending-p))
2401 (edebug--display value offset-index arg-mode)) ; <---------- display
2403 value)))
2406 ;; window-start now stored with each function.
2407 ;;(defvar edebug-window-start nil)
2408 ;; Remember where each buffers' window starts between edebug calls.
2409 ;; This is to avoid spurious recentering.
2410 ;; Does this still need to be buffer-local??
2411 ;;(setq-default edebug-window-start nil)
2412 ;;(make-variable-buffer-local 'edebug-window-start)
2415 ;; Dynamically declared unbound vars
2416 (defvar edebug-point) ; the point in edebug buffer
2417 (defvar edebug-outside-buffer) ; the current-buffer outside of edebug
2418 (defvar edebug-outside-point) ; the point outside of edebug
2419 (defvar edebug-outside-mark) ; the mark outside of edebug
2420 (defvar edebug-window-data) ; window and window-start for current function
2421 (defvar edebug-outside-windows) ; outside window configuration
2422 (defvar edebug-eval-buffer) ; for the evaluation list.
2423 (defvar edebug-outside-d-c-i-n-s-w) ; outside default cursor-in-non-selected-windows
2425 (defvar edebug-eval-list nil) ;; List of expressions to evaluate.
2427 (defvar edebug-previous-result nil) ;; Last result returned.
2429 ;; Emacs 19 adds an arg to mark and mark-marker.
2430 (defalias 'edebug-mark-marker 'mark-marker)
2432 (defun edebug--display (value offset-index arg-mode)
2433 ;; edebug--display-1 is too big, we should split it. This function
2434 ;; here was just introduced to avoid making edebug--display-1
2435 ;; yet a bit deeper.
2436 (save-excursion (edebug--display-1 value offset-index arg-mode)))
2438 (defun edebug--display-1 (value offset-index arg-mode)
2439 (unless (marker-position edebug-def-mark)
2440 ;; The buffer holding the source has been killed.
2441 ;; Let's at least show a backtrace so the user can figure out
2442 ;; which function we're talking about.
2443 (debug))
2444 ;; Setup windows for edebug, determine mode, maybe enter recursive-edit.
2445 ;; Uses local variables of edebug-enter, edebug-before, edebug-after
2446 ;; and edebug-debugger.
2447 (let ((edebug-active t) ; For minor mode alist.
2448 (edebug-with-timeout-suspend (with-timeout-suspend))
2449 edebug-stop ; Should we enter recursive-edit?
2450 (edebug-point (+ edebug-def-mark
2451 (aref (nth 2 edebug-data) offset-index)))
2452 edebug-buffer-outside-point ; current point in edebug-buffer
2453 ;; window displaying edebug-buffer
2454 (edebug-window-data (nth 3 edebug-data))
2455 (edebug-outside-window (selected-window))
2456 (edebug-outside-buffer (current-buffer))
2457 (edebug-outside-point (point))
2458 (edebug-outside-mark (edebug-mark))
2459 edebug-outside-windows ; Window or screen configuration.
2460 edebug-buffer-points
2462 edebug-eval-buffer ; Declared here so we can kill it below.
2463 (eval-result-list (and edebug-eval-list
2464 (edebug-eval-result-list)))
2465 edebug-trace-window
2466 edebug-trace-window-start
2468 (edebug-outside-d-c-i-n-s-w
2469 (default-value 'cursor-in-non-selected-windows)))
2470 (unwind-protect
2471 (let ((cursor-in-echo-area nil)
2472 (unread-command-events nil)
2473 ;; any others??
2475 (setq-default cursor-in-non-selected-windows t)
2476 (if (not (buffer-name edebug-buffer))
2477 (user-error "Buffer defining %s not found" edebug-function))
2479 (if (eq 'after arg-mode)
2480 ;; Compute result string now before windows are modified.
2481 (edebug-compute-previous-result value))
2483 (if edebug-save-windows
2484 ;; Save windows now before we modify them.
2485 (setq edebug-outside-windows
2486 (edebug-current-windows edebug-save-windows)))
2488 (if edebug-save-displayed-buffer-points
2489 (setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
2491 ;; First move the edebug buffer point to edebug-point
2492 ;; so that window start doesn't get changed when we display it.
2493 ;; I don't know if this is going to help.
2494 ;;(set-buffer edebug-buffer)
2495 ;;(goto-char edebug-point)
2497 ;; If edebug-buffer is not currently displayed,
2498 ;; first find a window for it.
2499 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data))
2500 (setcar edebug-window-data (selected-window))
2502 ;; Now display eval list, if any.
2503 ;; This is done after the pop to edebug-buffer
2504 ;; so that buffer-window correspondence is correct after quitting.
2505 (edebug-eval-display eval-result-list)
2506 ;; The evaluation list better not have deleted edebug-window-data.
2507 (select-window (car edebug-window-data))
2508 (set-buffer edebug-buffer)
2510 (setq edebug-buffer-outside-point (point))
2511 (goto-char edebug-point)
2513 (if (eq 'before arg-mode)
2514 ;; Check whether positions are up-to-date.
2515 ;; This assumes point is never before symbol.
2516 (if (not (memq (following-char) '(?\( ?\# ?\` )))
2517 (user-error "Source has changed - reevaluate definition of %s"
2518 edebug-function)
2521 ;; Make sure we bind those in the right buffer (bug#16410).
2522 (let ((overlay-arrow-position overlay-arrow-position)
2523 (overlay-arrow-string overlay-arrow-string))
2524 ;; Now display arrow based on mode.
2525 (edebug-overlay-arrow)
2527 (cond
2528 ((eq 'error arg-mode)
2529 ;; Display error message
2530 (setq edebug-execution-mode 'step)
2531 (edebug-overlay-arrow)
2532 (beep)
2533 (if (eq 'quit (car value))
2534 (message "Quit")
2535 (edebug-report-error value)))
2536 (edebug-break
2537 (cond
2538 (edebug-global-break
2539 (message "Global Break: %s => %s"
2540 edebug-global-break-condition
2541 edebug-global-break-result))
2542 (edebug-break-condition
2543 (message "Break: %s => %s"
2544 edebug-break-condition
2545 edebug-break-result))
2546 ((not (eq edebug-execution-mode 'Continue-fast))
2547 (message "Break"))
2548 (t)))
2550 (t (message "")))
2552 (if (eq 'after arg-mode)
2553 (progn
2554 ;; Display result of previous evaluation.
2555 (if (and edebug-break
2556 edebug-sit-on-break
2557 (not (eq edebug-execution-mode 'Continue-fast)))
2558 (sit-for edebug-sit-for-seconds)) ; Show message.
2559 (edebug-previous-result)))
2561 (cond
2562 (edebug-break
2563 (cond
2564 ((eq edebug-execution-mode 'continue)
2565 (sit-for edebug-sit-for-seconds))
2566 ((eq edebug-execution-mode 'Continue-fast) (sit-for 0))
2567 (t (setq edebug-stop t))))
2568 ;; not edebug-break
2569 ((eq edebug-execution-mode 'trace)
2570 (sit-for edebug-sit-for-seconds)) ; Force update and pause.
2571 ((eq edebug-execution-mode 'Trace-fast)
2572 (sit-for 0))) ; Force update and continue.
2574 (when (input-pending-p)
2575 (setq edebug-stop t)
2576 (setq edebug-execution-mode 'step) ; for `edebug-overlay-arrow'
2577 (edebug-stop))
2579 (edebug-overlay-arrow)
2581 (unwind-protect
2582 (if (or edebug-stop
2583 (memq edebug-execution-mode '(step next))
2584 (eq arg-mode 'error))
2585 (edebug--recursive-edit arg-mode)) ; <--- Recursive edit
2587 ;; Reset the edebug-window-data to whatever it is now.
2588 (let ((window (if (eq (window-buffer) edebug-buffer)
2589 (selected-window)
2590 (get-buffer-window edebug-buffer))))
2591 ;; Remember window-start for edebug-buffer, if still displayed.
2592 (if window
2593 (progn
2594 (setcar edebug-window-data window)
2595 (setcdr edebug-window-data (window-start window)))))
2597 ;; Save trace window point before restoring outside windows.
2598 ;; Could generalize this for other buffers.
2599 (setq edebug-trace-window
2600 (get-buffer-window edebug-trace-buffer))
2601 (if edebug-trace-window
2602 (setq edebug-trace-window-start
2603 (and edebug-trace-window
2604 (window-start edebug-trace-window))))
2606 ;; Restore windows before continuing.
2607 (if edebug-save-windows
2608 (progn
2609 (edebug-set-windows edebug-outside-windows)
2611 ;; Restore displayed buffer points.
2612 ;; Needed even if restoring windows because
2613 ;; window-points are not restored. (should they be??)
2614 (if edebug-save-displayed-buffer-points
2615 (edebug-set-buffer-points edebug-buffer-points))
2617 ;; Unrestore trace window's window-point.
2618 (if edebug-trace-window
2619 (set-window-start edebug-trace-window
2620 edebug-trace-window-start))
2622 ;; Unrestore edebug-buffer's window-start, if displayed.
2623 (let ((window (car edebug-window-data)))
2624 (if (and (edebug-window-live-p window)
2625 (eq (window-buffer) edebug-buffer))
2626 (progn
2627 (set-window-start window (cdr edebug-window-data)
2628 'no-force)
2629 ;; Unrestore edebug-buffer's window-point.
2630 ;; Needed in addition to setting the buffer point
2631 ;; - otherwise quitting doesn't leave point as is.
2632 ;; But can this causes point to not be restored.
2633 ;; Also, it may not be a visible window.
2634 ;; (set-window-point window edebug-point)
2637 ;; Unrestore edebug-buffer's point. Rerestored below.
2638 ;; (goto-char edebug-point) ;; in edebug-buffer
2640 ;; Since we may be in a save-excursion, in case of quit,
2641 ;; reselect the outside window only.
2642 ;; Only needed if we are not recovering windows??
2643 (if (edebug-window-live-p edebug-outside-window)
2644 (select-window edebug-outside-window))
2645 ) ; if edebug-save-windows
2647 ;; Restore current buffer always, in case application needs it.
2648 (if (buffer-name edebug-outside-buffer)
2649 (set-buffer edebug-outside-buffer))
2650 ;; Restore point, and mark.
2651 ;; Needed even if restoring windows because
2652 ;; that doesn't restore point and mark in the current buffer.
2653 ;; But don't restore point if edebug-buffer is current buffer.
2654 (if (not (eq edebug-buffer edebug-outside-buffer))
2655 (goto-char edebug-outside-point))
2656 (if (marker-buffer (edebug-mark-marker))
2657 ;; Does zmacs-regions need to be nil while doing set-marker?
2658 (set-marker (edebug-mark-marker) edebug-outside-mark))
2659 )) ; unwind-protect
2660 ;; None of the following is done if quit or signal occurs.
2662 ;; Restore edebug-buffer's outside point.
2663 ;; (edebug-trace "restore edebug-buffer point: %s"
2664 ;; edebug-buffer-outside-point)
2665 (with-current-buffer edebug-buffer
2666 (goto-char edebug-buffer-outside-point))
2667 ;; ... nothing more.
2669 ;; Could be an option to keep eval display up.
2670 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
2671 (with-timeout-unsuspend edebug-with-timeout-suspend)
2672 ;; Reset global variables to outside values in case they were changed.
2673 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
2677 (defvar edebug-number-of-recursions 0)
2678 ;; Number of recursive edits started by edebug.
2679 ;; Should be 0 at the top level.
2681 (defvar edebug-recursion-depth 0)
2682 ;; Value of recursion-depth when edebug was called.
2684 ;; Dynamically declared unbound vars
2685 (defvar edebug-outside-match-data) ; match data outside of edebug
2686 (defvar edebug-backtrace-buffer) ; each recursive edit gets its own
2687 (defvar edebug-inside-windows)
2688 (defvar edebug-interactive-p)
2690 (defun edebug--recursive-edit (arg-mode)
2691 ;; Start up a recursive edit inside of edebug.
2692 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
2693 ;; Assume that none of the variables below are buffer-local.
2694 (let (;; match-data must be done in the outside buffer
2695 (edebug-outside-match-data
2696 (with-current-buffer edebug-outside-buffer ; in case match buffer different
2697 (match-data)))
2699 ;;(edebug-number-of-recursions (1+ edebug-number-of-recursions))
2700 (edebug-recursion-depth (recursion-depth))
2701 edebug-entered ; bind locally to nil
2702 (edebug-interactive-p nil) ; again non-interactive
2703 edebug-backtrace-buffer ; each recursive edit gets its own
2704 ;; The window configuration may be saved and restored
2705 ;; during a recursive-edit
2706 edebug-inside-windows
2709 (unwind-protect
2710 (let (
2711 ;; Declare global values local but using the same global value.
2712 ;; We could set these to the values for previous edebug call.
2713 (last-command last-command)
2714 (this-command this-command)
2715 (current-prefix-arg nil)
2717 ;; More for Emacs 19
2718 (last-input-event nil)
2719 (last-command-event nil)
2720 (last-event-frame nil)
2721 (last-nonmenu-event nil)
2722 (track-mouse nil)
2724 (standard-output t)
2725 (standard-input t)
2727 ;; Don't keep reading from an executing kbd macro
2728 ;; within edebug unless edebug-continue-kbd-macro is
2729 ;; non-nil. Again, local binding may not be best.
2730 (executing-kbd-macro
2731 (if edebug-continue-kbd-macro executing-kbd-macro))
2733 ;; Don't get confused by the user's keymap changes.
2734 (overriding-local-map nil)
2735 (overriding-terminal-local-map nil)
2737 ;; Bind again to outside values.
2738 (debug-on-error edebug-outside-debug-on-error)
2739 (debug-on-quit edebug-outside-debug-on-quit)
2741 ;; Don't keep defining a kbd macro.
2742 (defining-kbd-macro
2743 (if edebug-continue-kbd-macro defining-kbd-macro))
2745 ;; others??
2748 (if (and (eq edebug-execution-mode 'go)
2749 (not (memq arg-mode '(after error))))
2750 (message "Break"))
2752 (setq signal-hook-function nil)
2754 (edebug-mode 1)
2755 (unwind-protect
2756 (recursive-edit) ; <<<<<<<<<< Recursive edit
2758 ;; Do the following, even if quit occurs.
2759 (setq signal-hook-function 'edebug-signal)
2760 (if edebug-backtrace-buffer
2761 (kill-buffer edebug-backtrace-buffer))
2763 ;; Remember selected-window after recursive-edit.
2764 ;; (setq edebug-inside-window (selected-window))
2766 (set-match-data edebug-outside-match-data)
2768 ;; Recursive edit may have changed buffers,
2769 ;; so set it back before exiting let.
2770 (if (buffer-name edebug-buffer) ; if it still exists
2771 (progn
2772 (set-buffer edebug-buffer)
2773 (when (memq edebug-execution-mode '(go Go-nonstop))
2774 (edebug-overlay-arrow)
2775 (sit-for 0))
2776 (edebug-mode -1))
2777 ;; gotta have a buffer to let its buffer local variables be set
2778 (get-buffer-create " bogus edebug buffer"))
2779 ));; inner let
2783 ;;; Display related functions
2785 (defconst edebug-arrow-alist
2786 '((Continue-fast . "=")
2787 (Trace-fast . "-")
2788 (continue . ">")
2789 (trace . "->")
2790 (step . "=>")
2791 (next . "=>")
2792 (go . "<>")
2793 (Go-nonstop . "..")
2795 "Association list of arrows for each edebug mode.")
2797 (defun edebug-overlay-arrow ()
2798 ;; Set up the overlay arrow at beginning-of-line in current buffer.
2799 ;; The arrow string is derived from edebug-arrow-alist and
2800 ;; edebug-execution-mode.
2801 (let ((pos (line-beginning-position)))
2802 (setq overlay-arrow-string
2803 (cdr (assq edebug-execution-mode edebug-arrow-alist)))
2804 (setq overlay-arrow-position (make-marker))
2805 (set-marker overlay-arrow-position pos (current-buffer))))
2808 (defun edebug-toggle-save-all-windows ()
2809 "Toggle the saving and restoring of all windows.
2810 Also, each time you toggle it on, the inside and outside window
2811 configurations become the same as the current configuration."
2812 (interactive)
2813 (setq edebug-save-windows (not edebug-save-windows))
2814 (if edebug-save-windows
2815 (setq edebug-inside-windows
2816 (setq edebug-outside-windows
2817 (edebug-current-windows
2818 edebug-save-windows))))
2819 (message "Window saving is %s for all windows."
2820 (if edebug-save-windows "on" "off")))
2822 (defmacro edebug-changing-windows (&rest body)
2823 `(let ((window (selected-window)))
2824 (setq edebug-inside-windows (edebug-current-windows t))
2825 (edebug-set-windows edebug-outside-windows)
2826 ,@body;; Code to change edebug-save-windows
2827 (setq edebug-outside-windows (edebug-current-windows
2828 edebug-save-windows))
2829 ;; Problem: what about outside windows that are deleted inside?
2830 (edebug-set-windows edebug-inside-windows)))
2832 (defun edebug-toggle-save-selected-window ()
2833 "Toggle the saving and restoring of the selected window.
2834 Also, each time you toggle it on, the inside and outside window
2835 configurations become the same as the current configuration."
2836 (interactive)
2837 (cond
2838 ((eq t edebug-save-windows)
2839 ;; Save all outside windows except the selected one.
2840 ;; Remove (selected-window) from outside-windows.
2841 (edebug-changing-windows
2842 (setq edebug-save-windows (delq window (edebug-window-list)))))
2844 ((memq (selected-window) edebug-save-windows)
2845 (setq edebug-outside-windows
2846 (delq (assq (selected-window) edebug-outside-windows)
2847 edebug-outside-windows))
2848 (setq edebug-save-windows
2849 (delq (selected-window) edebug-save-windows)))
2850 (t ; Save a new window.
2851 (edebug-changing-windows
2852 (setq edebug-save-windows (cons window edebug-save-windows)))))
2854 (message "Window saving is %s for %s."
2855 (if (memq (selected-window) edebug-save-windows)
2856 "on" "off")
2857 (selected-window)))
2859 (defun edebug-toggle-save-windows (arg)
2860 "Toggle the saving and restoring of windows.
2861 With prefix, toggle for just the selected window.
2862 Otherwise, toggle for all windows."
2863 (interactive "P")
2864 (if arg
2865 (edebug-toggle-save-selected-window)
2866 (edebug-toggle-save-all-windows)))
2868 (defun edebug-where ()
2869 "Show the debug windows and where we stopped in the program."
2870 (interactive)
2871 (if (not edebug-active)
2872 (error "Edebug is not active"))
2873 ;; Restore the window configuration to what it last was inside.
2874 ;; But it is not always set. - experiment
2875 ;;(if edebug-inside-windows
2876 ;; (edebug-set-windows edebug-inside-windows))
2877 (edebug-pop-to-buffer edebug-buffer)
2878 (goto-char edebug-point))
2880 (defun edebug-view-outside ()
2881 "Change to the outside window configuration.
2882 Use `edebug-where' to return."
2883 (interactive)
2884 (if (not edebug-active)
2885 (error "Edebug is not active"))
2886 (setq edebug-inside-windows
2887 (edebug-current-windows edebug-save-windows))
2888 (edebug-set-windows edebug-outside-windows)
2889 (goto-char edebug-outside-point)
2890 (message "Window configuration outside of Edebug. Return with %s"
2891 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
2894 (defun edebug-bounce-point (arg)
2895 "Bounce the point in the outside current buffer.
2896 If prefix argument ARG is supplied, sit for that many seconds
2897 before returning. The default is one second."
2898 (interactive "p")
2899 (if (not edebug-active)
2900 (error "Edebug is not active"))
2901 (save-excursion
2902 ;; If the buffer's currently displayed, avoid set-window-configuration.
2903 (save-window-excursion
2904 (edebug-pop-to-buffer edebug-outside-buffer)
2905 (goto-char edebug-outside-point)
2906 (message "Current buffer: %s Point: %s Mark: %s"
2907 (current-buffer) (point)
2908 (if (marker-buffer (edebug-mark-marker))
2909 (marker-position (edebug-mark-marker)) "<not set>"))
2910 (sit-for arg)
2911 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data)))))
2914 ;; Joe Wells, here is a start at your idea of adding a buffer to the internal
2915 ;; display list. Still need to use this list in edebug--display.
2917 '(defvar edebug-display-buffer-list nil
2918 "List of buffers that edebug will display when it is active.")
2920 '(defun edebug-display-buffer (buffer)
2921 "Toggle display of a buffer inside of edebug."
2922 (interactive "bBuffer: ")
2923 (let ((already-displaying (memq buffer edebug-display-buffer-list)))
2924 (setq edebug-display-buffer-list
2925 (if already-displaying
2926 (delq buffer edebug-display-buffer-list)
2927 (cons buffer edebug-display-buffer-list)))
2928 (message "Displaying %s %s" buffer
2929 (if already-displaying "off" "on"))))
2931 ;;; Breakpoint related functions
2933 (defun edebug-find-stop-point ()
2934 ;; Return (function . index) of the nearest edebug stop point.
2935 (let* ((edebug-def-name (edebug-form-data-symbol))
2936 (edebug-data
2937 (let ((data (get edebug-def-name 'edebug)))
2938 (if (or (null data) (markerp data))
2939 (error "%s is not instrumented for Edebug" edebug-def-name))
2940 data)) ; we could do it automatically, if data is a marker.
2941 ;; pull out parts of edebug-data.
2942 (edebug-def-mark (car edebug-data))
2943 ;; (edebug-breakpoints (car (cdr edebug-data)))
2945 (offset-vector (nth 2 edebug-data))
2946 (offset (- (save-excursion
2947 (if (looking-at "[ \t]")
2948 ;; skip backwards until non-whitespace, or bol
2949 (skip-chars-backward " \t"))
2950 (point))
2951 edebug-def-mark))
2952 len i)
2953 ;; the offsets are in order so we can do a linear search
2954 (setq len (length offset-vector))
2955 (setq i 0)
2956 (while (and (< i len) (> offset (aref offset-vector i)))
2957 (setq i (1+ i)))
2958 (if (and (< i len)
2959 (<= offset (aref offset-vector i)))
2960 ;; return the relevant info
2961 (cons edebug-def-name i)
2962 (message "Point is not on an expression in %s."
2963 edebug-def-name)
2967 (defun edebug-next-breakpoint ()
2968 "Move point to the next breakpoint, or first if none past point."
2969 (interactive)
2970 (let ((edebug-stop-point (edebug-find-stop-point)))
2971 (if edebug-stop-point
2972 (let* ((edebug-def-name (car edebug-stop-point))
2973 (index (cdr edebug-stop-point))
2974 (edebug-data (get edebug-def-name 'edebug))
2976 ;; pull out parts of edebug-data
2977 (edebug-def-mark (car edebug-data))
2978 (edebug-breakpoints (car (cdr edebug-data)))
2979 (offset-vector (nth 2 edebug-data))
2980 breakpoint)
2981 (if (not edebug-breakpoints)
2982 (message "No breakpoints in this function.")
2983 (let ((breaks edebug-breakpoints))
2984 (while (and breaks
2985 (<= (car (car breaks)) index))
2986 (setq breaks (cdr breaks)))
2987 (setq breakpoint
2988 (if breaks
2989 (car breaks)
2990 ;; goto the first breakpoint
2991 (car edebug-breakpoints)))
2992 (goto-char (+ edebug-def-mark
2993 (aref offset-vector (car breakpoint))))
2995 (message "%s"
2996 (concat (if (nth 2 breakpoint)
2997 "Temporary " "")
2998 (if (car (cdr breakpoint))
2999 (format "Condition: %s"
3000 (edebug-safe-prin1-to-string
3001 (car (cdr breakpoint))))
3002 "")))
3003 ))))))
3006 (defun edebug-modify-breakpoint (flag &optional condition temporary)
3007 "Modify the breakpoint for the form at point or after it.
3008 Set it if FLAG is non-nil, clear it otherwise. Then move to that point.
3009 If CONDITION or TEMPORARY are non-nil, add those attributes to
3010 the breakpoint."
3011 (let ((edebug-stop-point (edebug-find-stop-point)))
3012 (if edebug-stop-point
3013 (let* ((edebug-def-name (car edebug-stop-point))
3014 (index (cdr edebug-stop-point))
3015 (edebug-data (get edebug-def-name 'edebug))
3017 ;; pull out parts of edebug-data
3018 (edebug-def-mark (car edebug-data))
3019 (edebug-breakpoints (car (cdr edebug-data)))
3020 (offset-vector (nth 2 edebug-data))
3021 present)
3022 ;; delete it either way
3023 (setq present (assq index edebug-breakpoints))
3024 (setq edebug-breakpoints (delq present edebug-breakpoints))
3025 (if flag
3026 (progn
3027 ;; add it to the list and resort
3028 (setq edebug-breakpoints
3029 (edebug-sort-alist
3030 (cons
3031 (list index condition temporary)
3032 edebug-breakpoints) '<))
3033 (if condition
3034 (message "Breakpoint set in %s with condition: %s"
3035 edebug-def-name condition)
3036 (message "Breakpoint set in %s" edebug-def-name)))
3037 (if present
3038 (message "Breakpoint unset in %s" edebug-def-name)
3039 (message "No breakpoint here")))
3041 (setcar (cdr edebug-data) edebug-breakpoints)
3042 (goto-char (+ edebug-def-mark (aref offset-vector index)))
3043 ))))
3045 (defun edebug-set-breakpoint (arg)
3046 "Set the breakpoint of nearest sexp.
3047 With prefix argument, make it a temporary breakpoint."
3048 (interactive "P")
3049 (edebug-modify-breakpoint t nil arg))
3051 (defun edebug-unset-breakpoint ()
3052 "Clear the breakpoint of nearest sexp."
3053 (interactive)
3054 (edebug-modify-breakpoint nil))
3057 (defun edebug-set-global-break-condition (expression)
3058 "Set `edebug-global-break-condition' to EXPRESSION."
3059 (interactive
3060 (list
3061 (let ((initial (and edebug-global-break-condition
3062 (format "%s" edebug-global-break-condition))))
3063 (read-from-minibuffer
3064 "Global Condition: " initial read-expression-map t
3065 (if (equal (car read-expression-history) initial)
3066 '(read-expression-history . 1)
3067 'read-expression-history)))))
3068 (setq edebug-global-break-condition expression))
3071 ;;; Mode switching functions
3073 (defun edebug-set-mode (mode shortmsg msg)
3074 ;; Set the edebug mode to MODE.
3075 ;; Display SHORTMSG, or MSG if not within edebug.
3076 (if (eq (1+ edebug-recursion-depth) (recursion-depth))
3077 (progn
3078 (setq edebug-execution-mode mode)
3079 (message "%s" shortmsg)
3080 ;; Continue execution
3081 (exit-recursive-edit))
3082 ;; This is not terribly useful!!
3083 (setq edebug-next-execution-mode mode)
3084 (message "%s" msg)))
3087 (defalias 'edebug-step-through-mode 'edebug-step-mode)
3089 (defun edebug-step-mode ()
3090 "Proceed to next stop point."
3091 (interactive)
3092 (edebug-set-mode 'step "" "Edebug will stop at next stop point."))
3094 (defun edebug-next-mode ()
3095 "Proceed to next `after' stop point."
3096 (interactive)
3097 (edebug-set-mode 'next "" "Edebug will stop after next eval."))
3099 (defun edebug-go-mode (arg)
3100 "Go, evaluating until break.
3101 With prefix ARG, set temporary break at current point and go."
3102 (interactive "P")
3103 (if arg
3104 (edebug-set-breakpoint t))
3105 (edebug-set-mode 'go "Go..." "Edebug will go until break."))
3107 (defun edebug-Go-nonstop-mode ()
3108 "Go, evaluating without debugging.
3109 You can use `edebug-stop', or any editing command, to stop."
3110 (interactive)
3111 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
3112 "Edebug will not stop at breaks."))
3115 (defun edebug-trace-mode ()
3116 "Begin trace mode.
3117 Pauses for `edebug-sit-for-seconds' at each stop point."
3118 (interactive)
3119 (edebug-set-mode 'trace "Tracing..." "Edebug will trace with pause."))
3121 (defun edebug-Trace-fast-mode ()
3122 "Trace with no wait at each step.
3123 Updates the display at each stop point, but does not pause."
3124 (interactive)
3125 (edebug-set-mode 'Trace-fast
3126 "Trace fast..." "Edebug will trace without pause."))
3128 (defun edebug-continue-mode ()
3129 "Begin continue mode.
3130 Pauses for `edebug-sit-for-seconds' at each break point."
3131 (interactive)
3132 (edebug-set-mode 'continue "Continue..."
3133 "Edebug will pause at breakpoints."))
3135 (defun edebug-Continue-fast-mode ()
3136 "Trace with no wait at each step.
3137 Updates the display at each break point, but does not pause."
3138 (interactive)
3139 (edebug-set-mode 'Continue-fast "Continue fast..."
3140 "Edebug will stop and go at breakpoints."))
3142 ;; ------------------------------------------------------------
3143 ;; The following use the mode changing commands and breakpoints.
3146 (defun edebug-goto-here ()
3147 "Proceed to first stop-point at or after current position of point."
3148 (interactive)
3149 (edebug-go-mode t))
3152 (defun edebug-stop ()
3153 "Stop execution and do not continue.
3154 Useful for exiting from trace or continue loop."
3155 (interactive)
3156 (message "Stop"))
3159 '(defun edebug-forward ()
3160 "Proceed to the exit of the next expression to be evaluated."
3161 (interactive)
3162 (edebug-set-mode
3163 'forward "Forward"
3164 "Edebug will stop after exiting the next expression."))
3167 (defun edebug-forward-sexp (arg)
3168 "Proceed from the current point to the end of the ARGth sexp ahead.
3169 If there are not ARG sexps ahead, then do `edebug-step-out'."
3170 (interactive "p")
3171 (condition-case nil
3172 (let ((parse-sexp-ignore-comments t))
3173 ;; Call forward-sexp repeatedly until done or failure.
3174 (forward-sexp arg)
3175 (edebug-go-mode t))
3176 (error
3177 (edebug-step-out)
3180 (defun edebug-step-out ()
3181 "Proceed from the current point to the end of the containing sexp.
3182 If there is no containing sexp that is not the top level defun,
3183 go to the end of the last sexp, or if that is the same point, then step."
3184 (interactive)
3185 (condition-case nil
3186 (let ((parse-sexp-ignore-comments t))
3187 (up-list 1)
3188 (save-excursion
3189 ;; Is there still a containing expression?
3190 (up-list 1))
3191 (edebug-go-mode t))
3192 (error
3193 ;; At top level - 1, so first check if there are more sexps at this level.
3194 (let ((start-point (point)))
3195 ;; (up-list 1)
3196 (down-list -1)
3197 (if (= (point) start-point)
3198 (edebug-step-mode) ; No more at this level, so step.
3199 (edebug-go-mode t)
3200 )))))
3202 (defun edebug-instrument-function (func)
3203 "Instrument the function or generic method FUNC.
3204 Return the list of function symbols which were instrumented.
3205 This may be simply (FUNC) for a normal function, or a list of
3206 generated symbols for methods. If a function or method to
3207 instrument cannot be found, signal an error."
3208 (let ((func-marker (get func 'edebug)))
3209 (cond
3210 ((cl-generic-p func)
3211 (let ((method-defs (cl--generic-method-files func))
3212 symbols)
3213 (unless method-defs
3214 (error "Could not find any method definitions for %s" func))
3215 (pcase-dolist (`(,file . ,spec) method-defs)
3216 (let* ((loc (find-function-search-for-symbol spec 'cl-defmethod file)))
3217 (unless (cdr loc)
3218 (error "Could not find the definition for %s in its file" spec))
3219 (with-current-buffer (car loc)
3220 (goto-char (cdr loc))
3221 (edebug-eval-top-level-form)
3222 (push (edebug-form-data-symbol) symbols))))
3223 symbols))
3224 ((and (markerp func-marker) (marker-buffer func-marker))
3225 ;; It is uninstrumented, so instrument it.
3226 (with-current-buffer (marker-buffer func-marker)
3227 (goto-char func-marker)
3228 (edebug-eval-top-level-form)
3229 (list func)))
3230 ((consp func-marker)
3231 (message "%s is already instrumented." func)
3232 (list func))
3234 (let ((loc (find-function-noselect func t)))
3235 (unless (cdr loc)
3236 (error "Could not find the definition in its file"))
3237 (with-current-buffer (car loc)
3238 (goto-char (cdr loc))
3239 (edebug-eval-top-level-form)
3240 (list func)))))))
3242 (defun edebug-instrument-callee ()
3243 "Instrument the definition of the function or macro about to be called.
3244 Do this when stopped before the form or it will be too late.
3245 One side effect of using this command is that the next time the
3246 function or macro is called, Edebug will be called there as well.
3247 If the callee is a generic function, Edebug will instrument all
3248 the methods, not just the one which is about to be called. Return
3249 the list of symbols which were instrumented."
3250 (interactive)
3251 (if (not (looking-at "("))
3252 (error "You must be before a list form")
3253 (let ((func
3254 (save-excursion
3255 (down-list 1)
3256 (if (looking-at "(")
3257 (edebug--form-data-name
3258 (edebug-get-form-data-entry (point)))
3259 (read (current-buffer))))))
3260 (edebug-instrument-function func))))
3263 (defun edebug-step-in ()
3264 "Step into the definition of the function, macro or method about to be called.
3265 This first does `edebug-instrument-callee' to ensure that it is
3266 instrumented. Then it does `edebug-on-entry' and switches to `go' mode."
3267 (interactive)
3268 (let ((funcs (edebug-instrument-callee)))
3269 (if funcs
3270 (progn
3271 (mapc (lambda (func) (edebug-on-entry func 'temp)) funcs)
3272 (edebug-go-mode nil)))))
3274 (defun edebug-on-entry (function &optional flag)
3275 "Cause Edebug to stop when FUNCTION is called.
3276 With prefix argument, make this temporary so it is automatically
3277 canceled the first time the function is entered."
3278 (interactive "aEdebug on entry to: \nP")
3279 ;; Could store this in the edebug data instead.
3280 (put function 'edebug-on-entry (if flag 'temp t)))
3282 (defun cancel-edebug-on-entry (function)
3283 (interactive "aEdebug on entry to: ")
3284 (put function 'edebug-on-entry nil))
3287 '(advice-add 'debug-on-entry :around 'edebug--debug-on-entry) ;; Should we do this?
3288 ;; Also need edebug-cancel-debug-on-entry
3290 '(defun edebug--debug-on-entry (orig function)
3291 "If the function is instrumented for Edebug, call `edebug-on-entry'."
3292 (let ((func-data (get function 'edebug)))
3293 (if (or (null func-data) (markerp func-data))
3294 (funcall orig function)
3295 (edebug-on-entry function))))
3298 (defun edebug-top-level-nonstop ()
3299 "Set mode to Go-nonstop, and exit to top-level.
3300 This is useful for exiting even if `unwind-protect' code may be executed."
3301 (interactive)
3302 (setq edebug-execution-mode 'Go-nonstop)
3303 (top-level))
3305 ;;(defun edebug-exit-out ()
3306 ;; "Go until the current function exits."
3307 ;; (interactive)
3308 ;; (edebug-set-mode 'exiting "Exit..."))
3310 (defconst edebug-initial-mode-alist
3311 '((edebug-step-mode . step)
3312 (edebug-next-mode . next)
3313 (edebug-trace-mode . trace)
3314 (edebug-Trace-fast-mode . Trace-fast)
3315 (edebug-go-mode . go)
3316 (edebug-continue-mode . continue)
3317 (edebug-Continue-fast-mode . Continue-fast)
3318 (edebug-Go-nonstop-mode . Go-nonstop))
3319 "Association list between commands and the modes they set.")
3321 (defvar edebug-mode-map) ; will be defined fully later.
3323 (defun edebug-set-initial-mode ()
3324 "Set the initial execution mode of Edebug.
3325 The mode is requested via the key that would be used to set the mode in
3326 edebug-mode."
3327 (interactive)
3328 (let* ((old-mode edebug-initial-mode)
3329 (key (read-key-sequence
3330 (format
3331 "Change initial edebug mode from %s (%c) to (enter key): "
3332 old-mode
3333 (aref (where-is-internal
3334 (car (rassq old-mode edebug-initial-mode-alist))
3335 edebug-mode-map 'firstonly)
3336 0))))
3337 (mode (cdr (assq (lookup-key edebug-mode-map key)
3338 edebug-initial-mode-alist))))
3339 (if mode
3340 (progn
3341 (setq edebug-initial-mode mode)
3342 (message "Edebug's initial mode is now: %s" mode))
3343 (error "Key must map to one of the mode changing commands"))))
3345 ;;; Evaluation of expressions
3347 (defmacro edebug-outside-excursion (&rest body)
3348 "Evaluate an expression list in the outside context.
3349 Return the result of the last expression."
3350 ;; Only restores the non-variables context since all the variables let-bound
3351 ;; by Edebug will be properly reset to the appropriate context's value by
3352 ;; backtrace-eval.
3353 (declare (debug t))
3354 `(save-excursion ; of current-buffer
3355 (if edebug-save-windows
3356 (progn
3357 ;; After excursion, we will
3358 ;; restore to current window configuration.
3359 (setq edebug-inside-windows
3360 (edebug-current-windows edebug-save-windows))
3361 ;; Restore outside windows.
3362 (edebug-set-windows edebug-outside-windows)))
3364 (set-buffer edebug-buffer) ; why?
3365 (set-match-data edebug-outside-match-data)
3366 ;; Restore outside context.
3367 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
3368 (unwind-protect
3369 ;; FIXME: This restoring of edebug-outside-buffer and
3370 ;; edebug-outside-point is redundant now that backtrace-eval does it
3371 ;; for us.
3372 (with-current-buffer edebug-outside-buffer ; of edebug-buffer
3373 (goto-char edebug-outside-point)
3374 (if (marker-buffer (edebug-mark-marker))
3375 (set-marker (edebug-mark-marker) edebug-outside-mark))
3376 ,@body)
3378 ;; Back to edebug-buffer. Restore rest of inside context.
3379 ;; (use-local-map edebug-inside-map)
3380 (if edebug-save-windows
3381 ;; Restore inside windows.
3382 (edebug-set-windows edebug-inside-windows))
3384 ;; Save values that may have been changed.
3385 (setq edebug-outside-d-c-i-n-s-w
3386 (default-value 'cursor-in-non-selected-windows))
3388 ;; Restore the outside saved values; don't alter
3389 ;; the outside binding loci.
3390 (setq-default cursor-in-non-selected-windows t))))
3392 (defun edebug-eval (expr)
3393 (backtrace-eval expr 0 'edebug-after))
3395 (defun edebug-safe-eval (expr)
3396 ;; Evaluate EXPR safely.
3397 ;; If there is an error, a string is returned describing the error.
3398 (condition-case edebug-err
3399 (edebug-eval expr)
3400 (error (edebug-format "%s: %s" ;; could
3401 (get (car edebug-err) 'error-message)
3402 (car (cdr edebug-err))))))
3404 ;;; Printing
3407 (defun edebug-report-error (value)
3408 ;; Print an error message like command level does.
3409 ;; This also prints the error name if it has no error-message.
3410 (message "%s: %s"
3411 (or (get (car value) 'error-message)
3412 (format "peculiar error (%s)" (car value)))
3413 (mapconcat (lambda (edebug-arg)
3414 ;; continuing after an error may
3415 ;; complain about edebug-arg. why??
3416 (prin1-to-string edebug-arg))
3417 (cdr value) ", ")))
3419 (defvar print-readably) ; defined by lemacs
3420 ;; Alternatively, we could change the definition of
3421 ;; edebug-safe-prin1-to-string to only use these if defined.
3423 (defun edebug-safe-prin1-to-string (value)
3424 (let ((print-escape-newlines t)
3425 (print-length (or edebug-print-length print-length))
3426 (print-level (or edebug-print-level print-level))
3427 (print-circle (or edebug-print-circle print-circle))
3428 (print-readably nil)) ; lemacs uses this.
3429 (edebug-prin1-to-string value)))
3431 (defun edebug-compute-previous-result (previous-value)
3432 (if edebug-unwrap-results
3433 (setq previous-value
3434 (edebug-unwrap* previous-value)))
3435 (setq edebug-previous-result
3436 (concat "Result: "
3437 (edebug-safe-prin1-to-string previous-value)
3438 (eval-expression-print-format previous-value))))
3440 (defun edebug-previous-result ()
3441 "Print the previous result."
3442 (interactive)
3443 (message "%s" edebug-previous-result))
3445 ;;; Read, Eval and Print
3447 (defalias 'edebug-prin1-to-string #'cl-prin1-to-string)
3448 (defalias 'edebug-format #'format-message)
3449 (defalias 'edebug-message #'message)
3451 (defun edebug-eval-expression (expr)
3452 "Evaluate an expression in the outside environment.
3453 If interactive, prompt for the expression.
3454 Print result in minibuffer."
3455 (interactive (list (read-from-minibuffer
3456 "Eval: " nil read-expression-map t
3457 'read-expression-history)))
3458 (princ
3459 (edebug-outside-excursion
3460 (setq values (cons (edebug-eval expr) values))
3461 (concat (edebug-safe-prin1-to-string (car values))
3462 (eval-expression-print-format (car values))))))
3464 (defun edebug-eval-last-sexp ()
3465 "Evaluate sexp before point in the outside environment.
3466 Print value in minibuffer."
3467 (interactive)
3468 (edebug-eval-expression (edebug-last-sexp)))
3470 (defun edebug-eval-print-last-sexp ()
3471 "Evaluate sexp before point in outside environment; insert value.
3472 This prints the value into current buffer."
3473 (interactive)
3474 (let* ((form (edebug-last-sexp))
3475 (result-string
3476 (edebug-outside-excursion
3477 (edebug-safe-prin1-to-string (edebug-safe-eval form))))
3478 (standard-output (current-buffer)))
3479 (princ "\n")
3480 ;; princ the string to get rid of quotes.
3481 (princ result-string)
3482 (princ "\n")
3485 ;;; Edebug Minor Mode
3487 (defvar edebug-inhibit-emacs-lisp-mode-bindings nil
3488 "If non-nil, inhibit Edebug bindings on the C-x C-a key.
3489 By default, loading the `edebug' library causes these bindings to
3490 be installed in `emacs-lisp-mode-map'.")
3492 (define-obsolete-variable-alias 'gud-inhibit-global-bindings
3493 'edebug-inhibit-emacs-lisp-mode-bindings "24.3")
3495 ;; Global GUD bindings for all emacs-lisp-mode buffers.
3496 (unless edebug-inhibit-emacs-lisp-mode-bindings
3497 (define-key emacs-lisp-mode-map "\C-x\C-a\C-s" 'edebug-step-mode)
3498 (define-key emacs-lisp-mode-map "\C-x\C-a\C-n" 'edebug-next-mode)
3499 (define-key emacs-lisp-mode-map "\C-x\C-a\C-c" 'edebug-go-mode)
3500 (define-key emacs-lisp-mode-map "\C-x\C-a\C-l" 'edebug-where)
3501 ;; The following isn't a GUD binding.
3502 (define-key emacs-lisp-mode-map "\C-x\C-a\C-m" 'edebug-set-initial-mode))
3504 (defvar edebug-mode-map
3505 (let ((map (copy-keymap emacs-lisp-mode-map)))
3506 ;; control
3507 (define-key map " " 'edebug-step-mode)
3508 (define-key map "n" 'edebug-next-mode)
3509 (define-key map "g" 'edebug-go-mode)
3510 (define-key map "G" 'edebug-Go-nonstop-mode)
3511 (define-key map "t" 'edebug-trace-mode)
3512 (define-key map "T" 'edebug-Trace-fast-mode)
3513 (define-key map "c" 'edebug-continue-mode)
3514 (define-key map "C" 'edebug-Continue-fast-mode)
3516 ;;(define-key map "f" 'edebug-forward) not implemented
3517 (define-key map "f" 'edebug-forward-sexp)
3518 (define-key map "h" 'edebug-goto-here)
3520 (define-key map "I" 'edebug-instrument-callee)
3521 (define-key map "i" 'edebug-step-in)
3522 (define-key map "o" 'edebug-step-out)
3524 ;; quitting and stopping
3525 (define-key map "q" 'top-level)
3526 (define-key map "Q" 'edebug-top-level-nonstop)
3527 (define-key map "a" 'abort-recursive-edit)
3528 (define-key map "S" 'edebug-stop)
3530 ;; breakpoints
3531 (define-key map "b" 'edebug-set-breakpoint)
3532 (define-key map "u" 'edebug-unset-breakpoint)
3533 (define-key map "B" 'edebug-next-breakpoint)
3534 (define-key map "x" 'edebug-set-conditional-breakpoint)
3535 (define-key map "X" 'edebug-set-global-break-condition)
3537 ;; evaluation
3538 (define-key map "r" 'edebug-previous-result)
3539 (define-key map "e" 'edebug-eval-expression)
3540 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3541 (define-key map "E" 'edebug-visit-eval-list)
3543 ;; views
3544 (define-key map "w" 'edebug-where)
3545 (define-key map "v" 'edebug-view-outside) ;; maybe obsolete??
3546 (define-key map "p" 'edebug-bounce-point)
3547 (define-key map "P" 'edebug-view-outside) ;; same as v
3548 (define-key map "W" 'edebug-toggle-save-windows)
3550 ;; misc
3551 (define-key map "?" 'edebug-help)
3552 (define-key map "d" 'edebug-backtrace)
3554 (define-key map "-" 'negative-argument)
3556 ;; statistics
3557 (define-key map "=" 'edebug-temp-display-freq-count)
3559 ;; GUD bindings
3560 (define-key map "\C-c\C-s" 'edebug-step-mode)
3561 (define-key map "\C-c\C-n" 'edebug-next-mode)
3562 (define-key map "\C-c\C-c" 'edebug-go-mode)
3564 (define-key map "\C-x " 'edebug-set-breakpoint)
3565 (define-key map "\C-c\C-d" 'edebug-unset-breakpoint)
3566 (define-key map "\C-c\C-t"
3567 (lambda () (interactive) (edebug-set-breakpoint t)))
3568 (define-key map "\C-c\C-l" 'edebug-where)
3569 map))
3571 ;; Autoloading these global bindings doesn't make sense because
3572 ;; they cannot be used anyway unless Edebug is already loaded and active.
3574 (defvar global-edebug-prefix "\^XX"
3575 "Prefix key for global edebug commands, available from any buffer.")
3577 (defvar global-edebug-map
3578 (let ((map (make-sparse-keymap)))
3580 (define-key map " " 'edebug-step-mode)
3581 (define-key map "g" 'edebug-go-mode)
3582 (define-key map "G" 'edebug-Go-nonstop-mode)
3583 (define-key map "t" 'edebug-trace-mode)
3584 (define-key map "T" 'edebug-Trace-fast-mode)
3585 (define-key map "c" 'edebug-continue-mode)
3586 (define-key map "C" 'edebug-Continue-fast-mode)
3588 ;; breakpoints
3589 (define-key map "b" 'edebug-set-breakpoint)
3590 (define-key map "u" 'edebug-unset-breakpoint)
3591 (define-key map "x" 'edebug-set-conditional-breakpoint)
3592 (define-key map "X" 'edebug-set-global-break-condition)
3594 ;; views
3595 (define-key map "w" 'edebug-where)
3596 (define-key map "W" 'edebug-toggle-save-windows)
3598 ;; quitting
3599 (define-key map "q" 'top-level)
3600 (define-key map "Q" 'edebug-top-level-nonstop)
3601 (define-key map "a" 'abort-recursive-edit)
3603 ;; statistics
3604 (define-key map "=" 'edebug-display-freq-count)
3605 map)
3606 "Global map of edebug commands, available from any buffer.")
3608 (global-unset-key global-edebug-prefix)
3609 (global-set-key global-edebug-prefix global-edebug-map)
3612 (defun edebug-help ()
3613 "Describe `edebug-mode'."
3614 (interactive)
3615 (describe-function 'edebug-mode))
3617 (defvar edebug--mode-saved-vars nil)
3619 (define-minor-mode edebug-mode
3620 "Mode for Emacs Lisp buffers while in Edebug.
3622 In addition to all Emacs Lisp commands (except those that modify the
3623 buffer) there are local and global key bindings to several Edebug
3624 specific commands. E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
3625 in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3627 Also see bindings for the eval list buffer *edebug* in `edebug-eval-mode'.
3629 The edebug buffer commands:
3630 \\{edebug-mode-map}
3632 Global commands prefixed by `global-edebug-prefix':
3633 \\{global-edebug-map}
3635 Options:
3636 `edebug-setup-hook'
3637 `edebug-all-defs'
3638 `edebug-all-forms'
3639 `edebug-save-windows'
3640 `edebug-save-displayed-buffer-points'
3641 `edebug-initial-mode'
3642 `edebug-trace'
3643 `edebug-test-coverage'
3644 `edebug-continue-kbd-macro'
3645 `edebug-print-length'
3646 `edebug-print-level'
3647 `edebug-print-circle'
3648 `edebug-on-error'
3649 `edebug-on-quit'
3650 `edebug-on-signal'
3651 `edebug-unwrap-results'
3652 `edebug-global-break-condition'"
3653 :lighter " *Debugging*"
3654 :keymap edebug-mode-map
3655 ;; If the user kills the buffer in which edebug is currently active,
3656 ;; exit to top level, because the edebug command loop can't usefully
3657 ;; continue running in such a case.
3659 (if (not edebug-mode)
3660 (progn
3661 (while edebug--mode-saved-vars
3662 (let ((setting (pop edebug--mode-saved-vars)))
3663 (if (consp setting)
3664 (set (car setting) (cdr setting))
3665 (kill-local-variable setting))))
3666 (remove-hook 'kill-buffer-hook 'edebug-kill-buffer t))
3667 (pcase-dolist (`(,var . ,val) '((buffer-read-only . t)))
3668 (push
3669 (if (local-variable-p var) (cons var (symbol-value var)) var)
3670 edebug--mode-saved-vars)
3671 (set (make-local-variable var) val))
3672 ;; Append `edebug-kill-buffer' to the hook to avoid interfering with
3673 ;; other entries that are unguarded against deleted buffer.
3674 (add-hook 'kill-buffer-hook 'edebug-kill-buffer t t)))
3676 (defun edebug-kill-buffer ()
3677 "Used on `kill-buffer-hook' when Edebug is operating in a buffer of Lisp code."
3678 (run-with-timer 0 nil #'top-level))
3680 ;;; edebug eval list mode
3682 ;; A list of expressions and their evaluations is displayed in *edebug*.
3684 (defun edebug-eval-result-list ()
3685 "Return a list of evaluations of `edebug-eval-list'."
3686 ;; Assumes in outside environment.
3687 ;; Don't do any edebug things now.
3688 (let ((edebug-execution-mode 'Go-nonstop)
3689 (edebug-trace nil))
3690 (mapcar #'edebug-safe-eval edebug-eval-list)))
3692 (defun edebug-eval-display-list (eval-result-list)
3693 ;; Assumes edebug-eval-buffer exists.
3694 (let ((standard-output edebug-eval-buffer)
3695 (edebug-comment-line
3696 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
3697 (set-buffer edebug-eval-buffer)
3698 (erase-buffer)
3699 (dolist (exp edebug-eval-list)
3700 (prin1 exp) (terpri)
3701 (prin1 (pop eval-result-list)) (terpri)
3702 (princ edebug-comment-line))
3703 (edebug-pop-to-buffer edebug-eval-buffer)
3706 (defun edebug-create-eval-buffer ()
3707 (unless (and edebug-eval-buffer (buffer-name edebug-eval-buffer))
3708 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
3709 (edebug-eval-mode)))
3711 ;; Should generalize this to be callable outside of edebug
3712 ;; with calls in user functions, e.g. (edebug-eval-display)
3714 (defun edebug-eval-display (eval-result-list)
3715 "Display expressions and evaluations in EVAL-RESULT-LIST.
3716 It modifies the context by popping up the eval display."
3717 (when eval-result-list
3718 (edebug-create-eval-buffer)
3719 (edebug-eval-display-list eval-result-list)))
3721 (defun edebug-eval-redisplay ()
3722 "Redisplay eval list in outside environment.
3723 May only be called from within `edebug--recursive-edit'."
3724 (edebug-create-eval-buffer)
3725 (edebug-outside-excursion
3726 (edebug-eval-display-list (edebug-eval-result-list))
3729 (defun edebug-visit-eval-list ()
3730 "Switch to the evaluation list buffer \"*edebug*\"."
3731 (interactive)
3732 (edebug-eval-redisplay)
3733 (edebug-pop-to-buffer edebug-eval-buffer))
3736 (defun edebug-update-eval-list ()
3737 "Replace the evaluation list with the sexps now in the eval buffer."
3738 (interactive)
3739 (let ((starting-point (point))
3740 new-list)
3741 (goto-char (point-min))
3742 ;; get the first expression
3743 (edebug-skip-whitespace)
3744 (if (not (eobp))
3745 (progn
3746 (forward-sexp 1)
3747 (push (edebug-last-sexp) new-list)))
3749 (while (re-search-forward "^;" nil t)
3750 (forward-line 1)
3751 (skip-chars-forward " \t\n\r")
3752 (if (and (/= ?\; (following-char))
3753 (not (eobp)))
3754 (progn
3755 (forward-sexp 1)
3756 (push (edebug-last-sexp) new-list))))
3758 (setq edebug-eval-list (nreverse new-list))
3759 (edebug-eval-redisplay)
3760 (goto-char starting-point)))
3763 (defun edebug-delete-eval-item ()
3764 "Delete the item under point and redisplay."
3765 ;; could add arg to do repeatedly
3766 (interactive)
3767 (if (re-search-backward "^;" nil 'nofail)
3768 (forward-line 1))
3769 (delete-region
3770 (point) (progn (re-search-forward "^;" nil 'nofail)
3771 (beginning-of-line)
3772 (point)))
3773 (edebug-update-eval-list))
3777 (defvar edebug-eval-mode-map
3778 (let ((map (make-sparse-keymap)))
3779 (set-keymap-parent map lisp-interaction-mode-map)
3780 (define-key map "\C-c\C-w" 'edebug-where)
3781 (define-key map "\C-c\C-d" 'edebug-delete-eval-item)
3782 (define-key map "\C-c\C-u" 'edebug-update-eval-list)
3783 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3784 (define-key map "\C-j" 'edebug-eval-print-last-sexp)
3785 map)
3786 "Keymap for Edebug Eval mode. Superset of Lisp Interaction mode.")
3788 (put 'edebug-eval-mode 'mode-class 'special)
3790 (define-derived-mode edebug-eval-mode lisp-interaction-mode "Edebug Eval"
3791 "Mode for evaluation list buffer while in Edebug.
3793 In addition to all Interactive Emacs Lisp commands there are local and
3794 global key bindings to several Edebug specific commands. E.g.
3795 `edebug-step-mode' is bound to \\[edebug-step-mode] in the Edebug
3796 buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3798 Eval list buffer commands:
3799 \\{edebug-eval-mode-map}
3801 Global commands prefixed by `global-edebug-prefix':
3802 \\{global-edebug-map}")
3804 ;;; Interface with standard debugger.
3806 ;; (setq debugger 'edebug) ; to use the edebug debugger
3807 ;; (setq debugger 'debug) ; use the standard debugger
3809 ;; Note that debug and its utilities must be byte-compiled to work,
3810 ;; since they depend on the backtrace looking a certain way. But
3811 ;; edebug is not dependent on this, yet.
3813 (defun edebug (&optional arg-mode &rest args)
3814 "Replacement for `debug'.
3815 If we are running an edebugged function, show where we last were.
3816 Otherwise call `debug' normally."
3817 ;;(message "entered: %s depth: %s edebug-recursion-depth: %s"
3818 ;; edebug-entered (recursion-depth) edebug-recursion-depth) (sit-for 1)
3819 (if (and edebug-entered ; anything active?
3820 (eq (recursion-depth) edebug-recursion-depth))
3821 (let (;; Where were we before the error occurred?
3822 (offset-index (car edebug-offset-indices))
3823 (value (car args))
3824 ;; Bind variables required by edebug--display.
3825 edebug-breakpoints
3826 edebug-break-data
3827 edebug-break-condition
3828 edebug-global-break
3829 (edebug-break (null arg-mode)) ;; If called explicitly.
3831 (edebug--display value offset-index arg-mode)
3832 (if (eq arg-mode 'error)
3834 value))
3836 ;; Otherwise call debug normally.
3837 ;; Still need to remove extraneous edebug calls from stack.
3838 (apply #'debug arg-mode args)
3842 (defun edebug-backtrace ()
3843 "Display a non-working backtrace. Better than nothing..."
3844 (interactive)
3845 (if (or (not edebug-backtrace-buffer)
3846 (null (buffer-name edebug-backtrace-buffer)))
3847 (setq edebug-backtrace-buffer
3848 (generate-new-buffer "*Backtrace*"))
3849 ;; Else, could just display edebug-backtrace-buffer.
3851 (with-output-to-temp-buffer (buffer-name edebug-backtrace-buffer)
3852 (setq edebug-backtrace-buffer standard-output)
3853 (let ((print-escape-newlines t)
3854 (print-length 50) ; FIXME cf edebug-safe-prin1-to-string
3855 last-ok-point)
3856 (backtrace)
3858 ;; Clean up the backtrace.
3859 ;; Not quite right for current edebug scheme.
3860 (set-buffer edebug-backtrace-buffer)
3861 (setq truncate-lines t)
3862 (goto-char (point-min))
3863 (setq last-ok-point (point))
3864 (if t (progn
3866 ;; Delete interspersed edebug internals.
3867 (while (re-search-forward "^ (?edebug" nil t)
3868 (beginning-of-line)
3869 (cond
3870 ((looking-at "^ (edebug-after")
3871 ;; Previous lines may contain code, so just delete this line.
3872 (setq last-ok-point (point))
3873 (forward-line 1)
3874 (delete-region last-ok-point (point)))
3876 ((looking-at (if debugger-stack-frame-as-list
3877 "^ (edebug"
3878 "^ edebug"))
3879 (forward-line 1)
3880 (delete-region last-ok-point (point))
3882 )))))
3885 ;;; Trace display
3887 (defun edebug-trace-display (buf-name fmt &rest args)
3888 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
3889 The buffer is created if it does not exist.
3890 You must include newlines in FMT to break lines, but one newline is appended."
3891 ;; e.g.
3892 ;; (edebug-trace-display "*trace-point*"
3893 ;; "saving: point = %s window-start = %s"
3894 ;; (point) (window-start))
3895 (let* ((oldbuf (current-buffer))
3896 (selected-window (selected-window))
3897 (buffer (get-buffer-create buf-name))
3898 buf-window)
3899 ;; (message "before pop-to-buffer") (sit-for 1)
3900 (edebug-pop-to-buffer buffer)
3901 (setq truncate-lines t)
3902 (setq buf-window (selected-window))
3903 (goto-char (point-max))
3904 (insert (apply #'edebug-format fmt args) "\n")
3905 ;; Make it visible.
3906 (vertical-motion (- 1 (window-height)))
3907 (set-window-start buf-window (point))
3908 (goto-char (point-max))
3909 ;; (set-window-point buf-window (point))
3910 ;; (sit-for 0)
3911 (bury-buffer buffer)
3912 (select-window selected-window)
3913 (set-buffer oldbuf))
3914 buf-name)
3917 (defun edebug-trace (fmt &rest args)
3918 "Convenience call to `edebug-trace-display' using `edebug-trace-buffer'."
3919 (apply #'edebug-trace-display edebug-trace-buffer fmt args))
3922 ;;; Frequency count and coverage
3924 ;; FIXME should this use overlays instead?
3925 ;; Definitely, IMO. The current business with undo in
3926 ;; edebug-temp-display-freq-count is horrid.
3927 (defun edebug-display-freq-count ()
3928 "Display the frequency count data for each line of the current definition.
3929 The frequency counts are inserted as comment lines after each line,
3930 and you can undo all insertions with one `undo' command.
3932 The counts are inserted starting under the `(' before an expression
3933 or the `)' after an expression, or on the last char of a symbol.
3934 The counts are only displayed when they differ from previous counts on
3935 the same line.
3937 If coverage is being tested, whenever all known results of an expression
3938 are `eq', the char `=' will be appended after the count
3939 for that expression. Note that this is always the case for an
3940 expression only evaluated once.
3942 To clear the frequency count and coverage data for a definition,
3943 reinstrument it."
3944 (interactive)
3945 (let* ((function (edebug-form-data-symbol))
3946 (counts (get function 'edebug-freq-count))
3947 (coverages (get function 'edebug-coverage))
3948 (data (get function 'edebug))
3949 (def-mark (car data)) ; mark at def start
3950 (edebug-points (nth 2 data))
3951 (i (1- (length edebug-points)))
3952 (last-index)
3953 (first-index)
3954 (start-of-line)
3955 (start-of-count-line)
3956 (last-count)
3958 (save-excursion
3959 ;; Traverse in reverse order so offsets are correct.
3960 (while (<= 0 i)
3961 ;; Start at last expression in line.
3962 (goto-char (+ def-mark (aref edebug-points i)))
3963 (beginning-of-line)
3964 (setq start-of-line (- (point) def-mark)
3965 last-index i)
3967 ;; Find all indexes on same line.
3968 (while (and (<= 0 (setq i (1- i)))
3969 (<= start-of-line (aref edebug-points i))))
3970 ;; Insert all the indices for this line.
3971 (forward-line 1)
3972 (setq start-of-count-line (point)
3973 first-index i ; Really, last index for line above this one.
3974 last-count -1) ; Cause first count to always appear.
3975 (insert ";#")
3976 ;; i == first-index still
3977 (while (<= (setq i (1+ i)) last-index)
3978 (let ((count (aref counts i))
3979 (coverage (aref coverages i))
3980 (col (save-excursion
3981 (goto-char (+ (aref edebug-points i) def-mark))
3982 (- (current-column)
3983 (if (= ?\( (following-char)) 0 1)))))
3984 (insert (make-string
3985 (max 0 (- col (- (point) start-of-count-line))) ?\s)
3986 (if (and (< 0 count)
3987 (not (memq coverage
3988 '(unknown ok-coverage))))
3989 "=" "")
3990 (if (= count last-count) "" (int-to-string count))
3991 " ")
3992 (setq last-count count)))
3993 (insert "\n")
3994 (setq i first-index)))))
3996 ;; FIXME this does not work very well. Eg if you press an arrow key,
3997 ;; or make a mouse-click, it fails with "Non-character input-event".
3998 (defun edebug-temp-display-freq-count ()
3999 "Temporarily display the frequency count data for the current definition.
4000 It is removed when you hit any char."
4001 ;; This seems not to work with Emacs 18.59. It undoes too far.
4002 (interactive)
4003 (let ((inhibit-read-only t))
4004 (undo-boundary)
4005 (edebug-display-freq-count)
4006 (setq unread-command-events
4007 (append unread-command-events (list (read-event))))
4008 ;; Yuck! This doesn't seem to work at all for me.
4009 (undo)))
4012 ;;; Menus
4014 (defun edebug-toggle (variable)
4015 (set variable (not (symbol-value variable)))
4016 (message "%s: %s" variable (symbol-value variable)))
4018 ;; We have to require easymenu (even for Emacs 18) just so
4019 ;; the easy-menu-define macro call is compiled correctly.
4020 (require 'easymenu)
4022 (defconst edebug-mode-menus
4023 '("Edebug"
4024 ["Stop" edebug-stop t]
4025 ["Step" edebug-step-mode t]
4026 ["Next" edebug-next-mode t]
4027 ["Trace" edebug-trace-mode t]
4028 ["Trace Fast" edebug-Trace-fast-mode t]
4029 ["Continue" edebug-continue-mode t]
4030 ["Continue Fast" edebug-Continue-fast-mode t]
4031 ["Go" edebug-go-mode t]
4032 ["Go Nonstop" edebug-Go-nonstop-mode t]
4033 "----"
4034 ["Help" edebug-help t]
4035 ["Abort" abort-recursive-edit t]
4036 ["Quit to Top Level" top-level t]
4037 ["Quit Nonstop" edebug-top-level-nonstop t]
4038 "----"
4039 ("Jumps"
4040 ["Forward Sexp" edebug-forward-sexp t]
4041 ["Step In" edebug-step-in t]
4042 ["Step Out" edebug-step-out t]
4043 ["Goto Here" edebug-goto-here t])
4045 ("Breaks"
4046 ["Set Breakpoint" edebug-set-breakpoint t]
4047 ["Unset Breakpoint" edebug-unset-breakpoint t]
4048 ["Set Conditional Breakpoint" edebug-set-conditional-breakpoint t]
4049 ["Set Global Break Condition" edebug-set-global-break-condition t]
4050 ["Show Next Breakpoint" edebug-next-breakpoint t])
4052 ("Views"
4053 ["Where am I?" edebug-where t]
4054 ["Bounce to Current Point" edebug-bounce-point t]
4055 ["View Outside Windows" edebug-view-outside t]
4056 ["Previous Result" edebug-previous-result t]
4057 ["Show Backtrace" edebug-backtrace t]
4058 ["Display Freq Count" edebug-display-freq-count t])
4060 ("Eval"
4061 ["Expression" edebug-eval-expression t]
4062 ["Last Sexp" edebug-eval-last-sexp t]
4063 ["Visit Eval List" edebug-visit-eval-list t])
4065 ("Options"
4066 ["Edebug All Defs" edebug-all-defs
4067 :style toggle :selected edebug-all-defs]
4068 ["Edebug All Forms" edebug-all-forms
4069 :style toggle :selected edebug-all-forms]
4070 "----"
4071 ["Tracing" (edebug-toggle 'edebug-trace)
4072 :style toggle :selected edebug-trace]
4073 ["Test Coverage" (edebug-toggle 'edebug-test-coverage)
4074 :style toggle :selected edebug-test-coverage]
4075 ["Save Windows" edebug-toggle-save-windows
4076 :style toggle :selected edebug-save-windows]
4077 ["Save Point"
4078 (edebug-toggle 'edebug-save-displayed-buffer-points)
4079 :style toggle :selected edebug-save-displayed-buffer-points]
4081 "Menus for Edebug.")
4084 ;;; Emacs version specific code
4086 (defalias 'edebug-window-live-p 'window-live-p)
4088 (defun edebug-mark ()
4089 (mark t))
4091 (defun edebug-set-conditional-breakpoint (arg condition)
4092 "Set a conditional breakpoint at nearest sexp.
4093 The condition is evaluated in the outside context.
4094 With prefix argument, make it a temporary breakpoint."
4095 ;; (interactive "P\nxCondition: ")
4096 (interactive
4097 (list
4098 current-prefix-arg
4099 ;; Read condition as follows; getting previous condition is cumbersome:
4100 (let ((edebug-stop-point (edebug-find-stop-point)))
4101 (if edebug-stop-point
4102 (let* ((edebug-def-name (car edebug-stop-point))
4103 (index (cdr edebug-stop-point))
4104 (edebug-data (get edebug-def-name 'edebug))
4105 (edebug-breakpoints (car (cdr edebug-data)))
4106 (edebug-break-data (assq index edebug-breakpoints))
4107 (edebug-break-condition (car (cdr edebug-break-data)))
4108 (initial (and edebug-break-condition
4109 (format "%s" edebug-break-condition))))
4110 (read-from-minibuffer
4111 "Condition: " initial read-expression-map t
4112 (if (equal (car read-expression-history) initial)
4113 '(read-expression-history . 1)
4114 'read-expression-history)))))))
4115 (edebug-modify-breakpoint t condition arg))
4117 (easy-menu-define edebug-menu edebug-mode-map "Edebug menus" edebug-mode-menus)
4119 ;;; Autoloading of Edebug accessories
4121 ;; edebug-cl-read and cl-read are available from liberte@cs.uiuc.edu
4122 (defun edebug--require-cl-read ()
4123 (require 'edebug-cl-read))
4125 (if (featurep 'cl-read)
4126 (add-hook 'edebug-setup-hook #'edebug--require-cl-read)
4127 ;; The following causes edebug-cl-read to be loaded when you load cl-read.el.
4128 (add-hook 'cl-read-load-hooks #'edebug--require-cl-read))
4131 ;;; Finalize Loading
4133 ;; When edebugging a function, some of the sub-expressions are
4134 ;; wrapped in (edebug-enter (lambda () ..)), so we need to teach
4135 ;; called-interactively-p that calls within the inner lambda should refer to
4136 ;; the outside function.
4137 (add-hook 'called-interactively-p-functions
4138 #'edebug--called-interactively-skip)
4139 (defun edebug--called-interactively-skip (i frame1 frame2)
4140 (when (and (eq (car-safe (nth 1 frame1)) 'lambda)
4141 (eq (nth 1 (nth 1 frame1)) '())
4142 (eq (nth 1 frame2) 'edebug-enter))
4143 ;; `edebug-enter' calls itself on its first invocation.
4144 (if (eq (nth 1 (backtrace-frame i 'called-interactively-p))
4145 'edebug-enter)
4146 2 1)))
4148 ;; Finally, hook edebug into the rest of Emacs.
4149 ;; There are probably some other things that could go here.
4151 ;; Install edebug read and eval functions.
4152 (edebug-install-read-eval-functions)
4154 (defun edebug-unload-function ()
4155 "Unload the Edebug source level debugger."
4156 (when edebug-active
4157 (setq edebug-active nil)
4158 (unwind-protect
4159 (abort-recursive-edit)
4160 ;; We still want to run unload-feature to completion
4161 (run-with-idle-timer 0 nil #'(lambda () (unload-feature 'edebug)))))
4162 (remove-hook 'called-interactively-p-functions
4163 'edebug--called-interactively-skip)
4164 (remove-hook 'cl-read-load-hooks 'edebug--require-cl-read)
4165 (edebug-uninstall-read-eval-functions)
4166 ;; Continue standard unloading.
4167 nil)
4169 (provide 'edebug)
4170 ;;; edebug.el ends here