(ediff-autostore-merges): Doc fix.
[bpt/emacs.git] / src / window.c
CommitLineData
7ab12479
JB
1/* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
4a2f9c6a 3 Copyright (C) 1985,86,87,93,94,95,96,97,1998 Free Software Foundation, Inc.
7ab12479
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
1113d9db 9the Free Software Foundation; either version 2, or (at your option)
7ab12479
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
7ab12479 21
18160b98 22#include <config.h>
7ab12479
JB
23#include "lisp.h"
24#include "buffer.h"
44fa5b1e 25#include "frame.h"
7ab12479
JB
26#include "window.h"
27#include "commands.h"
28#include "indent.h"
29#include "termchar.h"
30#include "disptab.h"
f8026fd8 31#include "keyboard.h"
d2b35234 32#include "blockinput.h"
7ab12479 33
806b4d9b 34Lisp_Object Qwindowp, Qwindow_live_p;
7ab12479 35
5e14b1fc 36static struct window *decode_window P_ ((Lisp_Object));
7ab12479
JB
37
38/* This is the window in which the terminal's cursor should
39 be left when nothing is being done with it. This must
40 always be a leaf window, and its buffer is selected by
41 the top level editing loop at the end of each command.
42
43 This value is always the same as
44fa5b1e 44 FRAME_SELECTED_WINDOW (selected_frame). */
7ab12479
JB
45
46Lisp_Object selected_window;
47
44fa5b1e 48/* The minibuffer window of the selected frame.
7ab12479
JB
49 Note that you cannot test for minibufferness of an arbitrary window
50 by comparing against this; but you can test for minibufferness of
51 the selected window. */
52Lisp_Object minibuf_window;
53
54/* Non-nil means it is the window for C-M-v to scroll
55 when the minibuffer is selected. */
56Lisp_Object Vminibuf_scroll_window;
57
58/* Non-nil means this is the buffer whose window C-M-v should scroll. */
59Lisp_Object Vother_window_scroll_buffer;
60
7ab12479
JB
61/* Non-nil means it's function to call to display temp buffers. */
62Lisp_Object Vtemp_buffer_show_function;
63
64/* If a window gets smaller than either of these, it is removed. */
65int window_min_height;
66int window_min_width;
67
68/* Nonzero implies Fdisplay_buffer should create windows. */
69int pop_up_windows;
70
44fa5b1e
JB
71/* Nonzero implies make new frames for Fdisplay_buffer. */
72int pop_up_frames;
7ab12479
JB
73
74/* Non-nil means use this function instead of default */
44fa5b1e 75Lisp_Object Vpop_up_frame_function;
7ab12479
JB
76
77/* Function to call to handle Fdisplay_buffer. */
78Lisp_Object Vdisplay_buffer_function;
79
a90712c2
RS
80/* List of buffer *names* for buffers that should have their own frames. */
81Lisp_Object Vspecial_display_buffer_names;
82
83/* List of regexps for buffer names that should have their own frames. */
84Lisp_Object Vspecial_display_regexps;
85
86/* Function to pop up a special frame. */
87Lisp_Object Vspecial_display_function;
88
855d8627
RS
89/* List of buffer *names* for buffers to appear in selected window. */
90Lisp_Object Vsame_window_buffer_names;
91
92/* List of regexps for buffer names to appear in selected window. */
93Lisp_Object Vsame_window_regexps;
94
a58ec57d
RS
95/* Hook run at end of temp_output_buffer_show. */
96Lisp_Object Qtemp_buffer_show_hook;
97
37962e60 98/* Fdisplay_buffer always splits the largest window
7ab12479
JB
99 if that window is more than this high. */
100int split_height_threshold;
101
102/* Number of lines of continuity in scrolling by screenfuls. */
103int next_screen_context_lines;
104
105/* Incremented for each window created. */
106static int sequence_number;
107
5b03d3c0
RS
108/* Nonzero after init_window_once has finished. */
109static int window_initialized;
110
543f5fb1
RS
111/* Hook to run when window config changes. */
112Lisp_Object Qwindow_configuration_change_hook;
113Lisp_Object Vwindow_configuration_change_hook;
114
9317a85d
RS
115/* Nonzero means scroll commands try to put point
116 at the same screen height as previously. */
117static int scroll_preserve_screen_position;
118
91a78190 119/* Nonzero means we can split a frame even if it is "unsplittable". */
eddd51c2 120static int inhibit_frame_unsplittable;
7d601aaa 121
7ab12479 122#define min(a, b) ((a) < (b) ? (a) : (b))
dba06815 123
9317a85d
RS
124extern int scroll_margin;
125
dba06815 126extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
7ab12479
JB
127\f
128DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
413430c5
EN
129 "Returns t if OBJECT is a window.")
130 (object)
131 Lisp_Object object;
7ab12479 132{
413430c5 133 return WINDOWP (object) ? Qt : Qnil;
7ab12479
JB
134}
135
806b4d9b 136DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
413430c5
EN
137 "Returns t if OBJECT is a window which is currently visible.")
138 (object)
139 Lisp_Object object;
605be8af 140{
413430c5 141 return (WINDOWP (object) && ! NILP (XWINDOW (object)->buffer) ? Qt : Qnil);
605be8af
JB
142}
143
7ab12479
JB
144Lisp_Object
145make_window ()
146{
cffec418 147 Lisp_Object val;
7ab12479 148 register struct window *p;
cffec418
KH
149 register struct Lisp_Vector *vec;
150 int i;
151
152 vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct window));
153 for (i = 0; i < VECSIZE (struct window); i++)
154 vec->contents[i] = Qnil;
155 vec->size = VECSIZE (struct window);
156 p = (struct window *)vec;
d834a2e9
KH
157 XSETFASTINT (p->sequence_number, ++sequence_number);
158 XSETFASTINT (p->left, 0);
159 XSETFASTINT (p->top, 0);
160 XSETFASTINT (p->height, 0);
161 XSETFASTINT (p->width, 0);
162 XSETFASTINT (p->hscroll, 0);
163 XSETFASTINT (p->last_point_x, 0);
164 XSETFASTINT (p->last_point_y, 0);
7ab12479
JB
165 p->start = Fmake_marker ();
166 p->pointm = Fmake_marker ();
d834a2e9 167 XSETFASTINT (p->use_time, 0);
44fa5b1e 168 p->frame = Qnil;
7ab12479
JB
169 p->display_table = Qnil;
170 p->dedicated = Qnil;
cffec418 171 XSETWINDOW (val, p);
7ab12479
JB
172 return val;
173}
174
175DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
176 "Return the window that the cursor now appears in and commands apply to.")
177 ()
178{
179 return selected_window;
180}
181
83762ba4
JB
182DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
183 "Return the window used now for minibuffers.\n\
184If the optional argument FRAME is specified, return the minibuffer window\n\
185used by that frame.")
186 (frame)
187 Lisp_Object frame;
7ab12479 188{
83762ba4 189 if (NILP (frame))
74112613 190 XSETFRAME (frame, selected_frame);
83762ba4
JB
191 else
192 CHECK_LIVE_FRAME (frame, 0);
83762ba4
JB
193
194 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
7ab12479
JB
195}
196
605be8af 197DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
7ab12479
JB
198 "Returns non-nil if WINDOW is a minibuffer window.")
199 (window)
200 Lisp_Object window;
201{
202 struct window *w = decode_window (window);
203 return (MINI_WINDOW_P (w) ? Qt : Qnil);
204}
205
206DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
207 Spos_visible_in_window_p, 0, 2, 0,
44fa5b1e 208 "Return t if position POS is currently on the frame in WINDOW.\n\
7ab12479
JB
209Returns nil if that position is scrolled vertically out of view.\n\
210POS defaults to point; WINDOW, to the selected window.")
211 (pos, window)
212 Lisp_Object pos, window;
213{
214 register struct window *w;
215 register int top;
216 register int height;
217 register int posint;
218 register struct buffer *buf;
219 struct position posval;
afdb2485 220 int hscroll;
7ab12479 221
265a9e55 222 if (NILP (pos))
5ce7b543 223 posint = PT;
7ab12479
JB
224 else
225 {
226 CHECK_NUMBER_COERCE_MARKER (pos, 0);
227 posint = XINT (pos);
228 }
229
605be8af 230 w = decode_window (window);
7ab12479 231 top = marker_position (w->start);
afdb2485 232 hscroll = XINT (w->hscroll);
7ab12479
JB
233
234 if (posint < top)
235 return Qnil;
236
237 height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
238
239 buf = XBUFFER (w->buffer);
3cd21523
RS
240 if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
241 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf))
7ab12479 242 {
44fa5b1e 243 /* If frame is up to date,
7ab12479
JB
244 use the info recorded about how much text fit on it. */
245 if (posint < BUF_Z (buf) - XFASTINT (w->window_end_pos)
246 || (XFASTINT (w->window_end_vpos) < height))
247 return Qt;
248 return Qnil;
249 }
250 else
251 {
33eb4ede 252 if (posint > BUF_ZV (buf))
7ab12479
JB
253 return Qnil;
254
61b5322b
RS
255 /* w->start can be out of range. If it is, do something reasonable. */
256 if (top < BUF_BEGV (buf) || top > BUF_ZV (buf))
257 return Qnil;
258
7ab12479 259 /* If that info is not correct, calculate afresh */
5a255667
KH
260 /* BUG FIX for the 7th arg (TOHPOS).
261
262 '0' is harmless, however, ' - (1 << (BITS_PER_SHORT - 1))' is
263 more appropriate here. In case of HSCROLL > 0, this can avoid
264 needless calculation done until (HPOS == 0).
265
266 We want to determine if the position POSINT is in HEIGHT or
267 not. We don't have to do calculation until (HPOS == 0). We
268 can stop it when VPOS goes beyond HEIGHT. */
4efffd80 269 posval = *compute_motion (top, 0, (hscroll ? 1 - hscroll : 0), 0,
5a255667 270 posint, height, - (1 << (BITS_PER_SHORT - 1)),
535e0b8e 271 window_internal_width (w) - 1,
e37f06d7 272 hscroll, 0, w);
7ab12479
JB
273
274 return posval.vpos < height ? Qt : Qnil;
275 }
276}
277\f
278static struct window *
279decode_window (window)
280 register Lisp_Object window;
281{
265a9e55 282 if (NILP (window))
7ab12479
JB
283 return XWINDOW (selected_window);
284
605be8af 285 CHECK_LIVE_WINDOW (window, 0);
7ab12479
JB
286 return XWINDOW (window);
287}
288
289DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
290 "Return the buffer that WINDOW is displaying.")
291 (window)
292 Lisp_Object window;
293{
294 return decode_window (window)->buffer;
295}
296
297DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
298 "Return the number of lines in WINDOW (including its mode line).")
299 (window)
300 Lisp_Object window;
301{
302 return decode_window (window)->height;
303}
304
305DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
3b5908ef
RS
306 "Return the number of display columns in WINDOW.\n\
307This is the width that is usable columns available for text in WINDOW.\n\
308If you want to find out how many columns WINDOW takes up,\n\
309use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
7ab12479
JB
310 (window)
311 Lisp_Object window;
312{
ee61d94e 313 return make_number (window_internal_width (decode_window (window)));
7ab12479
JB
314}
315
316DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
317 "Return the number of columns by which WINDOW is scrolled from left margin.")
318 (window)
319 Lisp_Object window;
320{
321 return decode_window (window)->hscroll;
322}
323
324DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
325 "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
326NCOL should be zero or positive.")
327 (window, ncol)
328 register Lisp_Object window, ncol;
329{
330 register struct window *w;
331
332 CHECK_NUMBER (ncol, 1);
d834a2e9 333 if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
7ab12479 334 w = decode_window (window);
7f4161e0 335 if (XINT (w->hscroll) != XINT (ncol))
1479ef51 336 XBUFFER (w->buffer)->clip_changed = 1; /* Prevent redisplay shortcuts */
7ab12479
JB
337 w->hscroll = ncol;
338 return ncol;
339}
340
190eb263
RS
341DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
342 Swindow_redisplay_end_trigger, 0, 1, 0,
343 "Return WINDOW's redisplay end trigger value.\n\
344See `set-window-redisplay-end-trigger' for more information.")
345 (window)
346 Lisp_Object window;
347{
348 return decode_window (window)->redisplay_end_trigger;
349}
350
351DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
352 Sset_window_redisplay_end_trigger, 2, 2, 0,
353 "Set WINDOW's redisplay end trigger value to VALUE.\n\
354VALUE should be a buffer position (typically a marker) or nil.\n\
76854cf2
RS
355If it is a buffer position, then if redisplay in WINDOW reaches a position\n\
356beyond VALUE, the functions in `redisplay-end-trigger-functions' are called\n\
357with two arguments: WINDOW, and the end trigger value.\n\
358Afterwards the end-trigger value is reset to nil.")
190eb263
RS
359 (window, value)
360 register Lisp_Object window, value;
361{
362 register struct window *w;
363
364 w = decode_window (window);
365 w->redisplay_end_trigger = value;
366 return value;
367}
368
7ab12479
JB
369DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
370 "Return a list of the edge coordinates of WINDOW.\n\
44fa5b1e 371\(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.\n\
7ab12479
JB
372RIGHT is one more than the rightmost column used by WINDOW,\n\
373and BOTTOM is one more than the bottommost row used by WINDOW\n\
374 and its mode-line.")
375 (window)
376 Lisp_Object window;
377{
378 register struct window *w = decode_window (window);
379
380 return Fcons (w->left, Fcons (w->top,
111e5992 381 Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
7ab12479
JB
382 Fcons (make_number (XFASTINT (w->top)
383 + XFASTINT (w->height)),
384 Qnil))));
385}
386
d5783c40
JB
387/* Test if the character at column *x, row *y is within window *w.
388 If it is not, return 0;
389 if it is in the window's text area,
390 set *x and *y to its location relative to the upper left corner
391 of the window, and
392 return 1;
393 if it is on the window's modeline, return 2;
394 if it is on the border between the window and its right sibling,
395 return 3. */
396static int
397coordinates_in_window (w, x, y)
398 register struct window *w;
399 register int *x, *y;
400{
401 register int left = XINT (w->left);
111e5992
RS
402 register int right_edge = WINDOW_RIGHT_EDGE (w);
403 register int left_margin = WINDOW_LEFT_MARGIN (w);
404 register int right_margin = WINDOW_RIGHT_MARGIN (w);
d5783c40
JB
405 register int window_height = XINT (w->height);
406 register int top = XFASTINT (w->top);
111e5992
RS
407
408 if ( *x < left || *x >= right_edge
d5783c40
JB
409 || *y < top || *y >= top + window_height)
410 return 0;
411
111e5992
RS
412 if (left_margin != left && *x < left_margin && *x >= left)
413 return 3;
414
415 if (right_margin != right_edge && *x >= right_margin && *x < right_edge)
416 return 3;
417
d5783c40
JB
418 /* Is the character is the mode line? */
419 if (*y == top + window_height - 1
05c2896a 420 && ! MINI_WINDOW_P (w))
d5783c40 421 return 2;
37962e60 422
111e5992 423 *x -= WINDOW_LEFT_MARGIN (w);
d5783c40
JB
424 *y -= top;
425 return 1;
426}
427
428DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
429 Scoordinates_in_window_p, 2, 2, 0,
430 "Return non-nil if COORDINATES are in WINDOW.\n\
1113d9db 431COORDINATES is a cons of the form (X . Y), X and Y being distances\n\
44fa5b1e 432measured in characters from the upper-left corner of the frame.\n\
1113d9db 433(0 . 0) denotes the character in the upper left corner of the\n\
44fa5b1e 434frame.\n\
d5783c40
JB
435If COORDINATES are in the text portion of WINDOW,\n\
436 the coordinates relative to the window are returned.\n\
e5d77022 437If they are in the mode line of WINDOW, `mode-line' is returned.\n\
d5783c40 438If they are on the border between WINDOW and its right sibling,\n\
e5d77022 439 `vertical-line' is returned.")
d5783c40
JB
440 (coordinates, window)
441 register Lisp_Object coordinates, window;
442{
443 int x, y;
444
605be8af 445 CHECK_LIVE_WINDOW (window, 0);
d5783c40
JB
446 CHECK_CONS (coordinates, 1);
447 x = XINT (Fcar (coordinates));
448 y = XINT (Fcdr (coordinates));
449
450 switch (coordinates_in_window (XWINDOW (window), &x, &y))
451 {
452 case 0: /* NOT in window at all. */
453 return Qnil;
454
455 case 1: /* In text part of window. */
34c0dab2 456 return Fcons (make_number (x), make_number (y));
d5783c40
JB
457
458 case 2: /* In mode line of window. */
459 return Qmode_line;
37962e60 460
d5783c40 461 case 3: /* On right border of window. */
e5d77022 462 return Qvertical_line;
d5783c40
JB
463
464 default:
465 abort ();
466 }
467}
468
7ab12479 469/* Find the window containing column x, row y, and return it as a
d5783c40
JB
470 Lisp_Object. If x, y is on the window's modeline, set *part
471 to 1; if it is on the separating line between the window and its
472 right sibling, set it to 2; otherwise set it to 0. If there is no
473 window under x, y return nil and leave *part unmodified. */
7ab12479 474Lisp_Object
44fa5b1e
JB
475window_from_coordinates (frame, x, y, part)
476 FRAME_PTR frame;
7ab12479 477 int x, y;
d5783c40 478 int *part;
7ab12479
JB
479{
480 register Lisp_Object tem, first;
481
44fa5b1e 482 tem = first = FRAME_SELECTED_WINDOW (frame);
7ab12479 483
d5783c40 484 do
7ab12479
JB
485 {
486 int found = coordinates_in_window (XWINDOW (tem), &x, &y);
487
488 if (found)
489 {
d5783c40 490 *part = found - 1;
7ab12479
JB
491 return tem;
492 }
493
d5783c40 494 tem = Fnext_window (tem, Qt, Qlambda);
7ab12479 495 }
d5783c40 496 while (! EQ (tem, first));
37962e60 497
d5783c40 498 return Qnil;
7ab12479
JB
499}
500
ab17c3f2 501DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
b0c33a94 502 "Return window containing coordinates X and Y on FRAME.\n\
44fa5b1e
JB
503If omitted, FRAME defaults to the currently selected frame.\n\
504The top left corner of the frame is considered to be row 0,\n\
95605e15 505column 0.")
b0c33a94
RS
506 (x, y, frame)
507 Lisp_Object x, y, frame;
7ab12479
JB
508{
509 int part;
510
44fa5b1e 511 if (NILP (frame))
74112613 512 XSETFRAME (frame, selected_frame);
d5783c40 513 else
44fa5b1e 514 CHECK_LIVE_FRAME (frame, 2);
b0c33a94
RS
515 CHECK_NUMBER (x, 0);
516 CHECK_NUMBER (y, 1);
7ab12479 517
44fa5b1e 518 return window_from_coordinates (XFRAME (frame),
b0c33a94 519 XINT (x), XINT (y),
7ab12479
JB
520 &part);
521}
522
523DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
524 "Return current value of point in WINDOW.\n\
525For a nonselected window, this is the value point would have\n\
526if that window were selected.\n\
527\n\
528Note that, when WINDOW is the selected window and its buffer\n\
529is also currently selected, the value returned is the same as (point).\n\
530It would be more strictly correct to return the `top-level' value\n\
531of point, outside of any save-excursion forms.\n\
532But that is hard to define.")
533 (window)
534 Lisp_Object window;
535{
536 register struct window *w = decode_window (window);
537
538 if (w == XWINDOW (selected_window)
539 && current_buffer == XBUFFER (w->buffer))
540 return Fpoint ();
541 return Fmarker_position (w->pointm);
542}
543
544DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
0fea6c40
RS
545 "Return position at which display currently starts in WINDOW.\n\
546This is updated by redisplay or by calling `set-window-start'.")
7ab12479
JB
547 (window)
548 Lisp_Object window;
549{
550 return Fmarker_position (decode_window (window)->start);
551}
552
8646118f
RS
553/* This is text temporarily removed from the doc string below.
554
7250968e
RS
555This function returns nil if the position is not currently known.\n\
556That happens when redisplay is preempted and doesn't finish.\n\
557If in that case you want to compute where the end of the window would\n\
558have been if redisplay had finished, do this:\n\
559 (save-excursion\n\
560 (goto-char (window-start window))\n\
561 (vertical-motion (1- (window-height window)) window)\n\
8646118f
RS
562 (point))") */
563
478292ed 564DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
8646118f
RS
565 "Return position at which display currently ends in WINDOW.\n\
566This is updated by redisplay, when it runs to completion.\n\
567Simply changing the buffer text or setting `window-start'\n\
478292ed
RS
568does not update this value.\n\
569If UP-TO-DATE is non-nil, compute the up-to-date position\n\
570if it isn't already recorded.")
571 (window, update)
572 Lisp_Object window, update;
7ab12479
JB
573{
574 Lisp_Object value;
575 struct window *w = decode_window (window);
5a41ab94
RS
576 Lisp_Object buf;
577
578 buf = w->buffer;
579 CHECK_BUFFER (buf, 0);
580
8646118f 581#if 0 /* This change broke some things. We should make it later. */
7250968e
RS
582 /* If we don't know the end position, return nil.
583 The user can compute it with vertical-motion if he wants to.
584 It would be nicer to do it automatically,
585 but that's so slow that it would probably bother people. */
586 if (NILP (w->window_end_valid))
587 return Qnil;
8646118f 588#endif
7250968e 589
478292ed
RS
590 if (! NILP (update)
591 && ! (! NILP (w->window_end_valid)
592 && XFASTINT (w->last_modified) >= MODIFF))
593 {
594 int opoint = PT, opoint_byte = PT_BYTE;
595 TEMP_SET_PT_BOTH (XMARKER (w->start)->charpos,
596 XMARKER (w->start)->bytepos);
597 Fvertical_motion (make_number (window_internal_height (w)), Qnil);
598 XSETINT (value, PT);
599 TEMP_SET_PT_BOTH (opoint, opoint_byte);
600 }
601 else
602 XSETINT (value,
603 BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
7ab12479
JB
604
605 return value;
606}
607
608DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
609 "Make point value in WINDOW be at position POS in WINDOW's buffer.")
610 (window, pos)
611 Lisp_Object window, pos;
612{
613 register struct window *w = decode_window (window);
614
615 CHECK_NUMBER_COERCE_MARKER (pos, 1);
616 if (w == XWINDOW (selected_window))
617 Fgoto_char (pos);
618 else
619 set_marker_restricted (w->pointm, pos, w->buffer);
620
621 return pos;
622}
623
624DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
625 "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
626Optional third arg NOFORCE non-nil inhibits next redisplay\n\
627from overriding motion of point in order to display at this exact start.")
628 (window, pos, noforce)
629 Lisp_Object window, pos, noforce;
630{
631 register struct window *w = decode_window (window);
632
633 CHECK_NUMBER_COERCE_MARKER (pos, 1);
634 set_marker_restricted (w->start, pos, w->buffer);
635 /* this is not right, but much easier than doing what is right. */
636 w->start_at_line_beg = Qnil;
265a9e55 637 if (NILP (noforce))
7ab12479
JB
638 w->force_start = Qt;
639 w->update_mode_line = Qt;
d834a2e9 640 XSETFASTINT (w->last_modified, 0);
3cd21523 641 XSETFASTINT (w->last_overlay_modified, 0);
62c07cc7
JB
642 if (!EQ (window, selected_window))
643 windows_or_buffers_changed++;
7ab12479
JB
644 return pos;
645}
646
647DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
648 1, 1, 0,
649 "Return WINDOW's dedicated object, usually t or nil.\n\
1f18c48f 650See also `set-window-dedicated-p'.")
7ab12479
JB
651 (window)
652 Lisp_Object window;
653{
654 return decode_window (window)->dedicated;
655}
656
d207b766
RS
657DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
658 Sset_window_dedicated_p, 2, 2, 0,
659 "Control whether WINDOW is dedicated to the buffer it displays.\n\
660If it is dedicated, Emacs will not automatically change\n\
661which buffer appears in it.\n\
662The second argument is the new value for the dedication flag;\n\
663non-nil means yes.")
7ab12479
JB
664 (window, arg)
665 Lisp_Object window, arg;
666{
667 register struct window *w = decode_window (window);
668
265a9e55 669 if (NILP (arg))
7ab12479
JB
670 w->dedicated = Qnil;
671 else
d207b766 672 w->dedicated = Qt;
7ab12479
JB
673
674 return w->dedicated;
675}
676
677DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
678 0, 1, 0,
679 "Return the display-table that WINDOW is using.")
680 (window)
681 Lisp_Object window;
682{
683 return decode_window (window)->display_table;
684}
685
686/* Get the display table for use currently on window W.
687 This is either W's display table or W's buffer's display table.
688 Ignore the specified tables if they are not valid;
689 if no valid table is specified, return 0. */
690
319315f1 691struct Lisp_Char_Table *
7ab12479
JB
692window_display_table (w)
693 struct window *w;
694{
695 Lisp_Object tem;
696 tem = w->display_table;
319315f1
RS
697 if (DISP_TABLE_P (tem))
698 return XCHAR_TABLE (tem);
171d003c
RS
699 if (NILP (w->buffer))
700 return 0;
701
7ab12479 702 tem = XBUFFER (w->buffer)->display_table;
319315f1
RS
703 if (DISP_TABLE_P (tem))
704 return XCHAR_TABLE (tem);
7ab12479 705 tem = Vstandard_display_table;
319315f1
RS
706 if (DISP_TABLE_P (tem))
707 return XCHAR_TABLE (tem);
7ab12479
JB
708 return 0;
709}
710
3a2712f9 711DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
7ab12479
JB
712 "Set WINDOW's display-table to TABLE.")
713 (window, table)
714 register Lisp_Object window, table;
715{
716 register struct window *w;
717 register Lisp_Object z; /* Return value. */
718
719 w = decode_window (window);
720 w->display_table = table;
721 return table;
722}
723\f
724/* Record info on buffer window w is displaying
725 when it is about to cease to display that buffer. */
726static
727unshow_buffer (w)
728 register struct window *w;
729{
12cae7c0 730 Lisp_Object buf;
b73ea88e 731 struct buffer *b;
7ab12479 732
12cae7c0 733 buf = w->buffer;
b73ea88e
RS
734 b = XBUFFER (buf);
735 if (b != XMARKER (w->pointm)->buffer)
7ab12479
JB
736 abort ();
737
b73ea88e
RS
738 if (w == XWINDOW (b->last_selected_window))
739 b->last_selected_window = Qnil;
86e48436 740
573f41ab 741#if 0
7ab12479
JB
742 if (w == XWINDOW (selected_window)
743 || ! EQ (buf, XWINDOW (selected_window)->buffer))
744 /* Do this except when the selected window's buffer
745 is being removed from some other window. */
573f41ab
RS
746#endif
747 /* last_window_start records the start position that this buffer
748 had in the last window to be disconnected from it.
749 Now that this statement is unconditional,
750 it is possible for the buffer to be displayed in the
751 selected window, while last_window_start reflects another
752 window which was recently showing the same buffer.
753 Some people might say that might be a good thing. Let's see. */
b73ea88e 754 b->last_window_start = marker_position (w->start);
7ab12479
JB
755
756 /* Point in the selected window's buffer
757 is actually stored in that buffer, and the window's pointm isn't used.
758 So don't clobber point in that buffer. */
759 if (! EQ (buf, XWINDOW (selected_window)->buffer))
b73ea88e
RS
760 temp_set_point_both (b,
761 clip_to_bounds (BUF_BEGV (b),
762 XMARKER (w->pointm)->charpos,
763 BUF_ZV (b)),
764 clip_to_bounds (BUF_BEGV_BYTE (b),
765 marker_byte_position (w->pointm),
766 BUF_ZV_BYTE (b)));
7ab12479
JB
767}
768
769/* Put replacement into the window structure in place of old. */
770static
771replace_window (old, replacement)
772 Lisp_Object old, replacement;
773{
774 register Lisp_Object tem;
775 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
776
44fa5b1e
JB
777 /* If OLD is its frame's root_window, then replacement is the new
778 root_window for that frame. */
7ab12479 779
7f4161e0 780 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
44fa5b1e 781 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
7ab12479
JB
782
783 p->left = o->left;
784 p->top = o->top;
785 p->width = o->width;
786 p->height = o->height;
787
788 p->next = tem = o->next;
265a9e55 789 if (!NILP (tem))
7ab12479
JB
790 XWINDOW (tem)->prev = replacement;
791
792 p->prev = tem = o->prev;
265a9e55 793 if (!NILP (tem))
7ab12479
JB
794 XWINDOW (tem)->next = replacement;
795
796 p->parent = tem = o->parent;
265a9e55 797 if (!NILP (tem))
7ab12479
JB
798 {
799 if (EQ (XWINDOW (tem)->vchild, old))
800 XWINDOW (tem)->vchild = replacement;
801 if (EQ (XWINDOW (tem)->hchild, old))
802 XWINDOW (tem)->hchild = replacement;
803 }
804
805/*** Here, if replacement is a vertical combination
806and so is its new parent, we should make replacement's
807children be children of that parent instead. ***/
808}
809
810DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
811 "Remove WINDOW from the display. Default is selected window.")
812 (window)
813 register Lisp_Object window;
543f5fb1
RS
814{
815 delete_window (window);
816
817 if (! NILP (Vwindow_configuration_change_hook)
818 && ! NILP (Vrun_hooks))
819 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
820
821 return Qnil;
822}
823
5e14b1fc 824void
543f5fb1
RS
825delete_window (window)
826 register Lisp_Object window;
7ab12479
JB
827{
828 register Lisp_Object tem, parent, sib;
829 register struct window *p;
830 register struct window *par;
831
605be8af
JB
832 /* Because this function is called by other C code on non-leaf
833 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
834 so we can't decode_window here. */
265a9e55 835 if (NILP (window))
7ab12479
JB
836 window = selected_window;
837 else
838 CHECK_WINDOW (window, 0);
7ab12479 839 p = XWINDOW (window);
605be8af
JB
840
841 /* It's okay to delete an already-deleted window. */
842 if (NILP (p->buffer)
843 && NILP (p->hchild)
844 && NILP (p->vchild))
296b535c 845 return;
605be8af 846
7ab12479 847 parent = p->parent;
265a9e55 848 if (NILP (parent))
7ab12479
JB
849 error ("Attempt to delete minibuffer or sole ordinary window");
850 par = XWINDOW (parent);
851
852 windows_or_buffers_changed++;
29aeee73 853 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (p))) = 1;
7ab12479 854
605be8af
JB
855 /* Are we trying to delete any frame's selected window? */
856 {
0def0403 857 Lisp_Object frame, pwindow;
605be8af 858
0def0403
RS
859 /* See if the frame's selected window is either WINDOW
860 or any subwindow of it, by finding all that window's parents
861 and comparing each one with WINDOW. */
862 frame = WINDOW_FRAME (XWINDOW (window));
863 pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
864
865 while (!NILP (pwindow))
866 {
867 if (EQ (window, pwindow))
868 break;
869 pwindow = XWINDOW (pwindow)->parent;
870 }
871
872 if (EQ (window, pwindow))
605be8af 873 {
89bca612
RS
874 Lisp_Object alternative;
875 alternative = Fnext_window (window, Qlambda, Qnil);
605be8af
JB
876
877 /* If we're about to delete the selected window on the
878 selected frame, then we should use Fselect_window to select
879 the new window. On the other hand, if we're about to
880 delete the selected window on any other frame, we shouldn't do
881 anything but set the frame's selected_window slot. */
882 if (EQ (window, selected_window))
883 Fselect_window (alternative);
884 else
0def0403 885 FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
605be8af
JB
886 }
887 }
7ab12479
JB
888
889 tem = p->buffer;
890 /* tem is null for dummy parent windows
891 (which have inferiors but not any contents themselves) */
265a9e55 892 if (!NILP (tem))
7ab12479
JB
893 {
894 unshow_buffer (p);
895 unchain_marker (p->pointm);
896 unchain_marker (p->start);
7ab12479
JB
897 }
898
899 tem = p->next;
265a9e55 900 if (!NILP (tem))
7ab12479
JB
901 XWINDOW (tem)->prev = p->prev;
902
903 tem = p->prev;
265a9e55 904 if (!NILP (tem))
7ab12479
JB
905 XWINDOW (tem)->next = p->next;
906
907 if (EQ (window, par->hchild))
908 par->hchild = p->next;
909 if (EQ (window, par->vchild))
910 par->vchild = p->next;
911
912 /* Find one of our siblings to give our space to. */
913 sib = p->prev;
265a9e55 914 if (NILP (sib))
7ab12479
JB
915 {
916 /* If p gives its space to its next sibling, that sibling needs
917 to have its top/left side pulled back to where p's is.
918 set_window_{height,width} will re-position the sibling's
919 children. */
920 sib = p->next;
7f4161e0
JB
921 XWINDOW (sib)->top = p->top;
922 XWINDOW (sib)->left = p->left;
7ab12479
JB
923 }
924
925 /* Stretch that sibling. */
265a9e55 926 if (!NILP (par->vchild))
7ab12479
JB
927 set_window_height (sib,
928 XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
929 1);
265a9e55 930 if (!NILP (par->hchild))
7ab12479
JB
931 set_window_width (sib,
932 XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
933 1);
934
935 /* If parent now has only one child,
936 put the child into the parent's place. */
7ab12479 937 tem = par->hchild;
265a9e55 938 if (NILP (tem))
7ab12479 939 tem = par->vchild;
265a9e55 940 if (NILP (XWINDOW (tem)->next))
7ab12479 941 replace_window (parent, tem);
605be8af
JB
942
943 /* Since we may be deleting combination windows, we must make sure that
944 not only p but all its children have been marked as deleted. */
945 if (! NILP (p->hchild))
946 delete_all_subwindows (XWINDOW (p->hchild));
947 else if (! NILP (p->vchild))
948 delete_all_subwindows (XWINDOW (p->vchild));
949
950 /* Mark this window as deleted. */
951 p->buffer = p->hchild = p->vchild = Qnil;
7ab12479
JB
952}
953\f
7ab12479 954
44fa5b1e 955extern Lisp_Object next_frame (), prev_frame ();
7ab12479 956
26f6279d
JB
957/* This comment supplies the doc string for `next-window',
958 for make-docfile to see. We cannot put this in the real DEFUN
959 due to limits in the Unix cpp.
960
961DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
7ab12479 962 "Return next window after WINDOW in canonical ordering of windows.\n\
d5783c40
JB
963If omitted, WINDOW defaults to the selected window.\n\
964\n\
965Optional second arg MINIBUF t means count the minibuffer window even\n\
966if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
967it is active. MINIBUF neither t nor nil means not to count the\n\
968minibuffer even if it is active.\n\
969\n\
44fa5b1e
JB
970Several frames may share a single minibuffer; if the minibuffer\n\
971counts, all windows on all frames that share that minibuffer count\n\
ed160f1f 972too. Therefore, `next-window' can be used to iterate through the\n\
44fa5b1e
JB
973set of windows even when the minibuffer is on another frame. If the\n\
974minibuffer does not count, only windows from WINDOW's frame count.\n\
d5783c40 975\n\
44fa5b1e
JB
976Optional third arg ALL-FRAMES t means include windows on all frames.\n\
977ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
89bca612 978above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
f812f9c6 979ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1f4c5d09 980If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
89bca612 981Anything else means restrict to WINDOW's frame.\n\
dbc4e1c1
JB
982\n\
983If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
984`next-window' to iterate through the entire cycle of acceptable\n\
985windows, eventually ending up back at the window you started with.\n\
986`previous-window' traverses the same cycle, in the reverse order.")
26f6279d
JB
987 (window, minibuf, all_frames) */
988
989DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
990 0)
44fa5b1e
JB
991 (window, minibuf, all_frames)
992 register Lisp_Object window, minibuf, all_frames;
7ab12479
JB
993{
994 register Lisp_Object tem;
d5783c40 995 Lisp_Object start_window;
7ab12479 996
265a9e55 997 if (NILP (window))
7ab12479
JB
998 window = selected_window;
999 else
605be8af 1000 CHECK_LIVE_WINDOW (window, 0);
7ab12479 1001
d5783c40
JB
1002 start_window = window;
1003
1004 /* minibuf == nil may or may not include minibuffers.
1005 Decide if it does. */
265a9e55 1006 if (NILP (minibuf))
fbdc1545
RS
1007 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1008 else if (! EQ (minibuf, Qt))
1009 minibuf = Qlambda;
1010 /* Now minibuf can be t => count all minibuffer windows,
1011 lambda => count none of them,
1012 or a specific minibuffer window (the active one) to count. */
d5783c40 1013
1bb80f72 1014 /* all_frames == nil doesn't specify which frames to include. */
44fa5b1e 1015 if (NILP (all_frames))
fbdc1545 1016 all_frames = (! EQ (minibuf, Qlambda)
1bb80f72
RS
1017 ? (FRAME_MINIBUF_WINDOW
1018 (XFRAME
1019 (WINDOW_FRAME
1020 (XWINDOW (window)))))
1021 : Qnil);
89bca612
RS
1022 else if (EQ (all_frames, Qvisible))
1023 ;
f812f9c6
RS
1024 else if (XFASTINT (all_frames) == 0)
1025 ;
1f4c5d09
RS
1026 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1027 /* If all_frames is a frame and window arg isn't on that frame, just
1028 return the first window on the frame. */
1029 return Fframe_first_window (all_frames);
44fa5b1e
JB
1030 else if (! EQ (all_frames, Qt))
1031 all_frames = Qnil;
644b477c
RS
1032 /* Now all_frames is t meaning search all frames,
1033 nil meaning search just current frame,
f812f9c6
RS
1034 visible meaning search just visible frames,
1035 0 meaning search visible and iconified frames,
644b477c 1036 or a window, meaning search the frame that window belongs to. */
7ab12479
JB
1037
1038 /* Do this loop at least once, to get the next window, and perhaps
1039 again, if we hit the minibuffer and that is not acceptable. */
1040 do
1041 {
1042 /* Find a window that actually has a next one. This loop
1043 climbs up the tree. */
265a9e55
JB
1044 while (tem = XWINDOW (window)->next, NILP (tem))
1045 if (tem = XWINDOW (window)->parent, !NILP (tem))
7ab12479 1046 window = tem;
d5783c40 1047 else
7ab12479 1048 {
44fa5b1e
JB
1049 /* We've reached the end of this frame.
1050 Which other frames are acceptable? */
1051 tem = WINDOW_FRAME (XWINDOW (window));
44fa5b1e 1052 if (! NILP (all_frames))
1bb80f72
RS
1053 {
1054 Lisp_Object tem1;
1055
1056 tem1 = tem;
1057 tem = next_frame (tem, all_frames);
1058 /* In the case where the minibuffer is active,
1059 and we include its frame as well as the selected one,
1060 next_frame may get stuck in that frame.
1061 If that happens, go back to the selected frame
1062 so we can complete the cycle. */
1063 if (EQ (tem, tem1))
74112613 1064 XSETFRAME (tem, selected_frame);
1bb80f72 1065 }
44fa5b1e 1066 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
d5783c40 1067
7ab12479
JB
1068 break;
1069 }
1070
1071 window = tem;
d5783c40 1072
7ab12479
JB
1073 /* If we're in a combination window, find its first child and
1074 recurse on that. Otherwise, we've found the window we want. */
1075 while (1)
1076 {
265a9e55 1077 if (!NILP (XWINDOW (window)->hchild))
7ab12479 1078 window = XWINDOW (window)->hchild;
265a9e55 1079 else if (!NILP (XWINDOW (window)->vchild))
7ab12479
JB
1080 window = XWINDOW (window)->vchild;
1081 else break;
1082 }
1083 }
5c4d25a6 1084 /* Which windows are acceptable?
d5783c40 1085 Exit the loop and accept this window if
fbdc1545
RS
1086 this isn't a minibuffer window,
1087 or we're accepting all minibuffer windows,
1088 or this is the active minibuffer and we are accepting that one, or
d5783c40 1089 we've come all the way around and we're back at the original window. */
7ab12479 1090 while (MINI_WINDOW_P (XWINDOW (window))
d5783c40 1091 && ! EQ (minibuf, Qt)
fbdc1545 1092 && ! EQ (minibuf, window)
7f4161e0 1093 && ! EQ (window, start_window));
7ab12479
JB
1094
1095 return window;
1096}
1097
26f6279d
JB
1098/* This comment supplies the doc string for `previous-window',
1099 for make-docfile to see. We cannot put this in the real DEFUN
1100 due to limits in the Unix cpp.
1101
1102DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
5c4d25a6 1103 "Return the window preceding WINDOW in canonical ordering of windows.\n\
d5783c40
JB
1104If omitted, WINDOW defaults to the selected window.\n\
1105\n\
1106Optional second arg MINIBUF t means count the minibuffer window even\n\
1107if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
1108it is active. MINIBUF neither t nor nil means not to count the\n\
1109minibuffer even if it is active.\n\
1110\n\
44fa5b1e
JB
1111Several frames may share a single minibuffer; if the minibuffer\n\
1112counts, all windows on all frames that share that minibuffer count\n\
ed160f1f 1113too. Therefore, `previous-window' can be used to iterate through\n\
44fa5b1e 1114the set of windows even when the minibuffer is on another frame. If\n\
ed160f1f 1115the minibuffer does not count, only windows from WINDOW's frame count\n\
d5783c40 1116\n\
44fa5b1e
JB
1117Optional third arg ALL-FRAMES t means include windows on all frames.\n\
1118ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
89bca612 1119above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
f812f9c6 1120ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1f4c5d09 1121If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
89bca612 1122Anything else means restrict to WINDOW's frame.\n\
dbc4e1c1
JB
1123\n\
1124If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
1125`previous-window' to iterate through the entire cycle of acceptable\n\
1126windows, eventually ending up back at the window you started with.\n\
1127`next-window' traverses the same cycle, in the reverse order.")
26f6279d
JB
1128 (window, minibuf, all_frames) */
1129
1130
1131DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1132 0)
44fa5b1e
JB
1133 (window, minibuf, all_frames)
1134 register Lisp_Object window, minibuf, all_frames;
7ab12479
JB
1135{
1136 register Lisp_Object tem;
d5783c40 1137 Lisp_Object start_window;
7ab12479 1138
265a9e55 1139 if (NILP (window))
7ab12479
JB
1140 window = selected_window;
1141 else
605be8af 1142 CHECK_LIVE_WINDOW (window, 0);
7ab12479 1143
d5783c40
JB
1144 start_window = window;
1145
1146 /* minibuf == nil may or may not include minibuffers.
1147 Decide if it does. */
265a9e55 1148 if (NILP (minibuf))
fbdc1545
RS
1149 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1150 else if (! EQ (minibuf, Qt))
1151 minibuf = Qlambda;
1152 /* Now minibuf can be t => count all minibuffer windows,
1153 lambda => count none of them,
1154 or a specific minibuffer window (the active one) to count. */
d5783c40 1155
44fa5b1e
JB
1156 /* all_frames == nil doesn't specify which frames to include.
1157 Decide which frames it includes. */
1158 if (NILP (all_frames))
fbdc1545 1159 all_frames = (! EQ (minibuf, Qlambda)
44fa5b1e
JB
1160 ? (FRAME_MINIBUF_WINDOW
1161 (XFRAME
1162 (WINDOW_FRAME
d5783c40
JB
1163 (XWINDOW (window)))))
1164 : Qnil);
89bca612
RS
1165 else if (EQ (all_frames, Qvisible))
1166 ;
f812f9c6
RS
1167 else if (XFASTINT (all_frames) == 0)
1168 ;
1f4c5d09
RS
1169 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1170 /* If all_frames is a frame and window arg isn't on that frame, just
1171 return the first window on the frame. */
1172 return Fframe_first_window (all_frames);
44fa5b1e
JB
1173 else if (! EQ (all_frames, Qt))
1174 all_frames = Qnil;
644b477c
RS
1175 /* Now all_frames is t meaning search all frames,
1176 nil meaning search just current frame,
f812f9c6
RS
1177 visible meaning search just visible frames,
1178 0 meaning search visible and iconified frames,
644b477c 1179 or a window, meaning search the frame that window belongs to. */
7ab12479
JB
1180
1181 /* Do this loop at least once, to get the previous window, and perhaps
1182 again, if we hit the minibuffer and that is not acceptable. */
1183 do
1184 {
1185 /* Find a window that actually has a previous one. This loop
1186 climbs up the tree. */
265a9e55
JB
1187 while (tem = XWINDOW (window)->prev, NILP (tem))
1188 if (tem = XWINDOW (window)->parent, !NILP (tem))
7ab12479 1189 window = tem;
d5783c40 1190 else
7ab12479 1191 {
44fa5b1e
JB
1192 /* We have found the top window on the frame.
1193 Which frames are acceptable? */
1194 tem = WINDOW_FRAME (XWINDOW (window));
44fa5b1e 1195 if (! NILP (all_frames))
dbc4e1c1
JB
1196 /* It's actually important that we use prev_frame here,
1197 rather than next_frame. All the windows acceptable
1198 according to the given parameters should form a ring;
1199 Fnext_window and Fprevious_window should go back and
1200 forth around the ring. If we use next_frame here,
1201 then Fnext_window and Fprevious_window take different
1202 paths through the set of acceptable windows.
1203 window_loop assumes that these `ring' requirement are
1204 met. */
1bb80f72
RS
1205 {
1206 Lisp_Object tem1;
1207
1208 tem1 = tem;
1209 tem = prev_frame (tem, all_frames);
1210 /* In the case where the minibuffer is active,
1211 and we include its frame as well as the selected one,
1212 next_frame may get stuck in that frame.
1213 If that happens, go back to the selected frame
1214 so we can complete the cycle. */
1215 if (EQ (tem, tem1))
74112613 1216 XSETFRAME (tem, selected_frame);
1bb80f72 1217 }
644b477c
RS
1218 /* If this frame has a minibuffer, find that window first,
1219 because it is conceptually the last window in that frame. */
5e12e32f
JB
1220 if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
1221 tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
1222 else
644b477c 1223 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
d5783c40 1224
7ab12479
JB
1225 break;
1226 }
1227
1228 window = tem;
1229 /* If we're in a combination window, find its last child and
1230 recurse on that. Otherwise, we've found the window we want. */
1231 while (1)
1232 {
265a9e55 1233 if (!NILP (XWINDOW (window)->hchild))
7ab12479 1234 window = XWINDOW (window)->hchild;
265a9e55 1235 else if (!NILP (XWINDOW (window)->vchild))
7ab12479
JB
1236 window = XWINDOW (window)->vchild;
1237 else break;
265a9e55 1238 while (tem = XWINDOW (window)->next, !NILP (tem))
7ab12479
JB
1239 window = tem;
1240 }
1241 }
5c4d25a6 1242 /* Which windows are acceptable?
d5783c40 1243 Exit the loop and accept this window if
fbdc1545
RS
1244 this isn't a minibuffer window,
1245 or we're accepting all minibuffer windows,
1246 or this is the active minibuffer and we are accepting that one, or
d5783c40 1247 we've come all the way around and we're back at the original window. */
7ab12479 1248 while (MINI_WINDOW_P (XWINDOW (window))
fbdc1545
RS
1249 && ! EQ (minibuf, Qt)
1250 && ! EQ (minibuf, window)
1251 && ! EQ (window, start_window));
7ab12479
JB
1252
1253 return window;
1254}
1255
62c07cc7 1256DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
44fa5b1e
JB
1257 "Select the ARG'th different window on this frame.\n\
1258All windows on current frame are arranged in a cyclic order.\n\
7ab12479
JB
1259This command selects the window ARG steps away in that order.\n\
1260A negative ARG moves in the opposite order. If the optional second\n\
44fa5b1e 1261argument ALL_FRAMES is non-nil, cycle through all frames.")
413430c5
EN
1262 (arg, all_frames)
1263 register Lisp_Object arg, all_frames;
7ab12479
JB
1264{
1265 register int i;
1266 register Lisp_Object w;
1267
413430c5 1268 CHECK_NUMBER (arg, 0);
7ab12479 1269 w = selected_window;
413430c5 1270 i = XINT (arg);
7ab12479
JB
1271
1272 while (i > 0)
1273 {
44fa5b1e 1274 w = Fnext_window (w, Qnil, all_frames);
7ab12479
JB
1275 i--;
1276 }
1277 while (i < 0)
1278 {
44fa5b1e 1279 w = Fprevious_window (w, Qnil, all_frames);
7ab12479
JB
1280 i++;
1281 }
1282 Fselect_window (w);
1283 return Qnil;
1284}
1285\f
1286/* Look at all windows, performing an operation specified by TYPE
1287 with argument OBJ.
75d8f668 1288 If FRAMES is Qt, look at all frames;
75d8f668 1289 Qnil, look at just the selected frame;
89bca612 1290 Qvisible, look at visible frames;
75d8f668 1291 a frame, just look at windows on that frame.
7ab12479
JB
1292 If MINI is non-zero, perform the operation on minibuffer windows too.
1293*/
1294
1295enum window_loop
1296{
1297 WINDOW_LOOP_UNUSED,
1298 GET_BUFFER_WINDOW, /* Arg is buffer */
1299 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1300 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1301 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1302 GET_LARGEST_WINDOW,
a68c0d3b 1303 UNSHOW_BUFFER /* Arg is buffer */
7ab12479
JB
1304};
1305
1306static Lisp_Object
44fa5b1e 1307window_loop (type, obj, mini, frames)
7ab12479 1308 enum window_loop type;
44fa5b1e 1309 register Lisp_Object obj, frames;
7ab12479
JB
1310 int mini;
1311{
1312 register Lisp_Object w;
1313 register Lisp_Object best_window;
1314 register Lisp_Object next_window;
4b206065 1315 register Lisp_Object last_window;
44fa5b1e 1316 FRAME_PTR frame;
89bca612
RS
1317 Lisp_Object frame_arg;
1318 frame_arg = Qt;
44fa5b1e
JB
1319
1320 /* If we're only looping through windows on a particular frame,
1321 frame points to that frame. If we're looping through windows
1322 on all frames, frame is 0. */
1323 if (FRAMEP (frames))
1324 frame = XFRAME (frames);
1325 else if (NILP (frames))
1326 frame = selected_frame;
7ab12479 1327 else
44fa5b1e 1328 frame = 0;
89bca612
RS
1329 if (frame)
1330 frame_arg = Qlambda;
f812f9c6
RS
1331 else if (XFASTINT (frames) == 0)
1332 frame_arg = frames;
89bca612
RS
1333 else if (EQ (frames, Qvisible))
1334 frame_arg = frames;
7ab12479 1335
89bca612
RS
1336 /* frame_arg is Qlambda to stick to one frame,
1337 Qvisible to consider all visible frames,
1338 or Qt otherwise. */
1339
7ab12479 1340 /* Pick a window to start with. */
017b2bad 1341 if (WINDOWP (obj))
4b206065 1342 w = obj;
44fa5b1e 1343 else if (frame)
4b206065 1344 w = FRAME_SELECTED_WINDOW (frame);
7ab12479 1345 else
4b206065
JB
1346 w = FRAME_SELECTED_WINDOW (selected_frame);
1347
1348 /* Figure out the last window we're going to mess with. Since
1349 Fnext_window, given the same options, is guaranteed to go in a
1350 ring, we can just use Fprevious_window to find the last one.
1351
1352 We can't just wait until we hit the first window again, because
1353 it might be deleted. */
1354
89bca612 1355 last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
7ab12479 1356
7ab12479 1357 best_window = Qnil;
4b206065 1358 for (;;)
7ab12479 1359 {
75d8f668
JB
1360 FRAME_PTR w_frame = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1361
7ab12479
JB
1362 /* Pick the next window now, since some operations will delete
1363 the current window. */
89bca612 1364 next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
7ab12479 1365
d29a5631
RS
1366 /* Note that we do not pay attention here to whether
1367 the frame is visible, since Fnext_window skips non-visible frames
1368 if that is desired, under the control of frame_arg. */
75d8f668 1369 if (! MINI_WINDOW_P (XWINDOW (w))
7ab12479
JB
1370 || (mini && minibuf_level > 0))
1371 switch (type)
1372 {
1373 case GET_BUFFER_WINDOW:
5c204627
RS
1374 if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj)
1375 /* Don't find any minibuffer window
1376 except the one that is currently in use. */
1377 && (MINI_WINDOW_P (XWINDOW (w))
1378 ? EQ (w, minibuf_window) : 1))
7ab12479
JB
1379 return w;
1380 break;
1381
1382 case GET_LRU_WINDOW:
1383 /* t as arg means consider only full-width windows */
111e5992 1384 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (XWINDOW (w)))
7ab12479 1385 break;
7ab12479
JB
1386 /* Ignore dedicated windows and minibuffers. */
1387 if (MINI_WINDOW_P (XWINDOW (w))
265a9e55 1388 || !NILP (XWINDOW (w)->dedicated))
7ab12479 1389 break;
265a9e55 1390 if (NILP (best_window)
7ab12479
JB
1391 || (XFASTINT (XWINDOW (best_window)->use_time)
1392 > XFASTINT (XWINDOW (w)->use_time)))
1393 best_window = w;
1394 break;
1395
1396 case DELETE_OTHER_WINDOWS:
1397 if (XWINDOW (w) != XWINDOW (obj))
1398 Fdelete_window (w);
1399 break;
1400
1401 case DELETE_BUFFER_WINDOWS:
1402 if (EQ (XWINDOW (w)->buffer, obj))
1403 {
3548e138
RS
1404 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1405
1406 /* If this window is dedicated, and in a frame of its own,
1407 kill the frame. */
1408 if (EQ (w, FRAME_ROOT_WINDOW (f))
1409 && !NILP (XWINDOW (w)->dedicated)
1410 && other_visible_frames (f))
7ab12479 1411 {
3548e138
RS
1412 /* Skip the other windows on this frame.
1413 There might be one, the minibuffer! */
1414 if (! EQ (w, last_window))
1415 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1416 {
1417 /* As we go, check for the end of the loop.
1418 We mustn't start going around a second time. */
1419 if (EQ (next_window, last_window))
1420 {
1421 last_window = w;
1422 break;
1423 }
1424 next_window = Fnext_window (next_window,
1425 mini ? Qt : Qnil,
1426 frame_arg);
1427 }
1428 /* Now we can safely delete the frame. */
1429 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
7ab12479
JB
1430 }
1431 else
3548e138
RS
1432 /* If we're deleting the buffer displayed in the only window
1433 on the frame, find a new buffer to display there. */
1434 if (NILP (XWINDOW (w)->parent))
1435 {
1436 Lisp_Object new_buffer;
1437 new_buffer = Fother_buffer (obj, Qnil);
1438 if (NILP (new_buffer))
1439 new_buffer
1440 = Fget_buffer_create (build_string ("*scratch*"));
1441 Fset_window_buffer (w, new_buffer);
1442 if (EQ (w, selected_window))
1443 Fset_buffer (XWINDOW (w)->buffer);
1444 }
1445 else
1446 Fdelete_window (w);
7ab12479
JB
1447 }
1448 break;
1449
1450 case GET_LARGEST_WINDOW:
7ab12479
JB
1451 /* Ignore dedicated windows and minibuffers. */
1452 if (MINI_WINDOW_P (XWINDOW (w))
265a9e55 1453 || !NILP (XWINDOW (w)->dedicated))
7ab12479
JB
1454 break;
1455 {
1456 struct window *best_window_ptr = XWINDOW (best_window);
1457 struct window *w_ptr = XWINDOW (w);
6b54027b
RS
1458 if (NILP (best_window)
1459 || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
1460 > (XFASTINT (best_window_ptr->height)
1461 * XFASTINT (best_window_ptr->width))))
7ab12479
JB
1462 best_window = w;
1463 }
1464 break;
1465
1466 case UNSHOW_BUFFER:
1467 if (EQ (XWINDOW (w)->buffer, obj))
1468 {
1469 /* Find another buffer to show in this window. */
12cae7c0 1470 Lisp_Object another_buffer;
38ab08d1 1471 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
12cae7c0 1472 another_buffer = Fother_buffer (obj, Qnil);
265a9e55 1473 if (NILP (another_buffer))
7ab12479
JB
1474 another_buffer
1475 = Fget_buffer_create (build_string ("*scratch*"));
38ab08d1
RS
1476 /* If this window is dedicated, and in a frame of its own,
1477 kill the frame. */
1478 if (EQ (w, FRAME_ROOT_WINDOW (f))
596122a7 1479 && !NILP (XWINDOW (w)->dedicated)
38ab08d1 1480 && other_visible_frames (f))
45945a7b
RS
1481 {
1482 /* Skip the other windows on this frame.
1483 There might be one, the minibuffer! */
1484 if (! EQ (w, last_window))
1485 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1486 {
1487 /* As we go, check for the end of the loop.
1488 We mustn't start going around a second time. */
1489 if (EQ (next_window, last_window))
1490 {
1491 last_window = w;
1492 break;
1493 }
1494 next_window = Fnext_window (next_window,
1495 mini ? Qt : Qnil,
1496 frame_arg);
1497 }
1498 /* Now we can safely delete the frame. */
1499 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1500 }
38ab08d1 1501 else
38ab08d1
RS
1502 {
1503 /* Otherwise show a different buffer in the window. */
1504 XWINDOW (w)->dedicated = Qnil;
1505 Fset_window_buffer (w, another_buffer);
1506 if (EQ (w, selected_window))
1507 Fset_buffer (XWINDOW (w)->buffer);
1508 }
7ab12479
JB
1509 }
1510 break;
1511 }
4b206065
JB
1512
1513 if (EQ (w, last_window))
1514 break;
1515
7ab12479
JB
1516 w = next_window;
1517 }
7ab12479
JB
1518
1519 return best_window;
37962e60 1520}
605be8af 1521
7ab12479
JB
1522DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
1523 "Return the window least recently selected or used for display.\n\
89bca612 1524If optional argument FRAME is `visible', search all visible frames.\n\
cee67da9 1525If FRAME is 0, search all visible and iconified frames.\n\
89bca612
RS
1526If FRAME is t, search all frames.\n\
1527If FRAME is nil, search only the selected frame.\n\
1528If FRAME is a frame, search only that frame.")
1529 (frame)
1530 Lisp_Object frame;
7ab12479
JB
1531{
1532 register Lisp_Object w;
1533 /* First try for a window that is full-width */
89bca612 1534 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
265a9e55 1535 if (!NILP (w) && !EQ (w, selected_window))
7ab12479
JB
1536 return w;
1537 /* If none of them, try the rest */
89bca612 1538 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
7ab12479
JB
1539}
1540
1541DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
1542 "Return the largest window in area.\n\
89bca612 1543If optional argument FRAME is `visible', search all visible frames.\n\
cee67da9 1544If FRAME is 0, search all visible and iconified frames.\n\
89bca612
RS
1545If FRAME is t, search all frames.\n\
1546If FRAME is nil, search only the selected frame.\n\
1547If FRAME is a frame, search only that frame.")
44fa5b1e
JB
1548 (frame)
1549 Lisp_Object frame;
7ab12479
JB
1550{
1551 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
44fa5b1e 1552 frame);
7ab12479
JB
1553}
1554
1555DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
1556 "Return a window currently displaying BUFFER, or nil if none.\n\
89bca612 1557If optional argument FRAME is `visible', search all visible frames.\n\
f812f9c6 1558If optional argument FRAME is 0, search all visible and iconified frames.\n\
89bca612 1559If FRAME is t, search all frames.\n\
1bc981d2 1560If FRAME is nil, search only the selected frame.\n\
89bca612 1561If FRAME is a frame, search only that frame.")
44fa5b1e
JB
1562 (buffer, frame)
1563 Lisp_Object buffer, frame;
7ab12479
JB
1564{
1565 buffer = Fget_buffer (buffer);
017b2bad 1566 if (BUFFERP (buffer))
44fa5b1e 1567 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
7ab12479
JB
1568 else
1569 return Qnil;
1570}
1571
1572DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
1573 0, 1, "",
44fa5b1e 1574 "Make WINDOW (or the selected window) fill its frame.\n\
f16a1ed3
RS
1575Only the frame WINDOW is on is affected.\n\
1576This function tries to reduce display jumps\n\
1577by keeping the text previously visible in WINDOW\n\
1578in the same place on the frame. Doing this depends on\n\
1579the value of (window-start WINDOW), so if calling this function\n\
1580in a program gives strange scrolling, make sure the window-start\n\
1581value is reasonable when this function is called.")
7ab12479
JB
1582 (window)
1583 Lisp_Object window;
1584{
1585 struct window *w;
00d3d838 1586 int startpos;
7ab12479
JB
1587 int top;
1588
265a9e55 1589 if (NILP (window))
7ab12479
JB
1590 window = selected_window;
1591 else
605be8af 1592 CHECK_LIVE_WINDOW (window, 0);
7ab12479
JB
1593
1594 w = XWINDOW (window);
a2b38b3c 1595
00d3d838
KH
1596 startpos = marker_position (w->start);
1597 top = XFASTINT (w->top) - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w)));
7ab12479 1598
a2b38b3c
RS
1599 if (MINI_WINDOW_P (w) && top > 0)
1600 error ("Can't expand minibuffer to full frame");
1601
70728a80 1602 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
7ab12479 1603
00d3d838
KH
1604 /* Try to minimize scrolling, by setting the window start to the point
1605 will cause the text at the old window start to be at the same place
1606 on the frame. But don't try to do this if the window start is
1607 outside the visible portion (as might happen when the display is
1608 not current, due to typeahead). */
1609 if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
1610 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
1611 {
1612 struct position pos;
1613 struct buffer *obuf = current_buffer;
1614
1615 Fset_buffer (w->buffer);
1616 /* This computation used to temporarily move point, but that can
1617 have unwanted side effects due to text properties. */
0383eb57 1618 pos = *vmotion (startpos, -top, w);
4d047f50 1619
b73ea88e
RS
1620 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
1621 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
1622 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
00d3d838 1623 : Qnil);
80622eec
RS
1624 /* We need to do this, so that the window-scroll-functions
1625 get called. */
4d047f50 1626 w->optional_new_start = Qt;
00d3d838
KH
1627
1628 set_buffer_internal (obuf);
1629 }
7ab12479
JB
1630 return Qnil;
1631}
1632
1633DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
3cbbb729 1634 1, 2, "bDelete windows on (buffer): ",
26f6279d
JB
1635 "Delete all windows showing BUFFER.\n\
1636Optional second argument FRAME controls which frames are affected.\n\
1637If nil or omitted, delete all windows showing BUFFER in any frame.\n\
1638If t, delete only windows showing BUFFER in the selected frame.\n\
89bca612 1639If `visible', delete all windows showing BUFFER in any visible frame.\n\
26f6279d
JB
1640If a frame, delete only windows showing BUFFER in that frame.")
1641 (buffer, frame)
1642 Lisp_Object buffer, frame;
7ab12479 1643{
26f6279d
JB
1644 /* FRAME uses t and nil to mean the opposite of what window_loop
1645 expects. */
1646 if (! FRAMEP (frame))
1647 frame = NILP (frame) ? Qt : Qnil;
26f6279d 1648
265a9e55 1649 if (!NILP (buffer))
7ab12479
JB
1650 {
1651 buffer = Fget_buffer (buffer);
1652 CHECK_BUFFER (buffer, 0);
26f6279d 1653 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
7ab12479
JB
1654 }
1655 return Qnil;
1656}
1657
1658DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
1659 Sreplace_buffer_in_windows,
1660 1, 1, "bReplace buffer in windows: ",
1661 "Replace BUFFER with some other buffer in all windows showing it.")
1662 (buffer)
1663 Lisp_Object buffer;
1664{
265a9e55 1665 if (!NILP (buffer))
7ab12479
JB
1666 {
1667 buffer = Fget_buffer (buffer);
1668 CHECK_BUFFER (buffer, 0);
1669 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
1670 }
1671 return Qnil;
1672}
ff58478b
RS
1673
1674/* Replace BUFFER with some other buffer in all windows
1675 of all frames, even those on other keyboards. */
1676
1677void
1678replace_buffer_in_all_windows (buffer)
1679 Lisp_Object buffer;
1680{
27abb84f 1681#ifdef MULTI_KBOARD
ff58478b
RS
1682 Lisp_Object tail, frame;
1683
ff58478b
RS
1684 /* A single call to window_loop won't do the job
1685 because it only considers frames on the current keyboard.
1686 So loop manually over frames, and handle each one. */
1687 FOR_EACH_FRAME (tail, frame)
db7f721d 1688 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
ff58478b 1689#else
db7f721d 1690 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
ff58478b
RS
1691#endif
1692}
7ab12479
JB
1693\f
1694/* Set the height of WINDOW and all its inferiors. */
a481b3ea
JB
1695
1696/* The smallest acceptable dimensions for a window. Anything smaller
1697 might crash Emacs. */
1698#define MIN_SAFE_WINDOW_WIDTH (2)
1699#define MIN_SAFE_WINDOW_HEIGHT (2)
1700
1701/* Make sure that window_min_height and window_min_width are
1702 not too small; if they are, set them to safe minima. */
1703
1704static void
1705check_min_window_sizes ()
1706{
1707 /* Smaller values might permit a crash. */
1708 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
1709 window_min_width = MIN_SAFE_WINDOW_WIDTH;
1710 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
1711 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
1712}
1713
1714/* If *ROWS or *COLS are too small a size for FRAME, set them to the
1715 minimum allowable size. */
605be8af 1716void
a481b3ea 1717check_frame_size (frame, rows, cols)
605be8af
JB
1718 FRAME_PTR frame;
1719 int *rows, *cols;
a481b3ea 1720{
628df3bf 1721 /* For height, we have to see:
37962e60 1722 whether the frame has a minibuffer,
628df3bf
JB
1723 whether it wants a mode line, and
1724 whether it has a menu bar. */
a481b3ea 1725 int min_height =
79f92720
JB
1726 (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
1727 : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
a481b3ea 1728 : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
628df3bf
JB
1729 if (FRAME_MENU_BAR_LINES (frame) > 0)
1730 min_height += FRAME_MENU_BAR_LINES (frame);
a481b3ea
JB
1731
1732 if (*rows < min_height)
1733 *rows = min_height;
1734 if (*cols < MIN_SAFE_WINDOW_WIDTH)
1735 *cols = MIN_SAFE_WINDOW_WIDTH;
1736}
1737
7ab12479
JB
1738/* Normally the window is deleted if it gets too small.
1739 nodelete nonzero means do not do this.
1740 (The caller should check later and do so if appropriate) */
1741
5e14b1fc 1742void
7ab12479
JB
1743set_window_height (window, height, nodelete)
1744 Lisp_Object window;
1745 int height;
1746 int nodelete;
1747{
1748 register struct window *w = XWINDOW (window);
1749 register struct window *c;
1750 int oheight = XFASTINT (w->height);
1751 int top, pos, lastbot, opos, lastobot;
1752 Lisp_Object child;
1753
a481b3ea
JB
1754 check_min_window_sizes ();
1755
7ab12479 1756 if (!nodelete
265a9e55 1757 && ! NILP (w->parent)
c6b530ed
RS
1758 && (MINI_WINDOW_P (w)
1759 ? height < 1
1760 : height < window_min_height))
7ab12479 1761 {
543f5fb1 1762 delete_window (window);
7ab12479
JB
1763 return;
1764 }
1765
d834a2e9 1766 XSETFASTINT (w->last_modified, 0);
3cd21523 1767 XSETFASTINT (w->last_overlay_modified, 0);
7ab12479 1768 windows_or_buffers_changed++;
29aeee73
RS
1769 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1770
d834a2e9 1771 XSETFASTINT (w->height, height);
265a9e55 1772 if (!NILP (w->hchild))
7ab12479 1773 {
265a9e55 1774 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
7ab12479
JB
1775 {
1776 XWINDOW (child)->top = w->top;
1777 set_window_height (child, height, nodelete);
1778 }
1779 }
265a9e55 1780 else if (!NILP (w->vchild))
7ab12479
JB
1781 {
1782 lastbot = top = XFASTINT (w->top);
1783 lastobot = 0;
265a9e55 1784 for (child = w->vchild; !NILP (child); child = c->next)
7ab12479
JB
1785 {
1786 c = XWINDOW (child);
1787
1788 opos = lastobot + XFASTINT (c->height);
1789
d834a2e9 1790 XSETFASTINT (c->top, lastbot);
7ab12479
JB
1791
1792 pos = (((opos * height) << 1) + oheight) / (oheight << 1);
1793
1794 /* Avoid confusion: inhibit deletion of child if becomes too small */
1795 set_window_height (child, pos + top - lastbot, 1);
1796
1797 /* Now advance child to next window,
1798 and set lastbot if child was not just deleted. */
1799 lastbot = pos + top;
1800 lastobot = opos;
1801 }
1802 /* Now delete any children that became too small. */
1803 if (!nodelete)
265a9e55 1804 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
7ab12479
JB
1805 {
1806 set_window_height (child, XINT (XWINDOW (child)->height), 0);
1807 }
1808 }
1809}
1810
1811/* Recursively set width of WINDOW and its inferiors. */
1812
5e14b1fc 1813void
7ab12479
JB
1814set_window_width (window, width, nodelete)
1815 Lisp_Object window;
1816 int width;
1817 int nodelete;
1818{
1819 register struct window *w = XWINDOW (window);
1820 register struct window *c;
1821 int owidth = XFASTINT (w->width);
1822 int left, pos, lastright, opos, lastoright;
1823 Lisp_Object child;
1824
abdced83 1825 if (!nodelete && width < window_min_width && !NILP (w->parent))
7ab12479 1826 {
543f5fb1 1827 delete_window (window);
7ab12479
JB
1828 return;
1829 }
1830
d834a2e9 1831 XSETFASTINT (w->last_modified, 0);
3cd21523 1832 XSETFASTINT (w->last_overlay_modified, 0);
7ab12479 1833 windows_or_buffers_changed++;
29aeee73
RS
1834 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1835
d834a2e9 1836 XSETFASTINT (w->width, width);
265a9e55 1837 if (!NILP (w->vchild))
7ab12479 1838 {
265a9e55 1839 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
7ab12479
JB
1840 {
1841 XWINDOW (child)->left = w->left;
1842 set_window_width (child, width, nodelete);
1843 }
1844 }
265a9e55 1845 else if (!NILP (w->hchild))
7ab12479
JB
1846 {
1847 lastright = left = XFASTINT (w->left);
1848 lastoright = 0;
265a9e55 1849 for (child = w->hchild; !NILP (child); child = c->next)
7ab12479
JB
1850 {
1851 c = XWINDOW (child);
1852
1853 opos = lastoright + XFASTINT (c->width);
1854
d834a2e9 1855 XSETFASTINT (c->left, lastright);
7ab12479
JB
1856
1857 pos = (((opos * width) << 1) + owidth) / (owidth << 1);
1858
1859 /* Inhibit deletion for becoming too small */
1860 set_window_width (child, pos + left - lastright, 1);
1861
1862 /* Now advance child to next window,
1863 and set lastright if child was not just deleted. */
1864 lastright = pos + left, lastoright = opos;
1865 }
1866 /* Delete children that became too small */
1867 if (!nodelete)
265a9e55 1868 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
7ab12479
JB
1869 {
1870 set_window_width (child, XINT (XWINDOW (child)->width), 0);
1871 }
1872 }
1873}
1874\f
1d8d96fa 1875int window_select_count;
7ab12479 1876
5b03d3c0
RS
1877Lisp_Object
1878Fset_window_buffer_unwind (obuf)
1879 Lisp_Object obuf;
1880{
1881 Fset_buffer (obuf);
1882 return Qnil;
1883}
1884
7ab12479
JB
1885DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
1886 "Make WINDOW display BUFFER as its contents.\n\
1887BUFFER can be a buffer or buffer name.")
1888 (window, buffer)
1889 register Lisp_Object window, buffer;
1890{
1891 register Lisp_Object tem;
1892 register struct window *w = decode_window (window);
5b03d3c0 1893 int count = specpdl_ptr - specpdl;
7ab12479
JB
1894
1895 buffer = Fget_buffer (buffer);
1896 CHECK_BUFFER (buffer, 1);
1897
265a9e55 1898 if (NILP (XBUFFER (buffer)->name))
7ab12479
JB
1899 error ("Attempt to display deleted buffer");
1900
1901 tem = w->buffer;
265a9e55 1902 if (NILP (tem))
7ab12479
JB
1903 error ("Window is deleted");
1904 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
1905 is first being set up. */
1906 {
265a9e55 1907 if (!NILP (w->dedicated) && !EQ (tem, buffer))
3cf85532
RS
1908 error ("Window is dedicated to `%s'",
1909 XSTRING (XBUFFER (tem)->name)->data);
7ab12479
JB
1910
1911 unshow_buffer (w);
1912 }
1913
1914 w->buffer = buffer;
86e48436
RS
1915
1916 if (EQ (window, selected_window))
1917 XBUFFER (w->buffer)->last_selected_window = window;
850ebd4f
RS
1918 if (INTEGERP (XBUFFER (buffer)->display_count))
1919 XSETINT (XBUFFER (buffer)->display_count,
296b535c 1920 XINT (XBUFFER (buffer)->display_count) + 1);
86e48436 1921
d834a2e9 1922 XSETFASTINT (w->window_end_pos, 0);
5a41ab94 1923 w->window_end_valid = Qnil;
dba06815 1924 XSETFASTINT (w->hscroll, 0);
b73ea88e
RS
1925 set_marker_both (w->pointm, buffer,
1926 BUF_PT (XBUFFER (buffer)), BUF_PT_BYTE (XBUFFER (buffer)));
7ab12479
JB
1927 set_marker_restricted (w->start,
1928 make_number (XBUFFER (buffer)->last_window_start),
1929 buffer);
1930 w->start_at_line_beg = Qnil;
e36ab06b 1931 w->force_start = Qnil;
d834a2e9 1932 XSETFASTINT (w->last_modified, 0);
3cd21523 1933 XSETFASTINT (w->last_overlay_modified, 0);
7ab12479 1934 windows_or_buffers_changed++;
5b03d3c0
RS
1935
1936 /* We must select BUFFER for running the window-scroll-functions.
1937 If WINDOW is selected, switch permanently.
1938 Otherwise, switch but go back to the ambient buffer afterward. */
7ab12479
JB
1939 if (EQ (window, selected_window))
1940 Fset_buffer (buffer);
5b03d3c0
RS
1941 /* We can't check ! NILP (Vwindow_scroll_functions) here
1942 because that might itself be a local variable. */
1943 else if (window_initialized)
1944 {
1945 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
1946 Fset_buffer (buffer);
1947 }
1948
dba06815
RS
1949 if (! NILP (Vwindow_scroll_functions))
1950 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1951 Fmarker_position (w->start));
7ab12479 1952
543f5fb1
RS
1953 if (! NILP (Vwindow_configuration_change_hook)
1954 && ! NILP (Vrun_hooks))
1955 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1956
5b03d3c0
RS
1957 unbind_to (count, Qnil);
1958
7ab12479
JB
1959 return Qnil;
1960}
1961
1962DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
1963 "Select WINDOW. Most editing will apply to WINDOW's buffer.\n\
1964The main editor command loop selects the buffer of the selected window\n\
1965before each command.")
1966 (window)
1967 register Lisp_Object window;
1968{
1969 register struct window *w;
1970 register struct window *ow = XWINDOW (selected_window);
1971
605be8af 1972 CHECK_LIVE_WINDOW (window, 0);
7ab12479
JB
1973
1974 w = XWINDOW (window);
1975
265a9e55 1976 if (NILP (w->buffer))
7ab12479
JB
1977 error ("Trying to select deleted window or non-leaf window");
1978
d834a2e9 1979 XSETFASTINT (w->use_time, ++window_select_count);
7ab12479
JB
1980 if (EQ (window, selected_window))
1981 return window;
1982
596ae0cf
RS
1983 if (! NILP (ow->buffer))
1984 set_marker_both (ow->pointm, ow->buffer,
1985 BUF_PT (XBUFFER (ow->buffer)),
1986 BUF_PT_BYTE (XBUFFER (ow->buffer)));
7ab12479
JB
1987
1988 selected_window = window;
44fa5b1e 1989 if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
7ab12479 1990 {
44fa5b1e 1991 XFRAME (WINDOW_FRAME (w))->selected_window = window;
147a6615
RS
1992 /* Use this rather than Fhandle_switch_frame
1993 so that FRAME_FOCUS_FRAME is moved appropriately as we
1994 move around in the state where a minibuffer in a separate
1995 frame is active. */
1996 Fselect_frame (WINDOW_FRAME (w), Qnil);
7ab12479
JB
1997 }
1998 else
44fa5b1e 1999 selected_frame->selected_window = window;
7ab12479
JB
2000
2001 record_buffer (w->buffer);
2002 Fset_buffer (w->buffer);
2003
86e48436
RS
2004 XBUFFER (w->buffer)->last_selected_window = window;
2005
7ab12479
JB
2006 /* Go to the point recorded in the window.
2007 This is important when the buffer is in more
2008 than one window. It also matters when
2009 redisplay_window has altered point after scrolling,
2010 because it makes the change only in the window. */
2011 {
2012 register int new_point = marker_position (w->pointm);
2013 if (new_point < BEGV)
2014 SET_PT (BEGV);
a9c95e08 2015 else if (new_point > ZV)
7ab12479
JB
2016 SET_PT (ZV);
2017 else
2018 SET_PT (new_point);
2019 }
2020
2021 windows_or_buffers_changed++;
2022 return window;
2023}
2024
441a127e
RS
2025/* Deiconify the frame containing the window WINDOW,
2026 unless it is the selected frame;
2027 then return WINDOW.
2028
2029 The reason for the exception for the selected frame
2030 is that it seems better not to change the selected frames visibility
2031 merely because of displaying a different buffer in it.
2032 The deiconification is useful when a buffer gets shown in
2033 another frame that you were not using lately. */
d07f802a
RS
2034
2035static Lisp_Object
2036display_buffer_1 (window)
2037 Lisp_Object window;
2038{
d07f802a
RS
2039 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
2040 FRAME_SAMPLE_VISIBILITY (f);
60117126
RS
2041 if (f != selected_frame)
2042 {
2043 if (FRAME_ICONIFIED_P (f))
2044 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
37962e60 2045 else if (FRAME_VISIBLE_P (f))
60117126
RS
2046 Fraise_frame (WINDOW_FRAME (XWINDOW (window)));
2047 }
d07f802a
RS
2048 return window;
2049}
2050
4628f7a4 2051DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
0f7d64d2
GV
2052 "Returns non-nil if a buffer named BUFFER-NAME would be created specially.\n\
2053The value is actually t if the frame should be called with default frame\n\
2054parameters, and a list of frame parameters if they were specified.\n\
4628f7a4
EN
2055See `special-display-buffer-names', and `special-display-regexps'.")
2056 (buffer_name)
2057 Lisp_Object buffer_name;
2058{
2059 Lisp_Object tem;
2060
2061 CHECK_STRING (buffer_name, 1);
2062
2063 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
2064 if (!NILP (tem))
2065 return Qt;
2066
2067 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
2068 if (!NILP (tem))
2069 return XCDR (tem);
2070
2071 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
2072 {
2073 Lisp_Object car = XCAR (tem);
2074 if (STRINGP (car)
2075 && fast_string_match (car, buffer_name) >= 0)
2076 return Qt;
2077 else if (CONSP (car)
2078 && STRINGP (XCAR (car))
2079 && fast_string_match (XCAR (car), buffer_name) >= 0)
0057b00a 2080 return XCDR (car);
4628f7a4
EN
2081 }
2082 return Qnil;
2083}
2084
2085DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
0f7d64d2 2086 "Returns non-nil if a new buffer named BUFFER-NAME would use the same window.\n\
4628f7a4
EN
2087See `same-window-buffer-names' and `same-window-regexps'.")
2088 (buffer_name)
2089 Lisp_Object buffer_name;
2090{
2091 Lisp_Object tem;
2092
2093 CHECK_STRING (buffer_name, 1);
2094
2095 tem = Fmember (buffer_name, Vsame_window_buffer_names);
2096 if (!NILP (tem))
2097 return Qt;
2098
2099 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
2100 if (!NILP (tem))
2101 return Qt;
2102
2103 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
2104 {
2105 Lisp_Object car = XCAR (tem);
2106 if (STRINGP (car)
2107 && fast_string_match (car, buffer_name) >= 0)
2108 return Qt;
2109 else if (CONSP (car)
2110 && STRINGP (XCAR (car))
2111 && fast_string_match (XCAR (car), buffer_name) >= 0)
2112 return Qt;
2113 }
2114 return Qnil;
2115}
2116
53f76081
RS
2117 /* Use B so the default is (other-buffer). */
2118DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
2119 "BDisplay buffer: \nP",
7ab12479
JB
2120 "Make BUFFER appear in some window but don't select it.\n\
2121BUFFER can be a buffer or a buffer name.\n\
2122If BUFFER is shown already in some window, just use that one,\n\
2123unless the window is the selected window and the optional second\n\
46d3268a 2124argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
5141b901 2125If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
5abcf432
RS
2126Returns the window displaying BUFFER.\n\
2127\n\
2128The variables `special-display-buffer-names', `special-display-regexps',\n\
2129`same-window-buffer-names', and `same-window-regexps' customize how certain\n\
53f76081
RS
2130buffer names are handled.\n\
2131\n\
2132If optional argument FRAME is `visible', search all visible frames.\n\
2133If FRAME is 0, search all visible and iconified frames.\n\
2134If FRAME is t, search all frames.\n\
2135If FRAME is a frame, search only that frame.\n\
2136If FRAME is nil, search only the selected frame\n\
2137 (actually the last nonminibuffer frame),\n\
2138 unless `pop-up-frames' is non-nil,\n\
2139 which means search visible and iconified frames.")
2140 (buffer, not_this_window, frame)
2141 register Lisp_Object buffer, not_this_window, frame;
7ab12479 2142{
a90712c2 2143 register Lisp_Object window, tem;
7ab12479
JB
2144
2145 buffer = Fget_buffer (buffer);
2146 CHECK_BUFFER (buffer, 0);
2147
265a9e55 2148 if (!NILP (Vdisplay_buffer_function))
7ab12479
JB
2149 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2150
265a9e55 2151 if (NILP (not_this_window)
7ab12479 2152 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
d07f802a 2153 return display_buffer_1 (selected_window);
7ab12479 2154
855d8627
RS
2155 /* See if the user has specified this buffer should appear
2156 in the selected window. */
2157 if (NILP (not_this_window))
2158 {
4628f7a4 2159 tem = Fsame_window_p (XBUFFER (buffer)->name);
855d8627 2160 if (!NILP (tem))
c63dc4a2
RS
2161 {
2162 Fswitch_to_buffer (buffer, Qnil);
d07f802a 2163 return display_buffer_1 (selected_window);
c63dc4a2 2164 }
855d8627
RS
2165 }
2166
e8edb3ae 2167 /* If pop_up_frames,
73dc5198
KH
2168 look for a window showing BUFFER on any visible or iconified frame.
2169 Otherwise search only the current frame. */
53f76081
RS
2170 if (! NILP (frame))
2171 tem = frame;
2172 else if (pop_up_frames || last_nonminibuf_frame == 0)
73dc5198
KH
2173 XSETFASTINT (tem, 0);
2174 else
73dc5198
KH
2175 XSETFRAME (tem, last_nonminibuf_frame);
2176 window = Fget_buffer_window (buffer, tem);
265a9e55
JB
2177 if (!NILP (window)
2178 && (NILP (not_this_window) || !EQ (window, selected_window)))
f812f9c6 2179 {
d07f802a 2180 return display_buffer_1 (window);
f812f9c6 2181 }
7ab12479 2182
a90712c2 2183 /* Certain buffer names get special handling. */
4628f7a4 2184 if (!NILP (Vspecial_display_function))
a90712c2 2185 {
4628f7a4
EN
2186 tem = Fspecial_display_p (XBUFFER (buffer)->name);
2187 if (EQ (tem, Qt))
a90712c2 2188 return call1 (Vspecial_display_function, buffer);
4628f7a4
EN
2189 if (CONSP (tem))
2190 return call2 (Vspecial_display_function, buffer, tem);
a90712c2
RS
2191 }
2192
44fa5b1e
JB
2193 /* If there are no frames open that have more than a minibuffer,
2194 we need to create a new frame. */
2195 if (pop_up_frames || last_nonminibuf_frame == 0)
7ab12479 2196 {
a90712c2 2197 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
7ab12479 2198 Fset_window_buffer (window, buffer);
d07f802a 2199 return display_buffer_1 (window);
7ab12479 2200 }
7ab12479 2201
43bad991 2202 if (pop_up_windows
44fa5b1e 2203 || FRAME_MINIBUF_ONLY_P (selected_frame)
cee67da9
RS
2204 /* If the current frame is a special display frame,
2205 don't try to reuse its windows. */
2206 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->dedicated)
43bad991 2207 )
7ab12479 2208 {
12cae7c0
KH
2209 Lisp_Object frames;
2210
37962e60 2211 frames = Qnil;
44fa5b1e 2212 if (FRAME_MINIBUF_ONLY_P (selected_frame))
74112613 2213 XSETFRAME (frames, last_nonminibuf_frame);
7ab12479
JB
2214 /* Don't try to create a window if would get an error */
2215 if (split_height_threshold < window_min_height << 1)
2216 split_height_threshold = window_min_height << 1;
2217
cee67da9
RS
2218 /* Note that both Fget_largest_window and Fget_lru_window
2219 ignore minibuffers and dedicated windows.
2220 This means they can return nil. */
7ab12479 2221
cee67da9
RS
2222 /* If the frame we would try to split cannot be split,
2223 try other frames. */
2224 if (FRAME_NO_SPLIT_P (NILP (frames) ? selected_frame
2225 : last_nonminibuf_frame))
2226 {
2227 /* Try visible frames first. */
2228 window = Fget_largest_window (Qvisible);
2229 /* If that didn't work, try iconified frames. */
2230 if (NILP (window))
2231 window = Fget_largest_window (make_number (0));
2232 if (NILP (window))
2233 window = Fget_largest_window (Qt);
2234 }
2235 else
2236 window = Fget_largest_window (frames);
2237
92cca945
RS
2238 /* If we got a tall enough full-width window that can be split,
2239 split it. */
265a9e55 2240 if (!NILP (window)
92cca945 2241 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
7ab12479 2242 && window_height (window) >= split_height_threshold
111e5992 2243 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
7ab12479
JB
2244 window = Fsplit_window (window, Qnil, Qnil);
2245 else
2246 {
1942f68f
RS
2247 Lisp_Object upper, lower, other;
2248
44fa5b1e 2249 window = Fget_lru_window (frames);
92cca945
RS
2250 /* If the LRU window is selected, and big enough,
2251 and can be split, split it. */
cee67da9 2252 if (!NILP (window)
92cca945 2253 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
cee67da9
RS
2254 && (EQ (window, selected_window)
2255 || EQ (XWINDOW (window)->parent, Qnil))
7ab12479
JB
2256 && window_height (window) >= window_min_height << 1)
2257 window = Fsplit_window (window, Qnil, Qnil);
cee67da9 2258 /* If Fget_lru_window returned nil, try other approaches. */
48d9379d 2259
cee67da9 2260 /* Try visible frames first. */
48d9379d
RS
2261 if (NILP (window))
2262 window = Fget_buffer_window (buffer, Qvisible);
cee67da9
RS
2263 if (NILP (window))
2264 window = Fget_largest_window (Qvisible);
2265 /* If that didn't work, try iconified frames. */
48d9379d
RS
2266 if (NILP (window))
2267 window = Fget_buffer_window (buffer, make_number (0));
cee67da9
RS
2268 if (NILP (window))
2269 window = Fget_largest_window (make_number (0));
2270 /* Try invisible frames. */
48d9379d
RS
2271 if (NILP (window))
2272 window = Fget_buffer_window (buffer, Qt);
cee67da9
RS
2273 if (NILP (window))
2274 window = Fget_largest_window (Qt);
2275 /* As a last resort, make a new frame. */
2276 if (NILP (window))
2277 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
1942f68f
RS
2278 /* If window appears above or below another,
2279 even out their heights. */
cac66e4f 2280 other = upper = lower = Qnil;
1942f68f
RS
2281 if (!NILP (XWINDOW (window)->prev))
2282 other = upper = XWINDOW (window)->prev, lower = window;
2283 if (!NILP (XWINDOW (window)->next))
2284 other = lower = XWINDOW (window)->next, upper = window;
2285 if (!NILP (other)
2286 /* Check that OTHER and WINDOW are vertically arrayed. */
296b535c
KH
2287 && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
2288 && (XFASTINT (XWINDOW (other)->height)
2289 > XFASTINT (XWINDOW (window)->height)))
1942f68f 2290 {
296b535c
KH
2291 int total = (XFASTINT (XWINDOW (other)->height)
2292 + XFASTINT (XWINDOW (window)->height));
8d77c0c8
KH
2293 Lisp_Object old_selected_window;
2294 old_selected_window = selected_window;
1942f68f 2295
8d77c0c8 2296 selected_window = upper;
296b535c
KH
2297 change_window_height ((total / 2
2298 - XFASTINT (XWINDOW (upper)->height)),
2299 0);
1942f68f
RS
2300 selected_window = old_selected_window;
2301 }
7ab12479
JB
2302 }
2303 }
2304 else
2305 window = Fget_lru_window (Qnil);
2306
2307 Fset_window_buffer (window, buffer);
d07f802a 2308 return display_buffer_1 (window);
7ab12479
JB
2309}
2310
2311void
2312temp_output_buffer_show (buf)
2313 register Lisp_Object buf;
2314{
2315 register struct buffer *old = current_buffer;
2316 register Lisp_Object window;
2317 register struct window *w;
2318
bccd3dd1
RS
2319 XBUFFER (buf)->directory = current_buffer->directory;
2320
7ab12479 2321 Fset_buffer (buf);
c6367666 2322 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
7ab12479
JB
2323 BEGV = BEG;
2324 ZV = Z;
2325 SET_PT (BEG);
1479ef51 2326 XBUFFER (buf)->clip_changed = 1;
7ab12479
JB
2327 set_buffer_internal (old);
2328
2329 if (!EQ (Vtemp_buffer_show_function, Qnil))
2330 call1 (Vtemp_buffer_show_function, buf);
2331 else
2332 {
53f76081 2333 window = Fdisplay_buffer (buf, Qnil, Qnil);
7ab12479 2334
44fa5b1e
JB
2335 if (XFRAME (XWINDOW (window)->frame) != selected_frame)
2336 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
7ab12479
JB
2337 Vminibuf_scroll_window = window;
2338 w = XWINDOW (window);
d834a2e9 2339 XSETFASTINT (w->hscroll, 0);
b73ea88e
RS
2340 set_marker_restricted_both (w->start, buf, 1, 1);
2341 set_marker_restricted_both (w->pointm, buf, 1, 1);
a58ec57d 2342
37962e60 2343 /* Run temp-buffer-show-hook, with the chosen window selected. */
f52cca03 2344 if (!NILP (Vrun_hooks))
2cccc823 2345 {
f52cca03
RS
2346 Lisp_Object tem;
2347 tem = Fboundp (Qtemp_buffer_show_hook);
2cccc823
RS
2348 if (!NILP (tem))
2349 {
f52cca03
RS
2350 tem = Fsymbol_value (Qtemp_buffer_show_hook);
2351 if (!NILP (tem))
2352 {
2353 int count = specpdl_ptr - specpdl;
2cccc823 2354
f52cca03
RS
2355 /* Select the window that was chosen, for running the hook. */
2356 record_unwind_protect (Fset_window_configuration,
2357 Fcurrent_window_configuration (Qnil));
2cccc823 2358
f52cca03
RS
2359 Fselect_window (window);
2360 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
2361 unbind_to (count, Qnil);
2362 }
2cccc823
RS
2363 }
2364 }
2365 }
7ab12479
JB
2366}
2367\f
2368static
2369make_dummy_parent (window)
2370 Lisp_Object window;
2371{
cffec418 2372 Lisp_Object new;
7ab12479 2373 register struct window *o, *p;
cffec418
KH
2374 register struct Lisp_Vector *vec;
2375 int i;
7ab12479 2376
cffec418
KH
2377 o = XWINDOW (window);
2378 vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
2379 for (i = 0; i < VECSIZE (struct window); ++i)
2380 vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
2381 vec->size = VECSIZE (struct window);
2382 p = (struct window *)vec;
2383 XSETWINDOW (new, p);
7ab12479 2384
d834a2e9 2385 XSETFASTINT (p->sequence_number, ++sequence_number);
7ab12479
JB
2386
2387 /* Put new into window structure in place of window */
2388 replace_window (window, new);
2389
2390 o->next = Qnil;
2391 o->prev = Qnil;
2392 o->vchild = Qnil;
2393 o->hchild = Qnil;
2394 o->parent = new;
2395
2396 p->start = Qnil;
2397 p->pointm = Qnil;
2398 p->buffer = Qnil;
2399}
2400
2401DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
2402 "Split WINDOW, putting SIZE lines in the first of the pair.\n\
2403WINDOW defaults to selected one and SIZE to half its size.\n\
77ae0fe3 2404If optional third arg HORFLAG is non-nil, split side by side\n\
7ab12479 2405and put SIZE columns in the first of the pair.")
77ae0fe3
KH
2406 (window, size, horflag)
2407 Lisp_Object window, size, horflag;
7ab12479
JB
2408{
2409 register Lisp_Object new;
2410 register struct window *o, *p;
c0807608 2411 FRAME_PTR fo;
77ae0fe3 2412 register int size_int;
7ab12479 2413
265a9e55 2414 if (NILP (window))
7ab12479
JB
2415 window = selected_window;
2416 else
605be8af 2417 CHECK_LIVE_WINDOW (window, 0);
7ab12479
JB
2418
2419 o = XWINDOW (window);
c0807608 2420 fo = XFRAME (WINDOW_FRAME (o));
7ab12479 2421
77ae0fe3 2422 if (NILP (size))
7ab12479 2423 {
265a9e55 2424 if (!NILP (horflag))
c0807608
KH
2425 /* Calculate the size of the left-hand window, by dividing
2426 the usable space in columns by two. */
a59fed7e 2427 size_int = XFASTINT (o->width) >> 1;
7ab12479 2428 else
77ae0fe3 2429 size_int = XFASTINT (o->height) >> 1;
7ab12479
JB
2430 }
2431 else
2432 {
77ae0fe3
KH
2433 CHECK_NUMBER (size, 1);
2434 size_int = XINT (size);
7ab12479
JB
2435 }
2436
2437 if (MINI_WINDOW_P (o))
2438 error ("Attempt to split minibuffer window");
7ab12479 2439
a481b3ea 2440 check_min_window_sizes ();
7ab12479 2441
265a9e55 2442 if (NILP (horflag))
7ab12479 2443 {
77ae0fe3
KH
2444 if (size_int < window_min_height)
2445 error ("Window height %d too small (after splitting)", size_int);
2446 if (size_int + window_min_height > XFASTINT (o->height))
37962e60 2447 error ("Window height %d too small (after splitting)",
77ae0fe3 2448 XFASTINT (o->height) - size_int);
265a9e55
JB
2449 if (NILP (o->parent)
2450 || NILP (XWINDOW (o->parent)->vchild))
7ab12479
JB
2451 {
2452 make_dummy_parent (window);
2453 new = o->parent;
2454 XWINDOW (new)->vchild = window;
2455 }
2456 }
2457 else
2458 {
77ae0fe3
KH
2459 if (size_int < window_min_width)
2460 error ("Window width %d too small (after splitting)", size_int);
a59fed7e
RS
2461
2462 if (size_int + window_min_width > XFASTINT (o->width))
37962e60 2463 error ("Window width %d too small (after splitting)",
a59fed7e 2464 XFASTINT (o->width) - size_int);
265a9e55
JB
2465 if (NILP (o->parent)
2466 || NILP (XWINDOW (o->parent)->hchild))
7ab12479
JB
2467 {
2468 make_dummy_parent (window);
2469 new = o->parent;
2470 XWINDOW (new)->hchild = window;
2471 }
2472 }
2473
2474 /* Now we know that window's parent is a vertical combination
2475 if we are dividing vertically, or a horizontal combination
2476 if we are making side-by-side windows */
2477
2478 windows_or_buffers_changed++;
c0807608 2479 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
7ab12479
JB
2480 new = make_window ();
2481 p = XWINDOW (new);
2482
44fa5b1e 2483 p->frame = o->frame;
7ab12479 2484 p->next = o->next;
265a9e55 2485 if (!NILP (p->next))
7ab12479
JB
2486 XWINDOW (p->next)->prev = new;
2487 p->prev = window;
2488 o->next = new;
2489 p->parent = o->parent;
2490 p->buffer = Qt;
2491
44fa5b1e 2492 /* Apportion the available frame space among the two new windows */
7ab12479 2493
265a9e55 2494 if (!NILP (horflag))
7ab12479
JB
2495 {
2496 p->height = o->height;
2497 p->top = o->top;
a59fed7e 2498 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
77ae0fe3
KH
2499 XSETFASTINT (o->width, size_int);
2500 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
7ab12479
JB
2501 }
2502 else
2503 {
2504 p->left = o->left;
2505 p->width = o->width;
77ae0fe3
KH
2506 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
2507 XSETFASTINT (o->height, size_int);
2508 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
7ab12479
JB
2509 }
2510
543f5fb1
RS
2511 Fset_window_buffer (new, o->buffer);
2512
7ab12479
JB
2513 return new;
2514}
2515\f
2516DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
2517 "Make current window ARG lines bigger.\n\
2518From program, optional second arg non-nil means grow sideways ARG columns.")
413430c5
EN
2519 (arg, side)
2520 register Lisp_Object arg, side;
7ab12479 2521{
413430c5
EN
2522 CHECK_NUMBER (arg, 0);
2523 change_window_height (XINT (arg), !NILP (side));
543f5fb1
RS
2524
2525 if (! NILP (Vwindow_configuration_change_hook))
2526 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2527
7ab12479
JB
2528 return Qnil;
2529}
2530
2531DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
2532 "Make current window ARG lines smaller.\n\
413430c5
EN
2533From program, optional second arg non-nil means shrink sideways arg columns.")
2534 (arg, side)
2535 register Lisp_Object arg, side;
7ab12479 2536{
413430c5
EN
2537 CHECK_NUMBER (arg, 0);
2538 change_window_height (-XINT (arg), !NILP (side));
543f5fb1
RS
2539
2540 if (! NILP (Vwindow_configuration_change_hook))
2541 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2542
7ab12479
JB
2543 return Qnil;
2544}
2545
2546int
2547window_height (window)
2548 Lisp_Object window;
2549{
2550 register struct window *p = XWINDOW (window);
2551 return XFASTINT (p->height);
2552}
2553
2554int
2555window_width (window)
2556 Lisp_Object window;
2557{
2558 register struct window *p = XWINDOW (window);
2559 return XFASTINT (p->width);
2560}
2561
05c2896a
JB
2562#define MINSIZE(w) \
2563 (widthflag \
2564 ? window_min_width \
2565 : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
7ab12479
JB
2566
2567#define CURBEG(w) \
05c2896a 2568 *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
7ab12479
JB
2569
2570#define CURSIZE(w) \
05c2896a 2571 *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
7ab12479
JB
2572
2573/* Unlike set_window_height, this function
2574 also changes the heights of the siblings so as to
2575 keep everything consistent. */
2576
5e14b1fc 2577void
7ab12479
JB
2578change_window_height (delta, widthflag)
2579 register int delta;
2580 int widthflag;
2581{
2582 register Lisp_Object parent;
2583 Lisp_Object window;
2584 register struct window *p;
2585 int *sizep;
5e14b1fc
AS
2586 int (*sizefun) P_ ((Lisp_Object))
2587 = widthflag ? window_width : window_height;
2588 register void (*setsizefun) P_ ((Lisp_Object, int, int))
2589 = (widthflag ? set_window_width : set_window_height);
db98a733
RS
2590 int maximum;
2591 Lisp_Object next, prev;
7ab12479 2592
a481b3ea 2593 check_min_window_sizes ();
7ab12479
JB
2594
2595 window = selected_window;
2596 while (1)
2597 {
2598 p = XWINDOW (window);
2599 parent = p->parent;
265a9e55 2600 if (NILP (parent))
7ab12479
JB
2601 {
2602 if (widthflag)
2603 error ("No other window to side of this one");
2604 break;
2605 }
265a9e55
JB
2606 if (widthflag ? !NILP (XWINDOW (parent)->hchild)
2607 : !NILP (XWINDOW (parent)->vchild))
7ab12479
JB
2608 break;
2609 window = parent;
2610 }
2611
05c2896a 2612 sizep = &CURSIZE (window);
7ab12479 2613
7ab12479
JB
2614 {
2615 register int maxdelta;
7ab12479 2616
265a9e55
JB
2617 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
2618 : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
2619 : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
44fa5b1e
JB
2620 /* This is a frame with only one window, a minibuffer-only
2621 or a minibufferless frame. */
d5783c40 2622 : (delta = 0));
7ab12479
JB
2623
2624 if (delta > maxdelta)
2625 /* This case traps trying to make the minibuffer
44fa5b1e
JB
2626 the full frame, or make the only window aside from the
2627 minibuffer the full frame. */
7ab12479 2628 delta = maxdelta;
6b54027b 2629 }
d5783c40 2630
6b54027b
RS
2631 if (*sizep + delta < MINSIZE (window))
2632 {
543f5fb1 2633 delete_window (window);
d5783c40 2634 return;
6b54027b
RS
2635 }
2636
2637 if (delta == 0)
2638 return;
7ab12479 2639
db98a733
RS
2640 /* Find the total we can get from other siblings. */
2641 maximum = 0;
2642 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
2643 maximum += (*sizefun) (next) - MINSIZE (next);
2644 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
2645 maximum += (*sizefun) (prev) - MINSIZE (prev);
2646
2647 /* If we can get it all from them, do so. */
c6b530ed 2648 if (delta <= maximum)
7ab12479 2649 {
db98a733
RS
2650 Lisp_Object first_unaffected;
2651 Lisp_Object first_affected;
2652
2653 next = p->next;
2654 prev = p->prev;
2655 first_affected = window;
2656 /* Look at one sibling at a time,
2657 moving away from this window in both directions alternately,
2658 and take as much as we can get without deleting that sibling. */
dc91fb5d 2659 while (delta != 0)
db98a733
RS
2660 {
2661 if (delta == 0)
2662 break;
2663 if (! NILP (next))
2664 {
2665 int this_one = (*sizefun) (next) - MINSIZE (next);
2666 if (this_one > delta)
2667 this_one = delta;
2668
2669 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
2670 (*setsizefun) (window, *sizep + this_one, 0);
2671
2672 delta -= this_one;
2673 next = XWINDOW (next)->next;
2674 }
2675 if (delta == 0)
2676 break;
2677 if (! NILP (prev))
2678 {
2679 int this_one = (*sizefun) (prev) - MINSIZE (prev);
2680 if (this_one > delta)
2681 this_one = delta;
2682
2683 first_affected = prev;
2684
2685 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
2686 (*setsizefun) (window, *sizep + this_one, 0);
2687
2688 delta -= this_one;
2689 prev = XWINDOW (prev)->prev;
2690 }
2691 }
2692
2693 /* Now recalculate the edge positions of all the windows affected,
2694 based on the new sizes. */
2695 first_unaffected = next;
2696 prev = first_affected;
2697 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
2698 prev = next, next = XWINDOW (next)->next)
2699 {
2700 CURBEG (next) = CURBEG (prev) + (*sizefun) (prev);
2701 /* This does not change size of NEXT,
2702 but it propagates the new top edge to its children */
2703 (*setsizefun) (next, (*sizefun) (next), 0);
2704 }
7ab12479
JB
2705 }
2706 else
2707 {
2708 register int delta1;
2709 register int opht = (*sizefun) (parent);
2710
2711 /* If trying to grow this window to or beyond size of the parent,
2712 make delta1 so big that, on shrinking back down,
2713 all the siblings end up with less than one line and are deleted. */
2714 if (opht <= *sizep + delta)
2715 delta1 = opht * opht * 2;
2716 /* Otherwise, make delta1 just right so that if we add delta1
2717 lines to this window and to the parent, and then shrink
2718 the parent back to its original size, the new proportional
2719 size of this window will increase by delta. */
2720 else
2721 delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
2722
2723 /* Add delta1 lines or columns to this window, and to the parent,
2724 keeping things consistent while not affecting siblings. */
05c2896a 2725 CURSIZE (parent) = opht + delta1;
7ab12479
JB
2726 (*setsizefun) (window, *sizep + delta1, 0);
2727
2728 /* Squeeze out delta1 lines or columns from our parent,
2729 shriking this window and siblings proportionately.
2730 This brings parent back to correct size.
2731 Delta1 was calculated so this makes this window the desired size,
2732 taking it all out of the siblings. */
2733 (*setsizefun) (parent, opht, 0);
2734 }
2735
d834a2e9 2736 XSETFASTINT (p->last_modified, 0);
3cd21523 2737 XSETFASTINT (p->last_overlay_modified, 0);
7ab12479
JB
2738}
2739#undef MINSIZE
2740#undef CURBEG
2741#undef CURSIZE
2742
2743\f
2744/* Return number of lines of text (not counting mode line) in W. */
2745
2746int
2747window_internal_height (w)
2748 struct window *w;
2749{
2750 int ht = XFASTINT (w->height);
2751
2752 if (MINI_WINDOW_P (w))
2753 return ht;
2754
265a9e55
JB
2755 if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
2756 || !NILP (w->next) || !NILP (w->prev)
44fa5b1e 2757 || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
7ab12479
JB
2758 return ht - 1;
2759
2760 return ht;
2761}
2762
535e0b8e
JB
2763
2764/* Return the number of columns in W.
a3c87d4e 2765 Don't count columns occupied by scroll bars or the vertical bar
535e0b8e
JB
2766 separating W from the sibling to its right. */
2767int
2768window_internal_width (w)
2769 struct window *w;
2770{
2771 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
535e0b8e
JB
2772 int width = XINT (w->width);
2773
a3c87d4e
JB
2774 /* Scroll bars occupy a few columns. */
2775 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
ca0d0bb9 2776 return width - FRAME_SCROLL_BAR_COLS (f);
535e0b8e
JB
2777
2778 /* The column of `|' characters separating side-by-side windows
2779 occupies one column only. */
111e5992
RS
2780 if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
2781 return width - 1;
2782
2783 return width;
535e0b8e
JB
2784}
2785
2786
101d1605 2787/* Scroll contents of window WINDOW up N lines.
9317a85d 2788 If WHOLE is nonzero, it means scroll N screenfuls instead. */
7ab12479 2789
101d1605
RS
2790static void
2791window_scroll (window, n, whole, noerror)
7ab12479
JB
2792 Lisp_Object window;
2793 int n;
101d1605 2794 int whole;
f8026fd8 2795 int noerror;
7ab12479
JB
2796{
2797 register struct window *w = XWINDOW (window);
5ce7b543 2798 register int opoint = PT;
cb8419b5 2799 register int opoint_byte = PT_BYTE;
b73ea88e 2800 register int pos, pos_byte;
7ab12479
JB
2801 register int ht = window_internal_height (w);
2802 register Lisp_Object tem;
2803 int lose;
2804 Lisp_Object bolp, nmoved;
345d45b2 2805 int startpos;
101d1605
RS
2806 struct position posit;
2807 int original_vpos;
2808
2809 startpos = marker_position (w->start);
2810
2811 posit = *compute_motion (startpos, 0, 0, 0,
2812 PT, ht, 0,
2813 window_internal_width (w), XINT (w->hscroll),
2814 0, w);
2815 original_vpos = posit.vpos;
0a1f771a 2816
d834a2e9 2817 XSETFASTINT (tem, PT);
7ab12479
JB
2818 tem = Fpos_visible_in_window_p (tem, window);
2819
265a9e55 2820 if (NILP (tem))
7ab12479 2821 {
cd2be1dd 2822 Fvertical_motion (make_number (- (ht / 2)), window);
345d45b2 2823 startpos = PT;
7ab12479
JB
2824 }
2825
345d45b2 2826 SET_PT (startpos);
5ce7b543 2827 lose = n < 0 && PT == BEGV;
540b6aa0 2828 Fvertical_motion (make_number (n), window);
5ce7b543 2829 pos = PT;
b73ea88e 2830 pos_byte = PT_BYTE;
7ab12479 2831 bolp = Fbolp ();
b73ea88e 2832 SET_PT_BOTH (opoint, opoint_byte);
7ab12479
JB
2833
2834 if (lose)
f8026fd8
JB
2835 {
2836 if (noerror)
2837 return;
2838 else
2839 Fsignal (Qbeginning_of_buffer, Qnil);
2840 }
7ab12479
JB
2841
2842 if (pos < ZV)
7ab12479 2843 {
0c7da84e
RS
2844 int this_scroll_margin = scroll_margin;
2845
2846 /* Don't use a scroll margin that is negative or too large. */
2847 if (this_scroll_margin < 0)
2848 this_scroll_margin = 0;
2849
2850 if (XINT (w->height) < 4 * scroll_margin)
2851 this_scroll_margin = XINT (w->height) / 4;
2852
b73ea88e 2853 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
7ab12479
JB
2854 w->start_at_line_beg = bolp;
2855 w->update_mode_line = Qt;
d834a2e9 2856 XSETFASTINT (w->last_modified, 0);
3cd21523 2857 XSETFASTINT (w->last_overlay_modified, 0);
345d45b2
RS
2858 /* Set force_start so that redisplay_window will run
2859 the window-scroll-functions. */
2860 w->force_start = Qt;
0c7da84e 2861
9317a85d 2862 if (whole && scroll_preserve_screen_position)
0c7da84e 2863 {
b73ea88e 2864 SET_PT_BOTH (pos, pos_byte);
101d1605 2865 Fvertical_motion (make_number (original_vpos), window);
0c7da84e 2866 }
101d1605
RS
2867 /* If we scrolled forward, put point enough lines down
2868 that it is outside the scroll margin. */
2869 else if (n > 0)
0c7da84e 2870 {
101d1605
RS
2871 int top_margin;
2872
2873 if (this_scroll_margin > 0)
2874 {
b73ea88e 2875 SET_PT_BOTH (pos, pos_byte);
101d1605
RS
2876 Fvertical_motion (make_number (this_scroll_margin), window);
2877 top_margin = PT;
2878 }
2879 else
2880 top_margin = pos;
2881
2882 if (top_margin <= opoint)
b73ea88e 2883 SET_PT_BOTH (opoint, opoint_byte);
9317a85d 2884 else if (scroll_preserve_screen_position)
101d1605 2885 {
b73ea88e 2886 SET_PT_BOTH (pos, pos_byte);
101d1605
RS
2887 Fvertical_motion (make_number (original_vpos), window);
2888 }
9317a85d 2889 else
335406fc 2890 SET_PT (top_margin);
0c7da84e 2891 }
101d1605 2892 else if (n < 0)
7ab12479 2893 {
101d1605
RS
2894 int bottom_margin;
2895
0c7da84e
RS
2896 /* If we scrolled backward, put point near the end of the window
2897 but not within the scroll margin. */
b73ea88e 2898 SET_PT_BOTH (pos, pos_byte);
0c7da84e 2899 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
101d1605
RS
2900 if (XFASTINT (tem) == ht - this_scroll_margin)
2901 bottom_margin = PT;
2902 else
2903 bottom_margin = PT + 1;
2904
2905 if (bottom_margin > opoint)
b73ea88e 2906 SET_PT_BOTH (opoint, opoint_byte);
7ab12479 2907 else
101d1605 2908 {
9317a85d
RS
2909 if (scroll_preserve_screen_position)
2910 {
b73ea88e 2911 SET_PT_BOTH (pos, pos_byte);
9317a85d
RS
2912 Fvertical_motion (make_number (original_vpos), window);
2913 }
2914 else
2915 Fvertical_motion (make_number (-1), window);
101d1605 2916 }
7ab12479
JB
2917 }
2918 }
2919 else
f8026fd8
JB
2920 {
2921 if (noerror)
2922 return;
2923 else
2924 Fsignal (Qend_of_buffer, Qnil);
2925 }
7ab12479
JB
2926}
2927\f
2928/* This is the guts of Fscroll_up and Fscroll_down. */
2929
2930static void
2931scroll_command (n, direction)
2932 register Lisp_Object n;
2933 int direction;
2934{
2935 register int defalt;
2936 int count = specpdl_ptr - specpdl;
2937
95605e15
JB
2938 /* If selected window's buffer isn't current, make it current for the moment.
2939 But don't screw up if window_scroll gets an error. */
7ab12479 2940 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
95605e15
JB
2941 {
2942 record_unwind_protect (save_excursion_restore, save_excursion_save ());
2943 Fset_buffer (XWINDOW (selected_window)->buffer);
2944 }
7ab12479
JB
2945
2946 defalt = (window_internal_height (XWINDOW (selected_window))
2947 - next_screen_context_lines);
2948 defalt = direction * (defalt < 1 ? 1 : defalt);
2949
265a9e55 2950 if (NILP (n))
101d1605 2951 window_scroll (selected_window, defalt, 1, 0);
7ab12479 2952 else if (EQ (n, Qminus))
101d1605 2953 window_scroll (selected_window, - defalt, 1, 0);
7ab12479
JB
2954 else
2955 {
2956 n = Fprefix_numeric_value (n);
101d1605 2957 window_scroll (selected_window, XINT (n) * direction, 0, 0);
7ab12479 2958 }
95605e15
JB
2959
2960 unbind_to (count, Qnil);
7ab12479
JB
2961}
2962
2963DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
2964 "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
2965A near full screen is `next-screen-context-lines' less than a full screen.\n\
279e0e0c 2966Negative ARG means scroll downward.\n\
7ab12479 2967When calling from a program, supply a number as argument or nil.")
413430c5
EN
2968 (arg)
2969 Lisp_Object arg;
7ab12479 2970{
413430c5 2971 scroll_command (arg, 1);
7ab12479
JB
2972 return Qnil;
2973}
2974
2975DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
2976 "Scroll text of current window downward ARG lines; or near full screen if no ARG.\n\
2977A near full screen is `next-screen-context-lines' less than a full screen.\n\
279e0e0c 2978Negative ARG means scroll upward.\n\
7ab12479 2979When calling from a program, supply a number as argument or nil.")
413430c5
EN
2980 (arg)
2981 Lisp_Object arg;
7ab12479 2982{
413430c5 2983 scroll_command (arg, -1);
7ab12479
JB
2984 return Qnil;
2985}
ccd0664b
RS
2986\f
2987DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
2988 "Return the other window for \"other window scroll\" commands.\n\
77b24de6 2989If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
ccd0664b
RS
2990specifies the window.\n\
2991If `other-window-scroll-buffer' is non-nil, a window\n\
2992showing that buffer is used.")
eb16ec06 2993 ()
7ab12479 2994{
ccd0664b 2995 Lisp_Object window;
7ab12479
JB
2996
2997 if (MINI_WINDOW_P (XWINDOW (selected_window))
265a9e55 2998 && !NILP (Vminibuf_scroll_window))
7ab12479
JB
2999 window = Vminibuf_scroll_window;
3000 /* If buffer is specified, scroll that buffer. */
265a9e55 3001 else if (!NILP (Vother_window_scroll_buffer))
7ab12479
JB
3002 {
3003 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
265a9e55 3004 if (NILP (window))
53f76081 3005 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
7ab12479
JB
3006 }
3007 else
dbc4e1c1
JB
3008 {
3009 /* Nothing specified; look for a neighboring window on the same
3010 frame. */
3011 window = Fnext_window (selected_window, Qnil, Qnil);
3012
3013 if (EQ (window, selected_window))
3014 /* That didn't get us anywhere; look for a window on another
3015 visible frame. */
3016 do
3017 window = Fnext_window (window, Qnil, Qt);
3018 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
3019 && ! EQ (window, selected_window));
3020 }
3021
605be8af 3022 CHECK_LIVE_WINDOW (window, 0);
7ab12479
JB
3023
3024 if (EQ (window, selected_window))
3025 error ("There is no other window");
3026
ccd0664b
RS
3027 return window;
3028}
3029
3030DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
3031 "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
3032The next window is the one below the current one; or the one at the top\n\
3033if the current one is at the bottom. Negative ARG means scroll downward.\n\
3034When calling from a program, supply a number as argument or nil.\n\
3035\n\
3036If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
3037specifies the window to scroll.\n\
3038If `other-window-scroll-buffer' is non-nil, scroll the window\n\
3039showing that buffer, popping the buffer up if necessary.")
413430c5
EN
3040 (arg)
3041 register Lisp_Object arg;
ccd0664b
RS
3042{
3043 register Lisp_Object window;
2f787aa3 3044 register int defalt;
ccd0664b
RS
3045 register struct window *w;
3046 register int count = specpdl_ptr - specpdl;
3047
3048 window = Fother_window_for_scrolling ();
3049
7ab12479 3050 w = XWINDOW (window);
2f787aa3
KH
3051 defalt = window_internal_height (w) - next_screen_context_lines;
3052 if (defalt < 1) defalt = 1;
7ab12479
JB
3053
3054 /* Don't screw up if window_scroll gets an error. */
3055 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3056
3057 Fset_buffer (w->buffer);
3058 SET_PT (marker_position (w->pointm));
3059
413430c5 3060 if (NILP (arg))
101d1605 3061 window_scroll (window, defalt, 1, 1);
413430c5 3062 else if (EQ (arg, Qminus))
101d1605 3063 window_scroll (window, -defalt, 1, 1);
7ab12479
JB
3064 else
3065 {
413430c5
EN
3066 if (CONSP (arg))
3067 arg = Fcar (arg);
3068 CHECK_NUMBER (arg, 0);
101d1605 3069 window_scroll (window, XINT (arg), 0, 1);
7ab12479
JB
3070 }
3071
b73ea88e 3072 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
f4e7b2c2 3073 unbind_to (count, Qnil);
7ab12479
JB
3074
3075 return Qnil;
3076}
3077\f
644b477c 3078DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
7ab12479
JB
3079 "Scroll selected window display ARG columns left.\n\
3080Default for ARG is window width minus 2.")
3081 (arg)
3082 register Lisp_Object arg;
3083{
3084
265a9e55 3085 if (NILP (arg))
d834a2e9 3086 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
7ab12479
JB
3087 else
3088 arg = Fprefix_numeric_value (arg);
3089
3090 return
3091 Fset_window_hscroll (selected_window,
3092 make_number (XINT (XWINDOW (selected_window)->hscroll)
3093 + XINT (arg)));
3094}
3095
644b477c 3096DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
7ab12479
JB
3097 "Scroll selected window display ARG columns right.\n\
3098Default for ARG is window width minus 2.")
3099 (arg)
3100 register Lisp_Object arg;
3101{
265a9e55 3102 if (NILP (arg))
d834a2e9 3103 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
7ab12479
JB
3104 else
3105 arg = Fprefix_numeric_value (arg);
3106
3107 return
3108 Fset_window_hscroll (selected_window,
3109 make_number (XINT (XWINDOW (selected_window)->hscroll)
3110 - XINT (arg)));
3111}
3112
3113DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
44fa5b1e 3114 "Center point in window and redisplay frame. With ARG, put point on line ARG.\n\
7ab12479 3115The desired position of point is always relative to the current window.\n\
44fa5b1e 3116Just C-u as prefix means put point in the center of the window.\n\
413430c5 3117If ARG is omitted or nil, erases the entire frame and then\n\
44fa5b1e 3118redraws with point in the center of the current window.")
413430c5
EN
3119 (arg)
3120 register Lisp_Object arg;
7ab12479
JB
3121{
3122 register struct window *w = XWINDOW (selected_window);
3123 register int ht = window_internal_height (w);
113d9015 3124 struct position pos;
478292ed
RS
3125 struct buffer *buf = XBUFFER (w->buffer);
3126 struct buffer *obuf = current_buffer;
7ab12479 3127
413430c5 3128 if (NILP (arg))
7ab12479 3129 {
44fa5b1e 3130 extern int frame_garbaged;
7ab12479 3131
44fa5b1e 3132 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
413430c5 3133 XSETFASTINT (arg, ht / 2);
7ab12479 3134 }
413430c5 3135 else if (CONSP (arg)) /* Just C-u. */
7ab12479 3136 {
413430c5 3137 XSETFASTINT (arg, ht / 2);
7ab12479
JB
3138 }
3139 else
3140 {
413430c5
EN
3141 arg = Fprefix_numeric_value (arg);
3142 CHECK_NUMBER (arg, 0);
7ab12479
JB
3143 }
3144
413430c5
EN
3145 if (XINT (arg) < 0)
3146 XSETINT (arg, XINT (arg) + ht);
7ab12479 3147
478292ed 3148 set_buffer_internal (buf);
6ec8bbd2 3149 pos = *vmotion (PT, - XINT (arg), w);
7ab12479 3150
b73ea88e
RS
3151 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
3152 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
3153 || FETCH_BYTE (pos.bytepos - 1) == '\n')
113d9015 3154 ? Qt : Qnil);
7ab12479 3155 w->force_start = Qt;
478292ed 3156 set_buffer_internal (obuf);
7ab12479
JB
3157
3158 return Qnil;
3159}
3160\f
3161DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
3162 1, 1, "P",
3163 "Position point relative to window.\n\
19e3bf0a 3164With no argument, position point at center of window.\n\
44fa5b1e 3165An argument specifies frame line; zero means top of window,\n\
7ab12479
JB
3166negative means relative to bottom of window.")
3167 (arg)
3168 register Lisp_Object arg;
3169{
3170 register struct window *w = XWINDOW (selected_window);
3171 register int height = window_internal_height (w);
3172 register int start;
540b6aa0 3173 Lisp_Object window;
7ab12479 3174
265a9e55 3175 if (NILP (arg))
d834a2e9 3176 XSETFASTINT (arg, height / 2);
7ab12479
JB
3177 else
3178 {
3179 arg = Fprefix_numeric_value (arg);
3180 if (XINT (arg) < 0)
3181 XSETINT (arg, XINT (arg) + height);
3182 }
3183
3184 start = marker_position (w->start);
74112613 3185 XSETWINDOW (window, w);
7ab12479
JB
3186 if (start < BEGV || start > ZV)
3187 {
cd2be1dd 3188 Fvertical_motion (make_number (- (height / 2)), window);
b73ea88e 3189 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
7ab12479
JB
3190 w->start_at_line_beg = Fbolp ();
3191 w->force_start = Qt;
3192 }
3193 else
b73ea88e 3194 Fgoto_char (w->start);
7ab12479 3195
540b6aa0 3196 return Fvertical_motion (arg, window);
7ab12479
JB
3197}
3198\f
3199struct save_window_data
3200 {
f5ccc0cc 3201 EMACS_INT size_from_Lisp_Vector_struct;
7ab12479 3202 struct Lisp_Vector *next_from_Lisp_Vector_struct;
8f6ea2e9 3203 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
bdc727bf 3204 Lisp_Object selected_frame;
7ab12479
JB
3205 Lisp_Object current_window;
3206 Lisp_Object current_buffer;
3207 Lisp_Object minibuf_scroll_window;
3208 Lisp_Object root_window;
bdc727bf 3209 Lisp_Object focus_frame;
756b6edc
RS
3210 /* Record the values of window-min-width and window-min-height
3211 so that window sizes remain consistent with them. */
3212 Lisp_Object min_width, min_height;
cbff28e8
RS
3213 /* A vector, each of whose elements is a struct saved_window
3214 for one window. */
7ab12479
JB
3215 Lisp_Object saved_windows;
3216 };
ff06df24 3217
cbff28e8 3218/* This is saved as a Lisp_Vector */
7ab12479
JB
3219struct saved_window
3220 {
3221 /* these first two must agree with struct Lisp_Vector in lisp.h */
f5ccc0cc 3222 EMACS_INT size_from_Lisp_Vector_struct;
7ab12479
JB
3223 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3224
3225 Lisp_Object window;
3226 Lisp_Object buffer, start, pointm, mark;
3227 Lisp_Object left, top, width, height, hscroll;
3228 Lisp_Object parent, prev;
3229 Lisp_Object start_at_line_beg;
3230 Lisp_Object display_table;
3231 };
3232#define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
3233
3234#define SAVED_WINDOW_N(swv,n) \
3235 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
3236
3237DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
cbff28e8 3238 "Return t if OBJECT is a window-configuration object.")
413430c5
EN
3239 (object)
3240 Lisp_Object object;
7ab12479 3241{
413430c5 3242 if (WINDOW_CONFIGURATIONP (object))
7ab12479
JB
3243 return Qt;
3244 return Qnil;
3245}
3246
d5b2799e
RS
3247DEFUN ("set-window-configuration", Fset_window_configuration,
3248 Sset_window_configuration, 1, 1, 0,
7ab12479
JB
3249 "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
3250CONFIGURATION must be a value previously returned\n\
3251by `current-window-configuration' (which see).")
2f83aebe
JB
3252 (configuration)
3253 Lisp_Object configuration;
7ab12479 3254{
7ab12479
JB
3255 register struct save_window_data *data;
3256 struct Lisp_Vector *saved_windows;
7ab12479 3257 Lisp_Object new_current_buffer;
fd482be5 3258 Lisp_Object frame;
44fa5b1e 3259 FRAME_PTR f;
d2b35234 3260 int old_point = -1;
7ab12479 3261
017b2bad 3262 while (!WINDOW_CONFIGURATIONP (configuration))
7ab12479 3263 {
2f83aebe
JB
3264 configuration = wrong_type_argument (intern ("window-configuration-p"),
3265 configuration);
7ab12479
JB
3266 }
3267
2f83aebe 3268 data = (struct save_window_data *) XVECTOR (configuration);
7ab12479
JB
3269 saved_windows = XVECTOR (data->saved_windows);
3270
7ab12479 3271 new_current_buffer = data->current_buffer;
265a9e55 3272 if (NILP (XBUFFER (new_current_buffer)->name))
7ab12479 3273 new_current_buffer = Qnil;
d2b35234
RS
3274 else
3275 {
3276 if (XBUFFER (new_current_buffer) == current_buffer)
3277 old_point = PT;
cbff28e8 3278
d2b35234 3279 }
7ab12479 3280
fd482be5
JB
3281 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3282 f = XFRAME (frame);
9ace597f 3283
fd482be5
JB
3284 /* If f is a dead frame, don't bother rebuilding its window tree.
3285 However, there is other stuff we should still try to do below. */
3286 if (FRAME_LIVE_P (f))
7ab12479 3287 {
fd482be5
JB
3288 register struct window *w;
3289 register struct saved_window *p;
3290 int k;
3291
3292 /* If the frame has been resized since this window configuration was
3293 made, we change the frame to the size specified in the
3294 configuration, restore the configuration, and then resize it
3295 back. We keep track of the prevailing height in these variables. */
3296 int previous_frame_height = FRAME_HEIGHT (f);
3297 int previous_frame_width = FRAME_WIDTH (f);
8f6ea2e9 3298 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
fd482be5 3299
d2b35234
RS
3300 /* The mouse highlighting code could get screwed up
3301 if it runs during this. */
3302 BLOCK_INPUT;
3303
fd482be5
JB
3304 if (XFASTINT (data->frame_height) != previous_frame_height
3305 || XFASTINT (data->frame_width) != previous_frame_width)
3306 change_frame_size (f, data->frame_height, data->frame_width, 0, 0);
e3678b64 3307#if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
8f6ea2e9
KH
3308 if (XFASTINT (data->frame_menu_bar_lines)
3309 != previous_frame_menu_bar_lines)
3310 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, 0);
217f2871 3311#endif
fd482be5 3312
596ae0cf
RS
3313 if (! NILP (XWINDOW (selected_window)->buffer))
3314 {
3315 w = XWINDOW (selected_window);
3316 set_marker_both (w->pointm,
3317 w->buffer,
3318 BUF_PT (XBUFFER (w->buffer)),
3319 BUF_PT_BYTE (XBUFFER (w->buffer)));
3320 }
3321
fd482be5 3322 windows_or_buffers_changed++;
29aeee73 3323 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
fd482be5 3324
756b6edc
RS
3325 /* Temporarily avoid any problems with windows that are smaller
3326 than they are supposed to be. */
3327 window_min_height = 1;
3328 window_min_width = 1;
3329
fd482be5
JB
3330 /* Kludge Alert!
3331 Mark all windows now on frame as "deleted".
3332 Restoring the new configuration "undeletes" any that are in it.
37962e60 3333
fd482be5
JB
3334 Save their current buffers in their height fields, since we may
3335 need it later, if a buffer saved in the configuration is now
3336 dead. */
3337 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3338
3339 for (k = 0; k < saved_windows->size; k++)
3340 {
3341 p = SAVED_WINDOW_N (saved_windows, k);
3342 w = XWINDOW (p->window);
3343 w->next = Qnil;
7ab12479 3344
fd482be5
JB
3345 if (!NILP (p->parent))
3346 w->parent = SAVED_WINDOW_N (saved_windows,
3347 XFASTINT (p->parent))->window;
3348 else
3349 w->parent = Qnil;
7ab12479 3350
fd482be5 3351 if (!NILP (p->prev))
7ab12479 3352 {
fd482be5
JB
3353 w->prev = SAVED_WINDOW_N (saved_windows,
3354 XFASTINT (p->prev))->window;
3355 XWINDOW (w->prev)->next = p->window;
3356 }
3357 else
3358 {
3359 w->prev = Qnil;
3360 if (!NILP (w->parent))
3361 {
3362 if (EQ (p->width, XWINDOW (w->parent)->width))
3363 {
3364 XWINDOW (w->parent)->vchild = p->window;
3365 XWINDOW (w->parent)->hchild = Qnil;
3366 }
3367 else
3368 {
3369 XWINDOW (w->parent)->hchild = p->window;
3370 XWINDOW (w->parent)->vchild = Qnil;
3371 }
3372 }
3373 }
3374
3375 /* If we squirreled away the buffer in the window's height,
3376 restore it now. */
017b2bad 3377 if (BUFFERP (w->height))
fd482be5
JB
3378 w->buffer = w->height;
3379 w->left = p->left;
3380 w->top = p->top;
3381 w->width = p->width;
3382 w->height = p->height;
3383 w->hscroll = p->hscroll;
3384 w->display_table = p->display_table;
d834a2e9 3385 XSETFASTINT (w->last_modified, 0);
3cd21523 3386 XSETFASTINT (w->last_overlay_modified, 0);
fd482be5
JB
3387
3388 /* Reinstall the saved buffer and pointers into it. */
3389 if (NILP (p->buffer))
3390 w->buffer = p->buffer;
3391 else
3392 {
3393 if (!NILP (XBUFFER (p->buffer)->name))
3394 /* If saved buffer is alive, install it. */
3395 {
3396 w->buffer = p->buffer;
3397 w->start_at_line_beg = p->start_at_line_beg;
b73ea88e
RS
3398 set_marker_restricted (w->start, p->start, w->buffer);
3399 set_marker_restricted (w->pointm, p->pointm, w->buffer);
fd482be5 3400 Fset_marker (XBUFFER (w->buffer)->mark,
b73ea88e 3401 p->mark, w->buffer);
fd482be5
JB
3402
3403 /* As documented in Fcurrent_window_configuration, don't
3404 save the location of point in the buffer which was current
3405 when the window configuration was recorded. */
6b54027b
RS
3406 if (!EQ (p->buffer, new_current_buffer)
3407 && XBUFFER (p->buffer) == current_buffer)
fd482be5
JB
3408 Fgoto_char (w->pointm);
3409 }
52a68e98
RS
3410 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
3411 /* Else unless window has a live buffer, get one. */
7ab12479 3412 {
fd482be5
JB
3413 w->buffer = Fcdr (Fcar (Vbuffer_alist));
3414 /* This will set the markers to beginning of visible
3415 range. */
3416 set_marker_restricted (w->start, make_number (0), w->buffer);
3417 set_marker_restricted (w->pointm, make_number (0),w->buffer);
3418 w->start_at_line_beg = Qt;
7ab12479
JB
3419 }
3420 else
fd482be5 3421 /* Keeping window's old buffer; make sure the markers
52a68e98 3422 are real. */
7ab12479 3423 {
fd482be5
JB
3424 /* Set window markers at start of visible range. */
3425 if (XMARKER (w->start)->buffer == 0)
3426 set_marker_restricted (w->start, make_number (0),
3427 w->buffer);
3428 if (XMARKER (w->pointm)->buffer == 0)
b73ea88e
RS
3429 set_marker_restricted_both (w->pointm, w->buffer,
3430 BUF_PT (XBUFFER (w->buffer)),
3431 BUF_PT_BYTE (XBUFFER (w->buffer)));
fd482be5 3432 w->start_at_line_beg = Qt;
7ab12479
JB
3433 }
3434 }
3435 }
9ace597f 3436
fd482be5
JB
3437 FRAME_ROOT_WINDOW (f) = data->root_window;
3438 Fselect_window (data->current_window);
396a830c
RS
3439 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
3440 = selected_window;
7ab12479 3441
db269683 3442 if (NILP (data->focus_frame)
017b2bad 3443 || (FRAMEP (data->focus_frame)
db269683
JB
3444 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
3445 Fredirect_frame_focus (frame, data->focus_frame);
7ab12479 3446
fd482be5
JB
3447#if 0 /* I don't understand why this is needed, and it causes problems
3448 when the frame's old selected window has been deleted. */
e4e59717 3449 if (f != selected_frame && FRAME_WINDOW_P (f))
9a7c6fc3
RS
3450 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
3451 Qnil, 0);
fd482be5
JB
3452#endif
3453
3454 /* Set the screen height to the value it had before this function. */
3455 if (previous_frame_height != FRAME_HEIGHT (f)
3456 || previous_frame_width != FRAME_WIDTH (f))
3457 change_frame_size (f, previous_frame_height, previous_frame_width,
3458 0, 0);
e3678b64 3459#if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
8f6ea2e9
KH
3460 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
3461 x_set_menu_bar_lines (f, previous_frame_menu_bar_lines, 0);
217f2871 3462#endif
d2b35234
RS
3463
3464 UNBLOCK_INPUT;
756b6edc 3465
478292ed
RS
3466 /* Fselect_window will have made f the selected frame, so we
3467 reselect the proper frame here. Fhandle_switch_frame will change the
3468 selected window too, but that doesn't make the call to
3469 Fselect_window above totally superfluous; it still sets f's
3470 selected window. */
3471 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
3472 do_switch_frame (data->selected_frame, Qnil, 0);
3473
3474 if (! NILP (Vwindow_configuration_change_hook)
3475 && ! NILP (Vrun_hooks))
3476 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3477 }
bdc727bf
JB
3478
3479 if (!NILP (new_current_buffer))
d2b35234
RS
3480 {
3481 Fset_buffer (new_current_buffer);
3482
3483 /* If the buffer that is current now is the same
3484 that was current before setting the window configuration,
3485 don't alter its PT. */
3486 if (old_point >= 0)
3487 SET_PT (old_point);
3488 }
bdc727bf 3489
478292ed
RS
3490 /* Restore the minimum heights recorded in the configuration. */
3491 window_min_height = XINT (data->min_height);
3492 window_min_width = XINT (data->min_width);
543f5fb1 3493
478292ed 3494 Vminibuf_scroll_window = data->minibuf_scroll_window;
543f5fb1 3495
478292ed 3496 return Qnil;
7ab12479
JB
3497}
3498
44fa5b1e 3499/* Mark all windows now on frame as deleted
7ab12479
JB
3500 by setting their buffers to nil. */
3501
fd482be5 3502void
7ab12479
JB
3503delete_all_subwindows (w)
3504 register struct window *w;
3505{
265a9e55 3506 if (!NILP (w->next))
7ab12479 3507 delete_all_subwindows (XWINDOW (w->next));
265a9e55 3508 if (!NILP (w->vchild))
7ab12479 3509 delete_all_subwindows (XWINDOW (w->vchild));
265a9e55 3510 if (!NILP (w->hchild))
7ab12479 3511 delete_all_subwindows (XWINDOW (w->hchild));
605be8af
JB
3512
3513 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
3514
86e48436
RS
3515 if (!NILP (w->buffer))
3516 unshow_buffer (w);
3517
605be8af
JB
3518 /* We set all three of these fields to nil, to make sure that we can
3519 distinguish this dead window from any live window. Live leaf
3520 windows will have buffer set, and combination windows will have
3521 vchild or hchild set. */
3522 w->buffer = Qnil;
3523 w->vchild = Qnil;
3524 w->hchild = Qnil;
7ab12479
JB
3525}
3526\f
3527static int
3528count_windows (window)
3529 register struct window *window;
3530{
3531 register int count = 1;
265a9e55 3532 if (!NILP (window->next))
7ab12479 3533 count += count_windows (XWINDOW (window->next));
265a9e55 3534 if (!NILP (window->vchild))
7ab12479 3535 count += count_windows (XWINDOW (window->vchild));
265a9e55 3536 if (!NILP (window->hchild))
7ab12479
JB
3537 count += count_windows (XWINDOW (window->hchild));
3538 return count;
3539}
3540
3541static int
3542save_window_save (window, vector, i)
3543 Lisp_Object window;
3544 struct Lisp_Vector *vector;
3545 int i;
3546{
3547 register struct saved_window *p;
3548 register struct window *w;
3549 register Lisp_Object tem;
3550
265a9e55 3551 for (;!NILP (window); window = w->next)
7ab12479
JB
3552 {
3553 p = SAVED_WINDOW_N (vector, i);
3554 w = XWINDOW (window);
3555
d834a2e9 3556 XSETFASTINT (w->temslot, i++);
7ab12479
JB
3557 p->window = window;
3558 p->buffer = w->buffer;
3559 p->left = w->left;
3560 p->top = w->top;
3561 p->width = w->width;
3562 p->height = w->height;
3563 p->hscroll = w->hscroll;
3564 p->display_table = w->display_table;
265a9e55 3565 if (!NILP (w->buffer))
7ab12479
JB
3566 {
3567 /* Save w's value of point in the window configuration.
3568 If w is the selected window, then get the value of point
3569 from the buffer; pointm is garbage in the selected window. */
3570 if (EQ (window, selected_window))
3571 {
3572 p->pointm = Fmake_marker ();
b73ea88e
RS
3573 set_marker_both (p->pointm, w->buffer,
3574 BUF_PT (XBUFFER (w->buffer)),
3575 BUF_PT_BYTE (XBUFFER (w->buffer)));
7ab12479
JB
3576 }
3577 else
eeb82665 3578 p->pointm = Fcopy_marker (w->pointm, Qnil);
7ab12479 3579
eeb82665 3580 p->start = Fcopy_marker (w->start, Qnil);
7ab12479
JB
3581 p->start_at_line_beg = w->start_at_line_beg;
3582
3583 tem = XBUFFER (w->buffer)->mark;
eeb82665 3584 p->mark = Fcopy_marker (tem, Qnil);
7ab12479
JB
3585 }
3586 else
3587 {
3588 p->pointm = Qnil;
3589 p->start = Qnil;
3590 p->mark = Qnil;
3591 p->start_at_line_beg = Qnil;
3592 }
3593
265a9e55 3594 if (NILP (w->parent))
7ab12479
JB
3595 p->parent = Qnil;
3596 else
3597 p->parent = XWINDOW (w->parent)->temslot;
3598
265a9e55 3599 if (NILP (w->prev))
7ab12479
JB
3600 p->prev = Qnil;
3601 else
3602 p->prev = XWINDOW (w->prev)->temslot;
3603
265a9e55 3604 if (!NILP (w->vchild))
7ab12479 3605 i = save_window_save (w->vchild, vector, i);
265a9e55 3606 if (!NILP (w->hchild))
7ab12479
JB
3607 i = save_window_save (w->hchild, vector, i);
3608 }
3609
3610 return i;
3611}
3612
a0d76c27
EN
3613DEFUN ("current-window-configuration", Fcurrent_window_configuration,
3614 Scurrent_window_configuration, 0, 1, 0,
44fa5b1e
JB
3615 "Return an object representing the current window configuration of FRAME.\n\
3616If FRAME is nil or omitted, use the selected frame.\n\
7ab12479
JB
3617This describes the number of windows, their sizes and current buffers,\n\
3618and for each displayed buffer, where display starts, and the positions of\n\
3619point and mark. An exception is made for point in the current buffer:\n\
bdc727bf
JB
3620its value is -not- saved.\n\
3621This also records the currently selected frame, and FRAME's focus\n\
3622redirection (see `redirect-frame-focus').")
44fa5b1e
JB
3623 (frame)
3624 Lisp_Object frame;
7ab12479
JB
3625{
3626 register Lisp_Object tem;
3627 register int n_windows;
3628 register struct save_window_data *data;
da2792e0 3629 register struct Lisp_Vector *vec;
7ab12479 3630 register int i;
44fa5b1e 3631 FRAME_PTR f;
43bad991 3632
44fa5b1e
JB
3633 if (NILP (frame))
3634 f = selected_frame;
43bad991
JB
3635 else
3636 {
44fa5b1e
JB
3637 CHECK_LIVE_FRAME (frame, 0);
3638 f = XFRAME (frame);
43bad991 3639 }
7ab12479 3640
44fa5b1e 3641 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
da2792e0
KH
3642 vec = allocate_vectorlike (VECSIZE (struct save_window_data));
3643 for (i = 0; i < VECSIZE (struct save_window_data); i++)
3644 vec->contents[i] = Qnil;
3645 vec->size = VECSIZE (struct save_window_data);
3646 data = (struct save_window_data *)vec;
3647
d834a2e9
KH
3648 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
3649 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
3650 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
74112613 3651 XSETFRAME (data->selected_frame, selected_frame);
44fa5b1e 3652 data->current_window = FRAME_SELECTED_WINDOW (f);
74112613 3653 XSETBUFFER (data->current_buffer, current_buffer);
7ab12479 3654 data->minibuf_scroll_window = Vminibuf_scroll_window;
44fa5b1e 3655 data->root_window = FRAME_ROOT_WINDOW (f);
bdc727bf 3656 data->focus_frame = FRAME_FOCUS_FRAME (f);
74112613
KH
3657 XSETINT (data->min_height, window_min_height);
3658 XSETINT (data->min_width, window_min_width);
7ab12479
JB
3659 tem = Fmake_vector (make_number (n_windows), Qnil);
3660 data->saved_windows = tem;
3661 for (i = 0; i < n_windows; i++)
3662 XVECTOR (tem)->contents[i]
3663 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
44fa5b1e 3664 save_window_save (FRAME_ROOT_WINDOW (f),
7ab12479 3665 XVECTOR (tem), 0);
74112613 3666 XSETWINDOW_CONFIGURATION (tem, data);
7ab12479
JB
3667 return (tem);
3668}
3669
3670DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
3671 0, UNEVALLED, 0,
3672 "Execute body, preserving window sizes and contents.\n\
eb16ec06
RS
3673Restore which buffer appears in which window, where display starts,\n\
3674and the value of point and mark for each window.\n\
3675Also restore which buffer is current.\n\
3676But do not preserve point in the current buffer.\n\
7ab12479
JB
3677Does not restore the value of point in current buffer.")
3678 (args)
3679 Lisp_Object args;
3680{
3681 register Lisp_Object val;
3682 register int count = specpdl_ptr - specpdl;
3683
3684 record_unwind_protect (Fset_window_configuration,
43bad991 3685 Fcurrent_window_configuration (Qnil));
7ab12479
JB
3686 val = Fprogn (args);
3687 return unbind_to (count, val);
3688}
3689\f
cbff28e8
RS
3690/* Return 1 if window configurations C1 and C2
3691 describe the same state of affairs. This is used by Fequal. */
3692
3693int
2f8274be 3694compare_window_configurations (c1, c2, ignore_positions)
cbff28e8 3695 Lisp_Object c1, c2;
2f8274be 3696 int ignore_positions;
cbff28e8
RS
3697{
3698 register struct save_window_data *d1, *d2;
3699 struct Lisp_Vector *sw1, *sw2;
3700 int i;
3701
3702 d1 = (struct save_window_data *) XVECTOR (c1);
3703 d2 = (struct save_window_data *) XVECTOR (c2);
3704 sw1 = XVECTOR (d1->saved_windows);
3705 sw2 = XVECTOR (d2->saved_windows);
3706
3707 if (! EQ (d1->frame_width, d2->frame_width))
3708 return 0;
3709 if (! EQ (d1->frame_height, d2->frame_height))
3710 return 0;
3711 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
3712 return 0;
3713 if (! EQ (d1->selected_frame, d2->selected_frame))
3714 return 0;
3715 /* Don't compare the current_window field directly.
3716 Instead see w1_is_current and w2_is_current, below. */
3717 if (! EQ (d1->current_buffer, d2->current_buffer))
3718 return 0;
2f8274be
RS
3719 if (! ignore_positions)
3720 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
3721 return 0;
cbff28e8
RS
3722 /* Don't compare the root_window field.
3723 We don't require the two configurations
3724 to use the same window object,
3725 and the two root windows must be equivalent
3726 if everything else compares equal. */
3727 if (! EQ (d1->focus_frame, d2->focus_frame))
3728 return 0;
3729 if (! EQ (d1->min_width, d2->min_width))
3730 return 0;
3731 if (! EQ (d1->min_height, d2->min_height))
3732 return 0;
3733
3734 /* Verify that the two confis have the same number of windows. */
3735 if (sw1->size != sw2->size)
3736 return 0;
3737
3738 for (i = 0; i < sw1->size; i++)
3739 {
3740 struct saved_window *p1, *p2;
3741 int w1_is_current, w2_is_current;
3742
3743 p1 = SAVED_WINDOW_N (sw1, i);
3744 p2 = SAVED_WINDOW_N (sw2, i);
3745
3746 /* Verify that the current windows in the two
3747 configurations correspond to each other. */
3748 w1_is_current = EQ (d1->current_window, p1->window);
3749 w2_is_current = EQ (d2->current_window, p2->window);
3750
3751 if (w1_is_current != w2_is_current)
3752 return 0;
3753
3754 /* Verify that the corresponding windows do match. */
3755 if (! EQ (p1->buffer, p2->buffer))
3756 return 0;
3757 if (! EQ (p1->left, p2->left))
3758 return 0;
3759 if (! EQ (p1->top, p2->top))
3760 return 0;
3761 if (! EQ (p1->width, p2->width))
3762 return 0;
3763 if (! EQ (p1->height, p2->height))
3764 return 0;
cbff28e8
RS
3765 if (! EQ (p1->display_table, p2->display_table))
3766 return 0;
3767 if (! EQ (p1->parent, p2->parent))
3768 return 0;
3769 if (! EQ (p1->prev, p2->prev))
3770 return 0;
2f8274be
RS
3771 if (! ignore_positions)
3772 {
3773 if (! EQ (p1->hscroll, p2->hscroll))
3774 return 0;
3775 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
3776 return 0;
3777 if (NILP (Fequal (p1->start, p2->start)))
3778 return 0;
3779 if (NILP (Fequal (p1->pointm, p2->pointm)))
3780 return 0;
3781 if (NILP (Fequal (p1->mark, p2->mark)))
3782 return 0;
3783 }
cbff28e8
RS
3784 }
3785
3786 return 1;
3787}
2f8274be
RS
3788
3789DEFUN ("compare-window-configurations", Fcompare_window_configurations,
3790 Scompare_window_configurations, 2, 2, 0,
3791 "Compare two window configurations as regards the structure of windows.\n\
3792This function ignores details such as the values of point and mark\n\
3793and scrolling positions.")
3794 (x, y)
3795 Lisp_Object x, y;
3796{
3797 if (compare_window_configurations (x, y, 1))
3798 return Qt;
3799 return Qnil;
3800}
cbff28e8 3801\f
7ab12479
JB
3802init_window_once ()
3803{
44fa5b1e 3804 selected_frame = make_terminal_frame ();
bc6c324f 3805 XSETFRAME (Vterminal_frame, selected_frame);
44fa5b1e
JB
3806 minibuf_window = selected_frame->minibuffer_window;
3807 selected_window = selected_frame->selected_window;
3808 last_nonminibuf_frame = selected_frame;
5b03d3c0
RS
3809
3810 window_initialized = 1;
7ab12479
JB
3811}
3812
3813syms_of_window ()
3814{
543f5fb1
RS
3815 staticpro (&Qwindow_configuration_change_hook);
3816 Qwindow_configuration_change_hook
3817 = intern ("window-configuration-change-hook");
3818
7ab12479
JB
3819 Qwindowp = intern ("windowp");
3820 staticpro (&Qwindowp);
3821
806b4d9b
JB
3822 Qwindow_live_p = intern ("window-live-p");
3823 staticpro (&Qwindow_live_p);
605be8af 3824
2cccc823 3825 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
a58ec57d
RS
3826 staticpro (&Qtemp_buffer_show_hook);
3827
7ab12479
JB
3828 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
3829 "Non-nil means call as function to display a help buffer.\n\
c3ef6b1d 3830The function is called with one argument, the buffer to be displayed.\n\
f52cca03
RS
3831Used by `with-output-to-temp-buffer'.\n\
3832If this function is used, then it must do the entire job of showing\n\
3833the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
7ab12479
JB
3834 Vtemp_buffer_show_function = Qnil;
3835
3836 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
3837 "If non-nil, function to call to handle `display-buffer'.\n\
3838It will receive two args, the buffer and a flag which if non-nil means\n\
3839 that the currently selected window is not acceptable.\n\
3840Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
3841work using this function.");
3842 Vdisplay_buffer_function = Qnil;
3843
7ab12479
JB
3844 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
3845 "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
3846 Vminibuf_scroll_window = Qnil;
3847
3848 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
3849 "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
3850 Vother_window_scroll_buffer = Qnil;
3851
44fa5b1e 3852 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
700f75a4 3853 "*Non-nil means `display-buffer' should make a separate frame.");
44fa5b1e 3854 pop_up_frames = 0;
7ab12479 3855
44fa5b1e 3856 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
a90712c2 3857 "Function to call to handle automatic new frame creation.\n\
44fa5b1e 3858It is called with no arguments and should return a newly created frame.\n\
7ab12479 3859\n\
44fa5b1e
JB
3860A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
3861where `pop-up-frame-alist' would hold the default frame parameters.");
3862 Vpop_up_frame_function = Qnil;
7ab12479 3863
a90712c2
RS
3864 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
3865 "*List of buffer names that should have their own special frames.\n\
3866Displaying a buffer whose name is in this list makes a special frame for it\n\
524580a4 3867using `special-display-function'. See also `special-display-regexps'.\n\
3548e138 3868\n\
524580a4
RS
3869An element of the list can be a list instead of just a string.\n\
3870There are two ways to use a list as an element:\n\
3871 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)\n\
3872In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3873In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
3874followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
4caa7448
RS
3875All this is done by the function found in `special-display-function'.\n\
3876\n\
3877If this variable appears \"not to work\", because you add a name to it\n\
3878but that buffer still appears in the selected window, look at the\n\
3879values of `same-window-buffer-names' and `same-window-regexps'.\n\
3880Those variables take precedence over this one.");
a90712c2
RS
3881 Vspecial_display_buffer_names = Qnil;
3882
3883 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
3884 "*List of regexps saying which buffers should have their own special frames.\n\
3885If a buffer name matches one of these regexps, it gets its own frame.\n\
3886Displaying a buffer whose name is in this list makes a special frame for it\n\
0a952b57 3887using `special-display-function'.\n\
3548e138 3888\n\
524580a4
RS
3889An element of the list can be a list instead of just a string.\n\
3890There are two ways to use a list as an element:\n\
3891 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)\n\
3892In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3893In the latter case, FUNCTION is called with the buffer as first argument,\n\
3894followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
4caa7448
RS
3895All this is done by the function found in `special-display-function'.\n\
3896\n\
3897If this variable appears \"not to work\", because you add a regexp to it\n\
3898but the matching buffers still appear in the selected window, look at the\n\
3899values of `same-window-buffer-names' and `same-window-regexps'.\n\
3900Those variables take precedence over this one.");
a90712c2
RS
3901 Vspecial_display_regexps = Qnil;
3902
3903 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
3904 "Function to call to make a new frame for a special buffer.\n\
0a952b57
RS
3905It is called with two arguments, the buffer and optional buffer specific\n\
3906data, and should return a window displaying that buffer.\n\
a90712c2 3907The default value makes a separate frame for the buffer,\n\
bdd3a802 3908using `special-display-frame-alist' to specify the frame parameters.\n\
a90712c2
RS
3909\n\
3910A buffer is special if its is listed in `special-display-buffer-names'\n\
3911or matches a regexp in `special-display-regexps'.");
3912 Vspecial_display_function = Qnil;
3913
855d8627
RS
3914 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
3915 "*List of buffer names that should appear in the selected window.\n\
3916Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
3917switches to it in the selected window, rather than making it appear\n\
2e5ce1a0 3918in some other window.\n\
855d8627
RS
3919\n\
3920An element of the list can be a cons cell instead of just a string.\n\
3921Then the car must be a string, which specifies the buffer name.\n\
3922This is for compatibility with `special-display-buffer-names';\n\
3923the cdr of the cons cell is ignored.\n\
3924\n\
3925See also `same-window-regexps'.");
3926 Vsame_window_buffer_names = Qnil;
3927
3928 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
3929 "*List of regexps saying which buffers should appear in the selected window.\n\
3930If a buffer name matches one of these regexps, then displaying it\n\
3931using `display-buffer' or `pop-to-buffer' switches to it\n\
3932in the selected window, rather than making it appear in some other window.\n\
3933\n\
3934An element of the list can be a cons cell instead of just a string.\n\
3935Then the car must be a string, which specifies the buffer name.\n\
3936This is for compatibility with `special-display-buffer-names';\n\
3937the cdr of the cons cell is ignored.\n\
3938\n\
3939See also `same-window-buffer-names'.");
3940 Vsame_window_regexps = Qnil;
3941
7ab12479
JB
3942 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
3943 "*Non-nil means display-buffer should make new windows.");
3944 pop_up_windows = 1;
3945
3946 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
3947 "*Number of lines of continuity when scrolling by screenfuls.");
3948 next_screen_context_lines = 2;
3949
3950 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
3951 "*display-buffer would prefer to split the largest window if this large.\n\
3952If there is only one window, it is split regardless of this value.");
3953 split_height_threshold = 500;
3954
3955 DEFVAR_INT ("window-min-height", &window_min_height,
3956 "*Delete any window less than this tall (including its mode line).");
3957 window_min_height = 4;
3958
3959 DEFVAR_INT ("window-min-width", &window_min_width,
3960 "*Delete any window less than this wide.");
3961 window_min_width = 10;
3962
9317a85d
RS
3963 DEFVAR_BOOL ("scroll-preserve-screen-position",
3964 &scroll_preserve_screen_position,
3965 "*Nonzero means scroll commands move point to keep its screen line unchanged.");
3966 scroll_preserve_screen_position = 0;
3967
543f5fb1
RS
3968 DEFVAR_LISP ("window-configuration-change-hook",
3969 &Vwindow_configuration_change_hook,
3970 "Functions to call when window configuration changes.\n\
e3e041eb 3971The selected frame is the one whose configuration has changed.");
543f5fb1
RS
3972 Vwindow_configuration_change_hook = Qnil;
3973
7ab12479
JB
3974 defsubr (&Sselected_window);
3975 defsubr (&Sminibuffer_window);
3976 defsubr (&Swindow_minibuffer_p);
3977 defsubr (&Swindowp);
806b4d9b 3978 defsubr (&Swindow_live_p);
7ab12479
JB
3979 defsubr (&Spos_visible_in_window_p);
3980 defsubr (&Swindow_buffer);
3981 defsubr (&Swindow_height);
3982 defsubr (&Swindow_width);
3983 defsubr (&Swindow_hscroll);
3984 defsubr (&Sset_window_hscroll);
190eb263
RS
3985 defsubr (&Swindow_redisplay_end_trigger);
3986 defsubr (&Sset_window_redisplay_end_trigger);
7ab12479 3987 defsubr (&Swindow_edges);
d5783c40
JB
3988 defsubr (&Scoordinates_in_window_p);
3989 defsubr (&Swindow_at);
7ab12479
JB
3990 defsubr (&Swindow_point);
3991 defsubr (&Swindow_start);
3992 defsubr (&Swindow_end);
3993 defsubr (&Sset_window_point);
3994 defsubr (&Sset_window_start);
3995 defsubr (&Swindow_dedicated_p);
d207b766 3996 defsubr (&Sset_window_dedicated_p);
7ab12479
JB
3997 defsubr (&Swindow_display_table);
3998 defsubr (&Sset_window_display_table);
3999 defsubr (&Snext_window);
4000 defsubr (&Sprevious_window);
4001 defsubr (&Sother_window);
4002 defsubr (&Sget_lru_window);
4003 defsubr (&Sget_largest_window);
4004 defsubr (&Sget_buffer_window);
4005 defsubr (&Sdelete_other_windows);
4006 defsubr (&Sdelete_windows_on);
4007 defsubr (&Sreplace_buffer_in_windows);
4008 defsubr (&Sdelete_window);
4009 defsubr (&Sset_window_buffer);
4010 defsubr (&Sselect_window);
4628f7a4
EN
4011 defsubr (&Sspecial_display_p);
4012 defsubr (&Ssame_window_p);
7ab12479
JB
4013 defsubr (&Sdisplay_buffer);
4014 defsubr (&Ssplit_window);
4015 defsubr (&Senlarge_window);
4016 defsubr (&Sshrink_window);
4017 defsubr (&Sscroll_up);
4018 defsubr (&Sscroll_down);
4019 defsubr (&Sscroll_left);
4020 defsubr (&Sscroll_right);
ccd0664b 4021 defsubr (&Sother_window_for_scrolling);
7ab12479
JB
4022 defsubr (&Sscroll_other_window);
4023 defsubr (&Srecenter);
4024 defsubr (&Smove_to_window_line);
4025 defsubr (&Swindow_configuration_p);
4026 defsubr (&Sset_window_configuration);
4027 defsubr (&Scurrent_window_configuration);
4028 defsubr (&Ssave_window_excursion);
2f8274be 4029 defsubr (&Scompare_window_configurations);
7ab12479
JB
4030}
4031
4032keys_of_window ()
4033{
4034 initial_define_key (control_x_map, '1', "delete-other-windows");
4035 initial_define_key (control_x_map, '2', "split-window");
4036 initial_define_key (control_x_map, '0', "delete-window");
4037 initial_define_key (control_x_map, 'o', "other-window");
4038 initial_define_key (control_x_map, '^', "enlarge-window");
4039 initial_define_key (control_x_map, '<', "scroll-left");
4040 initial_define_key (control_x_map, '>', "scroll-right");
4041
4042 initial_define_key (global_map, Ctl ('V'), "scroll-up");
4043 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
4044 initial_define_key (meta_map, 'v', "scroll-down");
4045
4046 initial_define_key (global_map, Ctl('L'), "recenter");
4047 initial_define_key (meta_map, 'r', "move-to-window-line");
4048}