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