 
 
 ;; Set the window configuration state (and make sure we log this).
-(defun rdebug-set-window-configuration-state (state)
+(defun rdebug-set-window-configuration-state (state &optional dont-restore)
   (rdebug-debug-message "Setting state to %s (was %s)"
                         state rdebug-window-configuration-state)
-  (setq rdebug-window-configuration-state state))
+  (when (not (eq state rdebug-window-configuration-state))
+    ;; Save the previous state.
+    (cond ((eq rdebug-window-configuration-state 'debugger)
+           (setq rdebug-debugger-window-configuration
+                 (current-window-configuration)))
+          ((eq rdebug-window-configuration-state 'original)
+           (setq rdebug-original-window-configuration
+                 (current-window-configuration))))
+    (unless dont-restore
+      ;; Switch to the saved state,
+      (cond
+       ((eq state 'debugger)
+        (if rdebug-debugger-window-configuration
+            (set-window-configuration rdebug-debugger-window-configuration)))
+       ((eq state 'original)
+        (if rdebug-original-window-configuration
+            (set-window-configuration rdebug-original-window-configuration)))))
+    (setq rdebug-window-configuration-state state)))
 
 
 ;; -- Common key support.

  (gud-call (format "p %s=%s" var value)))


;; -- display (a.k.a watch window)

(defvar rdebug-display-mode-map
  (let ((map (make-sparse-keymap)))
    (suppress-keymap map)
    (define-key map "a" 'rdebug-display-add)
    (define-key map "\C-d" 'rdebug-display-delete)
    (define-key map "d" 'rdebug-display-delete)
    (define-key map "e" 'rdebug-display-edit)
    (define-key map "\r" 'rdebug-display-edit)
    (rdebug-populate-secondary-buffer-map map)
    map))

(defun rdebug-display-mode ()
  "Major mode for rdebug display (watch) window.

\\{rdebug-display-mode}"
  (interactive)
  (kill-all-local-variables)
  (setq major-mode 'rdebug-display-mode)
  (setq mode-name "RDEBUG Display")
  (setq buffer-read-only t)
  (set (make-local-variable 'rdebug-secondary-buffer) t)
  (use-local-map rdebug-display-mode-map)
  (run-mode-hooks 'rdebug-display-mode-hook))

(defun rdebug--setup-display-buffer (buf comint-buffer)
  (rdebug-debug-enter "rdebug--setup-display-buffer"
    (with-current-buffer buf
      (rdebug-display-mode)
      (set (make-local-variable 'gud-comint-buffer) comint-buffer))))

(defun rdebug-display-add (expr)
  (interactive "sRuby expression: ")
  (if (not (string= expr ""))
      (gud-call (format "display %s" expr))))

(defun rdebug-display-delete ()
  (interactive)
  (save-excursion
    (beginning-of-line)
    (if (looking-at "^\\([0-9]+\\):")
        (gud-call (format "undisplay %s" (match-string 1))))))

(defun rdebug-display-edit (number expr)
  (interactive
   (let ((number nil)
         (expr nil))

(defun rdebug-quit ()
  "Kill current debugger process.

When `rdebug-many-windows' is active, restores the original
window layout."
  (interactive)
  (if (yes-or-no-p "Really quit? ")
      (gud-call "quit unconditionally")))


