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