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