;; send-to-head.jl -- send focused window to next/previous xinerama head
;;
;; Author: Pat Regan <thehead@patshead.com>
;;

(require 'sawfish.wm.state.maximize)

(defun send-to-next-head (w)
  "Send window to the next head."
  (interactive "%W")

  (let ((x (car (window-position w)))
        (y (cdr (window-position w)))
	(chx (car (current-head-dimensions w)))
	(chy (cdr (current-head-dimensions w)))
	(chox (car (current-head-offset w)))
	(choy (cdr (current-head-offset w)))
	(nhx 0)
	(nhy 0)
	(ch (current-head w))
	(nh (current-head w))
	(newx 0)
	(newy 0)
	)

    (if (< ch (- (head-count) 1))
	(setq nh (+ ch 1))
      (setq nh 0)
      )



    (setq nhx (car (head-dimensions nh)))
    (setq nhy (cdr (head-dimensions nh)))
    (setq newx (+ (* (/ nhx chx) (- x chox)) (car (head-offset nh))))
    (setq newy (+ (* (/ nhy chy) (- y choy)) (cdr (head-offset nh))))

    (if (window-maximized-p w)
	(
	 (unmaximize-window w)
	 (move-window-to w newx newy)
	 (maximize-window w)
	 )
      (
       (move-window-to w newx newy)
       )
      )
    
    )
  )


(defun send-to-previous-head (w)
  "Send window to the previous head."
  (interactive "%W")

  (let ((x (car (window-position w)))
        (y (cdr (window-position w)))
	(chx (car (current-head-dimensions w)))
	(chy (cdr (current-head-dimensions w)))
	(chox (car (current-head-offset w)))
	(choy (cdr (current-head-offset w)))
	(nhx 0)
	(nhy 0)
	(ch (current-head w))
	(nh (current-head w))
	(newx 0)
	(newy 0)
	)

    (setq ch (+ ch 1))

    (if (= ch (head-count))
	(setq ch 0)
      )



    (setq nhx (car (head-dimensions nh)))
    (setq nhy (cdr (head-dimensions nh)))
    (setq newx (+ (* (/ nhx chx) (- x chox)) (car (head-offset nh))))
    (setq newy (+ (* (/ nhy chy) (- y choy)) (cdr (head-offset nh))))

    (if (window-maximized-p w)
	(
	 (unmaximize-window w)
	 (move-window-to w newx newy)
	 (maximize-window w)
	 )
      (
       (move-window-to w newx newy)
       )
      )
    
    )
  )


(provide 'send-to-head)
