Initial revision
[bpt/emacs.git] / lisp / window.el
1 ;; GNU Emacs window commands aside from those written in C.
2 ;; Copyright (C) 1985, 1989 Free Software Foundation, Inc.
3
4 ;; This file is part of GNU Emacs.
5
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
10
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
15
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20
21 (defun count-windows (&optional minibuf)
22 "Returns the number of visible windows.
23 Optional arg NO-MINI non-nil means don't count the minibuffer
24 even if it is active."
25 (let ((count 0))
26 (walk-windows (function (lambda ()
27 (setq count (+ count 1))))
28 minibuf)
29 count))
30
31 (defun balance-windows ()
32 "Makes all visible windows the same size (approximately)."
33 (interactive)
34 (let ((count 0))
35 (walk-windows (function (lambda (w)
36 (setq count (+ count 1))))
37 'nomini)
38 (let ((size (/ (screen-height) count)))
39 (walk-windows (function (lambda (w)
40 (select-window w)
41 (enlarge-window (- size (window-height)))))
42 'nomini))))
43
44 (defun split-window-vertically (&optional arg)
45 "Split current window into two windows, one above the other.
46 The uppermost window gets ARG lines and the other gets the rest.
47 With no argument, split equally or close to it.
48 Both windows display the same buffer now current.
49 The new selected window is the one that the current value of point
50 appears in.
51
52 The value of point can change if the text around point
53 is hidden by the new mode line."
54 (interactive "P")
55 (let ((old-w (selected-window))
56 (old-point (point))
57 new-w bottom switch)
58 (setq new-w (split-window nil (and arg (prefix-numeric-value arg))))
59 (save-excursion
60 (set-buffer (window-buffer))
61 (goto-char (window-start))
62 (vertical-motion (window-height))
63 (set-window-start new-w (point))
64 (if (> (point) (window-point new-w))
65 (set-window-point new-w (point)))
66 (vertical-motion -1)
67 (setq bottom (point)))
68 (if (<= bottom (point))
69 (set-window-point old-w (1- bottom)))
70 (if (< (window-start new-w) old-point)
71 (progn
72 (set-window-point new-w old-point)
73 (select-window new-w)))))
74
75 (defun split-window-horizontally (&optional arg)
76 "Split current window into two windows side by side.
77 This window becomes the leftmost of the two, and gets
78 ARG columns. No arg means split equally."
79 (interactive "P")
80 (split-window nil (and arg (prefix-numeric-value arg)) t))
81
82 (defun enlarge-window-horizontally (arg)
83 "Make current window ARG columns wider."
84 (interactive "p")
85 (enlarge-window arg t))
86
87 (defun shrink-window-horizontally (arg)
88 "Make current window ARG columns narrower."
89 (interactive "p")
90 (shrink-window arg t))
91
92 (defun window-config-to-register (name)
93 "Save the current window configuration in register REG (a letter).
94 It can be later retrieved using \\[M-x register-to-window-config]."
95 (interactive "cSave window configuration in register: ")
96 (set-register name (current-window-configuration)))
97
98 (defun register-to-window-config (name)
99 "Restore (make current) the window configuration in register REG (a letter).
100 Use with a register previously set with \\[window-config-to-register]."
101 (interactive "cRestore window configuration from register: ")
102 (set-window-configuration (get-register name)))
103
104 (define-key ctl-x-map "2" 'split-window-vertically)
105 (define-key ctl-x-map "5" 'split-window-horizontally)
106 (define-key ctl-x-map "6" 'window-config-to-register)
107 (define-key ctl-x-map "7" 'register-to-window-config)
108 (define-key ctl-x-map "}" 'enlarge-window-horizontally)
109 (define-key ctl-x-map "{" 'shrink-window-horizontally)