
(defvar zwgc-slow-display t
  "*Flag telling zwgc whether or not to keep the *zwgc* window
visible after a zwgc-punt, and to always keep the window the
same size.  Useful if you use a slow display.")

(defun zwgc-punt (&optional nodisplay)
  "Delete the current Zephyr notice.  Optional argument NODISPLAY
means not to display the new current notice after punting."
  (interactive)

  ;; Sanity check
  (if (< zwgc-notices 1)
      (error "No notices to punt!"))

  (let ((window-min-height 1)
	(buffer (current-buffer))
	(marker (nth zwgc-marker-index zwgc-marker-list)))
    (set-buffer (get-buffer "*zwgc*"))
    (widen)
    
    (if (not (eq marker (car zwgc-marker-tail)))
	(setq zwgc-marker-list (delq marker zwgc-marker-list))
      (setq zwgc-marker-list (delq marker zwgc-marker-list))
      (setq zwgc-marker-tail (last zwgc-marker-list))
      (if (> zwgc-marker-index 1)
	  (setq zwgc-marker-index (1- zwgc-marker-index))))
    (setq zwgc-notices (1- zwgc-notices))
    
    (delete-region (car marker) (cdr marker))
    (set-marker (car marker) nil)
    (set-marker (cdr marker) nil)
    (if (not (< (point-min) (point-max)))
	(if zwgc-slow-display
	    (if (eq (selected-window) (get-buffer-window "*zwgc*"))
		(other-window 1))
	  (if (not (one-window-p))
	      (progn 
		(delete-windows-on "*zwgc*")
		(bury-buffer "*zwgc*"))))
      (if (not nodisplay)
	  (zwgc-current-notice))
      )
    (run-hooks 'zwgc-killed-noticed-hook)
    (set-buffer buffer)
    ))

(defun zwgc-maybe-resize-window ()
  "Like zwgc-resize-window, but will selected the *zwgc* window
if it's visible, and will do nothing if it's not." 
  (let ((w (get-buffer-window "*zwgc*"))
	(ow (selected-window)))
    (if w
	(progn
	  (select-window w)
	  (zwgc-resize-window)
	  (select-window ow)))))

(defun zwgc-install-window-functions ()
  "Installs replacements for C-x 0, C-x 1, and C-x 2 that try hard 
to keep the *zwgc* buffer displayed and fixed in size."
  (interactive)
  (define-key global-map "\C-x0" 'zwgc-hacked-delete-window)
  (define-key global-map "\C-x1" 'zwgc-hacked-delete-other-windows)
  (define-key global-map "\C-x2" 'zwgc-hacked-split-window))

(defun zwgc-hacked-split-window (&rest args)
  "Replacement for split-window that tries hard
to keep the *zwgc* buffer displayed and fixed in size."
  (interactive)
  (apply 'split-window args)
  (zwgc-maybe-resize-window))

(defun zwgc-hacked-delete-window (&rest args)
  "Replacement for delete-window that tries hard
to keep the *zwgc* buffer displayed and fixed in size."
  (interactive)
  (apply 'delete-window args)
  (zwgc-maybe-resize-window))

(defun zwgc-hacked-delete-other-windows (&rest args)
  "Replacement for delete-other-windows that tries hard
to keep the *zwgc* buffer displayed and fixed in size."
  (interactive)
  (let ((sw (selected-window))
	(zw (get-buffer-window "*zwgc*"))
	(nw (next-window (selected-window) 1))
	w)
    (while (not (eq nw sw))
      (setq w nw)
      (setq nw (next-window nw 1))
      (or (eq w zw)
	  (eq w sw)
	  (delete-window w))))
  (zwgc-maybe-resize-window))
  
(if zwgc-slow-display
    (setq zwgc-window-min-height zwgc-window-max-height)
    (zwgc-install-window-functions))
