JimB's changes since January 18th
[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
ER
5;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
6;; Keywords: lisp, tools
7
84fc2cfa
ER
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is distributed in the hope that it will be useful,
11;; but WITHOUT ANY WARRANTY. No author or distributor
12;; accepts responsibility to anyone for the consequences of using it
13;; or for whether it serves any particular purpose or works at all,
14;; unless he says so in writing. Refer to the GNU Emacs General Public
15;; License for full details.
16
17;; Everyone is granted permission to copy, modify and redistribute
18;; GNU Emacs, but only under the conditions described in the
19;; GNU Emacs General Public License. A copy of this license is
20;; supposed to have been given to you along with GNU Emacs so you
21;; can know your rights and responsibilities. It should be in a
22;; file named COPYING. Among other things, the copyright notice
23;; and this notice must be preserved on all copies.
24
3a801d0c
ER
25;;;; Commentary:
26
27;;; This minor mode allows programmers to step through Emacs Lisp source
84fc2cfa
ER
28;;; code while executing, set breakpoints, etc. See the texinfo
29;;; document (being constructed...) for more detailed instructions
30;;; than contained here. Send me your enhancement, ideas, bugs, or
31;;; fixes.
32
33;;; Daniel LaLiberte 217-244-0785
34;;; University of Illinois, Urbana-Champaign
35;;; Department of Computer Science
36;;; 1304 W Springfield
37;;; Urbana, IL 61801
38
39;;; uiucdcs!liberte
40;;; liberte@cs.uiuc.edu
41
42;;; Contents:
43;;; =========
44;;; Change list
45;;; Installation
46;;; Todo list
47;;; Utilities
48;;; Parser
49;;; Debugger
50
51\f
52;;;================================================================
53;;; Change list
54;;; -----------
55
84fc2cfa
ER
56;;; Revision 2.5 91/07/25 13:32:53 liberte
57;;; Doc string cleanup.
58;;; If edebug-form-hook is t, evaluate all arguments.
59;;; If edebug-form-hook is 0, evaluate no arguments.
60;;; If edebug-form-hook is nil, evaluate macro args according
61;;; to edebug-eval-macro-args.
62;;; Save the outside value of executing macro.
63;;; Save and restore the outside restriction.
64;;; Dont force update for go and Go-nonstop.
65;;; Save and restore last-command-char, last-command,
66;;; this-command, last-input-char.
67;;; For epoch, do epoch::dispatch-events before sit-for
68;;; and input-pending-p since X events could interfere.
69;;; Warn about unsetting non-existent breakpoint.
70;;; Fix edebug-forward-sexp with prefix arg.
71;;; Add edebug-step-out to exit from current sexp.
72;;;
73;;; Revision 2.4 91/03/18 12:35:44 liberte
74;;; Force update after go or Go-nonstop modes, so overlay arrow is correct.
75;;; Support debug-on-quit. Remove edebug-on-error.
76;;; Fix edebug-anonymous. Bug found by jackr@wpd.sgi.com (Jack Repenning).
77;;; Don't discard-input anymore. Easier to change modes this way.
78;;; Fix max-lisp-eval-depth and max-specpdl-size incrementing.
79;;; Save and restore points in all buffers, if
80;;; edebug-save-buffer-points is non-nil. Expensive!
81;;; Bug caught by wolfgang@wsrcc.com (Wolfgang S. Rupprecht)
82;;; Save standard-output and standard-input in edebug-recursive-edit
83;;; so that edebug-outside-excursion can restore them.
84;;; Call set-buffer in edebug-pop-to-buffer since
85;;; select-window does not do that.
86;;; Fix edebug's eval-defun to remember current buffer inside evaluations
87;;; and to evaluate top-level forms. Found by Jamie Zawinski.
88;;; Add edebug-interactive-entry to support interactive forms with
89;;; non-string arg. Bug found by Jack Repenning.
90;;; Simplify edebug-restore-match-data to just store-match-data.
91;;; Motivated by linus@lysator.liu.se.
92;;; Move the match-data call to before the outside
93;;; buffer is changed, since it assumes that.
94;;;
95;;; Revision 2.3 91/01/17 20:55:14 liberte
96;;; Fix bug found by hollen@megatek.uucp.
97;;; Current buffer was not being restored.
98;;; Call edebug with (edebug begin end 'exp)
99;;; and add additional wrapper around body of functions:
100;;; (edebug-enter function body).
101;;; Make &optional only apply to immediate next arg
102;;; in edebug-form-parser (was edebug-macro-parser).
103;;; Catch debug errors with edebug. Yeah!
104;;; Reset edebug-mode on first function entry. Yeah!
105;;; Motivated by Dion Hollenbeck.
106;;; Add the missing bindings to the global-edebug-map.
107;;; eval-current-buffer now uses eval-region.
108;;; eval-region now does not narrow region.
109;;; Narrowing was the cause of the window-start being set wrong.
110;;; Reset edebug-mode only on
111;;; first entry of any function at each recursive-edit level.
112;;; Add edebug-backtrace, to generate cleaned up
113;;; backtrace. It doesnt "work" like the debug backtrace, however.
114;;; Require reselecting outside window even if
115;;; quit occurs, otherwise save-excursions may restore
116;;; buffer to the wrong window.
117;;;
118;;; Revision 2.2 90/11/26 21:14:22 liberte
119;;; Shadow eval-defun and eval-region. Toggle
120;;; edebugging with edebug-all-defuns.
121;;; Call edebug with (edebug 'function begin end 'exp)
122;;; Suggested by Jamie Zawinski <jwz@lucid.com>.
123;;; Add edebug-form-parser to process macro args.
124;;; Motivated by Darryl Okahata darrylo@hpnmxx.hp.com.
125;;; Fix by Roland McGrath <roland@ai.mit.edu>
126;;; to wrap body of edebug-save-restriction in progn.
127;;; Fix by Darryl Okahata <darrylo%hpnmd@hpcea.hp.com>
128;;; to add (set-window-hscroll (selected-window) 0) to
129;;; edebug-pop-to-buffer.
130;;;
131;;; Revision 2.1 90/11/16 21:55:35 liberte
132;;; Clean up.
133;;; Add edebug-form-hook to edebug macro calls. Thanks to Joe Wells.
134;;; edebug-forward-sexp uses step mode if no forward-sexp.
135;;;
136;;; Revision 2.0 90/11/14 22:30:54 liberte
137;;; Handle lambda forms, function, interactive evals, defmacro.
138;;; Clean up display for Epoch - save and restore screen configurations.
139;;; Note: epoch 3.2 broke set-window-configuration.
140;;; Also, sit-for pauses do not always work in epoch.
141;;; Display evaluations window.
142;;; Display result after expression evaluation.
143;;; Thanks to discussions with Shinichirou Sugou.
144;;; Conditional and temporary breakpoints.
145;;; Change "continue" to "go" mode and add different "continue" mode.
146;;; Option to stop before symbols.
147;;;
148;;; Fix by: Glen Ditchfield gjditchfield@violet.uwaterloo.ca
149;;; to handle ?# type chars.
150;;;
151;;; Revision 1.5 89/05/10 02:39:27 liberte
152;;; Fix condition-case expression lists.
153;;; Reorganize edebug.
154;;;
155;;; Revision 1.4 89/02/14 22:58:34 liberte
156;;; Fix broken breakpointing.
3a801d0c 157;;; Temporarily widen Emacs Lisp buffer during edebug.
84fc2cfa
ER
158;;;
159;;; Revision 1.3 89/01/30 00:26:09 liberte
160;;; More bug fixes for cond and let.
161;;; Another parsing fix backquote.
162;;; Fix for lambda forms inside defuns.
163;;; Leave point at syntax error, mark at starting position.
164;;;
165;;; Revision 1.2 88/11/28 12:14:15 liberte
166;;; Bug fixes: cond construct didnt execute.
167;;; () in sexp list didnt parse
168;;; () as variable in condition-case didnt parse.
169;;;
170;;; Revision 1.1 88/11/28 12:11:27 liberte
171;;; Initial revision
172;;;
173
174\f
175;;; Installation
176;;; ------------
177;; Put edebug.el in some directory in your load-path and byte-compile it.
178
179;; Put the following forms in your .emacs file.
84fc2cfa
ER
180;; (define-key emacs-lisp-mode-map "\^Xx" 'edebug-defun)
181;; (autoload 'edebug-defun "edebug")
182;; (autoload 'edebug-debug "edebug")
183;; (setq debugger 'edebug-debug)
184;; ... other options, described in the next section.
185
186;; Evaluate a defun for edebug with edebug-defun.
187;; Evaluate your function normally.
188;; Use the "?" command in edebug to describe other commands.
189;; See edebug.texinfo for more instructions.
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."
284 ;; This doesnt work for epoch.
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
1029;; Bug: this doesnt support condition name lists
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)
1168 (edebug-data (get edebug-func 'edebug))
1169 ;; pull out parts of the edebug-data
1170 (edebug-func-mark (car edebug-data)) ; mark at function start
1171
1172 (edebug-buffer (marker-buffer edebug-func-mark))
1173 (edebug-backtrace (cons edebug-func edebug-backtrace))
1174 (max-lisp-eval-depth (+ 6 max-lisp-eval-depth)) ; too much??
1175 (max-specpdl-size (+ 10 max-specpdl-size)) ; the args and these vars
1176 )
1177 (if edebug-trace
1178 (let ((edebug-stack-depth (1- (length edebug-backtrace)))
1179 edebug-result)
1180 (edebug-print-trace-entry
1181 "*edebug-trace*" edebug-func edebug-args edebug-stack-depth)
1182 (setq edebug-result (eval edebug-body))
1183 (edebug-print-trace-exit
1184 "*edebug-trace*" edebug-func edebug-result edebug-stack-depth)
1185 edebug-result)
1186 (eval edebug-body)
1187 )))
1188
1189(defun edebug-interactive-entry (edebug-func edebug-args)
1190 "Evaluating FUNCs non-string argument of interactive form ARGS."
1191 (if (and (not edebug-entered)
1192 edebug-initial-mode)
1193 ;; Reset edebug-mode to the initial mode.
1194 (setq edebug-mode edebug-initial-mode))
1195 (let* ((edebug-entered t)
1196 (edebug-data (get edebug-func 'edebug))
1197 ;; pull out parts of the edebug-data
1198 (edebug-func-mark (car edebug-data)) ; mark at function start
1199
1200 (edebug-buffer (marker-buffer edebug-func-mark))
1201;; (edebug-backtrace (cons edebug-func edebug-backtrace))
1202 )
1203 (eval edebug-args)))
1204
1205
1206(defun edebug-print-trace-entry
1207 (edebug-stream edebug-function edebug-args edebug-stack-depth)
1208 (edebug-trace-display
1209 edebug-stream
1210 "%sEnter: %s\n" (make-string edebug-stack-depth ?\ ) edebug-function)
1211 )
1212
1213(defun edebug-print-trace-exit
1214 (edebug-stream edebug-function edebug-result edebug-stack-depth)
1215 (edebug-trace-display
1216 edebug-stream
1217 "%sExit: %s\n" (make-string edebug-stack-depth ?\ ) edebug-function)
1218 )
1219
1220
1221(defun edebug (edebug-before-index edebug-after-index edebug-exp)
1222 "Debug current function given BEFORE and AFTER positions around EXP.
1223BEFORE and AFTER are indexes into the position offset vector in the
1224functions 'edebug property. edebug is called from functions compiled
1225with edebug-defun."
1226 (let ((max-lisp-eval-depth (+ 5 max-lisp-eval-depth)) ; enough??
1227 (max-specpdl-size (+ 7 max-specpdl-size)) ; the args and these vars
1228 (edebug-offset-indices
1229 (cons edebug-before-index edebug-offset-indices))
1230 ;; Save the outside value of executing macro.
1231 (edebug-outside-executing-macro executing-macro)
1232 ;; Don't keep reading from an executing kbd macro within edebug!
1233 (executing-macro nil)
1234 )
1235 (if (and (eq edebug-mode 'Go-nonstop)
1236 (not (edebug-input-pending-p)))
1237 ;; Just return evalled expression.
1238 (eval edebug-exp)
1239 (edebug-debugger edebug-before-index 'enter edebug-exp)
1240 (edebug-debugger edebug-after-index 'exit (eval edebug-exp))
1241 )))
1242
1243
1244(defun edebug-debugger (edebug-offset-index edebug-arg-mode edebug-exp)
1245 "Determine if edebug display should be updated."
1246 (let* (
1247 ;; This needs to be here since breakpoints may be changed.
1248 (edebug-breakpoints (car (cdr edebug-data))) ; list of breakpoints
1249 (edebug-break-data (assq edebug-offset-index edebug-breakpoints))
1250 (edebug-break
1251 (if edebug-break-data
1252 (let ((edebug-break-condition
1253 (car (cdr edebug-break-data))))
1254 (or (not edebug-break-condition)
1255 (eval edebug-break-condition)))))
1256 )
1257 (if (and edebug-break
1258 (car (cdr (cdr edebug-break-data)))) ; is it temporary?
1259 ;; Delete the breakpoint.
1260 (setcdr edebug-data
1261 (cons (delq edebug-break-data edebug-breakpoints)
1262 (cdr (cdr edebug-data)))))
1263
1264 ;; Dont do anything if mode is go, continue, or Continue-fast
1265 ;; and no break, and no input.
1266 (if (or (and (not (memq edebug-mode '(go continue Continue-fast)))
1267 (or edebug-stop-before-symbols
1268 (not (and (eq edebug-arg-mode 'enter)
1269 (symbolp edebug-exp)))))
1270 (edebug-input-pending-p)
1271 edebug-break)
1272 (edebug-display))
1273
1274 edebug-exp
1275 ))
1276
1277
1278(defvar edebug-window-start 0
1279 "Remember where each buffers' window starts between edebug calls.
1280This is to avoid spurious recentering.")
1281
1282(setq-default edebug-window-start 0)
1283(make-variable-buffer-local 'edebug-window-start)
1284
1285(defun edebug-display ()
1286 "Setup windows for edebug, determine mode, maybe enter recursive-edit."
1287 ;; uses local variables of edebug-enter, edebug, and edebug-debugger.
1288 (let ((edebug-active t) ; for minor mode alist
1289 edebug-stop ; should we enter recursive-edit
1290 (edebug-point (+ edebug-func-mark
1291 (aref (car (cdr (cdr edebug-data)))
1292 edebug-offset-index)))
1293 (edebug-buffer-points
1294 (if edebug-save-buffer-points (edebug-get-buffer-points)))
1295 edebug-window ; window displaying edebug-buffer
1296 edebug-inside-window ; window displayed after recursive edit
1297 (edebug-outside-window (selected-window))
1298 (edebug-outside-buffer (current-buffer))
1299 (edebug-outside-point (point))
1300 (edebug-outside-mark (mark))
1301 edebug-outside-windows ; window or screen configuration
1302 edebug-outside-edebug-point ; old point in edebug buffer
1303 edebug-outside-edebug-mark
1304
1305 edebug-eval-buffer ; declared here so we can kill it below
1306 (edebug-eval-result-list (and edebug-eval-list
1307 (edebug-eval-result-list)))
1308 (edebug-outside-o-a-p overlay-arrow-position)
1309 (edebug-outside-o-a-s overlay-arrow-string)
1310 (edebug-outside-c-i-e-a cursor-in-echo-area)
1311
1312 edebug-outside-point-min
1313 edebug-outside-point-max
1314
1315 overlay-arrow-position
1316 overlay-arrow-string
1317 (cursor-in-echo-area nil)
1318 ;; any others??
1319 )
1320 (if (not (buffer-name edebug-buffer))
1321 (let (debug-on-error nil)
1322 (error "Buffer defining %s not found." edebug-func)))
1323
1324 ;; Save windows now before we modify them.
1325 (if edebug-save-windows
1326 (setq edebug-outside-windows
1327 (edebug-current-window-configuration)))
1328
1329 ;; If edebug-buffer is not currently displayed,
1330 ;; first find a window for it.
1331 (edebug-pop-to-buffer edebug-buffer)
1332 (setq edebug-window (selected-window))
1333
1334 ;; Now display eval list, if any.
1335 ;; This is done after the pop to edebug-buffer
1336 ;; so that buffer-window correspondence is correct after quit.
1337 (edebug-eval-display edebug-eval-result-list)
1338 (select-window edebug-window)
1339
1340 (if edebug-save-point
1341 (progn
1342 (setq edebug-outside-edebug-point (point))
1343 (setq edebug-outside-edebug-mark (mark))))
1344
1345 (edebug-save-restriction
1346 (setq edebug-outside-point-min (point-min))
1347 (setq edebug-outside-point-max (point-max))
1348 (widen)
1349 (goto-char edebug-point)
1350
1351 (setq edebug-window-start
1352 (edebug-adjust-window edebug-window-start))
1353
1354 (if (edebug-input-pending-p) ; not including keyboard macros
1355 (progn
1356 (setq edebug-mode 'step)
1357 (setq edebug-stop t)
1358 (edebug-stop)
1359 ;; (discard-input) ; is this unfriendly??
1360 ))
1361 (edebug-overlay-arrow)
1362
1363 (cond
1364 ((eq 'exit edebug-arg-mode)
1365 ;; Display result of previous evaluation.
1366 (setq edebug-previous-result edebug-exp)
1367 (edebug-previous-result))
1368
1369 ((eq 'error edebug-arg-mode)
1370 ;; Display error message
1371 (beep)
1372 (if (eq 'quit (car edebug-exp))
1373 (message "Quit")
1374 (message "%s: %s"
1375 (get (car edebug-exp) 'error-message)
1376 (car (cdr edebug-exp)))))
1377
1378 (edebug-break
1379 (message "Break"))
1380 (t (message "")))
1381
1382 (if edebug-break
1383 (if (not (memq edebug-mode '(continue Continue-fast)))
1384 (setq edebug-stop t)
1385 (if (eq edebug-mode 'continue)
1386 (edebug-sit-for 1)
1387 (edebug-sit-for 0)))
1388 ;; not edebug-break
1389 (if (eq edebug-mode 'trace)
1390 (edebug-sit-for 1) ; Force update and pause.
1391 (if (eq edebug-mode 'Trace-fast)
1392 (edebug-sit-for 0) ; Force update and continue.
1393 )))
1394
1395 (unwind-protect
1396 (if (or edebug-stop
1397 (eq edebug-mode 'step)
1398 (eq edebug-arg-mode 'error))
1399 (progn
1400 (setq edebug-mode 'step)
1401 (edebug-overlay-arrow) ; this doesnt always show up.
1402 (edebug-recursive-edit));; <<<<<< Recursive edit
1403 )
1404
1405 (if edebug-save-buffer-points
1406 (edebug-set-buffer-points))
1407 ;; Since we may be in a save-excursion, in case of quit
1408 ;; restore the outside window only.
1409 (select-window edebug-outside-window)
1410 ) ; unwind-protect
1411
1412 ;; None of the following is done if quit or signal occurs.
1413 (if edebug-save-point
1414 ;; Restore point and mark in edebug-buffer.
1415 ;; This does the save-excursion recovery only if no quit.
1416 ;; If edebug-buffer == edebug-outside-buffer,
1417 ;; then this is redundant with outside save-excursion.
1418 (progn
1419 (set-buffer edebug-buffer)
1420 (goto-char edebug-outside-edebug-point)
1421 (if (mark-marker)
1422 (set-marker (mark-marker) edebug-outside-edebug-mark))
1423 ))
1424 ) ; edebug-save-restriction
1425
1426 ;; Restore windows, buffer, point, and mark.
1427 (if edebug-save-windows
1428 ;; Restore windows before continuing.
1429 (edebug-set-window-configuration edebug-outside-windows))
1430 (set-buffer edebug-outside-buffer)
1431 (goto-char edebug-outside-point)
1432 (if (mark-marker)
1433 (set-marker (mark-marker) edebug-outside-mark))
1434 ;; The following is not sufficient, and sometimes annoying.
1435 ;; (if (memq edebug-mode '(go Go-nonstop))
1436 ;; (edebug-sit-for 0))
1437 ))
1438
1439
1440(defvar edebug-depth 0
1441 "Number of recursive edits started by edebug.
1442Should be 0 at the top level.")
1443
1444(defvar edebug-recursion-depth 0
1445 "Value of recursion-depth when edebug was called.")
1446
1447
1448(defun edebug-recursive-edit ()
1449 "Start up a recursive edit inside of edebug."
1450 ;; The current buffer is the edebug-buffer, which is put into edebug-mode.
1451 (let ((edebug-buffer-read-only buffer-read-only)
1452 ;; match-data must be done in the outside buffer
1453 (edebug-outside-match-data
1454 (save-excursion
1455 (set-buffer edebug-outside-buffer)
1456 (match-data)))
1457
1458 (edebug-depth (1+ edebug-depth))
1459 (edebug-recursion-depth (recursion-depth))
1460 edebug-entered ; bind locally to nil
1461 edebug-backtrace-buffer ; each recursive edit gets its own
1462 ;; The window configuration may be saved and restored
1463 ;; during a recursive-edit
1464 edebug-inside-windows
1465
1466 (edebug-outside-map (current-local-map))
1467 (edebug-outside-standard-output standard-output)
1468 (edebug-outside-standard-input standard-input)
1469
1470 (edebug-outside-last-command-char last-command-char)
1471 (edebug-outside-last-command last-command)
1472 (edebug-outside-this-command this-command)
1473 (edebug-outside-last-input-char last-input-char)
1474;; (edebug-outside-unread-command-char unread-command-char)
1475
1476 ;; Declare the following local variables to protect global values.
1477 ;; We could set these to the values for previous edebug call.
1478 ;; But instead make it local, but use global value.
1479 (last-command-char last-command-char)
1480 (last-command last-command)
1481 (this-command this-command)
1482 (last-input-char last-input-char)
dbc4e1c1 1483 ;; Assume no edebug command sets unread-command-events.
84fc2cfa
ER
1484;; (unread-command-char -1)
1485
1486 (debug-on-error debug-on-error)
1487
1488 ;; others??
1489 )
1490
1491 (if (and (eq edebug-mode 'go)
1492 (not (memq edebug-arg-mode '(exit error))))
1493 (message "Break"))
1494 (edebug-mode)
1495 (if (boundp 'edebug-outside-debug-on-error)
1496 (setq debug-on-error edebug-outside-debug-on-error))
1497
1498 (setq buffer-read-only t)
1499 (unwind-protect
1500 (recursive-edit) ; <<<<<<<<<< Recursive edit
1501
1502 ;; Do the following, even if quit occurs.
1503 (if edebug-backtrace-buffer
1504 (kill-buffer edebug-backtrace-buffer))
1505 ;; Could be an option to keep eval display up.
1506 (if edebug-eval-buffer (kill-buffer edebug-eval-buffer))
1507
1508 ;; Remember selected-window after recursive-edit.
1509 (setq edebug-inside-window (selected-window))
1510
1511 (store-match-data edebug-outside-match-data)
1512
1513 ;; Recursive edit may have changed buffers,
1514 ;; so set it back before exiting let.
1515 (if (buffer-name edebug-buffer) ; if it still exists
1516 (progn
1517 (set-buffer edebug-buffer)
1518 (if (memq edebug-mode '(go Go-nonstop))
1519 (edebug-overlay-arrow))
1520 (setq buffer-read-only edebug-buffer-read-only)
1521 (use-local-map edebug-outside-map)
1522 ;; Remember current window-start for next visit.
1523 (select-window edebug-window)
1524 (if (eq edebug-buffer (window-buffer edebug-window))
1525 (setq edebug-window-start (window-start)))
1526 (select-window edebug-inside-window)
1527 ))
1528 )))
1529
1530\f
1531;;--------------------------
1532;; Display related functions
1533
1534(defun edebug-adjust-window (old-start)
1535 "Adjust window to fit as much as possible following point.
1536The display should prefer to start at OLD-START if point is not visible.
1537Return the new window-start."
1538 (if (not (pos-visible-in-window-p))
1539 (progn
1540 (set-window-start (selected-window) old-start)
1541 (if (not (pos-visible-in-window-p))
1542 (let ((start (window-start))
1543 (pnt (point)))
1544 (set-window-start
1545 (selected-window)
1546 (save-excursion
1547 (forward-line
1548 (if (< pnt start) -1 ; one line before
1549 (- (/ (window-height) 2)) ; center the line
1550 ))
1551 (beginning-of-line)
1552 (point)))))))
1553 (window-start))
1554
1555
1556(defconst edebug-arrow-alist
1557 '((Continue-fast . ">")
1558 (Trace-fast . ">")
1559 (continue . ">")
1560 (trace . "->")
1561 (step . "=>")
1562 (go . "<>")
1563 (Go-nonstop . "..") ; not used
1564 )
1565 "Association list of arrows for each edebug mode.
1566If you come up with arrows that make more sense, let me know.")
1567
1568(defun edebug-overlay-arrow ()
1569 "Set up the overlay arrow at beginning-of-line in current buffer.
1570The arrow string is derived from edebug-arrow-alist and edebug-mode."
1571 (let* ((pos))
1572 (save-excursion
1573 (beginning-of-line)
1574 (setq pos (point)))
1575 (setq overlay-arrow-string
1576 (cdr (assq edebug-mode edebug-arrow-alist)))
1577 (setq overlay-arrow-position (make-marker))
1578 (set-marker overlay-arrow-position pos (current-buffer))))
1579
1580
1581(put 'edebug-outside-excursion 'edebug-form-hook
1582 '(&rest form))
1583
1584(defmacro edebug-outside-excursion (&rest body)
1585 "Evaluate an expression list in the outside context.
1586Return the result of the last expression."
1587 (` (save-excursion ; of current-buffer
1588 (if edebug-save-windows
1589 (progn
1590 ;; After excursion, we will
1591 ;; restore to current window configuration.
1592 (setq edebug-inside-windows
1593 (edebug-current-window-configuration))
1594 ;; Restore outside windows.
1595 (edebug-set-window-configuration edebug-outside-windows)))
1596
1597 (set-buffer edebug-buffer)
1598 ;; Restore outside context.
1599 (let ((edebug-inside-map (current-local-map))
1600 (last-command-char edebug-outside-last-command-char)
1601 (last-command edebug-outside-last-command)
1602 (this-command edebug-outside-this-command)
1603;; (unread-command-char edebug-outside-unread-command-char)
1604 (last-input-char edebug-outside-last-input-char)
1605 (overlay-arrow-position edebug-outside-o-a-p)
1606 (overlay-arrow-string edebug-outside-o-a-s)
1607 (cursor-in-echo-area edebug-outside-c-i-e-a)
1608 (standard-output edebug-outside-standard-output)
1609 (standard-input edebug-outside-standard-input)
1610 (executing-macro edebug-outside-executing-macro)
1611 )
1612 (unwind-protect
1613 (save-restriction
1614 (narrow-to-region edebug-outside-point-min
1615 edebug-outside-point-max)
1616 (save-excursion ; of edebug-buffer
1617 (if edebug-save-point
1618 (progn
1619 (goto-char edebug-outside-edebug-point)
1620 (if (mark-marker)
1621 (set-marker (mark-marker)
1622 edebug-outside-edebug-mark))
1623 ))
1624 (use-local-map edebug-outside-map)
1625 (store-match-data edebug-outside-match-data)
1626 (select-window edebug-outside-window)
1627 (set-buffer edebug-outside-buffer)
1628 (goto-char edebug-outside-point)
1629 (,@ body)
1630 ) ; save-excursion
1631 ) ; save-restriction
1632 ;; Back to edebug-buffer. Restore rest of inside context.
1633 (use-local-map edebug-inside-map)
1634 (if edebug-save-windows
1635 ;; Restore inside windows.
1636 (edebug-set-window-configuration edebug-inside-windows))
1637 )) ; let
1638 )))
1639
1640
1641(defun edebug-toggle-save-windows ()
1642 "Toggle the edebug-save-windows variable.
1643Each time you toggle it, the inside and outside window configurations
1644become the same as the current configuration."
1645 (interactive)
1646 (if (setq edebug-save-windows (not edebug-save-windows))
1647 (setq edebug-inside-windows
1648 (setq edebug-outside-windows
1649 (edebug-current-window-configuration))))
1650 (message "Window saving is %s."
1651 (if edebug-save-windows "on" "off")))
1652
1653
1654(defun edebug-where ()
1655 "Show the debug windows and where we stopped in the program."
1656 (interactive)
1657 (if (not edebug-active)
1658 (error "edebug is not active."))
1659 (edebug-pop-to-buffer edebug-buffer)
1660 (goto-char edebug-point) ; from edebug
1661 )
1662
1663(defun edebug-view-outside ()
1664 "Change to the outside window configuration."
1665 (interactive)
1666 (if (not edebug-active)
1667 (error "edebug is not active."))
1668 (setq edebug-inside-windows (edebug-current-window-configuration))
1669 (edebug-set-window-configuration edebug-outside-windows)
1670 (goto-char edebug-outside-point)
1671 (message "Window configuration outside of edebug. Return with %s"
1672 (substitute-command-keys "\\<global-map>\\[edebug-where]")))
1673
1674
1675(defun edebug-bounce-point ()
1676 "Bounce the point in the outside current buffer."
1677 (interactive)
1678 (if (not edebug-active)
1679 (error "edebug is not active."))
1680 (save-excursion
1681 ;; If the buffer's currently displayed, avoid the set-window-configuration.
1682 (save-window-excursion
1683 (edebug-pop-to-buffer edebug-outside-buffer)
1684 ;; (edebug-sit-for 1) ; this shouldnt be necessary
1685 (goto-char edebug-outside-point)
1686 ;; (message "current buffer: %s" (current-buffer))
1687 (edebug-sit-for 1)
1688 (edebug-pop-to-buffer edebug-buffer))))
1689
1690
1691\f
1692;;--------------------------
1693;; epoch related things
1694
1695(defvar edebug-epoch-running (and (boundp 'epoch::version) epoch::version)
1696 "non-nil if epoch is running.
1697Windows are handled a little differently under epoch.")
1698
1699
1700(defun edebug-current-window-configuration ()
f98955ea 1701 "Return the current window or frame configuration."
84fc2cfa
ER
1702 (if edebug-epoch-running
1703 (edebug-current-screen-configuration)
1704 (current-window-configuration)))
1705
1706
1707(defun edebug-set-window-configuration (conf)
f98955ea 1708 "Set the window or frame configuration to CONF."
84fc2cfa
ER
1709 (if edebug-epoch-running
1710 (edebug-set-screen-configuration conf)
1711 (set-window-configuration conf)))
1712
1713
1714(defun edebug-get-buffer-window (buffer)
1715 (if edebug-epoch-running
1716 (epoch::get-buffer-window buffer)
1717 (get-buffer-window buffer)))
1718
1719
1720(defun edebug-pop-to-buffer (buffer)
f98955ea 1721 "Like pop-to-buffer, but select a frame that buffer was shown in."
84fc2cfa
ER
1722 (let ((edebug-window (edebug-get-buffer-window buffer)))
1723 (if edebug-window
1724 (select-window edebug-window)
1725 ;; It is not currently displayed, so find some place to display it.
1726 (if edebug-epoch-running
1727 ;; Select a screen that the buffer has been displayed in before
1728 ;; or the current screen otherwise.
1729 (select-screen
1730 ;; allowed-screens in epoch 3.2, was called screens before that
1731 (or (car (symbol-buffer-value 'allowed-screens buffer))
1732 (epoch::current-screen))))
1733 (if (one-window-p)
1734 (split-window))
1735 (select-window (next-window))
1736 (set-window-buffer (selected-window) buffer)
1737 (set-window-hscroll (selected-window) 0)
1738 ))
1739 ;; Selecting the window does not set the buffer.
1740 (set-buffer buffer)
1741 )
1742
1743
1744(defun edebug-current-screen-configuration ()
1745 "Return an object recording the current configuration of Epoch screen-list.
1746The object is a list of pairs of the form (SCREEN . CONFIGURATION)
1747where SCREEN has window-configuration CONFIGURATION. The current
1748screen is the head of the list."
1749 (let ((screen-list (epoch::screen-list 'unmapped))
1750 (current-screen (epoch::get-screen))
1751 (current-buffer (current-buffer))
1752 )
1753 ;; put current screen first
1754 (setq screen-list (cons current-screen (delq current-screen screen-list)))
1755 (prog1
1756 (mapcar (function
1757 (lambda (screen)
1758 (cons screen
1759 (progn
1760 (epoch::select-screen screen)
1761 (current-window-configuration)))))
1762 screen-list)
1763 (epoch::select-screen current-screen)
1764 (set-buffer current-buffer)
1765 )))
1766
1767(defun edebug-set-screen-configuration (sc)
1768 "Set the window-configuration for all the screens in SC.
1769Set the current screen to be the head of SC."
1770 (mapcar (function
1771 (lambda (screen-conf)
1772 (if (epoch::screen-p (car screen-conf)) ; still exist?
1773 (progn
1774 (epoch::select-screen (car screen-conf))
1775 (set-window-configuration (cdr screen-conf))))))
1776 sc)
1777 (if (epoch::screen-p (car (car sc)))
1778 (epoch::select-screen (car (car sc))))
1779 )
1780
1781
1782(defun edebug-sit-for (arg)
1783 (if edebug-epoch-running
1784 (epoch::dispatch-events))
1785 (sit-for arg)
1786)
1787
1788(defun edebug-input-pending-p ()
1789 (if edebug-epoch-running
1790 (epoch::dispatch-events))
1791 (input-pending-p)
1792)
1793
1794
1795\f
1796;;--------------------------
1797;; breakpoint related functions
1798
1799(defun edebug-find-stop-point ()
1800 "Return (function . index) of the nearest edebug stop point."
1801 (let* ((def-name (edebug-which-function))
1802 (edebug-data
1803 (or (get def-name 'edebug)
1804 (error
1805 "%s must first be evaluated with edebug-defun." def-name)))
1806 ;; pull out parts of edebug-data.
1807 (edebug-func-mark (car edebug-data))
1808 (edebug-breakpoints (car (cdr edebug-data)))
1809
1810 (offset-vector (car (cdr (cdr edebug-data))))
1811 (offset (- (save-excursion
1812 (if (looking-at "[ \t]")
1813 ;; skip backwards until non-whitespace, or bol
1814 (skip-chars-backward " \t"))
1815 (point))
1816 edebug-func-mark))
1817 len i)
1818 ;; the offsets are in order so we can do a linear search
1819 (setq len (length offset-vector))
1820 (setq i 0)
1821 (while (and (< i len) (> offset (aref offset-vector i)))
1822 (setq i (1+ i)))
1823 (if (and (< i len)
1824 (<= offset (aref offset-vector i)))
1825 ;; return the relevant info
1826 (cons def-name i)
1827 (message "Point is not on an expression in %s."
1828 def-name)
1829 )))
1830
1831
1832(defun edebug-next-breakpoint ()
1833 "Move point to the next breakpoint, or first if none past point."
1834 (interactive)
1835 (let ((edebug-stop-point (edebug-find-stop-point)))
1836 (if edebug-stop-point
1837 (let* ((def-name (car edebug-stop-point))
1838 (index (cdr edebug-stop-point))
1839 (edebug-data (get def-name 'edebug))
1840
1841 ;; pull out parts of edebug-data
1842 (edebug-func-mark (car edebug-data))
1843 (edebug-breakpoints (car (cdr edebug-data)))
1844 (offset-vector (car (cdr (cdr edebug-data))))
1845 breakpoint)
1846 (if (not edebug-breakpoints)
1847 (message "No breakpoints in this function.")
1848 (let ((breaks edebug-breakpoints))
1849 (while (and breaks
1850 (<= (car (car breaks)) index))
1851 (setq breaks (cdr breaks)))
1852 (setq breakpoint
1853 (if breaks
1854 (car breaks)
1855 ;; goto the first breakpoint
1856 (car edebug-breakpoints)))
1857 (goto-char (+ edebug-func-mark
1858 (aref offset-vector (car breakpoint))))
1859
1860 (message (concat (if (car (cdr (cdr breakpoint)))
1861 "Temporary " "")
1862 (if (car (cdr breakpoint))
1863 (format "Condition: %s"
1864 (prin1-to-string
1865 (car (cdr breakpoint))))
1866 "")))
1867 ))))))
1868
1869
1870(defun edebug-modify-breakpoint (flag &optional condition temporary)
1871 "Modify the breakpoint for the form at point or after it according
1872to FLAG: set if t, clear if nil. Then move to that point.
1873If CONDITION or TEMPORARY are non-nil, add those attributes to
1874the breakpoint. "
1875 (let ((edebug-stop-point (edebug-find-stop-point)))
1876 (if edebug-stop-point
1877 (let* ((def-name (car edebug-stop-point))
1878 (index (cdr edebug-stop-point))
1879 (edebug-data (get def-name 'edebug))
1880
1881 ;; pull out parts of edebug-data
1882 (edebug-func-mark (car edebug-data))
1883 (edebug-breakpoints (car (cdr edebug-data)))
1884 (offset-vector (car (cdr (cdr edebug-data))))
1885 present)
1886 ;; delete it either way
1887 (setq present (assq index edebug-breakpoints))
1888 (setq edebug-breakpoints (delq present edebug-breakpoints))
1889 (if flag
1890 (progn
1891 ;; add it to the list and resort
1892 (setq edebug-breakpoints
1893 (edebug-sort-alist
1894 (cons
1895 (list index condition temporary)
1896 edebug-breakpoints) '<))
1897 (message "Breakpoint set in %s." def-name))
1898 (if present
1899 (message "Breakpoint unset in %s." def-name)
1900 (message "No breakpoint here.")))
1901
1902 (setcdr edebug-data
1903 (cons edebug-breakpoints (cdr (cdr edebug-data))))
1904 (goto-char (+ edebug-func-mark (aref offset-vector index)))
1905 ))))
1906
1907(defun edebug-set-breakpoint (arg)
1908 "Set the breakpoint of nearest sexp.
1909With prefix argument, make it a temporary breakpoint."
1910 (interactive "P")
1911 (edebug-modify-breakpoint t nil arg))
1912
1913(defun edebug-unset-breakpoint ()
1914 "Clear the breakpoint of nearest sexp."
1915 (interactive)
1916 (edebug-modify-breakpoint nil))
1917
1918(defun edebug-set-conditional-breakpoint (arg condition)
1919 "Set a conditional breakpoint at nearest sexp.
1920The condition is evaluated in the outside context.
1921With prefix argument, make it a temporary breakpoint."
1922 (interactive "P\nxCondition: ")
1923 (edebug-modify-breakpoint t condition arg))
1924
1925\f
1926;;--------------------------
1927;; Mode switching functions
1928
1929(defun edebug-set-mode (mode shortmsg msg)
1930 "Set the edebug mode to MODE.
1931Display SHORTMSG, or MSG if not within edebug."
1932 (interactive)
1933 (setq edebug-mode mode)
1934 (if (< 0 edebug-depth)
1935 (if (eq (current-buffer) edebug-buffer)
1936 (progn
1937 (message shortmsg)
1938 (exit-recursive-edit)))
1939 (message msg)))
1940
1941
1942(defun edebug-step-through ()
1943 "Proceed to next debug step."
1944 (interactive)
1945 (edebug-set-mode 'step "" "edebug will stop before next eval."))
1946
1947(defun edebug-go (arg)
1948 "Go, evaluating until break.
1949With ARG set temporary break at stop point and go."
1950 (interactive "P")
1951 (if arg
1952 (edebug-set-breakpoint t))
1953 (edebug-set-mode 'go "Go..." "edebug will go until break."))
1954
1955(defun edebug-Go-nonstop ()
1956 "Go, evaluating without debugging."
1957 (interactive)
1958 (edebug-set-mode 'Go-nonstop "Go-Nonstop..."
1959 "edebug will not stop at breaks."))
1960
1961(defun edebug-forward-sexp (arg)
1962 "Proceed from the current point to the end of the ARGth sexp ahead.
1963If there are not ARG sexps ahead, then do edebug-step-out."
1964 (interactive "p")
1965 (condition-case err
1966 (let ((parse-sexp-ignore-comments t))
1967 ;; Call forward-sexp repeatedly until done or failure.
1968 (forward-sexp arg)
1969 (edebug-go t))
1970 (error
1971 (edebug-step-out)
1972 )))
1973
1974(defun edebug-step-out ()
1975 "Proceed from the current point to the end of the containing sexp.
1976If there is no containing sexp that is not the top level defun,
1977go to the end of the last sexp, or if that is the same point, then step."
1978 (interactive)
1979 (condition-case err
1980 (let ((parse-sexp-ignore-comments t))
1981 (up-list 1)
1982 (save-excursion
1983 ;; Is there still a containing expression?
1984 (up-list 1))
1985 (edebug-go t))
1986 (error
1987 ;; At top level - 1, so first check if there are more sexps at this level.
1988 (let ((start-point (point)))
1989;; (up-list 1)
1990 (down-list -1)
1991 (if (= (point) start-point)
1992 (edebug-step-through) ; No more at this level, so step.
1993 (edebug-go t)
1994 )))))
1995
1996
1997(defun edebug-goto-here ()
1998 "Proceed to this stop point."
1999 (interactive)
2000 (edebug-go t)
2001 )
2002
2003(defun edebug-trace ()
2004 "Begin trace mode."
2005 (interactive)
2006 (edebug-set-mode 'trace "Tracing..." "edebug will trace with pause."))
2007
2008(defun edebug-Trace-fast ()
2009 "Trace with no wait at each step."
2010 (interactive)
2011 (edebug-set-mode 'Trace-fast
2012 "Trace fast..." "edebug will trace without pause."))
2013
2014(defun edebug-continue ()
2015 "Begin continue mode."
2016 (interactive)
2017 (edebug-set-mode 'continue "Continue..."
2018 "edebug will pause at breakpoints."))
2019
2020(defun edebug-Continue-fast ()
2021 "Trace with no wait at each step."
2022 (interactive)
2023 (edebug-set-mode 'Continue-fast "Continue fast..."
2024 "edebug will stop and go at breakpoints."))
2025
2026
2027(defun edebug-step-in ()
2028 "Step into the function about to be called.
2029Do this before the arguments are evaluated since otherwise it will be
2030too late. One side effect of using edebug-step-in is that the next
2031time the function is called, edebug will be called there as well."
2032 (interactive)
2033 (if (not (eq 'enter edebug-arg-mode))
2034 (error "You must be in front of a function or macro call."))
2035 (let* ((func (car edebug-exp))
2036 (func-marker (get func 'edebug)))
2037 (cond
2038 ((markerp func-marker)
2039 (save-excursion
2040 (set-buffer (marker-buffer func-marker))
2041 (goto-char func-marker)
2042 (edebug-defun)))
2043 ((listp func-marker)
2044 ;; its already been evaluated for edebug
2045 nil)
2046 (t (error "You must first evaluate %s in a buffer." func))))
2047 (exit-recursive-edit))
2048
2049
2050;;(defun edebug-exit-out ()
2051;; "Go until the current function exits."
2052;; (interactive)
2053;; (edebug-set-mode 'exiting "Exit..."))
2054
2055
2056(defun edebug-stop ()
2057 "Useful for exiting from trace loop."
2058 (interactive)
2059 (message "Stop"))
2060
2061
2062;;; The following initial mode setting definitions are not used yet.
2063
2064(defconst edebug-initial-mode-alist
2065 '((edebug-Continue-fast . Continue-fast)
2066 (edebug-Trace-fast . Trace-fast)
2067 (edebug-continue . continue)
2068 (edebug-trace . trace)
2069 (edebug-go . go)
2070 (edebug-step-through . step)
2071 (edebug-Go-nonstop . Go-nonstop)
2072 )
2073 "Association list between commands and the modes they set.")
2074
2075
2076(defun edebug-set-initial-mode ()
2077 "Ask for the initial mode of the enclosing function.
2078The mode is requested via the key that would be used to set the mode in
2079edebug-mode."
2080 (interactive)
2081 (let* ((this-function (edebug-which-function))
2082 (keymap (if (eq edebug-mode-map (current-local-map))
2083 edebug-mode-map))
2084 (old-mode (or (get this-function 'edebug-initial-mode)
2085 edebug-initial-mode))
2086 (key (read-key-sequence
2087 (format
2088 "Change initial edebug mode for %s from %s (%s) to (enter key): "
2089 this-function
2090 old-mode
2091 (where-is-internal
2092 (car (rassq old-mode edebug-initial-mode-alist))
2093 keymap 'firstonly
2094 ))))
2095 (mode (cdr (assq (key-binding key) edebug-initial-mode-alist)))
2096 )
2097 (if (and mode
2098 (or (get this-function 'edebug-initial-mode)
2099 (not (eq mode edebug-initial-mode))))
2100 (progn
2101 (put this-function 'edebug-initial-mode mode)
2102 (message "Initial mode for %s is now: %s"
2103 this-function mode))
2104 (error "Key must map to one of the mode changing commands.")
2105 )))
2106
2107
2108\f
2109;;--------------------------
2110;; Evaluation of expressions
2111
2112(defvar edebug-previous-result nil
2113 "Last result returned from an expression.")
2114
2115(defun edebug-previous-result ()
2116 "Return the previous result."
2117 (interactive)
2118 (let ((print-escape-newlines t)
2119 (print-length 20))
2120 (message "Result: %s" (prin1-to-string edebug-previous-result))))
2121
2122
2123(defun edebug-eval (expr)
2124 "Evaluate EXPR in the outside environment."
2125 (if (not edebug-active)
2126 (error "edebug is not active."))
2127 (edebug-outside-excursion
2128 (eval expr)))
2129
2130(defun edebug-eval-expression (expr)
2131 "Prompt and evaluate an expression in the outside environment.
2132Print result in minibuffer."
2133 (interactive "xEval: ")
2134 (prin1 (edebug-eval expr)))
2135
2136(defun edebug-eval-last-sexp ()
2137 "Evaluate sexp before point in the outside environment;
2138print value in minibuffer."
2139 (interactive)
2140 (prin1 (edebug-eval (edebug-last-sexp))))
2141
2142(defun edebug-eval-print-last-sexp ()
2143 "Evaluate sexp before point in the outside environment;
2144print value into current buffer."
2145 (interactive)
2146 (let ((standard-output (current-buffer)))
2147 (print
2148 (condition-case err
2149 (edebug-eval (edebug-last-sexp))
2150 (error (format "%s: %s"
2151 (get (car err) 'error-message)
2152 (car (cdr err))))))))
2153\f
2154;;;---------------------------------
2155;;; edebug minor mode initialization
2156
2157(defvar edebug-mode 'step
2158 "Current edebug mode set by user.")
2159
2160(defvar edebug-mode-map nil)
2161(if edebug-mode-map
2162 nil
2163 (progn
2164 (setq edebug-mode-map (copy-keymap emacs-lisp-mode-map))
2165 ;; control
2166 (define-key edebug-mode-map " " 'edebug-step-through)
2167 (define-key edebug-mode-map "g" 'edebug-go)
2168 (define-key edebug-mode-map "G" 'edebug-Go-nonstop)
2169 (define-key edebug-mode-map "t" 'edebug-trace)
2170 (define-key edebug-mode-map "T" 'edebug-Trace-fast)
2171 (define-key edebug-mode-map "c" 'edebug-continue)
2172 (define-key edebug-mode-map "C" 'edebug-Continue-fast)
2173
2174 (define-key edebug-mode-map "f" 'edebug-forward-sexp)
2175 (define-key edebug-mode-map "h" 'edebug-goto-here)
2176
2177 (define-key edebug-mode-map "r" 'edebug-previous-result)
2178
2179 (define-key edebug-mode-map "i" 'edebug-step-in)
2180 (define-key edebug-mode-map "o" 'edebug-step-out)
2181
2182;; (define-key edebug-mode-map "m" 'edebug-set-initial-mode)
2183
2184 (define-key edebug-mode-map "q" 'top-level)
2185 (define-key edebug-mode-map "a" 'abort-recursive-edit)
2186 (define-key edebug-mode-map "S" 'edebug-stop)
2187
2188 ;; breakpoints
2189 (define-key edebug-mode-map "b" 'edebug-set-breakpoint)
2190 (define-key edebug-mode-map "u" 'edebug-unset-breakpoint)
2191 (define-key edebug-mode-map "B" 'edebug-next-breakpoint)
2192 (define-key edebug-mode-map "x" 'edebug-set-conditional-breakpoint)
2193
2194 ;; evaluation
2195 (define-key edebug-mode-map "e" 'edebug-eval-expression)
2196 (define-key edebug-mode-map "\C-x\C-e" 'edebug-eval-last-sexp)
2197 (define-key edebug-mode-map "E" 'edebug-visit-eval-list)
2198
2199 ;; views
2200 (define-key edebug-mode-map "w" 'edebug-where)
2201 (define-key edebug-mode-map "v" 'edebug-view-outside)
2202 (define-key edebug-mode-map "p" 'edebug-bounce-point)
2203 (define-key edebug-mode-map "W" 'edebug-toggle-save-windows)
2204
2205 ;; misc
2206 (define-key edebug-mode-map "?" 'edebug-help)
2207 (define-key edebug-mode-map "d" 'edebug-backtrace)
2208
2209 (define-key edebug-mode-map "-" 'negative-argument)
2210 ))
2211
2212
2213(defvar global-edebug-prefix "\^XX"
2214 "Prefix key for global edebug commands, available from any buffer.")
2215
2216(defvar global-edebug-map nil
2217 "Global map of edebug commands, available from any buffer.")
2218
2219(if global-edebug-map
2220 nil
2221 (setq global-edebug-map (make-sparse-keymap))
2222
2223 (global-unset-key global-edebug-prefix)
2224 (global-set-key global-edebug-prefix global-edebug-map)
2225
2226;; (define-key global-edebug-map "X" 'edebug-step-through)
2227 (define-key global-edebug-map " " 'edebug-step-through)
2228 (define-key global-edebug-map "g" 'edebug-go)
2229 (define-key global-edebug-map "G" 'edebug-Go-nonstop)
2230 (define-key global-edebug-map "t" 'edebug-trace)
2231 (define-key global-edebug-map "T" 'edebug-Trace-fast)
2232 (define-key global-edebug-map "c" 'edebug-continue)
2233 (define-key global-edebug-map "C" 'edebug-Continue-fast)
2234
2235;; (define-key global-edebug-map "m" 'edebug-set-initial-mode)
2236 (define-key global-edebug-map "b" 'edebug-set-breakpoint)
2237 (define-key global-edebug-map "x" 'edebug-set-conditional-breakpoint)
2238 (define-key global-edebug-map "u" 'edebug-unset-breakpoint)
2239 (define-key global-edebug-map "w" 'edebug-where)
2240 (define-key global-edebug-map "q" 'top-level)
2241 )
2242
2243
2244(defun edebug-help ()
2245 (interactive)
2246 (describe-function 'edebug-mode))
2247
2248
2249(defun edebug-mode ()
3a801d0c 2250 "Mode for Emacs Lisp buffers while in edebug. Under construction.
84fc2cfa
ER
2251
2252There are both buffer local and global key bindings to several
2253functions. E.g. edebug-step-through is bound to
2254\\[edebug-step-through] in the debug buffer and
2255\\<global-map>\\[edebug-step-through] in any buffer.
2256
2257edebug buffer commands:
2258\\{edebug-mode-map}
2259
2260Global commands prefixed by global-edbug-prefix:
2261\\{global-edebug-map}
2262
2263Options:
2264edebug-all-defuns
2265edebug-eval-macro-args
2266edebug-stop-before-symbols
2267edebug-save-windows
2268edebug-save-point
2269edebug-save-buffer-points
2270edebug-initial-mode
2271edebug-trace
2272"
2273 (use-local-map edebug-mode-map))
2274
2275
2276\f
2277;;===============================================
2278;; edebug eval list mode
2279;; A list of expressions and their evaluations is displayed
2280;; in edebug-eval-buffer
2281
2282(defvar edebug-eval-list nil
2283 "List of expressions to evaluate.")
2284
2285;;(defvar edebug-eval-buffer "*edebug*"
2286;; "*Declared globally so edebug-eval-display can be called independent
2287;;of edebug (not implemented yet).")
2288
2289
2290(defun edebug-eval-result-list ()
2291 "Return a list of evaluations of edebug-eval-list"
2292 ;; Assumes in outside environment.
2293 (mapcar (function
2294 (lambda (expr)
2295 (condition-case err
2296 (eval expr)
2297 (error (format "%s: %s"
2298 (get (car err) 'error-message)
2299 (car (cdr err))))
2300 )))
2301 edebug-eval-list))
2302
2303(defun edebug-eval-display-list (edebug-eval-result-list)
2304 ;; Assumes edebug-eval-buffer exists.
2305 (let ((edebug-eval-list-temp edebug-eval-list)
2306 (standard-output edebug-eval-buffer)
2307 (edebug-display-line
2308 (format ";%s\n" (make-string (- (window-width) 2) ?-))))
2309 (edebug-pop-to-buffer edebug-eval-buffer)
2310 (erase-buffer)
2311 (while edebug-eval-list-temp
2312 (prin1 (car edebug-eval-list-temp)) (terpri)
2313 (prin1 (car edebug-eval-result-list)) (terpri)
2314 (princ edebug-display-line)
2315 (setq edebug-eval-list-temp (cdr edebug-eval-list-temp))
2316 (setq edebug-eval-result-list (cdr edebug-eval-result-list)))
2317 ))
2318
2319(defun edebug-create-eval-buffer ()
2320 (if (not (and edebug-eval-buffer (buffer-name edebug-eval-buffer)))
2321 (progn
2322 (set-buffer (setq edebug-eval-buffer (get-buffer-create "*edebug*")))
2323 (edebug-eval-mode))))
2324
2325;; Should generalize this to be callable outside of edebug
2326;; with calls in user functions, e.g. (edebug-eval-display)
2327
2328(defun edebug-eval-display (edebug-eval-result-list)
2329 "Display expressions and evaluations in EVAL-LIST.
2330It modifies the context by popping up the eval display."
2331 (if edebug-eval-result-list
2332 (progn
2333 (edebug-create-eval-buffer)
2334 (edebug-pop-to-buffer edebug-eval-buffer)
2335 (edebug-eval-display-list edebug-eval-result-list)
2336 )))
2337
2338(defun edebug-eval-redisplay ()
2339 "Redisplay eval list in outside environment.
2340May only be called from within edebug-recursive-edit."
2341 (edebug-create-eval-buffer)
2342 (edebug-pop-to-buffer edebug-eval-buffer)
2343 (edebug-outside-excursion
2344 (edebug-eval-display-list (edebug-eval-result-list))
2345 ))
2346
2347(defun edebug-visit-eval-list ()
2348 (interactive)
2349 (edebug-eval-redisplay)
2350 (edebug-pop-to-buffer edebug-eval-buffer))
2351
2352
2353(defun edebug-update-eval-list ()
2354 "Replace the evaluation list with the sexps now in the eval buffer."
2355 (interactive)
2356 (let ((starting-point (point))
2357 new-list)
2358 (goto-char (point-min))
2359 ;; get the first expression
2360 (edebug-skip-whitespace)
2361 (if (not (eobp))
2362 (progn
2363 (forward-sexp 1)
2364 (setq new-list (cons (edebug-last-sexp) new-list))))
2365
2366 (while (re-search-forward "^;" nil t)
2367 (forward-line 1)
2368 (skip-chars-forward " \t\n\r")
2369 (if (and (/= ?\; (following-char))
2370 (not (eobp)))
2371 (progn
2372 (forward-sexp 1)
2373 (setq new-list (cons (edebug-last-sexp) new-list)))))
2374
2375 (setq edebug-eval-list (nreverse new-list))
2376 (edebug-eval-redisplay)
2377 (goto-char starting-point)))
2378
2379
2380(defun edebug-delete-eval-item ()
2381 "Delete the item under point and redisplay."
2382 ;; could add arg to do repeatedly
2383 (interactive)
2384 (if (re-search-backward "^;" nil 'nofail)
2385 (forward-line 1))
2386 (delete-region
2387 (point) (progn (re-search-forward "^;" nil 'nofail)
2388 (beginning-of-line)
2389 (point)))
2390 (edebug-update-eval-list))
2391
2392
2393
2394(defvar edebug-eval-mode-map nil
2395 "Keymap for edebug-eval-mode. Superset of lisp-interaction-mode.")
2396
2397(if edebug-eval-mode-map
2398 nil
2399 (setq edebug-eval-mode-map (copy-keymap lisp-interaction-mode-map))
2400
2401 (define-key edebug-eval-mode-map "\C-c\C-w" 'edebug-where)
2402 (define-key edebug-eval-mode-map "\C-c\C-d" 'edebug-delete-eval-item)
2403 (define-key edebug-eval-mode-map "\C-c\C-u" 'edebug-update-eval-list)
2404 (define-key edebug-eval-mode-map "\C-x\C-e" 'edebug-eval-last-sexp)
2405 (define-key edebug-eval-mode-map "\C-j" 'edebug-eval-print-last-sexp)
2406 )
2407
2408
2409(defun edebug-eval-mode ()
2410 "Mode for data display buffer while in edebug. Under construction.
2411... ignore the following...
2412There are both buffer local and global key bindings to several
2413functions. E.g. edebug-step-through is bound to
2414\\[edebug-step-through] in the debug buffer and
2415\\<global-map>\\[edebug-step-through] in any buffer.
2416
2417Eval list buffer commands:
2418\\{edebug-eval-mode-map}
2419
2420Global commands prefixed by global-edbug-prefix:
2421\\{global-edebug-map}
2422"
2423 (lisp-interaction-mode)
2424 (setq major-mode 'edebug-eval-mode)
2425 (setq mode-name "Edebug-Eval")
2426 (use-local-map edebug-eval-mode-map))
2427
2428
2429;;========================================
2430;; Interface with standard debugger.
2431
2432(setq debugger 'edebug-debug)
2433;; (setq debugger 'debug) ; use the default
2434
2435;; Note that debug and its utilities must be byte-compiled to work, since
2436;; they depend on the backtrace looking a certain way.
2437
daa37602 2438;;;###autoload
84fc2cfa
ER
2439(defun edebug-debug (&rest debugger-args)
2440 "Replacement for debug.
2441If an error or quit occurred and we are running an edebugged function,
2442show where we last were. Otherwise call debug normally."
2443 (if (and edebug-backtrace ; anything active?
2444 (eq (recursion-depth) edebug-recursion-depth)
2445 )
2446
2447 ;; Where were we before the error occurred?
2448 (let ((edebug-offset-index (car edebug-offset-indices))
2449 (edebug-arg-mode (car debugger-args))
2450 (edebug-exp (car (cdr debugger-args)))
2451 edebug-break-data
2452 edebug-break
2453 (edebug-outside-debug-on-eror debug-on-error)
2454 (debug-on-error nil))
2455 (edebug-display)
2456 )
2457
2458 ;; Otherwise call debug normally.
2459 ;; Still need to remove extraneous edebug calls from stack.
2460 (apply 'debug debugger-args)
2461 ))
2462
2463
2464(defun edebug-backtrace ()
2465 "Display a non-working backtrace. Better than nothing..."
2466 (interactive)
2467 (let ((old-buf (current-buffer)))
2468 (if (not edebug-backtrace-buffer)
2469 (setq edebug-backtrace-buffer
2470 (let ((default-major-mode 'fundamental-mode))
2471 (generate-new-buffer "*Backtrace*"))))
2472 (edebug-pop-to-buffer edebug-backtrace-buffer)
2473 (erase-buffer)
2474 (let ((standard-output (current-buffer))
2475 (print-escape-newlines t)
2476 (print-length 50)
2477 last-ok-point
2478 )
2479 (setq truncate-lines t)
2480 (backtrace)
2481
2482 ;; Clean up the backtrace.
2483 (goto-char (point-min))
2484 (delete-region
2485 (point)
2486 (progn
2487 ;; Everything up to the first edebug is internal.
2488 (re-search-forward "^ edebug(")
2489 (forward-line 1)
2490 (point)))
2491 (forward-line 1)
2492 (setq last-ok-point (point))
2493
2494 ;; Delete interspersed edebug internals.
2495 (while (re-search-forward "^ edebug" nil t)
2496 (if (looking-at "-enter")
2497 ;; delete extraneous progn at top level of function body
2498 (save-excursion
2499 (goto-char last-ok-point)
2500 (forward-line -1)
2501 (setq last-ok-point (point))))
2502 (forward-line 1)
2503 (delete-region last-ok-point (point))
2504 (forward-line 1) ; skip past the good line
2505 (setq last-ok-point (point))
2506 )
2507 )
2508 (edebug-pop-to-buffer old-buf)
2509 ))
2510
2511\f
2512;;========================================================================
2513;; Trace display - append text to a buffer, and update display.
2514;;; e.g.
2515;;; (edebug-trace-display
2516;;; "*trace-point*"
2517;;; "saving: point = %s window-start = %s\n"
2518;;; (point) (window-start))
2519
2520(defun edebug-trace-display (buf-name fmt &rest args)
2521 "In buffer BUF-NAME, display FMT and ARGS at the end and make it visible.
2522The buffer is created if it does not exist.
2523You must include newlines in FMT to break lines."
2524 (let* ((selected-window (selected-window))
2525 (buffer (get-buffer-create buf-name))
2526 (buf-window))
2527 (edebug-pop-to-buffer buffer)
2528 (save-excursion
2529 (setq buf-window (selected-window))
2530 (set-buffer buffer)
2531 (goto-char (point-max))
2532 (insert (apply 'format fmt args))
2533 (set-window-point buf-window (point))
2534 (forward-line (- 1 (window-height buf-window)))
2535 (set-window-start buf-window (point))
2536;; (edebug-sit-for 1)
2537 (bury-buffer buffer)
2538 )
2539 (select-window selected-window)))
2540
2541;;; edebug.el ends here