* alloc.c: Do not define struct catchtag.
[bpt/emacs.git] / src / window.c
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
4 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <setjmp.h>
25
26 #include "lisp.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "indent.h"
34 #include "termchar.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "blockinput.h"
38 #include "intervals.h"
39 #include "termhooks.h" /* For FRAME_TERMINAL. */
40
41 #ifdef HAVE_X_WINDOWS
42 #include "xterm.h"
43 #endif /* HAVE_X_WINDOWS */
44 #ifdef WINDOWSNT
45 #include "w32term.h"
46 #endif
47 #ifdef MSDOS
48 #include "msdos.h"
49 #endif
50 #ifdef HAVE_NS
51 #include "nsterm.h"
52 #endif
53
54
55 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
56 Lisp_Object Qdisplay_buffer;
57 Lisp_Object Qscroll_up, Qscroll_down;
58 Lisp_Object Qwindow_size_fixed;
59
60 extern Lisp_Object Qleft_margin, Qright_margin;
61
62 static int displayed_window_lines P_ ((struct window *));
63 static struct window *decode_window P_ ((Lisp_Object));
64 static int count_windows P_ ((struct window *));
65 static int get_leaf_windows P_ ((struct window *, struct window **, int));
66 static void window_scroll P_ ((Lisp_Object, int, int, int));
67 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
68 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
69 static int window_min_size_1 P_ ((struct window *, int, int));
70 static int window_min_size_2 P_ ((struct window *, int, int));
71 static int window_min_size P_ ((struct window *, int, int, int, int *));
72 static void size_window P_ ((Lisp_Object, int, int, int, int, int));
73 static int freeze_window_start P_ ((struct window *, void *));
74 static int window_fixed_size_p P_ ((struct window *, int, int));
75 static void enlarge_window P_ ((Lisp_Object, int, int));
76 static Lisp_Object window_list P_ ((void));
77 static int add_window_to_list P_ ((struct window *, void *));
78 static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
79 Lisp_Object));
80 static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
81 Lisp_Object, int));
82 static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
83 Lisp_Object *));
84 static int foreach_window_1 P_ ((struct window *,
85 int (* fn) (struct window *, void *),
86 void *));
87 static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
88
89 /* This is the window in which the terminal's cursor should
90 be left when nothing is being done with it. This must
91 always be a leaf window, and its buffer is selected by
92 the top level editing loop at the end of each command.
93
94 This value is always the same as
95 FRAME_SELECTED_WINDOW (selected_frame). */
96
97 Lisp_Object selected_window;
98
99 /* A list of all windows for use by next_window and Fwindow_list.
100 Functions creating or deleting windows should invalidate this cache
101 by setting it to nil. */
102
103 Lisp_Object Vwindow_list;
104
105 /* The mini-buffer window of the selected frame.
106 Note that you cannot test for mini-bufferness of an arbitrary window
107 by comparing against this; but you can test for mini-bufferness of
108 the selected window. */
109
110 Lisp_Object minibuf_window;
111
112 /* Non-nil means it is the window whose mode line should be
113 shown as the selected window when the minibuffer is selected. */
114
115 Lisp_Object minibuf_selected_window;
116
117 /* Non-nil means it is the window for C-M-v to scroll
118 when the mini-buffer is selected. */
119
120 Lisp_Object Vminibuf_scroll_window;
121
122 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
123
124 Lisp_Object Vother_window_scroll_buffer;
125
126 /* Non-nil means it's function to call to display temp buffers. */
127
128 Lisp_Object Vtemp_buffer_show_function;
129
130 /* Non-zero means line and page scrolling on tall lines (with images)
131 does partial scrolling by modifying window-vscroll. */
132
133 int auto_window_vscroll_p;
134
135 /* Non-zero means to use mode-line-inactive face in all windows but the
136 selected-window and the minibuffer-scroll-window when the
137 minibuffer is active. */
138 int mode_line_in_non_selected_windows;
139
140 /* If a window gets smaller than either of these, it is removed. */
141
142 EMACS_INT window_min_height;
143 EMACS_INT window_min_width;
144
145 /* Hook run at end of temp_output_buffer_show. */
146
147 Lisp_Object Qtemp_buffer_show_hook;
148
149 /* Number of lines of continuity in scrolling by screenfuls. */
150
151 EMACS_INT next_screen_context_lines;
152
153 /* Incremented for each window created. */
154
155 static int sequence_number;
156
157 /* Nonzero after init_window_once has finished. */
158
159 static int window_initialized;
160
161 /* Hook to run when window config changes. */
162
163 static Lisp_Object Qwindow_configuration_change_hook;
164 static Lisp_Object Vwindow_configuration_change_hook;
165
166 /* Non-nil means scroll commands try to put point
167 at the same screen height as previously. */
168
169 Lisp_Object Vscroll_preserve_screen_position;
170
171 /* Non-nil means that text is inserted before window's markers. */
172
173 Lisp_Object Vwindow_point_insertion_type;
174
175 /* Incremented by 1 whenever a window is deleted. */
176
177 int window_deletion_count;
178
179 /* Used by the function window_scroll_pixel_based */
180
181 static int window_scroll_pixel_based_preserve_x;
182 static int window_scroll_pixel_based_preserve_y;
183
184 /* Same for window_scroll_line_based. */
185
186 static int window_scroll_preserve_hpos;
187 static int window_scroll_preserve_vpos;
188
189 #if 0 /* This isn't used anywhere. */
190 /* Nonzero means we can split a frame even if it is "unsplittable". */
191 static int inhibit_frame_unsplittable;
192 #endif /* 0 */
193
194 extern EMACS_INT scroll_margin;
195
196 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
197
198 /* If non-nil, then the `recenter' command with a nil argument
199 the entire frame to be redrawn; the special value `tty' causes the
200 frame to be redrawn only if it is a tty frame. */
201
202 static Lisp_Object Vrecenter_redisplay;
203 extern Lisp_Object Qtty;
204
205 \f
206 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
207 doc: /* Return t if OBJECT is a window. */)
208 (object)
209 Lisp_Object object;
210 {
211 return WINDOWP (object) ? Qt : Qnil;
212 }
213
214 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
215 doc: /* Return t if OBJECT is a window which is currently visible. */)
216 (object)
217 Lisp_Object object;
218 {
219 return WINDOW_LIVE_P (object) ? Qt : Qnil;
220 }
221
222 Lisp_Object
223 make_window ()
224 {
225 Lisp_Object val;
226 register struct window *p;
227
228 p = allocate_window ();
229 ++sequence_number;
230 XSETFASTINT (p->sequence_number, sequence_number);
231 XSETFASTINT (p->left_col, 0);
232 XSETFASTINT (p->top_line, 0);
233 XSETFASTINT (p->total_lines, 0);
234 XSETFASTINT (p->total_cols, 0);
235 XSETFASTINT (p->hscroll, 0);
236 XSETFASTINT (p->min_hscroll, 0);
237 p->orig_top_line = p->orig_total_lines = Qnil;
238 p->start = Fmake_marker ();
239 p->pointm = Fmake_marker ();
240 XSETFASTINT (p->use_time, 0);
241 p->frame = Qnil;
242 p->display_table = Qnil;
243 p->dedicated = Qnil;
244 p->window_parameters = Qnil;
245 p->pseudo_window_p = 0;
246 bzero (&p->cursor, sizeof (p->cursor));
247 bzero (&p->last_cursor, sizeof (p->last_cursor));
248 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
249 p->desired_matrix = p->current_matrix = 0;
250 p->nrows_scale_factor = p->ncols_scale_factor = 1;
251 p->phys_cursor_type = -1;
252 p->phys_cursor_width = -1;
253 p->must_be_updated_p = 0;
254 XSETFASTINT (p->window_end_vpos, 0);
255 XSETFASTINT (p->window_end_pos, 0);
256 p->window_end_valid = Qnil;
257 p->vscroll = 0;
258 XSETWINDOW (val, p);
259 XSETFASTINT (p->last_point, 0);
260 p->frozen_window_start_p = 0;
261 p->last_cursor_off_p = p->cursor_off_p = 0;
262 p->left_margin_cols = Qnil;
263 p->right_margin_cols = Qnil;
264 p->left_fringe_width = Qnil;
265 p->right_fringe_width = Qnil;
266 p->fringes_outside_margins = Qnil;
267 p->scroll_bar_width = Qnil;
268 p->vertical_scroll_bar_type = Qt;
269 p->resize_proportionally = Qnil;
270
271 Vwindow_list = Qnil;
272 return val;
273 }
274
275 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
276 doc: /* Return the window that the cursor now appears in and commands apply to. */)
277 ()
278 {
279 return selected_window;
280 }
281
282 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
283 doc: /* Return the window used now for minibuffers.
284 If the optional argument FRAME is specified, return the minibuffer window
285 used by that frame. */)
286 (frame)
287 Lisp_Object frame;
288 {
289 if (NILP (frame))
290 frame = selected_frame;
291 CHECK_LIVE_FRAME (frame);
292 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
293 }
294
295 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
296 doc: /* Return non-nil if WINDOW is a minibuffer window.
297 WINDOW defaults to the selected window. */)
298 (window)
299 Lisp_Object window;
300 {
301 struct window *w = decode_window (window);
302 return MINI_WINDOW_P (w) ? Qt : Qnil;
303 }
304
305
306 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
307 Spos_visible_in_window_p, 0, 3, 0,
308 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
309 Return nil if that position is scrolled vertically out of view.
310 If a character is only partially visible, nil is returned, unless the
311 optional argument PARTIALLY is non-nil.
312 If POS is only out of view because of horizontal scrolling, return non-nil.
313 If POS is t, it specifies the position of the last visible glyph in WINDOW.
314 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
315
316 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
317 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
318 where X and Y are the pixel coordinates relative to the top left corner
319 of the window. The remaining elements are omitted if the character after
320 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
321 off-window at the top and bottom of the row, ROWH is the height of the
322 display row, and VPOS is the row number (0-based) containing POS. */)
323 (pos, window, partially)
324 Lisp_Object pos, window, partially;
325 {
326 register struct window *w;
327 register int posint;
328 register struct buffer *buf;
329 struct text_pos top;
330 Lisp_Object in_window = Qnil;
331 int rtop, rbot, rowh, vpos, fully_p = 1;
332 int x, y;
333
334 w = decode_window (window);
335 buf = XBUFFER (w->buffer);
336 SET_TEXT_POS_FROM_MARKER (top, w->start);
337
338 if (EQ (pos, Qt))
339 posint = -1;
340 else if (!NILP (pos))
341 {
342 CHECK_NUMBER_COERCE_MARKER (pos);
343 posint = XINT (pos);
344 }
345 else if (w == XWINDOW (selected_window))
346 posint = PT;
347 else
348 posint = XMARKER (w->pointm)->charpos;
349
350 /* If position is above window start or outside buffer boundaries,
351 or if window start is out of range, position is not visible. */
352 if ((EQ (pos, Qt)
353 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
354 && CHARPOS (top) >= BUF_BEGV (buf)
355 && CHARPOS (top) <= BUF_ZV (buf)
356 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
357 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
358 in_window = Qt;
359
360 if (!NILP (in_window) && !NILP (partially))
361 {
362 Lisp_Object part = Qnil;
363 if (!fully_p)
364 part = list4 (make_number (rtop), make_number (rbot),
365 make_number (rowh), make_number (vpos));
366 in_window = Fcons (make_number (x),
367 Fcons (make_number (y), part));
368 }
369
370 return in_window;
371 }
372
373 DEFUN ("window-line-height", Fwindow_line_height,
374 Swindow_line_height, 0, 2, 0,
375 doc: /* Return height in pixels of text line LINE in window WINDOW.
376 If WINDOW is nil or omitted, use selected window.
377
378 Return height of current line if LINE is omitted or nil. Return height of
379 header or mode line if LINE is `header-line' and `mode-line'.
380 Otherwise, LINE is a text line number starting from 0. A negative number
381 counts from the end of the window.
382
383 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
384 in pixels of the visible part of the line, VPOS and YPOS are the
385 vertical position in lines and pixels of the line, relative to the top
386 of the first text line, and OFFBOT is the number of off-window pixels at
387 the bottom of the text line. If there are off-window pixels at the top
388 of the (first) text line, YPOS is negative.
389
390 Return nil if window display is not up-to-date. In that case, use
391 `pos-visible-in-window-p' to obtain the information. */)
392 (line, window)
393 Lisp_Object line, window;
394 {
395 register struct window *w;
396 register struct buffer *b;
397 struct glyph_row *row, *end_row;
398 int max_y, crop, i, n;
399
400 w = decode_window (window);
401
402 if (noninteractive
403 || w->pseudo_window_p)
404 return Qnil;
405
406 CHECK_BUFFER (w->buffer);
407 b = XBUFFER (w->buffer);
408
409 /* Fail if current matrix is not up-to-date. */
410 if (NILP (w->window_end_valid)
411 || current_buffer->clip_changed
412 || current_buffer->prevent_redisplay_optimizations_p
413 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
414 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
415 return Qnil;
416
417 if (NILP (line))
418 {
419 i = w->cursor.vpos;
420 if (i < 0 || i >= w->current_matrix->nrows
421 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
422 return Qnil;
423 max_y = window_text_bottom_y (w);
424 goto found_row;
425 }
426
427 if (EQ (line, Qheader_line))
428 {
429 if (!WINDOW_WANTS_HEADER_LINE_P (w))
430 return Qnil;
431 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
432 if (!row->enabled_p)
433 return Qnil;
434 return list4 (make_number (row->height),
435 make_number (0), make_number (0),
436 make_number (0));
437 }
438
439 if (EQ (line, Qmode_line))
440 {
441 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
442 if (!row->enabled_p)
443 return Qnil;
444 return list4 (make_number (row->height),
445 make_number (0), /* not accurate */
446 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
447 + window_text_bottom_y (w)),
448 make_number (0));
449 }
450
451 CHECK_NUMBER (line);
452 n = XINT (line);
453
454 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
455 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
456 max_y = window_text_bottom_y (w);
457 i = 0;
458
459 while ((n < 0 || i < n)
460 && row <= end_row && row->enabled_p
461 && row->y + row->height < max_y)
462 row++, i++;
463
464 if (row > end_row || !row->enabled_p)
465 return Qnil;
466
467 if (++n < 0)
468 {
469 if (-n > i)
470 return Qnil;
471 row += n;
472 i += n;
473 }
474
475 found_row:
476 crop = max (0, (row->y + row->height) - max_y);
477 return list4 (make_number (row->height + min (0, row->y) - crop),
478 make_number (i),
479 make_number (row->y),
480 make_number (crop));
481 }
482
483
484 \f
485 static struct window *
486 decode_window (window)
487 register Lisp_Object window;
488 {
489 if (NILP (window))
490 return XWINDOW (selected_window);
491
492 CHECK_LIVE_WINDOW (window);
493 return XWINDOW (window);
494 }
495
496 static struct window *
497 decode_any_window (window)
498 register Lisp_Object window;
499 {
500 if (NILP (window))
501 return XWINDOW (selected_window);
502
503 CHECK_WINDOW (window);
504 return XWINDOW (window);
505 }
506
507 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
508 doc: /* Return the buffer that WINDOW is displaying.
509 WINDOW defaults to the selected window. */)
510 (window)
511 Lisp_Object window;
512 {
513 return decode_window (window)->buffer;
514 }
515
516 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
517 doc: /* Return the number of lines in WINDOW.
518 WINDOW defaults to the selected window.
519
520 The return value includes WINDOW's mode line and header line, if any. */)
521 (window)
522 Lisp_Object window;
523 {
524 return decode_any_window (window)->total_lines;
525 }
526
527 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
528 doc: /* Return the number of display columns in WINDOW.
529 WINDOW defaults to the selected window.
530
531 Note: The return value is the number of columns available for text in
532 WINDOW. If you want to find out how many columns WINDOW takes up, use
533 (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
534 (window)
535 Lisp_Object window;
536 {
537 return make_number (window_box_text_cols (decode_any_window (window)));
538 }
539
540 DEFUN ("window-full-width-p", Fwindow_full_width_p, Swindow_full_width_p, 0, 1, 0,
541 doc: /* Return t if WINDOW is as wide as its frame.
542 WINDOW defaults to the selected window. */)
543 (window)
544 Lisp_Object window;
545 {
546 return WINDOW_FULL_WIDTH_P (decode_any_window (window)) ? Qt : Qnil;
547 }
548
549 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
550 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
551 WINDOW defaults to the selected window. */)
552 (window)
553 Lisp_Object window;
554 {
555 return decode_window (window)->hscroll;
556 }
557
558 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
559 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
560 Return NCOL. NCOL should be zero or positive.
561
562 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
563 window so that the location of point moves off-window. */)
564 (window, ncol)
565 Lisp_Object window, ncol;
566 {
567 struct window *w = decode_window (window);
568 int hscroll;
569
570 CHECK_NUMBER (ncol);
571 hscroll = max (0, XINT (ncol));
572
573 /* Prevent redisplay shortcuts when changing the hscroll. */
574 if (XINT (w->hscroll) != hscroll)
575 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
576
577 w->hscroll = make_number (hscroll);
578 return ncol;
579 }
580
581 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
582 Swindow_redisplay_end_trigger, 0, 1, 0,
583 doc: /* Return WINDOW's redisplay end trigger value.
584 WINDOW defaults to the selected window.
585 See `set-window-redisplay-end-trigger' for more information. */)
586 (window)
587 Lisp_Object window;
588 {
589 return decode_window (window)->redisplay_end_trigger;
590 }
591
592 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
593 Sset_window_redisplay_end_trigger, 2, 2, 0,
594 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
595 VALUE should be a buffer position (typically a marker) or nil.
596 If it is a buffer position, then if redisplay in WINDOW reaches a position
597 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
598 with two arguments: WINDOW, and the end trigger value.
599 Afterwards the end-trigger value is reset to nil. */)
600 (window, value)
601 register Lisp_Object window, value;
602 {
603 register struct window *w;
604
605 w = decode_window (window);
606 w->redisplay_end_trigger = value;
607 return value;
608 }
609
610 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
611 doc: /* Return a list of the edge coordinates of WINDOW.
612 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
613 RIGHT is one more than the rightmost column occupied by WINDOW,
614 and BOTTOM is one more than the bottommost row occupied by WINDOW.
615 The edges include the space used by the window's scroll bar,
616 display margins, fringes, header line, and mode line, if it has them.
617 To get the edges of the actual text area, use `window-inside-edges'. */)
618 (window)
619 Lisp_Object window;
620 {
621 register struct window *w = decode_any_window (window);
622
623 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
624 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
625 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
626 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
627 Qnil))));
628 }
629
630 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
631 doc: /* Return a list of the edge pixel coordinates of WINDOW.
632 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
633 RIGHT is one more than the rightmost x position occupied by WINDOW,
634 and BOTTOM is one more than the bottommost y position occupied by WINDOW.
635 The pixel edges include the space used by the window's scroll bar,
636 display margins, fringes, header line, and mode line, if it has them.
637 To get the edges of the actual text area, use `window-inside-pixel-edges'. */)
638 (window)
639 Lisp_Object window;
640 {
641 register struct window *w = decode_any_window (window);
642
643 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
644 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
645 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
646 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
647 Qnil))));
648 }
649
650 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
651 doc: /* Return a list of the edge coordinates of WINDOW.
652 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
653 RIGHT is one more than the rightmost column used by text in WINDOW,
654 and BOTTOM is one more than the bottommost row used by text in WINDOW.
655 The inside edges do not include the space used by the window's scroll bar,
656 display margins, fringes, header line, and/or mode line. */)
657 (window)
658 Lisp_Object window;
659 {
660 register struct window *w = decode_any_window (window);
661
662 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
663 + WINDOW_LEFT_MARGIN_COLS (w)
664 + WINDOW_LEFT_FRINGE_COLS (w)),
665 make_number (WINDOW_TOP_EDGE_LINE (w)
666 + WINDOW_HEADER_LINE_LINES (w)),
667 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
668 - WINDOW_RIGHT_MARGIN_COLS (w)
669 - WINDOW_RIGHT_FRINGE_COLS (w)),
670 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
671 - WINDOW_MODE_LINE_LINES (w)));
672 }
673
674 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
675 doc: /* Return a list of the edge pixel coordinates of WINDOW.
676 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
677 RIGHT is one more than the rightmost x position used by text in WINDOW,
678 and BOTTOM is one more than the bottommost y position used by text in WINDOW.
679 The inside edges do not include the space used by the window's scroll bar,
680 display margins, fringes, header line, and/or mode line. */)
681 (window)
682 Lisp_Object window;
683 {
684 register struct window *w = decode_any_window (window);
685
686 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
687 + WINDOW_LEFT_MARGIN_WIDTH (w)
688 + WINDOW_LEFT_FRINGE_WIDTH (w)),
689 make_number (WINDOW_TOP_EDGE_Y (w)
690 + WINDOW_HEADER_LINE_HEIGHT (w)),
691 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
692 - WINDOW_RIGHT_MARGIN_WIDTH (w)
693 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
694 make_number (WINDOW_BOTTOM_EDGE_Y (w)
695 - WINDOW_MODE_LINE_HEIGHT (w)));
696 }
697
698 /* Test if the character at column *X, row *Y is within window W.
699 If it is not, return ON_NOTHING;
700 if it is in the window's text area,
701 set *x and *y to its location relative to the upper left corner
702 of the window, and
703 return ON_TEXT;
704 if it is on the window's modeline, return ON_MODE_LINE;
705 if it is on the border between the window and its right sibling,
706 return ON_VERTICAL_BORDER.
707 if it is on a scroll bar,
708 return ON_SCROLL_BAR.
709 if it is on the window's top line, return ON_HEADER_LINE;
710 if it is in left or right fringe of the window,
711 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
712 to window-relative coordinates;
713 if it is in the marginal area to the left/right of the window,
714 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
715 to window-relative coordinates.
716
717 X and Y are frame relative pixel coordinates. */
718
719 static enum window_part
720 coordinates_in_window (w, x, y)
721 register struct window *w;
722 register int *x, *y;
723 {
724 struct frame *f = XFRAME (WINDOW_FRAME (w));
725 int left_x, right_x, top_y, bottom_y;
726 enum window_part part;
727 int ux = FRAME_COLUMN_WIDTH (f);
728 int x0 = WINDOW_LEFT_EDGE_X (w);
729 int x1 = WINDOW_RIGHT_EDGE_X (w);
730 /* The width of the area where the vertical line can be dragged.
731 (Between mode lines for instance. */
732 int grabbable_width = ux;
733 int lmargin_width, rmargin_width, text_left, text_right;
734
735 /* In what's below, we subtract 1 when computing right_x because we
736 want the rightmost pixel, which is given by left_pixel+width-1. */
737 if (w->pseudo_window_p)
738 {
739 left_x = 0;
740 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
741 top_y = WINDOW_TOP_EDGE_Y (w);
742 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
743 }
744 else
745 {
746 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
747 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
748 top_y = WINDOW_TOP_EDGE_Y (w);
749 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
750 }
751
752 /* Outside any interesting row? */
753 if (*y < top_y || *y >= bottom_y)
754 return ON_NOTHING;
755
756 /* On the mode line or header line? If it's near the start of
757 the mode or header line of window that's has a horizontal
758 sibling, say it's on the vertical line. That's to be able
759 to resize windows horizontally in case we're using toolkit
760 scroll bars. */
761
762 if (WINDOW_WANTS_MODELINE_P (w)
763 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
764 {
765 part = ON_MODE_LINE;
766
767 header_vertical_border_check:
768 /* We're somewhere on the mode line. We consider the place
769 between mode lines of horizontally adjacent mode lines
770 as the vertical border. If scroll bars on the left,
771 return the right window. */
772 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
773 || WINDOW_RIGHTMOST_P (w))
774 {
775 if (!WINDOW_LEFTMOST_P (w) && eabs (*x - x0) < grabbable_width)
776 {
777 /* Convert X and Y to window relative coordinates.
778 Vertical border is at the left edge of window. */
779 *x = max (0, *x - x0);
780 *y -= top_y;
781 return ON_VERTICAL_BORDER;
782 }
783 }
784 else
785 {
786 /* Make sure we're not at the rightmost position of a
787 mode-/header-line and there's yet another window on
788 the right. (Bug#1372) */
789 if ((WINDOW_RIGHTMOST_P (w) || *x < x1)
790 && eabs (*x - x1) < grabbable_width)
791 {
792 /* Convert X and Y to window relative coordinates.
793 Vertical border is at the right edge of window. */
794 *x = min (x1, *x) - x0;
795 *y -= top_y;
796 return ON_VERTICAL_BORDER;
797 }
798 }
799
800 if (*x < x0 || *x >= x1)
801 return ON_NOTHING;
802
803 /* Convert X and Y to window relative coordinates.
804 Mode line starts at left edge of window. */
805 *x -= x0;
806 *y -= top_y;
807 return part;
808 }
809
810 if (WINDOW_WANTS_HEADER_LINE_P (w)
811 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
812 {
813 part = ON_HEADER_LINE;
814 goto header_vertical_border_check;
815 }
816
817 if (*x < x0 || *x >= x1)
818 return ON_NOTHING;
819
820 /* Outside any interesting column? */
821 if (*x < left_x || *x > right_x)
822 {
823 *y -= top_y;
824 return ON_SCROLL_BAR;
825 }
826
827 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
828 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
829
830 text_left = window_box_left (w, TEXT_AREA);
831 text_right = text_left + window_box_width (w, TEXT_AREA);
832
833 if (FRAME_WINDOW_P (f))
834 {
835 if (!w->pseudo_window_p
836 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
837 && !WINDOW_RIGHTMOST_P (w)
838 && (eabs (*x - right_x) < grabbable_width))
839 {
840 /* Convert X and Y to window relative coordinates.
841 Vertical border is at the right edge of window. */
842 *x = min (right_x, *x) - left_x;
843 *y -= top_y;
844 return ON_VERTICAL_BORDER;
845 }
846 }
847 else
848 {
849 /* Need to say "*x > right_x" rather than >=, since on character
850 terminals, the vertical line's x coordinate is right_x. */
851 if (!w->pseudo_window_p
852 && !WINDOW_RIGHTMOST_P (w)
853 && *x > right_x - ux)
854 {
855 /* On the border on the right side of the window? Assume that
856 this area begins at RIGHT_X minus a canonical char width. */
857 *x = min (right_x, *x) - left_x;
858 *y -= top_y;
859 return ON_VERTICAL_BORDER;
860 }
861 }
862
863 if (*x < text_left)
864 {
865 if (lmargin_width > 0
866 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
867 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
868 : (*x < left_x + lmargin_width)))
869 {
870 *x -= left_x;
871 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
872 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
873 *y -= top_y;
874 return ON_LEFT_MARGIN;
875 }
876
877 /* Convert X and Y to window-relative pixel coordinates. */
878 *x -= left_x;
879 *y -= top_y;
880 return ON_LEFT_FRINGE;
881 }
882
883 if (*x >= text_right)
884 {
885 if (rmargin_width > 0
886 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
887 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
888 : (*x >= right_x - rmargin_width)))
889 {
890 *x -= right_x - rmargin_width;
891 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
892 *x += WINDOW_RIGHT_FRINGE_WIDTH (w);
893 *y -= top_y;
894 return ON_RIGHT_MARGIN;
895 }
896
897 /* Convert X and Y to window-relative pixel coordinates. */
898 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
899 *y -= top_y;
900 return ON_RIGHT_FRINGE;
901 }
902
903 /* Everything special ruled out - must be on text area */
904 *x -= text_left;
905 *y -= top_y;
906 return ON_TEXT;
907 }
908
909
910 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
911 Scoordinates_in_window_p, 2, 2, 0,
912 doc: /* Return non-nil if COORDINATES are in WINDOW.
913 COORDINATES is a cons of the form (X . Y), X and Y being distances
914 measured in characters from the upper-left corner of the frame.
915 \(0 . 0) denotes the character in the upper left corner of the
916 frame.
917 If COORDINATES are in the text portion of WINDOW,
918 the coordinates relative to the window are returned.
919 If they are in the mode line of WINDOW, `mode-line' is returned.
920 If they are in the top mode line of WINDOW, `header-line' is returned.
921 If they are in the left fringe of WINDOW, `left-fringe' is returned.
922 If they are in the right fringe of WINDOW, `right-fringe' is returned.
923 If they are on the border between WINDOW and its right sibling,
924 `vertical-line' is returned.
925 If they are in the windows's left or right marginal areas, `left-margin'\n\
926 or `right-margin' is returned. */)
927 (coordinates, window)
928 register Lisp_Object coordinates, window;
929 {
930 struct window *w;
931 struct frame *f;
932 int x, y;
933 Lisp_Object lx, ly;
934
935 CHECK_WINDOW (window);
936 w = XWINDOW (window);
937 f = XFRAME (w->frame);
938 CHECK_CONS (coordinates);
939 lx = Fcar (coordinates);
940 ly = Fcdr (coordinates);
941 CHECK_NUMBER_OR_FLOAT (lx);
942 CHECK_NUMBER_OR_FLOAT (ly);
943 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
944 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
945
946 switch (coordinates_in_window (w, &x, &y))
947 {
948 case ON_NOTHING:
949 return Qnil;
950
951 case ON_TEXT:
952 /* X and Y are now window relative pixel coordinates. Convert
953 them to canonical char units before returning them. */
954 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
955 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
956
957 case ON_MODE_LINE:
958 return Qmode_line;
959
960 case ON_VERTICAL_BORDER:
961 return Qvertical_line;
962
963 case ON_HEADER_LINE:
964 return Qheader_line;
965
966 case ON_LEFT_FRINGE:
967 return Qleft_fringe;
968
969 case ON_RIGHT_FRINGE:
970 return Qright_fringe;
971
972 case ON_LEFT_MARGIN:
973 return Qleft_margin;
974
975 case ON_RIGHT_MARGIN:
976 return Qright_margin;
977
978 case ON_SCROLL_BAR:
979 /* Historically we are supposed to return nil in this case. */
980 return Qnil;
981
982 default:
983 abort ();
984 }
985 }
986
987
988 /* Callback for foreach_window, used in window_from_coordinates.
989 Check if window W contains coordinates specified by USER_DATA which
990 is actually a pointer to a struct check_window_data CW.
991
992 Check if window W contains coordinates *CW->x and *CW->y. If it
993 does, return W in *CW->window, as Lisp_Object, and return in
994 *CW->part the part of the window under coordinates *X,*Y. Return
995 zero from this function to stop iterating over windows. */
996
997 struct check_window_data
998 {
999 Lisp_Object *window;
1000 int *x, *y;
1001 enum window_part *part;
1002 };
1003
1004 static int
1005 check_window_containing (w, user_data)
1006 struct window *w;
1007 void *user_data;
1008 {
1009 struct check_window_data *cw = (struct check_window_data *) user_data;
1010 enum window_part found;
1011 int continue_p = 1;
1012
1013 found = coordinates_in_window (w, cw->x, cw->y);
1014 if (found != ON_NOTHING)
1015 {
1016 *cw->part = found;
1017 XSETWINDOW (*cw->window, w);
1018 continue_p = 0;
1019 }
1020
1021 return continue_p;
1022 }
1023
1024
1025 /* Find the window containing frame-relative pixel position X/Y and
1026 return it as a Lisp_Object.
1027
1028 If X, Y is on one of the window's special `window_part' elements,
1029 set *PART to the id of that element, and return X and Y converted
1030 to window relative coordinates in WX and WY.
1031
1032 If there is no window under X, Y return nil and leave *PART
1033 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1034
1035 This function was previously implemented with a loop cycling over
1036 windows with Fnext_window, and starting with the frame's selected
1037 window. It turned out that this doesn't work with an
1038 implementation of next_window using Vwindow_list, because
1039 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1040 tree of F when this function is called asynchronously from
1041 note_mouse_highlight. The original loop didn't terminate in this
1042 case. */
1043
1044 Lisp_Object
1045 window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
1046 struct frame *f;
1047 int x, y;
1048 enum window_part *part;
1049 int *wx, *wy;
1050 int tool_bar_p;
1051 {
1052 Lisp_Object window;
1053 struct check_window_data cw;
1054 enum window_part dummy;
1055
1056 if (part == 0)
1057 part = &dummy;
1058
1059 window = Qnil;
1060 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
1061 foreach_window (f, check_window_containing, &cw);
1062
1063 /* If not found above, see if it's in the tool bar window, if a tool
1064 bar exists. */
1065 if (NILP (window)
1066 && tool_bar_p
1067 && WINDOWP (f->tool_bar_window)
1068 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1069 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
1070 != ON_NOTHING))
1071 {
1072 *part = ON_TEXT;
1073 window = f->tool_bar_window;
1074 }
1075
1076 if (wx) *wx = x;
1077 if (wy) *wy = y;
1078
1079 return window;
1080 }
1081
1082 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1083 doc: /* Return window containing coordinates X and Y on FRAME.
1084 If omitted, FRAME defaults to the currently selected frame.
1085 The top left corner of the frame is considered to be row 0,
1086 column 0. */)
1087 (x, y, frame)
1088 Lisp_Object x, y, frame;
1089 {
1090 struct frame *f;
1091
1092 if (NILP (frame))
1093 frame = selected_frame;
1094 CHECK_LIVE_FRAME (frame);
1095 f = XFRAME (frame);
1096
1097 /* Check that arguments are integers or floats. */
1098 CHECK_NUMBER_OR_FLOAT (x);
1099 CHECK_NUMBER_OR_FLOAT (y);
1100
1101 return window_from_coordinates (f,
1102 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1103 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1104 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1105 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1106 0, 0, 0, 0);
1107 }
1108
1109 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1110 doc: /* Return current value of point in WINDOW.
1111 WINDOW defaults to the selected window.
1112
1113 For a nonselected window, this is the value point would have
1114 if that window were selected.
1115
1116 Note that, when WINDOW is the selected window and its buffer
1117 is also currently selected, the value returned is the same as (point).
1118 It would be more strictly correct to return the `top-level' value
1119 of point, outside of any save-excursion forms.
1120 But that is hard to define. */)
1121 (window)
1122 Lisp_Object window;
1123 {
1124 register struct window *w = decode_window (window);
1125
1126 if (w == XWINDOW (selected_window)
1127 && current_buffer == XBUFFER (w->buffer))
1128 return Fpoint ();
1129 return Fmarker_position (w->pointm);
1130 }
1131
1132 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1133 doc: /* Return position at which display currently starts in WINDOW.
1134 WINDOW defaults to the selected window.
1135 This is updated by redisplay or by calling `set-window-start'. */)
1136 (window)
1137 Lisp_Object window;
1138 {
1139 return Fmarker_position (decode_window (window)->start);
1140 }
1141
1142 /* This is text temporarily removed from the doc string below.
1143
1144 This function returns nil if the position is not currently known.
1145 That happens when redisplay is preempted and doesn't finish.
1146 If in that case you want to compute where the end of the window would
1147 have been if redisplay had finished, do this:
1148 (save-excursion
1149 (goto-char (window-start window))
1150 (vertical-motion (1- (window-height window)) window)
1151 (point))") */
1152
1153 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1154 doc: /* Return position at which display currently ends in WINDOW.
1155 WINDOW defaults to the selected window.
1156 This is updated by redisplay, when it runs to completion.
1157 Simply changing the buffer text or setting `window-start'
1158 does not update this value.
1159 Return nil if there is no recorded value. \(This can happen if the
1160 last redisplay of WINDOW was preempted, and did not finish.)
1161 If UPDATE is non-nil, compute the up-to-date position
1162 if it isn't already recorded. */)
1163 (window, update)
1164 Lisp_Object window, update;
1165 {
1166 Lisp_Object value;
1167 struct window *w = decode_window (window);
1168 Lisp_Object buf;
1169 struct buffer *b;
1170
1171 buf = w->buffer;
1172 CHECK_BUFFER (buf);
1173 b = XBUFFER (buf);
1174
1175 #if 0 /* This change broke some things. We should make it later. */
1176 /* If we don't know the end position, return nil.
1177 The user can compute it with vertical-motion if he wants to.
1178 It would be nicer to do it automatically,
1179 but that's so slow that it would probably bother people. */
1180 if (NILP (w->window_end_valid))
1181 return Qnil;
1182 #endif
1183
1184 if (! NILP (update)
1185 && ! (! NILP (w->window_end_valid)
1186 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1187 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1188 && !noninteractive)
1189 {
1190 struct text_pos startp;
1191 struct it it;
1192 struct buffer *old_buffer = NULL;
1193
1194 /* Cannot use Fvertical_motion because that function doesn't
1195 cope with variable-height lines. */
1196 if (b != current_buffer)
1197 {
1198 old_buffer = current_buffer;
1199 set_buffer_internal (b);
1200 }
1201
1202 /* In case W->start is out of the range, use something
1203 reasonable. This situation occurred when loading a file with
1204 `-l' containing a call to `rmail' with subsequent other
1205 commands. At the end, W->start happened to be BEG, while
1206 rmail had already narrowed the buffer. */
1207 if (XMARKER (w->start)->charpos < BEGV)
1208 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1209 else if (XMARKER (w->start)->charpos > ZV)
1210 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1211 else
1212 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1213
1214 start_display (&it, w, startp);
1215 move_it_vertically (&it, window_box_height (w));
1216 if (it.current_y < it.last_visible_y)
1217 move_it_past_eol (&it);
1218 value = make_number (IT_CHARPOS (it));
1219
1220 if (old_buffer)
1221 set_buffer_internal (old_buffer);
1222 }
1223 else
1224 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1225
1226 return value;
1227 }
1228
1229 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1230 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1231 Return POS. */)
1232 (window, pos)
1233 Lisp_Object window, pos;
1234 {
1235 register struct window *w = decode_window (window);
1236
1237 CHECK_NUMBER_COERCE_MARKER (pos);
1238 if (w == XWINDOW (selected_window)
1239 && XBUFFER (w->buffer) == current_buffer)
1240 Fgoto_char (pos);
1241 else
1242 set_marker_restricted (w->pointm, pos, w->buffer);
1243
1244 /* We have to make sure that redisplay updates the window to show
1245 the new value of point. */
1246 if (!EQ (window, selected_window))
1247 ++windows_or_buffers_changed;
1248
1249 return pos;
1250 }
1251
1252 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1253 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1254 WINDOW defaults to the selected window. Return POS.
1255 Optional third arg NOFORCE non-nil inhibits next redisplay from
1256 overriding motion of point in order to display at this exact start. */)
1257 (window, pos, noforce)
1258 Lisp_Object window, pos, noforce;
1259 {
1260 register struct window *w = decode_window (window);
1261
1262 CHECK_NUMBER_COERCE_MARKER (pos);
1263 set_marker_restricted (w->start, pos, w->buffer);
1264 /* this is not right, but much easier than doing what is right. */
1265 w->start_at_line_beg = Qnil;
1266 if (NILP (noforce))
1267 w->force_start = Qt;
1268 w->update_mode_line = Qt;
1269 XSETFASTINT (w->last_modified, 0);
1270 XSETFASTINT (w->last_overlay_modified, 0);
1271 if (!EQ (window, selected_window))
1272 windows_or_buffers_changed++;
1273
1274 return pos;
1275 }
1276
1277
1278 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1279 0, 1, 0,
1280 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1281 More precisely, return the value assigned by the last call of
1282 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1283 never called with WINDOW as its argument, or the value set by that
1284 function was internally reset since its last call. WINDOW defaults to
1285 the selected window.
1286
1287 When a window is dedicated to its buffer, `display-buffer' will refrain
1288 from displaying another buffer in it. `get-lru-window' and
1289 `get-largest-window' treat dedicated windows specially.
1290 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1291 `kill-buffer' can delete a dedicated window and the containing frame.
1292
1293 Functions like `set-window-buffer' may change the buffer displayed by a
1294 window, unless that window is "strongly" dedicated to its buffer, that
1295 is the value returned by `window-dedicated-p' is t. */)
1296 (window)
1297 Lisp_Object window;
1298 {
1299 return decode_window (window)->dedicated;
1300 }
1301
1302 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1303 Sset_window_dedicated_p, 2, 2, 0,
1304 doc: /* Mark WINDOW as dedicated according to FLAG.
1305 WINDOW defaults to the selected window. FLAG non-nil means mark WINDOW
1306 as dedicated to its buffer. FLAG nil means mark WINDOW as non-dedicated.
1307 Return FLAG.
1308
1309 When a window is dedicated to its buffer, `display-buffer' will refrain
1310 from displaying another buffer in it. `get-lru-window' and
1311 `get-largest-window' treat dedicated windows specially.
1312 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1313 `kill-buffer' can delete a dedicated window and the containing
1314 frame.
1315
1316 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1317 its buffer. Functions like `set-window-buffer' may change the buffer
1318 displayed by a window, unless that window is strongly dedicated to its
1319 buffer. If and when `set-window-buffer' displays another buffer in a
1320 window, it also makes sure that the window is not marked as dedicated. */)
1321 (window, flag)
1322 Lisp_Object window, flag;
1323 {
1324 register struct window *w = decode_window (window);
1325
1326 w->dedicated = flag;
1327 return w->dedicated;
1328 }
1329
1330
1331 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1332 0, 1, 0,
1333 doc: /* Return the parameters of WINDOW and their values.
1334 WINDOW defaults to the selected window. The return value is a list of
1335 elements of the form (PARAMETER . VALUE). */)
1336 (window)
1337 Lisp_Object window;
1338 {
1339 return Fcopy_alist (decode_window (window)->window_parameters);
1340 }
1341
1342 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1343 2, 2, 0,
1344 doc: /* Return WINDOW's value for PARAMETER.
1345 WINDOW defaults to the selected window. */)
1346 (window, parameter)
1347 Lisp_Object window, parameter;
1348 {
1349 Lisp_Object result;
1350
1351 result = Fassq (parameter, decode_window (window)->window_parameters);
1352 return CDR_SAFE (result);
1353 }
1354
1355 DEFUN ("set-window-parameter", Fset_window_parameter,
1356 Sset_window_parameter, 3, 3, 0,
1357 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1358 WINDOW defaults to the selected window. Return VALUE. */)
1359 (window, parameter, value)
1360 Lisp_Object window, parameter, value;
1361 {
1362 register struct window *w = decode_window (window);
1363 Lisp_Object old_alist_elt;
1364
1365 old_alist_elt = Fassq (parameter, w->window_parameters);
1366 if (NILP (old_alist_elt))
1367 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1368 else
1369 Fsetcdr (old_alist_elt, value);
1370 return value;
1371 }
1372
1373
1374 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1375 0, 1, 0,
1376 doc: /* Return the display-table that WINDOW is using.
1377 WINDOW defaults to the selected window. */)
1378 (window)
1379 Lisp_Object window;
1380 {
1381 return decode_window (window)->display_table;
1382 }
1383
1384 /* Get the display table for use on window W. This is either W's
1385 display table or W's buffer's display table. Ignore the specified
1386 tables if they are not valid; if no valid table is specified,
1387 return 0. */
1388
1389 struct Lisp_Char_Table *
1390 window_display_table (w)
1391 struct window *w;
1392 {
1393 struct Lisp_Char_Table *dp = NULL;
1394
1395 if (DISP_TABLE_P (w->display_table))
1396 dp = XCHAR_TABLE (w->display_table);
1397 else if (BUFFERP (w->buffer))
1398 {
1399 struct buffer *b = XBUFFER (w->buffer);
1400
1401 if (DISP_TABLE_P (b->display_table))
1402 dp = XCHAR_TABLE (b->display_table);
1403 else if (DISP_TABLE_P (Vstandard_display_table))
1404 dp = XCHAR_TABLE (Vstandard_display_table);
1405 }
1406
1407 return dp;
1408 }
1409
1410 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1411 doc: /* Set WINDOW's display-table to TABLE. */)
1412 (window, table)
1413 register Lisp_Object window, table;
1414 {
1415 register struct window *w;
1416
1417 w = decode_window (window);
1418 w->display_table = table;
1419 return table;
1420 }
1421 \f
1422 /* Record info on buffer window w is displaying
1423 when it is about to cease to display that buffer. */
1424 static void
1425 unshow_buffer (w)
1426 register struct window *w;
1427 {
1428 Lisp_Object buf;
1429 struct buffer *b;
1430
1431 buf = w->buffer;
1432 b = XBUFFER (buf);
1433 if (b != XMARKER (w->pointm)->buffer)
1434 abort ();
1435
1436 #if 0
1437 if (w == XWINDOW (selected_window)
1438 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1439 /* Do this except when the selected window's buffer
1440 is being removed from some other window. */
1441 #endif
1442 /* last_window_start records the start position that this buffer
1443 had in the last window to be disconnected from it.
1444 Now that this statement is unconditional,
1445 it is possible for the buffer to be displayed in the
1446 selected window, while last_window_start reflects another
1447 window which was recently showing the same buffer.
1448 Some people might say that might be a good thing. Let's see. */
1449 b->last_window_start = marker_position (w->start);
1450
1451 /* Point in the selected window's buffer
1452 is actually stored in that buffer, and the window's pointm isn't used.
1453 So don't clobber point in that buffer. */
1454 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1455 /* This line helps to fix Horsley's testbug.el bug. */
1456 && !(WINDOWP (b->last_selected_window)
1457 && w != XWINDOW (b->last_selected_window)
1458 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1459 temp_set_point_both (b,
1460 clip_to_bounds (BUF_BEGV (b),
1461 XMARKER (w->pointm)->charpos,
1462 BUF_ZV (b)),
1463 clip_to_bounds (BUF_BEGV_BYTE (b),
1464 marker_byte_position (w->pointm),
1465 BUF_ZV_BYTE (b)));
1466
1467 if (WINDOWP (b->last_selected_window)
1468 && w == XWINDOW (b->last_selected_window))
1469 b->last_selected_window = Qnil;
1470 }
1471
1472 /* Put replacement into the window structure in place of old. */
1473 static void
1474 replace_window (old, replacement)
1475 Lisp_Object old, replacement;
1476 {
1477 register Lisp_Object tem;
1478 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1479
1480 /* If OLD is its frame's root_window, then replacement is the new
1481 root_window for that frame. */
1482
1483 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1484 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1485
1486 p->left_col = o->left_col;
1487 p->top_line = o->top_line;
1488 p->total_cols = o->total_cols;
1489 p->total_lines = o->total_lines;
1490 p->desired_matrix = p->current_matrix = 0;
1491 p->vscroll = 0;
1492 bzero (&p->cursor, sizeof (p->cursor));
1493 bzero (&p->last_cursor, sizeof (p->last_cursor));
1494 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1495 p->phys_cursor_type = -1;
1496 p->phys_cursor_width = -1;
1497 p->must_be_updated_p = 0;
1498 p->pseudo_window_p = 0;
1499 XSETFASTINT (p->window_end_vpos, 0);
1500 XSETFASTINT (p->window_end_pos, 0);
1501 p->window_end_valid = Qnil;
1502 p->frozen_window_start_p = 0;
1503 p->orig_top_line = p->orig_total_lines = Qnil;
1504
1505 p->next = tem = o->next;
1506 if (!NILP (tem))
1507 XWINDOW (tem)->prev = replacement;
1508
1509 p->prev = tem = o->prev;
1510 if (!NILP (tem))
1511 XWINDOW (tem)->next = replacement;
1512
1513 p->parent = tem = o->parent;
1514 if (!NILP (tem))
1515 {
1516 if (EQ (XWINDOW (tem)->vchild, old))
1517 XWINDOW (tem)->vchild = replacement;
1518 if (EQ (XWINDOW (tem)->hchild, old))
1519 XWINDOW (tem)->hchild = replacement;
1520 }
1521
1522 /*** Here, if replacement is a vertical combination
1523 and so is its new parent, we should make replacement's
1524 children be children of that parent instead. ***/
1525 }
1526
1527 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1528 doc: /* Remove WINDOW from its frame.
1529 WINDOW defaults to the selected window. Return nil.
1530 Signal an error when WINDOW is the only window on its frame. */)
1531 (window)
1532 register Lisp_Object window;
1533 {
1534 struct frame *f;
1535 if (NILP (window))
1536 window = selected_window;
1537 else
1538 CHECK_LIVE_WINDOW (window);
1539
1540 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1541 delete_window (window);
1542
1543 run_window_configuration_change_hook (f);
1544
1545 return Qnil;
1546 }
1547
1548 void
1549 delete_window (window)
1550 register Lisp_Object window;
1551 {
1552 register Lisp_Object tem, parent, sib;
1553 register struct window *p;
1554 register struct window *par;
1555 struct frame *f;
1556
1557 /* Because this function is called by other C code on non-leaf
1558 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1559 so we can't decode_window here. */
1560 CHECK_WINDOW (window);
1561 p = XWINDOW (window);
1562
1563 /* It's a no-op to delete an already-deleted window. */
1564 if (NILP (p->buffer)
1565 && NILP (p->hchild)
1566 && NILP (p->vchild))
1567 return;
1568
1569 parent = p->parent;
1570 if (NILP (parent))
1571 error ("Attempt to delete minibuffer or sole ordinary window");
1572 par = XWINDOW (parent);
1573
1574 windows_or_buffers_changed++;
1575 Vwindow_list = Qnil;
1576 f = XFRAME (WINDOW_FRAME (p));
1577 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1578
1579 /* Are we trying to delete any frame's selected window? */
1580 {
1581 Lisp_Object swindow, pwindow;
1582
1583 /* See if the frame's selected window is either WINDOW
1584 or any subwindow of it, by finding all that window's parents
1585 and comparing each one with WINDOW. */
1586 swindow = FRAME_SELECTED_WINDOW (f);
1587
1588 while (1)
1589 {
1590 pwindow = swindow;
1591 while (!NILP (pwindow))
1592 {
1593 if (EQ (window, pwindow))
1594 break;
1595 pwindow = XWINDOW (pwindow)->parent;
1596 }
1597
1598 /* If the window being deleted is not a parent of SWINDOW,
1599 then SWINDOW is ok as the new selected window. */
1600 if (!EQ (window, pwindow))
1601 break;
1602 /* Otherwise, try another window for SWINDOW. */
1603 swindow = Fnext_window (swindow, Qlambda, Qnil);
1604
1605 /* If we get back to the frame's selected window,
1606 it means there was no acceptable alternative,
1607 so we cannot delete. */
1608 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1609 error ("Cannot delete window");
1610 }
1611
1612 /* If we need to change SWINDOW, do it. */
1613 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1614 {
1615 /* If we're about to delete the selected window on the
1616 selected frame, then we should use Fselect_window to select
1617 the new window. On the other hand, if we're about to
1618 delete the selected window on any other frame, we shouldn't do
1619 anything but set the frame's selected_window slot. */
1620 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1621 Fselect_window (swindow, Qnil);
1622 else
1623 FRAME_SELECTED_WINDOW (f) = swindow;
1624 }
1625 }
1626
1627 /* Now we know we can delete this one. */
1628 window_deletion_count++;
1629
1630 tem = p->buffer;
1631 /* tem is null for dummy parent windows
1632 (which have inferiors but not any contents themselves) */
1633 if (!NILP (tem))
1634 {
1635 unshow_buffer (p);
1636 unchain_marker (XMARKER (p->pointm));
1637 unchain_marker (XMARKER (p->start));
1638 }
1639
1640 /* Free window glyph matrices. It is sure that they are allocated
1641 again when ADJUST_GLYPHS is called. Block input so that expose
1642 events and other events that access glyph matrices are not
1643 processed while we are changing them. */
1644 BLOCK_INPUT;
1645 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1646
1647 tem = p->next;
1648 if (!NILP (tem))
1649 XWINDOW (tem)->prev = p->prev;
1650
1651 tem = p->prev;
1652 if (!NILP (tem))
1653 XWINDOW (tem)->next = p->next;
1654
1655 if (EQ (window, par->hchild))
1656 par->hchild = p->next;
1657 if (EQ (window, par->vchild))
1658 par->vchild = p->next;
1659
1660 /* Find one of our siblings to give our space to. */
1661 sib = p->prev;
1662 if (NILP (sib))
1663 {
1664 /* If p gives its space to its next sibling, that sibling needs
1665 to have its top/left side pulled back to where p's is.
1666 set_window_{height,width} will re-position the sibling's
1667 children. */
1668 sib = p->next;
1669 XWINDOW (sib)->top_line = p->top_line;
1670 XWINDOW (sib)->left_col = p->left_col;
1671 }
1672
1673 /* Stretch that sibling. */
1674 if (!NILP (par->vchild))
1675 set_window_height (sib,
1676 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1677 1);
1678 if (!NILP (par->hchild))
1679 set_window_width (sib,
1680 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1681 1);
1682
1683 /* If parent now has only one child,
1684 put the child into the parent's place. */
1685 tem = par->hchild;
1686 if (NILP (tem))
1687 tem = par->vchild;
1688 if (NILP (XWINDOW (tem)->next)) {
1689 replace_window (parent, tem);
1690 par = XWINDOW (tem);
1691 }
1692
1693 /* Since we may be deleting combination windows, we must make sure that
1694 not only p but all its children have been marked as deleted. */
1695 if (! NILP (p->hchild))
1696 delete_all_subwindows (XWINDOW (p->hchild));
1697 else if (! NILP (p->vchild))
1698 delete_all_subwindows (XWINDOW (p->vchild));
1699
1700 /* Mark this window as deleted. */
1701 p->buffer = p->hchild = p->vchild = Qnil;
1702
1703 if (! NILP (par->parent))
1704 par = XWINDOW (par->parent);
1705
1706 /* Check if we have a v/hchild with a v/hchild. In that case remove
1707 one of them. */
1708
1709 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1710 {
1711 p = XWINDOW (par->vchild);
1712 par->vchild = p->vchild;
1713 tem = p->vchild;
1714 }
1715 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1716 {
1717 p = XWINDOW (par->hchild);
1718 par->hchild = p->hchild;
1719 tem = p->hchild;
1720 }
1721 else
1722 p = 0;
1723
1724 if (p)
1725 {
1726 while (! NILP (tem)) {
1727 XWINDOW (tem)->parent = p->parent;
1728 if (NILP (XWINDOW (tem)->next))
1729 break;
1730 tem = XWINDOW (tem)->next;
1731 }
1732 if (! NILP (tem)) {
1733 /* The next of the v/hchild we are removing is now the next of the
1734 last child for the v/hchild:
1735 Before v/hchild -> v/hchild -> next1 -> next2
1736 |
1737 -> next3
1738 After: v/hchild -> next1 -> next2 -> next3
1739 */
1740 XWINDOW (tem)->next = p->next;
1741 if (! NILP (p->next))
1742 XWINDOW (p->next)->prev = tem;
1743 }
1744 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1745 }
1746
1747
1748 /* Adjust glyph matrices. */
1749 adjust_glyphs (f);
1750 UNBLOCK_INPUT;
1751 }
1752
1753
1754 \f
1755 /***********************************************************************
1756 Window List
1757 ***********************************************************************/
1758
1759 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1760 pointer. This is a callback function for foreach_window, used in
1761 function window_list. */
1762
1763 static int
1764 add_window_to_list (w, user_data)
1765 struct window *w;
1766 void *user_data;
1767 {
1768 Lisp_Object *list = (Lisp_Object *) user_data;
1769 Lisp_Object window;
1770 XSETWINDOW (window, w);
1771 *list = Fcons (window, *list);
1772 return 1;
1773 }
1774
1775
1776 /* Return a list of all windows, for use by next_window. If
1777 Vwindow_list is a list, return that list. Otherwise, build a new
1778 list, cache it in Vwindow_list, and return that. */
1779
1780 static Lisp_Object
1781 window_list ()
1782 {
1783 if (!CONSP (Vwindow_list))
1784 {
1785 Lisp_Object tail;
1786
1787 Vwindow_list = Qnil;
1788 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1789 {
1790 Lisp_Object args[2];
1791
1792 /* We are visiting windows in canonical order, and add
1793 new windows at the front of args[1], which means we
1794 have to reverse this list at the end. */
1795 args[1] = Qnil;
1796 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1797 args[0] = Vwindow_list;
1798 args[1] = Fnreverse (args[1]);
1799 Vwindow_list = Fnconc (2, args);
1800 }
1801 }
1802
1803 return Vwindow_list;
1804 }
1805
1806
1807 /* Value is non-zero if WINDOW satisfies the constraints given by
1808 OWINDOW, MINIBUF and ALL_FRAMES.
1809
1810 MINIBUF t means WINDOW may be minibuffer windows.
1811 `lambda' means WINDOW may not be a minibuffer window.
1812 a window means a specific minibuffer window
1813
1814 ALL_FRAMES t means search all frames,
1815 nil means search just current frame,
1816 `visible' means search just visible frames,
1817 0 means search visible and iconified frames,
1818 a window means search the frame that window belongs to,
1819 a frame means consider windows on that frame, only. */
1820
1821 static int
1822 candidate_window_p (window, owindow, minibuf, all_frames)
1823 Lisp_Object window, owindow, minibuf, all_frames;
1824 {
1825 struct window *w = XWINDOW (window);
1826 struct frame *f = XFRAME (w->frame);
1827 int candidate_p = 1;
1828
1829 if (!BUFFERP (w->buffer))
1830 candidate_p = 0;
1831 else if (MINI_WINDOW_P (w)
1832 && (EQ (minibuf, Qlambda)
1833 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1834 {
1835 /* If MINIBUF is `lambda' don't consider any mini-windows.
1836 If it is a window, consider only that one. */
1837 candidate_p = 0;
1838 }
1839 else if (EQ (all_frames, Qt))
1840 candidate_p = 1;
1841 else if (NILP (all_frames))
1842 {
1843 xassert (WINDOWP (owindow));
1844 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1845 }
1846 else if (EQ (all_frames, Qvisible))
1847 {
1848 FRAME_SAMPLE_VISIBILITY (f);
1849 candidate_p = FRAME_VISIBLE_P (f)
1850 && (FRAME_TERMINAL (XFRAME (w->frame))
1851 == FRAME_TERMINAL (XFRAME (selected_frame)));
1852
1853 }
1854 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1855 {
1856 FRAME_SAMPLE_VISIBILITY (f);
1857 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
1858 #ifdef HAVE_X_WINDOWS
1859 /* Yuck!! If we've just created the frame and the
1860 window-manager requested the user to place it
1861 manually, the window may still not be considered
1862 `visible'. I'd argue it should be at least
1863 something like `iconified', but don't know how to do
1864 that yet. --Stef */
1865 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
1866 && !f->output_data.x->has_been_visible)
1867 #endif
1868 )
1869 && (FRAME_TERMINAL (XFRAME (w->frame))
1870 == FRAME_TERMINAL (XFRAME (selected_frame)));
1871 }
1872 else if (WINDOWP (all_frames))
1873 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1874 || EQ (XWINDOW (all_frames)->frame, w->frame)
1875 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1876 else if (FRAMEP (all_frames))
1877 candidate_p = EQ (all_frames, w->frame);
1878
1879 return candidate_p;
1880 }
1881
1882
1883 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1884 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1885 ALL_FRAMES. */
1886
1887 static void
1888 decode_next_window_args (window, minibuf, all_frames)
1889 Lisp_Object *window, *minibuf, *all_frames;
1890 {
1891 if (NILP (*window))
1892 *window = selected_window;
1893 else
1894 CHECK_LIVE_WINDOW (*window);
1895
1896 /* MINIBUF nil may or may not include minibuffers. Decide if it
1897 does. */
1898 if (NILP (*minibuf))
1899 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1900 else if (!EQ (*minibuf, Qt))
1901 *minibuf = Qlambda;
1902
1903 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1904 => count none of them, or a specific minibuffer window (the
1905 active one) to count. */
1906
1907 /* ALL_FRAMES nil doesn't specify which frames to include. */
1908 if (NILP (*all_frames))
1909 *all_frames = (!EQ (*minibuf, Qlambda)
1910 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1911 : Qnil);
1912 else if (EQ (*all_frames, Qvisible))
1913 ;
1914 else if (EQ (*all_frames, make_number (0)))
1915 ;
1916 else if (FRAMEP (*all_frames))
1917 ;
1918 else if (!EQ (*all_frames, Qt))
1919 *all_frames = Qnil;
1920
1921 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1922 search just current frame, `visible' meaning search just visible
1923 frames, 0 meaning search visible and iconified frames, or a
1924 window, meaning search the frame that window belongs to, or a
1925 frame, meaning consider windows on that frame, only. */
1926 }
1927
1928
1929 /* Return the next or previous window of WINDOW in cyclic ordering
1930 of windows. NEXT_P non-zero means return the next window. See the
1931 documentation string of next-window for the meaning of MINIBUF and
1932 ALL_FRAMES. */
1933
1934 static Lisp_Object
1935 next_window (window, minibuf, all_frames, next_p)
1936 Lisp_Object window, minibuf, all_frames;
1937 int next_p;
1938 {
1939 decode_next_window_args (&window, &minibuf, &all_frames);
1940
1941 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1942 return the first window on the frame. */
1943 if (FRAMEP (all_frames)
1944 && !EQ (all_frames, XWINDOW (window)->frame))
1945 return Fframe_first_window (all_frames);
1946
1947 if (next_p)
1948 {
1949 Lisp_Object list;
1950
1951 /* Find WINDOW in the list of all windows. */
1952 list = Fmemq (window, window_list ());
1953
1954 /* Scan forward from WINDOW to the end of the window list. */
1955 if (CONSP (list))
1956 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1957 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1958 break;
1959
1960 /* Scan from the start of the window list up to WINDOW. */
1961 if (!CONSP (list))
1962 for (list = Vwindow_list;
1963 CONSP (list) && !EQ (XCAR (list), window);
1964 list = XCDR (list))
1965 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1966 break;
1967
1968 if (CONSP (list))
1969 window = XCAR (list);
1970 }
1971 else
1972 {
1973 Lisp_Object candidate, list;
1974
1975 /* Scan through the list of windows for candidates. If there are
1976 candidate windows in front of WINDOW, the last one of these
1977 is the one we want. If there are candidates following WINDOW
1978 in the list, again the last one of these is the one we want. */
1979 candidate = Qnil;
1980 for (list = window_list (); CONSP (list); list = XCDR (list))
1981 {
1982 if (EQ (XCAR (list), window))
1983 {
1984 if (WINDOWP (candidate))
1985 break;
1986 }
1987 else if (candidate_window_p (XCAR (list), window, minibuf,
1988 all_frames))
1989 candidate = XCAR (list);
1990 }
1991
1992 if (WINDOWP (candidate))
1993 window = candidate;
1994 }
1995
1996 return window;
1997 }
1998
1999
2000 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2001 doc: /* Return window following WINDOW in cyclic ordering of windows.
2002 WINDOW defaults to the selected window. The optional arguments
2003 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2004
2005 MINIBUF t means consider the minibuffer window even if the
2006 minibuffer is not active. MINIBUF nil or omitted means consider
2007 the minibuffer window only if the minibuffer is active. Any
2008 other value means do not consider the minibuffer window even if
2009 the minibuffer is active.
2010
2011 Several frames may share a single minibuffer; if the minibuffer
2012 is active, all windows on all frames that share that minibuffer
2013 are considered too. Therefore, if you are using a separate
2014 minibuffer frame and the minibuffer is active and MINIBUF says it
2015 counts, `next-window' considers the windows in the frame from
2016 which you entered the minibuffer, as well as the minibuffer
2017 window.
2018
2019 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
2020 frame, plus the minibuffer window if specified by the MINIBUF
2021 argument, see above. If the minibuffer counts, consider all
2022 windows on all frames that share that minibuffer too.
2023 ALL-FRAMES t means consider all windows on all existing frames.
2024 ALL-FRAMES `visible' means consider all windows on all visible
2025 frames.
2026 ALL-FRAMES 0 means consider all windows on all visible and
2027 iconified frames.
2028 ALL-FRAMES a frame means consider all windows on that frame only.
2029 Anything else means consider all windows on WINDOW's frame and no
2030 others.
2031
2032 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2033 `next-window' to iterate through the entire cycle of acceptable
2034 windows, eventually ending up back at the window you started with.
2035 `previous-window' traverses the same cycle, in the reverse order. */)
2036 (window, minibuf, all_frames)
2037 Lisp_Object window, minibuf, all_frames;
2038 {
2039 return next_window (window, minibuf, all_frames, 1);
2040 }
2041
2042
2043 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2044 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
2045 WINDOW defaults to the selected window. The optional arguments
2046 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2047 For the precise meaning of these arguments see `next-window'.
2048
2049 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2050 use `previous-window' to iterate through the entire cycle of
2051 acceptable windows, eventually ending up back at the window you
2052 started with. `next-window' traverses the same cycle, in the
2053 reverse order. */)
2054 (window, minibuf, all_frames)
2055 Lisp_Object window, minibuf, all_frames;
2056 {
2057 return next_window (window, minibuf, all_frames, 0);
2058 }
2059
2060
2061 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
2062 doc: /* Select another window in cyclic ordering of windows.
2063 COUNT specifies the number of windows to skip, starting with the
2064 selected window, before making the selection. If COUNT is
2065 positive, skip COUNT windows forwards. If COUNT is negative,
2066 skip -COUNT windows backwards. COUNT zero means do not skip any
2067 window, so select the selected window. In an interactive call,
2068 COUNT is the numeric prefix argument. Return nil.
2069
2070 This function uses `next-window' for finding the window to select.
2071 The argument ALL-FRAMES has the same meaning as in `next-window',
2072 but the MINIBUF argument of `next-window' is always effectively
2073 nil. */)
2074 (count, all_frames)
2075 Lisp_Object count, all_frames;
2076 {
2077 Lisp_Object window;
2078 int i;
2079
2080 CHECK_NUMBER (count);
2081 window = selected_window;
2082
2083 for (i = XINT (count); i > 0; --i)
2084 window = Fnext_window (window, Qnil, all_frames);
2085 for (; i < 0; ++i)
2086 window = Fprevious_window (window, Qnil, all_frames);
2087
2088 Fselect_window (window, Qnil);
2089 return Qnil;
2090 }
2091
2092
2093 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2094 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2095 FRAME nil or omitted means use the selected frame.
2096 WINDOW nil or omitted means use the selected window.
2097 MINIBUF t means include the minibuffer window, even if it isn't active.
2098 MINIBUF nil or omitted means include the minibuffer window only
2099 if it's active.
2100 MINIBUF neither nil nor t means never include the minibuffer window. */)
2101 (frame, minibuf, window)
2102 Lisp_Object frame, minibuf, window;
2103 {
2104 if (NILP (window))
2105 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2106 CHECK_WINDOW (window);
2107 if (NILP (frame))
2108 frame = selected_frame;
2109
2110 if (!EQ (frame, XWINDOW (window)->frame))
2111 error ("Window is on a different frame");
2112
2113 return window_list_1 (window, minibuf, frame);
2114 }
2115
2116
2117 /* Return a list of windows in cyclic ordering. Arguments are like
2118 for `next-window'. */
2119
2120 static Lisp_Object
2121 window_list_1 (window, minibuf, all_frames)
2122 Lisp_Object window, minibuf, all_frames;
2123 {
2124 Lisp_Object tail, list, rest;
2125
2126 decode_next_window_args (&window, &minibuf, &all_frames);
2127 list = Qnil;
2128
2129 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2130 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2131 list = Fcons (XCAR (tail), list);
2132
2133 /* Rotate the list to start with WINDOW. */
2134 list = Fnreverse (list);
2135 rest = Fmemq (window, list);
2136 if (!NILP (rest) && !EQ (rest, list))
2137 {
2138 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2139 ;
2140 XSETCDR (tail, Qnil);
2141 list = nconc2 (rest, list);
2142 }
2143 return list;
2144 }
2145
2146
2147 \f
2148 /* Look at all windows, performing an operation specified by TYPE
2149 with argument OBJ.
2150 If FRAMES is Qt, look at all frames;
2151 Qnil, look at just the selected frame;
2152 Qvisible, look at visible frames;
2153 a frame, just look at windows on that frame.
2154 If MINI is non-zero, perform the operation on minibuffer windows too. */
2155
2156 enum window_loop
2157 {
2158 WINDOW_LOOP_UNUSED,
2159 GET_BUFFER_WINDOW, /* Arg is buffer */
2160 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2161 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2162 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2163 GET_LARGEST_WINDOW,
2164 UNSHOW_BUFFER, /* Arg is buffer */
2165 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2166 CHECK_ALL_WINDOWS
2167 };
2168
2169 static Lisp_Object
2170 window_loop (type, obj, mini, frames)
2171 enum window_loop type;
2172 Lisp_Object obj, frames;
2173 int mini;
2174 {
2175 Lisp_Object window, windows, best_window, frame_arg;
2176 struct frame *f;
2177 struct gcpro gcpro1;
2178
2179 /* If we're only looping through windows on a particular frame,
2180 frame points to that frame. If we're looping through windows
2181 on all frames, frame is 0. */
2182 if (FRAMEP (frames))
2183 f = XFRAME (frames);
2184 else if (NILP (frames))
2185 f = SELECTED_FRAME ();
2186 else
2187 f = NULL;
2188
2189 if (f)
2190 frame_arg = Qlambda;
2191 else if (EQ (frames, make_number (0)))
2192 frame_arg = frames;
2193 else if (EQ (frames, Qvisible))
2194 frame_arg = frames;
2195 else
2196 frame_arg = Qt;
2197
2198 /* frame_arg is Qlambda to stick to one frame,
2199 Qvisible to consider all visible frames,
2200 or Qt otherwise. */
2201
2202 /* Pick a window to start with. */
2203 if (WINDOWP (obj))
2204 window = obj;
2205 else if (f)
2206 window = FRAME_SELECTED_WINDOW (f);
2207 else
2208 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2209
2210 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2211 GCPRO1 (windows);
2212 best_window = Qnil;
2213
2214 for (; CONSP (windows); windows = XCDR (windows))
2215 {
2216 struct window *w;
2217
2218 window = XCAR (windows);
2219 w = XWINDOW (window);
2220
2221 /* Note that we do not pay attention here to whether the frame
2222 is visible, since Fwindow_list skips non-visible frames if
2223 that is desired, under the control of frame_arg. */
2224 if (!MINI_WINDOW_P (w)
2225 /* For UNSHOW_BUFFER, we must always consider all windows. */
2226 || type == UNSHOW_BUFFER
2227 || (mini && minibuf_level > 0))
2228 switch (type)
2229 {
2230 case GET_BUFFER_WINDOW:
2231 if (EQ (w->buffer, obj)
2232 /* Don't find any minibuffer window
2233 except the one that is currently in use. */
2234 && (MINI_WINDOW_P (w)
2235 ? EQ (window, minibuf_window)
2236 : 1))
2237 {
2238 if (NILP (best_window))
2239 best_window = window;
2240 else if (EQ (window, selected_window))
2241 /* Prefer to return selected-window. */
2242 RETURN_UNGCPRO (window);
2243 else if (EQ (Fwindow_frame (window), selected_frame))
2244 /* Prefer windows on the current frame. */
2245 best_window = window;
2246 }
2247 break;
2248
2249 case GET_LRU_WINDOW:
2250 /* `obj' is an integer encoding a bitvector.
2251 `obj & 1' means consider only full-width windows.
2252 `obj & 2' means consider also dedicated windows. */
2253 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2254 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2255 /* Minibuffer windows are always ignored. */
2256 || MINI_WINDOW_P (w))
2257 break;
2258 if (NILP (best_window)
2259 || (XFASTINT (XWINDOW (best_window)->use_time)
2260 > XFASTINT (w->use_time)))
2261 best_window = window;
2262 break;
2263
2264 case DELETE_OTHER_WINDOWS:
2265 if (!EQ (window, obj))
2266 Fdelete_window (window);
2267 break;
2268
2269 case DELETE_BUFFER_WINDOWS:
2270 if (EQ (w->buffer, obj))
2271 {
2272 struct frame *f = XFRAME (WINDOW_FRAME (w));
2273
2274 /* If this window is dedicated, and in a frame of its own,
2275 kill the frame. */
2276 if (EQ (window, FRAME_ROOT_WINDOW (f))
2277 && !NILP (w->dedicated)
2278 && other_visible_frames (f))
2279 {
2280 /* Skip the other windows on this frame.
2281 There might be one, the minibuffer! */
2282 while (CONSP (XCDR (windows))
2283 && EQ (XWINDOW (XCAR (windows))->frame,
2284 XWINDOW (XCAR (XCDR (windows)))->frame))
2285 windows = XCDR (windows);
2286
2287 /* Now we can safely delete the frame. */
2288 delete_frame (w->frame, Qnil);
2289 }
2290 else if (NILP (w->parent))
2291 {
2292 /* If we're deleting the buffer displayed in the
2293 only window on the frame, find a new buffer to
2294 display there. */
2295 Lisp_Object buffer;
2296 buffer = Fother_buffer (obj, Qnil, w->frame);
2297 /* Reset dedicated state of window. */
2298 w->dedicated = Qnil;
2299 Fset_window_buffer (window, buffer, Qnil);
2300 if (EQ (window, selected_window))
2301 Fset_buffer (w->buffer);
2302 }
2303 else
2304 Fdelete_window (window);
2305 }
2306 break;
2307
2308 case GET_LARGEST_WINDOW:
2309 { /* nil `obj' means to ignore dedicated windows. */
2310 /* Ignore dedicated windows and minibuffers. */
2311 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2312 break;
2313
2314 if (NILP (best_window))
2315 best_window = window;
2316 else
2317 {
2318 struct window *b = XWINDOW (best_window);
2319 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2320 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2321 best_window = window;
2322 }
2323 }
2324 break;
2325
2326 case UNSHOW_BUFFER:
2327 if (EQ (w->buffer, obj))
2328 {
2329 Lisp_Object buffer;
2330 struct frame *f = XFRAME (w->frame);
2331
2332 /* Find another buffer to show in this window. */
2333 buffer = Fother_buffer (obj, Qnil, w->frame);
2334
2335 /* If this window is dedicated, and in a frame of its own,
2336 kill the frame. */
2337 if (EQ (window, FRAME_ROOT_WINDOW (f))
2338 && !NILP (w->dedicated)
2339 && other_visible_frames (f))
2340 {
2341 /* Skip the other windows on this frame.
2342 There might be one, the minibuffer! */
2343 while (CONSP (XCDR (windows))
2344 && EQ (XWINDOW (XCAR (windows))->frame,
2345 XWINDOW (XCAR (XCDR (windows)))->frame))
2346 windows = XCDR (windows);
2347
2348 /* Now we can safely delete the frame. */
2349 delete_frame (w->frame, Qnil);
2350 }
2351 else if (!NILP (w->dedicated) && !NILP (w->parent))
2352 {
2353 Lisp_Object window;
2354 XSETWINDOW (window, w);
2355 /* If this window is dedicated and not the only window
2356 in its frame, then kill it. */
2357 Fdelete_window (window);
2358 }
2359 else
2360 {
2361 /* Otherwise show a different buffer in the window. */
2362 w->dedicated = Qnil;
2363 Fset_window_buffer (window, buffer, Qnil);
2364 if (EQ (window, selected_window))
2365 Fset_buffer (w->buffer);
2366 }
2367 }
2368 break;
2369
2370 case REDISPLAY_BUFFER_WINDOWS:
2371 if (EQ (w->buffer, obj))
2372 {
2373 mark_window_display_accurate (window, 0);
2374 w->update_mode_line = Qt;
2375 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2376 ++update_mode_lines;
2377 best_window = window;
2378 }
2379 break;
2380
2381 /* Check for a window that has a killed buffer. */
2382 case CHECK_ALL_WINDOWS:
2383 if (! NILP (w->buffer)
2384 && NILP (XBUFFER (w->buffer)->name))
2385 abort ();
2386 break;
2387
2388 case WINDOW_LOOP_UNUSED:
2389 break;
2390 }
2391 }
2392
2393 UNGCPRO;
2394 return best_window;
2395 }
2396
2397 /* Used for debugging. Abort if any window has a dead buffer. */
2398
2399 void
2400 check_all_windows ()
2401 {
2402 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2403 }
2404
2405 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2406 doc: /* Return the window least recently selected or used for display.
2407 \(LRU means Least Recently Used.)
2408
2409 Return a full-width window if possible.
2410 A minibuffer window is never a candidate.
2411 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2412 so if all windows are dedicated, the value is nil.
2413 If optional argument FRAME is `visible', search all visible frames.
2414 If FRAME is 0, search all visible and iconified frames.
2415 If FRAME is t, search all frames.
2416 If FRAME is nil, search only the selected frame.
2417 If FRAME is a frame, search only that frame. */)
2418 (frame, dedicated)
2419 Lisp_Object frame, dedicated;
2420 {
2421 register Lisp_Object w;
2422 /* First try for a window that is full-width */
2423 w = window_loop (GET_LRU_WINDOW,
2424 NILP (dedicated) ? make_number (1) : make_number (3),
2425 0, frame);
2426 if (!NILP (w) && !EQ (w, selected_window))
2427 return w;
2428 /* If none of them, try the rest */
2429 return window_loop (GET_LRU_WINDOW,
2430 NILP (dedicated) ? make_number (0) : make_number (2),
2431 0, frame);
2432 }
2433
2434 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2435 doc: /* Return the largest window in area.
2436 A minibuffer window is never a candidate.
2437 A dedicated window is never a candidate unless DEDICATED is non-nil,
2438 so if all windows are dedicated, the value is nil.
2439 If optional argument FRAME is `visible', search all visible frames.
2440 If FRAME is 0, search all visible and iconified frames.
2441 If FRAME is t, search all frames.
2442 If FRAME is nil, search only the selected frame.
2443 If FRAME is a frame, search only that frame. */)
2444 (frame, dedicated)
2445 Lisp_Object frame, dedicated;
2446 {
2447 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2448 frame);
2449 }
2450
2451 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2452 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2453 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to the
2454 current buffer.
2455 If optional argument FRAME is `visible', search all visible frames.
2456 If optional argument FRAME is 0, search all visible and iconified frames.
2457 If FRAME is t, search all frames.
2458 If FRAME is nil, search only the selected frame.
2459 If FRAME is a frame, search only that frame. */)
2460 (buffer_or_name, frame)
2461 Lisp_Object buffer_or_name, frame;
2462 {
2463 Lisp_Object buffer;
2464
2465 if (NILP (buffer_or_name))
2466 buffer = Fcurrent_buffer ();
2467 else
2468 buffer = Fget_buffer (buffer_or_name);
2469
2470 if (BUFFERP (buffer))
2471 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2472 else
2473 return Qnil;
2474 }
2475
2476 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2477 0, 1, "",
2478 doc: /* Make WINDOW (or the selected window) fill its frame.
2479 Only the frame WINDOW is on is affected.
2480 This function tries to reduce display jumps by keeping the text
2481 previously visible in WINDOW in the same place on the frame. Doing this
2482 depends on the value of (window-start WINDOW), so if calling this
2483 function in a program gives strange scrolling, make sure the
2484 window-start value is reasonable when this function is called. */)
2485 (window)
2486 Lisp_Object window;
2487 {
2488 struct window *w;
2489 int startpos;
2490 int top, new_top;
2491
2492 if (NILP (window))
2493 window = selected_window;
2494 else
2495 CHECK_LIVE_WINDOW (window);
2496 w = XWINDOW (window);
2497
2498 startpos = marker_position (w->start);
2499 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2500
2501 if (MINI_WINDOW_P (w) && top > 0)
2502 error ("Can't expand minibuffer to full frame");
2503
2504 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2505
2506 /* Try to minimize scrolling, by setting the window start to the point
2507 will cause the text at the old window start to be at the same place
2508 on the frame. But don't try to do this if the window start is
2509 outside the visible portion (as might happen when the display is
2510 not current, due to typeahead). */
2511 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2512 if (new_top != top
2513 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2514 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2515 {
2516 struct position pos;
2517 struct buffer *obuf = current_buffer;
2518
2519 Fset_buffer (w->buffer);
2520 /* This computation used to temporarily move point, but that can
2521 have unwanted side effects due to text properties. */
2522 pos = *vmotion (startpos, -top, w);
2523
2524 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2525 w->window_end_valid = Qnil;
2526 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2527 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2528 : Qnil);
2529 /* We need to do this, so that the window-scroll-functions
2530 get called. */
2531 w->optional_new_start = Qt;
2532
2533 set_buffer_internal (obuf);
2534 }
2535
2536 return Qnil;
2537 }
2538
2539 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2540 0, 2, "bDelete windows on (buffer): ",
2541 doc: /* Delete all windows showing BUFFER-OR-NAME.
2542 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2543 defaults to the current buffer.
2544
2545 Optional second argument FRAME controls which frames are affected.
2546 If optional argument FRAME is `visible', search all visible frames.
2547 If FRAME is 0, search all visible and iconified frames.
2548 If FRAME is nil, search all frames.
2549 If FRAME is t, search only the selected frame.
2550 If FRAME is a frame, search only that frame.
2551 When a window showing BUFFER-OR-NAME is dedicated and the only window of
2552 its frame, that frame is deleted when there are other frames left. */)
2553 (buffer_or_name, frame)
2554 Lisp_Object buffer_or_name, frame;
2555 {
2556 Lisp_Object buffer;
2557
2558 /* FRAME uses t and nil to mean the opposite of what window_loop
2559 expects. */
2560 if (NILP (frame))
2561 frame = Qt;
2562 else if (EQ (frame, Qt))
2563 frame = Qnil;
2564
2565 if (NILP (buffer_or_name))
2566 buffer = Fcurrent_buffer ();
2567 else
2568 {
2569 buffer = Fget_buffer (buffer_or_name);
2570 CHECK_BUFFER (buffer);
2571 }
2572
2573 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2574
2575 return Qnil;
2576 }
2577
2578 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2579 Sreplace_buffer_in_windows,
2580 0, 1, "bReplace buffer in windows: ",
2581 doc: /* Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2582 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2583 defaults to the current buffer.
2584
2585 When a window showing BUFFER-OR-NAME is dedicated that window is
2586 deleted. If that window is the only window on its frame, that frame is
2587 deleted too when there are other frames left. If there are no other
2588 frames left, some other buffer is displayed in that window. */)
2589 (buffer_or_name)
2590 Lisp_Object buffer_or_name;
2591 {
2592 Lisp_Object buffer;
2593
2594 if (NILP (buffer_or_name))
2595 buffer = Fcurrent_buffer ();
2596 else
2597 {
2598 buffer = Fget_buffer (buffer_or_name);
2599 CHECK_BUFFER (buffer);
2600 }
2601
2602 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2603
2604 return Qnil;
2605 }
2606
2607 /* Replace BUFFER with some other buffer in all windows
2608 of all frames, even those on other keyboards. */
2609
2610 void
2611 replace_buffer_in_all_windows (buffer)
2612 Lisp_Object buffer;
2613 {
2614 Lisp_Object tail, frame;
2615
2616 /* A single call to window_loop won't do the job
2617 because it only considers frames on the current keyboard.
2618 So loop manually over frames, and handle each one. */
2619 FOR_EACH_FRAME (tail, frame)
2620 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2621 }
2622 \f
2623 /* Set the height of WINDOW and all its inferiors. */
2624
2625 /* The smallest acceptable dimensions for a window. Anything smaller
2626 might crash Emacs. */
2627
2628 #define MIN_SAFE_WINDOW_WIDTH (2)
2629 #define MIN_SAFE_WINDOW_HEIGHT (1)
2630
2631 /* For wp non-zero the total number of columns of window w. Otherwise
2632 the total number of lines of w. */
2633
2634 #define WINDOW_TOTAL_SIZE(w, wp) \
2635 (wp ? WINDOW_TOTAL_COLS (w) : WINDOW_TOTAL_LINES (w))
2636
2637 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2638 minimum allowable size. */
2639
2640 void
2641 check_frame_size (frame, rows, cols)
2642 FRAME_PTR frame;
2643 int *rows, *cols;
2644 {
2645 /* For height, we have to see:
2646 how many windows the frame has at minimum (one or two),
2647 and whether it has a menu bar or other special stuff at the top. */
2648 int min_height
2649 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2650 ? MIN_SAFE_WINDOW_HEIGHT
2651 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2652
2653 if (FRAME_TOP_MARGIN (frame) > 0)
2654 min_height += FRAME_TOP_MARGIN (frame);
2655
2656 if (*rows < min_height)
2657 *rows = min_height;
2658 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2659 *cols = MIN_SAFE_WINDOW_WIDTH;
2660 }
2661
2662 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2663 check if W's width can be changed, otherwise check W's height.
2664 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2665 siblings, too. If none of the siblings is resizable, WINDOW isn't
2666 either. */
2667
2668 static int
2669 window_fixed_size_p (w, width_p, check_siblings_p)
2670 struct window *w;
2671 int width_p, check_siblings_p;
2672 {
2673 int fixed_p;
2674 struct window *c;
2675
2676 if (!NILP (w->hchild))
2677 {
2678 c = XWINDOW (w->hchild);
2679
2680 if (width_p)
2681 {
2682 /* A horizontal combination is fixed-width if all of if its
2683 children are. */
2684 while (c && window_fixed_size_p (c, width_p, 0))
2685 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2686 fixed_p = c == NULL;
2687 }
2688 else
2689 {
2690 /* A horizontal combination is fixed-height if one of if its
2691 children is. */
2692 while (c && !window_fixed_size_p (c, width_p, 0))
2693 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2694 fixed_p = c != NULL;
2695 }
2696 }
2697 else if (!NILP (w->vchild))
2698 {
2699 c = XWINDOW (w->vchild);
2700
2701 if (width_p)
2702 {
2703 /* A vertical combination is fixed-width if one of if its
2704 children is. */
2705 while (c && !window_fixed_size_p (c, width_p, 0))
2706 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2707 fixed_p = c != NULL;
2708 }
2709 else
2710 {
2711 /* A vertical combination is fixed-height if all of if its
2712 children are. */
2713 while (c && window_fixed_size_p (c, width_p, 0))
2714 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2715 fixed_p = c == NULL;
2716 }
2717 }
2718 else if (BUFFERP (w->buffer))
2719 {
2720 struct buffer *old = current_buffer;
2721 Lisp_Object val;
2722
2723 current_buffer = XBUFFER (w->buffer);
2724 val = find_symbol_value (Qwindow_size_fixed);
2725 current_buffer = old;
2726
2727 fixed_p = 0;
2728 if (!EQ (val, Qunbound))
2729 {
2730 fixed_p = !NILP (val);
2731
2732 if (fixed_p
2733 && ((EQ (val, Qheight) && width_p)
2734 || (EQ (val, Qwidth) && !width_p)))
2735 fixed_p = 0;
2736 }
2737
2738 /* Can't tell if this one is resizable without looking at
2739 siblings. If all siblings are fixed-size this one is too. */
2740 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2741 {
2742 Lisp_Object child;
2743
2744 for (child = w->prev; WINDOWP (child); child = XWINDOW (child)->prev)
2745 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2746 break;
2747
2748 if (NILP (child))
2749 for (child = w->next; WINDOWP (child); child = XWINDOW (child)->next)
2750 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2751 break;
2752
2753 if (NILP (child))
2754 fixed_p = 1;
2755 }
2756 }
2757 else
2758 fixed_p = 1;
2759
2760 return fixed_p;
2761 }
2762
2763 /* Return minimum size of leaf window W. WIDTH_P non-zero means return
2764 the minimum width of W, WIDTH_P zero means return the minimum height
2765 of W. SAFE_P non-zero means ignore window-min-height|width but just
2766 return values that won't crash Emacs and don't hide components like
2767 fringes, scrollbars, or modelines. If WIDTH_P is zero and W is the
2768 minibuffer window, always return 1. */
2769
2770 static int
2771 window_min_size_2 (w, width_p, safe_p)
2772 struct window *w;
2773 int width_p, safe_p;
2774 {
2775 /* We should consider buffer-local values of window_min_height and
2776 window_min_width here. */
2777 if (width_p)
2778 {
2779 int safe_size = (MIN_SAFE_WINDOW_WIDTH
2780 + WINDOW_FRINGE_COLS (w)
2781 + WINDOW_SCROLL_BAR_COLS (w));
2782
2783 return safe_p ? safe_size : max (window_min_width, safe_size);
2784 }
2785 else if (MINI_WINDOW_P (w))
2786 return 1;
2787 else
2788 {
2789 int safe_size = (MIN_SAFE_WINDOW_HEIGHT
2790 + ((BUFFERP (w->buffer)
2791 && !NILP (XBUFFER (w->buffer)->mode_line_format))
2792 ? 1 : 0));
2793
2794 return safe_p ? safe_size : max (window_min_height, safe_size);
2795 }
2796 }
2797
2798 /* Return minimum size of window W, not taking fixed-width windows into
2799 account. WIDTH_P non-zero means return the minimum width, otherwise
2800 return the minimum height. SAFE_P non-zero means ignore
2801 window-min-height|width but just return values that won't crash Emacs
2802 and don't hide components like fringes, scrollbars, or modelines. If
2803 W is a combination window, compute the minimum size from the minimum
2804 sizes of W's children. */
2805
2806 static int
2807 window_min_size_1 (w, width_p, safe_p)
2808 struct window *w;
2809 int width_p, safe_p;
2810 {
2811 struct window *c;
2812 int size;
2813
2814 if (!NILP (w->hchild))
2815 {
2816 /* W is a horizontal combination. */
2817 c = XWINDOW (w->hchild);
2818 size = 0;
2819
2820 if (width_p)
2821 {
2822 /* The minimum width of a horizontal combination is the sum of
2823 the minimum widths of its children. */
2824 while (c)
2825 {
2826 size += window_min_size_1 (c, 1, safe_p);
2827 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2828 }
2829 }
2830 else
2831 {
2832 /* The minimum height of a horizontal combination is the
2833 maximum of the minimum heights of its children. */
2834 while (c)
2835 {
2836 size = max (window_min_size_1 (c, 0, safe_p), size);
2837 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2838 }
2839 }
2840 }
2841 else if (!NILP (w->vchild))
2842 {
2843 /* W is a vertical combination. */
2844 c = XWINDOW (w->vchild);
2845 size = 0;
2846
2847 if (width_p)
2848 {
2849 /* The minimum width of a vertical combination is the maximum
2850 of the minimum widths of its children. */
2851 while (c)
2852 {
2853 size = max (window_min_size_1 (c, 1, safe_p), size);
2854 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2855 }
2856 }
2857 else
2858 {
2859 /* The minimum height of a vertical combination is the sum of
2860 the minimum height of its children. */
2861 while (c)
2862 {
2863 size += window_min_size_1 (c, 0, safe_p);
2864 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2865 }
2866 }
2867 }
2868 else
2869 /* W is a leaf window. */
2870 size = window_min_size_2 (w, width_p, safe_p);
2871
2872 return size;
2873 }
2874
2875 /* Return the minimum size of window W, taking fixed-size windows into
2876 account. WIDTH_P non-zero means return the minimum width, otherwise
2877 return the minimum height. SAFE_P non-zero means ignore
2878 window-min-height|width but just return values that won't crash Emacs
2879 and don't hide components like fringes, scrollbars, or modelines.
2880 IGNORE_FIXED_P non-zero means ignore if W is fixed-size. Set *FIXED
2881 to 1 if W is fixed-size unless FIXED is null. */
2882
2883 static int
2884 window_min_size (w, width_p, safe_p, ignore_fixed_p, fixed)
2885 struct window *w;
2886 int width_p, safe_p, ignore_fixed_p, *fixed;
2887 {
2888 int size, fixed_p;
2889
2890 if (ignore_fixed_p)
2891 fixed_p = 0;
2892 else
2893 fixed_p = window_fixed_size_p (w, width_p, 1);
2894
2895 if (fixed)
2896 *fixed = fixed_p;
2897
2898 if (fixed_p)
2899 size = WINDOW_TOTAL_SIZE (w, width_p);
2900 else
2901 size = window_min_size_1 (w, width_p, safe_p);
2902
2903 return size;
2904 }
2905
2906
2907 /* Adjust the margins of window W if text area is too small.
2908 Return 1 if window width is ok after adjustment; 0 if window
2909 is still too narrow. */
2910
2911 static int
2912 adjust_window_margins (w)
2913 struct window *w;
2914 {
2915 int box_cols = (WINDOW_TOTAL_COLS (w)
2916 - WINDOW_FRINGE_COLS (w)
2917 - WINDOW_SCROLL_BAR_COLS (w));
2918 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2919 + WINDOW_RIGHT_MARGIN_COLS (w));
2920
2921 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2922 return 1;
2923
2924 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2925 return 0;
2926
2927 /* Window's text area is too narrow, but reducing the window
2928 margins will fix that. */
2929 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2930 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2931 {
2932 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2933 w->left_margin_cols = w->right_margin_cols
2934 = make_number (margin_cols/2);
2935 else
2936 w->right_margin_cols = make_number (margin_cols);
2937 }
2938 else
2939 w->left_margin_cols = make_number (margin_cols);
2940 return 1;
2941 }
2942
2943 /* Calculate new sizes for windows in the list FORWARD when their
2944 compound size goes from TOTAL to SIZE. TOTAL must be greater than
2945 SIZE. The number of windows in FORWARD is NCHILDREN, and the number
2946 that can shrink is SHRINKABLE. Fixed-size windows may be shrunk if
2947 and only if RESIZE_FIXED_P is non-zero. WIDTH_P non-zero means
2948 shrink columns, otherwise shrink lines.
2949
2950 SAFE_P zero means windows may be sized down to window-min-height
2951 lines (window-min-window columns for WIDTH_P non-zero). SAFE_P
2952 non-zero means windows may be sized down to their minimum safe sizes
2953 taking into account the space needed to display modelines, fringes,
2954 and scrollbars.
2955
2956 This function returns an allocated array of new sizes that the caller
2957 must free. A size -1 means the window is fixed and RESIZE_FIXED_P is
2958 zero. A size zero means the window shall be deleted. Array index 0
2959 refers to the first window in FORWARD, 1 to the second, and so on.
2960
2961 This function resizes windows proportionally to their size. It also
2962 tries to preserve smaller windows by resizing larger windows before
2963 resizing any window to zero. If resize_proportionally is non-nil for
2964 a specific window, it will attempt to strictly resize that window
2965 proportionally, even at the expense of deleting smaller windows. */
2966 static int *
2967 shrink_windows (total, size, nchildren, shrinkable, resize_fixed_p,
2968 forward, width_p, safe_p)
2969 int total, size, nchildren, shrinkable;
2970 int resize_fixed_p, width_p, safe_p;
2971 Lisp_Object forward;
2972 {
2973 int available_resize = 0;
2974 int *new_sizes, *min_sizes;
2975 struct window *c;
2976 Lisp_Object child;
2977 int smallest = total;
2978 int total_removed = 0;
2979 int total_shrink = total - size;
2980 int i;
2981
2982 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2983 min_sizes = xmalloc (sizeof (*min_sizes) * nchildren);
2984
2985 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2986 {
2987 int child_size;
2988
2989 c = XWINDOW (child);
2990 child_size = WINDOW_TOTAL_SIZE (c, width_p);
2991
2992 if (!resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2993 new_sizes[i] = -1;
2994 else
2995 {
2996 new_sizes[i] = child_size;
2997 min_sizes[i] = window_min_size_1 (c, width_p, safe_p);
2998 if (child_size > min_sizes[i]
2999 && NILP (c->resize_proportionally))
3000 available_resize += child_size - min_sizes[i];
3001 }
3002 }
3003 /* We might need to shrink some windows to zero. Find the smallest
3004 windows and set them to 0 until we can fulfil the new size. */
3005
3006 while (shrinkable > 1 && size + available_resize < total)
3007 {
3008 for (i = 0; i < nchildren; ++i)
3009 if (new_sizes[i] > 0 && smallest > new_sizes[i])
3010 smallest = new_sizes[i];
3011
3012 for (i = 0; i < nchildren; ++i)
3013 if (new_sizes[i] == smallest)
3014 {
3015 /* Resize this window down to zero. */
3016 new_sizes[i] = 0;
3017 if (smallest > min_sizes[i])
3018 available_resize -= smallest - min_sizes[i];
3019 available_resize += smallest;
3020 --shrinkable;
3021 total_removed += smallest;
3022
3023 /* We don't know what the smallest is now. */
3024 smallest = total;
3025
3026 /* Out of for, just remove one window at the time and
3027 check again if we have enough space. */
3028 break;
3029 }
3030 }
3031
3032 /* Now, calculate the new sizes. Try to shrink each window
3033 proportional to its size. */
3034 for (i = 0; i < nchildren; ++i)
3035 {
3036 if (new_sizes[i] > min_sizes[i])
3037 {
3038 int to_shrink = total_shrink * new_sizes[i] / total;
3039
3040 if (new_sizes[i] - to_shrink < min_sizes[i])
3041 to_shrink = new_sizes[i] - min_sizes[i];
3042 new_sizes[i] -= to_shrink;
3043 total_removed += to_shrink;
3044 }
3045 }
3046
3047 /* Any reminder due to rounding, we just subtract from windows
3048 that are left and still can be shrunk. */
3049 while (total_shrink > total_removed)
3050 {
3051 int nonzero_sizes = 0;
3052 int nonzero_idx = -1;
3053
3054 for (i = 0; i < nchildren; ++i)
3055 if (new_sizes[i] > 0)
3056 {
3057 ++nonzero_sizes;
3058 nonzero_idx = i;
3059 }
3060
3061 for (i = 0; i < nchildren; ++i)
3062 if (new_sizes[i] > min_sizes[i])
3063 {
3064 --new_sizes[i];
3065 ++total_removed;
3066
3067 /* Out of for, just shrink one window at the time and
3068 check again if we have enough space. */
3069 break;
3070 }
3071
3072 /* Special case, only one window left. */
3073 if (nonzero_sizes == 1)
3074 break;
3075 }
3076
3077 /* Any surplus due to rounding, we add to windows that are left. */
3078 while (total_shrink < total_removed)
3079 {
3080 for (i = 0; i < nchildren; ++i)
3081 {
3082 if (new_sizes[i] != 0 && total_shrink < total_removed)
3083 {
3084 ++new_sizes[i];
3085 --total_removed;
3086 break;
3087 }
3088 }
3089 }
3090
3091 xfree (min_sizes);
3092
3093 return new_sizes;
3094 }
3095
3096 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
3097 WINDOW's width. Resize WINDOW's children, if any, so that they keep
3098 their proportionate size relative to WINDOW.
3099
3100 If FIRST_ONLY is 1, change only the first of WINDOW's children when
3101 they are in series. If LAST_ONLY is 1, change only the last of
3102 WINDOW's children when they are in series.
3103
3104 Propagate WINDOW's top or left edge position to children. Delete
3105 windows that become too small unless NODELETE_P is 1. When
3106 NODELETE_P equals 2 do not honor settings for window-min-height and
3107 window-min-width when resizing windows but use safe defaults instead.
3108 This should give better behavior when resizing frames. */
3109
3110 static void
3111 size_window (window, size, width_p, nodelete_p, first_only, last_only)
3112 Lisp_Object window;
3113 int size, width_p, nodelete_p;
3114 int first_only, last_only;
3115 {
3116 struct window *w = XWINDOW (window);
3117 struct window *c;
3118 Lisp_Object child, *forward, *sideward;
3119 int old_size = WINDOW_TOTAL_SIZE (w, width_p);
3120
3121 size = max (0, size);
3122
3123 /* Delete WINDOW if it's too small. */
3124 if (nodelete_p != 1 && !NILP (w->parent)
3125 && size < window_min_size_1 (w, width_p, nodelete_p == 2))
3126 {
3127 delete_window (window);
3128 return;
3129 }
3130
3131 /* Set redisplay hints. */
3132 w->last_modified = make_number (0);
3133 w->last_overlay_modified = make_number (0);
3134 windows_or_buffers_changed++;
3135 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
3136
3137 if (width_p)
3138 {
3139 sideward = &w->vchild;
3140 forward = &w->hchild;
3141 w->total_cols = make_number (size);
3142 adjust_window_margins (w);
3143 }
3144 else
3145 {
3146 sideward = &w->hchild;
3147 forward = &w->vchild;
3148 w->total_lines = make_number (size);
3149 w->orig_total_lines = Qnil;
3150 }
3151
3152 if (!NILP (*sideward))
3153 {
3154 /* We have a chain of parallel siblings whose size should all change. */
3155 for (child = *sideward; !NILP (child); child = c->next)
3156 {
3157 c = XWINDOW (child);
3158 if (width_p)
3159 c->left_col = w->left_col;
3160 else
3161 c->top_line = w->top_line;
3162 size_window (child, size, width_p, nodelete_p,
3163 first_only, last_only);
3164 }
3165 }
3166 else if (!NILP (*forward) && last_only)
3167 {
3168 /* Change the last in a series of siblings. */
3169 Lisp_Object last_child;
3170 int child_size;
3171
3172 for (child = *forward; !NILP (child); child = c->next)
3173 {
3174 c = XWINDOW (child);
3175 last_child = child;
3176 }
3177
3178 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3179 size_window (last_child, size - old_size + child_size,
3180 width_p, nodelete_p, first_only, last_only);
3181 }
3182 else if (!NILP (*forward) && first_only)
3183 {
3184 /* Change the first in a series of siblings. */
3185 int child_size;
3186
3187 child = *forward;
3188 c = XWINDOW (child);
3189
3190 if (width_p)
3191 c->left_col = w->left_col;
3192 else
3193 c->top_line = w->top_line;
3194
3195 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3196 size_window (child, size - old_size + child_size,
3197 width_p, nodelete_p, first_only, last_only);
3198 }
3199 else if (!NILP (*forward))
3200 {
3201 int fixed_size, each, extra, n;
3202 int resize_fixed_p, nfixed;
3203 int last_pos, first_pos, nchildren, total;
3204 int *new_sizes = NULL;
3205
3206 /* Determine the fixed-size portion of this window, and the
3207 number of child windows. */
3208 fixed_size = nchildren = nfixed = total = 0;
3209 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
3210 {
3211 int child_size;
3212
3213 c = XWINDOW (child);
3214 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3215 total += child_size;
3216
3217 if (window_fixed_size_p (c, width_p, 0))
3218 {
3219 fixed_size += child_size;
3220 ++nfixed;
3221 }
3222 }
3223
3224 /* If the new size is smaller than fixed_size, or if there
3225 aren't any resizable windows, allow resizing fixed-size
3226 windows. */
3227 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3228
3229 /* Compute how many lines/columns to add/remove to each child. The
3230 value of extra takes care of rounding errors. */
3231 n = resize_fixed_p ? nchildren : nchildren - nfixed;
3232 if (size < total && n > 1)
3233 new_sizes = shrink_windows (total, size, nchildren, n,
3234 resize_fixed_p, *forward, width_p,
3235 nodelete_p == 2);
3236 else
3237 {
3238 each = (size - total) / n;
3239 extra = (size - total) - n * each;
3240 }
3241
3242 /* Compute new children heights and edge positions. */
3243 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
3244 last_pos = first_pos;
3245 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
3246 {
3247 int new_size, old_size;
3248
3249 c = XWINDOW (child);
3250 old_size = WINDOW_TOTAL_SIZE (c, width_p);
3251 new_size = old_size;
3252
3253 /* The top or left edge position of this child equals the
3254 bottom or right edge of its predecessor. */
3255 if (width_p)
3256 c->left_col = make_number (last_pos);
3257 else
3258 c->top_line = make_number (last_pos);
3259
3260 /* If this child can be resized, do it. */
3261 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3262 {
3263 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
3264 extra = 0;
3265 }
3266
3267 /* Set new size. Note that size_window also propagates
3268 edge positions to children, so it's not a no-op if we
3269 didn't change the child's size. */
3270 size_window (child, new_size, width_p, 1, first_only, last_only);
3271
3272 /* Remember the bottom/right edge position of this child; it
3273 will be used to set the top/left edge of the next child. */
3274 last_pos += new_size;
3275 }
3276
3277 xfree (new_sizes);
3278
3279 /* We should have covered the parent exactly with child windows. */
3280 xassert (size == last_pos - first_pos);
3281
3282 /* Now delete any children that became too small. */
3283 if (nodelete_p != 1)
3284 for (child = *forward; !NILP (child); child = c->next)
3285 {
3286 int child_size;
3287
3288 c = XWINDOW (child);
3289 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3290 size_window (child, child_size, width_p, nodelete_p,
3291 first_only, last_only);
3292 }
3293 }
3294 }
3295
3296 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3297 WINDOW's children. NODELETE zero means windows that have become
3298 smaller than window-min-height in the process may be deleted.
3299 NODELETE 1 means never delete windows that become too small in the
3300 process. (The caller should check later and do so if appropriate.)
3301 NODELETE 2 means delete only windows that have become too small to be
3302 displayed correctly. */
3303
3304 void
3305 set_window_height (window, height, nodelete)
3306 Lisp_Object window;
3307 int height;
3308 int nodelete;
3309 {
3310 size_window (window, height, 0, nodelete, 0, 0);
3311 }
3312
3313 /* Set WINDOW's width to WIDTH, and recursively change the width of
3314 WINDOW's children. NODELETE zero means windows that have become
3315 smaller than window-min-width in the process may be deleted.
3316 NODELETE 1 means never delete windows that become too small in the
3317 process. (The caller should check later and do so if appropriate.)
3318 NODELETE 2 means delete only windows that have become too small to be
3319 displayed correctly. */
3320
3321 void
3322 set_window_width (window, width, nodelete)
3323 Lisp_Object window;
3324 int width;
3325 int nodelete;
3326 {
3327 size_window (window, width, 1, nodelete, 0, 0);
3328 }
3329
3330 /* Change window heights in windows rooted in WINDOW by N lines. */
3331
3332 void
3333 change_window_heights (window, n)
3334 Lisp_Object window;
3335 int n;
3336 {
3337 struct window *w = XWINDOW (window);
3338
3339 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3340 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3341
3342 if (INTEGERP (w->orig_top_line))
3343 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3344 if (INTEGERP (w->orig_total_lines))
3345 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3346
3347 /* Handle just the top child in a vertical split. */
3348 if (!NILP (w->vchild))
3349 change_window_heights (w->vchild, n);
3350
3351 /* Adjust all children in a horizontal split. */
3352 for (window = w->hchild; !NILP (window); window = w->next)
3353 {
3354 w = XWINDOW (window);
3355 change_window_heights (window, n);
3356 }
3357 }
3358
3359 \f
3360 int window_select_count;
3361
3362 EXFUN (Fset_window_fringes, 4);
3363 EXFUN (Fset_window_scroll_bars, 4);
3364
3365 static void
3366 run_funs (Lisp_Object funs)
3367 {
3368 for (; CONSP (funs); funs = XCDR (funs))
3369 if (!EQ (XCAR (funs), Qt))
3370 call0 (XCAR (funs));
3371 }
3372
3373 static Lisp_Object select_window_norecord (Lisp_Object window);
3374 static Lisp_Object select_frame_norecord (Lisp_Object frame);
3375
3376 void
3377 run_window_configuration_change_hook (struct frame *f)
3378 {
3379 int count = SPECPDL_INDEX ();
3380 Lisp_Object frame, global_wcch
3381 = Fdefault_value (Qwindow_configuration_change_hook);
3382 XSETFRAME (frame, f);
3383
3384 if (NILP (Vrun_hooks))
3385 return;
3386
3387 if (SELECTED_FRAME () != f)
3388 {
3389 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3390 Fselect_frame (frame, Qt);
3391 }
3392
3393 /* Use the right buffer. Matters when running the local hooks. */
3394 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3395 {
3396 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3397 Fset_buffer (Fwindow_buffer (Qnil));
3398 }
3399
3400 /* Look for buffer-local values. */
3401 {
3402 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3403 for (; CONSP (windows); windows = XCDR (windows))
3404 {
3405 Lisp_Object window = XCAR (windows);
3406 Lisp_Object buffer = Fwindow_buffer (window);
3407 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3408 buffer)))
3409 {
3410 int count = SPECPDL_INDEX ();
3411 record_unwind_protect (select_window_norecord, Fselected_window ());
3412 select_window_norecord (window);
3413 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3414 buffer));
3415 unbind_to (count, Qnil);
3416 }
3417 }
3418 }
3419
3420 run_funs (global_wcch);
3421 unbind_to (count, Qnil);
3422 }
3423
3424 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3425 means it's allowed to run hooks. See make_frame for a case where
3426 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3427 margins, fringes, and scroll-bar settings of the window are not
3428 reset from the buffer's local settings. */
3429
3430 void
3431 set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
3432 Lisp_Object window, buffer;
3433 int run_hooks_p, keep_margins_p;
3434 {
3435 struct window *w = XWINDOW (window);
3436 struct buffer *b = XBUFFER (buffer);
3437 int count = SPECPDL_INDEX ();
3438 int samebuf = EQ (buffer, w->buffer);
3439
3440 w->buffer = buffer;
3441
3442 if (EQ (window, selected_window))
3443 b->last_selected_window = window;
3444
3445 /* Let redisplay errors through. */
3446 b->display_error_modiff = 0;
3447
3448 /* Update time stamps of buffer display. */
3449 if (INTEGERP (b->display_count))
3450 XSETINT (b->display_count, XINT (b->display_count) + 1);
3451 b->display_time = Fcurrent_time ();
3452
3453 XSETFASTINT (w->window_end_pos, 0);
3454 XSETFASTINT (w->window_end_vpos, 0);
3455 bzero (&w->last_cursor, sizeof w->last_cursor);
3456 w->window_end_valid = Qnil;
3457 if (!(keep_margins_p && samebuf))
3458 { /* If we're not actually changing the buffer, don't reset hscroll and
3459 vscroll. This case happens for example when called from
3460 change_frame_size_1, where we use a dummy call to
3461 Fset_window_buffer on the frame's selected window (and no other)
3462 just in order to run window-configuration-change-hook.
3463 Resetting hscroll and vscroll here is problematic for things like
3464 image-mode and doc-view-mode since it resets the image's position
3465 whenever we resize the frame. */
3466 w->hscroll = w->min_hscroll = make_number (0);
3467 w->vscroll = 0;
3468 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3469 set_marker_restricted (w->start,
3470 make_number (b->last_window_start),
3471 buffer);
3472 w->start_at_line_beg = Qnil;
3473 w->force_start = Qnil;
3474 XSETFASTINT (w->last_modified, 0);
3475 XSETFASTINT (w->last_overlay_modified, 0);
3476 }
3477 /* Maybe we could move this into the `if' but it's not obviously safe and
3478 I doubt it's worth the trouble. */
3479 windows_or_buffers_changed++;
3480
3481 /* We must select BUFFER for running the window-scroll-functions. */
3482 /* We can't check ! NILP (Vwindow_scroll_functions) here
3483 because that might itself be a local variable. */
3484 if (window_initialized)
3485 {
3486 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3487 Fset_buffer (buffer);
3488 }
3489
3490 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3491
3492 if (!keep_margins_p)
3493 {
3494 /* Set left and right marginal area width etc. from buffer. */
3495
3496 /* This may call adjust_window_margins three times, so
3497 temporarily disable window margins. */
3498 Lisp_Object save_left = w->left_margin_cols;
3499 Lisp_Object save_right = w->right_margin_cols;
3500
3501 w->left_margin_cols = w->right_margin_cols = Qnil;
3502
3503 Fset_window_fringes (window,
3504 b->left_fringe_width, b->right_fringe_width,
3505 b->fringes_outside_margins);
3506
3507 Fset_window_scroll_bars (window,
3508 b->scroll_bar_width,
3509 b->vertical_scroll_bar_type, Qnil);
3510
3511 w->left_margin_cols = save_left;
3512 w->right_margin_cols = save_right;
3513
3514 Fset_window_margins (window,
3515 b->left_margin_cols, b->right_margin_cols);
3516 }
3517
3518 if (run_hooks_p)
3519 {
3520 if (! NILP (Vwindow_scroll_functions))
3521 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3522 Fmarker_position (w->start));
3523 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3524 }
3525
3526 unbind_to (count, Qnil);
3527 }
3528
3529
3530 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3531 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3532 WINDOW defaults to the selected window. BUFFER-OR-NAME must be a buffer
3533 or the name of an existing buffer. Optional third argument KEEP-MARGINS
3534 non-nil means that WINDOW's current display margins, fringe widths, and
3535 scroll bar settings are preserved; the default is to reset these from
3536 the local settings for BUFFER-OR-NAME or the frame defaults. Return nil.
3537
3538 This function throws an error when WINDOW is strongly dedicated to its
3539 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3540 already display BUFFER-OR-NAME.
3541
3542 This function runs `window-scroll-functions' before running
3543 `window-configuration-change-hook'. */)
3544 (window, buffer_or_name, keep_margins)
3545 register Lisp_Object window, buffer_or_name, keep_margins;
3546 {
3547 register Lisp_Object tem, buffer;
3548 register struct window *w = decode_window (window);
3549
3550 XSETWINDOW (window, w);
3551 buffer = Fget_buffer (buffer_or_name);
3552 CHECK_BUFFER (buffer);
3553 if (NILP (XBUFFER (buffer)->name))
3554 error ("Attempt to display deleted buffer");
3555
3556 tem = w->buffer;
3557 if (NILP (tem))
3558 error ("Window is deleted");
3559 else if (!EQ (tem, Qt))
3560 /* w->buffer is t when the window is first being set up. */
3561 {
3562 if (!EQ (tem, buffer))
3563 if (EQ (w->dedicated, Qt))
3564 error ("Window is dedicated to `%s'", SDATA (XBUFFER (tem)->name));
3565 else
3566 w->dedicated = Qnil;
3567
3568 unshow_buffer (w);
3569 }
3570
3571 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3572 return Qnil;
3573 }
3574
3575 /* Note that selected_window can be nil when this is called from
3576 Fset_window_configuration. */
3577
3578 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3579 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3580 If WINDOW is not already selected, make WINDOW's buffer current
3581 and make WINDOW the frame's selected window. Return WINDOW.
3582 Optional second arg NORECORD non-nil means do not put this buffer
3583 at the front of the list of recently selected ones and do not
3584 make this window the most recently selected one.
3585
3586 Note that the main editor command loop selects the buffer of the
3587 selected window before each command. */)
3588 (window, norecord)
3589 register Lisp_Object window, norecord;
3590 {
3591 register struct window *w;
3592 register struct window *ow;
3593 struct frame *sf;
3594
3595 CHECK_LIVE_WINDOW (window);
3596
3597 w = XWINDOW (window);
3598 w->frozen_window_start_p = 0;
3599
3600 if (NILP (norecord))
3601 {
3602 ++window_select_count;
3603 XSETFASTINT (w->use_time, window_select_count);
3604 }
3605
3606 if (EQ (window, selected_window))
3607 return window;
3608
3609 sf = SELECTED_FRAME ();
3610 if (XFRAME (WINDOW_FRAME (w)) != sf)
3611 {
3612 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3613 /* Use this rather than Fhandle_switch_frame
3614 so that FRAME_FOCUS_FRAME is moved appropriately as we
3615 move around in the state where a minibuffer in a separate
3616 frame is active. */
3617 Fselect_frame (WINDOW_FRAME (w), norecord);
3618 /* Fselect_frame called us back so we've done all the work already. */
3619 eassert (EQ (window, selected_window));
3620 return window;
3621 }
3622 else
3623 sf->selected_window = window;
3624
3625 /* Store the current buffer's actual point into the
3626 old selected window. It belongs to that window,
3627 and when the window is not selected, must be in the window. */
3628 if (!NILP (selected_window))
3629 {
3630 ow = XWINDOW (selected_window);
3631 if (! NILP (ow->buffer))
3632 set_marker_both (ow->pointm, ow->buffer,
3633 BUF_PT (XBUFFER (ow->buffer)),
3634 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3635 }
3636
3637 selected_window = window;
3638
3639 if (NILP (norecord))
3640 record_buffer (w->buffer);
3641 Fset_buffer (w->buffer);
3642
3643 XBUFFER (w->buffer)->last_selected_window = window;
3644
3645 /* Go to the point recorded in the window.
3646 This is important when the buffer is in more
3647 than one window. It also matters when
3648 redisplay_window has altered point after scrolling,
3649 because it makes the change only in the window. */
3650 {
3651 register int new_point = marker_position (w->pointm);
3652 if (new_point < BEGV)
3653 SET_PT (BEGV);
3654 else if (new_point > ZV)
3655 SET_PT (ZV);
3656 else
3657 SET_PT (new_point);
3658 }
3659
3660 windows_or_buffers_changed++;
3661 return window;
3662 }
3663
3664 static Lisp_Object
3665 select_window_norecord (window)
3666 Lisp_Object window;
3667 {
3668 return WINDOW_LIVE_P (window)
3669 ? Fselect_window (window, Qt) : selected_window;
3670 }
3671
3672 static Lisp_Object
3673 select_frame_norecord (frame)
3674 Lisp_Object frame;
3675 {
3676 return FRAME_LIVE_P (XFRAME (frame))
3677 ? Fselect_frame (frame, Qt) : selected_frame;
3678 }
3679 \f
3680 Lisp_Object
3681 display_buffer (buffer, not_this_window_p, override_frame)
3682 Lisp_Object buffer, not_this_window_p, override_frame;
3683 {
3684 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3685 }
3686
3687 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3688 0, 1, 0,
3689 doc: /* Force all windows to be updated on next redisplay.
3690 If optional arg OBJECT is a window, force redisplay of that window only.
3691 If OBJECT is a buffer or buffer name, force redisplay of all windows
3692 displaying that buffer. */)
3693 (object)
3694 Lisp_Object object;
3695 {
3696 if (NILP (object))
3697 {
3698 windows_or_buffers_changed++;
3699 update_mode_lines++;
3700 return Qt;
3701 }
3702
3703 if (WINDOWP (object))
3704 {
3705 struct window *w = XWINDOW (object);
3706 mark_window_display_accurate (object, 0);
3707 w->update_mode_line = Qt;
3708 if (BUFFERP (w->buffer))
3709 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3710 ++update_mode_lines;
3711 return Qt;
3712 }
3713
3714 if (STRINGP (object))
3715 object = Fget_buffer (object);
3716 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3717 {
3718 /* Walk all windows looking for buffer, and force update
3719 of each of those windows. */
3720
3721 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3722 return NILP (object) ? Qnil : Qt;
3723 }
3724
3725 /* If nothing suitable was found, just return.
3726 We could signal an error, but this feature will typically be used
3727 asynchronously in timers or process sentinels, so we don't. */
3728 return Qnil;
3729 }
3730
3731
3732 void
3733 temp_output_buffer_show (buf)
3734 register Lisp_Object buf;
3735 {
3736 register struct buffer *old = current_buffer;
3737 register Lisp_Object window;
3738 register struct window *w;
3739
3740 XBUFFER (buf)->directory = current_buffer->directory;
3741
3742 Fset_buffer (buf);
3743 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3744 BEGV = BEG;
3745 ZV = Z;
3746 SET_PT (BEG);
3747 #if 0 /* rms: there should be no reason for this. */
3748 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3749 #endif
3750 set_buffer_internal (old);
3751
3752 if (!NILP (Vtemp_buffer_show_function))
3753 call1 (Vtemp_buffer_show_function, buf);
3754 else
3755 {
3756 window = display_buffer (buf, Qnil, Qnil);
3757
3758 if (!EQ (XWINDOW (window)->frame, selected_frame))
3759 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3760 Vminibuf_scroll_window = window;
3761 w = XWINDOW (window);
3762 XSETFASTINT (w->hscroll, 0);
3763 XSETFASTINT (w->min_hscroll, 0);
3764 set_marker_restricted_both (w->start, buf, BEG, BEG);
3765 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3766
3767 /* Run temp-buffer-show-hook, with the chosen window selected
3768 and its buffer current. */
3769
3770 if (!NILP (Vrun_hooks)
3771 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3772 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3773 {
3774 int count = SPECPDL_INDEX ();
3775 Lisp_Object prev_window, prev_buffer;
3776 prev_window = selected_window;
3777 XSETBUFFER (prev_buffer, old);
3778
3779 /* Select the window that was chosen, for running the hook.
3780 Note: Both Fselect_window and select_window_norecord may
3781 set-buffer to the buffer displayed in the window,
3782 so we need to save the current buffer. --stef */
3783 record_unwind_protect (Fset_buffer, prev_buffer);
3784 record_unwind_protect (select_window_norecord, prev_window);
3785 Fselect_window (window, Qt);
3786 Fset_buffer (w->buffer);
3787 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3788 unbind_to (count, Qnil);
3789 }
3790 }
3791 }
3792 \f
3793 static void
3794 make_dummy_parent (window)
3795 Lisp_Object window;
3796 {
3797 Lisp_Object new;
3798 register struct window *o, *p;
3799 int i;
3800
3801 o = XWINDOW (window);
3802 p = allocate_window ();
3803 for (i = 0; i < VECSIZE (struct window); ++i)
3804 ((struct Lisp_Vector *) p)->contents[i]
3805 = ((struct Lisp_Vector *)o)->contents[i];
3806 XSETWINDOW (new, p);
3807
3808 ++sequence_number;
3809 XSETFASTINT (p->sequence_number, sequence_number);
3810
3811 /* Put new into window structure in place of window */
3812 replace_window (window, new);
3813
3814 o->next = Qnil;
3815 o->prev = Qnil;
3816 o->vchild = Qnil;
3817 o->hchild = Qnil;
3818 o->parent = new;
3819
3820 p->start = Qnil;
3821 p->pointm = Qnil;
3822 p->buffer = Qnil;
3823 }
3824
3825 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3826 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3827 WINDOW defaults to selected one and SIZE to half its size.
3828 If optional third arg HORIZONTAL is non-nil, split side by side and put
3829 SIZE columns in the first of the pair. In that case, SIZE includes that
3830 window's scroll bar, or the divider column to its right.
3831 Interactively, all arguments are nil.
3832 Returns the newly created window (which is the lower or rightmost one).
3833 The upper or leftmost window is the original one, and remains selected
3834 if it was selected before.
3835
3836 See Info node `(elisp)Splitting Windows' for more details and examples. */)
3837 (window, size, horizontal)
3838 Lisp_Object window, size, horizontal;
3839 {
3840 register Lisp_Object new;
3841 register struct window *o, *p;
3842 FRAME_PTR fo;
3843 register int size_int;
3844
3845 if (NILP (window))
3846 window = selected_window;
3847 else
3848 CHECK_LIVE_WINDOW (window);
3849
3850 o = XWINDOW (window);
3851 fo = XFRAME (WINDOW_FRAME (o));
3852
3853 if (NILP (size))
3854 {
3855 if (!NILP (horizontal))
3856 /* Calculate the size of the left-hand window, by dividing
3857 the usable space in columns by two.
3858 We round up, since the left-hand window may include
3859 a dividing line, while the right-hand may not. */
3860 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3861 else
3862 size_int = XFASTINT (o->total_lines) >> 1;
3863 }
3864 else
3865 {
3866 CHECK_NUMBER (size);
3867 size_int = XINT (size);
3868 }
3869
3870 if (MINI_WINDOW_P (o))
3871 error ("Attempt to split minibuffer window");
3872 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
3873 error ("Attempt to split fixed-size window");
3874
3875 if (NILP (horizontal))
3876 {
3877 int window_safe_height = window_min_size_2 (o, 0, 0);
3878
3879 if (size_int < window_safe_height)
3880 error ("Window height %d too small (after splitting)", size_int);
3881 if (size_int + window_safe_height > XFASTINT (o->total_lines))
3882 error ("Window height %d too small (after splitting)",
3883 XFASTINT (o->total_lines) - size_int);
3884 if (NILP (o->parent)
3885 || NILP (XWINDOW (o->parent)->vchild))
3886 {
3887 make_dummy_parent (window);
3888 new = o->parent;
3889 XWINDOW (new)->vchild = window;
3890 }
3891 }
3892 else
3893 {
3894 int window_safe_width = window_min_size_2 (o, 1, 0);
3895
3896 if (size_int < window_safe_width)
3897 error ("Window width %d too small (after splitting)", size_int);
3898 if (size_int + window_safe_width > XFASTINT (o->total_cols))
3899 error ("Window width %d too small (after splitting)",
3900 XFASTINT (o->total_cols) - size_int);
3901 if (NILP (o->parent)
3902 || NILP (XWINDOW (o->parent)->hchild))
3903 {
3904 make_dummy_parent (window);
3905 new = o->parent;
3906 XWINDOW (new)->hchild = window;
3907 }
3908 }
3909
3910 /* Now we know that window's parent is a vertical combination
3911 if we are dividing vertically, or a horizontal combination
3912 if we are making side-by-side windows */
3913
3914 windows_or_buffers_changed++;
3915 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3916 new = make_window ();
3917 p = XWINDOW (new);
3918
3919 p->frame = o->frame;
3920 p->next = o->next;
3921 if (!NILP (p->next))
3922 XWINDOW (p->next)->prev = new;
3923 p->prev = window;
3924 o->next = new;
3925 p->parent = o->parent;
3926 p->buffer = Qt;
3927 p->window_end_valid = Qnil;
3928 bzero (&p->last_cursor, sizeof p->last_cursor);
3929
3930 /* Duplicate special geometry settings. */
3931
3932 p->left_margin_cols = o->left_margin_cols;
3933 p->right_margin_cols = o->right_margin_cols;
3934 p->left_fringe_width = o->left_fringe_width;
3935 p->right_fringe_width = o->right_fringe_width;
3936 p->fringes_outside_margins = o->fringes_outside_margins;
3937 p->scroll_bar_width = o->scroll_bar_width;
3938 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3939
3940 /* Apportion the available frame space among the two new windows */
3941
3942 if (!NILP (horizontal))
3943 {
3944 p->total_lines = o->total_lines;
3945 p->top_line = o->top_line;
3946 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3947 XSETFASTINT (o->total_cols, size_int);
3948 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3949 adjust_window_margins (p);
3950 adjust_window_margins (o);
3951 }
3952 else
3953 {
3954 p->left_col = o->left_col;
3955 p->total_cols = o->total_cols;
3956 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3957 XSETFASTINT (o->total_lines, size_int);
3958 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3959 }
3960
3961 /* Adjust glyph matrices. */
3962 adjust_glyphs (fo);
3963
3964 Fset_window_buffer (new, o->buffer, Qt);
3965 return new;
3966 }
3967 \f
3968 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3969 doc: /* Make selected window SIZE lines taller.
3970 Interactively, if no argument is given, make the selected window one
3971 line taller. If optional argument HORIZONTAL is non-nil, make selected
3972 window wider by SIZE columns. If SIZE is negative, shrink the window by
3973 -SIZE lines or columns. Return nil.
3974
3975 This function can delete windows if they get too small. The size of
3976 fixed size windows is not altered by this function. */)
3977 (size, horizontal)
3978 Lisp_Object size, horizontal;
3979 {
3980 CHECK_NUMBER (size);
3981 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
3982
3983 run_window_configuration_change_hook (SELECTED_FRAME ());
3984
3985 return Qnil;
3986 }
3987
3988 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3989 doc: /* Make selected window SIZE lines smaller.
3990 Interactively, if no argument is given, make the selected window one
3991 line smaller. If optional argument HORIZONTAL is non-nil, make the
3992 window narrower by SIZE columns. If SIZE is negative, enlarge selected
3993 window by -SIZE lines or columns. Return nil.
3994
3995 This function can delete windows if they get too small. The size of
3996 fixed size windows is not altered by this function. */)
3997 (size, horizontal)
3998 Lisp_Object size, horizontal;
3999 {
4000 CHECK_NUMBER (size);
4001 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
4002
4003 run_window_configuration_change_hook (SELECTED_FRAME ());
4004
4005 return Qnil;
4006 }
4007
4008 int
4009 window_height (window)
4010 Lisp_Object window;
4011 {
4012 register struct window *p = XWINDOW (window);
4013 return WINDOW_TOTAL_LINES (p);
4014 }
4015
4016 int
4017 window_width (window)
4018 Lisp_Object window;
4019 {
4020 register struct window *p = XWINDOW (window);
4021 return WINDOW_TOTAL_COLS (p);
4022 }
4023
4024
4025 #define CURBEG(w) \
4026 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
4027
4028 #define CURSIZE(w) \
4029 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
4030
4031
4032 /* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
4033 horizontally; zero means do it vertically.
4034
4035 Siblings of the selected window are resized to fulfill the size
4036 request. If they become too small in the process, they may be
4037 deleted. */
4038
4039 static void
4040 enlarge_window (window, delta, horiz_flag)
4041 Lisp_Object window;
4042 int delta, horiz_flag;
4043 {
4044 Lisp_Object parent, next, prev;
4045 struct window *p;
4046 Lisp_Object *sizep;
4047 int maximum;
4048 int (*sizefun) P_ ((Lisp_Object))
4049 = horiz_flag ? window_width : window_height;
4050 void (*setsizefun) P_ ((Lisp_Object, int, int))
4051 = (horiz_flag ? set_window_width : set_window_height);
4052
4053 /* Give up if this window cannot be resized. */
4054 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4055 error ("Window is not resizable");
4056
4057 /* Find the parent of the selected window. */
4058 while (1)
4059 {
4060 p = XWINDOW (window);
4061 parent = p->parent;
4062
4063 if (NILP (parent))
4064 {
4065 if (horiz_flag)
4066 error ("No other window to side of this one");
4067 break;
4068 }
4069
4070 if (horiz_flag
4071 ? !NILP (XWINDOW (parent)->hchild)
4072 : !NILP (XWINDOW (parent)->vchild))
4073 break;
4074
4075 window = parent;
4076 }
4077
4078 sizep = &CURSIZE (window);
4079
4080 {
4081 register int maxdelta;
4082
4083 /* Compute the maximum size increment this window can have. */
4084
4085 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4086 /* This is a main window followed by a minibuffer. */
4087 : !NILP (p->next) ? ((*sizefun) (p->next)
4088 - window_min_size (XWINDOW (p->next),
4089 horiz_flag, 0, 0, 0))
4090 /* This is a minibuffer following a main window. */
4091 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4092 - window_min_size (XWINDOW (p->prev),
4093 horiz_flag, 0, 0, 0))
4094 /* This is a frame with only one window, a minibuffer-only
4095 or a minibufferless frame. */
4096 : (delta = 0));
4097
4098 if (delta > maxdelta)
4099 /* This case traps trying to make the minibuffer
4100 the full frame, or make the only window aside from the
4101 minibuffer the full frame. */
4102 delta = maxdelta;
4103 }
4104
4105 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4106 horiz_flag, 0, 0, 0))
4107 {
4108 delete_window (window);
4109 return;
4110 }
4111
4112 if (delta == 0)
4113 return;
4114
4115 /* Find the total we can get from other siblings without deleting them. */
4116 maximum = 0;
4117 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
4118 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4119 horiz_flag, 0, 0, 0);
4120 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
4121 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4122 horiz_flag, 0, 0, 0);
4123
4124 /* If we can get it all from them without deleting them, do so. */
4125 if (delta <= maximum)
4126 {
4127 Lisp_Object first_unaffected;
4128 Lisp_Object first_affected;
4129 int fixed_p;
4130
4131 next = p->next;
4132 prev = p->prev;
4133 first_affected = window;
4134 /* Look at one sibling at a time,
4135 moving away from this window in both directions alternately,
4136 and take as much as we can get without deleting that sibling. */
4137 while (delta != 0
4138 && (!NILP (next) || !NILP (prev)))
4139 {
4140 if (! NILP (next))
4141 {
4142 int this_one = ((*sizefun) (next)
4143 - window_min_size (XWINDOW (next), horiz_flag,
4144 0, 0, &fixed_p));
4145 if (!fixed_p)
4146 {
4147 if (this_one > delta)
4148 this_one = delta;
4149
4150 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4151 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4152
4153 delta -= this_one;
4154 }
4155
4156 next = XWINDOW (next)->next;
4157 }
4158
4159 if (delta == 0)
4160 break;
4161
4162 if (! NILP (prev))
4163 {
4164 int this_one = ((*sizefun) (prev)
4165 - window_min_size (XWINDOW (prev), horiz_flag,
4166 0, 0, &fixed_p));
4167 if (!fixed_p)
4168 {
4169 if (this_one > delta)
4170 this_one = delta;
4171
4172 first_affected = prev;
4173
4174 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4175 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4176
4177 delta -= this_one;
4178 }
4179
4180 prev = XWINDOW (prev)->prev;
4181 }
4182 }
4183
4184 xassert (delta == 0);
4185
4186 /* Now recalculate the edge positions of all the windows affected,
4187 based on the new sizes. */
4188 first_unaffected = next;
4189 prev = first_affected;
4190 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4191 prev = next, next = XWINDOW (next)->next)
4192 {
4193 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4194 /* This does not change size of NEXT,
4195 but it propagates the new top edge to its children */
4196 (*setsizefun) (next, (*sizefun) (next), 0);
4197 }
4198 }
4199 else
4200 {
4201 register int delta1;
4202 register int opht = (*sizefun) (parent);
4203
4204 if (opht <= XINT (*sizep) + delta)
4205 {
4206 /* If trying to grow this window to or beyond size of the parent,
4207 just delete all the sibling windows. */
4208 Lisp_Object start, tem, next;
4209
4210 start = XWINDOW (parent)->vchild;
4211 if (NILP (start))
4212 start = XWINDOW (parent)->hchild;
4213
4214 /* Delete any siblings that come after WINDOW. */
4215 tem = XWINDOW (window)->next;
4216 while (! NILP (tem))
4217 {
4218 next = XWINDOW (tem)->next;
4219 delete_window (tem);
4220 tem = next;
4221 }
4222
4223 /* Delete any siblings that come after WINDOW.
4224 Note that if START is not WINDOW, then WINDOW still
4225 has siblings, so WINDOW has not yet replaced its parent. */
4226 tem = start;
4227 while (! EQ (tem, window))
4228 {
4229 next = XWINDOW (tem)->next;
4230 delete_window (tem);
4231 tem = next;
4232 }
4233 }
4234 else
4235 {
4236 /* Otherwise, make delta1 just right so that if we add
4237 delta1 lines to this window and to the parent, and then
4238 shrink the parent back to its original size, the new
4239 proportional size of this window will increase by delta.
4240
4241 The function size_window will compute the new height h'
4242 of the window from delta1 as:
4243
4244 e = delta1/n
4245 x = delta1 - delta1/n * n for the 1st resizable child
4246 h' = h + e + x
4247
4248 where n is the number of children that can be resized.
4249 We can ignore x by choosing a delta1 that is a multiple of
4250 n. We want the height of this window to come out as
4251
4252 h' = h + delta
4253
4254 So, delta1 must be
4255
4256 h + e = h + delta
4257 delta1/n = delta
4258 delta1 = n * delta.
4259
4260 The number of children n equals the number of resizable
4261 children of this window + 1 because we know window itself
4262 is resizable (otherwise we would have signaled an error).
4263
4264 This reasoning is not correct when other windows become too
4265 small and shrink_windows refuses to delete them. Below we
4266 use resize_proportionally to work around this problem. */
4267
4268 struct window *w = XWINDOW (window);
4269 Lisp_Object s;
4270 int n = 1;
4271
4272 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
4273 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4274 ++n;
4275 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
4276 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4277 ++n;
4278
4279 delta1 = n * delta;
4280
4281 /* Add delta1 lines or columns to this window, and to the parent,
4282 keeping things consistent while not affecting siblings. */
4283 XSETINT (CURSIZE (parent), opht + delta1);
4284 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4285
4286 /* Squeeze out delta1 lines or columns from our parent,
4287 shrinking this window and siblings proportionately. This
4288 brings parent back to correct size. Delta1 was calculated
4289 so this makes this window the desired size, taking it all
4290 out of the siblings.
4291
4292 Temporarily set resize_proportionally to Qt to assure that,
4293 if necessary, shrink_windows deletes smaller windows rather
4294 than shrink this window. */
4295 w->resize_proportionally = Qt;
4296 (*setsizefun) (parent, opht, 0);
4297 w->resize_proportionally = Qnil;
4298 }
4299 }
4300
4301 XSETFASTINT (p->last_modified, 0);
4302 XSETFASTINT (p->last_overlay_modified, 0);
4303
4304 /* Adjust glyph matrices. */
4305 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4306 }
4307
4308
4309 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4310 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4311 zero means adjust the height, moving the bottom edge.
4312
4313 Following siblings of the selected window are resized to fulfill
4314 the size request. If they become too small in the process, they
4315 are not deleted; instead, we signal an error. */
4316
4317 static void
4318 adjust_window_trailing_edge (window, delta, horiz_flag)
4319 Lisp_Object window;
4320 int delta, horiz_flag;
4321 {
4322 Lisp_Object parent, child;
4323 struct window *p;
4324 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4325 int delcount = window_deletion_count;
4326
4327 CHECK_WINDOW (window);
4328
4329 /* Give up if this window cannot be resized. */
4330 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4331 error ("Window is not resizable");
4332
4333 while (1)
4334 {
4335 Lisp_Object first_parallel = Qnil;
4336
4337 if (NILP (window))
4338 {
4339 /* This happens if WINDOW on the previous iteration was
4340 at top level of the window tree. */
4341 Fset_window_configuration (old_config);
4342 error ("Specified window edge is fixed");
4343 }
4344
4345 p = XWINDOW (window);
4346 parent = p->parent;
4347
4348 /* See if this level has windows in parallel in the specified
4349 direction. If so, set FIRST_PARALLEL to the first one. */
4350 if (horiz_flag)
4351 {
4352 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4353 first_parallel = XWINDOW (parent)->vchild;
4354 else if (NILP (parent) && !NILP (p->next))
4355 {
4356 /* Handle the vertical chain of main window and minibuffer
4357 which has no parent. */
4358 first_parallel = window;
4359 while (! NILP (XWINDOW (first_parallel)->prev))
4360 first_parallel = XWINDOW (first_parallel)->prev;
4361 }
4362 }
4363 else
4364 {
4365 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4366 first_parallel = XWINDOW (parent)->hchild;
4367 }
4368
4369 /* If this level's succession is in the desired dimension,
4370 and this window is the last one, and there is no higher level,
4371 its trailing edge is fixed. */
4372 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4373 && NILP (parent))
4374 {
4375 Fset_window_configuration (old_config);
4376 error ("Specified window edge is fixed");
4377 }
4378
4379 /* Don't make this window too small. */
4380 if (XINT (CURSIZE (window)) + delta
4381 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
4382 {
4383 Fset_window_configuration (old_config);
4384 error ("Cannot adjust window size as specified");
4385 }
4386
4387 /* Clear out some redisplay caches. */
4388 XSETFASTINT (p->last_modified, 0);
4389 XSETFASTINT (p->last_overlay_modified, 0);
4390
4391 /* Adjust this window's edge. */
4392 XSETINT (CURSIZE (window),
4393 XINT (CURSIZE (window)) + delta);
4394
4395 /* If this window has following siblings in the desired dimension,
4396 make them smaller, and exit the loop.
4397
4398 (If we reach the top of the tree and can never do this,
4399 we will fail and report an error, above.) */
4400 if (NILP (first_parallel))
4401 {
4402 if (!NILP (p->next))
4403 {
4404 /* This may happen for the minibuffer. In that case
4405 the window_deletion_count check below does not work. */
4406 if (XINT (CURSIZE (p->next)) - delta <= 0)
4407 {
4408 Fset_window_configuration (old_config);
4409 error ("Cannot adjust window size as specified");
4410 }
4411
4412 XSETINT (CURBEG (p->next),
4413 XINT (CURBEG (p->next)) + delta);
4414 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4415 horiz_flag, 0, 1, 0);
4416 break;
4417 }
4418 }
4419 else
4420 /* Here we have a chain of parallel siblings, in the other dimension.
4421 Change the size of the other siblings. */
4422 for (child = first_parallel;
4423 ! NILP (child);
4424 child = XWINDOW (child)->next)
4425 if (! EQ (child, window))
4426 size_window (child, XINT (CURSIZE (child)) + delta,
4427 horiz_flag, 0, 0, 1);
4428
4429 window = parent;
4430 }
4431
4432 /* If we made a window so small it got deleted,
4433 we failed. Report failure. */
4434 if (delcount != window_deletion_count)
4435 {
4436 Fset_window_configuration (old_config);
4437 error ("Cannot adjust window size as specified");
4438 }
4439
4440 /* Adjust glyph matrices. */
4441 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4442 }
4443
4444 #undef CURBEG
4445 #undef CURSIZE
4446
4447 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4448 Sadjust_window_trailing_edge, 3, 3, 0,
4449 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4450 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4451 Otherwise, adjust the height, moving the bottom edge.
4452
4453 Following siblings of the selected window are resized to fulfill
4454 the size request. If they become too small in the process, they
4455 are not deleted; instead, we signal an error. */)
4456 (window, delta, horizontal)
4457 Lisp_Object window, delta, horizontal;
4458 {
4459 CHECK_NUMBER (delta);
4460 if (NILP (window))
4461 window = selected_window;
4462 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4463
4464 run_window_configuration_change_hook
4465 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4466
4467 return Qnil;
4468 }
4469
4470
4471 \f
4472 /***********************************************************************
4473 Resizing Mini-Windows
4474 ***********************************************************************/
4475
4476 static void shrink_window_lowest_first P_ ((struct window *, int));
4477
4478 enum save_restore_action
4479 {
4480 CHECK_ORIG_SIZES,
4481 SAVE_ORIG_SIZES,
4482 RESTORE_ORIG_SIZES
4483 };
4484
4485 static int save_restore_orig_size P_ ((struct window *,
4486 enum save_restore_action));
4487
4488 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4489 from lowest windows first. */
4490
4491 static void
4492 shrink_window_lowest_first (w, height)
4493 struct window *w;
4494 int height;
4495 {
4496 struct window *c;
4497 Lisp_Object child;
4498 int old_height;
4499
4500 xassert (!MINI_WINDOW_P (w));
4501
4502 /* Set redisplay hints. */
4503 XSETFASTINT (w->last_modified, 0);
4504 XSETFASTINT (w->last_overlay_modified, 0);
4505 windows_or_buffers_changed++;
4506 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4507
4508 old_height = XFASTINT (w->total_lines);
4509 XSETFASTINT (w->total_lines, height);
4510
4511 if (!NILP (w->hchild))
4512 {
4513 for (child = w->hchild; !NILP (child); child = c->next)
4514 {
4515 c = XWINDOW (child);
4516 c->top_line = w->top_line;
4517 shrink_window_lowest_first (c, height);
4518 }
4519 }
4520 else if (!NILP (w->vchild))
4521 {
4522 Lisp_Object last_child;
4523 int delta = old_height - height;
4524 int last_top;
4525
4526 last_child = Qnil;
4527
4528 /* Find the last child. We are taking space from lowest windows
4529 first, so we iterate over children from the last child
4530 backwards. */
4531 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
4532 last_child = child;
4533
4534 /* Size children down to their safe heights. */
4535 for (child = last_child; delta && !NILP (child); child = c->prev)
4536 {
4537 int this_one;
4538
4539 c = XWINDOW (child);
4540 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
4541
4542 if (this_one > delta)
4543 this_one = delta;
4544
4545 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4546 delta -= this_one;
4547 }
4548
4549 /* Compute new positions. */
4550 last_top = XINT (w->top_line);
4551 for (child = w->vchild; !NILP (child); child = c->next)
4552 {
4553 c = XWINDOW (child);
4554 c->top_line = make_number (last_top);
4555 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4556 last_top += XFASTINT (c->total_lines);
4557 }
4558 }
4559 }
4560
4561
4562 /* Save, restore, or check positions and sizes in the window tree
4563 rooted at W. ACTION says what to do.
4564
4565 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4566 orig_total_lines members are valid for all windows in the window
4567 tree. Value is non-zero if they are valid.
4568
4569 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4570 orig_top_line and orig_total_lines for all windows in the tree.
4571
4572 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4573 stored in orig_top_line and orig_total_lines for all windows. */
4574
4575 static int
4576 save_restore_orig_size (w, action)
4577 struct window *w;
4578 enum save_restore_action action;
4579 {
4580 int success_p = 1;
4581
4582 while (w)
4583 {
4584 if (!NILP (w->hchild))
4585 {
4586 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4587 success_p = 0;
4588 }
4589 else if (!NILP (w->vchild))
4590 {
4591 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4592 success_p = 0;
4593 }
4594
4595 switch (action)
4596 {
4597 case CHECK_ORIG_SIZES:
4598 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4599 return 0;
4600 break;
4601
4602 case SAVE_ORIG_SIZES:
4603 w->orig_top_line = w->top_line;
4604 w->orig_total_lines = w->total_lines;
4605 XSETFASTINT (w->last_modified, 0);
4606 XSETFASTINT (w->last_overlay_modified, 0);
4607 break;
4608
4609 case RESTORE_ORIG_SIZES:
4610 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4611 w->top_line = w->orig_top_line;
4612 w->total_lines = w->orig_total_lines;
4613 w->orig_total_lines = w->orig_top_line = Qnil;
4614 XSETFASTINT (w->last_modified, 0);
4615 XSETFASTINT (w->last_overlay_modified, 0);
4616 break;
4617
4618 default:
4619 abort ();
4620 }
4621
4622 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4623 }
4624
4625 return success_p;
4626 }
4627
4628
4629 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4630 without deleting other windows. */
4631
4632 void
4633 grow_mini_window (w, delta)
4634 struct window *w;
4635 int delta;
4636 {
4637 struct frame *f = XFRAME (w->frame);
4638 struct window *root;
4639
4640 xassert (MINI_WINDOW_P (w));
4641 xassert (delta >= 0);
4642
4643 /* Compute how much we can enlarge the mini-window without deleting
4644 other windows. */
4645 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4646 if (delta)
4647 {
4648 int min_height = window_min_size (root, 0, 0, 0, 0);
4649 if (XFASTINT (root->total_lines) - delta < min_height)
4650 /* Note that the root window may already be smaller than
4651 min_height. */
4652 delta = max (0, XFASTINT (root->total_lines) - min_height);
4653 }
4654
4655 if (delta)
4656 {
4657 /* Save original window sizes and positions, if not already done. */
4658 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4659 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4660
4661 /* Shrink other windows. */
4662 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4663
4664 /* Grow the mini-window. */
4665 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4666 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4667 XSETFASTINT (w->last_modified, 0);
4668 XSETFASTINT (w->last_overlay_modified, 0);
4669
4670 adjust_glyphs (f);
4671 }
4672 }
4673
4674
4675 /* Shrink mini-window W. If there is recorded info about window sizes
4676 before a call to grow_mini_window, restore recorded window sizes.
4677 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4678 line. */
4679
4680 void
4681 shrink_mini_window (w)
4682 struct window *w;
4683 {
4684 struct frame *f = XFRAME (w->frame);
4685 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4686
4687 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4688 {
4689 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4690 adjust_glyphs (f);
4691 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4692 windows_or_buffers_changed = 1;
4693 }
4694 else if (XFASTINT (w->total_lines) > 1)
4695 {
4696 /* Distribute the additional lines of the mini-window
4697 among the other windows. */
4698 Lisp_Object window;
4699 XSETWINDOW (window, w);
4700 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4701 }
4702 }
4703
4704
4705 \f
4706 /* Mark window cursors off for all windows in the window tree rooted
4707 at W by setting their phys_cursor_on_p flag to zero. Called from
4708 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4709 the frame are cleared. */
4710
4711 void
4712 mark_window_cursors_off (w)
4713 struct window *w;
4714 {
4715 while (w)
4716 {
4717 if (!NILP (w->hchild))
4718 mark_window_cursors_off (XWINDOW (w->hchild));
4719 else if (!NILP (w->vchild))
4720 mark_window_cursors_off (XWINDOW (w->vchild));
4721 else
4722 w->phys_cursor_on_p = 0;
4723
4724 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4725 }
4726 }
4727
4728
4729 /* Return number of lines of text (not counting mode lines) in W. */
4730
4731 int
4732 window_internal_height (w)
4733 struct window *w;
4734 {
4735 int ht = XFASTINT (w->total_lines);
4736
4737 if (!MINI_WINDOW_P (w))
4738 {
4739 if (!NILP (w->parent)
4740 || !NILP (w->vchild)
4741 || !NILP (w->hchild)
4742 || !NILP (w->next)
4743 || !NILP (w->prev)
4744 || WINDOW_WANTS_MODELINE_P (w))
4745 --ht;
4746
4747 if (WINDOW_WANTS_HEADER_LINE_P (w))
4748 --ht;
4749 }
4750
4751 return ht;
4752 }
4753
4754
4755 /* Return the number of columns in W.
4756 Don't count columns occupied by scroll bars or the vertical bar
4757 separating W from the sibling to its right. */
4758
4759 int
4760 window_box_text_cols (w)
4761 struct window *w;
4762 {
4763 struct frame *f = XFRAME (WINDOW_FRAME (w));
4764 int width = XINT (w->total_cols);
4765
4766 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4767 /* Scroll bars occupy a few columns. */
4768 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4769 else if (!FRAME_WINDOW_P (f)
4770 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4771 /* The column of `|' characters separating side-by-side windows
4772 occupies one column only. */
4773 width -= 1;
4774
4775 if (FRAME_WINDOW_P (f))
4776 /* On window-systems, fringes and display margins cannot be
4777 used for normal text. */
4778 width -= (WINDOW_FRINGE_COLS (w)
4779 + WINDOW_LEFT_MARGIN_COLS (w)
4780 + WINDOW_RIGHT_MARGIN_COLS (w));
4781
4782 return width;
4783 }
4784
4785 \f
4786 /************************************************************************
4787 Window Scrolling
4788 ***********************************************************************/
4789
4790 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4791 N screen-fulls, which is defined as the height of the window minus
4792 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4793 instead. Negative values of N mean scroll down. NOERROR non-zero
4794 means don't signal an error if we try to move over BEGV or ZV,
4795 respectively. */
4796
4797 static void
4798 window_scroll (window, n, whole, noerror)
4799 Lisp_Object window;
4800 int n;
4801 int whole;
4802 int noerror;
4803 {
4804 immediate_quit = 1;
4805
4806 /* If we must, use the pixel-based version which is much slower than
4807 the line-based one but can handle varying line heights. */
4808 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4809 window_scroll_pixel_based (window, n, whole, noerror);
4810 else
4811 window_scroll_line_based (window, n, whole, noerror);
4812
4813 immediate_quit = 0;
4814 }
4815
4816
4817 /* Implementation of window_scroll that works based on pixel line
4818 heights. See the comment of window_scroll for parameter
4819 descriptions. */
4820
4821 static void
4822 window_scroll_pixel_based (window, n, whole, noerror)
4823 Lisp_Object window;
4824 int n;
4825 int whole;
4826 int noerror;
4827 {
4828 struct it it;
4829 struct window *w = XWINDOW (window);
4830 struct text_pos start;
4831 int this_scroll_margin;
4832 /* True if we fiddled the window vscroll field without really scrolling. */
4833 int vscrolled = 0;
4834 int x, y, rtop, rbot, rowh, vpos;
4835
4836 SET_TEXT_POS_FROM_MARKER (start, w->start);
4837
4838 /* If PT is not visible in WINDOW, move back one half of
4839 the screen. Allow PT to be partially visible, otherwise
4840 something like (scroll-down 1) with PT in the line before
4841 the partially visible one would recenter. */
4842
4843 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4844 {
4845 /* Move backward half the height of the window. Performance note:
4846 vmotion used here is about 10% faster, but would give wrong
4847 results for variable height lines. */
4848 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4849 it.current_y = it.last_visible_y;
4850 move_it_vertically_backward (&it, window_box_height (w) / 2);
4851
4852 /* The function move_iterator_vertically may move over more than
4853 the specified y-distance. If it->w is small, e.g. a
4854 mini-buffer window, we may end up in front of the window's
4855 display area. This is the case when Start displaying at the
4856 start of the line containing PT in this case. */
4857 if (it.current_y <= 0)
4858 {
4859 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4860 move_it_vertically_backward (&it, 0);
4861 it.current_y = 0;
4862 }
4863
4864 start = it.current.pos;
4865 }
4866 else if (auto_window_vscroll_p)
4867 {
4868 if (rtop || rbot) /* partially visible */
4869 {
4870 int px;
4871 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4872 if (whole)
4873 dy = max ((window_box_height (w)
4874 - next_screen_context_lines * dy),
4875 dy);
4876 dy *= n;
4877
4878 if (n < 0)
4879 {
4880 /* Only vscroll backwards if already vscrolled forwards. */
4881 if (w->vscroll < 0 && rtop > 0)
4882 {
4883 px = max (0, -w->vscroll - min (rtop, -dy));
4884 Fset_window_vscroll (window, make_number (px), Qt);
4885 return;
4886 }
4887 }
4888 if (n > 0)
4889 {
4890 /* Do vscroll if already vscrolled or only display line. */
4891 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4892 {
4893 px = max (0, -w->vscroll + min (rbot, dy));
4894 Fset_window_vscroll (window, make_number (px), Qt);
4895 return;
4896 }
4897
4898 /* Maybe modify window start instead of scrolling. */
4899 if (rbot > 0 || w->vscroll < 0)
4900 {
4901 int spos;
4902
4903 Fset_window_vscroll (window, make_number (0), Qt);
4904 /* If there are other text lines above the current row,
4905 move window start to current row. Else to next row. */
4906 if (rbot > 0)
4907 spos = XINT (Fline_beginning_position (Qnil));
4908 else
4909 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4910 set_marker_restricted (w->start, make_number (spos),
4911 w->buffer);
4912 w->start_at_line_beg = Qt;
4913 w->update_mode_line = Qt;
4914 XSETFASTINT (w->last_modified, 0);
4915 XSETFASTINT (w->last_overlay_modified, 0);
4916 /* Set force_start so that redisplay_window will run the
4917 window-scroll-functions. */
4918 w->force_start = Qt;
4919 return;
4920 }
4921 }
4922 }
4923 /* Cancel previous vscroll. */
4924 Fset_window_vscroll (window, make_number (0), Qt);
4925 }
4926
4927 /* If scroll_preserve_screen_position is non-nil, we try to set
4928 point in the same window line as it is now, so get that line. */
4929 if (!NILP (Vscroll_preserve_screen_position))
4930 {
4931 /* We preserve the goal pixel coordinate across consecutive
4932 calls to scroll-up or scroll-down. This avoids the
4933 possibility of point becoming "stuck" on a tall line when
4934 scrolling by one line. */
4935 if (window_scroll_pixel_based_preserve_y < 0
4936 || (!EQ (current_kboard->Vlast_command, Qscroll_up)
4937 && !EQ (current_kboard->Vlast_command, Qscroll_down)))
4938 {
4939 start_display (&it, w, start);
4940 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4941 window_scroll_pixel_based_preserve_y = it.current_y;
4942 window_scroll_pixel_based_preserve_x = it.current_x;
4943 }
4944 }
4945 else
4946 window_scroll_pixel_based_preserve_y
4947 = window_scroll_pixel_based_preserve_x = -1;
4948
4949 /* Move iterator it from start the specified distance forward or
4950 backward. The result is the new window start. */
4951 start_display (&it, w, start);
4952 if (whole)
4953 {
4954 int start_pos = IT_CHARPOS (it);
4955 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4956 dy = max ((window_box_height (w)
4957 - next_screen_context_lines * dy),
4958 dy) * n;
4959
4960 /* Note that move_it_vertically always moves the iterator to the
4961 start of a line. So, if the last line doesn't have a newline,
4962 we would end up at the start of the line ending at ZV. */
4963 if (dy <= 0)
4964 {
4965 move_it_vertically_backward (&it, -dy);
4966 /* Ensure we actually do move, e.g. in case we are currently
4967 looking at an image that is taller that the window height. */
4968 while (start_pos == IT_CHARPOS (it)
4969 && start_pos > BEGV)
4970 move_it_by_lines (&it, -1, 1);
4971 }
4972 else if (dy > 0)
4973 {
4974 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4975 MOVE_TO_POS | MOVE_TO_Y);
4976 /* Ensure we actually do move, e.g. in case we are currently
4977 looking at an image that is taller that the window height. */
4978 while (start_pos == IT_CHARPOS (it)
4979 && start_pos < ZV)
4980 move_it_by_lines (&it, 1, 1);
4981 }
4982 }
4983 else
4984 move_it_by_lines (&it, n, 1);
4985
4986 /* We failed if we find ZV is already on the screen (scrolling up,
4987 means there's nothing past the end), or if we can't start any
4988 earlier (scrolling down, means there's nothing past the top). */
4989 if ((n > 0 && IT_CHARPOS (it) == ZV)
4990 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4991 {
4992 if (IT_CHARPOS (it) == ZV)
4993 {
4994 if (it.current_y < it.last_visible_y
4995 && (it.current_y + it.max_ascent + it.max_descent
4996 > it.last_visible_y))
4997 {
4998 /* The last line was only partially visible, make it fully
4999 visible. */
5000 w->vscroll = (it.last_visible_y
5001 - it.current_y + it.max_ascent + it.max_descent);
5002 adjust_glyphs (it.f);
5003 }
5004 else if (noerror)
5005 return;
5006 else if (n < 0) /* could happen with empty buffers */
5007 xsignal0 (Qbeginning_of_buffer);
5008 else
5009 xsignal0 (Qend_of_buffer);
5010 }
5011 else
5012 {
5013 if (w->vscroll != 0)
5014 /* The first line was only partially visible, make it fully
5015 visible. */
5016 w->vscroll = 0;
5017 else if (noerror)
5018 return;
5019 else
5020 xsignal0 (Qbeginning_of_buffer);
5021 }
5022
5023 /* If control gets here, then we vscrolled. */
5024
5025 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
5026
5027 /* Don't try to change the window start below. */
5028 vscrolled = 1;
5029 }
5030
5031 if (! vscrolled)
5032 {
5033 int pos = IT_CHARPOS (it);
5034 int bytepos;
5035
5036 /* If in the middle of a multi-glyph character move forward to
5037 the next character. */
5038 if (in_display_vector_p (&it))
5039 {
5040 ++pos;
5041 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5042 }
5043
5044 /* Set the window start, and set up the window for redisplay. */
5045 set_marker_restricted (w->start, make_number (pos),
5046 w->buffer);
5047 bytepos = XMARKER (w->start)->bytepos;
5048 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
5049 ? Qt : Qnil);
5050 w->update_mode_line = Qt;
5051 XSETFASTINT (w->last_modified, 0);
5052 XSETFASTINT (w->last_overlay_modified, 0);
5053 /* Set force_start so that redisplay_window will run the
5054 window-scroll-functions. */
5055 w->force_start = Qt;
5056 }
5057
5058 /* The rest of this function uses current_y in a nonstandard way,
5059 not including the height of the header line if any. */
5060 it.current_y = it.vpos = 0;
5061
5062 /* Move PT out of scroll margins.
5063 This code wants current_y to be zero at the window start position
5064 even if there is a header line. */
5065 this_scroll_margin = max (0, scroll_margin);
5066 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
5067 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5068
5069 if (n > 0)
5070 {
5071 /* We moved the window start towards ZV, so PT may be now
5072 in the scroll margin at the top. */
5073 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5074 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5075 && (NILP (Vscroll_preserve_screen_position)
5076 || EQ (Vscroll_preserve_screen_position, Qt)))
5077 /* We found PT at a legitimate height. Leave it alone. */
5078 ;
5079 else if (window_scroll_pixel_based_preserve_y >= 0)
5080 {
5081 /* If we have a header line, take account of it.
5082 This is necessary because we set it.current_y to 0, above. */
5083 move_it_to (&it, -1,
5084 window_scroll_pixel_based_preserve_x,
5085 window_scroll_pixel_based_preserve_y
5086 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5087 -1, MOVE_TO_Y | MOVE_TO_X);
5088 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5089 }
5090 else
5091 {
5092 while (it.current_y < this_scroll_margin)
5093 {
5094 int prev = it.current_y;
5095 move_it_by_lines (&it, 1, 1);
5096 if (prev == it.current_y)
5097 break;
5098 }
5099 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5100 }
5101 }
5102 else if (n < 0)
5103 {
5104 int charpos, bytepos;
5105 int partial_p;
5106
5107 /* Save our position, for the
5108 window_scroll_pixel_based_preserve_y case. */
5109 charpos = IT_CHARPOS (it);
5110 bytepos = IT_BYTEPOS (it);
5111
5112 /* We moved the window start towards BEGV, so PT may be now
5113 in the scroll margin at the bottom. */
5114 move_it_to (&it, PT, -1,
5115 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5116 - this_scroll_margin - 1),
5117 -1,
5118 MOVE_TO_POS | MOVE_TO_Y);
5119
5120 /* Save our position, in case it's correct. */
5121 charpos = IT_CHARPOS (it);
5122 bytepos = IT_BYTEPOS (it);
5123
5124 /* See if point is on a partially visible line at the end. */
5125 if (it.what == IT_EOB)
5126 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5127 else
5128 {
5129 move_it_by_lines (&it, 1, 1);
5130 partial_p = it.current_y > it.last_visible_y;
5131 }
5132
5133 if (charpos == PT && !partial_p
5134 && (NILP (Vscroll_preserve_screen_position)
5135 || EQ (Vscroll_preserve_screen_position, Qt)))
5136 /* We found PT before we found the display margin, so PT is ok. */
5137 ;
5138 else if (window_scroll_pixel_based_preserve_y >= 0)
5139 {
5140 SET_TEXT_POS_FROM_MARKER (start, w->start);
5141 start_display (&it, w, start);
5142 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5143 here because we called start_display again and did not
5144 alter it.current_y this time. */
5145 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5146 window_scroll_pixel_based_preserve_y, -1,
5147 MOVE_TO_Y | MOVE_TO_X);
5148 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5149 }
5150 else
5151 {
5152 if (partial_p)
5153 /* The last line was only partially visible, so back up two
5154 lines to make sure we're on a fully visible line. */
5155 {
5156 move_it_by_lines (&it, -2, 0);
5157 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5158 }
5159 else
5160 /* No, the position we saved is OK, so use it. */
5161 SET_PT_BOTH (charpos, bytepos);
5162 }
5163 }
5164 }
5165
5166
5167 /* Implementation of window_scroll that works based on screen lines.
5168 See the comment of window_scroll for parameter descriptions. */
5169
5170 static void
5171 window_scroll_line_based (window, n, whole, noerror)
5172 Lisp_Object window;
5173 int n;
5174 int whole;
5175 int noerror;
5176 {
5177 register struct window *w = XWINDOW (window);
5178 register int opoint = PT, opoint_byte = PT_BYTE;
5179 register int pos, pos_byte;
5180 register int ht = window_internal_height (w);
5181 register Lisp_Object tem;
5182 int lose;
5183 Lisp_Object bolp;
5184 int startpos;
5185 Lisp_Object original_pos = Qnil;
5186
5187 /* If scrolling screen-fulls, compute the number of lines to
5188 scroll from the window's height. */
5189 if (whole)
5190 n *= max (1, ht - next_screen_context_lines);
5191
5192 startpos = marker_position (w->start);
5193
5194 if (!NILP (Vscroll_preserve_screen_position))
5195 {
5196 if (window_scroll_preserve_vpos <= 0
5197 || (!EQ (current_kboard->Vlast_command, Qscroll_up)
5198 && !EQ (current_kboard->Vlast_command, Qscroll_down)))
5199 {
5200 struct position posit
5201 = *compute_motion (startpos, 0, 0, 0,
5202 PT, ht, 0,
5203 -1, XINT (w->hscroll),
5204 0, w);
5205 window_scroll_preserve_vpos = posit.vpos;
5206 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
5207 }
5208
5209 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5210 make_number (window_scroll_preserve_vpos));
5211 }
5212
5213 XSETFASTINT (tem, PT);
5214 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5215
5216 if (NILP (tem))
5217 {
5218 Fvertical_motion (make_number (- (ht / 2)), window);
5219 startpos = PT;
5220 }
5221
5222 SET_PT (startpos);
5223 lose = n < 0 && PT == BEGV;
5224 Fvertical_motion (make_number (n), window);
5225 pos = PT;
5226 pos_byte = PT_BYTE;
5227 bolp = Fbolp ();
5228 SET_PT_BOTH (opoint, opoint_byte);
5229
5230 if (lose)
5231 {
5232 if (noerror)
5233 return;
5234 else
5235 xsignal0 (Qbeginning_of_buffer);
5236 }
5237
5238 if (pos < ZV)
5239 {
5240 int this_scroll_margin = scroll_margin;
5241
5242 /* Don't use a scroll margin that is negative or too large. */
5243 if (this_scroll_margin < 0)
5244 this_scroll_margin = 0;
5245
5246 if (XINT (w->total_lines) < 4 * scroll_margin)
5247 this_scroll_margin = XINT (w->total_lines) / 4;
5248
5249 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5250 w->start_at_line_beg = bolp;
5251 w->update_mode_line = Qt;
5252 XSETFASTINT (w->last_modified, 0);
5253 XSETFASTINT (w->last_overlay_modified, 0);
5254 /* Set force_start so that redisplay_window will run
5255 the window-scroll-functions. */
5256 w->force_start = Qt;
5257
5258 if (!NILP (Vscroll_preserve_screen_position)
5259 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5260 {
5261 SET_PT_BOTH (pos, pos_byte);
5262 Fvertical_motion (original_pos, window);
5263 }
5264 /* If we scrolled forward, put point enough lines down
5265 that it is outside the scroll margin. */
5266 else if (n > 0)
5267 {
5268 int top_margin;
5269
5270 if (this_scroll_margin > 0)
5271 {
5272 SET_PT_BOTH (pos, pos_byte);
5273 Fvertical_motion (make_number (this_scroll_margin), window);
5274 top_margin = PT;
5275 }
5276 else
5277 top_margin = pos;
5278
5279 if (top_margin <= opoint)
5280 SET_PT_BOTH (opoint, opoint_byte);
5281 else if (!NILP (Vscroll_preserve_screen_position))
5282 {
5283 SET_PT_BOTH (pos, pos_byte);
5284 Fvertical_motion (original_pos, window);
5285 }
5286 else
5287 SET_PT (top_margin);
5288 }
5289 else if (n < 0)
5290 {
5291 int bottom_margin;
5292
5293 /* If we scrolled backward, put point near the end of the window
5294 but not within the scroll margin. */
5295 SET_PT_BOTH (pos, pos_byte);
5296 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5297 if (XFASTINT (tem) == ht - this_scroll_margin)
5298 bottom_margin = PT;
5299 else
5300 bottom_margin = PT + 1;
5301
5302 if (bottom_margin > opoint)
5303 SET_PT_BOTH (opoint, opoint_byte);
5304 else
5305 {
5306 if (!NILP (Vscroll_preserve_screen_position))
5307 {
5308 SET_PT_BOTH (pos, pos_byte);
5309 Fvertical_motion (original_pos, window);
5310 }
5311 else
5312 Fvertical_motion (make_number (-1), window);
5313 }
5314 }
5315 }
5316 else
5317 {
5318 if (noerror)
5319 return;
5320 else
5321 xsignal0 (Qend_of_buffer);
5322 }
5323 }
5324
5325
5326 /* Scroll selected_window up or down. If N is nil, scroll a
5327 screen-full which is defined as the height of the window minus
5328 next_screen_context_lines. If N is the symbol `-', scroll.
5329 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5330 up. This is the guts of Fscroll_up and Fscroll_down. */
5331
5332 static void
5333 scroll_command (n, direction)
5334 Lisp_Object n;
5335 int direction;
5336 {
5337 int count = SPECPDL_INDEX ();
5338
5339 xassert (eabs (direction) == 1);
5340
5341 /* If selected window's buffer isn't current, make it current for
5342 the moment. But don't screw up if window_scroll gets an error. */
5343 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5344 {
5345 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5346 Fset_buffer (XWINDOW (selected_window)->buffer);
5347
5348 /* Make redisplay consider other windows than just selected_window. */
5349 ++windows_or_buffers_changed;
5350 }
5351
5352 if (NILP (n))
5353 window_scroll (selected_window, direction, 1, 0);
5354 else if (EQ (n, Qminus))
5355 window_scroll (selected_window, -direction, 1, 0);
5356 else
5357 {
5358 n = Fprefix_numeric_value (n);
5359 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5360 }
5361
5362 unbind_to (count, Qnil);
5363 }
5364
5365 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5366 doc: /* Scroll text of selected window upward ARG lines.
5367 If ARG is omitted or nil, scroll upward by a near full screen.
5368 A near full screen is `next-screen-context-lines' less than a full screen.
5369 Negative ARG means scroll downward.
5370 If ARG is the atom `-', scroll downward by nearly full screen.
5371 When calling from a program, supply as argument a number, nil, or `-'. */)
5372 (arg)
5373 Lisp_Object arg;
5374 {
5375 scroll_command (arg, 1);
5376 return Qnil;
5377 }
5378
5379 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5380 doc: /* Scroll text of selected window down ARG lines.
5381 If ARG is omitted or nil, scroll down by a near full screen.
5382 A near full screen is `next-screen-context-lines' less than a full screen.
5383 Negative ARG means scroll upward.
5384 If ARG is the atom `-', scroll upward by nearly full screen.
5385 When calling from a program, supply as argument a number, nil, or `-'. */)
5386 (arg)
5387 Lisp_Object arg;
5388 {
5389 scroll_command (arg, -1);
5390 return Qnil;
5391 }
5392 \f
5393 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5394 doc: /* Return the other window for \"other window scroll\" commands.
5395 If `other-window-scroll-buffer' is non-nil, a window
5396 showing that buffer is used.
5397 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5398 specifies the window. This takes precedence over
5399 `other-window-scroll-buffer'. */)
5400 ()
5401 {
5402 Lisp_Object window;
5403
5404 if (MINI_WINDOW_P (XWINDOW (selected_window))
5405 && !NILP (Vminibuf_scroll_window))
5406 window = Vminibuf_scroll_window;
5407 /* If buffer is specified, scroll that buffer. */
5408 else if (!NILP (Vother_window_scroll_buffer))
5409 {
5410 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5411 if (NILP (window))
5412 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5413 }
5414 else
5415 {
5416 /* Nothing specified; look for a neighboring window on the same
5417 frame. */
5418 window = Fnext_window (selected_window, Qnil, Qnil);
5419
5420 if (EQ (window, selected_window))
5421 /* That didn't get us anywhere; look for a window on another
5422 visible frame. */
5423 do
5424 window = Fnext_window (window, Qnil, Qt);
5425 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5426 && ! EQ (window, selected_window));
5427 }
5428
5429 CHECK_LIVE_WINDOW (window);
5430
5431 if (EQ (window, selected_window))
5432 error ("There is no other window");
5433
5434 return window;
5435 }
5436
5437 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5438 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5439 A near full screen is `next-screen-context-lines' less than a full screen.
5440 The next window is the one below the current one; or the one at the top
5441 if the current one is at the bottom. Negative ARG means scroll downward.
5442 If ARG is the atom `-', scroll downward by nearly full screen.
5443 When calling from a program, supply as argument a number, nil, or `-'.
5444
5445 If `other-window-scroll-buffer' is non-nil, scroll the window
5446 showing that buffer, popping the buffer up if necessary.
5447 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5448 specifies the window to scroll. This takes precedence over
5449 `other-window-scroll-buffer'. */)
5450 (arg)
5451 Lisp_Object arg;
5452 {
5453 Lisp_Object window;
5454 struct window *w;
5455 int count = SPECPDL_INDEX ();
5456
5457 window = Fother_window_for_scrolling ();
5458 w = XWINDOW (window);
5459
5460 /* Don't screw up if window_scroll gets an error. */
5461 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5462 ++windows_or_buffers_changed;
5463
5464 Fset_buffer (w->buffer);
5465 SET_PT (marker_position (w->pointm));
5466
5467 if (NILP (arg))
5468 window_scroll (window, 1, 1, 1);
5469 else if (EQ (arg, Qminus))
5470 window_scroll (window, -1, 1, 1);
5471 else
5472 {
5473 if (CONSP (arg))
5474 arg = Fcar (arg);
5475 CHECK_NUMBER (arg);
5476 window_scroll (window, XINT (arg), 0, 1);
5477 }
5478
5479 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5480 unbind_to (count, Qnil);
5481
5482 return Qnil;
5483 }
5484 \f
5485 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5486 doc: /* Scroll selected window display ARG columns left.
5487 Default for ARG is window width minus 2.
5488 Value is the total amount of leftward horizontal scrolling in
5489 effect after the change.
5490 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5491 lower bound for automatic scrolling, i.e. automatic scrolling
5492 will not scroll a window to a column less than the value returned
5493 by this function. This happens in an interactive call. */)
5494 (arg, set_minimum)
5495 register Lisp_Object arg, set_minimum;
5496 {
5497 Lisp_Object result;
5498 int hscroll;
5499 struct window *w = XWINDOW (selected_window);
5500
5501 if (NILP (arg))
5502 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5503 else
5504 arg = Fprefix_numeric_value (arg);
5505
5506 hscroll = XINT (w->hscroll) + XINT (arg);
5507 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5508
5509 if (!NILP (set_minimum))
5510 w->min_hscroll = w->hscroll;
5511
5512 return result;
5513 }
5514
5515 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5516 doc: /* Scroll selected window display ARG columns right.
5517 Default for ARG is window width minus 2.
5518 Value is the total amount of leftward horizontal scrolling in
5519 effect after the change.
5520 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5521 lower bound for automatic scrolling, i.e. automatic scrolling
5522 will not scroll a window to a column less than the value returned
5523 by this function. This happens in an interactive call. */)
5524 (arg, set_minimum)
5525 register Lisp_Object arg, set_minimum;
5526 {
5527 Lisp_Object result;
5528 int hscroll;
5529 struct window *w = XWINDOW (selected_window);
5530
5531 if (NILP (arg))
5532 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5533 else
5534 arg = Fprefix_numeric_value (arg);
5535
5536 hscroll = XINT (w->hscroll) - XINT (arg);
5537 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5538
5539 if (!NILP (set_minimum))
5540 w->min_hscroll = w->hscroll;
5541
5542 return result;
5543 }
5544
5545 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5546 doc: /* Return the window which was selected when entering the minibuffer.
5547 Returns nil, if selected window is not a minibuffer window. */)
5548 ()
5549 {
5550 if (minibuf_level > 0
5551 && MINI_WINDOW_P (XWINDOW (selected_window))
5552 && WINDOW_LIVE_P (minibuf_selected_window))
5553 return minibuf_selected_window;
5554
5555 return Qnil;
5556 }
5557
5558 /* Value is the number of lines actually displayed in window W,
5559 as opposed to its height. */
5560
5561 static int
5562 displayed_window_lines (w)
5563 struct window *w;
5564 {
5565 struct it it;
5566 struct text_pos start;
5567 int height = window_box_height (w);
5568 struct buffer *old_buffer;
5569 int bottom_y;
5570
5571 if (XBUFFER (w->buffer) != current_buffer)
5572 {
5573 old_buffer = current_buffer;
5574 set_buffer_internal (XBUFFER (w->buffer));
5575 }
5576 else
5577 old_buffer = NULL;
5578
5579 /* In case W->start is out of the accessible range, do something
5580 reasonable. This happens in Info mode when Info-scroll-down
5581 calls (recenter -1) while W->start is 1. */
5582 if (XMARKER (w->start)->charpos < BEGV)
5583 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5584 else if (XMARKER (w->start)->charpos > ZV)
5585 SET_TEXT_POS (start, ZV, ZV_BYTE);
5586 else
5587 SET_TEXT_POS_FROM_MARKER (start, w->start);
5588
5589 start_display (&it, w, start);
5590 move_it_vertically (&it, height);
5591 bottom_y = line_bottom_y (&it);
5592
5593 /* rms: On a non-window display,
5594 the value of it.vpos at the bottom of the screen
5595 seems to be 1 larger than window_box_height (w).
5596 This kludge fixes a bug whereby (move-to-window-line -1)
5597 when ZV is on the last screen line
5598 moves to the previous screen line instead of the last one. */
5599 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5600 height++;
5601
5602 /* Add in empty lines at the bottom of the window. */
5603 if (bottom_y < height)
5604 {
5605 int uy = FRAME_LINE_HEIGHT (it.f);
5606 it.vpos += (height - bottom_y + uy - 1) / uy;
5607 }
5608
5609 if (old_buffer)
5610 set_buffer_internal (old_buffer);
5611
5612 return it.vpos;
5613 }
5614
5615
5616 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5617 doc: /* Center point in selected window and maybe redisplay frame.
5618 With prefix argument ARG, recenter putting point on screen line ARG
5619 relative to the selected window. If ARG is negative, it counts up from the
5620 bottom of the window. (ARG should be less than the height of the window.)
5621
5622 If ARG is omitted or nil, then recenter with point on the middle line of
5623 the selected window; if the variable `recenter-redisplay' is non-nil,
5624 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5625 is set to `grow-only', this resets the tool-bar's height to the minimum
5626 height needed); if `recenter-redisplay' has the special value `tty',
5627 then only tty frame are redrawn.
5628
5629 Just C-u as prefix means put point in the center of the window
5630 and redisplay normally--don't erase and redraw the frame. */)
5631 (arg)
5632 register Lisp_Object arg;
5633 {
5634 struct window *w = XWINDOW (selected_window);
5635 struct buffer *buf = XBUFFER (w->buffer);
5636 struct buffer *obuf = current_buffer;
5637 int center_p = 0;
5638 int charpos, bytepos;
5639 int iarg;
5640 int this_scroll_margin;
5641
5642 /* If redisplay is suppressed due to an error, try again. */
5643 obuf->display_error_modiff = 0;
5644
5645 if (NILP (arg))
5646 {
5647 if (!NILP (Vrecenter_redisplay)
5648 && (!EQ (Vrecenter_redisplay, Qtty)
5649 || !NILP (Ftty_type (selected_frame))))
5650 {
5651 int i;
5652
5653 /* Invalidate pixel data calculated for all compositions. */
5654 for (i = 0; i < n_compositions; i++)
5655 composition_table[i]->font = NULL;
5656
5657 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5658
5659 Fredraw_frame (WINDOW_FRAME (w));
5660 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5661 }
5662
5663 center_p = 1;
5664 }
5665 else if (CONSP (arg)) /* Just C-u. */
5666 center_p = 1;
5667 else
5668 {
5669 arg = Fprefix_numeric_value (arg);
5670 CHECK_NUMBER (arg);
5671 iarg = XINT (arg);
5672 }
5673
5674 set_buffer_internal (buf);
5675
5676 /* Do this after making BUF current
5677 in case scroll_margin is buffer-local. */
5678 this_scroll_margin = max (0, scroll_margin);
5679 this_scroll_margin = min (this_scroll_margin,
5680 XFASTINT (w->total_lines) / 4);
5681
5682 /* Handle centering on a graphical frame specially. Such frames can
5683 have variable-height lines and centering point on the basis of
5684 line counts would lead to strange effects. */
5685 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5686 {
5687 if (center_p)
5688 {
5689 struct it it;
5690 struct text_pos pt;
5691
5692 SET_TEXT_POS (pt, PT, PT_BYTE);
5693 start_display (&it, w, pt);
5694 move_it_vertically_backward (&it, window_box_height (w) / 2);
5695 charpos = IT_CHARPOS (it);
5696 bytepos = IT_BYTEPOS (it);
5697 }
5698 else if (iarg < 0)
5699 {
5700 struct it it;
5701 struct text_pos pt;
5702 int nlines = -iarg;
5703 int extra_line_spacing;
5704 int h = window_box_height (w);
5705
5706 iarg = - max (-iarg, this_scroll_margin);
5707
5708 SET_TEXT_POS (pt, PT, PT_BYTE);
5709 start_display (&it, w, pt);
5710
5711 /* Be sure we have the exact height of the full line containing PT. */
5712 move_it_by_lines (&it, 0, 1);
5713
5714 /* The amount of pixels we have to move back is the window
5715 height minus what's displayed in the line containing PT,
5716 and the lines below. */
5717 it.current_y = 0;
5718 it.vpos = 0;
5719 move_it_by_lines (&it, nlines, 1);
5720
5721 if (it.vpos == nlines)
5722 h -= it.current_y;
5723 else
5724 {
5725 /* Last line has no newline */
5726 h -= line_bottom_y (&it);
5727 it.vpos++;
5728 }
5729
5730 /* Don't reserve space for extra line spacing of last line. */
5731 extra_line_spacing = it.max_extra_line_spacing;
5732
5733 /* If we can't move down NLINES lines because we hit
5734 the end of the buffer, count in some empty lines. */
5735 if (it.vpos < nlines)
5736 {
5737 nlines -= it.vpos;
5738 extra_line_spacing = it.extra_line_spacing;
5739 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5740 }
5741 if (h <= 0)
5742 return Qnil;
5743
5744 /* Now find the new top line (starting position) of the window. */
5745 start_display (&it, w, pt);
5746 it.current_y = 0;
5747 move_it_vertically_backward (&it, h);
5748
5749 /* If extra line spacing is present, we may move too far
5750 back. This causes the last line to be only partially
5751 visible (which triggers redisplay to recenter that line
5752 in the middle), so move forward.
5753 But ignore extra line spacing on last line, as it is not
5754 considered to be part of the visible height of the line.
5755 */
5756 h += extra_line_spacing;
5757 while (-it.current_y > h)
5758 move_it_by_lines (&it, 1, 1);
5759
5760 charpos = IT_CHARPOS (it);
5761 bytepos = IT_BYTEPOS (it);
5762 }
5763 else
5764 {
5765 struct position pos;
5766
5767 iarg = max (iarg, this_scroll_margin);
5768
5769 pos = *vmotion (PT, -iarg, w);
5770 charpos = pos.bufpos;
5771 bytepos = pos.bytepos;
5772 }
5773 }
5774 else
5775 {
5776 struct position pos;
5777 int ht = window_internal_height (w);
5778
5779 if (center_p)
5780 iarg = ht / 2;
5781 else if (iarg < 0)
5782 iarg += ht;
5783
5784 /* Don't let it get into the margin at either top or bottom. */
5785 iarg = max (iarg, this_scroll_margin);
5786 iarg = min (iarg, ht - this_scroll_margin - 1);
5787
5788 pos = *vmotion (PT, - iarg, w);
5789 charpos = pos.bufpos;
5790 bytepos = pos.bytepos;
5791 }
5792
5793 /* Set the new window start. */
5794 set_marker_both (w->start, w->buffer, charpos, bytepos);
5795 w->window_end_valid = Qnil;
5796
5797 w->optional_new_start = Qt;
5798
5799 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5800 w->start_at_line_beg = Qt;
5801 else
5802 w->start_at_line_beg = Qnil;
5803
5804 set_buffer_internal (obuf);
5805 return Qnil;
5806 }
5807
5808
5809 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5810 0, 1, 0,
5811 doc: /* Return the height in lines of the text display area of WINDOW.
5812 WINDOW defaults to the selected window.
5813
5814 The return value does not include the mode line, any header line, nor
5815 any partial-height lines in the text display area. */)
5816 (window)
5817 Lisp_Object window;
5818 {
5819 struct window *w = decode_window (window);
5820 int pixel_height = window_box_height (w);
5821 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5822 return make_number (line_height);
5823 }
5824
5825
5826 \f
5827 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5828 1, 1, "P",
5829 doc: /* Position point relative to window.
5830 With no argument, position point at center of window.
5831 An argument specifies vertical position within the window;
5832 zero means top of window, negative means relative to bottom of window. */)
5833 (arg)
5834 Lisp_Object arg;
5835 {
5836 struct window *w = XWINDOW (selected_window);
5837 int lines, start;
5838 Lisp_Object window;
5839 #if 0
5840 int this_scroll_margin;
5841 #endif
5842
5843 window = selected_window;
5844 start = marker_position (w->start);
5845 if (start < BEGV || start > ZV)
5846 {
5847 int height = window_internal_height (w);
5848 Fvertical_motion (make_number (- (height / 2)), window);
5849 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5850 w->start_at_line_beg = Fbolp ();
5851 w->force_start = Qt;
5852 }
5853 else
5854 Fgoto_char (w->start);
5855
5856 lines = displayed_window_lines (w);
5857
5858 #if 0
5859 this_scroll_margin = max (0, scroll_margin);
5860 this_scroll_margin = min (this_scroll_margin, lines / 4);
5861 #endif
5862
5863 if (NILP (arg))
5864 XSETFASTINT (arg, lines / 2);
5865 else
5866 {
5867 int iarg = XINT (Fprefix_numeric_value (arg));
5868
5869 if (iarg < 0)
5870 iarg = iarg + lines;
5871
5872 #if 0 /* This code would prevent move-to-window-line from moving point
5873 to a place inside the scroll margins (which would cause the
5874 next redisplay to scroll). I wrote this code, but then concluded
5875 it is probably better not to install it. However, it is here
5876 inside #if 0 so as not to lose it. -- rms. */
5877
5878 /* Don't let it get into the margin at either top or bottom. */
5879 iarg = max (iarg, this_scroll_margin);
5880 iarg = min (iarg, lines - this_scroll_margin - 1);
5881 #endif
5882
5883 arg = make_number (iarg);
5884 }
5885
5886 /* Skip past a partially visible first line. */
5887 if (w->vscroll)
5888 XSETINT (arg, XINT (arg) + 1);
5889
5890 return Fvertical_motion (arg, window);
5891 }
5892
5893
5894 \f
5895 /***********************************************************************
5896 Window Configuration
5897 ***********************************************************************/
5898
5899 struct save_window_data
5900 {
5901 EMACS_UINT size;
5902 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5903 Lisp_Object selected_frame;
5904 Lisp_Object current_window;
5905 Lisp_Object current_buffer;
5906 Lisp_Object minibuf_scroll_window;
5907 Lisp_Object minibuf_selected_window;
5908 Lisp_Object root_window;
5909 Lisp_Object focus_frame;
5910 /* A vector, each of whose elements is a struct saved_window
5911 for one window. */
5912 Lisp_Object saved_windows;
5913
5914 /* All fields above are traced by the GC.
5915 From `fame-cols' down, the fields are ignored by the GC. */
5916
5917 int frame_cols, frame_lines, frame_menu_bar_lines;
5918 int frame_tool_bar_lines;
5919 };
5920
5921 /* This is saved as a Lisp_Vector */
5922 struct saved_window
5923 {
5924 /* these first two must agree with struct Lisp_Vector in lisp.h */
5925 EMACS_UINT size;
5926 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5927
5928 Lisp_Object window;
5929 Lisp_Object buffer, start, pointm, mark;
5930 Lisp_Object left_col, top_line, total_cols, total_lines;
5931 Lisp_Object hscroll, min_hscroll;
5932 Lisp_Object parent, prev;
5933 Lisp_Object start_at_line_beg;
5934 Lisp_Object display_table;
5935 Lisp_Object orig_top_line, orig_total_lines;
5936 Lisp_Object left_margin_cols, right_margin_cols;
5937 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5938 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5939 Lisp_Object dedicated, resize_proportionally;
5940 };
5941
5942 #define SAVED_WINDOW_N(swv,n) \
5943 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5944
5945 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5946 doc: /* Return t if OBJECT is a window-configuration object. */)
5947 (object)
5948 Lisp_Object object;
5949 {
5950 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5951 }
5952
5953 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5954 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5955 (config)
5956 Lisp_Object config;
5957 {
5958 register struct save_window_data *data;
5959 struct Lisp_Vector *saved_windows;
5960
5961 CHECK_WINDOW_CONFIGURATION (config);
5962
5963 data = (struct save_window_data *) XVECTOR (config);
5964 saved_windows = XVECTOR (data->saved_windows);
5965 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5966 }
5967
5968 DEFUN ("set-window-configuration", Fset_window_configuration,
5969 Sset_window_configuration, 1, 1, 0,
5970 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5971 CONFIGURATION must be a value previously returned
5972 by `current-window-configuration' (which see).
5973 If CONFIGURATION was made from a frame that is now deleted,
5974 only frame-independent values can be restored. In this case,
5975 the return value is nil. Otherwise the value is t. */)
5976 (configuration)
5977 Lisp_Object configuration;
5978 {
5979 register struct save_window_data *data;
5980 struct Lisp_Vector *saved_windows;
5981 Lisp_Object new_current_buffer;
5982 Lisp_Object frame;
5983 FRAME_PTR f;
5984 int old_point = -1;
5985
5986 CHECK_WINDOW_CONFIGURATION (configuration);
5987
5988 data = (struct save_window_data *) XVECTOR (configuration);
5989 saved_windows = XVECTOR (data->saved_windows);
5990
5991 new_current_buffer = data->current_buffer;
5992 if (NILP (XBUFFER (new_current_buffer)->name))
5993 new_current_buffer = Qnil;
5994 else
5995 {
5996 if (XBUFFER (new_current_buffer) == current_buffer)
5997 /* The code further down "preserves point" by saving here PT in
5998 old_point and then setting it later back into PT. When the
5999 current-selected-window and the final-selected-window both show
6000 the current buffer, this suffers from the problem that the
6001 current PT is the window-point of the current-selected-window,
6002 while the final PT is the point of the final-selected-window, so
6003 this copy from one PT to the other would end up moving the
6004 window-point of the final-selected-window to the window-point of
6005 the current-selected-window. So we have to be careful which
6006 point of the current-buffer we copy into old_point. */
6007 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6008 && WINDOWP (selected_window)
6009 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
6010 && !EQ (selected_window, data->current_window))
6011 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6012 else
6013 old_point = PT;
6014 else
6015 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6016 point in new_current_buffer as of the last time this buffer was
6017 used. This can be non-deterministic since it can be changed by
6018 things like jit-lock by mere temporary selection of some random
6019 window that happens to show this buffer.
6020 So if possible we want this arbitrary choice of "which point" to
6021 be the one from the to-be-selected-window so as to prevent this
6022 window's cursor from being copied from another window. */
6023 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6024 /* If current_window = selected_window, its point is in BUF_PT. */
6025 && !EQ (selected_window, data->current_window))
6026 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6027 else
6028 old_point = BUF_PT (XBUFFER (new_current_buffer));
6029 }
6030
6031 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6032 f = XFRAME (frame);
6033
6034 /* If f is a dead frame, don't bother rebuilding its window tree.
6035 However, there is other stuff we should still try to do below. */
6036 if (FRAME_LIVE_P (f))
6037 {
6038 register struct window *w;
6039 register struct saved_window *p;
6040 struct window *root_window;
6041 struct window **leaf_windows;
6042 int n_leaf_windows;
6043 int k, i, n;
6044
6045 /* If the frame has been resized since this window configuration was
6046 made, we change the frame to the size specified in the
6047 configuration, restore the configuration, and then resize it
6048 back. We keep track of the prevailing height in these variables. */
6049 int previous_frame_lines = FRAME_LINES (f);
6050 int previous_frame_cols = FRAME_COLS (f);
6051 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6052 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6053
6054 /* The mouse highlighting code could get screwed up
6055 if it runs during this. */
6056 BLOCK_INPUT;
6057
6058 if (data->frame_lines != previous_frame_lines
6059 || data->frame_cols != previous_frame_cols)
6060 change_frame_size (f, data->frame_lines,
6061 data->frame_cols, 0, 0, 0);
6062 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6063 if (data->frame_menu_bar_lines
6064 != previous_frame_menu_bar_lines)
6065 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6066 make_number (0));
6067 #ifdef HAVE_WINDOW_SYSTEM
6068 if (data->frame_tool_bar_lines
6069 != previous_frame_tool_bar_lines)
6070 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6071 make_number (0));
6072 #endif
6073 #endif
6074
6075 /* "Swap out" point from the selected window's buffer
6076 into the window itself. (Normally the pointm of the selected
6077 window holds garbage.) We do this now, before
6078 restoring the window contents, and prevent it from
6079 being done later on when we select a new window. */
6080 if (! NILP (XWINDOW (selected_window)->buffer))
6081 {
6082 w = XWINDOW (selected_window);
6083 set_marker_both (w->pointm,
6084 w->buffer,
6085 BUF_PT (XBUFFER (w->buffer)),
6086 BUF_PT_BYTE (XBUFFER (w->buffer)));
6087 }
6088
6089 windows_or_buffers_changed++;
6090 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6091
6092 /* Problem: Freeing all matrices and later allocating them again
6093 is a serious redisplay flickering problem. What we would
6094 really like to do is to free only those matrices not reused
6095 below. */
6096 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6097 leaf_windows
6098 = (struct window **) alloca (count_windows (root_window)
6099 * sizeof (struct window *));
6100 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6101
6102 /* Kludge Alert!
6103 Mark all windows now on frame as "deleted".
6104 Restoring the new configuration "undeletes" any that are in it.
6105
6106 Save their current buffers in their height fields, since we may
6107 need it later, if a buffer saved in the configuration is now
6108 dead. */
6109 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6110
6111 for (k = 0; k < saved_windows->size; k++)
6112 {
6113 p = SAVED_WINDOW_N (saved_windows, k);
6114 w = XWINDOW (p->window);
6115 w->next = Qnil;
6116
6117 if (!NILP (p->parent))
6118 w->parent = SAVED_WINDOW_N (saved_windows,
6119 XFASTINT (p->parent))->window;
6120 else
6121 w->parent = Qnil;
6122
6123 if (!NILP (p->prev))
6124 {
6125 w->prev = SAVED_WINDOW_N (saved_windows,
6126 XFASTINT (p->prev))->window;
6127 XWINDOW (w->prev)->next = p->window;
6128 }
6129 else
6130 {
6131 w->prev = Qnil;
6132 if (!NILP (w->parent))
6133 {
6134 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6135 {
6136 XWINDOW (w->parent)->vchild = p->window;
6137 XWINDOW (w->parent)->hchild = Qnil;
6138 }
6139 else
6140 {
6141 XWINDOW (w->parent)->hchild = p->window;
6142 XWINDOW (w->parent)->vchild = Qnil;
6143 }
6144 }
6145 }
6146
6147 /* If we squirreled away the buffer in the window's height,
6148 restore it now. */
6149 if (BUFFERP (w->total_lines))
6150 w->buffer = w->total_lines;
6151 w->left_col = p->left_col;
6152 w->top_line = p->top_line;
6153 w->total_cols = p->total_cols;
6154 w->total_lines = p->total_lines;
6155 w->hscroll = p->hscroll;
6156 w->min_hscroll = p->min_hscroll;
6157 w->display_table = p->display_table;
6158 w->orig_top_line = p->orig_top_line;
6159 w->orig_total_lines = p->orig_total_lines;
6160 w->left_margin_cols = p->left_margin_cols;
6161 w->right_margin_cols = p->right_margin_cols;
6162 w->left_fringe_width = p->left_fringe_width;
6163 w->right_fringe_width = p->right_fringe_width;
6164 w->fringes_outside_margins = p->fringes_outside_margins;
6165 w->scroll_bar_width = p->scroll_bar_width;
6166 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6167 w->dedicated = p->dedicated;
6168 w->resize_proportionally = p->resize_proportionally;
6169 XSETFASTINT (w->last_modified, 0);
6170 XSETFASTINT (w->last_overlay_modified, 0);
6171
6172 /* Reinstall the saved buffer and pointers into it. */
6173 if (NILP (p->buffer))
6174 w->buffer = p->buffer;
6175 else
6176 {
6177 if (!NILP (XBUFFER (p->buffer)->name))
6178 /* If saved buffer is alive, install it. */
6179 {
6180 w->buffer = p->buffer;
6181 w->start_at_line_beg = p->start_at_line_beg;
6182 set_marker_restricted (w->start, p->start, w->buffer);
6183 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6184 Fset_marker (XBUFFER (w->buffer)->mark,
6185 p->mark, w->buffer);
6186
6187 /* As documented in Fcurrent_window_configuration, don't
6188 restore the location of point in the buffer which was
6189 current when the window configuration was recorded. */
6190 if (!EQ (p->buffer, new_current_buffer)
6191 && XBUFFER (p->buffer) == current_buffer)
6192 Fgoto_char (w->pointm);
6193 }
6194 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
6195 /* Else unless window has a live buffer, get one. */
6196 {
6197 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6198 /* This will set the markers to beginning of visible
6199 range. */
6200 set_marker_restricted (w->start, make_number (0), w->buffer);
6201 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6202 w->start_at_line_beg = Qt;
6203 }
6204 else
6205 /* Keeping window's old buffer; make sure the markers
6206 are real. */
6207 {
6208 /* Set window markers at start of visible range. */
6209 if (XMARKER (w->start)->buffer == 0)
6210 set_marker_restricted (w->start, make_number (0),
6211 w->buffer);
6212 if (XMARKER (w->pointm)->buffer == 0)
6213 set_marker_restricted_both (w->pointm, w->buffer,
6214 BUF_PT (XBUFFER (w->buffer)),
6215 BUF_PT_BYTE (XBUFFER (w->buffer)));
6216 w->start_at_line_beg = Qt;
6217 }
6218 }
6219 }
6220
6221 FRAME_ROOT_WINDOW (f) = data->root_window;
6222 /* Prevent "swapping out point" in the old selected window
6223 using the buffer that has been restored into it.
6224 We already swapped out point that from that window's old buffer. */
6225 selected_window = Qnil;
6226
6227 /* Arrange *not* to restore point in the buffer that was
6228 current when the window configuration was saved. */
6229 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6230 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6231 make_number (old_point),
6232 XWINDOW (data->current_window)->buffer);
6233
6234 Fselect_window (data->current_window, Qnil);
6235 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
6236 = selected_window;
6237
6238 if (NILP (data->focus_frame)
6239 || (FRAMEP (data->focus_frame)
6240 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6241 Fredirect_frame_focus (frame, data->focus_frame);
6242
6243 #if 0 /* I don't understand why this is needed, and it causes problems
6244 when the frame's old selected window has been deleted. */
6245 if (f != selected_frame && FRAME_WINDOW_P (f))
6246 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
6247 0, 0, Qnil);
6248 #endif
6249
6250 /* Set the screen height to the value it had before this function. */
6251 if (previous_frame_lines != FRAME_LINES (f)
6252 || previous_frame_cols != FRAME_COLS (f))
6253 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6254 0, 0, 0);
6255 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6256 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6257 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6258 make_number (0));
6259 #ifdef HAVE_WINDOW_SYSTEM
6260 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6261 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6262 make_number (0));
6263 #endif
6264 #endif
6265
6266 /* Now, free glyph matrices in windows that were not reused. */
6267 for (i = n = 0; i < n_leaf_windows; ++i)
6268 {
6269 if (NILP (leaf_windows[i]->buffer))
6270 {
6271 /* Assert it's not reused as a combination. */
6272 xassert (NILP (leaf_windows[i]->hchild)
6273 && NILP (leaf_windows[i]->vchild));
6274 free_window_matrices (leaf_windows[i]);
6275 }
6276 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6277 ++n;
6278 }
6279
6280 adjust_glyphs (f);
6281
6282 UNBLOCK_INPUT;
6283
6284 /* Fselect_window will have made f the selected frame, so we
6285 reselect the proper frame here. Fhandle_switch_frame will change the
6286 selected window too, but that doesn't make the call to
6287 Fselect_window above totally superfluous; it still sets f's
6288 selected window. */
6289 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6290 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6291
6292 run_window_configuration_change_hook (f);
6293 }
6294
6295 if (!NILP (new_current_buffer))
6296 Fset_buffer (new_current_buffer);
6297
6298 Vminibuf_scroll_window = data->minibuf_scroll_window;
6299 minibuf_selected_window = data->minibuf_selected_window;
6300
6301 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6302 }
6303
6304 /* Mark all windows now on frame as deleted
6305 by setting their buffers to nil. */
6306
6307 void
6308 delete_all_subwindows (w)
6309 register struct window *w;
6310 {
6311 if (!NILP (w->next))
6312 delete_all_subwindows (XWINDOW (w->next));
6313 if (!NILP (w->vchild))
6314 delete_all_subwindows (XWINDOW (w->vchild));
6315 if (!NILP (w->hchild))
6316 delete_all_subwindows (XWINDOW (w->hchild));
6317
6318 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6319
6320 if (!NILP (w->buffer))
6321 unshow_buffer (w);
6322
6323 /* We set all three of these fields to nil, to make sure that we can
6324 distinguish this dead window from any live window. Live leaf
6325 windows will have buffer set, and combination windows will have
6326 vchild or hchild set. */
6327 w->buffer = Qnil;
6328 w->vchild = Qnil;
6329 w->hchild = Qnil;
6330
6331 Vwindow_list = Qnil;
6332 }
6333 \f
6334 static int
6335 count_windows (window)
6336 register struct window *window;
6337 {
6338 register int count = 1;
6339 if (!NILP (window->next))
6340 count += count_windows (XWINDOW (window->next));
6341 if (!NILP (window->vchild))
6342 count += count_windows (XWINDOW (window->vchild));
6343 if (!NILP (window->hchild))
6344 count += count_windows (XWINDOW (window->hchild));
6345 return count;
6346 }
6347
6348
6349 /* Fill vector FLAT with leaf windows under W, starting at index I.
6350 Value is last index + 1. */
6351
6352 static int
6353 get_leaf_windows (w, flat, i)
6354 struct window *w;
6355 struct window **flat;
6356 int i;
6357 {
6358 while (w)
6359 {
6360 if (!NILP (w->hchild))
6361 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6362 else if (!NILP (w->vchild))
6363 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6364 else
6365 flat[i++] = w;
6366
6367 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6368 }
6369
6370 return i;
6371 }
6372
6373
6374 /* Return a pointer to the glyph W's physical cursor is on. Value is
6375 null if W's current matrix is invalid, so that no meaningfull glyph
6376 can be returned. */
6377
6378 struct glyph *
6379 get_phys_cursor_glyph (w)
6380 struct window *w;
6381 {
6382 struct glyph_row *row;
6383 struct glyph *glyph;
6384
6385 if (w->phys_cursor.vpos >= 0
6386 && w->phys_cursor.vpos < w->current_matrix->nrows
6387 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6388 row->enabled_p)
6389 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6390 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6391 else
6392 glyph = NULL;
6393
6394 return glyph;
6395 }
6396
6397
6398 static int
6399 save_window_save (window, vector, i)
6400 Lisp_Object window;
6401 struct Lisp_Vector *vector;
6402 int i;
6403 {
6404 register struct saved_window *p;
6405 register struct window *w;
6406 register Lisp_Object tem;
6407
6408 for (;!NILP (window); window = w->next)
6409 {
6410 p = SAVED_WINDOW_N (vector, i);
6411 w = XWINDOW (window);
6412
6413 XSETFASTINT (w->temslot, i); i++;
6414 p->window = window;
6415 p->buffer = w->buffer;
6416 p->left_col = w->left_col;
6417 p->top_line = w->top_line;
6418 p->total_cols = w->total_cols;
6419 p->total_lines = w->total_lines;
6420 p->hscroll = w->hscroll;
6421 p->min_hscroll = w->min_hscroll;
6422 p->display_table = w->display_table;
6423 p->orig_top_line = w->orig_top_line;
6424 p->orig_total_lines = w->orig_total_lines;
6425 p->left_margin_cols = w->left_margin_cols;
6426 p->right_margin_cols = w->right_margin_cols;
6427 p->left_fringe_width = w->left_fringe_width;
6428 p->right_fringe_width = w->right_fringe_width;
6429 p->fringes_outside_margins = w->fringes_outside_margins;
6430 p->scroll_bar_width = w->scroll_bar_width;
6431 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6432 p->dedicated = w->dedicated;
6433 p->resize_proportionally = w->resize_proportionally;
6434 if (!NILP (w->buffer))
6435 {
6436 /* Save w's value of point in the window configuration.
6437 If w is the selected window, then get the value of point
6438 from the buffer; pointm is garbage in the selected window. */
6439 if (EQ (window, selected_window))
6440 {
6441 p->pointm = Fmake_marker ();
6442 set_marker_both (p->pointm, w->buffer,
6443 BUF_PT (XBUFFER (w->buffer)),
6444 BUF_PT_BYTE (XBUFFER (w->buffer)));
6445 }
6446 else
6447 p->pointm = Fcopy_marker (w->pointm, Qnil);
6448
6449 p->start = Fcopy_marker (w->start, Qnil);
6450 p->start_at_line_beg = w->start_at_line_beg;
6451
6452 tem = XBUFFER (w->buffer)->mark;
6453 p->mark = Fcopy_marker (tem, Qnil);
6454 }
6455 else
6456 {
6457 p->pointm = Qnil;
6458 p->start = Qnil;
6459 p->mark = Qnil;
6460 p->start_at_line_beg = Qnil;
6461 }
6462
6463 if (NILP (w->parent))
6464 p->parent = Qnil;
6465 else
6466 p->parent = XWINDOW (w->parent)->temslot;
6467
6468 if (NILP (w->prev))
6469 p->prev = Qnil;
6470 else
6471 p->prev = XWINDOW (w->prev)->temslot;
6472
6473 if (!NILP (w->vchild))
6474 i = save_window_save (w->vchild, vector, i);
6475 if (!NILP (w->hchild))
6476 i = save_window_save (w->hchild, vector, i);
6477 }
6478
6479 return i;
6480 }
6481
6482 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6483 Scurrent_window_configuration, 0, 1, 0,
6484 doc: /* Return an object representing the current window configuration of FRAME.
6485 If FRAME is nil or omitted, use the selected frame.
6486 This describes the number of windows, their sizes and current buffers,
6487 and for each displayed buffer, where display starts, and the positions of
6488 point and mark. An exception is made for point in the current buffer:
6489 its value is -not- saved.
6490 This also records the currently selected frame, and FRAME's focus
6491 redirection (see `redirect-frame-focus'). */)
6492 (frame)
6493 Lisp_Object frame;
6494 {
6495 register Lisp_Object tem;
6496 register int n_windows;
6497 register struct save_window_data *data;
6498 register int i;
6499 FRAME_PTR f;
6500
6501 if (NILP (frame))
6502 frame = selected_frame;
6503 CHECK_LIVE_FRAME (frame);
6504 f = XFRAME (frame);
6505
6506 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6507 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6508 PVEC_WINDOW_CONFIGURATION);
6509
6510 data->frame_cols = FRAME_COLS (f);
6511 data->frame_lines = FRAME_LINES (f);
6512 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6513 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6514 data->selected_frame = selected_frame;
6515 data->current_window = FRAME_SELECTED_WINDOW (f);
6516 XSETBUFFER (data->current_buffer, current_buffer);
6517 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6518 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6519 data->root_window = FRAME_ROOT_WINDOW (f);
6520 data->focus_frame = FRAME_FOCUS_FRAME (f);
6521 tem = Fmake_vector (make_number (n_windows), Qnil);
6522 data->saved_windows = tem;
6523 for (i = 0; i < n_windows; i++)
6524 XVECTOR (tem)->contents[i]
6525 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6526 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6527 XSETWINDOW_CONFIGURATION (tem, data);
6528 return (tem);
6529 }
6530
6531 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
6532 0, UNEVALLED, 0,
6533 doc: /* Execute BODY, preserving window sizes and contents.
6534 Return the value of the last form in BODY.
6535 Restore which buffer appears in which window, where display starts,
6536 and the value of point and mark for each window.
6537 Also restore the choice of selected window.
6538 Also restore which buffer is current.
6539 Does not restore the value of point in current buffer.
6540 usage: (save-window-excursion BODY...) */)
6541 (args)
6542 Lisp_Object args;
6543 {
6544 register Lisp_Object val;
6545 register int count = SPECPDL_INDEX ();
6546
6547 record_unwind_protect (Fset_window_configuration,
6548 Fcurrent_window_configuration (Qnil));
6549 val = Fprogn (args);
6550 return unbind_to (count, val);
6551 }
6552
6553
6554 \f
6555 /***********************************************************************
6556 Window Split Tree
6557 ***********************************************************************/
6558
6559 static Lisp_Object
6560 window_tree (w)
6561 struct window *w;
6562 {
6563 Lisp_Object tail = Qnil;
6564 Lisp_Object result = Qnil;
6565
6566 while (w)
6567 {
6568 Lisp_Object wn;
6569
6570 XSETWINDOW (wn, w);
6571 if (!NILP (w->hchild))
6572 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6573 window_tree (XWINDOW (w->hchild))));
6574 else if (!NILP (w->vchild))
6575 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6576 window_tree (XWINDOW (w->vchild))));
6577
6578 if (NILP (result))
6579 {
6580 result = tail = Fcons (wn, Qnil);
6581 }
6582 else
6583 {
6584 XSETCDR (tail, Fcons (wn, Qnil));
6585 tail = XCDR (tail);
6586 }
6587
6588 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6589 }
6590
6591 return result;
6592 }
6593
6594
6595
6596 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6597 0, 1, 0,
6598 doc: /* Return the window tree for frame FRAME.
6599
6600 The return value is a list of the form (ROOT MINI), where ROOT
6601 represents the window tree of the frame's root window, and MINI
6602 is the frame's minibuffer window.
6603
6604 If the root window is not split, ROOT is the root window itself.
6605 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6606 horizontal split, and t for a vertical split, EDGES gives the combined
6607 size and position of the subwindows in the split, and the rest of the
6608 elements are the subwindows in the split. Each of the subwindows may
6609 again be a window or a list representing a window split, and so on.
6610 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6611
6612 If FRAME is nil or omitted, return information on the currently
6613 selected frame. */)
6614 (frame)
6615 Lisp_Object frame;
6616 {
6617 FRAME_PTR f;
6618
6619 if (NILP (frame))
6620 frame = selected_frame;
6621
6622 CHECK_FRAME (frame);
6623 f = XFRAME (frame);
6624
6625 if (!FRAME_LIVE_P (f))
6626 return Qnil;
6627
6628 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6629 }
6630
6631 \f
6632 /***********************************************************************
6633 Marginal Areas
6634 ***********************************************************************/
6635
6636 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6637 2, 3, 0,
6638 doc: /* Set width of marginal areas of window WINDOW.
6639 If WINDOW is nil, set margins of the currently selected window.
6640 Second arg LEFT-WIDTH specifies the number of character cells to
6641 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6642 does the same for the right marginal area. A nil width parameter
6643 means no margin. */)
6644 (window, left_width, right_width)
6645 Lisp_Object window, left_width, right_width;
6646 {
6647 struct window *w = decode_window (window);
6648
6649 /* Translate negative or zero widths to nil.
6650 Margins that are too wide have to be checked elsewhere. */
6651
6652 if (!NILP (left_width))
6653 {
6654 CHECK_NUMBER (left_width);
6655 if (XINT (left_width) <= 0)
6656 left_width = Qnil;
6657 }
6658
6659 if (!NILP (right_width))
6660 {
6661 CHECK_NUMBER (right_width);
6662 if (XINT (right_width) <= 0)
6663 right_width = Qnil;
6664 }
6665
6666 if (!EQ (w->left_margin_cols, left_width)
6667 || !EQ (w->right_margin_cols, right_width))
6668 {
6669 w->left_margin_cols = left_width;
6670 w->right_margin_cols = right_width;
6671
6672 adjust_window_margins (w);
6673
6674 ++windows_or_buffers_changed;
6675 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6676 }
6677
6678 return Qnil;
6679 }
6680
6681
6682 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6683 0, 1, 0,
6684 doc: /* Get width of marginal areas of window WINDOW.
6685 If WINDOW is omitted or nil, use the currently selected window.
6686 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6687 If a marginal area does not exist, its width will be returned
6688 as nil. */)
6689 (window)
6690 Lisp_Object window;
6691 {
6692 struct window *w = decode_window (window);
6693 return Fcons (w->left_margin_cols, w->right_margin_cols);
6694 }
6695
6696
6697 \f
6698 /***********************************************************************
6699 Fringes
6700 ***********************************************************************/
6701
6702 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6703 2, 4, 0,
6704 doc: /* Set the fringe widths of window WINDOW.
6705 If WINDOW is nil, set the fringe widths of the currently selected
6706 window.
6707 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6708 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6709 fringe width. If a fringe width arg is nil, that means to use the
6710 frame's default fringe width. Default fringe widths can be set with
6711 the command `set-fringe-style'.
6712 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6713 outside of the display margins. By default, fringes are drawn between
6714 display marginal areas and the text area. */)
6715 (window, left_width, right_width, outside_margins)
6716 Lisp_Object window, left_width, right_width, outside_margins;
6717 {
6718 struct window *w = decode_window (window);
6719
6720 if (!NILP (left_width))
6721 CHECK_NATNUM (left_width);
6722 if (!NILP (right_width))
6723 CHECK_NATNUM (right_width);
6724
6725 /* Do nothing on a tty. */
6726 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6727 && (!EQ (w->left_fringe_width, left_width)
6728 || !EQ (w->right_fringe_width, right_width)
6729 || !EQ (w->fringes_outside_margins, outside_margins)))
6730 {
6731 w->left_fringe_width = left_width;
6732 w->right_fringe_width = right_width;
6733 w->fringes_outside_margins = outside_margins;
6734
6735 adjust_window_margins (w);
6736
6737 clear_glyph_matrix (w->current_matrix);
6738 w->window_end_valid = Qnil;
6739
6740 ++windows_or_buffers_changed;
6741 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6742 }
6743
6744 return Qnil;
6745 }
6746
6747
6748 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6749 0, 1, 0,
6750 doc: /* Get width of fringes of window WINDOW.
6751 If WINDOW is omitted or nil, use the currently selected window.
6752 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6753 (window)
6754 Lisp_Object window;
6755 {
6756 struct window *w = decode_window (window);
6757
6758 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6759 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6760 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6761 ? Qt : Qnil), Qnil)));
6762 }
6763
6764
6765 \f
6766 /***********************************************************************
6767 Scroll bars
6768 ***********************************************************************/
6769
6770 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6771 2, 4, 0,
6772 doc: /* Set width and type of scroll bars of window WINDOW.
6773 If window is nil, set scroll bars of the currently selected window.
6774 Second parameter WIDTH specifies the pixel width for the scroll bar;
6775 this is automatically adjusted to a multiple of the frame column width.
6776 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6777 bar: left, right, or nil.
6778 If WIDTH is nil, use the frame's scroll-bar width.
6779 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6780 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6781 (window, width, vertical_type, horizontal_type)
6782 Lisp_Object window, width, vertical_type, horizontal_type;
6783 {
6784 struct window *w = decode_window (window);
6785
6786 if (!NILP (width))
6787 {
6788 CHECK_NATNUM (width);
6789
6790 if (XINT (width) == 0)
6791 vertical_type = Qnil;
6792 }
6793
6794 if (!(NILP (vertical_type)
6795 || EQ (vertical_type, Qleft)
6796 || EQ (vertical_type, Qright)
6797 || EQ (vertical_type, Qt)))
6798 error ("Invalid type of vertical scroll bar");
6799
6800 if (!EQ (w->scroll_bar_width, width)
6801 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6802 {
6803 w->scroll_bar_width = width;
6804 w->vertical_scroll_bar_type = vertical_type;
6805
6806 adjust_window_margins (w);
6807
6808 clear_glyph_matrix (w->current_matrix);
6809 w->window_end_valid = Qnil;
6810
6811 ++windows_or_buffers_changed;
6812 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6813 }
6814
6815 return Qnil;
6816 }
6817
6818
6819 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6820 0, 1, 0,
6821 doc: /* Get width and type of scroll bars of window WINDOW.
6822 If WINDOW is omitted or nil, use the currently selected window.
6823 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6824 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6825 value. */)
6826 (window)
6827 Lisp_Object window;
6828 {
6829 struct window *w = decode_window (window);
6830 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6831 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6832 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6833 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6834 Fcons (w->vertical_scroll_bar_type,
6835 Fcons (Qnil, Qnil))));
6836 }
6837
6838
6839 \f
6840 /***********************************************************************
6841 Smooth scrolling
6842 ***********************************************************************/
6843
6844 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6845 doc: /* Return the amount by which WINDOW is scrolled vertically.
6846 Use the selected window if WINDOW is nil or omitted.
6847 Normally, value is a multiple of the canonical character height of WINDOW;
6848 optional second arg PIXELS-P means value is measured in pixels. */)
6849 (window, pixels_p)
6850 Lisp_Object window, pixels_p;
6851 {
6852 Lisp_Object result;
6853 struct frame *f;
6854 struct window *w;
6855
6856 if (NILP (window))
6857 window = selected_window;
6858 else
6859 CHECK_WINDOW (window);
6860 w = XWINDOW (window);
6861 f = XFRAME (w->frame);
6862
6863 if (FRAME_WINDOW_P (f))
6864 result = (NILP (pixels_p)
6865 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6866 : make_number (-w->vscroll));
6867 else
6868 result = make_number (0);
6869 return result;
6870 }
6871
6872
6873 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6874 2, 3, 0,
6875 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6876 WINDOW nil means use the selected window. Normally, VSCROLL is a
6877 non-negative multiple of the canonical character height of WINDOW;
6878 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6879 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6880 corresponds to an integral number of pixels. The return value is the
6881 result of this rounding.
6882 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6883 (window, vscroll, pixels_p)
6884 Lisp_Object window, vscroll, pixels_p;
6885 {
6886 struct window *w;
6887 struct frame *f;
6888
6889 if (NILP (window))
6890 window = selected_window;
6891 else
6892 CHECK_WINDOW (window);
6893 CHECK_NUMBER_OR_FLOAT (vscroll);
6894
6895 w = XWINDOW (window);
6896 f = XFRAME (w->frame);
6897
6898 if (FRAME_WINDOW_P (f))
6899 {
6900 int old_dy = w->vscroll;
6901
6902 w->vscroll = - (NILP (pixels_p)
6903 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6904 : XFLOATINT (vscroll));
6905 w->vscroll = min (w->vscroll, 0);
6906
6907 if (w->vscroll != old_dy)
6908 {
6909 /* Adjust glyph matrix of the frame if the virtual display
6910 area becomes larger than before. */
6911 if (w->vscroll < 0 && w->vscroll < old_dy)
6912 adjust_glyphs (f);
6913
6914 /* Prevent redisplay shortcuts. */
6915 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6916 }
6917 }
6918
6919 return Fwindow_vscroll (window, pixels_p);
6920 }
6921
6922 \f
6923 /* Call FN for all leaf windows on frame F. FN is called with the
6924 first argument being a pointer to the leaf window, and with
6925 additional argument USER_DATA. Stops when FN returns 0. */
6926
6927 void
6928 foreach_window (f, fn, user_data)
6929 struct frame *f;
6930 int (* fn) P_ ((struct window *, void *));
6931 void *user_data;
6932 {
6933 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6934 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6935 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6936 }
6937
6938
6939 /* Helper function for foreach_window. Call FN for all leaf windows
6940 reachable from W. FN is called with the first argument being a
6941 pointer to the leaf window, and with additional argument USER_DATA.
6942 Stop when FN returns 0. Value is 0 if stopped by FN. */
6943
6944 static int
6945 foreach_window_1 (w, fn, user_data)
6946 struct window *w;
6947 int (* fn) P_ ((struct window *, void *));
6948 void *user_data;
6949 {
6950 int cont;
6951
6952 for (cont = 1; w && cont;)
6953 {
6954 if (!NILP (w->hchild))
6955 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6956 else if (!NILP (w->vchild))
6957 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6958 else
6959 cont = fn (w, user_data);
6960
6961 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6962 }
6963
6964 return cont;
6965 }
6966
6967
6968 /* Freeze or unfreeze the window start of W unless it is a
6969 mini-window or the selected window. FREEZE_P non-null means freeze
6970 the window start. */
6971
6972 static int
6973 freeze_window_start (w, freeze_p)
6974 struct window *w;
6975 void *freeze_p;
6976 {
6977 if (MINI_WINDOW_P (w)
6978 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6979 && (w == XWINDOW (selected_window)
6980 || (MINI_WINDOW_P (XWINDOW (selected_window))
6981 && ! NILP (Vminibuf_scroll_window)
6982 && w == XWINDOW (Vminibuf_scroll_window)))))
6983 freeze_p = NULL;
6984
6985 w->frozen_window_start_p = freeze_p != NULL;
6986 return 1;
6987 }
6988
6989
6990 /* Freeze or unfreeze the window starts of all leaf windows on frame
6991 F, except the selected window and a mini-window. FREEZE_P non-zero
6992 means freeze the window start. */
6993
6994 void
6995 freeze_window_starts (f, freeze_p)
6996 struct frame *f;
6997 int freeze_p;
6998 {
6999 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
7000 }
7001
7002 \f
7003 /***********************************************************************
7004 Initialization
7005 ***********************************************************************/
7006
7007 /* Return 1 if window configurations C1 and C2
7008 describe the same state of affairs. This is used by Fequal. */
7009
7010 int
7011 compare_window_configurations (c1, c2, ignore_positions)
7012 Lisp_Object c1, c2;
7013 int ignore_positions;
7014 {
7015 register struct save_window_data *d1, *d2;
7016 struct Lisp_Vector *sw1, *sw2;
7017 int i;
7018
7019 CHECK_WINDOW_CONFIGURATION (c1);
7020 CHECK_WINDOW_CONFIGURATION (c2);
7021
7022 d1 = (struct save_window_data *) XVECTOR (c1);
7023 d2 = (struct save_window_data *) XVECTOR (c2);
7024 sw1 = XVECTOR (d1->saved_windows);
7025 sw2 = XVECTOR (d2->saved_windows);
7026
7027 if (d1->frame_cols != d2->frame_cols)
7028 return 0;
7029 if (d1->frame_lines != d2->frame_lines)
7030 return 0;
7031 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
7032 return 0;
7033 if (! EQ (d1->selected_frame, d2->selected_frame))
7034 return 0;
7035 /* Don't compare the current_window field directly.
7036 Instead see w1_is_current and w2_is_current, below. */
7037 if (! EQ (d1->current_buffer, d2->current_buffer))
7038 return 0;
7039 if (! ignore_positions)
7040 {
7041 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
7042 return 0;
7043 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
7044 return 0;
7045 }
7046 /* Don't compare the root_window field.
7047 We don't require the two configurations
7048 to use the same window object,
7049 and the two root windows must be equivalent
7050 if everything else compares equal. */
7051 if (! EQ (d1->focus_frame, d2->focus_frame))
7052 return 0;
7053
7054 /* Verify that the two confis have the same number of windows. */
7055 if (sw1->size != sw2->size)
7056 return 0;
7057
7058 for (i = 0; i < sw1->size; i++)
7059 {
7060 struct saved_window *p1, *p2;
7061 int w1_is_current, w2_is_current;
7062
7063 p1 = SAVED_WINDOW_N (sw1, i);
7064 p2 = SAVED_WINDOW_N (sw2, i);
7065
7066 /* Verify that the current windows in the two
7067 configurations correspond to each other. */
7068 w1_is_current = EQ (d1->current_window, p1->window);
7069 w2_is_current = EQ (d2->current_window, p2->window);
7070
7071 if (w1_is_current != w2_is_current)
7072 return 0;
7073
7074 /* Verify that the corresponding windows do match. */
7075 if (! EQ (p1->buffer, p2->buffer))
7076 return 0;
7077 if (! EQ (p1->left_col, p2->left_col))
7078 return 0;
7079 if (! EQ (p1->top_line, p2->top_line))
7080 return 0;
7081 if (! EQ (p1->total_cols, p2->total_cols))
7082 return 0;
7083 if (! EQ (p1->total_lines, p2->total_lines))
7084 return 0;
7085 if (! EQ (p1->display_table, p2->display_table))
7086 return 0;
7087 if (! EQ (p1->parent, p2->parent))
7088 return 0;
7089 if (! EQ (p1->prev, p2->prev))
7090 return 0;
7091 if (! ignore_positions)
7092 {
7093 if (! EQ (p1->hscroll, p2->hscroll))
7094 return 0;
7095 if (!EQ (p1->min_hscroll, p2->min_hscroll))
7096 return 0;
7097 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
7098 return 0;
7099 if (NILP (Fequal (p1->start, p2->start)))
7100 return 0;
7101 if (NILP (Fequal (p1->pointm, p2->pointm)))
7102 return 0;
7103 if (NILP (Fequal (p1->mark, p2->mark)))
7104 return 0;
7105 }
7106 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
7107 return 0;
7108 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
7109 return 0;
7110 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
7111 return 0;
7112 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
7113 return 0;
7114 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
7115 return 0;
7116 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
7117 return 0;
7118 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
7119 return 0;
7120 }
7121
7122 return 1;
7123 }
7124
7125 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7126 Scompare_window_configurations, 2, 2, 0,
7127 doc: /* Compare two window configurations as regards the structure of windows.
7128 This function ignores details such as the values of point and mark
7129 and scrolling positions. */)
7130 (x, y)
7131 Lisp_Object x, y;
7132 {
7133 if (compare_window_configurations (x, y, 1))
7134 return Qt;
7135 return Qnil;
7136 }
7137 \f
7138 void
7139 init_window_once ()
7140 {
7141 struct frame *f = make_initial_frame ();
7142 XSETFRAME (selected_frame, f);
7143 Vterminal_frame = selected_frame;
7144 minibuf_window = f->minibuffer_window;
7145 selected_window = f->selected_window;
7146 last_nonminibuf_frame = f;
7147
7148 window_initialized = 1;
7149 }
7150
7151 void
7152 init_window ()
7153 {
7154 Vwindow_list = Qnil;
7155 }
7156
7157 void
7158 syms_of_window ()
7159 {
7160 Qscroll_up = intern ("scroll-up");
7161 staticpro (&Qscroll_up);
7162
7163 Qscroll_down = intern ("scroll-down");
7164 staticpro (&Qscroll_down);
7165
7166 Qwindow_size_fixed = intern ("window-size-fixed");
7167 staticpro (&Qwindow_size_fixed);
7168 Fset (Qwindow_size_fixed, Qnil);
7169
7170 staticpro (&Qwindow_configuration_change_hook);
7171 Qwindow_configuration_change_hook
7172 = intern ("window-configuration-change-hook");
7173
7174 Qwindowp = intern ("windowp");
7175 staticpro (&Qwindowp);
7176
7177 Qwindow_configuration_p = intern ("window-configuration-p");
7178 staticpro (&Qwindow_configuration_p);
7179
7180 Qwindow_live_p = intern ("window-live-p");
7181 staticpro (&Qwindow_live_p);
7182
7183 Qdisplay_buffer = intern ("display-buffer");
7184 staticpro (&Qdisplay_buffer);
7185
7186 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
7187 staticpro (&Qtemp_buffer_show_hook);
7188
7189 staticpro (&Vwindow_list);
7190
7191 minibuf_selected_window = Qnil;
7192 staticpro (&minibuf_selected_window);
7193
7194 window_scroll_pixel_based_preserve_x = -1;
7195 window_scroll_pixel_based_preserve_y = -1;
7196 window_scroll_preserve_hpos = -1;
7197 window_scroll_preserve_vpos = -1;
7198
7199 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
7200 doc: /* Non-nil means call as function to display a help buffer.
7201 The function is called with one argument, the buffer to be displayed.
7202 Used by `with-output-to-temp-buffer'.
7203 If this function is used, then it must do the entire job of showing
7204 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7205 Vtemp_buffer_show_function = Qnil;
7206
7207 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
7208 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7209 Vminibuf_scroll_window = Qnil;
7210
7211 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
7212 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7213 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7214 is displayed in the `mode-line' face. */);
7215 mode_line_in_non_selected_windows = 1;
7216
7217 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
7218 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7219 Vother_window_scroll_buffer = Qnil;
7220
7221 DEFVAR_BOOL ("auto-window-vscroll", &auto_window_vscroll_p,
7222 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7223 auto_window_vscroll_p = 1;
7224
7225 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
7226 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7227 next_screen_context_lines = 2;
7228
7229 DEFVAR_INT ("window-min-height", &window_min_height,
7230 doc: /* Allow deleting windows less than this tall.
7231 The value is measured in line units. If a window wants a modeline it
7232 is counted as one line.
7233
7234 Emacs honors settings of this variable when enlarging or shrinking
7235 windows vertically. A value less than 1 is invalid. */);
7236 window_min_height = 4;
7237
7238 DEFVAR_INT ("window-min-width", &window_min_width,
7239 doc: /* Allow deleting windows less than this wide.
7240 The value is measured in characters and includes any fringes or
7241 the scrollbar.
7242
7243 Emacs honors settings of this variable when enlarging or shrinking
7244 windows horizontally. A value less than 2 is invalid. */);
7245 window_min_width = 10;
7246
7247 DEFVAR_LISP ("scroll-preserve-screen-position",
7248 &Vscroll_preserve_screen_position,
7249 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
7250 A value of nil means point does not keep its screen position except
7251 at the scroll margin or window boundary respectively.
7252 A value of t means point keeps its screen position if the scroll
7253 command moved it vertically out of the window, e.g. when scrolling
7254 by full screens.
7255 Any other value means point always keeps its screen position. */);
7256 Vscroll_preserve_screen_position = Qnil;
7257
7258 DEFVAR_LISP ("window-point-insertion-type", &Vwindow_point_insertion_type,
7259 doc: /* Type of marker to use for `window-point'. */);
7260 Vwindow_point_insertion_type = Qnil;
7261
7262 DEFVAR_LISP ("window-configuration-change-hook",
7263 &Vwindow_configuration_change_hook,
7264 doc: /* Functions to call when window configuration changes.
7265 The buffer-local part is run once per window, with the relevant window
7266 selected; while the global part is run only once for the modified frame,
7267 with the relevant frame selected. */);
7268 Vwindow_configuration_change_hook = Qnil;
7269
7270 DEFVAR_LISP ("recenter-redisplay", &Vrecenter_redisplay,
7271 doc: /* If non-nil, then the `recenter' command with a nil argument
7272 the entire frame to be redrawn; the special value `tty' causes the
7273 frame to be redrawn only if it is a tty frame. */);
7274 Vrecenter_redisplay = Qtty;
7275
7276
7277 defsubr (&Sselected_window);
7278 defsubr (&Sminibuffer_window);
7279 defsubr (&Swindow_minibuffer_p);
7280 defsubr (&Swindowp);
7281 defsubr (&Swindow_live_p);
7282 defsubr (&Spos_visible_in_window_p);
7283 defsubr (&Swindow_line_height);
7284 defsubr (&Swindow_buffer);
7285 defsubr (&Swindow_height);
7286 defsubr (&Swindow_width);
7287 defsubr (&Swindow_full_width_p);
7288 defsubr (&Swindow_hscroll);
7289 defsubr (&Sset_window_hscroll);
7290 defsubr (&Swindow_redisplay_end_trigger);
7291 defsubr (&Sset_window_redisplay_end_trigger);
7292 defsubr (&Swindow_edges);
7293 defsubr (&Swindow_pixel_edges);
7294 defsubr (&Swindow_inside_edges);
7295 defsubr (&Swindow_inside_pixel_edges);
7296 defsubr (&Scoordinates_in_window_p);
7297 defsubr (&Swindow_at);
7298 defsubr (&Swindow_point);
7299 defsubr (&Swindow_start);
7300 defsubr (&Swindow_end);
7301 defsubr (&Sset_window_point);
7302 defsubr (&Sset_window_start);
7303 defsubr (&Swindow_dedicated_p);
7304 defsubr (&Sset_window_dedicated_p);
7305 defsubr (&Swindow_display_table);
7306 defsubr (&Sset_window_display_table);
7307 defsubr (&Snext_window);
7308 defsubr (&Sprevious_window);
7309 defsubr (&Sother_window);
7310 defsubr (&Sget_lru_window);
7311 defsubr (&Sget_largest_window);
7312 defsubr (&Sget_buffer_window);
7313 defsubr (&Sdelete_other_windows);
7314 defsubr (&Sdelete_windows_on);
7315 defsubr (&Sreplace_buffer_in_windows);
7316 defsubr (&Sdelete_window);
7317 defsubr (&Sset_window_buffer);
7318 defsubr (&Sselect_window);
7319 defsubr (&Sforce_window_update);
7320 defsubr (&Ssplit_window);
7321 defsubr (&Senlarge_window);
7322 defsubr (&Sshrink_window);
7323 defsubr (&Sadjust_window_trailing_edge);
7324 defsubr (&Sscroll_up);
7325 defsubr (&Sscroll_down);
7326 defsubr (&Sscroll_left);
7327 defsubr (&Sscroll_right);
7328 defsubr (&Sother_window_for_scrolling);
7329 defsubr (&Sscroll_other_window);
7330 defsubr (&Sminibuffer_selected_window);
7331 defsubr (&Srecenter);
7332 defsubr (&Swindow_text_height);
7333 defsubr (&Smove_to_window_line);
7334 defsubr (&Swindow_configuration_p);
7335 defsubr (&Swindow_configuration_frame);
7336 defsubr (&Sset_window_configuration);
7337 defsubr (&Scurrent_window_configuration);
7338 defsubr (&Ssave_window_excursion);
7339 defsubr (&Swindow_tree);
7340 defsubr (&Sset_window_margins);
7341 defsubr (&Swindow_margins);
7342 defsubr (&Sset_window_fringes);
7343 defsubr (&Swindow_fringes);
7344 defsubr (&Sset_window_scroll_bars);
7345 defsubr (&Swindow_scroll_bars);
7346 defsubr (&Swindow_vscroll);
7347 defsubr (&Sset_window_vscroll);
7348 defsubr (&Scompare_window_configurations);
7349 defsubr (&Swindow_list);
7350 defsubr (&Swindow_parameters);
7351 defsubr (&Swindow_parameter);
7352 defsubr (&Sset_window_parameter);
7353
7354 }
7355
7356 void
7357 keys_of_window ()
7358 {
7359 initial_define_key (control_x_map, '1', "delete-other-windows");
7360 initial_define_key (control_x_map, '2', "split-window");
7361 initial_define_key (control_x_map, '0', "delete-window");
7362 initial_define_key (control_x_map, 'o', "other-window");
7363 initial_define_key (control_x_map, '^', "enlarge-window");
7364 initial_define_key (control_x_map, '<', "scroll-left");
7365 initial_define_key (control_x_map, '>', "scroll-right");
7366
7367 initial_define_key (global_map, Ctl ('V'), "scroll-up");
7368 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7369 initial_define_key (meta_map, 'v', "scroll-down");
7370
7371 initial_define_key (global_map, Ctl('L'), "recenter");
7372 initial_define_key (meta_map, 'r', "move-to-window-line");
7373 }
7374
7375 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
7376 (do not change this comment) */