;; auto-lower.jl -- automatically lower windows
;; $Id: auto-lower.jl,v 1.3 2000/08/03 09:15:30 grossjoh Exp $

;; Copyright (C) 2000 Kai Grossjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE>

;; This file 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)
;; any later version.

;; sawmill is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with sawmill; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; This package provides functions for doing per-window auto-lower.
;; Most people know about auto-raise; auto-lower could be considered
;; the opposite.  With auto-raise, a window is raised after the mouse
;; enters it; with auto-lower, a window is lowered after the mouse
;; leaves it.

;; How can something like this ever be useful, you ask?  Well, suppose
;; you had an xconsole window which was catching the console output.
;; OT1H, you want to be able to see at a glance what the console
;; messages are, OTOH you don't want the console window to obstruct
;; other, more important windows.  The solution is: just turn on
;; auto-raise and auto-lower for the xconsole window, and then it is
;; sufficient to just move the mouse to a little spot of the window
;; which is still showing.  The window raises, you see what's in it,
;; then you move the mouse out of the window and it's gone again,
;; allowing you to look at the more important windows.
;;
;; Another example is for editing TeX.  There you have a text editor
;; and a previewer, and they don't both fit on screen without
;; obscuring each other.  If you turn on auto-raise and auto-lower for
;; the preview window, you can then just move the mouse into the part
;; of the previewer which is showing, then look at the preview, then
;; move it back into the text editor.

;; But why use this when we have auto-hide, you ask?  Well, it is an
;; alternative to auto-hiding (as provided by the Gnome panel, for
;; example).  But with an auto-hidden window you don't see the window
;; contents even if the screen is otherwise empty.

;;; Code:

(defcustom auto-lower nil
  "Lower windows when they lose focus."
  :type boolean
  :require auto-lower
  :group focus)

(require 'match-window)
(require 'menus)

;; Interoperability with match-window.jl.
(unless (assq 'auto-lower match-window-properties)
  (define-match-window-property 'auto-lower 'other 'boolean))

;; Internal variable.
(defvar disable-auto-lower nil)

;; This rather short function is the meat of the whole thing.
(defun auto-lower-window (w)
  (unless disable-auto-lower
    (when (or (window-get w 'auto-lower) auto-lower)
      (lower-window w))))

;; This one enables the execution of the function.  Maybe it would be
;; more useful to put this in leave-window-hook?  Try it and tell me
;; about your findings.  Or should the hook depend on the focus
;; policy?
(add-hook 'focus-out-hook auto-lower-window)

(defun toggle-window-auto-lower (w)
  "Toggle whether this window is auto-lowered."
  (interactive "%W")
  (window-put w 'auto-lower (not (window-get w 'auto-lower))))

;; Want to be able to toggle auto-lower from the menus.
(unless (rassoc '(toggle-window-auto-lower) window-ops-toggle-menu)
  (setq window-ops-toggle-menu
        (append window-ops-toggle-menu
                (list (list (_ "Auto lower") 'toggle-window-auto-lower)))))

;; The following code should really be in auto-raise.jl
(defun toggle-window-raise-on-focus (w)
  "Toggle whether this window raises on focus."
  (interactive "%W")
  (window-put w 'raise-on-focus (not (window-get w 'raise-on-focus))))

(unless (rassoc '(toggle-window-raise-on-focus) window-ops-toggle-menu)
  (setq window-ops-toggle-menu
        (append window-ops-toggle-menu
                (list (list (_ "Raise on focus") 'toggle-window-raise-on-focus)))))
;; End of code to be put in auto-raise.jl

(provide 'auto-lower)
