XEmacs 21.2.36 "Notos"
[chise/xemacs-chise.git.1] / lisp / window.el
1 ;;; window.el --- XEmacs window commands aside from those written in C.
2
3 ;; Copyright (C) 1985, 1989, 1993-94, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996 Ben Wing.
5
6 ;; Maintainer: XEmacs Development Team
7 ;; Keywords: frames, extensions, dumped
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Synched up with: Emacs/Mule zeta.
27
28 ;;; Commentary:
29
30 ;; This file is dumped with XEmacs.
31
32 ;;; Code:
33
34 ;;;; Window tree functions.
35
36 (defun one-window-p (&optional nomini all-frames device)
37   "Return non-nil if the selected window is the only window (in its frame).
38 Optional arg NOMINI non-nil means don't count the minibuffer
39 even if it is active.
40
41 The optional arg ALL-FRAMES t means count windows on all frames.
42 If it is `visible', count windows on all visible frames.
43 ALL-FRAMES nil or omitted means count only the selected frame,
44 plus the minibuffer it uses (which may be on another frame).
45 ALL-FRAMES = 0 means count windows on all visible and iconified frames.
46 If ALL-FRAMES is any other value, count only the selected frame.
47
48 If optional third argument DEVICE is nil or omitted, count frames
49 on all devices.
50 If a device, count frames only on that device.
51 If a device type, count frames only on devices of that type.
52 Otherwise, count frames only on the selected device."
53   (let ((base-window (selected-window)))
54     (if (and nomini (eq base-window (minibuffer-window)))
55         (setq base-window (next-window base-window)))
56     (eq base-window
57         (next-window base-window (if nomini 'arg) all-frames device))))
58
59 (defun walk-windows (proc &optional minibuf all-frames device)
60   "Cycle through all visible windows, calling PROC for each one.
61 PROC is called with a window as argument.
62
63 Optional second arg MINIBUF t means count the minibuffer window even
64 if not active.  MINIBUF nil or omitted means count the minibuffer iff
65 it is active.  MINIBUF neither t nor nil means not to count the
66 minibuffer even if it is active.
67
68 Several frames may share a single minibuffer; if the minibuffer
69 counts, all windows on all frames that share that minibuffer count
70 too.  Therefore, when a separate minibuffer frame is active,
71 `walk-windows' includes the windows in the frame from which you
72 entered the minibuffer, as well as the minibuffer window.  But if the
73 minibuffer does not count, only windows from WINDOW's frame count.
74
75 ALL-FRAMES is the optional third argument.
76 ALL-FRAMES nil or omitted means cycle within the frames as specified above.
77 ALL-FRAMES = `visible' means include windows on all visible frames.
78 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
79 ALL-FRAMES = t means include windows on all frames including invisible frames.
80 Anything else means restrict to WINDOW's frame.
81
82 If optional fourth argument DEVICE is nil or omitted, include frames
83 on all devices.
84 If a device, include frames only on that device.
85 If a device type, include frames only on devices of that type.
86 Otherwise, include frames only on the selected device."
87   ;; If we start from the minibuffer window, don't fail to come back to it.
88   (if (window-minibuffer-p (selected-window))
89       (setq minibuf t))
90   ;; Note that, like next-window & previous-window, this behaves a little
91   ;; strangely if the selected window is on an invisible frame: it hits
92   ;; some of the windows on that frame, and all windows on visible frames.
93   (let* ((walk-windows-start (selected-window))
94          (walk-windows-current walk-windows-start))
95     (while (progn
96              (setq walk-windows-current
97                    (next-window walk-windows-current minibuf all-frames
98                                 device))
99              (funcall proc walk-windows-current)
100              (not (eq walk-windows-current walk-windows-start))))))
101 ;; The old XEmacs definition of the above clause.  It's more correct in
102 ;; that it will never hit a window that's already been hit even if you
103 ;; do something odd like `delete-other-windows', but has the problem
104 ;; that it conses. (This may be called repeatedly, from lazy-lock
105 ;; for example.)
106 ;  (let* ((walk-windows-history nil)
107 ;        (walk-windows-current (selected-window)))
108 ;    (while (progn
109 ;            (setq walk-windows-current
110 ;                  (next-window walk-windows-current minibuf all-frames
111 ;                               device))
112 ;            (not (memq walk-windows-current walk-windows-history)))
113 ;      (setq walk-windows-history (cons walk-windows-current
114 ;                                      walk-windows-history))
115 ;      (funcall proc walk-windows-current))))
116
117 (defun minibuffer-window-active-p (window)
118   "Return t if WINDOW (a minibuffer window) is now active."
119   (eq window (active-minibuffer-window)))
120
121 (defmacro save-selected-window (&rest body)
122   "Execute BODY, then select the window that was selected before BODY."
123   `(let ((save-selected-window-window (selected-window)))
124      (unwind-protect
125          (progn ,@body)
126        (when (window-live-p save-selected-window-window)
127          (select-window save-selected-window-window)))))
128
129 (defmacro with-selected-window (window &rest body)
130   "Execute forms in BODY with WINDOW as the selected window.
131 The value returned is the value of the last form in BODY."
132   `(save-selected-window
133      (select-window ,window)
134      ,@body))
135
136 \f
137 (defun count-windows (&optional minibuf)
138    "Return the number of visible windows.
139 Optional arg MINIBUF non-nil means count the minibuffer
140 even if it is inactive."
141    (let ((count 0))
142      (walk-windows (function (lambda (w)
143                                (setq count (+ count 1))))
144                    minibuf)
145      count))
146
147 (defun balance-windows ()
148   "Make all visible windows the same height (approximately)."
149   (interactive)
150   (let ((count -1) levels newsizes size)
151         ;FSFmacs
152         ;;; Don't count the lines that are above the uppermost windows.
153         ;;; (These are the menu bar lines, if any.)
154         ;(mbl (nth 1 (window-edges (frame-first-window (selected-frame))))))
155     ;; Find all the different vpos's at which windows start,
156     ;; then count them.  But ignore levels that differ by only 1.
157     (save-window-excursion
158       (let (tops (prev-top -2))
159         (walk-windows (function (lambda (w)
160                         (setq tops (cons (nth 1 (window-pixel-edges w))
161                                          tops))))
162                       'nomini)
163         (setq tops (sort tops '<))
164         (while tops
165           (if (> (car tops) (1+ prev-top))
166               (setq prev-top (car tops)
167                     count (1+ count)))
168           (setq levels (cons (cons (car tops) count) levels))
169           (setq tops (cdr tops)))
170         (setq count (1+ count))))
171     ;; Subdivide the frame into that many vertical levels.
172     ;FSFmacs (setq size (/ (- (frame-height) mbl) count))
173     (setq size (/ (window-pixel-height (frame-root-window)) count))
174     (walk-windows (function
175                    (lambda (w)
176                     (select-window w)
177                     (let ((newtop (cdr (assq (nth 1 (window-pixel-edges))
178                                              levels)))
179                           (newbot (or (cdr (assq
180                                             (+ (window-pixel-height)
181                                                (nth 1 (window-pixel-edges)))
182                                             levels))
183                                       count)))
184                       (setq newsizes
185                             (cons (cons w (* size (- newbot newtop)))
186                                   newsizes)))))
187                   'nomini)
188     (walk-windows (function (lambda (w)
189                               (select-window w)
190                               (let ((newsize (cdr (assq w newsizes))))
191                                 (enlarge-window
192                                  (/ (- newsize (window-pixel-height))
193                                     (face-height 'default))))))
194                   'nomini)))
195 \f
196 ;;; I think this should be the default; I think people will prefer it--rms.
197 (defcustom split-window-keep-point t
198   "*If non-nil, split windows keeps the original point in both children.
199 This is often more convenient for editing.
200 If nil, adjust point in each of the two windows to minimize redisplay.
201 This is convenient on slow terminals, but point can move strangely."
202   :type 'boolean
203   :group 'windows)
204
205 (defun split-window-vertically (&optional arg)
206   "Split current window into two windows, one above the other.
207 The uppermost window gets ARG lines and the other gets the rest.
208 Negative arg means select the size of the lowermost window instead.
209 With no argument, split equally or close to it.
210 Both windows display the same buffer now current.
211
212 If the variable split-window-keep-point is non-nil, both new windows
213 will get the same value of point as the current window.  This is often
214 more convenient for editing.
215
216 Otherwise, we chose window starts so as to minimize the amount of
217 redisplay; this is convenient on slow terminals.  The new selected
218 window is the one that the current value of point appears in.  The
219 value of point can change if the text around point is hidden by the
220 new mode line.
221
222 Programs should probably use split-window instead of this."
223   (interactive "P")
224   (let ((old-w (selected-window))
225         (old-point (point))
226         (size (and arg (prefix-numeric-value arg)))
227         (window-full-p nil)
228         new-w bottom moved)
229     (and size (< size 0) (setq size (+ (window-height) size)))
230     (setq new-w (split-window nil size))
231     (or split-window-keep-point
232         (progn
233           (save-excursion
234             (set-buffer (window-buffer))
235             (goto-char (window-start))
236             (setq moved (vertical-motion (window-height)))
237             (set-window-start new-w (point))
238             (if (> (point) (window-point new-w))
239                 (set-window-point new-w (point)))
240             (and (= moved (window-height))
241                  (progn
242                    (setq window-full-p t)
243                    (vertical-motion -1)))
244             (setq bottom (point)))
245           (and window-full-p
246                (<= bottom (point))
247                (set-window-point old-w (1- bottom)))
248           (and window-full-p
249                (<= (window-start new-w) old-point)
250                (progn
251                  (set-window-point new-w old-point)
252                  (select-window new-w)))))
253     new-w))
254
255 (defun split-window-horizontally (&optional arg)
256   "Split current window into two windows side by side.
257 This window becomes the leftmost of the two, and gets ARG columns.
258 Negative arg means select the size of the rightmost window instead.
259 No arg means split equally."
260   (interactive "P")
261   (let ((size (and arg (prefix-numeric-value arg))))
262     (and size (< size 0)
263          (setq size (+ (window-width) size)))
264     (split-window nil size t)))
265 \f
266 (defun enlarge-window-horizontally (arg)
267   "Make current window ARG columns wider."
268   (interactive "p")
269   (enlarge-window arg t))
270
271 (defun shrink-window-horizontally (arg)
272   "Make current window ARG columns narrower."
273   (interactive "p")
274   (shrink-window arg t))
275
276 (defun shrink-window-if-larger-than-buffer (&optional window)
277   "Shrink the WINDOW to be as small as possible to display its contents.
278 Do not shrink to less than `window-min-height' lines.
279 Do nothing if the buffer contains more lines than the present window height,
280 or if some of the window's contents are scrolled out of view,
281 or if the window is not the full width of the frame,
282 or if the window is the only window of its frame."
283   (interactive)
284   (or window (setq window (selected-window)))
285   (save-excursion
286     (set-buffer (window-buffer window))
287     (let ((n 0)
288           (test-pos
289            (- (point-max)
290               ;; If buffer ends with a newline, ignore it when counting
291               ;; height unless point is after it.
292               (if (and (not (eobp))
293                        (eq ?\n (char-after (1- (point-max)))))
294                   1 0)))
295           (mini (frame-property (window-frame window) 'minibuffer)))
296       (if (and (< 1 (let ((frame (selected-frame)))
297                       (select-frame (window-frame window))
298                       (unwind-protect
299                           (count-windows)
300                         (select-frame frame))))
301                ;; check to make sure that the window is the full width
302                ;; of the frame
303                (window-leftmost-p window)
304                (window-rightmost-p window)
305                ;; The whole buffer must be visible.
306                (pos-visible-in-window-p (point-min) window)
307                ;; The frame must not be minibuffer-only.
308                (not (eq mini 'only)))
309           (progn
310             (save-window-excursion
311               (goto-char (point-min))
312               (while (and (window-live-p window)
313                           (pos-visible-in-window-p test-pos window))
314                 (shrink-window 1 nil window)
315                 (setq n (1+ n))))
316             (if (> n 0)
317                 (shrink-window (min (1- n)
318                                     (- (window-height window)
319                                        (1+ window-min-height)))
320                                nil
321                                window)))))))
322
323 (defun kill-buffer-and-window ()
324   "Kill the current buffer and delete the selected window."
325   (interactive)
326   (if (yes-or-no-p (format "Kill buffer `%s'? " (buffer-name)))
327       (let ((buffer (current-buffer)))
328         (delete-window (selected-window))
329         (kill-buffer buffer))
330     (error "Aborted")))
331
332 ;;; New with XEmacs 20.3
333 ;;; Suggested by Noah Friedman, and tuned by Hrvoje Niksic.
334 (defun window-list (&optional minibuf all-frames device)
335   "Return a list of existing windows.
336 If the optional argument MINIBUF is non-nil, then include minibuffer
337 windows in the result.
338
339 By default, only the windows in the selected frame are returned.
340 The optional argument ALL-FRAMES changes this behavior:
341 ALL-FRAMES = `visible' means include windows on all visible frames.
342 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
343 ALL-FRAMES = t means include windows on all frames including invisible frames.
344 Anything else means restrict to the selected frame.
345 The optional fourth argument DEVICE further clarifies which frames to
346 search as specified by ALL-FRAMES.  This value is only meaningful if
347 ALL-FRAMES is non-nil.
348 If nil or omitted, search only the selected device.
349 If a device, search frames only on that device.
350 If a device type, search frames only on devices of that type.
351 Any other non-nil value means search frames on all devices."
352   (let ((wins nil))
353     (walk-windows (lambda (win)
354                     (push win wins))
355                   minibuf all-frames device)
356     wins))
357
358 ;;; window.el ends here