Commit | Line | Data |
---|---|---|
76d7458e | 1 | ;;; windows.el --- GNU Emacs window commands aside from those written in C. |
d46bac56 | 2 | |
492878e4 | 3 | ;;; Copyright (C) 1985, 1989, 1992 Free Software Foundation, Inc. |
a2535589 | 4 | |
58142744 ER |
5 | ;; Maintainer: FSF |
6 | ||
a2535589 JA |
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 | |
492878e4 | 11 | ;; the Free Software Foundation; either version 2, or (at your option) |
a2535589 JA |
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 | ||
d46bac56 | 23 | ;;; Code: |
a2535589 JA |
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 () | |
31 | (setq count (+ count 1)))) | |
32 | minibuf) | |
33 | count)) | |
34 | ||
35 | (defun balance-windows () | |
36 | "Makes all visible windows the same size (approximately)." | |
37 | (interactive) | |
38 | (let ((count 0)) | |
39 | (walk-windows (function (lambda (w) | |
40 | (setq count (+ count 1)))) | |
41 | 'nomini) | |
0cc89026 | 42 | (let ((size (/ (frame-height) count))) |
a2535589 JA |
43 | (walk-windows (function (lambda (w) |
44 | (select-window w) | |
45 | (enlarge-window (- size (window-height))))) | |
46 | 'nomini)))) | |
47 | ||
69037c38 | 48 | ;;; I think this should be the default; I think people will prefer it--rms. |
8e4b71d8 | 49 | |
69037c38 | 50 | (defvar split-window-keep-point t |
8e4b71d8 JB |
51 | "*If non-nil, split windows so that both windows keep the original |
52 | value of point. This is often more convenient for editing. | |
53 | If nil, split windows to minimize redisplay. This is convenient on | |
54 | slow terminals, but point may be moved strangely to accommodate the | |
55 | redisplay.") | |
56 | ||
a2535589 JA |
57 | (defun split-window-vertically (&optional arg) |
58 | "Split current window into two windows, one above the other. | |
c65c1681 RS |
59 | The uppermost window gets ARG lines and the other gets the rest. |
60 | With no argument, split equally or close to it. | |
61 | Both windows display the same buffer now current. | |
c65c1681 | 62 | |
8e4b71d8 JB |
63 | If the variable split-window-keep-point is non-nil, both new windows |
64 | will get the same value of point as the current window. This is often | |
65 | more convenient for editing. | |
66 | ||
67 | Otherwise, we chose window starts so as to minimize the amount of | |
68 | redisplay; this is convenient on slow terminals. The new selected | |
69 | window is the one that the current value of point appears in. The | |
70 | value of point can change if the text around point is hidden by the | |
71 | new mode line." | |
a2535589 JA |
72 | (interactive "P") |
73 | (let ((old-w (selected-window)) | |
c65c1681 RS |
74 | (old-point (point)) |
75 | new-w bottom switch) | |
a2535589 | 76 | (setq new-w (split-window nil (and arg (prefix-numeric-value arg)))) |
7d7f1f33 | 77 | (or split-window-keep-point |
c65c1681 | 78 | (progn |
8e4b71d8 JB |
79 | (save-excursion |
80 | (set-buffer (window-buffer)) | |
81 | (goto-char (window-start)) | |
82 | (vertical-motion (window-height)) | |
83 | (set-window-start new-w (point)) | |
84 | (if (> (point) (window-point new-w)) | |
85 | (set-window-point new-w (point))) | |
86 | (vertical-motion -1) | |
87 | (setq bottom (point))) | |
88 | (if (<= bottom (point)) | |
89 | (set-window-point old-w (1- bottom))) | |
90 | (if (< (window-start new-w) old-point) | |
91 | (progn | |
92 | (set-window-point new-w old-point) | |
93 | (select-window new-w))))))) | |
a2535589 JA |
94 | |
95 | (defun split-window-horizontally (&optional arg) | |
96 | "Split current window into two windows side by side. | |
97 | This window becomes the leftmost of the two, and gets | |
98 | ARG columns. No arg means split equally." | |
99 | (interactive "P") | |
100 | (split-window nil (and arg (prefix-numeric-value arg)) t)) | |
101 | ||
102 | (defun enlarge-window-horizontally (arg) | |
103 | "Make current window ARG columns wider." | |
104 | (interactive "p") | |
105 | (enlarge-window arg t)) | |
106 | ||
107 | (defun shrink-window-horizontally (arg) | |
108 | "Make current window ARG columns narrower." | |
109 | (interactive "p") | |
110 | (shrink-window arg t)) | |
111 | ||
a0900d9f ER |
112 | (defun shrink-window-if-larger-than-buffer (&optional window) |
113 | "Shrink the WINDOW to be as small as possible to display its contents. Do | |
114 | nothing if only one window is displayed or if the buffer contains more lines | |
115 | than the present window height." | |
116 | (save-excursion | |
117 | (set-buffer (window-buffer window)) | |
118 | (let ((w (selected-window)) ;save-window-excursion can't win | |
119 | (buffer-file-name buffer-file-name) | |
120 | (p (point)) | |
121 | (n 0) | |
122 | (window-min-height 0) | |
123 | (buffer-read-only nil) | |
124 | (modified (buffer-modified-p)) | |
125 | (buffer (current-buffer))) | |
126 | (unwind-protect | |
127 | (progn | |
128 | (select-window (or window w)) | |
129 | (goto-char (point-min)) | |
130 | (while (pos-visible-in-window-p (point-max)) | |
131 | ;; defeat file locking... don't try this at home, kids! | |
132 | (setq buffer-file-name nil) | |
133 | (insert ?\n) (setq n (1+ n))) | |
134 | (if (> n 0) (shrink-window (1- n)))) | |
135 | (delete-region (point-min) (point)) | |
136 | (set-buffer-modified-p modified) | |
137 | (goto-char p) | |
138 | (select-window w) | |
139 | ;; Make sure we unbind buffer-read-only | |
140 | ;; with the proper current buffer. | |
141 | (set-buffer buffer))))) | |
142 | ||
a2535589 | 143 | (define-key ctl-x-map "2" 'split-window-vertically) |
492878e4 | 144 | (define-key ctl-x-map "3" 'split-window-horizontally) |
a2535589 JA |
145 | (define-key ctl-x-map "}" 'enlarge-window-horizontally) |
146 | (define-key ctl-x-map "{" 'shrink-window-horizontally) | |
a0900d9f ER |
147 | (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer) |
148 | (define-key ctl-x-map "+" 'balance-windows) | |
76d7458e ER |
149 | |
150 | ;;; windows.el ends here |