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