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