(remove-hook): Doc fix.
[bpt/emacs.git] / lisp / emacs-lisp / edebug.el
CommitLineData
3a801d0c 1;;; edebug.el --- a source-level debugger for emacs lisp
84fc2cfa
ER
2
3;; Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc
4
3a801d0c 5;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
e9571d2a 6;; Keywords: lisp, tools, maint
3a801d0c 7
e41b2db1
ER
8;; This is Dan's 2.5 version with some header comments rearranged to separate
9;; the Change Log from the Commentary (so the package-finder code can browse
10;; the Commentary).
11
84fc2cfa
ER
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY. No author or distributor
16;; accepts responsibility to anyone for the consequences of using it
17;; or for whether it serves any particular purpose or works at all,
18;; unless he says so in writing. Refer to the GNU Emacs General Public
19;; License for full details.
20
21;; Everyone is granted permission to copy, modify and redistribute
22;; GNU Emacs, but only under the conditions described in the
23;; GNU Emacs General Public License. A copy of this license is
24;; supposed to have been given to you along with GNU Emacs so you
25;; can know your rights and responsibilities. It should be in a
26;; file named COPYING. Among other things, the copyright notice
27;; and this notice must be preserved on all copies.
28
3a801d0c
ER
29;;;; Commentary:
30
31;;; This minor mode allows programmers to step through Emacs Lisp source
84fc2cfa
ER
32;;; code while executing, set breakpoints, etc. See the texinfo
33;;; document (being constructed...) for more detailed instructions
34;;; than contained here. Send me your enhancement, ideas, bugs, or
35;;; fixes.
36
37;;; Daniel LaLiberte 217-244-0785
38;;; University of Illinois, Urbana-Champaign
39;;; Department of Computer Science
40;;; 1304 W Springfield
41;;; Urbana, IL 61801
42
43;;; uiucdcs!liberte
44;;; liberte@cs.uiuc.edu
45
46;;; Contents:
47;;; =========
84fc2cfa 48;;; Installation
e41b2db1 49;;; Change list
84fc2cfa
ER
50;;; Utilities
51;;; Parser
52;;; Debugger
53
54\f
e41b2db1
ER
55;;; Installation
56;;; ------------
57;; Put edebug.el in some directory in your load-path and byte-compile it.
58
59;; Put the following forms in your .emacs file.
60;; (define-key emacs-lisp-mode-map "\^Xx" 'edebug-defun)
61;; (autoload 'edebug-defun "edebug")
62;; (autoload 'edebug-debug "edebug")
63;; (setq debugger 'edebug-debug)
64;; ... other options, described in the next section.
65
66;; Evaluate a defun for edebug with edebug-defun.
67;; Evaluate your function normally.
68;; Use the "?" command in edebug to describe other commands.
69;; See edebug.texinfo for more instructions.
70
71;;; Change Log:
84fc2cfa 72
84fc2cfa
ER
73;;; Revision 2.5 91/07/25 13:32:53 liberte
74;;; Doc string cleanup.
75;;; If edebug-form-hook is t, evaluate all arguments.
76;;; If edebug-form-hook is 0, evaluate no arguments.
77;;; If edebug-form-hook is nil, evaluate macro args according
78;;; to edebug-eval-macro-args.
79;;; Save the outside value of executing macro.
80;;; Save and restore the outside restriction.
81;;; Dont force update for go and Go-nonstop.
82;;; Save and restore last-command-char, last-command,
83;;; this-command, last-input-char.
84;;; For epoch, do epoch::dispatch-events before sit-for
85;;; and input-pending-p since X events could interfere.
86;;; Warn about unsetting non-existent breakpoint.
87;;; Fix edebug-forward-sexp with prefix arg.
88;;; Add edebug-step-out to exit from current sexp.
89;;;
90;;; Revision 2.4 91/03/18 12:35:44 liberte
91;;; Force update after go or Go-nonstop modes, so overlay arrow is correct.
92;;; Support debug-on-quit. Remove edebug-on-error.
93;;; Fix edebug-anonymous. Bug found by jackr@wpd.sgi.com (Jack Repenning).
94;;; Don't discard-input anymore. Easier to change modes this way.
95;;; Fix max-lisp-eval-depth and max-specpdl-size incrementing.
96;;; Save and restore points in all buffers, if
97;;; edebug-save-buffer-points is non-nil. Expensive!
98;;; Bug caught by wolfgang@wsrcc.com (Wolfgang S. Rupprecht)
99;;; Save standard-output and standard-input in edebug-recursive-edit
100;;; so that edebug-outside-excursion can restore them.
101;;; Call set-buffer in edebug-pop-to-buffer since
102;;; select-window does not do that.
103;;; Fix edebug's eval-defun to remember current buffer inside evaluations
104;;; and to evaluate top-level forms. Found by Jamie Zawinski.
105;;; Add edebug-interactive-entry to support interactive forms with
106;;; non-string arg. Bug found by Jack Repenning.
107;;; Simplify edebug-restore-match-data to just store-match-data.
108;;; Motivated by linus@lysator.liu.se.
109;;; Move the match-data call to before the outside
110;;; buffer is changed, since it assumes that.
111;;;
112;;; Revision 2.3 91/01/17 20:55:14 liberte
113;;; Fix bug found by hollen@megatek.uucp.
114;;; Current buffer was not being restored.
115;;; Call edebug with (edebug begin end 'exp)
116;;; and add additional wrapper around body of functions:
117;;; (edebug-enter function body).
118;;; Make &optional only apply to immediate next arg
119;;; in edebug-form-parser (was edebug-macro-parser).
120;;; Catch debug errors with edebug. Yeah!
121;;; Reset edebug-mode on first function entry. Yeah!
122;;; Motivated by Dion Hollenbeck.
123;;; Add the missing bindings to the global-edebug-map.
124;;; eval-current-buffer now uses eval-region.
125;;; eval-region now does not narrow region.
126;;; Narrowing was the cause of the window-start being set wrong.
127;;; Reset edebug-mode only on
128;;; first entry of any function at each recursive-edit level.
129;;; Add edebug-backtrace, to generate cleaned up
eb8c3be9 130;;; backtrace. It doesn't "work" like the debug backtrace, however.
84fc2cfa
ER
131;;; Require reselecting outside window even if
132;;; quit occurs, otherwise save-excursions may restore
133;;; buffer to the wrong window.
134;;;
135;;; Revision 2.2 90/11/26 21:14:22 liberte
136;;; Shadow eval-defun and eval-region. Toggle
137;;; edebugging with edebug-all-defuns.
138;;; Call edebug with (edebug 'function begin end 'exp)
139;;; Suggested by Jamie Zawinski <jwz@lucid.com>.
140;;; Add edebug-form-parser to process macro args.
141;;; Motivated by Darryl Okahata darrylo@hpnmxx.hp.com.
142;;; Fix by Roland McGrath <roland@ai.mit.edu>
143;;; to wrap body of edebug-save-restriction in progn.
144;;; Fix by Darryl Okahata <darrylo%hpnmd@hpcea.hp.com>
145;;; to add (set-window-hscroll (selected-window) 0) to
146;;; edebug-pop-to-buffer.
147;;;
148;;; Revision 2.1 90/11/16 21:55:35 liberte
149;;; Clean up.
150;;; Add edebug-form-hook to edebug macro calls. Thanks to Joe Wells.
151;;; edebug-forward-sexp uses step mode if no forward-sexp.
152;;;
153;;; Revision 2.0 90/11/14 22:30:54 liberte
154;;; Handle lambda forms, function, interactive evals, defmacro.
155;;; Clean up display for Epoch - save and restore screen configurations.
156;;; Note: epoch 3.2 broke set-window-configuration.
157;;; Also, sit-for pauses do not always work in epoch.
158;;; Display evaluations window.
159;;; Display result after expression evaluation.
160;;; Thanks to discussions with Shinichirou Sugou.
161;;; Conditional and temporary breakpoints.
162;;; Change "continue" to "go" mode and add different "continue" mode.
163;;; Option to stop before symbols.
164;;;
165;;; Fix by: Glen Ditchfield gjditchfield@violet.uwaterloo.ca
166;;; to handle ?# type chars.
167;;;
168;;; Revision 1.5 89/05/10 02:39:27 liberte
169;;; Fix condition-case expression lists.
170;;; Reorganize edebug.
171;;;
172;;; Revision 1.4 89/02/14 22:58:34 liberte
173;;; Fix broken breakpointing.
3a801d0c 174;;; Temporarily widen Emacs Lisp buffer during edebug.
84fc2cfa
ER
175;;;
176;;; Revision 1.3 89/01/30 00:26:09 liberte
177;;; More bug fixes for cond and let.
178;;; Another parsing fix backquote.
179;;; Fix for lambda forms inside defuns.
180;;; Leave point at syntax error, mark at starting position.
181;;;
182;;; Revision 1.2 88/11/28 12:14:15 liberte
eb8c3be9
JB
183;;; Bug fixes: cond construct didn't execute.
184;;; () in sexp list didn't parse
185;;; () as variable in condition-case didn't parse.
84fc2cfa
ER
186;;;
187;;; Revision 1.1 88/11/28 12:11:27 liberte
188;;; Initial revision
189;;;
190
3a801d0c
ER
191;;; Code:
192
84fc2cfa
ER
193\f
194;;; Options
195;;; -------
196
197(defvar edebug-all-defuns nil
198 "*If non-nil, all defuns and defmacros evaluated will use edebug.
199eval-defun without prefix arg and eval-region will use edebug-defun.
200
201If nil, eval-region evaluates normally, but eval-defun with prefix arg
202uses edebug-defun. eval-region is called by eval-defun, eval-last-sexp,
203and eval-print-last-sexp.
204
3a801d0c 205You may wish to make this variable local to each Emacs Lisp buffer by calling
84fc2cfa
ER
206(make-local-variable 'edebug-all-defuns) in your emacs-lisp-mode-hook.
207You can use the function edebug-all-defuns to toggle its value.")
208
209
210(defvar edebug-eval-macro-args nil
211 "*If non-nil, edebug will assume that all macro call arguments for
212macros that have no edebug-form-hook may be evaluated, otherwise it
213will not. To specify exceptions for macros that have some arguments
214evaluated and some not, you should specify an edebug-form-hook")
215
216(defvar edebug-stop-before-symbols nil
217 "*Non-nil causes edebug to stop before symbols as well as after.
218In any case, it is possible to stop before a symbol with a breakpoint or
219interrupt.")
220
221(defvar edebug-save-windows t
222 "*If non-nil, save and restore window configuration on edebug calls.
223It takes some time to save and restore, so if your program does not care
224what happens to the window configurations, it is better to set this
225variable to nil.")
226
227(defvar edebug-save-point t
228 "*If non-nil, save and restore the point and mark in source code buffers.")
229
230(defvar edebug-save-buffer-points nil
231 "*If non-nil, save and restore the points of all buffers, displayed or not.
232
233Saving and restoring buffer points is necessary if you are debugging
234code that changes the point of a buffer which is displayed in a
235non-selected window. If edebug or the user then selects the
236window, the buffer's point will be changed to the window's point.
237
238Saving and restoring all the points is an expensive operation since it
239visits each buffer twice for each edebug call, so it is best to avoid
240it if you can.")
241
242(defvar edebug-initial-mode 'step
243 "*Global initial mode for edebug, if non-nil.
244This is used when edebug is first entered for each recursive-edit level.
245Possible values are nil (meaning keep using edebug-mode), step, go,
246Go-nonstop, trace, Trace-fast, continue, and Continue-fast.")
247
248(defvar edebug-trace nil
249 "*Non-nil if edebug should show a trace of function entry and exit.
250Tracing output is displayed in a buffer named *edebug-trace*, one
251function entry or exit per line, indented by the recursion level. You
252can customize by replacing functions edebug-print-trace-entry and
253edebug-print-trace-exit.")
254
255
256\f
257;;;========================================================================
258;;; Utilities
259;;; ---------
260
261(defun edebug-which-function ()
262 "Return the symbol of the function we are in"
263 (save-excursion
264 (end-of-defun)
265 (beginning-of-defun)
266 (down-list 1)
267 (if (not (memq (read (current-buffer)) '(defun defmacro)))
268 (error "Not in defun or defmacro."))
269 (read (current-buffer))))
270
271(defun edebug-last-sexp ()
272 "Return the last sexp before point in current buffer.
3a801d0c 273Assumes Emacs Lisp syntax is active."
84fc2cfa
ER
274 (car
275 (read-from-string
276 (buffer-substring
277 (save-excursion
278 (forward-sexp -1)
279 (point))
280 (point)))))
281
282(defun edebug-window-list ()
283 "Return a list of windows, in order of next-window."
eb8c3be9 284 ;; This doesn't work for epoch.
84fc2cfa
ER
285 (let* ((first-window (selected-window))
286 (window-list (list first-window))
287 (next (next-window first-window)))
288 (while (not (eq next first-window))
289 (setq window-list (cons next window-list))
290 (setq next (next-window next)))
291 (nreverse window-list)))
292
293(defun edebug-get-buffer-points ()
294 "Return a list of buffer point pairs, for all buffers."
295 (save-excursion
296 (mapcar (function (lambda (buf)
297 (set-buffer buf)
298 (cons buf (point))))
299 (buffer-list))))
300
301(defun edebug-set-buffer-points ()
302 "Restore the buffer-points given by edebug-get-buffer-points."
303 (mapcar (function (lambda (buf-point)
304 (if (buffer-name (car buf-point)) ; still exists
305 (progn
306 (set-buffer (car buf-point))
307 (goto-char (cdr buf-point))))))
308 edebug-buffer-points))
309
310(defun edebug-two-window-p ()
311 "Return t if there are two windows."
312 (and (not (one-window-p))
313 (eq (selected-window)
314 (next-window (next-window (selected-window))))))
315
316(defun edebug-macrop (object)
317 "Return the macro named by OBJECT, or nil if it is not a macro."
318 (while (and (symbolp object) (fboundp object))
319 (setq object (symbol-function object)))
320 (if (and (listp object)
321 (eq 'macro (car object))
322 (edebug-functionp (cdr object)))
323 object))
324
325(defun edebug-functionp (object)
326 "Returns the function named by OBJECT, or nil if it is not a function."
327 (while (and (symbolp object) (fboundp object))
328 (setq object (symbol-function object)))
329 (if (or (subrp object)
330 (and (listp object)
331 (eq (car object) 'lambda)
332 (listp (car (cdr object)))))
333 object))
334
335(defun edebug-sort-alist (alist function)
336 "Return the ALIST sorted with comparison function FUNCTION.
337This uses 'sort so the sorting is destructive."
338 (sort alist (function
339 (lambda (e1 e2)
340 (funcall function (car e1) (car e2))))))
341
342(put 'edebug-save-restriction 'edebug-form-hook
343 '(&rest form))
344
345(defmacro edebug-save-restriction (&rest body)
346 "Evaluate BODY while saving the current buffers restriction.
347BODY may change buffer outside of current restriction, unlike
348save-restriction. BODY may change the current buffer,
349and the restriction will be restored to the original buffer,
350and the current buffer remains current.
351Return the result of the last expression in BODY."
352 (` (let ((edebug:s-r-beg (point-min-marker))
353 (edebug:s-r-end (point-max-marker)))
354 (unwind-protect
355 (progn (,@ body))
356 (save-excursion
357 (set-buffer (marker-buffer edebug:s-r-beg))
358 (narrow-to-region edebug:s-r-beg edebug:s-r-end))))))
359
360\f
361;;;=============================================================
362;;; Redefine eval-defun, eval-region, and eval-current-buffer.
363;;; -----------------------------------------------------------
364
365(defun edebug-all-defuns ()
366 "Toggle edebugging of all defuns and defmacros,
367not including those evaluated in the minibuffer, or during load."
368 (interactive)
369 (setq edebug-all-defuns (not edebug-all-defuns))
370 (message "Edebugging is %s." (if edebug-all-defuns "on" "off")))
371
372
373(if (not (fboundp 'edebug-emacs-eval-defun))
374 (fset 'edebug-emacs-eval-defun (symbol-function 'eval-defun)))
375;;(fset 'eval-defun (symbol-function 'edebug-emacs-eval-defun))
376
377(defun eval-defun (edebug-debug)
378 "Edebug replacement for eval-defun. Print value in the minibuffer.
379Evaluate the top-level form that point is in or before. Note:
380eval-defun normally evaluates any top-level form, not just defuns.
381
382Here are the differences from the standard eval-defun. If the prefix
383argument is the same as edebug-all-defuns (nil or non-nil), evaluate
384normally; otherwise edebug-defun is called to wrap edebug calls around
385evaluatable expressions in the defun or defmacro body. Also, the
386value printed by edebug-defun is not just the function name."
387 (interactive "P")
388 (let ((edebug-all-defuns
389 (not (eq (not edebug-debug) (not edebug-all-defuns)))))
390 (edebug-emacs-eval-defun nil)
391 ))
392
393
394(if (not (fboundp 'edebug-emacs-eval-region))
395 (fset 'edebug-emacs-eval-region (symbol-function 'eval-region)))
396;; (fset 'eval-region (symbol-function 'edebug-emacs-eval-region))
397
398(defun eval-region (edebug-e-r-start edebug-e-r-end
399 &optional edebug-e-r-output)
400 "Edebug replacement for eval-defun.
401Like eval-region, but call edebug-defun for defuns or defmacros.
402Also, this eval-region does not narrow to the region and
403if an error occurs, point is left at the error."
404 ;; One other piddling difference concerns whitespace after the expression.
405 (interactive "r")
406 (let ((standard-output (or edebug-e-r-output 'symbolp))
407 (edebug-e-r-pnt (point))
408 (edebug-e-r-buf (current-buffer))
409 (edebug-e-r-inside-buf (current-buffer))
410 ;; Mark the end because it may move.
411 (edebug-e-r-end-marker (set-marker (make-marker) edebug-e-r-end))
412 edebug-e-r-val
413 )
414 (goto-char edebug-e-r-start)
415 (edebug-skip-whitespace)
416 (while (< (point) edebug-e-r-end-marker)
417 (if (and edebug-all-defuns
418 (eq 'lparen (edebug-next-token-class))
419 (save-excursion
420 (forward-char 1) ; skip \(
421 (memq (edebug-read-sexp) '(defun defmacro))))
422 (progn
423 (edebug-defun)
424 ;; Potential problem: edebug-defun always prints name.
425 (forward-sexp 1) ; skip the defun
426 )
427 (if (and (eq 'lparen (edebug-next-token-class))
428 (save-excursion
429 (forward-char 1) ; skip \(
430 (memq (edebug-read-sexp) '(defun defmacro))))
431 ;; If it's a defun or defmacro, but not edebug-all-defuns
432 ;; reset the symbols edebug property to be just a marker at
433 ;; the definitions source code.
434 (put (edebug-which-function) 'edebug (point-marker)))
435
436 ;; Evaluate normally - after restoring the current-buffer.
437 (setq edebug-e-r-val (edebug-read-sexp))
438 (save-excursion
439 (set-buffer edebug-e-r-inside-buf)
440 (setq edebug-e-r-val (eval edebug-e-r-val))
441 ;; Remember current buffer for next time.
442 (setq edebug-e-r-inside-buf (current-buffer)))
443
444 (if edebug-e-r-output
445 (progn
446 (setq values (cons edebug-e-r-val values))
447 (if (eq standard-output t)
448 (prin1 edebug-e-r-val)
449 (print edebug-e-r-val))))
450 )
451 (goto-char
452 (min (max edebug-e-r-end-marker (point))
453 (progn (edebug-skip-whitespace) (point))))
454 ) ; while
455 (if (null edebug-e-r-output)
456 ;; do the save-excursion recovery
457 (progn
458 ;; but mark is not restored
459 (set-buffer edebug-e-r-buf)
460 (goto-char edebug-e-r-pnt)))
461 nil
462 ))
463
464
daa37602 465(defun edebug-eval-current-buffer (&optional edebug-e-c-b-output)
84fc2cfa
ER
466 "Call eval-region on the whole buffer."
467 (interactive)
468 (eval-region (point-min) (point-max) edebug-e-c-b-output))
469
daa37602
JB
470(defun edebug-eval-buffer (&optional buffer edebug-e-c-b-output)
471 "Call eval-region on the whole buffer."
472 (interactive "bEval buffer: ")
473 (save-excursion
474 (set-buffer buffer)
475 (eval-region (point-min) (point-max) edebug-e-c-b-output)))
476
477;; The standard eval-current-buffer doesn't use eval-region.
478(if (and (fboundp 'eval-current-buffer)
479 (not (fboundp 'edebug-emacs-eval-current-buffer)))
480 (progn
481 (fset 'edebug-emacs-eval-current-buffer
482 (symbol-function 'eval-current-buffer))
483 (fset 'eval-current-buffer 'edebug-eval-current-buffer)))
484(if (and (fboundp 'eval-buffer)
485 (not (fboundp 'edebug-emacs-eval-buffer)))
486 (progn
487 (fset 'edebug-emacs-eval-buffer
488 (symbol-function 'eval-buffer))
489 (fset 'eval-buffer 'edebug-eval-buffer)))
490
84fc2cfa
ER
491
492\f
493;;;======================================================================
494;;; The Parser
495;;; ----------
496
497;;; The top level function for parsing defuns is edebug-defun; it
498;;; calls all the rest. It checks the syntax a bit and leaves point
499;;; at any error it finds, but otherwise should appear to work like
500;;; eval-defun.
501
502;;; The basic plan is to surround each expression with a call to the
503;;; function edebug together with indexes into a table of positions of
504;;; all expressions. Thus an expression "exp" in function foo
505;;; becomes:
506
507;;; (edebug 1 2 'exp)
508
509;;; First point moved to to the beginning of exp (offset 1 of the
510;;; current function). Then the expression is evaluated and point is
511;;; moved to offset 2, at the end of exp.
512
513;;; The top level expressions of the function are wrapped in a call to
514;;; edebug-enter, which supplies the function name and the actual
515;;; arguments to the function. See functions edebug and edebug-enter
516;;; for more details.
517
518
daa37602 519;;;###autoload
84fc2cfa
ER
520(defun edebug-defun ()
521 "Evaluate defun or defmacro, like eval-defun, but with edebug calls.
522Print its name in the minibuffer and leave point after any error it finds,
523with mark at the original point."
524 (interactive)
525 (let (def-kind ; whether defmacro or defun
526 def-name
527 def-args
528 def-docstring
529 defun-interactive
530 (edebug-offset-index 0)
531 edebug-offset-list
532 edebug-func-mark
533 (starting-point (point))
534 tmp-point
535 (parse-sexp-ignore-comments t))
536
537 (condition-case err
538 (progn
539 (end-of-defun)
540 (beginning-of-defun)
541 (down-list 1)
542
543 (setq edebug-func-mark (point-marker))
544 (if (not (eq 'defun (setq def-kind (edebug-read-sexp))))
545 (if (not (eq 'defmacro def-kind))
546 (edebug-syntax-error "%s is not a defun or defmacro."
547 def-kind)))
548 (setq def-name (edebug-read-sexp))
549 (if (not (symbolp def-name))
550 (edebug-syntax-error "Bad defun name: %s" def-name))
551 (setq def-args (edebug-read-sexp))
552 (if (not (listp def-args))
553 (edebug-syntax-error "Bad defun arg list: %s" def-args))
554
555 ;; look for doc string
556 (setq tmp-point (point))
557 (if (eq 'string (edebug-next-token-class))
558 (progn
559 (setq def-docstring (edebug-read-sexp))
560 (setq tmp-point (point))))
561
562 ;; look for interactive form
563 (if (eq 'lparen (edebug-next-token-class))
564 (progn
565 (forward-char 1) ; skip \(
566 (if (eq 'interactive (edebug-read-sexp))
567 (progn
568 (setq defun-interactive
569 (cons 'interactive (edebug-interactive)))
570 (forward-char 1) ; skip \)
571 (setq tmp-point (point))
572 ))))
573
574 (goto-char tmp-point)
575
576 ;; build the new definition
577 (fset def-name (` (lambda
578 (, def-args)
579 (, def-docstring)
580 (, defun-interactive)
581 ;; the remainder is a list of sexps
582 (edebug-enter
583 (quote (, def-name))
584 (quote (, def-args))
585 (quote (progn
586 (,@ (edebug-sexp-list t)))))
587 )))
588 ;; if it is a defmacro, prepend 'macro
589 (if (eq 'defmacro def-kind)
590 (fset def-name (cons 'macro (symbol-function def-name))))
591
592 ;; recover point, like save-excursion but only if no error occurs
593 (goto-char starting-point)
594
595 ;; store the offset list in functions property list
596 (put def-name 'edebug
597 (list edebug-func-mark
598 nil ; clear breakpoints
599 (vconcat (nreverse edebug-offset-list))))
600 (message "edebug: %s" def-name)
601 ) ; progn
602
603 (invalid-read-syntax
604 ;; Set mark at starting-point so user can return.
605 ;; Leave point at error.
606 (save-excursion
607 (goto-char starting-point)
608 (set-mark-command nil))
609 (message "Syntax error: %s" (cdr err))
610;; (signal 'invalid-read-syntax (cdr err)) ; pass it on, to who?
611 )
612 ) ; condition-case
613 def-name
614 ))
615
616
617(defun edebug-sexp-list (debuggable)
618 "Return an edebug form built from the sexp list following point in the
619current buffer. If DEBUGGABLE then wrap edebug calls around each sexp.
620The sexp list does not start with a left paren; we are already in the list.
621Leave point at (before) the trailing right paren."
622 (let (sexp-list)
623 (while (not (eq 'rparen (edebug-next-token-class)))
624 (setq sexp-list (cons (if debuggable
625 (edebug-form)
626 (edebug-read-sexp))
627 sexp-list)))
628 (nreverse sexp-list)))
629
630
631(defun edebug-increment-offset ()
632 ;; accesses edebug-offset-index and edebug-offset-list
633 (setq edebug-offset-index (1+ edebug-offset-index))
634 (setq edebug-offset-list (cons (- (point) edebug-func-mark)
635 edebug-offset-list)))
636
637
638(defun edebug-make-edebug-form (index form)
639 "Return the edebug form for the current function at offset INDEX given FORM.
640Looks like: (edebug def-name INDEX edebug-offset-index 'FORM).
641Also increment the offset index."
642 (prog1
643 (list 'edebug
644 index
645 edebug-offset-index
646 (list 'quote form))
647 (edebug-increment-offset)
648 ))
649
650
651(defun edebug-form ()
652 "Return the debug form for the following form. Add the point offset
653to the edebug-offset-list for the function and move point to
654immediately after the form."
655 (let* ((index edebug-offset-index)
656 form class)
657 ;; The point must be added to the offset list now
658 ;; because edebug-list will add more offsets indirectly.
659 (edebug-skip-whitespace)
660 (edebug-increment-offset)
661 (setq class (edebug-next-token-class))
662 (cond
663 ((eq 'lparen class)
664 (edebug-make-edebug-form index (edebug-list)))
665
666 ((eq 'symbol class)
667 (if (and (not (memq (setq form (edebug-read-sexp)) '(nil t)))
668 ;; note: symbol includes numbers, see parsing utilities
669 (not (numberp form)))
670 (edebug-make-edebug-form index form)
671 form))
672 (t (edebug-read-sexp)))))
673
674
675(defun edebug-list ()
676 "Return an edebug form built from the list form that follows point.
677Insert debug calls as appropriate to the form. Start with point at
678the left paren. Leave point after the right paren."
679 (let ((beginning (point))
680 class
681 head)
682
683 (forward-char 1) ; skip \(
684 (setq class (edebug-next-token-class))
685 (cond
686 ((eq 'symbol class)
687 (setq head (edebug-read-sexp)))
688 ((eq 'lparen class)
689 (setq head (edebug-anonymous)))
690 ((eq 'rparen class)
691 (setq head nil))
692 (t (edebug-syntax-error
693 "Head of list must be a symbol or lambda expression.")))
694
695 (prog1
696 (if head
697 (cons head
698 (cond
699
700;; None of the edebug-form-hooks defined below are used, for speed.
701;; They are included for documentation, though the hook would not
702;; necessarily behave the same as the function it is replacing.
703
704;;; Using the edebug-form-hooks should work, but would take more time.
705;;; ((symbolp head)
706;;; (let ((form (get head 'edebug-form-hook)))
707;;; (if form
708;;; (edebug-form-parser form)
709;;; (if (edebug-macrop head)
710;;; (if edebug-eval-macro-args
711;;; (edebug-sexp-list t)
712;;; (edebug-sexp-list nil))
713;;; ;; assume it is a function
714;;; (edebug-sexp-list t)))))
715
716 ;; handle all special-forms with unevaluated arguments
717 ((memq head '(let let*)) (edebug-let))
718 ((memq head '(setq setq-default)) (edebug-setq))
719 ((eq head 'cond) (edebug-cond))
720 ((eq head 'condition-case) (edebug-condition-case))
721
722 ((memq head '(quote ; permits more than one arg
723 defun defvar defconst defmacro))
724 (edebug-sexp-list nil))
725 ((eq head 'function)
726 (list
727 (if (eq 'lparen (edebug-next-token-class))
728 (edebug-anonymous)
729 (edebug-read-sexp) ; should be just a symbol
730 )))
731
732 ;; is it a lisp macro?
733 ((edebug-macrop head)
734 (or (and (symbolp head)
735 (let ((form (get head 'edebug-form-hook)))
736 (if form
737 (if (eq form t)
738 (edebug-sexp-list t)
739 (if (eq form 0)
740 (edebug-sexp-list nil)
741 (edebug-form-parser form))))))
742 (edebug-sexp-list edebug-eval-macro-args)))
743
744 ((eq head 'interactive)
745 (edebug-syntax-error "interactive not expected here."))
746
747 ;; otherwise it is a function call
748 (t (edebug-sexp-list t))
749 )))
750
751 (if (eq 'rparen (edebug-next-token-class))
752 (forward-char 1) ; skip \)
753 (edebug-syntax-error "Too many arguments."))
754 )))
755
756
757(defun edebug-form-parser (args)
758 "Parse the macro arguments that follow based on ARGS.
759ARGS describes the types of the arguments of a list form. Each of the ARGS
760is processed left to right, in the same order as the arguments of the
761list form. See the edebug documentation for more details. The ARGS
762may be one of the following:
763
764 symbolp - an unevaluated symbol
765 integerp - an unevaluated number
766 stringp - an unevaluated string
767 vectorp - an unevaluated vector
768 atom - an unevaluated number, string, symbol, or vector
769
770 sexp - an unevaluated sexp (atom or list); may not be empty
771 form - an evaluated sexp; may not be empty
772
773 foo - any other symbol should be the name of a function; this
774 function is called on the argument as a predicate and an error
775 is signaled if the predicate fails.
776
777 &optional - one following arg in the list may or may not appear.
778 &rest - all following args are repeated zero or more times as a group.
779 This is an extension of the normal meaning of &rest.
780 &or - each of the following args are alternatives, processed left to
781 right until one succeeds. There is no way to group
782 more than one list element as one alternative.
783
784 (...) - a sublist, of the same format as the top level, processed recursively.
785 Special case: if the car of the list is quote, the argument must match
786 the quoted sexp (see example below of 'for macro).
787"
788
789 (let ((arglist args)
790 arg form form-list class
791 &optional &rest &or)
792 (while (and arglist
793 (not (eq 'rparen (setq class (edebug-next-token-class)))))
794 (catch 'no-match
795 (setq arg (car arglist))
796 (setq arglist (cdr arglist))
797 (if (and &rest (null arglist))
798 (setq arglist &rest))
799
800 (cond
801 ((memq arg '(&optional &rest &or))
802 ;; remember arglist at this point
803 (set arg arglist)
804 (throw 'no-match nil))
805
806 ((eq arg 'form)
807 (setq form (edebug-form)))
808
809 ((eq arg 'sexp)
810 (setq form (edebug-read-sexp)))
811
812 ((listp arg)
813 (if (eq 'quote (car arg))
814 ;; special case, match the quoted symbol
815 (let ((pnt (point)))
816 (setq arg (car (cdr arg)))
817 (if (not (eq arg (setq form (edebug-read-sexp))))
818 (edebug-form-parser-error)
819 ))
820 (if (eq class 'lparen)
821 (progn
822 (forward-char 1) ; skip \(
823 (setq form (edebug-form-parser arg))
824 (forward-char 1) ; skip \)
825 ))))
826 ((symbolp arg)
827 (let ((pnt (point))
828 (pred (if (fboundp arg) (symbol-function arg))))
829 (and pred
830 (not (funcall pred (setq form (edebug-read-sexp))))
831 (edebug-form-parser-error)
832 )))
833 (t (throw 'no-match nil))
834 ) ; cond
835 (setq &optional nil) ; only lasts for one match
836 (setq form-list (cons form form-list)) ; skipped by no-match throw
837 )) ; while
838
839 (if (and arglist (not (or &optional &rest
840 (memq (car arglist) '(&optional &rest)))))
841 (edebug-syntax-error "Not enough arguments."))
842 (if (not (eq 'rparen (edebug-next-token-class)))
843 (if &or
844 (edebug-syntax-error "Unrecognized argument.")
845 (edebug-syntax-error "Too many arguments.")))
846 (nreverse form-list)))
847
848
849(defun edebug-form-parser-error ()
850 (goto-char pnt)
851 (if &or
852 (throw 'no-match nil)
853 (if &optional
854 (progn
855 (setq &optional nil) ; only lasts for one failed match not in &or
856 (throw 'no-match nil))
857 (edebug-syntax-error "%s is not %s" form arg))))
858
3a801d0c 859;; for loop defined in Emacs Lisp manual
84fc2cfa
ER
860(put 'for 'edebug-form-hook
861 '(symbolp 'from form 'to form 'do &rest form))
862
863;; case and do defined in cl.el
864(put 'case 'edebug-form-hook
865 '(form &rest (sexp form)))
866
867(put 'do 'edebug-form-hook
868 '((&rest
869 &or symbolp
870 (symbolp &optional form
871 &optional form))
872 (form &rest form)
873 &rest body))
874
875(put 'defvar 'edebug-form-hook
876 (put 'defconst 'edebug-form-hook
877 '(symbolp &optional form &optional stringp)))
878
879(put 'defun 'edebug-form-hook
880 (put 'defmacro 'edebug-form-hook
881 '(symbolp (&rest symbolp)
882 &optional stringp
883 &optional ('interactive &or stringp form)
884 &rest form)))
885
886(put 'anonymous 'edebug-form-hook
887 '(&optional 'macro 'lambda (&rest symbolp) &rest form))
888
889(defun edebug-anonymous ()
890 "Return the edebug form for an anonymous lambda or macro.
891Point starts before the left paren and ends after it."
892 (forward-char 1) ; skip \(
893 (prog1
894 (let ((head (edebug-read-sexp)))
895 (cond
896 ((eq head 'lambda)
897 (edebug-lambda))
898 ((eq head 'macro)
899 (if (not (eq 'lambda (edebug-read-sexp)))
900 (edebug-syntax-error "lambda expected."))
901 (cons 'macro (edebug-lambda)))
902 (t (edebug-syntax-error "Anonymous lambda or macro expected."))))
903 (forward-char 1) ; skip \)
904 ))
905
906
907(defun edebug-lambda ()
908 "Return the edebug form for the lambda form that follows.
909Point starts after the lambda symbol and is moved to before the right paren."
910 (append
911 (list 'lambda (edebug-read-sexp)) ; the args
912 (edebug-sexp-list t))) ; the body
913
914
915
916(put 'let 'edebug-form-hook
917 (put 'let* 'edebug-form-hook
918 '((&rest
919 &or (symbolp &optional form)
920 symbolp)
921 &rest form)))
922
923(defun edebug-let ()
924 "Return the edebug form of the let or let* form.
925Leave point before the right paren."
926 (let (var-value-list
927 token
928 class)
929 (cons
930 ;; first process the var/value list
931 (if (not (eq 'lparen (edebug-next-token-class)))
932 (if (setq token (edebug-read-sexp))
933 (edebug-syntax-error "Bad var list in let.") ; should be nil
934 token ; == nil
935 )
936
937 (forward-char 1) ; lparen
938 (while (not (eq 'rparen (setq class (edebug-next-token-class))))
939 (setq var-value-list
940 (cons
941 (if (not (eq 'lparen class))
942 (edebug-read-sexp)
943 (forward-char 1) ; lparen
944 (prog1
945 (edebug-var-value)
946 (if (not (eq 'rparen (edebug-next-token-class)))
947 (edebug-syntax-error "Right paren expected in let.")
948 (forward-char 1) ; rparen
949 )))
950 var-value-list)))
951 (forward-char 1) ; rparen
952 (nreverse var-value-list))
953
954 ;; now process the expression list
955 (edebug-sexp-list t))))
956
957
958(defun edebug-var-value ()
959 "Return the edebug form of the var and optional value that follow point.
960Leave point after the value, if there is one."
961 (list
962 (edebug-read-sexp) ; the variable
963 (and (not (eq 'rparen (edebug-next-token-class)))
964 (edebug-form))))
965
966
967(put 'setq 'edebug-form-hook
968 (put 'setq-default 'edebug-form-hook
969 '(&rest symbolp form)))
970
971(defun edebug-setq ()
972 "Return the edebug form of the setq or setq-default var-value list."
973 (let (var-value-list)
974 (while (not (eq 'rparen (edebug-next-token-class)))
975 (setq var-value-list
976 (append var-value-list
977 (edebug-var-value))))
978 var-value-list))
979
980
981(put 'interactive 'edebug-form-hook
982 '(&optional &or stringp form))
983
984(defun edebug-interactive ()
985 "Return the edebug form of the interactive form."
986 (list
987 (if (not (eq 'rparen (edebug-next-token-class)))
988 (if (eq 'string (edebug-next-token-class))
989 (edebug-read-sexp)
990 (prog1
991 (` (edebug-interactive-entry
992 (quote (, def-name))
993 (quote ((,@ (edebug-form))))))
994 (if (not (eq 'rparen (edebug-next-token-class)))
995 (edebug-syntax-error
996 "Only first expression used in interactive form.")))))))
997
998
999(put 'cond 'edebug-form-hook
1000 '(&rest (form &rest form)))
1001
1002(defun edebug-cond ()
1003 "Return the edebug form of the cond form."
1004 (let (value-value-list
1005 class)
1006 (while (not (eq 'rparen (setq class (edebug-next-token-class))))
1007 (setq value-value-list
1008 (cons
1009 (if (not (eq 'lparen class))
1010 (let ((thing (edebug-read-sexp)))
1011 (if thing
1012 (edebug-syntax-error "Condition expected in cond")
1013 nil))
1014 (forward-char 1) ; \(
1015 (prog1
1016 (cons
1017 (edebug-form)
1018 (if (eq 'rparen (edebug-next-token-class))
1019 nil
1020 (edebug-sexp-list t)))
1021 (if (not (eq 'rparen (edebug-next-token-class)))
1022 (edebug-syntax-error "Right paren expected in cond"))
1023 (forward-char 1) ; \)
1024 ))
1025 value-value-list)))
1026 (nreverse value-value-list)))
1027
1028
eb8c3be9 1029;; Bug: this doesn't support condition name lists
84fc2cfa
ER
1030(put 'condition-case 'edebug-form-hook
1031 '(symbolp
1032 form
1033 &rest (symbolp &optional form)))
1034
1035(defun edebug-condition-case ()
1036 "Return the edebug form of the condition-case form."
1037 (cons
1038 (let (token)
1039 ;; read the variable or nil
1040 (setq token (edebug-read-sexp))
1041 (if (not (symbolp token))
1042 (edebug-syntax-error
1043 "Variable or nil required for condition-case; found: %s" token))
1044 token)
1045
1046 (cons
1047 (edebug-form) ; the form
1048
1049 ;; process handlers
1050 (let (symb-sexp-list
1051 class)
1052 (while (not (eq 'rparen (setq class (edebug-next-token-class))))
1053 (setq symb-sexp-list
1054 (cons
1055 (if (not (eq 'lparen class))
1056 (edebug-syntax-error "Bad handler in condition-case.")
1057 (forward-char 1) ; \(
1058 (prog1
1059 (cons
1060 (edebug-read-sexp) ; the error-condition
1061 (and (not (eq 'rparen (edebug-next-token-class)))
1062 (edebug-sexp-list t)))
1063 (forward-char 1) ; \)
1064 ))
1065 symb-sexp-list)))
1066 (nreverse symb-sexp-list)))))
1067
1068
1069\f
1070;;------------------------------------------------
1071;; Parser utilities
1072
1073(defun edebug-syntax-error (msg &rest args)
1074 "Signal an invalid-read-syntax with MSG and ARGS.
1075 This is caught by edebug-defun."
1076 (signal 'invalid-read-syntax (apply 'format msg args)))
1077
1078
1079(defun edebug-skip-whitespace ()
1080 "Leave point before the next token, skipping white space and comments."
1081 (skip-chars-forward " \t\r\n\f")
1082 (while (= (following-char) ?\;)
1083 (skip-chars-forward "^\n\r") ; skip the comment
1084 (skip-chars-forward " \t\r\n\f")))
1085
1086(defun edebug-read-sexp ()
1087 "Read one sexp from the current buffer starting at point.
1088Leave point immediately after it. A sexp can be a list or atom.
1089An atom is a symbol (or number), character, string, or vector."
1090 ;; This is gummed up by parser inconsistencies (bugs?)
1091 (let (token)
1092 (edebug-skip-whitespace)
1093 (if (or (= (following-char) ?\[) (= (following-char) ??))
1094 ;; scan-sexps doesn't read vectors or character literals correctly,
1095 ;; but read does.
1096 (setq token (read (current-buffer)))
1097 (goto-char
1098 (min ; use the lesser of the read and scan-sexps motion
1099 ;; read goes one too far if (quoted) string or symbol
1100 ;; is immediately followed by non-whitespace
1101 (save-excursion
1102 (setq token (read (current-buffer)))
1103 (point))
1104 ;; scan-sexps reads too far if a quoting character is read
1105 (scan-sexps (point) 1))))
1106 token))
1107
1108(defconst edebug-syntax-table
1109 (let ((table (make-vector 256 'symbol)))
1110 ;; Treat numbers as symbols, because of confusion with -, -1, and 1-.
1111 (aset table ?\( 'lparen)
1112 (aset table ?\) 'rparen)
1113 (aset table ?\' 'quote)
1114 (aset table ?\" 'string)
1115 (aset table ?\? 'char)
1116 (aset table ?\[ 'vector)
1117 (aset table ?\. 'dot)
1118 ;; We dont care about any other chars since they wont be seen.
1119 table)
1120 "Lookup table for the token class of each character.")
1121
1122(defun edebug-next-token-class ()
1123 "Move to the next token and return its class. We only care about
1124lparen, rparen, dot, quote, string, char, vector, or symbol."
1125 (edebug-skip-whitespace)
1126 (aref edebug-syntax-table (following-char)))
1127
1128\f
1129;;;=================================================================
1130;;; The debugger itself
1131;;; -------------------
1132
1133
1134(defvar edebug-active nil
1135 "Non-nil when edebug is active")
1136
1137
1138;;; add minor-mode-alist entry
1139(or (assq 'edebug-active minor-mode-alist)
1140 (setq minor-mode-alist (cons (list 'edebug-active " *Debugging*")
1141 minor-mode-alist)))
1142
1143(defvar edebug-backtrace nil
1144 "Stack of active functions evaluated via edebug.
1145Should be nil at the top level.")
1146
1147(defvar edebug-offset-indices nil ; not used yet.
1148 "Stack of offset indices of visited edebug sexps.
1149Should be nil at the top level.")
1150
1151(defvar edebug-entered nil
1152 "Non-nil if edebug has already been entered at this recursive edit level.")
1153
1154
1155(defun edebug-enter (edebug-func edebug-args edebug-body)
1156 "Entering FUNC. The arguments are ARGS, and the body is BODY.
1157Setup edebug variables and evaluate BODY. This function is called
1158when a function evaluated with edebug-defun is entered. Return the
1159result of BODY."
1160
1161 ;; Is this the first time we are entering edebug since
1162 ;; lower-level recursive-edit command?
1163 (if (and (not edebug-entered)
1164 edebug-initial-mode)
1165 ;; Reset edebug-mode to the initial mode.
1166 (setq edebug-mode edebug-initial-mode))
1167 (let* ((edebug-entered t)
9c0415e0
BF
1168 (pre-command-hook (if (memq edebug-func pre-command-hook)
1169 nil pre-command-hook))
1170 (post-command-hook (if (memq edebug-func post-command-hook)
1171 nil post-command-hook))
84fc2cfa
ER
1172 (edebug-data (get edebug-func 'edebug))
1173 ;; pull out parts of the edebug-data
1174 (edebug-func-mark (car edebug-data)) ; mark at function start
1175
1176 (edebug-buffer (marker-buffer edebug-func-mark))
1177 (edebug-backtrace (cons edebug-func edebug-backtrace))
1178 (max-lisp-eval-depth (+ 6 max-lisp-eval-depth)) ; too much??
1179 (max-specpdl-size (+ 10 max-specpdl-size)) ; the args and these vars
1180 )
1181 (if edebug-trace
1182 (let ((edebug-stack-depth (1- (length edebug-backtrace)))
1183 edebug-result)
1184 (edebug-print-trace-entry
1185 "*edebug-trace*" edebug-func edebug-args edebug-stack-depth)
1186 (setq edebug-result (eval edebug-body))
1187 (edebug-print-trace-exit
1188 "*edebug-trace*" edebug-func edebug-result edebug-stack-depth)
1189 edebug-result)
1190 (eval edebug-body)
1191 )))
1192
1193(defun edebug-interactive-entry (edebug-func edebug-args)
1194 "Evaluating FUNCs non-string argument of interactive form ARGS."
1195 (if (and (not edebug-entered)
1196 edebug-initial-mode)
1197 ;; Reset edebug-mode to the initial mode.
1198 (setq edebug-mode edebug-initial-mode))
1199 (let* ((edebug-entered t)
1200 (edebug-data (get edebug-func 'edebug))
1201 ;; pull out parts of the edebug-data
1202 (edebug-func-mark (car edebug-data)) ; mark at function start
1203
1204 (edebug-buffer (marker-buffer edebug-func-mark))
1205;; (edebug-backtrace (cons edebug-func edebug-backtrace))
1206 )
1207 (eval edebug-args)))
1208
1209
1210(defun edebug-print-trace-entry
1211 (edebug-stream edebug-function edebug-args edebug-stack-depth)
1212 (edebug-trace-display
1213 edebug-stream
1214 "%sEnter: %s\n" (make-string edebug-stack-depth ?\ ) edebug-function)
1215 )
1216
1217(defun edebug-print-trace-exit
1218 (edebug-stream edebug-function edebug-result edebug-stack-depth)
1219 (edebug-trace-display
1220 edebug-stream
1221 "%sExit: %s\n" (make-string edebug-stack-depth ?\ ) edebug-function)
1222 )
1223
1224
1225(defun edebug (edebug-before-index edebug-after-index edebug-exp)
1226 "Debug current function given BEFORE and AFTER positions around EXP.
1227BEFORE and AFTER are indexes into the position offset vector in the
1228functions 'edebug property. edebug is called from functions compiled
1229with edebug-defun."
1230 (let ((max-lisp-eval-depth (+ 5 max-lisp-eval-depth)) ; enough??
1231 (max-specpdl-size (+ 7 max-specpdl-size)) ; the args and these vars
1232 (edebug-offset-indices
1233 (cons edebug-before-index edebug-offset-indices))
1234 ;; Save the outside value of executing macro.
1235 (edebug-outside-executing-macro executing-macro)
1236 ;; Don't keep reading from an executing kbd macro within edebug!
1237 (executing-macro nil)
1238 )
1239 (if (and (eq edebug-mode 'Go-nonstop)
1240 (not (edebug-input-pending-p)))
1241 ;; Just return evalled expression.
1242 (eval edebug-exp)
1243 (edebug-debugger edebug-before-index 'enter edebug-exp)
1244 (edebug-debugger edebug-after-index 'exit (eval edebug-exp))
1245 )))
1246
1247
1248(defun edebug-debugger (edebug-offset-index edebug-arg-mode edebug-exp)
1249 "Determine if edebug display should be updated."
1250 (let* (
1251 ;; This needs to be here since breakpoints may be changed.
1252 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
1253 (edebug-break-data (assq edebug-offset-index edebug-breakpoints))
1254 (edebug-break
1255 (if edebug-break-data
1256 (let ((edebug-break-condition
1257 (car (cdr edebug-break-data))))
1258 (or (not edebug-break-condition)
1259 (eval edebug-break-condition)))))
1260 )
1261 (if (and edebug-break
1262 (car (cdr (cdr edebug-break-data)))) ; is it temporary?
1263 ;; Delete the breakpoint.
1264 (setcdr edebug-data
1265 (cons (delq edebug-break-data edebug-breakpoints)
1266 (cdr (cdr edebug-data)))))
1267
1268 ;; Dont do anything if mode is go, continue, or Continue-fast
1269 ;; and no break, and no input.
1270 (if (or (and (not (memq edebug-mode '(go continue Continue-fast)))
1271 (or edebug-stop-before-symbols
1272 (not (and (eq edebug-arg-mode 'enter)
1273 (symbolp edebug-exp)))))
1274 (edebug-input-pending-p)
1275 edebug-break)
1276 (edebug-display))
1277
1278 edebug-exp
1279 ))
1280
1281
1282(defvar edebug-window-start 0
1283 "Remember where each buffers' window starts between edebug calls.
1284This is to avoid spurious recentering.")
1285
1286(setq-default edebug-window-start 0)
1287(make-variable-buffer-local 'edebug-window-start)
1288
1289(defun edebug-display ()
1290 "Setup windows for edebug, determine mode, maybe enter recursive-edit."
1291 ;; uses local variables of edebug-enter, edebug, and edebug-debugger.
1292 (let ((edebug-active t) ; for minor mode alist
1293 edebug-stop ; should we enter recursive-edit
1294 (edebug-point (+ edebug-func-mark
1295 (aref (car (cdr (cdr edebug-data)))
1296 edebug-offset-index)))
1297 (edebug-buffer-points
1298 (if edebug-save-buffer-points (edebug-get-buffer-points)))
1299 edebug-window ; window displaying edebug-buffer
1300 edebug-inside-window ; window displayed after recursive edit
1301 (edebug-outside-window (selected-window))
1302 (edebug-outside-buffer (current-buffer))
1303 (edebug-outside-point (point))
85dd3810 1304 (edebug-outside-mark (mark t))
84fc2cfa
ER
1305 edebug-outside-windows ; window or screen configuration
1306 edebug-outside-edebug-point ; old point in edebug buffer
1307 edebug-outside-edebug-mark
1308
1309 edebug-eval-buffer ; declared here so we can kill it below
1310 (edebug-eval-result-list (and edebug-eval-list
1311 (edebug-eval-result-list)))
1312 (edebug-outside-o-a-p overlay-arrow-position)
1313 (edebug-outside-o-a-s overlay-arrow-string)
1314 (edebug-outside-c-i-e-a cursor-in-echo-area)
1315
1316 edebug-outside-point-min
1317 edebug-outside-point-max
1318
1319 overlay-arrow-position
1320 overlay-arrow-string
1321 (cursor-in-echo-area nil)
1322 ;; any others??
1323 )
1324 (if (not (buffer-name edebug-buffer))
1325 (let (debug-on-error nil)
1326 (error "Buffer defining %s not found." edebug-func)))
1327
1328 ;; Save windows now before we modify them.
1329 (if edebug-save-windows
1330 (setq edebug-outside-windows
1331 (edebug-current-window-configuration)))
1332
1333 ;; If edebug-buffer is not currently displayed,
1334 ;; first find a window for it.
1335 (edebug-pop-to-buffer edebug-buffer)
1336 (setq edebug-window (selected-window))
1337
1338 ;; Now display eval list, if any.
1339 ;; This is done after the pop to edebug-buffer
1340 ;; so that buffer-window correspondence is correct after quit.
1341 (edebug-eval-display edebug-eval-result-list)
1342 (select-window edebug-window)
1343
1344 (if edebug-save-point
1345 (progn
1346 (setq edebug-outside-edebug-point (point))
85dd3810 1347 (setq edebug-outside-edebug-mark (mark t))))
84fc2cfa
ER
1348
1349 (edebug-save-restriction
1350 (setq edebug-outside-point-min (point-min))
1351 (setq edebug-outside-point-max (point-max))
1352 (widen)
1353 (goto-char edebug-point)
1354
1355 (setq edebug-window-start
1356 (edebug-adjust-window edebug-window-start))
1357
1358 (if (edebug-input-pending-p) ; not including keyboard macros
1359 (progn
1360 (setq edebug-mode 'step)
1361 (setq edebug-stop t)
1362 (edebug-stop)
1363 ;; (discard-input) ; is this unfriendly??
1364 ))
1365 (edebug-overlay-arrow)
1366
1367 (cond
1368 ((eq 'exit edebug-arg-mode)
1369 ;; Display result of previous evaluation.
1370 (setq edebug-previous-result edebug-exp)
1371 (edebug-previous-result))
1372
1373 ((eq 'error edebug-arg-mode)
1374 ;; Display error message
1375 (beep)
1376 (if (eq 'quit (car edebug-exp))
1377 (message "Quit")
1378 (message "%s: %s"
1379 (get (car edebug-exp) 'error-message)
1380 (car (cdr edebug-exp)))))
1381
1382 (edebug-break
1383 (message "Break"))
1384 (t (message "")))
1385
1386 (if edebug-break
1387 (if (not (memq edebug-mode '(continue Continue-fast)))
1388 (setq edebug-stop t)
1389 (if (eq edebug-mode 'continue)
1390 (edebug-sit-for 1)
1391 (edebug-sit-for 0)))
1392 ;; not edebug-break
1393 (if (eq edebug-mode 'trace)
1394 (edebug-sit-for 1) ; Force update and pause.
1395 (if (eq edebug-mode 'Trace-fast)
1396 (edebug-sit-for 0) ; Force update and continue.
1397 )))
1398
1399 (unwind-protect
1400 (if (or edebug-stop
1401 (eq edebug-mode 'step)
1402 (eq edebug-arg-mode 'error))
1403 (progn
1404 (setq edebug-mode 'step)
eb8c3be9 1405 (edebug-overlay-arrow) ; this doesn't always show up.
84fc2cfa
ER
1406 (edebug-recursive-edit));; <<<<<< Recursive edit
1407 )
1408
1409 (if edebug-save-buffer-points
1410 (edebug-set-buffer-points))
1411 ;; Since we may be in a save-excursion, in case of quit
1412 ;; restore the outside window only.
1413 (select-window edebug-outside-window)
1414 ) ; unwind-protect
1415
1416 ;; None of the following is done if quit or signal occurs.
1417 (if edebug-save-point
1418 ;; Restore point and mark in edebug-buffer.
1419 ;; This does the save-excursion recovery only if no quit.
1420 ;; If edebug-buffer == edebug-outside-buffer,
1421 ;; then this is redundant with outside save-excursion.
1422 (progn
1423 (set-buffer edebug-buffer)
1424 (goto-char edebug-outside-edebug-point)
1425 (if (mark-marker)
1426 (set-marker (mark-marker) edebug-outside-edebug-mark))
1427 ))
1428 ) ; edebug-save-restriction
1429
1430 ;; Restore windows, buffer, point, and mark.
1431 (if edebug-save-windows
1432 ;; Restore windows before continuing.
1433 (edebug-set-window-configuration edebug-outside-windows))
1434 (set-buffer edebug-outside-buffer)
1435 (goto-char edebug-outside-point)
1436 (if (mark-marker)
1437 (set-marker (mark-marker) edebug-outside-mark))
1438 ;; The following is not sufficient, and sometimes annoying.
1439 ;; (if (memq edebug-mode '(go Go-nonstop))
1440 ;; (edebug-sit-for 0))
1441 ))
1442
1443
1444(defvar edebug-depth 0
1445 "Number of recursive edits started by edebug.
1446Should be 0 at the top level.")
1447
1448(defvar edebug-recursion-depth 0
1449 "Value of recursion-depth when edebug was called.")
1450
1451
1452(defun edebug-recursive-edit ()
1453 "Start up a recursive edit inside of edebug."
1454 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
1455 (let ((edebug-buffer-read-only buffer-read-only)
1456 ;; match-data must be done in the outside buffer
1457 (edebug-outside-match-data
1458 (save-excursion
1459 (set-buffer edebug-outside-buffer)
1460 (match-data)))
1461
1462 (edebug-depth (1+ edebug-depth))
1463 (edebug-recursion-depth (recursion-depth))
1464 edebug-entered ; bind locally to nil
1465 edebug-backtrace-buffer ; each recursive edit gets its own
1466 ;; The window configuration may be saved and restored
1467 ;; during a recursive-edit
1468 edebug-inside-windows
1469
1470 (edebug-outside-map (current-local-map))
1471 (edebug-outside-standard-output standard-output)
1472 (edebug-outside-standard-input standard-input)
1473
1474 (edebug-outside-last-command-char last-command-char)
1475 (edebug-outside-last-command last-command)
1476 (edebug-outside-this-command this-command)
1477 (edebug-outside-last-input-char last-input-char)
1478;; (edebug-outside-unread-command-char unread-command-char)
1479
1480 ;; Declare the following local variables to protect global values.
1481 ;; We could set these to the values for previous edebug call.
1482 ;; But instead make it local, but use global value.
1483 (last-command-char last-command-char)
1484 (last-command last-command)
1485 (this-command this-command)
1486 (last-input-char last-input-char)
dbc4e1c1 1487 ;; Assume no edebug command sets unread-command-events.
84fc2cfa
ER
1488;; (unread-command-char -1)
1489
1490 (debug-on-error debug-on-error)
1491
1492 ;; others??
1493 )
1494
1495 (if (and (eq edebug-mode 'go)
1496 (not (memq edebug-arg-mode '(exit error))))
1497 (message "Break"))
1498 (edebug-mode)
1499 (if (boundp 'edebug-outside-debug-on-error)
1500 (setq debug-on-error edebug-outside-debug-on-error))
1501
1502 (setq buffer-read-only t)
1503 (unwind-protect
1504 (recursive-edit) ; <<<<<<<<<< Recursive edit
1505
1506 ;; Do the following, even if quit occurs.
1507 (if edebug-backtrace-buffer
1508 (kill-buffer edebug-backtrace-buffer))
1509 ;; Could be an option to keep eval display up.
1510 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
1511
1512 ;; Remember selected-window after recursive-edit.
1513 (setq edebug-inside-window (selected-window))
1514
1515 (store-match-data edebug-outside-match-data)
1516
1517 ;; Recursive edit may have changed buffers,
1518 ;; so set it back before exiting let.
1519 (if (buffer-name edebug-buffer) ; if it still exists
1520 (progn
1521 (set-buffer edebug-buffer)
1522 (if (memq edebug-mode '(go Go-nonstop))
1523 (edebug-overlay-arrow))
1524 (setq buffer-read-only edebug-buffer-read-only)
1525 (use-local-map edebug-outside-map)
1526 ;; Remember current window-start for next visit.
1527 (select-window edebug-window)
1528 (if (eq edebug-buffer (window-buffer edebug-window))
1529 (setq edebug-window-start (window-start)))
1530 (select-window edebug-inside-window)
1531 ))
1532 )))
1533
1534\f
1535;;--------------------------
1536;; Display related functions
1537
1538(defun edebug-adjust-window (old-start)
1539 "Adjust window to fit as much as possible following point.
1540The display should prefer to start at OLD-START if point is not visible.
1541Return the new window-start."
1542 (if (not (pos-visible-in-window-p))
1543 (progn
1544 (set-window-start (selected-window) old-start)
1545 (if (not (pos-visible-in-window-p))
1546 (let ((start (window-start))
1547 (pnt (point)))
1548 (set-window-start
1549 (selected-window)
1550 (save-excursion
1551 (forward-line
1552 (if (< pnt start) -1 ; one line before
1553 (- (/ (window-height) 2)) ; center the line
1554 ))
1555 (beginning-of-line)
1556 (point)))))))
1557 (window-start))
1558
1559
1560(defconst edebug-arrow-alist
1561 '((Continue-fast . ">")
1562 (Trace-fast . ">")
1563 (continue . ">")
1564 (trace . "->")
1565 (step . "=>")
1566 (go . "<>")
1567 (Go-nonstop . "..") ; not used
1568 )
1569 "Association list of arrows for each edebug mode.
1570If you come up with arrows that make more sense, let me know.")
1571
1572(defun edebug-overlay-arrow ()
1573 "Set up the overlay arrow at beginning-of-line in current buffer.
1574The arrow string is derived from edebug-arrow-alist and edebug-mode."
1575 (let* ((pos))
1576 (save-excursion
1577 (beginning-of-line)
1578 (setq pos (point)))
1579 (setq overlay-arrow-string
1580 (cdr (assq edebug-mode edebug-arrow-alist)))
1581 (setq overlay-arrow-position (make-marker))
1582 (set-marker overlay-arrow-position pos (current-buffer))))
1583
1584
1585(put 'edebug-outside-excursion 'edebug-form-hook
1586 '(&rest form))
1587
1588(defmacro edebug-outside-excursion (&rest body)
1589 "Evaluate an expression list in the outside context.
1590Return the result of the last expression."
1591 (` (save-excursion ; of current-buffer
1592 (if edebug-save-windows
1593 (progn
1594 ;; After excursion, we will
1595 ;; restore to current window configuration.
1596 (setq edebug-inside-windows
1597 (edebug-current-window-configuration))
1598 ;; Restore outside windows.
1599 (edebug-set-window-configuration edebug-outside-windows)))
1600
1601 (set-buffer edebug-buffer)
1602 ;; Restore outside context.
1603 (let ((edebug-inside-map (current-local-map))
1604 (last-command-char edebug-outside-last-command-char)
1605 (last-command edebug-outside-last-command)
1606 (this-command edebug-outside-this-command)
1607;; (unread-command-char edebug-outside-unread-command-char)
1608 (last-input-char edebug-outside-last-input-char)
1609 (overlay-arrow-position edebug-outside-o-a-p)
1610 (overlay-arrow-string edebug-outside-o-a-s)
1611 (cursor-in-echo-area edebug-outside-c-i-e-a)
1612 (standard-output edebug-outside-standard-output)
1613 (standard-input edebug-outside-standard-input)
1614 (executing-macro edebug-outside-executing-macro)
1615 )
1616 (unwind-protect
1617 (save-restriction
1618 (narrow-to-region edebug-outside-point-min
1619 edebug-outside-point-max)
1620 (save-excursion ; of edebug-buffer
1621 (if edebug-save-point
1622 (progn
1623 (goto-char edebug-outside-edebug-point)
1624 (if (mark-marker)
1625 (set-marker (mark-marker)
1626 edebug-outside-edebug-mark))
1627 ))
1628 (use-local-map edebug-outside-map)
1629 (store-match-data edebug-outside-match-data)
1630 (select-window edebug-outside-window)
1631 (set-buffer edebug-outside-buffer)
1632 (goto-char edebug-outside-point)
1633 (,@ body)
1634 ) ; save-excursion
1635 ) ; save-restriction
1636 ;; Back to edebug-buffer. Restore rest of inside context.
1637 (use-local-map edebug-inside-map)
1638 (if edebug-save-windows
1639 ;; Restore inside windows.
1640 (edebug-set-window-configuration edebug-inside-windows))
1641 )) ; let
1642 )))
1643
1644
1645(defun edebug-toggle-save-windows ()
1646 "Toggle the edebug-save-windows variable.
1647Each time you toggle it, the inside and outside window configurations
1648become the same as the current configuration."
1649 (interactive)
1650 (if (setq edebug-save-windows (not edebug-save-windows))
1651 (setq edebug-inside-windows
1652 (setq edebug-outside-windows
1653 (edebug-current-window-configuration))))
1654 (message "Window saving is %s."
1655 (if edebug-save-windows "on" "off")))
1656
1657
1658(defun edebug-where ()
1659 "Show the debug windows and where we stopped in the program."
1660 (interactive)
1661 (if (not edebug-active)
1662 (error "edebug is not active."))
1663 (edebug-pop-to-buffer edebug-buffer)
1664 (goto-char edebug-point) ; from edebug
1665 )
1666
1667(defun edebug-view-outside ()
1668 "Change to the outside window configuration."
1669 (interactive)
1670 (if (not edebug-active)
1671 (error "edebug is not active."))
1672 (setq edebug-inside-windows (edebug-current-window-configuration))
1673 (edebug-set-window-configuration edebug-outside-windows)
1674 (goto-char edebug-outside-point)
1675 (message "Window configuration outside of edebug. Return with %s"
1676 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
1677
1678
1679(defun edebug-bounce-point ()
1680 "Bounce the point in the outside current buffer."
1681 (interactive)
1682 (if (not edebug-active)
1683 (error "edebug is not active."))
1684 (save-excursion
1685 ;; If the buffer's currently displayed, avoid the set-window-configuration.
1686 (save-window-excursion
1687 (edebug-pop-to-buffer edebug-outside-buffer)
1688 ;; (edebug-sit-for 1) ; this shouldnt be necessary
1689 (goto-char edebug-outside-point)
1690 ;; (message "current buffer: %s" (current-buffer))
1691 (edebug-sit-for 1)
1692 (edebug-pop-to-buffer edebug-buffer))))
1693
1694
1695\f
1696;;--------------------------
1697;; epoch related things
1698
1699(defvar edebug-epoch-running (and (boundp 'epoch::version) epoch::version)
1700 "non-nil if epoch is running.
1701Windows are handled a little differently under epoch.")
1702
1703
1704(defun edebug-current-window-configuration ()
f98955ea 1705 "Return the current window or frame configuration."
84fc2cfa
ER
1706 (if edebug-epoch-running
1707 (edebug-current-screen-configuration)
1708 (current-window-configuration)))
1709
1710
1711(defun edebug-set-window-configuration (conf)
f98955ea 1712 "Set the window or frame configuration to CONF."
84fc2cfa
ER
1713 (if edebug-epoch-running
1714 (edebug-set-screen-configuration conf)
1715 (set-window-configuration conf)))
1716
1717
1718(defun edebug-get-buffer-window (buffer)
1719 (if edebug-epoch-running
1720 (epoch::get-buffer-window buffer)
1721 (get-buffer-window buffer)))
1722
1723
1724(defun edebug-pop-to-buffer (buffer)
f98955ea 1725 "Like pop-to-buffer, but select a frame that buffer was shown in."
84fc2cfa
ER
1726 (let ((edebug-window (edebug-get-buffer-window buffer)))
1727 (if edebug-window
1728 (select-window edebug-window)
1729 ;; It is not currently displayed, so find some place to display it.
1730 (if edebug-epoch-running
1731 ;; Select a screen that the buffer has been displayed in before
1732 ;; or the current screen otherwise.
1733 (select-screen
1734 ;; allowed-screens in epoch 3.2, was called screens before that
1735 (or (car (symbol-buffer-value 'allowed-screens buffer))
1736 (epoch::current-screen))))
1737 (if (one-window-p)
1738 (split-window))
1739 (select-window (next-window))
1740 (set-window-buffer (selected-window) buffer)
1741 (set-window-hscroll (selected-window) 0)
1742 ))
1743 ;; Selecting the window does not set the buffer.
1744 (set-buffer buffer)
1745 )
1746
1747
1748(defun edebug-current-screen-configuration ()
1749 "Return an object recording the current configuration of Epoch screen-list.
1750The object is a list of pairs of the form (SCREEN . CONFIGURATION)
1751where SCREEN has window-configuration CONFIGURATION. The current
1752screen is the head of the list."
1753 (let ((screen-list (epoch::screen-list 'unmapped))
1754 (current-screen (epoch::get-screen))
1755 (current-buffer (current-buffer))
1756 )
1757 ;; put current screen first
1758 (setq screen-list (cons current-screen (delq current-screen screen-list)))
1759 (prog1
1760 (mapcar (function
1761 (lambda (screen)
1762 (cons screen
1763 (progn
1764 (epoch::select-screen screen)
1765 (current-window-configuration)))))
1766 screen-list)
1767 (epoch::select-screen current-screen)
1768 (set-buffer current-buffer)
1769 )))
1770
1771(defun edebug-set-screen-configuration (sc)
1772 "Set the window-configuration for all the screens in SC.
1773Set the current screen to be the head of SC."
1774 (mapcar (function
1775 (lambda (screen-conf)
1776 (if (epoch::screen-p (car screen-conf)) ; still exist?
1777 (progn
1778 (epoch::select-screen (car screen-conf))
1779 (set-window-configuration (cdr screen-conf))))))
1780 sc)
1781 (if (epoch::screen-p (car (car sc)))
1782 (epoch::select-screen (car (car sc))))
1783 )
1784
1785
1786(defun edebug-sit-for (arg)
1787 (if edebug-epoch-running
1788 (epoch::dispatch-events))
1789 (sit-for arg)
1790)
1791
1792(defun edebug-input-pending-p ()
1793 (if edebug-epoch-running
1794 (epoch::dispatch-events))
1795 (input-pending-p)
1796)
1797
1798
1799\f
1800;;--------------------------
1801;; breakpoint related functions
1802
1803(defun edebug-find-stop-point ()
1804 "Return (function . index) of the nearest edebug stop point."
1805 (let* ((def-name (edebug-which-function))
1806 (edebug-data
1807 (or (get def-name 'edebug)
1808 (error
1809 "%s must first be evaluated with edebug-defun." def-name)))
1810 ;; pull out parts of edebug-data.
1811 (edebug-func-mark (car edebug-data))
1812 (edebug-breakpoints (car (cdr edebug-data)))
1813
1814 (offset-vector (car (cdr (cdr edebug-data))))
1815 (offset (- (save-excursion
1816 (if (looking-at "[ \t]")
1817 ;; skip backwards until non-whitespace, or bol
1818 (skip-chars-backward " \t"))
1819 (point))
1820 edebug-func-mark))
1821 len i)
1822 ;; the offsets are in order so we can do a linear search
1823 (setq len (length offset-vector))
1824 (setq i 0)
1825 (while (and (< i len) (> offset (aref offset-vector i)))
1826 (setq i (1+ i)))
1827 (if (and (< i len)
1828 (<= offset (aref offset-vector i)))
1829 ;; return the relevant info
1830 (cons def-name i)
1831 (message "Point is not on an expression in %s."
1832 def-name)
1833 )))
1834
1835
1836(defun edebug-next-breakpoint ()
1837 "Move point to the next breakpoint, or first if none past point."
1838 (interactive)
1839 (let ((edebug-stop-point (edebug-find-stop-point)))
1840 (if edebug-stop-point
1841 (let* ((def-name (car edebug-stop-point))
1842 (index (cdr edebug-stop-point))
1843 (edebug-data (get def-name 'edebug))
1844
1845 ;; pull out parts of edebug-data
1846 (edebug-func-mark (car edebug-data))
1847 (edebug-breakpoints (car (cdr edebug-data)))
1848 (offset-vector (car (cdr (cdr edebug-data))))
1849 breakpoint)
1850 (if (not edebug-breakpoints)
1851 (message "No breakpoints in this function.")
1852 (let ((breaks edebug-breakpoints))
1853 (while (and breaks
1854 (<= (car (car breaks)) index))
1855 (setq breaks (cdr breaks)))
1856 (setq breakpoint
1857 (if breaks
1858 (car breaks)
1859 ;; goto the first breakpoint
1860 (car edebug-breakpoints)))
1861 (goto-char (+ edebug-func-mark
1862 (aref offset-vector (car breakpoint))))
1863
1864 (message (concat (if (car (cdr (cdr breakpoint)))
1865 "Temporary " "")
1866 (if (car (cdr breakpoint))
1867 (format "Condition: %s"
1868 (prin1-to-string
1869 (car (cdr breakpoint))))
1870 "")))
1871 ))))))
1872
1873
1874(defun edebug-modify-breakpoint (flag &optional condition temporary)
1875 "Modify the breakpoint for the form at point or after it according
1876to FLAG: set if t, clear if nil. Then move to that point.
1877If CONDITION or TEMPORARY are non-nil, add those attributes to
1878the breakpoint. "
1879 (let ((edebug-stop-point (edebug-find-stop-point)))
1880 (if edebug-stop-point
1881 (let* ((def-name (car edebug-stop-point))
1882 (index (cdr edebug-stop-point))
1883 (edebug-data (get def-name 'edebug))
1884
1885 ;; pull out parts of edebug-data
1886 (edebug-func-mark (car edebug-data))
1887 (edebug-breakpoints (car (cdr edebug-data)))
1888 (offset-vector (car (cdr (cdr edebug-data))))
1889 present)
1890 ;; delete it either way
1891 (setq present (assq index edebug-breakpoints))
1892 (setq edebug-breakpoints (delq present edebug-breakpoints))
1893 (if flag
1894 (progn
1895 ;; add it to the list and resort
1896 (setq edebug-breakpoints
1897 (edebug-sort-alist
1898 (cons
1899 (list index condition temporary)
1900 edebug-breakpoints) '<))
1901 (message "Breakpoint set in %s." def-name))
1902 (if present
1903 (message "Breakpoint unset in %s." def-name)
1904 (message "No breakpoint here.")))
1905
1906 (setcdr edebug-data
1907 (cons edebug-breakpoints (cdr (cdr edebug-data))))
1908 (goto-char (+ edebug-func-mark (aref offset-vector index)))
1909 ))))
1910
1911(defun edebug-set-breakpoint (arg)
1912 "Set the breakpoint of nearest sexp.
1913With prefix argument, make it a temporary breakpoint."
1914 (interactive "P")
1915 (edebug-modify-breakpoint t nil arg))
1916
1917(defun edebug-unset-breakpoint ()
1918 "Clear the breakpoint of nearest sexp."
1919 (interactive)
1920 (edebug-modify-breakpoint nil))
1921
1922(defun edebug-set-conditional-breakpoint (arg condition)
1923 "Set a conditional breakpoint at nearest sexp.
1924The condition is evaluated in the outside context.
1925With prefix argument, make it a temporary breakpoint."
1926 (interactive "P\nxCondition: ")
1927 (edebug-modify-breakpoint t condition arg))
1928
1929\f
1930;;--------------------------
1931;; Mode switching functions
1932
1933(defun edebug-set-mode (mode shortmsg msg)
1934 "Set the edebug mode to MODE.
1935Display SHORTMSG, or MSG if not within edebug."
1936 (interactive)
1937 (setq edebug-mode mode)
1938 (if (< 0 edebug-depth)
1939 (if (eq (current-buffer) edebug-buffer)
1940 (progn
1941 (message shortmsg)
1942 (exit-recursive-edit)))
1943 (message msg)))
1944
1945
1946(defun edebug-step-through ()
1947 "Proceed to next debug step."
1948 (interactive)
1949 (edebug-set-mode 'step "" "edebug will stop before next eval."))
1950
1951(defun edebug-go (arg)
1952 "Go, evaluating until break.
1953With ARG set temporary break at stop point and go."
1954 (interactive "P")
1955 (if arg
1956 (edebug-set-breakpoint t))
1957 (edebug-set-mode 'go "Go..." "edebug will go until break."))
1958
1959(defun edebug-Go-nonstop ()
1960 "Go, evaluating without debugging."
1961 (interactive)
1962 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
1963 "edebug will not stop at breaks."))
1964
1965(defun edebug-forward-sexp (arg)
1966 "Proceed from the current point to the end of the ARGth sexp ahead.
1967If there are not ARG sexps ahead, then do edebug-step-out."
1968 (interactive "p")
1969 (condition-case err
1970 (let ((parse-sexp-ignore-comments t))
1971 ;; Call forward-sexp repeatedly until done or failure.
1972 (forward-sexp arg)
1973 (edebug-go t))
1974 (error
1975 (edebug-step-out)
1976 )))
1977
1978(defun edebug-step-out ()
1979 "Proceed from the current point to the end of the containing sexp.
1980If there is no containing sexp that is not the top level defun,
1981go to the end of the last sexp, or if that is the same point, then step."
1982 (interactive)
1983 (condition-case err
1984 (let ((parse-sexp-ignore-comments t))
1985 (up-list 1)
1986 (save-excursion
1987 ;; Is there still a containing expression?
1988 (up-list 1))
1989 (edebug-go t))
1990 (error
1991 ;; At top level - 1, so first check if there are more sexps at this level.
1992 (let ((start-point (point)))
1993;; (up-list 1)
1994 (down-list -1)
1995 (if (= (point) start-point)
1996 (edebug-step-through) ; No more at this level, so step.
1997 (edebug-go t)
1998 )))))
1999
2000
2001(defun edebug-goto-here ()
2002 "Proceed to this stop point."
2003 (interactive)
2004 (edebug-go t)
2005 )
2006
2007(defun edebug-trace ()
2008 "Begin trace mode."
2009 (interactive)
2010 (edebug-set-mode 'trace "Tracing..." "edebug will trace with pause."))
2011
2012(defun edebug-Trace-fast ()
2013 "Trace with no wait at each step."
2014 (interactive)
2015 (edebug-set-mode 'Trace-fast
2016 "Trace fast..." "edebug will trace without pause."))
2017
2018(defun edebug-continue ()
2019 "Begin continue mode."
2020 (interactive)
2021 (edebug-set-mode 'continue "Continue..."
2022 "edebug will pause at breakpoints."))
2023
2024(defun edebug-Continue-fast ()
2025 "Trace with no wait at each step."
2026 (interactive)
2027 (edebug-set-mode 'Continue-fast "Continue fast..."
2028 "edebug will stop and go at breakpoints."))
2029
2030
2031(defun edebug-step-in ()
2032 "Step into the function about to be called.
2033Do this before the arguments are evaluated since otherwise it will be
2034too late. One side effect of using edebug-step-in is that the next
2035time the function is called, edebug will be called there as well."
2036 (interactive)
2037 (if (not (eq 'enter edebug-arg-mode))
2038 (error "You must be in front of a function or macro call."))
2039 (let* ((func (car edebug-exp))
2040 (func-marker (get func 'edebug)))
2041 (cond
2042 ((markerp func-marker)
2043 (save-excursion
2044 (set-buffer (marker-buffer func-marker))
2045 (goto-char func-marker)
2046 (edebug-defun)))
2047 ((listp func-marker)
2048 ;; its already been evaluated for edebug
2049 nil)
2050 (t (error "You must first evaluate %s in a buffer." func))))
2051 (exit-recursive-edit))
2052
2053
2054;;(defun edebug-exit-out ()
2055;; "Go until the current function exits."
2056;; (interactive)
2057;; (edebug-set-mode 'exiting "Exit..."))
2058
2059
2060(defun edebug-stop ()
2061 "Useful for exiting from trace loop."
2062 (interactive)
2063 (message "Stop"))
2064
2065
2066;;; The following initial mode setting definitions are not used yet.
2067
2068(defconst edebug-initial-mode-alist
2069 '((edebug-Continue-fast . Continue-fast)
2070 (edebug-Trace-fast . Trace-fast)
2071 (edebug-continue . continue)
2072 (edebug-trace . trace)
2073 (edebug-go . go)
2074 (edebug-step-through . step)
2075 (edebug-Go-nonstop . Go-nonstop)
2076 )
2077 "Association list between commands and the modes they set.")
2078
2079
2080(defun edebug-set-initial-mode ()
2081 "Ask for the initial mode of the enclosing function.
2082The mode is requested via the key that would be used to set the mode in
2083edebug-mode."
2084 (interactive)
2085 (let* ((this-function (edebug-which-function))
2086 (keymap (if (eq edebug-mode-map (current-local-map))
2087 edebug-mode-map))
2088 (old-mode (or (get this-function 'edebug-initial-mode)
2089 edebug-initial-mode))
2090 (key (read-key-sequence
2091 (format
2092 "Change initial edebug mode for %s from %s (%s) to (enter key): "
2093 this-function
2094 old-mode
2095 (where-is-internal
2096 (car (rassq old-mode edebug-initial-mode-alist))
2097 keymap 'firstonly
2098 ))))
2099 (mode (cdr (assq (key-binding key) edebug-initial-mode-alist)))
2100 )
2101 (if (and mode
2102 (or (get this-function 'edebug-initial-mode)
2103 (not (eq mode edebug-initial-mode))))
2104 (progn
2105 (put this-function 'edebug-initial-mode mode)
2106 (message "Initial mode for %s is now: %s"
2107 this-function mode))
2108 (error "Key must map to one of the mode changing commands.")
2109 )))
2110
2111
2112\f
2113;;--------------------------
2114;; Evaluation of expressions
2115
2116(defvar edebug-previous-result nil
2117 "Last result returned from an expression.")
2118
2119(defun edebug-previous-result ()
2120 "Return the previous result."
2121 (interactive)
2122 (let ((print-escape-newlines t)
2123 (print-length 20))
2124 (message "Result: %s" (prin1-to-string edebug-previous-result))))
2125
2126
2127(defun edebug-eval (expr)
2128 "Evaluate EXPR in the outside environment."
2129 (if (not edebug-active)
2130 (error "edebug is not active."))
2131 (edebug-outside-excursion
2132 (eval expr)))
2133
2134(defun edebug-eval-expression (expr)
2135 "Prompt and evaluate an expression in the outside environment.
2136Print result in minibuffer."
2137 (interactive "xEval: ")
2138 (prin1 (edebug-eval expr)))
2139
2140(defun edebug-eval-last-sexp ()
2141 "Evaluate sexp before point in the outside environment;
2142print value in minibuffer."
2143 (interactive)
2144 (prin1 (edebug-eval (edebug-last-sexp))))
2145
2146(defun edebug-eval-print-last-sexp ()
2147 "Evaluate sexp before point in the outside environment;
2148print value into current buffer."
2149 (interactive)
2150 (let ((standard-output (current-buffer)))
2151 (print
2152 (condition-case err
2153 (edebug-eval (edebug-last-sexp))
2154 (error (format "%s: %s"
2155 (get (car err) 'error-message)
2156 (car (cdr err))))))))
2157\f
2158;;;---------------------------------
2159;;; edebug minor mode initialization
2160
2161(defvar edebug-mode 'step
2162 "Current edebug mode set by user.")
2163
2164(defvar edebug-mode-map nil)
2165(if edebug-mode-map
2166 nil
2167 (progn
2168 (setq edebug-mode-map (copy-keymap emacs-lisp-mode-map))
2169 ;; control
2170 (define-key edebug-mode-map " " 'edebug-step-through)
2171 (define-key edebug-mode-map "g" 'edebug-go)
2172 (define-key edebug-mode-map "G" 'edebug-Go-nonstop)
2173 (define-key edebug-mode-map "t" 'edebug-trace)
2174 (define-key edebug-mode-map "T" 'edebug-Trace-fast)
2175 (define-key edebug-mode-map "c" 'edebug-continue)
2176 (define-key edebug-mode-map "C" 'edebug-Continue-fast)
2177
2178 (define-key edebug-mode-map "f" 'edebug-forward-sexp)
2179 (define-key edebug-mode-map "h" 'edebug-goto-here)
2180
2181 (define-key edebug-mode-map "r" 'edebug-previous-result)
2182
2183 (define-key edebug-mode-map "i" 'edebug-step-in)
2184 (define-key edebug-mode-map "o" 'edebug-step-out)
2185
2186;; (define-key edebug-mode-map "m" 'edebug-set-initial-mode)
2187
2188 (define-key edebug-mode-map "q" 'top-level)
2189 (define-key edebug-mode-map "a" 'abort-recursive-edit)
2190 (define-key edebug-mode-map "S" 'edebug-stop)
2191
2192 ;; breakpoints
2193 (define-key edebug-mode-map "b" 'edebug-set-breakpoint)
2194 (define-key edebug-mode-map "u" 'edebug-unset-breakpoint)
2195 (define-key edebug-mode-map "B" 'edebug-next-breakpoint)
2196 (define-key edebug-mode-map "x" 'edebug-set-conditional-breakpoint)
2197
2198 ;; evaluation
2199 (define-key edebug-mode-map "e" 'edebug-eval-expression)
2200 (define-key edebug-mode-map "\C-x\C-e" 'edebug-eval-last-sexp)
2201 (define-key edebug-mode-map "E" 'edebug-visit-eval-list)
2202
2203 ;; views
2204 (define-key edebug-mode-map "w" 'edebug-where)
2205 (define-key edebug-mode-map "v" 'edebug-view-outside)
2206 (define-key edebug-mode-map "p" 'edebug-bounce-point)
2207 (define-key edebug-mode-map "W" 'edebug-toggle-save-windows)
2208
2209 ;; misc
2210 (define-key edebug-mode-map "?" 'edebug-help)
2211 (define-key edebug-mode-map "d" 'edebug-backtrace)
2212
2213 (define-key edebug-mode-map "-" 'negative-argument)
2214 ))
2215
85dd3810 2216;;;###autoload
84fc2cfa
ER
2217(defvar global-edebug-prefix "\^XX"
2218 "Prefix key for global edebug commands, available from any buffer.")
2219
85dd3810 2220;;;###autoload
84fc2cfa
ER
2221(defvar global-edebug-map nil
2222 "Global map of edebug commands, available from any buffer.")
2223
85dd3810 2224;;;###autoload
84fc2cfa
ER
2225(if global-edebug-map
2226 nil
2227 (setq global-edebug-map (make-sparse-keymap))
2228
2229 (global-unset-key global-edebug-prefix)
2230 (global-set-key global-edebug-prefix global-edebug-map)
2231
2232;; (define-key global-edebug-map "X" 'edebug-step-through)
85dd3810 2233 (define-key global-edebug-map "d" 'edebug-defun)
84fc2cfa
ER
2234 (define-key global-edebug-map " " 'edebug-step-through)
2235 (define-key global-edebug-map "g" 'edebug-go)
2236 (define-key global-edebug-map "G" 'edebug-Go-nonstop)
2237 (define-key global-edebug-map "t" 'edebug-trace)
2238 (define-key global-edebug-map "T" 'edebug-Trace-fast)
2239 (define-key global-edebug-map "c" 'edebug-continue)
2240 (define-key global-edebug-map "C" 'edebug-Continue-fast)
2241
2242;; (define-key global-edebug-map "m" 'edebug-set-initial-mode)
2243 (define-key global-edebug-map "b" 'edebug-set-breakpoint)
2244 (define-key global-edebug-map "x" 'edebug-set-conditional-breakpoint)
2245 (define-key global-edebug-map "u" 'edebug-unset-breakpoint)
2246 (define-key global-edebug-map "w" 'edebug-where)
2247 (define-key global-edebug-map "q" 'top-level)
2248 )
2249
2250
2251(defun edebug-help ()
2252 (interactive)
2253 (describe-function 'edebug-mode))
2254
2255
2256(defun edebug-mode ()
3a801d0c 2257 "Mode for Emacs Lisp buffers while in edebug. Under construction.
84fc2cfa
ER
2258
2259There are both buffer local and global key bindings to several
2260functions. E.g. edebug-step-through is bound to
2261\\[edebug-step-through] in the debug buffer and
2262\\<global-map>\\[edebug-step-through] in any buffer.
2263
2264edebug buffer commands:
2265\\{edebug-mode-map}
2266
2267Global commands prefixed by global-edbug-prefix:
2268\\{global-edebug-map}
2269
2270Options:
2271edebug-all-defuns
2272edebug-eval-macro-args
2273edebug-stop-before-symbols
2274edebug-save-windows
2275edebug-save-point
2276edebug-save-buffer-points
2277edebug-initial-mode
2278edebug-trace
2279"
2280 (use-local-map edebug-mode-map))
2281
2282
2283\f
2284;;===============================================
2285;; edebug eval list mode
2286;; A list of expressions and their evaluations is displayed
2287;; in edebug-eval-buffer
2288
2289(defvar edebug-eval-list nil
2290 "List of expressions to evaluate.")
2291
2292;;(defvar edebug-eval-buffer "*edebug*"
2293;; "*Declared globally so edebug-eval-display can be called independent
2294;;of edebug (not implemented yet).")
2295
2296
2297(defun edebug-eval-result-list ()
2298 "Return a list of evaluations of edebug-eval-list"
2299 ;; Assumes in outside environment.
2300 (mapcar (function
2301 (lambda (expr)
2302 (condition-case err
2303 (eval expr)
2304 (error (format "%s: %s"
2305 (get (car err) 'error-message)
2306 (car (cdr err))))
2307 )))
2308 edebug-eval-list))
2309
2310(defun edebug-eval-display-list (edebug-eval-result-list)
2311 ;; Assumes edebug-eval-buffer exists.
2312 (let ((edebug-eval-list-temp edebug-eval-list)
2313 (standard-output edebug-eval-buffer)
2314 (edebug-display-line
2315 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
2316 (edebug-pop-to-buffer edebug-eval-buffer)
2317 (erase-buffer)
2318 (while edebug-eval-list-temp
2319 (prin1 (car edebug-eval-list-temp)) (terpri)
2320 (prin1 (car edebug-eval-result-list)) (terpri)
2321 (princ edebug-display-line)
2322 (setq edebug-eval-list-temp (cdr edebug-eval-list-temp))
2323 (setq edebug-eval-result-list (cdr edebug-eval-result-list)))
2324 ))
2325
2326(defun edebug-create-eval-buffer ()
2327 (if (not (and edebug-eval-buffer (buffer-name edebug-eval-buffer)))
2328 (progn
2329 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
2330 (edebug-eval-mode))))
2331
2332;; Should generalize this to be callable outside of edebug
2333;; with calls in user functions, e.g. (edebug-eval-display)
2334
2335(defun edebug-eval-display (edebug-eval-result-list)
2336 "Display expressions and evaluations in EVAL-LIST.
2337It modifies the context by popping up the eval display."
2338 (if edebug-eval-result-list
2339 (progn
2340 (edebug-create-eval-buffer)
2341 (edebug-pop-to-buffer edebug-eval-buffer)
2342 (edebug-eval-display-list edebug-eval-result-list)
2343 )))
2344
2345(defun edebug-eval-redisplay ()
2346 "Redisplay eval list in outside environment.
2347May only be called from within edebug-recursive-edit."
2348 (edebug-create-eval-buffer)
2349 (edebug-pop-to-buffer edebug-eval-buffer)
2350 (edebug-outside-excursion
2351 (edebug-eval-display-list (edebug-eval-result-list))
2352 ))
2353
2354(defun edebug-visit-eval-list ()
2355 (interactive)
2356 (edebug-eval-redisplay)
2357 (edebug-pop-to-buffer edebug-eval-buffer))
2358
2359
2360(defun edebug-update-eval-list ()
2361 "Replace the evaluation list with the sexps now in the eval buffer."
2362 (interactive)
2363 (let ((starting-point (point))
2364 new-list)
2365 (goto-char (point-min))
2366 ;; get the first expression
2367 (edebug-skip-whitespace)
2368 (if (not (eobp))
2369 (progn
2370 (forward-sexp 1)
2371 (setq new-list (cons (edebug-last-sexp) new-list))))
2372
2373 (while (re-search-forward "^;" nil t)
2374 (forward-line 1)
2375 (skip-chars-forward " \t\n\r")
2376 (if (and (/= ?\; (following-char))
2377 (not (eobp)))
2378 (progn
2379 (forward-sexp 1)
2380 (setq new-list (cons (edebug-last-sexp) new-list)))))
2381
2382 (setq edebug-eval-list (nreverse new-list))
2383 (edebug-eval-redisplay)
2384 (goto-char starting-point)))
2385
2386
2387(defun edebug-delete-eval-item ()
2388 "Delete the item under point and redisplay."
2389 ;; could add arg to do repeatedly
2390 (interactive)
2391 (if (re-search-backward "^;" nil 'nofail)
2392 (forward-line 1))
2393 (delete-region
2394 (point) (progn (re-search-forward "^;" nil 'nofail)
2395 (beginning-of-line)
2396 (point)))
2397 (edebug-update-eval-list))
2398
2399
2400
2401(defvar edebug-eval-mode-map nil
2402 "Keymap for edebug-eval-mode. Superset of lisp-interaction-mode.")
2403
2404(if edebug-eval-mode-map
2405 nil
2406 (setq edebug-eval-mode-map (copy-keymap lisp-interaction-mode-map))
2407
2408 (define-key edebug-eval-mode-map "\C-c\C-w" 'edebug-where)
2409 (define-key edebug-eval-mode-map "\C-c\C-d" 'edebug-delete-eval-item)
2410 (define-key edebug-eval-mode-map "\C-c\C-u" 'edebug-update-eval-list)
2411 (define-key edebug-eval-mode-map "\C-x\C-e" 'edebug-eval-last-sexp)
2412 (define-key edebug-eval-mode-map "\C-j" 'edebug-eval-print-last-sexp)
2413 )
2414
2415
2416(defun edebug-eval-mode ()
2417 "Mode for data display buffer while in edebug. Under construction.
2418... ignore the following...
2419There are both buffer local and global key bindings to several
2420functions. E.g. edebug-step-through is bound to
2421\\[edebug-step-through] in the debug buffer and
2422\\<global-map>\\[edebug-step-through] in any buffer.
2423
2424Eval list buffer commands:
2425\\{edebug-eval-mode-map}
2426
2427Global commands prefixed by global-edbug-prefix:
2428\\{global-edebug-map}
2429"
2430 (lisp-interaction-mode)
2431 (setq major-mode 'edebug-eval-mode)
2432 (setq mode-name "Edebug-Eval")
2433 (use-local-map edebug-eval-mode-map))
2434
2435
2436;;========================================
2437;; Interface with standard debugger.
2438
2439(setq debugger 'edebug-debug)
2440;; (setq debugger 'debug) ; use the default
2441
2442;; Note that debug and its utilities must be byte-compiled to work, since
2443;; they depend on the backtrace looking a certain way.
2444
daa37602 2445;;;###autoload
84fc2cfa
ER
2446(defun edebug-debug (&rest debugger-args)
2447 "Replacement for debug.
2448If an error or quit occurred and we are running an edebugged function,
2449show where we last were. Otherwise call debug normally."
2450 (if (and edebug-backtrace ; anything active?
2451 (eq (recursion-depth) edebug-recursion-depth)
2452 )
2453
2454 ;; Where were we before the error occurred?
2455 (let ((edebug-offset-index (car edebug-offset-indices))
2456 (edebug-arg-mode (car debugger-args))
2457 (edebug-exp (car (cdr debugger-args)))
2458 edebug-break-data
2459 edebug-break
2460 (edebug-outside-debug-on-eror debug-on-error)
2461 (debug-on-error nil))
2462 (edebug-display)
2463 )
2464
2465 ;; Otherwise call debug normally.
2466 ;; Still need to remove extraneous edebug calls from stack.
2467 (apply 'debug debugger-args)
2468 ))
2469
2470
2471(defun edebug-backtrace ()
2472 "Display a non-working backtrace. Better than nothing..."
2473 (interactive)
2474 (let ((old-buf (current-buffer)))
2475 (if (not edebug-backtrace-buffer)
2476 (setq edebug-backtrace-buffer
2477 (let ((default-major-mode 'fundamental-mode))
2478 (generate-new-buffer "*Backtrace*"))))
2479 (edebug-pop-to-buffer edebug-backtrace-buffer)
2480 (erase-buffer)
2481 (let ((standard-output (current-buffer))
2482 (print-escape-newlines t)
2483 (print-length 50)
2484 last-ok-point
2485 )
2486 (setq truncate-lines t)
2487 (backtrace)
2488
2489 ;; Clean up the backtrace.
2490 (goto-char (point-min))
2491 (delete-region
2492 (point)
2493 (progn
2494 ;; Everything up to the first edebug is internal.
2495 (re-search-forward "^ edebug(")
2496 (forward-line 1)
2497 (point)))
2498 (forward-line 1)
2499 (setq last-ok-point (point))
2500
2501 ;; Delete interspersed edebug internals.
2502 (while (re-search-forward "^ edebug" nil t)
2503 (if (looking-at "-enter")
2504 ;; delete extraneous progn at top level of function body
2505 (save-excursion
2506 (goto-char last-ok-point)
2507 (forward-line -1)
2508 (setq last-ok-point (point))))
2509 (forward-line 1)
2510 (delete-region last-ok-point (point))
2511 (forward-line 1) ; skip past the good line
2512 (setq last-ok-point (point))
2513 )
2514 )
2515 (edebug-pop-to-buffer old-buf)
2516 ))
2517
2518\f
2519;;========================================================================
2520;; Trace display - append text to a buffer, and update display.
2521;;; e.g.
2522;;; (edebug-trace-display
2523;;; "*trace-point*"
2524;;; "saving: point = %s window-start = %s\n"
2525;;; (point) (window-start))
2526
2527(defun edebug-trace-display (buf-name fmt &rest args)
2528 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
2529The buffer is created if it does not exist.
2530You must include newlines in FMT to break lines."
2531 (let* ((selected-window (selected-window))
2532 (buffer (get-buffer-create buf-name))
2533 (buf-window))
2534 (edebug-pop-to-buffer buffer)
2535 (save-excursion
2536 (setq buf-window (selected-window))
2537 (set-buffer buffer)
2538 (goto-char (point-max))
2539 (insert (apply 'format fmt args))
2540 (set-window-point buf-window (point))
2541 (forward-line (- 1 (window-height buf-window)))
2542 (set-window-start buf-window (point))
2543;; (edebug-sit-for 1)
2544 (bury-buffer buffer)
2545 )
2546 (select-window selected-window)))
2547
e8544af2
RS
2548(provide 'edebug)
2549
84fc2cfa 2550;;; edebug.el ends here