]> code.delx.au - gnu-emacs/blob - lisp/window.el
(abs, ceiling, floor): Remove, since they now redefine
[gnu-emacs] / lisp / window.el
1 ;;; window.el --- GNU Emacs window commands aside from those written in C.
2
3 ;;; Copyright (C) 1985, 1989, 1992, 1993 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Code:
24
25 (defun count-windows (&optional minibuf)
26 "Returns the number of visible windows.
27 Optional arg NO-MINI non-nil means don't count the minibuffer
28 even if it is active."
29 (let ((count 0))
30 (walk-windows (function (lambda (w)
31 (setq count (+ count 1))))
32 minibuf)
33 count))
34
35 (defun balance-windows ()
36 "Makes all visible windows the same height (approximately)."
37 (interactive)
38 (let ((count -1) levels newsizes size)
39 ;; Find all the different vpos's at which windows start,
40 ;; then count them. But ignore levels that differ by only 1.
41 (save-window-excursion
42 (let (tops (prev-top -2))
43 (walk-windows (function (lambda (w)
44 (setq tops (cons (nth 1 (window-edges w))
45 tops))))
46 'nomini)
47 (setq tops (sort tops '<))
48 (while tops
49 (if (> (car tops) (1+ prev-top))
50 (setq prev-top (car tops)
51 count (1+ count)))
52 (setq levels (cons (cons (car tops) count) levels))
53 (setq tops (cdr tops)))
54 (setq count (1+ count))))
55 ;; Subdivide the frame into that many vertical levels.
56 (setq size (/ (frame-height) count))
57 (walk-windows (function
58 (lambda (w)
59 (select-window w)
60 (let ((newtop (cdr (assq (nth 1 (window-edges))
61 levels)))
62 (newbot (or (cdr (assq (+ (window-height)
63 (nth 1 (window-edges)))
64 levels))
65 count)))
66 (setq newsizes
67 (cons (cons w (* size (- newbot newtop)))
68 newsizes))))))
69 (walk-windows (function (lambda (w)
70 (select-window w)
71 (let ((newsize (cdr (assq w newsizes))))
72 (enlarge-window (- newsize
73 (window-height))))))
74 'nomini)))
75
76 ;;; I think this should be the default; I think people will prefer it--rms.
77 (defvar split-window-keep-point t
78 "*If non-nil, split windows keeps the original point in both children.
79 This is often more convenient for editing.
80 If nil, adjust point in each of the two windows to minimize redisplay.
81 This is convenient on slow terminals, but point can move strangely.")
82
83 (defun split-window-vertically (&optional arg)
84 "Split current window into two windows, one above the other.
85 The uppermost window gets ARG lines and the other gets the rest.
86 With no argument, split equally or close to it.
87 Both windows display the same buffer now current.
88
89 If the variable split-window-keep-point is non-nil, both new windows
90 will get the same value of point as the current window. This is often
91 more convenient for editing.
92
93 Otherwise, we chose window starts so as to minimize the amount of
94 redisplay; this is convenient on slow terminals. The new selected
95 window is the one that the current value of point appears in. The
96 value of point can change if the text around point is hidden by the
97 new mode line."
98 (interactive "P")
99 (let ((old-w (selected-window))
100 (old-point (point))
101 new-w bottom switch)
102 (setq new-w (split-window nil (and arg (prefix-numeric-value arg))))
103 (or split-window-keep-point
104 (progn
105 (save-excursion
106 (set-buffer (window-buffer))
107 (goto-char (window-start))
108 (vertical-motion (window-height))
109 (set-window-start new-w (point))
110 (if (> (point) (window-point new-w))
111 (set-window-point new-w (point)))
112 (vertical-motion -1)
113 (setq bottom (point)))
114 (if (<= bottom (point))
115 (set-window-point old-w (1- bottom)))
116 (if (< (window-start new-w) old-point)
117 (progn
118 (set-window-point new-w old-point)
119 (select-window new-w)))))
120 new-w))
121
122 (defun split-window-horizontally (&optional arg)
123 "Split current window into two windows side by side.
124 This window becomes the leftmost of the two, and gets
125 ARG columns. No arg means split equally."
126 (interactive "P")
127 (split-window nil (and arg (prefix-numeric-value arg)) t))
128
129 (defun enlarge-window-horizontally (arg)
130 "Make current window ARG columns wider."
131 (interactive "p")
132 (enlarge-window arg t))
133
134 (defun shrink-window-horizontally (arg)
135 "Make current window ARG columns narrower."
136 (interactive "p")
137 (shrink-window arg t))
138
139 (defun shrink-window-if-larger-than-buffer (&optional window)
140 "Shrink the WINDOW to be as small as possible to display its contents.
141 Do nothing if the buffer contains more lines than the present window height,
142 or if some of the window's contents are scrolled out of view,
143 or if the window is the only window of its frame."
144 (interactive)
145 (save-excursion
146 (set-buffer (window-buffer window))
147 (let ((w (selected-window)) ;save-window-excursion can't win
148 (buffer-file-name buffer-file-name)
149 (p (point))
150 (n 0)
151 (ignore-final-newline
152 ;; If buffer ends with a newline, ignore it when counting height
153 ;; unless point is after it.
154 (and (not (eobp))
155 (eq ?\n (char-after (1- (point-max))))))
156 (window-min-height 0)
157 (buffer-read-only nil)
158 (modified (buffer-modified-p))
159 (buffer (current-buffer)))
160 (if (and (< 1 (count-windows))
161 (pos-visible-in-window-p (point-min) window))
162 (unwind-protect
163 (progn
164 (select-window (or window w))
165 (goto-char (point-min))
166 (while (pos-visible-in-window-p
167 (- (point-max)
168 (if ignore-final-newline 1 0)))
169 ;; defeat file locking... don't try this at home, kids!
170 (setq buffer-file-name nil)
171 (insert ?\n) (setq n (1+ n)))
172 (if (> n 0) (shrink-window (1- n))))
173 (delete-region (point-min) (point))
174 (set-buffer-modified-p modified)
175 (goto-char p)
176 (select-window w)
177 ;; Make sure we unbind buffer-read-only
178 ;; with the proper current buffer.
179 (set-buffer buffer))))))
180
181 (define-key ctl-x-map "2" 'split-window-vertically)
182 (define-key ctl-x-map "3" 'split-window-horizontally)
183 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
184 (define-key ctl-x-map "{" 'shrink-window-horizontally)
185 (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
186 (define-key ctl-x-map "+" 'balance-windows)
187
188 ;;; windows.el ends here