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