Avoid leaving garbage on screen when using 'raise' display property
[emacs.git] / lisp / emacs-lisp / edebug.el
blob601330556239c352d36225572a36df5c31c2818d
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 (lambda (window)
402 (if (edebug-window-live-p window)
403 (list window
404 (window-buffer window)
405 (window-point window)
406 (window-start window)
407 (window-hscroll window))))
408 which-windows)
409 (current-window-configuration)))
411 (defun edebug-set-windows (window-info)
412 ;; Set either a full window configuration or some window information.
413 (if (listp window-info)
414 (mapcar (lambda (one-window-info)
415 (if one-window-info
416 (apply (function
417 (lambda (window buffer point start hscroll)
418 (if (edebug-window-live-p window)
419 (progn
420 (set-window-buffer window buffer)
421 (set-window-point window point)
422 (set-window-start window start)
423 (set-window-hscroll window hscroll)))))
424 one-window-info)))
425 window-info)
426 (set-window-configuration window-info)))
428 ;;; Redefine read and eval functions
429 ;; read is redefined to maybe instrument forms.
430 ;; eval-defun is redefined to check edebug-all-forms and edebug-all-defs.
432 (defun edebug--read (orig &optional stream)
433 "Read one Lisp expression as text from STREAM, return as Lisp object.
434 If STREAM is nil, use the value of `standard-input' (which see).
435 STREAM or the value of `standard-input' may be:
436 a buffer (read from point and advance it)
437 a marker (read from where it points and advance it)
438 a function (call it with no arguments for each character,
439 call it with a char as argument to push a char back)
440 a string (takes text from string, starting at the beginning)
441 t (read text line using minibuffer and use it).
443 This version, from Edebug, maybe instruments the expression. But the
444 STREAM must be the current buffer to do so. Whether it instruments is
445 also dependent on the values of the option `edebug-all-defs' and
446 the option `edebug-all-forms'."
447 (or stream (setq stream standard-input))
448 (if (eq stream (current-buffer))
449 (edebug-read-and-maybe-wrap-form)
450 (funcall (or orig #'read) stream)))
452 (defvar edebug-result) ; The result of the function call returned by body.
454 ;; We should somehow arrange to be able to do this
455 ;; without actually replacing the eval-defun command.
456 (defun edebug-eval-defun (edebug-it)
457 "Evaluate the top-level form containing point, or after point.
459 If the current defun is actually a call to `defvar', then reset the
460 variable using its initial value expression even if the variable
461 already has some other value. (Normally `defvar' does not change the
462 variable's value if it already has a value.) Treat `defcustom'
463 similarly. Reinitialize the face according to `defface' specification.
465 With a prefix argument, instrument the code for Edebug.
467 Setting option `edebug-all-defs' to a non-nil value reverses the meaning
468 of the prefix argument. Code is then instrumented when this function is
469 invoked without a prefix argument.
471 If acting on a `defun' for FUNCTION, and the function was instrumented,
472 `Edebug: FUNCTION' is printed in the minibuffer. If not instrumented,
473 just FUNCTION is printed.
475 If not acting on a `defun', the result of evaluation is displayed in
476 the minibuffer."
477 (interactive "P")
478 (let* ((edebugging (not (eq (not edebug-it) (not edebug-all-defs))))
479 (edebug-result)
480 (form
481 (let ((edebug-all-forms edebugging)
482 (edebug-all-defs (eq edebug-all-defs (not edebug-it))))
483 (edebug-read-top-level-form))))
484 ;; This should be consistent with `eval-defun-1', but not the
485 ;; same, since that gets a macroexpanded form.
486 (cond ((and (eq (car form) 'defvar)
487 (cdr-safe (cdr-safe form)))
488 ;; Force variable to be bound.
489 (makunbound (nth 1 form)))
490 ((and (eq (car form) 'defcustom)
491 (default-boundp (nth 1 form)))
492 ;; Force variable to be bound.
493 ;; FIXME: Shouldn't this use the :setter or :initializer?
494 (set-default (nth 1 form) (eval (nth 2 form) lexical-binding)))
495 ((eq (car form) 'defface)
496 ;; Reset the face.
497 (setq face-new-frame-defaults
498 (assq-delete-all (nth 1 form) face-new-frame-defaults))
499 (put (nth 1 form) 'face-defface-spec nil)
500 (put (nth 1 form) 'face-documentation (nth 3 form))
501 ;; See comments in `eval-defun-1' for purpose of code below
502 (setq form (prog1 `(prog1 ,form
503 (put ',(nth 1 form) 'saved-face
504 ',(get (nth 1 form) 'saved-face))
505 (put ',(nth 1 form) 'customized-face
506 ,(nth 2 form)))
507 (put (nth 1 form) 'saved-face nil)))))
508 (setq edebug-result (eval (eval-sexp-add-defvars form) lexical-binding))
509 (if (not edebugging)
510 (prog1
511 (prin1 edebug-result)
512 (let ((str (eval-expression-print-format edebug-result)))
513 (if str (princ str))))
514 edebug-result)))
517 ;;;###autoload
518 (defalias 'edebug-defun 'edebug-eval-top-level-form)
520 ;;;###autoload
521 (defun edebug-eval-top-level-form ()
522 "Evaluate the top level form point is in, stepping through with Edebug.
523 This is like `eval-defun' except that it steps the code for Edebug
524 before evaluating it. It displays the value in the echo area
525 using `eval-expression' (which see).
527 If you do this on a function definition such as a defun or defmacro,
528 it defines the function and instruments its definition for Edebug,
529 so it will do Edebug stepping when called later. It displays
530 `Edebug: FUNCTION' in the echo area to indicate that FUNCTION is now
531 instrumented for Edebug.
533 If the current defun is actually a call to `defvar' or `defcustom',
534 evaluating it this way resets the variable using its initial value
535 expression even if the variable already has some other value.
536 \(Normally `defvar' and `defcustom' do not alter the value if there
537 already is one.)"
538 (interactive)
539 (eval-expression
540 ;; Bind edebug-all-forms only while reading, not while evalling
541 ;; but this causes problems while edebugging edebug.
542 (let ((edebug-all-forms t)
543 (edebug-all-defs t))
544 (eval-sexp-add-defvars
545 (edebug-read-top-level-form)))))
548 (defun edebug-read-top-level-form ()
549 (let ((starting-point (point)))
550 (end-of-defun)
551 (beginning-of-defun)
552 (prog1
553 (edebug-read-and-maybe-wrap-form)
554 ;; Recover point, but only if no error occurred.
555 (goto-char starting-point))))
558 ;; Compatibility with old versions.
559 (defalias 'edebug-all-defuns 'edebug-all-defs)
561 ;;;###autoload
562 (defun edebug-all-defs ()
563 "Toggle edebugging of all definitions."
564 (interactive)
565 (setq edebug-all-defs (not edebug-all-defs))
566 (message "Edebugging all definitions is %s."
567 (if edebug-all-defs "on" "off")))
570 ;;;###autoload
571 (defun edebug-all-forms ()
572 "Toggle edebugging of all forms."
573 (interactive)
574 (setq edebug-all-forms (not edebug-all-forms))
575 (message "Edebugging all forms is %s."
576 (if edebug-all-forms "on" "off")))
579 (defun edebug-install-read-eval-functions ()
580 (interactive)
581 (add-function :around load-read-function #'edebug--read)
582 (advice-add 'eval-defun :override #'edebug-eval-defun))
584 (defun edebug-uninstall-read-eval-functions ()
585 (interactive)
586 (remove-function load-read-function #'edebug--read)
587 (advice-remove 'eval-defun 'edebug-eval-defun))
589 ;;; Edebug internal data
591 ;; The internal data that is needed for edebugging is kept in the
592 ;; buffer-local variable `edebug-form-data'.
594 (defvar-local edebug-form-data nil
595 "A list of entries associating symbols with buffer regions.
596 Each entry is an `edebug--form-data' struct with fields:
597 SYMBOL, BEGIN-MARKER, and END-MARKER. The markers
598 are at the beginning and end of an entry level form and SYMBOL is
599 a symbol that holds all edebug related information for the form on its
600 property list.
602 In the future (haha!), the symbol will be irrelevant and edebug data will
603 be stored in the definitions themselves rather than in the property
604 list of a symbol.")
606 (cl-defstruct (edebug--form-data
607 ;; Some callers expect accessors to return nil when passed nil.
608 (:type list)
609 (:constructor edebug--make-form-data-entry (name begin end))
610 (:predicate nil) (:constructor nil) (:copier nil))
611 name begin end)
613 (defsubst edebug-set-form-data-entry (entry name begin end)
614 (setf (edebug--form-data-name entry) name) ;; In case name is changed.
615 (set-marker (edebug--form-data-begin entry) begin)
616 (set-marker (edebug--form-data-end entry) end))
618 (defun edebug-get-form-data-entry (pnt &optional end-point)
619 ;; Find the edebug form data entry which is closest to PNT.
620 ;; If END-POINT is supplied, match must be exact.
621 ;; Return nil if none found.
622 (let ((rest edebug-form-data)
623 closest-entry
624 (closest-dist 999999)) ;; Need maxint here.
625 (while (and rest (< 0 closest-dist))
626 (let* ((entry (car rest))
627 (begin (edebug--form-data-begin entry))
628 (dist (- pnt begin)))
629 (setq rest (cdr rest))
630 (if (and (<= 0 dist)
631 (< dist closest-dist)
632 (or (not end-point)
633 (= end-point (edebug--form-data-end entry)))
634 (<= pnt (edebug--form-data-end entry)))
635 (setq closest-dist dist
636 closest-entry entry))))
637 closest-entry))
639 ;; Also need to find all contained entries,
640 ;; and find an entry given a symbol, which should be just assq.
642 (defun edebug-form-data-symbol ()
643 "Return the edebug data symbol of the form where point is in.
644 If point is not inside a edebuggable form, cause error."
645 (or (edebug--form-data-name (edebug-get-form-data-entry (point)))
646 (error "Not inside instrumented form")))
648 (defun edebug-make-top-form-data-entry (new-entry)
649 ;; Make NEW-ENTRY the first element in the `edebug-form-data' list.
650 (edebug-clear-form-data-entry new-entry)
651 (push new-entry edebug-form-data))
653 (defun edebug-clear-form-data-entry (entry)
654 "If non-nil, clear ENTRY out of the form data.
655 Maybe clear the markers and delete the symbol's edebug property?"
656 (if entry
657 (progn
658 ;; Instead of this, we could just find all contained forms.
659 ;; (put (car entry) 'edebug nil) ;
660 ;; (mapcar #'edebug-clear-form-data-entry ; dangerous
661 ;; (get (car entry) 'edebug-dependents))
662 ;; (set-marker (nth 1 entry) nil)
663 ;; (set-marker (nth 2 entry) nil)
664 (setq edebug-form-data (delq entry edebug-form-data)))))
666 ;;; Parser utilities
668 (defun edebug-syntax-error (&rest args)
669 ;; Signal an invalid-read-syntax with ARGS.
670 (signal 'invalid-read-syntax args))
673 (defconst edebug-read-syntax-table
674 ;; Lookup table for significant characters indicating the class of the
675 ;; token that follows. This is not a \"real\" syntax table.
676 (let ((table (make-char-table 'syntax-table 'symbol))
677 (i 0))
678 (while (< i ?!)
679 (aset table i 'space)
680 (setq i (1+ i)))
681 (aset table ?\( 'lparen)
682 (aset table ?\) 'rparen)
683 (aset table ?\' 'quote)
684 (aset table ?\` 'backquote)
685 (aset table ?\, 'comma)
686 (aset table ?\" 'string)
687 (aset table ?\? 'char)
688 (aset table ?\[ 'lbracket)
689 (aset table ?\] 'rbracket)
690 (aset table ?\. 'dot)
691 (aset table ?\# 'hash)
692 ;; We treat numbers as symbols, because of confusion with -, -1, and 1-.
693 ;; We don't care about any other chars since they won't be seen.
694 table))
696 (defun edebug-next-token-class ()
697 ;; Move to the next token and return its class. We only care about
698 ;; lparen, rparen, dot, quote, backquote, comma, string, char, vector,
699 ;; or symbol.
700 (edebug-skip-whitespace)
701 (if (and (eq (following-char) ?.)
702 (save-excursion
703 (forward-char 1)
704 (or (and (eq (aref edebug-read-syntax-table (following-char))
705 'symbol)
706 (not (= (following-char) ?\;)))
707 (memq (following-char) '(?\, ?\.)))))
708 'symbol
709 (aref edebug-read-syntax-table (following-char))))
712 (defun edebug-skip-whitespace ()
713 ;; Leave point before the next token, skipping white space and comments.
714 (skip-chars-forward " \t\r\n\f")
715 (while (= (following-char) ?\;)
716 (skip-chars-forward "^\n") ; skip the comment
717 (skip-chars-forward " \t\r\n\f")))
720 ;; Mostly obsolete reader; still used in one case.
722 (defun edebug-read-sexp ()
723 ;; Read one sexp from the current buffer starting at point.
724 ;; Leave point immediately after it. A sexp can be a list or atom.
725 ;; An atom is a symbol (or number), character, string, or vector.
726 ;; This works for reading anything legitimate, but it
727 ;; is gummed up by parser inconsistencies (bugs?)
728 (let ((class (edebug-next-token-class)))
729 (cond
730 ;; read goes one too far if a (possibly quoted) string or symbol
731 ;; is immediately followed by non-whitespace.
732 ((eq class 'symbol) (read (current-buffer)))
733 ((eq class 'string) (read (current-buffer)))
734 ((eq class 'quote) (forward-char 1)
735 (list 'quote (edebug-read-sexp)))
736 ((eq class 'backquote)
737 (list '\` (edebug-read-sexp)))
738 ((eq class 'comma)
739 (list '\, (edebug-read-sexp)))
740 (t ; anything else, just read it.
741 (read (current-buffer))))))
743 ;;; Offsets for reader
745 ;; Define a structure to represent offset positions of expressions.
746 ;; Each offset structure looks like: (before . after) for constituents,
747 ;; or for structures that have elements: (before <subexpressions> . after)
748 ;; where the <subexpressions> are the offset structures for subexpressions
749 ;; including the head of a list.
750 (defvar edebug-offsets nil)
752 ;; Stack of offset structures in reverse order of the nesting.
753 ;; This is used to get back to previous levels.
754 (defvar edebug-offsets-stack nil)
755 (defvar edebug-current-offset nil) ; Top of the stack, for convenience.
757 ;; The association list of objects read with the #n=object form.
758 ;; Each member of the list has the form (n . object), and is used to
759 ;; look up the object for the corresponding #n# construct.
760 (defvar edebug-read-objects nil)
762 ;; We must store whether we just read a list with a dotted form that
763 ;; is itself a list. This structure will be condensed, so the offsets
764 ;; must also be condensed.
765 (defvar edebug-read-dotted-list nil)
767 (defsubst edebug-initialize-offsets ()
768 ;; Reinitialize offset recording.
769 (setq edebug-current-offset nil))
771 (defun edebug-store-before-offset (point)
772 ;; Add a new offset pair with POINT as the before offset.
773 (let ((new-offset (list point)))
774 (if edebug-current-offset
775 (setcdr edebug-current-offset
776 (cons new-offset (cdr edebug-current-offset)))
777 ;; Otherwise, we are at the top level, so initialize.
778 (setq edebug-offsets new-offset
779 edebug-offsets-stack nil
780 edebug-read-dotted-list nil))
781 ;; Cons the new offset to the front of the stack.
782 (setq edebug-offsets-stack (cons new-offset edebug-offsets-stack)
783 edebug-current-offset new-offset)
786 (defun edebug-store-after-offset (point)
787 ;; Finalize the current offset struct by reversing it and
788 ;; store POINT as the after offset.
789 (if (not edebug-read-dotted-list)
790 ;; Just reverse the offsets of all subexpressions.
791 (setcdr edebug-current-offset (nreverse (cdr edebug-current-offset)))
793 ;; We just read a list after a dot, which will be abbreviated out.
794 (setq edebug-read-dotted-list nil)
795 ;; Drop the corresponding offset pair.
796 ;; That is, nconc the reverse of the rest of the offsets
797 ;; with the cdr of last offset.
798 (setcdr edebug-current-offset
799 (nconc (nreverse (cdr (cdr edebug-current-offset)))
800 (cdr (car (cdr edebug-current-offset))))))
802 ;; Now append the point using nconc.
803 (setq edebug-current-offset (nconc edebug-current-offset point))
804 ;; Pop the stack.
805 (setq edebug-offsets-stack (cdr edebug-offsets-stack)
806 edebug-current-offset (car edebug-offsets-stack)))
808 (defun edebug-ignore-offset ()
809 ;; Ignore the last created offset pair.
810 (setcdr edebug-current-offset (cdr (cdr edebug-current-offset))))
812 (defmacro edebug-storing-offsets (point &rest body)
813 (declare (debug (form body)) (indent 1))
814 `(unwind-protect
815 (progn
816 (edebug-store-before-offset ,point)
817 ,@body)
818 (edebug-store-after-offset (point))))
821 ;;; Reader for Emacs Lisp.
823 ;; Uses edebug-next-token-class (and edebug-skip-whitespace) above.
825 (defconst edebug-read-alist
826 '((symbol . edebug-read-symbol)
827 (lparen . edebug-read-list)
828 (string . edebug-read-string)
829 (quote . edebug-read-quote)
830 (backquote . edebug-read-backquote)
831 (comma . edebug-read-comma)
832 (lbracket . edebug-read-vector)
833 (hash . edebug-read-special)
836 (defun edebug-read-storing-offsets (stream)
837 (let (edebug-read-dotted-list) ; see edebug-store-after-offset
838 (edebug-storing-offsets (point)
839 (funcall
840 (or (cdr (assq (edebug-next-token-class) edebug-read-alist))
841 ;; anything else, just read it.
842 #'read)
843 stream))))
845 (defalias 'edebug-read-symbol #'read)
846 (defalias 'edebug-read-string #'read)
848 (defun edebug-read-quote (stream)
849 ;; Turn 'thing into (quote thing)
850 (forward-char 1)
851 (list
852 (edebug-storing-offsets (1- (point)) 'quote)
853 (edebug-read-storing-offsets stream)))
855 (defun edebug-read-backquote (stream)
856 ;; Turn `thing into (\` thing)
857 (forward-char 1)
858 (list
859 (edebug-storing-offsets (1- (point)) '\`)
860 (edebug-read-storing-offsets stream)))
862 (defun edebug-read-comma (stream)
863 ;; Turn ,thing into (\, thing). Handle ,@ and ,. also.
864 (let ((opoint (point)))
865 (forward-char 1)
866 (let ((symbol '\,))
867 (cond ((eq (following-char) ?\.)
868 (setq symbol '\,\.)
869 (forward-char 1))
870 ((eq (following-char) ?\@)
871 (setq symbol '\,@)
872 (forward-char 1)))
873 ;; Generate the same structure of offsets we would have
874 ;; if the resulting list appeared verbatim in the input text.
875 (list
876 (edebug-storing-offsets opoint symbol)
877 (edebug-read-storing-offsets stream)))))
879 (defun edebug-read-special (stream)
880 "Read from STREAM a Lisp object beginning with #.
881 Turn #'thing into (function thing) and handle the read syntax for
882 circular objects. Let `read' read everything else."
883 (catch 'return
884 (forward-char 1)
885 (let ((start (point)))
886 (cond
887 ((eq ?\' (following-char))
888 (forward-char 1)
889 (throw 'return
890 (list
891 (edebug-storing-offsets (- (point) 2) 'function)
892 (edebug-read-storing-offsets stream))))
893 ((and (>= (following-char) ?0) (<= (following-char) ?9))
894 (while (and (>= (following-char) ?0) (<= (following-char) ?9))
895 (forward-char 1))
896 (let ((n (string-to-number (buffer-substring start (point)))))
897 (when (and read-circle
898 (<= n most-positive-fixnum))
899 (cond
900 ((eq ?= (following-char))
901 ;; Make a placeholder for #n# to use temporarily.
902 (let* ((placeholder (cons nil nil))
903 (elem (cons n placeholder)))
904 (push elem edebug-read-objects)
905 ;; Read the object and then replace the placeholder
906 ;; with the object itself, wherever it occurs.
907 (forward-char 1)
908 (let ((obj (edebug-read-storing-offsets stream)))
909 (substitute-object-in-subtree obj placeholder)
910 (throw 'return (setf (cdr elem) obj)))))
911 ((eq ?# (following-char))
912 ;; #n# returns a previously read object.
913 (let ((elem (assq n edebug-read-objects)))
914 (when (consp elem)
915 (forward-char 1)
916 (throw 'return (cdr elem))))))))))
917 ;; Let read handle errors, radix notation, and anything else.
918 (goto-char (1- start))
919 (read stream))))
921 (defun edebug-read-list (stream)
922 (forward-char 1) ; skip \(
923 (prog1
924 (let ((elements))
925 (while (not (memq (edebug-next-token-class) '(rparen dot)))
926 (push (edebug-read-storing-offsets stream) elements))
927 (setq elements (nreverse elements))
928 (if (eq 'dot (edebug-next-token-class))
929 (let (dotted-form)
930 (forward-char 1) ; skip \.
931 (setq dotted-form (edebug-read-storing-offsets stream))
932 elements (nconc elements dotted-form)
933 (if (not (eq (edebug-next-token-class) 'rparen))
934 (edebug-syntax-error "Expected `)'"))
935 (setq edebug-read-dotted-list (listp dotted-form))
937 elements)
938 (forward-char 1) ; skip \)
941 (defun edebug-read-vector (stream)
942 (forward-char 1) ; skip \[
943 (prog1
944 (let ((elements))
945 (while (not (eq 'rbracket (edebug-next-token-class)))
946 (push (edebug-read-storing-offsets stream) elements))
947 (apply #'vector (nreverse elements)))
948 (forward-char 1) ; skip \]
951 ;;; Cursors for traversal of list and vector elements with offsets.
953 (defvar edebug-dotted-spec nil)
955 (defun edebug-new-cursor (expressions offsets)
956 ;; Return a new cursor for EXPRESSIONS with OFFSETS.
957 (if (vectorp expressions)
958 (setq expressions (append expressions nil)))
959 (cons expressions offsets))
961 (defsubst edebug-set-cursor (cursor expressions offsets)
962 ;; Set the CURSOR's EXPRESSIONS and OFFSETS to the given.
963 ;; Return the cursor.
964 (setcar cursor expressions)
965 (setcdr cursor offsets)
966 cursor)
968 (defun edebug-copy-cursor (cursor)
969 ;; Copy the cursor using the same object and offsets.
970 (cons (car cursor) (cdr cursor)))
972 (defsubst edebug-cursor-expressions (cursor)
973 (car cursor))
974 (defsubst edebug-cursor-offsets (cursor)
975 (cdr cursor))
977 (defsubst edebug-empty-cursor (cursor)
978 ;; Return non-nil if CURSOR is empty - meaning no more elements.
979 (null (car cursor)))
981 (defsubst edebug-top-element (cursor)
982 ;; Return the top element at the cursor.
983 ;; Assumes not empty.
984 (car (car cursor)))
986 (defun edebug-top-element-required (cursor &rest error)
987 ;; Check if a dotted form is required.
988 (if edebug-dotted-spec (edebug-no-match cursor "Dot expected."))
989 ;; Check if there is at least one more argument.
990 (if (edebug-empty-cursor cursor) (apply #'edebug-no-match cursor error))
991 ;; Return that top element.
992 (edebug-top-element cursor))
994 (defsubst edebug-top-offset (cursor)
995 ;; Return the top offset pair corresponding to the top element.
996 (car (cdr cursor)))
998 (defun edebug-move-cursor (cursor)
999 ;; Advance and return the cursor to the next element and offset.
1000 ;; throw no-match if empty before moving.
1001 ;; This is a violation of the cursor encapsulation, but
1002 ;; there is plenty of that going on while matching.
1003 ;; The following test should always fail.
1004 (if (edebug-empty-cursor cursor)
1005 (edebug-no-match cursor "Not enough arguments."))
1006 (setcar cursor (cdr (car cursor)))
1007 (setcdr cursor (cdr (cdr cursor)))
1008 cursor)
1011 (defun edebug-before-offset (cursor)
1012 ;; Return the before offset of the cursor.
1013 ;; If there is nothing left in the offsets,
1014 ;; return one less than the offset itself,
1015 ;; which is the after offset for a list.
1016 (let ((offset (edebug-cursor-offsets cursor)))
1017 (if (consp offset)
1018 (car (car offset))
1019 (1- offset))))
1021 (defun edebug-after-offset (cursor)
1022 ;; Return the after offset of the cursor object.
1023 (let ((offset (edebug-top-offset cursor)))
1024 (while (consp offset)
1025 (setq offset (cdr offset)))
1026 offset))
1028 ;;; The Parser
1030 ;; The top level function for parsing forms is
1031 ;; edebug-read-and-maybe-wrap-form; it calls all the rest. It checks the
1032 ;; syntax a bit and leaves point at any error it finds, but otherwise
1033 ;; should appear to work like eval-defun.
1035 ;; The basic plan is to surround each expression with a call to
1036 ;; the edebug debugger together with indexes into a table of positions of
1037 ;; all expressions. Thus an expression "exp" becomes:
1039 ;; (edebug-after (edebug-before 1) 2 exp)
1041 ;; When this is evaluated, first point is moved to the beginning of
1042 ;; exp at offset 1 of the current function. The expression is
1043 ;; evaluated, which may cause more edebug calls, and then point is
1044 ;; moved to offset 2 after the end of exp.
1046 ;; The highest level expressions of the function are wrapped in a call to
1047 ;; edebug-enter, which supplies the function name and the actual
1048 ;; arguments to the function. See functions edebug-enter, edebug-before,
1049 ;; and edebug-after for more details.
1051 ;; Dynamically bound vars, left unbound, but globally declared.
1052 ;; This is to quiet the byte compiler.
1054 ;; Window data of the highest definition being wrapped.
1055 ;; This data is shared by all embedded definitions.
1056 (defvar edebug-top-window-data)
1058 (defvar edebug-&optional)
1059 (defvar edebug-&rest)
1060 (defvar edebug-gate nil) ;; whether no-match forces an error.
1062 (defvar edebug-def-name nil) ; name of definition, used by interactive-form
1063 (defvar edebug-old-def-name nil) ; previous name of containing definition.
1065 (defvar edebug-error-point nil)
1066 (defvar edebug-best-error nil)
1069 (defun edebug-read-and-maybe-wrap-form ()
1070 ;; Read a form and wrap it with edebug calls, if the conditions are right.
1071 ;; Here we just catch any no-match not caught below and signal an error.
1073 ;; Run the setup hook.
1074 ;; If it gets an error, make it nil.
1075 (let ((temp-hook edebug-setup-hook))
1076 (setq edebug-setup-hook nil)
1077 (if (functionp temp-hook) (funcall temp-hook)
1078 (mapc #'funcall temp-hook)))
1080 (let (result
1081 edebug-top-window-data
1082 edebug-def-name;; make sure it is locally nil
1083 ;; I don't like these here!!
1084 edebug-&optional
1085 edebug-&rest
1086 edebug-gate
1087 edebug-best-error
1088 edebug-error-point
1089 ;; Do this once here instead of several times.
1090 (max-lisp-eval-depth (+ 800 max-lisp-eval-depth))
1091 (max-specpdl-size (+ 2000 max-specpdl-size)))
1092 (let ((no-match
1093 (catch 'no-match
1094 (setq result (edebug-read-and-maybe-wrap-form1))
1095 nil)))
1096 (if no-match
1097 (apply #'edebug-syntax-error no-match)))
1098 result))
1101 (defun edebug-read-and-maybe-wrap-form1 ()
1102 (let (spec
1103 def-kind
1104 defining-form-p
1105 def-name
1106 ;; These offset things don't belong here, but to support recursive
1107 ;; calls to edebug-read, they need to be here.
1108 edebug-offsets
1109 edebug-offsets-stack
1110 edebug-current-offset ; reset to nil
1111 edebug-read-objects
1113 (save-excursion
1114 (if (and (eq 'lparen (edebug-next-token-class))
1115 (eq 'symbol (progn (forward-char 1) (edebug-next-token-class))))
1116 ;; Find out if this is a defining form from first symbol
1117 (setq def-kind (read (current-buffer))
1118 spec (and (symbolp def-kind) (get-edebug-spec def-kind))
1119 defining-form-p (and (listp spec)
1120 (eq '&define (car spec)))
1121 ;; This is incorrect in general!! But OK most of the time.
1122 def-name (if (and defining-form-p
1123 (eq 'name (car (cdr spec)))
1124 (eq 'symbol (edebug-next-token-class)))
1125 (read (current-buffer))))))
1126 ;;;(message "all defs: %s all forms: %s" edebug-all-defs edebug-all-forms)
1127 (cond
1128 (defining-form-p
1129 (if (or edebug-all-defs edebug-all-forms)
1130 ;; If it is a defining form and we are edebugging defs,
1131 ;; then let edebug-list-form start it.
1132 (let ((cursor (edebug-new-cursor
1133 (list (edebug-read-storing-offsets (current-buffer)))
1134 (list edebug-offsets))))
1135 (car
1136 (edebug-make-form-wrapper
1137 cursor
1138 (edebug-before-offset cursor)
1139 (1- (edebug-after-offset cursor))
1140 (list (cons (symbol-name def-kind) (cdr spec))))))
1142 ;; Not edebugging this form, so reset the symbol's edebug
1143 ;; property to be just a marker at the definition's source code.
1144 ;; This only works for defs with simple names.
1145 (put def-name 'edebug (point-marker))
1146 ;; Also nil out dependent defs.
1147 '(mapcar (function
1148 (lambda (def)
1149 (put def-name 'edebug nil)))
1150 (get def-name 'edebug-dependents))
1151 (edebug-read-sexp)))
1153 ;; If all forms are being edebugged, explicitly wrap it.
1154 (edebug-all-forms
1155 (let ((cursor (edebug-new-cursor
1156 (list (edebug-read-storing-offsets (current-buffer)))
1157 (list edebug-offsets))))
1158 (edebug-make-form-wrapper
1159 cursor
1160 (edebug-before-offset cursor)
1161 (edebug-after-offset cursor)
1162 nil)))
1164 ;; Not a defining form, and not edebugging.
1165 (t (edebug-read-sexp)))
1169 (defvar edebug-def-args) ; args of defining form.
1170 (defvar edebug-def-interactive) ; is it an emacs interactive function?
1171 (defvar edebug-inside-func) ;; whether code is inside function context.
1172 ;; Currently def-form sets this to nil; def-body sets it to t.
1174 (defun edebug-interactive-p-name ()
1175 ;; Return a unique symbol for the variable used to store the
1176 ;; status of interactive-p for this function.
1177 (intern (format "edebug-%s-interactive-p" edebug-def-name)))
1180 (defun edebug-wrap-def-body (forms)
1181 "Wrap the FORMS of a definition body."
1182 (if edebug-def-interactive
1183 `(let ((,(edebug-interactive-p-name)
1184 (interactive-p)))
1185 ,(edebug-make-enter-wrapper forms))
1186 (edebug-make-enter-wrapper forms)))
1189 (defun edebug-make-enter-wrapper (forms)
1190 ;; Generate the enter wrapper for some forms of a definition.
1191 ;; This is not to be used for the body of other forms, e.g. `while',
1192 ;; since it wraps the list of forms with a call to `edebug-enter'.
1193 ;; Uses the dynamically bound vars edebug-def-name and edebug-def-args.
1194 ;; Do this after parsing since that may find a name.
1195 (setq edebug-def-name
1196 (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
1197 `(edebug-enter
1198 (quote ,edebug-def-name)
1199 ,(if edebug-inside-func
1200 `(list
1201 ;; Doesn't work with more than one def-body!!
1202 ;; But the list will just be reversed.
1203 ,@(nreverse edebug-def-args))
1204 'nil)
1205 (function (lambda () ,@forms))
1209 (defvar edebug-form-begin-marker) ; the mark for def being instrumented
1211 (defvar edebug-offset-index) ; the next available offset index.
1212 (defvar edebug-offset-list) ; the list of offset positions.
1214 (defun edebug-inc-offset (offset)
1215 ;; Modifies edebug-offset-index and edebug-offset-list
1216 ;; accesses edebug-func-marc and buffer point.
1217 (prog1
1218 edebug-offset-index
1219 (setq edebug-offset-list (cons (- offset edebug-form-begin-marker)
1220 edebug-offset-list)
1221 edebug-offset-index (1+ edebug-offset-index))))
1224 (defun edebug-make-before-and-after-form (before-index form after-index)
1225 ;; Return the edebug form for the current function at offset BEFORE-INDEX
1226 ;; given FORM. Looks like:
1227 ;; (edebug-after (edebug-before BEFORE-INDEX) AFTER-INDEX FORM)
1228 ;; Also increment the offset index for subsequent use.
1229 `(edebug-after (edebug-before ,before-index) ,after-index ,form))
1231 (defun edebug-make-after-form (form after-index)
1232 ;; Like edebug-make-before-and-after-form, but only after.
1233 `(edebug-after 0 ,after-index ,form))
1236 (defun edebug-unwrap (sexp)
1237 "Return the unwrapped SEXP or return it as is if it is not wrapped.
1238 The SEXP might be the result of wrapping a body, which is a list of
1239 expressions; a `progn' form will be returned enclosing these forms."
1240 (if (consp sexp)
1241 (cond
1242 ((eq 'edebug-after (car sexp))
1243 (nth 3 sexp))
1244 ((eq 'edebug-enter (car sexp))
1245 (macroexp-progn (nthcdr 2 (nth 1 (nth 3 sexp)))))
1246 (t sexp);; otherwise it is not wrapped, so just return it.
1248 sexp))
1250 (defun edebug-unwrap* (sexp)
1251 "Return the SEXP recursively unwrapped."
1252 (let ((new-sexp (edebug-unwrap sexp)))
1253 (while (not (eq sexp new-sexp))
1254 (setq sexp new-sexp
1255 new-sexp (edebug-unwrap sexp)))
1256 (if (consp new-sexp)
1257 (mapcar #'edebug-unwrap* new-sexp)
1258 new-sexp)))
1261 (defun edebug-defining-form (cursor form-begin form-end speclist)
1262 ;; Process the defining form, starting outside the form.
1263 ;; The speclist is a generated list spec that looks like:
1264 ;; (("def-symbol" defining-form-spec-sans-&define))
1265 ;; Skip the first offset.
1266 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1267 (cdr (edebug-cursor-offsets cursor)))
1268 (edebug-make-form-wrapper
1269 cursor
1270 form-begin (1- form-end)
1271 speclist))
1273 (defun edebug-make-form-wrapper (cursor form-begin form-end
1274 &optional speclist)
1275 ;; Wrap a form, usually a defining form, but any evaluated one.
1276 ;; If speclist is non-nil, this is being called by edebug-defining-form.
1277 ;; Otherwise it is being called from edebug-read-and-maybe-wrap-form1.
1278 ;; This is a hack, but I haven't figured out a simpler way yet.
1279 (let* ((form-data-entry (edebug-get-form-data-entry form-begin form-end))
1280 ;; Set this marker before parsing.
1281 (edebug-form-begin-marker
1282 (if form-data-entry
1283 (edebug--form-data-begin form-data-entry)
1284 ;; Buffer must be current-buffer for this to work:
1285 (set-marker (make-marker) form-begin))))
1287 (let (edebug-offset-list
1288 (edebug-offset-index 0)
1289 result
1290 ;; For definitions.
1291 ;; (edebug-containing-def-name edebug-def-name)
1292 ;; Get name from form-data, if any.
1293 (edebug-old-def-name (edebug--form-data-name form-data-entry))
1294 edebug-def-name
1295 edebug-def-args
1296 edebug-def-interactive
1297 edebug-inside-func;; whether wrapped code executes inside a function.
1300 (setq result
1301 (if speclist
1302 (edebug-match cursor speclist)
1304 ;; else wrap as an enter-form.
1305 (edebug-make-enter-wrapper (list (edebug-form cursor)))))
1307 ;; Set the name here if it was not set by edebug-make-enter-wrapper.
1308 (setq edebug-def-name
1309 (or edebug-def-name edebug-old-def-name (cl-gensym "edebug-anon")))
1311 ;; Add this def as a dependent of containing def. Buggy.
1312 '(if (and edebug-containing-def-name
1313 (not (get edebug-containing-def-name 'edebug-dependents)))
1314 (put edebug-containing-def-name 'edebug-dependents
1315 (cons edebug-def-name
1316 (get edebug-containing-def-name
1317 'edebug-dependents))))
1319 ;; Create a form-data-entry or modify existing entry's markers.
1320 ;; In the latter case, pointers to the entry remain eq.
1321 (if (not form-data-entry)
1322 (setq form-data-entry
1323 (edebug--make-form-data-entry
1324 edebug-def-name
1325 edebug-form-begin-marker
1326 ;; Buffer must be current-buffer.
1327 (set-marker (make-marker) form-end)
1329 (edebug-set-form-data-entry
1330 form-data-entry edebug-def-name ;; in case name is changed
1331 form-begin form-end))
1333 ;; (message "defining: %s" edebug-def-name) (sit-for 2)
1334 (edebug-make-top-form-data-entry form-data-entry)
1335 (message "Edebug: %s" edebug-def-name)
1336 ;;(debug edebug-def-name)
1338 ;; Destructively reverse edebug-offset-list and make vector from it.
1339 (setq edebug-offset-list (vconcat (nreverse edebug-offset-list)))
1341 ;; Side effects on the property list of edebug-def-name.
1342 (edebug-clear-frequency-count edebug-def-name)
1343 (edebug-clear-coverage edebug-def-name)
1345 ;; Set up the initial window data.
1346 (if (not edebug-top-window-data) ;; if not already set, do it now.
1347 (let ((window ;; Find the best window for this buffer.
1348 (or (get-buffer-window (current-buffer))
1349 (selected-window))))
1350 (setq edebug-top-window-data
1351 (cons window (window-start window)))))
1353 ;; Store the edebug data in symbol's property list.
1354 (put edebug-def-name 'edebug
1355 ;; A struct or vector would be better here!!
1356 (list edebug-form-begin-marker
1357 nil ; clear breakpoints
1358 edebug-offset-list
1359 edebug-top-window-data
1361 result
1365 (defun edebug-clear-frequency-count (name)
1366 ;; Create initial frequency count vector.
1367 ;; For each stop point, the counter is incremented each time it is visited.
1368 (put name 'edebug-freq-count
1369 (make-vector (length edebug-offset-list) 0)))
1372 (defun edebug-clear-coverage (name)
1373 ;; Create initial coverage vector.
1374 ;; Only need one per expression, but it is simpler to use stop points.
1375 (put name 'edebug-coverage
1376 (make-vector (length edebug-offset-list) 'unknown)))
1379 (defun edebug-form (cursor)
1380 ;; Return the instrumented form for the following form.
1381 ;; Add the point offsets to the edebug-offset-list for the form.
1382 (let* ((form (edebug-top-element-required cursor "Expected form"))
1383 (offset (edebug-top-offset cursor)))
1384 (prog1
1385 (cond
1386 ((consp form)
1387 ;; The first offset for a list form is for the list form itself.
1388 (if (eq 'quote (car form))
1389 form
1390 (let* ((head (car form))
1391 (spec (and (symbolp head) (get-edebug-spec head)))
1392 (new-cursor (edebug-new-cursor form offset)))
1393 ;; Find out if this is a defining form from first symbol.
1394 ;; An indirect spec would not work here, yet.
1395 (if (and (consp spec) (eq '&define (car spec)))
1396 (edebug-defining-form
1397 new-cursor
1398 (car offset);; before the form
1399 (edebug-after-offset cursor)
1400 (cons (symbol-name head) (cdr spec)))
1401 ;; Wrap a regular form.
1402 (edebug-make-before-and-after-form
1403 (edebug-inc-offset (car offset))
1404 (edebug-list-form new-cursor)
1405 ;; After processing the list form, the new-cursor is left
1406 ;; with the offset after the form.
1407 (edebug-inc-offset (edebug-cursor-offsets new-cursor))))
1410 ((symbolp form)
1411 (cond
1412 ;; Check for constant symbols that don't get wrapped.
1413 ((or (memq form '(t nil))
1414 (keywordp form))
1415 form)
1417 (t ;; just a variable
1418 (edebug-make-after-form form (edebug-inc-offset (cdr offset))))))
1420 ;; Anything else is self-evaluating.
1421 (t form))
1422 (edebug-move-cursor cursor))))
1425 (defsubst edebug-forms (cursor) (edebug-match cursor '(&rest form)))
1426 (defsubst edebug-sexps (cursor) (edebug-match cursor '(&rest sexp)))
1428 (defsubst edebug-list-form-args (head cursor)
1429 ;; Process the arguments of a list form given that head of form is a symbol.
1430 ;; Helper for edebug-list-form
1431 (let ((spec (get-edebug-spec head)))
1432 (cond
1433 (spec
1434 (cond
1435 ((consp spec)
1436 ;; It is a speclist.
1437 (let (edebug-best-error
1438 edebug-error-point);; This may not be needed.
1439 (edebug-match-sublist cursor spec)))
1440 ((eq t spec) (edebug-forms cursor))
1441 ((eq 0 spec) (edebug-sexps cursor))
1442 ((symbolp spec) (funcall spec cursor));; Not used by edebug,
1443 ; but leave it in for compatibility.
1445 ;; No edebug-form-spec provided.
1446 ((macrop head)
1447 (if edebug-eval-macro-args
1448 (edebug-forms cursor)
1449 (edebug-sexps cursor)))
1450 (t ;; Otherwise it is a function call.
1451 (edebug-forms cursor)))))
1454 (defun edebug-list-form (cursor)
1455 ;; Return an instrumented form built from the list form.
1456 ;; The after offset will be left in the cursor after processing the form.
1457 (let ((head (edebug-top-element-required cursor "Expected elements"))
1458 ;; Prevent backtracking whenever instrumenting.
1459 (edebug-gate t)
1460 ;; A list form is never optional because it matches anything.
1461 (edebug-&optional nil)
1462 (edebug-&rest nil))
1463 ;; Skip the first offset.
1464 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1465 (cdr (edebug-cursor-offsets cursor)))
1466 (cond
1467 ((symbolp head)
1468 (cond
1469 ((null head) nil) ; () is valid.
1470 ((eq head 'interactive-p)
1471 ;; Special case: replace (interactive-p) with variable
1472 (setq edebug-def-interactive 'check-it)
1473 (edebug-move-cursor cursor)
1474 (edebug-interactive-p-name))
1476 (cons head (edebug-list-form-args
1477 head (edebug-move-cursor cursor))))))
1479 ((consp head)
1480 (if (eq (car head) '\,)
1481 ;; The head of a form should normally be a symbol or a lambda
1482 ;; expression but it can also be an unquote form to be filled
1483 ;; before evaluation. We evaluate the arguments anyway, on the
1484 ;; assumption that the unquote form will place a proper function
1485 ;; name (rather than a macro name).
1486 (edebug-match cursor '(("," def-form) body))
1487 ;; Process anonymous function and args.
1488 ;; This assumes no anonymous macros.
1489 (edebug-match-specs cursor '(lambda-expr body) 'edebug-match-specs)))
1491 (t (edebug-syntax-error
1492 "Head of list form must be a symbol or lambda expression")))
1495 ;;; Matching of specs.
1497 (defvar edebug-after-dotted-spec nil)
1499 (defvar edebug-matching-depth 0) ;; initial value
1502 ;;; Failure to match
1504 ;; This throws to no-match, if there are higher alternatives.
1505 ;; Otherwise it signals an error. The place of the error is found
1506 ;; with the two before- and after-offset functions.
1508 (defun edebug-no-match (cursor &rest args)
1509 ;; Throw a no-match, or signal an error immediately if gate is active.
1510 ;; Remember this point in case we need to report this error.
1511 (setq edebug-error-point (or edebug-error-point
1512 (edebug-before-offset cursor))
1513 edebug-best-error (or edebug-best-error args))
1514 (if (and edebug-gate (not edebug-&optional))
1515 (progn
1516 (if edebug-error-point
1517 (goto-char edebug-error-point))
1518 (apply #'edebug-syntax-error args))
1519 (throw 'no-match args)))
1522 (defun edebug-match (cursor specs)
1523 ;; Top level spec matching function.
1524 ;; Used also at each lower level of specs.
1525 (let (edebug-&optional
1526 edebug-&rest
1527 edebug-best-error
1528 edebug-error-point
1529 (edebug-gate edebug-gate) ;; locally bound to limit effect
1531 (edebug-match-specs cursor specs 'edebug-match-specs)))
1534 (defun edebug-match-one-spec (cursor spec)
1535 ;; Match one spec, which is not a keyword &-spec.
1536 (cond
1537 ((symbolp spec) (edebug-match-symbol cursor spec))
1538 ((vectorp spec) (edebug-match cursor (append spec nil)))
1539 ((stringp spec) (edebug-match-string cursor spec))
1540 ((listp spec) (edebug-match-list cursor spec))
1544 (defun edebug-match-specs (cursor specs remainder-handler)
1545 ;; Append results of matching the list of specs.
1546 ;; The first spec is handled and the remainder-handler handles the rest.
1547 (let ((edebug-matching-depth
1548 (if (> edebug-matching-depth edebug-max-depth)
1549 (error "Too deep - perhaps infinite loop in spec?")
1550 (1+ edebug-matching-depth))))
1551 (cond
1552 ((null specs) nil)
1554 ;; Is the spec dotted?
1555 ((atom specs)
1556 (let ((edebug-dotted-spec t));; Containing spec list was dotted.
1557 (edebug-match-specs cursor (list specs) remainder-handler)))
1559 ;; Is the form dotted?
1560 ((not (listp (edebug-cursor-expressions cursor)));; allow nil
1561 (if (not edebug-dotted-spec)
1562 (edebug-no-match cursor "Dotted spec required."))
1563 ;; Cancel dotted spec and dotted form.
1564 (let ((edebug-dotted-spec)
1565 (this-form (edebug-cursor-expressions cursor))
1566 (this-offset (edebug-cursor-offsets cursor)))
1567 ;; Wrap the form in a list, (by changing the cursor??)...
1568 (edebug-set-cursor cursor (list this-form) this-offset)
1569 ;; and process normally, then unwrap the result.
1570 (car (edebug-match-specs cursor specs remainder-handler))))
1572 (t;; Process normally.
1573 (let* ((spec (car specs))
1574 (rest)
1575 (first-char (and (symbolp spec) (aref (symbol-name spec) 0))))
1576 ;;(message "spec = %s first char = %s" spec first-char) (sit-for 1)
1577 (nconc
1578 (cond
1579 ((eq ?& first-char);; "&" symbols take all following specs.
1580 (funcall (get-edebug-spec spec) cursor (cdr specs)))
1581 ((eq ?: first-char);; ":" symbols take one following spec.
1582 (setq rest (cdr (cdr specs)))
1583 (funcall (get-edebug-spec spec) cursor (car (cdr specs))))
1584 (t;; Any other normal spec.
1585 (setq rest (cdr specs))
1586 (edebug-match-one-spec cursor spec)))
1587 (funcall remainder-handler cursor rest remainder-handler)))))))
1590 ;; Define specs for all the symbol specs with functions used to process them.
1591 ;; Perhaps we shouldn't be doing this with edebug-form-specs since the
1592 ;; user may want to define macros or functions with the same names.
1593 ;; We could use an internal obarray for these primitive specs.
1595 (dolist (pair '((&optional . edebug-match-&optional)
1596 (&rest . edebug-match-&rest)
1597 (&or . edebug-match-&or)
1598 (form . edebug-match-form)
1599 (sexp . edebug-match-sexp)
1600 (body . edebug-match-body)
1601 (&define . edebug-match-&define)
1602 (name . edebug-match-name)
1603 (:name . edebug-match-colon-name)
1604 (arg . edebug-match-arg)
1605 (def-body . edebug-match-def-body)
1606 (def-form . edebug-match-def-form)
1607 ;; Less frequently used:
1608 ;; (function . edebug-match-function)
1609 (lambda-expr . edebug-match-lambda-expr)
1610 (&not . edebug-match-&not)
1611 (&key . edebug-match-&key)
1612 (place . edebug-match-place)
1613 (gate . edebug-match-gate)
1614 ;; (nil . edebug-match-nil) not this one - special case it.
1616 (put (car pair) 'edebug-form-spec (cdr pair)))
1618 (defun edebug-match-symbol (cursor symbol)
1619 ;; Match a symbol spec.
1620 (let* ((spec (get-edebug-spec symbol)))
1621 (cond
1622 (spec
1623 (if (consp spec)
1624 ;; It is an indirect spec.
1625 (edebug-match cursor spec)
1626 ;; Otherwise it should be the symbol name of a function.
1627 ;; There could be a bug here - maybe need to do edebug-match bindings.
1628 (funcall spec cursor)))
1630 ((null symbol) ;; special case this.
1631 (edebug-match-nil cursor))
1633 ((fboundp symbol) ; is it a predicate?
1634 (let ((sexp (edebug-top-element-required cursor "Expected" symbol)))
1635 ;; Special case for edebug-`.
1636 (if (and (listp sexp) (eq (car sexp) '\,))
1637 (edebug-match cursor '(("," def-form)))
1638 (if (not (funcall symbol sexp))
1639 (edebug-no-match cursor symbol "failed"))
1640 (edebug-move-cursor cursor)
1641 (list sexp))))
1642 (t (error "%s is not a form-spec or function" symbol))
1646 (defun edebug-match-sexp (cursor)
1647 (list (prog1 (edebug-top-element-required cursor "Expected sexp")
1648 (edebug-move-cursor cursor))))
1650 (defun edebug-match-form (cursor)
1651 (list (edebug-form cursor)))
1653 (defalias 'edebug-match-place 'edebug-match-form)
1654 ;; Currently identical to edebug-match-form.
1655 ;; This is for common lisp setf-style place arguments.
1657 (defsubst edebug-match-body (cursor) (edebug-forms cursor))
1659 (defun edebug-match-&optional (cursor specs)
1660 ;; Keep matching until one spec fails.
1661 (edebug-&optional-wrapper cursor specs 'edebug-&optional-wrapper))
1663 (defun edebug-&optional-wrapper (cursor specs remainder-handler)
1664 (let (result
1665 (edebug-&optional specs)
1666 (edebug-gate nil)
1667 (this-form (edebug-cursor-expressions cursor))
1668 (this-offset (edebug-cursor-offsets cursor)))
1669 (if (null (catch 'no-match
1670 (setq result
1671 (edebug-match-specs cursor specs remainder-handler))
1672 ;; Returning nil means no no-match was thrown.
1673 nil))
1674 result
1675 ;; no-match, but don't fail; just reset cursor and return nil.
1676 (edebug-set-cursor cursor this-form this-offset)
1677 nil)))
1680 (defun edebug-&rest-wrapper (cursor specs remainder-handler)
1681 (if (null specs) (setq specs edebug-&rest))
1682 ;; Reuse the &optional handler with this as the remainder handler.
1683 (edebug-&optional-wrapper cursor specs remainder-handler))
1685 (defun edebug-match-&rest (cursor specs)
1686 ;; Repeatedly use specs until failure.
1687 (let ((edebug-&rest specs) ;; remember these
1688 edebug-best-error
1689 edebug-error-point)
1690 (edebug-&rest-wrapper cursor specs 'edebug-&rest-wrapper)))
1693 (defun edebug-match-&or (cursor specs)
1694 ;; Keep matching until one spec succeeds, and return its results.
1695 ;; If none match, fail.
1696 ;; This needs to be optimized since most specs spend time here.
1697 (let ((original-specs specs)
1698 (this-form (edebug-cursor-expressions cursor))
1699 (this-offset (edebug-cursor-offsets cursor)))
1700 (catch 'matched
1701 (while specs
1702 (catch 'no-match
1703 (throw 'matched
1704 (let (edebug-gate ;; only while matching each spec
1705 edebug-best-error
1706 edebug-error-point)
1707 ;; Doesn't support e.g. &or symbolp &rest form
1708 (edebug-match-one-spec cursor (car specs)))))
1709 ;; Match failed, so reset and try again.
1710 (setq specs (cdr specs))
1711 ;; Reset the cursor for the next match.
1712 (edebug-set-cursor cursor this-form this-offset))
1713 ;; All failed.
1714 (apply #'edebug-no-match cursor "Expected one of" original-specs))
1718 (defun edebug-match-&not (cursor specs)
1719 ;; If any specs match, then fail
1720 (if (null (catch 'no-match
1721 (let ((edebug-gate nil))
1722 (save-excursion
1723 (edebug-match-&or cursor specs)))
1724 nil))
1725 ;; This means something matched, so it is a no match.
1726 (edebug-no-match cursor "Unexpected"))
1727 ;; This means nothing matched, so it is OK.
1728 nil) ;; So, return nothing
1731 (def-edebug-spec &key edebug-match-&key)
1733 (defun edebug-match-&key (cursor specs)
1734 ;; Following specs must look like (<name> <spec>) ...
1735 ;; where <name> is the name of a keyword, and spec is its spec.
1736 ;; This really doesn't save much over the expanded form and takes time.
1737 (edebug-match-&rest
1738 cursor
1739 (cons '&or
1740 (mapcar (lambda (pair)
1741 (vector (format ":%s" (car pair))
1742 (car (cdr pair))))
1743 specs))))
1746 (defun edebug-match-gate (_cursor)
1747 ;; Simply set the gate to prevent backtracking at this level.
1748 (setq edebug-gate t)
1749 nil)
1752 (defun edebug-match-list (cursor specs)
1753 ;; The spec is a list, but what kind of list, and what context?
1754 (if edebug-dotted-spec
1755 ;; After dotted spec but form did not contain dot,
1756 ;; so match list spec elements as if spliced in.
1757 (prog1
1758 (let ((edebug-dotted-spec))
1759 (edebug-match-specs cursor specs 'edebug-match-specs))
1760 ;; If it matched, really clear the dotted-spec flag.
1761 (setq edebug-dotted-spec nil))
1762 (let ((spec (car specs))
1763 (form (edebug-top-element-required cursor "Expected" specs)))
1764 (cond
1765 ((eq 'quote spec)
1766 (let ((spec (car (cdr specs))))
1767 (cond
1768 ((symbolp spec)
1769 ;; Special case: spec quotes a symbol to match.
1770 ;; Change in future. Use "..." instead.
1771 (if (not (eq spec form))
1772 (edebug-no-match cursor "Expected" spec))
1773 (edebug-move-cursor cursor)
1774 (setq edebug-gate t)
1775 form)
1777 (error "Bad spec: %s" specs)))))
1779 ((eq 'vector spec)
1780 (if (vectorp form)
1781 ;; Special case: match a vector with the specs.
1782 (let ((result (edebug-match-sublist
1783 (edebug-new-cursor
1784 form (cdr (edebug-top-offset cursor)))
1785 (cdr specs))))
1786 (edebug-move-cursor cursor)
1787 (list (apply #'vector result)))
1788 (edebug-no-match cursor "Expected" specs)))
1790 ((listp form)
1791 (prog1
1792 (list (edebug-match-sublist
1793 ;; First offset is for the list form itself.
1794 ;; Treat nil as empty list.
1795 (edebug-new-cursor form (cdr (edebug-top-offset cursor)))
1796 specs))
1797 (edebug-move-cursor cursor)))
1799 (t (edebug-no-match cursor "Expected" specs)))
1803 (defun edebug-match-sublist (cursor specs)
1804 ;; Match a sublist of specs.
1805 (let (edebug-&optional
1806 ;;edebug-best-error
1807 ;;edebug-error-point
1809 (prog1
1810 ;; match with edebug-match-specs so edebug-best-error is not bound.
1811 (edebug-match-specs cursor specs 'edebug-match-specs)
1812 (if (not (edebug-empty-cursor cursor))
1813 (if edebug-best-error
1814 (apply #'edebug-no-match cursor edebug-best-error)
1815 ;; A failed &rest or &optional spec may leave some args.
1816 (edebug-no-match cursor "Failed matching" specs)
1817 )))))
1820 (defun edebug-match-string (cursor spec)
1821 (let ((sexp (edebug-top-element-required cursor "Expected" spec)))
1822 (if (not (eq (intern spec) sexp))
1823 (edebug-no-match cursor "Expected" spec)
1824 ;; Since it matched, failure means immediate error, unless &optional.
1825 (setq edebug-gate t)
1826 (edebug-move-cursor cursor)
1827 (list sexp)
1830 (defun edebug-match-nil (cursor)
1831 ;; There must be nothing left to match a nil.
1832 (if (not (edebug-empty-cursor cursor))
1833 (edebug-no-match cursor "Unmatched argument(s)")
1834 nil))
1837 (defun edebug-match-function (_cursor)
1838 (error "Use function-form instead of function in edebug spec"))
1840 (defun edebug-match-&define (cursor specs)
1841 ;; Match a defining form.
1842 ;; Normally, &define is interpreted specially other places.
1843 ;; This should only be called inside of a spec list to match the remainder
1844 ;; of the current list. e.g. ("lambda" &define args def-body)
1845 (edebug-make-form-wrapper
1846 cursor
1847 (edebug-before-offset cursor)
1848 ;; Find the last offset in the list.
1849 (let ((offsets (edebug-cursor-offsets cursor)))
1850 (while (consp offsets) (setq offsets (cdr offsets)))
1851 offsets)
1852 specs))
1854 (defun edebug-match-lambda-expr (cursor)
1855 ;; The expression must be a function.
1856 ;; This will match any list form that begins with a symbol
1857 ;; that has an edebug-form-spec beginning with &define. In
1858 ;; practice, only lambda expressions should be used.
1859 ;; I could add a &lambda specification to avoid confusion.
1860 (let* ((sexp (edebug-top-element-required
1861 cursor "Expected lambda expression"))
1862 (offset (edebug-top-offset cursor))
1863 (head (and (consp sexp) (car sexp)))
1864 (spec (and (symbolp head) (get-edebug-spec head)))
1865 (edebug-inside-func nil))
1866 ;; Find out if this is a defining form from first symbol.
1867 (if (and (consp spec) (eq '&define (car spec)))
1868 (prog1
1869 (list
1870 (edebug-defining-form
1871 (edebug-new-cursor sexp offset)
1872 (car offset);; before the sexp
1873 (edebug-after-offset cursor)
1874 (cons (symbol-name head) (cdr spec))))
1875 (edebug-move-cursor cursor))
1876 (edebug-no-match cursor "Expected lambda expression")
1880 (defun edebug-match-name (cursor)
1881 ;; Set the edebug-def-name bound in edebug-defining-form.
1882 (let ((name (edebug-top-element-required cursor "Expected name")))
1883 ;; Maybe strings and numbers could be used.
1884 (if (not (symbolp name))
1885 (edebug-no-match cursor "Symbol expected for name of definition"))
1886 (setq edebug-def-name
1887 (if edebug-def-name
1888 ;; Construct a new name by appending to previous name.
1889 (intern (format "%s@%s" edebug-def-name name))
1890 name))
1891 (edebug-move-cursor cursor)
1892 (list name)))
1894 (defun edebug-match-colon-name (_cursor spec)
1895 ;; Set the edebug-def-name to the spec.
1896 (setq edebug-def-name
1897 (if edebug-def-name
1898 ;; Construct a new name by appending to previous name.
1899 (intern (format "%s@%s" edebug-def-name spec))
1900 spec))
1901 nil)
1903 (defun edebug-match-arg (cursor)
1904 ;; set the def-args bound in edebug-defining-form
1905 (let ((edebug-arg (edebug-top-element-required cursor "Expected arg")))
1906 (if (or (not (symbolp edebug-arg))
1907 (edebug-lambda-list-keywordp edebug-arg))
1908 (edebug-no-match cursor "Bad argument:" edebug-arg))
1909 (edebug-move-cursor cursor)
1910 (setq edebug-def-args (cons edebug-arg edebug-def-args))
1911 (list edebug-arg)))
1913 (defun edebug-match-def-form (cursor)
1914 ;; Like form but the form is wrapped in edebug-enter form.
1915 ;; The form is assumed to be executing outside of the function context.
1916 ;; This is a hack for now, since a def-form might execute inside as well.
1917 ;; Not to be used otherwise.
1918 (let ((edebug-inside-func nil))
1919 (list (edebug-make-enter-wrapper (list (edebug-form cursor))))))
1921 (defun edebug-match-def-body (cursor)
1922 ;; Like body but body is wrapped in edebug-enter form.
1923 ;; The body is assumed to be executing inside of the function context.
1924 ;; Not to be used otherwise.
1925 (let* ((edebug-inside-func t)
1926 (forms (edebug-forms cursor)))
1927 ;; If there's no form, there's nothing to wrap!
1928 ;; This happens to handle bug#20281, tho maybe a better fix would be to
1929 ;; improve the `defun' spec.
1930 (when forms
1931 (list (edebug-wrap-def-body forms)))))
1934 ;;;; Edebug Form Specs
1935 ;;; ==========================================================
1937 ;;;;* Spec for def-edebug-spec
1938 ;;; Out of date.
1940 (defun edebug-spec-p (object)
1941 "Return non-nil if OBJECT is a symbol with an edebug-form-spec property."
1942 (and (symbolp object)
1943 (get object 'edebug-form-spec)))
1945 (def-edebug-spec def-edebug-spec
1946 ;; Top level is different from lower levels.
1947 (&define :name edebug-spec name
1948 &or "nil" edebug-spec-p "t" "0" (&rest edebug-spec)))
1950 (def-edebug-spec edebug-spec-list
1951 ;; A list must have something in it, or it is nil, a symbolp
1952 ((edebug-spec . [&or nil edebug-spec])))
1954 (def-edebug-spec edebug-spec
1955 (&or
1956 (vector &rest edebug-spec) ; matches a vector
1957 ("vector" &rest edebug-spec) ; matches a vector spec
1958 ("quote" symbolp)
1959 edebug-spec-list
1960 stringp
1961 [edebug-lambda-list-keywordp &rest edebug-spec]
1962 [keywordp gate edebug-spec]
1963 edebug-spec-p ;; Including all the special ones e.g. form.
1964 symbolp;; a predicate
1968 ;;;* Emacs special forms and some functions.
1970 ;; quote expects only one argument, although it allows any number.
1971 (def-edebug-spec quote sexp)
1973 ;; The standard defining forms.
1974 (def-edebug-spec defconst defvar)
1975 (def-edebug-spec defvar (symbolp &optional form stringp))
1977 (def-edebug-spec defun
1978 (&define name lambda-list
1979 [&optional stringp]
1980 [&optional ("declare" &rest sexp)]
1981 [&optional ("interactive" interactive)]
1982 def-body))
1983 (def-edebug-spec defmacro
1984 ;; FIXME: Improve `declare' so we can Edebug gv-expander and
1985 ;; gv-setter declarations.
1986 (&define name lambda-list [&optional stringp]
1987 [&optional ("declare" &rest sexp)] def-body))
1989 (def-edebug-spec arglist lambda-list) ;; deprecated - use lambda-list.
1991 (def-edebug-spec lambda-list
1992 (([&rest arg]
1993 [&optional ["&optional" arg &rest arg]]
1994 &optional ["&rest" arg]
1997 (def-edebug-spec interactive
1998 (&optional &or stringp def-form))
2000 ;; A function-form is for an argument that may be a function or a form.
2001 ;; This specially recognizes anonymous functions quoted with quote.
2002 (def-edebug-spec function-form
2003 ;; form at the end could also handle "function",
2004 ;; but recognize it specially to avoid wrapping function forms.
2005 (&or ([&or "quote" "function"] &or symbolp lambda-expr) form))
2007 ;; function expects a symbol or a lambda or macro expression
2008 ;; A macro is allowed by Emacs.
2009 (def-edebug-spec function (&or symbolp lambda-expr))
2011 ;; A macro expression is a lambda expression with "macro" prepended.
2012 (def-edebug-spec macro (&define "lambda" lambda-list def-body))
2014 ;; (def-edebug-spec anonymous-form ((&or ["lambda" lambda] ["macro" macro])))
2016 ;; Standard functions that take function-forms arguments.
2018 ;; FIXME? The manual uses this form (maybe that's just for illustration?):
2019 ;; (def-edebug-spec let
2020 ;; ((&rest &or symbolp (gate symbolp &optional form))
2021 ;; body))
2022 (def-edebug-spec let
2023 ((&rest &or (symbolp &optional form) symbolp)
2024 body))
2026 (def-edebug-spec let* let)
2028 (def-edebug-spec setq (&rest symbolp form))
2029 (def-edebug-spec setq-default setq)
2031 (def-edebug-spec cond (&rest (&rest form)))
2033 (def-edebug-spec condition-case
2034 (symbolp
2035 form
2036 &rest ([&or symbolp (&rest symbolp)] body)))
2039 (def-edebug-spec \` (backquote-form))
2041 ;; Supports quotes inside backquotes,
2042 ;; but only at the top level inside unquotes.
2043 (def-edebug-spec backquote-form
2044 (&or
2045 ([&or "," ",@"] &or ("quote" backquote-form) form)
2046 ;; The simple version:
2047 ;; (backquote-form &rest backquote-form)
2048 ;; doesn't handle (a . ,b). The straightforward fix:
2049 ;; (backquote-form . [&or nil backquote-form])
2050 ;; uses up too much stack space.
2051 ;; Note that `(foo . ,@bar) is not valid, so we don't need to handle it.
2052 (backquote-form [&rest [&not ","] backquote-form]
2053 . [&or nil backquote-form])
2054 ;; If you use dotted forms in backquotes, replace the previous line
2055 ;; with the following. This takes quite a bit more stack space, however.
2056 ;; (backquote-form . [&or nil backquote-form])
2057 (vector &rest backquote-form)
2058 sexp))
2060 ;; Special version of backquote that instruments backquoted forms
2061 ;; destined to be evaluated, usually as the result of a
2062 ;; macroexpansion. Backquoted code can only have unquotes (, and ,@)
2063 ;; in places where list forms are allowed, and predicates. If the
2064 ;; backquote is used in a macro, unquoted code that come from
2065 ;; arguments must be instrumented, if at all, with def-form not def-body.
2067 ;; We could assume that all forms (not nested in other forms)
2068 ;; in arguments of macros should be def-forms, whether or not the macros
2069 ;; are defined with edebug-` but this would be expensive.
2071 ;; ,@ might have some problems.
2073 (defalias 'edebug-\` '\`) ;; same macro as regular backquote.
2074 (def-edebug-spec edebug-\` (def-form))
2076 ;; Assume immediate quote in unquotes mean backquote at next higher level.
2077 (def-edebug-spec \, (&or ("quote" edebug-\`) def-form))
2078 (def-edebug-spec \,@ (&define ;; so (,@ form) is never wrapped.
2079 &or ("quote" edebug-\`) def-form))
2081 ;; New byte compiler.
2083 (def-edebug-spec save-selected-window t)
2084 (def-edebug-spec save-current-buffer t)
2086 ;; Anything else?
2088 ;;; The debugger itself
2090 (defvar edebug-active nil) ;; Non-nil when edebug is active
2092 (defvar edebug-stack nil)
2093 ;; Stack of active functions evaluated via edebug.
2094 ;; Should be nil at the top level.
2096 (defvar edebug-stack-depth -1)
2097 ;; Index of last edebug-stack item.
2099 (defvar edebug-offset-indices nil)
2100 ;; Stack of offset indices of visited edebug sexps.
2101 ;; Should be nil at the top level.
2102 ;; Each function adds one cons. Top is modified with setcar.
2105 (defvar edebug-entered nil
2106 ;; Non-nil if edebug has already been entered at this recursive edit level.
2107 ;; This should stay nil at the top level.
2110 ;; Should these be options?
2111 (defconst edebug-debugger 'edebug
2112 ;; Name of function to use for debugging when error or quit occurs.
2113 ;; Set this to 'debug if you want to debug edebug.
2117 ;; Dynamically bound variables, declared globally but left unbound.
2118 (defvar edebug-function) ; the function being executed. change name!!
2119 (defvar edebug-data) ; the edebug data for the function
2120 (defvar edebug-def-mark) ; the mark for the definition
2121 (defvar edebug-freq-count) ; the count of expression visits.
2122 (defvar edebug-coverage) ; the coverage results of each expression of function.
2124 (defvar edebug-buffer) ; which buffer the function is in.
2126 (defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2127 (defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2129 (defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2130 (defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2132 ;;; Handling signals
2134 (defun edebug-signal (signal-name signal-data)
2135 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2136 A signal name is a symbol with an `error-conditions' property
2137 that is a list of condition names.
2138 A handler for any of those names will get to handle this signal.
2139 The symbol `error' should always be one of them.
2141 DATA should be a list. Its elements are printed as part of the error message.
2142 If the signal is handled, DATA is made available to the handler.
2143 See `condition-case'.
2145 This is the Edebug replacement for the standard `signal'. It should
2146 only be active while Edebug is. It checks `debug-on-error' to see
2147 whether it should call the debugger. When execution is resumed, the
2148 error is signaled again."
2149 (if (and (listp debug-on-error) (memq signal-name debug-on-error))
2150 (edebug 'error (cons signal-name signal-data)))
2151 ;; If we reach here without another non-local exit, then send signal again.
2152 ;; i.e. the signal is not continuable, yet.
2153 ;; Avoid infinite recursion.
2154 (let ((signal-hook-function nil))
2155 (signal signal-name signal-data)))
2157 ;;; Entering Edebug
2159 (defun edebug-enter (function args body)
2160 ;; Entering FUNC. The arguments are ARGS, and the body is BODY.
2161 ;; Setup edebug variables and evaluate BODY. This function is called
2162 ;; when a function evaluated with edebug-eval-top-level-form is entered.
2163 ;; Return the result of BODY.
2165 ;; Is this the first time we are entering edebug since
2166 ;; lower-level recursive-edit command?
2167 ;; More precisely, this tests whether Edebug is currently active.
2168 (let ((edebug-function function))
2169 (if (not edebug-entered)
2170 (let ((edebug-entered t)
2171 ;; Binding max-lisp-eval-depth here is OK,
2172 ;; but not inside an unwind-protect.
2173 ;; Doing it here also keeps it from growing too large.
2174 (max-lisp-eval-depth (+ 100 max-lisp-eval-depth)) ; too much??
2175 (max-specpdl-size (+ 200 max-specpdl-size))
2177 (debugger edebug-debugger) ; only while edebug is active.
2178 (edebug-outside-debug-on-error debug-on-error)
2179 (edebug-outside-debug-on-quit debug-on-quit)
2180 ;; Binding these may not be the right thing to do.
2181 ;; We want to allow the global values to be changed.
2182 (debug-on-error (or debug-on-error edebug-on-error))
2183 (debug-on-quit edebug-on-quit))
2184 (unwind-protect
2185 (let ((signal-hook-function 'edebug-signal))
2186 (setq edebug-execution-mode (or edebug-next-execution-mode
2187 edebug-initial-mode
2188 edebug-execution-mode)
2189 edebug-next-execution-mode nil)
2190 (edebug-enter function args body))))
2192 (let* ((edebug-data (get function 'edebug))
2193 (edebug-def-mark (car edebug-data)) ; mark at def start
2194 (edebug-freq-count (get function 'edebug-freq-count))
2195 (edebug-coverage (get function 'edebug-coverage))
2196 (edebug-buffer (marker-buffer edebug-def-mark))
2198 (edebug-stack (cons function edebug-stack))
2199 (edebug-offset-indices (cons 0 edebug-offset-indices))
2201 (if (get function 'edebug-on-entry)
2202 (progn
2203 (setq edebug-execution-mode 'step)
2204 (if (eq (get function 'edebug-on-entry) 'temp)
2205 (put function 'edebug-on-entry nil))))
2206 (if edebug-trace
2207 (edebug--enter-trace function args body)
2208 (funcall body))
2209 ))))
2211 (defun edebug-var-status (var)
2212 "Return a cons cell describing the status of VAR's current binding.
2213 The purpose of this function is so you can properly undo
2214 subsequent changes to the same binding, by passing the status
2215 cons cell to `edebug-restore-status'. The status cons cell
2216 has the form (LOCUS . VALUE), where LOCUS can be a buffer
2217 \(for a buffer-local binding), or nil (if the default binding is current)."
2218 (cons (variable-binding-locus var)
2219 (symbol-value var)))
2221 (defun edebug-restore-status (var status)
2222 "Reset VAR based on STATUS.
2223 STATUS should be a list returned by `edebug-var-status'."
2224 (let ((locus (car status))
2225 (value (cdr status)))
2226 (cond ((bufferp locus)
2227 (if (buffer-live-p locus)
2228 (with-current-buffer locus
2229 (set var value))))
2230 ((framep locus)
2231 (modify-frame-parameters locus (list (cons var value))))
2233 (set var value)))))
2235 (defun edebug--enter-trace (function args body)
2236 (let ((edebug-stack-depth (1+ edebug-stack-depth))
2237 edebug-result)
2238 (edebug-print-trace-before
2239 (format "%s args: %s" function args))
2240 (prog1 (setq edebug-result (funcall body))
2241 (edebug-print-trace-after
2242 (format "%s result: %s" function edebug-result)))))
2244 (def-edebug-spec edebug-tracing (form body))
2246 (defmacro edebug-tracing (msg &rest body)
2247 "Print MSG in *edebug-trace* before and after evaluating BODY.
2248 The result of BODY is also printed."
2249 `(let ((edebug-stack-depth (1+ edebug-stack-depth))
2250 edebug-result)
2251 (edebug-print-trace-before ,msg)
2252 (prog1 (setq edebug-result (progn ,@body))
2253 (edebug-print-trace-after
2254 (format "%s result: %s" ,msg edebug-result)))))
2256 (defun edebug-print-trace-before (msg)
2257 "Function called to print trace info before expression evaluation.
2258 MSG is printed after `::::{ '."
2259 (edebug-trace-display
2260 edebug-trace-buffer "%s{ %s" (make-string edebug-stack-depth ?\:) msg))
2262 (defun edebug-print-trace-after (msg)
2263 "Function called to print trace info after expression evaluation.
2264 MSG is printed after `::::} '."
2265 (edebug-trace-display
2266 edebug-trace-buffer "%s} %s" (make-string edebug-stack-depth ?\:) msg))
2270 (defun edebug-slow-before (before-index)
2271 (unless edebug-active
2272 ;; Debug current function given BEFORE position.
2273 ;; Called from functions compiled with edebug-eval-top-level-form.
2274 ;; Return the before index.
2275 (setcar edebug-offset-indices before-index)
2277 ;; Increment frequency count
2278 (aset edebug-freq-count before-index
2279 (1+ (aref edebug-freq-count before-index)))
2281 (if (or (not (memq edebug-execution-mode '(Go-nonstop next)))
2282 (input-pending-p))
2283 (edebug-debugger before-index 'before nil)))
2284 before-index)
2286 (defun edebug-fast-before (_before-index)
2287 ;; Do nothing.
2290 (defun edebug-slow-after (_before-index after-index value)
2291 (if edebug-active
2292 value
2293 ;; Debug current function given AFTER position and VALUE.
2294 ;; Called from functions compiled with edebug-eval-top-level-form.
2295 ;; Return VALUE.
2296 (setcar edebug-offset-indices after-index)
2298 ;; Increment frequency count
2299 (aset edebug-freq-count after-index
2300 (1+ (aref edebug-freq-count after-index)))
2301 (if edebug-test-coverage (edebug--update-coverage after-index value))
2303 (if (and (eq edebug-execution-mode 'Go-nonstop)
2304 (not (input-pending-p)))
2305 ;; Just return result.
2306 value
2307 (edebug-debugger after-index 'after value)
2310 (defun edebug-fast-after (_before-index _after-index value)
2311 ;; Do nothing but return the value.
2312 value)
2314 (defun edebug-run-slow ()
2315 (defalias 'edebug-before 'edebug-slow-before)
2316 (defalias 'edebug-after 'edebug-slow-after))
2318 ;; This is not used, yet.
2319 (defun edebug-run-fast ()
2320 (defalias 'edebug-before 'edebug-fast-before)
2321 (defalias 'edebug-after 'edebug-fast-after))
2323 (edebug-run-slow)
2326 (defun edebug--update-coverage (after-index value)
2327 (let ((old-result (aref edebug-coverage after-index)))
2328 (cond
2329 ((eq 'ok-coverage old-result))
2330 ((eq 'unknown old-result)
2331 (aset edebug-coverage after-index value))
2332 ;; Test if a different result.
2333 ((not (eq value old-result))
2334 (aset edebug-coverage after-index 'ok-coverage)))))
2337 ;; Dynamically declared unbound variables.
2338 (defvar edebug-breakpoints)
2339 (defvar edebug-break-data) ; break data for current function.
2340 (defvar edebug-break) ; whether a break occurred.
2341 (defvar edebug-global-break) ; whether a global break occurred.
2342 (defvar edebug-break-condition) ; whether the breakpoint is conditional.
2344 (defvar edebug-break-result nil)
2345 (defvar edebug-global-break-result nil)
2348 (defun edebug-debugger (offset-index arg-mode value)
2349 (if inhibit-redisplay
2350 ;; Don't really try to enter edebug within an eval from redisplay.
2351 value
2352 ;; Check breakpoints and pending input.
2353 ;; If edebug display should be updated, call edebug--display.
2354 ;; Return value.
2355 (let* ( ;; This needs to be here since breakpoints may be changed.
2356 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
2357 (edebug-break-data (assq offset-index edebug-breakpoints))
2358 (edebug-break-condition (car (cdr edebug-break-data)))
2359 (edebug-global-break
2360 (if edebug-global-break-condition
2361 (condition-case nil
2362 (setq edebug-global-break-result
2363 (edebug-eval edebug-global-break-condition))
2364 (error nil))))
2365 (edebug-break))
2367 ;;(edebug-trace "exp: %s" value)
2368 ;; Test whether we should break.
2369 (setq edebug-break
2370 (or edebug-global-break
2371 (and edebug-break-data
2372 (or (not edebug-break-condition)
2373 (setq edebug-break-result
2374 (edebug-eval edebug-break-condition))))))
2375 (if (and edebug-break
2376 (nth 2 edebug-break-data)) ; is it temporary?
2377 ;; Delete the breakpoint.
2378 (setcdr edebug-data
2379 (cons (delq edebug-break-data edebug-breakpoints)
2380 (cdr (cdr edebug-data)))))
2382 ;; Display if mode is not go, continue, or Continue-fast
2383 ;; or break, or input is pending,
2384 (if (or (not (memq edebug-execution-mode '(go continue Continue-fast)))
2385 edebug-break
2386 (input-pending-p))
2387 (edebug--display value offset-index arg-mode)) ; <---------- display
2389 value)))
2392 ;; window-start now stored with each function.
2393 ;;(defvar edebug-window-start nil)
2394 ;; Remember where each buffers' window starts between edebug calls.
2395 ;; This is to avoid spurious recentering.
2396 ;; Does this still need to be buffer-local??
2397 ;;(setq-default edebug-window-start nil)
2398 ;;(make-variable-buffer-local 'edebug-window-start)
2401 ;; Dynamically declared unbound vars
2402 (defvar edebug-point) ; the point in edebug buffer
2403 (defvar edebug-outside-buffer) ; the current-buffer outside of edebug
2404 (defvar edebug-outside-point) ; the point outside of edebug
2405 (defvar edebug-outside-mark) ; the mark outside of edebug
2406 (defvar edebug-window-data) ; window and window-start for current function
2407 (defvar edebug-outside-windows) ; outside window configuration
2408 (defvar edebug-eval-buffer) ; for the evaluation list.
2409 (defvar edebug-outside-d-c-i-n-s-w) ; outside default cursor-in-non-selected-windows
2411 (defvar edebug-eval-list nil) ;; List of expressions to evaluate.
2413 (defvar edebug-previous-result nil) ;; Last result returned.
2415 ;; Emacs 19 adds an arg to mark and mark-marker.
2416 (defalias 'edebug-mark-marker 'mark-marker)
2418 (defun edebug--display (value offset-index arg-mode)
2419 ;; edebug--display-1 is too big, we should split it. This function
2420 ;; here was just introduced to avoid making edebug--display-1
2421 ;; yet a bit deeper.
2422 (save-excursion (edebug--display-1 value offset-index arg-mode)))
2424 (defun edebug--display-1 (value offset-index arg-mode)
2425 (unless (marker-position edebug-def-mark)
2426 ;; The buffer holding the source has been killed.
2427 ;; Let's at least show a backtrace so the user can figure out
2428 ;; which function we're talking about.
2429 (debug))
2430 ;; Setup windows for edebug, determine mode, maybe enter recursive-edit.
2431 ;; Uses local variables of edebug-enter, edebug-before, edebug-after
2432 ;; and edebug-debugger.
2433 (let ((edebug-active t) ; For minor mode alist.
2434 (edebug-with-timeout-suspend (with-timeout-suspend))
2435 edebug-stop ; Should we enter recursive-edit?
2436 (edebug-point (+ edebug-def-mark
2437 (aref (nth 2 edebug-data) offset-index)))
2438 edebug-buffer-outside-point ; current point in edebug-buffer
2439 ;; window displaying edebug-buffer
2440 (edebug-window-data (nth 3 edebug-data))
2441 (edebug-outside-window (selected-window))
2442 (edebug-outside-buffer (current-buffer))
2443 (edebug-outside-point (point))
2444 (edebug-outside-mark (edebug-mark))
2445 edebug-outside-windows ; Window or screen configuration.
2446 edebug-buffer-points
2448 edebug-eval-buffer ; Declared here so we can kill it below.
2449 (eval-result-list (and edebug-eval-list
2450 (edebug-eval-result-list)))
2451 edebug-trace-window
2452 edebug-trace-window-start
2454 (edebug-outside-d-c-i-n-s-w
2455 (default-value 'cursor-in-non-selected-windows)))
2456 (unwind-protect
2457 (let ((cursor-in-echo-area nil)
2458 (unread-command-events nil)
2459 ;; any others??
2461 (setq-default cursor-in-non-selected-windows t)
2462 (if (not (buffer-name edebug-buffer))
2463 (user-error "Buffer defining %s not found" edebug-function))
2465 (if (eq 'after arg-mode)
2466 ;; Compute result string now before windows are modified.
2467 (edebug-compute-previous-result value))
2469 (if edebug-save-windows
2470 ;; Save windows now before we modify them.
2471 (setq edebug-outside-windows
2472 (edebug-current-windows edebug-save-windows)))
2474 (if edebug-save-displayed-buffer-points
2475 (setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
2477 ;; First move the edebug buffer point to edebug-point
2478 ;; so that window start doesn't get changed when we display it.
2479 ;; I don't know if this is going to help.
2480 ;;(set-buffer edebug-buffer)
2481 ;;(goto-char edebug-point)
2483 ;; If edebug-buffer is not currently displayed,
2484 ;; first find a window for it.
2485 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data))
2486 (setcar edebug-window-data (selected-window))
2488 ;; Now display eval list, if any.
2489 ;; This is done after the pop to edebug-buffer
2490 ;; so that buffer-window correspondence is correct after quitting.
2491 (edebug-eval-display eval-result-list)
2492 ;; The evaluation list better not have deleted edebug-window-data.
2493 (select-window (car edebug-window-data))
2494 (set-buffer edebug-buffer)
2496 (setq edebug-buffer-outside-point (point))
2497 (goto-char edebug-point)
2499 (if (eq 'before arg-mode)
2500 ;; Check whether positions are up-to-date.
2501 ;; This assumes point is never before symbol.
2502 (if (not (memq (following-char) '(?\( ?\# ?\` )))
2503 (user-error "Source has changed - reevaluate definition of %s"
2504 edebug-function)
2507 ;; Make sure we bind those in the right buffer (bug#16410).
2508 (let ((overlay-arrow-position overlay-arrow-position)
2509 (overlay-arrow-string overlay-arrow-string))
2510 ;; Now display arrow based on mode.
2511 (edebug-overlay-arrow)
2513 (cond
2514 ((eq 'error arg-mode)
2515 ;; Display error message
2516 (setq edebug-execution-mode 'step)
2517 (edebug-overlay-arrow)
2518 (beep)
2519 (if (eq 'quit (car value))
2520 (message "Quit")
2521 (edebug-report-error value)))
2522 (edebug-break
2523 (cond
2524 (edebug-global-break
2525 (message "Global Break: %s => %s"
2526 edebug-global-break-condition
2527 edebug-global-break-result))
2528 (edebug-break-condition
2529 (message "Break: %s => %s"
2530 edebug-break-condition
2531 edebug-break-result))
2532 ((not (eq edebug-execution-mode 'Continue-fast))
2533 (message "Break"))
2534 (t)))
2536 (t (message "")))
2538 (if (eq 'after arg-mode)
2539 (progn
2540 ;; Display result of previous evaluation.
2541 (if (and edebug-break
2542 edebug-sit-on-break
2543 (not (eq edebug-execution-mode 'Continue-fast)))
2544 (sit-for edebug-sit-for-seconds)) ; Show message.
2545 (edebug-previous-result)))
2547 (cond
2548 (edebug-break
2549 (cond
2550 ((eq edebug-execution-mode 'continue)
2551 (sit-for edebug-sit-for-seconds))
2552 ((eq edebug-execution-mode 'Continue-fast) (sit-for 0))
2553 (t (setq edebug-stop t))))
2554 ;; not edebug-break
2555 ((eq edebug-execution-mode 'trace)
2556 (sit-for edebug-sit-for-seconds)) ; Force update and pause.
2557 ((eq edebug-execution-mode 'Trace-fast)
2558 (sit-for 0))) ; Force update and continue.
2560 (when (input-pending-p)
2561 (setq edebug-stop t)
2562 (setq edebug-execution-mode 'step) ; for `edebug-overlay-arrow'
2563 (edebug-stop))
2565 (edebug-overlay-arrow)
2567 (unwind-protect
2568 (if (or edebug-stop
2569 (memq edebug-execution-mode '(step next))
2570 (eq arg-mode 'error))
2571 (edebug--recursive-edit arg-mode)) ; <--- Recursive edit
2573 ;; Reset the edebug-window-data to whatever it is now.
2574 (let ((window (if (eq (window-buffer) edebug-buffer)
2575 (selected-window)
2576 (get-buffer-window edebug-buffer))))
2577 ;; Remember window-start for edebug-buffer, if still displayed.
2578 (if window
2579 (progn
2580 (setcar edebug-window-data window)
2581 (setcdr edebug-window-data (window-start window)))))
2583 ;; Save trace window point before restoring outside windows.
2584 ;; Could generalize this for other buffers.
2585 (setq edebug-trace-window
2586 (get-buffer-window edebug-trace-buffer))
2587 (if edebug-trace-window
2588 (setq edebug-trace-window-start
2589 (and edebug-trace-window
2590 (window-start edebug-trace-window))))
2592 ;; Restore windows before continuing.
2593 (if edebug-save-windows
2594 (progn
2595 (edebug-set-windows edebug-outside-windows)
2597 ;; Restore displayed buffer points.
2598 ;; Needed even if restoring windows because
2599 ;; window-points are not restored. (should they be??)
2600 (if edebug-save-displayed-buffer-points
2601 (edebug-set-buffer-points edebug-buffer-points))
2603 ;; Unrestore trace window's window-point.
2604 (if edebug-trace-window
2605 (set-window-start edebug-trace-window
2606 edebug-trace-window-start))
2608 ;; Unrestore edebug-buffer's window-start, if displayed.
2609 (let ((window (car edebug-window-data)))
2610 (if (and (edebug-window-live-p window)
2611 (eq (window-buffer) edebug-buffer))
2612 (progn
2613 (set-window-start window (cdr edebug-window-data)
2614 'no-force)
2615 ;; Unrestore edebug-buffer's window-point.
2616 ;; Needed in addition to setting the buffer point
2617 ;; - otherwise quitting doesn't leave point as is.
2618 ;; But can this causes point to not be restored.
2619 ;; Also, it may not be a visible window.
2620 ;; (set-window-point window edebug-point)
2623 ;; Unrestore edebug-buffer's point. Rerestored below.
2624 ;; (goto-char edebug-point) ;; in edebug-buffer
2626 ;; Since we may be in a save-excursion, in case of quit,
2627 ;; reselect the outside window only.
2628 ;; Only needed if we are not recovering windows??
2629 (if (edebug-window-live-p edebug-outside-window)
2630 (select-window edebug-outside-window))
2631 ) ; if edebug-save-windows
2633 ;; Restore current buffer always, in case application needs it.
2634 (if (buffer-name edebug-outside-buffer)
2635 (set-buffer edebug-outside-buffer))
2636 ;; Restore point, and mark.
2637 ;; Needed even if restoring windows because
2638 ;; that doesn't restore point and mark in the current buffer.
2639 ;; But don't restore point if edebug-buffer is current buffer.
2640 (if (not (eq edebug-buffer edebug-outside-buffer))
2641 (goto-char edebug-outside-point))
2642 (if (marker-buffer (edebug-mark-marker))
2643 ;; Does zmacs-regions need to be nil while doing set-marker?
2644 (set-marker (edebug-mark-marker) edebug-outside-mark))
2645 )) ; unwind-protect
2646 ;; None of the following is done if quit or signal occurs.
2648 ;; Restore edebug-buffer's outside point.
2649 ;; (edebug-trace "restore edebug-buffer point: %s"
2650 ;; edebug-buffer-outside-point)
2651 (with-current-buffer edebug-buffer
2652 (goto-char edebug-buffer-outside-point))
2653 ;; ... nothing more.
2655 ;; Could be an option to keep eval display up.
2656 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
2657 (with-timeout-unsuspend edebug-with-timeout-suspend)
2658 ;; Reset global variables to outside values in case they were changed.
2659 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
2663 (defvar edebug-number-of-recursions 0)
2664 ;; Number of recursive edits started by edebug.
2665 ;; Should be 0 at the top level.
2667 (defvar edebug-recursion-depth 0)
2668 ;; Value of recursion-depth when edebug was called.
2670 ;; Dynamically declared unbound vars
2671 (defvar edebug-outside-match-data) ; match data outside of edebug
2672 (defvar edebug-backtrace-buffer) ; each recursive edit gets its own
2673 (defvar edebug-inside-windows)
2674 (defvar edebug-interactive-p)
2676 (defun edebug--recursive-edit (arg-mode)
2677 ;; Start up a recursive edit inside of edebug.
2678 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
2679 ;; Assume that none of the variables below are buffer-local.
2680 (let (;; match-data must be done in the outside buffer
2681 (edebug-outside-match-data
2682 (with-current-buffer edebug-outside-buffer ; in case match buffer different
2683 (match-data)))
2685 ;;(edebug-number-of-recursions (1+ edebug-number-of-recursions))
2686 (edebug-recursion-depth (recursion-depth))
2687 edebug-entered ; bind locally to nil
2688 (edebug-interactive-p nil) ; again non-interactive
2689 edebug-backtrace-buffer ; each recursive edit gets its own
2690 ;; The window configuration may be saved and restored
2691 ;; during a recursive-edit
2692 edebug-inside-windows
2695 (unwind-protect
2696 (let (
2697 ;; Declare global values local but using the same global value.
2698 ;; We could set these to the values for previous edebug call.
2699 (last-command last-command)
2700 (this-command this-command)
2701 (current-prefix-arg nil)
2703 ;; More for Emacs 19
2704 (last-input-event nil)
2705 (last-command-event nil)
2706 (last-event-frame nil)
2707 (last-nonmenu-event nil)
2708 (track-mouse nil)
2710 (standard-output t)
2711 (standard-input t)
2713 ;; Don't keep reading from an executing kbd macro
2714 ;; within edebug unless edebug-continue-kbd-macro is
2715 ;; non-nil. Again, local binding may not be best.
2716 (executing-kbd-macro
2717 (if edebug-continue-kbd-macro executing-kbd-macro))
2719 ;; Don't get confused by the user's keymap changes.
2720 (overriding-local-map nil)
2721 (overriding-terminal-local-map nil)
2723 ;; Bind again to outside values.
2724 (debug-on-error edebug-outside-debug-on-error)
2725 (debug-on-quit edebug-outside-debug-on-quit)
2727 ;; Don't keep defining a kbd macro.
2728 (defining-kbd-macro
2729 (if edebug-continue-kbd-macro defining-kbd-macro))
2731 ;; others??
2734 (if (and (eq edebug-execution-mode 'go)
2735 (not (memq arg-mode '(after error))))
2736 (message "Break"))
2738 (setq signal-hook-function nil)
2740 (edebug-mode 1)
2741 (unwind-protect
2742 (recursive-edit) ; <<<<<<<<<< Recursive edit
2744 ;; Do the following, even if quit occurs.
2745 (setq signal-hook-function 'edebug-signal)
2746 (if edebug-backtrace-buffer
2747 (kill-buffer edebug-backtrace-buffer))
2749 ;; Remember selected-window after recursive-edit.
2750 ;; (setq edebug-inside-window (selected-window))
2752 (set-match-data edebug-outside-match-data)
2754 ;; Recursive edit may have changed buffers,
2755 ;; so set it back before exiting let.
2756 (if (buffer-name edebug-buffer) ; if it still exists
2757 (progn
2758 (set-buffer edebug-buffer)
2759 (when (memq edebug-execution-mode '(go Go-nonstop))
2760 (edebug-overlay-arrow)
2761 (sit-for 0))
2762 (edebug-mode -1))
2763 ;; gotta have a buffer to let its buffer local variables be set
2764 (get-buffer-create " bogus edebug buffer"))
2765 ));; inner let
2769 ;;; Display related functions
2771 (defconst edebug-arrow-alist
2772 '((Continue-fast . "=")
2773 (Trace-fast . "-")
2774 (continue . ">")
2775 (trace . "->")
2776 (step . "=>")
2777 (next . "=>")
2778 (go . "<>")
2779 (Go-nonstop . "..")
2781 "Association list of arrows for each edebug mode.")
2783 (defun edebug-overlay-arrow ()
2784 ;; Set up the overlay arrow at beginning-of-line in current buffer.
2785 ;; The arrow string is derived from edebug-arrow-alist and
2786 ;; edebug-execution-mode.
2787 (let ((pos (line-beginning-position)))
2788 (setq overlay-arrow-string
2789 (cdr (assq edebug-execution-mode edebug-arrow-alist)))
2790 (setq overlay-arrow-position (make-marker))
2791 (set-marker overlay-arrow-position pos (current-buffer))))
2794 (defun edebug-toggle-save-all-windows ()
2795 "Toggle the saving and restoring of all windows.
2796 Also, each time you toggle it on, the inside and outside window
2797 configurations become the same as the current configuration."
2798 (interactive)
2799 (setq edebug-save-windows (not edebug-save-windows))
2800 (if edebug-save-windows
2801 (setq edebug-inside-windows
2802 (setq edebug-outside-windows
2803 (edebug-current-windows
2804 edebug-save-windows))))
2805 (message "Window saving is %s for all windows."
2806 (if edebug-save-windows "on" "off")))
2808 (defmacro edebug-changing-windows (&rest body)
2809 `(let ((window (selected-window)))
2810 (setq edebug-inside-windows (edebug-current-windows t))
2811 (edebug-set-windows edebug-outside-windows)
2812 ,@body;; Code to change edebug-save-windows
2813 (setq edebug-outside-windows (edebug-current-windows
2814 edebug-save-windows))
2815 ;; Problem: what about outside windows that are deleted inside?
2816 (edebug-set-windows edebug-inside-windows)))
2818 (defun edebug-toggle-save-selected-window ()
2819 "Toggle the saving and restoring of the selected window.
2820 Also, each time you toggle it on, the inside and outside window
2821 configurations become the same as the current configuration."
2822 (interactive)
2823 (cond
2824 ((eq t edebug-save-windows)
2825 ;; Save all outside windows except the selected one.
2826 ;; Remove (selected-window) from outside-windows.
2827 (edebug-changing-windows
2828 (setq edebug-save-windows (delq window (edebug-window-list)))))
2830 ((memq (selected-window) edebug-save-windows)
2831 (setq edebug-outside-windows
2832 (delq (assq (selected-window) edebug-outside-windows)
2833 edebug-outside-windows))
2834 (setq edebug-save-windows
2835 (delq (selected-window) edebug-save-windows)))
2836 (t ; Save a new window.
2837 (edebug-changing-windows
2838 (setq edebug-save-windows (cons window edebug-save-windows)))))
2840 (message "Window saving is %s for %s."
2841 (if (memq (selected-window) edebug-save-windows)
2842 "on" "off")
2843 (selected-window)))
2845 (defun edebug-toggle-save-windows (arg)
2846 "Toggle the saving and restoring of windows.
2847 With prefix, toggle for just the selected window.
2848 Otherwise, toggle for all windows."
2849 (interactive "P")
2850 (if arg
2851 (edebug-toggle-save-selected-window)
2852 (edebug-toggle-save-all-windows)))
2854 (defun edebug-where ()
2855 "Show the debug windows and where we stopped in the program."
2856 (interactive)
2857 (if (not edebug-active)
2858 (error "Edebug is not active"))
2859 ;; Restore the window configuration to what it last was inside.
2860 ;; But it is not always set. - experiment
2861 ;;(if edebug-inside-windows
2862 ;; (edebug-set-windows edebug-inside-windows))
2863 (edebug-pop-to-buffer edebug-buffer)
2864 (goto-char edebug-point))
2866 (defun edebug-view-outside ()
2867 "Change to the outside window configuration.
2868 Use `edebug-where' to return."
2869 (interactive)
2870 (if (not edebug-active)
2871 (error "Edebug is not active"))
2872 (setq edebug-inside-windows
2873 (edebug-current-windows edebug-save-windows))
2874 (edebug-set-windows edebug-outside-windows)
2875 (goto-char edebug-outside-point)
2876 (message "Window configuration outside of Edebug. Return with %s"
2877 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
2880 (defun edebug-bounce-point (arg)
2881 "Bounce the point in the outside current buffer.
2882 If prefix argument ARG is supplied, sit for that many seconds
2883 before returning. The default is one second."
2884 (interactive "p")
2885 (if (not edebug-active)
2886 (error "Edebug is not active"))
2887 (save-excursion
2888 ;; If the buffer's currently displayed, avoid set-window-configuration.
2889 (save-window-excursion
2890 (edebug-pop-to-buffer edebug-outside-buffer)
2891 (goto-char edebug-outside-point)
2892 (message "Current buffer: %s Point: %s Mark: %s"
2893 (current-buffer) (point)
2894 (if (marker-buffer (edebug-mark-marker))
2895 (marker-position (edebug-mark-marker)) "<not set>"))
2896 (sit-for arg)
2897 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data)))))
2900 ;; Joe Wells, here is a start at your idea of adding a buffer to the internal
2901 ;; display list. Still need to use this list in edebug--display.
2903 '(defvar edebug-display-buffer-list nil
2904 "List of buffers that edebug will display when it is active.")
2906 '(defun edebug-display-buffer (buffer)
2907 "Toggle display of a buffer inside of edebug."
2908 (interactive "bBuffer: ")
2909 (let ((already-displaying (memq buffer edebug-display-buffer-list)))
2910 (setq edebug-display-buffer-list
2911 (if already-displaying
2912 (delq buffer edebug-display-buffer-list)
2913 (cons buffer edebug-display-buffer-list)))
2914 (message "Displaying %s %s" buffer
2915 (if already-displaying "off" "on"))))
2917 ;;; Breakpoint related functions
2919 (defun edebug-find-stop-point ()
2920 ;; Return (function . index) of the nearest edebug stop point.
2921 (let* ((edebug-def-name (edebug-form-data-symbol))
2922 (edebug-data
2923 (let ((data (get edebug-def-name 'edebug)))
2924 (if (or (null data) (markerp data))
2925 (error "%s is not instrumented for Edebug" edebug-def-name))
2926 data)) ; we could do it automatically, if data is a marker.
2927 ;; pull out parts of edebug-data.
2928 (edebug-def-mark (car edebug-data))
2929 ;; (edebug-breakpoints (car (cdr edebug-data)))
2931 (offset-vector (nth 2 edebug-data))
2932 (offset (- (save-excursion
2933 (if (looking-at "[ \t]")
2934 ;; skip backwards until non-whitespace, or bol
2935 (skip-chars-backward " \t"))
2936 (point))
2937 edebug-def-mark))
2938 len i)
2939 ;; the offsets are in order so we can do a linear search
2940 (setq len (length offset-vector))
2941 (setq i 0)
2942 (while (and (< i len) (> offset (aref offset-vector i)))
2943 (setq i (1+ i)))
2944 (if (and (< i len)
2945 (<= offset (aref offset-vector i)))
2946 ;; return the relevant info
2947 (cons edebug-def-name i)
2948 (message "Point is not on an expression in %s."
2949 edebug-def-name)
2953 (defun edebug-next-breakpoint ()
2954 "Move point to the next breakpoint, or first if none past point."
2955 (interactive)
2956 (let ((edebug-stop-point (edebug-find-stop-point)))
2957 (if edebug-stop-point
2958 (let* ((edebug-def-name (car edebug-stop-point))
2959 (index (cdr edebug-stop-point))
2960 (edebug-data (get edebug-def-name 'edebug))
2962 ;; pull out parts of edebug-data
2963 (edebug-def-mark (car edebug-data))
2964 (edebug-breakpoints (car (cdr edebug-data)))
2965 (offset-vector (nth 2 edebug-data))
2966 breakpoint)
2967 (if (not edebug-breakpoints)
2968 (message "No breakpoints in this function.")
2969 (let ((breaks edebug-breakpoints))
2970 (while (and breaks
2971 (<= (car (car breaks)) index))
2972 (setq breaks (cdr breaks)))
2973 (setq breakpoint
2974 (if breaks
2975 (car breaks)
2976 ;; goto the first breakpoint
2977 (car edebug-breakpoints)))
2978 (goto-char (+ edebug-def-mark
2979 (aref offset-vector (car breakpoint))))
2981 (message "%s"
2982 (concat (if (nth 2 breakpoint)
2983 "Temporary " "")
2984 (if (car (cdr breakpoint))
2985 (format "Condition: %s"
2986 (edebug-safe-prin1-to-string
2987 (car (cdr breakpoint))))
2988 "")))
2989 ))))))
2992 (defun edebug-modify-breakpoint (flag &optional condition temporary)
2993 "Modify the breakpoint for the form at point or after it.
2994 Set it if FLAG is non-nil, clear it otherwise. Then move to that point.
2995 If CONDITION or TEMPORARY are non-nil, add those attributes to
2996 the breakpoint."
2997 (let ((edebug-stop-point (edebug-find-stop-point)))
2998 (if edebug-stop-point
2999 (let* ((edebug-def-name (car edebug-stop-point))
3000 (index (cdr edebug-stop-point))
3001 (edebug-data (get edebug-def-name 'edebug))
3003 ;; pull out parts of edebug-data
3004 (edebug-def-mark (car edebug-data))
3005 (edebug-breakpoints (car (cdr edebug-data)))
3006 (offset-vector (nth 2 edebug-data))
3007 present)
3008 ;; delete it either way
3009 (setq present (assq index edebug-breakpoints))
3010 (setq edebug-breakpoints (delq present edebug-breakpoints))
3011 (if flag
3012 (progn
3013 ;; add it to the list and resort
3014 (setq edebug-breakpoints
3015 (edebug-sort-alist
3016 (cons
3017 (list index condition temporary)
3018 edebug-breakpoints) '<))
3019 (if condition
3020 (message "Breakpoint set in %s with condition: %s"
3021 edebug-def-name condition)
3022 (message "Breakpoint set in %s" edebug-def-name)))
3023 (if present
3024 (message "Breakpoint unset in %s" edebug-def-name)
3025 (message "No breakpoint here")))
3027 (setcar (cdr edebug-data) edebug-breakpoints)
3028 (goto-char (+ edebug-def-mark (aref offset-vector index)))
3029 ))))
3031 (defun edebug-set-breakpoint (arg)
3032 "Set the breakpoint of nearest sexp.
3033 With prefix argument, make it a temporary breakpoint."
3034 (interactive "P")
3035 (edebug-modify-breakpoint t nil arg))
3037 (defun edebug-unset-breakpoint ()
3038 "Clear the breakpoint of nearest sexp."
3039 (interactive)
3040 (edebug-modify-breakpoint nil))
3043 (defun edebug-set-global-break-condition (expression)
3044 "Set `edebug-global-break-condition' to EXPRESSION."
3045 (interactive
3046 (list
3047 (let ((initial (and edebug-global-break-condition
3048 (format "%s" edebug-global-break-condition))))
3049 (read-from-minibuffer
3050 "Global Condition: " initial read-expression-map t
3051 (if (equal (car read-expression-history) initial)
3052 '(read-expression-history . 1)
3053 'read-expression-history)))))
3054 (setq edebug-global-break-condition expression))
3057 ;;; Mode switching functions
3059 (defun edebug-set-mode (mode shortmsg msg)
3060 ;; Set the edebug mode to MODE.
3061 ;; Display SHORTMSG, or MSG if not within edebug.
3062 (if (eq (1+ edebug-recursion-depth) (recursion-depth))
3063 (progn
3064 (setq edebug-execution-mode mode)
3065 (message "%s" shortmsg)
3066 ;; Continue execution
3067 (exit-recursive-edit))
3068 ;; This is not terribly useful!!
3069 (setq edebug-next-execution-mode mode)
3070 (message "%s" msg)))
3073 (defalias 'edebug-step-through-mode 'edebug-step-mode)
3075 (defun edebug-step-mode ()
3076 "Proceed to next stop point."
3077 (interactive)
3078 (edebug-set-mode 'step "" "Edebug will stop at next stop point."))
3080 (defun edebug-next-mode ()
3081 "Proceed to next `after' stop point."
3082 (interactive)
3083 (edebug-set-mode 'next "" "Edebug will stop after next eval."))
3085 (defun edebug-go-mode (arg)
3086 "Go, evaluating until break.
3087 With prefix ARG, set temporary break at current point and go."
3088 (interactive "P")
3089 (if arg
3090 (edebug-set-breakpoint t))
3091 (edebug-set-mode 'go "Go..." "Edebug will go until break."))
3093 (defun edebug-Go-nonstop-mode ()
3094 "Go, evaluating without debugging.
3095 You can use `edebug-stop', or any editing command, to stop."
3096 (interactive)
3097 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
3098 "Edebug will not stop at breaks."))
3101 (defun edebug-trace-mode ()
3102 "Begin trace mode.
3103 Pauses for `edebug-sit-for-seconds' at each stop point."
3104 (interactive)
3105 (edebug-set-mode 'trace "Tracing..." "Edebug will trace with pause."))
3107 (defun edebug-Trace-fast-mode ()
3108 "Trace with no wait at each step.
3109 Updates the display at each stop point, but does not pause."
3110 (interactive)
3111 (edebug-set-mode 'Trace-fast
3112 "Trace fast..." "Edebug will trace without pause."))
3114 (defun edebug-continue-mode ()
3115 "Begin continue mode.
3116 Pauses for `edebug-sit-for-seconds' at each break point."
3117 (interactive)
3118 (edebug-set-mode 'continue "Continue..."
3119 "Edebug will pause at breakpoints."))
3121 (defun edebug-Continue-fast-mode ()
3122 "Trace with no wait at each step.
3123 Updates the display at each break point, but does not pause."
3124 (interactive)
3125 (edebug-set-mode 'Continue-fast "Continue fast..."
3126 "Edebug will stop and go at breakpoints."))
3128 ;; ------------------------------------------------------------
3129 ;; The following use the mode changing commands and breakpoints.
3132 (defun edebug-goto-here ()
3133 "Proceed to first stop-point at or after current position of point."
3134 (interactive)
3135 (edebug-go-mode t))
3138 (defun edebug-stop ()
3139 "Stop execution and do not continue.
3140 Useful for exiting from trace or continue loop."
3141 (interactive)
3142 (message "Stop"))
3145 '(defun edebug-forward ()
3146 "Proceed to the exit of the next expression to be evaluated."
3147 (interactive)
3148 (edebug-set-mode
3149 'forward "Forward"
3150 "Edebug will stop after exiting the next expression."))
3153 (defun edebug-forward-sexp (arg)
3154 "Proceed from the current point to the end of the ARGth sexp ahead.
3155 If there are not ARG sexps ahead, then do `edebug-step-out'."
3156 (interactive "p")
3157 (condition-case nil
3158 (let ((parse-sexp-ignore-comments t))
3159 ;; Call forward-sexp repeatedly until done or failure.
3160 (forward-sexp arg)
3161 (edebug-go-mode t))
3162 (error
3163 (edebug-step-out)
3166 (defun edebug-step-out ()
3167 "Proceed from the current point to the end of the containing sexp.
3168 If there is no containing sexp that is not the top level defun,
3169 go to the end of the last sexp, or if that is the same point, then step."
3170 (interactive)
3171 (condition-case nil
3172 (let ((parse-sexp-ignore-comments t))
3173 (up-list 1)
3174 (save-excursion
3175 ;; Is there still a containing expression?
3176 (up-list 1))
3177 (edebug-go-mode t))
3178 (error
3179 ;; At top level - 1, so first check if there are more sexps at this level.
3180 (let ((start-point (point)))
3181 ;; (up-list 1)
3182 (down-list -1)
3183 (if (= (point) start-point)
3184 (edebug-step-mode) ; No more at this level, so step.
3185 (edebug-go-mode t)
3186 )))))
3188 (defun edebug-instrument-function (func)
3189 ;; Func should be a function symbol.
3190 ;; Return the function symbol, or nil if not instrumented.
3191 (let ((func-marker (get func 'edebug)))
3192 (cond
3193 ((and (markerp func-marker) (marker-buffer func-marker))
3194 ;; It is uninstrumented, so instrument it.
3195 (with-current-buffer (marker-buffer func-marker)
3196 (goto-char func-marker)
3197 (edebug-eval-top-level-form)
3198 func))
3199 ((consp func-marker)
3200 (message "%s is already instrumented." func)
3201 func)
3203 (let ((loc (find-function-noselect func t)))
3204 (unless (cdr loc)
3205 (error "Could not find the definition in its file"))
3206 (with-current-buffer (car loc)
3207 (goto-char (cdr loc))
3208 (edebug-eval-top-level-form)
3209 func))))))
3211 (defun edebug-instrument-callee ()
3212 "Instrument the definition of the function or macro about to be called.
3213 Do this when stopped before the form or it will be too late.
3214 One side effect of using this command is that the next time the
3215 function or macro is called, Edebug will be called there as well."
3216 (interactive)
3217 (if (not (looking-at "("))
3218 (error "You must be before a list form")
3219 (let ((func
3220 (save-excursion
3221 (down-list 1)
3222 (if (looking-at "(")
3223 (edebug--form-data-name
3224 (edebug-get-form-data-entry (point)))
3225 (read (current-buffer))))))
3226 (edebug-instrument-function func))))
3229 (defun edebug-step-in ()
3230 "Step into the definition of the function or macro about to be called.
3231 This first does `edebug-instrument-callee' to ensure that it is
3232 instrumented. Then it does `edebug-on-entry' and switches to `go' mode."
3233 (interactive)
3234 (let ((func (edebug-instrument-callee)))
3235 (if func
3236 (progn
3237 (edebug-on-entry func 'temp)
3238 (edebug-go-mode nil)))))
3240 (defun edebug-on-entry (function &optional flag)
3241 "Cause Edebug to stop when FUNCTION is called.
3242 With prefix argument, make this temporary so it is automatically
3243 canceled the first time the function is entered."
3244 (interactive "aEdebug on entry to: \nP")
3245 ;; Could store this in the edebug data instead.
3246 (put function 'edebug-on-entry (if flag 'temp t)))
3248 (defun cancel-edebug-on-entry (function)
3249 (interactive "aEdebug on entry to: ")
3250 (put function 'edebug-on-entry nil))
3253 '(advice-add 'debug-on-entry :around 'edebug--debug-on-entry) ;; Should we do this?
3254 ;; Also need edebug-cancel-debug-on-entry
3256 '(defun edebug--debug-on-entry (orig function)
3257 "If the function is instrumented for Edebug, call `edebug-on-entry'."
3258 (let ((func-data (get function 'edebug)))
3259 (if (or (null func-data) (markerp func-data))
3260 (funcall orig function)
3261 (edebug-on-entry function))))
3264 (defun edebug-top-level-nonstop ()
3265 "Set mode to Go-nonstop, and exit to top-level.
3266 This is useful for exiting even if `unwind-protect' code may be executed."
3267 (interactive)
3268 (setq edebug-execution-mode 'Go-nonstop)
3269 (top-level))
3271 ;;(defun edebug-exit-out ()
3272 ;; "Go until the current function exits."
3273 ;; (interactive)
3274 ;; (edebug-set-mode 'exiting "Exit..."))
3276 (defconst edebug-initial-mode-alist
3277 '((edebug-step-mode . step)
3278 (edebug-next-mode . next)
3279 (edebug-trace-mode . trace)
3280 (edebug-Trace-fast-mode . Trace-fast)
3281 (edebug-go-mode . go)
3282 (edebug-continue-mode . continue)
3283 (edebug-Continue-fast-mode . Continue-fast)
3284 (edebug-Go-nonstop-mode . Go-nonstop))
3285 "Association list between commands and the modes they set.")
3287 (defvar edebug-mode-map) ; will be defined fully later.
3289 (defun edebug-set-initial-mode ()
3290 "Set the initial execution mode of Edebug.
3291 The mode is requested via the key that would be used to set the mode in
3292 edebug-mode."
3293 (interactive)
3294 (let* ((old-mode edebug-initial-mode)
3295 (key (read-key-sequence
3296 (format
3297 "Change initial edebug mode from %s (%c) to (enter key): "
3298 old-mode
3299 (aref (where-is-internal
3300 (car (rassq old-mode edebug-initial-mode-alist))
3301 edebug-mode-map 'firstonly)
3302 0))))
3303 (mode (cdr (assq (lookup-key edebug-mode-map key)
3304 edebug-initial-mode-alist))))
3305 (if mode
3306 (progn
3307 (setq edebug-initial-mode mode)
3308 (message "Edebug's initial mode is now: %s" mode))
3309 (error "Key must map to one of the mode changing commands"))))
3311 ;;; Evaluation of expressions
3313 (defmacro edebug-outside-excursion (&rest body)
3314 "Evaluate an expression list in the outside context.
3315 Return the result of the last expression."
3316 ;; Only restores the non-variables context since all the variables let-bound
3317 ;; by Edebug will be properly reset to the appropriate context's value by
3318 ;; backtrace-eval.
3319 (declare (debug t))
3320 `(save-excursion ; of current-buffer
3321 (if edebug-save-windows
3322 (progn
3323 ;; After excursion, we will
3324 ;; restore to current window configuration.
3325 (setq edebug-inside-windows
3326 (edebug-current-windows edebug-save-windows))
3327 ;; Restore outside windows.
3328 (edebug-set-windows edebug-outside-windows)))
3330 (set-buffer edebug-buffer) ; why?
3331 (set-match-data edebug-outside-match-data)
3332 ;; Restore outside context.
3333 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
3334 (unwind-protect
3335 ;; FIXME: This restoring of edebug-outside-buffer and
3336 ;; edebug-outside-point is redundant now that backtrace-eval does it
3337 ;; for us.
3338 (with-current-buffer edebug-outside-buffer ; of edebug-buffer
3339 (goto-char edebug-outside-point)
3340 (if (marker-buffer (edebug-mark-marker))
3341 (set-marker (edebug-mark-marker) edebug-outside-mark))
3342 ,@body)
3344 ;; Back to edebug-buffer. Restore rest of inside context.
3345 ;; (use-local-map edebug-inside-map)
3346 (if edebug-save-windows
3347 ;; Restore inside windows.
3348 (edebug-set-windows edebug-inside-windows))
3350 ;; Save values that may have been changed.
3351 (setq edebug-outside-d-c-i-n-s-w
3352 (default-value 'cursor-in-non-selected-windows))
3354 ;; Restore the outside saved values; don't alter
3355 ;; the outside binding loci.
3356 (setq-default cursor-in-non-selected-windows t))))
3358 (defun edebug-eval (expr)
3359 (backtrace-eval expr 0 'edebug-after))
3361 (defun edebug-safe-eval (expr)
3362 ;; Evaluate EXPR safely.
3363 ;; If there is an error, a string is returned describing the error.
3364 (condition-case edebug-err
3365 (edebug-eval expr)
3366 (error (edebug-format "%s: %s" ;; could
3367 (get (car edebug-err) 'error-message)
3368 (car (cdr edebug-err))))))
3370 ;;; Printing
3373 (defun edebug-report-error (value)
3374 ;; Print an error message like command level does.
3375 ;; This also prints the error name if it has no error-message.
3376 (message "%s: %s"
3377 (or (get (car value) 'error-message)
3378 (format "peculiar error (%s)" (car value)))
3379 (mapconcat (lambda (edebug-arg)
3380 ;; continuing after an error may
3381 ;; complain about edebug-arg. why??
3382 (prin1-to-string edebug-arg))
3383 (cdr value) ", ")))
3385 (defvar print-readably) ; defined by lemacs
3386 ;; Alternatively, we could change the definition of
3387 ;; edebug-safe-prin1-to-string to only use these if defined.
3389 (defun edebug-safe-prin1-to-string (value)
3390 (let ((print-escape-newlines t)
3391 (print-length (or edebug-print-length print-length))
3392 (print-level (or edebug-print-level print-level))
3393 (print-circle (or edebug-print-circle print-circle))
3394 (print-readably nil)) ; lemacs uses this.
3395 (edebug-prin1-to-string value)))
3397 (defun edebug-compute-previous-result (previous-value)
3398 (if edebug-unwrap-results
3399 (setq previous-value
3400 (edebug-unwrap* previous-value)))
3401 (setq edebug-previous-result
3402 (concat "Result: "
3403 (edebug-safe-prin1-to-string previous-value)
3404 (eval-expression-print-format previous-value))))
3406 (defun edebug-previous-result ()
3407 "Print the previous result."
3408 (interactive)
3409 (message "%s" edebug-previous-result))
3411 ;;; Read, Eval and Print
3413 (defalias 'edebug-prin1-to-string #'cl-prin1-to-string)
3414 (defalias 'edebug-format #'format-message)
3415 (defalias 'edebug-message #'message)
3417 (defun edebug-eval-expression (expr)
3418 "Evaluate an expression in the outside environment.
3419 If interactive, prompt for the expression.
3420 Print result in minibuffer."
3421 (interactive (list (read-from-minibuffer
3422 "Eval: " nil read-expression-map t
3423 'read-expression-history)))
3424 (princ
3425 (edebug-outside-excursion
3426 (setq values (cons (edebug-eval expr) values))
3427 (concat (edebug-safe-prin1-to-string (car values))
3428 (eval-expression-print-format (car values))))))
3430 (defun edebug-eval-last-sexp ()
3431 "Evaluate sexp before point in the outside environment.
3432 Print value in minibuffer."
3433 (interactive)
3434 (edebug-eval-expression (edebug-last-sexp)))
3436 (defun edebug-eval-print-last-sexp ()
3437 "Evaluate sexp before point in outside environment; insert value.
3438 This prints the value into current buffer."
3439 (interactive)
3440 (let* ((form (edebug-last-sexp))
3441 (result-string
3442 (edebug-outside-excursion
3443 (edebug-safe-prin1-to-string (edebug-safe-eval form))))
3444 (standard-output (current-buffer)))
3445 (princ "\n")
3446 ;; princ the string to get rid of quotes.
3447 (princ result-string)
3448 (princ "\n")
3451 ;;; Edebug Minor Mode
3453 (defvar edebug-inhibit-emacs-lisp-mode-bindings nil
3454 "If non-nil, inhibit Edebug bindings on the C-x C-a key.
3455 By default, loading the `edebug' library causes these bindings to
3456 be installed in `emacs-lisp-mode-map'.")
3458 (define-obsolete-variable-alias 'gud-inhibit-global-bindings
3459 'edebug-inhibit-emacs-lisp-mode-bindings "24.3")
3461 ;; Global GUD bindings for all emacs-lisp-mode buffers.
3462 (unless edebug-inhibit-emacs-lisp-mode-bindings
3463 (define-key emacs-lisp-mode-map "\C-x\C-a\C-s" 'edebug-step-mode)
3464 (define-key emacs-lisp-mode-map "\C-x\C-a\C-n" 'edebug-next-mode)
3465 (define-key emacs-lisp-mode-map "\C-x\C-a\C-c" 'edebug-go-mode)
3466 (define-key emacs-lisp-mode-map "\C-x\C-a\C-l" 'edebug-where)
3467 ;; The following isn't a GUD binding.
3468 (define-key emacs-lisp-mode-map "\C-x\C-a\C-m" 'edebug-set-initial-mode))
3470 (defvar edebug-mode-map
3471 (let ((map (copy-keymap emacs-lisp-mode-map)))
3472 ;; control
3473 (define-key map " " 'edebug-step-mode)
3474 (define-key map "n" 'edebug-next-mode)
3475 (define-key map "g" 'edebug-go-mode)
3476 (define-key map "G" 'edebug-Go-nonstop-mode)
3477 (define-key map "t" 'edebug-trace-mode)
3478 (define-key map "T" 'edebug-Trace-fast-mode)
3479 (define-key map "c" 'edebug-continue-mode)
3480 (define-key map "C" 'edebug-Continue-fast-mode)
3482 ;;(define-key map "f" 'edebug-forward) not implemented
3483 (define-key map "f" 'edebug-forward-sexp)
3484 (define-key map "h" 'edebug-goto-here)
3486 (define-key map "I" 'edebug-instrument-callee)
3487 (define-key map "i" 'edebug-step-in)
3488 (define-key map "o" 'edebug-step-out)
3490 ;; quitting and stopping
3491 (define-key map "q" 'top-level)
3492 (define-key map "Q" 'edebug-top-level-nonstop)
3493 (define-key map "a" 'abort-recursive-edit)
3494 (define-key map "S" 'edebug-stop)
3496 ;; breakpoints
3497 (define-key map "b" 'edebug-set-breakpoint)
3498 (define-key map "u" 'edebug-unset-breakpoint)
3499 (define-key map "B" 'edebug-next-breakpoint)
3500 (define-key map "x" 'edebug-set-conditional-breakpoint)
3501 (define-key map "X" 'edebug-set-global-break-condition)
3503 ;; evaluation
3504 (define-key map "r" 'edebug-previous-result)
3505 (define-key map "e" 'edebug-eval-expression)
3506 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3507 (define-key map "E" 'edebug-visit-eval-list)
3509 ;; views
3510 (define-key map "w" 'edebug-where)
3511 (define-key map "v" 'edebug-view-outside) ;; maybe obsolete??
3512 (define-key map "p" 'edebug-bounce-point)
3513 (define-key map "P" 'edebug-view-outside) ;; same as v
3514 (define-key map "W" 'edebug-toggle-save-windows)
3516 ;; misc
3517 (define-key map "?" 'edebug-help)
3518 (define-key map "d" 'edebug-backtrace)
3520 (define-key map "-" 'negative-argument)
3522 ;; statistics
3523 (define-key map "=" 'edebug-temp-display-freq-count)
3525 ;; GUD bindings
3526 (define-key map "\C-c\C-s" 'edebug-step-mode)
3527 (define-key map "\C-c\C-n" 'edebug-next-mode)
3528 (define-key map "\C-c\C-c" 'edebug-go-mode)
3530 (define-key map "\C-x " 'edebug-set-breakpoint)
3531 (define-key map "\C-c\C-d" 'edebug-unset-breakpoint)
3532 (define-key map "\C-c\C-t"
3533 (lambda () (interactive) (edebug-set-breakpoint t)))
3534 (define-key map "\C-c\C-l" 'edebug-where)
3535 map))
3537 ;; Autoloading these global bindings doesn't make sense because
3538 ;; they cannot be used anyway unless Edebug is already loaded and active.
3540 (defvar global-edebug-prefix "\^XX"
3541 "Prefix key for global edebug commands, available from any buffer.")
3543 (defvar global-edebug-map
3544 (let ((map (make-sparse-keymap)))
3546 (define-key map " " 'edebug-step-mode)
3547 (define-key map "g" 'edebug-go-mode)
3548 (define-key map "G" 'edebug-Go-nonstop-mode)
3549 (define-key map "t" 'edebug-trace-mode)
3550 (define-key map "T" 'edebug-Trace-fast-mode)
3551 (define-key map "c" 'edebug-continue-mode)
3552 (define-key map "C" 'edebug-Continue-fast-mode)
3554 ;; breakpoints
3555 (define-key map "b" 'edebug-set-breakpoint)
3556 (define-key map "u" 'edebug-unset-breakpoint)
3557 (define-key map "x" 'edebug-set-conditional-breakpoint)
3558 (define-key map "X" 'edebug-set-global-break-condition)
3560 ;; views
3561 (define-key map "w" 'edebug-where)
3562 (define-key map "W" 'edebug-toggle-save-windows)
3564 ;; quitting
3565 (define-key map "q" 'top-level)
3566 (define-key map "Q" 'edebug-top-level-nonstop)
3567 (define-key map "a" 'abort-recursive-edit)
3569 ;; statistics
3570 (define-key map "=" 'edebug-display-freq-count)
3571 map)
3572 "Global map of edebug commands, available from any buffer.")
3574 (global-unset-key global-edebug-prefix)
3575 (global-set-key global-edebug-prefix global-edebug-map)
3578 (defun edebug-help ()
3579 "Describe `edebug-mode'."
3580 (interactive)
3581 (describe-function 'edebug-mode))
3583 (defvar edebug--mode-saved-vars nil)
3585 (define-minor-mode edebug-mode
3586 "Mode for Emacs Lisp buffers while in Edebug.
3588 In addition to all Emacs Lisp commands (except those that modify the
3589 buffer) there are local and global key bindings to several Edebug
3590 specific commands. E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
3591 in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3593 Also see bindings for the eval list buffer *edebug* in `edebug-eval-mode'.
3595 The edebug buffer commands:
3596 \\{edebug-mode-map}
3598 Global commands prefixed by `global-edebug-prefix':
3599 \\{global-edebug-map}
3601 Options:
3602 `edebug-setup-hook'
3603 `edebug-all-defs'
3604 `edebug-all-forms'
3605 `edebug-save-windows'
3606 `edebug-save-displayed-buffer-points'
3607 `edebug-initial-mode'
3608 `edebug-trace'
3609 `edebug-test-coverage'
3610 `edebug-continue-kbd-macro'
3611 `edebug-print-length'
3612 `edebug-print-level'
3613 `edebug-print-circle'
3614 `edebug-on-error'
3615 `edebug-on-quit'
3616 `edebug-on-signal'
3617 `edebug-unwrap-results'
3618 `edebug-global-break-condition'"
3619 :lighter " *Debugging*"
3620 :keymap edebug-mode-map
3621 ;; If the user kills the buffer in which edebug is currently active,
3622 ;; exit to top level, because the edebug command loop can't usefully
3623 ;; continue running in such a case.
3625 (if (not edebug-mode)
3626 (progn
3627 (while edebug--mode-saved-vars
3628 (let ((setting (pop edebug--mode-saved-vars)))
3629 (if (consp setting)
3630 (set (car setting) (cdr setting))
3631 (kill-local-variable setting))))
3632 (remove-hook 'kill-buffer-hook 'edebug-kill-buffer t))
3633 (pcase-dolist (`(,var . ,val) '((buffer-read-only . t)))
3634 (push
3635 (if (local-variable-p var) (cons var (symbol-value var)) var)
3636 edebug--mode-saved-vars)
3637 (set (make-local-variable var) val))
3638 ;; Append `edebug-kill-buffer' to the hook to avoid interfering with
3639 ;; other entries that are unguarded against deleted buffer.
3640 (add-hook 'kill-buffer-hook 'edebug-kill-buffer t t)))
3642 (defun edebug-kill-buffer ()
3643 "Used on `kill-buffer-hook' when Edebug is operating in a buffer of Lisp code."
3644 (run-with-timer 0 nil #'top-level))
3646 ;;; edebug eval list mode
3648 ;; A list of expressions and their evaluations is displayed in *edebug*.
3650 (defun edebug-eval-result-list ()
3651 "Return a list of evaluations of `edebug-eval-list'."
3652 ;; Assumes in outside environment.
3653 ;; Don't do any edebug things now.
3654 (let ((edebug-execution-mode 'Go-nonstop)
3655 (edebug-trace nil))
3656 (mapcar #'edebug-safe-eval edebug-eval-list)))
3658 (defun edebug-eval-display-list (eval-result-list)
3659 ;; Assumes edebug-eval-buffer exists.
3660 (let ((standard-output edebug-eval-buffer)
3661 (edebug-comment-line
3662 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
3663 (set-buffer edebug-eval-buffer)
3664 (erase-buffer)
3665 (dolist (exp edebug-eval-list)
3666 (prin1 exp) (terpri)
3667 (prin1 (pop eval-result-list)) (terpri)
3668 (princ edebug-comment-line))
3669 (edebug-pop-to-buffer edebug-eval-buffer)
3672 (defun edebug-create-eval-buffer ()
3673 (unless (and edebug-eval-buffer (buffer-name edebug-eval-buffer))
3674 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
3675 (edebug-eval-mode)))
3677 ;; Should generalize this to be callable outside of edebug
3678 ;; with calls in user functions, e.g. (edebug-eval-display)
3680 (defun edebug-eval-display (eval-result-list)
3681 "Display expressions and evaluations in EVAL-RESULT-LIST.
3682 It modifies the context by popping up the eval display."
3683 (when eval-result-list
3684 (edebug-create-eval-buffer)
3685 (edebug-eval-display-list eval-result-list)))
3687 (defun edebug-eval-redisplay ()
3688 "Redisplay eval list in outside environment.
3689 May only be called from within `edebug--recursive-edit'."
3690 (edebug-create-eval-buffer)
3691 (edebug-outside-excursion
3692 (edebug-eval-display-list (edebug-eval-result-list))
3695 (defun edebug-visit-eval-list ()
3696 "Switch to the evaluation list buffer \"*edebug*\"."
3697 (interactive)
3698 (edebug-eval-redisplay)
3699 (edebug-pop-to-buffer edebug-eval-buffer))
3702 (defun edebug-update-eval-list ()
3703 "Replace the evaluation list with the sexps now in the eval buffer."
3704 (interactive)
3705 (let ((starting-point (point))
3706 new-list)
3707 (goto-char (point-min))
3708 ;; get the first expression
3709 (edebug-skip-whitespace)
3710 (if (not (eobp))
3711 (progn
3712 (forward-sexp 1)
3713 (push (edebug-last-sexp) new-list)))
3715 (while (re-search-forward "^;" nil t)
3716 (forward-line 1)
3717 (skip-chars-forward " \t\n\r")
3718 (if (and (/= ?\; (following-char))
3719 (not (eobp)))
3720 (progn
3721 (forward-sexp 1)
3722 (push (edebug-last-sexp) new-list))))
3724 (setq edebug-eval-list (nreverse new-list))
3725 (edebug-eval-redisplay)
3726 (goto-char starting-point)))
3729 (defun edebug-delete-eval-item ()
3730 "Delete the item under point and redisplay."
3731 ;; could add arg to do repeatedly
3732 (interactive)
3733 (if (re-search-backward "^;" nil 'nofail)
3734 (forward-line 1))
3735 (delete-region
3736 (point) (progn (re-search-forward "^;" nil 'nofail)
3737 (beginning-of-line)
3738 (point)))
3739 (edebug-update-eval-list))
3743 (defvar edebug-eval-mode-map
3744 (let ((map (make-sparse-keymap)))
3745 (set-keymap-parent map lisp-interaction-mode-map)
3746 (define-key map "\C-c\C-w" 'edebug-where)
3747 (define-key map "\C-c\C-d" 'edebug-delete-eval-item)
3748 (define-key map "\C-c\C-u" 'edebug-update-eval-list)
3749 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3750 (define-key map "\C-j" 'edebug-eval-print-last-sexp)
3751 map)
3752 "Keymap for Edebug Eval mode. Superset of Lisp Interaction mode.")
3754 (put 'edebug-eval-mode 'mode-class 'special)
3756 (define-derived-mode edebug-eval-mode lisp-interaction-mode "Edebug Eval"
3757 "Mode for evaluation list buffer while in Edebug.
3759 In addition to all Interactive Emacs Lisp commands there are local and
3760 global key bindings to several Edebug specific commands. E.g.
3761 `edebug-step-mode' is bound to \\[edebug-step-mode] in the Edebug
3762 buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
3764 Eval list buffer commands:
3765 \\{edebug-eval-mode-map}
3767 Global commands prefixed by `global-edebug-prefix':
3768 \\{global-edebug-map}")
3770 ;;; Interface with standard debugger.
3772 ;; (setq debugger 'edebug) ; to use the edebug debugger
3773 ;; (setq debugger 'debug) ; use the standard debugger
3775 ;; Note that debug and its utilities must be byte-compiled to work,
3776 ;; since they depend on the backtrace looking a certain way. But
3777 ;; edebug is not dependent on this, yet.
3779 (defun edebug (&optional arg-mode &rest args)
3780 "Replacement for `debug'.
3781 If we are running an edebugged function, show where we last were.
3782 Otherwise call `debug' normally."
3783 ;;(message "entered: %s depth: %s edebug-recursion-depth: %s"
3784 ;; edebug-entered (recursion-depth) edebug-recursion-depth) (sit-for 1)
3785 (if (and edebug-entered ; anything active?
3786 (eq (recursion-depth) edebug-recursion-depth))
3787 (let (;; Where were we before the error occurred?
3788 (offset-index (car edebug-offset-indices))
3789 (value (car args))
3790 ;; Bind variables required by edebug--display.
3791 edebug-breakpoints
3792 edebug-break-data
3793 edebug-break-condition
3794 edebug-global-break
3795 (edebug-break (null arg-mode)) ;; If called explicitly.
3797 (edebug--display value offset-index arg-mode)
3798 (if (eq arg-mode 'error)
3800 value))
3802 ;; Otherwise call debug normally.
3803 ;; Still need to remove extraneous edebug calls from stack.
3804 (apply #'debug arg-mode args)
3808 (defun edebug-backtrace ()
3809 "Display a non-working backtrace. Better than nothing..."
3810 (interactive)
3811 (if (or (not edebug-backtrace-buffer)
3812 (null (buffer-name edebug-backtrace-buffer)))
3813 (setq edebug-backtrace-buffer
3814 (generate-new-buffer "*Backtrace*"))
3815 ;; Else, could just display edebug-backtrace-buffer.
3817 (with-output-to-temp-buffer (buffer-name edebug-backtrace-buffer)
3818 (setq edebug-backtrace-buffer standard-output)
3819 (let ((print-escape-newlines t)
3820 (print-length 50) ; FIXME cf edebug-safe-prin1-to-string
3821 last-ok-point)
3822 (backtrace)
3824 ;; Clean up the backtrace.
3825 ;; Not quite right for current edebug scheme.
3826 (set-buffer edebug-backtrace-buffer)
3827 (setq truncate-lines t)
3828 (goto-char (point-min))
3829 (setq last-ok-point (point))
3830 (if t (progn
3832 ;; Delete interspersed edebug internals.
3833 (while (re-search-forward "^ (?edebug" nil t)
3834 (beginning-of-line)
3835 (cond
3836 ((looking-at "^ (edebug-after")
3837 ;; Previous lines may contain code, so just delete this line.
3838 (setq last-ok-point (point))
3839 (forward-line 1)
3840 (delete-region last-ok-point (point)))
3842 ((looking-at (if debugger-stack-frame-as-list
3843 "^ (edebug"
3844 "^ edebug"))
3845 (forward-line 1)
3846 (delete-region last-ok-point (point))
3848 )))))
3851 ;;; Trace display
3853 (defun edebug-trace-display (buf-name fmt &rest args)
3854 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
3855 The buffer is created if it does not exist.
3856 You must include newlines in FMT to break lines, but one newline is appended."
3857 ;; e.g.
3858 ;; (edebug-trace-display "*trace-point*"
3859 ;; "saving: point = %s window-start = %s"
3860 ;; (point) (window-start))
3861 (let* ((oldbuf (current-buffer))
3862 (selected-window (selected-window))
3863 (buffer (get-buffer-create buf-name))
3864 buf-window)
3865 ;; (message "before pop-to-buffer") (sit-for 1)
3866 (edebug-pop-to-buffer buffer)
3867 (setq truncate-lines t)
3868 (setq buf-window (selected-window))
3869 (goto-char (point-max))
3870 (insert (apply #'edebug-format fmt args) "\n")
3871 ;; Make it visible.
3872 (vertical-motion (- 1 (window-height)))
3873 (set-window-start buf-window (point))
3874 (goto-char (point-max))
3875 ;; (set-window-point buf-window (point))
3876 ;; (sit-for 0)
3877 (bury-buffer buffer)
3878 (select-window selected-window)
3879 (set-buffer oldbuf))
3880 buf-name)
3883 (defun edebug-trace (fmt &rest args)
3884 "Convenience call to `edebug-trace-display' using `edebug-trace-buffer'."
3885 (apply #'edebug-trace-display edebug-trace-buffer fmt args))
3888 ;;; Frequency count and coverage
3890 ;; FIXME should this use overlays instead?
3891 ;; Definitely, IMO. The current business with undo in
3892 ;; edebug-temp-display-freq-count is horrid.
3893 (defun edebug-display-freq-count ()
3894 "Display the frequency count data for each line of the current definition.
3895 The frequency counts are inserted as comment lines after each line,
3896 and you can undo all insertions with one `undo' command.
3898 The counts are inserted starting under the `(' before an expression
3899 or the `)' after an expression, or on the last char of a symbol.
3900 The counts are only displayed when they differ from previous counts on
3901 the same line.
3903 If coverage is being tested, whenever all known results of an expression
3904 are `eq', the char `=' will be appended after the count
3905 for that expression. Note that this is always the case for an
3906 expression only evaluated once.
3908 To clear the frequency count and coverage data for a definition,
3909 reinstrument it."
3910 (interactive)
3911 (let* ((function (edebug-form-data-symbol))
3912 (counts (get function 'edebug-freq-count))
3913 (coverages (get function 'edebug-coverage))
3914 (data (get function 'edebug))
3915 (def-mark (car data)) ; mark at def start
3916 (edebug-points (nth 2 data))
3917 (i (1- (length edebug-points)))
3918 (last-index)
3919 (first-index)
3920 (start-of-line)
3921 (start-of-count-line)
3922 (last-count)
3924 (save-excursion
3925 ;; Traverse in reverse order so offsets are correct.
3926 (while (<= 0 i)
3927 ;; Start at last expression in line.
3928 (goto-char (+ def-mark (aref edebug-points i)))
3929 (beginning-of-line)
3930 (setq start-of-line (- (point) def-mark)
3931 last-index i)
3933 ;; Find all indexes on same line.
3934 (while (and (<= 0 (setq i (1- i)))
3935 (<= start-of-line (aref edebug-points i))))
3936 ;; Insert all the indices for this line.
3937 (forward-line 1)
3938 (setq start-of-count-line (point)
3939 first-index i ; Really, last index for line above this one.
3940 last-count -1) ; Cause first count to always appear.
3941 (insert ";#")
3942 ;; i == first-index still
3943 (while (<= (setq i (1+ i)) last-index)
3944 (let ((count (aref counts i))
3945 (coverage (aref coverages i))
3946 (col (save-excursion
3947 (goto-char (+ (aref edebug-points i) def-mark))
3948 (- (current-column)
3949 (if (= ?\( (following-char)) 0 1)))))
3950 (insert (make-string
3951 (max 0 (- col (- (point) start-of-count-line))) ?\s)
3952 (if (and (< 0 count)
3953 (not (memq coverage
3954 '(unknown ok-coverage))))
3955 "=" "")
3956 (if (= count last-count) "" (int-to-string count))
3957 " ")
3958 (setq last-count count)))
3959 (insert "\n")
3960 (setq i first-index)))))
3962 ;; FIXME this does not work very well. Eg if you press an arrow key,
3963 ;; or make a mouse-click, it fails with "Non-character input-event".
3964 (defun edebug-temp-display-freq-count ()
3965 "Temporarily display the frequency count data for the current definition.
3966 It is removed when you hit any char."
3967 ;; This seems not to work with Emacs 18.59. It undoes too far.
3968 (interactive)
3969 (let ((inhibit-read-only t))
3970 (undo-boundary)
3971 (edebug-display-freq-count)
3972 (setq unread-command-events
3973 (append unread-command-events (list (read-event))))
3974 ;; Yuck! This doesn't seem to work at all for me.
3975 (undo)))
3978 ;;; Menus
3980 (defun edebug-toggle (variable)
3981 (set variable (not (symbol-value variable)))
3982 (message "%s: %s" variable (symbol-value variable)))
3984 ;; We have to require easymenu (even for Emacs 18) just so
3985 ;; the easy-menu-define macro call is compiled correctly.
3986 (require 'easymenu)
3988 (defconst edebug-mode-menus
3989 '("Edebug"
3990 ["Stop" edebug-stop t]
3991 ["Step" edebug-step-mode t]
3992 ["Next" edebug-next-mode t]
3993 ["Trace" edebug-trace-mode t]
3994 ["Trace Fast" edebug-Trace-fast-mode t]
3995 ["Continue" edebug-continue-mode t]
3996 ["Continue Fast" edebug-Continue-fast-mode t]
3997 ["Go" edebug-go-mode t]
3998 ["Go Nonstop" edebug-Go-nonstop-mode t]
3999 "----"
4000 ["Help" edebug-help t]
4001 ["Abort" abort-recursive-edit t]
4002 ["Quit to Top Level" top-level t]
4003 ["Quit Nonstop" edebug-top-level-nonstop t]
4004 "----"
4005 ("Jumps"
4006 ["Forward Sexp" edebug-forward-sexp t]
4007 ["Step In" edebug-step-in t]
4008 ["Step Out" edebug-step-out t]
4009 ["Goto Here" edebug-goto-here t])
4011 ("Breaks"
4012 ["Set Breakpoint" edebug-set-breakpoint t]
4013 ["Unset Breakpoint" edebug-unset-breakpoint t]
4014 ["Set Conditional Breakpoint" edebug-set-conditional-breakpoint t]
4015 ["Set Global Break Condition" edebug-set-global-break-condition t]
4016 ["Show Next Breakpoint" edebug-next-breakpoint t])
4018 ("Views"
4019 ["Where am I?" edebug-where t]
4020 ["Bounce to Current Point" edebug-bounce-point t]
4021 ["View Outside Windows" edebug-view-outside t]
4022 ["Previous Result" edebug-previous-result t]
4023 ["Show Backtrace" edebug-backtrace t]
4024 ["Display Freq Count" edebug-display-freq-count t])
4026 ("Eval"
4027 ["Expression" edebug-eval-expression t]
4028 ["Last Sexp" edebug-eval-last-sexp t]
4029 ["Visit Eval List" edebug-visit-eval-list t])
4031 ("Options"
4032 ["Edebug All Defs" edebug-all-defs
4033 :style toggle :selected edebug-all-defs]
4034 ["Edebug All Forms" edebug-all-forms
4035 :style toggle :selected edebug-all-forms]
4036 "----"
4037 ["Tracing" (edebug-toggle 'edebug-trace)
4038 :style toggle :selected edebug-trace]
4039 ["Test Coverage" (edebug-toggle 'edebug-test-coverage)
4040 :style toggle :selected edebug-test-coverage]
4041 ["Save Windows" edebug-toggle-save-windows
4042 :style toggle :selected edebug-save-windows]
4043 ["Save Point"
4044 (edebug-toggle 'edebug-save-displayed-buffer-points)
4045 :style toggle :selected edebug-save-displayed-buffer-points]
4047 "Menus for Edebug.")
4050 ;;; Emacs version specific code
4052 (defalias 'edebug-window-live-p 'window-live-p)
4054 (defun edebug-mark ()
4055 (mark t))
4057 (defun edebug-set-conditional-breakpoint (arg condition)
4058 "Set a conditional breakpoint at nearest sexp.
4059 The condition is evaluated in the outside context.
4060 With prefix argument, make it a temporary breakpoint."
4061 ;; (interactive "P\nxCondition: ")
4062 (interactive
4063 (list
4064 current-prefix-arg
4065 ;; Read condition as follows; getting previous condition is cumbersome:
4066 (let ((edebug-stop-point (edebug-find-stop-point)))
4067 (if edebug-stop-point
4068 (let* ((edebug-def-name (car edebug-stop-point))
4069 (index (cdr edebug-stop-point))
4070 (edebug-data (get edebug-def-name 'edebug))
4071 (edebug-breakpoints (car (cdr edebug-data)))
4072 (edebug-break-data (assq index edebug-breakpoints))
4073 (edebug-break-condition (car (cdr edebug-break-data)))
4074 (initial (and edebug-break-condition
4075 (format "%s" edebug-break-condition))))
4076 (read-from-minibuffer
4077 "Condition: " initial read-expression-map t
4078 (if (equal (car read-expression-history) initial)
4079 '(read-expression-history . 1)
4080 'read-expression-history)))))))
4081 (edebug-modify-breakpoint t condition arg))
4083 (easy-menu-define edebug-menu edebug-mode-map "Edebug menus" edebug-mode-menus)
4085 ;;; Autoloading of Edebug accessories
4087 ;; edebug-cl-read and cl-read are available from liberte@cs.uiuc.edu
4088 (defun edebug--require-cl-read ()
4089 (require 'edebug-cl-read))
4091 (if (featurep 'cl-read)
4092 (add-hook 'edebug-setup-hook #'edebug--require-cl-read)
4093 ;; The following causes edebug-cl-read to be loaded when you load cl-read.el.
4094 (add-hook 'cl-read-load-hooks #'edebug--require-cl-read))
4097 ;;; Finalize Loading
4099 ;; When edebugging a function, some of the sub-expressions are
4100 ;; wrapped in (edebug-enter (lambda () ..)), so we need to teach
4101 ;; called-interactively-p that calls within the inner lambda should refer to
4102 ;; the outside function.
4103 (add-hook 'called-interactively-p-functions
4104 #'edebug--called-interactively-skip)
4105 (defun edebug--called-interactively-skip (i frame1 frame2)
4106 (when (and (eq (car-safe (nth 1 frame1)) 'lambda)
4107 (eq (nth 1 (nth 1 frame1)) '())
4108 (eq (nth 1 frame2) 'edebug-enter))
4109 ;; `edebug-enter' calls itself on its first invocation.
4110 (if (eq (nth 1 (backtrace-frame i 'called-interactively-p))
4111 'edebug-enter)
4112 2 1)))
4114 ;; Finally, hook edebug into the rest of Emacs.
4115 ;; There are probably some other things that could go here.
4117 ;; Install edebug read and eval functions.
4118 (edebug-install-read-eval-functions)
4120 (defun edebug-unload-function ()
4121 "Unload the Edebug source level debugger."
4122 (when edebug-active
4123 (setq edebug-active nil)
4124 (unwind-protect
4125 (abort-recursive-edit)
4126 ;; We still want to run unload-feature to completion
4127 (run-with-idle-timer 0 nil #'(lambda () (unload-feature 'edebug)))))
4128 (remove-hook 'called-interactively-p-functions
4129 'edebug--called-interactively-skip)
4130 (remove-hook 'cl-read-load-hooks 'edebug--require-cl-read)
4131 (edebug-uninstall-read-eval-functions)
4132 ;; Continue standard unloading.
4133 nil)
4135 (provide 'edebug)
4136 ;;; edebug.el ends here