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