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