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