Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; window.el --- GNU Emacs window commands aside from those written in C |
d46bac56 | 2 | |
0d30b337 | 3 | ;; Copyright (C) 1985, 1989, 1992, 1993, 1994, 2000, 2001, 2002, |
d7a0267c | 4 | ;; 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc. |
a2535589 | 5 | |
58142744 | 6 | ;; Maintainer: FSF |
284b3043 | 7 | ;; Keywords: internal |
58142744 | 8 | |
a2535589 JA |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
a2535589 JA |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
a2535589 | 25 | |
fe7a0e7d SM |
26 | ;;; Commentary: |
27 | ||
28 | ;; Window tree functions. | |
29 | ||
30 | ;;; Code: | |
82e6d8cb | 31 | |
64da8b20 RS |
32 | (defvar window-size-fixed nil |
33 | "*Non-nil in a buffer means windows displaying the buffer are fixed-size. | |
9fa87e0d | 34 | If the value is `height', then only the window's height is fixed. |
64da8b20 RS |
35 | If the value is `width', then only the window's width is fixed. |
36 | Any other non-nil value fixes both the width and the height. | |
37 | Emacs won't change the size of any window displaying that buffer, | |
38 | unless you explicitly change the size, or Emacs has no other choice.") | |
39 | (make-variable-buffer-local 'window-size-fixed) | |
40 | ||
471af22c AS |
41 | (defmacro save-selected-window (&rest body) |
42 | "Execute BODY, then select the window that was selected before BODY. | |
7db3c58f RS |
43 | The value returned is the value of the last form in BODY. |
44 | ||
45 | This macro saves and restores the current buffer, since otherwise | |
46 | its normal operation could potentially make a different | |
198081c8 | 47 | buffer current. It does not alter the buffer list ordering. |
7db3c58f RS |
48 | |
49 | This macro saves and restores the selected window, as well as | |
50 | the selected window in each frame. If the previously selected | |
51 | window of some frame is no longer live at the end of BODY, that | |
52 | frame's selected window is left alone. If the selected window is | |
53 | no longer live, then whatever window is selected at the end of | |
54 | BODY remains selected." | |
991ce473 | 55 | `(let ((save-selected-window-window (selected-window)) |
1c795a0e RS |
56 | ;; It is necessary to save all of these, because calling |
57 | ;; select-window changes frame-selected-window for whatever | |
58 | ;; frame that window is in. | |
991ce473 | 59 | (save-selected-window-alist |
62e3c31f | 60 | (mapcar (lambda (frame) (cons frame (frame-selected-window frame))) |
991ce473 | 61 | (frame-list)))) |
7db3c58f RS |
62 | (save-current-buffer |
63 | (unwind-protect | |
64 | (progn ,@body) | |
65 | (dolist (elt save-selected-window-alist) | |
66 | (and (frame-live-p (car elt)) | |
62e3c31f SM |
67 | (window-live-p (cdr elt)) |
68 | (set-frame-selected-window (car elt) (cdr elt)))) | |
7db3c58f RS |
69 | (if (window-live-p save-selected-window-window) |
70 | (select-window save-selected-window-window)))))) | |
471af22c | 71 | |
e58bff59 RS |
72 | (defun window-body-height (&optional window) |
73 | "Return number of lines in window WINDOW for actual buffer text. | |
74 | This does not include the mode line (if any) or the header line (if any)." | |
75 | (or window (setq window (selected-window))) | |
9df6e638 RS |
76 | (if (window-minibuffer-p window) |
77 | (window-height window) | |
78 | (with-current-buffer (window-buffer window) | |
79 | (max 1 (- (window-height window) | |
80 | (if mode-line-format 1 0) | |
81 | (if header-line-format 1 0)))))) | |
e58bff59 | 82 | |
82e6d8cb | 83 | (defun one-window-p (&optional nomini all-frames) |
90dc1922 | 84 | "Return non-nil if the selected window is the only window. |
82e6d8cb | 85 | Optional arg NOMINI non-nil means don't count the minibuffer |
90dc1922 LT |
86 | even if it is active. Otherwise, the minibuffer is counted |
87 | when it is active. | |
82e6d8cb RS |
88 | |
89 | The optional arg ALL-FRAMES t means count windows on all frames. | |
90 | If it is `visible', count windows on all visible frames. | |
fe7a0e7d | 91 | ALL-FRAMES nil or omitted means count only the selected frame, |
82e6d8cb | 92 | plus the minibuffer it uses (which may be on another frame). |
90dc1922 LT |
93 | ALL-FRAMES 0 means count all windows in all visible or iconified frames. |
94 | If ALL-FRAMES is anything else, count only the selected frame." | |
82e6d8cb RS |
95 | (let ((base-window (selected-window))) |
96 | (if (and nomini (eq base-window (minibuffer-window))) | |
97 | (setq base-window (next-window base-window))) | |
98 | (eq base-window | |
99 | (next-window base-window (if nomini 'arg) all-frames)))) | |
100 | ||
22f9c48c KS |
101 | (defun window-current-scroll-bars (&optional window) |
102 | "Return the current scroll-bar settings in window WINDOW. | |
c62195fa | 103 | Value is a cons (VERTICAL . HORIZONTAL) where VERTICAL specifies the |
22f9c48c | 104 | current location of the vertical scroll-bars (left, right, or nil), |
c62195fa | 105 | and HORIZONTAL specifies the current location of the horizontal scroll |
22f9c48c KS |
106 | bars (top, bottom, or nil)." |
107 | (let ((vert (nth 2 (window-scroll-bars window))) | |
108 | (hor nil)) | |
109 | (when (or (eq vert t) (eq hor t)) | |
90dc1922 | 110 | (let ((fcsb (frame-current-scroll-bars |
22f9c48c KS |
111 | (window-frame (or window (selected-window)))))) |
112 | (if (eq vert t) | |
113 | (setq vert (car fcsb))) | |
114 | (if (eq hor t) | |
115 | (setq hor (cdr fcsb))))) | |
116 | (cons vert hor))) | |
117 | ||
82e6d8cb RS |
118 | (defun walk-windows (proc &optional minibuf all-frames) |
119 | "Cycle through all visible windows, calling PROC for each one. | |
120 | PROC is called with a window as argument. | |
121 | ||
122 | Optional second arg MINIBUF t means count the minibuffer window even | |
4837b516 | 123 | if not active. MINIBUF nil or omitted means count the minibuffer only if |
82e6d8cb RS |
124 | it is active. MINIBUF neither t nor nil means not to count the |
125 | minibuffer even if it is active. | |
126 | ||
127 | Several frames may share a single minibuffer; if the minibuffer | |
128 | counts, all windows on all frames that share that minibuffer count | |
5aa29df8 RS |
129 | too. Therefore, if you are using a separate minibuffer frame |
130 | and the minibuffer is active and MINIBUF says it counts, | |
82e6d8cb | 131 | `walk-windows' includes the windows in the frame from which you |
5aa29df8 | 132 | entered the minibuffer, as well as the minibuffer window. |
82e6d8cb | 133 | |
04ea572f RS |
134 | ALL-FRAMES is the optional third argument. |
135 | ALL-FRAMES nil or omitted means cycle within the frames as specified above. | |
136 | ALL-FRAMES = `visible' means include windows on all visible frames. | |
82e6d8cb | 137 | ALL-FRAMES = 0 means include windows on all visible and iconified frames. |
04ea572f | 138 | ALL-FRAMES = t means include windows on all frames including invisible frames. |
aeb721fe | 139 | If ALL-FRAMES is a frame, it means include windows on that frame. |
c7d031ed | 140 | Anything else means restrict to the selected frame." |
82e6d8cb RS |
141 | ;; If we start from the minibuffer window, don't fail to come back to it. |
142 | (if (window-minibuffer-p (selected-window)) | |
143 | (setq minibuf t)) | |
aeb721fe GM |
144 | (save-selected-window |
145 | (if (framep all-frames) | |
146 | (select-window (frame-first-window all-frames))) | |
d4aff7cc GM |
147 | (let* (walk-windows-already-seen |
148 | (walk-windows-current (selected-window))) | |
aeb721fe GM |
149 | (while (progn |
150 | (setq walk-windows-current | |
151 | (next-window walk-windows-current minibuf all-frames)) | |
d4aff7cc GM |
152 | (not (memq walk-windows-current walk-windows-already-seen))) |
153 | (setq walk-windows-already-seen | |
154 | (cons walk-windows-current walk-windows-already-seen)) | |
155 | (funcall proc walk-windows-current))))) | |
156 | ||
d4d986f2 GM |
157 | (defun get-window-with-predicate (predicate &optional minibuf |
158 | all-frames default) | |
d4aff7cc GM |
159 | "Return a window satisfying PREDICATE. |
160 | ||
161 | This function cycles through all visible windows using `walk-windows', | |
162 | calling PREDICATE on each one. PREDICATE is called with a window as | |
163 | argument. The first window for which PREDICATE returns a non-nil | |
164 | value is returned. If no window satisfies PREDICATE, DEFAULT is | |
165 | returned. | |
166 | ||
167 | Optional second arg MINIBUF t means count the minibuffer window even | |
4837b516 | 168 | if not active. MINIBUF nil or omitted means count the minibuffer only if |
d4aff7cc GM |
169 | it is active. MINIBUF neither t nor nil means not to count the |
170 | minibuffer even if it is active. | |
171 | ||
172 | Several frames may share a single minibuffer; if the minibuffer | |
173 | counts, all windows on all frames that share that minibuffer count | |
174 | too. Therefore, if you are using a separate minibuffer frame | |
175 | and the minibuffer is active and MINIBUF says it counts, | |
176 | `walk-windows' includes the windows in the frame from which you | |
177 | entered the minibuffer, as well as the minibuffer window. | |
178 | ||
179 | ALL-FRAMES is the optional third argument. | |
180 | ALL-FRAMES nil or omitted means cycle within the frames as specified above. | |
181 | ALL-FRAMES = `visible' means include windows on all visible frames. | |
182 | ALL-FRAMES = 0 means include windows on all visible and iconified frames. | |
183 | ALL-FRAMES = t means include windows on all frames including invisible frames. | |
184 | If ALL-FRAMES is a frame, it means include windows on that frame. | |
185 | Anything else means restrict to the selected frame." | |
186 | (catch 'found | |
fe7a0e7d | 187 | (walk-windows #'(lambda (window) |
d4aff7cc GM |
188 | (when (funcall predicate window) |
189 | (throw 'found window))) | |
190 | minibuf all-frames) | |
191 | default)) | |
82e6d8cb | 192 | |
d4d986f2 GM |
193 | (defalias 'some-window 'get-window-with-predicate) |
194 | ||
ba27aa4c RS |
195 | ;; This should probably be written in C (i.e., without using `walk-windows'). |
196 | (defun get-buffer-window-list (buffer &optional minibuf frame) | |
197 | "Return list of all windows displaying BUFFER, or nil if none. | |
198 | BUFFER can be a buffer or a buffer name. | |
199 | See `walk-windows' for the meaning of MINIBUF and FRAME." | |
200 | (let ((buffer (if (bufferp buffer) buffer (get-buffer buffer))) windows) | |
201 | (walk-windows (function (lambda (window) | |
202 | (if (eq (window-buffer window) buffer) | |
203 | (setq windows (cons window windows))))) | |
204 | minibuf frame) | |
205 | windows)) | |
206 | ||
82e6d8cb RS |
207 | (defun minibuffer-window-active-p (window) |
208 | "Return t if WINDOW (a minibuffer window) is now active." | |
0d624ea4 | 209 | (eq window (active-minibuffer-window))) |
82e6d8cb | 210 | \f |
a2535589 | 211 | (defun count-windows (&optional minibuf) |
fe7a0e7d | 212 | "Return the number of visible windows. |
b70b2dd2 KH |
213 | This counts the windows in the selected frame and (if the minibuffer is |
214 | to be counted) its minibuffer frame (if that's not the same frame). | |
215 | The optional arg MINIBUF non-nil means count the minibuffer | |
c6897d8e | 216 | even if it is inactive." |
a2535589 | 217 | (let ((count 0)) |
693b2fe2 | 218 | (walk-windows (lambda (w) (setq count (+ count 1))) |
a2535589 JA |
219 | minibuf) |
220 | count)) | |
221 | ||
fe7a0e7d | 222 | (defun window-safely-shrinkable-p (&optional window) |
119171dd EZ |
223 | "Non-nil if the WINDOW can be shrunk without shrinking other windows. |
224 | If WINDOW is nil or omitted, it defaults to the currently selected window." | |
fc4d69e1 SM |
225 | (with-selected-window (or window (selected-window)) |
226 | (let ((edges (window-edges))) | |
227 | (or (= (nth 2 edges) (nth 2 (window-edges (previous-window)))) | |
228 | (= (nth 0 edges) (nth 0 (window-edges (next-window)))))))) | |
229 | ||
614b38a9 EZ |
230 | \f |
231 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
232 | ;;; `balance-windows' subroutines using `window-tree' | |
233 | ||
234 | ;;; Translate from internal window tree format | |
235 | ||
236 | (defun bw-get-tree (&optional window-or-frame) | |
237 | "Get a window split tree in our format. | |
238 | ||
239 | WINDOW-OR-FRAME must be nil, a frame, or a window. If it is nil, | |
240 | then the whole window split tree for `selected-frame' is returned. | |
241 | If it is a frame, then this is used instead. If it is a window, | |
242 | then the smallest tree containing that window is returned." | |
243 | (when window-or-frame | |
244 | (unless (or (framep window-or-frame) | |
245 | (windowp window-or-frame)) | |
246 | (error "Not a frame or window: %s" window-or-frame))) | |
247 | (let ((subtree (bw-find-tree-sub window-or-frame))) | |
114b3e94 EZ |
248 | (when subtree |
249 | (if (integerp subtree) | |
250 | nil | |
251 | (bw-get-tree-1 subtree))))) | |
614b38a9 EZ |
252 | |
253 | (defun bw-get-tree-1 (split) | |
254 | (if (windowp split) | |
255 | split | |
256 | (let ((dir (car split)) | |
257 | (edges (car (cdr split))) | |
258 | (childs (cdr (cdr split)))) | |
259 | (list | |
260 | (cons 'dir (if dir 'ver 'hor)) | |
261 | (cons 'b (nth 3 edges)) | |
262 | (cons 'r (nth 2 edges)) | |
263 | (cons 't (nth 1 edges)) | |
264 | (cons 'l (nth 0 edges)) | |
265 | (cons 'childs (mapcar #'bw-get-tree-1 childs)))))) | |
266 | ||
267 | (defun bw-find-tree-sub (window-or-frame &optional get-parent) | |
268 | (let* ((window (when (windowp window-or-frame) window-or-frame)) | |
269 | (frame (when (windowp window) (window-frame window))) | |
270 | (wt (car (window-tree frame)))) | |
271 | (when (< 1 (length (window-list frame 0))) | |
272 | (if window | |
273 | (bw-find-tree-sub-1 wt window get-parent) | |
274 | wt)))) | |
275 | ||
276 | (defun bw-find-tree-sub-1 (tree win &optional get-parent) | |
277 | (unless (windowp win) (error "Not a window: %s" win)) | |
278 | (if (memq win tree) | |
279 | (if get-parent | |
280 | get-parent | |
281 | tree) | |
282 | (let ((childs (cdr (cdr tree))) | |
283 | child | |
284 | subtree) | |
285 | (while (and childs (not subtree)) | |
286 | (setq child (car childs)) | |
287 | (setq childs (cdr childs)) | |
288 | (when (and child (listp child)) | |
289 | (setq subtree (bw-find-tree-sub-1 child win get-parent)))) | |
290 | (if (integerp subtree) | |
291 | (progn | |
292 | (if (= 1 subtree) | |
293 | tree | |
294 | (1- subtree))) | |
295 | subtree | |
296 | )))) | |
297 | ||
298 | ;;; Window or object edges | |
299 | ||
7017d254 | 300 | (defun bw-l (obj) |
614b38a9 EZ |
301 | "Left edge of OBJ." |
302 | (if (windowp obj) (nth 0 (window-edges obj)) (cdr (assq 'l obj)))) | |
7017d254 | 303 | (defun bw-t (obj) |
614b38a9 EZ |
304 | "Top edge of OBJ." |
305 | (if (windowp obj) (nth 1 (window-edges obj)) (cdr (assq 't obj)))) | |
7017d254 | 306 | (defun bw-r (obj) |
614b38a9 EZ |
307 | "Right edge of OBJ." |
308 | (if (windowp obj) (nth 2 (window-edges obj)) (cdr (assq 'r obj)))) | |
7017d254 | 309 | (defun bw-b (obj) |
614b38a9 EZ |
310 | "Bottom edge of OBJ." |
311 | (if (windowp obj) (nth 3 (window-edges obj)) (cdr (assq 'b obj)))) | |
312 | ||
313 | ;;; Split directions | |
314 | ||
7017d254 | 315 | (defun bw-dir (obj) |
614b38a9 | 316 | "Return window split tree direction if OBJ. |
7017d254 | 317 | If OBJ is a window return 'both. If it is a window split tree |
614b38a9 EZ |
318 | then return its direction." |
319 | (if (symbolp obj) | |
320 | obj | |
321 | (if (windowp obj) | |
322 | 'both | |
323 | (let ((dir (cdr (assq 'dir obj)))) | |
324 | (unless (memq dir '(hor ver both)) | |
325 | (error "Can't find dir in %s" obj)) | |
326 | dir)))) | |
327 | ||
7017d254 | 328 | (defun bw-eqdir (obj1 obj2) |
614b38a9 EZ |
329 | "Return t if window split tree directions are equal. |
330 | OBJ1 and OBJ2 should be either windows or window split trees in | |
7017d254 | 331 | our format. The directions returned by `bw-dir' are compared and |
614b38a9 EZ |
332 | t is returned if they are `eq' or one of them is 'both." |
333 | (let ((dir1 (bw-dir obj1)) | |
334 | (dir2 (bw-dir obj2))) | |
335 | (or (eq dir1 dir2) | |
336 | (eq dir1 'both) | |
337 | (eq dir2 'both)))) | |
338 | ||
339 | ;;; Building split tree | |
340 | ||
7017d254 | 341 | (defun bw-refresh-edges (obj) |
614b38a9 EZ |
342 | "Refresh the edge information of OBJ and return OBJ." |
343 | (unless (windowp obj) | |
344 | (let ((childs (cdr (assq 'childs obj))) | |
345 | (ol 1000) | |
346 | (ot 1000) | |
347 | (or -1) | |
348 | (ob -1)) | |
349 | (dolist (o childs) | |
350 | (when (> ol (bw-l o)) (setq ol (bw-l o))) | |
351 | (when (> ot (bw-t o)) (setq ot (bw-t o))) | |
352 | (when (< or (bw-r o)) (setq or (bw-r o))) | |
353 | (when (< ob (bw-b o)) (setq ob (bw-b o)))) | |
354 | (setq obj (delq 'l obj)) | |
355 | (setq obj (delq 't obj)) | |
356 | (setq obj (delq 'r obj)) | |
357 | (setq obj (delq 'b obj)) | |
358 | (add-to-list 'obj (cons 'l ol)) | |
359 | (add-to-list 'obj (cons 't ot)) | |
360 | (add-to-list 'obj (cons 'r or)) | |
361 | (add-to-list 'obj (cons 'b ob)) | |
362 | )) | |
363 | obj) | |
364 | ||
365 | ;;; Balance windows | |
366 | ||
7017d254 | 367 | (defun balance-windows (&optional window-or-frame) |
614b38a9 EZ |
368 | "Make windows the same heights or widths in window split subtrees. |
369 | ||
370 | When called non-interactively WINDOW-OR-FRAME may be either a | |
7017d254 JB |
371 | window or a frame. It then balances the windows on the implied |
372 | frame. If the parameter is a window only the corresponding window | |
614b38a9 | 373 | subtree is balanced." |
a2535589 | 374 | (interactive) |
614b38a9 EZ |
375 | (let ( |
376 | (wt (bw-get-tree window-or-frame)) | |
377 | (w) | |
378 | (h) | |
379 | (tried-sizes) | |
380 | (last-sizes) | |
693b2fe2 | 381 | (windows (window-list nil 0))) |
614b38a9 EZ |
382 | (when wt |
383 | (while (not (member last-sizes tried-sizes)) | |
384 | (when last-sizes (setq tried-sizes (cons last-sizes tried-sizes))) | |
7017d254 | 385 | (setq last-sizes (mapcar (lambda (w) |
614b38a9 EZ |
386 | (window-edges w)) |
387 | windows)) | |
388 | (when (eq 'hor (bw-dir wt)) | |
389 | (setq w (- (bw-r wt) (bw-l wt)))) | |
390 | (when (eq 'ver (bw-dir wt)) | |
391 | (setq h (- (bw-b wt) (bw-t wt)))) | |
392 | (bw-balance-sub wt w h))))) | |
393 | ||
7017d254 | 394 | (defun bw-adjust-window (window delta horizontal) |
614b38a9 EZ |
395 | "Wrapper around `adjust-window-trailing-edge' with error checking. |
396 | Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function." | |
62e3c31f SM |
397 | ;; `adjust-window-trailing-edge' may fail if delta is too large. |
398 | (while (>= (abs delta) 1) | |
399 | (condition-case err | |
400 | (progn | |
401 | (adjust-window-trailing-edge window delta horizontal) | |
402 | (setq delta 0)) | |
403 | (error | |
404 | ;;(message "adjust: %s" (error-message-string err)) | |
405 | (setq delta (/ delta 2)))))) | |
614b38a9 | 406 | |
7017d254 | 407 | (defun bw-balance-sub (wt w h) |
614b38a9 EZ |
408 | (setq wt (bw-refresh-edges wt)) |
409 | (unless w (setq w (- (bw-r wt) (bw-l wt)))) | |
410 | (unless h (setq h (- (bw-b wt) (bw-t wt)))) | |
411 | (if (windowp wt) | |
412 | (progn | |
413 | (when w | |
414 | (let ((dw (- w (- (bw-r wt) (bw-l wt))))) | |
415 | (when (/= 0 dw) | |
693b2fe2 | 416 | (bw-adjust-window wt dw t)))) |
614b38a9 EZ |
417 | (when h |
418 | (let ((dh (- h (- (bw-b wt) (bw-t wt))))) | |
419 | (when (/= 0 dh) | |
420 | (bw-adjust-window wt dh nil))))) | |
421 | (let* ((childs (cdr (assq 'childs wt))) | |
614b38a9 EZ |
422 | (cw (when w (/ w (if (bw-eqdir 'hor wt) (length childs) 1)))) |
423 | (ch (when h (/ h (if (bw-eqdir 'ver wt) (length childs) 1))))) | |
424 | (dolist (c childs) | |
693b2fe2 | 425 | (bw-balance-sub c cw ch))))) |
614b38a9 | 426 | |
62e3c31f SM |
427 | ;;; A different solution to balance-windows |
428 | ||
429 | (defun window-fixed-size-p (&optional window direction) | |
430 | "Non-nil if WINDOW cannot be resized in DIRECTION. | |
431 | DIRECTION can be nil (i.e. any), `height' or `width'." | |
432 | (with-current-buffer (window-buffer window) | |
433 | (let ((fixed (and (boundp 'window-size-fixed) window-size-fixed))) | |
434 | (when fixed | |
435 | (not (and direction | |
436 | (member (cons direction window-size-fixed) | |
437 | '((height . width) (width . height))))))))) | |
438 | ||
439 | (defvar window-area-factor 1 | |
440 | "Factor by which the window area should be over-estimated. | |
441 | This is used by `balance-windows-area'. | |
442 | Changing this globally has no effect.") | |
443 | ||
444 | (defun balance-windows-area () | |
445 | "Make all visible windows the same area (approximately). | |
446 | See also `window-area-factor' to change the relative size of specific buffers." | |
447 | (interactive) | |
448 | (let* ((unchanged 0) (carry 0) (round 0) | |
449 | ;; Remove fixed-size windows. | |
450 | (wins (delq nil (mapcar (lambda (win) | |
451 | (if (not (window-fixed-size-p win)) win)) | |
452 | (window-list nil 'nomini)))) | |
453 | (changelog nil) | |
454 | next) | |
455 | ;; Resizing a window changes the size of surrounding windows in complex | |
456 | ;; ways, so it's difficult to balance them all. The introduction of | |
457 | ;; `adjust-window-trailing-edge' made it a bit easier, but it is still | |
458 | ;; very difficult to do. `balance-window' above takes an off-line | |
459 | ;; approach: get the whole window tree, then balance it, then try to | |
460 | ;; adjust the windows so they fit the result. | |
461 | ;; Here, instead, we take a "local optimization" approach, where we just | |
462 | ;; go through all the windows several times until nothing needs to be | |
463 | ;; changed. The main problem with this approach is that it's difficult | |
464 | ;; to make sure it terminates, so we use some heuristic to try and break | |
465 | ;; off infinite loops. | |
466 | ;; After a round without any change, we allow a second, to give a chance | |
467 | ;; to the carry to propagate a minor imbalance from the end back to | |
468 | ;; the beginning. | |
469 | (while (< unchanged 2) | |
470 | ;; (message "New round") | |
471 | (setq unchanged (1+ unchanged) round (1+ round)) | |
472 | (dolist (win wins) | |
473 | (setq next win) | |
474 | (while (progn (setq next (next-window next)) | |
475 | (window-fixed-size-p next))) | |
476 | ;; (assert (eq next (or (cadr (member win wins)) (car wins)))) | |
477 | (let* ((horiz | |
478 | (< (car (window-edges win)) (car (window-edges next)))) | |
479 | (areadiff (/ (- (* (window-height next) (window-width next) | |
480 | (buffer-local-value 'window-area-factor | |
481 | (window-buffer next))) | |
482 | (* (window-height win) (window-width win) | |
483 | (buffer-local-value 'window-area-factor | |
484 | (window-buffer win)))) | |
485 | (max (buffer-local-value 'window-area-factor | |
486 | (window-buffer win)) | |
487 | (buffer-local-value 'window-area-factor | |
488 | (window-buffer next))))) | |
489 | (edgesize (if horiz | |
490 | (+ (window-height win) (window-height next)) | |
491 | (+ (window-width win) (window-width next)))) | |
492 | (diff (/ areadiff edgesize))) | |
493 | (when (zerop diff) | |
494 | ;; Maybe diff is actually closer to 1 than to 0. | |
495 | (setq diff (/ (* 3 areadiff) (* 2 edgesize)))) | |
496 | (when (and (zerop diff) (not (zerop areadiff))) | |
497 | (setq diff (/ (+ areadiff carry) edgesize)) | |
498 | ;; Change things smoothly. | |
499 | (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2)))) | |
500 | (if (zerop diff) | |
501 | ;; Make sure negligible differences don't accumulate to | |
502 | ;; become significant. | |
503 | (setq carry (+ carry areadiff)) | |
504 | (bw-adjust-window win diff horiz) | |
505 | ;; (sit-for 0.5) | |
506 | (let ((change (cons win (window-edges win)))) | |
507 | ;; If the same change has been seen already for this window, | |
508 | ;; we're most likely in an endless loop, so don't count it as | |
509 | ;; a change. | |
510 | (unless (member change changelog) | |
511 | (push change changelog) | |
512 | (setq unchanged 0 carry 0))))))) | |
513 | ;; We've now basically balanced all the windows. | |
514 | ;; But there may be some minor off-by-one imbalance left over, | |
515 | ;; so let's do some fine tuning. | |
516 | ;; (bw-finetune wins) | |
517 | ;; (message "Done in %d rounds" round) | |
518 | )) | |
519 | ||
614b38a9 | 520 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
82e6d8cb | 521 | \f |
4a4accf7 | 522 | ;; I think this should be the default; I think people will prefer it--rms. |
30e19aee | 523 | (defcustom split-window-keep-point t |
90dc1922 LT |
524 | "*If non-nil, \\[split-window-vertically] keeps the original point \ |
525 | in both children. | |
7162c5c4 RS |
526 | This is often more convenient for editing. |
527 | If nil, adjust point in each of the two windows to minimize redisplay. | |
90dc1922 LT |
528 | This is convenient on slow terminals, but point can move strangely. |
529 | ||
530 | This option applies only to `split-window-vertically' and | |
531 | functions that call it. `split-window' always keeps the original | |
7017d254 | 532 | point in both children." |
30e19aee RS |
533 | :type 'boolean |
534 | :group 'windows) | |
8e4b71d8 | 535 | |
a2535589 JA |
536 | (defun split-window-vertically (&optional arg) |
537 | "Split current window into two windows, one above the other. | |
c65c1681 | 538 | The uppermost window gets ARG lines and the other gets the rest. |
90dc1922 | 539 | Negative ARG means select the size of the lowermost window instead. |
c65c1681 RS |
540 | With no argument, split equally or close to it. |
541 | Both windows display the same buffer now current. | |
c65c1681 | 542 | |
3e9890d1 | 543 | If the variable `split-window-keep-point' is non-nil, both new windows |
8e4b71d8 | 544 | will get the same value of point as the current window. This is often |
90dc1922 | 545 | more convenient for editing. The upper window is the selected window. |
8e4b71d8 | 546 | |
90dc1922 | 547 | Otherwise, we choose window starts so as to minimize the amount of |
8e4b71d8 JB |
548 | redisplay; this is convenient on slow terminals. The new selected |
549 | window is the one that the current value of point appears in. The | |
550 | value of point can change if the text around point is hidden by the | |
90dc1922 LT |
551 | new mode line. |
552 | ||
553 | Regardless of the value of `split-window-keep-point', the upper | |
554 | window is the original one and the return value is the new, lower | |
555 | window." | |
a2535589 JA |
556 | (interactive "P") |
557 | (let ((old-w (selected-window)) | |
c65c1681 | 558 | (old-point (point)) |
ab94bf9f | 559 | (size (and arg (prefix-numeric-value arg))) |
4464514e | 560 | (window-full-p nil) |
693b2fe2 | 561 | new-w bottom moved) |
ab94bf9f KH |
562 | (and size (< size 0) (setq size (+ (window-height) size))) |
563 | (setq new-w (split-window nil size)) | |
7d7f1f33 | 564 | (or split-window-keep-point |
c65c1681 | 565 | (progn |
8e4b71d8 JB |
566 | (save-excursion |
567 | (set-buffer (window-buffer)) | |
568 | (goto-char (window-start)) | |
4464514e | 569 | (setq moved (vertical-motion (window-height))) |
8e4b71d8 JB |
570 | (set-window-start new-w (point)) |
571 | (if (> (point) (window-point new-w)) | |
572 | (set-window-point new-w (point))) | |
4464514e RS |
573 | (and (= moved (window-height)) |
574 | (progn | |
575 | (setq window-full-p t) | |
576 | (vertical-motion -1))) | |
577 | (setq bottom (point))) | |
578 | (and window-full-p | |
579 | (<= bottom (point)) | |
580 | (set-window-point old-w (1- bottom))) | |
581 | (and window-full-p | |
582 | (<= (window-start new-w) old-point) | |
583 | (progn | |
584 | (set-window-point new-w old-point) | |
585 | (select-window new-w))))) | |
c361280d RS |
586 | (split-window-save-restore-data new-w old-w))) |
587 | ||
3d2f23d9 RS |
588 | ;; This is to avoid compiler warnings. |
589 | (defvar view-return-to-alist) | |
590 | ||
c361280d | 591 | (defun split-window-save-restore-data (new-w old-w) |
4a4accf7 | 592 | (with-current-buffer (window-buffer) |
c361280d RS |
593 | (if view-mode |
594 | (let ((old-info (assq old-w view-return-to-alist))) | |
6b3b4dbb RS |
595 | (if old-info |
596 | (push (cons new-w (cons (car (cdr old-info)) t)) | |
597 | view-return-to-alist)))) | |
2ed3f64c | 598 | new-w)) |
a2535589 JA |
599 | |
600 | (defun split-window-horizontally (&optional arg) | |
601 | "Split current window into two windows side by side. | |
0ef2c2f2 | 602 | This window becomes the leftmost of the two, and gets ARG columns. |
90dc1922 | 603 | Negative ARG means select the size of the rightmost window instead. |
dd1455c2 RS |
604 | The argument includes the width of the window's scroll bar; if there |
605 | are no scroll bars, it includes the width of the divider column | |
90dc1922 LT |
606 | to the window's right, if any. No ARG means split equally. |
607 | ||
608 | The original, leftmost window remains selected. | |
609 | The return value is the new, rightmost window." | |
a2535589 | 610 | (interactive "P") |
c361280d RS |
611 | (let ((old-w (selected-window)) |
612 | (size (and arg (prefix-numeric-value arg)))) | |
0ef2c2f2 KH |
613 | (and size (< size 0) |
614 | (setq size (+ (window-width) size))) | |
c361280d | 615 | (split-window-save-restore-data (split-window nil size t) old-w))) |
361691dc MB |
616 | |
617 | \f | |
361691dc MB |
618 | (defun set-window-text-height (window height) |
619 | "Sets the height in lines of the text display area of WINDOW to HEIGHT. | |
620 | This doesn't include the mode-line (or header-line if any) or any | |
621 | partial-height lines in the text display area. | |
622 | ||
623 | If WINDOW is nil, the selected window is used. | |
361691dc MB |
624 | |
625 | Note that the current implementation of this function cannot always set | |
626 | the height exactly, but attempts to be conservative, by allocating more | |
627 | lines than are actually needed in the case where some error may be present." | |
628 | (let ((delta (- height (window-text-height window)))) | |
629 | (unless (zerop delta) | |
5937a3fd SM |
630 | ;; Setting window-min-height to a value like 1 can lead to very |
631 | ;; bizarre displays because it also allows Emacs to make *other* | |
632 | ;; windows 1-line tall, which means that there's no more space for | |
633 | ;; the modeline. | |
634 | (let ((window-min-height (min 2 height))) ;One text line plus a modeline. | |
38f99c27 MB |
635 | (if (and window (not (eq window (selected-window)))) |
636 | (save-selected-window | |
637 | (select-window window) | |
638 | (enlarge-window delta)) | |
639 | (enlarge-window delta)))))) | |
361691dc | 640 | |
8075a110 | 641 | \f |
a2535589 JA |
642 | (defun enlarge-window-horizontally (arg) |
643 | "Make current window ARG columns wider." | |
644 | (interactive "p") | |
645 | (enlarge-window arg t)) | |
646 | ||
647 | (defun shrink-window-horizontally (arg) | |
648 | "Make current window ARG columns narrower." | |
649 | (interactive "p") | |
650 | (shrink-window arg t)) | |
651 | ||
3b134005 RS |
652 | (defun window-buffer-height (window) |
653 | "Return the height (in screen lines) of the buffer that WINDOW is displaying." | |
ba96f392 SM |
654 | (with-current-buffer (window-buffer window) |
655 | (max 1 | |
656 | (count-screen-lines (point-min) (point-max) | |
657 | ;; If buffer ends with a newline, ignore it when | |
658 | ;; counting height unless point is after it. | |
659 | (eobp) | |
660 | window)))) | |
3b134005 | 661 | |
c16c855b GM |
662 | (defun count-screen-lines (&optional beg end count-final-newline window) |
663 | "Return the number of screen lines in the region. | |
664 | The number of screen lines may be different from the number of actual lines, | |
665 | due to line breaking, display table, etc. | |
666 | ||
667 | Optional arguments BEG and END default to `point-min' and `point-max' | |
668 | respectively. | |
669 | ||
fe7a0e7d | 670 | If region ends with a newline, ignore it unless optional third argument |
c16c855b GM |
671 | COUNT-FINAL-NEWLINE is non-nil. |
672 | ||
673 | The optional fourth argument WINDOW specifies the window used for obtaining | |
fe7a0e7d | 674 | parameters such as width, horizontal scrolling, and so on. The default is |
c16c855b GM |
675 | to use the selected window's parameters. |
676 | ||
677 | Like `vertical-motion', `count-screen-lines' always uses the current buffer, | |
fe7a0e7d | 678 | regardless of which buffer is displayed in WINDOW. This makes possible to use |
c16c855b GM |
679 | `count-screen-lines' in any buffer, whether or not it is currently displayed |
680 | in some window." | |
681 | (unless beg | |
682 | (setq beg (point-min))) | |
683 | (unless end | |
684 | (setq end (point-max))) | |
685 | (if (= beg end) | |
686 | 0 | |
687 | (save-excursion | |
688 | (save-restriction | |
689 | (widen) | |
690 | (narrow-to-region (min beg end) | |
691 | (if (and (not count-final-newline) | |
692 | (= ?\n (char-before (max beg end)))) | |
693 | (1- (max beg end)) | |
694 | (max beg end))) | |
695 | (goto-char (point-min)) | |
696 | (1+ (vertical-motion (buffer-size) window)))))) | |
697 | ||
65e742bd | 698 | (defun fit-window-to-buffer (&optional window max-height min-height) |
1cce8267 | 699 | "Make WINDOW the right height to display its contents exactly. |
119171dd | 700 | If WINDOW is omitted or nil, it defaults to the selected window. |
65e742bd MB |
701 | If the optional argument MAX-HEIGHT is supplied, it is the maximum height |
702 | the window is allowed to be, defaulting to the frame height. | |
703 | If the optional argument MIN-HEIGHT is supplied, it is the minimum | |
704 | height the window is allowed to be, defaulting to `window-min-height'. | |
705 | ||
706 | The heights in MAX-HEIGHT and MIN-HEIGHT include the mode-line and/or | |
707 | header-line." | |
708 | (interactive) | |
709 | ||
710 | (when (null window) | |
711 | (setq window (selected-window))) | |
3511cde8 MB |
712 | (when (null max-height) |
713 | (setq max-height (frame-height (window-frame window)))) | |
65e742bd | 714 | |
88f0a1eb MB |
715 | (let* ((buf |
716 | ;; Buffer that is displayed in WINDOW | |
717 | (window-buffer window)) | |
718 | (window-height | |
65e742bd MB |
719 | ;; The current height of WINDOW |
720 | (window-height window)) | |
88f0a1eb | 721 | (desired-height |
65e742bd MB |
722 | ;; The height necessary to show the buffer displayed by WINDOW |
723 | ;; (`count-screen-lines' always works on the current buffer). | |
88f0a1eb MB |
724 | (with-current-buffer buf |
725 | (+ (count-screen-lines) | |
b1491763 KH |
726 | ;; If the buffer is empty, (count-screen-lines) is |
727 | ;; zero. But, even in that case, we need one text line | |
728 | ;; for cursor. | |
729 | (if (= (point-min) (point-max)) | |
730 | 1 0) | |
88f0a1eb MB |
731 | ;; For non-minibuffers, count the mode-line, if any |
732 | (if (and (not (window-minibuffer-p window)) | |
733 | mode-line-format) | |
734 | 1 0) | |
735 | ;; Count the header-line, if any | |
736 | (if header-line-format 1 0)))) | |
65e742bd MB |
737 | (delta |
738 | ;; Calculate how much the window height has to change to show | |
88f0a1eb MB |
739 | ;; desired-height lines, constrained by MIN-HEIGHT and MAX-HEIGHT. |
740 | (- (max (min desired-height max-height) | |
65e742bd | 741 | (or min-height window-min-height)) |
825906b8 | 742 | window-height))) |
65e742bd MB |
743 | |
744 | ;; Don't try to redisplay with the cursor at the end | |
745 | ;; on its own line--that would force a scroll and spoil things. | |
88f0a1eb MB |
746 | (when (with-current-buffer buf |
747 | (and (eobp) (bolp) (not (bobp)))) | |
748 | (set-window-point window (1- (window-point window)))) | |
749 | ||
750 | (save-selected-window | |
751 | (select-window window) | |
752 | ||
753 | ;; Adjust WINDOW to the nominally correct size (which may actually | |
754 | ;; be slightly off because of variable height text, etc). | |
755 | (unless (zerop delta) | |
756 | (enlarge-window delta)) | |
757 | ||
758 | ;; Check if the last line is surely fully visible. If not, | |
759 | ;; enlarge the window. | |
760 | (let ((end (with-current-buffer buf | |
761 | (save-excursion | |
762 | (goto-char (point-max)) | |
45450dd5 MB |
763 | (when (and (bolp) (not (bobp))) |
764 | ;; Don't include final newline | |
765 | (backward-char 1)) | |
766 | (when truncate-lines | |
767 | ;; If line-wrapping is turned off, test the | |
768 | ;; beginning of the last line for visibility | |
769 | ;; instead of the end, as the end of the line | |
770 | ;; could be invisible by virtue of extending past | |
771 | ;; the edge of the window. | |
772 | (forward-line 0)) | |
773 | (point))))) | |
88f0a1eb MB |
774 | (set-window-vscroll window 0) |
775 | (while (and (< desired-height max-height) | |
776 | (= desired-height (window-height window)) | |
df0677c3 | 777 | (not (pos-visible-in-window-p end window))) |
88f0a1eb | 778 | (enlarge-window 1) |
edb08287 | 779 | (setq desired-height (1+ desired-height))))))) |
65e742bd | 780 | |
a0900d9f | 781 | (defun shrink-window-if-larger-than-buffer (&optional window) |
d0bee390 | 782 | "Shrink the WINDOW to be as small as possible to display its contents. |
119171dd | 783 | If WINDOW is omitted or nil, it defaults to the selected window. |
30f2e5cc | 784 | Do not shrink to less than `window-min-height' lines. |
d0bee390 | 785 | Do nothing if the buffer contains more lines than the present window height, |
3eb217a0 | 786 | or if some of the window's contents are scrolled out of view, |
fd8529d0 | 787 | or if shrinking this window would also shrink another window, |
18fde850 | 788 | or if the window is the only window of its frame." |
d0bee390 | 789 | (interactive) |
65e742bd MB |
790 | (when (null window) |
791 | (setq window (selected-window))) | |
792 | (let* ((frame (window-frame window)) | |
793 | (mini (frame-parameter frame 'minibuffer)) | |
794 | (edges (window-edges window))) | |
795 | (if (and (not (eq window (frame-root-window frame))) | |
fe7a0e7d | 796 | (window-safely-shrinkable-p) |
65e742bd MB |
797 | (pos-visible-in-window-p (point-min) window) |
798 | (not (eq mini 'only)) | |
799 | (or (not mini) | |
f92c5e7d GM |
800 | (let ((mini-window (minibuffer-window frame))) |
801 | (or (null mini-window) | |
802 | (not (eq frame (window-frame mini-window))) | |
803 | (< (nth 3 edges) | |
804 | (nth 1 (window-edges mini-window))) | |
f1180544 | 805 | (> (nth 1 edges) |
f92c5e7d | 806 | (frame-parameter frame 'menu-bar-lines)))))) |
65e742bd | 807 | (fit-window-to-buffer window (window-height window))))) |
b691df0c RS |
808 | |
809 | (defun kill-buffer-and-window () | |
810 | "Kill the current buffer and delete the selected window." | |
811 | (interactive) | |
0f790c74 | 812 | (let ((window-to-delete (selected-window)) |
f5da083e | 813 | (buffer-to-kill (current-buffer)) |
0f790c74 RS |
814 | (delete-window-hook (lambda () |
815 | (condition-case nil | |
816 | (delete-window) | |
817 | (error nil))))) | |
f5da083e DK |
818 | (unwind-protect |
819 | (progn | |
820 | (add-hook 'kill-buffer-hook delete-window-hook t t) | |
821 | (if (kill-buffer (current-buffer)) | |
822 | ;; If `delete-window' failed before, we rerun it to regenerate | |
823 | ;; the error so it can be seen in the echo area. | |
824 | (when (eq (selected-window) window-to-delete) | |
825 | (delete-window)))) | |
826 | ;; If the buffer is not dead for some reason (probably because | |
827 | ;; of a `quit' signal), remove the hook again. | |
828 | (condition-case nil | |
829 | (with-current-buffer buffer-to-kill | |
830 | (remove-hook 'kill-buffer-hook delete-window-hook t)) | |
831 | (error nil))))) | |
b691df0c | 832 | |
3d2f23d9 RS |
833 | (defun quit-window (&optional kill window) |
834 | "Quit the current buffer. Bury it, and maybe delete the selected frame. | |
18fde850 | 835 | \(The frame is deleted if it contains a dedicated window for the buffer.) |
3d2f23d9 RS |
836 | With a prefix argument, kill the buffer instead. |
837 | ||
838 | Noninteractively, if KILL is non-nil, then kill the current buffer, | |
839 | otherwise bury it. | |
840 | ||
841 | If WINDOW is non-nil, it specifies a window; we delete that window, | |
842 | and the buffer that is killed or buried is the one in that window." | |
843 | (interactive "P") | |
844 | (let ((buffer (window-buffer window)) | |
c81845b6 | 845 | (frame (window-frame (or window (selected-window)))) |
3d2f23d9 RS |
846 | (window-solitary |
847 | (save-selected-window | |
848 | (if window | |
849 | (select-window window)) | |
850 | (one-window-p t))) | |
851 | window-handled) | |
852 | ||
853 | (save-selected-window | |
854 | (if window | |
855 | (select-window window)) | |
eed1360c RS |
856 | (or (window-minibuffer-p) |
857 | (window-dedicated-p (selected-window)) | |
858 | (switch-to-buffer (other-buffer)))) | |
3d2f23d9 RS |
859 | |
860 | ;; Get rid of the frame, if it has just one dedicated window | |
861 | ;; and other visible frames exist. | |
eed1360c | 862 | (and (or (window-minibuffer-p) (window-dedicated-p window)) |
3d2f23d9 RS |
863 | (delq frame (visible-frame-list)) |
864 | window-solitary | |
865 | (if (and (eq default-minibuffer-frame frame) | |
866 | (= 1 (length (minibuffer-frame-list)))) | |
867 | (setq window nil) | |
868 | (delete-frame frame) | |
869 | (setq window-handled t))) | |
870 | ||
871 | ;; Deal with the buffer. | |
872 | (if kill | |
873 | (kill-buffer buffer) | |
874 | (bury-buffer buffer)) | |
875 | ||
876 | ;; Maybe get rid of the window. | |
877 | (and window (not window-handled) (not window-solitary) | |
878 | (delete-window window)))) | |
de8ebf62 SM |
879 | |
880 | (defvar recenter-last-op nil | |
881 | "Indicates the last recenter operation performed. | |
882 | Possible values: `top', `middle', `bottom'.") | |
883 | ||
884 | (defun recenter-top-bottom (&optional arg) | |
885 | "Move current line to window center, top, and bottom, successively. | |
886 | With a prefix argument, this is the same as `recenter': | |
887 | With numeric prefix ARG, move current line to window-line ARG. | |
888 | With plain `C-u', move current line to window center. | |
889 | ||
890 | Otherwise move current line to window center on first call, and to | |
891 | top, middle, or bottom on successive calls. | |
892 | ||
893 | The starting position of the window determines the cycling order: | |
894 | If initially in the top or middle third: top -> middle -> bottom. | |
895 | If initially in the bottom third: bottom -> middle -> top. | |
896 | ||
897 | Top and bottom destinations are actually `scroll-conservatively' lines | |
898 | from true window top and bottom." | |
899 | (interactive "P") | |
900 | (cond | |
901 | (arg (recenter arg)) ; Always respect ARG. | |
902 | ((not (eq this-command last-command)) | |
903 | ;; First time - save mode and recenter. | |
693b2fe2 SM |
904 | (setq recenter-last-op 'middle) |
905 | (recenter)) | |
de8ebf62 SM |
906 | (t ;; repeat: loop through various options. |
907 | (setq recenter-last-op | |
908 | (ecase recenter-last-op | |
909 | (middle (recenter scroll-conservatively) 'top) | |
910 | (top (recenter (1- (- scroll-conservatively))) 'bottom) | |
911 | (bottom (recenter) 'middle)))))) | |
912 | ||
913 | (define-key global-map [?\C-l] 'recenter-top-bottom) | |
612c16f1 CY |
914 | \f |
915 | (defvar mouse-autoselect-window-timer nil | |
916 | "Timer used by delayed window autoselection.") | |
917 | ||
918 | (defvar mouse-autoselect-window-position nil | |
919 | "Last mouse position recorded by delayed window autoselection.") | |
920 | ||
921 | (defvar mouse-autoselect-window-window nil | |
922 | "Last window recorded by delayed window autoselection.") | |
923 | ||
6a6af58e EZ |
924 | (defvar mouse-autoselect-window-state nil |
925 | "When non-nil, special state of delayed window autoselection. | |
926 | Possible values are `suspend' \(suspend autoselection after a menu or | |
927 | scrollbar interaction\) and `select' \(the next invocation of | |
928 | 'handle-select-window' shall select the window immediately\).") | |
612c16f1 CY |
929 | |
930 | (defun mouse-autoselect-window-cancel (&optional force) | |
931 | "Cancel delayed window autoselection. | |
932 | Optional argument FORCE means cancel unconditionally." | |
933 | (unless (and (not force) | |
1398509c MR |
934 | ;; Don't cancel for select-window or select-frame events |
935 | ;; or when the user drags a scroll bar. | |
936 | (or (memq this-command | |
937 | '(handle-select-window handle-switch-frame)) | |
938 | (and (eq this-command 'scroll-bar-toolkit-scroll) | |
939 | (memq (nth 4 (event-end last-input-event)) | |
940 | '(handle end-scroll))))) | |
6a6af58e | 941 | (setq mouse-autoselect-window-state nil) |
612c16f1 CY |
942 | (when (timerp mouse-autoselect-window-timer) |
943 | (cancel-timer mouse-autoselect-window-timer)) | |
944 | (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel))) | |
945 | ||
6a6af58e | 946 | (defun mouse-autoselect-window-start (mouse-position &optional window suspend) |
612c16f1 | 947 | "Start delayed window autoselection. |
6a6af58e EZ |
948 | MOUSE-POSITION is the last position where the mouse was seen as returned |
949 | by `mouse-position'. Optional argument WINDOW non-nil denotes the | |
950 | window where the mouse was seen. Optional argument SUSPEND non-nil | |
951 | means suspend autoselection." | |
952 | ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND. | |
953 | (setq mouse-autoselect-window-position mouse-position) | |
954 | (when window (setq mouse-autoselect-window-window window)) | |
955 | (setq mouse-autoselect-window-state (when suspend 'suspend)) | |
956 | ;; Install timer which runs `mouse-autoselect-window-select' after | |
612c16f1 CY |
957 | ;; `mouse-autoselect-window' seconds. |
958 | (setq mouse-autoselect-window-timer | |
959 | (run-at-time | |
6a6af58e | 960 | (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select))) |
612c16f1 CY |
961 | |
962 | (defun mouse-autoselect-window-select () | |
963 | "Select window with delayed window autoselection. | |
964 | If the mouse position has stabilized in a non-selected window, select | |
4837b516 | 965 | that window. The minibuffer window is selected only if the minibuffer is |
612c16f1 CY |
966 | active. This function is run by `mouse-autoselect-window-timer'." |
967 | (condition-case nil | |
968 | (let* ((mouse-position (mouse-position)) | |
6a6af58e EZ |
969 | (window |
970 | (condition-case nil | |
971 | (window-at (cadr mouse-position) (cddr mouse-position) | |
972 | (car mouse-position)) | |
973 | (error nil)))) | |
612c16f1 | 974 | (cond |
6a6af58e EZ |
975 | ((or (menu-or-popup-active-p) |
976 | (and window | |
977 | (not (coordinates-in-window-p (cdr mouse-position) window)))) | |
978 | ;; A menu / popup dialog is active or the mouse is on the scroll-bar | |
979 | ;; of WINDOW, temporarily suspend delayed autoselection. | |
980 | (mouse-autoselect-window-start mouse-position nil t)) | |
981 | ((eq mouse-autoselect-window-state 'suspend) | |
982 | ;; Delayed autoselection was temporarily suspended, reenable it. | |
983 | (mouse-autoselect-window-start mouse-position)) | |
612c16f1 CY |
984 | ((and window (not (eq window (selected-window))) |
985 | (or (not (numberp mouse-autoselect-window)) | |
986 | (and (> mouse-autoselect-window 0) | |
987 | ;; If `mouse-autoselect-window' is positive, select | |
988 | ;; window if the window is the same as before. | |
989 | (eq window mouse-autoselect-window-window)) | |
4837b516 | 990 | ;; Otherwise select window if the mouse is at the same |
612c16f1 | 991 | ;; position as before. Observe that the first test after |
6a6af58e EZ |
992 | ;; starting autoselection usually fails since the value of |
993 | ;; `mouse-autoselect-window-position' recorded there is the | |
994 | ;; position where the mouse has entered the new window and | |
995 | ;; not necessarily where the mouse has stopped moving. | |
612c16f1 | 996 | (equal mouse-position mouse-autoselect-window-position)) |
4837b516 | 997 | ;; The minibuffer is a candidate window if it's active. |
612c16f1 CY |
998 | (or (not (window-minibuffer-p window)) |
999 | (eq window (active-minibuffer-window)))) | |
6a6af58e EZ |
1000 | ;; Mouse position has stabilized in non-selected window: Cancel |
1001 | ;; delayed autoselection and try to select that window. | |
612c16f1 CY |
1002 | (mouse-autoselect-window-cancel t) |
1003 | ;; Select window where mouse appears unless the selected window is the | |
1004 | ;; minibuffer. Use `unread-command-events' in order to execute pre- | |
1005 | ;; and post-command hooks and trigger idle timers. To avoid delaying | |
6a6af58e | 1006 | ;; autoselection again, set `mouse-autoselect-window-state'." |
612c16f1 | 1007 | (unless (window-minibuffer-p (selected-window)) |
6a6af58e | 1008 | (setq mouse-autoselect-window-state 'select) |
612c16f1 CY |
1009 | (setq unread-command-events |
1010 | (cons (list 'select-window (list window)) | |
1011 | unread-command-events)))) | |
1012 | ((or (and window (eq window (selected-window))) | |
1013 | (not (numberp mouse-autoselect-window)) | |
1014 | (equal mouse-position mouse-autoselect-window-position)) | |
1015 | ;; Mouse position has either stabilized in the selected window or at | |
6a6af58e | 1016 | ;; `mouse-autoselect-window-position': Cancel delayed autoselection. |
612c16f1 CY |
1017 | (mouse-autoselect-window-cancel t)) |
1018 | (t | |
6a6af58e EZ |
1019 | ;; Mouse position has not stabilized yet, resume delayed |
1020 | ;; autoselection. | |
1021 | (mouse-autoselect-window-start mouse-position window)))) | |
612c16f1 | 1022 | (error nil))) |
3d2f23d9 | 1023 | |
aa8d5134 PJ |
1024 | (defun handle-select-window (event) |
1025 | "Handle select-window events." | |
1026 | (interactive "e") | |
1027 | (let ((window (posn-window (event-start event)))) | |
1398509c MR |
1028 | (unless (or (not (window-live-p window)) |
1029 | ;; Don't switch if we're currently in the minibuffer. | |
1030 | ;; This tries to work around problems where the | |
1031 | ;; minibuffer gets unselected unexpectedly, and where | |
1032 | ;; you then have to move your mouse all the way down to | |
1033 | ;; the minibuffer to select it. | |
1034 | (window-minibuffer-p (selected-window)) | |
1035 | ;; Don't switch to minibuffer window unless it's active. | |
1036 | (and (window-minibuffer-p window) | |
1037 | (not (minibuffer-window-active-p window))) | |
1038 | ;; Don't switch when autoselection shall be delayed. | |
1039 | (and (numberp mouse-autoselect-window) | |
1040 | (not (zerop mouse-autoselect-window)) | |
1041 | (not (eq mouse-autoselect-window-state 'select)) | |
1042 | (progn | |
1043 | ;; Cancel any delayed autoselection. | |
1044 | (mouse-autoselect-window-cancel t) | |
1045 | ;; Start delayed autoselection from current mouse position | |
1046 | ;; and window. | |
1047 | (mouse-autoselect-window-start (mouse-position) window) | |
1048 | ;; Executing a command cancels delayed autoselection. | |
1049 | (add-hook | |
1050 | 'pre-command-hook 'mouse-autoselect-window-cancel)))) | |
1051 | (when mouse-autoselect-window | |
6a6af58e EZ |
1052 | ;; Reset state of delayed autoselection. |
1053 | (setq mouse-autoselect-window-state nil) | |
1398509c MR |
1054 | ;; Run `mouse-leave-buffer-hook' when autoselecting window. |
1055 | (run-hooks 'mouse-leave-buffer-hook)) | |
1056 | (select-window window)))) | |
aa8d5134 | 1057 | |
a2535589 | 1058 | (define-key ctl-x-map "2" 'split-window-vertically) |
492878e4 | 1059 | (define-key ctl-x-map "3" 'split-window-horizontally) |
a2535589 JA |
1060 | (define-key ctl-x-map "}" 'enlarge-window-horizontally) |
1061 | (define-key ctl-x-map "{" 'shrink-window-horizontally) | |
a0900d9f ER |
1062 | (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer) |
1063 | (define-key ctl-x-map "+" 'balance-windows) | |
b691df0c | 1064 | (define-key ctl-x-4-map "0" 'kill-buffer-and-window) |
76d7458e | 1065 | |
66c226bf | 1066 | ;; arch-tag: b508dfcc-c353-4c37-89fa-e773fe10cea9 |
fe7a0e7d | 1067 | ;;; window.el ends here |