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