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