1 ;;; tapestry.el --- Tools to configure your GNU Emacs windows
3 ;; Copyright (C) 1991, 1993, 1994, 1995, 1997 Kyle E. Jones
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2 of the License, or
8 ;; (at your option) any later version.
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License along
16 ;; with this program; if not, write to the Free Software Foundation, Inc.,
17 ;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 (defvar tapestry-version "1.09")
22 ;; Pass state information between the tapestry-set-window-map
23 ;; and tapestry-set-buffer-map stages. UGH. The reason for this
24 ;; is explained in tapestry-set-buffer-map.
25 (defvar tapestry-windows-changed nil)
28 (defun tapestry (&optional frame-list)
29 "Returns a list containing complete information about the current
30 configuration of Emacs frames, windows, buffers and cursor
31 positions. Call the function set-tapestry with the list that this function
32 returns to restore the configuration.
34 Optional first arg FRAME-LIST should be a list of frames; only
35 configuration information about these frames will be returned.
37 The configuration information is returned in a form that can be saved and
38 restored across multiple Emacs sessions."
39 (let ((frames (or frame-list (tapestry-frame-list)))
40 (frame-map (tapestry-frame-map))
41 (sf (tapestry-selected-frame))
45 (tapestry-select-frame (car frames))
46 (setq other-maps (cons (list (tapestry-window-map)
48 (tapestry-position-map))
51 (tapestry-select-frame sf))
52 (list frame-map other-maps)))
56 (defun set-tapestry (map &optional n root-window-edges)
57 "Restore the frame/window/buffer configuration described by MAP,
58 which should be a list previously returned by a call to
61 Optional second arg N causes frame reconfiguration to be skipped
62 and the windows of the current frame will configured according to
63 the window map of the Nth frame in MAP.
65 Optional third arg ROOT-WINDOW-EDGES non-nil should be a list
66 containing the edges of a window in the current frame. This list
67 should be in the same form as returned by the `window-edges'
68 function. The window configuration from MAP will be restored in
69 this window. If no window with these exact edges exists, a
70 window that lies entirely within the edge coordinates will be
71 expanded until the edge coordinates match or the window bounded by
72 ROOT-WINDOW-EDGES is entirely contained within the expanded
73 window. If no window entirely within the ROOT-WINDOW-EDGES edge
74 coordinates can be found, the window with the greatest overlap of
75 ROOT-WINDOW-EDGES will be used."
76 (let ((sf (tapestry-selected-frame))
77 (tapestry-windows-changed nil)
78 frame-list frame-map other-maps other-map)
79 (setq frame-map (nth 0 map)
80 other-maps (nth 1 map))
81 (if (and root-window-edges (null n))
85 (setq other-map (nth (1- n) other-maps))
87 (error "No such map, %d" n))
89 (tapestry-set-window-map (nth 0 other-map) root-window-edges))
90 (tapestry-set-buffer-map (nth 1 other-map) first-window)
91 (tapestry-set-position-map (nth 2 other-map) first-window))
92 (tapestry-set-frame-map frame-map)
93 ;; frame list is reversed relative to the map order because
94 ;; created frames are added to the head of the list instead
96 (setq frame-list (nreverse (tapestry-frame-list)))
99 (tapestry-select-frame (car frame-list))
100 (tapestry-set-window-map (nth 0 (car other-maps)))
101 (tapestry-set-buffer-map (nth 1 (car other-maps)))
102 (tapestry-set-position-map (nth 2 (car other-maps)))
103 (setq other-maps (cdr other-maps)
104 frame-list (cdr frame-list)))
105 (and (tapestry-frame-live-p sf) (tapestry-select-frame sf))))))
107 (defun tapestry-frame-map ()
108 (let ((map (mapcar 'tapestry-frame-parameters (tapestry-frame-list)))
109 list cell frame-list)
111 frame-list (tapestry-frame-list))
113 (setq cell (assq 'minibuffer (car list)))
114 (if (and cell (windowp (cdr cell)))
115 (if (eq (tapestry-window-frame (cdr cell)) (car frame-list))
117 (setcdr cell 'none)))
118 (setq list (cdr list)
119 frame-list (cdr frame-list)))
122 (defun tapestry-set-frame-map (map)
123 ;; some parameters can only be set only at frame creation time.
124 ;; so all existing frames must die.
125 (let ((doomed-frames (tapestry-frame-list)))
127 (tapestry-make-frame (car map))
128 (setq map (cdr map)))
130 (tapestry-delete-frame (car doomed-frames))
131 (setq doomed-frames (cdr doomed-frames)))))
133 (defun tapestry-window-map ()
134 (let (maps map0 map1 map0-edges map1-edges x-unchanged y-unchanged)
135 (setq maps (mapcar 'tapestry-window-edges (tapestry-window-list)))
139 (setq map1 (cdr map0)
140 map0-edges (tapestry-find-window-map-edges (car map0))
141 map1-edges (tapestry-find-window-map-edges (car map1))
142 x-unchanged (and (= (car map0-edges) (car map1-edges))
143 (= (nth 2 map0-edges) (nth 2 map1-edges)))
144 y-unchanged (and (= (nth 1 map0-edges) (nth 1 map1-edges))
145 (= (nth 3 map0-edges) (nth 3 map1-edges))))
146 (cond ((and (not x-unchanged) (not y-unchanged))
147 (setq map0 (cdr map0)))
148 ((or (and x-unchanged (eq (car (car map0)) '-))
149 (and y-unchanged (eq (car (car map0)) '|)))
150 (nconc (car map0) (list (car map1)))
151 (setcdr map0 (cdr map1)))
153 (setcar map0 (list (if x-unchanged '- '|)
156 (setcdr map0 (cdr map1))))))
159 (defun tapestry-set-window-map (map &optional root-window-edges)
160 (let ((map-width (tapestry-compute-map-width map))
161 (map-height (tapestry-compute-map-height map))
163 (if root-window-edges
164 (let (w-list w-edges w-area
165 exact-w inside-w overlap-w max-overlap overlap)
166 (while (null root-window)
171 w-list (tapestry-window-list))
173 (setq w-edges (tapestry-window-edges (car w-list))
174 w-area (tapestry-window-area w-edges))
175 (if (equal w-edges root-window-edges)
176 (setq exact-w (car w-list)
178 (setq overlap (tapestry-window-overlap w-edges
180 overlap (if overlap (tapestry-window-area overlap) 0)
181 w-area (tapestry-window-area w-edges))
182 (if (< max-overlap overlap)
183 (setq max-overlap overlap
184 overlap-w (car w-list)))
185 ;; set inside-w each time we find a window inside
186 ;; the root window edges. FSF Emacs gives space
187 ;; to the window above or to the left if there is
188 ;; such a window. therefore we want to find the
189 ;; inside window that is bottom-most or right-most so that
190 ;; when we delete it, its space will be given to
191 ;; what will be the root window.
192 (if (= w-area overlap)
193 (setq inside-w (car w-list)))
194 (setq w-list (cdr w-list))))
195 (cond (exact-w (setq root-window exact-w))
197 ;; how could a window be inside the root window
198 ;; edges and there only be one window? a
199 ;; multi-line minibuffer, that's how!
200 (if (not (one-window-p t))
201 (delete-window inside-w)))
202 (t (setq root-window overlap-w))))
203 (tapestry-apply-window-map map map-width map-height root-window)
204 (setq tapestry-windows-changed t)
206 (if (tapestry-windows-match-map map map-width map-height)
207 (tapestry-first-window)
208 (if (fboundp 'frame-reduce-to-one-window)
209 (frame-reduce-to-one-window (selected-frame))
210 ;; `delete-other-windows' may cause the window point to move
211 ;; as it tries to minimize redisplay
212 (delete-other-windows))
213 (setq root-window (selected-window))
214 (tapestry-apply-window-map map map-width map-height root-window)
215 (setq tapestry-windows-changed t)
218 (defun tapestry-buffer-map ()
219 (let ((w-list (tapestry-window-list))
222 (setq b (window-buffer (car w-list))
223 list (cons (list (buffer-file-name b)
226 w-list (cdr w-list)))
229 ;; This version of tapestry-set-buffer-map unconditionally set
230 ;; the window buffer. This confused XEmacs 19.14's scroll-up
231 ;; function when scrolling VM presentation buffers.
232 ;; end-of-buffer was never signaled after a scroll. You can
233 ;; duplicate this by creating a buffer that can be displayed
234 ;; fully in the current window and then run
237 ;; (set-window-buffer (selected-window) (current-buffer))
240 ;;(defun tapestry-set-buffer-map (buffer-map &optional first-window)
241 ;; (let ((w-list (tapestry-window-list first-window)) wb)
242 ;; (while (and w-list buffer-map)
243 ;; (setq wb (car buffer-map))
244 ;; (set-window-buffer
247 ;; (or (get-file-buffer (car wb))
248 ;; (find-file-noselect (car wb)))
249 ;; (get-buffer-create (nth 1 wb))))
250 ;; (setq w-list (cdr w-list)
251 ;; buffer-map (cdr buffer-map)))))
253 (defun tapestry-set-buffer-map (buffer-map &optional first-window)
254 (let ((w-list (tapestry-window-list first-window))
255 current-wb proposed-wb cell)
256 (while (and w-list buffer-map)
257 (setq cell (car buffer-map)
258 proposed-wb (if (car cell)
259 (or (get-file-buffer (car cell))
260 (find-file-noselect (car cell)))
261 (get-buffer-create (nth 1 cell)))
262 current-wb (window-buffer (car w-list)))
263 ;; Setting the window buffer to the same value it already
264 ;; has seems to confuse XEmacs' scroll-up function. But
265 ;; _not_ setting it after windows torn down seem to cause
266 ;; window point to sometimes drift away from point at
267 ;; redisplay time. The solution (hopefully!) is to track
268 ;; when windows have been rearranged and unconditionally do
269 ;; the set-window-buffer, otherwise do it only if the
270 ;; window buffer and the proposed window buffer differ.
271 (if (or tapestry-windows-changed (not (eq proposed-wb current-wb)))
272 (set-window-buffer (car w-list) proposed-wb))
273 (setq w-list (cdr w-list)
274 buffer-map (cdr buffer-map)))))
276 (defun tapestry-position-map ()
277 (let ((sw (selected-window))
278 (w-list (tapestry-window-list))
281 (setq list (cons (list (window-start (car w-list))
282 (window-point (car w-list))
283 (window-hscroll (car w-list))
284 (eq (car w-list) sw))
286 w-list (cdr w-list)))
289 (defun tapestry-set-position-map (position-map &optional first-window)
290 (let ((w-list (tapestry-window-list first-window))
291 (osw (selected-window))
293 (while (and w-list position-map)
294 (setq p (car position-map))
295 (and (car p) (set-window-start (car w-list) (car p)))
296 (and (nth 1 p) (set-window-point (car w-list) (nth 1 p)))
297 (and (nth 2 p) (set-window-hscroll (car w-list) (nth 2 p)))
298 (and (nth 3 p) (setq sw (car w-list)))
299 ;; move this buffer up in the buffer-list
300 (select-window (car w-list))
301 (setq w-list (cdr w-list)
302 position-map (cdr position-map)))
303 (select-window (or sw osw))))
305 (defun tapestry-apply-window-map (map map-width map-height current-window
309 (let ((window-min-height 1)
312 (if (null root-window-width)
313 (setq root-window-height (window-height current-window)
314 root-window-width (window-width current-window)))
317 ((numberp (car map)) (setq map nil))
318 ((eq (car map) '-) (setq horizontal nil))
319 ((eq (car map) '|) (setq horizontal t))
325 (/ (* (tapestry-compute-map-width (car map))
328 (/ (* (tapestry-compute-map-height (car map))
332 (if (not (numberp (car (car map))))
334 (tapestry-apply-window-map (car map)
338 root-window-height)))
339 (and (cdr map) (setq current-window (next-window current-window 0)))))
340 (setq map (cdr map)))
343 (defun tapestry-windows-match-map (map
350 (setq map-width (tapestry-compute-map-width map)
351 map-height (tapestry-compute-map-height map)))
353 (setq window-map (tapestry-window-map)
354 window-map-height (tapestry-compute-map-height window-map)
355 window-map-width (tapestry-compute-map-width window-map)))
357 (cond ((numberp (car map))
358 (and (numberp (car window-map))
359 (= (/ (* (nth 0 map) window-map-width)
362 (= (/ (* (nth 1 map) window-map-height)
365 (= (/ (* (nth 2 map) window-map-width)
368 (= (/ (* (nth 3 map) window-map-height)
370 (nth 3 window-map))))
372 (if (not (eq (car window-map) '-))
375 window-map (cdr window-map))
376 (while (and result map window-map)
377 (setq result (tapestry-windows-match-map (car map)
384 window-map (cdr window-map)))
385 (and result (null map) (null window-map))))
387 (if (not (eq (car window-map) '|))
390 window-map (cdr window-map))
391 (while (and result map window-map)
392 (setq result (tapestry-windows-match-map (car map)
399 window-map (cdr window-map)))
400 (and result (null map) (null window-map)))))))
402 (defun tapestry-find-window-map-edges (map)
403 (let (nw-edges se-edges)
405 (while (and (consp nw-edges) (not (numberp (car nw-edges))))
406 (setq nw-edges (car (cdr nw-edges))))
408 (while (and (consp se-edges) (not (numberp (car se-edges))))
409 (while (cdr se-edges)
410 (setq se-edges (cdr se-edges)))
411 (setq se-edges (car se-edges)))
412 (if (eq nw-edges se-edges)
414 (setq nw-edges (copy-sequence nw-edges))
415 (setcdr (nthcdr 1 nw-edges) (nthcdr 2 se-edges))
418 (defun tapestry-compute-map-width (map)
419 (let ((edges (tapestry-find-window-map-edges map)))
420 (- (nth 2 edges) (car edges))))
422 (defun tapestry-compute-map-height (map)
423 (let ((edges (tapestry-find-window-map-edges map)))
424 (- (nth 3 edges) (nth 1 edges))))
426 ;; delq is to memq as delassq is to assq
427 (defun tapestry-delassq (elt list)
431 (if (eq elt (car (car curr)))
433 (setq list (cdr list) curr list)
434 (setcdr prev (cdr curr))
435 (setq curr (cdr curr)))
436 (setq prev curr curr (cdr curr))))
440 (defun tapestry-remove-frame-parameters (map params)
443 (setq frame-map (nth 0 map))
445 (setcar frame-map (tapestry-delassq (car params) (car frame-map)))
446 (setq frame-map (cdr frame-map)))
447 (setq params (cdr params)))))
450 (defun tapestry-nullify-tapestry-elements (map &optional buf-file-name buf-name
451 window-start window-point
452 window-hscroll selected-window)
454 (setq map (nth 1 map))
456 (setq p (nth 1 (car map)))
458 (and buf-file-name (setcar (car p) nil))
459 (and buf-name (setcar (cdr (car p)) nil))
461 (setq p (nth 2 (car map)))
463 (and window-start (setcar (car p) nil))
464 (and window-point (setcar (cdr (car p)) nil))
465 (and window-hscroll (setcar (nthcdr 2 (car p)) nil))
466 (and selected-window (setcar (nthcdr 3 (car p)) nil))
468 (setq map (cdr map)))))
471 (defun tapestry-replace-tapestry-element (map what function)
472 (let (mapi mapj p old new)
473 (cond ((eq what 'buffer-file-name)
474 (setq mapi 1 mapj 0))
475 ((eq what 'buffer-name)
476 (setq mapi 1 mapj 1))
477 ((eq what 'window-start)
478 (setq mapi 2 mapj 0))
479 ((eq what 'window-point)
480 (setq mapi 2 mapj 1))
481 ((eq what 'window-hscroll)
482 (setq mapi 2 mapj 2))
483 ((eq what 'selected-window)
484 (setq mapi 2 mapj 3)))
485 (setq map (nth 1 map))
487 (setq p (nth mapi (car map)))
489 (setq old (nth mapj (car p))
490 new (funcall function old))
491 (if (not (equal old new))
492 (setcar (nthcdr mapj (car p)) new))
494 (setq map (cdr map)))))
496 (defun tapestry-window-list (&optional first-window)
497 (let* ((first-window (or first-window (tapestry-first-window)))
498 (windows (cons first-window nil))
499 (current-cons windows)
500 (w (next-window first-window 'nomini)))
501 (while (not (eq w first-window))
502 (setq current-cons (setcdr current-cons (cons w nil)))
503 (setq w (next-window w 'nomini)))
506 (defun tapestry-first-window ()
507 (if (eq (tapestry-selected-frame)
508 (tapestry-window-frame (minibuffer-window)))
509 (next-window (minibuffer-window))
510 (let ((w (selected-window))
511 (top (or (cdr (assq 'menu-bar-lines (tapestry-frame-parameters))) 0))
513 (while (or (not (= 0 (car (setq edges (tapestry-window-edges w)))))
514 ;; >= instead of = because in FSF Emacs 19.2x
515 ;; (whenever the Lucid menubar code was added) the
516 ;; menu-bar-lines frame parameter == 1 when the
517 ;; Lucid menubar is enabled even though the
518 ;; menubar doesn't steal the first line from the
520 (not (>= top (nth 1 edges))))
521 (setq w (next-window w 'nomini)))
524 (defun tapestry-window-area (edges)
525 (* (- (nth 3 edges) (nth 1 edges))
526 (- (nth 2 edges) (nth 0 edges))))
528 (defun tapestry-window-overlap (e0 e1)
529 (let (top left bottom right)
530 (cond ((and (<= (nth 0 e0) (nth 0 e1)) (< (nth 0 e1) (nth 2 e0)))
531 (setq left (nth 0 e1)))
532 ((and (<= (nth 0 e1) (nth 0 e0)) (< (nth 0 e0) (nth 2 e1)))
533 (setq left (nth 0 e0))))
534 (cond ((and (< (nth 0 e0) (nth 2 e1)) (<= (nth 2 e1) (nth 2 e0)))
535 (setq right (nth 2 e1)))
536 ((and (< (nth 0 e1) (nth 2 e0)) (<= (nth 2 e0) (nth 2 e1)))
537 (setq right (nth 2 e0))))
538 (cond ((and (<= (nth 1 e0) (nth 1 e1)) (< (nth 1 e1) (nth 3 e0)))
539 (setq top (nth 1 e1)))
540 ((and (<= (nth 1 e1) (nth 1 e0)) (< (nth 1 e0) (nth 3 e1)))
541 (setq top (nth 1 e0))))
542 (cond ((and (< (nth 1 e0) (nth 3 e1)) (<= (nth 3 e1) (nth 3 e0)))
543 (setq bottom (nth 3 e1)))
544 ((and (< (nth 1 e1) (nth 3 e0)) (<= (nth 3 e0) (nth 3 e1)))
545 (setq bottom (nth 3 e0))))
546 (and left top right bottom (list left top right bottom))))
548 (defun tapestry-window-edges (&optional window)
549 (if (and (fboundp 'window-pixel-edges)
550 (fboundp 'face-width)
551 (fboundp 'face-height))
552 (let ((edges (window-pixel-edges window))
555 (setcar tmp (/ (car tmp) (face-width 'default)))
557 (setcar tmp (/ (car tmp) (face-height 'default)))
559 (setcar tmp (/ (car tmp) (face-width 'default)))
561 (setcar tmp (/ (car tmp) (face-height 'default)))
563 (window-edges window)))
565 ;; We call these functions instead of calling the Emacs 19 frame
566 ;; functions directly to let this package work with v18 Emacs.
568 (defun tapestry-frame-list ()
569 (if (fboundp 'frame-list)
573 (defun tapestry-frame-parameters (&optional f)
574 (if (fboundp 'frame-parameters)
578 (defun tapestry-window-frame (w)
579 (if (fboundp 'window-frame)
583 (defun tapestry-modify-frame-parameters (f alist)
584 (if (fboundp 'modify-frame-parameters)
585 (modify-frame-parameters f alist)
588 (defun tapestry-select-frame (f)
589 (if (fboundp 'select-frame)
593 (defun tapestry-selected-frame ()
594 (if (fboundp 'selected-frame)
598 (defun tapestry-next-frame (&optional f all)
599 (if (fboundp 'next-frame)
603 (defun tapestry-make-frame (&optional alist)
604 (if (fboundp 'make-frame)
608 (defun tapestry-delete-frame (&optional f)
609 (if (fboundp 'delete-frame)
613 (defun tapestry-frame-live-p (f)
614 (if (fboundp 'frame-live-p)
620 ;;; tapestry.el ends here