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