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