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