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