(hs-special-modes-alist): Include also the
[bpt/emacs.git] / lisp / emacs-lisp / edebug.el
CommitLineData
f7359658 1;;; edebug.el --- a source-level debugger for Emacs Lisp
84fc2cfa 2
c5292bc8
RS
3;; Copyright (C) 1988,'89,'90,'91,'92,'93,'94,'95,'97
4;; Free Software Foundation, Inc
84fc2cfa 5
3a801d0c 6;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
e9571d2a 7;; Keywords: lisp, tools, maint
3a801d0c 8
1fe3d507
DL
9;; LCD Archive Entry:
10;; edebug|Daniel LaLiberte|liberte@cs.uiuc.edu
11;; |A source level debugger for Emacs Lisp.
b7414395 12;; |$Date: 1997/04/14 20:57:39 $|$Revision: 3.15 $|~/modes/edebug.el|
e41b2db1 13
84fc2cfa
ER
14;; This file is part of GNU Emacs.
15
1fe3d507
DL
16;; GNU Emacs is free software; you can redistribute it and/or modify
17;; it under the terms of the GNU General Public License as published by
18;; the Free Software Foundation; either version 2, or (at your option)
19;; any later version.
20
84fc2cfa 21;; GNU Emacs is distributed in the hope that it will be useful,
1fe3d507
DL
22;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24;; GNU General Public License for more details.
25
26;; You should have received a copy of the GNU General Public License
f7359658
RS
27;; along with GNU Emacs; see the file COPYING. If not, write to the
28;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29;; Boston, MA 02111-1307, USA.
30
31;;; Commentary:
32
33;; This minor mode allows programmers to step through Emacs Lisp
34;; source code while executing functions. You can also set
35;; breakpoints, trace (stopping at each expression), evaluate
36;; expressions as if outside Edebug, reevaluate and display a list of
37;; expressions, trap errors normally caught by debug, and display a
38;; debug style backtrace.
39
40;;; Installation
41;; =============
42
43;; Put edebug.el in some directory in your load-path and
44;; byte-compile it. Also read the beginning of edebug-epoch.el,
45;; cl-specs.el, and edebug-cl-read.el if they apply to you.
46
47;; Unless you are using Emacs 19 which is already set up to use Edebug,
48;; put the following forms in your .emacs file.
49;; (define-key emacs-lisp-mode-map "\C-xx" 'edebug-eval-top-level-form)
50;; (autoload 'edebug-eval-top-level-form "edebug")
51
52;; If you wish to change the default edebug global command prefix, change:
53;; (setq edebug-global-prefix "\C-xX")
54
55;; Other options, are described in the manual.
56
57;; In previous versions of Edebug, users were directed to set
58;; `debugger' to `edebug-debug'. This is no longer necessary
59;; since Edebug automatically sets it whenever Edebug is active.
60
61;;; Minimal Instructions
62;; =====================
63
64;; First evaluate a defun with C-xx, then run the function. Step
65;; through the code with SPC, mark breakpoints with b, go until a
66;; breakpoint is reached with g, and quit execution with q. Use the
67;; "?" command in edebug to describe other commands. See edebug.tex
68;; or the Emacs 19 Lisp Reference Manual for more instructions.
69
70;; Send me your enhancements, ideas, bugs, or fixes.
71;; For bugs, you can call edebug-submit-bug-report if you have reporter.el.
72;; There is an edebug mailing list if you want to keep up
73;; with the latest developments. Requests to: edebug-request@cs.uiuc.edu
74
75;; Daniel LaLiberte 217-398-4114
76;; University of Illinois, Urbana-Champaign
77;; Department of Computer Science
78;; 1304 W Springfield
79;; Urbana, IL 61801
80
81;; uiucdcs!liberte
82;; liberte@cs.uiuc.edu
83
84;; For the early revision history, see edebug-history.
85
86;;; Code:
1fe3d507
DL
87
88(defconst edebug-version
b7414395 89 (let ((raw-version "$Revision: 3.15 $"))
1576fe1d 90 (substring raw-version (string-match "[0-9.]*" raw-version)
1fe3d507
DL
91 (match-end 0))))
92
93(require 'backquote)
94
f7359658 95;; Emacs 18 doesn't have defalias.
1fe3d507
DL
96(eval-and-compile
97 (or (fboundp 'defalias) (fset 'defalias 'fset)))
84fc2cfa 98
84fc2cfa 99
f7359658 100;;; Bug reporting
84fc2cfa 101
1fe3d507 102(defconst edebug-maintainer-address "liberte@cs.uiuc.edu")
84fc2cfa 103
1fe3d507
DL
104(defun edebug-submit-bug-report ()
105 "Submit, via mail, a bug report on edebug."
106 (interactive)
107 (require 'reporter)
108 (and (y-or-n-p "Do you really want to submit a report on edebug? ")
109 (reporter-submit-bug-report
110 edebug-maintainer-address
111 (concat "edebug.el " edebug-version)
112 (list 'edebug-setup-hook
113 'edebug-all-defs
114 'edebug-all-forms
115 'edebug-eval-macro-args
116 'edebug-stop-before-symbols
117 'edebug-save-windows
118 'edebug-save-displayed-buffer-points
119 'edebug-initial-mode
120 'edebug-trace
121 'edebug-test-coverage
122 'edebug-continue-kbd-macro
123 'edebug-print-length
124 'edebug-print-level
125 'edebug-print-circle
126 ))))
127
f7359658 128;;; Options
1fe3d507 129
c5292bc8
RS
130(defgroup edebug nil
131 "A source-level debugger for Emacs Lisp"
132 :group 'lisp)
133
134
135(defcustom edebug-setup-hook nil
1fe3d507 136 "*Functions to call before edebug is used.
a50d4326
DL
137Each time it is set to a new value, Edebug will call those functions
138once and then `edebug-setup-hook' is reset to nil. You could use this
139to load up Edebug specifications associated with a package you are
c5292bc8
RS
140using but only when you also use Edebug."
141 :type 'hook
142 :group 'edebug)
1fe3d507 143
c5292bc8 144(defcustom edebug-all-defs nil
a50d4326
DL
145 "*If non-nil, evaluation of any defining forms will instrument for Edebug.
146This applies to `eval-defun', `eval-region', `eval-buffer', and
147`eval-current-buffer'. `eval-region' is also called by
1fe3d507
DL
148`eval-last-sexp', and `eval-print-last-sexp'.
149
150You can use the command `edebug-all-defs' to toggle the value of this
a50d4326 151variable. You may wish to make it local to each buffer with
f7359658 152\(make-local-variable 'edebug-all-defs) in your
c5292bc8
RS
153`emacs-lisp-mode-hook'."
154 :type 'boolean
155 :group 'edebug)
1fe3d507 156
c5292bc8 157(defcustom edebug-all-forms nil
a50d4326 158 "*Non-nil evaluation of all forms will instrument for Edebug.
1fe3d507 159This doesn't apply to loading or evaluations in the minibuffer.
c5292bc8
RS
160Use the command `edebug-all-forms' to toggle the value of this option."
161 :type 'boolean
162 :group 'edebug)
84fc2cfa 163
c5292bc8 164(defcustom edebug-eval-macro-args nil
1fe3d507 165 "*Non-nil means all macro call arguments may be evaluated.
f7359658 166If this variable is nil, the default, Edebug will *not* wrap
1fe3d507 167macro call arguments as if they will be evaluated.
f7359658 168For each macro, a `edebug-form-spec' overrides this option.
1fe3d507 169So to specify exceptions for macros that have some arguments evaluated
f7359658 170and some not, you should specify an `edebug-form-spec'.
1fe3d507 171
c5292bc8
RS
172This option is going away soon."
173 :type 'boolean
174 :group 'edebug)
84fc2cfa 175
c5292bc8 176(defcustom edebug-stop-before-symbols nil
a50d4326
DL
177 "*Non-nil causes Edebug to stop before symbols as well as after.
178In any case, a breakpoint or interrupt may stop before a symbol.
179
c5292bc8
RS
180This option is going away soon."
181 :type 'boolean
182 :group 'edebug)
84fc2cfa 183
c5292bc8 184(defcustom edebug-save-windows t
a50d4326
DL
185 "*If non-nil, Edebug saves and restores the window configuration.
186That takes some time, so if your program does not care what happens to
187the window configurations, it is better to set this variable to nil.
1fe3d507
DL
188
189If the value is a list, only the listed windows are saved and
190restored.
84fc2cfa 191
c5292bc8
RS
192`edebug-toggle-save-windows' may be used to change this variable."
193 :type '(choice boolean (repeat string))
194 :group 'edebug)
84fc2cfa 195
c5292bc8 196(defcustom edebug-save-displayed-buffer-points nil
a50d4326 197 "*If non-nil, save and restore point in all displayed buffers.
84fc2cfa 198
a50d4326
DL
199Saving and restoring point in other buffers is necessary if you are
200debugging code that changes the point of a buffer which is displayed
201in a non-selected window. If Edebug or the user then selects the
84fc2cfa
ER
202window, the buffer's point will be changed to the window's point.
203
a50d4326
DL
204Saving and restoring point in all buffers is expensive, since it
205requires selecting each window twice, so enable this only if you need
c5292bc8
RS
206it."
207 :type 'boolean
208 :group 'edebug)
84fc2cfa 209
c5292bc8 210(defcustom edebug-initial-mode 'step
a50d4326
DL
211 "*Initial execution mode for Edebug, if non-nil. If this variable
212is non-@code{nil}, it specifies the initial execution mode for Edebug
213when it is first activated. Possible values are step, next, go,
c5292bc8
RS
214Go-nonstop, trace, Trace-fast, continue, and Continue-fast."
215 :type '(choice (const step) (const next) (const go)
216 (const Go-nonstop) (const trace)
217 (const Trace-fast) (const continue)
218 (const continue-fast))
219 :group 'edebug)
220
221(defcustom edebug-trace nil
a50d4326
DL
222 "*Non-nil means display a trace of function entry and exit.
223Tracing output is displayed in a buffer named `*edebug-trace*', one
224function entry or exit per line, indented by the recursion level.
225
226You can customize by replacing functions `edebug-print-trace-before'
c5292bc8
RS
227and `edebug-print-trace-after'."
228 :type 'boolean
229 :group 'edebug)
84fc2cfa 230
c5292bc8 231(defcustom edebug-test-coverage nil
1fe3d507
DL
232 "*If non-nil, Edebug tests coverage of all expressions debugged.
233This is done by comparing the result of each expression
234with the previous result. Coverage is considered OK if two different
a50d4326 235results are found.
84fc2cfa 236
1fe3d507 237Use `edebug-display-freq-count' to display the frequency count and
c5292bc8
RS
238coverage information for a definition."
239 :type 'boolean
240 :group 'edebug)
1fe3d507 241
c5292bc8 242(defcustom edebug-continue-kbd-macro nil
a50d4326 243 "*If non-nil, continue defining or executing any keyboard macro.
c5292bc8
RS
244Use this with caution since it is not debugged."
245 :type 'boolean
246 :group 'edebug)
247
248
249(defcustom edebug-print-length 50
250 "*Default value of `print-length' to use while printing results in Edebug."
251 :type 'integer
252 :group 'edebug)
253(defcustom edebug-print-level 50
254 "*Default value of `print-level' to use while printing results in Edebug."
255 :type 'integer
256 :group 'edebug)
257(defcustom edebug-print-circle t
258 "*Default value of `print-circle' to use while printing results in Edebug."
259 :type 'boolean
260 :group 'edebug)
261
262(defcustom edebug-unwrap-results nil
1fe3d507
DL
263 "*Non-nil if Edebug should unwrap results of expressions.
264This is useful when debugging macros where the results of expressions
265are instrumented expressions. But don't do this when results might be
c5292bc8
RS
266circular or an infinite loop will result."
267 :type 'boolean
268 :group 'edebug)
1fe3d507 269
c5292bc8 270(defcustom edebug-on-error t
1fe3d507
DL
271 "*Value bound to `debug-on-error' while Edebug is active.
272
273If `debug-on-error' is non-nil, that value is still used.
274
275If the value is a list of signal names, Edebug will stop when any of
276these errors are signaled from Lisp code whether or not the signal is
277handled by a `condition-case'. This option is useful for debugging
278signals that *are* handled since they would otherwise be missed.
c5292bc8
RS
279After execution is resumed, the error is signaled again."
280 :type '(choice boolean (repeat string))
281 :group 'edebug)
1fe3d507 282
c5292bc8
RS
283(defcustom edebug-on-quit t
284 "*Value bound to `debug-on-quit' while Edebug is active."
285 :type 'boolean
286 :group 'edebug)
1fe3d507 287
c5292bc8 288(defcustom edebug-global-break-condition nil
a50d4326 289 "*If non-nil, an expression to test for at every stop point.
c5292bc8
RS
290If the result is non-nil, then break. Errors are ignored."
291 :type 'sexp
292 :group 'edebug)
a50d4326 293
f7359658 294;;; Form spec utilities.
1fe3d507
DL
295
296;;;###autoload
297(defmacro def-edebug-spec (symbol spec)
298 "Set the edebug-form-spec property of SYMBOL according to SPEC.
299Both SYMBOL and SPEC are unevaluated. The SPEC can be 0, t, a symbol
f7359658 300\(naming a function), or a list."
1fe3d507
DL
301 (` (put (quote (, symbol)) 'edebug-form-spec (quote (, spec)))))
302
303(defmacro def-edebug-form-spec (symbol spec-form)
f7359658 304 "For compatibility with old version. Use `def-edebug-spec' instead."
1fe3d507
DL
305 (message "Obsolete: use def-edebug-spec instead.")
306 (def-edebug-spec symbol (eval spec-form)))
307
308(defun get-edebug-spec (symbol)
309 ;; Get the spec of symbol resolving all indirection.
310 (let ((edebug-form-spec (get symbol 'edebug-form-spec))
311 indirect)
312 (while (and (symbolp edebug-form-spec)
313 (setq indirect (get edebug-form-spec 'edebug-form-spec)))
314 ;; (edebug-trace "indirection: %s" edebug-form-spec)
315 (setq edebug-form-spec indirect))
316 edebug-form-spec
317 ))
318
f7359658 319;;; Utilities
1fe3d507 320
f7359658
RS
321;; Define edebug-gensym - from old cl.el
322(defvar edebug-gensym-index 0
323 "Integer used by `edebug-gensym' to produce new names.")
1fe3d507 324
f7359658 325(defun edebug-gensym (&optional prefix)
1fe3d507
DL
326 "Generate a fresh uninterned symbol.
327There is an optional argument, PREFIX. PREFIX is the
328string that begins the new name. Most people take just the default,
329except when debugging needs suggest otherwise."
330 (if (null prefix)
331 (setq prefix "G"))
332 (let ((newsymbol nil)
333 (newname ""))
334 (while (not newsymbol)
f7359658
RS
335 (setq newname (concat prefix (int-to-string edebug-gensym-index)))
336 (setq edebug-gensym-index (+ edebug-gensym-index 1))
1fe3d507
DL
337 (if (not (intern-soft newname))
338 (setq newsymbol (make-symbol newname))))
339 newsymbol))
1fe3d507 340
a50d4326 341;; Only used by CL-like code.
f7359658
RS
342(defun edebug-keywordp (object)
343 "Return t if OBJECT is a keyword.
344A keyword is a symbol that starts with `:'."
345 (and (symbolp object)
346 (= ?: (aref (symbol-name object) 0))))
347
348(defun edebug-lambda-list-keywordp (object)
1fe3d507 349 "Return t if OBJECT is a lambda list keyword.
f7359658 350A lambda list keyword is a symbol that starts with `&'."
1fe3d507
DL
351 (and (symbolp object)
352 (= ?& (aref (symbol-name object) 0))))
84fc2cfa 353
84fc2cfa
ER
354
355(defun edebug-last-sexp ()
1fe3d507 356 ;; Return the last sexp before point in current buffer.
f7359658 357 ;; Assumes Emacs Lisp syntax is active.
84fc2cfa
ER
358 (car
359 (read-from-string
360 (buffer-substring
361 (save-excursion
362 (forward-sexp -1)
363 (point))
364 (point)))))
365
366(defun edebug-window-list ()
f7359658
RS
367 "Return a list of windows, in order of `next-window'."
368 ;; This doesn't work for epoch.
84fc2cfa
ER
369 (let* ((first-window (selected-window))
370 (window-list (list first-window))
371 (next (next-window first-window)))
372 (while (not (eq next first-window))
373 (setq window-list (cons next window-list))
374 (setq next (next-window next)))
375 (nreverse window-list)))
376
1fe3d507
DL
377(defun edebug-window-live-p (window)
378 "Return non-nil if WINDOW is visible."
379 (let* ((first-window (selected-window))
380 (next (next-window first-window t)))
381 (while (not (or (eq next window)
382 (eq next first-window)))
383 (setq next (next-window next t)))
384 (eq next window)))
385
386;; Not used.
387'(defun edebug-two-window-p ()
84fc2cfa
ER
388 "Return t if there are two windows."
389 (and (not (one-window-p))
390 (eq (selected-window)
391 (next-window (next-window (selected-window))))))
392
1fe3d507 393(defsubst edebug-lookup-function (object)
84fc2cfa
ER
394 (while (and (symbolp object) (fboundp object))
395 (setq object (symbol-function object)))
1fe3d507
DL
396 object)
397
398(defun edebug-macrop (object)
399 "Return the macro named by OBJECT, or nil if it is not a macro."
400 (setq object (edebug-lookup-function object))
84fc2cfa
ER
401 (if (and (listp object)
402 (eq 'macro (car object))
403 (edebug-functionp (cdr object)))
404 object))
405
406(defun edebug-functionp (object)
407 "Returns the function named by OBJECT, or nil if it is not a function."
1fe3d507 408 (setq object (edebug-lookup-function object))
84fc2cfa 409 (if (or (subrp object)
f7359658 410 (byte-code-function-p object)
84fc2cfa
ER
411 (and (listp object)
412 (eq (car object) 'lambda)
413 (listp (car (cdr object)))))
414 object))
415
416(defun edebug-sort-alist (alist function)
1fe3d507
DL
417 ;; Return the ALIST sorted with comparison function FUNCTION.
418 ;; This uses 'sort so the sorting is destructive.
84fc2cfa
ER
419 (sort alist (function
420 (lambda (e1 e2)
421 (funcall function (car e1) (car e2))))))
422
1fe3d507 423;;(def-edebug-spec edebug-save-restriction t)
84fc2cfa 424
1fe3d507
DL
425;; Not used. If it is used, def-edebug-spec must be defined before use.
426'(defmacro edebug-save-restriction (&rest body)
84fc2cfa
ER
427 "Evaluate BODY while saving the current buffers restriction.
428BODY may change buffer outside of current restriction, unlike
429save-restriction. BODY may change the current buffer,
430and the restriction will be restored to the original buffer,
431and the current buffer remains current.
432Return the result of the last expression in BODY."
433 (` (let ((edebug:s-r-beg (point-min-marker))
434 (edebug:s-r-end (point-max-marker)))
435 (unwind-protect
436 (progn (,@ body))
437 (save-excursion
438 (set-buffer (marker-buffer edebug:s-r-beg))
439 (narrow-to-region edebug:s-r-beg edebug:s-r-end))))))
440
f7359658 441;;; Display
1fe3d507
DL
442
443(defconst edebug-trace-buffer "*edebug-trace*"
444 "Name of the buffer to put trace info in.")
445
446(defun edebug-pop-to-buffer (buffer &optional window)
447 ;; Like pop-to-buffer, but select window where BUFFER was last shown.
448 ;; Select WINDOW if it provided and it still exists. Otherwise,
449 ;; if buffer is currently shown in several windows, choose one.
450 ;; Otherwise, find a new window, possibly splitting one.
451 (setq window (if (and (windowp window) (edebug-window-live-p window)
452 (eq (window-buffer window) buffer))
453 window
454 (if (eq (window-buffer (selected-window)) buffer)
455 (selected-window)
456 (edebug-get-buffer-window buffer))))
457 (if window
458 (select-window window)
459 (if (one-window-p)
460 (split-window))
461 ;; (message "next window: %s" (next-window)) (sit-for 1)
462 (if (eq (get-buffer-window edebug-trace-buffer) (next-window))
f7359658 463 ;; Don't select trace window
1fe3d507
DL
464 nil
465 (select-window (next-window))))
466 (set-window-buffer (selected-window) buffer)
467 (set-window-hscroll (selected-window) 0);; should this be??
468 ;; Selecting the window does not set the buffer until command loop.
469 ;;(set-buffer buffer)
470 )
84fc2cfa 471
1fe3d507
DL
472
473(defun edebug-get-displayed-buffer-points ()
474 ;; Return a list of buffer point pairs, for all displayed buffers.
475 (save-excursion
476 (let* ((first-window (selected-window))
477 (next (next-window first-window))
478 (buffer-point-list nil)
479 buffer)
480 (while (not (eq next first-window))
481 (set-buffer (setq buffer (window-buffer next)))
482 (setq buffer-point-list
483 (cons (cons buffer (point)) buffer-point-list))
484 (setq next (next-window next)))
485 buffer-point-list)))
486
487
488(defun edebug-set-buffer-points (buffer-points)
489 ;; Restore the buffer-points created by edebug-get-displayed-buffer-points.
490 (let ((current-buffer (current-buffer)))
491 (mapcar (function (lambda (buf-point)
492 (if (buffer-name (car buf-point)) ; still exists
493 (progn
494 (set-buffer (car buf-point))
495 (goto-char (cdr buf-point))))))
496 buffer-points)
497 (set-buffer current-buffer)))
498
499(defun edebug-current-windows (which-windows)
500 ;; Get either a full window configuration or some window information.
501 (if (listp which-windows)
502 (mapcar (function (lambda (window)
503 (if (edebug-window-live-p window)
504 (list window
505 (window-buffer window)
506 (window-point window)
507 (window-start window)
508 (window-hscroll window)))))
509 which-windows)
510 (current-window-configuration)))
511
512(defun edebug-set-windows (window-info)
513 ;; Set either a full window configuration or some window information.
514 (if (listp window-info)
515 (mapcar (function
516 (lambda (one-window-info)
517 (if one-window-info
518 (apply (function
519 (lambda (window buffer point start hscroll)
520 (if (edebug-window-live-p window)
521 (progn
522 (set-window-buffer window buffer)
523 (set-window-point window point)
524 (set-window-start window start)
525 (set-window-hscroll window hscroll)))))
526 one-window-info))))
527 window-info)
528 (set-window-configuration window-info)))
529
530(defalias 'edebug-get-buffer-window 'get-buffer-window)
531(defalias 'edebug-sit-for 'sit-for)
532(defalias 'edebug-input-pending-p 'input-pending-p)
533
534
f7359658
RS
535;;; Redefine read and eval functions
536;; read is redefined to maybe instrument forms.
537;; eval-defun is redefined to check edebug-all-forms and edebug-all-defs.
1fe3d507 538
f7359658
RS
539;; Use the Lisp version of eval-region.
540(require 'eval-reg "eval-reg")
1fe3d507
DL
541
542;; Save the original read function
543(or (fboundp 'edebug-original-read)
544 (defalias 'edebug-original-read (symbol-function 'read)))
545
88668147
DL
546(defun edebug-read (&optional stream)
547 "Read one Lisp expression as text from STREAM, return as Lisp object.
548If STREAM is nil, use the value of `standard-input' (which see).
549STREAM or the value of `standard-input' may be:
550 a buffer (read from point and advance it)
551 a marker (read from where it points and advance it)
552 a function (call it with no arguments for each character,
553 call it with a char as argument to push a char back)
554 a string (takes text from string, starting at the beginning)
555 t (read text line using minibuffer and use it).
556
557This version, from Edebug, maybe instruments the expression. But the
f7359658 558STREAM must be the current buffer to do so. Whether it instruments is
88668147 559also dependent on the values of `edebug-all-defs' and
1fe3d507 560`edebug-all-forms'."
88668147
DL
561 (or stream (setq stream standard-input))
562 (if (eq stream (current-buffer))
1fe3d507
DL
563 (edebug-read-and-maybe-wrap-form)
564 (edebug-original-read stream)))
565
1fe3d507
DL
566(or (fboundp 'edebug-original-eval-defun)
567 (defalias 'edebug-original-eval-defun (symbol-function 'eval-defun)))
568
f7359658
RS
569;; We should somehow arrange to be able to do this
570;; without actually replacing the eval-defun command.
1fe3d507
DL
571(defun edebug-eval-defun (edebug-it)
572 "Evaluate the top-level form containing point, or after point.
573
574This version, from Edebug, has the following differences: With a
88668147 575prefix argument instrument the code for Edebug. If `edebug-all-defs' is
1fe3d507 576non-nil, then the code is instrumented *unless* there is a prefix
f7359658 577argument. If instrumenting, it prints: `Edebug: FUNCTIONNAME'.
1fe3d507
DL
578Otherwise, it prints in the minibuffer."
579 (interactive "P")
f7359658
RS
580 (let* ((edebugging (not (eq (not edebug-it) (not edebug-all-defs))))
581 (edebug-result)
582 (form
583 (let ((edebug-all-forms edebugging)
584 (edebug-all-defs (eq edebug-all-defs (not edebug-it))))
585 (edebug-read-top-level-form))))
586 (if (and (eq (car form) 'defvar)
587 (cdr-safe (cdr-safe form)))
588 (setq form (cons 'defconst (cdr form))))
589 (setq edebug-result (eval form))
1fe3d507
DL
590 (if (not edebugging)
591 (princ edebug-result)
592 edebug-result)))
593
594
595;;;###autoload
596(defalias 'edebug-defun 'edebug-eval-top-level-form)
597
598;;;###autoload
599(defun edebug-eval-top-level-form ()
600 "Evaluate a top level form, such as a defun or defmacro.
601This is like `eval-defun', but the code is always instrumented for Edebug.
602Print its name in the minibuffer and leave point where it is,
f7359658 603or if an error occurs, leave point after it with mark at the original point."
84fc2cfa 604 (interactive)
1fe3d507 605 (eval
f7359658 606 ;; Bind edebug-all-forms only while reading, not while evalling
1fe3d507 607 ;; but this causes problems while edebugging edebug.
88668147
DL
608 (let ((edebug-all-forms t)
609 (edebug-all-defs t))
1fe3d507 610 (edebug-read-top-level-form))))
84fc2cfa
ER
611
612
1fe3d507
DL
613(defun edebug-read-top-level-form ()
614 (let ((starting-point (point)))
615 (end-of-defun)
616 (beginning-of-defun)
617 (prog1
618 (edebug-read-and-maybe-wrap-form)
619 ;; Recover point, but only if no error occurred.
620 (goto-char starting-point))))
84fc2cfa 621
84fc2cfa 622
1fe3d507
DL
623;; Compatibility with old versions.
624(defalias 'edebug-all-defuns 'edebug-all-defs)
84fc2cfa 625
1fe3d507
DL
626(defun edebug-all-defs ()
627 "Toggle edebugging of all definitions."
628 (interactive)
629 (setq edebug-all-defs (not edebug-all-defs))
630 (message "Edebugging all definitions is %s."
631 (if edebug-all-defs "on" "off")))
84fc2cfa 632
84fc2cfa 633
1fe3d507
DL
634(defun edebug-all-forms ()
635 "Toggle edebugging of all forms."
636 (interactive)
637 (setq edebug-all-forms (not edebug-all-forms))
638 (message "Edebugging all forms is %s."
639 (if edebug-all-forms "on" "off")))
84fc2cfa
ER
640
641
1fe3d507 642(defun edebug-install-read-eval-functions ()
1c222bca 643 (interactive)
88668147 644 ;; Don't install if already installed.
f7359658 645 (if (eq (symbol-function 'read) 'edebug-read) nil
88668147
DL
646 (elisp-eval-region-install)
647 (defalias 'read 'edebug-read)
648 (defalias 'eval-defun 'edebug-eval-defun)))
daa37602 649
1fe3d507
DL
650(defun edebug-uninstall-read-eval-functions ()
651 (interactive)
88668147
DL
652 (elisp-eval-region-uninstall)
653 (defalias 'read (symbol-function 'edebug-original-read))
654 (defalias 'eval-defun (symbol-function 'edebug-original-eval-defun)))
1fe3d507
DL
655
656
f7359658 657;;; Edebug internal data
1fe3d507 658
f7359658
RS
659;; The internal data that is needed for edebugging is kept in the
660;; buffer-local variable `edebug-form-data'.
1fe3d507
DL
661
662(make-variable-buffer-local 'edebug-form-data)
663
664(defconst edebug-form-data nil)
665;; A list of entries associating symbols with buffer regions.
666;; This is an automatic buffer local variable. Each entry looks like:
667;; @code{(@var{symbol} @var{begin-marker} @var{end-marker}). The markers
668;; are at the beginning and end of an entry level form and @var{symbol} is
669;; a symbol that holds all edebug related information for the form on its
670;; property list.
671
672;; In the future, the symbol will be irrelevant and edebug data will
673;; be stored in the definitions themselves rather than in the property
674;; list of a symbol.
675
676(defun edebug-make-form-data-entry (symbol begin end)
677 (list symbol begin end))
678
679(defsubst edebug-form-data-name (entry)
680 (car entry))
681
682(defsubst edebug-form-data-begin (entry)
683 (nth 1 entry))
684
685(defsubst edebug-form-data-end (entry)
686 (nth 2 entry))
687
688(defsubst edebug-set-form-data-entry (entry name begin end)
689 (setcar entry name);; in case name is changed
690 (set-marker (nth 1 entry) begin)
691 (set-marker (nth 2 entry) end))
692
693(defun edebug-get-form-data-entry (pnt &optional end-point)
694 ;; Find the edebug form data entry which is closest to PNT.
695 ;; If END-POINT is supplied, match must be exact.
696 ;; Return `nil' if none found.
697 (let ((rest edebug-form-data)
698 closest-entry
699 (closest-dist 999999)) ;; need maxint here
700 (while (and rest (< 0 closest-dist))
701 (let* ((entry (car rest))
702 (begin (edebug-form-data-begin entry))
703 (dist (- pnt begin)))
704 (setq rest (cdr rest))
705 (if (and (<= 0 dist)
706 (< dist closest-dist)
707 (or (not end-point)
708 (= end-point (edebug-form-data-end entry)))
709 (<= pnt (edebug-form-data-end entry)))
710 (setq closest-dist dist
711 closest-entry entry))))
712 closest-entry))
713
714;; Also need to find all contained entries,
715;; and find an entry given a symbol, which should be just assq.
716
717(defun edebug-form-data-symbol ()
718;; Return the edebug data symbol of the form where point is in.
719;; If point is not inside a edebuggable form, cause error.
720 (or (edebug-form-data-name (edebug-get-form-data-entry (point)))
721 (error "Not inside instrumented form")))
722
723(defun edebug-make-top-form-data-entry (new-entry)
724 ;; Make NEW-ENTRY the first element in the `edebug-form-data' list.
725 (edebug-clear-form-data-entry new-entry)
726 (setq edebug-form-data (cons new-entry edebug-form-data)))
727
728(defun edebug-clear-form-data-entry (entry)
729;; If non-nil, clear ENTRY out of the form data.
730;; Maybe clear the markers and delete the symbol's edebug property?
731 (if entry
732 (progn
733 ;; Instead of this, we could just find all contained forms.
734 ;; (put (car entry) 'edebug nil) ;
735 ;; (mapcar 'edebug-clear-form-data-entry ; dangerous
736 ;; (get (car entry) 'edebug-dependents))
737 ;; (set-marker (nth 1 entry) nil)
738 ;; (set-marker (nth 2 entry) nil)
739 (setq edebug-form-data (delq entry edebug-form-data)))))
daa37602 740
f7359658 741;;; Parser utilities
1fe3d507
DL
742
743(defun edebug-syntax-error (&rest args)
744 ;; Signal an invalid-read-syntax with ARGS.
745 (signal 'invalid-read-syntax args))
746
84fc2cfa 747
1fe3d507
DL
748(defconst edebug-read-syntax-table
749 ;; Lookup table for significant characters indicating the class of the
750 ;; token that follows. This is not a \"real\" syntax table.
751 (let ((table (make-vector 256 'symbol))
752 (i 0))
753 (while (< i ?!)
754 (aset table i 'space)
755 (setq i (1+ i)))
756 (aset table ?\( 'lparen)
757 (aset table ?\) 'rparen)
758 (aset table ?\' 'quote)
f7359658
RS
759 (aset table ?\` 'backquote)
760 (aset table ?\, 'comma)
1fe3d507
DL
761 (aset table ?\" 'string)
762 (aset table ?\? 'char)
763 (aset table ?\[ 'lbracket)
764 (aset table ?\] 'rbracket)
765 (aset table ?\. 'dot)
766 (aset table ?\# 'hash)
767 ;; We treat numbers as symbols, because of confusion with -, -1, and 1-.
f7359658 768 ;; We don't care about any other chars since they won't be seen.
1fe3d507 769 table))
84fc2cfa 770
1fe3d507
DL
771(defun edebug-next-token-class ()
772 ;; Move to the next token and return its class. We only care about
f7359658
RS
773 ;; lparen, rparen, dot, quote, backquote, comma, string, char, vector,
774 ;; or symbol.
1fe3d507
DL
775 (edebug-skip-whitespace)
776 (aref edebug-read-syntax-table (following-char)))
84fc2cfa 777
84fc2cfa 778
1fe3d507
DL
779(defun edebug-skip-whitespace ()
780 ;; Leave point before the next token, skipping white space and comments.
781 (skip-chars-forward " \t\r\n\f")
782 (while (= (following-char) ?\;)
783 ;; \r is counted as a comment terminator to support selective display.
784 (skip-chars-forward "^\n\r") ; skip the comment
785 (skip-chars-forward " \t\r\n\f")))
84fc2cfa 786
84fc2cfa 787
1fe3d507 788;; Mostly obsolete reader; still used in one case.
84fc2cfa 789
1fe3d507
DL
790(defun edebug-read-sexp ()
791 ;; Read one sexp from the current buffer starting at point.
792 ;; Leave point immediately after it. A sexp can be a list or atom.
793 ;; An atom is a symbol (or number), character, string, or vector.
794 ;; This works for reading anything legitimate, but it
795 ;; is gummed up by parser inconsistencies (bugs?)
796 (let ((class (edebug-next-token-class)))
797 (cond
798 ;; read goes one too far if a (possibly quoted) string or symbol
799 ;; is immediately followed by non-whitespace.
f4897e40
RS
800 ((eq class 'symbol) (edebug-original-read (current-buffer)))
801 ((eq class 'string) (edebug-original-read (current-buffer)))
1fe3d507
DL
802 ((eq class 'quote) (forward-char 1)
803 (list 'quote (edebug-read-sexp)))
f7359658
RS
804 ((eq class 'backquote)
805 (list '\` (edebug-read-sexp)))
806 ((eq class 'comma)
807 (list '\, (edebug-read-sexp)))
1fe3d507
DL
808 (t ; anything else, just read it.
809 (edebug-original-read (current-buffer))))))
810
f7359658 811;;; Offsets for reader
1fe3d507
DL
812
813;; Define a structure to represent offset positions of expressions.
814;; Each offset structure looks like: (before . after) for constituents,
815;; or for structures that have elements: (before <subexpressions> . after)
816;; where the <subexpressions> are the offset structures for subexpressions
817;; including the head of a list.
818(defconst edebug-offsets nil)
819
820;; Stack of offset structures in reverse order of the nesting.
821;; This is used to get back to previous levels.
822(defconst edebug-offsets-stack nil)
823(defconst edebug-current-offset nil) ; Top of the stack, for convenience.
824
825;; We must store whether we just read a list with a dotted form that
826;; is itself a list. This structure will be condensed, so the offsets
827;; must also be condensed.
828(defconst edebug-read-dotted-list nil)
829
830(defsubst edebug-initialize-offsets ()
831 ;; Reinitialize offset recording.
832 (setq edebug-current-offset nil))
833
834(defun edebug-store-before-offset (point)
835 ;; Add a new offset pair with POINT as the before offset.
836 (let ((new-offset (list point)))
837 (if edebug-current-offset
838 (setcdr edebug-current-offset
839 (cons new-offset (cdr edebug-current-offset)))
840 ;; Otherwise, we are at the top level, so initialize.
841 (setq edebug-offsets new-offset
842 edebug-offsets-stack nil
843 edebug-read-dotted-list nil))
844 ;; Cons the new offset to the front of the stack.
845 (setq edebug-offsets-stack (cons new-offset edebug-offsets-stack)
846 edebug-current-offset new-offset)
84fc2cfa
ER
847 ))
848
1fe3d507
DL
849(defun edebug-store-after-offset (point)
850 ;; Finalize the current offset struct by reversing it and
851 ;; store POINT as the after offset.
852 (if (not edebug-read-dotted-list)
853 ;; Just reverse the offsets of all subexpressions.
854 (setcdr edebug-current-offset (nreverse (cdr edebug-current-offset)))
855
856 ;; We just read a list after a dot, which will be abbreviated out.
857 (setq edebug-read-dotted-list nil)
858 ;; Drop the corresponding offset pair.
859 ;; That is, nconc the reverse of the rest of the offsets
860 ;; with the cdr of last offset.
861 (setcdr edebug-current-offset
862 (nconc (nreverse (cdr (cdr edebug-current-offset)))
863 (cdr (car (cdr edebug-current-offset))))))
864
865 ;; Now append the point using nconc.
866 (setq edebug-current-offset (nconc edebug-current-offset point))
867 ;; Pop the stack.
868 (setq edebug-offsets-stack (cdr edebug-offsets-stack)
869 edebug-current-offset (car edebug-offsets-stack)))
870
871(defun edebug-ignore-offset ()
872 ;; Ignore the last created offset pair.
873 (setcdr edebug-current-offset (cdr (cdr edebug-current-offset))))
874
875(def-edebug-spec edebug-storing-offsets (form body))
876(put 'edebug-storing-offsets 'lisp-indent-hook 1)
877
878(defmacro edebug-storing-offsets (point &rest body)
879 (` (unwind-protect
880 (progn
881 (edebug-store-before-offset (, point))
882 (,@ body))
883 (edebug-store-after-offset (point)))))
884
885
f7359658
RS
886;;; Reader for Emacs Lisp.
887
1fe3d507
DL
888;; Uses edebug-next-token-class (and edebug-skip-whitespace) above.
889
890(defconst edebug-read-alist
891 '((symbol . edebug-read-symbol)
892 (lparen . edebug-read-list)
893 (string . edebug-read-string)
894 (quote . edebug-read-quote)
f7359658
RS
895 (backquote . edebug-read-backquote)
896 (comma . edebug-read-comma)
1fe3d507
DL
897 (lbracket . edebug-read-vector)
898 (hash . edebug-read-function)
899 ))
84fc2cfa 900
1fe3d507
DL
901(defun edebug-read-storing-offsets (stream)
902 (let ((class (edebug-next-token-class))
903 func
904 edebug-read-dotted-list) ; see edebug-store-after-offset
905 (edebug-storing-offsets (point)
906 (if (setq func (assq class edebug-read-alist))
907 (funcall (cdr func) stream)
908 ;; anything else, just read it.
909 (edebug-original-read stream))
910 )))
84fc2cfa 911
1fe3d507 912(defun edebug-read-symbol (stream)
f4897e40 913 (edebug-original-read stream))
84fc2cfa 914
1fe3d507 915(defun edebug-read-string (stream)
f4897e40 916 (edebug-original-read stream))
84fc2cfa 917
1fe3d507
DL
918(defun edebug-read-quote (stream)
919 ;; Turn 'thing into (quote thing)
920 (forward-char 1)
921 (list
922 (edebug-storing-offsets (point) 'quote)
923 (edebug-read-storing-offsets stream)))
924
f7359658
RS
925(defun edebug-read-backquote (stream)
926 ;; Turn `thing into (\` thing)
927 (let ((opoint (point)))
928 (forward-char 1)
929 ;; Generate the same structure of offsets we would have
930 ;; if the resulting list appeared verbatim in the input text.
931 (edebug-storing-offsets opoint
932 (list
933 (edebug-storing-offsets opoint '\`)
934 (edebug-read-storing-offsets stream)))))
935
936(defvar edebug-read-backquote-new nil
937 "Non-nil if reading the inside of a new-style backquote with no parens around it.
938Value of nil means reading the inside of an old-style backquote construct
939which is surrounded by an extra set of parentheses.
940This controls how we read comma constructs.")
941
942(defun edebug-read-comma (stream)
943 ;; Turn ,thing into (\, thing). Handle ,@ and ,. also.
944 (let ((opoint (point)))
945 (forward-char 1)
946 (let ((symbol '\,))
947 (cond ((eq (following-char) ?\.)
948 (setq symbol '\,\.)
949 (forward-char 1))
950 ((eq (following-char) ?\@)
951 (setq symbol '\,@)
952 (forward-char 1)))
953 ;; Generate the same structure of offsets we would have
954 ;; if the resulting list appeared verbatim in the input text.
955 (if edebug-read-backquote-new
956 (list
957 (edebug-storing-offsets opoint symbol)
958 (edebug-read-storing-offsets stream))
959 (edebug-storing-offsets opoint symbol)))))
960
1fe3d507
DL
961(defun edebug-read-function (stream)
962 ;; Turn #'thing into (function thing)
963 (forward-char 1)
964 (if (/= ?\' (following-char)) (edebug-syntax-error "Bad char"))
965 (forward-char 1)
966 (list
967 (edebug-storing-offsets (point)
968 (if (featurep 'cl) 'function* 'function))
969 (edebug-read-storing-offsets stream)))
970
971(defun edebug-read-list (stream)
972 (forward-char 1) ; skip \(
973 (prog1
974 (let ((elements))
975 (while (not (memq (edebug-next-token-class) '(rparen dot)))
f7359658
RS
976 (if (eq (edebug-next-token-class) 'backquote)
977 (let ((edebug-read-backquote-new (not (null elements)))
978 (opoint (point)))
979 (if edebug-read-backquote-new
980 (setq elements (cons (edebug-read-backquote stream) elements))
981 (forward-char 1) ; Skip backquote.
982 ;; Call edebug-storing-offsets here so that we
983 ;; produce the same offsets we would have had
984 ;; if the backquote were an ordinary symbol.
985 (setq elements (cons (edebug-storing-offsets opoint '\`)
986 elements))))
987 (setq elements (cons (edebug-read-storing-offsets stream) elements))))
1fe3d507
DL
988 (setq elements (nreverse elements))
989 (if (eq 'dot (edebug-next-token-class))
990 (let (dotted-form)
991 (forward-char 1) ; skip \.
992 (setq dotted-form (edebug-read-storing-offsets stream))
993 elements (nconc elements dotted-form)
994 (if (not (eq (edebug-next-token-class) 'rparen))
995 (edebug-syntax-error "Expected `)'"))
996 (setq edebug-read-dotted-list (listp dotted-form))
997 ))
998 elements)
999 (forward-char 1) ; skip \)
1000 ))
84fc2cfa 1001
1fe3d507
DL
1002(defun edebug-read-vector (stream)
1003 (forward-char 1) ; skip \[
1004 (prog1
1005 (let ((elements))
1006 (while (not (eq 'rbracket (edebug-next-token-class)))
1007 (setq elements (cons (edebug-read-storing-offsets stream) elements)))
1008 (apply 'vector (nreverse elements)))
1009 (forward-char 1) ; skip \]
84fc2cfa
ER
1010 ))
1011
f7359658 1012;;; Cursors for traversal of list and vector elements with offsets.
1fe3d507
DL
1013
1014(defvar edebug-dotted-spec nil)
1015
1016(defun edebug-new-cursor (expressions offsets)
1017 ;; Return a new cursor for EXPRESSIONS with OFFSETS.
1018 (if (vectorp expressions)
1019 (setq expressions (append expressions nil)))
1020 (cons expressions offsets))
1021
1022(defsubst edebug-set-cursor (cursor expressions offsets)
1023 ;; Set the CURSOR's EXPRESSIONS and OFFSETS to the given.
1024 ;; Return the cursor.
1025 (setcar cursor expressions)
1026 (setcdr cursor offsets)
1027 cursor)
1028
1029'(defun edebug-copy-cursor (cursor)
1030 ;; Copy the cursor using the same object and offsets.
1031 (cons (car cursor) (cdr cursor)))
1032
1033(defsubst edebug-cursor-expressions (cursor)
1034 (car cursor))
1035(defsubst edebug-cursor-offsets (cursor)
1036 (cdr cursor))
1037
1038(defsubst edebug-empty-cursor (cursor)
1039 ;; Return non-nil if CURSOR is empty - meaning no more elements.
1040 (null (car cursor)))
1041
1042(defsubst edebug-top-element (cursor)
1043 ;; Return the top element at the cursor.
1044 ;; Assumes not empty.
1045 (car (car cursor)))
1046
1047(defun edebug-top-element-required (cursor &rest error)
1048 ;; Check if a dotted form is required.
1049 (if edebug-dotted-spec (edebug-no-match cursor "Dot expected."))
1050 ;; Check if there is at least one more argument.
1051 (if (edebug-empty-cursor cursor) (apply 'edebug-no-match cursor error))
1052 ;; Return that top element.
1053 (edebug-top-element cursor))
1054
1055(defsubst edebug-top-offset (cursor)
1056 ;; Return the top offset pair corresponding to the top element.
1057 (car (cdr cursor)))
1058
1059(defun edebug-move-cursor (cursor)
1060 ;; Advance and return the cursor to the next element and offset.
1061 ;; throw no-match if empty before moving.
1062 ;; This is a violation of the cursor encapsulation, but
1063 ;; there is plenty of that going on while matching.
1064 ;; The following test should always fail.
1065 (if (edebug-empty-cursor cursor)
1066 (edebug-no-match cursor "Not enough arguments."))
1067 (setcar cursor (cdr (car cursor)))
1068 (setcdr cursor (cdr (cdr cursor)))
1069 cursor)
1070
1071
1072(defun edebug-before-offset (cursor)
1073 ;; Return the before offset of the cursor.
1074 ;; If there is nothing left in the offsets,
1075 ;; return one less than the offset itself,
1076 ;; which is the after offset for a list.
1077 (let ((offset (edebug-cursor-offsets cursor)))
1078 (if (consp offset)
1079 (car (car offset))
1080 (1- offset))))
1081
1082(defun edebug-after-offset (cursor)
1083 ;; Return the after offset of the cursor object.
1084 (let ((offset (edebug-top-offset cursor)))
1085 (while (consp offset)
1086 (setq offset (cdr offset)))
1087 offset))
1088
f7359658 1089;;; The Parser
1fe3d507 1090
f7359658
RS
1091;; The top level function for parsing forms is
1092;; edebug-read-and-maybe-wrap-form; it calls all the rest. It checks the
1093;; syntax a bit and leaves point at any error it finds, but otherwise
1094;; should appear to work like eval-defun.
1fe3d507 1095
f7359658
RS
1096;; The basic plan is to surround each expression with a call to
1097;; the edebug debugger together with indexes into a table of positions of
1098;; all expressions. Thus an expression "exp" becomes:
1fe3d507 1099
f7359658 1100;; (edebug-after (edebug-before 1) 2 exp)
1fe3d507 1101
f7359658
RS
1102;; When this is evaluated, first point is moved to the beginning of
1103;; exp at offset 1 of the current function. The expression is
1104;; evaluated, which may cause more edebug calls, and then point is
1105;; moved to offset 2 after the end of exp.
1fe3d507 1106
f7359658
RS
1107;; The highest level expressions of the function are wrapped in a call to
1108;; edebug-enter, which supplies the function name and the actual
1109;; arguments to the function. See functions edebug-enter, edebug-before,
1110;; and edebug-after for more details.
1fe3d507
DL
1111
1112;; Dynamically bound vars, left unbound, but globally declared.
1113;; This is to quiet the byte compiler.
1114
1115;; Window data of the highest definition being wrapped.
1116;; This data is shared by all embedded definitions.
1117(defvar edebug-top-window-data)
1118
1119(defvar edebug-&optional)
1120(defvar edebug-&rest)
1121(defvar edebug-gate nil) ;; whether no-match forces an error.
1122
1123(defconst edebug-def-name nil) ; name of definition, used by interactive-form
1124(defconst edebug-old-def-name nil) ; previous name of containing definition.
1125
1126(defconst edebug-error-point nil)
1127(defconst edebug-best-error nil)
1128
1129
1130(defun edebug-read-and-maybe-wrap-form ()
1131 ;; Read a form and wrap it with edebug calls, if the conditions are right.
1132 ;; Here we just catch any no-match not caught below and signal an error.
1133
1134 ;; Run the setup hook.
88b52bf5
RS
1135 ;; If it gets an error, make it nil.
1136 (let ((temp-hook edebug-setup-hook))
1137 (setq edebug-setup-hook nil)
1138 (run-hooks 'temp-hook))
1fe3d507
DL
1139
1140 (let (result
1141 edebug-top-window-data
1142 edebug-def-name;; make sure it is locally nil
f7359658 1143 ;; I don't like these here!!
1fe3d507
DL
1144 edebug-&optional
1145 edebug-&rest
1146 edebug-gate
1147 edebug-best-error
1148 edebug-error-point
1149 no-match
1150 ;; Do this once here instead of several times.
1151 (max-lisp-eval-depth (+ 800 max-lisp-eval-depth))
f7359658 1152 (max-specpdl-size (+ 2000 max-specpdl-size)))
1fe3d507
DL
1153 (setq no-match
1154 (catch 'no-match
1155 (setq result (edebug-read-and-maybe-wrap-form1))
1156 nil))
1157 (if no-match
1158 (apply 'edebug-syntax-error no-match))
1159 result))
1160
1161
1162(defun edebug-read-and-maybe-wrap-form1 ()
1163 (let (spec
1164 def-kind
1165 defining-form-p
1166 def-name
f7359658 1167 ;; These offset things don't belong here, but to support recursive
1fe3d507
DL
1168 ;; calls to edebug-read, they need to be here.
1169 edebug-offsets
1170 edebug-offsets-stack
1171 edebug-current-offset ; reset to nil
1172 )
1173 (save-excursion
1174 (if (and (eq 'lparen (edebug-next-token-class))
1175 (eq 'symbol (progn (forward-char 1) (edebug-next-token-class))))
1176 ;; Find out if this is a defining form from first symbol
88668147 1177 (setq def-kind (edebug-original-read (current-buffer))
1fe3d507
DL
1178 spec (and (symbolp def-kind) (get-edebug-spec def-kind))
1179 defining-form-p (and (listp spec)
1180 (eq '&define (car spec)))
1181 ;; This is incorrect in general!! But OK most of the time.
1182 def-name (if (and defining-form-p
1183 (eq 'name (car (cdr spec)))
1184 (eq 'symbol (edebug-next-token-class)))
88668147
DL
1185 (edebug-original-read (current-buffer))))))
1186;;;(message "all defs: %s all forms: %s" edebug-all-defs edebug-all-forms)
84fc2cfa 1187 (cond
1fe3d507
DL
1188 (defining-form-p
1189 (if (or edebug-all-defs edebug-all-forms)
1190 ;; If it is a defining form and we are edebugging defs,
1191 ;; then let edebug-list-form start it.
1192 (let ((cursor (edebug-new-cursor
1193 (list (edebug-read-storing-offsets (current-buffer)))
1194 (list edebug-offsets))))
1195 (car
1196 (edebug-make-form-wrapper
1197 cursor
1198 (edebug-before-offset cursor)
1199 (1- (edebug-after-offset cursor))
1200 (list (cons (symbol-name def-kind) (cdr spec))))))
1201
1202 ;; Not edebugging this form, so reset the symbol's edebug
1203 ;; property to be just a marker at the definition's source code.
1204 ;; This only works for defs with simple names.
1205 (put def-name 'edebug (point-marker))
1206 ;; Also nil out dependent defs.
1207 '(mapcar (function
1208 (lambda (def)
1209 (put def-name 'edebug nil)))
1210 (get def-name 'edebug-dependents))
1211 (edebug-read-sexp)))
1212
1213 ;; If all forms are being edebugged, explicitly wrap it.
1214 (edebug-all-forms
1215 (let ((cursor (edebug-new-cursor
1216 (list (edebug-read-storing-offsets (current-buffer)))
1217 (list edebug-offsets))))
1218 (edebug-make-form-wrapper
1219 cursor
1220 (edebug-before-offset cursor)
1221 (edebug-after-offset cursor)
1222 nil)))
1223
1224 ;; Not a defining form, and not edebugging.
1225 (t (edebug-read-sexp)))
1226 ))
1227
1228
1229(defvar edebug-def-args) ; args of defining form.
1230(defvar edebug-def-interactive) ; is it an emacs interactive function?
1231(defvar edebug-inside-func) ;; whether code is inside function context.
1232;; Currently def-form sets this to nil; def-body sets it to t.
1233
1234(defun edebug-interactive-p-name ()
1235 ;; Return a unique symbol for the variable used to store the
1236 ;; status of interactive-p for this function.
1237 (intern (format "edebug-%s-interactive-p" edebug-def-name)))
1238
1239
1240(defun edebug-wrap-def-body (forms)
1241 "Wrap the FORMS of a definition body."
1242 (if edebug-def-interactive
1243 (` (let (((, (edebug-interactive-p-name))
1244 (interactive-p)))
1245 (, (edebug-make-enter-wrapper forms))))
1246 (edebug-make-enter-wrapper forms)))
1247
1248
1249(defun edebug-make-enter-wrapper (forms)
1250 ;; Generate the enter wrapper for some forms of a definition.
1251 ;; This is not to be used for the body of other forms, e.g. `while',
1252 ;; since it wraps the list of forms with a call to `edebug-enter'.
1253 ;; Uses the dynamically bound vars edebug-def-name and edebug-def-args.
1254 ;; Do this after parsing since that may find a name.
1255 (setq edebug-def-name
f7359658 1256 (or edebug-def-name edebug-old-def-name (edebug-gensym "edebug-anon")))
1fe3d507
DL
1257 (` (edebug-enter
1258 (quote (, edebug-def-name))
1259 (, (if edebug-inside-func
1260 (` (list (,@
f7359658 1261 ;; Doesn't work with more than one def-body!!
1fe3d507
DL
1262 ;; But the list will just be reversed.
1263 (nreverse edebug-def-args))))
1264 'nil))
1265 (function (lambda () (,@ forms)))
1266 )))
1267
1268
1269(defvar edebug-form-begin-marker) ; the mark for def being instrumented
1270
1271(defvar edebug-offset-index) ; the next available offset index.
1272(defvar edebug-offset-list) ; the list of offset positions.
1273
1274(defun edebug-inc-offset (offset)
1275 ;; modifies edebug-offset-index and edebug-offset-list
1276 ;; accesses edebug-func-marc and buffer point
1277 (prog1
1278 edebug-offset-index
1279 (setq edebug-offset-list (cons (- offset edebug-form-begin-marker)
1280 edebug-offset-list)
1281 edebug-offset-index (1+ edebug-offset-index))))
1282
1283
1284(defun edebug-make-before-and-after-form (before-index form after-index)
1285 ;; Return the edebug form for the current function at offset BEFORE-INDEX
1286 ;; given FORM. Looks like:
1287 ;; (edebug-after (edebug-before BEFORE-INDEX) AFTER-INDEX FORM)
1288 ;; Also increment the offset index for subsequent use.
1289 ;; if (not edebug-stop-before-symbols) and form is a symbol,
f7359658 1290 ;; then don't call edebug-before.
1fe3d507
DL
1291 (list 'edebug-after
1292 (list 'edebug-before before-index)
1293 after-index form))
1294
1295(defun edebug-make-after-form (form after-index)
1296 ;; Like edebug-make-before-and-after-form, but only after.
1297 (list 'edebug-after 0 after-index form))
1298
1299
1300(defun edebug-unwrap (sexp)
1301 "Return the unwrapped SEXP or return it as is if it is not wrapped.
1302The SEXP might be the result of wrapping a body, which is a list of
1303expressions; a `progn' form will be returned enclosing these forms."
1304 (if (consp sexp)
1305 (cond
1306 ((eq 'edebug-after (car sexp))
1307 (nth 3 sexp))
1308 ((eq 'edebug-enter (car sexp))
1309 (let ((forms (nthcdr 2 (nth 1 (nth 3 sexp)))))
1310 (if (> (length forms) 1)
1311 (cons 'progn forms) ;; could return (values forms) instead.
1312 (car forms))))
1313 (t sexp);; otherwise it is not wrapped, so just return it.
1314 )
1315 sexp))
1316
1317(defun edebug-unwrap* (sexp)
1318 "Return the sexp recursively unwrapped."
1319 (let ((new-sexp (edebug-unwrap sexp)))
1320 (while (not (eq sexp new-sexp))
1321 (setq sexp new-sexp
1322 new-sexp (edebug-unwrap sexp)))
1323 (if (consp new-sexp)
1324 (mapcar 'edebug-unwrap* new-sexp)
1325 new-sexp)))
1326
1327
1328(defun edebug-defining-form (cursor form-begin form-end speclist)
1329 ;; Process the defining form, starting outside the form.
1330 ;; The speclist is a generated list spec that looks like:
1331 ;; (("def-symbol" defining-form-spec-sans-&define))
1332 ;; Skip the first offset.
1333 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1334 (cdr (edebug-cursor-offsets cursor)))
1335 (edebug-make-form-wrapper
1336 cursor
1337 form-begin (1- form-end)
1338 speclist))
1339
1340(defun edebug-make-form-wrapper (cursor form-begin form-end
1341 &optional speclist)
1342 ;; Wrap a form, usually a defining form, but any evaluated one.
1343 ;; If speclist is non-nil, this is being called by edebug-defining-form.
1344 ;; Otherwise it is being called from edebug-read-and-maybe-wrap-form1.
1345 ;; This is a hack, but I havent figured out a simpler way yet.
1346 (let* ((form-data-entry (edebug-get-form-data-entry form-begin form-end))
1347 ;; Set this marker before parsing.
1348 (edebug-form-begin-marker
1349 (if form-data-entry
1350 (edebug-form-data-begin form-data-entry)
1351 ;; Buffer must be current-buffer for this to work:
1352 (set-marker (make-marker) form-begin))))
1353
1354 (let (edebug-offset-list
1355 (edebug-offset-index 0)
1356 result
1357 ;; For definitions.
1358 ;; (edebug-containing-def-name edebug-def-name)
1359 ;; Get name from form-data, if any.
1360 (edebug-old-def-name (edebug-form-data-name form-data-entry))
1361 edebug-def-name
1362 edebug-def-args
1363 edebug-def-interactive
1364 edebug-inside-func;; whether wrapped code executes inside a function.
1365 )
84fc2cfa 1366
1fe3d507
DL
1367 (setq result
1368 (if speclist
1369 (edebug-match cursor speclist)
1370
1371 ;; else wrap as an enter-form.
1372 (edebug-make-enter-wrapper (list (edebug-form cursor)))))
84fc2cfa 1373
1fe3d507
DL
1374 ;; Set the name here if it was not set by edebug-make-enter-wrapper.
1375 (setq edebug-def-name
f7359658 1376 (or edebug-def-name edebug-old-def-name (edebug-gensym "edebug-anon")))
1fe3d507
DL
1377
1378 ;; Add this def as a dependent of containing def. Buggy.
1379 '(if (and edebug-containing-def-name
1380 (not (get edebug-containing-def-name 'edebug-dependents)))
1381 (put edebug-containing-def-name 'edebug-dependents
1382 (cons edebug-def-name
1383 (get edebug-containing-def-name
1384 'edebug-dependents))))
1385
1386 ;; Create a form-data-entry or modify existing entry's markers.
1387 ;; In the latter case, pointers to the entry remain eq.
1388 (if (not form-data-entry)
1389 (setq form-data-entry
1390 (edebug-make-form-data-entry
1391 edebug-def-name
1392 edebug-form-begin-marker
1393 ;; Buffer must be current-buffer.
1394 (set-marker (make-marker) form-end)
1395 ))
1396 (edebug-set-form-data-entry
1397 form-data-entry edebug-def-name ;; in case name is changed
1398 form-begin form-end))
1399
1400 ;; (message "defining: %s" edebug-def-name) (sit-for 2)
1401 (edebug-make-top-form-data-entry form-data-entry)
1402 (message "Edebug: %s" edebug-def-name)
1403 ;;(debug edebug-def-name)
1404
1405 ;; Destructively reverse edebug-offset-list and make vector from it.
1406 (setq edebug-offset-list (vconcat (nreverse edebug-offset-list)))
1407
1408 ;; Side effects on the property list of edebug-def-name.
1409 (edebug-clear-frequency-count edebug-def-name)
1410 (edebug-clear-coverage edebug-def-name)
1411
1412 ;; Set up the initial window data.
1413 (if (not edebug-top-window-data) ;; if not already set, do it now.
1414 (let ((window ;; Find the best window for this buffer.
1415 (or (get-buffer-window (current-buffer))
1416 (selected-window))))
1417 (setq edebug-top-window-data
1418 (cons window (window-start window)))))
1419
1420 ;; Store the edebug data in symbol's property list.
1421 (put edebug-def-name 'edebug
1422 ;; A struct or vector would be better here!!
1423 (list edebug-form-begin-marker
1424 nil ; clear breakpoints
1425 edebug-offset-list
1426 edebug-top-window-data
1427 ))
1428 result
84fc2cfa
ER
1429 )))
1430
1431
1fe3d507
DL
1432(defun edebug-clear-frequency-count (name)
1433 ;; Create initial frequency count vector.
1434 ;; For each stop point, the counter is incremented each time it is visited.
1435 (put name 'edebug-freq-count
1436 (make-vector (length edebug-offset-list) 0)))
1437
1438
1439(defun edebug-clear-coverage (name)
1440 ;; Create initial coverage vector.
1441 ;; Only need one per expression, but it is simpler to use stop points.
1442 (put name 'edebug-coverage
1443 (make-vector (length edebug-offset-list) 'unknown)))
84fc2cfa 1444
84fc2cfa 1445
1fe3d507
DL
1446(defun edebug-form (cursor)
1447 ;; Return the instrumented form for the following form.
1448 ;; Add the point offsets to the edebug-offset-list for the form.
1449 (let* ((form (edebug-top-element-required cursor "Expected form"))
1450 (offset (edebug-top-offset cursor)))
1451 (prog1
84fc2cfa 1452 (cond
1fe3d507
DL
1453 ((consp form)
1454 ;; The first offset for a list form is for the list form itself.
1455 (if (eq 'quote (car form))
1456 form
1457 (let* ((head (car form))
1458 (spec (and (symbolp head) (get-edebug-spec head)))
1459 (new-cursor (edebug-new-cursor form offset)))
1460 ;; Find out if this is a defining form from first symbol.
1461 ;; An indirect spec would not work here, yet.
1462 (if (and (consp spec) (eq '&define (car spec)))
1463 (edebug-defining-form
1464 new-cursor
1465 (car offset);; before the form
1466 (edebug-after-offset cursor)
1467 (cons (symbol-name head) (cdr spec)))
1468 ;; Wrap a regular form.
1469 (edebug-make-before-and-after-form
1470 (edebug-inc-offset (car offset))
1471 (edebug-list-form new-cursor)
1472 ;; After processing the list form, the new-cursor is left
1473 ;; with the offset after the form.
1474 (edebug-inc-offset (edebug-cursor-offsets new-cursor))))
1475 )))
1476
1477 ((symbolp form)
1478 (cond
f7359658 1479 ;; Check for constant symbols that don't get wrapped.
1fe3d507 1480 ((or (memq form '(t nil))
f7359658 1481 (and (fboundp 'edebug-keywordp) (edebug-keywordp form)))
1fe3d507
DL
1482 form)
1483
1484 ;; This option may go away.
1485 (edebug-stop-before-symbols
1486 (edebug-make-before-and-after-form
1487 (edebug-inc-offset (car offset))
1488 form
1489 (edebug-inc-offset (cdr offset))
1490 ))
1491
1492 (t ;; just a variable
1493 (edebug-make-after-form form (edebug-inc-offset (cdr offset))))))
1494
1495 ;; Anything else is self-evaluating.
1496 (t form))
1497 (edebug-move-cursor cursor))))
1498
1499
1500(defsubst edebug-forms (cursor) (edebug-match cursor '(&rest form)))
1501(defsubst edebug-sexps (cursor) (edebug-match cursor '(&rest sexp)))
1502
1503(defsubst edebug-list-form-args (head cursor)
1504 ;; Process the arguments of a list form given that head of form is a symbol.
1505 ;; Helper for edebug-list-form
1506 (let ((spec (get-edebug-spec head)))
1507 (cond
1508 (spec
1509 (cond
1510 ((consp spec)
1511 ;; It is a speclist.
1512 (let (edebug-best-error
1513 edebug-error-point);; This may not be needed.
1514 (edebug-match-sublist cursor spec)))
1515 ((eq t spec) (edebug-forms cursor))
1516 ((eq 0 spec) (edebug-sexps cursor))
1517 ((symbolp spec) (funcall spec cursor));; Not used by edebug,
1518 ; but leave it in for compatibility.
1519 ))
1520 ;; No edebug-form-spec provided.
1521 ((edebug-macrop head)
1522 (if edebug-eval-macro-args
1523 (edebug-forms cursor)
1524 (edebug-sexps cursor)))
1525 (t ;; Otherwise it is a function call.
1526 (edebug-forms cursor)))))
1527
1528
1529(defun edebug-list-form (cursor)
1530 ;; Return an instrumented form built from the list form.
1531 ;; The after offset will be left in the cursor after processing the form.
1532 (let ((head (edebug-top-element-required cursor "Expected elements"))
1533 ;; Prevent backtracking whenever instrumenting.
1534 (edebug-gate t)
1535 ;; A list form is never optional because it matches anything.
1536 (edebug-&optional nil)
1537 (edebug-&rest nil))
1538 ;; Skip the first offset.
1539 (edebug-set-cursor cursor (edebug-cursor-expressions cursor)
1540 (cdr (edebug-cursor-offsets cursor)))
1541 (cond
1542 ((null head) nil) ; () is legal.
1543
1544 ((symbolp head)
1545 (cond
1546 ((null head)
1547 (edebug-syntax-error "nil head"))
1548 ((eq head 'interactive-p)
1549 ;; Special case: replace (interactive-p) with variable
1550 (setq edebug-def-interactive 'check-it)
1551 (edebug-move-cursor cursor)
1552 (edebug-interactive-p-name))
1553 (t
1554 (cons head (edebug-list-form-args
1555 head (edebug-move-cursor cursor))))))
1556
1557 ((consp head)
1558 (if (and (listp head) (eq (car head) ',))
1559 (edebug-match cursor '(("," def-form) body))
1560 ;; Process anonymous function and args.
1561 ;; This assumes no anonymous macros.
1562 (edebug-match-specs cursor '(lambda-expr body) 'edebug-match-specs)))
1563
1564 (t (edebug-syntax-error
1565 "Head of list form must be a symbol or lambda expression.")))
1566 ))
1567
f7359658 1568;;; Matching of specs.
1fe3d507
DL
1569
1570(defvar edebug-after-dotted-spec nil)
1571
1572(defvar edebug-matching-depth 0) ;; initial value
1573(defconst edebug-max-depth 150) ;; maximum number of matching recursions.
1574
1575
f7359658
RS
1576;;; Failure to match
1577
1fe3d507
DL
1578;; This throws to no-match, if there are higher alternatives.
1579;; Otherwise it signals an error. The place of the error is found
1580;; with the two before- and after-offset functions.
1581
1582(defun edebug-no-match (cursor &rest edebug-args)
1583 ;; Throw a no-match, or signal an error immediately if gate is active.
1584 ;; Remember this point in case we need to report this error.
1585 (setq edebug-error-point (or edebug-error-point
1586 (edebug-before-offset cursor))
1587 edebug-best-error (or edebug-best-error edebug-args))
1588 (if (and edebug-gate (not edebug-&optional))
1589 (progn
1590 (if edebug-error-point
1591 (goto-char edebug-error-point))
1592 (apply 'edebug-syntax-error edebug-args))
1593 (funcall 'throw 'no-match edebug-args)))
1594
1595
1596(defun edebug-match (cursor specs)
1597 ;; Top level spec matching function.
1598 ;; Used also at each lower level of specs.
1599 (let (edebug-&optional
1600 edebug-&rest
1601 edebug-best-error
1602 edebug-error-point
1603 (edebug-gate edebug-gate) ;; locally bound to limit effect
1604 )
1605 (edebug-match-specs cursor specs 'edebug-match-specs)))
1606
1607
1608(defun edebug-match-one-spec (cursor spec)
1609 ;; Match one spec, which is not a keyword &-spec.
1610 (cond
1611 ((symbolp spec) (edebug-match-symbol cursor spec))
1612 ((vectorp spec) (edebug-match cursor (append spec nil)))
1613 ((stringp spec) (edebug-match-string cursor spec))
1614 ((listp spec) (edebug-match-list cursor spec))
1615 ))
1616
1617
1618(defun edebug-match-specs (cursor specs remainder-handler)
1619 ;; Append results of matching the list of specs.
1620 ;; The first spec is handled and the remainder-handler handles the rest.
1621 (let ((edebug-matching-depth
1622 (if (> edebug-matching-depth edebug-max-depth)
1623 (error "too deep - perhaps infinite loop in spec?")
1624 (1+ edebug-matching-depth))))
1625 (cond
1626 ((null specs) nil)
1627
1628 ;; Is the spec dotted?
1629 ((atom specs)
1630 (let ((edebug-dotted-spec t));; Containing spec list was dotted.
1631 (edebug-match-specs cursor (list specs) remainder-handler)))
1632
1633 ;; Is the form dotted?
1634 ((not (listp (edebug-cursor-expressions cursor)));; allow nil
1635 (if (not edebug-dotted-spec)
1636 (edebug-no-match cursor "Dotted spec required."))
1637 ;; Cancel dotted spec and dotted form.
1638 (let ((edebug-dotted-spec)
1639 (this-form (edebug-cursor-expressions cursor))
1640 (this-offset (edebug-cursor-offsets cursor)))
1641 ;; Wrap the form in a list, (by changing the cursor??)...
1642 (edebug-set-cursor cursor (list this-form) this-offset)
1643 ;; and process normally, then unwrap the result.
1644 (car (edebug-match-specs cursor specs remainder-handler))))
1645
1646 (t;; Process normally.
1647 (let* ((spec (car specs))
1648 (rest)
1649 (first-char (and (symbolp spec) (aref (symbol-name spec) 0))))
1650 ;;(message "spec = %s first char = %s" spec first-char) (sit-for 1)
1651 (nconc
1652 (cond
1653 ((eq ?& first-char);; "&" symbols take all following specs.
1654 (funcall (get-edebug-spec spec) cursor (cdr specs)))
1655 ((eq ?: first-char);; ":" symbols take one following spec.
1656 (setq rest (cdr (cdr specs)))
1657 (funcall (get-edebug-spec spec) cursor (car (cdr specs))))
1658 (t;; Any other normal spec.
1659 (setq rest (cdr specs))
1660 (edebug-match-one-spec cursor spec)))
1661 (funcall remainder-handler cursor rest remainder-handler)))))))
1662
1663
1664;; Define specs for all the symbol specs with functions used to process them.
f7359658 1665;; Perhaps we shouldn't be doing this with edebug-form-specs since the
1fe3d507
DL
1666;; user may want to define macros or functions with the same names.
1667;; We could use an internal obarray for these primitive specs.
1668
1669(mapcar
1670 (function (lambda (pair)
1671 (put (car pair) 'edebug-form-spec (cdr pair))))
1672 '((&optional . edebug-match-&optional)
1673 (&rest . edebug-match-&rest)
1674 (&or . edebug-match-&or)
1675 (form . edebug-match-form)
1676 (sexp . edebug-match-sexp)
1677 (body . edebug-match-body)
1678 (&define . edebug-match-&define)
1679 (name . edebug-match-name)
1680 (:name . edebug-match-colon-name)
1681 (arg . edebug-match-arg)
1682 (def-body . edebug-match-def-body)
1683 (def-form . edebug-match-def-form)
1684 ;; Less frequently used:
1685 ;; (function . edebug-match-function)
1686 (lambda-expr . edebug-match-lambda-expr)
1fe3d507
DL
1687 (&not . edebug-match-&not)
1688 (&key . edebug-match-&key)
1689 (place . edebug-match-place)
1690 (gate . edebug-match-gate)
1691 ;; (nil . edebug-match-nil) not this one - special case it.
1692 ))
1693
1694(defun edebug-match-symbol (cursor symbol)
1695 ;; Match a symbol spec.
1696 (let* ((spec (get-edebug-spec symbol)))
1697 (cond
1698 (spec
1699 (if (consp spec)
1700 ;; It is an indirect spec.
1701 (edebug-match cursor spec)
1702 ;; Otherwise it should be the symbol name of a function.
1703 ;; There could be a bug here - maybe need to do edebug-match bindings.
1704 (funcall spec cursor)))
1705
1706 ((null symbol) ;; special case this.
1707 (edebug-match-nil cursor))
1708
1709 ((fboundp symbol) ; is it a predicate?
1710 (let ((sexp (edebug-top-element-required cursor "Expected" symbol)))
1711 ;; Special case for edebug-`.
1712 (if (and (listp sexp) (eq (car sexp) ',))
1713 (edebug-match cursor '(("," def-form)))
1714 (if (not (funcall symbol sexp))
1715 (edebug-no-match cursor symbol "failed"))
1716 (edebug-move-cursor cursor)
1717 (list sexp))))
1718 (t (error "%s is not a form-spec or function" symbol))
1719 )))
1720
1721
1722(defun edebug-match-sexp (cursor)
1723 (list (prog1 (edebug-top-element-required cursor "Expected sexp")
1724 (edebug-move-cursor cursor))))
1725
1726(defun edebug-match-form (cursor)
1727 (list (edebug-form cursor)))
1728
1729(defalias 'edebug-match-place 'edebug-match-form)
1730 ;; Currently identical to edebug-match-form.
1731 ;; This is for common lisp setf-style place arguments.
1732
1733(defsubst edebug-match-body (cursor) (edebug-forms cursor))
1734
1735(defun edebug-match-&optional (cursor specs)
1736 ;; Keep matching until one spec fails.
1737 (edebug-&optional-wrapper cursor specs 'edebug-&optional-wrapper))
1738
1739(defun edebug-&optional-wrapper (cursor specs remainder-handler)
1740 (let (result
1741 (edebug-&optional specs)
1742 (edebug-gate nil)
1743 (this-form (edebug-cursor-expressions cursor))
1744 (this-offset (edebug-cursor-offsets cursor)))
1745 (if (null (catch 'no-match
1746 (setq result
1747 (edebug-match-specs cursor specs remainder-handler))
1748 ;; Returning nil means no no-match was thrown.
1749 nil))
1750 result
1751 ;; no-match, but don't fail; just reset cursor and return nil.
1752 (edebug-set-cursor cursor this-form this-offset)
1753 nil)))
1754
1755
1756(defun edebug-&rest-wrapper (cursor specs remainder-handler)
1757 (if (null specs) (setq specs edebug-&rest))
1758 ;; Reuse the &optional handler with this as the remainder handler.
1759 (edebug-&optional-wrapper cursor specs remainder-handler))
1760
1761(defun edebug-match-&rest (cursor specs)
1762 ;; Repeatedly use specs until failure.
1763 (let ((edebug-&rest specs) ;; remember these
1764 edebug-best-error
1765 edebug-error-point)
1766 (edebug-&rest-wrapper cursor specs 'edebug-&rest-wrapper)))
1767
1768
1769(defun edebug-match-&or (cursor specs)
1770 ;; Keep matching until one spec succeeds, and return its results.
1771 ;; If none match, fail.
1772 ;; This needs to be optimized since most specs spend time here.
1773 (let ((original-specs specs)
1774 (this-form (edebug-cursor-expressions cursor))
1775 (this-offset (edebug-cursor-offsets cursor)))
1776 (catch 'matched
1777 (while specs
1778 (catch 'no-match
1779 (throw 'matched
1780 (let (edebug-gate ;; only while matching each spec
1781 edebug-best-error
1782 edebug-error-point)
f7359658 1783 ;; Doesn't support e.g. &or symbolp &rest form
1fe3d507
DL
1784 (edebug-match-one-spec cursor (car specs)))))
1785 ;; Match failed, so reset and try again.
1786 (setq specs (cdr specs))
1787 ;; Reset the cursor for the next match.
1788 (edebug-set-cursor cursor this-form this-offset))
1789 ;; All failed.
1790 (apply 'edebug-no-match cursor "Expected one of" original-specs))
84fc2cfa
ER
1791 ))
1792
1793
1fe3d507
DL
1794(defun edebug-match-&not (cursor specs)
1795 ;; If any specs match, then fail
1796 (if (null (catch 'no-match
1797 (let ((edebug-gate nil))
1798 (save-excursion
1799 (edebug-match-&or cursor specs)))
1800 nil))
1801 ;; This means something matched, so it is a no match.
1802 (edebug-no-match cursor "Unexpected"))
1803 ;; This means nothing matched, so it is OK.
1804 nil) ;; So, return nothing
1805
1806
1807(def-edebug-spec &key edebug-match-&key)
1808
1809(defun edebug-match-&key (cursor specs)
1810 ;; Following specs must look like (<name> <spec>) ...
1811 ;; where <name> is the name of a keyword, and spec is its spec.
f7359658 1812 ;; This really doesn't save much over the expanded form and takes time.
1fe3d507
DL
1813 (edebug-match-&rest
1814 cursor
1815 (cons '&or
1816 (mapcar (function (lambda (pair)
1817 (vector (format ":%s" (car pair))
1818 (car (cdr pair)))))
1819 specs))))
1820
1821
1822(defun edebug-match-gate (cursor)
1823 ;; Simply set the gate to prevent backtracking at this level.
1824 (setq edebug-gate t)
1825 nil)
1826
1827
1828(defun edebug-match-list (cursor specs)
1829 ;; The spec is a list, but what kind of list, and what context?
1830 (if edebug-dotted-spec
1831 ;; After dotted spec but form did not contain dot,
1832 ;; so match list spec elements as if spliced in.
1833 (prog1
1834 (let ((edebug-dotted-spec))
1835 (edebug-match-specs cursor specs 'edebug-match-specs))
1836 ;; If it matched, really clear the dotted-spec flag.
1837 (setq edebug-dotted-spec nil))
1838 (let ((spec (car specs))
1839 (form (edebug-top-element-required cursor "Expected" specs)))
1840 (cond
1841 ((eq 'quote spec)
1842 (let ((spec (car (cdr specs))))
1843 (cond
1844 ((symbolp spec)
1845 ;; Special case: spec quotes a symbol to match.
1846 ;; Change in future. Use "..." instead.
1847 (if (not (eq spec form))
1848 (edebug-no-match cursor "Expected" spec))
1849 (edebug-move-cursor cursor)
1850 (setq edebug-gate t)
1851 form)
1852 (t
1853 (error "Bad spec: %s" specs)))))
1854
1855 ((listp form)
1856 (prog1
1857 (list (edebug-match-sublist
1858 ;; First offset is for the list form itself.
1859 ;; Treat nil as empty list.
1860 (edebug-new-cursor form (cdr (edebug-top-offset cursor)))
1861 specs))
1862 (edebug-move-cursor cursor)))
1863
1864 ((and (eq 'vector spec) (vectorp form))
1865 ;; Special case: match a vector with the specs.
1866 (let ((result (edebug-match-sublist
1867 (edebug-new-cursor
1868 form (cdr (edebug-top-offset cursor)))
1869 (cdr specs))))
1870 (edebug-move-cursor cursor)
1871 (list (apply 'vector result))))
84fc2cfa 1872
1fe3d507
DL
1873 (t (edebug-no-match cursor "Expected" specs)))
1874 )))
84fc2cfa
ER
1875
1876
1fe3d507
DL
1877(defun edebug-match-sublist (cursor specs)
1878 ;; Match a sublist of specs.
1879 (let (edebug-&optional
1880 ;;edebug-best-error
1881 ;;edebug-error-point
1882 )
1883 (prog1
1884 ;; match with edebug-match-specs so edebug-best-error is not bound.
1885 (edebug-match-specs cursor specs 'edebug-match-specs)
1886 (if (not (edebug-empty-cursor cursor))
1887 (if edebug-best-error
1888 (apply 'edebug-no-match cursor edebug-best-error)
1889 ;; A failed &rest or &optional spec may leave some args.
1890 (edebug-no-match cursor "Failed matching" specs)
1891 )))))
1892
1893
1894(defun edebug-match-string (cursor spec)
1895 (let ((sexp (edebug-top-element-required cursor "Expected" spec)))
1896 (if (not (eq (intern spec) sexp))
1897 (edebug-no-match cursor "Expected" spec)
1898 ;; Since it matched, failure means immediate error, unless &optional.
1899 (setq edebug-gate t)
1900 (edebug-move-cursor cursor)
1901 (list sexp)
1902 )))
84fc2cfa 1903
1fe3d507
DL
1904(defun edebug-match-nil (cursor)
1905 ;; There must be nothing left to match a nil.
1906 (if (not (edebug-empty-cursor cursor))
1907 (edebug-no-match cursor "Unmatched argument(s)")
1908 nil))
1909
1910
1911(defun edebug-match-function (cursor)
1912 (error "Use function-form instead of function in edebug spec"))
1913
1914(defun edebug-match-&define (cursor specs)
1915 ;; Match a defining form.
f7359658 1916 ;; Normally, &define is interpreted specially other places.
1fe3d507
DL
1917 ;; This should only be called inside of a spec list to match the remainder
1918 ;; of the current list. e.g. ("lambda" &define args def-body)
1919 (edebug-make-form-wrapper
1920 cursor
1921 (edebug-before-offset cursor)
1922 ;; Find the last offset in the list.
1923 (let ((offsets (edebug-cursor-offsets cursor)))
1924 (while (consp offsets) (setq offsets (cdr offsets)))
1925 offsets)
1926 specs))
1927
1928(defun edebug-match-lambda-expr (cursor)
1929 ;; The expression must be a function.
1930 ;; This will match any list form that begins with a symbol
1931 ;; that has an edebug-form-spec beginning with &define. In
1932 ;; practice, only lambda expressions should be used.
1933 ;; I could add a &lambda specification to avoid confusion.
1934 (let* ((sexp (edebug-top-element-required
1935 cursor "Expected lambda expression"))
1936 (offset (edebug-top-offset cursor))
1937 (head (and (consp sexp) (car sexp)))
1938 (spec (and (symbolp head) (get-edebug-spec head)))
1939 (edebug-inside-func nil))
1940 ;; Find out if this is a defining form from first symbol.
1941 (if (and (consp spec) (eq '&define (car spec)))
1942 (prog1
1943 (list
1944 (edebug-defining-form
1945 (edebug-new-cursor sexp offset)
1946 (car offset);; before the sexp
1947 (edebug-after-offset cursor)
1948 (cons (symbol-name head) (cdr spec))))
1949 (edebug-move-cursor cursor))
1950 (edebug-no-match cursor "Expected lambda expression")
1951 )))
84fc2cfa 1952
84fc2cfa 1953
1fe3d507
DL
1954(defun edebug-match-name (cursor)
1955 ;; Set the edebug-def-name bound in edebug-defining-form.
1956 (let ((name (edebug-top-element-required cursor "Expected name")))
1957 ;; Maybe strings and numbers could be used.
1958 (if (not (symbolp name))
1959 (edebug-no-match cursor "Symbol expected for name of definition"))
1960 (setq edebug-def-name
1961 (if edebug-def-name
1962 ;; Construct a new name by appending to previous name.
1963 (intern (format "%s@%s" edebug-def-name name))
1964 name))
1965 (edebug-move-cursor cursor)
1966 (list name)))
1967
1968(defun edebug-match-colon-name (cursor spec)
1969 ;; Set the edebug-def-name to the spec.
1970 (setq edebug-def-name
1971 (if edebug-def-name
1972 ;; Construct a new name by appending to previous name.
1973 (intern (format "%s@%s" edebug-def-name spec))
1974 spec))
1975 nil)
1976
1977(defun edebug-match-arg (cursor)
1978 ;; set the def-args bound in edebug-defining-form
1979 (let ((edebug-arg (edebug-top-element-required cursor "Expected arg")))
1980 (if (or (not (symbolp edebug-arg))
f7359658 1981 (edebug-lambda-list-keywordp edebug-arg))
1fe3d507
DL
1982 (edebug-no-match cursor "Bad argument:" edebug-arg))
1983 (edebug-move-cursor cursor)
1984 (setq edebug-def-args (cons edebug-arg edebug-def-args))
1985 (list edebug-arg)))
1986
1987(defun edebug-match-def-form (cursor)
1988 ;; Like form but the form is wrapped in edebug-enter form.
1989 ;; The form is assumed to be executing outside of the function context.
1990 ;; This is a hack for now, since a def-form might execute inside as well.
1991 ;; Not to be used otherwise.
1992 (let ((edebug-inside-func nil))
1993 (list (edebug-make-enter-wrapper (list (edebug-form cursor))))))
1994
1995(defun edebug-match-def-body (cursor)
1996 ;; Like body but body is wrapped in edebug-enter form.
1997 ;; The body is assumed to be executing inside of the function context.
1998 ;; Not to be used otherwise.
1999 (let ((edebug-inside-func t))
2000 (list (edebug-wrap-def-body (edebug-forms cursor)))))
2001
2002
2003;;;; Edebug Form Specs
2004;;; ==========================================================
2005;;; See cl-specs.el for common lisp specs.
2006
2007;;;;* Spec for def-edebug-spec
2008;;; Out of date.
2009
2010(defun edebug-spec-p (object)
2011 "Return non-nil if OBJECT is a symbol with an edebug-form-spec property."
2012 (and (symbolp object)
2013 (get object 'edebug-form-spec)))
2014
2015(def-edebug-spec def-edebug-spec
2016 ;; Top level is different from lower levels.
2017 (&define :name edebug-spec name
2018 &or "nil" edebug-spec-p "t" "0" (&rest edebug-spec)))
2019
2020(def-edebug-spec edebug-spec-list
2021 ;; A list must have something in it, or it is nil, a symbolp
2022 ((edebug-spec . [&or nil edebug-spec])))
2023
2024(def-edebug-spec edebug-spec
2025 (&or
2026 (vector &rest edebug-spec) ; matches a vector
2027 ("vector" &rest edebug-spec) ; matches a vector spec
2028 ("quote" symbolp)
2029 edebug-spec-list
2030 stringp
f7359658
RS
2031 [edebug-lambda-list-keywordp &rest edebug-spec]
2032 ;; [edebug-keywordp gate edebug-spec] ;; need edebug-keywordp for this.
1fe3d507
DL
2033 edebug-spec-p ;; Including all the special ones e.g. form.
2034 symbolp;; a predicate
2035 ))
84fc2cfa
ER
2036
2037
f7359658 2038;;;* Emacs special forms and some functions.
84fc2cfa 2039
1fe3d507
DL
2040;; quote expects only one argument, although it allows any number.
2041(def-edebug-spec quote sexp)
84fc2cfa 2042
1fe3d507
DL
2043;; The standard defining forms.
2044(def-edebug-spec defconst defvar)
2045(def-edebug-spec defvar (symbolp &optional form stringp))
84fc2cfa 2046
1fe3d507
DL
2047(def-edebug-spec defun
2048 (&define name lambda-list
2049 [&optional stringp]
2050 [&optional ("interactive" interactive)]
2051 def-body))
2052(def-edebug-spec defmacro
2053 (&define name lambda-list def-body))
84fc2cfa 2054
f7359658 2055(def-edebug-spec arglist lambda-list) ;; deprecated - use lambda-list.
84fc2cfa 2056
1fe3d507
DL
2057(def-edebug-spec lambda-list
2058 (([&rest arg]
2059 [&optional ["&optional" arg &rest arg]]
2060 &optional ["&rest" arg]
2061 )))
84fc2cfa 2062
1fe3d507
DL
2063(def-edebug-spec interactive
2064 (&optional &or stringp def-form))
84fc2cfa 2065
1fe3d507
DL
2066;; A function-form is for an argument that may be a function or a form.
2067;; This specially recognizes anonymous functions quoted with quote.
2068(def-edebug-spec function-form
2069 ;; form at the end could also handle "function",
2070 ;; but recognize it specially to avoid wrapping function forms.
2071 (&or ([&or "quote" "function"] &or symbolp lambda-expr) form))
84fc2cfa 2072
1fe3d507
DL
2073;; function expects a symbol or a lambda or macro expression
2074;; A macro is allowed by Emacs.
2075(def-edebug-spec function (&or symbolp lambda-expr))
84fc2cfa 2076
1fe3d507
DL
2077;; lambda is a macro in emacs 19.
2078(def-edebug-spec lambda (&define lambda-list
2079 [&optional stringp]
2080 [&optional ("interactive" interactive)]
2081 def-body))
2082
2083;; A macro expression is a lambda expression with "macro" prepended.
2084(def-edebug-spec macro (&define "lambda" lambda-list def-body))
2085
2086;; (def-edebug-spec anonymous-form ((&or ["lambda" lambda] ["macro" macro])))
2087
2088;; Standard functions that take function-forms arguments.
2089(def-edebug-spec mapcar (function-form form))
2090(def-edebug-spec mapconcat (function-form form form))
2091(def-edebug-spec mapatoms (function-form &optional form))
2092(def-edebug-spec apply (function-form &rest form))
2093(def-edebug-spec funcall (function-form &rest form))
2094
2095(def-edebug-spec let
2096 ((&rest &or (symbolp &optional form) symbolp)
2097 body))
2098
2099(def-edebug-spec let* let)
2100
2101(def-edebug-spec setq (&rest symbolp form))
2102(def-edebug-spec setq-default setq)
2103
2104(def-edebug-spec cond (&rest (&rest form)))
2105
2106(def-edebug-spec condition-case
2107 (symbolp
2108 form
2109 &rest (symbolp body)))
2110
2111
f7359658 2112(def-edebug-spec \` (backquote-form))
1fe3d507
DL
2113
2114;; Supports quotes inside backquotes,
2115;; but only at the top level inside unquotes.
2116(def-edebug-spec backquote-form
2117 (&or
2118 ([&or "," ",@"] &or ("quote" backquote-form) form)
2119 (backquote-form &rest backquote-form)
2120 ;; If you use dotted forms in backquotes, replace the previous line
2121 ;; with the following. This takes quite a bit more stack space, however.
2122 ;; (backquote-form . [&or nil backquote-form])
2123 (vector &rest backquote-form)
2124 sexp))
84fc2cfa 2125
1fe3d507
DL
2126;; Special version of backquote that instruments backquoted forms
2127;; destined to be evaluated, usually as the result of a
2128;; macroexpansion. Backquoted code can only have unquotes (, and ,@)
2129;; in places where list forms are allowed, and predicates. If the
2130;; backquote is used in a macro, unquoted code that come from
2131;; arguments must be instrumented, if at all, with def-form not def-body.
84fc2cfa 2132
1fe3d507
DL
2133;; We could assume that all forms (not nested in other forms)
2134;; in arguments of macros should be def-forms, whether or not the macros
2135;; are defined with edebug-` but this would be expensive.
84fc2cfa 2136
1fe3d507
DL
2137;; ,@ might have some problems.
2138
f7359658
RS
2139(defalias 'edebug-\` '\`) ;; same macro as regular backquote.
2140(def-edebug-spec edebug-\` (def-form))
1fe3d507
DL
2141
2142;; Assume immediate quote in unquotes mean backquote at next higher level.
2143(def-edebug-spec , (&or ("quote" edebug-`) def-form))
2144(def-edebug-spec ,@ (&define ;; so (,@ form) is never wrapped.
2145 &or ("quote" edebug-`) def-form))
2146
2147;; New byte compiler.
2148(def-edebug-spec defsubst defun)
2149(def-edebug-spec dont-compile t)
2150(def-edebug-spec eval-when-compile t)
2151(def-edebug-spec eval-and-compile t)
2152
8fd29408
RS
2153(def-edebug-spec save-selected-window t)
2154(def-edebug-spec save-current-buffer t)
2155(def-edebug-spec save-match-data t)
2156(def-edebug-spec with-output-to-string t)
2157(def-edebug-spec with-current-buffer t)
5398a9e7 2158(def-edebug-spec combine-after-change-calls t)
8fd29408
RS
2159(def-edebug-spec with-temp-file t)
2160(def-edebug-spec with-temp-buffer t)
2161
1fe3d507
DL
2162;; Anything else?
2163
2164
1fe3d507
DL
2165;; Some miscellaneous specs for macros in public packages.
2166;; Send me yours.
2167
2168;; advice.el by Hans Chalupsky (hans@cs.buffalo.edu)
2169
2170(def-edebug-spec ad-dolist ((symbolp form &optional form) body))
2171(def-edebug-spec defadvice
2172 (&define name ;; thing being advised.
2173 (name ;; class is [&or "before" "around" "after"
2174 ;; "activation" "deactivation"]
2175 name ;; name of advice
2176 &rest sexp ;; optional position and flags
2177 )
2178 [&optional stringp]
2179 [&optional ("interactive" interactive)]
2180 def-body))
2181
f7359658 2182;;; The debugger itself
1fe3d507
DL
2183
2184(defvar edebug-active nil) ;; Non-nil when edebug is active
84fc2cfa
ER
2185
2186;;; add minor-mode-alist entry
2187(or (assq 'edebug-active minor-mode-alist)
2188 (setq minor-mode-alist (cons (list 'edebug-active " *Debugging*")
2189 minor-mode-alist)))
2190
1fe3d507
DL
2191(defvar edebug-stack nil)
2192;; Stack of active functions evaluated via edebug.
2193;; Should be nil at the top level.
2194
2195(defvar edebug-stack-depth -1)
2196;; Index of last edebug-stack item.
2197
2198(defvar edebug-offset-indices nil)
2199;; Stack of offset indices of visited edebug sexps.
2200;; Should be nil at the top level.
2201;; Each function adds one cons. Top is modified with setcar.
84fc2cfa 2202
84fc2cfa
ER
2203
2204(defvar edebug-entered nil
1fe3d507
DL
2205 ;; Non-nil if edebug has already been entered at this recursive edit level.
2206 ;; This should stay nil at the top level.
2207 )
2208
2209;; Should these be options?
2210(defconst edebug-debugger 'edebug
2211 ;; Name of function to use for debugging when error or quit occurs.
2212 ;; Set this to 'debug if you want to debug edebug.
2213 )
2214
2215
2216;; Dynamically bound variables, declared globally but left unbound.
2217(defvar edebug-function) ; the function being executed. change name!!
2218(defvar edebug-args) ; the arguments of the function
2219(defvar edebug-data) ; the edebug data for the function
2220(defvar edebug-value) ; the result of the expression
2221(defvar edebug-after-index)
2222(defvar edebug-def-mark) ; the mark for the definition
2223(defvar edebug-freq-count) ; the count of expression visits.
2224(defvar edebug-coverage) ; the coverage results of each expression of function.
2225
2226(defvar edebug-buffer) ; which buffer the function is in.
2227(defvar edebug-result) ; the result of the function call returned by body
2228(defvar edebug-outside-executing-macro)
2229(defvar edebug-outside-defining-kbd-macro)
2230
2231(defvar edebug-execution-mode 'step) ; Current edebug mode set by user.
2232(defvar edebug-next-execution-mode nil) ; Use once instead of initial mode.
2233
2234(defvar edebug-outside-debug-on-error) ; the value of debug-on-error outside
2235(defvar edebug-outside-debug-on-quit) ; the value of debug-on-quit outside
2236
88668147
DL
2237(defvar edebug-outside-pre-command-hook)
2238(defvar edebug-outside-post-command-hook)
88668147
DL
2239
2240(defvar cl-lexical-debug) ;; Defined in cl.el
2241
1fe3d507 2242;;; Handling signals
1fe3d507 2243
1fe3d507
DL
2244(defun edebug-signal (edebug-signal-name edebug-signal-data)
2245 "Signal an error. Args are SIGNAL-NAME, and associated DATA.
2246A signal name is a symbol with an `error-conditions' property
2247that is a list of condition names.
2248A handler for any of those names will get to handle this signal.
2249The symbol `error' should always be one of them.
2250
2251DATA should be a list. Its elements are printed as part of the error message.
2252If the signal is handled, DATA is made available to the handler.
2253See `condition-case'.
2254
2255This is the Edebug replacement for the standard `signal'. It should
2256only be active while Edebug is. It checks `debug-on-error' to see
2257whether it should call the debugger. When execution is resumed, the
2258error is signaled again."
2259 (if (and (listp debug-on-error) (memq edebug-signal-name debug-on-error))
2260 (edebug 'error (cons edebug-signal-name edebug-signal-data)))
2261 ;; If we reach here without another non-local exit, then send signal again.
2262 ;; i.e. the signal is not continuable, yet.
1e3ab67b 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)
2662 (edebug-sit-for 1)) ; Force update and pause.
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