;;From: Eyvind Ness <eyvind@hrp.no>
;;Date: Wed, 22 Jan 92 13:30 GMT
;;Reply-To: Eyvind.Ness@hrp.no
;;To: gwm-talk@mirsa.inria.fr
;;Subject: lost windows and how to recover them

;; Here is a little GWM code to recover from major window lossage.

;; I have experienced that Xclients suddenly become out of GWM's reach due
;; to corrupted values for window co-ordinates, size, mapped-status etc. In
;; case some of you have seen similar problems, you may find the follwing
;; code-fragments of interest.
;; 
;; Eyvind.
;; 
;; 
;; +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
;;  Eyvind Ness                    Internet Email: eyvind@hrp.no
;;  Research Scientist             Phone: +47 9 183100
;;  Control Room Systems Division  Fax: +47 9 187109
;;  OECD Halden Reactor Project    Surface Mail: P.O. Box 173, N-1751 Halden
;;  Norway
;; +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+

;;;
;;; Put this at the bottom of your ~/.profile.gwm

(require 'dvrooms)     ; or remove the call to (dvroom-reattach) below

;;; make a new menu entry, or modify standard-behaviour, if you prefer.
(if (not (boundp 'en-recover.menu-added)) (progn
    (setq en-recover.menu-added t)
    
    (insert-at
      '(item-make "Recover lost windows" (recover-lost-windows))
      root-pop-items 1)
))

(defun recover-lost-windows ()
  (progn
    (print "\nRecovering lost windows...\n")
    (setq ix 20) (setq iy 100)
    (setq window-list (list-of-windows 'window))
    
    ;; Now, sort all windows ordered by their width:
    (setq
     window-list
     (sort
      window-list
      '(lambda (w1 w2)
	(compare (with (window w2) window-width)
	 (with (window w1) window-width)))))
    
    ;; Then, arrange potential patological cases from top-left to
    ;; bottom-right. Patology is suspected in the following situations:
    ;;		- the window is unmapped
    ;; 		- current position is outside the screen
    ;;		- the window width > the screen width, or < 5 pixels
    ;;		- the window height > the screen height, or < 5 pixels
    (for window window-list
	 (if (or 
	      (not window-is-mapped)
	      (< window-width 5)
	      (> window-width screen-width)
	      (< window-height 5)
	      (> window-height screen-height)
	      (< window-x 0)
	      (> window-x screen-width)
	      (< window-y 0)
	      (> window-y screen-height))
	     (progn
	       (map-window window)
	       (move-window window ix iy)
	       (raise-window)
	       (if (or
		    (< window-width 5) (< window-height 5)
		    (> window-width screen-width)
		    (> window-height screen-height))
		   ;; restore the corrupted values to some arbitrary,
		   ;; though not totally absurd, value:
		   (resize-window 100 100))
	       (print window-status
		      " " window-name
		      "(" window ") moved to (" ix ", " iy ")\n")
	       (setq ix (+ ix 20)) (setq iy (+ iy 20)))))
    ;; Strictly speaking, this could be defined as a separate item in
    ;; the main menu. Added here for convenience only:
    (dvroom-reattach)
    ;; Typically, you dont't want the icons to be stacked, so re-pack
    ;; them if disturbed by the procedure above:
    (rows.pack)
    (print "\nRecovering lost windows... done.\n")))

