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