;; window-info.jl -- popup a list of all non nil window properties

;; Jonas Linde <jonas@init.se> 2000/03/14
;; two-tier solution by Dave Pearson <davep@davep.org> 2000/03/15
;; identify-window by Isaac Feitler <ikeisaac@netscape.net> 2000/03/16
;; Minor bugfix by Teika kazura in 2010.

;; 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.

(defun describe-window (window)
  "Describe WINDOW.

Details about the X, sawmill and meta properties of WINDOW are written to
`standard-output'."
  (interactive "%W")
  (let ((seq2str (lambda (seq #!optional ofs)
                   (let ((i (1- (length seq)))
			 res)
		     (when (> i 0)
		       (setq res (prin1-to-string (elt seq i))))
                     (while (> i 0)
                       (setq i (1- i))
                       (setq res (concat (prin1-to-string (elt seq i)) 
                                         (if (null ofs) " " ofs) res)))
                     res)))
        (position (window-absolute-position window))
        (viewport (window-viewport window))
        (dimensions (window-dimensions window)))
    (format standard-output "Window Properties %s\n\nX\n" (prin1-to-string window))
    (mapc (lambda (prop)
            (let ((value (get-x-property window prop)))
              (unless (null value)
                (format standard-output "   %s: %s\n"
                        prop
                        (if (= (elt value 0) 'STRING)
                            (translate-string (elt value 2) " ")
                          (seq2str (elt value 2)))))))
          (reverse (delete-if (lambda (el) (eq '_NET_WM_ICON el))
			      (list-x-properties window))))
    (format standard-output "\nsawfish\n")
    (mapc (lambda (prop)
            (let ((value (window-get window prop)))
              (unless (null value)
                (format standard-output "   %s: %s\n" prop value))))
          '(ignored avoid workspaces viewport sticky sticky-viewport focus-click-through
            ignore-window-input-hint never-focus focus-when-mapped
            ignore-program-position place-mode placement-weight type frame-style
            current-frame-style removed-classes shaded hide-client depth placed
            iconified gravity))
    (format standard-output "\nmeta\n   position: %s\n   viewport: %s\n   dimensions: %s\n"
            position viewport dimensions)
    (flush-file standard-output)))

(defun show-window-props ()
  (interactive)
  (call-command-with-output-to-screen describe-window))

(defmacro with-output-to-file (file #!optional body)
  "Writes the output produced by BODY to FILE."
  `(let ((standard-output (open-file ,file 'write)))
    (unwind-protect
         (progn
           ,@body)
      (close-file standard-output))))

(defun dump-window-details (file)
  "Write the description of all managed windows in FILE."
  (with-output-to-file file
    (mapc (lambda (w)
            (format standard-output "%s\n" (make-string 76 ?-))
            (describe-window w)
            (princ "\n"))
          (managed-windows))))

;;useful for calling from a popup menu
(defun identify-window ()
  (call-command-with-output-to-screen '(describe-window (select-window))))

(provide 'window-info)
