home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / epoch / epoch-config.el < prev    next >
Encoding:
Text File  |  1991-06-03  |  7.3 KB  |  204 lines

  1. ; Path: dg-rtp!rock.concert.net!mcnc!gatech!usenet.ins.cwru.edu!magnus.acs.ohio-state.edu!zaphod.mps.ohio-state.edu!wuarchive!uunet!mcsun!inesc!dec4pt.puug.pt!spa
  2. ; From: spa@dec4pt.puug.pt (Salvador Pinto Abreu)
  3. ; Newsgroups: gnu.emacs.sources,gnu.epoch.misc
  4. ; Subject: epoch-config.el: save/restore epoch screen configuration (new version)
  5. ; Date: 30 May 91 17:04:30 GMT
  6. ; Organization: Portuguese Unix User's Group
  7. ; OK, here's another try.
  8. ; This file allows the user to save the epoch screen configuration (ie.
  9. ; the size&position of most relevant screens [relevant => eg. not
  10. ; *scratch*], as well as the screens' contents (buffer name, file name
  11. ; and major mode are saved as well)).
  12. ; It produces (with C-x S [upper-case S]) a file suitable for loading
  13. ; into epoch via M-x load-file (or C-x L), which will restore the state
  14. ; of the screens.
  15. ; Useful esp. when you're running a window manager with a large virtual
  16. ; desktop, such as TVTWM (in fact, I've only tested it with tvtwm), and
  17. ; want to keep lots of windows around, carefully positioned.
  18. ; It still needs some work for:
  19. ;  1) support of other window managers (it makes some assumptions about
  20. ;     window containment that may apply only to tvtwm).
  21. ;  2) handling of non-file buffers, such as dired or monkey buffers.
  22. ; It now supports screens with more than one window, and preserves the
  23. ; information regarding what part of the buffer is visible (ie. the
  24. ; first line on the window), and the value of (point).
  25. ; Enjoy,
  26. ; /.salvador
  27.  
  28. ; Save and restore Epoch screen/window configuration.
  29.  
  30. ;; Copyright (C) 1991 Salvador Pinto Abreu <spa@fct.unl.pt>.
  31. ;; Copyright (C) 1991 Free Software Foundation, Inc.
  32.  
  33. ;; This file is part of GNU Emacs.
  34.  
  35. ;; GNU Emacs is distributed in the hope that it will be useful,
  36. ;; but WITHOUT ANY WARRANTY.  No author or distributor
  37. ;; accepts responsibility to anyone for the consequences of using it
  38. ;; or for whether it serves any particular purpose or works at all,
  39. ;; unless he says so in writing.  Refer to the GNU Emacs General Public
  40. ;; License for full details.
  41.  
  42. ;; Everyone is granted permission to copy, modify and redistribute
  43. ;; GNU Emacs, but only under the conditions described in the
  44. ;; GNU Emacs General Public License.   A copy of this license is
  45. ;; supposed to have been given to you along with GNU Emacs so you
  46. ;; can know your rights and responsibilities.  It should be in a
  47. ;; file named COPYING.  Among other things, the copyright notice
  48. ;; and this notice must be preserved on all copies.
  49.  
  50. (provide 'epoch-config)
  51.  
  52. (defun epoch-info ()
  53.   (mapcar 'epoch-screen-info (epoch::screen-list)))
  54.  
  55. (defun dump-epoch-info (buffer)
  56.   (let ((info (epoch-info)))
  57.     (set-buffer buffer)
  58.     (goto-char (point-max))
  59.     (while info
  60.       (cond ((epoch-screen-ok (car info)) (print (car info))))
  61.       (setq info (cdr info)))))
  62.  
  63. (defun epoch-screen-ok (info)
  64.   "Decide whether a screen should be saved in the configuration or not.
  65. For the moment, this saves everything except screens whose name starts with
  66. a '*'."
  67.   (not (= ?* (aref (nth 1 info) 0))))
  68.  
  69. (defun save-epoch-configuration (the-file)
  70.   "Save the current window configuration to THE-FILE."
  71.   (interactive "FSave configuration to file: ")
  72.   (let* ((the-buffer (get-buffer-create "*screen-config*"))
  73.      (standard-output the-buffer))
  74.     (set-buffer the-buffer) (erase-buffer)
  75.     (insert ";;; File automatically generated by epoch-save-configuration.\n")
  76.     (insert (format ";;; Dumped by %s@%s (%s) at %s.\n"
  77.             (user-real-login-name) (system-name) (user-full-name)
  78.             (current-time-string)))
  79.     (dump-epoch-info the-buffer)
  80.     (set-buffer the-buffer) (write-file the-file)
  81.     (kill-buffer the-buffer)))
  82.  
  83. (defun epoch-screen-info (screen)
  84.   (epoch::select-screen screen)
  85.  
  86.   (let* ((parent-window    (car (cdr (epoch::query-tree screen))))
  87.      (vdesk-window     (car (cdr (epoch::query-tree parent-window))))
  88.      (root-window      (car (epoch::query-tree screen)))
  89.  
  90.      (vdesk-info       (cond ((eq vdesk-window root-window) nil)
  91.                  (t (screen-information vdesk-window))))
  92.  
  93.      (x-offset (cond (vdesk-info (- (nth 0 vdesk-info))) (t 0)))
  94.      (y-offset (cond (vdesk-info (- (nth 1 vdesk-info))) (t 0)))
  95.  
  96.      (screen-info      (screen-information))
  97.      (screen-x         (+ (nth 0 screen-info) x-offset))
  98.      (screen-y         (+ (nth 1 screen-info) y-offset))
  99.      (screen-width     (epoch::screen-width screen))
  100.      (screen-height    (epoch::screen-height screen))
  101.      (screen-title     (get-property "WM_NAME"      screen))
  102.      (screen-icon-name (get-property "WM_ICON_NAME" screen))
  103.      (border-width     (nth 4 screen-info))
  104.      (map-state        (nth 5 screen-info))
  105.      (windows          (mapcar 'epoch-window-info
  106.                    (epoch-window-list screen))))
  107.     (list 'epoch-select-one-screen
  108.       screen-title screen-icon-name
  109.       screen-x screen-y
  110.       screen-width screen-height
  111.       (cons 'list windows))))
  112.  
  113.  
  114. (defun epoch-window-list (screen &optional first this)
  115.   "Return the list of windows displayed on SCREEN."
  116.  
  117.   (cond (first (cond
  118.         ((and (not (eq first this))
  119.               (eq screen (epoch::screen-of-window this)))
  120.          (cons this
  121.                (epoch-window-list screen first (next-window this))))))
  122.     (t (let ((first (epoch::first-window screen)))
  123.          (cons first
  124.            (epoch-window-list screen first (next-window first)))))))
  125.  
  126.  
  127. (defun epoch-window-info (window)
  128.   "Return a list (BUFFER-NAME FILE-NAME MAJOR-MODE WINDOW-HEIGHT
  129. WINDOW-START POINT) for WINDOW."
  130.   (save-window-excursion
  131.     (select-window window)
  132.     (` (list (, (buffer-name))        ; 0
  133.          (, (buffer-file-name))    ; 1
  134.          (quote (, major-mode))    ; 2
  135.          (, (window-height))    ; 3
  136.          (, (window-start))        ; 6
  137.          (, (point))))))        ; 7
  138.  
  139.  
  140. (defun epoch-select-one-screen (title icon-name x y width height windows)
  141.   "This function is called from files saved by (dump-epoch-info)."
  142.  
  143.   (let* ((n-windows (length windows))
  144.      (screen-attributes
  145.       (append
  146.        (list (cons 'title title)
  147.          (cons 'icon-name icon-name)
  148.          (cons 'geometry (compute-epoch-geometry x y width height)))
  149.        epoch::screen-properties))
  150.      (screen (create-screen
  151.           (cond ((> n-windows 1) (get-buffer-create-screen "*junk*"))
  152.             (t (let* ((the-window (car windows))
  153.                   (the-buffer (nth 0 the-window))
  154.                   (the-file   (nth 1 the-window))
  155.                   (the-mode   (nth 2 the-window))
  156.                   (w-start    (nth 4 the-window))
  157.                   (p          (nth 5 the-window)))
  158.                  (prog1
  159.                  (set-buffer (find-file-noselect the-file))
  160.                    (funcall the-mode)
  161.                    (cond (w-start
  162.                       (goto-char w-start)
  163.                       (recenter 0)))
  164.                    (cond (p (goto-char p)
  165.                     (beginning-of-line 1)))))))
  166.           screen-attributes)))
  167.  
  168.     (epoch::select-screen screen)
  169.  
  170. ;;; Split screen in as many windows as needed, and select the
  171. ;;; appropriate buffer for each window.
  172.  
  173.     (cond ((> n-windows 1)
  174.        (let ((d-w-height (/ height n-windows)))
  175.          (while windows
  176.            (let* ((the-window (car windows))
  177.               (the-buffer (nth 0 the-window))
  178.               (the-file   (nth 1 the-window))
  179.               (the-mode   (nth 2 the-window))
  180.               (w-start    (nth 4 the-window))
  181.               (p          (nth 5 the-window)))
  182.          (switch-to-buffer (find-file the-file))
  183.          (funcall the-mode)
  184.          (setq windows (cdr windows))
  185.          (cond (windows (split-window (selected-window)
  186.                           (cond (w-height) (d-w-height)))))
  187.          (other-window 1))))
  188.  
  189.             (kill-buffer "*junk*")))))
  190.  
  191. (defun compute-epoch-geometry (x y w h)
  192.   (format "%dx%d+%d+%d" w h x y))
  193.  
  194. (global-set-key "\C-xL" 'load-file)
  195. (global-set-key "\C-xS" 'save-epoch-configuration)
  196.