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