Merge from emacs--rel--22
[bpt/emacs.git] / lisp / emacs-lisp / edebug.el
index 9290ede..24d3284 100644 (file)
@@ -1,7 +1,7 @@
 ;;; edebug.el --- a source-level debugger for Emacs Lisp
 
 ;; Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1997, 1999,
-;;   2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+;;   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Daniel LaLiberte <liberte@holonexus.org>
 ;; Maintainer: FSF
@@ -11,7 +11,7 @@
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -235,13 +235,6 @@ If the result is non-nil, then break.  Errors are ignored."
 
 ;;; Form spec utilities.
 
-;;;###autoload
-(defmacro def-edebug-spec (symbol spec)
-  "Set the `edebug-form-spec' property of SYMBOL according to SPEC.
-Both SYMBOL and SPEC are unevaluated. The SPEC can be 0, t, a symbol
-\(naming a function), or a list."
-  `(put (quote ,symbol) 'edebug-form-spec (quote ,spec)))
-
 (defmacro def-edebug-form-spec (symbol spec-form)
   "For compatibility with old version."
   (def-edebug-spec symbol (eval spec-form)))
@@ -258,6 +251,20 @@ Both SYMBOL and SPEC are unevaluated. The SPEC can be 0, t, a symbol
     edebug-form-spec
     ))
 
+;;;###autoload
+(defun edebug-basic-spec (spec)
+  "Return t if SPEC uses only extant spec symbols.
+An extant spec symbol is a symbol that is not a function and has a
+`edebug-form-spec' property."
+  (cond ((listp spec)
+        (catch 'basic
+          (while spec
+            (unless (edebug-basic-spec (car spec)) (throw 'basic nil))
+            (setq spec (cdr spec)))
+          t))
+       ((symbolp spec)
+        (unless (functionp spec) (get spec 'edebug-form-spec)))))
+
 ;;; Utilities
 
 ;; Define edebug-gensym - from old cl.el
@@ -357,31 +364,39 @@ Return the result of the last expression in BODY."
 
 (defun edebug-pop-to-buffer (buffer &optional window)
   ;; Like pop-to-buffer, but select window where BUFFER was last shown.
-  ;; Select WINDOW if it provided and it still exists.  Otherwise,
+  ;; Select WINDOW if it is provided and still exists.  Otherwise,
   ;; if buffer is currently shown in several windows, choose one.
   ;; Otherwise, find a new window, possibly splitting one.
-  (setq window (if (and (windowp window) (edebug-window-live-p window)
-                       (eq (window-buffer window) buffer))
-                  window
-                (if (eq (window-buffer (selected-window)) buffer)
-                    (selected-window)
-                  (edebug-get-buffer-window buffer))))
-  (if window
-      (select-window window)
-    (if (one-window-p)
-       (split-window))
-    ;;      (message "next window: %s" (next-window)) (sit-for 1)
-    (if (eq (get-buffer-window edebug-trace-buffer) (next-window))
-       ;; Don't select trace window
-       nil
-      (select-window (next-window))))
-  (set-window-buffer (selected-window) buffer)
-  (set-window-hscroll (selected-window) 0);; should this be??
+  (setq window
+       (cond
+        ((and (edebug-window-live-p window)
+              (eq (window-buffer window) buffer))
+         window)
+        ((eq (window-buffer (selected-window)) buffer)
+         ;; Selected window already displays BUFFER.
+         (selected-window))
+        ((edebug-get-buffer-window buffer))
+        ((one-window-p 'nomini)
+         ;; When there's one window only, split it.
+         (split-window))
+        ((let ((trace-window (get-buffer-window edebug-trace-buffer)))
+           (catch 'found
+             (dolist (elt (window-list nil 'nomini))
+               (unless (or (eq elt (selected-window)) (eq elt trace-window)
+                           (window-dedicated-p elt))
+                 ;; Found a non-dedicated window not showing
+                 ;; `edebug-trace-buffer', use it.
+                 (throw 'found elt))))))
+        ;; All windows are dedicated or show `edebug-trace-buffer', split
+        ;; selected one.
+        (t (split-window))))
+  (select-window window)
+  (set-window-buffer window buffer)
+  (set-window-hscroll window 0);; should this be??
   ;; Selecting the window does not set the buffer until command loop.
   ;;(set-buffer buffer)
   )
 
-
 (defun edebug-get-displayed-buffer-points ()
   ;; Return a list of buffer point pairs, for all displayed buffers.
   (let (list)
@@ -570,6 +585,7 @@ already is one.)"
 ;; Compatibility with old versions.
 (defalias 'edebug-all-defuns 'edebug-all-defs)
 
+;;;###autoload
 (defun edebug-all-defs ()
   "Toggle edebugging of all definitions."
   (interactive)
@@ -578,6 +594,7 @@ already is one.)"
           (if edebug-all-defs "on" "off")))
 
 
+;;;###autoload
 (defun edebug-all-forms ()
   "Toggle edebugging of all forms."
   (interactive)
@@ -1490,7 +1507,7 @@ expressions; a `progn' form will be returned enclosing these forms."
                    head (edebug-move-cursor cursor))))))
 
      ((consp head)
-      (if (eq (car head) ',)
+      (if (eq (car head) '\,)
          ;; The head of a form should normally be a symbol or a lambda
          ;; expression but it can also be an unquote form to be filled
          ;; before evaluation.  We evaluate the arguments anyway, on the
@@ -1647,7 +1664,7 @@ expressions; a `progn' form will be returned enclosing these forms."
      ((fboundp symbol)                 ; is it a predicate?
       (let ((sexp (edebug-top-element-required cursor "Expected" symbol)))
        ;; Special case for edebug-`.
-       (if (and (listp sexp) (eq (car sexp) ',))
+       (if (and (listp sexp) (eq (car sexp) '\,))
            (edebug-match cursor '(("," def-form)))
          (if (not (funcall symbol sexp))
              (edebug-no-match cursor symbol "failed"))
@@ -2085,8 +2102,8 @@ expressions; a `progn' form will be returned enclosing these forms."
 (def-edebug-spec edebug-\` (def-form))
 
 ;; Assume immediate quote in unquotes mean backquote at next higher level.
-(def-edebug-spec , (&or ("quote" edebug-\`) def-form))
-(def-edebug-spec ,@ (&define  ;; so (,@ form) is never wrapped.
+(def-edebug-spec \, (&or ("quote" edebug-\`) def-form))
+(def-edebug-spec \,@ (&define  ;; so (,@ form) is never wrapped.
                     &or ("quote" edebug-\`) def-form))
 
 ;; New byte compiler.
@@ -2516,6 +2533,7 @@ MSG is printed after `::::} '."
 (defvar edebug-outside-o-a-p) ; outside overlay-arrow-position
 (defvar edebug-outside-o-a-s) ; outside overlay-arrow-string
 (defvar edebug-outside-c-i-e-a) ; outside cursor-in-echo-area
+(defvar edebug-outside-d-c-i-n-s-w) ; outside default-cursor-in-non-selected-windows
 
 (defvar edebug-eval-list nil) ;; List of expressions to evaluate.
 
@@ -2546,6 +2564,7 @@ MSG is printed after `::::} '."
        (edebug-outside-buffer (current-buffer))
        (edebug-outside-point (point))
        (edebug-outside-mark (edebug-mark))
+       (edebug-outside-unread-command-events unread-command-events)
        edebug-outside-windows          ; window or screen configuration
        edebug-buffer-points
 
@@ -2557,11 +2576,14 @@ MSG is printed after `::::} '."
 
        (edebug-outside-o-a-p overlay-arrow-position)
        (edebug-outside-o-a-s overlay-arrow-string)
-       (edebug-outside-c-i-e-a cursor-in-echo-area))
+       (edebug-outside-c-i-e-a cursor-in-echo-area)
+       (edebug-outside-d-c-i-n-s-w default-cursor-in-non-selected-windows))
     (unwind-protect
        (let ((overlay-arrow-position overlay-arrow-position)
              (overlay-arrow-string overlay-arrow-string)
              (cursor-in-echo-area nil)
+             (default-cursor-in-non-selected-windows t)
+             (unread-command-events unread-command-events)
              ;; any others??
              )
          (if (not (buffer-name edebug-buffer))
@@ -2650,26 +2672,27 @@ MSG is printed after `::::} '."
 
           (t (message "")))
 
+         (setq unread-command-events nil)
          (if (eq 'after edebug-arg-mode)
              (progn
                ;; Display result of previous evaluation.
                (if (and edebug-break
                         (not (eq edebug-execution-mode 'Continue-fast)))
-                   (sit-for 1))        ; Show break message.
+                    (edebug-sit-for edebug-sit-for-seconds)) ; Show message.
                (edebug-previous-result)))
 
          (cond
           (edebug-break
            (cond
-            ((eq edebug-execution-mode 'continue) (edebug-sit-for 1))
+            ((eq edebug-execution-mode 'continue)
+              (edebug-sit-for edebug-sit-for-seconds))
             ((eq edebug-execution-mode 'Continue-fast) (edebug-sit-for 0))
             (t (setq edebug-stop t))))
           ;; not edebug-break
           ((eq edebug-execution-mode 'trace)
            (edebug-sit-for edebug-sit-for-seconds)) ; Force update and pause.
           ((eq edebug-execution-mode 'Trace-fast)
-           (edebug-sit-for 0))         ; Force update and continue.
-          )
+           (edebug-sit-for 0)))                ; Force update and continue.
 
          (unwind-protect
              (if (or edebug-stop
@@ -2716,7 +2739,7 @@ MSG is printed after `::::} '."
 
                  ;; Unrestore edebug-buffer's window-start, if displayed.
                  (let ((window (car edebug-window-data)))
-                   (if (and window (edebug-window-live-p window)
+                   (if (and (edebug-window-live-p window)
                             (eq (window-buffer) edebug-buffer))
                        (progn
                          (set-window-start window (cdr edebug-window-data)
@@ -2740,7 +2763,8 @@ MSG is printed after `::::} '."
              )                         ; if edebug-save-windows
 
            ;; Restore current buffer always, in case application needs it.
-           (set-buffer edebug-outside-buffer)
+           (if (buffer-name edebug-outside-buffer)
+               (set-buffer edebug-outside-buffer))
            ;; Restore point, and mark.
            ;; Needed even if restoring windows because
            ;; that doesn't restore point and mark in the current buffer.
@@ -2765,9 +2789,11 @@ MSG is printed after `::::} '."
       (with-timeout-unsuspend edebug-with-timeout-suspend)
       ;; Reset global variables to outside values in case they were changed.
       (setq
+       unread-command-events edebug-outside-unread-command-events
        overlay-arrow-position edebug-outside-o-a-p
        overlay-arrow-string edebug-outside-o-a-s
-       cursor-in-echo-area edebug-outside-c-i-e-a)
+       cursor-in-echo-area edebug-outside-c-i-e-a
+       default-cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
       )))
 
 
@@ -2854,7 +2880,6 @@ MSG is printed after `::::} '."
 
        (edebug-outside-last-input-event last-input-event)
        (edebug-outside-last-command-event last-command-event)
-       (edebug-outside-unread-command-events unread-command-events)
        (edebug-outside-last-event-frame last-event-frame)
        (edebug-outside-last-nonmenu-event last-nonmenu-event)
        (edebug-outside-track-mouse track-mouse)
@@ -2876,7 +2901,6 @@ MSG is printed after `::::} '."
              ;; More for Emacs 19
              (last-input-event nil)
              (last-command-event nil)
-             (unread-command-events nil)
              (last-event-frame nil)
              (last-nonmenu-event nil)
              (track-mouse nil)
@@ -2924,6 +2948,7 @@ MSG is printed after `::::} '."
                      (edebug-overlay-arrow))
                  (setq buffer-read-only edebug-buffer-read-only)
                  (use-local-map edebug-outside-map)
+                 (remove-hook 'kill-buffer-hook 'edebug-kill-buffer t)
                  )
              ;; gotta have a buffer to let its buffer local variables be set
              (get-buffer-create " bogus edebug buffer"))
@@ -2936,7 +2961,6 @@ MSG is printed after `::::} '."
        last-command edebug-outside-last-command
        this-command edebug-outside-this-command
        unread-command-char edebug-outside-unread-command-char
-       unread-command-events edebug-outside-unread-command-events
        current-prefix-arg edebug-outside-current-prefix-arg
        last-input-char edebug-outside-last-input-char
        last-input-event edebug-outside-last-input-event
@@ -3271,12 +3295,12 @@ With prefix argument, make it a temporary breakpoint."
   (if (eq (1+ edebug-recursion-depth) (recursion-depth))
       (progn
        (setq edebug-execution-mode mode)
-       (message shortmsg)
+       (message "%s" shortmsg)
        ;; Continue execution
        (exit-recursive-edit))
     ;; This is not terribly useful!!
     (setq edebug-next-execution-mode mode)
-    (message msg)))
+    (message "%s" msg)))
 
 
 (defalias 'edebug-step-through-mode 'edebug-step-mode)
@@ -3405,6 +3429,8 @@ go to the end of the last sexp, or if that is the same point, then step."
       func)
      (t
       (let ((loc (find-function-noselect func)))
+       (unless (cdr loc)
+         (error "Could not find the definition in its file"))
        (with-current-buffer (car loc)
          (goto-char (cdr loc))
          (edebug-eval-top-level-form)
@@ -3580,6 +3606,7 @@ Return the result of the last expression."
           (overlay-arrow-position edebug-outside-o-a-p)
           (overlay-arrow-string edebug-outside-o-a-s)
           (cursor-in-echo-area edebug-outside-c-i-e-a)
+          (default-cursor-in-non-selected-windows edebug-outside-d-c-i-n-s-w)
           )
        (unwind-protect
           (save-excursion              ; of edebug-buffer
@@ -3618,6 +3645,7 @@ Return the result of the last expression."
          edebug-outside-o-a-p overlay-arrow-position
          edebug-outside-o-a-s overlay-arrow-string
          edebug-outside-c-i-e-a cursor-in-echo-area
+         edebug-outside-d-c-i-n-s-w default-cursor-in-non-selected-windows
          )
 
         ;; Restore the outside saved values; don't alter
@@ -3647,44 +3675,6 @@ Return the result of the last expression."
 
 ;;; Printing
 
-;; Replace printing functions.
-
-;; obsolete names
-(define-obsolete-function-alias 'edebug-install-custom-print-funcs
-    'edebug-install-custom-print "22.1")
-(define-obsolete-function-alias 'edebug-reset-print-funcs
-    'edebug-uninstall-custom-print "22.1")
-(define-obsolete-function-alias 'edebug-uninstall-custom-print-funcs
-    'edebug-uninstall-custom-print "22.1")
-
-(defun edebug-install-custom-print ()
-  "Replace print functions used by Edebug with custom versions."
-  ;; Modifying the custom print functions, or changing print-length,
-  ;; print-level, print-circle, custom-print-list or custom-print-vector
-  ;; have immediate effect.
-  (interactive)
-  (require 'cust-print)
-  (defalias 'edebug-prin1 'custom-prin1)
-  (defalias 'edebug-print 'custom-print)
-  (defalias 'edebug-prin1-to-string 'custom-prin1-to-string)
-  (defalias 'edebug-format 'custom-format)
-  (defalias 'edebug-message 'custom-message)
-  "Installed")
-
-(eval-and-compile
-  (defun edebug-uninstall-custom-print ()
-    "Replace edebug custom print functions with internal versions."
-    (interactive)
-    (defalias 'edebug-prin1 'prin1)
-    (defalias 'edebug-print 'print)
-    (defalias 'edebug-prin1-to-string 'prin1-to-string)
-    (defalias 'edebug-format 'format)
-    (defalias 'edebug-message 'message)
-    "Uninstalled")
-
-  ;; Default print functions are the same as Emacs'.
-  (edebug-uninstall-custom-print))
-
 
 (defun edebug-report-error (edebug-value)
   ;; Print an error message like command level does.
@@ -3731,6 +3721,12 @@ Return the result of the last expression."
 
 ;;; Read, Eval and Print
 
+(defalias 'edebug-prin1 'prin1)
+(defalias 'edebug-print 'print)
+(defalias 'edebug-prin1-to-string 'prin1-to-string)
+(defalias 'edebug-format 'format)
+(defalias 'edebug-message 'message)
+
 (defun edebug-eval-expression (edebug-expr)
   "Evaluate an expression in the outside environment.
 If interactive, prompt for the expression.
@@ -3897,7 +3893,7 @@ buffer) there are local and global key bindings to several Edebug
 specific commands.  E.g. `edebug-step-mode' is bound to \\[edebug-step-mode]
 in the Edebug buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
 
-Also see bindings for the eval list buffer, *edebug*.
+Also see bindings for the eval list buffer *edebug* in `edebug-eval-mode'.
 
 The edebug buffer commands:
 \\{edebug-mode-map}
@@ -3924,8 +3920,18 @@ edebug-on-signal
 edebug-unwrap-results
 edebug-global-break-condition
 "
+  ;; If the user kills the buffer in which edebug is currently active,
+  ;; exit to top level, because the edebug command loop can't usefully
+  ;; continue running in such a case.
+  (add-hook 'kill-buffer-hook 'edebug-kill-buffer nil t)
   (use-local-map edebug-mode-map))
 
+(defun edebug-kill-buffer ()
+  "Used on `kill-buffer-hook' when Edebug is operating in a buffer of Lisp code."
+  (let (kill-buffer-hook)
+    (kill-buffer (current-buffer)))
+  (top-level))
+
 ;;; edebug eval list mode
 
 ;; A list of expressions and their evaluations is displayed in *edebug*.
@@ -4054,7 +4060,7 @@ buffer and \\<global-map>\\[edebug-step-mode] in any buffer.
 Eval list buffer commands:
 \\{edebug-eval-mode-map}
 
-Global commands prefixed by global-edebug-prefix:
+Global commands prefixed by `global-edebug-prefix':
 \\{global-edebug-map}")
 
 ;;; Interface with standard debugger.
@@ -4383,7 +4389,7 @@ With prefix argument, make it a temporary breakpoint."
 
   (defun byte-compile-resolve-functions (funcs)
     "Say it is OK for the named functions to be unresolved."
-    (mapcar
+    (mapc
      (function
       (lambda (func)
        (setq byte-compile-unresolved-functions