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