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