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