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