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