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