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