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