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