Emacs-PDE

 view release on metacpan or  search on metacpan

lisp/windata.el  view on Meta::CPAN

;;; windata.el --- convert window configuration to list

;; Copyright 2007 Ye Wenbin
;;
;; Author: wenbinye@gmail.com
;; Version: $Id: windata.el,v 0.0 2007/12/13 00:32:15 ywb Exp $
;; Keywords: convenience, frames
;; 
;; This file is part of PDE (Perl Development Environment).
;; But it is useful for generic programming.

;; This program 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.
;;
;; This program 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 this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:
;; This extension is useful when you want save window configuration
;; between emacs sessions or for emacs lisp programer who want handle
;; window layout.

;;; Dependencies:
;;  no extra libraries is required

;; Put this file into your load-path and the following into your ~/.emacs:
;;   (require 'windata)
;; You can use desktop to save window configuration between different
;; session:
;;   (require 'desktop)
;;   (add-to-list 'desktop-globals-to-save 'windata-named-winconf)

;;; Code:

(eval-when-compile
  (require 'cl))

(defvar windata-named-winconf nil
  "Name of all window configuration")
(defvar windata-data-function 'windata-data-default
  "Function to save window data.
The data should be persistent permanent.")
(defvar windata-data-restore-function 'windata-data-restore-default
  "Function to restore window data.")

(defvar windata-total-width nil
  "Internal variable.")
(defvar windata-total-height nil
  "Internal variable.")

(defun windata-fix (arg fix)
  "Round the ARG with FIX decimal.

  (windata-fix 0.123456 4) => 0.1234"
  (let ((n (expt 10 fix)))
    (/ (float (floor (* arg n))) n)))

(defun windata-window-width (win)
  (let ((width (if (windowp win)
                   (window-width win)
                 (let ((edge (cadr win)))
                   (- (nth 2 edge) (car edge))))))
    (windata-fix (/ width windata-total-width) 4)))

(defun windata-window-height (win)
  (let ((height (if (windowp win)
                    (window-height win)
                  (let ((edge (cadr win)))
                    (- (nth 3 edge) (cadr edge))))))
    (windata-fix (/ height windata-total-height) 4)))

(defun windata-data-default (win)
  (buffer-name (window-buffer win)))

(defun windata-data-restore-default (win name)
  (and (buffer-live-p (get-buffer name))
       (set-window-buffer win (get-buffer name))))

(defun windata-window-tree->list (tree)
  (if (windowp tree)
      (funcall windata-data-function tree)
    (let ((dir (car tree))
          (children (cddr tree)))
      (list (if dir 'vertical 'horizontal)
            (if dir
                (windata-window-height (car children))
              (windata-window-width (car children)))
            (windata-window-tree->list (car children))
            (if (> (length children) 2)
                (windata-window-tree->list (cons dir (cons nil (cdr children))))
              (windata-window-tree->list (cadr children)))))))



( run in 1.539 second using v1.01-cache-2.11-cpan-bbb979687b5 )