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