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