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