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