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