use xmalloc_unsafe in current_minor_maps
[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
1fe3d507
DL
2041;; Anything else?
2042
f7359658 2043;;; The debugger itself
1fe3d507
DL
2044
2045(defvar edebug-active nil) ;; Non-nil when edebug is active
84fc2cfa 2046
1fe3d507
DL
2047(defvar edebug-stack nil)
2048;; Stack of active functions evaluated via edebug.
2049;; Should be nil at the top level.
2050
2051(defvar edebug-stack-depth -1)
2052;; Index of last edebug-stack item.
2053
2054(defvar edebug-offset-indices nil)
2055;; Stack of offset indices of visited edebug sexps.
2056;; Should be nil at the top level.
2057;; Each function adds one cons. Top is modified with setcar.
84fc2cfa 2058
84fc2cfa
ER
2059
2060(defvar edebug-entered nil
1fe3d507
DL
2061 ;; Non-nil if edebug has already been entered at this recursive edit level.
2062 ;; This should stay nil at the top level.
2063 )
2064
2065;; Should these be options?
2066(defconst edebug-debugger 'edebug
2067 ;; Name of function to use for debugging when error or quit occurs.
2068 ;; Set this to 'debug if you want to debug edebug.
2069 )
2070
2071
2072;; Dynamically bound variables, declared globally but left unbound.
2073(defvar edebug-function) ; the function being executed. change name!!
1fe3d507 2074(defvar edebug-data) ; the edebug data for the function
1fe3d507
DL
2075(defvar edebug-def-mark) ; the mark for the definition
2076(defvar edebug-freq-count) ; the count of expression visits.
2077(defvar edebug-coverage) ; the coverage results of each expression of function.
2078
2079(defvar edebug-buffer) ; which buffer the function is in.
1fe3d507
DL
2080
2081(defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2082(defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2083
2084(defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2085(defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2086
2087;;; Handling signals
1fe3d507 2088
bd8d6108 2089(defun edebug-signal (signal-name signal-data)
1fe3d507
DL
2090 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2091A signal name is a symbol with an `error-conditions' property
2092that is a list of condition names.
2093A handler for any of those names will get to handle this signal.
2094The symbol `error' should always be one of them.
2095
2096DATA should be a list. Its elements are printed as part of the error message.
2097If the signal is handled, DATA is made available to the handler.
2098See `condition-case'.
2099
2100This is the Edebug replacement for the standard `signal'. It should
2101only be active while Edebug is. It checks `debug-on-error' to see
2102whether it should call the debugger. When execution is resumed, the
bd8d6108
SM
2103error is signaled again."
2104 (if (and (listp debug-on-error) (memq signal-name debug-on-error))
2105 (edebug 'error (cons signal-name signal-data)))
1fe3d507
DL
2106 ;; If we reach here without another non-local exit, then send signal again.
2107 ;; i.e. the signal is not continuable, yet.
e76b547b
RS
2108 ;; Avoid infinite recursion.
2109 (let ((signal-hook-function nil))
bd8d6108 2110 (signal signal-name signal-data)))
1fe3d507
DL
2111
2112;;; Entering Edebug
84fc2cfa 2113
bd8d6108 2114(defun edebug-enter (function args body)
1fe3d507
DL
2115 ;; Entering FUNC. The arguments are ARGS, and the body is BODY.
2116 ;; Setup edebug variables and evaluate BODY. This function is called
a1506d29 2117 ;; when a function evaluated with edebug-eval-top-level-form is entered.
1fe3d507 2118 ;; Return the result of BODY.
84fc2cfa
ER
2119
2120 ;; Is this the first time we are entering edebug since
2121 ;; lower-level recursive-edit command?
1fe3d507 2122 ;; More precisely, this tests whether Edebug is currently active.
bd8d6108
SM
2123 (let ((edebug-function function))
2124 (if (not edebug-entered)
2125 (let ((edebug-entered t)
2126 ;; Binding max-lisp-eval-depth here is OK,
2127 ;; but not inside an unwind-protect.
2128 ;; Doing it here also keeps it from growing too large.
2129 (max-lisp-eval-depth (+ 100 max-lisp-eval-depth)) ; too much??
2130 (max-specpdl-size (+ 200 max-specpdl-size))
2131
2132 (debugger edebug-debugger) ; only while edebug is active.
2133 (edebug-outside-debug-on-error debug-on-error)
2134 (edebug-outside-debug-on-quit debug-on-quit)
2135 ;; Binding these may not be the right thing to do.
2136 ;; We want to allow the global values to be changed.
2137 (debug-on-error (or debug-on-error edebug-on-error))
53ff3e77 2138 (debug-on-quit edebug-on-quit))
bd8d6108
SM
2139 (unwind-protect
2140 (let ((signal-hook-function 'edebug-signal))
2141 (setq edebug-execution-mode (or edebug-next-execution-mode
2142 edebug-initial-mode
2143 edebug-execution-mode)
2144 edebug-next-execution-mode nil)
2145 (edebug-enter function args body))))
2146
2147 (let* ((edebug-data (get function 'edebug))
2148 (edebug-def-mark (car edebug-data)) ; mark at def start
2149 (edebug-freq-count (get function 'edebug-freq-count))
2150 (edebug-coverage (get function 'edebug-coverage))
2151 (edebug-buffer (marker-buffer edebug-def-mark))
2152
2153 (edebug-stack (cons function edebug-stack))
2154 (edebug-offset-indices (cons 0 edebug-offset-indices))
2155 )
2156 (if (get function 'edebug-on-entry)
2157 (progn
2158 (setq edebug-execution-mode 'step)
2159 (if (eq (get function 'edebug-on-entry) 'temp)
2160 (put function 'edebug-on-entry nil))))
2161 (if edebug-trace
2162 (edebug--enter-trace function args body)
2163 (funcall body))
2164 ))))
84fc2cfa 2165
5fc58d83
RS
2166(defun edebug-var-status (var)
2167 "Return a cons cell describing the status of VAR's current binding.
2168The purpose of this function is so you can properly undo
2169subsequent changes to the same binding, by passing the status
2170cons cell to `edebug-restore-status'. The status cons cell
2171has the form (LOCUS . VALUE), where LOCUS can be a buffer
2172\(for a buffer-local binding), a frame (for a frame-local binding),
2173or nil (if the default binding is current)."
2174 (cons (variable-binding-locus var)
2175 (symbol-value var)))
2176
2177(defun edebug-restore-status (var status)
2178 "Reset VAR based on STATUS.
242b1f68 2179STATUS should be a list returned by `edebug-var-status'."
5fc58d83
RS
2180 (let ((locus (car status))
2181 (value (cdr status)))
2182 (cond ((bufferp locus)
2183 (if (buffer-live-p locus)
2184 (with-current-buffer locus
2185 (set var value))))
2186 ((framep locus)
2187 (modify-frame-parameters locus (list (cons var value))))
2188 (t
2189 (set var value)))))
84fc2cfa 2190
bd8d6108 2191(defun edebug--enter-trace (function args body)
1fe3d507
DL
2192 (let ((edebug-stack-depth (1+ edebug-stack-depth))
2193 edebug-result)
a1506d29 2194 (edebug-print-trace-before
bd8d6108
SM
2195 (format "%s args: %s" function args))
2196 (prog1 (setq edebug-result (funcall body))
1fe3d507 2197 (edebug-print-trace-after
bd8d6108 2198 (format "%s result: %s" function edebug-result)))))
1fe3d507
DL
2199
2200(def-edebug-spec edebug-tracing (form body))
2201
2202(defmacro edebug-tracing (msg &rest body)
2203 "Print MSG in *edebug-trace* before and after evaluating BODY.
2204The result of BODY is also printed."
d8f1319a
GM
2205 `(let ((edebug-stack-depth (1+ edebug-stack-depth))
2206 edebug-result)
2207 (edebug-print-trace-before ,msg)
2208 (prog1 (setq edebug-result (progn ,@body))
a1506d29 2209 (edebug-print-trace-after
d8f1319a 2210 (format "%s result: %s" ,msg edebug-result)))))
1fe3d507
DL
2211
2212(defun edebug-print-trace-before (msg)
2213 "Function called to print trace info before expression evaluation.
2214MSG is printed after `::::{ '."
84fc2cfa 2215 (edebug-trace-display
1fe3d507 2216 edebug-trace-buffer "%s{ %s" (make-string edebug-stack-depth ?\:) msg))
84fc2cfa 2217
1fe3d507
DL
2218(defun edebug-print-trace-after (msg)
2219 "Function called to print trace info after expression evaluation.
2220MSG is printed after `::::} '."
84fc2cfa 2221 (edebug-trace-display
1fe3d507
DL
2222 edebug-trace-buffer "%s} %s" (make-string edebug-stack-depth ?\:) msg))
2223
2224
84fc2cfa 2225
bd8d6108 2226(defun edebug-slow-before (before-index)
3cc9e6d8
RS
2227 (unless edebug-active
2228 ;; Debug current function given BEFORE position.
2229 ;; Called from functions compiled with edebug-eval-top-level-form.
2230 ;; Return the before index.
bd8d6108 2231 (setcar edebug-offset-indices before-index)
3cc9e6d8
RS
2232
2233 ;; Increment frequency count
bd8d6108
SM
2234 (aset edebug-freq-count before-index
2235 (1+ (aref edebug-freq-count before-index)))
3cc9e6d8
RS
2236
2237 (if (or (not (memq edebug-execution-mode '(Go-nonstop next)))
2de39f08 2238 (input-pending-p))
bd8d6108
SM
2239 (edebug-debugger before-index 'before nil)))
2240 before-index)
1fe3d507 2241
bd8d6108 2242(defun edebug-fast-before (_before-index)
1fe3d507
DL
2243 ;; Do nothing.
2244 )
2245
bd8d6108 2246(defun edebug-slow-after (_before-index after-index value)
3cc9e6d8 2247 (if edebug-active
bd8d6108 2248 value
3cc9e6d8
RS
2249 ;; Debug current function given AFTER position and VALUE.
2250 ;; Called from functions compiled with edebug-eval-top-level-form.
2251 ;; Return VALUE.
bd8d6108 2252 (setcar edebug-offset-indices after-index)
3cc9e6d8
RS
2253
2254 ;; Increment frequency count
bd8d6108
SM
2255 (aset edebug-freq-count after-index
2256 (1+ (aref edebug-freq-count after-index)))
2257 (if edebug-test-coverage (edebug--update-coverage after-index value))
3cc9e6d8
RS
2258
2259 (if (and (eq edebug-execution-mode 'Go-nonstop)
2de39f08 2260 (not (input-pending-p)))
3cc9e6d8 2261 ;; Just return result.
bd8d6108
SM
2262 value
2263 (edebug-debugger after-index 'after value)
3cc9e6d8 2264 )))
1fe3d507 2265
bd8d6108 2266(defun edebug-fast-after (_before-index _after-index value)
1fe3d507 2267 ;; Do nothing but return the value.
bd8d6108 2268 value)
1fe3d507
DL
2269
2270(defun edebug-run-slow ()
2271 (defalias 'edebug-before 'edebug-slow-before)
2272 (defalias 'edebug-after 'edebug-slow-after))
2273
2274;; This is not used, yet.
2275(defun edebug-run-fast ()
2276 (defalias 'edebug-before 'edebug-fast-before)
2277 (defalias 'edebug-after 'edebug-fast-after))
2278
2279(edebug-run-slow)
2280
2281
bd8d6108
SM
2282(defun edebug--update-coverage (after-index value)
2283 (let ((old-result (aref edebug-coverage after-index)))
1fe3d507
DL
2284 (cond
2285 ((eq 'ok-coverage old-result))
2286 ((eq 'unknown old-result)
bd8d6108 2287 (aset edebug-coverage after-index value))
1fe3d507 2288 ;; Test if a different result.
bd8d6108
SM
2289 ((not (eq value old-result))
2290 (aset edebug-coverage after-index 'ok-coverage)))))
1fe3d507
DL
2291
2292
2293;; Dynamically declared unbound variables.
1fe3d507
DL
2294(defvar edebug-breakpoints)
2295(defvar edebug-break-data) ; break data for current function.
2296(defvar edebug-break) ; whether a break occurred.
2297(defvar edebug-global-break) ; whether a global break occurred.
2298(defvar edebug-break-condition) ; whether the breakpoint is conditional.
2299
2300(defvar edebug-break-result nil)
2301(defvar edebug-global-break-result nil)
2302
2303
bd8d6108 2304(defun edebug-debugger (offset-index arg-mode value)
3795fe52
RS
2305 (if inhibit-redisplay
2306 ;; Don't really try to enter edebug within an eval from redisplay.
bd8d6108 2307 value
3795fe52 2308 ;; Check breakpoints and pending input.
bd8d6108
SM
2309 ;; If edebug display should be updated, call edebug--display.
2310 ;; Return value.
3795fe52
RS
2311 (let* ( ;; This needs to be here since breakpoints may be changed.
2312 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
bd8d6108 2313 (edebug-break-data (assq offset-index edebug-breakpoints))
3795fe52
RS
2314 (edebug-break-condition (car (cdr edebug-break-data)))
2315 (edebug-global-break
2316 (if edebug-global-break-condition
2317 (condition-case nil
2318 (setq edebug-global-break-result
0ca3f70e 2319 (edebug-eval edebug-global-break-condition))
3795fe52
RS
2320 (error nil))))
2321 (edebug-break))
1fe3d507 2322
bd8d6108 2323 ;;(edebug-trace "exp: %s" value)
3795fe52 2324 ;; Test whether we should break.
a1506d29 2325 (setq edebug-break
3795fe52
RS
2326 (or edebug-global-break
2327 (and edebug-break-data
2328 (or (not edebug-break-condition)
2329 (setq edebug-break-result
0ca3f70e 2330 (edebug-eval edebug-break-condition))))))
3795fe52
RS
2331 (if (and edebug-break
2332 (nth 2 edebug-break-data)) ; is it temporary?
2333 ;; Delete the breakpoint.
2334 (setcdr edebug-data
2335 (cons (delq edebug-break-data edebug-breakpoints)
2336 (cdr (cdr edebug-data)))))
2337
2338 ;; Display if mode is not go, continue, or Continue-fast
a1506d29 2339 ;; or break, or input is pending,
3795fe52
RS
2340 (if (or (not (memq edebug-execution-mode '(go continue Continue-fast)))
2341 edebug-break
2de39f08 2342 (input-pending-p))
bd8d6108 2343 (edebug--display value offset-index arg-mode)) ; <---------- display
a1506d29 2344
bd8d6108 2345 value)))
84fc2cfa
ER
2346
2347
1fe3d507
DL
2348;; window-start now stored with each function.
2349;;(defvar edebug-window-start nil)
2350;; Remember where each buffers' window starts between edebug calls.
2351;; This is to avoid spurious recentering.
2352;; Does this still need to be buffer-local??
2353;;(setq-default edebug-window-start nil)
2354;;(make-variable-buffer-local 'edebug-window-start)
2355
2356
2357;; Dynamically declared unbound vars
2358(defvar edebug-point) ; the point in edebug buffer
2359(defvar edebug-outside-buffer) ; the current-buffer outside of edebug
2360(defvar edebug-outside-point) ; the point outside of edebug
2361(defvar edebug-outside-mark) ; the mark outside of edebug
2362(defvar edebug-window-data) ; window and window-start for current function
2363(defvar edebug-outside-windows) ; outside window configuration
2364(defvar edebug-eval-buffer) ; for the evaluation list.
eb533587 2365(defvar edebug-outside-d-c-i-n-s-w) ; outside default-cursor-in-non-selected-windows
1fe3d507
DL
2366
2367(defvar edebug-eval-list nil) ;; List of expressions to evaluate.
2368
2369(defvar edebug-previous-result nil) ;; Last result returned.
2370
88668147 2371;; Emacs 19 adds an arg to mark and mark-marker.
1fe3d507 2372(defalias 'edebug-mark-marker 'mark-marker)
84fc2cfa 2373
bd8d6108 2374(defun edebug--display (value offset-index arg-mode)
e409c527
SM
2375 (unless (marker-position edebug-def-mark)
2376 ;; The buffer holding the source has been killed.
2377 ;; Let's at least show a backtrace so the user can figure out
2378 ;; which function we're talking about.
2379 (debug))
1fe3d507
DL
2380 ;; Setup windows for edebug, determine mode, maybe enter recursive-edit.
2381 ;; Uses local variables of edebug-enter, edebug-before, edebug-after
2382 ;; and edebug-debugger.
bd8d6108 2383 (let ((edebug-active t) ; For minor mode alist.
3cc9e6d8 2384 (edebug-with-timeout-suspend (with-timeout-suspend))
bd8d6108 2385 edebug-stop ; Should we enter recursive-edit?
1fe3d507 2386 (edebug-point (+ edebug-def-mark
bd8d6108 2387 (aref (nth 2 edebug-data) offset-index)))
1fe3d507
DL
2388 edebug-buffer-outside-point ; current point in edebug-buffer
2389 ;; window displaying edebug-buffer
2390 (edebug-window-data (nth 3 edebug-data))
84fc2cfa
ER
2391 (edebug-outside-window (selected-window))
2392 (edebug-outside-buffer (current-buffer))
2393 (edebug-outside-point (point))
1fe3d507 2394 (edebug-outside-mark (edebug-mark))
bd8d6108 2395 edebug-outside-windows ; Window or screen configuration.
1fe3d507 2396 edebug-buffer-points
a1506d29 2397
bd8d6108
SM
2398 edebug-eval-buffer ; Declared here so we can kill it below.
2399 (eval-result-list (and edebug-eval-list
2400 (edebug-eval-result-list)))
1fe3d507
DL
2401 edebug-trace-window
2402 edebug-trace-window-start
88668147 2403
de70529f
SM
2404 (edebug-outside-d-c-i-n-s-w
2405 (default-value 'cursor-in-non-selected-windows)))
88668147 2406 (unwind-protect
fd7fde15 2407 (let ((cursor-in-echo-area nil)
2de39f08 2408 (unread-command-events nil)
88668147
DL
2409 ;; any others??
2410 )
de70529f 2411 (setq-default cursor-in-non-selected-windows t)
88668147 2412 (if (not (buffer-name edebug-buffer))
53ff3e77 2413 (user-error "Buffer defining %s not found" edebug-function))
a1506d29 2414
bd8d6108 2415 (if (eq 'after arg-mode)
88668147 2416 ;; Compute result string now before windows are modified.
bd8d6108 2417 (edebug-compute-previous-result value))
88668147
DL
2418
2419 (if edebug-save-windows
2420 ;; Save windows now before we modify them.
a1506d29 2421 (setq edebug-outside-windows
88668147 2422 (edebug-current-windows edebug-save-windows)))
a1506d29 2423
88668147
DL
2424 (if edebug-save-displayed-buffer-points
2425 (setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
2426
2427 ;; First move the edebug buffer point to edebug-point
f7359658
RS
2428 ;; so that window start doesn't get changed when we display it.
2429 ;; I don't know if this is going to help.
88668147
DL
2430 ;;(set-buffer edebug-buffer)
2431 ;;(goto-char edebug-point)
2432
2433 ;; If edebug-buffer is not currently displayed,
2434 ;; first find a window for it.
2435 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data))
2436 (setcar edebug-window-data (selected-window))
2437
2438 ;; Now display eval list, if any.
a1506d29 2439 ;; This is done after the pop to edebug-buffer
88668147 2440 ;; so that buffer-window correspondence is correct after quitting.
bd8d6108 2441 (edebug-eval-display eval-result-list)
88668147
DL
2442 ;; The evaluation list better not have deleted edebug-window-data.
2443 (select-window (car edebug-window-data))
2444 (set-buffer edebug-buffer)
2445
2446 (setq edebug-buffer-outside-point (point))
2447 (goto-char edebug-point)
a1506d29 2448
bd8d6108 2449 (if (eq 'before arg-mode)
88668147
DL
2450 ;; Check whether positions are up-to-date.
2451 ;; This assumes point is never before symbol.
2452 (if (not (memq (following-char) '(?\( ?\# ?\` )))
53ff3e77
SM
2453 (user-error "Source has changed - reevaluate definition of %s"
2454 edebug-function)
2455 ))
1fe3d507 2456
88668147
DL
2457 (setcdr edebug-window-data
2458 (edebug-adjust-window (cdr edebug-window-data)))
a1506d29 2459
88668147 2460 ;; Test if there is input, not including keyboard macros.
2de39f08 2461 (if (input-pending-p)
88668147
DL
2462 (progn
2463 (setq edebug-execution-mode 'step
2464 edebug-stop t)
2465 (edebug-stop)
2466 ;; (discard-input) ; is this unfriendly??
2467 ))
a1506d29 2468
fd7fde15
SM
2469 ;; Make sure we bind those in the right buffer (bug#16410).
2470 (let ((overlay-arrow-position overlay-arrow-position)
2471 (overlay-arrow-string overlay-arrow-string))
2472 ;; Now display arrow based on mode.
2473 (edebug-overlay-arrow)
a1506d29 2474
fd7fde15
SM
2475 (cond
2476 ((eq 'error arg-mode)
2477 ;; Display error message
2478 (setq edebug-execution-mode 'step)
2479 (edebug-overlay-arrow)
2480 (beep)
2481 (if (eq 'quit (car value))
2482 (message "Quit")
2483 (edebug-report-error value)))
2484 (edebug-break
2485 (cond
2486 (edebug-global-break
2487 (message "Global Break: %s => %s"
2488 edebug-global-break-condition
2489 edebug-global-break-result))
2490 (edebug-break-condition
2491 (message "Break: %s => %s"
2492 edebug-break-condition
2493 edebug-break-result))
2494 ((not (eq edebug-execution-mode 'Continue-fast))
2495 (message "Break"))
2496 (t)))
2497
2498 (t (message "")))
2499
2500 (if (eq 'after arg-mode)
2501 (progn
2502 ;; Display result of previous evaluation.
2503 (if (and edebug-break
2504 (not (eq edebug-execution-mode 'Continue-fast)))
2505 (sit-for edebug-sit-for-seconds)) ; Show message.
2506 (edebug-previous-result)))
2507
2508 (cond
2509 (edebug-break
2510 (cond
2511 ((eq edebug-execution-mode 'continue)
2512 (sit-for edebug-sit-for-seconds))
2513 ((eq edebug-execution-mode 'Continue-fast) (sit-for 0))
2514 (t (setq edebug-stop t))))
2515 ;; not edebug-break
2516 ((eq edebug-execution-mode 'trace)
2517 (sit-for edebug-sit-for-seconds)) ; Force update and pause.
2518 ((eq edebug-execution-mode 'Trace-fast)
2519 (sit-for 0))) ; Force update and continue.
2520
2521 (unwind-protect
2522 (if (or edebug-stop
2523 (memq edebug-execution-mode '(step next))
2524 (eq arg-mode 'error))
2525 (progn
2526 ;; (setq edebug-execution-mode 'step)
2527 ;; (edebug-overlay-arrow) ; This doesn't always show up.
2528 (edebug--recursive-edit arg-mode))) ; <--- Recursive edit
2529
2530 ;; Reset the edebug-window-data to whatever it is now.
2531 (let ((window (if (eq (window-buffer) edebug-buffer)
2532 (selected-window)
2533 (get-buffer-window edebug-buffer))))
2534 ;; Remember window-start for edebug-buffer, if still displayed.
2535 (if window
2536 (progn
2537 (setcar edebug-window-data window)
2538 (setcdr edebug-window-data (window-start window)))))
2539
2540 ;; Save trace window point before restoring outside windows.
2541 ;; Could generalize this for other buffers.
2542 (setq edebug-trace-window
2543 (get-buffer-window edebug-trace-buffer))
2544 (if edebug-trace-window
2545 (setq edebug-trace-window-start
2546 (and edebug-trace-window
2547 (window-start edebug-trace-window))))
2548
2549 ;; Restore windows before continuing.
2550 (if edebug-save-windows
2551 (progn
2552 (edebug-set-windows edebug-outside-windows)
2553
2554 ;; Restore displayed buffer points.
2555 ;; Needed even if restoring windows because
2556 ;; window-points are not restored. (should they be??)
2557 (if edebug-save-displayed-buffer-points
2558 (edebug-set-buffer-points edebug-buffer-points))
2559
2560 ;; Unrestore trace window's window-point.
2561 (if edebug-trace-window
2562 (set-window-start edebug-trace-window
2563 edebug-trace-window-start))
2564
2565 ;; Unrestore edebug-buffer's window-start, if displayed.
2566 (let ((window (car edebug-window-data)))
2567 (if (and (edebug-window-live-p window)
2568 (eq (window-buffer) edebug-buffer))
2569 (progn
2570 (set-window-start window (cdr edebug-window-data)
2571 'no-force)
2572 ;; Unrestore edebug-buffer's window-point.
2573 ;; Needed in addition to setting the buffer point
2574 ;; - otherwise quitting doesn't leave point as is.
2575 ;; But can this causes point to not be restored.
2576 ;; Also, it may not be a visible window.
2577 ;; (set-window-point window edebug-point)
2578 )))
2579
2580 ;; Unrestore edebug-buffer's point. Rerestored below.
2581 ;; (goto-char edebug-point) ;; in edebug-buffer
2582 )
2583 ;; Since we may be in a save-excursion, in case of quit,
2584 ;; reselect the outside window only.
2585 ;; Only needed if we are not recovering windows??
2586 (if (edebug-window-live-p edebug-outside-window)
2587 (select-window edebug-outside-window))
2588 ) ; if edebug-save-windows
2589
2590 ;; Restore current buffer always, in case application needs it.
2591 (if (buffer-name edebug-outside-buffer)
2592 (set-buffer edebug-outside-buffer))
2593 ;; Restore point, and mark.
2594 ;; Needed even if restoring windows because
2595 ;; that doesn't restore point and mark in the current buffer.
2596 ;; But don't restore point if edebug-buffer is current buffer.
2597 (if (not (eq edebug-buffer edebug-outside-buffer))
2598 (goto-char edebug-outside-point))
2599 (if (marker-buffer (edebug-mark-marker))
2600 ;; Does zmacs-regions need to be nil while doing set-marker?
2601 (set-marker (edebug-mark-marker) edebug-outside-mark))
2602 )) ; unwind-protect
88668147
DL
2603 ;; None of the following is done if quit or signal occurs.
2604
2605 ;; Restore edebug-buffer's outside point.
a1506d29 2606 ;; (edebug-trace "restore edebug-buffer point: %s"
88668147 2607 ;; edebug-buffer-outside-point)
de70529f
SM
2608 (with-current-buffer edebug-buffer
2609 (goto-char edebug-buffer-outside-point))
88668147
DL
2610 ;; ... nothing more.
2611 )
2de39f08
SM
2612 ;; Could be an option to keep eval display up.
2613 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
3cc9e6d8 2614 (with-timeout-unsuspend edebug-with-timeout-suspend)
88668147 2615 ;; Reset global variables to outside values in case they were changed.
de70529f 2616 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
88668147 2617 )))
1fe3d507 2618
84fc2cfa 2619
1fe3d507
DL
2620(defvar edebug-number-of-recursions 0)
2621;; Number of recursive edits started by edebug.
2622;; Should be 0 at the top level.
2623
2624(defvar edebug-recursion-depth 0)
2625;; Value of recursion-depth when edebug was called.
2626
2627;; Dynamically declared unbound vars
2628(defvar edebug-outside-match-data) ; match data outside of edebug
2629(defvar edebug-backtrace-buffer) ; each recursive edit gets its own
a1506d29 2630(defvar edebug-inside-windows)
1fe3d507
DL
2631(defvar edebug-interactive-p)
2632
bd8d6108 2633(defun edebug--recursive-edit (arg-mode)
1fe3d507 2634 ;; Start up a recursive edit inside of edebug.
84fc2cfa 2635 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
1fe3d507 2636 ;; Assume that none of the variables below are buffer-local.
17c781d1 2637 (let (;; match-data must be done in the outside buffer
84fc2cfa 2638 (edebug-outside-match-data
de70529f 2639 (with-current-buffer edebug-outside-buffer ; in case match buffer different
84fc2cfa
ER
2640 (match-data)))
2641
1fe3d507 2642 ;;(edebug-number-of-recursions (1+ edebug-number-of-recursions))
84fc2cfa
ER
2643 (edebug-recursion-depth (recursion-depth))
2644 edebug-entered ; bind locally to nil
1fe3d507 2645 (edebug-interactive-p nil) ; again non-interactive
84fc2cfa
ER
2646 edebug-backtrace-buffer ; each recursive edit gets its own
2647 ;; The window configuration may be saved and restored
2648 ;; during a recursive-edit
2649 edebug-inside-windows
84fc2cfa
ER
2650 )
2651
84fc2cfa 2652 (unwind-protect
88668147
DL
2653 (let (
2654 ;; Declare global values local but using the same global value.
2655 ;; We could set these to the values for previous edebug call.
a1506d29 2656 (last-command last-command)
88668147 2657 (this-command this-command)
c820ffeb 2658 (current-prefix-arg nil)
88668147
DL
2659
2660 ;; More for Emacs 19
2661 (last-input-event nil)
2662 (last-command-event nil)
88668147
DL
2663 (last-event-frame nil)
2664 (last-nonmenu-event nil)
2665 (track-mouse nil)
2666
2de39f08
SM
2667 (standard-output t)
2668 (standard-input t)
2669
60c49c0f
SM
2670 ;; Don't keep reading from an executing kbd macro
2671 ;; within edebug unless edebug-continue-kbd-macro is
2672 ;; non-nil. Again, local binding may not be best.
2673 (executing-kbd-macro
2674 (if edebug-continue-kbd-macro executing-kbd-macro))
2675
2676 ;; Don't get confused by the user's keymap changes.
2677 (overriding-local-map nil)
2678 (overriding-terminal-local-map nil)
2679
53ff3e77 2680 ;; Bind again to outside values.
88668147
DL
2681 (debug-on-error edebug-outside-debug-on-error)
2682 (debug-on-quit edebug-outside-debug-on-quit)
2683
2684 ;; Don't keep defining a kbd macro.
a1506d29 2685 (defining-kbd-macro
88668147
DL
2686 (if edebug-continue-kbd-macro defining-kbd-macro))
2687
60c49c0f
SM
2688 ;; Disable command hooks. This is essential when
2689 ;; a hook function is instrumented - to avoid infinite loop.
2690 ;; This may be more than we need, however.
2691 (pre-command-hook nil)
2692 (post-command-hook nil)
2693
88668147
DL
2694 ;; others??
2695 )
2696
88668147 2697 (if (and (eq edebug-execution-mode 'go)
bd8d6108 2698 (not (memq arg-mode '(after error))))
88668147
DL
2699 (message "Break"))
2700
1e3ab67b 2701 (setq signal-hook-function nil)
88668147 2702
17c781d1 2703 (edebug-mode 1)
88668147
DL
2704 (unwind-protect
2705 (recursive-edit) ; <<<<<<<<<< Recursive edit
2706
2707 ;; Do the following, even if quit occurs.
1e3ab67b 2708 (setq signal-hook-function 'edebug-signal)
88668147
DL
2709 (if edebug-backtrace-buffer
2710 (kill-buffer edebug-backtrace-buffer))
88668147
DL
2711
2712 ;; Remember selected-window after recursive-edit.
2713 ;; (setq edebug-inside-window (selected-window))
2714
ccb61a97 2715 (set-match-data edebug-outside-match-data)
88668147
DL
2716
2717 ;; Recursive edit may have changed buffers,
2718 ;; so set it back before exiting let.
2719 (if (buffer-name edebug-buffer) ; if it still exists
2720 (progn
2721 (set-buffer edebug-buffer)
2722 (if (memq edebug-execution-mode '(go Go-nonstop))
2723 (edebug-overlay-arrow))
17c781d1 2724 (edebug-mode -1))
88668147
DL
2725 ;; gotta have a buffer to let its buffer local variables be set
2726 (get-buffer-create " bogus edebug buffer"))
2727 ));; inner let
53ff3e77 2728 )))
84fc2cfa 2729
1fe3d507
DL
2730
2731;;; Display related functions
84fc2cfa
ER
2732
2733(defun edebug-adjust-window (old-start)
1fe3d507 2734 ;; If pos is not visible, adjust current window to fit following context.
2de39f08
SM
2735 ;; (message "window: %s old-start: %s window-start: %s pos: %s"
2736 ;; (selected-window) old-start (window-start) (point)) (sit-for 5)
84fc2cfa
ER
2737 (if (not (pos-visible-in-window-p))
2738 (progn
1fe3d507
DL
2739 ;; First try old-start
2740 (if old-start
2741 (set-window-start (selected-window) old-start))
84fc2cfa 2742 (if (not (pos-visible-in-window-p))
1fe3d507 2743 (progn
2de39f08 2744 ;; (message "resetting window start") (sit-for 2)
1fe3d507
DL
2745 (set-window-start
2746 (selected-window)
2747 (save-excursion
2748 (forward-line
2749 (if (< (point) (window-start)) -1 ; one line before if in back
2750 (- (/ (window-height) 2)) ; center the line moving forward
2751 ))
2752 (beginning-of-line)
2753 (point)))))))
84fc2cfa 2754 (window-start))
a1506d29 2755
84fc2cfa
ER
2756
2757
1fe3d507
DL
2758(defconst edebug-arrow-alist
2759 '((Continue-fast . "=")
2760 (Trace-fast . "-")
2761 (continue . ">")
2762 (trace . "->")
2763 (step . "=>")
2764 (next . "=>")
2765 (go . "<>")
2766 (Go-nonstop . "..") ; not used
2767 )
2768 "Association list of arrows for each edebug mode.")
2769
2770(defun edebug-overlay-arrow ()
2771 ;; Set up the overlay arrow at beginning-of-line in current buffer.
a1506d29 2772 ;; The arrow string is derived from edebug-arrow-alist and
1fe3d507 2773 ;; edebug-execution-mode.
5ed619e0 2774 (let ((pos (line-beginning-position)))
1fe3d507
DL
2775 (setq overlay-arrow-string
2776 (cdr (assq edebug-execution-mode edebug-arrow-alist)))
2777 (setq overlay-arrow-position (make-marker))
2778 (set-marker overlay-arrow-position pos (current-buffer))))
84fc2cfa 2779
1fe3d507
DL
2780
2781(defun edebug-toggle-save-all-windows ()
2782 "Toggle the saving and restoring of all windows.
2783Also, each time you toggle it on, the inside and outside window
2784configurations become the same as the current configuration."
84fc2cfa 2785 (interactive)
1fe3d507
DL
2786 (setq edebug-save-windows (not edebug-save-windows))
2787 (if edebug-save-windows
84fc2cfa
ER
2788 (setq edebug-inside-windows
2789 (setq edebug-outside-windows
1fe3d507
DL
2790 (edebug-current-windows
2791 edebug-save-windows))))
2792 (message "Window saving is %s for all windows."
84fc2cfa
ER
2793 (if edebug-save-windows "on" "off")))
2794
1fe3d507 2795(defmacro edebug-changing-windows (&rest body)
d8f1319a
GM
2796 `(let ((window (selected-window)))
2797 (setq edebug-inside-windows (edebug-current-windows t))
2798 (edebug-set-windows edebug-outside-windows)
2799 ,@body;; Code to change edebug-save-windows
a1506d29 2800 (setq edebug-outside-windows (edebug-current-windows
d8f1319a
GM
2801 edebug-save-windows))
2802 ;; Problem: what about outside windows that are deleted inside?
2803 (edebug-set-windows edebug-inside-windows)))
1fe3d507
DL
2804
2805(defun edebug-toggle-save-selected-window ()
a1506d29 2806 "Toggle the saving and restoring of the selected window.
1fe3d507
DL
2807Also, each time you toggle it on, the inside and outside window
2808configurations become the same as the current configuration."
2809 (interactive)
2810 (cond
2811 ((eq t edebug-save-windows)
2812 ;; Save all outside windows except the selected one.
2813 ;; Remove (selected-window) from outside-windows.
2814 (edebug-changing-windows
2815 (setq edebug-save-windows (delq window (edebug-window-list)))))
2816
2817 ((memq (selected-window) edebug-save-windows)
2818 (setq edebug-outside-windows
2819 (delq (assq (selected-window) edebug-outside-windows)
2820 edebug-outside-windows))
2821 (setq edebug-save-windows
2822 (delq (selected-window) edebug-save-windows)))
2823 (t ; Save a new window.
2824 (edebug-changing-windows
2825 (setq edebug-save-windows (cons window edebug-save-windows)))))
2826
2827 (message "Window saving is %s for %s."
2828 (if (memq (selected-window) edebug-save-windows)
2829 "on" "off")
2830 (selected-window)))
2831
2832(defun edebug-toggle-save-windows (arg)
2833 "Toggle the saving and restoring of windows.
2834With prefix, toggle for just the selected window.
2835Otherwise, toggle for all windows."
2836 (interactive "P")
2837 (if arg
2838 (edebug-toggle-save-selected-window)
2839 (edebug-toggle-save-all-windows)))
2840
84fc2cfa
ER
2841(defun edebug-where ()
2842 "Show the debug windows and where we stopped in the program."
2843 (interactive)
2844 (if (not edebug-active)
1fe3d507
DL
2845 (error "Edebug is not active"))
2846 ;; Restore the window configuration to what it last was inside.
2847 ;; But it is not always set. - experiment
2848 ;;(if edebug-inside-windows
2849 ;; (edebug-set-windows edebug-inside-windows))
84fc2cfa 2850 (edebug-pop-to-buffer edebug-buffer)
1fe3d507 2851 (goto-char edebug-point))
84fc2cfa
ER
2852
2853(defun edebug-view-outside ()
1e46f9e4
GM
2854 "Change to the outside window configuration.
2855Use `edebug-where' to return."
84fc2cfa
ER
2856 (interactive)
2857 (if (not edebug-active)
1fe3d507 2858 (error "Edebug is not active"))
a1506d29 2859 (setq edebug-inside-windows
1fe3d507
DL
2860 (edebug-current-windows edebug-save-windows))
2861 (edebug-set-windows edebug-outside-windows)
84fc2cfa 2862 (goto-char edebug-outside-point)
1fe3d507 2863 (message "Window configuration outside of Edebug. Return with %s"
84fc2cfa
ER
2864 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
2865
2866
1fe3d507
DL
2867(defun edebug-bounce-point (arg)
2868 "Bounce the point in the outside current buffer.
1e46f9e4
GM
2869If prefix argument ARG is supplied, sit for that many seconds
2870before returning. The default is one second."
1fe3d507 2871 (interactive "p")
84fc2cfa 2872 (if (not edebug-active)
1fe3d507 2873 (error "Edebug is not active"))
84fc2cfa 2874 (save-excursion
1fe3d507 2875 ;; If the buffer's currently displayed, avoid set-window-configuration.
84fc2cfa
ER
2876 (save-window-excursion
2877 (edebug-pop-to-buffer edebug-outside-buffer)
84fc2cfa 2878 (goto-char edebug-outside-point)
a1506d29
JB
2879 (message "Current buffer: %s Point: %s Mark: %s"
2880 (current-buffer) (point)
1fe3d507
DL
2881 (if (marker-buffer (edebug-mark-marker))
2882 (marker-position (edebug-mark-marker)) "<not set>"))
2de39f08 2883 (sit-for arg)
1fe3d507 2884 (edebug-pop-to-buffer edebug-buffer (car edebug-window-data)))))
84fc2cfa 2885
84fc2cfa 2886
a1506d29 2887;; Joe Wells, here is a start at your idea of adding a buffer to the internal
bd8d6108 2888;; display list. Still need to use this list in edebug--display.
84fc2cfa 2889
1fe3d507
DL
2890'(defvar edebug-display-buffer-list nil
2891 "List of buffers that edebug will display when it is active.")
84fc2cfa 2892
1fe3d507
DL
2893'(defun edebug-display-buffer (buffer)
2894 "Toggle display of a buffer inside of edebug."
2895 (interactive "bBuffer: ")
2896 (let ((already-displaying (memq buffer edebug-display-buffer-list)))
2897 (setq edebug-display-buffer-list
2898 (if already-displaying
2899 (delq buffer edebug-display-buffer-list)
2900 (cons buffer edebug-display-buffer-list)))
2901 (message "Displaying %s %s" buffer
2902 (if already-displaying "off" "on"))))
84fc2cfa 2903
1fe3d507 2904;;; Breakpoint related functions
84fc2cfa
ER
2905
2906(defun edebug-find-stop-point ()
1fe3d507
DL
2907 ;; Return (function . index) of the nearest edebug stop point.
2908 (let* ((edebug-def-name (edebug-form-data-symbol))
84fc2cfa 2909 (edebug-data
1fe3d507
DL
2910 (let ((data (get edebug-def-name 'edebug)))
2911 (if (or (null data) (markerp data))
2912 (error "%s is not instrumented for Edebug" edebug-def-name))
2913 data)) ; we could do it automatically, if data is a marker.
84fc2cfa 2914 ;; pull out parts of edebug-data.
1fe3d507
DL
2915 (edebug-def-mark (car edebug-data))
2916 ;; (edebug-breakpoints (car (cdr edebug-data)))
84fc2cfa 2917
1fe3d507 2918 (offset-vector (nth 2 edebug-data))
84fc2cfa
ER
2919 (offset (- (save-excursion
2920 (if (looking-at "[ \t]")
2921 ;; skip backwards until non-whitespace, or bol
2922 (skip-chars-backward " \t"))
2923 (point))
1fe3d507 2924 edebug-def-mark))
84fc2cfa
ER
2925 len i)
2926 ;; the offsets are in order so we can do a linear search
2927 (setq len (length offset-vector))
2928 (setq i 0)
2929 (while (and (< i len) (> offset (aref offset-vector i)))
2930 (setq i (1+ i)))
2931 (if (and (< i len)
2932 (<= offset (aref offset-vector i)))
2933 ;; return the relevant info
1fe3d507 2934 (cons edebug-def-name i)
84fc2cfa 2935 (message "Point is not on an expression in %s."
1fe3d507 2936 edebug-def-name)
84fc2cfa
ER
2937 )))
2938
2939
2940(defun edebug-next-breakpoint ()
2941 "Move point to the next breakpoint, or first if none past point."
2942 (interactive)
2943 (let ((edebug-stop-point (edebug-find-stop-point)))
2944 (if edebug-stop-point
1fe3d507 2945 (let* ((edebug-def-name (car edebug-stop-point))
84fc2cfa 2946 (index (cdr edebug-stop-point))
1fe3d507 2947 (edebug-data (get edebug-def-name 'edebug))
a1506d29 2948
84fc2cfa 2949 ;; pull out parts of edebug-data
1fe3d507 2950 (edebug-def-mark (car edebug-data))
84fc2cfa 2951 (edebug-breakpoints (car (cdr edebug-data)))
1fe3d507 2952 (offset-vector (nth 2 edebug-data))
84fc2cfa
ER
2953 breakpoint)
2954 (if (not edebug-breakpoints)
2955 (message "No breakpoints in this function.")
2956 (let ((breaks edebug-breakpoints))
2957 (while (and breaks
2958 (<= (car (car breaks)) index))
2959 (setq breaks (cdr breaks)))
2960 (setq breakpoint
2961 (if breaks
2962 (car breaks)
2963 ;; goto the first breakpoint
2964 (car edebug-breakpoints)))
1fe3d507 2965 (goto-char (+ edebug-def-mark
84fc2cfa 2966 (aref offset-vector (car breakpoint))))
a1506d29 2967
f7359658
RS
2968 (message "%s"
2969 (concat (if (nth 2 breakpoint)
84fc2cfa
ER
2970 "Temporary " "")
2971 (if (car (cdr breakpoint))
2972 (format "Condition: %s"
1fe3d507 2973 (edebug-safe-prin1-to-string
84fc2cfa
ER
2974 (car (cdr breakpoint))))
2975 "")))
2976 ))))))
2977
2978
2979(defun edebug-modify-breakpoint (flag &optional condition temporary)
b6386e63
LT
2980 "Modify the breakpoint for the form at point or after it.
2981Set it if FLAG is non-nil, clear it otherwise. Then move to that point.
84fc2cfa 2982If CONDITION or TEMPORARY are non-nil, add those attributes to
9854542e 2983the breakpoint."
84fc2cfa
ER
2984 (let ((edebug-stop-point (edebug-find-stop-point)))
2985 (if edebug-stop-point
1fe3d507 2986 (let* ((edebug-def-name (car edebug-stop-point))
84fc2cfa 2987 (index (cdr edebug-stop-point))
1fe3d507 2988 (edebug-data (get edebug-def-name 'edebug))
a1506d29 2989
84fc2cfa 2990 ;; pull out parts of edebug-data
1fe3d507 2991 (edebug-def-mark (car edebug-data))
84fc2cfa 2992 (edebug-breakpoints (car (cdr edebug-data)))
1fe3d507 2993 (offset-vector (nth 2 edebug-data))
84fc2cfa
ER
2994 present)
2995 ;; delete it either way
2996 (setq present (assq index edebug-breakpoints))
2997 (setq edebug-breakpoints (delq present edebug-breakpoints))
2998 (if flag
2999 (progn
3000 ;; add it to the list and resort
3001 (setq edebug-breakpoints
3002 (edebug-sort-alist
3003 (cons
3004 (list index condition temporary)
3005 edebug-breakpoints) '<))
1fe3d507
DL
3006 (if condition
3007 (message "Breakpoint set in %s with condition: %s"
3008 edebug-def-name condition)
3009 (message "Breakpoint set in %s" edebug-def-name)))
84fc2cfa 3010 (if present
1fe3d507
DL
3011 (message "Breakpoint unset in %s" edebug-def-name)
3012 (message "No breakpoint here")))
a1506d29 3013
1fe3d507
DL
3014 (setcar (cdr edebug-data) edebug-breakpoints)
3015 (goto-char (+ edebug-def-mark (aref offset-vector index)))
84fc2cfa
ER
3016 ))))
3017
3018(defun edebug-set-breakpoint (arg)
3019 "Set the breakpoint of nearest sexp.
3020With prefix argument, make it a temporary breakpoint."
3021 (interactive "P")
3022 (edebug-modify-breakpoint t nil arg))
3023
3024(defun edebug-unset-breakpoint ()
3025 "Clear the breakpoint of nearest sexp."
3026 (interactive)
3027 (edebug-modify-breakpoint nil))
3028
1fe3d507 3029
1fe3d507 3030(defun edebug-set-global-break-condition (expression)
1e46f9e4 3031 "Set `edebug-global-break-condition' to EXPRESSION."
20c78df0
JL
3032 (interactive
3033 (list
3034 (let ((initial (and edebug-global-break-condition
3035 (format "%s" edebug-global-break-condition))))
3036 (read-from-minibuffer
3037 "Global Condition: " initial read-expression-map t
3038 (if (equal (car read-expression-history) initial)
3039 '(read-expression-history . 1)
3040 'read-expression-history)))))
1fe3d507
DL
3041 (setq edebug-global-break-condition expression))
3042
3043
3044;;; Mode switching functions
84fc2cfa
ER
3045
3046(defun edebug-set-mode (mode shortmsg msg)
1fe3d507
DL
3047 ;; Set the edebug mode to MODE.
3048 ;; Display SHORTMSG, or MSG if not within edebug.
3049 (if (eq (1+ edebug-recursion-depth) (recursion-depth))
3050 (progn
3051 (setq edebug-execution-mode mode)
274f1353 3052 (message "%s" shortmsg)
1fe3d507
DL
3053 ;; Continue execution
3054 (exit-recursive-edit))
3055 ;; This is not terribly useful!!
3056 (setq edebug-next-execution-mode mode)
274f1353 3057 (message "%s" msg)))
84fc2cfa
ER
3058
3059
1fe3d507
DL
3060(defalias 'edebug-step-through-mode 'edebug-step-mode)
3061
3062(defun edebug-step-mode ()
3063 "Proceed to next stop point."
3064 (interactive)
3065 (edebug-set-mode 'step "" "Edebug will stop at next stop point."))
3066
3067(defun edebug-next-mode ()
3068 "Proceed to next `after' stop point."
84fc2cfa 3069 (interactive)
1fe3d507 3070 (edebug-set-mode 'next "" "Edebug will stop after next eval."))
84fc2cfa 3071
1fe3d507 3072(defun edebug-go-mode (arg)
84fc2cfa 3073 "Go, evaluating until break.
1fe3d507 3074With prefix ARG, set temporary break at current point and go."
84fc2cfa
ER
3075 (interactive "P")
3076 (if arg
3077 (edebug-set-breakpoint t))
1fe3d507 3078 (edebug-set-mode 'go "Go..." "Edebug will go until break."))
84fc2cfa 3079
1fe3d507 3080(defun edebug-Go-nonstop-mode ()
1e46f9e4
GM
3081 "Go, evaluating without debugging.
3082You can use `edebug-stop', or any editing command, to stop."
84fc2cfa
ER
3083 (interactive)
3084 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
1fe3d507
DL
3085 "Edebug will not stop at breaks."))
3086
3087
3088(defun edebug-trace-mode ()
1e46f9e4
GM
3089 "Begin trace mode.
3090Pauses for `edebug-sit-for-seconds' at each stop point."
1fe3d507
DL
3091 (interactive)
3092 (edebug-set-mode 'trace "Tracing..." "Edebug will trace with pause."))
3093
3094(defun edebug-Trace-fast-mode ()
1e46f9e4
GM
3095 "Trace with no wait at each step.
3096Updates the display at each stop point, but does not pause."
1fe3d507
DL
3097 (interactive)
3098 (edebug-set-mode 'Trace-fast
3099 "Trace fast..." "Edebug will trace without pause."))
3100
3101(defun edebug-continue-mode ()
1e46f9e4
GM
3102 "Begin continue mode.
3103Pauses for `edebug-sit-for-seconds' at each break point."
1fe3d507
DL
3104 (interactive)
3105 (edebug-set-mode 'continue "Continue..."
3106 "Edebug will pause at breakpoints."))
3107
3108(defun edebug-Continue-fast-mode ()
1e46f9e4
GM
3109 "Trace with no wait at each step.
3110Updates the display at each break point, but does not pause."
1fe3d507
DL
3111 (interactive)
3112 (edebug-set-mode 'Continue-fast "Continue fast..."
3113 "Edebug will stop and go at breakpoints."))
3114
3115;; ------------------------------------------------------------
3116;; The following use the mode changing commands and breakpoints.
3117
3118
3119(defun edebug-goto-here ()
8a33e9f1 3120 "Proceed to first stop-point at or after current position of point."
1fe3d507
DL
3121 (interactive)
3122 (edebug-go-mode t))
3123
3124
3125(defun edebug-stop ()
3126 "Stop execution and do not continue.
3127Useful for exiting from trace or continue loop."
3128 (interactive)
3129 (message "Stop"))
3130
3131
3132'(defun edebug-forward ()
3133 "Proceed to the exit of the next expression to be evaluated."
3134 (interactive)
a1506d29 3135 (edebug-set-mode
1fe3d507
DL
3136 'forward "Forward"
3137 "Edebug will stop after exiting the next expression."))
3138
84fc2cfa
ER
3139
3140(defun edebug-forward-sexp (arg)
3141 "Proceed from the current point to the end of the ARGth sexp ahead.
1e46f9e4 3142If there are not ARG sexps ahead, then do `edebug-step-out'."
84fc2cfa 3143 (interactive "p")
1fe3d507 3144 (condition-case nil
84fc2cfa
ER
3145 (let ((parse-sexp-ignore-comments t))
3146 ;; Call forward-sexp repeatedly until done or failure.
3147 (forward-sexp arg)
1fe3d507 3148 (edebug-go-mode t))
84fc2cfa
ER
3149 (error
3150 (edebug-step-out)
3151 )))
3152
3153(defun edebug-step-out ()
3154 "Proceed from the current point to the end of the containing sexp.
3155If there is no containing sexp that is not the top level defun,
3156go to the end of the last sexp, or if that is the same point, then step."
3157 (interactive)
1fe3d507 3158 (condition-case nil
84fc2cfa
ER
3159 (let ((parse-sexp-ignore-comments t))
3160 (up-list 1)
3161 (save-excursion
3162 ;; Is there still a containing expression?
3163 (up-list 1))
1fe3d507 3164 (edebug-go-mode t))
84fc2cfa
ER
3165 (error
3166 ;; At top level - 1, so first check if there are more sexps at this level.
3167 (let ((start-point (point)))
3168;; (up-list 1)
3169 (down-list -1)
3170 (if (= (point) start-point)
1fe3d507
DL
3171 (edebug-step-mode) ; No more at this level, so step.
3172 (edebug-go-mode t)
84fc2cfa
ER
3173 )))))
3174
1fe3d507
DL
3175(defun edebug-instrument-function (func)
3176 ;; Func should be a function symbol.
3177 ;; Return the function symbol, or nil if not instrumented.
3ebb8416 3178 (let ((func-marker (get func 'edebug)))
1fe3d507 3179 (cond
49ffc078 3180 ((and (markerp func-marker) (marker-buffer func-marker))
1fe3d507 3181 ;; It is uninstrumented, so instrument it.
3ebb8416 3182 (with-current-buffer (marker-buffer func-marker)
1fe3d507
DL
3183 (goto-char func-marker)
3184 (edebug-eval-top-level-form)
3185 func))
3186 ((consp func-marker)
3187 (message "%s is already instrumented." func)
3188 func)
3ebb8416 3189 (t
23a8a5ab 3190 (let ((loc (find-function-noselect func t)))
fea7b514
RS
3191 (unless (cdr loc)
3192 (error "Could not find the definition in its file"))
3ebb8416
SM
3193 (with-current-buffer (car loc)
3194 (goto-char (cdr loc))
3195 (edebug-eval-top-level-form)
3196 func))))))
1fe3d507
DL
3197
3198(defun edebug-instrument-callee ()
a1506d29 3199 "Instrument the definition of the function or macro about to be called.
1fe3d507
DL
3200Do this when stopped before the form or it will be too late.
3201One side effect of using this command is that the next time the
3202function or macro is called, Edebug will be called there as well."
84fc2cfa 3203 (interactive)
1fe3d507
DL
3204 (if (not (looking-at "\("))
3205 (error "You must be before a list form")
3206 (let ((func
3207 (save-excursion
3208 (down-list 1)
3209 (if (looking-at "\(")
2de39f08 3210 (edebug--form-data-name
1fe3d507 3211 (edebug-get-form-data-entry (point)))
88668147 3212 (edebug-original-read (current-buffer))))))
1fe3d507 3213 (edebug-instrument-function func))))
84fc2cfa 3214
84fc2cfa 3215
1fe3d507 3216(defun edebug-step-in ()
a1506d29
JB
3217 "Step into the definition of the function or macro about to be called.
3218This first does `edebug-instrument-callee' to ensure that it is
1fe3d507 3219instrumented. Then it does `edebug-on-entry' and switches to `go' mode."
84fc2cfa 3220 (interactive)
1fe3d507
DL
3221 (let ((func (edebug-instrument-callee)))
3222 (if func
3223 (progn
3224 (edebug-on-entry func 'temp)
3225 (edebug-go-mode nil)))))
84fc2cfa 3226
1fe3d507
DL
3227(defun edebug-on-entry (function &optional flag)
3228 "Cause Edebug to stop when FUNCTION is called.
3229With prefix argument, make this temporary so it is automatically
c80e3b4a 3230canceled the first time the function is entered."
1fe3d507
DL
3231 (interactive "aEdebug on entry to: \nP")
3232 ;; Could store this in the edebug data instead.
3233 (put function 'edebug-on-entry (if flag 'temp t)))
84fc2cfa 3234
1fe3d507
DL
3235(defun cancel-edebug-on-entry (function)
3236 (interactive "aEdebug on entry to: ")
3237 (put function 'edebug-on-entry nil))
84fc2cfa 3238
a1506d29 3239
88668147
DL
3240(if (not (fboundp 'edebug-original-debug-on-entry))
3241 (fset 'edebug-original-debug-on-entry (symbol-function 'debug-on-entry)))
1fe3d507
DL
3242'(fset 'debug-on-entry 'edebug-debug-on-entry) ;; Should we do this?
3243;; Also need edebug-cancel-debug-on-entry
3244
3245'(defun edebug-debug-on-entry (function)
3246 "Request FUNCTION to invoke debugger each time it is called.
3247If the user continues, FUNCTION's execution proceeds.
3248Works by modifying the definition of FUNCTION,
3249which must be written in Lisp, not predefined.
3250Use `cancel-debug-on-entry' to cancel the effect of this command.
3251Redefining FUNCTION also does that.
3252
3253This version is from Edebug. If the function is instrumented for
6db746da 3254Edebug, it calls `edebug-on-entry'."
1fe3d507
DL
3255 (interactive "aDebug on entry (to function): ")
3256 (let ((func-data (get function 'edebug)))
3257 (if (or (null func-data) (markerp func-data))
88668147 3258 (edebug-original-debug-on-entry function)
1fe3d507
DL
3259 (edebug-on-entry function))))
3260
3261
3262(defun edebug-top-level-nonstop ()
3263 "Set mode to Go-nonstop, and exit to top-level.
242b1f68 3264This is useful for exiting even if `unwind-protect' code may be executed."
84fc2cfa 3265 (interactive)
1fe3d507
DL
3266 (setq edebug-execution-mode 'Go-nonstop)
3267 (top-level))
84fc2cfa
ER
3268
3269
3270;;(defun edebug-exit-out ()
3271;; "Go until the current function exits."
3272;; (interactive)
3273;; (edebug-set-mode 'exiting "Exit..."))
3274
3275
84fc2cfa
ER
3276;;; The following initial mode setting definitions are not used yet.
3277
1fe3d507 3278'(defconst edebug-initial-mode-alist
84fc2cfa
ER
3279 '((edebug-Continue-fast . Continue-fast)
3280 (edebug-Trace-fast . Trace-fast)
3281 (edebug-continue . continue)
3282 (edebug-trace . trace)
3283 (edebug-go . go)
3284 (edebug-step-through . step)
3285 (edebug-Go-nonstop . Go-nonstop)
3286 )
3287 "Association list between commands and the modes they set.")
3288
3289
1fe3d507 3290'(defun edebug-set-initial-mode ()
84fc2cfa
ER
3291 "Ask for the initial mode of the enclosing function.
3292The mode is requested via the key that would be used to set the mode in
3293edebug-mode."
3294 (interactive)
3295 (let* ((this-function (edebug-which-function))
3296 (keymap (if (eq edebug-mode-map (current-local-map))
3297 edebug-mode-map))
3298 (old-mode (or (get this-function 'edebug-initial-mode)
3299 edebug-initial-mode))
3300 (key (read-key-sequence
3301 (format
3302 "Change initial edebug mode for %s from %s (%s) to (enter key): "
3303 this-function
3304 old-mode
3305 (where-is-internal
3306 (car (rassq old-mode edebug-initial-mode-alist))
3307 keymap 'firstonly
3308 ))))
3309 (mode (cdr (assq (key-binding key) edebug-initial-mode-alist)))
3310 )
3311 (if (and mode
3312 (or (get this-function 'edebug-initial-mode)
3313 (not (eq mode edebug-initial-mode))))
3314 (progn
3315 (put this-function 'edebug-initial-mode mode)
3316 (message "Initial mode for %s is now: %s"
3317 this-function mode))
4897b0a0 3318 (error "Key must map to one of the mode changing commands")
84fc2cfa
ER
3319 )))
3320
1fe3d507 3321;;; Evaluation of expressions
84fc2cfa 3322
1fe3d507
DL
3323(defmacro edebug-outside-excursion (&rest body)
3324 "Evaluate an expression list in the outside context.
3325Return the result of the last expression."
53ff3e77
SM
3326 ;; Only restores the non-variables context since all the variables let-bound
3327 ;; by Edebug will be properly reset to the appropriate context's value by
3328 ;; backtrace-eval.
bd8d6108 3329 (declare (debug t))
d8f1319a
GM
3330 `(save-excursion ; of current-buffer
3331 (if edebug-save-windows
3332 (progn
a1506d29 3333 ;; After excursion, we will
d8f1319a
GM
3334 ;; restore to current window configuration.
3335 (setq edebug-inside-windows
3336 (edebug-current-windows edebug-save-windows))
3337 ;; Restore outside windows.
3338 (edebug-set-windows edebug-outside-windows)))
3339
3340 (set-buffer edebug-buffer) ; why?
d8f1319a
GM
3341 (set-match-data edebug-outside-match-data)
3342 ;; Restore outside context.
53ff3e77
SM
3343 (setq-default cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
3344 (unwind-protect
3345 (with-current-buffer edebug-outside-buffer ; of edebug-buffer
3346 (goto-char edebug-outside-point)
3347 (if (marker-buffer (edebug-mark-marker))
3348 (set-marker (edebug-mark-marker) edebug-outside-mark))
3349 ,@body)
3350
3351 ;; Back to edebug-buffer. Restore rest of inside context.
3352 ;; (use-local-map edebug-inside-map)
3353 (if edebug-save-windows
3354 ;; Restore inside windows.
3355 (edebug-set-windows edebug-inside-windows))
3356
3357 ;; Save values that may have been changed.
3358 (setq edebug-outside-d-c-i-n-s-w
3359 (default-value 'cursor-in-non-selected-windows))
3360
3361 ;; Restore the outside saved values; don't alter
3362 ;; the outside binding loci.
3363 (setq-default cursor-in-non-selected-windows t))))
1fe3d507 3364
bd8d6108 3365(defun edebug-eval (expr)
53ff3e77 3366 (backtrace-eval expr 0 'edebug-after))
1fe3d507 3367
bd8d6108 3368(defun edebug-safe-eval (expr)
a1506d29 3369 ;; Evaluate EXPR safely.
1fe3d507
DL
3370 ;; If there is an error, a string is returned describing the error.
3371 (condition-case edebug-err
bd8d6108 3372 (edebug-eval expr)
a1506d29 3373 (error (edebug-format "%s: %s" ;; could
1fe3d507
DL
3374 (get (car edebug-err) 'error-message)
3375 (car (cdr edebug-err))))))
3376
f7359658
RS
3377;;; Printing
3378
1fe3d507 3379
bd8d6108 3380(defun edebug-report-error (value)
1fe3d507
DL
3381 ;; Print an error message like command level does.
3382 ;; This also prints the error name if it has no error-message.
a1506d29 3383 (message "%s: %s"
bd8d6108
SM
3384 (or (get (car value) 'error-message)
3385 (format "peculiar error (%s)" (car value)))
a1506d29 3386 (mapconcat (function (lambda (edebug-arg)
1fe3d507
DL
3387 ;; continuing after an error may
3388 ;; complain about edebug-arg. why??
3389 (prin1-to-string edebug-arg)))
bd8d6108 3390 (cdr value) ", ")))
1fe3d507 3391
08ee6200 3392(defvar print-readably) ; defined by lemacs
a1506d29 3393;; Alternatively, we could change the definition of
88668147 3394;; edebug-safe-prin1-to-string to only use these if defined.
1fe3d507
DL
3395
3396(defun edebug-safe-prin1-to-string (value)
84fc2cfa 3397 (let ((print-escape-newlines t)
1fe3d507
DL
3398 (print-length (or edebug-print-length print-length))
3399 (print-level (or edebug-print-level print-level))
3400 (print-circle (or edebug-print-circle print-circle))
08ee6200 3401 (print-readably nil)) ; lemacs uses this.
14e7cb94
CY
3402 (condition-case nil
3403 (edebug-prin1-to-string value)
3404 (error "#Apparently circular structure#"))))
1fe3d507 3405
bd8d6108 3406(defun edebug-compute-previous-result (previous-value)
e409c527 3407 (if edebug-unwrap-results
bd8d6108
SM
3408 (setq previous-value
3409 (edebug-unwrap* previous-value)))
1fe3d507 3410 (setq edebug-previous-result
e409c527 3411 (concat "Result: "
bd8d6108
SM
3412 (edebug-safe-prin1-to-string previous-value)
3413 (eval-expression-print-format previous-value))))
84fc2cfa 3414
1fe3d507
DL
3415(defun edebug-previous-result ()
3416 "Print the previous result."
3417 (interactive)
3418 (message "%s" edebug-previous-result))
84fc2cfa 3419
f7359658 3420;;; Read, Eval and Print
84fc2cfa 3421
03d5bbb0
RS
3422(defalias 'edebug-prin1 'prin1)
3423(defalias 'edebug-print 'print)
3424(defalias 'edebug-prin1-to-string 'prin1-to-string)
3425(defalias 'edebug-format 'format)
3426(defalias 'edebug-message 'message)
3427
bd8d6108 3428(defun edebug-eval-expression (expr)
a1506d29 3429 "Evaluate an expression in the outside environment.
1fe3d507 3430If interactive, prompt for the expression.
84fc2cfa 3431Print result in minibuffer."
a1506d29 3432 (interactive (list (read-from-minibuffer
10b088c6
RS
3433 "Eval: " nil read-expression-map t
3434 'read-expression-history)))
1fe3d507
DL
3435 (princ
3436 (edebug-outside-excursion
bd8d6108 3437 (setq values (cons (edebug-eval expr) values))
50a27de2
JL
3438 (concat (edebug-safe-prin1-to-string (car values))
3439 (eval-expression-print-format (car values))))))
84fc2cfa
ER
3440
3441(defun edebug-eval-last-sexp ()
b6386e63
LT
3442 "Evaluate sexp before point in the outside environment.
3443Print value in minibuffer."
84fc2cfa 3444 (interactive)
1fe3d507 3445 (edebug-eval-expression (edebug-last-sexp)))
84fc2cfa
ER
3446
3447(defun edebug-eval-print-last-sexp ()
b6386e63 3448 "Evaluate sexp before point in outside environment; insert value.
5fc58d83 3449This prints the value into current buffer."
84fc2cfa 3450 (interactive)
bd8d6108
SM
3451 (let* ((form (edebug-last-sexp))
3452 (result-string
a1506d29 3453 (edebug-outside-excursion
bd8d6108 3454 (edebug-safe-prin1-to-string (edebug-safe-eval form))))
1fe3d507
DL
3455 (standard-output (current-buffer)))
3456 (princ "\n")
3457 ;; princ the string to get rid of quotes.
bd8d6108 3458 (princ result-string)
1fe3d507
DL
3459 (princ "\n")
3460 ))
3461
a1506d29 3462;;; Edebug Minor Mode
84fc2cfa 3463
f1a4e679
CY
3464(defvar edebug-inhibit-emacs-lisp-mode-bindings nil
3465 "If non-nil, inhibit Edebug bindings on the C-x C-a key.
3466By default, loading the `edebug' library causes these bindings to
3467be installed in `emacs-lisp-mode-map'.")
3468
3469(define-obsolete-variable-alias 'gud-inhibit-global-bindings
2a1e2476 3470 'edebug-inhibit-emacs-lisp-mode-bindings "24.3")
a1506d29 3471
5fc58d83 3472;; Global GUD bindings for all emacs-lisp-mode buffers.
f1a4e679 3473(unless edebug-inhibit-emacs-lisp-mode-bindings
5fc58d83
RS
3474 (define-key emacs-lisp-mode-map "\C-x\C-a\C-s" 'edebug-step-mode)
3475 (define-key emacs-lisp-mode-map "\C-x\C-a\C-n" 'edebug-next-mode)
3476 (define-key emacs-lisp-mode-map "\C-x\C-a\C-c" 'edebug-go-mode)
3477 (define-key emacs-lisp-mode-map "\C-x\C-a\C-l" 'edebug-where))
84fc2cfa 3478
d778509c
SM
3479(defvar edebug-mode-map
3480 (let ((map (copy-keymap emacs-lisp-mode-map)))
84fc2cfa 3481 ;; control
d778509c
SM
3482 (define-key map " " 'edebug-step-mode)
3483 (define-key map "n" 'edebug-next-mode)
3484 (define-key map "g" 'edebug-go-mode)
3485 (define-key map "G" 'edebug-Go-nonstop-mode)
3486 (define-key map "t" 'edebug-trace-mode)
3487 (define-key map "T" 'edebug-Trace-fast-mode)
3488 (define-key map "c" 'edebug-continue-mode)
3489 (define-key map "C" 'edebug-Continue-fast-mode)
3490
3491 ;;(define-key map "f" 'edebug-forward) not implemented
3492 (define-key map "f" 'edebug-forward-sexp)
3493 (define-key map "h" 'edebug-goto-here)
3494
3495 (define-key map "I" 'edebug-instrument-callee)
3496 (define-key map "i" 'edebug-step-in)
3497 (define-key map "o" 'edebug-step-out)
a1506d29 3498
1fe3d507 3499 ;; quitting and stopping
d778509c
SM
3500 (define-key map "q" 'top-level)
3501 (define-key map "Q" 'edebug-top-level-nonstop)
3502 (define-key map "a" 'abort-recursive-edit)
3503 (define-key map "S" 'edebug-stop)
84fc2cfa
ER
3504
3505 ;; breakpoints
d778509c
SM
3506 (define-key map "b" 'edebug-set-breakpoint)
3507 (define-key map "u" 'edebug-unset-breakpoint)
3508 (define-key map "B" 'edebug-next-breakpoint)
3509 (define-key map "x" 'edebug-set-conditional-breakpoint)
3510 (define-key map "X" 'edebug-set-global-break-condition)
a1506d29 3511
84fc2cfa 3512 ;; evaluation
d778509c
SM
3513 (define-key map "r" 'edebug-previous-result)
3514 (define-key map "e" 'edebug-eval-expression)
3515 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3516 (define-key map "E" 'edebug-visit-eval-list)
a1506d29 3517
84fc2cfa 3518 ;; views
d778509c
SM
3519 (define-key map "w" 'edebug-where)
3520 (define-key map "v" 'edebug-view-outside) ;; maybe obsolete??
3521 (define-key map "p" 'edebug-bounce-point)
3522 (define-key map "P" 'edebug-view-outside) ;; same as v
3523 (define-key map "W" 'edebug-toggle-save-windows)
1fe3d507 3524
84fc2cfa 3525 ;; misc
d778509c
SM
3526 (define-key map "?" 'edebug-help)
3527 (define-key map "d" 'edebug-backtrace)
a1506d29 3528
d778509c 3529 (define-key map "-" 'negative-argument)
1fe3d507
DL
3530
3531 ;; statistics
d778509c 3532 (define-key map "=" 'edebug-temp-display-freq-count)
1fe3d507
DL
3533
3534 ;; GUD bindings
d778509c
SM
3535 (define-key map "\C-c\C-s" 'edebug-step-mode)
3536 (define-key map "\C-c\C-n" 'edebug-next-mode)
3537 (define-key map "\C-c\C-c" 'edebug-go-mode)
3538
3539 (define-key map "\C-x " 'edebug-set-breakpoint)
3540 (define-key map "\C-c\C-d" 'edebug-unset-breakpoint)
3541 (define-key map "\C-c\C-t"
3542 (lambda () (interactive) (edebug-set-breakpoint t)))
3543 (define-key map "\C-c\C-l" 'edebug-where)
3544 map))
84fc2cfa 3545
1fe3d507
DL
3546;; Autoloading these global bindings doesn't make sense because
3547;; they cannot be used anyway unless Edebug is already loaded and active.
3548
84fc2cfa
ER
3549(defvar global-edebug-prefix "\^XX"
3550 "Prefix key for global edebug commands, available from any buffer.")
3551
d778509c
SM
3552(defvar global-edebug-map
3553 (let ((map (make-sparse-keymap)))
3554
3555 (define-key map " " 'edebug-step-mode)
3556 (define-key map "g" 'edebug-go-mode)
3557 (define-key map "G" 'edebug-Go-nonstop-mode)
3558 (define-key map "t" 'edebug-trace-mode)
3559 (define-key map "T" 'edebug-Trace-fast-mode)
3560 (define-key map "c" 'edebug-continue-mode)
3561 (define-key map "C" 'edebug-Continue-fast-mode)
3562
3563 ;; breakpoints
3564 (define-key map "b" 'edebug-set-breakpoint)
3565 (define-key map "u" 'edebug-unset-breakpoint)
3566 (define-key map "x" 'edebug-set-conditional-breakpoint)
3567 (define-key map "X" 'edebug-set-global-break-condition)
3568
3569 ;; views
3570 (define-key map "w" 'edebug-where)
3571 (define-key map "W" 'edebug-toggle-save-windows)
3572
3573 ;; quitting
3574 (define-key map "q" 'top-level)
3575 (define-key map "Q" 'edebug-top-level-nonstop)
3576 (define-key map "a" 'abort-recursive-edit)
3577
3578 ;; statistics
3579 (define-key map "=" 'edebug-display-freq-count)
3580 map)
84fc2cfa
ER
3581 "Global map of edebug commands, available from any buffer.")
3582
d778509c
SM
3583(global-unset-key global-edebug-prefix)
3584(global-set-key global-edebug-prefix global-edebug-map)
3585
84fc2cfa
ER
3586
3587(defun edebug-help ()
1e46f9e4 3588 "Describe `edebug-mode'."
84fc2cfa
ER
3589 (interactive)
3590 (describe-function 'edebug-mode))
3591
17c781d1
SM
3592(defvar edebug--mode-saved-vars nil)
3593
3594(define-minor-mode edebug-mode
1fe3d507
DL
3595 "Mode for Emacs Lisp buffers while in Edebug.
3596
3597In addition to all Emacs Lisp commands (except those that modify the
3598buffer) there are local and global key bindings to several Edebug
a1506d29 3599specific commands. E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
1fe3d507 3600in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
84fc2cfa 3601
eb533587 3602Also see bindings for the eval list buffer *edebug* in `edebug-eval-mode'.
84fc2cfa 3603
1fe3d507 3604The edebug buffer commands:
84fc2cfa
ER
3605\\{edebug-mode-map}
3606
1fe3d507 3607Global commands prefixed by `global-edebug-prefix':
84fc2cfa
ER
3608\\{global-edebug-map}
3609
3610Options:
9854542e
SM
3611`edebug-setup-hook'
3612`edebug-all-defs'
3613`edebug-all-forms'
3614`edebug-save-windows'
3615`edebug-save-displayed-buffer-points'
3616`edebug-initial-mode'
3617`edebug-trace'
3618`edebug-test-coverage'
3619`edebug-continue-kbd-macro'
3620`edebug-print-length'
3621`edebug-print-level'
3622`edebug-print-circle'
3623`edebug-on-error'
3624`edebug-on-quit'
3625`edebug-on-signal'
3626`edebug-unwrap-results'
3627`edebug-global-break-condition'"
17c781d1
SM
3628 :lighter " *Debugging*"
3629 :keymap edebug-mode-map
e20e4a48
RS
3630 ;; If the user kills the buffer in which edebug is currently active,
3631 ;; exit to top level, because the edebug command loop can't usefully
3632 ;; continue running in such a case.
7b0e2f85 3633 ;;
17c781d1
SM
3634 (if (not edebug-mode)
3635 (progn
3636 (while edebug--mode-saved-vars
3637 (let ((setting (pop edebug--mode-saved-vars)))
3638 (if (consp setting)
3639 (set (car setting) (cdr setting))
3640 (kill-local-variable setting))))
3641 (remove-hook 'kill-buffer-hook 'edebug-kill-buffer t))
3642 (pcase-dolist (`(,var . ,val) '((buffer-read-only . t)))
3643 (push
6fcdab68 3644 (if (local-variable-p var) (cons var (symbol-value var)) var)
17c781d1
SM
3645 edebug--mode-saved-vars)
3646 (set (make-local-variable var) val))
3647 ;; Append `edebug-kill-buffer' to the hook to avoid interfering with
b9edfa5c 3648 ;; other entries that are unguarded against deleted buffer.
17c781d1 3649 (add-hook 'kill-buffer-hook 'edebug-kill-buffer t t)))
84fc2cfa 3650
e20e4a48
RS
3651(defun edebug-kill-buffer ()
3652 "Used on `kill-buffer-hook' when Edebug is operating in a buffer of Lisp code."
17c781d1 3653 (run-with-timer 0 nil #'top-level))
e20e4a48 3654
f7359658 3655;;; edebug eval list mode
84fc2cfa 3656
1fe3d507 3657;; A list of expressions and their evaluations is displayed in *edebug*.
84fc2cfa
ER
3658
3659(defun edebug-eval-result-list ()
242b1f68 3660 "Return a list of evaluations of `edebug-eval-list'."
84fc2cfa 3661 ;; Assumes in outside environment.
88668147
DL
3662 ;; Don't do any edebug things now.
3663 (let ((edebug-execution-mode 'Go-nonstop)
a1506d29 3664 (edebug-trace nil))
88668147 3665 (mapcar 'edebug-safe-eval edebug-eval-list)))
84fc2cfa 3666
bd8d6108 3667(defun edebug-eval-display-list (eval-result-list)
84fc2cfa 3668 ;; Assumes edebug-eval-buffer exists.
bd8d6108 3669 (let ((standard-output edebug-eval-buffer)
1fe3d507 3670 (edebug-comment-line
84fc2cfa 3671 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
1fe3d507 3672 (set-buffer edebug-eval-buffer)
84fc2cfa 3673 (erase-buffer)
bd8d6108
SM
3674 (dolist (exp edebug-eval-list)
3675 (prin1 exp) (terpri)
3676 (prin1 (pop eval-result-list)) (terpri)
3677 (princ edebug-comment-line))
1fe3d507 3678 (edebug-pop-to-buffer edebug-eval-buffer)
84fc2cfa
ER
3679 ))
3680
3681(defun edebug-create-eval-buffer ()
c0c54fbd
SM
3682 (unless (and edebug-eval-buffer (buffer-name edebug-eval-buffer))
3683 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
3684 (edebug-eval-mode)))
84fc2cfa
ER
3685
3686;; Should generalize this to be callable outside of edebug
3687;; with calls in user functions, e.g. (edebug-eval-display)
3688
bd8d6108
SM
3689(defun edebug-eval-display (eval-result-list)
3690 "Display expressions and evaluations in EVAL-RESULT-LIST.
84fc2cfa 3691It modifies the context by popping up the eval display."
bd8d6108
SM
3692 (when eval-result-list
3693 (edebug-create-eval-buffer)
3694 (edebug-eval-display-list eval-result-list)))
84fc2cfa
ER
3695
3696(defun edebug-eval-redisplay ()
3697 "Redisplay eval list in outside environment.
bd8d6108 3698May only be called from within `edebug--recursive-edit'."
84fc2cfa 3699 (edebug-create-eval-buffer)
84fc2cfa
ER
3700 (edebug-outside-excursion
3701 (edebug-eval-display-list (edebug-eval-result-list))
3702 ))
3703
3704(defun edebug-visit-eval-list ()
1e46f9e4 3705 "Switch to the evaluation list buffer \"*edebug*\"."
84fc2cfa
ER
3706 (interactive)
3707 (edebug-eval-redisplay)
3708 (edebug-pop-to-buffer edebug-eval-buffer))
3709
3710
3711(defun edebug-update-eval-list ()
3712 "Replace the evaluation list with the sexps now in the eval buffer."
3713 (interactive)
3714 (let ((starting-point (point))
3715 new-list)
3716 (goto-char (point-min))
3717 ;; get the first expression
3718 (edebug-skip-whitespace)
3719 (if (not (eobp))
3720 (progn
3721 (forward-sexp 1)
c0c54fbd 3722 (push (edebug-last-sexp) new-list)))
a1506d29 3723
84fc2cfa
ER
3724 (while (re-search-forward "^;" nil t)
3725 (forward-line 1)
3726 (skip-chars-forward " \t\n\r")
3727 (if (and (/= ?\; (following-char))
3728 (not (eobp)))
3729 (progn
3730 (forward-sexp 1)
c0c54fbd 3731 (push (edebug-last-sexp) new-list))))
a1506d29 3732
84fc2cfa
ER
3733 (setq edebug-eval-list (nreverse new-list))
3734 (edebug-eval-redisplay)
3735 (goto-char starting-point)))
3736
3737
3738(defun edebug-delete-eval-item ()
3739 "Delete the item under point and redisplay."
3740 ;; could add arg to do repeatedly
3741 (interactive)
3742 (if (re-search-backward "^;" nil 'nofail)
3743 (forward-line 1))
3744 (delete-region
3745 (point) (progn (re-search-forward "^;" nil 'nofail)
3746 (beginning-of-line)
3747 (point)))
3748 (edebug-update-eval-list))
3749
3750
3751
a0310a6c
DN
3752(defvar edebug-eval-mode-map
3753 (let ((map (make-sparse-keymap)))
3754 (set-keymap-parent map lisp-interaction-mode-map)
3755 (define-key map "\C-c\C-w" 'edebug-where)
3756 (define-key map "\C-c\C-d" 'edebug-delete-eval-item)
3757 (define-key map "\C-c\C-u" 'edebug-update-eval-list)
3758 (define-key map "\C-x\C-e" 'edebug-eval-last-sexp)
3759 (define-key map "\C-j" 'edebug-eval-print-last-sexp)
bd8d6108
SM
3760 map)
3761 "Keymap for Edebug Eval mode. Superset of Lisp Interaction mode.")
84fc2cfa 3762
b7414395 3763(put 'edebug-eval-mode 'mode-class 'special)
84fc2cfa 3764
e5d79aa5 3765(define-derived-mode edebug-eval-mode lisp-interaction-mode "Edebug Eval"
1fe3d507
DL
3766 "Mode for evaluation list buffer while in Edebug.
3767
3768In addition to all Interactive Emacs Lisp commands there are local and
3769global key bindings to several Edebug specific commands. E.g.
3770`edebug-step-mode' is bound to \\[edebug-step-mode] in the Edebug
3771buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
84fc2cfa
ER
3772
3773Eval list buffer commands:
3774\\{edebug-eval-mode-map}
3775
eb533587 3776Global commands prefixed by `global-edebug-prefix':
e5d79aa5 3777\\{global-edebug-map}")
84fc2cfa 3778
f7359658 3779;;; Interface with standard debugger.
84fc2cfa 3780
1fe3d507
DL
3781;; (setq debugger 'edebug) ; to use the edebug debugger
3782;; (setq debugger 'debug) ; use the standard debugger
84fc2cfa 3783
1fe3d507
DL
3784;; Note that debug and its utilities must be byte-compiled to work,
3785;; since they depend on the backtrace looking a certain way. But
3786;; edebug is not dependent on this, yet.
84fc2cfa 3787
bd8d6108 3788(defun edebug (&optional arg-mode &rest args)
242b1f68
JB
3789 "Replacement for `debug'.
3790If we are running an edebugged function, show where we last were.
3791Otherwise call `debug' normally."
bd8d6108
SM
3792 ;;(message "entered: %s depth: %s edebug-recursion-depth: %s"
3793 ;; edebug-entered (recursion-depth) edebug-recursion-depth) (sit-for 1)
1fe3d507
DL
3794 (if (and edebug-entered ; anything active?
3795 (eq (recursion-depth) edebug-recursion-depth))
3796 (let (;; Where were we before the error occurred?
bd8d6108
SM
3797 (offset-index (car edebug-offset-indices))
3798 (value (car args))
3799 ;; Bind variables required by edebug--display.
1fe3d507
DL
3800 edebug-breakpoints
3801 edebug-break-data
3802 edebug-break-condition
3803 edebug-global-break
bd8d6108 3804 (edebug-break (null arg-mode)) ;; If called explicitly.
1fe3d507 3805 )
bd8d6108
SM
3806 (edebug--display value offset-index arg-mode)
3807 (if (eq arg-mode 'error)
1fe3d507 3808 nil
bd8d6108 3809 value))
84fc2cfa
ER
3810
3811 ;; Otherwise call debug normally.
3812 ;; Still need to remove extraneous edebug calls from stack.
bd8d6108 3813 (apply 'debug arg-mode args)
84fc2cfa
ER
3814 ))
3815
3816
3817(defun edebug-backtrace ()
3818 "Display a non-working backtrace. Better than nothing..."
3819 (interactive)
1fe3d507
DL
3820 (if (or (not edebug-backtrace-buffer)
3821 (null (buffer-name edebug-backtrace-buffer)))
3822 (setq edebug-backtrace-buffer
3823 (generate-new-buffer "*Backtrace*"))
bd8d6108 3824 ;; Else, could just display edebug-backtrace-buffer.
1fe3d507
DL
3825 )
3826 (with-output-to-temp-buffer (buffer-name edebug-backtrace-buffer)
3827 (setq edebug-backtrace-buffer standard-output)
3828 (let ((print-escape-newlines t)
1e46f9e4 3829 (print-length 50) ; FIXME cf edebug-safe-prin1-to-string
1fe3d507 3830 last-ok-point)
84fc2cfa
ER
3831 (backtrace)
3832
a1506d29 3833 ;; Clean up the backtrace.
1fe3d507
DL
3834 ;; Not quite right for current edebug scheme.
3835 (set-buffer edebug-backtrace-buffer)
3836 (setq truncate-lines t)
84fc2cfa 3837 (goto-char (point-min))
84fc2cfa 3838 (setq last-ok-point (point))
1fe3d507 3839 (if t (progn
84fc2cfa
ER
3840
3841 ;; Delete interspersed edebug internals.
1fe3d507
DL
3842 (while (re-search-forward "^ \(?edebug" nil t)
3843 (beginning-of-line)
a1506d29 3844 (cond
1fe3d507 3845 ((looking-at "^ \(edebug-after")
bd8d6108 3846 ;; Previous lines may contain code, so just delete this line.
1fe3d507
DL
3847 (setq last-ok-point (point))
3848 (forward-line 1)
3849 (delete-region last-ok-point (point)))
3850
3851 ((looking-at "^ edebug")
3852 (forward-line 1)
3853 (delete-region last-ok-point (point))
3854 )))
3855 )))))
84fc2cfa
ER
3856
3857\f
f7359658 3858;;; Trace display
84fc2cfa
ER
3859
3860(defun edebug-trace-display (buf-name fmt &rest args)
3861 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
3862The buffer is created if it does not exist.
1fe3d507 3863You must include newlines in FMT to break lines, but one newline is appended."
2de39f08
SM
3864 ;; e.g.
3865 ;; (edebug-trace-display "*trace-point*"
3866 ;; "saving: point = %s window-start = %s"
3867 ;; (point) (window-start))
dc0e03f3
RS
3868 (let* ((oldbuf (current-buffer))
3869 (selected-window (selected-window))
84fc2cfa 3870 (buffer (get-buffer-create buf-name))
1fe3d507 3871 buf-window)
2de39f08 3872 ;; (message "before pop-to-buffer") (sit-for 1)
84fc2cfa 3873 (edebug-pop-to-buffer buffer)
1fe3d507
DL
3874 (setq truncate-lines t)
3875 (setq buf-window (selected-window))
3876 (goto-char (point-max))
3877 (insert (apply 'edebug-format fmt args) "\n")
3878 ;; Make it visible.
3879 (vertical-motion (- 1 (window-height)))
3880 (set-window-start buf-window (point))
3881 (goto-char (point-max))
2de39f08
SM
3882 ;; (set-window-point buf-window (point))
3883 ;; (sit-for 0)
1fe3d507 3884 (bury-buffer buffer)
dc0e03f3
RS
3885 (select-window selected-window)
3886 (set-buffer oldbuf))
1fe3d507
DL
3887 buf-name)
3888
3889
3890(defun edebug-trace (fmt &rest args)
242b1f68 3891 "Convenience call to `edebug-trace-display' using `edebug-trace-buffer'."
1fe3d507
DL
3892 (apply 'edebug-trace-display edebug-trace-buffer fmt args))
3893
3894\f
f7359658 3895;;; Frequency count and coverage
1fe3d507 3896
1e46f9e4 3897;; FIXME should this use overlays instead?
0b021094
GM
3898;; Definitely, IMO. The current business with undo in
3899;; edebug-temp-display-freq-count is horrid.
1fe3d507 3900(defun edebug-display-freq-count ()
1ae7cf5e 3901 "Display the frequency count data for each line of the current definition.
242b1f68
JB
3902The frequency counts are inserted as comment lines after each line,
3903and you can undo all insertions with one `undo' command.
1fe3d507
DL
3904
3905The counts are inserted starting under the `(' before an expression
3906or the `)' after an expression, or on the last char of a symbol.
3907The counts are only displayed when they differ from previous counts on
3908the same line.
3909
3910If coverage is being tested, whenever all known results of an expression
3911are `eq', the char `=' will be appended after the count
3912for that expression. Note that this is always the case for an
3913expression only evaluated once.
3914
3915To clear the frequency count and coverage data for a definition,
3916reinstrument it."
3917 (interactive)
3918 (let* ((function (edebug-form-data-symbol))
3919 (counts (get function 'edebug-freq-count))
3920 (coverages (get function 'edebug-coverage))
3921 (data (get function 'edebug))
3922 (def-mark (car data)) ; mark at def start
3923 (edebug-points (nth 2 data))
3924 (i (1- (length edebug-points)))
3925 (last-index)
3926 (first-index)
3927 (start-of-line)
3928 (start-of-count-line)
3929 (last-count)
3930 )
84fc2cfa 3931 (save-excursion
1fe3d507
DL
3932 ;; Traverse in reverse order so offsets are correct.
3933 (while (<= 0 i)
3934 ;; Start at last expression in line.
3935 (goto-char (+ def-mark (aref edebug-points i)))
3936 (beginning-of-line)
3937 (setq start-of-line (- (point) def-mark)
3938 last-index i)
3939
3940 ;; Find all indexes on same line.
a1506d29 3941 (while (and (<= 0 (setq i (1- i)))
1fe3d507
DL
3942 (<= start-of-line (aref edebug-points i))))
3943 ;; Insert all the indices for this line.
3944 (forward-line 1)
3945 (setq start-of-count-line (point)
2de39f08
SM
3946 first-index i ; Really, last index for line above this one.
3947 last-count -1) ; Cause first count to always appear.
1fe3d507
DL
3948 (insert ";#")
3949 ;; i == first-index still
3950 (while (<= (setq i (1+ i)) last-index)
3951 (let ((count (aref counts i))
3952 (coverage (aref coverages i))
3953 (col (save-excursion
3954 (goto-char (+ (aref edebug-points i) def-mark))
3955 (- (current-column)
3956 (if (= ?\( (following-char)) 0 1)))))
a1506d29 3957 (insert (make-string
ea400c88 3958 (max 0 (- col (- (point) start-of-count-line))) ?\s)
1fe3d507 3959 (if (and (< 0 count)
a1506d29 3960 (not (memq coverage
1fe3d507
DL
3961 '(unknown ok-coverage))))
3962 "=" "")
3963 (if (= count last-count) "" (int-to-string count))
3964 " ")
3965 (setq last-count count)))
3966 (insert "\n")
3967 (setq i first-index)))))
3968
0b021094
GM
3969;; FIXME this does not work very well. Eg if you press an arrow key,
3970;; or make a mouse-click, it fails with "Non-character input-event".
1fe3d507
DL
3971(defun edebug-temp-display-freq-count ()
3972 "Temporarily display the frequency count data for the current definition.
3973It is removed when you hit any char."
3974 ;; This seems not to work with Emacs 18.59. It undoes too far.
3975 (interactive)
17c781d1 3976 (let ((inhibit-read-only t))
1fe3d507
DL
3977 (undo-boundary)
3978 (edebug-display-freq-count)
2de39f08
SM
3979 (setq unread-command-events
3980 (append unread-command-events (list (read-event))))
0b021094 3981 ;; Yuck! This doesn't seem to work at all for me.
1fe3d507
DL
3982 (undo)))
3983
3984\f
f7359658 3985;;; Menus
1fe3d507
DL
3986
3987(defun edebug-toggle (variable)
a0ee6f27
SM
3988 (set variable (not (symbol-value variable)))
3989 (message "%s: %s" variable (symbol-value variable)))
1fe3d507
DL
3990
3991;; We have to require easymenu (even for Emacs 18) just so
3992;; the easy-menu-define macro call is compiled correctly.
3993(require 'easymenu)
3994
3995(defconst edebug-mode-menus
3996 '("Edebug"
1fe3d507
DL
3997 ["Stop" edebug-stop t]
3998 ["Step" edebug-step-mode t]
3999 ["Next" edebug-next-mode t]
4000 ["Trace" edebug-trace-mode t]
4001 ["Trace Fast" edebug-Trace-fast-mode t]
4002 ["Continue" edebug-continue-mode t]
4003 ["Continue Fast" edebug-Continue-fast-mode t]
4004 ["Go" edebug-go-mode t]
4005 ["Go Nonstop" edebug-Go-nonstop-mode t]
4006 "----"
4007 ["Help" edebug-help t]
4008 ["Abort" abort-recursive-edit t]
4009 ["Quit to Top Level" top-level t]
4010 ["Quit Nonstop" edebug-top-level-nonstop t]
4011 "----"
4012 ("Jumps"
4013 ["Forward Sexp" edebug-forward-sexp t]
4014 ["Step In" edebug-step-in t]
4015 ["Step Out" edebug-step-out t]
4016 ["Goto Here" edebug-goto-here t])
4017
4018 ("Breaks"
4019 ["Set Breakpoint" edebug-set-breakpoint t]
4020 ["Unset Breakpoint" edebug-unset-breakpoint t]
4021 ["Set Conditional Breakpoint" edebug-set-conditional-breakpoint t]
4022 ["Set Global Break Condition" edebug-set-global-break-condition t]
4023 ["Show Next Breakpoint" edebug-next-breakpoint t])
4024
4025 ("Views"
4026 ["Where am I?" edebug-where t]
4027 ["Bounce to Current Point" edebug-bounce-point t]
4028 ["View Outside Windows" edebug-view-outside t]
4029 ["Previous Result" edebug-previous-result t]
4030 ["Show Backtrace" edebug-backtrace t]
4031 ["Display Freq Count" edebug-display-freq-count t])
4032
4033 ("Eval"
4034 ["Expression" edebug-eval-expression t]
4035 ["Last Sexp" edebug-eval-last-sexp t]
4036 ["Visit Eval List" edebug-visit-eval-list t])
4037
4038 ("Options"
6db746da
DL
4039 ["Edebug All Defs" edebug-all-defs
4040 :style toggle :selected edebug-all-defs]
4041 ["Edebug All Forms" edebug-all-forms
4042 :style toggle :selected edebug-all-forms]
1fe3d507 4043 "----"
6db746da
DL
4044 ["Tracing" (edebug-toggle 'edebug-trace)
4045 :style toggle :selected edebug-trace]
4046 ["Test Coverage" (edebug-toggle 'edebug-test-coverage)
4047 :style toggle :selected edebug-test-coverage]
4048 ["Save Windows" edebug-toggle-save-windows
4049 :style toggle :selected edebug-save-windows]
a1506d29 4050 ["Save Point"
6db746da
DL
4051 (edebug-toggle 'edebug-save-displayed-buffer-points)
4052 :style toggle :selected edebug-save-displayed-buffer-points]
1fe3d507 4053 ))
6db746da 4054 "Menus for Edebug.")
1fe3d507
DL
4055
4056\f
f7359658
RS
4057;;; Emacs version specific code
4058
10b088c6 4059(defalias 'edebug-window-live-p 'window-live-p)
1fe3d507 4060
10b088c6 4061(defun edebug-mark ()
f5c9c551 4062 (mark t))
10b088c6
RS
4063
4064(defun edebug-set-conditional-breakpoint (arg condition)
4065 "Set a conditional breakpoint at nearest sexp.
4066The condition is evaluated in the outside context.
4067With prefix argument, make it a temporary breakpoint."
4068 ;; (interactive "P\nxCondition: ")
a1506d29 4069 (interactive
10b088c6
RS
4070 (list
4071 current-prefix-arg
20c78df0 4072 ;; Read condition as follows; getting previous condition is cumbersome:
10b088c6
RS
4073 (let ((edebug-stop-point (edebug-find-stop-point)))
4074 (if edebug-stop-point
4075 (let* ((edebug-def-name (car edebug-stop-point))
4076 (index (cdr edebug-stop-point))
4077 (edebug-data (get edebug-def-name 'edebug))
4078 (edebug-breakpoints (car (cdr edebug-data)))
4079 (edebug-break-data (assq index edebug-breakpoints))
4080 (edebug-break-condition (car (cdr edebug-break-data)))
20c78df0
JL
4081 (initial (and edebug-break-condition
4082 (format "%s" edebug-break-condition))))
4083 (read-from-minibuffer
4084 "Condition: " initial read-expression-map t
4085 (if (equal (car read-expression-history) initial)
4086 '(read-expression-history . 1)
4087 'read-expression-history)))))))
10b088c6
RS
4088 (edebug-modify-breakpoint t condition arg))
4089
0b936a1e 4090(easy-menu-define edebug-menu edebug-mode-map "Edebug menus" edebug-mode-menus)
1fe3d507 4091\f
f7359658 4092;;; Autoloading of Edebug accessories
1fe3d507 4093
b1a03ef6 4094;; edebug-cl-read and cl-read are available from liberte@cs.uiuc.edu
5f3a3bb1
JB
4095(defun edebug--require-cl-read ()
4096 (require 'edebug-cl-read))
4097
1fe3d507 4098(if (featurep 'cl-read)
5f3a3bb1 4099 (add-hook 'edebug-setup-hook #'edebug--require-cl-read)
1fe3d507 4100 ;; The following causes edebug-cl-read to be loaded when you load cl-read.el.
5f3a3bb1 4101 (add-hook 'cl-read-load-hooks #'edebug--require-cl-read))
1fe3d507
DL
4102
4103\f
f7359658 4104;;; Finalize Loading
1fe3d507 4105
23ba2705
SM
4106;; When edebugging a function, some of the sub-expressions are
4107;; wrapped in (edebug-enter (lambda () ..)), so we need to teach
4108;; called-interactively-p that calls within the inner lambda should refer to
4109;; the outside function.
4110(add-hook 'called-interactively-p-functions
4111 #'edebug--called-interactively-skip)
4112(defun edebug--called-interactively-skip (i frame1 frame2)
4113 (when (and (eq (car-safe (nth 1 frame1)) 'lambda)
4114 (eq (nth 1 (nth 1 frame1)) '())
4115 (eq (nth 1 frame2) 'edebug-enter))
4116 ;; `edebug-enter' calls itself on its first invocation.
56ea7291 4117 (if (eq (nth 1 (backtrace-frame i 'called-interactively-p))
23ba2705
SM
4118 'edebug-enter)
4119 2 1)))
4120
b1a03ef6
SM
4121;; Finally, hook edebug into the rest of Emacs.
4122;; There are probably some other things that could go here.
1fe3d507
DL
4123
4124;; Install edebug read and eval functions.
4125(edebug-install-read-eval-functions)
84fc2cfa 4126
5f3a3bb1
JB
4127(defun edebug-unload-function ()
4128 "Unload the Edebug source level debugger."
4129 (when edebug-active
ce0440ff 4130 (setq edebug-active nil)
5f3a3bb1
JB
4131 (unwind-protect
4132 (abort-recursive-edit)
ce0440ff
JB
4133 ;; We still want to run unload-feature to completion
4134 (run-with-idle-timer 0 nil #'(lambda () (unload-feature 'edebug)))))
5f3a3bb1
JB
4135 (remove-hook 'called-interactively-p-functions
4136 'edebug--called-interactively-skip)
4137 (remove-hook 'cl-read-load-hooks 'edebug--require-cl-read)
4138 (edebug-uninstall-read-eval-functions)
4139 ;; continue standard unloading
4140 nil)
4141
e8544af2
RS
4142(provide 'edebug)
4143
84fc2cfa 4144;;; edebug.el ends here