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