home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / edebug.el < prev    next >
Encoding:
Text File  |  1992-08-18  |  96.6 KB  |  2,972 lines

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