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