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