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