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