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