Merge branch 'master' into comment-cache
[emacs.git] / lisp / emacs-lisp / edebug.el
blobec0f08de35626a523e16f26cc689decdf553a217
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 <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This minor mode allows programmers to step through Emacs Lisp
28 ;; source code while executing functions. You can also set
29 ;; breakpoints, trace (stopping at each expression), evaluate
30 ;; expressions as if outside Edebug, reevaluate and display a list of
31 ;; expressions, trap errors normally caught by debug, and display a
32 ;; debug style backtrace.
34 ;;; Minimal Instructions
35 ;; =====================
37 ;; First evaluate a defun with C-M-x, then run the function. Step
38 ;; through the code with SPC, mark breakpoints with b, go until a
39 ;; breakpoint is reached with g, and quit execution with q. Use the
40 ;; "?" command in edebug to describe other commands.
41 ;; See the Emacs Lisp Reference Manual for more details.
43 ;; If you wish to change the default edebug global command prefix, change:
44 ;; (setq edebug-global-prefix "\C-xX")
46 ;; Edebug was written by
47 ;; Daniel LaLiberte
48 ;; GTE Labs
49 ;; 40 Sylvan Rd
50 ;; Waltham, MA 02254
51 ;; liberte@holonexus.org
53 ;;; Code:
55 (require 'macroexp)
56 (require 'cl-lib)
57 (eval-when-compile (require 'pcase))
59 ;;; Options
61 (defgroup edebug nil
62 "A source-level debugger for Emacs Lisp."
63 :group 'lisp)
66 (defcustom edebug-setup-hook nil
67 "Functions to call before edebug is used.
68 Each time it is set to a new value, Edebug will call those functions
69 once and then reset `edebug-setup-hook' to nil. You could use this
70 to load up Edebug specifications associated with a package you are
71 using, but only when you also use Edebug."
72 :type 'hook
73 :group 'edebug)
75 ;; edebug-all-defs and edebug-all-forms need to be autoloaded
76 ;; because the byte compiler binds them; as a result, if edebug
77 ;; is first loaded for a require in a compilation, they will be left unbound.
79 ;;;###autoload
80 (defcustom edebug-all-defs nil
81 "If non-nil, evaluating defining forms instruments for Edebug.
82 This applies to `eval-defun', `eval-region', `eval-buffer', and
83 `eval-current-buffer'. `eval-region' is also called by
84 `eval-last-sexp', and `eval-print-last-sexp'.
86 You can use the command `edebug-all-defs' to toggle the value of this
87 variable. You may wish to make it local to each buffer with
88 \(make-local-variable \\='edebug-all-defs) in your
89 `emacs-lisp-mode-hook'."
90 :type 'boolean
91 :group 'edebug)
93 ;; edebug-all-defs and edebug-all-forms need to be autoloaded
94 ;; because the byte compiler binds them; as a result, if edebug
95 ;; is first loaded for a require in a compilation, they will be left unbound.
97 ;;;###autoload
98 (defcustom edebug-all-forms nil
99 "Non-nil means evaluation of all forms will instrument for Edebug.
100 This doesn't apply to loading or evaluations in the minibuffer.
101 Use the command `edebug-all-forms' to toggle the value of this option."
102 :type 'boolean
103 :group 'edebug)
105 (defcustom edebug-eval-macro-args nil
106 "Non-nil means all macro call arguments may be evaluated.
107 If this variable is nil, the default, Edebug will *not* wrap
108 macro call arguments as if they will be evaluated.
109 For each macro, an `edebug-form-spec' overrides this option.
110 So to specify exceptions for macros that have some arguments evaluated
111 and some not, use `def-edebug-spec' to specify an `edebug-form-spec'."
112 :type 'boolean
113 :group 'edebug)
115 (defcustom edebug-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 (function (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 (function
415 (lambda (one-window-info)
416 (if one-window-info
417 (apply (function
418 (lambda (window buffer point start hscroll)
419 (if (edebug-window-live-p window)
420 (progn
421 (set-window-buffer window buffer)
422 (set-window-point window point)
423 (set-window-start window start)
424 (set-window-hscroll window hscroll)))))
425 one-window-info))))
426 window-info)
427 (set-window-configuration window-info)))
429 ;;; Redefine read and eval functions
430 ;; read is redefined to maybe instrument forms.
431 ;; eval-defun is redefined to check edebug-all-forms and edebug-all-defs.
433 (defun edebug--read (orig &optional stream)
434 "Read one Lisp expression as text from STREAM, return as Lisp object.
435 If STREAM is nil, use the value of `standard-input' (which see).
436 STREAM or the value of `standard-input' may be:
437 a buffer (read from point and advance it)
438 a marker (read from where it points and advance it)
439 a function (call it with no arguments for each character,
440 call it with a char as argument to push a char back)
441 a string (takes text from string, starting at the beginning)
442 t (read text line using minibuffer and use it).
444 This version, from Edebug, maybe instruments the expression. But the
445 STREAM must be the current buffer to do so. Whether it instruments is
446 also dependent on the values of the option `edebug-all-defs' and
447 the option `edebug-all-forms'."
448 (or stream (setq stream standard-input))
449 (if (eq stream (current-buffer))
450 (edebug-read-and-maybe-wrap-form)
451 (funcall (or orig #'read) stream)))
453 (defvar edebug-result) ; The result of the function call returned by body.
455 ;; We should somehow arrange to be able to do this
456 ;; without actually replacing the eval-defun command.
457 (defun edebug-eval-defun (edebug-it)
458 "Evaluate the top-level form containing point, or after point.
460 If the current defun is actually a call to `defvar', then reset the
461 variable using its initial value expression even if the variable
462 already has some other value. (Normally `defvar' does not change the
463 variable's value if it already has a value.) Treat `defcustom'
464 similarly. Reinitialize the face according to `defface' specification.
466 With a prefix argument, instrument the code for Edebug.
468 Setting option `edebug-all-defs' to a non-nil value reverses the meaning
469 of the prefix argument. Code is then instrumented when this function is
470 invoked without a prefix argument.
472 If acting on a `defun' for FUNCTION, and the function was instrumented,
473 `Edebug: FUNCTION' is printed in the minibuffer. If not instrumented,
474 just FUNCTION is printed.
476 If not acting on a `defun', the result of evaluation is displayed in
477 the minibuffer."
478 (interactive "P")
479 (let* ((edebugging (not (eq (not edebug-it) (not edebug-all-defs))))
480 (edebug-result)
481 (form
482 (let ((edebug-all-forms edebugging)
483 (edebug-all-defs (eq edebug-all-defs (not edebug-it))))
484 (edebug-read-top-level-form))))
485 ;; This should be consistent with `eval-defun-1', but not the
486 ;; same, since that gets a macroexpanded form.
487 (cond ((and (eq (car form) 'defvar)
488 (cdr-safe (cdr-safe form)))
489 ;; Force variable to be bound.
490 (makunbound (nth 1 form)))
491 ((and (eq (car form) 'defcustom)
492 (default-boundp (nth 1 form)))
493 ;; Force variable to be bound.
494 ;; FIXME: Shouldn't this use the :setter or :initializer?
495 (set-default (nth 1 form) (eval (nth 2 form) lexical-binding)))
496 ((eq (car form) 'defface)
497 ;; Reset the face.
498 (setq face-new-frame-defaults
499 (assq-delete-all (nth 1 form) face-new-frame-defaults))
500 (put (nth 1 form) 'face-defface-spec nil)
501 (put (nth 1 form) 'face-documentation (nth 3 form))
502 ;; See comments in `eval-defun-1' for purpose of code below
503 (setq form (prog1 `(prog1 ,form
504 (put ',(nth 1 form) 'saved-face
505 ',(get (nth 1 form) 'saved-face))
506 (put ',(nth 1 form) 'customized-face
507 ,(nth 2 form)))
508 (put (nth 1 form) 'saved-face nil)))))
509 (setq edebug-result (eval (eval-sexp-add-defvars form) lexical-binding))
510 (if (not edebugging)
511 (prog1
512 (prin1 edebug-result)
513 (let ((str (eval-expression-print-format edebug-result)))
514 (if str (princ str))))
515 edebug-result)))
518 ;;;###autoload
519 (defalias 'edebug-defun 'edebug-eval-top-level-form)
521 ;;;###autoload
522 (defun edebug-eval-top-level-form ()
523 "Evaluate the top level form point is in, stepping through with Edebug.
524 This is like `eval-defun' except that it steps the code for Edebug
525 before evaluating it. It displays the value in the echo area
526 using `eval-expression' (which see).
528 If you do this on a function definition such as a defun or defmacro,
529 it defines the function and instruments its definition for Edebug,
530 so it will do Edebug stepping when called later. It displays
531 `Edebug: FUNCTION' in the echo area to indicate that FUNCTION is now
532 instrumented for Edebug.
534 If the current defun is actually a call to `defvar' or `defcustom',
535 evaluating it this way resets the variable using its initial value
536 expression even if the variable already has some other value.
537 \(Normally `defvar' and `defcustom' do not alter the value if there
538 already is one.)"
539 (interactive)
540 (eval-expression
541 ;; Bind edebug-all-forms only while reading, not while evalling
542 ;; but this causes problems while edebugging edebug.
543 (let ((edebug-all-forms t)
544 (edebug-all-defs t))
545 (eval-sexp-add-defvars
546 (edebug-read-top-level-form)))))
549 (defun edebug-read-top-level-form ()
550 (let ((starting-point (point)))
551 (end-of-defun)
552 (beginning-of-defun)
553 (prog1
554 (edebug-read-and-maybe-wrap-form)
555 ;; Recover point, but only if no error occurred.
556 (goto-char starting-point))))
559 ;; Compatibility with old versions.
560 (defalias 'edebug-all-defuns 'edebug-all-defs)
562 ;;;###autoload
563 (defun edebug-all-defs ()
564 "Toggle edebugging of all definitions."
565 (interactive)
566 (setq edebug-all-defs (not edebug-all-defs))
567 (message "Edebugging all definitions is %s."
568 (if edebug-all-defs "on" "off")))
571 ;;;###autoload
572 (defun edebug-all-forms ()
573 "Toggle edebugging of all forms."
574 (interactive)
575 (setq edebug-all-forms (not edebug-all-forms))
576 (message "Edebugging all forms is %s."
577 (if edebug-all-forms "on" "off")))
580 (defun edebug-install-read-eval-functions ()
581 (interactive)
582 (add-function :around load-read-function #'edebug--read)
583 (advice-add 'eval-defun :override #'edebug-eval-defun))
585 (defun edebug-uninstall-read-eval-functions ()
586 (interactive)
587 (remove-function load-read-function #'edebug--read)
588 (advice-remove 'eval-defun 'edebug-eval-defun))
590 ;;; Edebug internal data
592 ;; The internal data that is needed for edebugging is kept in the
593 ;; buffer-local variable `edebug-form-data'.
595 (defvar-local edebug-form-data nil
596 "A list of entries associating symbols with buffer regions.
597 Each entry is an `edebug--form-data' struct with fields:
598 SYMBOL, BEGIN-MARKER, and END-MARKER. The markers
599 are at the beginning and end of an entry level form and SYMBOL is
600 a symbol that holds all edebug related information for the form on its
601 property list.
603 In the future (haha!), the symbol will be irrelevant and edebug data will
604 be stored in the definitions themselves rather than in the property
605 list of a symbol.")
607 (cl-defstruct (edebug--form-data
608 ;; Some callers expect accessors to return nil when passed nil.
609 (:type list)
610 (:constructor edebug--make-form-data-entry (name begin end))
611 (:predicate nil) (:constructor nil) (:copier nil))
612 name begin end)
614 (defsubst edebug-set-form-data-entry (entry name begin end)
615 (setf (edebug--form-data-name entry) name) ;; In case name is changed.
616 (set-marker (edebug--form-data-begin entry) begin)
617 (set-marker (edebug--form-data-end entry) end))
619 (defun edebug-get-form-data-entry (pnt &optional end-point)
620 ;; Find the edebug form data entry which is closest to PNT.
621 ;; If END-POINT is supplied, match must be exact.
622 ;; Return nil if none found.
623 (let ((rest edebug-form-data)
624 closest-entry
625 (closest-dist 999999)) ;; Need maxint here.
626 (while (and rest (< 0 closest-dist))
627 (let* ((entry (car rest))
628 (begin (edebug--form-data-begin entry))
629 (dist (- pnt begin)))
630 (setq rest (cdr rest))
631 (if (and (<= 0 dist)
632 (< dist closest-dist)
633 (or (not end-point)
634 (= end-point (edebug--form-data-end entry)))
635 (<= pnt (edebug--form-data-end entry)))
636 (setq closest-dist dist
637 closest-entry entry))))
638 closest-entry))
640 ;; Also need to find all contained entries,
641 ;; and find an entry given a symbol, which should be just assq.
643 (defun edebug-form-data-symbol ()
644 "Return the edebug data symbol of the form where point is in.
645 If point is not inside a edebuggable form, cause error."
646 (or (edebug--form-data-name (edebug-get-form-data-entry (point)))
647 (error "Not inside instrumented form")))
649 (defun edebug-make-top-form-data-entry (new-entry)
650 ;; Make NEW-ENTRY the first element in the `edebug-form-data' list.
651 (edebug-clear-form-data-entry new-entry)
652 (push new-entry edebug-form-data))
654 (defun edebug-clear-form-data-entry (entry)
655 "If non-nil, clear ENTRY out of the form data.
656 Maybe clear the markers and delete the symbol's edebug property?"
657 (if entry
658 (progn
659 ;; Instead of this, we could just find all contained forms.
660 ;; (put (car entry) 'edebug nil) ;
661 ;; (mapcar 'edebug-clear-form-data-entry ; dangerous
662 ;; (get (car entry) 'edebug-dependents))
663 ;; (set-marker (nth 1 entry) nil)
664 ;; (set-marker (nth 2 entry) nil)
665 (setq edebug-form-data (delq entry edebug-form-data)))))
667 ;;; Parser utilities
669 (defun edebug-syntax-error (&rest args)
670 ;; Signal an invalid-read-syntax with ARGS.
671 (signal 'invalid-read-syntax args))
674 (defconst edebug-read-syntax-table
675 ;; Lookup table for significant characters indicating the class of the
676 ;; token that follows. This is not a \"real\" syntax table.
677 (let ((table (make-char-table 'syntax-table 'symbol))
678 (i 0))
679 (while (< i ?!)
680 (aset table i 'space)
681 (setq i (1+ i)))
682 (aset table ?\( 'lparen)
683 (aset table ?\) 'rparen)
684 (aset table ?\' 'quote)
685 (aset table ?\` 'backquote)
686 (aset table ?\, 'comma)
687 (aset table ?\" 'string)
688 (aset table ?\? 'char)
689 (aset table ?\[ 'lbracket)
690 (aset table ?\] 'rbracket)
691 (aset table ?\. 'dot)
692 (aset table ?\# 'hash)
693 ;; We treat numbers as symbols, because of confusion with -, -1, and 1-.
694 ;; We don't care about any other chars since they won't be seen.
695 table))
697 (defun edebug-next-token-class ()
698 ;; Move to the next token and return its class. We only care about
699 ;; lparen, rparen, dot, quote, backquote, comma, string, char, vector,
700 ;; or symbol.
701 (edebug-skip-whitespace)
702 (if (and (eq (following-char) ?.)
703 (save-excursion
704 (forward-char 1)
705 (or (and (eq (aref edebug-read-syntax-table (following-char))
706 'symbol)
707 (not (= (following-char) ?\;)))
708 (memq (following-char) '(?\, ?\.)))))
709 'symbol
710 (aref edebug-read-syntax-table (following-char))))
713 (defun edebug-skip-whitespace ()
714 ;; Leave point before the next token, skipping white space and comments.
715 (skip-chars-forward " \t\r\n\f")
716 (while (= (following-char) ?\;)
717 (skip-chars-forward "^\n") ; skip the comment
718 (skip-chars-forward " \t\r\n\f")))
721 ;; Mostly obsolete reader; still used in one case.
723 (defun edebug-read-sexp ()
724 ;; Read one sexp from the current buffer starting at point.
725 ;; Leave point immediately after it. A sexp can be a list or atom.
726 ;; An atom is a symbol (or number), character, string, or vector.
727 ;; This works for reading anything legitimate, but it
728 ;; is gummed up by parser inconsistencies (bugs?)
729 (let ((class (edebug-next-token-class)))
730 (cond
731 ;; read goes one too far if a (possibly quoted) string or symbol
732 ;; is immediately followed by non-whitespace.
733 ((eq class 'symbol) (read (current-buffer)))
734 ((eq class 'string) (read (current-buffer)))
735 ((eq class 'quote) (forward-char 1)
736 (list 'quote (edebug-read-sexp)))
737 ((eq class 'backquote)
738 (list '\` (edebug-read-sexp)))
739 ((eq class 'comma)
740 (list '\, (edebug-read-sexp)))
741 (t ; anything else, just read it.
742 (read (current-buffer))))))
744 ;;; Offsets for reader
746 ;; Define a structure to represent offset positions of expressions.
747 ;; Each offset structure looks like: (before . after) for constituents,
748 ;; or for structures that have elements: (before <subexpressions> . after)
749 ;; where the <subexpressions> are the offset structures for subexpressions
750 ;; including the head of a list.
751 (defvar edebug-offsets nil)
753 ;; Stack of offset structures in reverse order of the nesting.
754 ;; This is used to get back to previous levels.
755 (defvar edebug-offsets-stack nil)
756 (defvar edebug-current-offset nil) ; Top of the stack, for convenience.
758 ;; We must store whether we just read a list with a dotted form that
759 ;; is itself a list. This structure will be condensed, so the offsets
760 ;; must also be condensed.
761 (defvar edebug-read-dotted-list nil)
763 (defsubst edebug-initialize-offsets ()
764 ;; Reinitialize offset recording.
765 (setq edebug-current-offset nil))
767 (defun edebug-store-before-offset (point)
768 ;; Add a new offset pair with POINT as the before offset.
769 (let ((new-offset (list point)))
770 (if edebug-current-offset
771 (setcdr edebug-current-offset
772 (cons new-offset (cdr edebug-current-offset)))
773 ;; Otherwise, we are at the top level, so initialize.
774 (setq edebug-offsets new-offset
775 edebug-offsets-stack nil
776 edebug-read-dotted-list nil))
777 ;; Cons the new offset to the front of the stack.
778 (setq edebug-offsets-stack (cons new-offset edebug-offsets-stack)
779 edebug-current-offset new-offset)
782 (defun edebug-store-after-offset (point)
783 ;; Finalize the current offset struct by reversing it and
784 ;; store POINT as the after offset.
785 (if (not edebug-read-dotted-list)
786 ;; Just reverse the offsets of all subexpressions.
787 (setcdr edebug-current-offset (nreverse (cdr edebug-current-offset)))
789 ;; We just read a list after a dot, which will be abbreviated out.
790 (setq edebug-read-dotted-list nil)
791 ;; Drop the corresponding offset pair.
792 ;; That is, nconc the reverse of the rest of the offsets
793 ;; with the cdr of last offset.
794 (setcdr edebug-current-offset
795 (nconc (nreverse (cdr (cdr edebug-current-offset)))
796 (cdr (car (cdr edebug-current-offset))))))
798 ;; Now append the point using nconc.
799 (setq edebug-current-offset (nconc edebug-current-offset point))
800 ;; Pop the stack.
801 (setq edebug-offsets-stack (cdr edebug-offsets-stack)
802 edebug-current-offset (car edebug-offsets-stack)))
804 (defun edebug-ignore-offset ()
805 ;; Ignore the last created offset pair.
806 (setcdr edebug-current-offset (cdr (cdr edebug-current-offset))))
808 (defmacro edebug-storing-offsets (point &rest body)
809 (declare (debug (form body)) (indent 1))
810 `(unwind-protect
811 (progn
812 (edebug-store-before-offset ,point)
813 ,@body)
814 (edebug-store-after-offset (point))))
817 ;;; Reader for Emacs Lisp.
819 ;; Uses edebug-next-token-class (and edebug-skip-whitespace) above.
821 (defconst edebug-read-alist
822 '((symbol . edebug-read-symbol)
823 (lparen . edebug-read-list)
824 (string . edebug-read-string)
825 (quote . edebug-read-quote)
826 (backquote . edebug-read-backquote)
827 (comma . edebug-read-comma)
828 (lbracket . edebug-read-vector)
829 (hash . edebug-read-function)
832 (defun edebug-read-storing-offsets (stream)
833 (let (edebug-read-dotted-list) ; see edebug-store-after-offset
834 (edebug-storing-offsets (point)
835 (funcall
836 (or (cdr (assq (edebug-next-token-class) edebug-read-alist))
837 ;; anything else, just read it.
838 #'read)
839 stream))))
841 (defalias 'edebug-read-symbol #'read)
842 (defalias 'edebug-read-string #'read)
844 (defun edebug-read-quote (stream)
845 ;; Turn 'thing into (quote thing)
846 (forward-char 1)
847 (list
848 (edebug-storing-offsets (1- (point)) 'quote)
849 (edebug-read-storing-offsets stream)))
851 (defun edebug-read-backquote (stream)
852 ;; Turn `thing into (\` thing)
853 (forward-char 1)
854 (list
855 (edebug-storing-offsets (1- (point)) '\`)
856 (edebug-read-storing-offsets stream)))
858 (defun edebug-read-comma (stream)
859 ;; Turn ,thing into (\, thing). Handle ,@ and ,. also.
860 (let ((opoint (point)))
861 (forward-char 1)
862 (let ((symbol '\,))
863 (cond ((eq (following-char) ?\.)
864 (setq symbol '\,\.)
865 (forward-char 1))
866 ((eq (following-char) ?\@)
867 (setq symbol '\,@)
868 (forward-char 1)))
869 ;; Generate the same structure of offsets we would have
870 ;; if the resulting list appeared verbatim in the input text.
871 (list
872 (edebug-storing-offsets opoint symbol)
873 (edebug-read-storing-offsets stream)))))
875 (defun edebug-read-function (stream)
876 ;; Turn #'thing into (function thing)
877 (forward-char 1)
878 (cond ((eq ?\' (following-char))
879 (forward-char 1)
880 (list
881 (edebug-storing-offsets (- (point) 2) 'function)
882 (edebug-read-storing-offsets stream)))
883 ((memq (following-char) '(?: ?B ?O ?X ?b ?o ?x ?1 ?2 ?3 ?4 ?5 ?6
884 ?7 ?8 ?9 ?0))
885 (backward-char 1)
886 (read stream))
887 (t (edebug-syntax-error "Bad char after #"))))
889 (defun edebug-read-list (stream)
890 (forward-char 1) ; skip \(
891 (prog1
892 (let ((elements))
893 (while (not (memq (edebug-next-token-class) '(rparen dot)))
894 (push (edebug-read-storing-offsets stream) elements))
895 (setq elements (nreverse elements))
896 (if (eq 'dot (edebug-next-token-class))
897 (let (dotted-form)
898 (forward-char 1) ; skip \.
899 (setq dotted-form (edebug-read-storing-offsets stream))
900 elements (nconc elements dotted-form)
901 (if (not (eq (edebug-next-token-class) 'rparen))
902 (edebug-syntax-error "Expected `)'"))
903 (setq edebug-read-dotted-list (listp dotted-form))
905 elements)
906 (forward-char 1) ; skip \)
909 (defun edebug-read-vector (stream)
910 (forward-char 1) ; skip \[
911 (prog1
912 (let ((elements))
913 (while (not (eq 'rbracket (edebug-next-token-class)))
914 (push (edebug-read-storing-offsets stream) elements))
915 (apply 'vector (nreverse elements)))
916 (forward-char 1) ; skip \]
919 ;;; Cursors for traversal of list and vector elements with offsets.
921 (defvar edebug-dotted-spec nil)
923 (defun edebug-new-cursor (expressions offsets)
924 ;; Return a new cursor for EXPRESSIONS with OFFSETS.
925 (if (vectorp expressions)
926 (setq expressions (append expressions nil)))
927 (cons expressions offsets))
929 (defsubst edebug-set-cursor (cursor expressions offsets)
930 ;; Set the CURSOR's EXPRESSIONS and OFFSETS to the given.
931 ;; Return the cursor.
932 (setcar cursor expressions)
933 (setcdr cursor offsets)
934 cursor)
936 (defun edebug-copy-cursor (cursor)
937 ;; Copy the cursor using the same object and offsets.
938 (cons (car cursor) (cdr cursor)))
940 (defsubst edebug-cursor-expressions (cursor)
941 (car cursor))
942 (defsubst edebug-cursor-offsets (cursor)
943 (cdr cursor))
945 (defsubst edebug-empty-cursor (cursor)
946 ;; Return non-nil if CURSOR is empty - meaning no more elements.
947 (null (car cursor)))
949 (defsubst edebug-top-element (cursor)
950 ;; Return the top element at the cursor.
951 ;; Assumes not empty.
952 (car (car cursor)))
954 (defun edebug-top-element-required (cursor &rest error)
955 ;; Check if a dotted form is required.
956 (if edebug-dotted-spec (edebug-no-match cursor "Dot expected."))
957 ;; Check if there is at least one more argument.
958 (if (edebug-empty-cursor cursor) (apply 'edebug-no-match cursor error))
959 ;; Return that top element.
960 (edebug-top-element cursor))
962 (defsubst edebug-top-offset (cursor)
963 ;; Return the top offset pair corresponding to the top element.
964 (car (cdr cursor)))
966 (defun edebug-move-cursor (cursor)
967 ;; Advance and return the cursor to the next element and offset.
968 ;; throw no-match if empty before moving.
969 ;; This is a violation of the cursor encapsulation, but
970 ;; there is plenty of that going on while matching.
971 ;; The following test should always fail.
972 (if (edebug-empty-cursor cursor)
973 (edebug-no-match cursor "Not enough arguments."))
974 (setcar cursor (cdr (car cursor)))
975 (setcdr cursor (cdr (cdr cursor)))
976 cursor)
979 (defun edebug-before-offset (cursor)
980 ;; Return the before offset of the cursor.
981 ;; If there is nothing left in the offsets,
982 ;; return one less than the offset itself,
983 ;; which is the after offset for a list.
984 (let ((offset (edebug-cursor-offsets cursor)))
985 (if (consp offset)
986 (car (car offset))
987 (1- offset))))
989 (defun edebug-after-offset (cursor)
990 ;; Return the after offset of the cursor object.
991 (let ((offset (edebug-top-offset cursor)))
992 (while (consp offset)
993 (setq offset (cdr offset)))
994 offset))
996 ;;; The Parser
998 ;; The top level function for parsing forms is
999 ;; edebug-read-and-maybe-wrap-form; it calls all the rest. It checks the
1000 ;; syntax a bit and leaves point at any error it finds, but otherwise
1001 ;; should appear to work like eval-defun.
1003 ;; The basic plan is to surround each expression with a call to
1004 ;; the edebug debugger together with indexes into a table of positions of
1005 ;; all expressions. Thus an expression "exp" becomes:
1007 ;; (edebug-after (edebug-before 1) 2 exp)
1009 ;; When this is evaluated, first point is moved to the beginning of
1010 ;; exp at offset 1 of the current function. The expression is
1011 ;; evaluated, which may cause more edebug calls, and then point is
1012 ;; moved to offset 2 after the end of exp.
1014 ;; The highest level expressions of the function are wrapped in a call to
1015 ;; edebug-enter, which supplies the function name and the actual
1016 ;; arguments to the function. See functions edebug-enter, edebug-before,
1017 ;; and edebug-after for more details.
1019 ;; Dynamically bound vars, left unbound, but globally declared.
1020 ;; This is to quiet the byte compiler.
1022 ;; Window data of the highest definition being wrapped.
1023 ;; This data is shared by all embedded definitions.
1024 (defvar edebug-top-window-data)
1026 (defvar edebug-&optional)
1027 (defvar edebug-&rest)
1028 (defvar edebug-gate nil) ;; whether no-match forces an error.
1030 (defvar edebug-def-name nil) ; name of definition, used by interactive-form
1031 (defvar edebug-old-def-name nil) ; previous name of containing definition.
1033 (defvar edebug-error-point nil)
1034 (defvar edebug-best-error nil)
1037 (defun edebug-read-and-maybe-wrap-form ()
1038 ;; Read a form and wrap it with edebug calls, if the conditions are right.
1039 ;; Here we just catch any no-match not caught below and signal an error.
1041 ;; Run the setup hook.
1042 ;; If it gets an error, make it nil.
1043 (let ((temp-hook edebug-setup-hook))
1044 (setq edebug-setup-hook nil)
1045 (if (functionp temp-hook) (funcall temp-hook)
1046 (mapc #'funcall temp-hook)))
1048 (let (result
1049 edebug-top-window-data
1050 edebug-def-name;; make sure it is locally nil
1051 ;; I don't like these here!!
1052 edebug-&optional
1053 edebug-&rest
1054 edebug-gate
1055 edebug-best-error
1056 edebug-error-point
1057 ;; Do this once here instead of several times.
1058 (max-lisp-eval-depth (+ 800 max-lisp-eval-depth))
1059 (max-specpdl-size (+ 2000 max-specpdl-size)))
1060 (let ((no-match
1061 (catch 'no-match
1062 (setq result (edebug-read-and-maybe-wrap-form1))
1063 nil)))
1064 (if no-match
1065 (apply 'edebug-syntax-error no-match)))
1066 result))
1069 (defun edebug-read-and-maybe-wrap-form1 ()
1070 (let (spec
1071 def-kind
1072 defining-form-p
1073 def-name
1074 ;; These offset things don't belong here, but to support recursive
1075 ;; calls to edebug-read, they need to be here.
1076 edebug-offsets
1077 edebug-offsets-stack
1078 edebug-current-offset ; reset to nil
1080 (save-excursion
1081 (if (and (eq 'lparen (edebug-next-token-class))
1082 (eq 'symbol (progn (forward-char 1) (edebug-next-token-class))))
1083 ;; Find out if this is a defining form from first symbol
1084 (setq def-kind (read (current-buffer))
1085 spec (and (symbolp def-kind) (get-edebug-spec def-kind))
1086 defining-form-p (and (listp spec)
1087 (eq '&define (car spec)))
1088 ;; This is incorrect in general!! But OK most of the time.
1089 def-name (if (and defining-form-p
1090 (eq 'name (car (cdr spec)))
1091 (eq 'symbol (edebug-next-token-class)))
1092 (read (current-buffer))))))
1093 ;;;(message "all defs: %s all forms: %s" edebug-all-defs edebug-all-forms)
1094 (cond
1095 (defining-form-p
1096 (if (or edebug-all-defs edebug-all-forms)
1097 ;; If it is a defining form and we are edebugging defs,
1098 ;; then let edebug-list-form start it.
1099 (let ((cursor (edebug-new-cursor
1100 (list (edebug-read-storing-offsets (current-buffer)))
1101 (list edebug-offsets))))
1102 (car
1103 (edebug-make-form-wrapper
1104 cursor
1105 (edebug-before-offset cursor)
1106 (1- (edebug-after-offset cursor))
1107 (list (cons (symbol-name def-kind) (cdr spec))))))
1109 ;; Not edebugging this form, so reset the symbol's edebug
1110 ;; property to be just a marker at the definition's source code.
1111 ;; This only works for defs with simple names.
1112 (put def-name 'edebug (point-marker))
1113 ;; Also nil out dependent defs.
1114 '(mapcar (function
1115 (lambda (def)
1116 (put def-name 'edebug nil)))
1117 (get def-name 'edebug-dependents))
1118 (edebug-read-sexp)))
1120 ;; If all forms are being edebugged, explicitly wrap it.
1121 (edebug-all-forms
1122 (let ((cursor (edebug-new-cursor
1123 (list (edebug-read-storing-offsets (current-buffer)))
1124 (list edebug-offsets))))
1125 (edebug-make-form-wrapper
1126 cursor
1127 (edebug-before-offset cursor)
1128 (edebug-after-offset cursor)
1129 nil)))
1131 ;; Not a defining form, and not edebugging.
1132 (t (edebug-read-sexp)))
1136 (defvar edebug-def-args) ; args of defining form.
1137 (defvar edebug-def-interactive) ; is it an emacs interactive function?
1138 (defvar edebug-inside-func) ;; whether code is inside function context.
1139 ;; Currently def-form sets this to nil; def-body sets it to t.
1141 (defun edebug-interactive-p-name ()
1142 ;; Return a unique symbol for the variable used to store the
1143 ;; status of interactive-p for this function.
1144 (intern (format "edebug-%s-interactive-p" edebug-def-name)))
1147 (defun edebug-wrap-def-body (forms)
1148 "Wrap the FORMS of a definition body."
1149 (if edebug-def-interactive
1150 `(let ((,(edebug-interactive-p-name)
1151 (interactive-p)))
1152 ,(edebug-make-enter-wrapper forms))
1153 (edebug-make-enter-wrapper forms)))
1156 (defun edebug-make-enter-wrapper (forms)
1157 ;; Generate the enter wrapper for some forms of a definition.
1158 ;; This is not to be used for the body of other forms, e.g. `while',
1159 ;; since it wraps the list of forms with a call to `edebug-enter'.
1160 ;; Uses the dynamically bound vars edebug-def-name and edebug-def-args.
1161 ;; Do this after parsing since that may find a name.
1162 (setq edebug-def-name
1163 (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
1164 `(edebug-enter
1165 (quote ,edebug-def-name)
1166 ,(if edebug-inside-func
1167 `(list
1168 ;; Doesn't work with more than one def-body!!
1169 ;; But the list will just be reversed.
1170 ,@(nreverse edebug-def-args))
1171 'nil)
1172 (function (lambda () ,@forms))
1176 (defvar edebug-form-begin-marker) ; the mark for def being instrumented
1178 (defvar edebug-offset-index) ; the next available offset index.
1179 (defvar edebug-offset-list) ; the list of offset positions.
1181 (defun edebug-inc-offset (offset)
1182 ;; Modifies edebug-offset-index and edebug-offset-list
1183 ;; accesses edebug-func-marc and buffer point.
1184 (prog1
1185 edebug-offset-index
1186 (setq edebug-offset-list (cons (- offset edebug-form-begin-marker)
1187 edebug-offset-list)
1188 edebug-offset-index (1+ edebug-offset-index))))
1191 (defun edebug-make-before-and-after-form (before-index form after-index)
1192 ;; Return the edebug form for the current function at offset BEFORE-INDEX
1193 ;; given FORM. Looks like:
1194 ;; (edebug-after (edebug-before BEFORE-INDEX) AFTER-INDEX FORM)
1195 ;; Also increment the offset index for subsequent use.
1196 `(edebug-after (edebug-before ,before-index) ,after-index ,form))
1198 (defun edebug-make-after-form (form after-index)
1199 ;; Like edebug-make-before-and-after-form, but only after.
1200 `(edebug-after 0 ,after-index ,form))
1203 (defun edebug-unwrap (sexp)
1204 "Return the unwrapped SEXP or return it as is if it is not wrapped.
1205 The SEXP might be the result of wrapping a body, which is a list of
1206 expressions; a `progn' form will be returned enclosing these forms."
1207 (if (consp sexp)
1208 (cond
1209 ((eq 'edebug-after (car sexp))
1210 (nth 3 sexp))
1211 ((eq 'edebug-enter (car sexp))
1212 (macroexp-progn (nthcdr 2 (nth 1 (nth 3 sexp)))))
1213 (t sexp);; otherwise it is not wrapped, so just return it.
1215 sexp))
1217 (defun edebug-unwrap* (sexp)
1218 "Return the SEXP recursively unwrapped."
1219 (let ((new-sexp (edebug-unwrap sexp)))
1220 (while (not (eq sexp new-sexp))
1221 (setq sexp new-sexp
1222 new-sexp (edebug-unwrap sexp)))
1223 (if (consp new-sexp)
1224 (mapcar 'edebug-unwrap* new-sexp)
1225 new-sexp)))
1228 (defun edebug-defining-form (cursor form-begin form-end speclist)
1229 ;; Process the defining form, starting outside the form.
1230 ;; The speclist is a generated list spec that looks like:
1231 ;; (("def-symbol" defining-form-spec-sans-&define))
1232 ;; Skip the first offset.
1233 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1234 (cdr (edebug-cursor-offsets cursor)))
1235 (edebug-make-form-wrapper
1236 cursor
1237 form-begin (1- form-end)
1238 speclist))
1240 (defun edebug-make-form-wrapper (cursor form-begin form-end
1241 &optional speclist)
1242 ;; Wrap a form, usually a defining form, but any evaluated one.
1243 ;; If speclist is non-nil, this is being called by edebug-defining-form.
1244 ;; Otherwise it is being called from edebug-read-and-maybe-wrap-form1.
1245 ;; This is a hack, but I haven't figured out a simpler way yet.
1246 (let* ((form-data-entry (edebug-get-form-data-entry form-begin form-end))
1247 ;; Set this marker before parsing.
1248 (edebug-form-begin-marker
1249 (if form-data-entry
1250 (edebug--form-data-begin form-data-entry)
1251 ;; Buffer must be current-buffer for this to work:
1252 (set-marker (make-marker) form-begin))))
1254 (let (edebug-offset-list
1255 (edebug-offset-index 0)
1256 result
1257 ;; For definitions.
1258 ;; (edebug-containing-def-name edebug-def-name)
1259 ;; Get name from form-data, if any.
1260 (edebug-old-def-name (edebug--form-data-name form-data-entry))
1261 edebug-def-name
1262 edebug-def-args
1263 edebug-def-interactive
1264 edebug-inside-func;; whether wrapped code executes inside a function.
1267 (setq result
1268 (if speclist
1269 (edebug-match cursor speclist)
1271 ;; else wrap as an enter-form.
1272 (edebug-make-enter-wrapper (list (edebug-form cursor)))))
1274 ;; Set the name here if it was not set by edebug-make-enter-wrapper.
1275 (setq edebug-def-name
1276 (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
1278 ;; Add this def as a dependent of containing def. Buggy.
1279 '(if (and edebug-containing-def-name
1280 (not (get edebug-containing-def-name 'edebug-dependents)))
1281 (put edebug-containing-def-name 'edebug-dependents
1282 (cons edebug-def-name
1283 (get edebug-containing-def-name
1284 'edebug-dependents))))
1286 ;; Create a form-data-entry or modify existing entry's markers.
1287 ;; In the latter case, pointers to the entry remain eq.
1288 (if (not form-data-entry)
1289 (setq form-data-entry
1290 (edebug--make-form-data-entry
1291 edebug-def-name
1292 edebug-form-begin-marker
1293 ;; Buffer must be current-buffer.
1294 (set-marker (make-marker) form-end)
1296 (edebug-set-form-data-entry
1297 form-data-entry edebug-def-name ;; in case name is changed
1298 form-begin form-end))
1300 ;; (message "defining: %s" edebug-def-name) (sit-for 2)
1301 (edebug-make-top-form-data-entry form-data-entry)
1302 (message "Edebug: %s" edebug-def-name)
1303 ;;(debug edebug-def-name)
1305 ;; Destructively reverse edebug-offset-list and make vector from it.
1306 (setq edebug-offset-list (vconcat (nreverse edebug-offset-list)))
1308 ;; Side effects on the property list of edebug-def-name.
1309 (edebug-clear-frequency-count edebug-def-name)
1310 (edebug-clear-coverage edebug-def-name)
1312 ;; Set up the initial window data.
1313 (if (not edebug-top-window-data) ;; if not already set, do it now.
1314 (let ((window ;; Find the best window for this buffer.
1315 (or (get-buffer-window (current-buffer))
1316 (selected-window))))
1317 (setq edebug-top-window-data
1318 (cons window (window-start window)))))
1320 ;; Store the edebug data in symbol's property list.
1321 (put edebug-def-name 'edebug
1322 ;; A struct or vector would be better here!!
1323 (list edebug-form-begin-marker
1324 nil ; clear breakpoints
1325 edebug-offset-list
1326 edebug-top-window-data
1328 result
1332 (defun edebug-clear-frequency-count (name)
1333 ;; Create initial frequency count vector.
1334 ;; For each stop point, the counter is incremented each time it is visited.
1335 (put name 'edebug-freq-count
1336 (make-vector (length edebug-offset-list) 0)))
1339 (defun edebug-clear-coverage (name)
1340 ;; Create initial coverage vector.
1341 ;; Only need one per expression, but it is simpler to use stop points.
1342 (put name 'edebug-coverage
1343 (make-vector (length edebug-offset-list) 'unknown)))
1346 (defun edebug-form (cursor)
1347 ;; Return the instrumented form for the following form.
1348 ;; Add the point offsets to the edebug-offset-list for the form.
1349 (let* ((form (edebug-top-element-required cursor "Expected form"))
1350 (offset (edebug-top-offset cursor)))
1351 (prog1
1352 (cond
1353 ((consp form)
1354 ;; The first offset for a list form is for the list form itself.
1355 (if (eq 'quote (car form))
1356 form
1357 (let* ((head (car form))
1358 (spec (and (symbolp head) (get-edebug-spec head)))
1359 (new-cursor (edebug-new-cursor form offset)))
1360 ;; Find out if this is a defining form from first symbol.
1361 ;; An indirect spec would not work here, yet.
1362 (if (and (consp spec) (eq '&define (car spec)))
1363 (edebug-defining-form
1364 new-cursor
1365 (car offset);; before the form
1366 (edebug-after-offset cursor)
1367 (cons (symbol-name head) (cdr spec)))
1368 ;; Wrap a regular form.
1369 (edebug-make-before-and-after-form
1370 (edebug-inc-offset (car offset))
1371 (edebug-list-form new-cursor)
1372 ;; After processing the list form, the new-cursor is left
1373 ;; with the offset after the form.
1374 (edebug-inc-offset (edebug-cursor-offsets new-cursor))))
1377 ((symbolp form)
1378 (cond
1379 ;; Check for constant symbols that don't get wrapped.
1380 ((or (memq form '(t nil))
1381 (keywordp form))
1382 form)
1384 (t ;; just a variable
1385 (edebug-make-after-form form (edebug-inc-offset (cdr offset))))))
1387 ;; Anything else is self-evaluating.
1388 (t form))
1389 (edebug-move-cursor cursor))))
1392 (defsubst edebug-forms (cursor) (edebug-match cursor '(&rest form)))
1393 (defsubst edebug-sexps (cursor) (edebug-match cursor '(&rest sexp)))
1395 (defsubst edebug-list-form-args (head cursor)
1396 ;; Process the arguments of a list form given that head of form is a symbol.
1397 ;; Helper for edebug-list-form
1398 (let ((spec (get-edebug-spec head)))
1399 (cond
1400 (spec
1401 (cond
1402 ((consp spec)
1403 ;; It is a speclist.
1404 (let (edebug-best-error
1405 edebug-error-point);; This may not be needed.
1406 (edebug-match-sublist cursor spec)))
1407 ((eq t spec) (edebug-forms cursor))
1408 ((eq 0 spec) (edebug-sexps cursor))
1409 ((symbolp spec) (funcall spec cursor));; Not used by edebug,
1410 ; but leave it in for compatibility.
1412 ;; No edebug-form-spec provided.
1413 ((macrop head)
1414 (if edebug-eval-macro-args
1415 (edebug-forms cursor)
1416 (edebug-sexps cursor)))
1417 (t ;; Otherwise it is a function call.
1418 (edebug-forms cursor)))))
1421 (defun edebug-list-form (cursor)
1422 ;; Return an instrumented form built from the list form.
1423 ;; The after offset will be left in the cursor after processing the form.
1424 (let ((head (edebug-top-element-required cursor "Expected elements"))
1425 ;; Prevent backtracking whenever instrumenting.
1426 (edebug-gate t)
1427 ;; A list form is never optional because it matches anything.
1428 (edebug-&optional nil)
1429 (edebug-&rest nil))
1430 ;; Skip the first offset.
1431 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1432 (cdr (edebug-cursor-offsets cursor)))
1433 (cond
1434 ((symbolp head)
1435 (cond
1436 ((null head) nil) ; () is valid.
1437 ((eq head 'interactive-p)
1438 ;; Special case: replace (interactive-p) with variable
1439 (setq edebug-def-interactive 'check-it)
1440 (edebug-move-cursor cursor)
1441 (edebug-interactive-p-name))
1443 (cons head (edebug-list-form-args
1444 head (edebug-move-cursor cursor))))))
1446 ((consp head)
1447 (if (eq (car head) '\,)
1448 ;; The head of a form should normally be a symbol or a lambda
1449 ;; expression but it can also be an unquote form to be filled
1450 ;; before evaluation. We evaluate the arguments anyway, on the
1451 ;; assumption that the unquote form will place a proper function
1452 ;; name (rather than a macro name).
1453 (edebug-match cursor '(("," def-form) body))
1454 ;; Process anonymous function and args.
1455 ;; This assumes no anonymous macros.
1456 (edebug-match-specs cursor '(lambda-expr body) 'edebug-match-specs)))
1458 (t (edebug-syntax-error
1459 "Head of list form must be a symbol or lambda expression")))
1462 ;;; Matching of specs.
1464 (defvar edebug-after-dotted-spec nil)
1466 (defvar edebug-matching-depth 0) ;; initial value
1469 ;;; Failure to match
1471 ;; This throws to no-match, if there are higher alternatives.
1472 ;; Otherwise it signals an error. The place of the error is found
1473 ;; with the two before- and after-offset functions.
1475 (defun edebug-no-match (cursor &rest args)
1476 ;; Throw a no-match, or signal an error immediately if gate is active.
1477 ;; Remember this point in case we need to report this error.
1478 (setq edebug-error-point (or edebug-error-point
1479 (edebug-before-offset cursor))
1480 edebug-best-error (or edebug-best-error args))
1481 (if (and edebug-gate (not edebug-&optional))
1482 (progn
1483 (if edebug-error-point
1484 (goto-char edebug-error-point))
1485 (apply 'edebug-syntax-error args))
1486 (throw 'no-match args)))
1489 (defun edebug-match (cursor specs)
1490 ;; Top level spec matching function.
1491 ;; Used also at each lower level of specs.
1492 (let (edebug-&optional
1493 edebug-&rest
1494 edebug-best-error
1495 edebug-error-point
1496 (edebug-gate edebug-gate) ;; locally bound to limit effect
1498 (edebug-match-specs cursor specs 'edebug-match-specs)))
1501 (defun edebug-match-one-spec (cursor spec)
1502 ;; Match one spec, which is not a keyword &-spec.
1503 (cond
1504 ((symbolp spec) (edebug-match-symbol cursor spec))
1505 ((vectorp spec) (edebug-match cursor (append spec nil)))
1506 ((stringp spec) (edebug-match-string cursor spec))
1507 ((listp spec) (edebug-match-list cursor spec))
1511 (defun edebug-match-specs (cursor specs remainder-handler)
1512 ;; Append results of matching the list of specs.
1513 ;; The first spec is handled and the remainder-handler handles the rest.
1514 (let ((edebug-matching-depth
1515 (if (> edebug-matching-depth edebug-max-depth)
1516 (error "Too deep - perhaps infinite loop in spec?")
1517 (1+ edebug-matching-depth))))
1518 (cond
1519 ((null specs) nil)
1521 ;; Is the spec dotted?
1522 ((atom specs)
1523 (let ((edebug-dotted-spec t));; Containing spec list was dotted.
1524 (edebug-match-specs cursor (list specs) remainder-handler)))
1526 ;; Is the form dotted?
1527 ((not (listp (edebug-cursor-expressions cursor)));; allow nil
1528 (if (not edebug-dotted-spec)
1529 (edebug-no-match cursor "Dotted spec required."))
1530 ;; Cancel dotted spec and dotted form.
1531 (let ((edebug-dotted-spec)
1532 (this-form (edebug-cursor-expressions cursor))
1533 (this-offset (edebug-cursor-offsets cursor)))
1534 ;; Wrap the form in a list, (by changing the cursor??)...
1535 (edebug-set-cursor cursor (list this-form) this-offset)
1536 ;; and process normally, then unwrap the result.
1537 (car (edebug-match-specs cursor specs remainder-handler))))
1539 (t;; Process normally.
1540 (let* ((spec (car specs))
1541 (rest)
1542 (first-char (and (symbolp spec) (aref (symbol-name spec) 0))))
1543 ;;(message "spec = %s first char = %s" spec first-char) (sit-for 1)
1544 (nconc
1545 (cond
1546 ((eq ?& first-char);; "&" symbols take all following specs.
1547 (funcall (get-edebug-spec spec) cursor (cdr specs)))
1548 ((eq ?: first-char);; ":" symbols take one following spec.
1549 (setq rest (cdr (cdr specs)))
1550 (funcall (get-edebug-spec spec) cursor (car (cdr specs))))
1551 (t;; Any other normal spec.
1552 (setq rest (cdr specs))
1553 (edebug-match-one-spec cursor spec)))
1554 (funcall remainder-handler cursor rest remainder-handler)))))))
1557 ;; Define specs for all the symbol specs with functions used to process them.
1558 ;; Perhaps we shouldn't be doing this with edebug-form-specs since the
1559 ;; user may want to define macros or functions with the same names.
1560 ;; We could use an internal obarray for these primitive specs.
1562 (dolist (pair '((&optional . edebug-match-&optional)
1563 (&rest . edebug-match-&rest)
1564 (&or . edebug-match-&or)
1565 (form . edebug-match-form)
1566 (sexp . edebug-match-sexp)
1567 (body . edebug-match-body)
1568 (&define . edebug-match-&define)
1569 (name . edebug-match-name)
1570 (:name . edebug-match-colon-name)
1571 (arg . edebug-match-arg)
1572 (def-body . edebug-match-def-body)
1573 (def-form . edebug-match-def-form)
1574 ;; Less frequently used:
1575 ;; (function . edebug-match-function)
1576 (lambda-expr . edebug-match-lambda-expr)
1577 (&not . edebug-match-&not)
1578 (&key . edebug-match-&key)
1579 (place . edebug-match-place)
1580 (gate . edebug-match-gate)
1581 ;; (nil . edebug-match-nil) not this one - special case it.
1583 (put (car pair) 'edebug-form-spec (cdr pair)))
1585 (defun edebug-match-symbol (cursor symbol)
1586 ;; Match a symbol spec.
1587 (let* ((spec (get-edebug-spec symbol)))
1588 (cond
1589 (spec
1590 (if (consp spec)
1591 ;; It is an indirect spec.
1592 (edebug-match cursor spec)
1593 ;; Otherwise it should be the symbol name of a function.
1594 ;; There could be a bug here - maybe need to do edebug-match bindings.
1595 (funcall spec cursor)))
1597 ((null symbol) ;; special case this.
1598 (edebug-match-nil cursor))
1600 ((fboundp symbol) ; is it a predicate?
1601 (let ((sexp (edebug-top-element-required cursor "Expected" symbol)))
1602 ;; Special case for edebug-`.
1603 (if (and (listp sexp) (eq (car sexp) '\,))
1604 (edebug-match cursor '(("," def-form)))
1605 (if (not (funcall symbol sexp))
1606 (edebug-no-match cursor symbol "failed"))
1607 (edebug-move-cursor cursor)
1608 (list sexp))))
1609 (t (error "%s is not a form-spec or function" symbol))
1613 (defun edebug-match-sexp (cursor)
1614 (list (prog1 (edebug-top-element-required cursor "Expected sexp")
1615 (edebug-move-cursor cursor))))
1617 (defun edebug-match-form (cursor)
1618 (list (edebug-form cursor)))
1620 (defalias 'edebug-match-place 'edebug-match-form)
1621 ;; Currently identical to edebug-match-form.
1622 ;; This is for common lisp setf-style place arguments.
1624 (defsubst edebug-match-body (cursor) (edebug-forms cursor))
1626 (defun edebug-match-&optional (cursor specs)
1627 ;; Keep matching until one spec fails.
1628 (edebug-&optional-wrapper cursor specs 'edebug-&optional-wrapper))
1630 (defun edebug-&optional-wrapper (cursor specs remainder-handler)
1631 (let (result
1632 (edebug-&optional specs)
1633 (edebug-gate nil)
1634 (this-form (edebug-cursor-expressions cursor))
1635 (this-offset (edebug-cursor-offsets cursor)))
1636 (if (null (catch 'no-match
1637 (setq result
1638 (edebug-match-specs cursor specs remainder-handler))
1639 ;; Returning nil means no no-match was thrown.
1640 nil))
1641 result
1642 ;; no-match, but don't fail; just reset cursor and return nil.
1643 (edebug-set-cursor cursor this-form this-offset)
1644 nil)))
1647 (defun edebug-&rest-wrapper (cursor specs remainder-handler)
1648 (if (null specs) (setq specs edebug-&rest))
1649 ;; Reuse the &optional handler with this as the remainder handler.
1650 (edebug-&optional-wrapper cursor specs remainder-handler))
1652 (defun edebug-match-&rest (cursor specs)
1653 ;; Repeatedly use specs until failure.
1654 (let ((edebug-&rest specs) ;; remember these
1655 edebug-best-error
1656 edebug-error-point)
1657 (edebug-&rest-wrapper cursor specs 'edebug-&rest-wrapper)))
1660 (defun edebug-match-&or (cursor specs)
1661 ;; Keep matching until one spec succeeds, and return its results.
1662 ;; If none match, fail.
1663 ;; This needs to be optimized since most specs spend time here.
1664 (let ((original-specs specs)
1665 (this-form (edebug-cursor-expressions cursor))
1666 (this-offset (edebug-cursor-offsets cursor)))
1667 (catch 'matched
1668 (while specs
1669 (catch 'no-match
1670 (throw 'matched
1671 (let (edebug-gate ;; only while matching each spec
1672 edebug-best-error
1673 edebug-error-point)
1674 ;; Doesn't support e.g. &or symbolp &rest form
1675 (edebug-match-one-spec cursor (car specs)))))
1676 ;; Match failed, so reset and try again.
1677 (setq specs (cdr specs))
1678 ;; Reset the cursor for the next match.
1679 (edebug-set-cursor cursor this-form this-offset))
1680 ;; All failed.
1681 (apply 'edebug-no-match cursor "Expected one of" original-specs))
1685 (defun edebug-match-&not (cursor specs)
1686 ;; If any specs match, then fail
1687 (if (null (catch 'no-match
1688 (let ((edebug-gate nil))
1689 (save-excursion
1690 (edebug-match-&or cursor specs)))
1691 nil))
1692 ;; This means something matched, so it is a no match.
1693 (edebug-no-match cursor "Unexpected"))
1694 ;; This means nothing matched, so it is OK.
1695 nil) ;; So, return nothing
1698 (def-edebug-spec &key edebug-match-&key)
1700 (defun edebug-match-&key (cursor specs)
1701 ;; Following specs must look like (<name> <spec>) ...
1702 ;; where <name> is the name of a keyword, and spec is its spec.
1703 ;; This really doesn't save much over the expanded form and takes time.
1704 (edebug-match-&rest
1705 cursor
1706 (cons '&or
1707 (mapcar (function (lambda (pair)
1708 (vector (format ":%s" (car pair))
1709 (car (cdr pair)))))
1710 specs))))
1713 (defun edebug-match-gate (_cursor)
1714 ;; Simply set the gate to prevent backtracking at this level.
1715 (setq edebug-gate t)
1716 nil)
1719 (defun edebug-match-list (cursor specs)
1720 ;; The spec is a list, but what kind of list, and what context?
1721 (if edebug-dotted-spec
1722 ;; After dotted spec but form did not contain dot,
1723 ;; so match list spec elements as if spliced in.
1724 (prog1
1725 (let ((edebug-dotted-spec))
1726 (edebug-match-specs cursor specs 'edebug-match-specs))
1727 ;; If it matched, really clear the dotted-spec flag.
1728 (setq edebug-dotted-spec nil))
1729 (let ((spec (car specs))
1730 (form (edebug-top-element-required cursor "Expected" specs)))
1731 (cond
1732 ((eq 'quote spec)
1733 (let ((spec (car (cdr specs))))
1734 (cond
1735 ((symbolp spec)
1736 ;; Special case: spec quotes a symbol to match.
1737 ;; Change in future. Use "..." instead.
1738 (if (not (eq spec form))
1739 (edebug-no-match cursor "Expected" spec))
1740 (edebug-move-cursor cursor)
1741 (setq edebug-gate t)
1742 form)
1744 (error "Bad spec: %s" specs)))))
1746 ((eq 'vector spec)
1747 (if (vectorp form)
1748 ;; Special case: match a vector with the specs.
1749 (let ((result (edebug-match-sublist
1750 (edebug-new-cursor
1751 form (cdr (edebug-top-offset cursor)))
1752 (cdr specs))))
1753 (edebug-move-cursor cursor)
1754 (list (apply 'vector result)))
1755 (edebug-no-match cursor "Expected" specs)))
1757 ((listp form)
1758 (prog1
1759 (list (edebug-match-sublist
1760 ;; First offset is for the list form itself.
1761 ;; Treat nil as empty list.
1762 (edebug-new-cursor form (cdr (edebug-top-offset cursor)))
1763 specs))
1764 (edebug-move-cursor cursor)))
1766 (t (edebug-no-match cursor "Expected" specs)))
1770 (defun edebug-match-sublist (cursor specs)
1771 ;; Match a sublist of specs.
1772 (let (edebug-&optional
1773 ;;edebug-best-error
1774 ;;edebug-error-point
1776 (prog1
1777 ;; match with edebug-match-specs so edebug-best-error is not bound.
1778 (edebug-match-specs cursor specs 'edebug-match-specs)
1779 (if (not (edebug-empty-cursor cursor))
1780 (if edebug-best-error
1781 (apply 'edebug-no-match cursor edebug-best-error)
1782 ;; A failed &rest or &optional spec may leave some args.
1783 (edebug-no-match cursor "Failed matching" specs)
1784 )))))
1787 (defun edebug-match-string (cursor spec)
1788 (let ((sexp (edebug-top-element-required cursor "Expected" spec)))
1789 (if (not (eq (intern spec) sexp))
1790 (edebug-no-match cursor "Expected" spec)
1791 ;; Since it matched, failure means immediate error, unless &optional.
1792 (setq edebug-gate t)
1793 (edebug-move-cursor cursor)
1794 (list sexp)
1797 (defun edebug-match-nil (cursor)
1798 ;; There must be nothing left to match a nil.
1799 (if (not (edebug-empty-cursor cursor))
1800 (edebug-no-match cursor "Unmatched argument(s)")
1801 nil))
1804 (defun edebug-match-function (_cursor)
1805 (error "Use function-form instead of function in edebug spec"))
1807 (defun edebug-match-&define (cursor specs)
1808 ;; Match a defining form.
1809 ;; Normally, &define is interpreted specially other places.
1810 ;; This should only be called inside of a spec list to match the remainder
1811 ;; of the current list. e.g. ("lambda" &define args def-body)
1812 (edebug-make-form-wrapper
1813 cursor
1814 (edebug-before-offset cursor)
1815 ;; Find the last offset in the list.
1816 (let ((offsets (edebug-cursor-offsets cursor)))
1817 (while (consp offsets) (setq offsets (cdr offsets)))
1818 offsets)
1819 specs))
1821 (defun edebug-match-lambda-expr (cursor)
1822 ;; The expression must be a function.
1823 ;; This will match any list form that begins with a symbol
1824 ;; that has an edebug-form-spec beginning with &define. In
1825 ;; practice, only lambda expressions should be used.
1826 ;; I could add a &lambda specification to avoid confusion.
1827 (let* ((sexp (edebug-top-element-required
1828 cursor "Expected lambda expression"))
1829 (offset (edebug-top-offset cursor))
1830 (head (and (consp sexp) (car sexp)))
1831 (spec (and (symbolp head) (get-edebug-spec head)))
1832 (edebug-inside-func nil))
1833 ;; Find out if this is a defining form from first symbol.
1834 (if (and (consp spec) (eq '&define (car spec)))
1835 (prog1
1836 (list
1837 (edebug-defining-form
1838 (edebug-new-cursor sexp offset)
1839 (car offset);; before the sexp
1840 (edebug-after-offset cursor)
1841 (cons (symbol-name head) (cdr spec))))
1842 (edebug-move-cursor cursor))
1843 (edebug-no-match cursor "Expected lambda expression")
1847 (defun edebug-match-name (cursor)
1848 ;; Set the edebug-def-name bound in edebug-defining-form.
1849 (let ((name (edebug-top-element-required cursor "Expected name")))
1850 ;; Maybe strings and numbers could be used.
1851 (if (not (symbolp name))
1852 (edebug-no-match cursor "Symbol expected for name of definition"))
1853 (setq edebug-def-name
1854 (if edebug-def-name
1855 ;; Construct a new name by appending to previous name.
1856 (intern (format "%s@%s" edebug-def-name name))
1857 name))
1858 (edebug-move-cursor cursor)
1859 (list name)))
1861 (defun edebug-match-colon-name (_cursor spec)
1862 ;; Set the edebug-def-name to the spec.
1863 (setq edebug-def-name
1864 (if edebug-def-name
1865 ;; Construct a new name by appending to previous name.
1866 (intern (format "%s@%s" edebug-def-name spec))
1867 spec))
1868 nil)
1870 (defun edebug-match-arg (cursor)
1871 ;; set the def-args bound in edebug-defining-form
1872 (let ((edebug-arg (edebug-top-element-required cursor "Expected arg")))
1873 (if (or (not (symbolp edebug-arg))
1874 (edebug-lambda-list-keywordp edebug-arg))
1875 (edebug-no-match cursor "Bad argument:" edebug-arg))
1876 (edebug-move-cursor cursor)
1877 (setq edebug-def-args (cons edebug-arg edebug-def-args))
1878 (list edebug-arg)))
1880 (defun edebug-match-def-form (cursor)
1881 ;; Like form but the form is wrapped in edebug-enter form.
1882 ;; The form is assumed to be executing outside of the function context.
1883 ;; This is a hack for now, since a def-form might execute inside as well.
1884 ;; Not to be used otherwise.
1885 (let ((edebug-inside-func nil))
1886 (list (edebug-make-enter-wrapper (list (edebug-form cursor))))))
1888 (defun edebug-match-def-body (cursor)
1889 ;; Like body but body is wrapped in edebug-enter form.
1890 ;; The body is assumed to be executing inside of the function context.
1891 ;; Not to be used otherwise.
1892 (let* ((edebug-inside-func t)
1893 (forms (edebug-forms cursor)))
1894 ;; If there's no form, there's nothing to wrap!
1895 ;; This happens to handle bug#20281, tho maybe a better fix would be to
1896 ;; improve the `defun' spec.
1897 (when forms
1898 (list (edebug-wrap-def-body forms)))))
1901 ;;;; Edebug Form Specs
1902 ;;; ==========================================================
1904 ;;;;* Spec for def-edebug-spec
1905 ;;; Out of date.
1907 (defun edebug-spec-p (object)
1908 "Return non-nil if OBJECT is a symbol with an edebug-form-spec property."
1909 (and (symbolp object)
1910 (get object 'edebug-form-spec)))
1912 (def-edebug-spec def-edebug-spec
1913 ;; Top level is different from lower levels.
1914 (&define :name edebug-spec name
1915 &or "nil" edebug-spec-p "t" "0" (&rest edebug-spec)))
1917 (def-edebug-spec edebug-spec-list
1918 ;; A list must have something in it, or it is nil, a symbolp
1919 ((edebug-spec . [&or nil edebug-spec])))
1921 (def-edebug-spec edebug-spec
1922 (&or
1923 (vector &rest edebug-spec) ; matches a vector
1924 ("vector" &rest edebug-spec) ; matches a vector spec
1925 ("quote" symbolp)
1926 edebug-spec-list
1927 stringp
1928 [edebug-lambda-list-keywordp &rest edebug-spec]
1929 [keywordp gate edebug-spec]
1930 edebug-spec-p ;; Including all the special ones e.g. form.
1931 symbolp;; a predicate
1935 ;;;* Emacs special forms and some functions.
1937 ;; quote expects only one argument, although it allows any number.
1938 (def-edebug-spec quote sexp)
1940 ;; The standard defining forms.
1941 (def-edebug-spec defconst defvar)
1942 (def-edebug-spec defvar (symbolp &optional form stringp))
1944 (def-edebug-spec defun
1945 (&define name lambda-list
1946 [&optional stringp]
1947 [&optional ("declare" &rest sexp)]
1948 [&optional ("interactive" interactive)]
1949 def-body))
1950 (def-edebug-spec defmacro
1951 ;; FIXME: Improve `declare' so we can Edebug gv-expander and
1952 ;; gv-setter declarations.
1953 (&define name lambda-list [&optional stringp]
1954 [&optional ("declare" &rest sexp)] def-body))
1956 (def-edebug-spec arglist lambda-list) ;; deprecated - use lambda-list.
1958 (def-edebug-spec lambda-list
1959 (([&rest arg]
1960 [&optional ["&optional" arg &rest arg]]
1961 &optional ["&rest" arg]
1964 (def-edebug-spec interactive
1965 (&optional &or stringp def-form))
1967 ;; A function-form is for an argument that may be a function or a form.
1968 ;; This specially recognizes anonymous functions quoted with quote.
1969 (def-edebug-spec function-form
1970 ;; form at the end could also handle "function",
1971 ;; but recognize it specially to avoid wrapping function forms.
1972 (&or ([&or "quote" "function"] &or symbolp lambda-expr) form))
1974 ;; function expects a symbol or a lambda or macro expression
1975 ;; A macro is allowed by Emacs.
1976 (def-edebug-spec function (&or symbolp lambda-expr))
1978 ;; A macro expression is a lambda expression with "macro" prepended.
1979 (def-edebug-spec macro (&define "lambda" lambda-list def-body))
1981 ;; (def-edebug-spec anonymous-form ((&or ["lambda" lambda] ["macro" macro])))
1983 ;; Standard functions that take function-forms arguments.
1985 ;; FIXME? The manual uses this form (maybe that's just for illustration?):
1986 ;; (def-edebug-spec let
1987 ;; ((&rest &or symbolp (gate symbolp &optional form))
1988 ;; body))
1989 (def-edebug-spec let
1990 ((&rest &or (symbolp &optional form) symbolp)
1991 body))
1993 (def-edebug-spec let* let)
1995 (def-edebug-spec setq (&rest symbolp form))
1996 (def-edebug-spec setq-default setq)
1998 (def-edebug-spec cond (&rest (&rest form)))
2000 (def-edebug-spec condition-case
2001 (symbolp
2002 form
2003 &rest ([&or symbolp (&rest symbolp)] body)))
2006 (def-edebug-spec \` (backquote-form))
2008 ;; Supports quotes inside backquotes,
2009 ;; but only at the top level inside unquotes.
2010 (def-edebug-spec backquote-form
2011 (&or
2012 ([&or "," ",@"] &or ("quote" backquote-form) form)
2013 ;; The simple version:
2014 ;; (backquote-form &rest backquote-form)
2015 ;; doesn't handle (a . ,b). The straightforward fix:
2016 ;; (backquote-form . [&or nil backquote-form])
2017 ;; uses up too much stack space.
2018 ;; Note that `(foo . ,@bar) is not valid, so we don't need to handle it.
2019 (backquote-form [&rest [&not ","] backquote-form]
2020 . [&or nil backquote-form])
2021 ;; If you use dotted forms in backquotes, replace the previous line
2022 ;; with the following. This takes quite a bit more stack space, however.
2023 ;; (backquote-form . [&or nil backquote-form])
2024 (vector &rest backquote-form)
2025 sexp))
2027 ;; Special version of backquote that instruments backquoted forms
2028 ;; destined to be evaluated, usually as the result of a
2029 ;; macroexpansion. Backquoted code can only have unquotes (, and ,@)
2030 ;; in places where list forms are allowed, and predicates. If the
2031 ;; backquote is used in a macro, unquoted code that come from
2032 ;; arguments must be instrumented, if at all, with def-form not def-body.
2034 ;; We could assume that all forms (not nested in other forms)
2035 ;; in arguments of macros should be def-forms, whether or not the macros
2036 ;; are defined with edebug-` but this would be expensive.
2038 ;; ,@ might have some problems.
2040 (defalias 'edebug-\` '\`) ;; same macro as regular backquote.
2041 (def-edebug-spec edebug-\` (def-form))
2043 ;; Assume immediate quote in unquotes mean backquote at next higher level.
2044 (def-edebug-spec \, (&or ("quote" edebug-\`) def-form))
2045 (def-edebug-spec \,@ (&define ;; so (,@ form) is never wrapped.
2046 &or ("quote" edebug-\`) def-form))
2048 ;; New byte compiler.
2050 (def-edebug-spec save-selected-window t)
2051 (def-edebug-spec save-current-buffer t)
2053 ;; Anything else?
2055 ;;; The debugger itself
2057 (defvar edebug-active nil) ;; Non-nil when edebug is active
2059 (defvar edebug-stack nil)
2060 ;; Stack of active functions evaluated via edebug.
2061 ;; Should be nil at the top level.
2063 (defvar edebug-stack-depth -1)
2064 ;; Index of last edebug-stack item.
2066 (defvar edebug-offset-indices nil)
2067 ;; Stack of offset indices of visited edebug sexps.
2068 ;; Should be nil at the top level.
2069 ;; Each function adds one cons. Top is modified with setcar.
2072 (defvar edebug-entered nil
2073 ;; Non-nil if edebug has already been entered at this recursive edit level.
2074 ;; This should stay nil at the top level.
2077 ;; Should these be options?
2078 (defconst edebug-debugger 'edebug
2079 ;; Name of function to use for debugging when error or quit occurs.
2080 ;; Set this to 'debug if you want to debug edebug.
2084 ;; Dynamically bound variables, declared globally but left unbound.
2085 (defvar edebug-function) ; the function being executed. change name!!
2086 (defvar edebug-data) ; the edebug data for the function
2087 (defvar edebug-def-mark) ; the mark for the definition
2088 (defvar edebug-freq-count) ; the count of expression visits.
2089 (defvar edebug-coverage) ; the coverage results of each expression of function.
2091 (defvar edebug-buffer) ; which buffer the function is in.
2093 (defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2094 (defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2096 (defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2097 (defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2099 ;;; Handling signals
2101 (defun edebug-signal (signal-name signal-data)
2102 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2103 A signal name is a symbol with an `error-conditions' property
2104 that is a list of condition names.
2105 A handler for any of those names will get to handle this signal.
2106 The symbol `error' should always be one of them.
2108 DATA should be a list. Its elements are printed as part of the error message.
2109 If the signal is handled, DATA is made available to the handler.
2110 See `condition-case'.
2112 This is the Edebug replacement for the standard `signal'. It should
2113 only be active while Edebug is. It checks `debug-on-error' to see
2114 whether it should call the debugger. When execution is resumed, the
2115 error is signaled again."
2116 (if (and (listp debug-on-error) (memq signal-name debug-on-error))
2117 (edebug 'error (cons signal-name signal-data)))
2118 ;; If we reach here without another non-local exit, then send signal again.
2119 ;; i.e. the signal is not continuable, yet.
2120 ;; Avoid infinite recursion.
2121 (let ((signal-hook-function nil))
2122 (signal signal-name signal-data)))
2124 ;;; Entering Edebug
2126 (defun edebug-enter (function args body)
2127 ;; Entering FUNC. The arguments are ARGS, and the body is BODY.
2128 ;; Setup edebug variables and evaluate BODY. This function is called
2129 ;; when a function evaluated with edebug-eval-top-level-form is entered.
2130 ;; Return the result of BODY.
2132 ;; Is this the first time we are entering edebug since
2133 ;; lower-level recursive-edit command?
2134 ;; More precisely, this tests whether Edebug is currently active.
2135 (let ((edebug-function function))
2136 (if (not edebug-entered)
2137 (let ((edebug-entered t)
2138 ;; Binding max-lisp-eval-depth here is OK,
2139 ;; but not inside an unwind-protect.
2140 ;; Doing it here also keeps it from growing too large.
2141 (max-lisp-eval-depth (+ 100 max-lisp-eval-depth)) ; too much??
2142 (max-specpdl-size (+ 200 max-specpdl-size))
2144 (debugger edebug-debugger) ; only while edebug is active.
2145 (edebug-outside-debug-on-error debug-on-error)
2146 (edebug-outside-debug-on-quit debug-on-quit)
2147 ;; Binding these may not be the right thing to do.
2148 ;; We want to allow the global values to be changed.
2149 (debug-on-error (or debug-on-error edebug-on-error))
2150 (debug-on-quit edebug-on-quit))
2151 (unwind-protect
2152 (let ((signal-hook-function 'edebug-signal))
2153 (setq edebug-execution-mode (or edebug-next-execution-mode
2154 edebug-initial-mode
2155 edebug-execution-mode)
2156 edebug-next-execution-mode nil)
2157 (edebug-enter function args body))))
2159 (let* ((edebug-data (get function 'edebug))
2160 (edebug-def-mark (car edebug-data)) ; mark at def start
2161 (edebug-freq-count (get function 'edebug-freq-count))
2162 (edebug-coverage (get function 'edebug-coverage))
2163 (edebug-buffer (marker-buffer edebug-def-mark))
2165 (edebug-stack (cons function edebug-stack))
2166 (edebug-offset-indices (cons 0 edebug-offset-indices))
2168 (if (get function 'edebug-on-entry)
2169 (progn
2170 (setq edebug-execution-mode 'step)
2171 (if (eq (get function 'edebug-on-entry) 'temp)
2172 (put function 'edebug-on-entry nil))))
2173 (if edebug-trace
2174 (edebug--enter-trace function args body)
2175 (funcall body))
2176 ))))
2178 (defun edebug-var-status (var)
2179 "Return a cons cell describing the status of VAR's current binding.
2180 The purpose of this function is so you can properly undo
2181 subsequent changes to the same binding, by passing the status
2182 cons cell to `edebug-restore-status'. The status cons cell
2183 has the form (LOCUS . VALUE), where LOCUS can be a buffer
2184 \(for a buffer-local binding), or nil (if the default binding is current)."
2185 (cons (variable-binding-locus var)
2186 (symbol-value var)))
2188 (defun edebug-restore-status (var status)
2189 "Reset VAR based on STATUS.
2190 STATUS should be a list returned by `edebug-var-status'."
2191 (let ((locus (car status))
2192 (value (cdr status)))
2193 (cond ((bufferp locus)
2194 (if (buffer-live-p locus)
2195 (with-current-buffer locus
2196 (set var value))))
2197 ((framep locus)
2198 (modify-frame-parameters locus (list (cons var value))))
2200 (set var value)))))
2202 (defun edebug--enter-trace (function args body)
2203 (let ((edebug-stack-depth (1+ edebug-stack-depth))
2204 edebug-result)
2205 (edebug-print-trace-before
2206 (format "%s args: %s" function args))
2207 (prog1 (setq edebug-result (funcall body))
2208 (edebug-print-trace-after
2209 (format "%s result: %s" function edebug-result)))))
2211 (def-edebug-spec edebug-tracing (form body))
2213 (defmacro edebug-tracing (msg &rest body)
2214 "Print MSG in *edebug-trace* before and after evaluating BODY.
2215 The result of BODY is also printed."
2216 `(let ((edebug-stack-depth (1+ edebug-stack-depth))
2217 edebug-result)
2218 (edebug-print-trace-before ,msg)
2219 (prog1 (setq edebug-result (progn ,@body))
2220 (edebug-print-trace-after
2221 (format "%s result: %s" ,msg edebug-result)))))
2223 (defun edebug-print-trace-before (msg)
2224 "Function called to print trace info before expression evaluation.
2225 MSG is printed after `::::{ '."
2226 (edebug-trace-display
2227 edebug-trace-buffer "%s{ %s" (make-string edebug-stack-depth ?\:) msg))
2229 (defun edebug-print-trace-after (msg)
2230 "Function called to print trace info after expression evaluation.
2231 MSG is printed after `::::} '."
2232 (edebug-trace-display
2233 edebug-trace-buffer "%s} %s" (make-string edebug-stack-depth ?\:) msg))
2237 (defun edebug-slow-before (before-index)
2238 (unless edebug-active
2239 ;; Debug current function given BEFORE position.
2240 ;; Called from functions compiled with edebug-eval-top-level-form.
2241 ;; Return the before index.
2242 (setcar edebug-offset-indices before-index)
2244 ;; Increment frequency count
2245 (aset edebug-freq-count before-index
2246 (1+ (aref edebug-freq-count before-index)))
2248 (if (or (not (memq edebug-execution-mode '(Go-nonstop next)))
2249 (input-pending-p))
2250 (edebug-debugger before-index 'before nil)))
2251 before-index)
2253 (defun edebug-fast-before (_before-index)
2254 ;; Do nothing.
2257 (defun edebug-slow-after (_before-index after-index value)
2258 (if edebug-active
2259 value
2260 ;; Debug current function given AFTER position and VALUE.
2261 ;; Called from functions compiled with edebug-eval-top-level-form.
2262 ;; Return VALUE.
2263 (setcar edebug-offset-indices after-index)
2265 ;; Increment frequency count
2266 (aset edebug-freq-count after-index
2267 (1+ (aref edebug-freq-count after-index)))
2268 (if edebug-test-coverage (edebug--update-coverage after-index value))
2270 (if (and (eq edebug-execution-mode 'Go-nonstop)
2271 (not (input-pending-p)))
2272 ;; Just return result.
2273 value
2274 (edebug-debugger after-index 'after value)
2277 (defun edebug-fast-after (_before-index _after-index value)
2278 ;; Do nothing but return the value.
2279 value)
2281 (defun edebug-run-slow ()
2282 (defalias 'edebug-before 'edebug-slow-before)
2283 (defalias 'edebug-after 'edebug-slow-after))
2285 ;; This is not used, yet.
2286 (defun edebug-run-fast ()
2287 (defalias 'edebug-before 'edebug-fast-before)
2288 (defalias 'edebug-after 'edebug-fast-after))
2290 (edebug-run-slow)
2293 (defun edebug--update-coverage (after-index value)
2294 (let ((old-result (aref edebug-coverage after-index)))
2295 (cond
2296 ((eq 'ok-coverage old-result))
2297 ((eq 'unknown old-result)
2298 (aset edebug-coverage after-index value))
2299 ;; Test if a different result.
2300 ((not (eq value old-result))
2301 (aset edebug-coverage after-index 'ok-coverage)))))
2304 ;; Dynamically declared unbound variables.
2305 (defvar edebug-breakpoints)
2306 (defvar edebug-break-data) ; break data for current function.
2307 (defvar edebug-break) ; whether a break occurred.
2308 (defvar edebug-global-break) ; whether a global break occurred.
2309 (defvar edebug-break-condition) ; whether the breakpoint is conditional.
2311 (defvar edebug-break-result nil)
2312 (defvar edebug-global-break-result nil)
2315 (defun edebug-debugger (offset-index arg-mode value)
2316 (if inhibit-redisplay
2317 ;; Don't really try to enter edebug within an eval from redisplay.
2318 value
2319 ;; Check breakpoints and pending input.
2320 ;; If edebug display should be updated, call edebug--display.
2321 ;; Return value.
2322 (let* ( ;; This needs to be here since breakpoints may be changed.
2323 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
2324 (edebug-break-data (assq offset-index edebug-breakpoints))
2325 (edebug-break-condition (car (cdr edebug-break-data)))
2326 (edebug-global-break
2327 (if edebug-global-break-condition
2328 (condition-case nil
2329 (setq edebug-global-break-result
2330 (edebug-eval edebug-global-break-condition))
2331 (error nil))))
2332 (edebug-break))
2334 ;;(edebug-trace "exp: %s" value)
2335 ;; Test whether we should break.
2336 (setq edebug-break
2337 (or edebug-global-break
2338 (and edebug-break-data
2339 (or (not edebug-break-condition)
2340 (setq edebug-break-result
2341 (edebug-eval edebug-break-condition))))))
2342 (if (and edebug-break
2343 (nth 2 edebug-break-data)) ; is it temporary?
2344 ;; Delete the breakpoint.
2345 (setcdr edebug-data
2346 (cons (delq edebug-break-data edebug-breakpoints)
2347 (cdr (cdr edebug-data)))))
2349 ;; Display if mode is not go, continue, or Continue-fast
2350 ;; or break, or input is pending,
2351 (if (or (not (memq edebug-execution-mode '(go continue Continue-fast)))
2352 edebug-break
2353 (input-pending-p))
2354 (edebug--display value offset-index arg-mode)) ; <---------- display
2356 value)))
2359 ;; window-start now stored with each function.
2360 ;;(defvar edebug-window-start nil)
2361 ;; Remember where each buffers' window starts between edebug calls.
2362 ;; This is to avoid spurious recentering.
2363 ;; Does this still need to be buffer-local??
2364 ;;(setq-default edebug-window-start nil)
2365 ;;(make-variable-buffer-local 'edebug-window-start)
2368 ;; Dynamically declared unbound vars
2369 (defvar edebug-point) ; the point in edebug buffer
2370 (defvar edebug-outside-buffer) ; the current-buffer outside of edebug
2371 (defvar edebug-outside-point) ; the point outside of edebug
2372 (defvar edebug-outside-mark) ; the mark outside of edebug
2373 (defvar edebug-window-data) ; window and window-start for current function
2374 (defvar edebug-outside-windows) ; outside window configuration
2375 (defvar edebug-eval-buffer) ; for the evaluation list.
2376 (defvar edebug-outside-d-c-i-n-s-w) ; outside default cursor-in-non-selected-windows
2378 (defvar edebug-eval-list nil) ;; List of expressions to evaluate.
2380 (defvar edebug-previous-result nil) ;; Last result returned.
2382 ;; Emacs 19 adds an arg to mark and mark-marker.
2383 (defalias 'edebug-mark-marker 'mark-marker)
2385 (defun edebug--display (value offset-index arg-mode)
2386 ;; edebug--display-1 is too big, we should split it. This function
2387 ;; here was just introduced to avoid making edebug--display-1
2388 ;; yet a bit deeper.
2389 (save-excursion (edebug--display-1 value offset-index arg-mode)))
2391 (defun edebug--display-1 (value offset-index arg-mode)
2392 (unless (marker-position edebug-def-mark)
2393 ;; The buffer holding the source has been killed.
2394 ;; Let's at least show a backtrace so the user can figure out
2395 ;; which function we're talking about.
2396 (debug))
2397 ;; Setup windows for edebug, determine mode, maybe enter recursive-edit.
2398 ;; Uses local variables of edebug-enter, edebug-before, edebug-after
2399 ;; and edebug-debugger.
2400 (let ((edebug-active t) ; For minor mode alist.
2401 (edebug-with-timeout-suspend (with-timeout-suspend))
2402 edebug-stop ; Should we enter recursive-edit?
2403 (edebug-point (+ edebug-def-mark
2404 (aref (nth 2 edebug-data) offset-index)))
2405 edebug-buffer-outside-point ; current point in edebug-buffer
2406 ;; window displaying edebug-buffer
2407 (edebug-window-data (nth 3 edebug-data))
2408 (edebug-outside-window (selected-window))
2409 (edebug-outside-buffer (current-buffer))
2410 (edebug-outside-point (point))
2411 (edebug-outside-mark (edebug-mark))
2412 edebug-outside-windows ; Window or screen configuration.
2413 edebug-buffer-points
2415 edebug-eval-buffer ; Declared here so we can kill it below.
2416 (eval-result-list (and edebug-eval-list
2417 (edebug-eval-result-list)))
2418 edebug-trace-window
2419 edebug-trace-window-start
2421 (edebug-outside-d-c-i-n-s-w
2422 (default-value 'cursor-in-non-selected-windows)))
2423 (unwind-protect
2424 (let ((cursor-in-echo-area nil)
2425 (unread-command-events nil)
2426 ;; any others??
2428 (setq-default cursor-in-non-selected-windows t)
2429 (if (not (buffer-name edebug-buffer))
2430 (user-error "Buffer defining %s not found" edebug-function))
2432 (if (eq 'after arg-mode)
2433 ;; Compute result string now before windows are modified.
2434 (edebug-compute-previous-result value))
2436 (if edebug-save-windows
2437 ;; Save windows now before we modify them.
2438 (setq edebug-outside-windows
2439 (edebug-current-windows edebug-save-windows)))
2441 (if edebug-save-displayed-buffer-points
2442 (setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
2444 ;; First move the edebug buffer point to edebug-point
2445 ;; so that window start doesn't get changed when we display it.
2446 ;; I don't know if this is going to help.
2447 ;;(set-buffer edebug-buffer)
2448 ;;(goto-char edebug-point)
2450 ;; If edebug-buffer is not currently displayed,
2451 ;; first find a window for it.
2452 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data))
2453 (setcar edebug-window-data (selected-window))
2455 ;; Now display eval list, if any.
2456 ;; This is done after the pop to edebug-buffer
2457 ;; so that buffer-window correspondence is correct after quitting.
2458 (edebug-eval-display eval-result-list)
2459 ;; The evaluation list better not have deleted edebug-window-data.
2460 (select-window (car edebug-window-data))
2461 (set-buffer edebug-buffer)
2463 (setq edebug-buffer-outside-point (point))
2464 (goto-char edebug-point)
2466 (if (eq 'before arg-mode)
2467 ;; Check whether positions are up-to-date.
2468 ;; This assumes point is never before symbol.
2469 (if (not (memq (following-char) '(?\( ?\# ?\` )))
2470 (user-error "Source has changed - reevaluate definition of %s"
2471 edebug-function)
2474 ;; Make sure we bind those in the right buffer (bug#16410).
2475 (let ((overlay-arrow-position overlay-arrow-position)
2476 (overlay-arrow-string overlay-arrow-string))
2477 ;; Now display arrow based on mode.
2478 (edebug-overlay-arrow)
2480 (cond
2481 ((eq 'error arg-mode)
2482 ;; Display error message
2483 (setq edebug-execution-mode 'step)
2484 (edebug-overlay-arrow)
2485 (beep)
2486 (if (eq 'quit (car value))
2487 (message "Quit")
2488 (edebug-report-error value)))
2489 (edebug-break
2490 (cond
2491 (edebug-global-break
2492 (message "Global Break: %s => %s"
2493 edebug-global-break-condition
2494 edebug-global-break-result))
2495 (edebug-break-condition
2496 (message "Break: %s => %s"
2497 edebug-break-condition
2498 edebug-break-result))
2499 ((not (eq edebug-execution-mode 'Continue-fast))
2500 (message "Break"))
2501 (t)))
2503 (t (message "")))
2505 (if (eq 'after arg-mode)
2506 (progn
2507 ;; Display result of previous evaluation.
2508 (if (and edebug-break
2509 edebug-sit-on-break
2510 (not (eq edebug-execution-mode 'Continue-fast)))
2511 (sit-for edebug-sit-for-seconds)) ; Show message.
2512 (edebug-previous-result)))
2514 (cond
2515 (edebug-break
2516 (cond
2517 ((eq edebug-execution-mode 'continue)
2518 (sit-for edebug-sit-for-seconds))
2519 ((eq edebug-execution-mode 'Continue-fast) (sit-for 0))
2520 (t (setq edebug-stop t))))
2521 ;; not edebug-break
2522 ((eq edebug-execution-mode 'trace)
2523 (sit-for edebug-sit-for-seconds)) ; Force update and pause.
2524 ((eq edebug-execution-mode 'Trace-fast)
2525 (sit-for 0))) ; Force update and continue.
2527 (when (input-pending-p)
2528 (setq edebug-stop t)
2529 (setq edebug-execution-mode 'step) ; for `edebug-overlay-arrow'
2530 (edebug-stop))
2532 (edebug-overlay-arrow)
2534 (unwind-protect
2535 (if (or edebug-stop
2536 (memq edebug-execution-mode '(step next))
2537 (eq arg-mode 'error))
2538 (edebug--recursive-edit arg-mode)) ; <--- Recursive edit
2540 ;; Reset the edebug-window-data to whatever it is now.
2541 (let ((window (if (eq (window-buffer) edebug-buffer)
2542 (selected-window)
2543 (get-buffer-window edebug-buffer))))
2544 ;; Remember window-start for edebug-buffer, if still displayed.
2545 (if window
2546 (progn
2547 (setcar edebug-window-data window)
2548 (setcdr edebug-window-data (window-start window)))))
2550 ;; Save trace window point before restoring outside windows.
2551 ;; Could generalize this for other buffers.
2552 (setq edebug-trace-window
2553 (get-buffer-window edebug-trace-buffer))
2554 (if edebug-trace-window
2555 (setq edebug-trace-window-start
2556 (and edebug-trace-window
2557 (window-start edebug-trace-window))))
2559 ;; Restore windows before continuing.
2560 (if edebug-save-windows
2561 (progn
2562 (edebug-set-windows edebug-outside-windows)
2564 ;; Restore displayed buffer points.
2565 ;; Needed even if restoring windows because
2566 ;; window-points are not restored. (should they be??)
2567 (if edebug-save-displayed-buffer-points
2568 (edebug-set-buffer-points edebug-buffer-points))
2570 ;; Unrestore trace window's window-point.
2571 (if edebug-trace-window
2572 (set-window-start edebug-trace-window
2573 edebug-trace-window-start))
2575 ;; Unrestore edebug-buffer's window-start, if displayed.
2576 (let ((window (car edebug-window-data)))
2577 (if (and (edebug-window-live-p window)
2578 (eq (window-buffer) edebug-buffer))
2579 (progn
2580 (set-window-start window (cdr edebug-window-data)
2581 'no-force)
2582 ;; Unrestore edebug-buffer's window-point.
2583 ;; Needed in addition to setting the buffer point
2584 ;; - otherwise quitting doesn't leave point as is.
2585 ;; But can this causes point to not be restored.
2586 ;; Also, it may not be a visible window.
2587 ;; (set-window-point window edebug-point)
2590 ;; Unrestore edebug-buffer's point. Rerestored below.
2591 ;; (goto-char edebug-point) ;; in edebug-buffer
2593 ;; Since we may be in a save-excursion, in case of quit,
2594 ;; reselect the outside window only.
2595 ;; Only needed if we are not recovering windows??
2596 (if (edebug-window-live-p edebug-outside-window)
2597 (select-window edebug-outside-window))
2598 ) ; if edebug-save-windows
2600 ;; Restore current buffer always, in case application needs it.
2601 (if (buffer-name edebug-outside-buffer)
2602 (set-buffer edebug-outside-buffer))
2603 ;; Restore point, and mark.
2604 ;; Needed even if restoring windows because
2605 ;; that doesn't restore point and mark in the current buffer.
2606 ;; But don't restore point if edebug-buffer is current buffer.
2607 (if (not (eq edebug-buffer edebug-outside-buffer))
2608 (goto-char edebug-outside-point))
2609 (if (marker-buffer (edebug-mark-marker))
2610 ;; Does zmacs-regions need to be nil while doing set-marker?
2611 (set-marker (edebug-mark-marker) edebug-outside-mark))
2612 )) ; unwind-protect
2613 ;; None of the following is done if quit or signal occurs.
2615 ;; Restore edebug-buffer's outside point.
2616 ;; (edebug-trace "restore edebug-buffer point: %s"
2617 ;; edebug-buffer-outside-point)
2618 (with-current-buffer edebug-buffer
2619 (goto-char edebug-buffer-outside-point))
2620 ;; ... nothing more.
2622 ;; Could be an option to keep eval display up.
2623 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
2624 (with-timeout-unsuspend edebug-with-timeout-suspend)
2625 ;; Reset global variables to outside values in case they were changed.
2626 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
2630 (defvar edebug-number-of-recursions 0)
2631 ;; Number of recursive edits started by edebug.
2632 ;; Should be 0 at the top level.
2634 (defvar edebug-recursion-depth 0)
2635 ;; Value of recursion-depth when edebug was called.
2637 ;; Dynamically declared unbound vars
2638 (defvar edebug-outside-match-data) ; match data outside of edebug
2639 (defvar edebug-backtrace-buffer) ; each recursive edit gets its own
2640 (defvar edebug-inside-windows)
2641 (defvar edebug-interactive-p)
2643 (defun edebug--recursive-edit (arg-mode)
2644 ;; Start up a recursive edit inside of edebug.
2645 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
2646 ;; Assume that none of the variables below are buffer-local.
2647 (let (;; match-data must be done in the outside buffer
2648 (edebug-outside-match-data
2649 (with-current-buffer edebug-outside-buffer ; in case match buffer different
2650 (match-data)))
2652 ;;(edebug-number-of-recursions (1+ edebug-number-of-recursions))
2653 (edebug-recursion-depth (recursion-depth))
2654 edebug-entered ; bind locally to nil
2655 (edebug-interactive-p nil) ; again non-interactive
2656 edebug-backtrace-buffer ; each recursive edit gets its own
2657 ;; The window configuration may be saved and restored
2658 ;; during a recursive-edit
2659 edebug-inside-windows
2662 (unwind-protect
2663 (let (
2664 ;; Declare global values local but using the same global value.
2665 ;; We could set these to the values for previous edebug call.
2666 (last-command last-command)
2667 (this-command this-command)
2668 (current-prefix-arg nil)
2670 ;; More for Emacs 19
2671 (last-input-event nil)
2672 (last-command-event nil)
2673 (last-event-frame nil)
2674 (last-nonmenu-event nil)
2675 (track-mouse nil)
2677 (standard-output t)
2678 (standard-input t)
2680 ;; Don't keep reading from an executing kbd macro
2681 ;; within edebug unless edebug-continue-kbd-macro is
2682 ;; non-nil. Again, local binding may not be best.
2683 (executing-kbd-macro
2684 (if edebug-continue-kbd-macro executing-kbd-macro))
2686 ;; Don't get confused by the user's keymap changes.
2687 (overriding-local-map nil)
2688 (overriding-terminal-local-map nil)
2690 ;; Bind again to outside values.
2691 (debug-on-error edebug-outside-debug-on-error)
2692 (debug-on-quit edebug-outside-debug-on-quit)
2694 ;; Don't keep defining a kbd macro.
2695 (defining-kbd-macro
2696 (if edebug-continue-kbd-macro defining-kbd-macro))
2698 ;; others??
2701 (if (and (eq edebug-execution-mode 'go)
2702 (not (memq arg-mode '(after error))))
2703 (message "Break"))
2705 (setq signal-hook-function nil)
2707 (edebug-mode 1)
2708 (unwind-protect
2709 (recursive-edit) ; <<<<<<<<<< Recursive edit
2711 ;; Do the following, even if quit occurs.
2712 (setq signal-hook-function 'edebug-signal)
2713 (if edebug-backtrace-buffer
2714 (kill-buffer edebug-backtrace-buffer))
2716 ;; Remember selected-window after recursive-edit.
2717 ;; (setq edebug-inside-window (selected-window))
2719 (set-match-data edebug-outside-match-data)
2721 ;; Recursive edit may have changed buffers,
2722 ;; so set it back before exiting let.
2723 (if (buffer-name edebug-buffer) ; if it still exists
2724 (progn
2725 (set-buffer edebug-buffer)
2726 (when (memq edebug-execution-mode '(go Go-nonstop))
2727 (edebug-overlay-arrow)
2728 (sit-for 0))
2729 (edebug-mode -1))
2730 ;; gotta have a buffer to let its buffer local variables be set
2731 (get-buffer-create " bogus edebug buffer"))
2732 ));; inner let
2736 ;;; Display related functions
2738 (defconst edebug-arrow-alist
2739 '((Continue-fast . "=")
2740 (Trace-fast . "-")
2741 (continue . ">")
2742 (trace . "->")
2743 (step . "=>")
2744 (next . "=>")
2745 (go . "<>")
2746 (Go-nonstop . "..")
2748 "Association list of arrows for each edebug mode.")
2750 (defun edebug-overlay-arrow ()
2751 ;; Set up the overlay arrow at beginning-of-line in current buffer.
2752 ;; The arrow string is derived from edebug-arrow-alist and
2753 ;; edebug-execution-mode.
2754 (let ((pos (line-beginning-position)))
2755 (setq overlay-arrow-string
2756 (cdr (assq edebug-execution-mode edebug-arrow-alist)))
2757 (setq overlay-arrow-position (make-marker))
2758 (set-marker overlay-arrow-position pos (current-buffer))))
2761 (defun edebug-toggle-save-all-windows ()
2762 "Toggle the saving and restoring of all windows.
2763 Also, each time you toggle it on, the inside and outside window
2764 configurations become the same as the current configuration."
2765 (interactive)
2766 (setq edebug-save-windows (not edebug-save-windows))
2767 (if edebug-save-windows
2768 (setq edebug-inside-windows
2769 (setq edebug-outside-windows
2770 (edebug-current-windows
2771 edebug-save-windows))))
2772 (message "Window saving is %s for all windows."
2773 (if edebug-save-windows "on" "off")))
2775 (defmacro edebug-changing-windows (&rest body)
2776 `(let ((window (selected-window)))
2777 (setq edebug-inside-windows (edebug-current-windows t))
2778 (edebug-set-windows edebug-outside-windows)
2779 ,@body;; Code to change edebug-save-windows
2780 (setq edebug-outside-windows (edebug-current-windows
2781 edebug-save-windows))
2782 ;; Problem: what about outside windows that are deleted inside?
2783 (edebug-set-windows edebug-inside-windows)))
2785 (defun edebug-toggle-save-selected-window ()
2786 "Toggle the saving and restoring of the selected window.
2787 Also, each time you toggle it on, the inside and outside window
2788 configurations become the same as the current configuration."
2789 (interactive)
2790 (cond
2791 ((eq t edebug-save-windows)
2792 ;; Save all outside windows except the selected one.
2793 ;; Remove (selected-window) from outside-windows.
2794 (edebug-changing-windows
2795 (setq edebug-save-windows (delq window (edebug-window-list)))))
2797 ((memq (selected-window) edebug-save-windows)
2798 (setq edebug-outside-windows
2799 (delq (assq (selected-window) edebug-outside-windows)
2800 edebug-outside-windows))
2801 (setq edebug-save-windows
2802 (delq (selected-window) edebug-save-windows)))
2803 (t ; Save a new window.
2804 (edebug-changing-windows
2805 (setq edebug-save-windows (cons window edebug-save-windows)))))
2807 (message "Window saving is %s for %s."
2808 (if (memq (selected-window) edebug-save-windows)
2809 "on" "off")
2810 (selected-window)))
2812 (defun edebug-toggle-save-windows (arg)
2813 "Toggle the saving and restoring of windows.
2814 With prefix, toggle for just the selected window.
2815 Otherwise, toggle for all windows."
2816 (interactive "P")
2817 (if arg
2818 (edebug-toggle-save-selected-window)
2819 (edebug-toggle-save-all-windows)))
2821 (defun edebug-where ()
2822 "Show the debug windows and where we stopped in the program."
2823 (interactive)
2824 (if (not edebug-active)
2825 (error "Edebug is not active"))
2826 ;; Restore the window configuration to what it last was inside.
2827 ;; But it is not always set. - experiment
2828 ;;(if edebug-inside-windows
2829 ;; (edebug-set-windows edebug-inside-windows))
2830 (edebug-pop-to-buffer edebug-buffer)
2831 (goto-char edebug-point))
2833 (defun edebug-view-outside ()
2834 "Change to the outside window configuration.
2835 Use `edebug-where' to return."
2836 (interactive)
2837 (if (not edebug-active)
2838 (error "Edebug is not active"))
2839 (setq edebug-inside-windows
2840 (edebug-current-windows edebug-save-windows))
2841 (edebug-set-windows edebug-outside-windows)
2842 (goto-char edebug-outside-point)
2843 (message "Window configuration outside of Edebug. Return with %s"
2844 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
2847 (defun edebug-bounce-point (arg)
2848 "Bounce the point in the outside current buffer.
2849 If prefix argument ARG is supplied, sit for that many seconds
2850 before returning. The default is one second."
2851 (interactive "p")
2852 (if (not edebug-active)
2853 (error "Edebug is not active"))
2854 (save-excursion
2855 ;; If the buffer's currently displayed, avoid set-window-configuration.
2856 (save-window-excursion
2857 (edebug-pop-to-buffer edebug-outside-buffer)
2858 (goto-char edebug-outside-point)
2859 (message "Current buffer: %s Point: %s Mark: %s"
2860 (current-buffer) (point)
2861 (if (marker-buffer (edebug-mark-marker))
2862 (marker-position (edebug-mark-marker)) "<not set>"))
2863 (sit-for arg)
2864 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data)))))
2867 ;; Joe Wells, here is a start at your idea of adding a buffer to the internal
2868 ;; display list. Still need to use this list in edebug--display.
2870 '(defvar edebug-display-buffer-list nil
2871 "List of buffers that edebug will display when it is active.")
2873 '(defun edebug-display-buffer (buffer)
2874 "Toggle display of a buffer inside of edebug."
2875 (interactive "bBuffer: ")
2876 (let ((already-displaying (memq buffer edebug-display-buffer-list)))
2877 (setq edebug-display-buffer-list
2878 (if already-displaying
2879 (delq buffer edebug-display-buffer-list)
2880 (cons buffer edebug-display-buffer-list)))
2881 (message "Displaying %s %s" buffer
2882 (if already-displaying "off" "on"))))
2884 ;;; Breakpoint related functions
2886 (defun edebug-find-stop-point ()
2887 ;; Return (function . index) of the nearest edebug stop point.
2888 (let* ((edebug-def-name (edebug-form-data-symbol))
2889 (edebug-data
2890 (let ((data (get edebug-def-name 'edebug)))
2891 (if (or (null data) (markerp data))
2892 (error "%s is not instrumented for Edebug" edebug-def-name))
2893 data)) ; we could do it automatically, if data is a marker.
2894 ;; pull out parts of edebug-data.
2895 (edebug-def-mark (car edebug-data))
2896 ;; (edebug-breakpoints (car (cdr edebug-data)))
2898 (offset-vector (nth 2 edebug-data))
2899 (offset (- (save-excursion
2900 (if (looking-at "[ \t]")
2901 ;; skip backwards until non-whitespace, or bol
2902 (skip-chars-backward " \t"))
2903 (point))
2904 edebug-def-mark))
2905 len i)
2906 ;; the offsets are in order so we can do a linear search
2907 (setq len (length offset-vector))
2908 (setq i 0)
2909 (while (and (< i len) (> offset (aref offset-vector i)))
2910 (setq i (1+ i)))
2911 (if (and (< i len)
2912 (<= offset (aref offset-vector i)))
2913 ;; return the relevant info
2914 (cons edebug-def-name i)
2915 (message "Point is not on an expression in %s."
2916 edebug-def-name)
2920 (defun edebug-next-breakpoint ()
2921 "Move point to the next breakpoint, or first if none past point."
2922 (interactive)
2923 (let ((edebug-stop-point (edebug-find-stop-point)))
2924 (if edebug-stop-point
2925 (let* ((edebug-def-name (car edebug-stop-point))
2926 (index (cdr edebug-stop-point))
2927 (edebug-data (get edebug-def-name 'edebug))
2929 ;; pull out parts of edebug-data
2930 (edebug-def-mark (car edebug-data))
2931 (edebug-breakpoints (car (cdr edebug-data)))
2932 (offset-vector (nth 2 edebug-data))
2933 breakpoint)
2934 (if (not edebug-breakpoints)
2935 (message "No breakpoints in this function.")
2936 (let ((breaks edebug-breakpoints))
2937 (while (and breaks
2938 (<= (car (car breaks)) index))
2939 (setq breaks (cdr breaks)))
2940 (setq breakpoint
2941 (if breaks
2942 (car breaks)
2943 ;; goto the first breakpoint
2944 (car edebug-breakpoints)))
2945 (goto-char (+ edebug-def-mark
2946 (aref offset-vector (car breakpoint))))
2948 (message "%s"
2949 (concat (if (nth 2 breakpoint)
2950 "Temporary " "")
2951 (if (car (cdr breakpoint))
2952 (format "Condition: %s"
2953 (edebug-safe-prin1-to-string
2954 (car (cdr breakpoint))))
2955 "")))
2956 ))))))
2959 (defun edebug-modify-breakpoint (flag &optional condition temporary)
2960 "Modify the breakpoint for the form at point or after it.
2961 Set it if FLAG is non-nil, clear it otherwise. Then move to that point.
2962 If CONDITION or TEMPORARY are non-nil, add those attributes to
2963 the breakpoint."
2964 (let ((edebug-stop-point (edebug-find-stop-point)))
2965 (if edebug-stop-point
2966 (let* ((edebug-def-name (car edebug-stop-point))
2967 (index (cdr edebug-stop-point))
2968 (edebug-data (get edebug-def-name 'edebug))
2970 ;; pull out parts of edebug-data
2971 (edebug-def-mark (car edebug-data))
2972 (edebug-breakpoints (car (cdr edebug-data)))
2973 (offset-vector (nth 2 edebug-data))
2974 present)
2975 ;; delete it either way
2976 (setq present (assq index edebug-breakpoints))
2977 (setq edebug-breakpoints (delq present edebug-breakpoints))
2978 (if flag
2979 (progn
2980 ;; add it to the list and resort
2981 (setq edebug-breakpoints
2982 (edebug-sort-alist
2983 (cons
2984 (list index condition temporary)
2985 edebug-breakpoints) '<))
2986 (if condition
2987 (message "Breakpoint set in %s with condition: %s"
2988 edebug-def-name condition)
2989 (message "Breakpoint set in %s" edebug-def-name)))
2990 (if present
2991 (message "Breakpoint unset in %s" edebug-def-name)
2992 (message "No breakpoint here")))
2994 (setcar (cdr edebug-data) edebug-breakpoints)
2995 (goto-char (+ edebug-def-mark (aref offset-vector index)))
2996 ))))
2998 (defun edebug-set-breakpoint (arg)
2999 "Set the breakpoint of nearest sexp.
3000 With prefix argument, make it a temporary breakpoint."
3001 (interactive "P")
3002 (edebug-modify-breakpoint t nil arg))
3004 (defun edebug-unset-breakpoint ()
3005 "Clear the breakpoint of nearest sexp."
3006 (interactive)
3007 (edebug-modify-breakpoint nil))
3010 (defun edebug-set-global-break-condition (expression)
3011 "Set `edebug-global-break-condition' to EXPRESSION."
3012 (interactive
3013 (list
3014 (let ((initial (and edebug-global-break-condition
3015 (format "%s" edebug-global-break-condition))))
3016 (read-from-minibuffer
3017 "Global Condition: " initial read-expression-map t
3018 (if (equal (car read-expression-history) initial)
3019 '(read-expression-history . 1)
3020 'read-expression-history)))))
3021 (setq edebug-global-break-condition expression))
3024 ;;; Mode switching functions
3026 (defun edebug-set-mode (mode shortmsg msg)
3027 ;; Set the edebug mode to MODE.
3028 ;; Display SHORTMSG, or MSG if not within edebug.
3029 (if (eq (1+ edebug-recursion-depth) (recursion-depth))
3030 (progn
3031 (setq edebug-execution-mode mode)
3032 (message "%s" shortmsg)
3033 ;; Continue execution
3034 (exit-recursive-edit))
3035 ;; This is not terribly useful!!
3036 (setq edebug-next-execution-mode mode)
3037 (message "%s" msg)))
3040 (defalias 'edebug-step-through-mode 'edebug-step-mode)
3042 (defun edebug-step-mode ()
3043 "Proceed to next stop point."
3044 (interactive)
3045 (edebug-set-mode 'step "" "Edebug will stop at next stop point."))
3047 (defun edebug-next-mode ()
3048 "Proceed to next `after' stop point."
3049 (interactive)
3050 (edebug-set-mode 'next "" "Edebug will stop after next eval."))
3052 (defun edebug-go-mode (arg)
3053 "Go, evaluating until break.
3054 With prefix ARG, set temporary break at current point and go."
3055 (interactive "P")
3056 (if arg
3057 (edebug-set-breakpoint t))
3058 (edebug-set-mode 'go "Go..." "Edebug will go until break."))
3060 (defun edebug-Go-nonstop-mode ()
3061 "Go, evaluating without debugging.
3062 You can use `edebug-stop', or any editing command, to stop."
3063 (interactive)
3064 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
3065 "Edebug will not stop at breaks."))
3068 (defun edebug-trace-mode ()
3069 "Begin trace mode.
3070 Pauses for `edebug-sit-for-seconds' at each stop point."
3071 (interactive)
3072 (edebug-set-mode 'trace "Tracing..." "Edebug will trace with pause."))
3074 (defun edebug-Trace-fast-mode ()
3075 "Trace with no wait at each step.
3076 Updates the display at each stop point, but does not pause."
3077 (interactive)
3078 (edebug-set-mode 'Trace-fast
3079 "Trace fast..." "Edebug will trace without pause."))
3081 (defun edebug-continue-mode ()
3082 "Begin continue mode.
3083 Pauses for `edebug-sit-for-seconds' at each break point."
3084 (interactive)
3085 (edebug-set-mode 'continue "Continue..."
3086 "Edebug will pause at breakpoints."))
3088 (defun edebug-Continue-fast-mode ()
3089 "Trace with no wait at each step.
3090 Updates the display at each break point, but does not pause."
3091 (interactive)
3092 (edebug-set-mode 'Continue-fast "Continue fast..."
3093 "Edebug will stop and go at breakpoints."))
3095 ;; ------------------------------------------------------------
3096 ;; The following use the mode changing commands and breakpoints.
3099 (defun edebug-goto-here ()
3100 "Proceed to first stop-point at or after current position of point."
3101 (interactive)
3102 (edebug-go-mode t))
3105 (defun edebug-stop ()
3106 "Stop execution and do not continue.
3107 Useful for exiting from trace or continue loop."
3108 (interactive)
3109 (message "Stop"))
3112 '(defun edebug-forward ()
3113 "Proceed to the exit of the next expression to be evaluated."
3114 (interactive)
3115 (edebug-set-mode
3116 'forward "Forward"
3117 "Edebug will stop after exiting the next expression."))
3120 (defun edebug-forward-sexp (arg)
3121 "Proceed from the current point to the end of the ARGth sexp ahead.
3122 If there are not ARG sexps ahead, then do `edebug-step-out'."
3123 (interactive "p")
3124 (condition-case nil
3125 (let ((parse-sexp-ignore-comments t))
3126 ;; Call forward-sexp repeatedly until done or failure.
3127 (forward-sexp arg)
3128 (edebug-go-mode t))
3129 (error
3130 (edebug-step-out)
3133 (defun edebug-step-out ()
3134 "Proceed from the current point to the end of the containing sexp.
3135 If there is no containing sexp that is not the top level defun,
3136 go to the end of the last sexp, or if that is the same point, then step."
3137 (interactive)
3138 (condition-case nil
3139 (let ((parse-sexp-ignore-comments t))
3140 (up-list 1)
3141 (save-excursion
3142 ;; Is there still a containing expression?
3143 (up-list 1))
3144 (edebug-go-mode t))
3145 (error
3146 ;; At top level - 1, so first check if there are more sexps at this level.
3147 (let ((start-point (point)))
3148 ;; (up-list 1)
3149 (down-list -1)
3150 (if (= (point) start-point)
3151 (edebug-step-mode) ; No more at this level, so step.
3152 (edebug-go-mode t)
3153 )))))
3155 (defun edebug-instrument-function (func)
3156 ;; Func should be a function symbol.
3157 ;; Return the function symbol, or nil if not instrumented.
3158 (let ((func-marker (get func 'edebug)))
3159 (cond
3160 ((and (markerp func-marker) (marker-buffer func-marker))
3161 ;; It is uninstrumented, so instrument it.
3162 (with-current-buffer (marker-buffer func-marker)
3163 (goto-char func-marker)
3164 (edebug-eval-top-level-form)
3165 func))
3166 ((consp func-marker)
3167 (message "%s is already instrumented." func)
3168 func)
3170 (let ((loc (find-function-noselect func t)))
3171 (unless (cdr loc)
3172 (error "Could not find the definition in its file"))
3173 (with-current-buffer (car loc)
3174 (goto-char (cdr loc))
3175 (edebug-eval-top-level-form)
3176 func))))))
3178 (defun edebug-instrument-callee ()
3179 "Instrument the definition of the function or macro about to be called.
3180 Do this when stopped before the form or it will be too late.
3181 One side effect of using this command is that the next time the
3182 function or macro is called, Edebug will be called there as well."
3183 (interactive)
3184 (if (not (looking-at "("))
3185 (error "You must be before a list form")
3186 (let ((func
3187 (save-excursion
3188 (down-list 1)
3189 (if (looking-at "(")
3190 (edebug--form-data-name
3191 (edebug-get-form-data-entry (point)))
3192 (read (current-buffer))))))
3193 (edebug-instrument-function func))))
3196 (defun edebug-step-in ()
3197 "Step into the definition of the function or macro about to be called.
3198 This first does `edebug-instrument-callee' to ensure that it is
3199 instrumented. Then it does `edebug-on-entry' and switches to `go' mode."
3200 (interactive)
3201 (let ((func (edebug-instrument-callee)))
3202 (if func
3203 (progn
3204 (edebug-on-entry func 'temp)
3205 (edebug-go-mode nil)))))
3207 (defun edebug-on-entry (function &optional flag)
3208 "Cause Edebug to stop when FUNCTION is called.
3209 With prefix argument, make this temporary so it is automatically
3210 canceled the first time the function is entered."
3211 (interactive "aEdebug on entry to: \nP")
3212 ;; Could store this in the edebug data instead.
3213 (put function 'edebug-on-entry (if flag 'temp t)))
3215 (defun cancel-edebug-on-entry (function)
3216 (interactive "aEdebug on entry to: ")
3217 (put function 'edebug-on-entry nil))
3220 '(advice-add 'debug-on-entry :around 'edebug--debug-on-entry) ;; Should we do this?
3221 ;; Also need edebug-cancel-debug-on-entry
3223 '(defun edebug--debug-on-entry (orig function)
3224 "If the function is instrumented for Edebug, call `edebug-on-entry'."
3225 (let ((func-data (get function 'edebug)))
3226 (if (or (null func-data) (markerp func-data))
3227 (funcall orig function)
3228 (edebug-on-entry function))))
3231 (defun edebug-top-level-nonstop ()
3232 "Set mode to Go-nonstop, and exit to top-level.
3233 This is useful for exiting even if `unwind-protect' code may be executed."
3234 (interactive)
3235 (setq edebug-execution-mode 'Go-nonstop)
3236 (top-level))
3238 ;;(defun edebug-exit-out ()
3239 ;; "Go until the current function exits."
3240 ;; (interactive)
3241 ;; (edebug-set-mode 'exiting "Exit..."))
3243 (defconst edebug-initial-mode-alist
3244 '((edebug-step-mode . step)
3245 (edebug-next-mode . next)
3246 (edebug-trace-mode . trace)
3247 (edebug-Trace-fast-mode . Trace-fast)
3248 (edebug-go-mode . go)
3249 (edebug-continue-mode . continue)
3250 (edebug-Continue-fast-mode . Continue-fast)
3251 (edebug-Go-nonstop-mode . Go-nonstop))
3252 "Association list between commands and the modes they set.")
3254 (defvar edebug-mode-map) ; will be defined fully later.
3256 (defun edebug-set-initial-mode ()
3257 "Set the initial execution mode of Edebug.
3258 The mode is requested via the key that would be used to set the mode in
3259 edebug-mode."
3260 (interactive)
3261 (let* ((old-mode edebug-initial-mode)
3262 (key (read-key-sequence
3263 (format
3264 "Change initial edebug mode from %s (%c) to (enter key): "
3265 old-mode
3266 (aref (where-is-internal
3267 (car (rassq old-mode edebug-initial-mode-alist))
3268 edebug-mode-map 'firstonly)
3269 0))))
3270 (mode (cdr (assq (lookup-key edebug-mode-map key)
3271 edebug-initial-mode-alist))))
3272 (if mode
3273 (progn
3274 (setq edebug-initial-mode mode)
3275 (message "Edebug's initial mode is now: %s" mode))
3276 (error "Key must map to one of the mode changing commands"))))
3278 ;;; Evaluation of expressions
3280 (defmacro edebug-outside-excursion (&rest body)
3281 "Evaluate an expression list in the outside context.
3282 Return the result of the last expression."
3283 ;; Only restores the non-variables context since all the variables let-bound
3284 ;; by Edebug will be properly reset to the appropriate context's value by
3285 ;; backtrace-eval.
3286 (declare (debug t))
3287 `(save-excursion ; of current-buffer
3288 (if edebug-save-windows
3289 (progn
3290 ;; After excursion, we will
3291 ;; restore to current window configuration.
3292 (setq edebug-inside-windows
3293 (edebug-current-windows edebug-save-windows))
3294 ;; Restore outside windows.
3295 (edebug-set-windows edebug-outside-windows)))
3297 (set-buffer edebug-buffer) ; why?
3298 (set-match-data edebug-outside-match-data)
3299 ;; Restore outside context.
3300 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
3301 (unwind-protect
3302 ;; FIXME: This restoring of edebug-outside-buffer and
3303 ;; edebug-outside-point is redundant now that backtrace-eval does it
3304 ;; for us.
3305 (with-current-buffer edebug-outside-buffer ; of edebug-buffer
3306 (goto-char edebug-outside-point)
3307 (if (marker-buffer (edebug-mark-marker))
3308 (set-marker (edebug-mark-marker) edebug-outside-mark))
3309 ,@body)
3311 ;; Back to edebug-buffer. Restore rest of inside context.
3312 ;; (use-local-map edebug-inside-map)
3313 (if edebug-save-windows
3314 ;; Restore inside windows.
3315 (edebug-set-windows edebug-inside-windows))
3317 ;; Save values that may have been changed.
3318 (setq edebug-outside-d-c-i-n-s-w
3319 (default-value 'cursor-in-non-selected-windows))
3321 ;; Restore the outside saved values; don't alter
3322 ;; the outside binding loci.
3323 (setq-default cursor-in-non-selected-windows t))))
3325 (defun edebug-eval (expr)
3326 (backtrace-eval expr 0 'edebug-after))
3328 (defun edebug-safe-eval (expr)
3329 ;; Evaluate EXPR safely.
3330 ;; If there is an error, a string is returned describing the error.
3331 (condition-case edebug-err
3332 (edebug-eval expr)
3333 (error (edebug-format "%s: %s" ;; could
3334 (get (car edebug-err) 'error-message)
3335 (car (cdr edebug-err))))))
3337 ;;; Printing
3340 (defun edebug-report-error (value)
3341 ;; Print an error message like command level does.
3342 ;; This also prints the error name if it has no error-message.
3343 (message "%s: %s"
3344 (or (get (car value) 'error-message)
3345 (format "peculiar error (%s)" (car value)))
3346 (mapconcat (function (lambda (edebug-arg)
3347 ;; continuing after an error may
3348 ;; complain about edebug-arg. why??
3349 (prin1-to-string edebug-arg)))
3350 (cdr value) ", ")))
3352 (defvar print-readably) ; defined by lemacs
3353 ;; Alternatively, we could change the definition of
3354 ;; edebug-safe-prin1-to-string to only use these if defined.
3356 (defun edebug-safe-prin1-to-string (value)
3357 (let ((print-escape-newlines t)
3358 (print-length (or edebug-print-length print-length))
3359 (print-level (or edebug-print-level print-level))
3360 (print-circle (or edebug-print-circle print-circle))
3361 (print-readably nil)) ; lemacs uses this.
3362 (edebug-prin1-to-string value)))
3364 (defun edebug-compute-previous-result (previous-value)
3365 (if edebug-unwrap-results
3366 (setq previous-value
3367 (edebug-unwrap* previous-value)))
3368 (setq edebug-previous-result
3369 (concat "Result: "
3370 (edebug-safe-prin1-to-string previous-value)
3371 (eval-expression-print-format previous-value))))
3373 (defun edebug-previous-result ()
3374 "Print the previous result."
3375 (interactive)
3376 (message "%s" edebug-previous-result))
3378 ;;; Read, Eval and Print
3380 (defalias 'edebug-prin1 'prin1)
3381 (defalias 'edebug-print 'print)
3382 (defalias 'edebug-prin1-to-string 'prin1-to-string)
3383 (defalias 'edebug-format 'format-message)
3384 (defalias 'edebug-message 'message)
3386 (defun edebug-eval-expression (expr)
3387 "Evaluate an expression in the outside environment.
3388 If interactive, prompt for the expression.
3389 Print result in minibuffer."
3390 (interactive (list (read-from-minibuffer
3391 "Eval: " nil read-expression-map t
3392 'read-expression-history)))
3393 (princ
3394 (edebug-outside-excursion
3395 (setq values (cons (edebug-eval expr) values))
3396 (concat (edebug-safe-prin1-to-string (car values))
3397 (eval-expression-print-format (car values))))))
3399 (defun edebug-eval-last-sexp ()
3400 "Evaluate sexp before point in the outside environment.
3401 Print value in minibuffer."
3402 (interactive)
3403 (edebug-eval-expression (edebug-last-sexp)))
3405 (defun edebug-eval-print-last-sexp ()
3406 "Evaluate sexp before point in outside environment; insert value.
3407 This prints the value into current buffer."
3408 (interactive)
3409 (let* ((form (edebug-last-sexp))
3410 (result-string
3411 (edebug-outside-excursion
3412 (edebug-safe-prin1-to-string (edebug-safe-eval form))))
3413 (standard-output (current-buffer)))
3414 (princ "\n")
3415 ;; princ the string to get rid of quotes.
3416 (princ result-string)
3417 (princ "\n")
3420 ;;; Edebug Minor Mode
3422 (defvar edebug-inhibit-emacs-lisp-mode-bindings nil
3423 "If non-nil, inhibit Edebug bindings on the C-x C-a key.
3424 By default, loading the `edebug' library causes these bindings to
3425 be installed in `emacs-lisp-mode-map'.")
3427 (define-obsolete-variable-alias 'gud-inhibit-global-bindings
3428 'edebug-inhibit-emacs-lisp-mode-bindings "24.3")
3430 ;; Global GUD bindings for all emacs-lisp-mode buffers.
3431 (unless edebug-inhibit-emacs-lisp-mode-bindings
3432 (define-key emacs-lisp-mode-map "\C-x\C-a\C-s" 'edebug-step-mode)
3433 (define-key emacs-lisp-mode-map "\C-x\C-a\C-n" 'edebug-next-mode)
3434 (define-key emacs-lisp-mode-map "\C-x\C-a\C-c" 'edebug-go-mode)
3435 (define-key emacs-lisp-mode-map "\C-x\C-a\C-l" 'edebug-where)
3436 ;; The following isn't a GUD binding.
3437 (define-key emacs-lisp-mode-map "\C-x\C-a\C-m" 'edebug-set-initial-mode))
3439 (defvar edebug-mode-map
3440 (let ((map (copy-keymap emacs-lisp-mode-map)))
3441 ;; control
3442 (define-key map " " 'edebug-step-mode)
3443 (define-key map "n" 'edebug-next-mode)
3444 (define-key map "g" 'edebug-go-mode)
3445 (define-key map "G" 'edebug-Go-nonstop-mode)
3446 (define-key map "t" 'edebug-trace-mode)
3447 (define-key map "T" 'edebug-Trace-fast-mode)
3448 (define-key map "c" 'edebug-continue-mode)
3449 (define-key map "C" 'edebug-Continue-fast-mode)
3451 ;;(define-key map "f" 'edebug-forward) not implemented
3452 (define-key map "f" 'edebug-forward-sexp)
3453 (define-key map "h" 'edebug-goto-here)
3455 (define-key map "I" 'edebug-instrument-callee)
3456 (define-key map "i" 'edebug-step-in)
3457 (define-key map "o" 'edebug-step-out)
3459 ;; quitting and stopping
3460 (define-key map "q" 'top-level)
3461 (define-key map "Q" 'edebug-top-level-nonstop)
3462 (define-key map "a" 'abort-recursive-edit)
3463 (define-key map "S" 'edebug-stop)
3465 ;; breakpoints
3466 (define-key map "b" 'edebug-set-breakpoint)
3467 (define-key map "u" 'edebug-unset-breakpoint)
3468 (define-key map "B" 'edebug-next-breakpoint)
3469 (define-key map "x" 'edebug-set-conditional-breakpoint)
3470 (define-key map "X" 'edebug-set-global-break-condition)
3472 ;; evaluation
3473 (define-key map "r" 'edebug-previous-result)
3474 (define-key map "e" 'edebug-eval-expression)
3475 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3476 (define-key map "E" 'edebug-visit-eval-list)
3478 ;; views
3479 (define-key map "w" 'edebug-where)
3480 (define-key map "v" 'edebug-view-outside) ;; maybe obsolete??
3481 (define-key map "p" 'edebug-bounce-point)
3482 (define-key map "P" 'edebug-view-outside) ;; same as v
3483 (define-key map "W" 'edebug-toggle-save-windows)
3485 ;; misc
3486 (define-key map "?" 'edebug-help)
3487 (define-key map "d" 'edebug-backtrace)
3489 (define-key map "-" 'negative-argument)
3491 ;; statistics
3492 (define-key map "=" 'edebug-temp-display-freq-count)
3494 ;; GUD bindings
3495 (define-key map "\C-c\C-s" 'edebug-step-mode)
3496 (define-key map "\C-c\C-n" 'edebug-next-mode)
3497 (define-key map "\C-c\C-c" 'edebug-go-mode)
3499 (define-key map "\C-x " 'edebug-set-breakpoint)
3500 (define-key map "\C-c\C-d" 'edebug-unset-breakpoint)
3501 (define-key map "\C-c\C-t"
3502 (lambda () (interactive) (edebug-set-breakpoint t)))
3503 (define-key map "\C-c\C-l" 'edebug-where)
3504 map))
3506 ;; Autoloading these global bindings doesn't make sense because
3507 ;; they cannot be used anyway unless Edebug is already loaded and active.
3509 (defvar global-edebug-prefix "\^XX"
3510 "Prefix key for global edebug commands, available from any buffer.")
3512 (defvar global-edebug-map
3513 (let ((map (make-sparse-keymap)))
3515 (define-key map " " 'edebug-step-mode)
3516 (define-key map "g" 'edebug-go-mode)
3517 (define-key map "G" 'edebug-Go-nonstop-mode)
3518 (define-key map "t" 'edebug-trace-mode)
3519 (define-key map "T" 'edebug-Trace-fast-mode)
3520 (define-key map "c" 'edebug-continue-mode)
3521 (define-key map "C" 'edebug-Continue-fast-mode)
3523 ;; breakpoints
3524 (define-key map "b" 'edebug-set-breakpoint)
3525 (define-key map "u" 'edebug-unset-breakpoint)
3526 (define-key map "x" 'edebug-set-conditional-breakpoint)
3527 (define-key map "X" 'edebug-set-global-break-condition)
3529 ;; views
3530 (define-key map "w" 'edebug-where)
3531 (define-key map "W" 'edebug-toggle-save-windows)
3533 ;; quitting
3534 (define-key map "q" 'top-level)
3535 (define-key map "Q" 'edebug-top-level-nonstop)
3536 (define-key map "a" 'abort-recursive-edit)
3538 ;; statistics
3539 (define-key map "=" 'edebug-display-freq-count)
3540 map)
3541 "Global map of edebug commands, available from any buffer.")
3543 (global-unset-key global-edebug-prefix)
3544 (global-set-key global-edebug-prefix global-edebug-map)
3547 (defun edebug-help ()
3548 "Describe `edebug-mode'."
3549 (interactive)
3550 (describe-function 'edebug-mode))
3552 (defvar edebug--mode-saved-vars nil)
3554 (define-minor-mode edebug-mode
3555 "Mode for Emacs Lisp buffers while in Edebug.
3557 In addition to all Emacs Lisp commands (except those that modify the
3558 buffer) there are local and global key bindings to several Edebug
3559 specific commands. E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
3560 in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3562 Also see bindings for the eval list buffer *edebug* in `edebug-eval-mode'.
3564 The edebug buffer commands:
3565 \\{edebug-mode-map}
3567 Global commands prefixed by `global-edebug-prefix':
3568 \\{global-edebug-map}
3570 Options:
3571 `edebug-setup-hook'
3572 `edebug-all-defs'
3573 `edebug-all-forms'
3574 `edebug-save-windows'
3575 `edebug-save-displayed-buffer-points'
3576 `edebug-initial-mode'
3577 `edebug-trace'
3578 `edebug-test-coverage'
3579 `edebug-continue-kbd-macro'
3580 `edebug-print-length'
3581 `edebug-print-level'
3582 `edebug-print-circle'
3583 `edebug-on-error'
3584 `edebug-on-quit'
3585 `edebug-on-signal'
3586 `edebug-unwrap-results'
3587 `edebug-global-break-condition'"
3588 :lighter " *Debugging*"
3589 :keymap edebug-mode-map
3590 ;; If the user kills the buffer in which edebug is currently active,
3591 ;; exit to top level, because the edebug command loop can't usefully
3592 ;; continue running in such a case.
3594 (if (not edebug-mode)
3595 (progn
3596 (while edebug--mode-saved-vars
3597 (let ((setting (pop edebug--mode-saved-vars)))
3598 (if (consp setting)
3599 (set (car setting) (cdr setting))
3600 (kill-local-variable setting))))
3601 (remove-hook 'kill-buffer-hook 'edebug-kill-buffer t))
3602 (pcase-dolist (`(,var . ,val) '((buffer-read-only . t)))
3603 (push
3604 (if (local-variable-p var) (cons var (symbol-value var)) var)
3605 edebug--mode-saved-vars)
3606 (set (make-local-variable var) val))
3607 ;; Append `edebug-kill-buffer' to the hook to avoid interfering with
3608 ;; other entries that are unguarded against deleted buffer.
3609 (add-hook 'kill-buffer-hook 'edebug-kill-buffer t t)))
3611 (defun edebug-kill-buffer ()
3612 "Used on `kill-buffer-hook' when Edebug is operating in a buffer of Lisp code."
3613 (run-with-timer 0 nil #'top-level))
3615 ;;; edebug eval list mode
3617 ;; A list of expressions and their evaluations is displayed in *edebug*.
3619 (defun edebug-eval-result-list ()
3620 "Return a list of evaluations of `edebug-eval-list'."
3621 ;; Assumes in outside environment.
3622 ;; Don't do any edebug things now.
3623 (let ((edebug-execution-mode 'Go-nonstop)
3624 (edebug-trace nil))
3625 (mapcar 'edebug-safe-eval edebug-eval-list)))
3627 (defun edebug-eval-display-list (eval-result-list)
3628 ;; Assumes edebug-eval-buffer exists.
3629 (let ((standard-output edebug-eval-buffer)
3630 (edebug-comment-line
3631 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
3632 (set-buffer edebug-eval-buffer)
3633 (erase-buffer)
3634 (dolist (exp edebug-eval-list)
3635 (prin1 exp) (terpri)
3636 (prin1 (pop eval-result-list)) (terpri)
3637 (princ edebug-comment-line))
3638 (edebug-pop-to-buffer edebug-eval-buffer)
3641 (defun edebug-create-eval-buffer ()
3642 (unless (and edebug-eval-buffer (buffer-name edebug-eval-buffer))
3643 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
3644 (edebug-eval-mode)))
3646 ;; Should generalize this to be callable outside of edebug
3647 ;; with calls in user functions, e.g. (edebug-eval-display)
3649 (defun edebug-eval-display (eval-result-list)
3650 "Display expressions and evaluations in EVAL-RESULT-LIST.
3651 It modifies the context by popping up the eval display."
3652 (when eval-result-list
3653 (edebug-create-eval-buffer)
3654 (edebug-eval-display-list eval-result-list)))
3656 (defun edebug-eval-redisplay ()
3657 "Redisplay eval list in outside environment.
3658 May only be called from within `edebug--recursive-edit'."
3659 (edebug-create-eval-buffer)
3660 (edebug-outside-excursion
3661 (edebug-eval-display-list (edebug-eval-result-list))
3664 (defun edebug-visit-eval-list ()
3665 "Switch to the evaluation list buffer \"*edebug*\"."
3666 (interactive)
3667 (edebug-eval-redisplay)
3668 (edebug-pop-to-buffer edebug-eval-buffer))
3671 (defun edebug-update-eval-list ()
3672 "Replace the evaluation list with the sexps now in the eval buffer."
3673 (interactive)
3674 (let ((starting-point (point))
3675 new-list)
3676 (goto-char (point-min))
3677 ;; get the first expression
3678 (edebug-skip-whitespace)
3679 (if (not (eobp))
3680 (progn
3681 (forward-sexp 1)
3682 (push (edebug-last-sexp) new-list)))
3684 (while (re-search-forward "^;" nil t)
3685 (forward-line 1)
3686 (skip-chars-forward " \t\n\r")
3687 (if (and (/= ?\; (following-char))
3688 (not (eobp)))
3689 (progn
3690 (forward-sexp 1)
3691 (push (edebug-last-sexp) new-list))))
3693 (setq edebug-eval-list (nreverse new-list))
3694 (edebug-eval-redisplay)
3695 (goto-char starting-point)))
3698 (defun edebug-delete-eval-item ()
3699 "Delete the item under point and redisplay."
3700 ;; could add arg to do repeatedly
3701 (interactive)
3702 (if (re-search-backward "^;" nil 'nofail)
3703 (forward-line 1))
3704 (delete-region
3705 (point) (progn (re-search-forward "^;" nil 'nofail)
3706 (beginning-of-line)
3707 (point)))
3708 (edebug-update-eval-list))
3712 (defvar edebug-eval-mode-map
3713 (let ((map (make-sparse-keymap)))
3714 (set-keymap-parent map lisp-interaction-mode-map)
3715 (define-key map "\C-c\C-w" 'edebug-where)
3716 (define-key map "\C-c\C-d" 'edebug-delete-eval-item)
3717 (define-key map "\C-c\C-u" 'edebug-update-eval-list)
3718 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3719 (define-key map "\C-j" 'edebug-eval-print-last-sexp)
3720 map)
3721 "Keymap for Edebug Eval mode. Superset of Lisp Interaction mode.")
3723 (put 'edebug-eval-mode 'mode-class 'special)
3725 (define-derived-mode edebug-eval-mode lisp-interaction-mode "Edebug Eval"
3726 "Mode for evaluation list buffer while in Edebug.
3728 In addition to all Interactive Emacs Lisp commands there are local and
3729 global key bindings to several Edebug specific commands. E.g.
3730 `edebug-step-mode' is bound to \\[edebug-step-mode] in the Edebug
3731 buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3733 Eval list buffer commands:
3734 \\{edebug-eval-mode-map}
3736 Global commands prefixed by `global-edebug-prefix':
3737 \\{global-edebug-map}")
3739 ;;; Interface with standard debugger.
3741 ;; (setq debugger 'edebug) ; to use the edebug debugger
3742 ;; (setq debugger 'debug) ; use the standard debugger
3744 ;; Note that debug and its utilities must be byte-compiled to work,
3745 ;; since they depend on the backtrace looking a certain way. But
3746 ;; edebug is not dependent on this, yet.
3748 (defun edebug (&optional arg-mode &rest args)
3749 "Replacement for `debug'.
3750 If we are running an edebugged function, show where we last were.
3751 Otherwise call `debug' normally."
3752 ;;(message "entered: %s depth: %s edebug-recursion-depth: %s"
3753 ;; edebug-entered (recursion-depth) edebug-recursion-depth) (sit-for 1)
3754 (if (and edebug-entered ; anything active?
3755 (eq (recursion-depth) edebug-recursion-depth))
3756 (let (;; Where were we before the error occurred?
3757 (offset-index (car edebug-offset-indices))
3758 (value (car args))
3759 ;; Bind variables required by edebug--display.
3760 edebug-breakpoints
3761 edebug-break-data
3762 edebug-break-condition
3763 edebug-global-break
3764 (edebug-break (null arg-mode)) ;; If called explicitly.
3766 (edebug--display value offset-index arg-mode)
3767 (if (eq arg-mode 'error)
3769 value))
3771 ;; Otherwise call debug normally.
3772 ;; Still need to remove extraneous edebug calls from stack.
3773 (apply 'debug arg-mode args)
3777 (defun edebug-backtrace ()
3778 "Display a non-working backtrace. Better than nothing..."
3779 (interactive)
3780 (if (or (not edebug-backtrace-buffer)
3781 (null (buffer-name edebug-backtrace-buffer)))
3782 (setq edebug-backtrace-buffer
3783 (generate-new-buffer "*Backtrace*"))
3784 ;; Else, could just display edebug-backtrace-buffer.
3786 (with-output-to-temp-buffer (buffer-name edebug-backtrace-buffer)
3787 (setq edebug-backtrace-buffer standard-output)
3788 (let ((print-escape-newlines t)
3789 (print-length 50) ; FIXME cf edebug-safe-prin1-to-string
3790 last-ok-point)
3791 (backtrace)
3793 ;; Clean up the backtrace.
3794 ;; Not quite right for current edebug scheme.
3795 (set-buffer edebug-backtrace-buffer)
3796 (setq truncate-lines t)
3797 (goto-char (point-min))
3798 (setq last-ok-point (point))
3799 (if t (progn
3801 ;; Delete interspersed edebug internals.
3802 (while (re-search-forward "^ (?edebug" nil t)
3803 (beginning-of-line)
3804 (cond
3805 ((looking-at "^ (edebug-after")
3806 ;; Previous lines may contain code, so just delete this line.
3807 (setq last-ok-point (point))
3808 (forward-line 1)
3809 (delete-region last-ok-point (point)))
3811 ((looking-at (if debugger-stack-frame-as-list
3812 "^ (edebug"
3813 "^ edebug"))
3814 (forward-line 1)
3815 (delete-region last-ok-point (point))
3817 )))))
3820 ;;; Trace display
3822 (defun edebug-trace-display (buf-name fmt &rest args)
3823 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
3824 The buffer is created if it does not exist.
3825 You must include newlines in FMT to break lines, but one newline is appended."
3826 ;; e.g.
3827 ;; (edebug-trace-display "*trace-point*"
3828 ;; "saving: point = %s window-start = %s"
3829 ;; (point) (window-start))
3830 (let* ((oldbuf (current-buffer))
3831 (selected-window (selected-window))
3832 (buffer (get-buffer-create buf-name))
3833 buf-window)
3834 ;; (message "before pop-to-buffer") (sit-for 1)
3835 (edebug-pop-to-buffer buffer)
3836 (setq truncate-lines t)
3837 (setq buf-window (selected-window))
3838 (goto-char (point-max))
3839 (insert (apply 'edebug-format fmt args) "\n")
3840 ;; Make it visible.
3841 (vertical-motion (- 1 (window-height)))
3842 (set-window-start buf-window (point))
3843 (goto-char (point-max))
3844 ;; (set-window-point buf-window (point))
3845 ;; (sit-for 0)
3846 (bury-buffer buffer)
3847 (select-window selected-window)
3848 (set-buffer oldbuf))
3849 buf-name)
3852 (defun edebug-trace (fmt &rest args)
3853 "Convenience call to `edebug-trace-display' using `edebug-trace-buffer'."
3854 (apply 'edebug-trace-display edebug-trace-buffer fmt args))
3857 ;;; Frequency count and coverage
3859 ;; FIXME should this use overlays instead?
3860 ;; Definitely, IMO. The current business with undo in
3861 ;; edebug-temp-display-freq-count is horrid.
3862 (defun edebug-display-freq-count ()
3863 "Display the frequency count data for each line of the current definition.
3864 The frequency counts are inserted as comment lines after each line,
3865 and you can undo all insertions with one `undo' command.
3867 The counts are inserted starting under the `(' before an expression
3868 or the `)' after an expression, or on the last char of a symbol.
3869 The counts are only displayed when they differ from previous counts on
3870 the same line.
3872 If coverage is being tested, whenever all known results of an expression
3873 are `eq', the char `=' will be appended after the count
3874 for that expression. Note that this is always the case for an
3875 expression only evaluated once.
3877 To clear the frequency count and coverage data for a definition,
3878 reinstrument it."
3879 (interactive)
3880 (let* ((function (edebug-form-data-symbol))
3881 (counts (get function 'edebug-freq-count))
3882 (coverages (get function 'edebug-coverage))
3883 (data (get function 'edebug))
3884 (def-mark (car data)) ; mark at def start
3885 (edebug-points (nth 2 data))
3886 (i (1- (length edebug-points)))
3887 (last-index)
3888 (first-index)
3889 (start-of-line)
3890 (start-of-count-line)
3891 (last-count)
3893 (save-excursion
3894 ;; Traverse in reverse order so offsets are correct.
3895 (while (<= 0 i)
3896 ;; Start at last expression in line.
3897 (goto-char (+ def-mark (aref edebug-points i)))
3898 (beginning-of-line)
3899 (setq start-of-line (- (point) def-mark)
3900 last-index i)
3902 ;; Find all indexes on same line.
3903 (while (and (<= 0 (setq i (1- i)))
3904 (<= start-of-line (aref edebug-points i))))
3905 ;; Insert all the indices for this line.
3906 (forward-line 1)
3907 (setq start-of-count-line (point)
3908 first-index i ; Really, last index for line above this one.
3909 last-count -1) ; Cause first count to always appear.
3910 (insert ";#")
3911 ;; i == first-index still
3912 (while (<= (setq i (1+ i)) last-index)
3913 (let ((count (aref counts i))
3914 (coverage (aref coverages i))
3915 (col (save-excursion
3916 (goto-char (+ (aref edebug-points i) def-mark))
3917 (- (current-column)
3918 (if (= ?\( (following-char)) 0 1)))))
3919 (insert (make-string
3920 (max 0 (- col (- (point) start-of-count-line))) ?\s)
3921 (if (and (< 0 count)
3922 (not (memq coverage
3923 '(unknown ok-coverage))))
3924 "=" "")
3925 (if (= count last-count) "" (int-to-string count))
3926 " ")
3927 (setq last-count count)))
3928 (insert "\n")
3929 (setq i first-index)))))
3931 ;; FIXME this does not work very well. Eg if you press an arrow key,
3932 ;; or make a mouse-click, it fails with "Non-character input-event".
3933 (defun edebug-temp-display-freq-count ()
3934 "Temporarily display the frequency count data for the current definition.
3935 It is removed when you hit any char."
3936 ;; This seems not to work with Emacs 18.59. It undoes too far.
3937 (interactive)
3938 (let ((inhibit-read-only t))
3939 (undo-boundary)
3940 (edebug-display-freq-count)
3941 (setq unread-command-events
3942 (append unread-command-events (list (read-event))))
3943 ;; Yuck! This doesn't seem to work at all for me.
3944 (undo)))
3947 ;;; Menus
3949 (defun edebug-toggle (variable)
3950 (set variable (not (symbol-value variable)))
3951 (message "%s: %s" variable (symbol-value variable)))
3953 ;; We have to require easymenu (even for Emacs 18) just so
3954 ;; the easy-menu-define macro call is compiled correctly.
3955 (require 'easymenu)
3957 (defconst edebug-mode-menus
3958 '("Edebug"
3959 ["Stop" edebug-stop t]
3960 ["Step" edebug-step-mode t]
3961 ["Next" edebug-next-mode t]
3962 ["Trace" edebug-trace-mode t]
3963 ["Trace Fast" edebug-Trace-fast-mode t]
3964 ["Continue" edebug-continue-mode t]
3965 ["Continue Fast" edebug-Continue-fast-mode t]
3966 ["Go" edebug-go-mode t]
3967 ["Go Nonstop" edebug-Go-nonstop-mode t]
3968 "----"
3969 ["Help" edebug-help t]
3970 ["Abort" abort-recursive-edit t]
3971 ["Quit to Top Level" top-level t]
3972 ["Quit Nonstop" edebug-top-level-nonstop t]
3973 "----"
3974 ("Jumps"
3975 ["Forward Sexp" edebug-forward-sexp t]
3976 ["Step In" edebug-step-in t]
3977 ["Step Out" edebug-step-out t]
3978 ["Goto Here" edebug-goto-here t])
3980 ("Breaks"
3981 ["Set Breakpoint" edebug-set-breakpoint t]
3982 ["Unset Breakpoint" edebug-unset-breakpoint t]
3983 ["Set Conditional Breakpoint" edebug-set-conditional-breakpoint t]
3984 ["Set Global Break Condition" edebug-set-global-break-condition t]
3985 ["Show Next Breakpoint" edebug-next-breakpoint t])
3987 ("Views"
3988 ["Where am I?" edebug-where t]
3989 ["Bounce to Current Point" edebug-bounce-point t]
3990 ["View Outside Windows" edebug-view-outside t]
3991 ["Previous Result" edebug-previous-result t]
3992 ["Show Backtrace" edebug-backtrace t]
3993 ["Display Freq Count" edebug-display-freq-count t])
3995 ("Eval"
3996 ["Expression" edebug-eval-expression t]
3997 ["Last Sexp" edebug-eval-last-sexp t]
3998 ["Visit Eval List" edebug-visit-eval-list t])
4000 ("Options"
4001 ["Edebug All Defs" edebug-all-defs
4002 :style toggle :selected edebug-all-defs]
4003 ["Edebug All Forms" edebug-all-forms
4004 :style toggle :selected edebug-all-forms]
4005 "----"
4006 ["Tracing" (edebug-toggle 'edebug-trace)
4007 :style toggle :selected edebug-trace]
4008 ["Test Coverage" (edebug-toggle 'edebug-test-coverage)
4009 :style toggle :selected edebug-test-coverage]
4010 ["Save Windows" edebug-toggle-save-windows
4011 :style toggle :selected edebug-save-windows]
4012 ["Save Point"
4013 (edebug-toggle 'edebug-save-displayed-buffer-points)
4014 :style toggle :selected edebug-save-displayed-buffer-points]
4016 "Menus for Edebug.")
4019 ;;; Emacs version specific code
4021 (defalias 'edebug-window-live-p 'window-live-p)
4023 (defun edebug-mark ()
4024 (mark t))
4026 (defun edebug-set-conditional-breakpoint (arg condition)
4027 "Set a conditional breakpoint at nearest sexp.
4028 The condition is evaluated in the outside context.
4029 With prefix argument, make it a temporary breakpoint."
4030 ;; (interactive "P\nxCondition: ")
4031 (interactive
4032 (list
4033 current-prefix-arg
4034 ;; Read condition as follows; getting previous condition is cumbersome:
4035 (let ((edebug-stop-point (edebug-find-stop-point)))
4036 (if edebug-stop-point
4037 (let* ((edebug-def-name (car edebug-stop-point))
4038 (index (cdr edebug-stop-point))
4039 (edebug-data (get edebug-def-name 'edebug))
4040 (edebug-breakpoints (car (cdr edebug-data)))
4041 (edebug-break-data (assq index edebug-breakpoints))
4042 (edebug-break-condition (car (cdr edebug-break-data)))
4043 (initial (and edebug-break-condition
4044 (format "%s" edebug-break-condition))))
4045 (read-from-minibuffer
4046 "Condition: " initial read-expression-map t
4047 (if (equal (car read-expression-history) initial)
4048 '(read-expression-history . 1)
4049 'read-expression-history)))))))
4050 (edebug-modify-breakpoint t condition arg))
4052 (easy-menu-define edebug-menu edebug-mode-map "Edebug menus" edebug-mode-menus)
4054 ;;; Autoloading of Edebug accessories
4056 ;; edebug-cl-read and cl-read are available from liberte@cs.uiuc.edu
4057 (defun edebug--require-cl-read ()
4058 (require 'edebug-cl-read))
4060 (if (featurep 'cl-read)
4061 (add-hook 'edebug-setup-hook #'edebug--require-cl-read)
4062 ;; The following causes edebug-cl-read to be loaded when you load cl-read.el.
4063 (add-hook 'cl-read-load-hooks #'edebug--require-cl-read))
4066 ;;; Finalize Loading
4068 ;; When edebugging a function, some of the sub-expressions are
4069 ;; wrapped in (edebug-enter (lambda () ..)), so we need to teach
4070 ;; called-interactively-p that calls within the inner lambda should refer to
4071 ;; the outside function.
4072 (add-hook 'called-interactively-p-functions
4073 #'edebug--called-interactively-skip)
4074 (defun edebug--called-interactively-skip (i frame1 frame2)
4075 (when (and (eq (car-safe (nth 1 frame1)) 'lambda)
4076 (eq (nth 1 (nth 1 frame1)) '())
4077 (eq (nth 1 frame2) 'edebug-enter))
4078 ;; `edebug-enter' calls itself on its first invocation.
4079 (if (eq (nth 1 (backtrace-frame i 'called-interactively-p))
4080 'edebug-enter)
4081 2 1)))
4083 ;; Finally, hook edebug into the rest of Emacs.
4084 ;; There are probably some other things that could go here.
4086 ;; Install edebug read and eval functions.
4087 (edebug-install-read-eval-functions)
4089 (defun edebug-unload-function ()
4090 "Unload the Edebug source level debugger."
4091 (when edebug-active
4092 (setq edebug-active nil)
4093 (unwind-protect
4094 (abort-recursive-edit)
4095 ;; We still want to run unload-feature to completion
4096 (run-with-idle-timer 0 nil #'(lambda () (unload-feature 'edebug)))))
4097 (remove-hook 'called-interactively-p-functions
4098 'edebug--called-interactively-skip)
4099 (remove-hook 'cl-read-load-hooks 'edebug--require-cl-read)
4100 (edebug-uninstall-read-eval-functions)
4101 ;; Continue standard unloading.
4102 nil)
4104 (provide 'edebug)
4105 ;;; edebug.el ends here