* doc/lispref/modes.texi (Defining Minor Modes): Use C-backspace, not C-delete.
[emacs.git] / src / window.c
blobf9a2d31b4fe3347e5540b59c1b9a9cd3c9b7f03a
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, 2010
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
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.
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.
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/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <setjmp.h>
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. */
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
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;
60 extern Lisp_Object Qleft_margin, Qright_margin;
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));
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.
94 This value is always the same as
95 FRAME_SELECTED_WINDOW (selected_frame). */
97 Lisp_Object selected_window;
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. */
103 Lisp_Object Vwindow_list;
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. */
110 Lisp_Object minibuf_window;
112 /* Non-nil means it is the window whose mode line should be
113 shown as the selected window when the minibuffer is selected. */
115 Lisp_Object minibuf_selected_window;
117 /* Non-nil means it is the window for C-M-v to scroll
118 when the mini-buffer is selected. */
120 Lisp_Object Vminibuf_scroll_window;
122 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
124 Lisp_Object Vother_window_scroll_buffer;
126 /* Non-nil means it's function to call to display temp buffers. */
128 Lisp_Object Vtemp_buffer_show_function;
130 /* Non-zero means line and page scrolling on tall lines (with images)
131 does partial scrolling by modifying window-vscroll. */
133 int auto_window_vscroll_p;
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;
140 /* If a window gets smaller than either of these, it is removed. */
142 EMACS_INT window_min_height;
143 EMACS_INT window_min_width;
145 /* Hook run at end of temp_output_buffer_show. */
147 Lisp_Object Qtemp_buffer_show_hook;
149 /* Number of lines of continuity in scrolling by screenfuls. */
151 EMACS_INT next_screen_context_lines;
153 /* Incremented for each window created. */
155 static int sequence_number;
157 /* Nonzero after init_window_once has finished. */
159 static int window_initialized;
161 /* Hook to run when window config changes. */
163 static Lisp_Object Qwindow_configuration_change_hook;
164 static Lisp_Object Vwindow_configuration_change_hook;
166 /* Non-nil means scroll commands try to put point
167 at the same screen height as previously. */
169 Lisp_Object Vscroll_preserve_screen_position;
171 /* Non-nil means that text is inserted before window's markers. */
173 Lisp_Object Vwindow_point_insertion_type;
175 /* Incremented by 1 whenever a window is deleted. */
177 int window_deletion_count;
179 /* Used by the function window_scroll_pixel_based */
181 static int window_scroll_pixel_based_preserve_x;
182 static int window_scroll_pixel_based_preserve_y;
184 /* Same for window_scroll_line_based. */
186 static int window_scroll_preserve_hpos;
187 static int window_scroll_preserve_vpos;
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 */
194 extern EMACS_INT scroll_margin;
196 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
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. */
202 static Lisp_Object Vrecenter_redisplay;
203 extern Lisp_Object Qtty;
206 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
207 doc: /* Return t if OBJECT is a window. */)
208 (object)
209 Lisp_Object object;
211 return WINDOWP (object) ? Qt : Qnil;
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;
219 return WINDOW_LIVE_P (object) ? Qt : Qnil;
222 Lisp_Object
223 make_window ()
225 Lisp_Object val;
226 register struct window *p;
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;
271 Vwindow_list = Qnil;
272 return val;
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. */)
279 return selected_window;
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;
289 if (NILP (frame))
290 frame = selected_frame;
291 CHECK_LIVE_FRAME (frame);
292 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
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;
301 struct window *w = decode_window (window);
302 return MINI_WINDOW_P (w) ? Qt : Qnil;
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.
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;
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;
334 w = decode_window (window);
335 buf = XBUFFER (w->buffer);
336 SET_TEXT_POS_FROM_MARKER (top, w->start);
338 if (EQ (pos, Qt))
339 posint = -1;
340 else if (!NILP (pos))
342 CHECK_NUMBER_COERCE_MARKER (pos);
343 posint = XINT (pos);
345 else if (w == XWINDOW (selected_window))
346 posint = PT;
347 else
348 posint = XMARKER (w->pointm)->charpos;
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;
360 if (!NILP (in_window) && !NILP (partially))
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));
370 return in_window;
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.
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.
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.
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;
395 register struct window *w;
396 register struct buffer *b;
397 struct glyph_row *row, *end_row;
398 int max_y, crop, i, n;
400 w = decode_window (window);
402 if (noninteractive
403 || w->pseudo_window_p)
404 return Qnil;
406 CHECK_BUFFER (w->buffer);
407 b = XBUFFER (w->buffer);
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;
417 if (NILP (line))
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;
427 if (EQ (line, Qheader_line))
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));
439 if (EQ (line, Qmode_line))
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));
451 CHECK_NUMBER (line);
452 n = XINT (line);
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;
459 while ((n < 0 || i < n)
460 && row <= end_row && row->enabled_p
461 && row->y + row->height < max_y)
462 row++, i++;
464 if (row > end_row || !row->enabled_p)
465 return Qnil;
467 if (++n < 0)
469 if (-n > i)
470 return Qnil;
471 row += n;
472 i += n;
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));
485 static struct window *
486 decode_window (window)
487 register Lisp_Object window;
489 if (NILP (window))
490 return XWINDOW (selected_window);
492 CHECK_LIVE_WINDOW (window);
493 return XWINDOW (window);
496 static struct window *
497 decode_any_window (window)
498 register Lisp_Object window;
500 if (NILP (window))
501 return XWINDOW (selected_window);
503 CHECK_WINDOW (window);
504 return XWINDOW (window);
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;
513 return decode_window (window)->buffer;
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.
520 The return value includes WINDOW's mode line and header line, if any.
522 Note: The function does not take into account the value of `line-spacing'
523 when calculating the number of lines in WINDOW. */)
524 (window)
525 Lisp_Object window;
527 return decode_any_window (window)->total_lines;
530 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
531 doc: /* Return the number of display columns in WINDOW.
532 WINDOW defaults to the selected window.
534 Note: The return value is the number of columns available for text in
535 WINDOW. If you want to find out how many columns WINDOW takes up, use
536 (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
537 (window)
538 Lisp_Object window;
540 return make_number (window_box_text_cols (decode_any_window (window)));
543 DEFUN ("window-full-width-p", Fwindow_full_width_p, Swindow_full_width_p, 0, 1, 0,
544 doc: /* Return t if WINDOW is as wide as its frame.
545 WINDOW defaults to the selected window. */)
546 (window)
547 Lisp_Object window;
549 return WINDOW_FULL_WIDTH_P (decode_any_window (window)) ? Qt : Qnil;
552 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
553 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
554 WINDOW defaults to the selected window. */)
555 (window)
556 Lisp_Object window;
558 return decode_window (window)->hscroll;
561 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
562 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
563 Return NCOL. NCOL should be zero or positive.
565 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
566 window so that the location of point moves off-window. */)
567 (window, ncol)
568 Lisp_Object window, ncol;
570 struct window *w = decode_window (window);
571 int hscroll;
573 CHECK_NUMBER (ncol);
574 hscroll = max (0, XINT (ncol));
576 /* Prevent redisplay shortcuts when changing the hscroll. */
577 if (XINT (w->hscroll) != hscroll)
578 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
580 w->hscroll = make_number (hscroll);
581 return ncol;
584 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
585 Swindow_redisplay_end_trigger, 0, 1, 0,
586 doc: /* Return WINDOW's redisplay end trigger value.
587 WINDOW defaults to the selected window.
588 See `set-window-redisplay-end-trigger' for more information. */)
589 (window)
590 Lisp_Object window;
592 return decode_window (window)->redisplay_end_trigger;
595 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
596 Sset_window_redisplay_end_trigger, 2, 2, 0,
597 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
598 VALUE should be a buffer position (typically a marker) or nil.
599 If it is a buffer position, then if redisplay in WINDOW reaches a position
600 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
601 with two arguments: WINDOW, and the end trigger value.
602 Afterwards the end-trigger value is reset to nil. */)
603 (window, value)
604 register Lisp_Object window, value;
606 register struct window *w;
608 w = decode_window (window);
609 w->redisplay_end_trigger = value;
610 return value;
613 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
614 doc: /* Return a list of the edge coordinates of WINDOW.
615 The list has the form (LEFT TOP RIGHT BOTTOM).
616 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
617 all relative to 0, 0 at top left corner of frame.
619 RIGHT is one more than the rightmost column occupied by WINDOW.
620 BOTTOM is one more than the bottommost row occupied by WINDOW.
621 The edges include the space used by WINDOW's scroll bar, display
622 margins, fringes, header line, and/or mode line. For the edges of
623 just the text area, use `window-inside-edges'. */)
624 (window)
625 Lisp_Object window;
627 register struct window *w = decode_any_window (window);
629 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
630 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
631 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
632 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
633 Qnil))));
636 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
637 doc: /* Return a list of the edge pixel coordinates of WINDOW.
638 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
639 the top left corner of the frame.
641 RIGHT is one more than the rightmost x position occupied by WINDOW.
642 BOTTOM is one more than the bottommost y position occupied by WINDOW.
643 The pixel edges include the space used by WINDOW's scroll bar, display
644 margins, fringes, header line, and/or mode line. For the pixel edges
645 of just the text area, use `window-inside-pixel-edges'. */)
646 (window)
647 Lisp_Object window;
649 register struct window *w = decode_any_window (window);
651 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
652 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
653 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
654 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
655 Qnil))));
658 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
659 doc: /* Return a list of the edge coordinates of WINDOW.
660 The list has the form (LEFT TOP RIGHT BOTTOM).
661 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
662 all relative to 0, 0 at top left corner of frame.
664 RIGHT is one more than the rightmost column of WINDOW's text area.
665 BOTTOM is one more than the bottommost row of WINDOW's text area.
666 The inside edges do not include the space used by the WINDOW's scroll
667 bar, display margins, fringes, header line, and/or mode line. */)
668 (window)
669 Lisp_Object window;
671 register struct window *w = decode_any_window (window);
673 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
674 + WINDOW_LEFT_MARGIN_COLS (w)
675 + WINDOW_LEFT_FRINGE_COLS (w)),
676 make_number (WINDOW_TOP_EDGE_LINE (w)
677 + WINDOW_HEADER_LINE_LINES (w)),
678 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
679 - WINDOW_RIGHT_MARGIN_COLS (w)
680 - WINDOW_RIGHT_FRINGE_COLS (w)),
681 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
682 - WINDOW_MODE_LINE_LINES (w)));
685 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
686 doc: /* Return a list of the edge pixel coordinates of WINDOW.
687 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
688 the top left corner of the frame.
690 RIGHT is one more than the rightmost x position of WINDOW's text area.
691 BOTTOM is one more than the bottommost y position of WINDOW's text area.
692 The inside edges do not include the space used by WINDOW's scroll bar,
693 display margins, fringes, header line, and/or mode line. */)
694 (window)
695 Lisp_Object window;
697 register struct window *w = decode_any_window (window);
699 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
700 + WINDOW_LEFT_MARGIN_WIDTH (w)
701 + WINDOW_LEFT_FRINGE_WIDTH (w)),
702 make_number (WINDOW_TOP_EDGE_Y (w)
703 + WINDOW_HEADER_LINE_HEIGHT (w)),
704 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
705 - WINDOW_RIGHT_MARGIN_WIDTH (w)
706 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
707 make_number (WINDOW_BOTTOM_EDGE_Y (w)
708 - WINDOW_MODE_LINE_HEIGHT (w)));
711 /* Test if the character at column *X, row *Y is within window W.
712 If it is not, return ON_NOTHING;
713 if it is in the window's text area,
714 set *x and *y to its location relative to the upper left corner
715 of the window, and
716 return ON_TEXT;
717 if it is on the window's modeline, return ON_MODE_LINE;
718 if it is on the border between the window and its right sibling,
719 return ON_VERTICAL_BORDER.
720 if it is on a scroll bar,
721 return ON_SCROLL_BAR.
722 if it is on the window's top line, return ON_HEADER_LINE;
723 if it is in left or right fringe of the window,
724 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
725 to window-relative coordinates;
726 if it is in the marginal area to the left/right of the window,
727 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
728 to window-relative coordinates.
730 X and Y are frame relative pixel coordinates. */
732 static enum window_part
733 coordinates_in_window (w, x, y)
734 register struct window *w;
735 register int *x, *y;
737 struct frame *f = XFRAME (WINDOW_FRAME (w));
738 int left_x, right_x, top_y, bottom_y;
739 enum window_part part;
740 int ux = FRAME_COLUMN_WIDTH (f);
741 int x0 = WINDOW_LEFT_EDGE_X (w);
742 int x1 = WINDOW_RIGHT_EDGE_X (w);
743 /* The width of the area where the vertical line can be dragged.
744 (Between mode lines for instance. */
745 int grabbable_width = ux;
746 int lmargin_width, rmargin_width, text_left, text_right;
748 /* In what's below, we subtract 1 when computing right_x because we
749 want the rightmost pixel, which is given by left_pixel+width-1. */
750 if (w->pseudo_window_p)
752 left_x = 0;
753 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
754 top_y = WINDOW_TOP_EDGE_Y (w);
755 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
757 else
759 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
760 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
761 top_y = WINDOW_TOP_EDGE_Y (w);
762 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
765 /* Outside any interesting row? */
766 if (*y < top_y || *y >= bottom_y)
767 return ON_NOTHING;
769 /* On the mode line or header line? If it's near the start of
770 the mode or header line of window that's has a horizontal
771 sibling, say it's on the vertical line. That's to be able
772 to resize windows horizontally in case we're using toolkit
773 scroll bars. */
775 if (WINDOW_WANTS_MODELINE_P (w)
776 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
778 part = ON_MODE_LINE;
780 header_vertical_border_check:
781 /* We're somewhere on the mode line. We consider the place
782 between mode lines of horizontally adjacent mode lines
783 as the vertical border. If scroll bars on the left,
784 return the right window. */
785 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
786 || WINDOW_RIGHTMOST_P (w))
788 if (!WINDOW_LEFTMOST_P (w) && eabs (*x - x0) < grabbable_width)
790 /* Convert X and Y to window relative coordinates.
791 Vertical border is at the left edge of window. */
792 *x = max (0, *x - x0);
793 *y -= top_y;
794 return ON_VERTICAL_BORDER;
797 else
799 /* Make sure we're not at the rightmost position of a
800 mode-/header-line and there's yet another window on
801 the right. (Bug#1372) */
802 if ((WINDOW_RIGHTMOST_P (w) || *x < x1)
803 && eabs (*x - x1) < grabbable_width)
805 /* Convert X and Y to window relative coordinates.
806 Vertical border is at the right edge of window. */
807 *x = min (x1, *x) - x0;
808 *y -= top_y;
809 return ON_VERTICAL_BORDER;
813 if (*x < x0 || *x >= x1)
814 return ON_NOTHING;
816 /* Convert X and Y to window relative coordinates.
817 Mode line starts at left edge of window. */
818 *x -= x0;
819 *y -= top_y;
820 return part;
823 if (WINDOW_WANTS_HEADER_LINE_P (w)
824 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
826 part = ON_HEADER_LINE;
827 goto header_vertical_border_check;
830 if (*x < x0 || *x >= x1)
831 return ON_NOTHING;
833 /* Outside any interesting column? */
834 if (*x < left_x || *x > right_x)
836 *y -= top_y;
837 return ON_SCROLL_BAR;
840 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
841 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
843 text_left = window_box_left (w, TEXT_AREA);
844 text_right = text_left + window_box_width (w, TEXT_AREA);
846 if (FRAME_WINDOW_P (f))
848 if (!w->pseudo_window_p
849 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
850 && !WINDOW_RIGHTMOST_P (w)
851 && (eabs (*x - right_x) < grabbable_width))
853 /* Convert X and Y to window relative coordinates.
854 Vertical border is at the right edge of window. */
855 *x = min (right_x, *x) - left_x;
856 *y -= top_y;
857 return ON_VERTICAL_BORDER;
860 else
862 /* Need to say "*x > right_x" rather than >=, since on character
863 terminals, the vertical line's x coordinate is right_x. */
864 if (!w->pseudo_window_p
865 && !WINDOW_RIGHTMOST_P (w)
866 && *x > right_x - ux)
868 /* On the border on the right side of the window? Assume that
869 this area begins at RIGHT_X minus a canonical char width. */
870 *x = min (right_x, *x) - left_x;
871 *y -= top_y;
872 return ON_VERTICAL_BORDER;
876 if (*x < text_left)
878 if (lmargin_width > 0
879 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
880 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
881 : (*x < left_x + lmargin_width)))
883 *x -= left_x;
884 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
885 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
886 *y -= top_y;
887 return ON_LEFT_MARGIN;
890 /* Convert X and Y to window-relative pixel coordinates. */
891 *x -= left_x;
892 *y -= top_y;
893 return ON_LEFT_FRINGE;
896 if (*x >= text_right)
898 if (rmargin_width > 0
899 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
900 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
901 : (*x >= right_x - rmargin_width)))
903 *x -= right_x - rmargin_width;
904 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
905 *x += WINDOW_RIGHT_FRINGE_WIDTH (w);
906 *y -= top_y;
907 return ON_RIGHT_MARGIN;
910 /* Convert X and Y to window-relative pixel coordinates. */
911 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
912 *y -= top_y;
913 return ON_RIGHT_FRINGE;
916 /* Everything special ruled out - must be on text area */
917 *x -= text_left;
918 *y -= top_y;
919 return ON_TEXT;
923 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
924 Scoordinates_in_window_p, 2, 2, 0,
925 doc: /* Return non-nil if COORDINATES are in WINDOW.
926 COORDINATES is a cons of the form (X . Y), X and Y being distances
927 measured in characters from the upper-left corner of the frame.
928 \(0 . 0) denotes the character in the upper left corner of the
929 frame.
930 If COORDINATES are in the text portion of WINDOW,
931 the coordinates relative to the window are returned.
932 If they are in the mode line of WINDOW, `mode-line' is returned.
933 If they are in the top mode line of WINDOW, `header-line' is returned.
934 If they are in the left fringe of WINDOW, `left-fringe' is returned.
935 If they are in the right fringe of WINDOW, `right-fringe' is returned.
936 If they are on the border between WINDOW and its right sibling,
937 `vertical-line' is returned.
938 If they are in the windows's left or right marginal areas, `left-margin'\n\
939 or `right-margin' is returned. */)
940 (coordinates, window)
941 register Lisp_Object coordinates, window;
943 struct window *w;
944 struct frame *f;
945 int x, y;
946 Lisp_Object lx, ly;
948 CHECK_WINDOW (window);
949 w = XWINDOW (window);
950 f = XFRAME (w->frame);
951 CHECK_CONS (coordinates);
952 lx = Fcar (coordinates);
953 ly = Fcdr (coordinates);
954 CHECK_NUMBER_OR_FLOAT (lx);
955 CHECK_NUMBER_OR_FLOAT (ly);
956 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
957 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
959 switch (coordinates_in_window (w, &x, &y))
961 case ON_NOTHING:
962 return Qnil;
964 case ON_TEXT:
965 /* X and Y are now window relative pixel coordinates. Convert
966 them to canonical char units before returning them. */
967 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
968 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
970 case ON_MODE_LINE:
971 return Qmode_line;
973 case ON_VERTICAL_BORDER:
974 return Qvertical_line;
976 case ON_HEADER_LINE:
977 return Qheader_line;
979 case ON_LEFT_FRINGE:
980 return Qleft_fringe;
982 case ON_RIGHT_FRINGE:
983 return Qright_fringe;
985 case ON_LEFT_MARGIN:
986 return Qleft_margin;
988 case ON_RIGHT_MARGIN:
989 return Qright_margin;
991 case ON_SCROLL_BAR:
992 /* Historically we are supposed to return nil in this case. */
993 return Qnil;
995 default:
996 abort ();
1001 /* Callback for foreach_window, used in window_from_coordinates.
1002 Check if window W contains coordinates specified by USER_DATA which
1003 is actually a pointer to a struct check_window_data CW.
1005 Check if window W contains coordinates *CW->x and *CW->y. If it
1006 does, return W in *CW->window, as Lisp_Object, and return in
1007 *CW->part the part of the window under coordinates *X,*Y. Return
1008 zero from this function to stop iterating over windows. */
1010 struct check_window_data
1012 Lisp_Object *window;
1013 int *x, *y;
1014 enum window_part *part;
1017 static int
1018 check_window_containing (w, user_data)
1019 struct window *w;
1020 void *user_data;
1022 struct check_window_data *cw = (struct check_window_data *) user_data;
1023 enum window_part found;
1024 int continue_p = 1;
1026 found = coordinates_in_window (w, cw->x, cw->y);
1027 if (found != ON_NOTHING)
1029 *cw->part = found;
1030 XSETWINDOW (*cw->window, w);
1031 continue_p = 0;
1034 return continue_p;
1038 /* Find the window containing frame-relative pixel position X/Y and
1039 return it as a Lisp_Object.
1041 If X, Y is on one of the window's special `window_part' elements,
1042 set *PART to the id of that element, and return X and Y converted
1043 to window relative coordinates in WX and WY.
1045 If there is no window under X, Y return nil and leave *PART
1046 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1048 This function was previously implemented with a loop cycling over
1049 windows with Fnext_window, and starting with the frame's selected
1050 window. It turned out that this doesn't work with an
1051 implementation of next_window using Vwindow_list, because
1052 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1053 tree of F when this function is called asynchronously from
1054 note_mouse_highlight. The original loop didn't terminate in this
1055 case. */
1057 Lisp_Object
1058 window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
1059 struct frame *f;
1060 int x, y;
1061 enum window_part *part;
1062 int *wx, *wy;
1063 int tool_bar_p;
1065 Lisp_Object window;
1066 struct check_window_data cw;
1067 enum window_part dummy;
1069 if (part == 0)
1070 part = &dummy;
1072 window = Qnil;
1073 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
1074 foreach_window (f, check_window_containing, &cw);
1076 /* If not found above, see if it's in the tool bar window, if a tool
1077 bar exists. */
1078 if (NILP (window)
1079 && tool_bar_p
1080 && WINDOWP (f->tool_bar_window)
1081 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1082 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
1083 != ON_NOTHING))
1085 *part = ON_TEXT;
1086 window = f->tool_bar_window;
1089 if (wx) *wx = x;
1090 if (wy) *wy = y;
1092 return window;
1095 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1096 doc: /* Return window containing coordinates X and Y on FRAME.
1097 If omitted, FRAME defaults to the currently selected frame.
1098 The top left corner of the frame is considered to be row 0,
1099 column 0. */)
1100 (x, y, frame)
1101 Lisp_Object x, y, frame;
1103 struct frame *f;
1105 if (NILP (frame))
1106 frame = selected_frame;
1107 CHECK_LIVE_FRAME (frame);
1108 f = XFRAME (frame);
1110 /* Check that arguments are integers or floats. */
1111 CHECK_NUMBER_OR_FLOAT (x);
1112 CHECK_NUMBER_OR_FLOAT (y);
1114 return window_from_coordinates (f,
1115 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1116 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1117 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1118 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1119 0, 0, 0, 0);
1122 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1123 doc: /* Return current value of point in WINDOW.
1124 WINDOW defaults to the selected window.
1126 For a nonselected window, this is the value point would have
1127 if that window were selected.
1129 Note that, when WINDOW is the selected window and its buffer
1130 is also currently selected, the value returned is the same as (point).
1131 It would be more strictly correct to return the `top-level' value
1132 of point, outside of any save-excursion forms.
1133 But that is hard to define. */)
1134 (window)
1135 Lisp_Object window;
1137 register struct window *w = decode_window (window);
1139 if (w == XWINDOW (selected_window)
1140 && current_buffer == XBUFFER (w->buffer))
1141 return Fpoint ();
1142 return Fmarker_position (w->pointm);
1145 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1146 doc: /* Return position at which display currently starts in WINDOW.
1147 WINDOW defaults to the selected window.
1148 This is updated by redisplay or by calling `set-window-start'. */)
1149 (window)
1150 Lisp_Object window;
1152 return Fmarker_position (decode_window (window)->start);
1155 /* This is text temporarily removed from the doc string below.
1157 This function returns nil if the position is not currently known.
1158 That happens when redisplay is preempted and doesn't finish.
1159 If in that case you want to compute where the end of the window would
1160 have been if redisplay had finished, do this:
1161 (save-excursion
1162 (goto-char (window-start window))
1163 (vertical-motion (1- (window-height window)) window)
1164 (point))") */
1166 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1167 doc: /* Return position at which display currently ends in WINDOW.
1168 WINDOW defaults to the selected window.
1169 This is updated by redisplay, when it runs to completion.
1170 Simply changing the buffer text or setting `window-start'
1171 does not update this value.
1172 Return nil if there is no recorded value. \(This can happen if the
1173 last redisplay of WINDOW was preempted, and did not finish.)
1174 If UPDATE is non-nil, compute the up-to-date position
1175 if it isn't already recorded. */)
1176 (window, update)
1177 Lisp_Object window, update;
1179 Lisp_Object value;
1180 struct window *w = decode_window (window);
1181 Lisp_Object buf;
1182 struct buffer *b;
1184 buf = w->buffer;
1185 CHECK_BUFFER (buf);
1186 b = XBUFFER (buf);
1188 #if 0 /* This change broke some things. We should make it later. */
1189 /* If we don't know the end position, return nil.
1190 The user can compute it with vertical-motion if he wants to.
1191 It would be nicer to do it automatically,
1192 but that's so slow that it would probably bother people. */
1193 if (NILP (w->window_end_valid))
1194 return Qnil;
1195 #endif
1197 if (! NILP (update)
1198 && ! (! NILP (w->window_end_valid)
1199 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1200 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1201 && !noninteractive)
1203 struct text_pos startp;
1204 struct it it;
1205 struct buffer *old_buffer = NULL;
1207 /* Cannot use Fvertical_motion because that function doesn't
1208 cope with variable-height lines. */
1209 if (b != current_buffer)
1211 old_buffer = current_buffer;
1212 set_buffer_internal (b);
1215 /* In case W->start is out of the range, use something
1216 reasonable. This situation occurred when loading a file with
1217 `-l' containing a call to `rmail' with subsequent other
1218 commands. At the end, W->start happened to be BEG, while
1219 rmail had already narrowed the buffer. */
1220 if (XMARKER (w->start)->charpos < BEGV)
1221 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1222 else if (XMARKER (w->start)->charpos > ZV)
1223 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1224 else
1225 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1227 start_display (&it, w, startp);
1228 move_it_vertically (&it, window_box_height (w));
1229 if (it.current_y < it.last_visible_y)
1230 move_it_past_eol (&it);
1231 value = make_number (IT_CHARPOS (it));
1233 if (old_buffer)
1234 set_buffer_internal (old_buffer);
1236 else
1237 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1239 return value;
1242 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1243 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1244 Return POS. */)
1245 (window, pos)
1246 Lisp_Object window, pos;
1248 register struct window *w = decode_window (window);
1250 CHECK_NUMBER_COERCE_MARKER (pos);
1251 if (w == XWINDOW (selected_window)
1252 && XBUFFER (w->buffer) == current_buffer)
1253 Fgoto_char (pos);
1254 else
1255 set_marker_restricted (w->pointm, pos, w->buffer);
1257 /* We have to make sure that redisplay updates the window to show
1258 the new value of point. */
1259 if (!EQ (window, selected_window))
1260 ++windows_or_buffers_changed;
1262 return pos;
1265 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1266 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1267 WINDOW defaults to the selected window. Return POS.
1268 Optional third arg NOFORCE non-nil inhibits next redisplay from
1269 overriding motion of point in order to display at this exact start. */)
1270 (window, pos, noforce)
1271 Lisp_Object window, pos, noforce;
1273 register struct window *w = decode_window (window);
1275 CHECK_NUMBER_COERCE_MARKER (pos);
1276 set_marker_restricted (w->start, pos, w->buffer);
1277 /* this is not right, but much easier than doing what is right. */
1278 w->start_at_line_beg = Qnil;
1279 if (NILP (noforce))
1280 w->force_start = Qt;
1281 w->update_mode_line = Qt;
1282 XSETFASTINT (w->last_modified, 0);
1283 XSETFASTINT (w->last_overlay_modified, 0);
1284 if (!EQ (window, selected_window))
1285 windows_or_buffers_changed++;
1287 return pos;
1291 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1292 0, 1, 0,
1293 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1294 More precisely, return the value assigned by the last call of
1295 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1296 never called with WINDOW as its argument, or the value set by that
1297 function was internally reset since its last call. WINDOW defaults to
1298 the selected window.
1300 When a window is dedicated to its buffer, `display-buffer' will refrain
1301 from displaying another buffer in it. `get-lru-window' and
1302 `get-largest-window' treat dedicated windows specially.
1303 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1304 `kill-buffer' can delete a dedicated window and the containing frame.
1306 Functions like `set-window-buffer' may change the buffer displayed by a
1307 window, unless that window is "strongly" dedicated to its buffer, that
1308 is the value returned by `window-dedicated-p' is t. */)
1309 (window)
1310 Lisp_Object window;
1312 return decode_window (window)->dedicated;
1315 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1316 Sset_window_dedicated_p, 2, 2, 0,
1317 doc: /* Mark WINDOW as dedicated according to FLAG.
1318 WINDOW defaults to the selected window. FLAG non-nil means mark WINDOW
1319 as dedicated to its buffer. FLAG nil means mark WINDOW as non-dedicated.
1320 Return FLAG.
1322 When a window is dedicated to its buffer, `display-buffer' will refrain
1323 from displaying another buffer in it. `get-lru-window' and
1324 `get-largest-window' treat dedicated windows specially.
1325 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1326 `kill-buffer' can delete a dedicated window and the containing
1327 frame.
1329 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1330 its buffer. Functions like `set-window-buffer' may change the buffer
1331 displayed by a window, unless that window is strongly dedicated to its
1332 buffer. If and when `set-window-buffer' displays another buffer in a
1333 window, it also makes sure that the window is not marked as dedicated. */)
1334 (window, flag)
1335 Lisp_Object window, flag;
1337 register struct window *w = decode_window (window);
1339 w->dedicated = flag;
1340 return w->dedicated;
1344 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1345 0, 1, 0,
1346 doc: /* Return the parameters of WINDOW and their values.
1347 WINDOW defaults to the selected window. The return value is a list of
1348 elements of the form (PARAMETER . VALUE). */)
1349 (window)
1350 Lisp_Object window;
1352 return Fcopy_alist (decode_window (window)->window_parameters);
1355 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1356 2, 2, 0,
1357 doc: /* Return WINDOW's value for PARAMETER.
1358 WINDOW defaults to the selected window. */)
1359 (window, parameter)
1360 Lisp_Object window, parameter;
1362 Lisp_Object result;
1364 result = Fassq (parameter, decode_window (window)->window_parameters);
1365 return CDR_SAFE (result);
1368 DEFUN ("set-window-parameter", Fset_window_parameter,
1369 Sset_window_parameter, 3, 3, 0,
1370 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1371 WINDOW defaults to the selected window. Return VALUE. */)
1372 (window, parameter, value)
1373 Lisp_Object window, parameter, value;
1375 register struct window *w = decode_window (window);
1376 Lisp_Object old_alist_elt;
1378 old_alist_elt = Fassq (parameter, w->window_parameters);
1379 if (NILP (old_alist_elt))
1380 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1381 else
1382 Fsetcdr (old_alist_elt, value);
1383 return value;
1387 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1388 0, 1, 0,
1389 doc: /* Return the display-table that WINDOW is using.
1390 WINDOW defaults to the selected window. */)
1391 (window)
1392 Lisp_Object window;
1394 return decode_window (window)->display_table;
1397 /* Get the display table for use on window W. This is either W's
1398 display table or W's buffer's display table. Ignore the specified
1399 tables if they are not valid; if no valid table is specified,
1400 return 0. */
1402 struct Lisp_Char_Table *
1403 window_display_table (w)
1404 struct window *w;
1406 struct Lisp_Char_Table *dp = NULL;
1408 if (DISP_TABLE_P (w->display_table))
1409 dp = XCHAR_TABLE (w->display_table);
1410 else if (BUFFERP (w->buffer))
1412 struct buffer *b = XBUFFER (w->buffer);
1414 if (DISP_TABLE_P (b->display_table))
1415 dp = XCHAR_TABLE (b->display_table);
1416 else if (DISP_TABLE_P (Vstandard_display_table))
1417 dp = XCHAR_TABLE (Vstandard_display_table);
1420 return dp;
1423 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1424 doc: /* Set WINDOW's display-table to TABLE. */)
1425 (window, table)
1426 register Lisp_Object window, table;
1428 register struct window *w;
1430 w = decode_window (window);
1431 w->display_table = table;
1432 return table;
1435 /* Record info on buffer window w is displaying
1436 when it is about to cease to display that buffer. */
1437 static void
1438 unshow_buffer (w)
1439 register struct window *w;
1441 Lisp_Object buf;
1442 struct buffer *b;
1444 buf = w->buffer;
1445 b = XBUFFER (buf);
1446 if (b != XMARKER (w->pointm)->buffer)
1447 abort ();
1449 #if 0
1450 if (w == XWINDOW (selected_window)
1451 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1452 /* Do this except when the selected window's buffer
1453 is being removed from some other window. */
1454 #endif
1455 /* last_window_start records the start position that this buffer
1456 had in the last window to be disconnected from it.
1457 Now that this statement is unconditional,
1458 it is possible for the buffer to be displayed in the
1459 selected window, while last_window_start reflects another
1460 window which was recently showing the same buffer.
1461 Some people might say that might be a good thing. Let's see. */
1462 b->last_window_start = marker_position (w->start);
1464 /* Point in the selected window's buffer
1465 is actually stored in that buffer, and the window's pointm isn't used.
1466 So don't clobber point in that buffer. */
1467 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1468 /* This line helps to fix Horsley's testbug.el bug. */
1469 && !(WINDOWP (b->last_selected_window)
1470 && w != XWINDOW (b->last_selected_window)
1471 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1472 temp_set_point_both (b,
1473 clip_to_bounds (BUF_BEGV (b),
1474 XMARKER (w->pointm)->charpos,
1475 BUF_ZV (b)),
1476 clip_to_bounds (BUF_BEGV_BYTE (b),
1477 marker_byte_position (w->pointm),
1478 BUF_ZV_BYTE (b)));
1480 if (WINDOWP (b->last_selected_window)
1481 && w == XWINDOW (b->last_selected_window))
1482 b->last_selected_window = Qnil;
1485 /* Put replacement into the window structure in place of old. */
1486 static void
1487 replace_window (old, replacement)
1488 Lisp_Object old, replacement;
1490 register Lisp_Object tem;
1491 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1493 /* If OLD is its frame's root_window, then replacement is the new
1494 root_window for that frame. */
1496 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1497 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1499 p->left_col = o->left_col;
1500 p->top_line = o->top_line;
1501 p->total_cols = o->total_cols;
1502 p->total_lines = o->total_lines;
1503 p->desired_matrix = p->current_matrix = 0;
1504 p->vscroll = 0;
1505 bzero (&p->cursor, sizeof (p->cursor));
1506 bzero (&p->last_cursor, sizeof (p->last_cursor));
1507 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1508 p->phys_cursor_type = -1;
1509 p->phys_cursor_width = -1;
1510 p->must_be_updated_p = 0;
1511 p->pseudo_window_p = 0;
1512 XSETFASTINT (p->window_end_vpos, 0);
1513 XSETFASTINT (p->window_end_pos, 0);
1514 p->window_end_valid = Qnil;
1515 p->frozen_window_start_p = 0;
1516 p->orig_top_line = p->orig_total_lines = Qnil;
1518 p->next = tem = o->next;
1519 if (!NILP (tem))
1520 XWINDOW (tem)->prev = replacement;
1522 p->prev = tem = o->prev;
1523 if (!NILP (tem))
1524 XWINDOW (tem)->next = replacement;
1526 p->parent = tem = o->parent;
1527 if (!NILP (tem))
1529 if (EQ (XWINDOW (tem)->vchild, old))
1530 XWINDOW (tem)->vchild = replacement;
1531 if (EQ (XWINDOW (tem)->hchild, old))
1532 XWINDOW (tem)->hchild = replacement;
1535 /*** Here, if replacement is a vertical combination
1536 and so is its new parent, we should make replacement's
1537 children be children of that parent instead. ***/
1540 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1541 doc: /* Remove WINDOW from its frame.
1542 WINDOW defaults to the selected window. Return nil.
1543 Signal an error when WINDOW is the only window on its frame. */)
1544 (window)
1545 register Lisp_Object window;
1547 struct frame *f;
1548 if (NILP (window))
1549 window = selected_window;
1550 else
1551 CHECK_LIVE_WINDOW (window);
1553 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1554 delete_window (window);
1556 run_window_configuration_change_hook (f);
1558 return Qnil;
1561 void
1562 delete_window (window)
1563 register Lisp_Object window;
1565 register Lisp_Object tem, parent, sib;
1566 register struct window *p;
1567 register struct window *par;
1568 struct frame *f;
1570 /* Because this function is called by other C code on non-leaf
1571 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1572 so we can't decode_window here. */
1573 CHECK_WINDOW (window);
1574 p = XWINDOW (window);
1576 /* It's a no-op to delete an already-deleted window. */
1577 if (NILP (p->buffer)
1578 && NILP (p->hchild)
1579 && NILP (p->vchild))
1580 return;
1582 parent = p->parent;
1583 if (NILP (parent))
1584 error ("Attempt to delete minibuffer or sole ordinary window");
1585 par = XWINDOW (parent);
1587 windows_or_buffers_changed++;
1588 Vwindow_list = Qnil;
1589 f = XFRAME (WINDOW_FRAME (p));
1590 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1592 /* Are we trying to delete any frame's selected window? */
1594 Lisp_Object swindow, pwindow;
1596 /* See if the frame's selected window is either WINDOW
1597 or any subwindow of it, by finding all that window's parents
1598 and comparing each one with WINDOW. */
1599 swindow = FRAME_SELECTED_WINDOW (f);
1601 while (1)
1603 pwindow = swindow;
1604 while (!NILP (pwindow))
1606 if (EQ (window, pwindow))
1607 break;
1608 pwindow = XWINDOW (pwindow)->parent;
1611 /* If the window being deleted is not a parent of SWINDOW,
1612 then SWINDOW is ok as the new selected window. */
1613 if (!EQ (window, pwindow))
1614 break;
1615 /* Otherwise, try another window for SWINDOW. */
1616 swindow = Fnext_window (swindow, Qlambda, Qnil);
1618 /* If we get back to the frame's selected window,
1619 it means there was no acceptable alternative,
1620 so we cannot delete. */
1621 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1622 error ("Cannot delete window");
1625 /* If we need to change SWINDOW, do it. */
1626 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1628 /* If we're about to delete the selected window on the
1629 selected frame, then we should use Fselect_window to select
1630 the new window. On the other hand, if we're about to
1631 delete the selected window on any other frame, we shouldn't do
1632 anything but set the frame's selected_window slot. */
1633 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1634 Fselect_window (swindow, Qnil);
1635 else
1636 FRAME_SELECTED_WINDOW (f) = swindow;
1640 /* Now we know we can delete this one. */
1641 window_deletion_count++;
1643 tem = p->buffer;
1644 /* tem is null for dummy parent windows
1645 (which have inferiors but not any contents themselves) */
1646 if (!NILP (tem))
1648 unshow_buffer (p);
1649 unchain_marker (XMARKER (p->pointm));
1650 unchain_marker (XMARKER (p->start));
1653 /* Free window glyph matrices. It is sure that they are allocated
1654 again when ADJUST_GLYPHS is called. Block input so that expose
1655 events and other events that access glyph matrices are not
1656 processed while we are changing them. */
1657 BLOCK_INPUT;
1658 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1660 tem = p->next;
1661 if (!NILP (tem))
1662 XWINDOW (tem)->prev = p->prev;
1664 tem = p->prev;
1665 if (!NILP (tem))
1666 XWINDOW (tem)->next = p->next;
1668 if (EQ (window, par->hchild))
1669 par->hchild = p->next;
1670 if (EQ (window, par->vchild))
1671 par->vchild = p->next;
1673 /* Find one of our siblings to give our space to. */
1674 sib = p->prev;
1675 if (NILP (sib))
1677 /* If p gives its space to its next sibling, that sibling needs
1678 to have its top/left side pulled back to where p's is.
1679 set_window_{height,width} will re-position the sibling's
1680 children. */
1681 sib = p->next;
1682 XWINDOW (sib)->top_line = p->top_line;
1683 XWINDOW (sib)->left_col = p->left_col;
1686 /* Stretch that sibling. */
1687 if (!NILP (par->vchild))
1688 set_window_height (sib,
1689 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1691 if (!NILP (par->hchild))
1692 set_window_width (sib,
1693 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1696 /* If parent now has only one child,
1697 put the child into the parent's place. */
1698 tem = par->hchild;
1699 if (NILP (tem))
1700 tem = par->vchild;
1701 if (NILP (XWINDOW (tem)->next)) {
1702 replace_window (parent, tem);
1703 par = XWINDOW (tem);
1706 /* Since we may be deleting combination windows, we must make sure that
1707 not only p but all its children have been marked as deleted. */
1708 if (! NILP (p->hchild))
1709 delete_all_subwindows (XWINDOW (p->hchild));
1710 else if (! NILP (p->vchild))
1711 delete_all_subwindows (XWINDOW (p->vchild));
1713 /* Mark this window as deleted. */
1714 p->buffer = p->hchild = p->vchild = Qnil;
1716 if (! NILP (par->parent))
1717 par = XWINDOW (par->parent);
1719 /* Check if we have a v/hchild with a v/hchild. In that case remove
1720 one of them. */
1722 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1724 p = XWINDOW (par->vchild);
1725 par->vchild = p->vchild;
1726 tem = p->vchild;
1728 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1730 p = XWINDOW (par->hchild);
1731 par->hchild = p->hchild;
1732 tem = p->hchild;
1734 else
1735 p = 0;
1737 if (p)
1739 while (! NILP (tem)) {
1740 XWINDOW (tem)->parent = p->parent;
1741 if (NILP (XWINDOW (tem)->next))
1742 break;
1743 tem = XWINDOW (tem)->next;
1745 if (! NILP (tem)) {
1746 /* The next of the v/hchild we are removing is now the next of the
1747 last child for the v/hchild:
1748 Before v/hchild -> v/hchild -> next1 -> next2
1750 -> next3
1751 After: v/hchild -> next1 -> next2 -> next3
1753 XWINDOW (tem)->next = p->next;
1754 if (! NILP (p->next))
1755 XWINDOW (p->next)->prev = tem;
1757 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1761 /* Adjust glyph matrices. */
1762 adjust_glyphs (f);
1763 UNBLOCK_INPUT;
1768 /***********************************************************************
1769 Window List
1770 ***********************************************************************/
1772 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1773 pointer. This is a callback function for foreach_window, used in
1774 function window_list. */
1776 static int
1777 add_window_to_list (w, user_data)
1778 struct window *w;
1779 void *user_data;
1781 Lisp_Object *list = (Lisp_Object *) user_data;
1782 Lisp_Object window;
1783 XSETWINDOW (window, w);
1784 *list = Fcons (window, *list);
1785 return 1;
1789 /* Return a list of all windows, for use by next_window. If
1790 Vwindow_list is a list, return that list. Otherwise, build a new
1791 list, cache it in Vwindow_list, and return that. */
1793 static Lisp_Object
1794 window_list ()
1796 if (!CONSP (Vwindow_list))
1798 Lisp_Object tail;
1800 Vwindow_list = Qnil;
1801 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1803 Lisp_Object args[2];
1805 /* We are visiting windows in canonical order, and add
1806 new windows at the front of args[1], which means we
1807 have to reverse this list at the end. */
1808 args[1] = Qnil;
1809 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1810 args[0] = Vwindow_list;
1811 args[1] = Fnreverse (args[1]);
1812 Vwindow_list = Fnconc (2, args);
1816 return Vwindow_list;
1820 /* Value is non-zero if WINDOW satisfies the constraints given by
1821 OWINDOW, MINIBUF and ALL_FRAMES.
1823 MINIBUF t means WINDOW may be minibuffer windows.
1824 `lambda' means WINDOW may not be a minibuffer window.
1825 a window means a specific minibuffer window
1827 ALL_FRAMES t means search all frames,
1828 nil means search just current frame,
1829 `visible' means search just visible frames,
1830 0 means search visible and iconified frames,
1831 a window means search the frame that window belongs to,
1832 a frame means consider windows on that frame, only. */
1834 static int
1835 candidate_window_p (window, owindow, minibuf, all_frames)
1836 Lisp_Object window, owindow, minibuf, all_frames;
1838 struct window *w = XWINDOW (window);
1839 struct frame *f = XFRAME (w->frame);
1840 int candidate_p = 1;
1842 if (!BUFFERP (w->buffer))
1843 candidate_p = 0;
1844 else if (MINI_WINDOW_P (w)
1845 && (EQ (minibuf, Qlambda)
1846 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1848 /* If MINIBUF is `lambda' don't consider any mini-windows.
1849 If it is a window, consider only that one. */
1850 candidate_p = 0;
1852 else if (EQ (all_frames, Qt))
1853 candidate_p = 1;
1854 else if (NILP (all_frames))
1856 xassert (WINDOWP (owindow));
1857 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1859 else if (EQ (all_frames, Qvisible))
1861 FRAME_SAMPLE_VISIBILITY (f);
1862 candidate_p = FRAME_VISIBLE_P (f)
1863 && (FRAME_TERMINAL (XFRAME (w->frame))
1864 == FRAME_TERMINAL (XFRAME (selected_frame)));
1867 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1869 FRAME_SAMPLE_VISIBILITY (f);
1870 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
1871 #ifdef HAVE_X_WINDOWS
1872 /* Yuck!! If we've just created the frame and the
1873 window-manager requested the user to place it
1874 manually, the window may still not be considered
1875 `visible'. I'd argue it should be at least
1876 something like `iconified', but don't know how to do
1877 that yet. --Stef */
1878 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
1879 && !f->output_data.x->has_been_visible)
1880 #endif
1882 && (FRAME_TERMINAL (XFRAME (w->frame))
1883 == FRAME_TERMINAL (XFRAME (selected_frame)));
1885 else if (WINDOWP (all_frames))
1886 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1887 || EQ (XWINDOW (all_frames)->frame, w->frame)
1888 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1889 else if (FRAMEP (all_frames))
1890 candidate_p = EQ (all_frames, w->frame);
1892 return candidate_p;
1896 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1897 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1898 ALL_FRAMES. */
1900 static void
1901 decode_next_window_args (window, minibuf, all_frames)
1902 Lisp_Object *window, *minibuf, *all_frames;
1904 if (NILP (*window))
1905 *window = selected_window;
1906 else
1907 CHECK_LIVE_WINDOW (*window);
1909 /* MINIBUF nil may or may not include minibuffers. Decide if it
1910 does. */
1911 if (NILP (*minibuf))
1912 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1913 else if (!EQ (*minibuf, Qt))
1914 *minibuf = Qlambda;
1916 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1917 => count none of them, or a specific minibuffer window (the
1918 active one) to count. */
1920 /* ALL_FRAMES nil doesn't specify which frames to include. */
1921 if (NILP (*all_frames))
1922 *all_frames = (!EQ (*minibuf, Qlambda)
1923 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1924 : Qnil);
1925 else if (EQ (*all_frames, Qvisible))
1927 else if (EQ (*all_frames, make_number (0)))
1929 else if (FRAMEP (*all_frames))
1931 else if (!EQ (*all_frames, Qt))
1932 *all_frames = Qnil;
1934 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1935 search just current frame, `visible' meaning search just visible
1936 frames, 0 meaning search visible and iconified frames, or a
1937 window, meaning search the frame that window belongs to, or a
1938 frame, meaning consider windows on that frame, only. */
1942 /* Return the next or previous window of WINDOW in cyclic ordering
1943 of windows. NEXT_P non-zero means return the next window. See the
1944 documentation string of next-window for the meaning of MINIBUF and
1945 ALL_FRAMES. */
1947 static Lisp_Object
1948 next_window (window, minibuf, all_frames, next_p)
1949 Lisp_Object window, minibuf, all_frames;
1950 int next_p;
1952 decode_next_window_args (&window, &minibuf, &all_frames);
1954 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1955 return the first window on the frame. */
1956 if (FRAMEP (all_frames)
1957 && !EQ (all_frames, XWINDOW (window)->frame))
1958 return Fframe_first_window (all_frames);
1960 if (next_p)
1962 Lisp_Object list;
1964 /* Find WINDOW in the list of all windows. */
1965 list = Fmemq (window, window_list ());
1967 /* Scan forward from WINDOW to the end of the window list. */
1968 if (CONSP (list))
1969 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1970 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1971 break;
1973 /* Scan from the start of the window list up to WINDOW. */
1974 if (!CONSP (list))
1975 for (list = Vwindow_list;
1976 CONSP (list) && !EQ (XCAR (list), window);
1977 list = XCDR (list))
1978 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1979 break;
1981 if (CONSP (list))
1982 window = XCAR (list);
1984 else
1986 Lisp_Object candidate, list;
1988 /* Scan through the list of windows for candidates. If there are
1989 candidate windows in front of WINDOW, the last one of these
1990 is the one we want. If there are candidates following WINDOW
1991 in the list, again the last one of these is the one we want. */
1992 candidate = Qnil;
1993 for (list = window_list (); CONSP (list); list = XCDR (list))
1995 if (EQ (XCAR (list), window))
1997 if (WINDOWP (candidate))
1998 break;
2000 else if (candidate_window_p (XCAR (list), window, minibuf,
2001 all_frames))
2002 candidate = XCAR (list);
2005 if (WINDOWP (candidate))
2006 window = candidate;
2009 return window;
2013 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2014 doc: /* Return window following WINDOW in cyclic ordering of windows.
2015 WINDOW defaults to the selected window. The optional arguments
2016 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2018 MINIBUF t means consider the minibuffer window even if the
2019 minibuffer is not active. MINIBUF nil or omitted means consider
2020 the minibuffer window only if the minibuffer is active. Any
2021 other value means do not consider the minibuffer window even if
2022 the minibuffer is active.
2024 Several frames may share a single minibuffer; if the minibuffer
2025 is active, all windows on all frames that share that minibuffer
2026 are considered too. Therefore, if you are using a separate
2027 minibuffer frame and the minibuffer is active and MINIBUF says it
2028 counts, `next-window' considers the windows in the frame from
2029 which you entered the minibuffer, as well as the minibuffer
2030 window.
2032 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
2033 frame, plus the minibuffer window if specified by the MINIBUF
2034 argument, see above. If the minibuffer counts, consider all
2035 windows on all frames that share that minibuffer too.
2036 ALL-FRAMES t means consider all windows on all existing frames.
2037 ALL-FRAMES `visible' means consider all windows on all visible
2038 frames.
2039 ALL-FRAMES 0 means consider all windows on all visible and
2040 iconified frames.
2041 ALL-FRAMES a frame means consider all windows on that frame only.
2042 Anything else means consider all windows on WINDOW's frame and no
2043 others.
2045 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2046 `next-window' to iterate through the entire cycle of acceptable
2047 windows, eventually ending up back at the window you started with.
2048 `previous-window' traverses the same cycle, in the reverse order. */)
2049 (window, minibuf, all_frames)
2050 Lisp_Object window, minibuf, all_frames;
2052 return next_window (window, minibuf, all_frames, 1);
2056 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2057 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
2058 WINDOW defaults to the selected window. The optional arguments
2059 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2060 For the precise meaning of these arguments see `next-window'.
2062 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2063 use `previous-window' to iterate through the entire cycle of
2064 acceptable windows, eventually ending up back at the window you
2065 started with. `next-window' traverses the same cycle, in the
2066 reverse order. */)
2067 (window, minibuf, all_frames)
2068 Lisp_Object window, minibuf, all_frames;
2070 return next_window (window, minibuf, all_frames, 0);
2074 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
2075 doc: /* Select another window in cyclic ordering of windows.
2076 COUNT specifies the number of windows to skip, starting with the
2077 selected window, before making the selection. If COUNT is
2078 positive, skip COUNT windows forwards. If COUNT is negative,
2079 skip -COUNT windows backwards. COUNT zero means do not skip any
2080 window, so select the selected window. In an interactive call,
2081 COUNT is the numeric prefix argument. Return nil.
2083 This function uses `next-window' for finding the window to select.
2084 The argument ALL-FRAMES has the same meaning as in `next-window',
2085 but the MINIBUF argument of `next-window' is always effectively
2086 nil. */)
2087 (count, all_frames)
2088 Lisp_Object count, all_frames;
2090 Lisp_Object window;
2091 int i;
2093 CHECK_NUMBER (count);
2094 window = selected_window;
2096 for (i = XINT (count); i > 0; --i)
2097 window = Fnext_window (window, Qnil, all_frames);
2098 for (; i < 0; ++i)
2099 window = Fprevious_window (window, Qnil, all_frames);
2101 Fselect_window (window, Qnil);
2102 return Qnil;
2106 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2107 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2108 FRAME nil or omitted means use the selected frame.
2109 WINDOW nil or omitted means use the selected window.
2110 MINIBUF t means include the minibuffer window, even if it isn't active.
2111 MINIBUF nil or omitted means include the minibuffer window only
2112 if it's active.
2113 MINIBUF neither nil nor t means never include the minibuffer window. */)
2114 (frame, minibuf, window)
2115 Lisp_Object frame, minibuf, window;
2117 if (NILP (window))
2118 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2119 CHECK_WINDOW (window);
2120 if (NILP (frame))
2121 frame = selected_frame;
2123 if (!EQ (frame, XWINDOW (window)->frame))
2124 error ("Window is on a different frame");
2126 return window_list_1 (window, minibuf, frame);
2130 /* Return a list of windows in cyclic ordering. Arguments are like
2131 for `next-window'. */
2133 static Lisp_Object
2134 window_list_1 (window, minibuf, all_frames)
2135 Lisp_Object window, minibuf, all_frames;
2137 Lisp_Object tail, list, rest;
2139 decode_next_window_args (&window, &minibuf, &all_frames);
2140 list = Qnil;
2142 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2143 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2144 list = Fcons (XCAR (tail), list);
2146 /* Rotate the list to start with WINDOW. */
2147 list = Fnreverse (list);
2148 rest = Fmemq (window, list);
2149 if (!NILP (rest) && !EQ (rest, list))
2151 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2153 XSETCDR (tail, Qnil);
2154 list = nconc2 (rest, list);
2156 return list;
2161 /* Look at all windows, performing an operation specified by TYPE
2162 with argument OBJ.
2163 If FRAMES is Qt, look at all frames;
2164 Qnil, look at just the selected frame;
2165 Qvisible, look at visible frames;
2166 a frame, just look at windows on that frame.
2167 If MINI is non-zero, perform the operation on minibuffer windows too. */
2169 enum window_loop
2171 WINDOW_LOOP_UNUSED,
2172 GET_BUFFER_WINDOW, /* Arg is buffer */
2173 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2174 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2175 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2176 GET_LARGEST_WINDOW,
2177 UNSHOW_BUFFER, /* Arg is buffer */
2178 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2179 CHECK_ALL_WINDOWS
2182 static Lisp_Object
2183 window_loop (type, obj, mini, frames)
2184 enum window_loop type;
2185 Lisp_Object obj, frames;
2186 int mini;
2188 Lisp_Object window, windows, best_window, frame_arg;
2189 struct frame *f;
2190 struct gcpro gcpro1;
2192 /* If we're only looping through windows on a particular frame,
2193 frame points to that frame. If we're looping through windows
2194 on all frames, frame is 0. */
2195 if (FRAMEP (frames))
2196 f = XFRAME (frames);
2197 else if (NILP (frames))
2198 f = SELECTED_FRAME ();
2199 else
2200 f = NULL;
2202 if (f)
2203 frame_arg = Qlambda;
2204 else if (EQ (frames, make_number (0)))
2205 frame_arg = frames;
2206 else if (EQ (frames, Qvisible))
2207 frame_arg = frames;
2208 else
2209 frame_arg = Qt;
2211 /* frame_arg is Qlambda to stick to one frame,
2212 Qvisible to consider all visible frames,
2213 or Qt otherwise. */
2215 /* Pick a window to start with. */
2216 if (WINDOWP (obj))
2217 window = obj;
2218 else if (f)
2219 window = FRAME_SELECTED_WINDOW (f);
2220 else
2221 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2223 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2224 GCPRO1 (windows);
2225 best_window = Qnil;
2227 for (; CONSP (windows); windows = XCDR (windows))
2229 struct window *w;
2231 window = XCAR (windows);
2232 w = XWINDOW (window);
2234 /* Note that we do not pay attention here to whether the frame
2235 is visible, since Fwindow_list skips non-visible frames if
2236 that is desired, under the control of frame_arg. */
2237 if (!MINI_WINDOW_P (w)
2238 /* For UNSHOW_BUFFER, we must always consider all windows. */
2239 || type == UNSHOW_BUFFER
2240 || (mini && minibuf_level > 0))
2241 switch (type)
2243 case GET_BUFFER_WINDOW:
2244 if (EQ (w->buffer, obj)
2245 /* Don't find any minibuffer window
2246 except the one that is currently in use. */
2247 && (MINI_WINDOW_P (w)
2248 ? EQ (window, minibuf_window)
2249 : 1))
2251 if (NILP (best_window))
2252 best_window = window;
2253 else if (EQ (window, selected_window))
2254 /* Prefer to return selected-window. */
2255 RETURN_UNGCPRO (window);
2256 else if (EQ (Fwindow_frame (window), selected_frame))
2257 /* Prefer windows on the current frame. */
2258 best_window = window;
2260 break;
2262 case GET_LRU_WINDOW:
2263 /* `obj' is an integer encoding a bitvector.
2264 `obj & 1' means consider only full-width windows.
2265 `obj & 2' means consider also dedicated windows. */
2266 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2267 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2268 /* Minibuffer windows are always ignored. */
2269 || MINI_WINDOW_P (w))
2270 break;
2271 if (NILP (best_window)
2272 || (XFASTINT (XWINDOW (best_window)->use_time)
2273 > XFASTINT (w->use_time)))
2274 best_window = window;
2275 break;
2277 case DELETE_OTHER_WINDOWS:
2278 if (!EQ (window, obj))
2279 Fdelete_window (window);
2280 break;
2282 case DELETE_BUFFER_WINDOWS:
2283 if (EQ (w->buffer, obj))
2285 struct frame *f = XFRAME (WINDOW_FRAME (w));
2287 /* If this window is dedicated, and in a frame of its own,
2288 kill the frame. */
2289 if (EQ (window, FRAME_ROOT_WINDOW (f))
2290 && !NILP (w->dedicated)
2291 && other_visible_frames (f))
2293 /* Skip the other windows on this frame.
2294 There might be one, the minibuffer! */
2295 while (CONSP (XCDR (windows))
2296 && EQ (XWINDOW (XCAR (windows))->frame,
2297 XWINDOW (XCAR (XCDR (windows)))->frame))
2298 windows = XCDR (windows);
2300 /* Now we can safely delete the frame. */
2301 delete_frame (w->frame, Qnil);
2303 else if (NILP (w->parent))
2305 /* If we're deleting the buffer displayed in the
2306 only window on the frame, find a new buffer to
2307 display there. */
2308 Lisp_Object buffer;
2309 buffer = Fother_buffer (obj, Qnil, w->frame);
2310 /* Reset dedicated state of window. */
2311 w->dedicated = Qnil;
2312 Fset_window_buffer (window, buffer, Qnil);
2313 if (EQ (window, selected_window))
2314 Fset_buffer (w->buffer);
2316 else
2317 Fdelete_window (window);
2319 break;
2321 case GET_LARGEST_WINDOW:
2322 { /* nil `obj' means to ignore dedicated windows. */
2323 /* Ignore dedicated windows and minibuffers. */
2324 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2325 break;
2327 if (NILP (best_window))
2328 best_window = window;
2329 else
2331 struct window *b = XWINDOW (best_window);
2332 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2333 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2334 best_window = window;
2337 break;
2339 case UNSHOW_BUFFER:
2340 if (EQ (w->buffer, obj))
2342 Lisp_Object buffer;
2343 struct frame *f = XFRAME (w->frame);
2345 /* Find another buffer to show in this window. */
2346 buffer = Fother_buffer (obj, Qnil, w->frame);
2348 /* If this window is dedicated, and in a frame of its own,
2349 kill the frame. */
2350 if (EQ (window, FRAME_ROOT_WINDOW (f))
2351 && !NILP (w->dedicated)
2352 && other_visible_frames (f))
2354 /* Skip the other windows on this frame.
2355 There might be one, the minibuffer! */
2356 while (CONSP (XCDR (windows))
2357 && EQ (XWINDOW (XCAR (windows))->frame,
2358 XWINDOW (XCAR (XCDR (windows)))->frame))
2359 windows = XCDR (windows);
2361 /* Now we can safely delete the frame. */
2362 delete_frame (w->frame, Qnil);
2364 else if (!NILP (w->dedicated) && !NILP (w->parent))
2366 Lisp_Object window;
2367 XSETWINDOW (window, w);
2368 /* If this window is dedicated and not the only window
2369 in its frame, then kill it. */
2370 Fdelete_window (window);
2372 else
2374 /* Otherwise show a different buffer in the window. */
2375 w->dedicated = Qnil;
2376 Fset_window_buffer (window, buffer, Qnil);
2377 if (EQ (window, selected_window))
2378 Fset_buffer (w->buffer);
2381 break;
2383 case REDISPLAY_BUFFER_WINDOWS:
2384 if (EQ (w->buffer, obj))
2386 mark_window_display_accurate (window, 0);
2387 w->update_mode_line = Qt;
2388 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2389 ++update_mode_lines;
2390 best_window = window;
2392 break;
2394 /* Check for a window that has a killed buffer. */
2395 case CHECK_ALL_WINDOWS:
2396 if (! NILP (w->buffer)
2397 && NILP (XBUFFER (w->buffer)->name))
2398 abort ();
2399 break;
2401 case WINDOW_LOOP_UNUSED:
2402 break;
2406 UNGCPRO;
2407 return best_window;
2410 /* Used for debugging. Abort if any window has a dead buffer. */
2412 void
2413 check_all_windows ()
2415 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2418 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2419 doc: /* Return the window least recently selected or used for display.
2420 \(LRU means Least Recently Used.)
2422 Return a full-width window if possible.
2423 A minibuffer window is never a candidate.
2424 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2425 so if all windows are dedicated, the value is nil.
2426 If optional argument FRAME is `visible', search all visible frames.
2427 If FRAME is 0, search all visible and iconified frames.
2428 If FRAME is t, search all frames.
2429 If FRAME is nil, search only the selected frame.
2430 If FRAME is a frame, search only that frame. */)
2431 (frame, dedicated)
2432 Lisp_Object frame, dedicated;
2434 register Lisp_Object w;
2435 /* First try for a window that is full-width */
2436 w = window_loop (GET_LRU_WINDOW,
2437 NILP (dedicated) ? make_number (1) : make_number (3),
2438 0, frame);
2439 if (!NILP (w) && !EQ (w, selected_window))
2440 return w;
2441 /* If none of them, try the rest */
2442 return window_loop (GET_LRU_WINDOW,
2443 NILP (dedicated) ? make_number (0) : make_number (2),
2444 0, frame);
2447 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2448 doc: /* Return the largest window in area.
2449 A minibuffer window is never a candidate.
2450 A dedicated window is never a candidate unless DEDICATED is non-nil,
2451 so if all windows are dedicated, the value is nil.
2452 If optional argument FRAME is `visible', search all visible frames.
2453 If FRAME is 0, search all visible and iconified frames.
2454 If FRAME is t, search all frames.
2455 If FRAME is nil, search only the selected frame.
2456 If FRAME is a frame, search only that frame. */)
2457 (frame, dedicated)
2458 Lisp_Object frame, dedicated;
2460 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2461 frame);
2464 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2465 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2466 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to the
2467 current buffer.
2468 If optional argument FRAME is `visible', search all visible frames.
2469 If optional argument FRAME is 0, search all visible and iconified frames.
2470 If FRAME is t, search all frames.
2471 If FRAME is nil, search only the selected frame.
2472 If FRAME is a frame, search only that frame. */)
2473 (buffer_or_name, frame)
2474 Lisp_Object buffer_or_name, frame;
2476 Lisp_Object buffer;
2478 if (NILP (buffer_or_name))
2479 buffer = Fcurrent_buffer ();
2480 else
2481 buffer = Fget_buffer (buffer_or_name);
2483 if (BUFFERP (buffer))
2484 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2485 else
2486 return Qnil;
2489 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2490 0, 1, "",
2491 doc: /* Make WINDOW (or the selected window) fill its frame.
2492 Only the frame WINDOW is on is affected.
2493 This function tries to reduce display jumps by keeping the text
2494 previously visible in WINDOW in the same place on the frame. Doing this
2495 depends on the value of (window-start WINDOW), so if calling this
2496 function in a program gives strange scrolling, make sure the
2497 window-start value is reasonable when this function is called. */)
2498 (window)
2499 Lisp_Object window;
2501 struct window *w;
2502 int startpos;
2503 int top, new_top;
2505 if (NILP (window))
2506 window = selected_window;
2507 else
2508 CHECK_LIVE_WINDOW (window);
2509 w = XWINDOW (window);
2511 startpos = marker_position (w->start);
2512 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2514 if (MINI_WINDOW_P (w) && top > 0)
2515 error ("Can't expand minibuffer to full frame");
2517 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2519 /* Try to minimize scrolling, by setting the window start to the point
2520 will cause the text at the old window start to be at the same place
2521 on the frame. But don't try to do this if the window start is
2522 outside the visible portion (as might happen when the display is
2523 not current, due to typeahead). */
2524 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2525 if (new_top != top
2526 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2527 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2529 struct position pos;
2530 struct buffer *obuf = current_buffer;
2532 Fset_buffer (w->buffer);
2533 /* This computation used to temporarily move point, but that can
2534 have unwanted side effects due to text properties. */
2535 pos = *vmotion (startpos, -top, w);
2537 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2538 w->window_end_valid = Qnil;
2539 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2540 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2541 : Qnil);
2542 /* We need to do this, so that the window-scroll-functions
2543 get called. */
2544 w->optional_new_start = Qt;
2546 set_buffer_internal (obuf);
2549 return Qnil;
2552 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2553 0, 2, "bDelete windows on (buffer): ",
2554 doc: /* Delete all windows showing BUFFER-OR-NAME.
2555 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2556 defaults to the current buffer.
2558 Optional second argument FRAME controls which frames are affected.
2559 If optional argument FRAME is `visible', search all visible frames.
2560 If FRAME is 0, search all visible and iconified frames.
2561 If FRAME is nil, search all frames.
2562 If FRAME is t, search only the selected frame.
2563 If FRAME is a frame, search only that frame.
2564 When a window showing BUFFER-OR-NAME is dedicated and the only window of
2565 its frame, that frame is deleted when there are other frames left. */)
2566 (buffer_or_name, frame)
2567 Lisp_Object buffer_or_name, frame;
2569 Lisp_Object buffer;
2571 /* FRAME uses t and nil to mean the opposite of what window_loop
2572 expects. */
2573 if (NILP (frame))
2574 frame = Qt;
2575 else if (EQ (frame, Qt))
2576 frame = Qnil;
2578 if (NILP (buffer_or_name))
2579 buffer = Fcurrent_buffer ();
2580 else
2582 buffer = Fget_buffer (buffer_or_name);
2583 CHECK_BUFFER (buffer);
2586 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2588 return Qnil;
2591 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2592 Sreplace_buffer_in_windows,
2593 0, 1, "bReplace buffer in windows: ",
2594 doc: /* Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2595 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2596 defaults to the current buffer.
2598 When a window showing BUFFER-OR-NAME is dedicated that window is
2599 deleted. If that window is the only window on its frame, that frame is
2600 deleted too when there are other frames left. If there are no other
2601 frames left, some other buffer is displayed in that window. */)
2602 (buffer_or_name)
2603 Lisp_Object buffer_or_name;
2605 Lisp_Object buffer;
2607 if (NILP (buffer_or_name))
2608 buffer = Fcurrent_buffer ();
2609 else
2611 buffer = Fget_buffer (buffer_or_name);
2612 CHECK_BUFFER (buffer);
2615 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2617 return Qnil;
2620 /* Replace BUFFER with some other buffer in all windows
2621 of all frames, even those on other keyboards. */
2623 void
2624 replace_buffer_in_all_windows (buffer)
2625 Lisp_Object buffer;
2627 Lisp_Object tail, frame;
2629 /* A single call to window_loop won't do the job
2630 because it only considers frames on the current keyboard.
2631 So loop manually over frames, and handle each one. */
2632 FOR_EACH_FRAME (tail, frame)
2633 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2636 /* Set the height of WINDOW and all its inferiors. */
2638 /* The smallest acceptable dimensions for a window. Anything smaller
2639 might crash Emacs. */
2641 #define MIN_SAFE_WINDOW_WIDTH (2)
2642 #define MIN_SAFE_WINDOW_HEIGHT (1)
2644 /* For wp non-zero the total number of columns of window w. Otherwise
2645 the total number of lines of w. */
2647 #define WINDOW_TOTAL_SIZE(w, wp) \
2648 (wp ? WINDOW_TOTAL_COLS (w) : WINDOW_TOTAL_LINES (w))
2650 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2651 minimum allowable size. */
2653 void
2654 check_frame_size (frame, rows, cols)
2655 FRAME_PTR frame;
2656 int *rows, *cols;
2658 /* For height, we have to see:
2659 how many windows the frame has at minimum (one or two),
2660 and whether it has a menu bar or other special stuff at the top. */
2661 int min_height
2662 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2663 ? MIN_SAFE_WINDOW_HEIGHT
2664 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2666 if (FRAME_TOP_MARGIN (frame) > 0)
2667 min_height += FRAME_TOP_MARGIN (frame);
2669 if (*rows < min_height)
2670 *rows = min_height;
2671 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2672 *cols = MIN_SAFE_WINDOW_WIDTH;
2675 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2676 check if W's width can be changed, otherwise check W's height.
2677 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2678 siblings, too. If none of the siblings is resizable, WINDOW isn't
2679 either. */
2681 static int
2682 window_fixed_size_p (w, width_p, check_siblings_p)
2683 struct window *w;
2684 int width_p, check_siblings_p;
2686 int fixed_p;
2687 struct window *c;
2689 if (!NILP (w->hchild))
2691 c = XWINDOW (w->hchild);
2693 if (width_p)
2695 /* A horizontal combination is fixed-width if all of if its
2696 children are. */
2697 while (c && window_fixed_size_p (c, width_p, 0))
2698 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2699 fixed_p = c == NULL;
2701 else
2703 /* A horizontal combination is fixed-height 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;
2710 else if (!NILP (w->vchild))
2712 c = XWINDOW (w->vchild);
2714 if (width_p)
2716 /* A vertical combination is fixed-width if one of if its
2717 children is. */
2718 while (c && !window_fixed_size_p (c, width_p, 0))
2719 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2720 fixed_p = c != NULL;
2722 else
2724 /* A vertical combination is fixed-height if all of if its
2725 children are. */
2726 while (c && window_fixed_size_p (c, width_p, 0))
2727 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2728 fixed_p = c == NULL;
2731 else if (BUFFERP (w->buffer))
2733 struct buffer *old = current_buffer;
2734 Lisp_Object val;
2736 current_buffer = XBUFFER (w->buffer);
2737 val = find_symbol_value (Qwindow_size_fixed);
2738 current_buffer = old;
2740 fixed_p = 0;
2741 if (!EQ (val, Qunbound))
2743 fixed_p = !NILP (val);
2745 if (fixed_p
2746 && ((EQ (val, Qheight) && width_p)
2747 || (EQ (val, Qwidth) && !width_p)))
2748 fixed_p = 0;
2751 /* Can't tell if this one is resizable without looking at
2752 siblings. If all siblings are fixed-size this one is too. */
2753 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2755 Lisp_Object child;
2757 for (child = w->prev; WINDOWP (child); child = XWINDOW (child)->prev)
2758 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2759 break;
2761 if (NILP (child))
2762 for (child = w->next; WINDOWP (child); child = XWINDOW (child)->next)
2763 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2764 break;
2766 if (NILP (child))
2767 fixed_p = 1;
2770 else
2771 fixed_p = 1;
2773 return fixed_p;
2776 /* Return minimum size of leaf window W. WIDTH_P non-zero means return
2777 the minimum width of W, WIDTH_P zero means return the minimum height
2778 of W. SAFE_P non-zero means ignore window-min-height|width but just
2779 return values that won't crash Emacs and don't hide components like
2780 fringes, scrollbars, or modelines. If WIDTH_P is zero and W is the
2781 minibuffer window, always return 1. */
2783 static int
2784 window_min_size_2 (w, width_p, safe_p)
2785 struct window *w;
2786 int width_p, safe_p;
2788 /* We should consider buffer-local values of window_min_height and
2789 window_min_width here. */
2790 if (width_p)
2792 int safe_size = (MIN_SAFE_WINDOW_WIDTH
2793 + WINDOW_FRINGE_COLS (w)
2794 + WINDOW_SCROLL_BAR_COLS (w));
2796 return safe_p ? safe_size : max (window_min_width, safe_size);
2798 else if (MINI_WINDOW_P (w))
2799 return 1;
2800 else
2802 int safe_size = (MIN_SAFE_WINDOW_HEIGHT
2803 + ((BUFFERP (w->buffer)
2804 && !NILP (XBUFFER (w->buffer)->mode_line_format))
2805 ? 1 : 0));
2807 return safe_p ? safe_size : max (window_min_height, safe_size);
2811 /* Return minimum size of window W, not taking fixed-width windows into
2812 account. WIDTH_P non-zero means return the minimum width, otherwise
2813 return the minimum height. SAFE_P non-zero means ignore
2814 window-min-height|width but just return values that won't crash Emacs
2815 and don't hide components like fringes, scrollbars, or modelines. If
2816 W is a combination window, compute the minimum size from the minimum
2817 sizes of W's children. */
2819 static int
2820 window_min_size_1 (w, width_p, safe_p)
2821 struct window *w;
2822 int width_p, safe_p;
2824 struct window *c;
2825 int size;
2827 if (!NILP (w->hchild))
2829 /* W is a horizontal combination. */
2830 c = XWINDOW (w->hchild);
2831 size = 0;
2833 if (width_p)
2835 /* The minimum width of a horizontal combination is the sum of
2836 the minimum widths of its children. */
2837 while (c)
2839 size += window_min_size_1 (c, 1, safe_p);
2840 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2843 else
2845 /* The minimum height of a horizontal combination is the
2846 maximum of the minimum heights of its children. */
2847 while (c)
2849 size = max (window_min_size_1 (c, 0, safe_p), size);
2850 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2854 else if (!NILP (w->vchild))
2856 /* W is a vertical combination. */
2857 c = XWINDOW (w->vchild);
2858 size = 0;
2860 if (width_p)
2862 /* The minimum width of a vertical combination is the maximum
2863 of the minimum widths of its children. */
2864 while (c)
2866 size = max (window_min_size_1 (c, 1, safe_p), size);
2867 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2870 else
2872 /* The minimum height of a vertical combination is the sum of
2873 the minimum height of its children. */
2874 while (c)
2876 size += window_min_size_1 (c, 0, safe_p);
2877 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2881 else
2882 /* W is a leaf window. */
2883 size = window_min_size_2 (w, width_p, safe_p);
2885 return size;
2888 /* Return the minimum size of window W, taking fixed-size windows into
2889 account. WIDTH_P non-zero means return the minimum width, otherwise
2890 return the minimum height. SAFE_P non-zero means ignore
2891 window-min-height|width but just return values that won't crash Emacs
2892 and don't hide components like fringes, scrollbars, or modelines.
2893 IGNORE_FIXED_P non-zero means ignore if W is fixed-size. Set *FIXED
2894 to 1 if W is fixed-size unless FIXED is null. */
2896 static int
2897 window_min_size (w, width_p, safe_p, ignore_fixed_p, fixed)
2898 struct window *w;
2899 int width_p, safe_p, ignore_fixed_p, *fixed;
2901 int size, fixed_p;
2903 if (ignore_fixed_p)
2904 fixed_p = 0;
2905 else
2906 fixed_p = window_fixed_size_p (w, width_p, 1);
2908 if (fixed)
2909 *fixed = fixed_p;
2911 if (fixed_p)
2912 size = WINDOW_TOTAL_SIZE (w, width_p);
2913 else
2914 size = window_min_size_1 (w, width_p, safe_p);
2916 return size;
2920 /* Adjust the margins of window W if text area is too small.
2921 Return 1 if window width is ok after adjustment; 0 if window
2922 is still too narrow. */
2924 static int
2925 adjust_window_margins (w)
2926 struct window *w;
2928 int box_cols = (WINDOW_TOTAL_COLS (w)
2929 - WINDOW_FRINGE_COLS (w)
2930 - WINDOW_SCROLL_BAR_COLS (w));
2931 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2932 + WINDOW_RIGHT_MARGIN_COLS (w));
2934 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2935 return 1;
2937 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2938 return 0;
2940 /* Window's text area is too narrow, but reducing the window
2941 margins will fix that. */
2942 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2943 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2945 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2946 w->left_margin_cols = w->right_margin_cols
2947 = make_number (margin_cols/2);
2948 else
2949 w->right_margin_cols = make_number (margin_cols);
2951 else
2952 w->left_margin_cols = make_number (margin_cols);
2953 return 1;
2956 /* Calculate new sizes for windows in the list FORWARD when their
2957 compound size goes from TOTAL to SIZE. TOTAL must be greater than
2958 SIZE. The number of windows in FORWARD is NCHILDREN, and the number
2959 that can shrink is SHRINKABLE. Fixed-size windows may be shrunk if
2960 and only if RESIZE_FIXED_P is non-zero. WIDTH_P non-zero means
2961 shrink columns, otherwise shrink lines.
2963 SAFE_P zero means windows may be sized down to window-min-height
2964 lines (window-min-window columns for WIDTH_P non-zero). SAFE_P
2965 non-zero means windows may be sized down to their minimum safe sizes
2966 taking into account the space needed to display modelines, fringes,
2967 and scrollbars.
2969 This function returns an allocated array of new sizes that the caller
2970 must free. A size -1 means the window is fixed and RESIZE_FIXED_P is
2971 zero. A size zero means the window shall be deleted. Array index 0
2972 refers to the first window in FORWARD, 1 to the second, and so on.
2974 This function resizes windows proportionally to their size. It also
2975 tries to preserve smaller windows by resizing larger windows before
2976 resizing any window to zero. If resize_proportionally is non-nil for
2977 a specific window, it will attempt to strictly resize that window
2978 proportionally, even at the expense of deleting smaller windows. */
2979 static int *
2980 shrink_windows (total, size, nchildren, shrinkable, resize_fixed_p,
2981 forward, width_p, safe_p)
2982 int total, size, nchildren, shrinkable;
2983 int resize_fixed_p, width_p, safe_p;
2984 Lisp_Object forward;
2986 int available_resize = 0;
2987 int *new_sizes, *min_sizes;
2988 struct window *c;
2989 Lisp_Object child;
2990 int smallest = total;
2991 int total_removed = 0;
2992 int total_shrink = total - size;
2993 int i;
2995 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2996 min_sizes = xmalloc (sizeof (*min_sizes) * nchildren);
2998 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
3000 int child_size;
3002 c = XWINDOW (child);
3003 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3005 if (!resize_fixed_p && window_fixed_size_p (c, width_p, 0))
3006 new_sizes[i] = -1;
3007 else
3009 new_sizes[i] = child_size;
3010 min_sizes[i] = window_min_size_1 (c, width_p, safe_p);
3011 if (child_size > min_sizes[i]
3012 && NILP (c->resize_proportionally))
3013 available_resize += child_size - min_sizes[i];
3016 /* We might need to shrink some windows to zero. Find the smallest
3017 windows and set them to 0 until we can fulfil the new size. */
3019 while (shrinkable > 1 && size + available_resize < total)
3021 for (i = 0; i < nchildren; ++i)
3022 if (new_sizes[i] > 0 && smallest > new_sizes[i])
3023 smallest = new_sizes[i];
3025 for (i = 0; i < nchildren; ++i)
3026 if (new_sizes[i] == smallest)
3028 /* Resize this window down to zero. */
3029 new_sizes[i] = 0;
3030 if (smallest > min_sizes[i])
3031 available_resize -= smallest - min_sizes[i];
3032 available_resize += smallest;
3033 --shrinkable;
3034 total_removed += smallest;
3036 /* We don't know what the smallest is now. */
3037 smallest = total;
3039 /* Out of for, just remove one window at the time and
3040 check again if we have enough space. */
3041 break;
3045 /* Now, calculate the new sizes. Try to shrink each window
3046 proportional to its size. */
3047 for (i = 0; i < nchildren; ++i)
3049 if (new_sizes[i] > min_sizes[i])
3051 int to_shrink = total_shrink * new_sizes[i] / total;
3053 if (new_sizes[i] - to_shrink < min_sizes[i])
3054 to_shrink = new_sizes[i] - min_sizes[i];
3055 new_sizes[i] -= to_shrink;
3056 total_removed += to_shrink;
3060 /* Any reminder due to rounding, we just subtract from windows
3061 that are left and still can be shrunk. */
3062 while (total_shrink > total_removed)
3064 int nonzero_sizes = 0;
3065 int nonzero_idx = -1;
3067 for (i = 0; i < nchildren; ++i)
3068 if (new_sizes[i] > 0)
3070 ++nonzero_sizes;
3071 nonzero_idx = i;
3074 for (i = 0; i < nchildren; ++i)
3075 if (new_sizes[i] > min_sizes[i])
3077 --new_sizes[i];
3078 ++total_removed;
3080 /* Out of for, just shrink one window at the time and
3081 check again if we have enough space. */
3082 break;
3085 /* Special case, only one window left. */
3086 if (nonzero_sizes == 1)
3087 break;
3090 /* Any surplus due to rounding, we add to windows that are left. */
3091 while (total_shrink < total_removed)
3093 for (i = 0; i < nchildren; ++i)
3095 if (new_sizes[i] != 0 && total_shrink < total_removed)
3097 ++new_sizes[i];
3098 --total_removed;
3099 break;
3104 xfree (min_sizes);
3106 return new_sizes;
3109 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
3110 WINDOW's width. Resize WINDOW's children, if any, so that they keep
3111 their proportionate size relative to WINDOW.
3113 If FIRST_ONLY is 1, change only the first of WINDOW's children when
3114 they are in series. If LAST_ONLY is 1, change only the last of
3115 WINDOW's children when they are in series.
3117 Propagate WINDOW's top or left edge position to children. Delete
3118 windows that become too small unless NODELETE_P is 1. When
3119 NODELETE_P equals 2 do not honor settings for window-min-height and
3120 window-min-width when resizing windows but use safe defaults instead.
3121 This should give better behavior when resizing frames. */
3123 static void
3124 size_window (window, size, width_p, nodelete_p, first_only, last_only)
3125 Lisp_Object window;
3126 int size, width_p, nodelete_p;
3127 int first_only, last_only;
3129 struct window *w = XWINDOW (window);
3130 struct window *c;
3131 Lisp_Object child, *forward, *sideward;
3132 int old_size = WINDOW_TOTAL_SIZE (w, width_p);
3134 size = max (0, size);
3136 /* Delete WINDOW if it's too small. */
3137 if (nodelete_p != 1 && !NILP (w->parent)
3138 && size < window_min_size_1 (w, width_p, nodelete_p == 2))
3140 delete_window (window);
3141 return;
3144 /* Set redisplay hints. */
3145 w->last_modified = make_number (0);
3146 w->last_overlay_modified = make_number (0);
3147 windows_or_buffers_changed++;
3148 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
3150 if (width_p)
3152 sideward = &w->vchild;
3153 forward = &w->hchild;
3154 w->total_cols = make_number (size);
3155 adjust_window_margins (w);
3157 else
3159 sideward = &w->hchild;
3160 forward = &w->vchild;
3161 w->total_lines = make_number (size);
3162 w->orig_total_lines = Qnil;
3165 if (!NILP (*sideward))
3167 /* We have a chain of parallel siblings whose size should all change. */
3168 for (child = *sideward; !NILP (child); child = c->next)
3170 c = XWINDOW (child);
3171 if (width_p)
3172 c->left_col = w->left_col;
3173 else
3174 c->top_line = w->top_line;
3175 size_window (child, size, width_p, nodelete_p,
3176 first_only, last_only);
3179 else if (!NILP (*forward) && last_only)
3181 /* Change the last in a series of siblings. */
3182 Lisp_Object last_child;
3183 int child_size;
3185 for (child = *forward; !NILP (child); child = c->next)
3187 c = XWINDOW (child);
3188 last_child = child;
3191 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3192 size_window (last_child, size - old_size + child_size,
3193 width_p, nodelete_p, first_only, last_only);
3195 else if (!NILP (*forward) && first_only)
3197 /* Change the first in a series of siblings. */
3198 int child_size;
3200 child = *forward;
3201 c = XWINDOW (child);
3203 if (width_p)
3204 c->left_col = w->left_col;
3205 else
3206 c->top_line = w->top_line;
3208 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3209 size_window (child, size - old_size + child_size,
3210 width_p, nodelete_p, first_only, last_only);
3212 else if (!NILP (*forward))
3214 int fixed_size, each, extra, n;
3215 int resize_fixed_p, nfixed;
3216 int last_pos, first_pos, nchildren, total;
3217 int *new_sizes = NULL;
3219 /* Determine the fixed-size portion of this window, and the
3220 number of child windows. */
3221 fixed_size = nchildren = nfixed = total = 0;
3222 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
3224 int child_size;
3226 c = XWINDOW (child);
3227 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3228 total += child_size;
3230 if (window_fixed_size_p (c, width_p, 0))
3232 fixed_size += child_size;
3233 ++nfixed;
3237 /* If the new size is smaller than fixed_size, or if there
3238 aren't any resizable windows, allow resizing fixed-size
3239 windows. */
3240 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3242 /* Compute how many lines/columns to add/remove to each child. The
3243 value of extra takes care of rounding errors. */
3244 n = resize_fixed_p ? nchildren : nchildren - nfixed;
3245 if (size < total && n > 1)
3246 new_sizes = shrink_windows (total, size, nchildren, n,
3247 resize_fixed_p, *forward, width_p,
3248 nodelete_p == 2);
3249 else
3251 each = (size - total) / n;
3252 extra = (size - total) - n * each;
3255 /* Compute new children heights and edge positions. */
3256 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
3257 last_pos = first_pos;
3258 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
3260 int new_size, old_size;
3262 c = XWINDOW (child);
3263 old_size = WINDOW_TOTAL_SIZE (c, width_p);
3264 new_size = old_size;
3266 /* The top or left edge position of this child equals the
3267 bottom or right edge of its predecessor. */
3268 if (width_p)
3269 c->left_col = make_number (last_pos);
3270 else
3271 c->top_line = make_number (last_pos);
3273 /* If this child can be resized, do it. */
3274 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3276 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
3277 extra = 0;
3280 /* Set new size. Note that size_window also propagates
3281 edge positions to children, so it's not a no-op if we
3282 didn't change the child's size. */
3283 size_window (child, new_size, width_p, 1, first_only, last_only);
3285 /* Remember the bottom/right edge position of this child; it
3286 will be used to set the top/left edge of the next child. */
3287 last_pos += new_size;
3290 xfree (new_sizes);
3292 /* We should have covered the parent exactly with child windows. */
3293 xassert (size == last_pos - first_pos);
3295 /* Now delete any children that became too small. */
3296 if (nodelete_p != 1)
3297 for (child = *forward; !NILP (child); child = c->next)
3299 int child_size;
3301 c = XWINDOW (child);
3302 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3303 size_window (child, child_size, width_p, nodelete_p,
3304 first_only, last_only);
3309 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3310 WINDOW's children. NODELETE zero means windows that have become
3311 smaller than window-min-height in the process may be deleted.
3312 NODELETE 1 means never delete windows that become too small in the
3313 process. (The caller should check later and do so if appropriate.)
3314 NODELETE 2 means delete only windows that have become too small to be
3315 displayed correctly. */
3317 void
3318 set_window_height (window, height, nodelete)
3319 Lisp_Object window;
3320 int height;
3321 int nodelete;
3323 size_window (window, height, 0, nodelete, 0, 0);
3326 /* Set WINDOW's width to WIDTH, and recursively change the width of
3327 WINDOW's children. NODELETE zero means windows that have become
3328 smaller than window-min-width in the process may be deleted.
3329 NODELETE 1 means never delete windows that become too small in the
3330 process. (The caller should check later and do so if appropriate.)
3331 NODELETE 2 means delete only windows that have become too small to be
3332 displayed correctly. */
3334 void
3335 set_window_width (window, width, nodelete)
3336 Lisp_Object window;
3337 int width;
3338 int nodelete;
3340 size_window (window, width, 1, nodelete, 0, 0);
3343 /* Change window heights in windows rooted in WINDOW by N lines. */
3345 void
3346 change_window_heights (window, n)
3347 Lisp_Object window;
3348 int n;
3350 struct window *w = XWINDOW (window);
3352 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3353 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3355 if (INTEGERP (w->orig_top_line))
3356 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3357 if (INTEGERP (w->orig_total_lines))
3358 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3360 /* Handle just the top child in a vertical split. */
3361 if (!NILP (w->vchild))
3362 change_window_heights (w->vchild, n);
3364 /* Adjust all children in a horizontal split. */
3365 for (window = w->hchild; !NILP (window); window = w->next)
3367 w = XWINDOW (window);
3368 change_window_heights (window, n);
3373 int window_select_count;
3375 EXFUN (Fset_window_fringes, 4);
3376 EXFUN (Fset_window_scroll_bars, 4);
3378 static void
3379 run_funs (Lisp_Object funs)
3381 for (; CONSP (funs); funs = XCDR (funs))
3382 if (!EQ (XCAR (funs), Qt))
3383 call0 (XCAR (funs));
3386 static Lisp_Object select_window_norecord (Lisp_Object window);
3387 static Lisp_Object select_frame_norecord (Lisp_Object frame);
3389 void
3390 run_window_configuration_change_hook (struct frame *f)
3392 int count = SPECPDL_INDEX ();
3393 Lisp_Object frame, global_wcch
3394 = Fdefault_value (Qwindow_configuration_change_hook);
3395 XSETFRAME (frame, f);
3397 if (NILP (Vrun_hooks))
3398 return;
3400 if (SELECTED_FRAME () != f)
3402 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3403 Fselect_frame (frame, Qt);
3406 /* Use the right buffer. Matters when running the local hooks. */
3407 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3409 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3410 Fset_buffer (Fwindow_buffer (Qnil));
3413 /* Look for buffer-local values. */
3415 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3416 for (; CONSP (windows); windows = XCDR (windows))
3418 Lisp_Object window = XCAR (windows);
3419 Lisp_Object buffer = Fwindow_buffer (window);
3420 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3421 buffer)))
3423 int count = SPECPDL_INDEX ();
3424 record_unwind_protect (select_window_norecord, Fselected_window ());
3425 select_window_norecord (window);
3426 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3427 buffer));
3428 unbind_to (count, Qnil);
3433 run_funs (global_wcch);
3434 unbind_to (count, Qnil);
3437 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3438 means it's allowed to run hooks. See make_frame for a case where
3439 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3440 margins, fringes, and scroll-bar settings of the window are not
3441 reset from the buffer's local settings. */
3443 void
3444 set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
3445 Lisp_Object window, buffer;
3446 int run_hooks_p, keep_margins_p;
3448 struct window *w = XWINDOW (window);
3449 struct buffer *b = XBUFFER (buffer);
3450 int count = SPECPDL_INDEX ();
3451 int samebuf = EQ (buffer, w->buffer);
3453 w->buffer = buffer;
3455 if (EQ (window, selected_window))
3456 b->last_selected_window = window;
3458 /* Let redisplay errors through. */
3459 b->display_error_modiff = 0;
3461 /* Update time stamps of buffer display. */
3462 if (INTEGERP (b->display_count))
3463 XSETINT (b->display_count, XINT (b->display_count) + 1);
3464 b->display_time = Fcurrent_time ();
3466 XSETFASTINT (w->window_end_pos, 0);
3467 XSETFASTINT (w->window_end_vpos, 0);
3468 bzero (&w->last_cursor, sizeof w->last_cursor);
3469 w->window_end_valid = Qnil;
3470 if (!(keep_margins_p && samebuf))
3471 { /* If we're not actually changing the buffer, don't reset hscroll and
3472 vscroll. This case happens for example when called from
3473 change_frame_size_1, where we use a dummy call to
3474 Fset_window_buffer on the frame's selected window (and no other)
3475 just in order to run window-configuration-change-hook.
3476 Resetting hscroll and vscroll here is problematic for things like
3477 image-mode and doc-view-mode since it resets the image's position
3478 whenever we resize the frame. */
3479 w->hscroll = w->min_hscroll = make_number (0);
3480 w->vscroll = 0;
3481 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3482 set_marker_restricted (w->start,
3483 make_number (b->last_window_start),
3484 buffer);
3485 w->start_at_line_beg = Qnil;
3486 w->force_start = Qnil;
3487 XSETFASTINT (w->last_modified, 0);
3488 XSETFASTINT (w->last_overlay_modified, 0);
3490 /* Maybe we could move this into the `if' but it's not obviously safe and
3491 I doubt it's worth the trouble. */
3492 windows_or_buffers_changed++;
3494 /* We must select BUFFER for running the window-scroll-functions. */
3495 /* We can't check ! NILP (Vwindow_scroll_functions) here
3496 because that might itself be a local variable. */
3497 if (window_initialized)
3499 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3500 Fset_buffer (buffer);
3503 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3505 if (!keep_margins_p)
3507 /* Set left and right marginal area width etc. from buffer. */
3509 /* This may call adjust_window_margins three times, so
3510 temporarily disable window margins. */
3511 Lisp_Object save_left = w->left_margin_cols;
3512 Lisp_Object save_right = w->right_margin_cols;
3514 w->left_margin_cols = w->right_margin_cols = Qnil;
3516 Fset_window_fringes (window,
3517 b->left_fringe_width, b->right_fringe_width,
3518 b->fringes_outside_margins);
3520 Fset_window_scroll_bars (window,
3521 b->scroll_bar_width,
3522 b->vertical_scroll_bar_type, Qnil);
3524 w->left_margin_cols = save_left;
3525 w->right_margin_cols = save_right;
3527 Fset_window_margins (window,
3528 b->left_margin_cols, b->right_margin_cols);
3531 if (run_hooks_p)
3533 if (! NILP (Vwindow_scroll_functions))
3534 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3535 Fmarker_position (w->start));
3536 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3539 unbind_to (count, Qnil);
3543 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3544 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3545 WINDOW defaults to the selected window. BUFFER-OR-NAME must be a buffer
3546 or the name of an existing buffer. Optional third argument KEEP-MARGINS
3547 non-nil means that WINDOW's current display margins, fringe widths, and
3548 scroll bar settings are preserved; the default is to reset these from
3549 the local settings for BUFFER-OR-NAME or the frame defaults. Return nil.
3551 This function throws an error when WINDOW is strongly dedicated to its
3552 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3553 already display BUFFER-OR-NAME.
3555 This function runs `window-scroll-functions' before running
3556 `window-configuration-change-hook'. */)
3557 (window, buffer_or_name, keep_margins)
3558 register Lisp_Object window, buffer_or_name, keep_margins;
3560 register Lisp_Object tem, buffer;
3561 register struct window *w = decode_window (window);
3563 XSETWINDOW (window, w);
3564 buffer = Fget_buffer (buffer_or_name);
3565 CHECK_BUFFER (buffer);
3566 if (NILP (XBUFFER (buffer)->name))
3567 error ("Attempt to display deleted buffer");
3569 tem = w->buffer;
3570 if (NILP (tem))
3571 error ("Window is deleted");
3572 else if (!EQ (tem, Qt))
3573 /* w->buffer is t when the window is first being set up. */
3575 if (!EQ (tem, buffer))
3576 if (EQ (w->dedicated, Qt))
3577 error ("Window is dedicated to `%s'", SDATA (XBUFFER (tem)->name));
3578 else
3579 w->dedicated = Qnil;
3581 unshow_buffer (w);
3584 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3585 return Qnil;
3588 /* Note that selected_window can be nil when this is called from
3589 Fset_window_configuration. */
3591 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3592 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3593 If WINDOW is not already selected, make WINDOW's buffer current
3594 and make WINDOW the frame's selected window. Return WINDOW.
3595 Optional second arg NORECORD non-nil means do not put this buffer
3596 at the front of the list of recently selected ones and do not
3597 make this window the most recently selected one.
3599 Note that the main editor command loop selects the buffer of the
3600 selected window before each command. */)
3601 (window, norecord)
3602 register Lisp_Object window, norecord;
3604 register struct window *w;
3605 register struct window *ow;
3606 struct frame *sf;
3608 CHECK_LIVE_WINDOW (window);
3610 w = XWINDOW (window);
3611 w->frozen_window_start_p = 0;
3613 if (NILP (norecord))
3615 ++window_select_count;
3616 XSETFASTINT (w->use_time, window_select_count);
3619 if (EQ (window, selected_window))
3620 return window;
3622 sf = SELECTED_FRAME ();
3623 if (XFRAME (WINDOW_FRAME (w)) != sf)
3625 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3626 /* Use this rather than Fhandle_switch_frame
3627 so that FRAME_FOCUS_FRAME is moved appropriately as we
3628 move around in the state where a minibuffer in a separate
3629 frame is active. */
3630 Fselect_frame (WINDOW_FRAME (w), norecord);
3631 /* Fselect_frame called us back so we've done all the work already. */
3632 eassert (EQ (window, selected_window));
3633 return window;
3635 else
3636 sf->selected_window = window;
3638 /* Store the current buffer's actual point into the
3639 old selected window. It belongs to that window,
3640 and when the window is not selected, must be in the window. */
3641 if (!NILP (selected_window))
3643 ow = XWINDOW (selected_window);
3644 if (! NILP (ow->buffer))
3645 set_marker_both (ow->pointm, ow->buffer,
3646 BUF_PT (XBUFFER (ow->buffer)),
3647 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3650 selected_window = window;
3652 if (NILP (norecord))
3653 record_buffer (w->buffer);
3654 Fset_buffer (w->buffer);
3656 XBUFFER (w->buffer)->last_selected_window = window;
3658 /* Go to the point recorded in the window.
3659 This is important when the buffer is in more
3660 than one window. It also matters when
3661 redisplay_window has altered point after scrolling,
3662 because it makes the change only in the window. */
3664 register int new_point = marker_position (w->pointm);
3665 if (new_point < BEGV)
3666 SET_PT (BEGV);
3667 else if (new_point > ZV)
3668 SET_PT (ZV);
3669 else
3670 SET_PT (new_point);
3673 windows_or_buffers_changed++;
3674 return window;
3677 static Lisp_Object
3678 select_window_norecord (window)
3679 Lisp_Object window;
3681 return WINDOW_LIVE_P (window)
3682 ? Fselect_window (window, Qt) : selected_window;
3685 static Lisp_Object
3686 select_frame_norecord (frame)
3687 Lisp_Object frame;
3689 return FRAME_LIVE_P (XFRAME (frame))
3690 ? Fselect_frame (frame, Qt) : selected_frame;
3693 Lisp_Object
3694 display_buffer (buffer, not_this_window_p, override_frame)
3695 Lisp_Object buffer, not_this_window_p, override_frame;
3697 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3700 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3701 0, 1, 0,
3702 doc: /* Force all windows to be updated on next redisplay.
3703 If optional arg OBJECT is a window, force redisplay of that window only.
3704 If OBJECT is a buffer or buffer name, force redisplay of all windows
3705 displaying that buffer. */)
3706 (object)
3707 Lisp_Object object;
3709 if (NILP (object))
3711 windows_or_buffers_changed++;
3712 update_mode_lines++;
3713 return Qt;
3716 if (WINDOWP (object))
3718 struct window *w = XWINDOW (object);
3719 mark_window_display_accurate (object, 0);
3720 w->update_mode_line = Qt;
3721 if (BUFFERP (w->buffer))
3722 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3723 ++update_mode_lines;
3724 return Qt;
3727 if (STRINGP (object))
3728 object = Fget_buffer (object);
3729 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3731 /* Walk all windows looking for buffer, and force update
3732 of each of those windows. */
3734 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3735 return NILP (object) ? Qnil : Qt;
3738 /* If nothing suitable was found, just return.
3739 We could signal an error, but this feature will typically be used
3740 asynchronously in timers or process sentinels, so we don't. */
3741 return Qnil;
3745 void
3746 temp_output_buffer_show (buf)
3747 register Lisp_Object buf;
3749 register struct buffer *old = current_buffer;
3750 register Lisp_Object window;
3751 register struct window *w;
3753 XBUFFER (buf)->directory = current_buffer->directory;
3755 Fset_buffer (buf);
3756 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3757 BEGV = BEG;
3758 ZV = Z;
3759 SET_PT (BEG);
3760 #if 0 /* rms: there should be no reason for this. */
3761 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3762 #endif
3763 set_buffer_internal (old);
3765 if (!NILP (Vtemp_buffer_show_function))
3766 call1 (Vtemp_buffer_show_function, buf);
3767 else
3769 window = display_buffer (buf, Qnil, Qnil);
3771 if (!EQ (XWINDOW (window)->frame, selected_frame))
3772 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3773 Vminibuf_scroll_window = window;
3774 w = XWINDOW (window);
3775 XSETFASTINT (w->hscroll, 0);
3776 XSETFASTINT (w->min_hscroll, 0);
3777 set_marker_restricted_both (w->start, buf, BEG, BEG);
3778 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3780 /* Run temp-buffer-show-hook, with the chosen window selected
3781 and its buffer current. */
3783 if (!NILP (Vrun_hooks)
3784 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3785 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3787 int count = SPECPDL_INDEX ();
3788 Lisp_Object prev_window, prev_buffer;
3789 prev_window = selected_window;
3790 XSETBUFFER (prev_buffer, old);
3792 /* Select the window that was chosen, for running the hook.
3793 Note: Both Fselect_window and select_window_norecord may
3794 set-buffer to the buffer displayed in the window,
3795 so we need to save the current buffer. --stef */
3796 record_unwind_protect (Fset_buffer, prev_buffer);
3797 record_unwind_protect (select_window_norecord, prev_window);
3798 Fselect_window (window, Qt);
3799 Fset_buffer (w->buffer);
3800 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3801 unbind_to (count, Qnil);
3806 static void
3807 make_dummy_parent (window)
3808 Lisp_Object window;
3810 Lisp_Object new;
3811 register struct window *o, *p;
3812 int i;
3814 o = XWINDOW (window);
3815 p = allocate_window ();
3816 for (i = 0; i < VECSIZE (struct window); ++i)
3817 ((struct Lisp_Vector *) p)->contents[i]
3818 = ((struct Lisp_Vector *)o)->contents[i];
3819 XSETWINDOW (new, p);
3821 ++sequence_number;
3822 XSETFASTINT (p->sequence_number, sequence_number);
3824 /* Put new into window structure in place of window */
3825 replace_window (window, new);
3827 o->next = Qnil;
3828 o->prev = Qnil;
3829 o->vchild = Qnil;
3830 o->hchild = Qnil;
3831 o->parent = new;
3833 p->start = Qnil;
3834 p->pointm = Qnil;
3835 p->buffer = Qnil;
3838 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3839 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3840 WINDOW defaults to selected one and SIZE to half its size.
3841 If optional third arg HORIZONTAL is non-nil, split side by side and put
3842 SIZE columns in the first of the pair. In that case, SIZE includes that
3843 window's scroll bar, or the divider column to its right.
3844 Interactively, all arguments are nil.
3845 Returns the newly created window (which is the lower or rightmost one).
3846 The upper or leftmost window is the original one, and remains selected
3847 if it was selected before.
3849 See Info node `(elisp)Splitting Windows' for more details and examples. */)
3850 (window, size, horizontal)
3851 Lisp_Object window, size, horizontal;
3853 register Lisp_Object new;
3854 register struct window *o, *p;
3855 FRAME_PTR fo;
3856 register int size_int;
3858 if (NILP (window))
3859 window = selected_window;
3860 else
3861 CHECK_LIVE_WINDOW (window);
3863 o = XWINDOW (window);
3864 fo = XFRAME (WINDOW_FRAME (o));
3866 if (NILP (size))
3868 if (!NILP (horizontal))
3869 /* Calculate the size of the left-hand window, by dividing
3870 the usable space in columns by two.
3871 We round up, since the left-hand window may include
3872 a dividing line, while the right-hand may not. */
3873 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3874 else
3875 size_int = XFASTINT (o->total_lines) >> 1;
3877 else
3879 CHECK_NUMBER (size);
3880 size_int = XINT (size);
3883 if (MINI_WINDOW_P (o))
3884 error ("Attempt to split minibuffer window");
3885 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
3886 error ("Attempt to split fixed-size window");
3888 if (NILP (horizontal))
3890 int window_safe_height = window_min_size_2 (o, 0, 0);
3892 if (size_int < window_safe_height)
3893 error ("Window height %d too small (after splitting)", size_int);
3894 if (size_int + window_safe_height > XFASTINT (o->total_lines))
3895 error ("Window height %d too small (after splitting)",
3896 XFASTINT (o->total_lines) - size_int);
3897 if (NILP (o->parent)
3898 || NILP (XWINDOW (o->parent)->vchild))
3900 make_dummy_parent (window);
3901 new = o->parent;
3902 XWINDOW (new)->vchild = window;
3905 else
3907 int window_safe_width = window_min_size_2 (o, 1, 0);
3909 if (size_int < window_safe_width)
3910 error ("Window width %d too small (after splitting)", size_int);
3911 if (size_int + window_safe_width > XFASTINT (o->total_cols))
3912 error ("Window width %d too small (after splitting)",
3913 XFASTINT (o->total_cols) - size_int);
3914 if (NILP (o->parent)
3915 || NILP (XWINDOW (o->parent)->hchild))
3917 make_dummy_parent (window);
3918 new = o->parent;
3919 XWINDOW (new)->hchild = window;
3923 /* Now we know that window's parent is a vertical combination
3924 if we are dividing vertically, or a horizontal combination
3925 if we are making side-by-side windows */
3927 windows_or_buffers_changed++;
3928 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3929 new = make_window ();
3930 p = XWINDOW (new);
3932 p->frame = o->frame;
3933 p->next = o->next;
3934 if (!NILP (p->next))
3935 XWINDOW (p->next)->prev = new;
3936 p->prev = window;
3937 o->next = new;
3938 p->parent = o->parent;
3939 p->buffer = Qt;
3940 p->window_end_valid = Qnil;
3941 bzero (&p->last_cursor, sizeof p->last_cursor);
3943 /* Duplicate special geometry settings. */
3945 p->left_margin_cols = o->left_margin_cols;
3946 p->right_margin_cols = o->right_margin_cols;
3947 p->left_fringe_width = o->left_fringe_width;
3948 p->right_fringe_width = o->right_fringe_width;
3949 p->fringes_outside_margins = o->fringes_outside_margins;
3950 p->scroll_bar_width = o->scroll_bar_width;
3951 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3953 /* Apportion the available frame space among the two new windows */
3955 if (!NILP (horizontal))
3957 p->total_lines = o->total_lines;
3958 p->top_line = o->top_line;
3959 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3960 XSETFASTINT (o->total_cols, size_int);
3961 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3962 adjust_window_margins (p);
3963 adjust_window_margins (o);
3965 else
3967 p->left_col = o->left_col;
3968 p->total_cols = o->total_cols;
3969 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3970 XSETFASTINT (o->total_lines, size_int);
3971 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3974 /* Adjust glyph matrices. */
3975 adjust_glyphs (fo);
3977 Fset_window_buffer (new, o->buffer, Qt);
3978 return new;
3981 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3982 doc: /* Make selected window SIZE lines taller.
3983 Interactively, if no argument is given, make the selected window one
3984 line taller. If optional argument HORIZONTAL is non-nil, make selected
3985 window wider by SIZE columns. If SIZE is negative, shrink the window by
3986 -SIZE lines or columns. Return nil.
3988 This function can delete windows if they get too small. The size of
3989 fixed size windows is not altered by this function. */)
3990 (size, horizontal)
3991 Lisp_Object size, horizontal;
3993 CHECK_NUMBER (size);
3994 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
3996 run_window_configuration_change_hook (SELECTED_FRAME ());
3998 return Qnil;
4001 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
4002 doc: /* Make selected window SIZE lines smaller.
4003 Interactively, if no argument is given, make the selected window one
4004 line smaller. If optional argument HORIZONTAL is non-nil, make the
4005 window narrower by SIZE columns. If SIZE is negative, enlarge selected
4006 window by -SIZE lines or columns. Return nil.
4008 This function can delete windows if they get too small. The size of
4009 fixed size windows is not altered by this function. */)
4010 (size, horizontal)
4011 Lisp_Object size, horizontal;
4013 CHECK_NUMBER (size);
4014 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
4016 run_window_configuration_change_hook (SELECTED_FRAME ());
4018 return Qnil;
4022 window_height (window)
4023 Lisp_Object window;
4025 register struct window *p = XWINDOW (window);
4026 return WINDOW_TOTAL_LINES (p);
4030 window_width (window)
4031 Lisp_Object window;
4033 register struct window *p = XWINDOW (window);
4034 return WINDOW_TOTAL_COLS (p);
4038 #define CURBEG(w) \
4039 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
4041 #define CURSIZE(w) \
4042 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
4045 /* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
4046 horizontally; zero means do it vertically.
4048 Siblings of the selected window are resized to fulfill the size
4049 request. If they become too small in the process, they may be
4050 deleted. */
4052 static void
4053 enlarge_window (window, delta, horiz_flag)
4054 Lisp_Object window;
4055 int delta, horiz_flag;
4057 Lisp_Object parent, next, prev;
4058 struct window *p;
4059 Lisp_Object *sizep;
4060 int maximum;
4061 int (*sizefun) P_ ((Lisp_Object))
4062 = horiz_flag ? window_width : window_height;
4063 void (*setsizefun) P_ ((Lisp_Object, int, int))
4064 = (horiz_flag ? set_window_width : set_window_height);
4066 /* Give up if this window cannot be resized. */
4067 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4068 error ("Window is not resizable");
4070 /* Find the parent of the selected window. */
4071 while (1)
4073 p = XWINDOW (window);
4074 parent = p->parent;
4076 if (NILP (parent))
4078 if (horiz_flag)
4079 error ("No other window to side of this one");
4080 break;
4083 if (horiz_flag
4084 ? !NILP (XWINDOW (parent)->hchild)
4085 : !NILP (XWINDOW (parent)->vchild))
4086 break;
4088 window = parent;
4091 sizep = &CURSIZE (window);
4094 register int maxdelta;
4096 /* Compute the maximum size increment this window can have. */
4098 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4099 /* This is a main window followed by a minibuffer. */
4100 : !NILP (p->next) ? ((*sizefun) (p->next)
4101 - window_min_size (XWINDOW (p->next),
4102 horiz_flag, 0, 0, 0))
4103 /* This is a minibuffer following a main window. */
4104 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4105 - window_min_size (XWINDOW (p->prev),
4106 horiz_flag, 0, 0, 0))
4107 /* This is a frame with only one window, a minibuffer-only
4108 or a minibufferless frame. */
4109 : (delta = 0));
4111 if (delta > maxdelta)
4112 /* This case traps trying to make the minibuffer
4113 the full frame, or make the only window aside from the
4114 minibuffer the full frame. */
4115 delta = maxdelta;
4118 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4119 horiz_flag, 0, 0, 0))
4121 delete_window (window);
4122 return;
4125 if (delta == 0)
4126 return;
4128 /* Find the total we can get from other siblings without deleting them. */
4129 maximum = 0;
4130 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
4131 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4132 horiz_flag, 0, 0, 0);
4133 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
4134 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4135 horiz_flag, 0, 0, 0);
4137 /* If we can get it all from them without deleting them, do so. */
4138 if (delta <= maximum)
4140 Lisp_Object first_unaffected;
4141 Lisp_Object first_affected;
4142 int fixed_p;
4144 next = p->next;
4145 prev = p->prev;
4146 first_affected = window;
4147 /* Look at one sibling at a time,
4148 moving away from this window in both directions alternately,
4149 and take as much as we can get without deleting that sibling. */
4150 while (delta != 0
4151 && (!NILP (next) || !NILP (prev)))
4153 if (! NILP (next))
4155 int this_one = ((*sizefun) (next)
4156 - window_min_size (XWINDOW (next), horiz_flag,
4157 0, 0, &fixed_p));
4158 if (!fixed_p)
4160 if (this_one > delta)
4161 this_one = delta;
4163 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4164 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4166 delta -= this_one;
4169 next = XWINDOW (next)->next;
4172 if (delta == 0)
4173 break;
4175 if (! NILP (prev))
4177 int this_one = ((*sizefun) (prev)
4178 - window_min_size (XWINDOW (prev), horiz_flag,
4179 0, 0, &fixed_p));
4180 if (!fixed_p)
4182 if (this_one > delta)
4183 this_one = delta;
4185 first_affected = prev;
4187 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4188 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4190 delta -= this_one;
4193 prev = XWINDOW (prev)->prev;
4197 xassert (delta == 0);
4199 /* Now recalculate the edge positions of all the windows affected,
4200 based on the new sizes. */
4201 first_unaffected = next;
4202 prev = first_affected;
4203 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4204 prev = next, next = XWINDOW (next)->next)
4206 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4207 /* This does not change size of NEXT,
4208 but it propagates the new top edge to its children */
4209 (*setsizefun) (next, (*sizefun) (next), 0);
4212 else
4214 register int delta1;
4215 register int opht = (*sizefun) (parent);
4217 if (opht <= XINT (*sizep) + delta)
4219 /* If trying to grow this window to or beyond size of the parent,
4220 just delete all the sibling windows. */
4221 Lisp_Object start, tem, next;
4223 start = XWINDOW (parent)->vchild;
4224 if (NILP (start))
4225 start = XWINDOW (parent)->hchild;
4227 /* Delete any siblings that come after WINDOW. */
4228 tem = XWINDOW (window)->next;
4229 while (! NILP (tem))
4231 next = XWINDOW (tem)->next;
4232 delete_window (tem);
4233 tem = next;
4236 /* Delete any siblings that come after WINDOW.
4237 Note that if START is not WINDOW, then WINDOW still
4238 has siblings, so WINDOW has not yet replaced its parent. */
4239 tem = start;
4240 while (! EQ (tem, window))
4242 next = XWINDOW (tem)->next;
4243 delete_window (tem);
4244 tem = next;
4247 else
4249 /* Otherwise, make delta1 just right so that if we add
4250 delta1 lines to this window and to the parent, and then
4251 shrink the parent back to its original size, the new
4252 proportional size of this window will increase by delta.
4254 The function size_window will compute the new height h'
4255 of the window from delta1 as:
4257 e = delta1/n
4258 x = delta1 - delta1/n * n for the 1st resizable child
4259 h' = h + e + x
4261 where n is the number of children that can be resized.
4262 We can ignore x by choosing a delta1 that is a multiple of
4263 n. We want the height of this window to come out as
4265 h' = h + delta
4267 So, delta1 must be
4269 h + e = h + delta
4270 delta1/n = delta
4271 delta1 = n * delta.
4273 The number of children n equals the number of resizable
4274 children of this window + 1 because we know window itself
4275 is resizable (otherwise we would have signaled an error).
4277 This reasoning is not correct when other windows become too
4278 small and shrink_windows refuses to delete them. Below we
4279 use resize_proportionally to work around this problem. */
4281 struct window *w = XWINDOW (window);
4282 Lisp_Object s;
4283 int n = 1;
4285 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
4286 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4287 ++n;
4288 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
4289 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4290 ++n;
4292 delta1 = n * delta;
4294 /* Add delta1 lines or columns to this window, and to the parent,
4295 keeping things consistent while not affecting siblings. */
4296 XSETINT (CURSIZE (parent), opht + delta1);
4297 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4299 /* Squeeze out delta1 lines or columns from our parent,
4300 shrinking this window and siblings proportionately. This
4301 brings parent back to correct size. Delta1 was calculated
4302 so this makes this window the desired size, taking it all
4303 out of the siblings.
4305 Temporarily set resize_proportionally to Qt to assure that,
4306 if necessary, shrink_windows deletes smaller windows rather
4307 than shrink this window. */
4308 w->resize_proportionally = Qt;
4309 (*setsizefun) (parent, opht, 0);
4310 w->resize_proportionally = Qnil;
4314 XSETFASTINT (p->last_modified, 0);
4315 XSETFASTINT (p->last_overlay_modified, 0);
4317 /* Adjust glyph matrices. */
4318 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4322 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4323 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4324 zero means adjust the height, moving the bottom edge.
4326 Following siblings of the selected window are resized to fulfill
4327 the size request. If they become too small in the process, they
4328 are not deleted; instead, we signal an error. */
4330 static void
4331 adjust_window_trailing_edge (window, delta, horiz_flag)
4332 Lisp_Object window;
4333 int delta, horiz_flag;
4335 Lisp_Object parent, child;
4336 struct window *p;
4337 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4338 int delcount = window_deletion_count;
4340 CHECK_WINDOW (window);
4342 /* Give up if this window cannot be resized. */
4343 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4344 error ("Window is not resizable");
4346 while (1)
4348 Lisp_Object first_parallel = Qnil;
4350 if (NILP (window))
4352 /* This happens if WINDOW on the previous iteration was
4353 at top level of the window tree. */
4354 Fset_window_configuration (old_config);
4355 error ("Specified window edge is fixed");
4358 p = XWINDOW (window);
4359 parent = p->parent;
4361 /* See if this level has windows in parallel in the specified
4362 direction. If so, set FIRST_PARALLEL to the first one. */
4363 if (horiz_flag)
4365 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4366 first_parallel = XWINDOW (parent)->vchild;
4367 else if (NILP (parent) && !NILP (p->next))
4369 /* Handle the vertical chain of main window and minibuffer
4370 which has no parent. */
4371 first_parallel = window;
4372 while (! NILP (XWINDOW (first_parallel)->prev))
4373 first_parallel = XWINDOW (first_parallel)->prev;
4376 else
4378 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4379 first_parallel = XWINDOW (parent)->hchild;
4382 /* If this level's succession is in the desired dimension,
4383 and this window is the last one, and there is no higher level,
4384 its trailing edge is fixed. */
4385 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4386 && NILP (parent))
4388 Fset_window_configuration (old_config);
4389 error ("Specified window edge is fixed");
4392 /* Don't make this window too small. */
4393 if (XINT (CURSIZE (window)) + delta
4394 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
4396 Fset_window_configuration (old_config);
4397 error ("Cannot adjust window size as specified");
4400 /* Clear out some redisplay caches. */
4401 XSETFASTINT (p->last_modified, 0);
4402 XSETFASTINT (p->last_overlay_modified, 0);
4404 /* Adjust this window's edge. */
4405 XSETINT (CURSIZE (window),
4406 XINT (CURSIZE (window)) + delta);
4408 /* If this window has following siblings in the desired dimension,
4409 make them smaller, and exit the loop.
4411 (If we reach the top of the tree and can never do this,
4412 we will fail and report an error, above.) */
4413 if (NILP (first_parallel))
4415 if (!NILP (p->next))
4417 /* This may happen for the minibuffer. In that case
4418 the window_deletion_count check below does not work. */
4419 if (XINT (CURSIZE (p->next)) - delta <= 0)
4421 Fset_window_configuration (old_config);
4422 error ("Cannot adjust window size as specified");
4425 XSETINT (CURBEG (p->next),
4426 XINT (CURBEG (p->next)) + delta);
4427 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4428 horiz_flag, 0, 1, 0);
4429 break;
4432 else
4433 /* Here we have a chain of parallel siblings, in the other dimension.
4434 Change the size of the other siblings. */
4435 for (child = first_parallel;
4436 ! NILP (child);
4437 child = XWINDOW (child)->next)
4438 if (! EQ (child, window))
4439 size_window (child, XINT (CURSIZE (child)) + delta,
4440 horiz_flag, 0, 0, 1);
4442 window = parent;
4445 /* If we made a window so small it got deleted,
4446 we failed. Report failure. */
4447 if (delcount != window_deletion_count)
4449 Fset_window_configuration (old_config);
4450 error ("Cannot adjust window size as specified");
4453 /* Adjust glyph matrices. */
4454 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4457 #undef CURBEG
4458 #undef CURSIZE
4460 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4461 Sadjust_window_trailing_edge, 3, 3, 0,
4462 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4463 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4464 Otherwise, adjust the height, moving the bottom edge.
4466 Following siblings of the selected window are resized to fulfill
4467 the size request. If they become too small in the process, they
4468 are not deleted; instead, we signal an error. */)
4469 (window, delta, horizontal)
4470 Lisp_Object window, delta, horizontal;
4472 CHECK_NUMBER (delta);
4473 if (NILP (window))
4474 window = selected_window;
4475 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4477 run_window_configuration_change_hook
4478 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4480 return Qnil;
4485 /***********************************************************************
4486 Resizing Mini-Windows
4487 ***********************************************************************/
4489 static void shrink_window_lowest_first P_ ((struct window *, int));
4491 enum save_restore_action
4493 CHECK_ORIG_SIZES,
4494 SAVE_ORIG_SIZES,
4495 RESTORE_ORIG_SIZES
4498 static int save_restore_orig_size P_ ((struct window *,
4499 enum save_restore_action));
4501 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4502 from lowest windows first. */
4504 static void
4505 shrink_window_lowest_first (w, height)
4506 struct window *w;
4507 int height;
4509 struct window *c;
4510 Lisp_Object child;
4511 int old_height;
4513 xassert (!MINI_WINDOW_P (w));
4515 /* Set redisplay hints. */
4516 XSETFASTINT (w->last_modified, 0);
4517 XSETFASTINT (w->last_overlay_modified, 0);
4518 windows_or_buffers_changed++;
4519 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4521 old_height = XFASTINT (w->total_lines);
4522 XSETFASTINT (w->total_lines, height);
4524 if (!NILP (w->hchild))
4526 for (child = w->hchild; !NILP (child); child = c->next)
4528 c = XWINDOW (child);
4529 c->top_line = w->top_line;
4530 shrink_window_lowest_first (c, height);
4533 else if (!NILP (w->vchild))
4535 Lisp_Object last_child;
4536 int delta = old_height - height;
4537 int last_top;
4539 last_child = Qnil;
4541 /* Find the last child. We are taking space from lowest windows
4542 first, so we iterate over children from the last child
4543 backwards. */
4544 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
4545 last_child = child;
4547 /* Size children down to their safe heights. */
4548 for (child = last_child; delta && !NILP (child); child = c->prev)
4550 int this_one;
4552 c = XWINDOW (child);
4553 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
4555 if (this_one > delta)
4556 this_one = delta;
4558 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4559 delta -= this_one;
4562 /* Compute new positions. */
4563 last_top = XINT (w->top_line);
4564 for (child = w->vchild; !NILP (child); child = c->next)
4566 c = XWINDOW (child);
4567 c->top_line = make_number (last_top);
4568 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4569 last_top += XFASTINT (c->total_lines);
4575 /* Save, restore, or check positions and sizes in the window tree
4576 rooted at W. ACTION says what to do.
4578 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4579 orig_total_lines members are valid for all windows in the window
4580 tree. Value is non-zero if they are valid.
4582 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4583 orig_top_line and orig_total_lines for all windows in the tree.
4585 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4586 stored in orig_top_line and orig_total_lines for all windows. */
4588 static int
4589 save_restore_orig_size (w, action)
4590 struct window *w;
4591 enum save_restore_action action;
4593 int success_p = 1;
4595 while (w)
4597 if (!NILP (w->hchild))
4599 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4600 success_p = 0;
4602 else if (!NILP (w->vchild))
4604 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4605 success_p = 0;
4608 switch (action)
4610 case CHECK_ORIG_SIZES:
4611 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4612 return 0;
4613 break;
4615 case SAVE_ORIG_SIZES:
4616 w->orig_top_line = w->top_line;
4617 w->orig_total_lines = w->total_lines;
4618 XSETFASTINT (w->last_modified, 0);
4619 XSETFASTINT (w->last_overlay_modified, 0);
4620 break;
4622 case RESTORE_ORIG_SIZES:
4623 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4624 w->top_line = w->orig_top_line;
4625 w->total_lines = w->orig_total_lines;
4626 w->orig_total_lines = w->orig_top_line = Qnil;
4627 XSETFASTINT (w->last_modified, 0);
4628 XSETFASTINT (w->last_overlay_modified, 0);
4629 break;
4631 default:
4632 abort ();
4635 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4638 return success_p;
4642 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4643 without deleting other windows. */
4645 void
4646 grow_mini_window (w, delta)
4647 struct window *w;
4648 int delta;
4650 struct frame *f = XFRAME (w->frame);
4651 struct window *root;
4653 xassert (MINI_WINDOW_P (w));
4654 /* Commenting out the following assertion goes against the stated interface
4655 of the function, but it currently does not seem to do anything useful.
4656 See discussion of this issue in the thread for bug#4534.
4657 xassert (delta >= 0); */
4659 /* Compute how much we can enlarge the mini-window without deleting
4660 other windows. */
4661 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4662 if (delta > 0)
4664 int min_height = window_min_size (root, 0, 0, 0, 0);
4665 if (XFASTINT (root->total_lines) - delta < min_height)
4666 /* Note that the root window may already be smaller than
4667 min_height. */
4668 delta = max (0, XFASTINT (root->total_lines) - min_height);
4671 if (delta)
4673 /* Save original window sizes and positions, if not already done. */
4674 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4675 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4677 /* Shrink other windows. */
4678 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4680 /* Grow the mini-window. */
4681 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4682 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4683 XSETFASTINT (w->last_modified, 0);
4684 XSETFASTINT (w->last_overlay_modified, 0);
4686 adjust_glyphs (f);
4691 /* Shrink mini-window W. If there is recorded info about window sizes
4692 before a call to grow_mini_window, restore recorded window sizes.
4693 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4694 line. */
4696 void
4697 shrink_mini_window (w)
4698 struct window *w;
4700 struct frame *f = XFRAME (w->frame);
4701 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4703 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4705 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4706 adjust_glyphs (f);
4707 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4708 windows_or_buffers_changed = 1;
4710 else if (XFASTINT (w->total_lines) > 1)
4712 /* Distribute the additional lines of the mini-window
4713 among the other windows. */
4714 Lisp_Object window;
4715 XSETWINDOW (window, w);
4716 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4722 /* Mark window cursors off for all windows in the window tree rooted
4723 at W by setting their phys_cursor_on_p flag to zero. Called from
4724 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4725 the frame are cleared. */
4727 void
4728 mark_window_cursors_off (w)
4729 struct window *w;
4731 while (w)
4733 if (!NILP (w->hchild))
4734 mark_window_cursors_off (XWINDOW (w->hchild));
4735 else if (!NILP (w->vchild))
4736 mark_window_cursors_off (XWINDOW (w->vchild));
4737 else
4738 w->phys_cursor_on_p = 0;
4740 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4745 /* Return number of lines of text (not counting mode lines) in W. */
4748 window_internal_height (w)
4749 struct window *w;
4751 int ht = XFASTINT (w->total_lines);
4753 if (!MINI_WINDOW_P (w))
4755 if (!NILP (w->parent)
4756 || !NILP (w->vchild)
4757 || !NILP (w->hchild)
4758 || !NILP (w->next)
4759 || !NILP (w->prev)
4760 || WINDOW_WANTS_MODELINE_P (w))
4761 --ht;
4763 if (WINDOW_WANTS_HEADER_LINE_P (w))
4764 --ht;
4767 return ht;
4771 /* Return the number of columns in W.
4772 Don't count columns occupied by scroll bars or the vertical bar
4773 separating W from the sibling to its right. */
4776 window_box_text_cols (w)
4777 struct window *w;
4779 struct frame *f = XFRAME (WINDOW_FRAME (w));
4780 int width = XINT (w->total_cols);
4782 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4783 /* Scroll bars occupy a few columns. */
4784 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4785 else if (!FRAME_WINDOW_P (f)
4786 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4787 /* The column of `|' characters separating side-by-side windows
4788 occupies one column only. */
4789 width -= 1;
4791 if (FRAME_WINDOW_P (f))
4792 /* On window-systems, fringes and display margins cannot be
4793 used for normal text. */
4794 width -= (WINDOW_FRINGE_COLS (w)
4795 + WINDOW_LEFT_MARGIN_COLS (w)
4796 + WINDOW_RIGHT_MARGIN_COLS (w));
4798 return width;
4802 /************************************************************************
4803 Window Scrolling
4804 ***********************************************************************/
4806 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4807 N screen-fulls, which is defined as the height of the window minus
4808 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4809 instead. Negative values of N mean scroll down. NOERROR non-zero
4810 means don't signal an error if we try to move over BEGV or ZV,
4811 respectively. */
4813 static void
4814 window_scroll (window, n, whole, noerror)
4815 Lisp_Object window;
4816 int n;
4817 int whole;
4818 int noerror;
4820 immediate_quit = 1;
4822 /* If we must, use the pixel-based version which is much slower than
4823 the line-based one but can handle varying line heights. */
4824 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4825 window_scroll_pixel_based (window, n, whole, noerror);
4826 else
4827 window_scroll_line_based (window, n, whole, noerror);
4829 immediate_quit = 0;
4833 /* Implementation of window_scroll that works based on pixel line
4834 heights. See the comment of window_scroll for parameter
4835 descriptions. */
4837 static void
4838 window_scroll_pixel_based (window, n, whole, noerror)
4839 Lisp_Object window;
4840 int n;
4841 int whole;
4842 int noerror;
4844 struct it it;
4845 struct window *w = XWINDOW (window);
4846 struct text_pos start;
4847 int this_scroll_margin;
4848 /* True if we fiddled the window vscroll field without really scrolling. */
4849 int vscrolled = 0;
4850 int x, y, rtop, rbot, rowh, vpos;
4852 SET_TEXT_POS_FROM_MARKER (start, w->start);
4854 /* If PT is not visible in WINDOW, move back one half of
4855 the screen. Allow PT to be partially visible, otherwise
4856 something like (scroll-down 1) with PT in the line before
4857 the partially visible one would recenter. */
4859 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4861 /* Move backward half the height of the window. Performance note:
4862 vmotion used here is about 10% faster, but would give wrong
4863 results for variable height lines. */
4864 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4865 it.current_y = it.last_visible_y;
4866 move_it_vertically_backward (&it, window_box_height (w) / 2);
4868 /* The function move_iterator_vertically may move over more than
4869 the specified y-distance. If it->w is small, e.g. a
4870 mini-buffer window, we may end up in front of the window's
4871 display area. This is the case when Start displaying at the
4872 start of the line containing PT in this case. */
4873 if (it.current_y <= 0)
4875 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4876 move_it_vertically_backward (&it, 0);
4877 it.current_y = 0;
4880 start = it.current.pos;
4882 else if (auto_window_vscroll_p)
4884 if (rtop || rbot) /* partially visible */
4886 int px;
4887 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4888 if (whole)
4889 dy = max ((window_box_height (w)
4890 - next_screen_context_lines * dy),
4891 dy);
4892 dy *= n;
4894 if (n < 0)
4896 /* Only vscroll backwards if already vscrolled forwards. */
4897 if (w->vscroll < 0 && rtop > 0)
4899 px = max (0, -w->vscroll - min (rtop, -dy));
4900 Fset_window_vscroll (window, make_number (px), Qt);
4901 return;
4904 if (n > 0)
4906 /* Do vscroll if already vscrolled or only display line. */
4907 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4909 px = max (0, -w->vscroll + min (rbot, dy));
4910 Fset_window_vscroll (window, make_number (px), Qt);
4911 return;
4914 /* Maybe modify window start instead of scrolling. */
4915 if (rbot > 0 || w->vscroll < 0)
4917 int spos;
4919 Fset_window_vscroll (window, make_number (0), Qt);
4920 /* If there are other text lines above the current row,
4921 move window start to current row. Else to next row. */
4922 if (rbot > 0)
4923 spos = XINT (Fline_beginning_position (Qnil));
4924 else
4925 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4926 set_marker_restricted (w->start, make_number (spos),
4927 w->buffer);
4928 w->start_at_line_beg = Qt;
4929 w->update_mode_line = Qt;
4930 XSETFASTINT (w->last_modified, 0);
4931 XSETFASTINT (w->last_overlay_modified, 0);
4932 /* Set force_start so that redisplay_window will run the
4933 window-scroll-functions. */
4934 w->force_start = Qt;
4935 return;
4939 /* Cancel previous vscroll. */
4940 Fset_window_vscroll (window, make_number (0), Qt);
4943 /* If scroll_preserve_screen_position is non-nil, we try to set
4944 point in the same window line as it is now, so get that line. */
4945 if (!NILP (Vscroll_preserve_screen_position))
4947 /* We preserve the goal pixel coordinate across consecutive
4948 calls to scroll-up or scroll-down. This avoids the
4949 possibility of point becoming "stuck" on a tall line when
4950 scrolling by one line. */
4951 if (window_scroll_pixel_based_preserve_y < 0
4952 || (!EQ (current_kboard->Vlast_command, Qscroll_up)
4953 && !EQ (current_kboard->Vlast_command, Qscroll_down)))
4955 start_display (&it, w, start);
4956 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4957 window_scroll_pixel_based_preserve_y = it.current_y;
4958 window_scroll_pixel_based_preserve_x = it.current_x;
4961 else
4962 window_scroll_pixel_based_preserve_y
4963 = window_scroll_pixel_based_preserve_x = -1;
4965 /* Move iterator it from start the specified distance forward or
4966 backward. The result is the new window start. */
4967 start_display (&it, w, start);
4968 if (whole)
4970 int start_pos = IT_CHARPOS (it);
4971 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4972 dy = max ((window_box_height (w)
4973 - next_screen_context_lines * dy),
4974 dy) * n;
4976 /* Note that move_it_vertically always moves the iterator to the
4977 start of a line. So, if the last line doesn't have a newline,
4978 we would end up at the start of the line ending at ZV. */
4979 if (dy <= 0)
4981 move_it_vertically_backward (&it, -dy);
4982 /* Ensure we actually do move, e.g. in case we are currently
4983 looking at an image that is taller that the window height. */
4984 while (start_pos == IT_CHARPOS (it)
4985 && start_pos > BEGV)
4986 move_it_by_lines (&it, -1, 1);
4988 else if (dy > 0)
4990 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4991 MOVE_TO_POS | MOVE_TO_Y);
4992 /* Ensure we actually do move, e.g. in case we are currently
4993 looking at an image that is taller that the window height. */
4994 while (start_pos == IT_CHARPOS (it)
4995 && start_pos < ZV)
4996 move_it_by_lines (&it, 1, 1);
4999 else
5000 move_it_by_lines (&it, n, 1);
5002 /* We failed if we find ZV is already on the screen (scrolling up,
5003 means there's nothing past the end), or if we can't start any
5004 earlier (scrolling down, means there's nothing past the top). */
5005 if ((n > 0 && IT_CHARPOS (it) == ZV)
5006 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5008 if (IT_CHARPOS (it) == ZV)
5010 if (it.current_y < it.last_visible_y
5011 && (it.current_y + it.max_ascent + it.max_descent
5012 > it.last_visible_y))
5014 /* The last line was only partially visible, make it fully
5015 visible. */
5016 w->vscroll = (it.last_visible_y
5017 - it.current_y + it.max_ascent + it.max_descent);
5018 adjust_glyphs (it.f);
5020 else if (noerror)
5021 return;
5022 else if (n < 0) /* could happen with empty buffers */
5023 xsignal0 (Qbeginning_of_buffer);
5024 else
5025 xsignal0 (Qend_of_buffer);
5027 else
5029 if (w->vscroll != 0)
5030 /* The first line was only partially visible, make it fully
5031 visible. */
5032 w->vscroll = 0;
5033 else if (noerror)
5034 return;
5035 else
5036 xsignal0 (Qbeginning_of_buffer);
5039 /* If control gets here, then we vscrolled. */
5041 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
5043 /* Don't try to change the window start below. */
5044 vscrolled = 1;
5047 if (! vscrolled)
5049 int pos = IT_CHARPOS (it);
5050 int bytepos;
5052 /* If in the middle of a multi-glyph character move forward to
5053 the next character. */
5054 if (in_display_vector_p (&it))
5056 ++pos;
5057 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5060 /* Set the window start, and set up the window for redisplay. */
5061 set_marker_restricted (w->start, make_number (pos),
5062 w->buffer);
5063 bytepos = XMARKER (w->start)->bytepos;
5064 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
5065 ? Qt : Qnil);
5066 w->update_mode_line = Qt;
5067 XSETFASTINT (w->last_modified, 0);
5068 XSETFASTINT (w->last_overlay_modified, 0);
5069 /* Set force_start so that redisplay_window will run the
5070 window-scroll-functions. */
5071 w->force_start = Qt;
5074 /* The rest of this function uses current_y in a nonstandard way,
5075 not including the height of the header line if any. */
5076 it.current_y = it.vpos = 0;
5078 /* Move PT out of scroll margins.
5079 This code wants current_y to be zero at the window start position
5080 even if there is a header line. */
5081 this_scroll_margin = max (0, scroll_margin);
5082 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
5083 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5085 if (n > 0)
5087 /* We moved the window start towards ZV, so PT may be now
5088 in the scroll margin at the top. */
5089 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5090 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5091 && (NILP (Vscroll_preserve_screen_position)
5092 || EQ (Vscroll_preserve_screen_position, Qt)))
5093 /* We found PT at a legitimate height. Leave it alone. */
5095 else if (window_scroll_pixel_based_preserve_y >= 0)
5097 /* If we have a header line, take account of it.
5098 This is necessary because we set it.current_y to 0, above. */
5099 move_it_to (&it, -1,
5100 window_scroll_pixel_based_preserve_x,
5101 window_scroll_pixel_based_preserve_y
5102 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5103 -1, MOVE_TO_Y | MOVE_TO_X);
5104 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5106 else
5108 while (it.current_y < this_scroll_margin)
5110 int prev = it.current_y;
5111 move_it_by_lines (&it, 1, 1);
5112 if (prev == it.current_y)
5113 break;
5115 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5118 else if (n < 0)
5120 int charpos, bytepos;
5121 int partial_p;
5123 /* Save our position, for the
5124 window_scroll_pixel_based_preserve_y case. */
5125 charpos = IT_CHARPOS (it);
5126 bytepos = IT_BYTEPOS (it);
5128 /* We moved the window start towards BEGV, so PT may be now
5129 in the scroll margin at the bottom. */
5130 move_it_to (&it, PT, -1,
5131 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5132 - this_scroll_margin - 1),
5134 MOVE_TO_POS | MOVE_TO_Y);
5136 /* Save our position, in case it's correct. */
5137 charpos = IT_CHARPOS (it);
5138 bytepos = IT_BYTEPOS (it);
5140 /* See if point is on a partially visible line at the end. */
5141 if (it.what == IT_EOB)
5142 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5143 else
5145 move_it_by_lines (&it, 1, 1);
5146 partial_p = it.current_y > it.last_visible_y;
5149 if (charpos == PT && !partial_p
5150 && (NILP (Vscroll_preserve_screen_position)
5151 || EQ (Vscroll_preserve_screen_position, Qt)))
5152 /* We found PT before we found the display margin, so PT is ok. */
5154 else if (window_scroll_pixel_based_preserve_y >= 0)
5156 SET_TEXT_POS_FROM_MARKER (start, w->start);
5157 start_display (&it, w, start);
5158 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5159 here because we called start_display again and did not
5160 alter it.current_y this time. */
5161 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5162 window_scroll_pixel_based_preserve_y, -1,
5163 MOVE_TO_Y | MOVE_TO_X);
5164 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5166 else
5168 if (partial_p)
5169 /* The last line was only partially visible, so back up two
5170 lines to make sure we're on a fully visible line. */
5172 move_it_by_lines (&it, -2, 0);
5173 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5175 else
5176 /* No, the position we saved is OK, so use it. */
5177 SET_PT_BOTH (charpos, bytepos);
5183 /* Implementation of window_scroll that works based on screen lines.
5184 See the comment of window_scroll for parameter descriptions. */
5186 static void
5187 window_scroll_line_based (window, n, whole, noerror)
5188 Lisp_Object window;
5189 int n;
5190 int whole;
5191 int noerror;
5193 register struct window *w = XWINDOW (window);
5194 register int opoint = PT, opoint_byte = PT_BYTE;
5195 register int pos, pos_byte;
5196 register int ht = window_internal_height (w);
5197 register Lisp_Object tem;
5198 int lose;
5199 Lisp_Object bolp;
5200 int startpos;
5201 Lisp_Object original_pos = Qnil;
5203 /* If scrolling screen-fulls, compute the number of lines to
5204 scroll from the window's height. */
5205 if (whole)
5206 n *= max (1, ht - next_screen_context_lines);
5208 startpos = marker_position (w->start);
5210 if (!NILP (Vscroll_preserve_screen_position))
5212 if (window_scroll_preserve_vpos <= 0
5213 || (!EQ (current_kboard->Vlast_command, Qscroll_up)
5214 && !EQ (current_kboard->Vlast_command, Qscroll_down)))
5216 struct position posit
5217 = *compute_motion (startpos, 0, 0, 0,
5218 PT, ht, 0,
5219 -1, XINT (w->hscroll),
5220 0, w);
5221 window_scroll_preserve_vpos = posit.vpos;
5222 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
5225 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5226 make_number (window_scroll_preserve_vpos));
5229 XSETFASTINT (tem, PT);
5230 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5232 if (NILP (tem))
5234 Fvertical_motion (make_number (- (ht / 2)), window);
5235 startpos = PT;
5238 SET_PT (startpos);
5239 lose = n < 0 && PT == BEGV;
5240 Fvertical_motion (make_number (n), window);
5241 pos = PT;
5242 pos_byte = PT_BYTE;
5243 bolp = Fbolp ();
5244 SET_PT_BOTH (opoint, opoint_byte);
5246 if (lose)
5248 if (noerror)
5249 return;
5250 else
5251 xsignal0 (Qbeginning_of_buffer);
5254 if (pos < ZV)
5256 int this_scroll_margin = scroll_margin;
5258 /* Don't use a scroll margin that is negative or too large. */
5259 if (this_scroll_margin < 0)
5260 this_scroll_margin = 0;
5262 if (XINT (w->total_lines) < 4 * scroll_margin)
5263 this_scroll_margin = XINT (w->total_lines) / 4;
5265 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5266 w->start_at_line_beg = bolp;
5267 w->update_mode_line = Qt;
5268 XSETFASTINT (w->last_modified, 0);
5269 XSETFASTINT (w->last_overlay_modified, 0);
5270 /* Set force_start so that redisplay_window will run
5271 the window-scroll-functions. */
5272 w->force_start = Qt;
5274 if (!NILP (Vscroll_preserve_screen_position)
5275 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5277 SET_PT_BOTH (pos, pos_byte);
5278 Fvertical_motion (original_pos, window);
5280 /* If we scrolled forward, put point enough lines down
5281 that it is outside the scroll margin. */
5282 else if (n > 0)
5284 int top_margin;
5286 if (this_scroll_margin > 0)
5288 SET_PT_BOTH (pos, pos_byte);
5289 Fvertical_motion (make_number (this_scroll_margin), window);
5290 top_margin = PT;
5292 else
5293 top_margin = pos;
5295 if (top_margin <= opoint)
5296 SET_PT_BOTH (opoint, opoint_byte);
5297 else if (!NILP (Vscroll_preserve_screen_position))
5299 SET_PT_BOTH (pos, pos_byte);
5300 Fvertical_motion (original_pos, window);
5302 else
5303 SET_PT (top_margin);
5305 else if (n < 0)
5307 int bottom_margin;
5309 /* If we scrolled backward, put point near the end of the window
5310 but not within the scroll margin. */
5311 SET_PT_BOTH (pos, pos_byte);
5312 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5313 if (XFASTINT (tem) == ht - this_scroll_margin)
5314 bottom_margin = PT;
5315 else
5316 bottom_margin = PT + 1;
5318 if (bottom_margin > opoint)
5319 SET_PT_BOTH (opoint, opoint_byte);
5320 else
5322 if (!NILP (Vscroll_preserve_screen_position))
5324 SET_PT_BOTH (pos, pos_byte);
5325 Fvertical_motion (original_pos, window);
5327 else
5328 Fvertical_motion (make_number (-1), window);
5332 else
5334 if (noerror)
5335 return;
5336 else
5337 xsignal0 (Qend_of_buffer);
5342 /* Scroll selected_window up or down. If N is nil, scroll a
5343 screen-full which is defined as the height of the window minus
5344 next_screen_context_lines. If N is the symbol `-', scroll.
5345 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5346 up. This is the guts of Fscroll_up and Fscroll_down. */
5348 static void
5349 scroll_command (n, direction)
5350 Lisp_Object n;
5351 int direction;
5353 int count = SPECPDL_INDEX ();
5355 xassert (eabs (direction) == 1);
5357 /* If selected window's buffer isn't current, make it current for
5358 the moment. But don't screw up if window_scroll gets an error. */
5359 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5361 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5362 Fset_buffer (XWINDOW (selected_window)->buffer);
5364 /* Make redisplay consider other windows than just selected_window. */
5365 ++windows_or_buffers_changed;
5368 if (NILP (n))
5369 window_scroll (selected_window, direction, 1, 0);
5370 else if (EQ (n, Qminus))
5371 window_scroll (selected_window, -direction, 1, 0);
5372 else
5374 n = Fprefix_numeric_value (n);
5375 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5378 unbind_to (count, Qnil);
5381 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5382 doc: /* Scroll text of selected window upward ARG lines.
5383 If ARG is omitted or nil, scroll upward by a near full screen.
5384 A near full screen is `next-screen-context-lines' less than a full screen.
5385 Negative ARG means scroll downward.
5386 If ARG is the atom `-', scroll downward by nearly full screen.
5387 When calling from a program, supply as argument a number, nil, or `-'. */)
5388 (arg)
5389 Lisp_Object arg;
5391 scroll_command (arg, 1);
5392 return Qnil;
5395 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5396 doc: /* Scroll text of selected window down ARG lines.
5397 If ARG is omitted or nil, scroll down by a near full screen.
5398 A near full screen is `next-screen-context-lines' less than a full screen.
5399 Negative ARG means scroll upward.
5400 If ARG is the atom `-', scroll upward by nearly full screen.
5401 When calling from a program, supply as argument a number, nil, or `-'. */)
5402 (arg)
5403 Lisp_Object arg;
5405 scroll_command (arg, -1);
5406 return Qnil;
5409 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5410 doc: /* Return the other window for \"other window scroll\" commands.
5411 If `other-window-scroll-buffer' is non-nil, a window
5412 showing that buffer is used.
5413 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5414 specifies the window. This takes precedence over
5415 `other-window-scroll-buffer'. */)
5418 Lisp_Object window;
5420 if (MINI_WINDOW_P (XWINDOW (selected_window))
5421 && !NILP (Vminibuf_scroll_window))
5422 window = Vminibuf_scroll_window;
5423 /* If buffer is specified, scroll that buffer. */
5424 else if (!NILP (Vother_window_scroll_buffer))
5426 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5427 if (NILP (window))
5428 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5430 else
5432 /* Nothing specified; look for a neighboring window on the same
5433 frame. */
5434 window = Fnext_window (selected_window, Qnil, Qnil);
5436 if (EQ (window, selected_window))
5437 /* That didn't get us anywhere; look for a window on another
5438 visible frame. */
5440 window = Fnext_window (window, Qnil, Qt);
5441 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5442 && ! EQ (window, selected_window));
5445 CHECK_LIVE_WINDOW (window);
5447 if (EQ (window, selected_window))
5448 error ("There is no other window");
5450 return window;
5453 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5454 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5455 A near full screen is `next-screen-context-lines' less than a full screen.
5456 The next window is the one below the current one; or the one at the top
5457 if the current one is at the bottom. Negative ARG means scroll downward.
5458 If ARG is the atom `-', scroll downward by nearly full screen.
5459 When calling from a program, supply as argument a number, nil, or `-'.
5461 If `other-window-scroll-buffer' is non-nil, scroll the window
5462 showing that buffer, popping the buffer up if necessary.
5463 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5464 specifies the window to scroll. This takes precedence over
5465 `other-window-scroll-buffer'. */)
5466 (arg)
5467 Lisp_Object arg;
5469 Lisp_Object window;
5470 struct window *w;
5471 int count = SPECPDL_INDEX ();
5473 window = Fother_window_for_scrolling ();
5474 w = XWINDOW (window);
5476 /* Don't screw up if window_scroll gets an error. */
5477 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5478 ++windows_or_buffers_changed;
5480 Fset_buffer (w->buffer);
5481 SET_PT (marker_position (w->pointm));
5483 if (NILP (arg))
5484 window_scroll (window, 1, 1, 1);
5485 else if (EQ (arg, Qminus))
5486 window_scroll (window, -1, 1, 1);
5487 else
5489 if (CONSP (arg))
5490 arg = Fcar (arg);
5491 CHECK_NUMBER (arg);
5492 window_scroll (window, XINT (arg), 0, 1);
5495 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5496 unbind_to (count, Qnil);
5498 return Qnil;
5501 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5502 doc: /* Scroll selected window display ARG columns left.
5503 Default for ARG is window width minus 2.
5504 Value is the total amount of leftward horizontal scrolling in
5505 effect after the change.
5506 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5507 lower bound for automatic scrolling, i.e. automatic scrolling
5508 will not scroll a window to a column less than the value returned
5509 by this function. This happens in an interactive call. */)
5510 (arg, set_minimum)
5511 register Lisp_Object arg, set_minimum;
5513 Lisp_Object result;
5514 int hscroll;
5515 struct window *w = XWINDOW (selected_window);
5517 if (NILP (arg))
5518 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5519 else
5520 arg = Fprefix_numeric_value (arg);
5522 hscroll = XINT (w->hscroll) + XINT (arg);
5523 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5525 if (!NILP (set_minimum))
5526 w->min_hscroll = w->hscroll;
5528 return result;
5531 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5532 doc: /* Scroll selected window display ARG columns right.
5533 Default for ARG is window width minus 2.
5534 Value is the total amount of leftward horizontal scrolling in
5535 effect after the change.
5536 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5537 lower bound for automatic scrolling, i.e. automatic scrolling
5538 will not scroll a window to a column less than the value returned
5539 by this function. This happens in an interactive call. */)
5540 (arg, set_minimum)
5541 register Lisp_Object arg, set_minimum;
5543 Lisp_Object result;
5544 int hscroll;
5545 struct window *w = XWINDOW (selected_window);
5547 if (NILP (arg))
5548 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5549 else
5550 arg = Fprefix_numeric_value (arg);
5552 hscroll = XINT (w->hscroll) - XINT (arg);
5553 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5555 if (!NILP (set_minimum))
5556 w->min_hscroll = w->hscroll;
5558 return result;
5561 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5562 doc: /* Return the window which was selected when entering the minibuffer.
5563 Returns nil, if selected window is not a minibuffer window. */)
5566 if (minibuf_level > 0
5567 && MINI_WINDOW_P (XWINDOW (selected_window))
5568 && WINDOW_LIVE_P (minibuf_selected_window))
5569 return minibuf_selected_window;
5571 return Qnil;
5574 /* Value is the number of lines actually displayed in window W,
5575 as opposed to its height. */
5577 static int
5578 displayed_window_lines (w)
5579 struct window *w;
5581 struct it it;
5582 struct text_pos start;
5583 int height = window_box_height (w);
5584 struct buffer *old_buffer;
5585 int bottom_y;
5587 if (XBUFFER (w->buffer) != current_buffer)
5589 old_buffer = current_buffer;
5590 set_buffer_internal (XBUFFER (w->buffer));
5592 else
5593 old_buffer = NULL;
5595 /* In case W->start is out of the accessible range, do something
5596 reasonable. This happens in Info mode when Info-scroll-down
5597 calls (recenter -1) while W->start is 1. */
5598 if (XMARKER (w->start)->charpos < BEGV)
5599 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5600 else if (XMARKER (w->start)->charpos > ZV)
5601 SET_TEXT_POS (start, ZV, ZV_BYTE);
5602 else
5603 SET_TEXT_POS_FROM_MARKER (start, w->start);
5605 start_display (&it, w, start);
5606 move_it_vertically (&it, height);
5607 bottom_y = line_bottom_y (&it);
5609 /* rms: On a non-window display,
5610 the value of it.vpos at the bottom of the screen
5611 seems to be 1 larger than window_box_height (w).
5612 This kludge fixes a bug whereby (move-to-window-line -1)
5613 when ZV is on the last screen line
5614 moves to the previous screen line instead of the last one. */
5615 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5616 height++;
5618 /* Add in empty lines at the bottom of the window. */
5619 if (bottom_y < height)
5621 int uy = FRAME_LINE_HEIGHT (it.f);
5622 it.vpos += (height - bottom_y + uy - 1) / uy;
5625 if (old_buffer)
5626 set_buffer_internal (old_buffer);
5628 return it.vpos;
5632 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5633 doc: /* Center point in selected window and maybe redisplay frame.
5634 With prefix argument ARG, recenter putting point on screen line ARG
5635 relative to the selected window. If ARG is negative, it counts up from the
5636 bottom of the window. (ARG should be less than the height of the window.)
5638 If ARG is omitted or nil, then recenter with point on the middle line of
5639 the selected window; if the variable `recenter-redisplay' is non-nil,
5640 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5641 is set to `grow-only', this resets the tool-bar's height to the minimum
5642 height needed); if `recenter-redisplay' has the special value `tty',
5643 then only tty frame are redrawn.
5645 Just C-u as prefix means put point in the center of the window
5646 and redisplay normally--don't erase and redraw the frame. */)
5647 (arg)
5648 register Lisp_Object arg;
5650 struct window *w = XWINDOW (selected_window);
5651 struct buffer *buf = XBUFFER (w->buffer);
5652 struct buffer *obuf = current_buffer;
5653 int center_p = 0;
5654 int charpos, bytepos;
5655 int iarg;
5656 int this_scroll_margin;
5658 /* If redisplay is suppressed due to an error, try again. */
5659 obuf->display_error_modiff = 0;
5661 if (NILP (arg))
5663 if (!NILP (Vrecenter_redisplay)
5664 && (!EQ (Vrecenter_redisplay, Qtty)
5665 || !NILP (Ftty_type (selected_frame))))
5667 int i;
5669 /* Invalidate pixel data calculated for all compositions. */
5670 for (i = 0; i < n_compositions; i++)
5671 composition_table[i]->font = NULL;
5673 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5675 Fredraw_frame (WINDOW_FRAME (w));
5676 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5679 center_p = 1;
5681 else if (CONSP (arg)) /* Just C-u. */
5682 center_p = 1;
5683 else
5685 arg = Fprefix_numeric_value (arg);
5686 CHECK_NUMBER (arg);
5687 iarg = XINT (arg);
5690 set_buffer_internal (buf);
5692 /* Do this after making BUF current
5693 in case scroll_margin is buffer-local. */
5694 this_scroll_margin = max (0, scroll_margin);
5695 this_scroll_margin = min (this_scroll_margin,
5696 XFASTINT (w->total_lines) / 4);
5698 /* Handle centering on a graphical frame specially. Such frames can
5699 have variable-height lines and centering point on the basis of
5700 line counts would lead to strange effects. */
5701 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5703 if (center_p)
5705 struct it it;
5706 struct text_pos pt;
5708 SET_TEXT_POS (pt, PT, PT_BYTE);
5709 start_display (&it, w, pt);
5710 move_it_vertically_backward (&it, window_box_height (w) / 2);
5711 charpos = IT_CHARPOS (it);
5712 bytepos = IT_BYTEPOS (it);
5714 else if (iarg < 0)
5716 struct it it;
5717 struct text_pos pt;
5718 int nlines = -iarg;
5719 int extra_line_spacing;
5720 int h = window_box_height (w);
5722 iarg = - max (-iarg, this_scroll_margin);
5724 SET_TEXT_POS (pt, PT, PT_BYTE);
5725 start_display (&it, w, pt);
5727 /* Be sure we have the exact height of the full line containing PT. */
5728 move_it_by_lines (&it, 0, 1);
5730 /* The amount of pixels we have to move back is the window
5731 height minus what's displayed in the line containing PT,
5732 and the lines below. */
5733 it.current_y = 0;
5734 it.vpos = 0;
5735 move_it_by_lines (&it, nlines, 1);
5737 if (it.vpos == nlines)
5738 h -= it.current_y;
5739 else
5741 /* Last line has no newline */
5742 h -= line_bottom_y (&it);
5743 it.vpos++;
5746 /* Don't reserve space for extra line spacing of last line. */
5747 extra_line_spacing = it.max_extra_line_spacing;
5749 /* If we can't move down NLINES lines because we hit
5750 the end of the buffer, count in some empty lines. */
5751 if (it.vpos < nlines)
5753 nlines -= it.vpos;
5754 extra_line_spacing = it.extra_line_spacing;
5755 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5757 if (h <= 0)
5758 return Qnil;
5760 /* Now find the new top line (starting position) of the window. */
5761 start_display (&it, w, pt);
5762 it.current_y = 0;
5763 move_it_vertically_backward (&it, h);
5765 /* If extra line spacing is present, we may move too far
5766 back. This causes the last line to be only partially
5767 visible (which triggers redisplay to recenter that line
5768 in the middle), so move forward.
5769 But ignore extra line spacing on last line, as it is not
5770 considered to be part of the visible height of the line.
5772 h += extra_line_spacing;
5773 while (-it.current_y > h)
5774 move_it_by_lines (&it, 1, 1);
5776 charpos = IT_CHARPOS (it);
5777 bytepos = IT_BYTEPOS (it);
5779 else
5781 struct position pos;
5783 iarg = max (iarg, this_scroll_margin);
5785 pos = *vmotion (PT, -iarg, w);
5786 charpos = pos.bufpos;
5787 bytepos = pos.bytepos;
5790 else
5792 struct position pos;
5793 int ht = window_internal_height (w);
5795 if (center_p)
5796 iarg = ht / 2;
5797 else if (iarg < 0)
5798 iarg += ht;
5800 /* Don't let it get into the margin at either top or bottom. */
5801 iarg = max (iarg, this_scroll_margin);
5802 iarg = min (iarg, ht - this_scroll_margin - 1);
5804 pos = *vmotion (PT, - iarg, w);
5805 charpos = pos.bufpos;
5806 bytepos = pos.bytepos;
5809 /* Set the new window start. */
5810 set_marker_both (w->start, w->buffer, charpos, bytepos);
5811 w->window_end_valid = Qnil;
5813 w->optional_new_start = Qt;
5815 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5816 w->start_at_line_beg = Qt;
5817 else
5818 w->start_at_line_beg = Qnil;
5820 set_buffer_internal (obuf);
5821 return Qnil;
5825 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5826 0, 1, 0,
5827 doc: /* Return the height in lines of the text display area of WINDOW.
5828 WINDOW defaults to the selected window.
5830 The return value does not include the mode line, any header line, nor
5831 any partial-height lines in the text display area. */)
5832 (window)
5833 Lisp_Object window;
5835 struct window *w = decode_window (window);
5836 int pixel_height = window_box_height (w);
5837 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5838 return make_number (line_height);
5843 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5844 1, 1, "P",
5845 doc: /* Position point relative to window.
5846 With no argument, position point at center of window.
5847 An argument specifies vertical position within the window;
5848 zero means top of window, negative means relative to bottom of window. */)
5849 (arg)
5850 Lisp_Object arg;
5852 struct window *w = XWINDOW (selected_window);
5853 int lines, start;
5854 Lisp_Object window;
5855 #if 0
5856 int this_scroll_margin;
5857 #endif
5859 window = selected_window;
5860 start = marker_position (w->start);
5861 if (start < BEGV || start > ZV)
5863 int height = window_internal_height (w);
5864 Fvertical_motion (make_number (- (height / 2)), window);
5865 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5866 w->start_at_line_beg = Fbolp ();
5867 w->force_start = Qt;
5869 else
5870 Fgoto_char (w->start);
5872 lines = displayed_window_lines (w);
5874 #if 0
5875 this_scroll_margin = max (0, scroll_margin);
5876 this_scroll_margin = min (this_scroll_margin, lines / 4);
5877 #endif
5879 if (NILP (arg))
5880 XSETFASTINT (arg, lines / 2);
5881 else
5883 int iarg = XINT (Fprefix_numeric_value (arg));
5885 if (iarg < 0)
5886 iarg = iarg + lines;
5888 #if 0 /* This code would prevent move-to-window-line from moving point
5889 to a place inside the scroll margins (which would cause the
5890 next redisplay to scroll). I wrote this code, but then concluded
5891 it is probably better not to install it. However, it is here
5892 inside #if 0 so as not to lose it. -- rms. */
5894 /* Don't let it get into the margin at either top or bottom. */
5895 iarg = max (iarg, this_scroll_margin);
5896 iarg = min (iarg, lines - this_scroll_margin - 1);
5897 #endif
5899 arg = make_number (iarg);
5902 /* Skip past a partially visible first line. */
5903 if (w->vscroll)
5904 XSETINT (arg, XINT (arg) + 1);
5906 return Fvertical_motion (arg, window);
5911 /***********************************************************************
5912 Window Configuration
5913 ***********************************************************************/
5915 struct save_window_data
5917 EMACS_UINT size;
5918 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5919 Lisp_Object selected_frame;
5920 Lisp_Object current_window;
5921 Lisp_Object current_buffer;
5922 Lisp_Object minibuf_scroll_window;
5923 Lisp_Object minibuf_selected_window;
5924 Lisp_Object root_window;
5925 Lisp_Object focus_frame;
5926 /* A vector, each of whose elements is a struct saved_window
5927 for one window. */
5928 Lisp_Object saved_windows;
5930 /* All fields above are traced by the GC.
5931 From `fame-cols' down, the fields are ignored by the GC. */
5933 int frame_cols, frame_lines, frame_menu_bar_lines;
5934 int frame_tool_bar_lines;
5937 /* This is saved as a Lisp_Vector */
5938 struct saved_window
5940 /* these first two must agree with struct Lisp_Vector in lisp.h */
5941 EMACS_UINT size;
5942 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5944 Lisp_Object window;
5945 Lisp_Object buffer, start, pointm, mark;
5946 Lisp_Object left_col, top_line, total_cols, total_lines;
5947 Lisp_Object hscroll, min_hscroll;
5948 Lisp_Object parent, prev;
5949 Lisp_Object start_at_line_beg;
5950 Lisp_Object display_table;
5951 Lisp_Object orig_top_line, orig_total_lines;
5952 Lisp_Object left_margin_cols, right_margin_cols;
5953 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5954 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5955 Lisp_Object dedicated, resize_proportionally;
5958 #define SAVED_WINDOW_N(swv,n) \
5959 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5961 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5962 doc: /* Return t if OBJECT is a window-configuration object. */)
5963 (object)
5964 Lisp_Object object;
5966 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5969 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5970 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5971 (config)
5972 Lisp_Object config;
5974 register struct save_window_data *data;
5975 struct Lisp_Vector *saved_windows;
5977 CHECK_WINDOW_CONFIGURATION (config);
5979 data = (struct save_window_data *) XVECTOR (config);
5980 saved_windows = XVECTOR (data->saved_windows);
5981 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5984 DEFUN ("set-window-configuration", Fset_window_configuration,
5985 Sset_window_configuration, 1, 1, 0,
5986 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5987 CONFIGURATION must be a value previously returned
5988 by `current-window-configuration' (which see).
5989 If CONFIGURATION was made from a frame that is now deleted,
5990 only frame-independent values can be restored. In this case,
5991 the return value is nil. Otherwise the value is t. */)
5992 (configuration)
5993 Lisp_Object configuration;
5995 register struct save_window_data *data;
5996 struct Lisp_Vector *saved_windows;
5997 Lisp_Object new_current_buffer;
5998 Lisp_Object frame;
5999 FRAME_PTR f;
6000 int old_point = -1;
6002 CHECK_WINDOW_CONFIGURATION (configuration);
6004 data = (struct save_window_data *) XVECTOR (configuration);
6005 saved_windows = XVECTOR (data->saved_windows);
6007 new_current_buffer = data->current_buffer;
6008 if (NILP (XBUFFER (new_current_buffer)->name))
6009 new_current_buffer = Qnil;
6010 else
6012 if (XBUFFER (new_current_buffer) == current_buffer)
6013 /* The code further down "preserves point" by saving here PT in
6014 old_point and then setting it later back into PT. When the
6015 current-selected-window and the final-selected-window both show
6016 the current buffer, this suffers from the problem that the
6017 current PT is the window-point of the current-selected-window,
6018 while the final PT is the point of the final-selected-window, so
6019 this copy from one PT to the other would end up moving the
6020 window-point of the final-selected-window to the window-point of
6021 the current-selected-window. So we have to be careful which
6022 point of the current-buffer we copy into old_point. */
6023 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6024 && WINDOWP (selected_window)
6025 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
6026 && !EQ (selected_window, data->current_window))
6027 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6028 else
6029 old_point = PT;
6030 else
6031 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6032 point in new_current_buffer as of the last time this buffer was
6033 used. This can be non-deterministic since it can be changed by
6034 things like jit-lock by mere temporary selection of some random
6035 window that happens to show this buffer.
6036 So if possible we want this arbitrary choice of "which point" to
6037 be the one from the to-be-selected-window so as to prevent this
6038 window's cursor from being copied from another window. */
6039 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6040 /* If current_window = selected_window, its point is in BUF_PT. */
6041 && !EQ (selected_window, data->current_window))
6042 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6043 else
6044 old_point = BUF_PT (XBUFFER (new_current_buffer));
6047 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6048 f = XFRAME (frame);
6050 /* If f is a dead frame, don't bother rebuilding its window tree.
6051 However, there is other stuff we should still try to do below. */
6052 if (FRAME_LIVE_P (f))
6054 register struct window *w;
6055 register struct saved_window *p;
6056 struct window *root_window;
6057 struct window **leaf_windows;
6058 int n_leaf_windows;
6059 int k, i, n;
6061 /* If the frame has been resized since this window configuration was
6062 made, we change the frame to the size specified in the
6063 configuration, restore the configuration, and then resize it
6064 back. We keep track of the prevailing height in these variables. */
6065 int previous_frame_lines = FRAME_LINES (f);
6066 int previous_frame_cols = FRAME_COLS (f);
6067 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6068 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6070 /* The mouse highlighting code could get screwed up
6071 if it runs during this. */
6072 BLOCK_INPUT;
6074 if (data->frame_lines != previous_frame_lines
6075 || data->frame_cols != previous_frame_cols)
6076 change_frame_size (f, data->frame_lines,
6077 data->frame_cols, 0, 0, 0);
6078 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6079 if (data->frame_menu_bar_lines
6080 != previous_frame_menu_bar_lines)
6081 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6082 make_number (0));
6083 #ifdef HAVE_WINDOW_SYSTEM
6084 if (data->frame_tool_bar_lines
6085 != previous_frame_tool_bar_lines)
6086 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6087 make_number (0));
6088 #endif
6089 #endif
6091 /* "Swap out" point from the selected window's buffer
6092 into the window itself. (Normally the pointm of the selected
6093 window holds garbage.) We do this now, before
6094 restoring the window contents, and prevent it from
6095 being done later on when we select a new window. */
6096 if (! NILP (XWINDOW (selected_window)->buffer))
6098 w = XWINDOW (selected_window);
6099 set_marker_both (w->pointm,
6100 w->buffer,
6101 BUF_PT (XBUFFER (w->buffer)),
6102 BUF_PT_BYTE (XBUFFER (w->buffer)));
6105 windows_or_buffers_changed++;
6106 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6108 /* Problem: Freeing all matrices and later allocating them again
6109 is a serious redisplay flickering problem. What we would
6110 really like to do is to free only those matrices not reused
6111 below. */
6112 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6113 leaf_windows
6114 = (struct window **) alloca (count_windows (root_window)
6115 * sizeof (struct window *));
6116 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6118 /* Kludge Alert!
6119 Mark all windows now on frame as "deleted".
6120 Restoring the new configuration "undeletes" any that are in it.
6122 Save their current buffers in their height fields, since we may
6123 need it later, if a buffer saved in the configuration is now
6124 dead. */
6125 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6127 for (k = 0; k < saved_windows->size; k++)
6129 p = SAVED_WINDOW_N (saved_windows, k);
6130 w = XWINDOW (p->window);
6131 w->next = Qnil;
6133 if (!NILP (p->parent))
6134 w->parent = SAVED_WINDOW_N (saved_windows,
6135 XFASTINT (p->parent))->window;
6136 else
6137 w->parent = Qnil;
6139 if (!NILP (p->prev))
6141 w->prev = SAVED_WINDOW_N (saved_windows,
6142 XFASTINT (p->prev))->window;
6143 XWINDOW (w->prev)->next = p->window;
6145 else
6147 w->prev = Qnil;
6148 if (!NILP (w->parent))
6150 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6152 XWINDOW (w->parent)->vchild = p->window;
6153 XWINDOW (w->parent)->hchild = Qnil;
6155 else
6157 XWINDOW (w->parent)->hchild = p->window;
6158 XWINDOW (w->parent)->vchild = Qnil;
6163 /* If we squirreled away the buffer in the window's height,
6164 restore it now. */
6165 if (BUFFERP (w->total_lines))
6166 w->buffer = w->total_lines;
6167 w->left_col = p->left_col;
6168 w->top_line = p->top_line;
6169 w->total_cols = p->total_cols;
6170 w->total_lines = p->total_lines;
6171 w->hscroll = p->hscroll;
6172 w->min_hscroll = p->min_hscroll;
6173 w->display_table = p->display_table;
6174 w->orig_top_line = p->orig_top_line;
6175 w->orig_total_lines = p->orig_total_lines;
6176 w->left_margin_cols = p->left_margin_cols;
6177 w->right_margin_cols = p->right_margin_cols;
6178 w->left_fringe_width = p->left_fringe_width;
6179 w->right_fringe_width = p->right_fringe_width;
6180 w->fringes_outside_margins = p->fringes_outside_margins;
6181 w->scroll_bar_width = p->scroll_bar_width;
6182 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6183 w->dedicated = p->dedicated;
6184 w->resize_proportionally = p->resize_proportionally;
6185 XSETFASTINT (w->last_modified, 0);
6186 XSETFASTINT (w->last_overlay_modified, 0);
6188 /* Reinstall the saved buffer and pointers into it. */
6189 if (NILP (p->buffer))
6190 w->buffer = p->buffer;
6191 else
6193 if (!NILP (XBUFFER (p->buffer)->name))
6194 /* If saved buffer is alive, install it. */
6196 w->buffer = p->buffer;
6197 w->start_at_line_beg = p->start_at_line_beg;
6198 set_marker_restricted (w->start, p->start, w->buffer);
6199 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6200 Fset_marker (XBUFFER (w->buffer)->mark,
6201 p->mark, w->buffer);
6203 /* As documented in Fcurrent_window_configuration, don't
6204 restore the location of point in the buffer which was
6205 current when the window configuration was recorded. */
6206 if (!EQ (p->buffer, new_current_buffer)
6207 && XBUFFER (p->buffer) == current_buffer)
6208 Fgoto_char (w->pointm);
6210 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
6211 /* Else unless window has a live buffer, get one. */
6213 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6214 /* This will set the markers to beginning of visible
6215 range. */
6216 set_marker_restricted (w->start, make_number (0), w->buffer);
6217 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6218 w->start_at_line_beg = Qt;
6220 else
6221 /* Keeping window's old buffer; make sure the markers
6222 are real. */
6224 /* Set window markers at start of visible range. */
6225 if (XMARKER (w->start)->buffer == 0)
6226 set_marker_restricted (w->start, make_number (0),
6227 w->buffer);
6228 if (XMARKER (w->pointm)->buffer == 0)
6229 set_marker_restricted_both (w->pointm, w->buffer,
6230 BUF_PT (XBUFFER (w->buffer)),
6231 BUF_PT_BYTE (XBUFFER (w->buffer)));
6232 w->start_at_line_beg = Qt;
6237 FRAME_ROOT_WINDOW (f) = data->root_window;
6238 /* Prevent "swapping out point" in the old selected window
6239 using the buffer that has been restored into it.
6240 We already swapped out point that from that window's old buffer. */
6241 selected_window = Qnil;
6243 /* Arrange *not* to restore point in the buffer that was
6244 current when the window configuration was saved. */
6245 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6246 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6247 make_number (old_point),
6248 XWINDOW (data->current_window)->buffer);
6250 Fselect_window (data->current_window, Qnil);
6251 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
6252 = selected_window;
6254 if (NILP (data->focus_frame)
6255 || (FRAMEP (data->focus_frame)
6256 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6257 Fredirect_frame_focus (frame, data->focus_frame);
6259 #if 0 /* I don't understand why this is needed, and it causes problems
6260 when the frame's old selected window has been deleted. */
6261 if (f != selected_frame && FRAME_WINDOW_P (f))
6262 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
6263 0, 0, Qnil);
6264 #endif
6266 /* Set the screen height to the value it had before this function. */
6267 if (previous_frame_lines != FRAME_LINES (f)
6268 || previous_frame_cols != FRAME_COLS (f))
6269 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6270 0, 0, 0);
6271 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6272 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6273 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6274 make_number (0));
6275 #ifdef HAVE_WINDOW_SYSTEM
6276 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6277 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6278 make_number (0));
6279 #endif
6280 #endif
6282 /* Now, free glyph matrices in windows that were not reused. */
6283 for (i = n = 0; i < n_leaf_windows; ++i)
6285 if (NILP (leaf_windows[i]->buffer))
6287 /* Assert it's not reused as a combination. */
6288 xassert (NILP (leaf_windows[i]->hchild)
6289 && NILP (leaf_windows[i]->vchild));
6290 free_window_matrices (leaf_windows[i]);
6292 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6293 ++n;
6296 adjust_glyphs (f);
6298 UNBLOCK_INPUT;
6300 /* Fselect_window will have made f the selected frame, so we
6301 reselect the proper frame here. Fhandle_switch_frame will change the
6302 selected window too, but that doesn't make the call to
6303 Fselect_window above totally superfluous; it still sets f's
6304 selected window. */
6305 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6306 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6308 run_window_configuration_change_hook (f);
6311 if (!NILP (new_current_buffer))
6312 Fset_buffer (new_current_buffer);
6314 Vminibuf_scroll_window = data->minibuf_scroll_window;
6315 minibuf_selected_window = data->minibuf_selected_window;
6317 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6320 /* Mark all windows now on frame as deleted
6321 by setting their buffers to nil. */
6323 void
6324 delete_all_subwindows (w)
6325 register struct window *w;
6327 if (!NILP (w->next))
6328 delete_all_subwindows (XWINDOW (w->next));
6329 if (!NILP (w->vchild))
6330 delete_all_subwindows (XWINDOW (w->vchild));
6331 if (!NILP (w->hchild))
6332 delete_all_subwindows (XWINDOW (w->hchild));
6334 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6336 if (!NILP (w->buffer))
6337 unshow_buffer (w);
6339 /* We set all three of these fields to nil, to make sure that we can
6340 distinguish this dead window from any live window. Live leaf
6341 windows will have buffer set, and combination windows will have
6342 vchild or hchild set. */
6343 w->buffer = Qnil;
6344 w->vchild = Qnil;
6345 w->hchild = Qnil;
6347 Vwindow_list = Qnil;
6350 static int
6351 count_windows (window)
6352 register struct window *window;
6354 register int count = 1;
6355 if (!NILP (window->next))
6356 count += count_windows (XWINDOW (window->next));
6357 if (!NILP (window->vchild))
6358 count += count_windows (XWINDOW (window->vchild));
6359 if (!NILP (window->hchild))
6360 count += count_windows (XWINDOW (window->hchild));
6361 return count;
6365 /* Fill vector FLAT with leaf windows under W, starting at index I.
6366 Value is last index + 1. */
6368 static int
6369 get_leaf_windows (w, flat, i)
6370 struct window *w;
6371 struct window **flat;
6372 int i;
6374 while (w)
6376 if (!NILP (w->hchild))
6377 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6378 else if (!NILP (w->vchild))
6379 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6380 else
6381 flat[i++] = w;
6383 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6386 return i;
6390 /* Return a pointer to the glyph W's physical cursor is on. Value is
6391 null if W's current matrix is invalid, so that no meaningfull glyph
6392 can be returned. */
6394 struct glyph *
6395 get_phys_cursor_glyph (w)
6396 struct window *w;
6398 struct glyph_row *row;
6399 struct glyph *glyph;
6401 if (w->phys_cursor.vpos >= 0
6402 && w->phys_cursor.vpos < w->current_matrix->nrows
6403 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6404 row->enabled_p)
6405 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6406 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6407 else
6408 glyph = NULL;
6410 return glyph;
6414 static int
6415 save_window_save (window, vector, i)
6416 Lisp_Object window;
6417 struct Lisp_Vector *vector;
6418 int i;
6420 register struct saved_window *p;
6421 register struct window *w;
6422 register Lisp_Object tem;
6424 for (;!NILP (window); window = w->next)
6426 p = SAVED_WINDOW_N (vector, i);
6427 w = XWINDOW (window);
6429 XSETFASTINT (w->temslot, i); i++;
6430 p->window = window;
6431 p->buffer = w->buffer;
6432 p->left_col = w->left_col;
6433 p->top_line = w->top_line;
6434 p->total_cols = w->total_cols;
6435 p->total_lines = w->total_lines;
6436 p->hscroll = w->hscroll;
6437 p->min_hscroll = w->min_hscroll;
6438 p->display_table = w->display_table;
6439 p->orig_top_line = w->orig_top_line;
6440 p->orig_total_lines = w->orig_total_lines;
6441 p->left_margin_cols = w->left_margin_cols;
6442 p->right_margin_cols = w->right_margin_cols;
6443 p->left_fringe_width = w->left_fringe_width;
6444 p->right_fringe_width = w->right_fringe_width;
6445 p->fringes_outside_margins = w->fringes_outside_margins;
6446 p->scroll_bar_width = w->scroll_bar_width;
6447 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6448 p->dedicated = w->dedicated;
6449 p->resize_proportionally = w->resize_proportionally;
6450 if (!NILP (w->buffer))
6452 /* Save w's value of point in the window configuration.
6453 If w is the selected window, then get the value of point
6454 from the buffer; pointm is garbage in the selected window. */
6455 if (EQ (window, selected_window))
6457 p->pointm = Fmake_marker ();
6458 set_marker_both (p->pointm, w->buffer,
6459 BUF_PT (XBUFFER (w->buffer)),
6460 BUF_PT_BYTE (XBUFFER (w->buffer)));
6462 else
6463 p->pointm = Fcopy_marker (w->pointm, Qnil);
6465 p->start = Fcopy_marker (w->start, Qnil);
6466 p->start_at_line_beg = w->start_at_line_beg;
6468 tem = XBUFFER (w->buffer)->mark;
6469 p->mark = Fcopy_marker (tem, Qnil);
6471 else
6473 p->pointm = Qnil;
6474 p->start = Qnil;
6475 p->mark = Qnil;
6476 p->start_at_line_beg = Qnil;
6479 if (NILP (w->parent))
6480 p->parent = Qnil;
6481 else
6482 p->parent = XWINDOW (w->parent)->temslot;
6484 if (NILP (w->prev))
6485 p->prev = Qnil;
6486 else
6487 p->prev = XWINDOW (w->prev)->temslot;
6489 if (!NILP (w->vchild))
6490 i = save_window_save (w->vchild, vector, i);
6491 if (!NILP (w->hchild))
6492 i = save_window_save (w->hchild, vector, i);
6495 return i;
6498 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6499 Scurrent_window_configuration, 0, 1, 0,
6500 doc: /* Return an object representing the current window configuration of FRAME.
6501 If FRAME is nil or omitted, use the selected frame.
6502 This describes the number of windows, their sizes and current buffers,
6503 and for each displayed buffer, where display starts, and the positions of
6504 point and mark. An exception is made for point in the current buffer:
6505 its value is -not- saved.
6506 This also records the currently selected frame, and FRAME's focus
6507 redirection (see `redirect-frame-focus'). */)
6508 (frame)
6509 Lisp_Object frame;
6511 register Lisp_Object tem;
6512 register int n_windows;
6513 register struct save_window_data *data;
6514 register int i;
6515 FRAME_PTR f;
6517 if (NILP (frame))
6518 frame = selected_frame;
6519 CHECK_LIVE_FRAME (frame);
6520 f = XFRAME (frame);
6522 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6523 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6524 PVEC_WINDOW_CONFIGURATION);
6526 data->frame_cols = FRAME_COLS (f);
6527 data->frame_lines = FRAME_LINES (f);
6528 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6529 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6530 data->selected_frame = selected_frame;
6531 data->current_window = FRAME_SELECTED_WINDOW (f);
6532 XSETBUFFER (data->current_buffer, current_buffer);
6533 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6534 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6535 data->root_window = FRAME_ROOT_WINDOW (f);
6536 data->focus_frame = FRAME_FOCUS_FRAME (f);
6537 tem = Fmake_vector (make_number (n_windows), Qnil);
6538 data->saved_windows = tem;
6539 for (i = 0; i < n_windows; i++)
6540 XVECTOR (tem)->contents[i]
6541 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6542 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6543 XSETWINDOW_CONFIGURATION (tem, data);
6544 return (tem);
6547 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
6548 0, UNEVALLED, 0,
6549 doc: /* Execute BODY, preserving window sizes and contents.
6550 Return the value of the last form in BODY.
6551 Restore which buffer appears in which window, where display starts,
6552 and the value of point and mark for each window.
6553 Also restore the choice of selected window.
6554 Also restore which buffer is current.
6555 Does not restore the value of point in current buffer.
6556 usage: (save-window-excursion BODY...) */)
6557 (args)
6558 Lisp_Object args;
6560 register Lisp_Object val;
6561 register int count = SPECPDL_INDEX ();
6563 record_unwind_protect (Fset_window_configuration,
6564 Fcurrent_window_configuration (Qnil));
6565 val = Fprogn (args);
6566 return unbind_to (count, val);
6571 /***********************************************************************
6572 Window Split Tree
6573 ***********************************************************************/
6575 static Lisp_Object
6576 window_tree (w)
6577 struct window *w;
6579 Lisp_Object tail = Qnil;
6580 Lisp_Object result = Qnil;
6582 while (w)
6584 Lisp_Object wn;
6586 XSETWINDOW (wn, w);
6587 if (!NILP (w->hchild))
6588 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6589 window_tree (XWINDOW (w->hchild))));
6590 else if (!NILP (w->vchild))
6591 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6592 window_tree (XWINDOW (w->vchild))));
6594 if (NILP (result))
6596 result = tail = Fcons (wn, Qnil);
6598 else
6600 XSETCDR (tail, Fcons (wn, Qnil));
6601 tail = XCDR (tail);
6604 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6607 return result;
6612 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6613 0, 1, 0,
6614 doc: /* Return the window tree for frame FRAME.
6616 The return value is a list of the form (ROOT MINI), where ROOT
6617 represents the window tree of the frame's root window, and MINI
6618 is the frame's minibuffer window.
6620 If the root window is not split, ROOT is the root window itself.
6621 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6622 horizontal split, and t for a vertical split, EDGES gives the combined
6623 size and position of the subwindows in the split, and the rest of the
6624 elements are the subwindows in the split. Each of the subwindows may
6625 again be a window or a list representing a window split, and so on.
6626 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6628 If FRAME is nil or omitted, return information on the currently
6629 selected frame. */)
6630 (frame)
6631 Lisp_Object frame;
6633 FRAME_PTR f;
6635 if (NILP (frame))
6636 frame = selected_frame;
6638 CHECK_FRAME (frame);
6639 f = XFRAME (frame);
6641 if (!FRAME_LIVE_P (f))
6642 return Qnil;
6644 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6648 /***********************************************************************
6649 Marginal Areas
6650 ***********************************************************************/
6652 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6653 2, 3, 0,
6654 doc: /* Set width of marginal areas of window WINDOW.
6655 If WINDOW is nil, set margins of the currently selected window.
6656 Second arg LEFT-WIDTH specifies the number of character cells to
6657 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6658 does the same for the right marginal area. A nil width parameter
6659 means no margin. */)
6660 (window, left_width, right_width)
6661 Lisp_Object window, left_width, right_width;
6663 struct window *w = decode_window (window);
6665 /* Translate negative or zero widths to nil.
6666 Margins that are too wide have to be checked elsewhere. */
6668 if (!NILP (left_width))
6670 CHECK_NUMBER (left_width);
6671 if (XINT (left_width) <= 0)
6672 left_width = Qnil;
6675 if (!NILP (right_width))
6677 CHECK_NUMBER (right_width);
6678 if (XINT (right_width) <= 0)
6679 right_width = Qnil;
6682 if (!EQ (w->left_margin_cols, left_width)
6683 || !EQ (w->right_margin_cols, right_width))
6685 w->left_margin_cols = left_width;
6686 w->right_margin_cols = right_width;
6688 adjust_window_margins (w);
6690 ++windows_or_buffers_changed;
6691 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6694 return Qnil;
6698 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6699 0, 1, 0,
6700 doc: /* Get width of marginal areas of window WINDOW.
6701 If WINDOW is omitted or nil, use the currently selected window.
6702 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6703 If a marginal area does not exist, its width will be returned
6704 as nil. */)
6705 (window)
6706 Lisp_Object window;
6708 struct window *w = decode_window (window);
6709 return Fcons (w->left_margin_cols, w->right_margin_cols);
6714 /***********************************************************************
6715 Fringes
6716 ***********************************************************************/
6718 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6719 2, 4, 0,
6720 doc: /* Set the fringe widths of window WINDOW.
6721 If WINDOW is nil, set the fringe widths of the currently selected
6722 window.
6723 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6724 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6725 fringe width. If a fringe width arg is nil, that means to use the
6726 frame's default fringe width. Default fringe widths can be set with
6727 the command `set-fringe-style'.
6728 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6729 outside of the display margins. By default, fringes are drawn between
6730 display marginal areas and the text area. */)
6731 (window, left_width, right_width, outside_margins)
6732 Lisp_Object window, left_width, right_width, outside_margins;
6734 struct window *w = decode_window (window);
6736 if (!NILP (left_width))
6737 CHECK_NATNUM (left_width);
6738 if (!NILP (right_width))
6739 CHECK_NATNUM (right_width);
6741 /* Do nothing on a tty. */
6742 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6743 && (!EQ (w->left_fringe_width, left_width)
6744 || !EQ (w->right_fringe_width, right_width)
6745 || !EQ (w->fringes_outside_margins, outside_margins)))
6747 w->left_fringe_width = left_width;
6748 w->right_fringe_width = right_width;
6749 w->fringes_outside_margins = outside_margins;
6751 adjust_window_margins (w);
6753 clear_glyph_matrix (w->current_matrix);
6754 w->window_end_valid = Qnil;
6756 ++windows_or_buffers_changed;
6757 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6760 return Qnil;
6764 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6765 0, 1, 0,
6766 doc: /* Get width of fringes of window WINDOW.
6767 If WINDOW is omitted or nil, use the currently selected window.
6768 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6769 (window)
6770 Lisp_Object window;
6772 struct window *w = decode_window (window);
6774 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6775 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6776 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6777 ? Qt : Qnil), Qnil)));
6782 /***********************************************************************
6783 Scroll bars
6784 ***********************************************************************/
6786 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6787 2, 4, 0,
6788 doc: /* Set width and type of scroll bars of window WINDOW.
6789 If window is nil, set scroll bars of the currently selected window.
6790 Second parameter WIDTH specifies the pixel width for the scroll bar;
6791 this is automatically adjusted to a multiple of the frame column width.
6792 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6793 bar: left, right, or nil.
6794 If WIDTH is nil, use the frame's scroll-bar width.
6795 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6796 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6797 (window, width, vertical_type, horizontal_type)
6798 Lisp_Object window, width, vertical_type, horizontal_type;
6800 struct window *w = decode_window (window);
6802 if (!NILP (width))
6804 CHECK_NATNUM (width);
6806 if (XINT (width) == 0)
6807 vertical_type = Qnil;
6810 if (!(NILP (vertical_type)
6811 || EQ (vertical_type, Qleft)
6812 || EQ (vertical_type, Qright)
6813 || EQ (vertical_type, Qt)))
6814 error ("Invalid type of vertical scroll bar");
6816 if (!EQ (w->scroll_bar_width, width)
6817 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6819 w->scroll_bar_width = width;
6820 w->vertical_scroll_bar_type = vertical_type;
6822 adjust_window_margins (w);
6824 clear_glyph_matrix (w->current_matrix);
6825 w->window_end_valid = Qnil;
6827 ++windows_or_buffers_changed;
6828 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6831 return Qnil;
6835 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6836 0, 1, 0,
6837 doc: /* Get width and type of scroll bars of window WINDOW.
6838 If WINDOW is omitted or nil, use the currently selected window.
6839 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6840 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6841 value. */)
6842 (window)
6843 Lisp_Object window;
6845 struct window *w = decode_window (window);
6846 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6847 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6848 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6849 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6850 Fcons (w->vertical_scroll_bar_type,
6851 Fcons (Qnil, Qnil))));
6856 /***********************************************************************
6857 Smooth scrolling
6858 ***********************************************************************/
6860 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6861 doc: /* Return the amount by which WINDOW is scrolled vertically.
6862 Use the selected window if WINDOW is nil or omitted.
6863 Normally, value is a multiple of the canonical character height of WINDOW;
6864 optional second arg PIXELS-P means value is measured in pixels. */)
6865 (window, pixels_p)
6866 Lisp_Object window, pixels_p;
6868 Lisp_Object result;
6869 struct frame *f;
6870 struct window *w;
6872 if (NILP (window))
6873 window = selected_window;
6874 else
6875 CHECK_WINDOW (window);
6876 w = XWINDOW (window);
6877 f = XFRAME (w->frame);
6879 if (FRAME_WINDOW_P (f))
6880 result = (NILP (pixels_p)
6881 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6882 : make_number (-w->vscroll));
6883 else
6884 result = make_number (0);
6885 return result;
6889 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6890 2, 3, 0,
6891 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6892 WINDOW nil means use the selected window. Normally, VSCROLL is a
6893 non-negative multiple of the canonical character height of WINDOW;
6894 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6895 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6896 corresponds to an integral number of pixels. The return value is the
6897 result of this rounding.
6898 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6899 (window, vscroll, pixels_p)
6900 Lisp_Object window, vscroll, pixels_p;
6902 struct window *w;
6903 struct frame *f;
6905 if (NILP (window))
6906 window = selected_window;
6907 else
6908 CHECK_WINDOW (window);
6909 CHECK_NUMBER_OR_FLOAT (vscroll);
6911 w = XWINDOW (window);
6912 f = XFRAME (w->frame);
6914 if (FRAME_WINDOW_P (f))
6916 int old_dy = w->vscroll;
6918 w->vscroll = - (NILP (pixels_p)
6919 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6920 : XFLOATINT (vscroll));
6921 w->vscroll = min (w->vscroll, 0);
6923 if (w->vscroll != old_dy)
6925 /* Adjust glyph matrix of the frame if the virtual display
6926 area becomes larger than before. */
6927 if (w->vscroll < 0 && w->vscroll < old_dy)
6928 adjust_glyphs (f);
6930 /* Prevent redisplay shortcuts. */
6931 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6935 return Fwindow_vscroll (window, pixels_p);
6939 /* Call FN for all leaf windows on frame F. FN is called with the
6940 first argument being a pointer to the leaf window, and with
6941 additional argument USER_DATA. Stops when FN returns 0. */
6943 void
6944 foreach_window (f, fn, user_data)
6945 struct frame *f;
6946 int (* fn) P_ ((struct window *, void *));
6947 void *user_data;
6949 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6950 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6951 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6955 /* Helper function for foreach_window. Call FN for all leaf windows
6956 reachable from W. FN is called with the first argument being a
6957 pointer to the leaf window, and with additional argument USER_DATA.
6958 Stop when FN returns 0. Value is 0 if stopped by FN. */
6960 static int
6961 foreach_window_1 (w, fn, user_data)
6962 struct window *w;
6963 int (* fn) P_ ((struct window *, void *));
6964 void *user_data;
6966 int cont;
6968 for (cont = 1; w && cont;)
6970 if (!NILP (w->hchild))
6971 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6972 else if (!NILP (w->vchild))
6973 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6974 else
6975 cont = fn (w, user_data);
6977 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6980 return cont;
6984 /* Freeze or unfreeze the window start of W unless it is a
6985 mini-window or the selected window. FREEZE_P non-null means freeze
6986 the window start. */
6988 static int
6989 freeze_window_start (w, freeze_p)
6990 struct window *w;
6991 void *freeze_p;
6993 if (MINI_WINDOW_P (w)
6994 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6995 && (w == XWINDOW (selected_window)
6996 || (MINI_WINDOW_P (XWINDOW (selected_window))
6997 && ! NILP (Vminibuf_scroll_window)
6998 && w == XWINDOW (Vminibuf_scroll_window)))))
6999 freeze_p = NULL;
7001 w->frozen_window_start_p = freeze_p != NULL;
7002 return 1;
7006 /* Freeze or unfreeze the window starts of all leaf windows on frame
7007 F, except the selected window and a mini-window. FREEZE_P non-zero
7008 means freeze the window start. */
7010 void
7011 freeze_window_starts (f, freeze_p)
7012 struct frame *f;
7013 int freeze_p;
7015 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
7019 /***********************************************************************
7020 Initialization
7021 ***********************************************************************/
7023 /* Return 1 if window configurations C1 and C2
7024 describe the same state of affairs. This is used by Fequal. */
7027 compare_window_configurations (c1, c2, ignore_positions)
7028 Lisp_Object c1, c2;
7029 int ignore_positions;
7031 register struct save_window_data *d1, *d2;
7032 struct Lisp_Vector *sw1, *sw2;
7033 int i;
7035 CHECK_WINDOW_CONFIGURATION (c1);
7036 CHECK_WINDOW_CONFIGURATION (c2);
7038 d1 = (struct save_window_data *) XVECTOR (c1);
7039 d2 = (struct save_window_data *) XVECTOR (c2);
7040 sw1 = XVECTOR (d1->saved_windows);
7041 sw2 = XVECTOR (d2->saved_windows);
7043 if (d1->frame_cols != d2->frame_cols)
7044 return 0;
7045 if (d1->frame_lines != d2->frame_lines)
7046 return 0;
7047 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
7048 return 0;
7049 if (! EQ (d1->selected_frame, d2->selected_frame))
7050 return 0;
7051 /* Don't compare the current_window field directly.
7052 Instead see w1_is_current and w2_is_current, below. */
7053 if (! EQ (d1->current_buffer, d2->current_buffer))
7054 return 0;
7055 if (! ignore_positions)
7057 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
7058 return 0;
7059 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
7060 return 0;
7062 /* Don't compare the root_window field.
7063 We don't require the two configurations
7064 to use the same window object,
7065 and the two root windows must be equivalent
7066 if everything else compares equal. */
7067 if (! EQ (d1->focus_frame, d2->focus_frame))
7068 return 0;
7070 /* Verify that the two confis have the same number of windows. */
7071 if (sw1->size != sw2->size)
7072 return 0;
7074 for (i = 0; i < sw1->size; i++)
7076 struct saved_window *p1, *p2;
7077 int w1_is_current, w2_is_current;
7079 p1 = SAVED_WINDOW_N (sw1, i);
7080 p2 = SAVED_WINDOW_N (sw2, i);
7082 /* Verify that the current windows in the two
7083 configurations correspond to each other. */
7084 w1_is_current = EQ (d1->current_window, p1->window);
7085 w2_is_current = EQ (d2->current_window, p2->window);
7087 if (w1_is_current != w2_is_current)
7088 return 0;
7090 /* Verify that the corresponding windows do match. */
7091 if (! EQ (p1->buffer, p2->buffer))
7092 return 0;
7093 if (! EQ (p1->left_col, p2->left_col))
7094 return 0;
7095 if (! EQ (p1->top_line, p2->top_line))
7096 return 0;
7097 if (! EQ (p1->total_cols, p2->total_cols))
7098 return 0;
7099 if (! EQ (p1->total_lines, p2->total_lines))
7100 return 0;
7101 if (! EQ (p1->display_table, p2->display_table))
7102 return 0;
7103 if (! EQ (p1->parent, p2->parent))
7104 return 0;
7105 if (! EQ (p1->prev, p2->prev))
7106 return 0;
7107 if (! ignore_positions)
7109 if (! EQ (p1->hscroll, p2->hscroll))
7110 return 0;
7111 if (!EQ (p1->min_hscroll, p2->min_hscroll))
7112 return 0;
7113 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
7114 return 0;
7115 if (NILP (Fequal (p1->start, p2->start)))
7116 return 0;
7117 if (NILP (Fequal (p1->pointm, p2->pointm)))
7118 return 0;
7119 if (NILP (Fequal (p1->mark, p2->mark)))
7120 return 0;
7122 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
7123 return 0;
7124 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
7125 return 0;
7126 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
7127 return 0;
7128 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
7129 return 0;
7130 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
7131 return 0;
7132 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
7133 return 0;
7134 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
7135 return 0;
7138 return 1;
7141 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7142 Scompare_window_configurations, 2, 2, 0,
7143 doc: /* Compare two window configurations as regards the structure of windows.
7144 This function ignores details such as the values of point and mark
7145 and scrolling positions. */)
7146 (x, y)
7147 Lisp_Object x, y;
7149 if (compare_window_configurations (x, y, 1))
7150 return Qt;
7151 return Qnil;
7154 void
7155 init_window_once ()
7157 struct frame *f = make_initial_frame ();
7158 XSETFRAME (selected_frame, f);
7159 Vterminal_frame = selected_frame;
7160 minibuf_window = f->minibuffer_window;
7161 selected_window = f->selected_window;
7162 last_nonminibuf_frame = f;
7164 window_initialized = 1;
7167 void
7168 init_window ()
7170 Vwindow_list = Qnil;
7173 void
7174 syms_of_window ()
7176 Qscroll_up = intern_c_string ("scroll-up");
7177 staticpro (&Qscroll_up);
7179 Qscroll_down = intern_c_string ("scroll-down");
7180 staticpro (&Qscroll_down);
7182 Qwindow_size_fixed = intern_c_string ("window-size-fixed");
7183 staticpro (&Qwindow_size_fixed);
7184 Fset (Qwindow_size_fixed, Qnil);
7186 staticpro (&Qwindow_configuration_change_hook);
7187 Qwindow_configuration_change_hook
7188 = intern_c_string ("window-configuration-change-hook");
7190 Qwindowp = intern_c_string ("windowp");
7191 staticpro (&Qwindowp);
7193 Qwindow_configuration_p = intern_c_string ("window-configuration-p");
7194 staticpro (&Qwindow_configuration_p);
7196 Qwindow_live_p = intern_c_string ("window-live-p");
7197 staticpro (&Qwindow_live_p);
7199 Qdisplay_buffer = intern_c_string ("display-buffer");
7200 staticpro (&Qdisplay_buffer);
7202 Qtemp_buffer_show_hook = intern_c_string ("temp-buffer-show-hook");
7203 staticpro (&Qtemp_buffer_show_hook);
7205 staticpro (&Vwindow_list);
7207 minibuf_selected_window = Qnil;
7208 staticpro (&minibuf_selected_window);
7210 window_scroll_pixel_based_preserve_x = -1;
7211 window_scroll_pixel_based_preserve_y = -1;
7212 window_scroll_preserve_hpos = -1;
7213 window_scroll_preserve_vpos = -1;
7215 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
7216 doc: /* Non-nil means call as function to display a help buffer.
7217 The function is called with one argument, the buffer to be displayed.
7218 Used by `with-output-to-temp-buffer'.
7219 If this function is used, then it must do the entire job of showing
7220 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7221 Vtemp_buffer_show_function = Qnil;
7223 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
7224 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7225 Vminibuf_scroll_window = Qnil;
7227 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
7228 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7229 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7230 is displayed in the `mode-line' face. */);
7231 mode_line_in_non_selected_windows = 1;
7233 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
7234 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7235 Vother_window_scroll_buffer = Qnil;
7237 DEFVAR_BOOL ("auto-window-vscroll", &auto_window_vscroll_p,
7238 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7239 auto_window_vscroll_p = 1;
7241 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
7242 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7243 next_screen_context_lines = 2;
7245 DEFVAR_INT ("window-min-height", &window_min_height,
7246 doc: /* Allow deleting windows less than this tall.
7247 The value is measured in line units. If a window wants a modeline it
7248 is counted as one line.
7250 Emacs honors settings of this variable when enlarging or shrinking
7251 windows vertically. A value less than 1 is invalid. */);
7252 window_min_height = 4;
7254 DEFVAR_INT ("window-min-width", &window_min_width,
7255 doc: /* Allow deleting windows less than this wide.
7256 The value is measured in characters and includes any fringes or
7257 the scrollbar.
7259 Emacs honors settings of this variable when enlarging or shrinking
7260 windows horizontally. A value less than 2 is invalid. */);
7261 window_min_width = 10;
7263 DEFVAR_LISP ("scroll-preserve-screen-position",
7264 &Vscroll_preserve_screen_position,
7265 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
7266 A value of nil means point does not keep its screen position except
7267 at the scroll margin or window boundary respectively.
7268 A value of t means point keeps its screen position if the scroll
7269 command moved it vertically out of the window, e.g. when scrolling
7270 by full screens.
7271 Any other value means point always keeps its screen position. */);
7272 Vscroll_preserve_screen_position = Qnil;
7274 DEFVAR_LISP ("window-point-insertion-type", &Vwindow_point_insertion_type,
7275 doc: /* Type of marker to use for `window-point'. */);
7276 Vwindow_point_insertion_type = Qnil;
7278 DEFVAR_LISP ("window-configuration-change-hook",
7279 &Vwindow_configuration_change_hook,
7280 doc: /* Functions to call when window configuration changes.
7281 The buffer-local part is run once per window, with the relevant window
7282 selected; while the global part is run only once for the modified frame,
7283 with the relevant frame selected. */);
7284 Vwindow_configuration_change_hook = Qnil;
7286 DEFVAR_LISP ("recenter-redisplay", &Vrecenter_redisplay,
7287 doc: /* If non-nil, then the `recenter' command with a nil argument
7288 the entire frame to be redrawn; the special value `tty' causes the
7289 frame to be redrawn only if it is a tty frame. */);
7290 Vrecenter_redisplay = Qtty;
7293 defsubr (&Sselected_window);
7294 defsubr (&Sminibuffer_window);
7295 defsubr (&Swindow_minibuffer_p);
7296 defsubr (&Swindowp);
7297 defsubr (&Swindow_live_p);
7298 defsubr (&Spos_visible_in_window_p);
7299 defsubr (&Swindow_line_height);
7300 defsubr (&Swindow_buffer);
7301 defsubr (&Swindow_height);
7302 defsubr (&Swindow_width);
7303 defsubr (&Swindow_full_width_p);
7304 defsubr (&Swindow_hscroll);
7305 defsubr (&Sset_window_hscroll);
7306 defsubr (&Swindow_redisplay_end_trigger);
7307 defsubr (&Sset_window_redisplay_end_trigger);
7308 defsubr (&Swindow_edges);
7309 defsubr (&Swindow_pixel_edges);
7310 defsubr (&Swindow_inside_edges);
7311 defsubr (&Swindow_inside_pixel_edges);
7312 defsubr (&Scoordinates_in_window_p);
7313 defsubr (&Swindow_at);
7314 defsubr (&Swindow_point);
7315 defsubr (&Swindow_start);
7316 defsubr (&Swindow_end);
7317 defsubr (&Sset_window_point);
7318 defsubr (&Sset_window_start);
7319 defsubr (&Swindow_dedicated_p);
7320 defsubr (&Sset_window_dedicated_p);
7321 defsubr (&Swindow_display_table);
7322 defsubr (&Sset_window_display_table);
7323 defsubr (&Snext_window);
7324 defsubr (&Sprevious_window);
7325 defsubr (&Sother_window);
7326 defsubr (&Sget_lru_window);
7327 defsubr (&Sget_largest_window);
7328 defsubr (&Sget_buffer_window);
7329 defsubr (&Sdelete_other_windows);
7330 defsubr (&Sdelete_windows_on);
7331 defsubr (&Sreplace_buffer_in_windows);
7332 defsubr (&Sdelete_window);
7333 defsubr (&Sset_window_buffer);
7334 defsubr (&Sselect_window);
7335 defsubr (&Sforce_window_update);
7336 defsubr (&Ssplit_window);
7337 defsubr (&Senlarge_window);
7338 defsubr (&Sshrink_window);
7339 defsubr (&Sadjust_window_trailing_edge);
7340 defsubr (&Sscroll_up);
7341 defsubr (&Sscroll_down);
7342 defsubr (&Sscroll_left);
7343 defsubr (&Sscroll_right);
7344 defsubr (&Sother_window_for_scrolling);
7345 defsubr (&Sscroll_other_window);
7346 defsubr (&Sminibuffer_selected_window);
7347 defsubr (&Srecenter);
7348 defsubr (&Swindow_text_height);
7349 defsubr (&Smove_to_window_line);
7350 defsubr (&Swindow_configuration_p);
7351 defsubr (&Swindow_configuration_frame);
7352 defsubr (&Sset_window_configuration);
7353 defsubr (&Scurrent_window_configuration);
7354 defsubr (&Ssave_window_excursion);
7355 defsubr (&Swindow_tree);
7356 defsubr (&Sset_window_margins);
7357 defsubr (&Swindow_margins);
7358 defsubr (&Sset_window_fringes);
7359 defsubr (&Swindow_fringes);
7360 defsubr (&Sset_window_scroll_bars);
7361 defsubr (&Swindow_scroll_bars);
7362 defsubr (&Swindow_vscroll);
7363 defsubr (&Sset_window_vscroll);
7364 defsubr (&Scompare_window_configurations);
7365 defsubr (&Swindow_list);
7366 defsubr (&Swindow_parameters);
7367 defsubr (&Swindow_parameter);
7368 defsubr (&Sset_window_parameter);
7372 void
7373 keys_of_window ()
7375 initial_define_key (control_x_map, '1', "delete-other-windows");
7376 initial_define_key (control_x_map, '2', "split-window");
7377 initial_define_key (control_x_map, '0', "delete-window");
7378 initial_define_key (control_x_map, 'o', "other-window");
7379 initial_define_key (control_x_map, '^', "enlarge-window");
7380 initial_define_key (control_x_map, '<', "scroll-left");
7381 initial_define_key (control_x_map, '>', "scroll-right");
7383 initial_define_key (global_map, Ctl ('V'), "scroll-up");
7384 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7385 initial_define_key (meta_map, 'v', "scroll-down");
7387 initial_define_key (global_map, Ctl('L'), "recenter");
7388 initial_define_key (meta_map, 'r', "move-to-window-line");
7391 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
7392 (do not change this comment) */