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