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