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