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