Cleanup of window coordinate positioning code.
[emacs.git] / src / window.c
bloba2a0c793111c742819d24bd0195044759b1c6879
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
54 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
55 Lisp_Object Qdisplay_buffer;
56 Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
57 Lisp_Object Qwindow_size_fixed;
59 static int displayed_window_lines (struct window *);
60 static struct window *decode_window (Lisp_Object);
61 static int count_windows (struct window *);
62 static int get_leaf_windows (struct window *, struct window **, int);
63 static void window_scroll (Lisp_Object, int, int, int);
64 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
65 static void window_scroll_line_based (Lisp_Object, int, int, int);
66 static int window_min_size_1 (struct window *, int, int);
67 static int window_min_size_2 (struct window *, int, int);
68 static int window_min_size (struct window *, int, int, int, int *);
69 static void size_window (Lisp_Object, int, int, int, int, int);
70 static int freeze_window_start (struct window *, void *);
71 static int window_fixed_size_p (struct window *, int, int);
72 static void enlarge_window (Lisp_Object, int, int);
73 static Lisp_Object window_list (void);
74 static int add_window_to_list (struct window *, void *);
75 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
76 Lisp_Object);
77 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
78 Lisp_Object, int);
79 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
80 Lisp_Object *);
81 static int foreach_window_1 (struct window *,
82 int (* fn) (struct window *, void *),
83 void *);
84 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
86 /* This is the window in which the terminal's cursor should
87 be left when nothing is being done with it. This must
88 always be a leaf window, and its buffer is selected by
89 the top level editing loop at the end of each command.
91 This value is always the same as
92 FRAME_SELECTED_WINDOW (selected_frame). */
94 Lisp_Object selected_window;
96 /* A list of all windows for use by next_window and Fwindow_list.
97 Functions creating or deleting windows should invalidate this cache
98 by setting it to nil. */
100 Lisp_Object Vwindow_list;
102 /* The mini-buffer window of the selected frame.
103 Note that you cannot test for mini-bufferness of an arbitrary window
104 by comparing against this; but you can test for mini-bufferness of
105 the selected window. */
107 Lisp_Object minibuf_window;
109 /* Non-nil means it is the window whose mode line should be
110 shown as the selected window when the minibuffer is selected. */
112 Lisp_Object minibuf_selected_window;
114 /* Non-nil means it is the window for C-M-v to scroll
115 when the mini-buffer is selected. */
117 Lisp_Object Vminibuf_scroll_window;
119 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
121 Lisp_Object Vother_window_scroll_buffer;
123 /* Non-nil means it's function to call to display temp buffers. */
125 Lisp_Object Vtemp_buffer_show_function;
127 /* Non-zero means line and page scrolling on tall lines (with images)
128 does partial scrolling by modifying window-vscroll. */
130 int auto_window_vscroll_p;
132 /* Non-zero means to use mode-line-inactive face in all windows but the
133 selected-window and the minibuffer-scroll-window when the
134 minibuffer is active. */
135 int mode_line_in_non_selected_windows;
137 /* If a window gets smaller than either of these, it is removed. */
139 EMACS_INT window_min_height;
140 EMACS_INT window_min_width;
142 /* Hook run at end of temp_output_buffer_show. */
144 Lisp_Object Qtemp_buffer_show_hook;
146 /* Number of lines of continuity in scrolling by screenfuls. */
148 EMACS_INT next_screen_context_lines;
150 /* Incremented for each window created. */
152 static int sequence_number;
154 /* Nonzero after init_window_once has finished. */
156 static int window_initialized;
158 /* Hook to run when window config changes. */
160 static Lisp_Object Qwindow_configuration_change_hook;
161 static Lisp_Object Vwindow_configuration_change_hook;
163 /* Non-nil means scroll commands try to put point
164 at the same screen height as previously. */
166 Lisp_Object Vscroll_preserve_screen_position;
168 /* Non-nil means that text is inserted before window's markers. */
170 Lisp_Object Vwindow_point_insertion_type;
172 /* Incremented by 1 whenever a window is deleted. */
174 int window_deletion_count;
176 /* Used by the function window_scroll_pixel_based */
178 static int window_scroll_pixel_based_preserve_x;
179 static int window_scroll_pixel_based_preserve_y;
181 /* Same for window_scroll_line_based. */
183 static int window_scroll_preserve_hpos;
184 static int window_scroll_preserve_vpos;
186 #if 0 /* This isn't used anywhere. */
187 /* Nonzero means we can split a frame even if it is "unsplittable". */
188 static int inhibit_frame_unsplittable;
189 #endif /* 0 */
191 /* If non-nil, then the `recenter' command with a nil argument
192 the entire frame to be redrawn; the special value `tty' causes the
193 frame to be redrawn only if it is a tty frame. */
195 static Lisp_Object Vrecenter_redisplay;
198 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
199 doc: /* Return t if OBJECT is a window. */)
200 (Lisp_Object object)
202 return WINDOWP (object) ? Qt : Qnil;
205 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
206 doc: /* Return t if OBJECT is a window which is currently visible. */)
207 (Lisp_Object object)
209 return WINDOW_LIVE_P (object) ? Qt : Qnil;
212 Lisp_Object
213 make_window (void)
215 Lisp_Object val;
216 register struct window *p;
218 p = allocate_window ();
219 ++sequence_number;
220 XSETFASTINT (p->sequence_number, sequence_number);
221 XSETFASTINT (p->left_col, 0);
222 XSETFASTINT (p->top_line, 0);
223 XSETFASTINT (p->total_lines, 0);
224 XSETFASTINT (p->total_cols, 0);
225 XSETFASTINT (p->hscroll, 0);
226 XSETFASTINT (p->min_hscroll, 0);
227 p->orig_top_line = p->orig_total_lines = Qnil;
228 p->start = Fmake_marker ();
229 p->pointm = Fmake_marker ();
230 XSETFASTINT (p->use_time, 0);
231 p->frame = Qnil;
232 p->display_table = Qnil;
233 p->dedicated = Qnil;
234 p->window_parameters = Qnil;
235 p->pseudo_window_p = 0;
236 memset (&p->cursor, 0, sizeof (p->cursor));
237 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
238 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
239 p->desired_matrix = p->current_matrix = 0;
240 p->nrows_scale_factor = p->ncols_scale_factor = 1;
241 p->phys_cursor_type = -1;
242 p->phys_cursor_width = -1;
243 p->must_be_updated_p = 0;
244 XSETFASTINT (p->window_end_vpos, 0);
245 XSETFASTINT (p->window_end_pos, 0);
246 p->window_end_valid = Qnil;
247 p->vscroll = 0;
248 XSETWINDOW (val, p);
249 XSETFASTINT (p->last_point, 0);
250 p->frozen_window_start_p = 0;
251 p->last_cursor_off_p = p->cursor_off_p = 0;
252 p->left_margin_cols = Qnil;
253 p->right_margin_cols = Qnil;
254 p->left_fringe_width = Qnil;
255 p->right_fringe_width = Qnil;
256 p->fringes_outside_margins = Qnil;
257 p->scroll_bar_width = Qnil;
258 p->vertical_scroll_bar_type = Qt;
259 p->resize_proportionally = Qnil;
261 Vwindow_list = Qnil;
262 return val;
265 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
266 doc: /* Return the window that the cursor now appears in and commands apply to. */)
267 (void)
269 return selected_window;
272 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
273 doc: /* Return the window used now for minibuffers.
274 If the optional argument FRAME is specified, return the minibuffer window
275 used by that frame. */)
276 (Lisp_Object frame)
278 if (NILP (frame))
279 frame = selected_frame;
280 CHECK_LIVE_FRAME (frame);
281 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
284 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
285 doc: /* Return non-nil if WINDOW is a minibuffer window.
286 WINDOW defaults to the selected window. */)
287 (Lisp_Object window)
289 struct window *w = decode_window (window);
290 return MINI_WINDOW_P (w) ? Qt : Qnil;
294 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
295 Spos_visible_in_window_p, 0, 3, 0,
296 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
297 Return nil if that position is scrolled vertically out of view.
298 If a character is only partially visible, nil is returned, unless the
299 optional argument PARTIALLY is non-nil.
300 If POS is only out of view because of horizontal scrolling, return non-nil.
301 If POS is t, it specifies the position of the last visible glyph in WINDOW.
302 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
304 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
305 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
306 where X and Y are the pixel coordinates relative to the top left corner
307 of the window. The remaining elements are omitted if the character after
308 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
309 off-window at the top and bottom of the row, ROWH is the height of the
310 display row, and VPOS is the row number (0-based) containing POS. */)
311 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
313 register struct window *w;
314 register EMACS_INT posint;
315 register struct buffer *buf;
316 struct text_pos top;
317 Lisp_Object in_window = Qnil;
318 int rtop, rbot, rowh, vpos, fully_p = 1;
319 int x, y;
321 w = decode_window (window);
322 buf = XBUFFER (w->buffer);
323 SET_TEXT_POS_FROM_MARKER (top, w->start);
325 if (EQ (pos, Qt))
326 posint = -1;
327 else if (!NILP (pos))
329 CHECK_NUMBER_COERCE_MARKER (pos);
330 posint = XINT (pos);
332 else if (w == XWINDOW (selected_window))
333 posint = PT;
334 else
335 posint = XMARKER (w->pointm)->charpos;
337 /* If position is above window start or outside buffer boundaries,
338 or if window start is out of range, position is not visible. */
339 if ((EQ (pos, Qt)
340 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
341 && CHARPOS (top) >= BUF_BEGV (buf)
342 && CHARPOS (top) <= BUF_ZV (buf)
343 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
344 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
345 in_window = Qt;
347 if (!NILP (in_window) && !NILP (partially))
349 Lisp_Object part = Qnil;
350 if (!fully_p)
351 part = list4 (make_number (rtop), make_number (rbot),
352 make_number (rowh), make_number (vpos));
353 in_window = Fcons (make_number (x),
354 Fcons (make_number (y), part));
357 return in_window;
360 DEFUN ("window-line-height", Fwindow_line_height,
361 Swindow_line_height, 0, 2, 0,
362 doc: /* Return height in pixels of text line LINE in window WINDOW.
363 If WINDOW is nil or omitted, use selected window.
365 Return height of current line if LINE is omitted or nil. Return height of
366 header or mode line if LINE is `header-line' and `mode-line'.
367 Otherwise, LINE is a text line number starting from 0. A negative number
368 counts from the end of the window.
370 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
371 in pixels of the visible part of the line, VPOS and YPOS are the
372 vertical position in lines and pixels of the line, relative to the top
373 of the first text line, and OFFBOT is the number of off-window pixels at
374 the bottom of the text line. If there are off-window pixels at the top
375 of the (first) text line, YPOS is negative.
377 Return nil if window display is not up-to-date. In that case, use
378 `pos-visible-in-window-p' to obtain the information. */)
379 (Lisp_Object line, Lisp_Object window)
381 register struct window *w;
382 register struct buffer *b;
383 struct glyph_row *row, *end_row;
384 int max_y, crop, i, n;
386 w = decode_window (window);
388 if (noninteractive
389 || w->pseudo_window_p)
390 return Qnil;
392 CHECK_BUFFER (w->buffer);
393 b = XBUFFER (w->buffer);
395 /* Fail if current matrix is not up-to-date. */
396 if (NILP (w->window_end_valid)
397 || current_buffer->clip_changed
398 || current_buffer->prevent_redisplay_optimizations_p
399 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
400 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
401 return Qnil;
403 if (NILP (line))
405 i = w->cursor.vpos;
406 if (i < 0 || i >= w->current_matrix->nrows
407 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
408 return Qnil;
409 max_y = window_text_bottom_y (w);
410 goto found_row;
413 if (EQ (line, Qheader_line))
415 if (!WINDOW_WANTS_HEADER_LINE_P (w))
416 return Qnil;
417 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
418 if (!row->enabled_p)
419 return Qnil;
420 return list4 (make_number (row->height),
421 make_number (0), make_number (0),
422 make_number (0));
425 if (EQ (line, Qmode_line))
427 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
428 if (!row->enabled_p)
429 return Qnil;
430 return list4 (make_number (row->height),
431 make_number (0), /* not accurate */
432 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
433 + window_text_bottom_y (w)),
434 make_number (0));
437 CHECK_NUMBER (line);
438 n = XINT (line);
440 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
441 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
442 max_y = window_text_bottom_y (w);
443 i = 0;
445 while ((n < 0 || i < n)
446 && row <= end_row && row->enabled_p
447 && row->y + row->height < max_y)
448 row++, i++;
450 if (row > end_row || !row->enabled_p)
451 return Qnil;
453 if (++n < 0)
455 if (-n > i)
456 return Qnil;
457 row += n;
458 i += n;
461 found_row:
462 crop = max (0, (row->y + row->height) - max_y);
463 return list4 (make_number (row->height + min (0, row->y) - crop),
464 make_number (i),
465 make_number (row->y),
466 make_number (crop));
471 static struct window *
472 decode_window (register Lisp_Object window)
474 if (NILP (window))
475 return XWINDOW (selected_window);
477 CHECK_LIVE_WINDOW (window);
478 return XWINDOW (window);
481 static struct window *
482 decode_any_window (register Lisp_Object window)
484 if (NILP (window))
485 return XWINDOW (selected_window);
487 CHECK_WINDOW (window);
488 return XWINDOW (window);
491 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
492 doc: /* Return the buffer that WINDOW is displaying.
493 WINDOW defaults to the selected window. */)
494 (Lisp_Object window)
496 return decode_window (window)->buffer;
499 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
500 doc: /* Return the number of lines in WINDOW.
501 WINDOW defaults to the selected window.
503 The return value includes WINDOW's mode line and header line, if any.
505 Note: The function does not take into account the value of `line-spacing'
506 when calculating the number of lines in WINDOW. */)
507 (Lisp_Object window)
509 return decode_any_window (window)->total_lines;
512 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
513 doc: /* Return the number of display columns in WINDOW.
514 WINDOW defaults to the selected window.
516 Note: The return value is the number of columns available for text in
517 WINDOW. If you want to find out how many columns WINDOW takes up, use
518 (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
519 (Lisp_Object window)
521 return make_number (window_box_text_cols (decode_any_window (window)));
524 DEFUN ("window-full-width-p", Fwindow_full_width_p, Swindow_full_width_p, 0, 1, 0,
525 doc: /* Return t if WINDOW is as wide as its frame.
526 WINDOW defaults to the selected window. */)
527 (Lisp_Object window)
529 return WINDOW_FULL_WIDTH_P (decode_any_window (window)) ? Qt : Qnil;
532 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
533 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
534 WINDOW defaults to the selected window. */)
535 (Lisp_Object window)
537 return decode_window (window)->hscroll;
540 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
541 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
542 Return NCOL. NCOL should be zero or positive.
544 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
545 window so that the location of point moves off-window. */)
546 (Lisp_Object window, Lisp_Object ncol)
548 struct window *w = decode_window (window);
549 int hscroll;
551 CHECK_NUMBER (ncol);
552 hscroll = max (0, XINT (ncol));
554 /* Prevent redisplay shortcuts when changing the hscroll. */
555 if (XINT (w->hscroll) != hscroll)
556 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
558 w->hscroll = make_number (hscroll);
559 return ncol;
562 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
563 Swindow_redisplay_end_trigger, 0, 1, 0,
564 doc: /* Return WINDOW's redisplay end trigger value.
565 WINDOW defaults to the selected window.
566 See `set-window-redisplay-end-trigger' for more information. */)
567 (Lisp_Object window)
569 return decode_window (window)->redisplay_end_trigger;
572 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
573 Sset_window_redisplay_end_trigger, 2, 2, 0,
574 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
575 VALUE should be a buffer position (typically a marker) or nil.
576 If it is a buffer position, then if redisplay in WINDOW reaches a position
577 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
578 with two arguments: WINDOW, and the end trigger value.
579 Afterwards the end-trigger value is reset to nil. */)
580 (register Lisp_Object window, Lisp_Object value)
582 register struct window *w;
584 w = decode_window (window);
585 w->redisplay_end_trigger = value;
586 return value;
589 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
590 doc: /* Return a list of the edge coordinates of WINDOW.
591 The list has the form (LEFT TOP RIGHT BOTTOM).
592 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
593 all relative to 0, 0 at top left corner of frame.
595 RIGHT is one more than the rightmost column occupied by WINDOW.
596 BOTTOM is one more than the bottommost row occupied by WINDOW.
597 The edges include the space used by WINDOW's scroll bar, display
598 margins, fringes, header line, and/or mode line. For the edges of
599 just the text area, use `window-inside-edges'. */)
600 (Lisp_Object window)
602 register struct window *w = decode_any_window (window);
604 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
605 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
606 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
607 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
608 Qnil))));
611 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
612 doc: /* Return a list of the edge pixel coordinates of WINDOW.
613 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
614 the top left corner of the frame.
616 RIGHT is one more than the rightmost x position occupied by WINDOW.
617 BOTTOM is one more than the bottommost y position occupied by WINDOW.
618 The pixel edges include the space used by WINDOW's scroll bar, display
619 margins, fringes, header line, and/or mode line. For the pixel edges
620 of just the text area, use `window-inside-pixel-edges'. */)
621 (Lisp_Object window)
623 register struct window *w = decode_any_window (window);
625 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
626 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
627 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
628 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
629 Qnil))));
632 static void
633 calc_absolute_offset(struct window *w, int *add_x, int *add_y)
635 struct frame *f = XFRAME (w->frame);
636 *add_y = f->top_pos;
637 #ifdef FRAME_MENUBAR_HEIGHT
638 *add_y += FRAME_MENUBAR_HEIGHT (f);
639 #endif
640 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
641 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
642 #elif FRAME_TOOLBAR_HEIGHT
643 *add_y += FRAME_TOOLBAR_HEIGHT (f);
644 #endif
645 #ifdef FRAME_NS_TITLEBAR_HEIGHT
646 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
647 #endif
648 *add_x = f->left_pos;
649 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
650 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
651 #endif
654 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
655 Swindow_absolute_pixel_edges, 0, 1, 0,
656 doc: /* Return a list of the edge pixel coordinates of WINDOW.
657 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
658 the top left corner of the display.
660 RIGHT is one more than the rightmost x position occupied by WINDOW.
661 BOTTOM is one more than the bottommost y position occupied by WINDOW.
662 The pixel edges include the space used by WINDOW's scroll bar, display
663 margins, fringes, header line, and/or mode line. For the pixel edges
664 of just the text area, use `window-inside-absolute-pixel-edges'. */)
665 (Lisp_Object window)
667 register struct window *w = decode_any_window (window);
668 int add_x, add_y;
669 calc_absolute_offset (w, &add_x, &add_y);
671 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
672 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
673 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
674 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
675 Qnil))));
678 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
679 doc: /* Return a list of the edge coordinates of WINDOW.
680 The list has the form (LEFT TOP RIGHT BOTTOM).
681 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
682 all relative to 0, 0 at top left corner of frame.
684 RIGHT is one more than the rightmost column of WINDOW's text area.
685 BOTTOM is one more than the bottommost row of WINDOW's text area.
686 The inside edges do not include the space used by the WINDOW's scroll
687 bar, display margins, fringes, header line, and/or mode line. */)
688 (Lisp_Object window)
690 register struct window *w = decode_any_window (window);
692 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
693 + WINDOW_LEFT_MARGIN_COLS (w)
694 + WINDOW_LEFT_FRINGE_COLS (w)),
695 make_number (WINDOW_TOP_EDGE_LINE (w)
696 + WINDOW_HEADER_LINE_LINES (w)),
697 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
698 - WINDOW_RIGHT_MARGIN_COLS (w)
699 - WINDOW_RIGHT_FRINGE_COLS (w)),
700 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
701 - WINDOW_MODE_LINE_LINES (w)));
704 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
705 doc: /* Return a list of the edge pixel coordinates of WINDOW.
706 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
707 the top left corner of the frame.
709 RIGHT is one more than the rightmost x position of WINDOW's text area.
710 BOTTOM is one more than the bottommost y position of WINDOW's text area.
711 The inside edges do not include the space used by WINDOW's scroll bar,
712 display margins, fringes, header line, and/or mode line. */)
713 (Lisp_Object window)
715 register struct window *w = decode_any_window (window);
717 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
718 + WINDOW_LEFT_MARGIN_WIDTH (w)
719 + WINDOW_LEFT_FRINGE_WIDTH (w)),
720 make_number (WINDOW_TOP_EDGE_Y (w)
721 + WINDOW_HEADER_LINE_HEIGHT (w)),
722 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
723 - WINDOW_RIGHT_MARGIN_WIDTH (w)
724 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
725 make_number (WINDOW_BOTTOM_EDGE_Y (w)
726 - WINDOW_MODE_LINE_HEIGHT (w)));
729 DEFUN ("window-inside-absolute-pixel-edges",
730 Fwindow_inside_absolute_pixel_edges,
731 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
732 doc: /* Return a list of the edge pixel coordinates of WINDOW.
733 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
734 the top left corner of the display.
736 RIGHT is one more than the rightmost x position of WINDOW's text area.
737 BOTTOM is one more than the bottommost y position of WINDOW's text area.
738 The inside edges do not include the space used by WINDOW's scroll bar,
739 display margins, fringes, header line, and/or mode line. */)
740 (Lisp_Object window)
742 register struct window *w = decode_any_window (window);
743 int add_x, add_y;
744 calc_absolute_offset (w, &add_x, &add_y);
746 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
747 + WINDOW_LEFT_MARGIN_WIDTH (w)
748 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
749 make_number (WINDOW_TOP_EDGE_Y (w)
750 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
751 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
752 - WINDOW_RIGHT_MARGIN_WIDTH (w)
753 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
754 make_number (WINDOW_BOTTOM_EDGE_Y (w)
755 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
758 /* Test if the character at column X, row Y is within window W.
759 If it is not, return ON_NOTHING;
760 if it is in the window's text area, return ON_TEXT;
761 if it is on the window's modeline, return ON_MODE_LINE;
762 if it is on the border between the window and its right sibling,
763 return ON_VERTICAL_BORDER.
764 if it is on a scroll bar, return ON_SCROLL_BAR.
765 if it is on the window's top line, return ON_HEADER_LINE;
766 if it is in left or right fringe of the window,
767 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
768 if it is in the marginal area to the left/right of the window,
769 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
771 X and Y are frame relative pixel coordinates. */
773 static enum window_part
774 coordinates_in_window (register struct window *w, int x, int y)
776 struct frame *f = XFRAME (WINDOW_FRAME (w));
777 int left_x, right_x;
778 enum window_part part;
779 int ux = FRAME_COLUMN_WIDTH (f);
780 int x0 = WINDOW_LEFT_EDGE_X (w);
781 int x1 = WINDOW_RIGHT_EDGE_X (w);
782 /* The width of the area where the vertical line can be dragged.
783 (Between mode lines for instance. */
784 int grabbable_width = ux;
785 int lmargin_width, rmargin_width, text_left, text_right;
786 int top_y = WINDOW_TOP_EDGE_Y (w);
787 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
789 /* Outside any interesting row? */
790 if (y < top_y || y >= bottom_y)
791 return ON_NOTHING;
793 /* In what's below, we subtract 1 when computing right_x because we
794 want the rightmost pixel, which is given by left_pixel+width-1. */
795 if (w->pseudo_window_p)
797 left_x = 0;
798 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
800 else
802 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
803 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
806 /* On the mode line or header line? If it's near the start of
807 the mode or header line of window that's has a horizontal
808 sibling, say it's on the vertical line. That's to be able
809 to resize windows horizontally in case we're using toolkit
810 scroll bars. */
812 if (WINDOW_WANTS_MODELINE_P (w)
813 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
815 part = ON_MODE_LINE;
817 header_vertical_border_check:
818 /* We're somewhere on the mode line. We consider the place
819 between mode lines of horizontally adjacent mode lines
820 as the vertical border. If scroll bars on the left,
821 return the right window. */
822 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
823 || WINDOW_RIGHTMOST_P (w))
824 && !WINDOW_LEFTMOST_P (w)
825 && eabs (x - x0) < grabbable_width)
826 return ON_VERTICAL_BORDER;
828 /* Make sure we're not at the rightmost position of a
829 mode-/header-line and there's yet another window on the
830 right. (Bug#1372) */
831 else if ((WINDOW_RIGHTMOST_P (w) || x < x1)
832 && eabs (x - x1) < grabbable_width)
833 return ON_VERTICAL_BORDER;
835 if (x < x0 || x >= x1)
836 return ON_NOTHING;
838 return part;
841 if (WINDOW_WANTS_HEADER_LINE_P (w)
842 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
844 part = ON_HEADER_LINE;
845 goto header_vertical_border_check;
848 if (x < x0 || x >= x1) return ON_NOTHING;
850 /* Outside any interesting column? */
851 if (x < left_x || x > right_x)
852 return ON_SCROLL_BAR;
854 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
855 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
857 text_left = window_box_left (w, TEXT_AREA);
858 text_right = text_left + window_box_width (w, TEXT_AREA);
860 if (FRAME_WINDOW_P (f))
862 if (!w->pseudo_window_p
863 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
864 && !WINDOW_RIGHTMOST_P (w)
865 && (eabs (x - right_x) < grabbable_width))
866 return ON_VERTICAL_BORDER;
868 /* Need to say "x > right_x" rather than >=, since on character
869 terminals, the vertical line's x coordinate is right_x. */
870 else if (!w->pseudo_window_p
871 && !WINDOW_RIGHTMOST_P (w)
872 && x > right_x - ux)
873 return ON_VERTICAL_BORDER;
875 if (x < text_left)
877 if (lmargin_width > 0
878 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
879 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
880 : (x < left_x + lmargin_width)))
881 return ON_LEFT_MARGIN;
883 return ON_LEFT_FRINGE;
886 if (x >= text_right)
888 if (rmargin_width > 0
889 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
890 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
891 : (x >= right_x - rmargin_width)))
892 return ON_RIGHT_MARGIN;
894 return ON_RIGHT_FRINGE;
897 /* Everything special ruled out - must be on text area */
898 return ON_TEXT;
901 /* Take X is the frame-relative pixel x-coordinate, and return the
902 x-coordinate relative to part PART of window W. */
904 window_relative_x_coord (struct window *w, enum window_part part, int x)
906 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
908 switch (part)
910 case ON_TEXT:
911 return x - window_box_left (w, TEXT_AREA);
913 case ON_LEFT_FRINGE:
914 return x - left_x;
916 case ON_RIGHT_FRINGE:
917 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
919 case ON_LEFT_MARGIN:
920 return (x - left_x
921 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
922 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
924 case ON_RIGHT_MARGIN:
925 return (x + 1
926 - ((w->pseudo_window_p)
927 ? WINDOW_TOTAL_WIDTH (w)
928 : WINDOW_BOX_RIGHT_EDGE_X (w))
929 + window_box_width (w, RIGHT_MARGIN_AREA)
930 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
931 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
934 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
935 return 0;
939 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
940 Scoordinates_in_window_p, 2, 2, 0,
941 doc: /* Return non-nil if COORDINATES are in WINDOW.
942 COORDINATES is a cons of the form (X . Y), X and Y being distances
943 measured in characters from the upper-left corner of the frame.
944 \(0 . 0) denotes the character in the upper left corner of the
945 frame.
946 If COORDINATES are in the text portion of WINDOW,
947 the coordinates relative to the window are returned.
948 If they are in the mode line of WINDOW, `mode-line' is returned.
949 If they are in the top mode line of WINDOW, `header-line' is returned.
950 If they are in the left fringe of WINDOW, `left-fringe' is returned.
951 If they are in the right fringe of WINDOW, `right-fringe' is returned.
952 If they are on the border between WINDOW and its right sibling,
953 `vertical-line' is returned.
954 If they are in the windows's left or right marginal areas, `left-margin'\n\
955 or `right-margin' is returned. */)
956 (register Lisp_Object coordinates, Lisp_Object window)
958 struct window *w;
959 struct frame *f;
960 int x, y;
961 Lisp_Object lx, ly;
963 CHECK_WINDOW (window);
964 w = XWINDOW (window);
965 f = XFRAME (w->frame);
966 CHECK_CONS (coordinates);
967 lx = Fcar (coordinates);
968 ly = Fcdr (coordinates);
969 CHECK_NUMBER_OR_FLOAT (lx);
970 CHECK_NUMBER_OR_FLOAT (ly);
971 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
972 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
974 switch (coordinates_in_window (w, x, y))
976 case ON_NOTHING:
977 return Qnil;
979 case ON_TEXT:
980 /* Convert X and Y to window relative pixel coordinates, and
981 return the canonical char units. */
982 x -= window_box_left (w, TEXT_AREA);
983 y -= WINDOW_TOP_EDGE_Y (w);
984 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
985 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
987 case ON_MODE_LINE:
988 return Qmode_line;
990 case ON_VERTICAL_BORDER:
991 return Qvertical_line;
993 case ON_HEADER_LINE:
994 return Qheader_line;
996 case ON_LEFT_FRINGE:
997 return Qleft_fringe;
999 case ON_RIGHT_FRINGE:
1000 return Qright_fringe;
1002 case ON_LEFT_MARGIN:
1003 return Qleft_margin;
1005 case ON_RIGHT_MARGIN:
1006 return Qright_margin;
1008 case ON_SCROLL_BAR:
1009 /* Historically we are supposed to return nil in this case. */
1010 return Qnil;
1012 default:
1013 abort ();
1018 /* Callback for foreach_window, used in window_from_coordinates.
1019 Check if window W contains coordinates specified by USER_DATA which
1020 is actually a pointer to a struct check_window_data CW.
1022 Check if window W contains coordinates *CW->x and *CW->y. If it
1023 does, return W in *CW->window, as Lisp_Object, and return in
1024 *CW->part the part of the window under coordinates *X,*Y. Return
1025 zero from this function to stop iterating over windows. */
1027 struct check_window_data
1029 Lisp_Object *window;
1030 int x, y;
1031 enum window_part *part;
1034 static int
1035 check_window_containing (struct window *w, void *user_data)
1037 struct check_window_data *cw = (struct check_window_data *) user_data;
1038 enum window_part found;
1039 int continue_p = 1;
1041 found = coordinates_in_window (w, cw->x, cw->y);
1042 if (found != ON_NOTHING)
1044 *cw->part = found;
1045 XSETWINDOW (*cw->window, w);
1046 continue_p = 0;
1049 return continue_p;
1053 /* Find the window containing frame-relative pixel position X/Y and
1054 return it as a Lisp_Object.
1056 If X, Y is on one of the window's special `window_part' elements,
1057 set *PART to the id of that element.
1059 If there is no window under X, Y return nil and leave *PART
1060 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1062 This function was previously implemented with a loop cycling over
1063 windows with Fnext_window, and starting with the frame's selected
1064 window. It turned out that this doesn't work with an
1065 implementation of next_window using Vwindow_list, because
1066 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1067 tree of F when this function is called asynchronously from
1068 note_mouse_highlight. The original loop didn't terminate in this
1069 case. */
1071 Lisp_Object
1072 window_from_coordinates (struct frame *f, int x, int y,
1073 enum window_part *part, int tool_bar_p)
1075 Lisp_Object window;
1076 struct check_window_data cw;
1077 enum window_part dummy;
1079 if (part == 0)
1080 part = &dummy;
1082 window = Qnil;
1083 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1084 foreach_window (f, check_window_containing, &cw);
1086 /* If not found above, see if it's in the tool bar window, if a tool
1087 bar exists. */
1088 if (NILP (window)
1089 && tool_bar_p
1090 && WINDOWP (f->tool_bar_window)
1091 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1092 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1093 != ON_NOTHING))
1095 *part = ON_TEXT;
1096 window = f->tool_bar_window;
1099 return window;
1102 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1103 doc: /* Return window containing coordinates X and Y on FRAME.
1104 If omitted, FRAME defaults to the currently selected frame.
1105 The top left corner of the frame is considered to be row 0,
1106 column 0. */)
1107 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1109 struct frame *f;
1111 if (NILP (frame))
1112 frame = selected_frame;
1113 CHECK_LIVE_FRAME (frame);
1114 f = XFRAME (frame);
1116 /* Check that arguments are integers or floats. */
1117 CHECK_NUMBER_OR_FLOAT (x);
1118 CHECK_NUMBER_OR_FLOAT (y);
1120 return window_from_coordinates (f,
1121 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1122 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1123 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1124 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1125 0, 0);
1128 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1129 doc: /* Return current value of point in WINDOW.
1130 WINDOW defaults to the selected window.
1132 For a nonselected window, this is the value point would have
1133 if that window were selected.
1135 Note that, when WINDOW is the selected window and its buffer
1136 is also currently selected, the value returned is the same as (point).
1137 It would be more strictly correct to return the `top-level' value
1138 of point, outside of any save-excursion forms.
1139 But that is hard to define. */)
1140 (Lisp_Object window)
1142 register struct window *w = decode_window (window);
1144 if (w == XWINDOW (selected_window)
1145 && current_buffer == XBUFFER (w->buffer))
1146 return Fpoint ();
1147 return Fmarker_position (w->pointm);
1150 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1151 doc: /* Return position at which display currently starts in WINDOW.
1152 WINDOW defaults to the selected window.
1153 This is updated by redisplay or by calling `set-window-start'. */)
1154 (Lisp_Object window)
1156 return Fmarker_position (decode_window (window)->start);
1159 /* This is text temporarily removed from the doc string below.
1161 This function returns nil if the position is not currently known.
1162 That happens when redisplay is preempted and doesn't finish.
1163 If in that case you want to compute where the end of the window would
1164 have been if redisplay had finished, do this:
1165 (save-excursion
1166 (goto-char (window-start window))
1167 (vertical-motion (1- (window-height window)) window)
1168 (point))") */
1170 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1171 doc: /* Return position at which display currently ends in WINDOW.
1172 WINDOW defaults to the selected window.
1173 This is updated by redisplay, when it runs to completion.
1174 Simply changing the buffer text or setting `window-start'
1175 does not update this value.
1176 Return nil if there is no recorded value. \(This can happen if the
1177 last redisplay of WINDOW was preempted, and did not finish.)
1178 If UPDATE is non-nil, compute the up-to-date position
1179 if it isn't already recorded. */)
1180 (Lisp_Object window, Lisp_Object update)
1182 Lisp_Object value;
1183 struct window *w = decode_window (window);
1184 Lisp_Object buf;
1185 struct buffer *b;
1187 buf = w->buffer;
1188 CHECK_BUFFER (buf);
1189 b = XBUFFER (buf);
1191 #if 0 /* This change broke some things. We should make it later. */
1192 /* If we don't know the end position, return nil.
1193 The user can compute it with vertical-motion if he wants to.
1194 It would be nicer to do it automatically,
1195 but that's so slow that it would probably bother people. */
1196 if (NILP (w->window_end_valid))
1197 return Qnil;
1198 #endif
1200 if (! NILP (update)
1201 && ! (! NILP (w->window_end_valid)
1202 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1203 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1204 && !noninteractive)
1206 struct text_pos startp;
1207 struct it it;
1208 struct buffer *old_buffer = NULL;
1210 /* Cannot use Fvertical_motion because that function doesn't
1211 cope with variable-height lines. */
1212 if (b != current_buffer)
1214 old_buffer = current_buffer;
1215 set_buffer_internal (b);
1218 /* In case W->start is out of the range, use something
1219 reasonable. This situation occurred when loading a file with
1220 `-l' containing a call to `rmail' with subsequent other
1221 commands. At the end, W->start happened to be BEG, while
1222 rmail had already narrowed the buffer. */
1223 if (XMARKER (w->start)->charpos < BEGV)
1224 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1225 else if (XMARKER (w->start)->charpos > ZV)
1226 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1227 else
1228 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1230 start_display (&it, w, startp);
1231 move_it_vertically (&it, window_box_height (w));
1232 if (it.current_y < it.last_visible_y)
1233 move_it_past_eol (&it);
1234 value = make_number (IT_CHARPOS (it));
1236 if (old_buffer)
1237 set_buffer_internal (old_buffer);
1239 else
1240 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1242 return value;
1245 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1246 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1247 Return POS. */)
1248 (Lisp_Object window, Lisp_Object pos)
1250 register struct window *w = decode_window (window);
1252 CHECK_NUMBER_COERCE_MARKER (pos);
1253 if (w == XWINDOW (selected_window)
1254 && XBUFFER (w->buffer) == current_buffer)
1255 Fgoto_char (pos);
1256 else
1257 set_marker_restricted (w->pointm, pos, w->buffer);
1259 /* We have to make sure that redisplay updates the window to show
1260 the new value of point. */
1261 if (!EQ (window, selected_window))
1262 ++windows_or_buffers_changed;
1264 return pos;
1267 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1268 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1269 WINDOW defaults to the selected window. Return POS.
1270 Optional third arg NOFORCE non-nil inhibits next redisplay from
1271 overriding motion of point in order to display at this exact start. */)
1272 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1274 register struct window *w = decode_window (window);
1276 CHECK_NUMBER_COERCE_MARKER (pos);
1277 set_marker_restricted (w->start, pos, w->buffer);
1278 /* this is not right, but much easier than doing what is right. */
1279 w->start_at_line_beg = Qnil;
1280 if (NILP (noforce))
1281 w->force_start = Qt;
1282 w->update_mode_line = Qt;
1283 XSETFASTINT (w->last_modified, 0);
1284 XSETFASTINT (w->last_overlay_modified, 0);
1285 if (!EQ (window, selected_window))
1286 windows_or_buffers_changed++;
1288 return pos;
1292 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1293 0, 1, 0,
1294 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1295 More precisely, return the value assigned by the last call of
1296 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1297 never called with WINDOW as its argument, or the value set by that
1298 function was internally reset since its last call. WINDOW defaults to
1299 the selected window.
1301 When a window is dedicated to its buffer, `display-buffer' will refrain
1302 from displaying another buffer in it. `get-lru-window' and
1303 `get-largest-window' treat dedicated windows specially.
1304 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1305 `kill-buffer' can delete a dedicated window and the containing frame.
1307 Functions like `set-window-buffer' may change the buffer displayed by a
1308 window, unless that window is "strongly" dedicated to its buffer, that
1309 is the value returned by `window-dedicated-p' is t. */)
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 (Lisp_Object window, Lisp_Object flag)
1336 register struct window *w = decode_window (window);
1338 w->dedicated = flag;
1339 return w->dedicated;
1343 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1344 0, 1, 0,
1345 doc: /* Return the parameters of WINDOW and their values.
1346 WINDOW defaults to the selected window. The return value is a list of
1347 elements of the form (PARAMETER . VALUE). */)
1348 (Lisp_Object window)
1350 return Fcopy_alist (decode_window (window)->window_parameters);
1353 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1354 2, 2, 0,
1355 doc: /* Return WINDOW's value for PARAMETER.
1356 WINDOW defaults to the selected window. */)
1357 (Lisp_Object window, Lisp_Object parameter)
1359 Lisp_Object result;
1361 result = Fassq (parameter, decode_window (window)->window_parameters);
1362 return CDR_SAFE (result);
1365 DEFUN ("set-window-parameter", Fset_window_parameter,
1366 Sset_window_parameter, 3, 3, 0,
1367 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1368 WINDOW defaults to the selected window. Return VALUE. */)
1369 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1371 register struct window *w = decode_window (window);
1372 Lisp_Object old_alist_elt;
1374 old_alist_elt = Fassq (parameter, w->window_parameters);
1375 if (NILP (old_alist_elt))
1376 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1377 else
1378 Fsetcdr (old_alist_elt, value);
1379 return value;
1383 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1384 0, 1, 0,
1385 doc: /* Return the display-table that WINDOW is using.
1386 WINDOW defaults to the selected window. */)
1387 (Lisp_Object window)
1389 return decode_window (window)->display_table;
1392 /* Get the display table for use on window W. This is either W's
1393 display table or W's buffer's display table. Ignore the specified
1394 tables if they are not valid; if no valid table is specified,
1395 return 0. */
1397 struct Lisp_Char_Table *
1398 window_display_table (struct window *w)
1400 struct Lisp_Char_Table *dp = NULL;
1402 if (DISP_TABLE_P (w->display_table))
1403 dp = XCHAR_TABLE (w->display_table);
1404 else if (BUFFERP (w->buffer))
1406 struct buffer *b = XBUFFER (w->buffer);
1408 if (DISP_TABLE_P (b->display_table))
1409 dp = XCHAR_TABLE (b->display_table);
1410 else if (DISP_TABLE_P (Vstandard_display_table))
1411 dp = XCHAR_TABLE (Vstandard_display_table);
1414 return dp;
1417 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1418 doc: /* Set WINDOW's display-table to TABLE. */)
1419 (register Lisp_Object window, Lisp_Object table)
1421 register struct window *w;
1423 w = decode_window (window);
1424 w->display_table = table;
1425 return table;
1428 /* Record info on buffer window w is displaying
1429 when it is about to cease to display that buffer. */
1430 static void
1431 unshow_buffer (register struct window *w)
1433 Lisp_Object buf;
1434 struct buffer *b;
1436 buf = w->buffer;
1437 b = XBUFFER (buf);
1438 if (b != XMARKER (w->pointm)->buffer)
1439 abort ();
1441 #if 0
1442 if (w == XWINDOW (selected_window)
1443 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1444 /* Do this except when the selected window's buffer
1445 is being removed from some other window. */
1446 #endif
1447 /* last_window_start records the start position that this buffer
1448 had in the last window to be disconnected from it.
1449 Now that this statement is unconditional,
1450 it is possible for the buffer to be displayed in the
1451 selected window, while last_window_start reflects another
1452 window which was recently showing the same buffer.
1453 Some people might say that might be a good thing. Let's see. */
1454 b->last_window_start = marker_position (w->start);
1456 /* Point in the selected window's buffer
1457 is actually stored in that buffer, and the window's pointm isn't used.
1458 So don't clobber point in that buffer. */
1459 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1460 /* This line helps to fix Horsley's testbug.el bug. */
1461 && !(WINDOWP (b->last_selected_window)
1462 && w != XWINDOW (b->last_selected_window)
1463 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1464 temp_set_point_both (b,
1465 clip_to_bounds (BUF_BEGV (b),
1466 XMARKER (w->pointm)->charpos,
1467 BUF_ZV (b)),
1468 clip_to_bounds (BUF_BEGV_BYTE (b),
1469 marker_byte_position (w->pointm),
1470 BUF_ZV_BYTE (b)));
1472 if (WINDOWP (b->last_selected_window)
1473 && w == XWINDOW (b->last_selected_window))
1474 b->last_selected_window = Qnil;
1477 /* Put replacement into the window structure in place of old. */
1478 static void
1479 replace_window (Lisp_Object old, Lisp_Object replacement)
1481 register Lisp_Object tem;
1482 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1484 /* If OLD is its frame's root_window, then replacement is the new
1485 root_window for that frame. */
1487 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1488 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1490 p->left_col = o->left_col;
1491 p->top_line = o->top_line;
1492 p->total_cols = o->total_cols;
1493 p->total_lines = o->total_lines;
1494 p->desired_matrix = p->current_matrix = 0;
1495 p->vscroll = 0;
1496 memset (&p->cursor, 0, sizeof (p->cursor));
1497 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
1498 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
1499 p->phys_cursor_type = -1;
1500 p->phys_cursor_width = -1;
1501 p->must_be_updated_p = 0;
1502 p->pseudo_window_p = 0;
1503 XSETFASTINT (p->window_end_vpos, 0);
1504 XSETFASTINT (p->window_end_pos, 0);
1505 p->window_end_valid = Qnil;
1506 p->frozen_window_start_p = 0;
1507 p->orig_top_line = p->orig_total_lines = Qnil;
1509 p->next = tem = o->next;
1510 if (!NILP (tem))
1511 XWINDOW (tem)->prev = replacement;
1513 p->prev = tem = o->prev;
1514 if (!NILP (tem))
1515 XWINDOW (tem)->next = replacement;
1517 p->parent = tem = o->parent;
1518 if (!NILP (tem))
1520 if (EQ (XWINDOW (tem)->vchild, old))
1521 XWINDOW (tem)->vchild = replacement;
1522 if (EQ (XWINDOW (tem)->hchild, old))
1523 XWINDOW (tem)->hchild = replacement;
1526 /*** Here, if replacement is a vertical combination
1527 and so is its new parent, we should make replacement's
1528 children be children of that parent instead. ***/
1531 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1532 doc: /* Remove WINDOW from its frame.
1533 WINDOW defaults to the selected window. Return nil.
1534 Signal an error when WINDOW is the only window on its frame. */)
1535 (register Lisp_Object window)
1537 struct frame *f;
1538 if (NILP (window))
1539 window = selected_window;
1540 else
1541 CHECK_LIVE_WINDOW (window);
1543 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1544 delete_window (window);
1546 run_window_configuration_change_hook (f);
1548 return Qnil;
1551 void
1552 delete_window (register Lisp_Object window)
1554 register Lisp_Object tem, parent, sib;
1555 register struct window *p;
1556 register struct window *par;
1557 struct frame *f;
1559 /* Because this function is called by other C code on non-leaf
1560 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1561 so we can't decode_window here. */
1562 CHECK_WINDOW (window);
1563 p = XWINDOW (window);
1565 /* It's a no-op to delete an already-deleted window. */
1566 if (NILP (p->buffer)
1567 && NILP (p->hchild)
1568 && NILP (p->vchild))
1569 return;
1571 parent = p->parent;
1572 if (NILP (parent))
1573 error ("Attempt to delete minibuffer or sole ordinary window");
1574 par = XWINDOW (parent);
1576 windows_or_buffers_changed++;
1577 Vwindow_list = Qnil;
1578 f = XFRAME (WINDOW_FRAME (p));
1579 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1581 /* Are we trying to delete any frame's selected window? */
1583 Lisp_Object swindow, pwindow;
1585 /* See if the frame's selected window is either WINDOW
1586 or any subwindow of it, by finding all that window's parents
1587 and comparing each one with WINDOW. */
1588 swindow = FRAME_SELECTED_WINDOW (f);
1590 while (1)
1592 pwindow = swindow;
1593 while (!NILP (pwindow))
1595 if (EQ (window, pwindow))
1596 break;
1597 pwindow = XWINDOW (pwindow)->parent;
1600 /* If the window being deleted is not a parent of SWINDOW,
1601 then SWINDOW is ok as the new selected window. */
1602 if (!EQ (window, pwindow))
1603 break;
1604 /* Otherwise, try another window for SWINDOW. */
1605 swindow = Fnext_window (swindow, Qlambda, Qnil);
1607 /* If we get back to the frame's selected window,
1608 it means there was no acceptable alternative,
1609 so we cannot delete. */
1610 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1611 error ("Cannot delete window");
1614 /* If we need to change SWINDOW, do it. */
1615 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1617 /* If we're about to delete the selected window on the
1618 selected frame, then we should use Fselect_window to select
1619 the new window. On the other hand, if we're about to
1620 delete the selected window on any other frame, we shouldn't do
1621 anything but set the frame's selected_window slot. */
1622 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1623 Fselect_window (swindow, Qnil);
1624 else
1625 FRAME_SELECTED_WINDOW (f) = swindow;
1629 /* Now we know we can delete this one. */
1630 window_deletion_count++;
1632 tem = p->buffer;
1633 /* tem is null for dummy parent windows
1634 (which have inferiors but not any contents themselves) */
1635 if (!NILP (tem))
1637 unshow_buffer (p);
1638 unchain_marker (XMARKER (p->pointm));
1639 unchain_marker (XMARKER (p->start));
1642 /* Free window glyph matrices. It is sure that they are allocated
1643 again when ADJUST_GLYPHS is called. Block input so that expose
1644 events and other events that access glyph matrices are not
1645 processed while we are changing them. */
1646 BLOCK_INPUT;
1647 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1649 tem = p->next;
1650 if (!NILP (tem))
1651 XWINDOW (tem)->prev = p->prev;
1653 tem = p->prev;
1654 if (!NILP (tem))
1655 XWINDOW (tem)->next = p->next;
1657 if (EQ (window, par->hchild))
1658 par->hchild = p->next;
1659 if (EQ (window, par->vchild))
1660 par->vchild = p->next;
1662 /* Find one of our siblings to give our space to. */
1663 sib = p->prev;
1664 if (NILP (sib))
1666 /* If p gives its space to its next sibling, that sibling needs
1667 to have its top/left side pulled back to where p's is.
1668 set_window_{height,width} will re-position the sibling's
1669 children. */
1670 sib = p->next;
1671 XWINDOW (sib)->top_line = p->top_line;
1672 XWINDOW (sib)->left_col = p->left_col;
1675 /* Stretch that sibling. */
1676 if (!NILP (par->vchild))
1677 set_window_height (sib,
1678 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1680 if (!NILP (par->hchild))
1681 set_window_width (sib,
1682 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1685 /* If parent now has only one child,
1686 put the child into the parent's place. */
1687 tem = par->hchild;
1688 if (NILP (tem))
1689 tem = par->vchild;
1690 if (NILP (XWINDOW (tem)->next)) {
1691 replace_window (parent, tem);
1692 par = XWINDOW (tem);
1695 /* Since we may be deleting combination windows, we must make sure that
1696 not only p but all its children have been marked as deleted. */
1697 if (! NILP (p->hchild))
1698 delete_all_subwindows (XWINDOW (p->hchild));
1699 else if (! NILP (p->vchild))
1700 delete_all_subwindows (XWINDOW (p->vchild));
1702 /* Mark this window as deleted. */
1703 p->buffer = p->hchild = p->vchild = Qnil;
1705 if (! NILP (par->parent))
1706 par = XWINDOW (par->parent);
1708 /* Check if we have a v/hchild with a v/hchild. In that case remove
1709 one of them. */
1711 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1713 p = XWINDOW (par->vchild);
1714 par->vchild = p->vchild;
1715 tem = p->vchild;
1717 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1719 p = XWINDOW (par->hchild);
1720 par->hchild = p->hchild;
1721 tem = p->hchild;
1723 else
1724 p = 0;
1726 if (p)
1728 while (! NILP (tem)) {
1729 XWINDOW (tem)->parent = p->parent;
1730 if (NILP (XWINDOW (tem)->next))
1731 break;
1732 tem = XWINDOW (tem)->next;
1734 if (! NILP (tem)) {
1735 /* The next of the v/hchild we are removing is now the next of the
1736 last child for the v/hchild:
1737 Before v/hchild -> v/hchild -> next1 -> next2
1739 -> next3
1740 After: v/hchild -> next1 -> next2 -> next3
1742 XWINDOW (tem)->next = p->next;
1743 if (! NILP (p->next))
1744 XWINDOW (p->next)->prev = tem;
1746 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1750 /* Adjust glyph matrices. */
1751 adjust_glyphs (f);
1752 UNBLOCK_INPUT;
1757 /***********************************************************************
1758 Window List
1759 ***********************************************************************/
1761 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1762 pointer. This is a callback function for foreach_window, used in
1763 function window_list. */
1765 static int
1766 add_window_to_list (struct window *w, void *user_data)
1768 Lisp_Object *list = (Lisp_Object *) user_data;
1769 Lisp_Object window;
1770 XSETWINDOW (window, w);
1771 *list = Fcons (window, *list);
1772 return 1;
1776 /* Return a list of all windows, for use by next_window. If
1777 Vwindow_list is a list, return that list. Otherwise, build a new
1778 list, cache it in Vwindow_list, and return that. */
1780 static Lisp_Object
1781 window_list (void)
1783 if (!CONSP (Vwindow_list))
1785 Lisp_Object tail;
1787 Vwindow_list = Qnil;
1788 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1790 Lisp_Object args[2];
1792 /* We are visiting windows in canonical order, and add
1793 new windows at the front of args[1], which means we
1794 have to reverse this list at the end. */
1795 args[1] = Qnil;
1796 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1797 args[0] = Vwindow_list;
1798 args[1] = Fnreverse (args[1]);
1799 Vwindow_list = Fnconc (2, args);
1803 return Vwindow_list;
1807 /* Value is non-zero if WINDOW satisfies the constraints given by
1808 OWINDOW, MINIBUF and ALL_FRAMES.
1810 MINIBUF t means WINDOW may be minibuffer windows.
1811 `lambda' means WINDOW may not be a minibuffer window.
1812 a window means a specific minibuffer window
1814 ALL_FRAMES t means search all frames,
1815 nil means search just current frame,
1816 `visible' means search just visible frames,
1817 0 means search visible and iconified frames,
1818 a window means search the frame that window belongs to,
1819 a frame means consider windows on that frame, only. */
1821 static int
1822 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
1824 struct window *w = XWINDOW (window);
1825 struct frame *f = XFRAME (w->frame);
1826 int candidate_p = 1;
1828 if (!BUFFERP (w->buffer))
1829 candidate_p = 0;
1830 else if (MINI_WINDOW_P (w)
1831 && (EQ (minibuf, Qlambda)
1832 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1834 /* If MINIBUF is `lambda' don't consider any mini-windows.
1835 If it is a window, consider only that one. */
1836 candidate_p = 0;
1838 else if (EQ (all_frames, Qt))
1839 candidate_p = 1;
1840 else if (NILP (all_frames))
1842 xassert (WINDOWP (owindow));
1843 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1845 else if (EQ (all_frames, Qvisible))
1847 FRAME_SAMPLE_VISIBILITY (f);
1848 candidate_p = FRAME_VISIBLE_P (f)
1849 && (FRAME_TERMINAL (XFRAME (w->frame))
1850 == FRAME_TERMINAL (XFRAME (selected_frame)));
1853 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1855 FRAME_SAMPLE_VISIBILITY (f);
1856 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
1857 #ifdef HAVE_X_WINDOWS
1858 /* Yuck!! If we've just created the frame and the
1859 window-manager requested the user to place it
1860 manually, the window may still not be considered
1861 `visible'. I'd argue it should be at least
1862 something like `iconified', but don't know how to do
1863 that yet. --Stef */
1864 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
1865 && !f->output_data.x->has_been_visible)
1866 #endif
1868 && (FRAME_TERMINAL (XFRAME (w->frame))
1869 == FRAME_TERMINAL (XFRAME (selected_frame)));
1871 else if (WINDOWP (all_frames))
1872 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1873 || EQ (XWINDOW (all_frames)->frame, w->frame)
1874 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1875 else if (FRAMEP (all_frames))
1876 candidate_p = EQ (all_frames, w->frame);
1878 return candidate_p;
1882 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1883 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1884 ALL_FRAMES. */
1886 static void
1887 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
1889 if (NILP (*window))
1890 *window = selected_window;
1891 else
1892 CHECK_LIVE_WINDOW (*window);
1894 /* MINIBUF nil may or may not include minibuffers. Decide if it
1895 does. */
1896 if (NILP (*minibuf))
1897 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1898 else if (!EQ (*minibuf, Qt))
1899 *minibuf = Qlambda;
1901 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1902 => count none of them, or a specific minibuffer window (the
1903 active one) to count. */
1905 /* ALL_FRAMES nil doesn't specify which frames to include. */
1906 if (NILP (*all_frames))
1907 *all_frames = (!EQ (*minibuf, Qlambda)
1908 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1909 : Qnil);
1910 else if (EQ (*all_frames, Qvisible))
1912 else if (EQ (*all_frames, make_number (0)))
1914 else if (FRAMEP (*all_frames))
1916 else if (!EQ (*all_frames, Qt))
1917 *all_frames = Qnil;
1919 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1920 search just current frame, `visible' meaning search just visible
1921 frames, 0 meaning search visible and iconified frames, or a
1922 window, meaning search the frame that window belongs to, or a
1923 frame, meaning consider windows on that frame, only. */
1927 /* Return the next or previous window of WINDOW in cyclic ordering
1928 of windows. NEXT_P non-zero means return the next window. See the
1929 documentation string of next-window for the meaning of MINIBUF and
1930 ALL_FRAMES. */
1932 static Lisp_Object
1933 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
1935 decode_next_window_args (&window, &minibuf, &all_frames);
1937 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1938 return the first window on the frame. */
1939 if (FRAMEP (all_frames)
1940 && !EQ (all_frames, XWINDOW (window)->frame))
1941 return Fframe_first_window (all_frames);
1943 if (next_p)
1945 Lisp_Object list;
1947 /* Find WINDOW in the list of all windows. */
1948 list = Fmemq (window, window_list ());
1950 /* Scan forward from WINDOW to the end of the window list. */
1951 if (CONSP (list))
1952 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1953 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1954 break;
1956 /* Scan from the start of the window list up to WINDOW. */
1957 if (!CONSP (list))
1958 for (list = Vwindow_list;
1959 CONSP (list) && !EQ (XCAR (list), window);
1960 list = XCDR (list))
1961 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1962 break;
1964 if (CONSP (list))
1965 window = XCAR (list);
1967 else
1969 Lisp_Object candidate, list;
1971 /* Scan through the list of windows for candidates. If there are
1972 candidate windows in front of WINDOW, the last one of these
1973 is the one we want. If there are candidates following WINDOW
1974 in the list, again the last one of these is the one we want. */
1975 candidate = Qnil;
1976 for (list = window_list (); CONSP (list); list = XCDR (list))
1978 if (EQ (XCAR (list), window))
1980 if (WINDOWP (candidate))
1981 break;
1983 else if (candidate_window_p (XCAR (list), window, minibuf,
1984 all_frames))
1985 candidate = XCAR (list);
1988 if (WINDOWP (candidate))
1989 window = candidate;
1992 return window;
1996 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1997 doc: /* Return window following WINDOW in cyclic ordering of windows.
1998 WINDOW defaults to the selected window. The optional arguments
1999 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2001 MINIBUF t means consider the minibuffer window even if the
2002 minibuffer is not active. MINIBUF nil or omitted means consider
2003 the minibuffer window only if the minibuffer is active. Any
2004 other value means do not consider the minibuffer window even if
2005 the minibuffer is active.
2007 Several frames may share a single minibuffer; if the minibuffer
2008 is active, all windows on all frames that share that minibuffer
2009 are considered too. Therefore, if you are using a separate
2010 minibuffer frame and the minibuffer is active and MINIBUF says it
2011 counts, `next-window' considers the windows in the frame from
2012 which you entered the minibuffer, as well as the minibuffer
2013 window.
2015 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
2016 frame, plus the minibuffer window if specified by the MINIBUF
2017 argument, see above. If the minibuffer counts, consider all
2018 windows on all frames that share that minibuffer too.
2019 ALL-FRAMES t means consider all windows on all existing frames.
2020 ALL-FRAMES `visible' means consider all windows on all visible
2021 frames.
2022 ALL-FRAMES 0 means consider all windows on all visible and
2023 iconified frames.
2024 ALL-FRAMES a frame means consider all windows on that frame only.
2025 Anything else means consider all windows on WINDOW's frame and no
2026 others.
2028 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2029 `next-window' to iterate through the entire cycle of acceptable
2030 windows, eventually ending up back at the window you started with.
2031 `previous-window' traverses the same cycle, in the reverse order. */)
2032 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2034 return next_window (window, minibuf, all_frames, 1);
2038 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2039 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
2040 WINDOW defaults to the selected window. The optional arguments
2041 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2042 For the precise meaning of these arguments see `next-window'.
2044 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2045 use `previous-window' to iterate through the entire cycle of
2046 acceptable windows, eventually ending up back at the window you
2047 started with. `next-window' traverses the same cycle, in the
2048 reverse order. */)
2049 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2051 return next_window (window, minibuf, all_frames, 0);
2055 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
2056 doc: /* Select another window in cyclic ordering of windows.
2057 COUNT specifies the number of windows to skip, starting with the
2058 selected window, before making the selection. If COUNT is
2059 positive, skip COUNT windows forwards. If COUNT is negative,
2060 skip -COUNT windows backwards. COUNT zero means do not skip any
2061 window, so select the selected window. In an interactive call,
2062 COUNT is the numeric prefix argument. Return nil.
2064 This function uses `next-window' for finding the window to select.
2065 The argument ALL-FRAMES has the same meaning as in `next-window',
2066 but the MINIBUF argument of `next-window' is always effectively
2067 nil. */)
2068 (Lisp_Object count, Lisp_Object all_frames)
2070 Lisp_Object window;
2071 int i;
2073 CHECK_NUMBER (count);
2074 window = selected_window;
2076 for (i = XINT (count); i > 0; --i)
2077 window = Fnext_window (window, Qnil, all_frames);
2078 for (; i < 0; ++i)
2079 window = Fprevious_window (window, Qnil, all_frames);
2081 Fselect_window (window, Qnil);
2082 return Qnil;
2086 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2087 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2088 FRAME nil or omitted means use the selected frame.
2089 WINDOW nil or omitted means use the selected window.
2090 MINIBUF t means include the minibuffer window, even if it isn't active.
2091 MINIBUF nil or omitted means include the minibuffer window only
2092 if it's active.
2093 MINIBUF neither nil nor t means never include the minibuffer window. */)
2094 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2096 if (NILP (window))
2097 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2098 CHECK_WINDOW (window);
2099 if (NILP (frame))
2100 frame = selected_frame;
2102 if (!EQ (frame, XWINDOW (window)->frame))
2103 error ("Window is on a different frame");
2105 return window_list_1 (window, minibuf, frame);
2109 /* Return a list of windows in cyclic ordering. Arguments are like
2110 for `next-window'. */
2112 static Lisp_Object
2113 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2115 Lisp_Object tail, list, rest;
2117 decode_next_window_args (&window, &minibuf, &all_frames);
2118 list = Qnil;
2120 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2121 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2122 list = Fcons (XCAR (tail), list);
2124 /* Rotate the list to start with WINDOW. */
2125 list = Fnreverse (list);
2126 rest = Fmemq (window, list);
2127 if (!NILP (rest) && !EQ (rest, list))
2129 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2131 XSETCDR (tail, Qnil);
2132 list = nconc2 (rest, list);
2134 return list;
2139 /* Look at all windows, performing an operation specified by TYPE
2140 with argument OBJ.
2141 If FRAMES is Qt, look at all frames;
2142 Qnil, look at just the selected frame;
2143 Qvisible, look at visible frames;
2144 a frame, just look at windows on that frame.
2145 If MINI is non-zero, perform the operation on minibuffer windows too. */
2147 enum window_loop
2149 WINDOW_LOOP_UNUSED,
2150 GET_BUFFER_WINDOW, /* Arg is buffer */
2151 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2152 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2153 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2154 GET_LARGEST_WINDOW,
2155 UNSHOW_BUFFER, /* Arg is buffer */
2156 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2157 CHECK_ALL_WINDOWS
2160 static Lisp_Object
2161 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2163 Lisp_Object window, windows, best_window, frame_arg;
2164 struct frame *f;
2165 struct gcpro gcpro1;
2167 /* If we're only looping through windows on a particular frame,
2168 frame points to that frame. If we're looping through windows
2169 on all frames, frame is 0. */
2170 if (FRAMEP (frames))
2171 f = XFRAME (frames);
2172 else if (NILP (frames))
2173 f = SELECTED_FRAME ();
2174 else
2175 f = NULL;
2177 if (f)
2178 frame_arg = Qlambda;
2179 else if (EQ (frames, make_number (0)))
2180 frame_arg = frames;
2181 else if (EQ (frames, Qvisible))
2182 frame_arg = frames;
2183 else
2184 frame_arg = Qt;
2186 /* frame_arg is Qlambda to stick to one frame,
2187 Qvisible to consider all visible frames,
2188 or Qt otherwise. */
2190 /* Pick a window to start with. */
2191 if (WINDOWP (obj))
2192 window = obj;
2193 else if (f)
2194 window = FRAME_SELECTED_WINDOW (f);
2195 else
2196 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2198 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2199 GCPRO1 (windows);
2200 best_window = Qnil;
2202 for (; CONSP (windows); windows = XCDR (windows))
2204 struct window *w;
2206 window = XCAR (windows);
2207 w = XWINDOW (window);
2209 /* Note that we do not pay attention here to whether the frame
2210 is visible, since Fwindow_list skips non-visible frames if
2211 that is desired, under the control of frame_arg. */
2212 if (!MINI_WINDOW_P (w)
2213 /* For UNSHOW_BUFFER, we must always consider all windows. */
2214 || type == UNSHOW_BUFFER
2215 || (mini && minibuf_level > 0))
2216 switch (type)
2218 case GET_BUFFER_WINDOW:
2219 if (EQ (w->buffer, obj)
2220 /* Don't find any minibuffer window
2221 except the one that is currently in use. */
2222 && (MINI_WINDOW_P (w)
2223 ? EQ (window, minibuf_window)
2224 : 1))
2226 if (NILP (best_window))
2227 best_window = window;
2228 else if (EQ (window, selected_window))
2229 /* Prefer to return selected-window. */
2230 RETURN_UNGCPRO (window);
2231 else if (EQ (Fwindow_frame (window), selected_frame))
2232 /* Prefer windows on the current frame. */
2233 best_window = window;
2235 break;
2237 case GET_LRU_WINDOW:
2238 /* `obj' is an integer encoding a bitvector.
2239 `obj & 1' means consider only full-width windows.
2240 `obj & 2' means consider also dedicated windows. */
2241 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2242 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2243 /* Minibuffer windows are always ignored. */
2244 || MINI_WINDOW_P (w))
2245 break;
2246 if (NILP (best_window)
2247 || (XFASTINT (XWINDOW (best_window)->use_time)
2248 > XFASTINT (w->use_time)))
2249 best_window = window;
2250 break;
2252 case DELETE_OTHER_WINDOWS:
2253 if (!EQ (window, obj))
2254 Fdelete_window (window);
2255 break;
2257 case DELETE_BUFFER_WINDOWS:
2258 if (EQ (w->buffer, obj))
2260 struct frame *f = XFRAME (WINDOW_FRAME (w));
2262 /* If this window is dedicated, and in a frame of its own,
2263 kill the frame. */
2264 if (EQ (window, FRAME_ROOT_WINDOW (f))
2265 && !NILP (w->dedicated)
2266 && other_visible_frames (f))
2268 /* Skip the other windows on this frame.
2269 There might be one, the minibuffer! */
2270 while (CONSP (XCDR (windows))
2271 && EQ (XWINDOW (XCAR (windows))->frame,
2272 XWINDOW (XCAR (XCDR (windows)))->frame))
2273 windows = XCDR (windows);
2275 /* Now we can safely delete the frame. */
2276 delete_frame (w->frame, Qnil);
2278 else if (NILP (w->parent))
2280 /* If we're deleting the buffer displayed in the
2281 only window on the frame, find a new buffer to
2282 display there. */
2283 Lisp_Object buffer;
2284 buffer = Fother_buffer (obj, Qnil, w->frame);
2285 /* Reset dedicated state of window. */
2286 w->dedicated = Qnil;
2287 Fset_window_buffer (window, buffer, Qnil);
2288 if (EQ (window, selected_window))
2289 Fset_buffer (w->buffer);
2291 else
2292 Fdelete_window (window);
2294 break;
2296 case GET_LARGEST_WINDOW:
2297 { /* nil `obj' means to ignore dedicated windows. */
2298 /* Ignore dedicated windows and minibuffers. */
2299 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2300 break;
2302 if (NILP (best_window))
2303 best_window = window;
2304 else
2306 struct window *b = XWINDOW (best_window);
2307 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2308 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2309 best_window = window;
2312 break;
2314 case UNSHOW_BUFFER:
2315 if (EQ (w->buffer, obj))
2317 Lisp_Object buffer;
2318 struct frame *f = XFRAME (w->frame);
2320 /* Find another buffer to show in this window. */
2321 buffer = Fother_buffer (obj, Qnil, w->frame);
2323 /* If this window is dedicated, and in a frame of its own,
2324 kill the frame. */
2325 if (EQ (window, FRAME_ROOT_WINDOW (f))
2326 && !NILP (w->dedicated)
2327 && other_visible_frames (f))
2329 /* Skip the other windows on this frame.
2330 There might be one, the minibuffer! */
2331 while (CONSP (XCDR (windows))
2332 && EQ (XWINDOW (XCAR (windows))->frame,
2333 XWINDOW (XCAR (XCDR (windows)))->frame))
2334 windows = XCDR (windows);
2336 /* Now we can safely delete the frame. */
2337 delete_frame (w->frame, Qnil);
2339 else if (!NILP (w->dedicated) && !NILP (w->parent))
2341 Lisp_Object window;
2342 XSETWINDOW (window, w);
2343 /* If this window is dedicated and not the only window
2344 in its frame, then kill it. */
2345 Fdelete_window (window);
2347 else
2349 /* Otherwise show a different buffer in the window. */
2350 w->dedicated = Qnil;
2351 Fset_window_buffer (window, buffer, Qnil);
2352 if (EQ (window, selected_window))
2353 Fset_buffer (w->buffer);
2356 break;
2358 case REDISPLAY_BUFFER_WINDOWS:
2359 if (EQ (w->buffer, obj))
2361 mark_window_display_accurate (window, 0);
2362 w->update_mode_line = Qt;
2363 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2364 ++update_mode_lines;
2365 best_window = window;
2367 break;
2369 /* Check for a window that has a killed buffer. */
2370 case CHECK_ALL_WINDOWS:
2371 if (! NILP (w->buffer)
2372 && NILP (XBUFFER (w->buffer)->name))
2373 abort ();
2374 break;
2376 case WINDOW_LOOP_UNUSED:
2377 break;
2381 UNGCPRO;
2382 return best_window;
2385 /* Used for debugging. Abort if any window has a dead buffer. */
2387 void
2388 check_all_windows (void)
2390 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2393 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
2394 doc: /* Return WINDOW's use time.
2395 WINDOW defaults to the selected window. The window with the highest use
2396 time is the most recently selected one. The window with the lowest use
2397 time is the least recently selected one. */)
2398 (Lisp_Object window)
2400 return decode_window (window)->use_time;
2403 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2404 doc: /* Return the window least recently selected or used for display.
2405 \(LRU means Least Recently Used.)
2407 Return a full-width window if possible.
2408 A minibuffer window is never a candidate.
2409 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2410 so if all windows are dedicated, the value is nil.
2411 If optional argument FRAME is `visible', search all visible frames.
2412 If FRAME is 0, search all visible and iconified frames.
2413 If FRAME is t, search all frames.
2414 If FRAME is nil, search only the selected frame.
2415 If FRAME is a frame, search only that frame. */)
2416 (Lisp_Object frame, Lisp_Object dedicated)
2418 register Lisp_Object w;
2419 /* First try for a window that is full-width */
2420 w = window_loop (GET_LRU_WINDOW,
2421 NILP (dedicated) ? make_number (1) : make_number (3),
2422 0, frame);
2423 if (!NILP (w) && !EQ (w, selected_window))
2424 return w;
2425 /* If none of them, try the rest */
2426 return window_loop (GET_LRU_WINDOW,
2427 NILP (dedicated) ? make_number (0) : make_number (2),
2428 0, frame);
2431 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2432 doc: /* Return the largest window in area.
2433 A minibuffer window is never a candidate.
2434 A dedicated window is never a candidate unless DEDICATED is non-nil,
2435 so if all windows are dedicated, the value is nil.
2436 If optional argument FRAME is `visible', search all visible frames.
2437 If FRAME is 0, search all visible and iconified frames.
2438 If FRAME is t, search all frames.
2439 If FRAME is nil, search only the selected frame.
2440 If FRAME is a frame, search only that frame. */)
2441 (Lisp_Object frame, Lisp_Object dedicated)
2443 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2444 frame);
2447 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2448 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2449 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to the
2450 current buffer.
2451 If optional argument FRAME is `visible', search all visible frames.
2452 If optional argument FRAME is 0, search all visible and iconified frames.
2453 If FRAME is t, search all frames.
2454 If FRAME is nil, search only the selected frame.
2455 If FRAME is a frame, search only that frame. */)
2456 (Lisp_Object buffer_or_name, Lisp_Object frame)
2458 Lisp_Object buffer;
2460 if (NILP (buffer_or_name))
2461 buffer = Fcurrent_buffer ();
2462 else
2463 buffer = Fget_buffer (buffer_or_name);
2465 if (BUFFERP (buffer))
2466 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2467 else
2468 return Qnil;
2471 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2472 0, 1, "",
2473 doc: /* Make WINDOW (or the selected window) fill its frame.
2474 Only the frame WINDOW is on is affected.
2475 This function tries to reduce display jumps by keeping the text
2476 previously visible in WINDOW in the same place on the frame. Doing this
2477 depends on the value of (window-start WINDOW), so if calling this
2478 function in a program gives strange scrolling, make sure the
2479 window-start value is reasonable when this function is called. */)
2480 (Lisp_Object window)
2482 struct window *w;
2483 EMACS_INT startpos;
2484 int top, new_top;
2486 if (NILP (window))
2487 window = selected_window;
2488 else
2489 CHECK_LIVE_WINDOW (window);
2490 w = XWINDOW (window);
2492 startpos = marker_position (w->start);
2493 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2495 if (MINI_WINDOW_P (w) && top > 0)
2496 error ("Can't expand minibuffer to full frame");
2498 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2500 /* Try to minimize scrolling, by setting the window start to the point
2501 will cause the text at the old window start to be at the same place
2502 on the frame. But don't try to do this if the window start is
2503 outside the visible portion (as might happen when the display is
2504 not current, due to typeahead). */
2505 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2506 if (new_top != top
2507 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2508 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2510 struct position pos;
2511 struct buffer *obuf = current_buffer;
2513 Fset_buffer (w->buffer);
2514 /* This computation used to temporarily move point, but that can
2515 have unwanted side effects due to text properties. */
2516 pos = *vmotion (startpos, -top, w);
2518 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2519 w->window_end_valid = Qnil;
2520 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2521 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2522 : Qnil);
2523 /* We need to do this, so that the window-scroll-functions
2524 get called. */
2525 w->optional_new_start = Qt;
2527 set_buffer_internal (obuf);
2530 return Qnil;
2533 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2534 0, 2, "bDelete windows on (buffer): ",
2535 doc: /* Delete all windows showing BUFFER-OR-NAME.
2536 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2537 defaults to the current buffer.
2539 Optional second argument FRAME controls which frames are affected.
2540 If optional argument FRAME is `visible', search all visible frames.
2541 If FRAME is 0, search all visible and iconified frames.
2542 If FRAME is nil, search all frames.
2543 If FRAME is t, search only the selected frame.
2544 If FRAME is a frame, search only that frame.
2545 When a window showing BUFFER-OR-NAME is dedicated and the only window of
2546 its frame, that frame is deleted when there are other frames left. */)
2547 (Lisp_Object buffer_or_name, Lisp_Object frame)
2549 Lisp_Object buffer;
2551 /* FRAME uses t and nil to mean the opposite of what window_loop
2552 expects. */
2553 if (NILP (frame))
2554 frame = Qt;
2555 else if (EQ (frame, Qt))
2556 frame = Qnil;
2558 if (NILP (buffer_or_name))
2559 buffer = Fcurrent_buffer ();
2560 else
2562 buffer = Fget_buffer (buffer_or_name);
2563 CHECK_BUFFER (buffer);
2566 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2568 return Qnil;
2571 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2572 Sreplace_buffer_in_windows,
2573 0, 1, "bReplace buffer in windows: ",
2574 doc: /* Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2575 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2576 defaults to the current buffer.
2578 When a window showing BUFFER-OR-NAME is dedicated that window is
2579 deleted. If that window is the only window on its frame, that frame is
2580 deleted too when there are other frames left. If there are no other
2581 frames left, some other buffer is displayed in that window. */)
2582 (Lisp_Object buffer_or_name)
2584 Lisp_Object buffer;
2586 if (NILP (buffer_or_name))
2587 buffer = Fcurrent_buffer ();
2588 else
2590 buffer = Fget_buffer (buffer_or_name);
2591 CHECK_BUFFER (buffer);
2594 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2596 return Qnil;
2599 /* Replace BUFFER with some other buffer in all windows
2600 of all frames, even those on other keyboards. */
2602 void
2603 replace_buffer_in_all_windows (Lisp_Object buffer)
2605 Lisp_Object tail, frame;
2607 /* A single call to window_loop won't do the job
2608 because it only considers frames on the current keyboard.
2609 So loop manually over frames, and handle each one. */
2610 FOR_EACH_FRAME (tail, frame)
2611 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2614 /* Set the height of WINDOW and all its inferiors. */
2616 /* The smallest acceptable dimensions for a window. Anything smaller
2617 might crash Emacs. */
2619 #define MIN_SAFE_WINDOW_WIDTH (2)
2620 #define MIN_SAFE_WINDOW_HEIGHT (1)
2622 /* For wp non-zero the total number of columns of window w. Otherwise
2623 the total number of lines of w. */
2625 #define WINDOW_TOTAL_SIZE(w, wp) \
2626 (wp ? WINDOW_TOTAL_COLS (w) : WINDOW_TOTAL_LINES (w))
2628 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2629 minimum allowable size. */
2631 void
2632 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
2634 /* For height, we have to see:
2635 how many windows the frame has at minimum (one or two),
2636 and whether it has a menu bar or other special stuff at the top. */
2637 int min_height
2638 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2639 ? MIN_SAFE_WINDOW_HEIGHT
2640 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2642 if (FRAME_TOP_MARGIN (frame) > 0)
2643 min_height += FRAME_TOP_MARGIN (frame);
2645 if (*rows < min_height)
2646 *rows = min_height;
2647 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2648 *cols = MIN_SAFE_WINDOW_WIDTH;
2651 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2652 check if W's width can be changed, otherwise check W's height.
2653 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2654 siblings, too. If none of the siblings is resizable, WINDOW isn't
2655 either. */
2657 static int
2658 window_fixed_size_p (struct window *w, int width_p, int check_siblings_p)
2660 int fixed_p;
2661 struct window *c;
2663 if (!NILP (w->hchild))
2665 c = XWINDOW (w->hchild);
2667 if (width_p)
2669 /* A horizontal combination is fixed-width if all of if its
2670 children are. */
2671 while (c && window_fixed_size_p (c, width_p, 0))
2672 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2673 fixed_p = c == NULL;
2675 else
2677 /* A horizontal combination is fixed-height if one of if its
2678 children is. */
2679 while (c && !window_fixed_size_p (c, width_p, 0))
2680 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2681 fixed_p = c != NULL;
2684 else if (!NILP (w->vchild))
2686 c = XWINDOW (w->vchild);
2688 if (width_p)
2690 /* A vertical combination is fixed-width if one of if its
2691 children is. */
2692 while (c && !window_fixed_size_p (c, width_p, 0))
2693 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2694 fixed_p = c != NULL;
2696 else
2698 /* A vertical combination is fixed-height if all of if its
2699 children are. */
2700 while (c && window_fixed_size_p (c, width_p, 0))
2701 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2702 fixed_p = c == NULL;
2705 else if (BUFFERP (w->buffer))
2707 struct buffer *old = current_buffer;
2708 Lisp_Object val;
2710 current_buffer = XBUFFER (w->buffer);
2711 val = find_symbol_value (Qwindow_size_fixed);
2712 current_buffer = old;
2714 fixed_p = 0;
2715 if (!EQ (val, Qunbound))
2717 fixed_p = !NILP (val);
2719 if (fixed_p
2720 && ((EQ (val, Qheight) && width_p)
2721 || (EQ (val, Qwidth) && !width_p)))
2722 fixed_p = 0;
2725 /* Can't tell if this one is resizable without looking at
2726 siblings. If all siblings are fixed-size this one is too. */
2727 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2729 Lisp_Object child;
2731 for (child = w->prev; WINDOWP (child); child = XWINDOW (child)->prev)
2732 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2733 break;
2735 if (NILP (child))
2736 for (child = w->next; WINDOWP (child); child = XWINDOW (child)->next)
2737 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2738 break;
2740 if (NILP (child))
2741 fixed_p = 1;
2744 else
2745 fixed_p = 1;
2747 return fixed_p;
2750 /* Return minimum size of leaf window W. WIDTH_P non-zero means return
2751 the minimum width of W, WIDTH_P zero means return the minimum height
2752 of W. SAFE_P non-zero means ignore window-min-height|width but just
2753 return values that won't crash Emacs and don't hide components like
2754 fringes, scrollbars, or modelines. If WIDTH_P is zero and W is the
2755 minibuffer window, always return 1. */
2757 static int
2758 window_min_size_2 (struct window *w, int width_p, int safe_p)
2760 /* We should consider buffer-local values of window_min_height and
2761 window_min_width here. */
2762 if (width_p)
2764 int safe_size = (MIN_SAFE_WINDOW_WIDTH
2765 + WINDOW_FRINGE_COLS (w)
2766 + WINDOW_SCROLL_BAR_COLS (w));
2768 return safe_p ? safe_size : max (window_min_width, safe_size);
2770 else if (MINI_WINDOW_P (w))
2771 return 1;
2772 else
2774 int safe_size = (MIN_SAFE_WINDOW_HEIGHT
2775 + ((BUFFERP (w->buffer)
2776 && !NILP (XBUFFER (w->buffer)->mode_line_format))
2777 ? 1 : 0));
2779 return safe_p ? safe_size : max (window_min_height, safe_size);
2783 /* Return minimum size of window W, not taking fixed-width windows into
2784 account. WIDTH_P non-zero means return the minimum width, otherwise
2785 return the minimum height. SAFE_P non-zero means ignore
2786 window-min-height|width but just return values that won't crash Emacs
2787 and don't hide components like fringes, scrollbars, or modelines. If
2788 W is a combination window, compute the minimum size from the minimum
2789 sizes of W's children. */
2791 static int
2792 window_min_size_1 (struct window *w, int width_p, int safe_p)
2794 struct window *c;
2795 int size;
2797 if (!NILP (w->hchild))
2799 /* W is a horizontal combination. */
2800 c = XWINDOW (w->hchild);
2801 size = 0;
2803 if (width_p)
2805 /* The minimum width of a horizontal combination is the sum of
2806 the minimum widths of its children. */
2807 while (c)
2809 size += window_min_size_1 (c, 1, safe_p);
2810 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2813 else
2815 /* The minimum height of a horizontal combination is the
2816 maximum of the minimum heights of its children. */
2817 while (c)
2819 size = max (window_min_size_1 (c, 0, safe_p), size);
2820 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2824 else if (!NILP (w->vchild))
2826 /* W is a vertical combination. */
2827 c = XWINDOW (w->vchild);
2828 size = 0;
2830 if (width_p)
2832 /* The minimum width of a vertical combination is the maximum
2833 of the minimum widths of its children. */
2834 while (c)
2836 size = max (window_min_size_1 (c, 1, safe_p), size);
2837 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2840 else
2842 /* The minimum height of a vertical combination is the sum of
2843 the minimum height of its children. */
2844 while (c)
2846 size += window_min_size_1 (c, 0, safe_p);
2847 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2851 else
2852 /* W is a leaf window. */
2853 size = window_min_size_2 (w, width_p, safe_p);
2855 return size;
2858 /* Return the minimum size of window W, taking fixed-size windows into
2859 account. WIDTH_P non-zero means return the minimum width, otherwise
2860 return the minimum height. SAFE_P non-zero means ignore
2861 window-min-height|width but just return values that won't crash Emacs
2862 and don't hide components like fringes, scrollbars, or modelines.
2863 IGNORE_FIXED_P non-zero means ignore if W is fixed-size. Set *FIXED
2864 to 1 if W is fixed-size unless FIXED is null. */
2866 static int
2867 window_min_size (struct window *w, int width_p, int safe_p, int ignore_fixed_p, int *fixed)
2869 int size, fixed_p;
2871 if (ignore_fixed_p)
2872 fixed_p = 0;
2873 else
2874 fixed_p = window_fixed_size_p (w, width_p, 1);
2876 if (fixed)
2877 *fixed = fixed_p;
2879 if (fixed_p)
2880 size = WINDOW_TOTAL_SIZE (w, width_p);
2881 else
2882 size = window_min_size_1 (w, width_p, safe_p);
2884 return size;
2888 /* Adjust the margins of window W if text area is too small.
2889 Return 1 if window width is ok after adjustment; 0 if window
2890 is still too narrow. */
2892 static int
2893 adjust_window_margins (struct window *w)
2895 int box_cols = (WINDOW_TOTAL_COLS (w)
2896 - WINDOW_FRINGE_COLS (w)
2897 - WINDOW_SCROLL_BAR_COLS (w));
2898 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2899 + WINDOW_RIGHT_MARGIN_COLS (w));
2901 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2902 return 1;
2904 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2905 return 0;
2907 /* Window's text area is too narrow, but reducing the window
2908 margins will fix that. */
2909 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2910 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2912 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2913 w->left_margin_cols = w->right_margin_cols
2914 = make_number (margin_cols/2);
2915 else
2916 w->right_margin_cols = make_number (margin_cols);
2918 else
2919 w->left_margin_cols = make_number (margin_cols);
2920 return 1;
2923 /* Calculate new sizes for windows in the list FORWARD when their
2924 compound size goes from TOTAL to SIZE. TOTAL must be greater than
2925 SIZE. The number of windows in FORWARD is NCHILDREN, and the number
2926 that can shrink is SHRINKABLE. Fixed-size windows may be shrunk if
2927 and only if RESIZE_FIXED_P is non-zero. WIDTH_P non-zero means
2928 shrink columns, otherwise shrink lines.
2930 SAFE_P zero means windows may be sized down to window-min-height
2931 lines (window-min-window columns for WIDTH_P non-zero). SAFE_P
2932 non-zero means windows may be sized down to their minimum safe sizes
2933 taking into account the space needed to display modelines, fringes,
2934 and scrollbars.
2936 This function returns an allocated array of new sizes that the caller
2937 must free. A size -1 means the window is fixed and RESIZE_FIXED_P is
2938 zero. A size zero means the window shall be deleted. Array index 0
2939 refers to the first window in FORWARD, 1 to the second, and so on.
2941 This function resizes windows proportionally to their size. It also
2942 tries to preserve smaller windows by resizing larger windows before
2943 resizing any window to zero. If resize_proportionally is non-nil for
2944 a specific window, it will attempt to strictly resize that window
2945 proportionally, even at the expense of deleting smaller windows. */
2946 static int *
2947 shrink_windows (int total, int size, int nchildren, int shrinkable,
2948 int resize_fixed_p, Lisp_Object forward, int width_p, int safe_p)
2950 int available_resize = 0;
2951 int *new_sizes, *min_sizes;
2952 struct window *c;
2953 Lisp_Object child;
2954 int smallest = total;
2955 int total_removed = 0;
2956 int total_shrink = total - size;
2957 int i;
2959 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2960 min_sizes = xmalloc (sizeof (*min_sizes) * nchildren);
2962 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2964 int child_size;
2966 c = XWINDOW (child);
2967 child_size = WINDOW_TOTAL_SIZE (c, width_p);
2969 if (!resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2970 new_sizes[i] = -1;
2971 else
2973 new_sizes[i] = child_size;
2974 min_sizes[i] = window_min_size_1 (c, width_p, safe_p);
2975 if (child_size > min_sizes[i]
2976 && NILP (c->resize_proportionally))
2977 available_resize += child_size - min_sizes[i];
2980 /* We might need to shrink some windows to zero. Find the smallest
2981 windows and set them to 0 until we can fulfil the new size. */
2983 while (shrinkable > 1 && size + available_resize < total)
2985 for (i = 0; i < nchildren; ++i)
2986 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2987 smallest = new_sizes[i];
2989 for (i = 0; i < nchildren; ++i)
2990 if (new_sizes[i] == smallest)
2992 /* Resize this window down to zero. */
2993 new_sizes[i] = 0;
2994 if (smallest > min_sizes[i])
2995 available_resize -= smallest - min_sizes[i];
2996 available_resize += smallest;
2997 --shrinkable;
2998 total_removed += smallest;
3000 /* We don't know what the smallest is now. */
3001 smallest = total;
3003 /* Out of for, just remove one window at the time and
3004 check again if we have enough space. */
3005 break;
3009 /* Now, calculate the new sizes. Try to shrink each window
3010 proportional to its size. */
3011 for (i = 0; i < nchildren; ++i)
3013 if (new_sizes[i] > min_sizes[i])
3015 int to_shrink = total_shrink * new_sizes[i] / total;
3017 if (new_sizes[i] - to_shrink < min_sizes[i])
3018 to_shrink = new_sizes[i] - min_sizes[i];
3019 new_sizes[i] -= to_shrink;
3020 total_removed += to_shrink;
3024 /* Any reminder due to rounding, we just subtract from windows
3025 that are left and still can be shrunk. */
3026 while (total_shrink > total_removed)
3028 int nonzero_sizes = 0;
3029 int nonzero_idx = -1;
3031 for (i = 0; i < nchildren; ++i)
3032 if (new_sizes[i] > 0)
3034 ++nonzero_sizes;
3035 nonzero_idx = i;
3038 for (i = 0; i < nchildren; ++i)
3039 if (new_sizes[i] > min_sizes[i])
3041 --new_sizes[i];
3042 ++total_removed;
3044 /* Out of for, just shrink one window at the time and
3045 check again if we have enough space. */
3046 break;
3049 /* Special case, only one window left. */
3050 if (nonzero_sizes == 1)
3051 break;
3054 /* Any surplus due to rounding, we add to windows that are left. */
3055 while (total_shrink < total_removed)
3057 for (i = 0; i < nchildren; ++i)
3059 if (new_sizes[i] != 0 && total_shrink < total_removed)
3061 ++new_sizes[i];
3062 --total_removed;
3063 break;
3068 xfree (min_sizes);
3070 return new_sizes;
3073 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
3074 WINDOW's width. Resize WINDOW's children, if any, so that they keep
3075 their proportionate size relative to WINDOW.
3077 If FIRST_ONLY is 1, change only the first of WINDOW's children when
3078 they are in series. If LAST_ONLY is 1, change only the last of
3079 WINDOW's children when they are in series.
3081 Propagate WINDOW's top or left edge position to children. Delete
3082 windows that become too small unless NODELETE_P is 1. When
3083 NODELETE_P equals 2 do not honor settings for window-min-height and
3084 window-min-width when resizing windows but use safe defaults instead.
3085 This should give better behavior when resizing frames. */
3087 static void
3088 size_window (Lisp_Object window, int size, int width_p, int nodelete_p, int first_only, int last_only)
3090 struct window *w = XWINDOW (window);
3091 struct window *c;
3092 Lisp_Object child, *forward, *sideward;
3093 int old_size = WINDOW_TOTAL_SIZE (w, width_p);
3095 size = max (0, size);
3097 /* Delete WINDOW if it's too small. */
3098 if (nodelete_p != 1 && !NILP (w->parent)
3099 && size < window_min_size_1 (w, width_p, nodelete_p == 2))
3101 delete_window (window);
3102 return;
3105 /* Set redisplay hints. */
3106 w->last_modified = make_number (0);
3107 w->last_overlay_modified = make_number (0);
3108 windows_or_buffers_changed++;
3109 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
3111 if (width_p)
3113 sideward = &w->vchild;
3114 forward = &w->hchild;
3115 w->total_cols = make_number (size);
3116 adjust_window_margins (w);
3118 else
3120 sideward = &w->hchild;
3121 forward = &w->vchild;
3122 w->total_lines = make_number (size);
3123 w->orig_total_lines = Qnil;
3126 if (!NILP (*sideward))
3128 /* We have a chain of parallel siblings whose size should all change. */
3129 for (child = *sideward; !NILP (child); child = c->next)
3131 c = XWINDOW (child);
3132 if (width_p)
3133 c->left_col = w->left_col;
3134 else
3135 c->top_line = w->top_line;
3136 size_window (child, size, width_p, nodelete_p,
3137 first_only, last_only);
3140 else if (!NILP (*forward) && last_only)
3142 /* Change the last in a series of siblings. */
3143 Lisp_Object last_child;
3144 int child_size;
3146 for (child = *forward; !NILP (child); child = c->next)
3148 c = XWINDOW (child);
3149 last_child = child;
3152 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3153 size_window (last_child, size - old_size + child_size,
3154 width_p, nodelete_p, first_only, last_only);
3156 else if (!NILP (*forward) && first_only)
3158 /* Change the first in a series of siblings. */
3159 int child_size;
3161 child = *forward;
3162 c = XWINDOW (child);
3164 if (width_p)
3165 c->left_col = w->left_col;
3166 else
3167 c->top_line = w->top_line;
3169 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3170 size_window (child, size - old_size + child_size,
3171 width_p, nodelete_p, first_only, last_only);
3173 else if (!NILP (*forward))
3175 int fixed_size, each, extra, n;
3176 int resize_fixed_p, nfixed;
3177 int last_pos, first_pos, nchildren, total;
3178 int *new_sizes = NULL;
3180 /* Determine the fixed-size portion of this window, and the
3181 number of child windows. */
3182 fixed_size = nchildren = nfixed = total = 0;
3183 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
3185 int child_size;
3187 c = XWINDOW (child);
3188 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3189 total += child_size;
3191 if (window_fixed_size_p (c, width_p, 0))
3193 fixed_size += child_size;
3194 ++nfixed;
3198 /* If the new size is smaller than fixed_size, or if there
3199 aren't any resizable windows, allow resizing fixed-size
3200 windows. */
3201 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3203 /* Compute how many lines/columns to add/remove to each child. The
3204 value of extra takes care of rounding errors. */
3205 n = resize_fixed_p ? nchildren : nchildren - nfixed;
3206 if (size < total && n > 1)
3207 new_sizes = shrink_windows (total, size, nchildren, n,
3208 resize_fixed_p, *forward, width_p,
3209 nodelete_p == 2);
3210 else
3212 each = (size - total) / n;
3213 extra = (size - total) - n * each;
3216 /* Compute new children heights and edge positions. */
3217 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
3218 last_pos = first_pos;
3219 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
3221 int new_size, old_size;
3223 c = XWINDOW (child);
3224 old_size = WINDOW_TOTAL_SIZE (c, width_p);
3225 new_size = old_size;
3227 /* The top or left edge position of this child equals the
3228 bottom or right edge of its predecessor. */
3229 if (width_p)
3230 c->left_col = make_number (last_pos);
3231 else
3232 c->top_line = make_number (last_pos);
3234 /* If this child can be resized, do it. */
3235 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3237 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
3238 extra = 0;
3241 /* Set new size. Note that size_window also propagates
3242 edge positions to children, so it's not a no-op if we
3243 didn't change the child's size. */
3244 size_window (child, new_size, width_p, 1, first_only, last_only);
3246 /* Remember the bottom/right edge position of this child; it
3247 will be used to set the top/left edge of the next child. */
3248 last_pos += new_size;
3251 xfree (new_sizes);
3253 /* We should have covered the parent exactly with child windows. */
3254 xassert (size == last_pos - first_pos);
3256 /* Now delete any children that became too small. */
3257 if (nodelete_p != 1)
3258 for (child = *forward; !NILP (child); child = c->next)
3260 int child_size;
3262 c = XWINDOW (child);
3263 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3264 size_window (child, child_size, width_p, nodelete_p,
3265 first_only, last_only);
3270 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3271 WINDOW's children. NODELETE zero means windows that have become
3272 smaller than window-min-height in the process may be deleted.
3273 NODELETE 1 means never delete windows that become too small in the
3274 process. (The caller should check later and do so if appropriate.)
3275 NODELETE 2 means delete only windows that have become too small to be
3276 displayed correctly. */
3278 void
3279 set_window_height (Lisp_Object window, int height, int nodelete)
3281 size_window (window, height, 0, nodelete, 0, 0);
3284 /* Set WINDOW's width to WIDTH, and recursively change the width of
3285 WINDOW's children. NODELETE zero means windows that have become
3286 smaller than window-min-width in the process may be deleted.
3287 NODELETE 1 means never delete windows that become too small in the
3288 process. (The caller should check later and do so if appropriate.)
3289 NODELETE 2 means delete only windows that have become too small to be
3290 displayed correctly. */
3292 void
3293 set_window_width (Lisp_Object window, int width, int nodelete)
3295 size_window (window, width, 1, nodelete, 0, 0);
3298 /* Change window heights in windows rooted in WINDOW by N lines. */
3300 void
3301 change_window_heights (Lisp_Object window, int n)
3303 struct window *w = XWINDOW (window);
3305 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3306 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3308 if (INTEGERP (w->orig_top_line))
3309 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3310 if (INTEGERP (w->orig_total_lines))
3311 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3313 /* Handle just the top child in a vertical split. */
3314 if (!NILP (w->vchild))
3315 change_window_heights (w->vchild, n);
3317 /* Adjust all children in a horizontal split. */
3318 for (window = w->hchild; !NILP (window); window = w->next)
3320 w = XWINDOW (window);
3321 change_window_heights (window, n);
3326 int window_select_count;
3328 EXFUN (Fset_window_fringes, 4);
3329 EXFUN (Fset_window_scroll_bars, 4);
3331 static void
3332 run_funs (Lisp_Object funs)
3334 for (; CONSP (funs); funs = XCDR (funs))
3335 if (!EQ (XCAR (funs), Qt))
3336 call0 (XCAR (funs));
3339 static Lisp_Object select_window_norecord (Lisp_Object window);
3340 static Lisp_Object select_frame_norecord (Lisp_Object frame);
3342 void
3343 run_window_configuration_change_hook (struct frame *f)
3345 int count = SPECPDL_INDEX ();
3346 Lisp_Object frame, global_wcch
3347 = Fdefault_value (Qwindow_configuration_change_hook);
3348 XSETFRAME (frame, f);
3350 if (NILP (Vrun_hooks))
3351 return;
3353 if (SELECTED_FRAME () != f)
3355 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3356 Fselect_frame (frame, Qt);
3359 /* Use the right buffer. Matters when running the local hooks. */
3360 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3362 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3363 Fset_buffer (Fwindow_buffer (Qnil));
3366 /* Look for buffer-local values. */
3368 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3369 for (; CONSP (windows); windows = XCDR (windows))
3371 Lisp_Object window = XCAR (windows);
3372 Lisp_Object buffer = Fwindow_buffer (window);
3373 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3374 buffer)))
3376 int count = SPECPDL_INDEX ();
3377 record_unwind_protect (select_window_norecord, Fselected_window ());
3378 select_window_norecord (window);
3379 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3380 buffer));
3381 unbind_to (count, Qnil);
3386 run_funs (global_wcch);
3387 unbind_to (count, Qnil);
3390 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3391 means it's allowed to run hooks. See make_frame for a case where
3392 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3393 margins, fringes, and scroll-bar settings of the window are not
3394 reset from the buffer's local settings. */
3396 void
3397 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
3399 struct window *w = XWINDOW (window);
3400 struct buffer *b = XBUFFER (buffer);
3401 int count = SPECPDL_INDEX ();
3402 int samebuf = EQ (buffer, w->buffer);
3404 w->buffer = buffer;
3406 if (EQ (window, selected_window))
3407 b->last_selected_window = window;
3409 /* Let redisplay errors through. */
3410 b->display_error_modiff = 0;
3412 /* Update time stamps of buffer display. */
3413 if (INTEGERP (b->display_count))
3414 XSETINT (b->display_count, XINT (b->display_count) + 1);
3415 b->display_time = Fcurrent_time ();
3417 XSETFASTINT (w->window_end_pos, 0);
3418 XSETFASTINT (w->window_end_vpos, 0);
3419 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3420 w->window_end_valid = Qnil;
3421 if (!(keep_margins_p && samebuf))
3422 { /* If we're not actually changing the buffer, don't reset hscroll and
3423 vscroll. This case happens for example when called from
3424 change_frame_size_1, where we use a dummy call to
3425 Fset_window_buffer on the frame's selected window (and no other)
3426 just in order to run window-configuration-change-hook.
3427 Resetting hscroll and vscroll here is problematic for things like
3428 image-mode and doc-view-mode since it resets the image's position
3429 whenever we resize the frame. */
3430 w->hscroll = w->min_hscroll = make_number (0);
3431 w->vscroll = 0;
3432 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3433 set_marker_restricted (w->start,
3434 make_number (b->last_window_start),
3435 buffer);
3436 w->start_at_line_beg = Qnil;
3437 w->force_start = Qnil;
3438 XSETFASTINT (w->last_modified, 0);
3439 XSETFASTINT (w->last_overlay_modified, 0);
3441 /* Maybe we could move this into the `if' but it's not obviously safe and
3442 I doubt it's worth the trouble. */
3443 windows_or_buffers_changed++;
3445 /* We must select BUFFER for running the window-scroll-functions. */
3446 /* We can't check ! NILP (Vwindow_scroll_functions) here
3447 because that might itself be a local variable. */
3448 if (window_initialized)
3450 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3451 Fset_buffer (buffer);
3454 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3456 if (!keep_margins_p)
3458 /* Set left and right marginal area width etc. from buffer. */
3460 /* This may call adjust_window_margins three times, so
3461 temporarily disable window margins. */
3462 Lisp_Object save_left = w->left_margin_cols;
3463 Lisp_Object save_right = w->right_margin_cols;
3465 w->left_margin_cols = w->right_margin_cols = Qnil;
3467 Fset_window_fringes (window,
3468 b->left_fringe_width, b->right_fringe_width,
3469 b->fringes_outside_margins);
3471 Fset_window_scroll_bars (window,
3472 b->scroll_bar_width,
3473 b->vertical_scroll_bar_type, Qnil);
3475 w->left_margin_cols = save_left;
3476 w->right_margin_cols = save_right;
3478 Fset_window_margins (window,
3479 b->left_margin_cols, b->right_margin_cols);
3482 if (run_hooks_p)
3484 if (! NILP (Vwindow_scroll_functions))
3485 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3486 Fmarker_position (w->start));
3487 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3490 unbind_to (count, Qnil);
3494 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3495 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3496 WINDOW defaults to the selected window. BUFFER-OR-NAME must be a buffer
3497 or the name of an existing buffer. Optional third argument KEEP-MARGINS
3498 non-nil means that WINDOW's current display margins, fringe widths, and
3499 scroll bar settings are preserved; the default is to reset these from
3500 the local settings for BUFFER-OR-NAME or the frame defaults. Return nil.
3502 This function throws an error when WINDOW is strongly dedicated to its
3503 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3504 already display BUFFER-OR-NAME.
3506 This function runs `window-scroll-functions' before running
3507 `window-configuration-change-hook'. */)
3508 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3510 register Lisp_Object tem, buffer;
3511 register struct window *w = decode_window (window);
3513 XSETWINDOW (window, w);
3514 buffer = Fget_buffer (buffer_or_name);
3515 CHECK_BUFFER (buffer);
3516 if (NILP (XBUFFER (buffer)->name))
3517 error ("Attempt to display deleted buffer");
3519 tem = w->buffer;
3520 if (NILP (tem))
3521 error ("Window is deleted");
3522 else if (!EQ (tem, Qt))
3523 /* w->buffer is t when the window is first being set up. */
3525 if (!EQ (tem, buffer))
3526 if (EQ (w->dedicated, Qt))
3527 error ("Window is dedicated to `%s'", SDATA (XBUFFER (tem)->name));
3528 else
3529 w->dedicated = Qnil;
3531 unshow_buffer (w);
3534 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3535 return Qnil;
3538 /* Note that selected_window can be nil when this is called from
3539 Fset_window_configuration. */
3541 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3542 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3543 If WINDOW is not already selected, make WINDOW's buffer current
3544 and make WINDOW the frame's selected window. Return WINDOW.
3545 Optional second arg NORECORD non-nil means do not put this buffer
3546 at the front of the list of recently selected ones and do not
3547 make this window the most recently selected one.
3549 Note that the main editor command loop selects the buffer of the
3550 selected window before each command. */)
3551 (register Lisp_Object window, Lisp_Object norecord)
3553 register struct window *w;
3554 register struct window *ow;
3555 struct frame *sf;
3557 CHECK_LIVE_WINDOW (window);
3559 w = XWINDOW (window);
3560 w->frozen_window_start_p = 0;
3562 if (NILP (norecord))
3564 ++window_select_count;
3565 XSETFASTINT (w->use_time, window_select_count);
3566 record_buffer (w->buffer);
3569 if (EQ (window, selected_window))
3570 return window;
3572 sf = SELECTED_FRAME ();
3573 if (XFRAME (WINDOW_FRAME (w)) != sf)
3575 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3576 /* Use this rather than Fhandle_switch_frame
3577 so that FRAME_FOCUS_FRAME is moved appropriately as we
3578 move around in the state where a minibuffer in a separate
3579 frame is active. */
3580 Fselect_frame (WINDOW_FRAME (w), norecord);
3581 /* Fselect_frame called us back so we've done all the work already. */
3582 eassert (EQ (window, selected_window));
3583 return window;
3585 else
3586 sf->selected_window = window;
3588 /* Store the current buffer's actual point into the
3589 old selected window. It belongs to that window,
3590 and when the window is not selected, must be in the window. */
3591 if (!NILP (selected_window))
3593 ow = XWINDOW (selected_window);
3594 if (! NILP (ow->buffer))
3595 set_marker_both (ow->pointm, ow->buffer,
3596 BUF_PT (XBUFFER (ow->buffer)),
3597 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3600 selected_window = window;
3602 Fset_buffer (w->buffer);
3604 XBUFFER (w->buffer)->last_selected_window = window;
3606 /* Go to the point recorded in the window.
3607 This is important when the buffer is in more
3608 than one window. It also matters when
3609 redisplay_window has altered point after scrolling,
3610 because it makes the change only in the window. */
3612 register EMACS_INT new_point = marker_position (w->pointm);
3613 if (new_point < BEGV)
3614 SET_PT (BEGV);
3615 else if (new_point > ZV)
3616 SET_PT (ZV);
3617 else
3618 SET_PT (new_point);
3621 windows_or_buffers_changed++;
3622 return window;
3625 static Lisp_Object
3626 select_window_norecord (Lisp_Object window)
3628 return WINDOW_LIVE_P (window)
3629 ? Fselect_window (window, Qt) : selected_window;
3632 static Lisp_Object
3633 select_frame_norecord (Lisp_Object frame)
3635 return FRAME_LIVE_P (XFRAME (frame))
3636 ? Fselect_frame (frame, Qt) : selected_frame;
3639 Lisp_Object
3640 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3642 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3645 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3646 0, 1, 0,
3647 doc: /* Force all windows to be updated on next redisplay.
3648 If optional arg OBJECT is a window, force redisplay of that window only.
3649 If OBJECT is a buffer or buffer name, force redisplay of all windows
3650 displaying that buffer. */)
3651 (Lisp_Object object)
3653 if (NILP (object))
3655 windows_or_buffers_changed++;
3656 update_mode_lines++;
3657 return Qt;
3660 if (WINDOWP (object))
3662 struct window *w = XWINDOW (object);
3663 mark_window_display_accurate (object, 0);
3664 w->update_mode_line = Qt;
3665 if (BUFFERP (w->buffer))
3666 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3667 ++update_mode_lines;
3668 return Qt;
3671 if (STRINGP (object))
3672 object = Fget_buffer (object);
3673 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3675 /* Walk all windows looking for buffer, and force update
3676 of each of those windows. */
3678 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3679 return NILP (object) ? Qnil : Qt;
3682 /* If nothing suitable was found, just return.
3683 We could signal an error, but this feature will typically be used
3684 asynchronously in timers or process sentinels, so we don't. */
3685 return Qnil;
3689 void
3690 temp_output_buffer_show (register Lisp_Object buf)
3692 register struct buffer *old = current_buffer;
3693 register Lisp_Object window;
3694 register struct window *w;
3696 XBUFFER (buf)->directory = current_buffer->directory;
3698 Fset_buffer (buf);
3699 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3700 BEGV = BEG;
3701 ZV = Z;
3702 SET_PT (BEG);
3703 #if 0 /* rms: there should be no reason for this. */
3704 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3705 #endif
3706 set_buffer_internal (old);
3708 if (!NILP (Vtemp_buffer_show_function))
3709 call1 (Vtemp_buffer_show_function, buf);
3710 else
3712 window = display_buffer (buf, Qnil, Qnil);
3714 if (!EQ (XWINDOW (window)->frame, selected_frame))
3715 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3716 Vminibuf_scroll_window = window;
3717 w = XWINDOW (window);
3718 XSETFASTINT (w->hscroll, 0);
3719 XSETFASTINT (w->min_hscroll, 0);
3720 set_marker_restricted_both (w->start, buf, BEG, BEG);
3721 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3723 /* Run temp-buffer-show-hook, with the chosen window selected
3724 and its buffer current. */
3726 if (!NILP (Vrun_hooks)
3727 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3728 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3730 int count = SPECPDL_INDEX ();
3731 Lisp_Object prev_window, prev_buffer;
3732 prev_window = selected_window;
3733 XSETBUFFER (prev_buffer, old);
3735 /* Select the window that was chosen, for running the hook.
3736 Note: Both Fselect_window and select_window_norecord may
3737 set-buffer to the buffer displayed in the window,
3738 so we need to save the current buffer. --stef */
3739 record_unwind_protect (Fset_buffer, prev_buffer);
3740 record_unwind_protect (select_window_norecord, prev_window);
3741 Fselect_window (window, Qt);
3742 Fset_buffer (w->buffer);
3743 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3744 unbind_to (count, Qnil);
3749 static void
3750 make_dummy_parent (Lisp_Object window)
3752 Lisp_Object new;
3753 register struct window *o, *p;
3754 int i;
3756 o = XWINDOW (window);
3757 p = allocate_window ();
3758 for (i = 0; i < VECSIZE (struct window); ++i)
3759 ((struct Lisp_Vector *) p)->contents[i]
3760 = ((struct Lisp_Vector *)o)->contents[i];
3761 XSETWINDOW (new, p);
3763 ++sequence_number;
3764 XSETFASTINT (p->sequence_number, sequence_number);
3766 /* Put new into window structure in place of window */
3767 replace_window (window, new);
3769 o->next = Qnil;
3770 o->prev = Qnil;
3771 o->vchild = Qnil;
3772 o->hchild = Qnil;
3773 o->parent = new;
3775 p->start = Qnil;
3776 p->pointm = Qnil;
3777 p->buffer = Qnil;
3780 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3781 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3782 WINDOW defaults to selected one and SIZE to half its size.
3783 If optional third arg HORIZONTAL is non-nil, split side by side and put
3784 SIZE columns in the first of the pair. In that case, SIZE includes that
3785 window's scroll bar, or the divider column to its right.
3786 Interactively, all arguments are nil.
3787 Returns the newly created window (which is the lower or rightmost one).
3788 The upper or leftmost window is the original one, and remains selected
3789 if it was selected before.
3791 See Info node `(elisp)Splitting Windows' for more details and examples. */)
3792 (Lisp_Object window, Lisp_Object size, Lisp_Object horizontal)
3794 register Lisp_Object new;
3795 register struct window *o, *p;
3796 FRAME_PTR fo;
3797 register int size_int;
3799 if (NILP (window))
3800 window = selected_window;
3801 else
3802 CHECK_LIVE_WINDOW (window);
3804 o = XWINDOW (window);
3805 fo = XFRAME (WINDOW_FRAME (o));
3807 if (NILP (size))
3809 if (!NILP (horizontal))
3810 /* Calculate the size of the left-hand window, by dividing
3811 the usable space in columns by two.
3812 We round up, since the left-hand window may include
3813 a dividing line, while the right-hand may not. */
3814 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3815 else
3816 size_int = XFASTINT (o->total_lines) >> 1;
3818 else
3820 CHECK_NUMBER (size);
3821 size_int = XINT (size);
3824 if (MINI_WINDOW_P (o))
3825 error ("Attempt to split minibuffer window");
3826 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
3827 error ("Attempt to split fixed-size window");
3829 if (NILP (horizontal))
3831 int window_safe_height = window_min_size_2 (o, 0, 0);
3833 if (size_int < window_safe_height)
3834 error ("Window height %d too small (after splitting)", size_int);
3835 if (size_int + window_safe_height > XFASTINT (o->total_lines))
3836 error ("Window height %d too small (after splitting)",
3837 XFASTINT (o->total_lines) - size_int);
3838 if (NILP (o->parent)
3839 || NILP (XWINDOW (o->parent)->vchild))
3841 make_dummy_parent (window);
3842 new = o->parent;
3843 XWINDOW (new)->vchild = window;
3846 else
3848 int window_safe_width = window_min_size_2 (o, 1, 0);
3850 if (size_int < window_safe_width)
3851 error ("Window width %d too small (after splitting)", size_int);
3852 if (size_int + window_safe_width > XFASTINT (o->total_cols))
3853 error ("Window width %d too small (after splitting)",
3854 XFASTINT (o->total_cols) - size_int);
3855 if (NILP (o->parent)
3856 || NILP (XWINDOW (o->parent)->hchild))
3858 make_dummy_parent (window);
3859 new = o->parent;
3860 XWINDOW (new)->hchild = window;
3864 /* Now we know that window's parent is a vertical combination
3865 if we are dividing vertically, or a horizontal combination
3866 if we are making side-by-side windows */
3868 windows_or_buffers_changed++;
3869 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3870 new = make_window ();
3871 p = XWINDOW (new);
3873 p->frame = o->frame;
3874 p->next = o->next;
3875 if (!NILP (p->next))
3876 XWINDOW (p->next)->prev = new;
3877 p->prev = window;
3878 o->next = new;
3879 p->parent = o->parent;
3880 p->buffer = Qt;
3881 p->window_end_valid = Qnil;
3882 memset (&p->last_cursor, 0, sizeof p->last_cursor);
3884 /* Duplicate special geometry settings. */
3886 p->left_margin_cols = o->left_margin_cols;
3887 p->right_margin_cols = o->right_margin_cols;
3888 p->left_fringe_width = o->left_fringe_width;
3889 p->right_fringe_width = o->right_fringe_width;
3890 p->fringes_outside_margins = o->fringes_outside_margins;
3891 p->scroll_bar_width = o->scroll_bar_width;
3892 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3894 /* Apportion the available frame space among the two new windows */
3896 if (!NILP (horizontal))
3898 p->total_lines = o->total_lines;
3899 p->top_line = o->top_line;
3900 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3901 XSETFASTINT (o->total_cols, size_int);
3902 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3903 adjust_window_margins (p);
3904 adjust_window_margins (o);
3906 else
3908 p->left_col = o->left_col;
3909 p->total_cols = o->total_cols;
3910 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3911 XSETFASTINT (o->total_lines, size_int);
3912 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3915 /* Adjust glyph matrices. */
3916 adjust_glyphs (fo);
3918 Fset_window_buffer (new, o->buffer, Qt);
3919 return new;
3922 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3923 doc: /* Make selected window SIZE lines taller.
3924 Interactively, if no argument is given, make the selected window one
3925 line taller. If optional argument HORIZONTAL is non-nil, make selected
3926 window wider by SIZE columns. If SIZE is negative, shrink the window by
3927 -SIZE lines or columns. Return nil.
3929 This function can delete windows if they get too small. The size of
3930 fixed size windows is not altered by this function. */)
3931 (Lisp_Object size, Lisp_Object horizontal)
3933 CHECK_NUMBER (size);
3934 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
3936 run_window_configuration_change_hook (SELECTED_FRAME ());
3938 return Qnil;
3941 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3942 doc: /* Make selected window SIZE lines smaller.
3943 Interactively, if no argument is given, make the selected window one
3944 line smaller. If optional argument HORIZONTAL is non-nil, make the
3945 window narrower by SIZE columns. If SIZE is negative, enlarge selected
3946 window by -SIZE lines or columns. Return nil.
3948 This function can delete windows if they get too small. The size of
3949 fixed size windows is not altered by this function. */)
3950 (Lisp_Object size, Lisp_Object horizontal)
3952 CHECK_NUMBER (size);
3953 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
3955 run_window_configuration_change_hook (SELECTED_FRAME ());
3957 return Qnil;
3961 window_height (Lisp_Object window)
3963 register struct window *p = XWINDOW (window);
3964 return WINDOW_TOTAL_LINES (p);
3968 window_width (Lisp_Object window)
3970 register struct window *p = XWINDOW (window);
3971 return WINDOW_TOTAL_COLS (p);
3975 #define CURBEG(w) \
3976 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3978 #define CURSIZE(w) \
3979 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3982 /* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
3983 horizontally; zero means do it vertically.
3985 Siblings of the selected window are resized to fulfill the size
3986 request. If they become too small in the process, they may be
3987 deleted. */
3989 static void
3990 enlarge_window (Lisp_Object window, int delta, int horiz_flag)
3992 Lisp_Object parent, next, prev;
3993 struct window *p;
3994 Lisp_Object *sizep;
3995 int maximum;
3996 int (*sizefun) (Lisp_Object)
3997 = horiz_flag ? window_width : window_height;
3998 void (*setsizefun) (Lisp_Object, int, int)
3999 = (horiz_flag ? set_window_width : set_window_height);
4001 /* Give up if this window cannot be resized. */
4002 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4003 error ("Window is not resizable");
4005 /* Find the parent of the selected window. */
4006 while (1)
4008 p = XWINDOW (window);
4009 parent = p->parent;
4011 if (NILP (parent))
4013 if (horiz_flag)
4014 error ("No other window to side of this one");
4015 break;
4018 if (horiz_flag
4019 ? !NILP (XWINDOW (parent)->hchild)
4020 : !NILP (XWINDOW (parent)->vchild))
4021 break;
4023 window = parent;
4026 sizep = &CURSIZE (window);
4029 register int maxdelta;
4031 /* Compute the maximum size increment this window can have. */
4033 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4034 /* This is a main window followed by a minibuffer. */
4035 : !NILP (p->next) ? ((*sizefun) (p->next)
4036 - window_min_size (XWINDOW (p->next),
4037 horiz_flag, 0, 0, 0))
4038 /* This is a minibuffer following a main window. */
4039 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4040 - window_min_size (XWINDOW (p->prev),
4041 horiz_flag, 0, 0, 0))
4042 /* This is a frame with only one window, a minibuffer-only
4043 or a minibufferless frame. */
4044 : (delta = 0));
4046 if (delta > maxdelta)
4047 /* This case traps trying to make the minibuffer
4048 the full frame, or make the only window aside from the
4049 minibuffer the full frame. */
4050 delta = maxdelta;
4053 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4054 horiz_flag, 0, 0, 0))
4056 delete_window (window);
4057 return;
4060 if (delta == 0)
4061 return;
4063 /* Find the total we can get from other siblings without deleting them. */
4064 maximum = 0;
4065 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
4066 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4067 horiz_flag, 0, 0, 0);
4068 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
4069 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4070 horiz_flag, 0, 0, 0);
4072 /* If we can get it all from them without deleting them, do so. */
4073 if (delta <= maximum)
4075 Lisp_Object first_unaffected;
4076 Lisp_Object first_affected;
4077 int fixed_p;
4079 next = p->next;
4080 prev = p->prev;
4081 first_affected = window;
4082 /* Look at one sibling at a time,
4083 moving away from this window in both directions alternately,
4084 and take as much as we can get without deleting that sibling. */
4085 while (delta != 0
4086 && (!NILP (next) || !NILP (prev)))
4088 if (! NILP (next))
4090 int this_one = ((*sizefun) (next)
4091 - window_min_size (XWINDOW (next), horiz_flag,
4092 0, 0, &fixed_p));
4093 if (!fixed_p)
4095 if (this_one > delta)
4096 this_one = delta;
4098 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4099 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4101 delta -= this_one;
4104 next = XWINDOW (next)->next;
4107 if (delta == 0)
4108 break;
4110 if (! NILP (prev))
4112 int this_one = ((*sizefun) (prev)
4113 - window_min_size (XWINDOW (prev), horiz_flag,
4114 0, 0, &fixed_p));
4115 if (!fixed_p)
4117 if (this_one > delta)
4118 this_one = delta;
4120 first_affected = prev;
4122 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4123 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4125 delta -= this_one;
4128 prev = XWINDOW (prev)->prev;
4132 xassert (delta == 0);
4134 /* Now recalculate the edge positions of all the windows affected,
4135 based on the new sizes. */
4136 first_unaffected = next;
4137 prev = first_affected;
4138 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4139 prev = next, next = XWINDOW (next)->next)
4141 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4142 /* This does not change size of NEXT,
4143 but it propagates the new top edge to its children */
4144 (*setsizefun) (next, (*sizefun) (next), 0);
4147 else
4149 register int delta1;
4150 register int opht = (*sizefun) (parent);
4152 if (opht <= XINT (*sizep) + delta)
4154 /* If trying to grow this window to or beyond size of the parent,
4155 just delete all the sibling windows. */
4156 Lisp_Object start, tem, next;
4158 start = XWINDOW (parent)->vchild;
4159 if (NILP (start))
4160 start = XWINDOW (parent)->hchild;
4162 /* Delete any siblings that come after WINDOW. */
4163 tem = XWINDOW (window)->next;
4164 while (! NILP (tem))
4166 next = XWINDOW (tem)->next;
4167 delete_window (tem);
4168 tem = next;
4171 /* Delete any siblings that come after WINDOW.
4172 Note that if START is not WINDOW, then WINDOW still
4173 has siblings, so WINDOW has not yet replaced its parent. */
4174 tem = start;
4175 while (! EQ (tem, window))
4177 next = XWINDOW (tem)->next;
4178 delete_window (tem);
4179 tem = next;
4182 else
4184 /* Otherwise, make delta1 just right so that if we add
4185 delta1 lines to this window and to the parent, and then
4186 shrink the parent back to its original size, the new
4187 proportional size of this window will increase by delta.
4189 The function size_window will compute the new height h'
4190 of the window from delta1 as:
4192 e = delta1/n
4193 x = delta1 - delta1/n * n for the 1st resizable child
4194 h' = h + e + x
4196 where n is the number of children that can be resized.
4197 We can ignore x by choosing a delta1 that is a multiple of
4198 n. We want the height of this window to come out as
4200 h' = h + delta
4202 So, delta1 must be
4204 h + e = h + delta
4205 delta1/n = delta
4206 delta1 = n * delta.
4208 The number of children n equals the number of resizable
4209 children of this window + 1 because we know window itself
4210 is resizable (otherwise we would have signaled an error).
4212 This reasoning is not correct when other windows become too
4213 small and shrink_windows refuses to delete them. Below we
4214 use resize_proportionally to work around this problem. */
4216 struct window *w = XWINDOW (window);
4217 Lisp_Object s;
4218 int n = 1;
4220 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
4221 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4222 ++n;
4223 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
4224 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4225 ++n;
4227 delta1 = n * delta;
4229 /* Add delta1 lines or columns to this window, and to the parent,
4230 keeping things consistent while not affecting siblings. */
4231 XSETINT (CURSIZE (parent), opht + delta1);
4232 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4234 /* Squeeze out delta1 lines or columns from our parent,
4235 shrinking this window and siblings proportionately. This
4236 brings parent back to correct size. Delta1 was calculated
4237 so this makes this window the desired size, taking it all
4238 out of the siblings.
4240 Temporarily set resize_proportionally to Qt to assure that,
4241 if necessary, shrink_windows deletes smaller windows rather
4242 than shrink this window. */
4243 w->resize_proportionally = Qt;
4244 (*setsizefun) (parent, opht, 0);
4245 w->resize_proportionally = Qnil;
4249 XSETFASTINT (p->last_modified, 0);
4250 XSETFASTINT (p->last_overlay_modified, 0);
4252 /* Adjust glyph matrices. */
4253 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4257 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4258 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4259 zero means adjust the height, moving the bottom edge.
4261 Following siblings of the selected window are resized to fulfill
4262 the size request. If they become too small in the process, they
4263 are not deleted; instead, we signal an error. */
4265 static void
4266 adjust_window_trailing_edge (Lisp_Object window, int delta, int horiz_flag)
4268 Lisp_Object parent, child;
4269 struct window *p;
4270 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4271 int delcount = window_deletion_count;
4273 CHECK_WINDOW (window);
4275 /* Give up if this window cannot be resized. */
4276 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4277 error ("Window is not resizable");
4279 while (1)
4281 Lisp_Object first_parallel = Qnil;
4283 if (NILP (window))
4285 /* This happens if WINDOW on the previous iteration was
4286 at top level of the window tree. */
4287 Fset_window_configuration (old_config);
4288 error ("Specified window edge is fixed");
4291 p = XWINDOW (window);
4292 parent = p->parent;
4294 /* See if this level has windows in parallel in the specified
4295 direction. If so, set FIRST_PARALLEL to the first one. */
4296 if (horiz_flag)
4298 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4299 first_parallel = XWINDOW (parent)->vchild;
4300 else if (NILP (parent) && !NILP (p->next))
4302 /* Handle the vertical chain of main window and minibuffer
4303 which has no parent. */
4304 first_parallel = window;
4305 while (! NILP (XWINDOW (first_parallel)->prev))
4306 first_parallel = XWINDOW (first_parallel)->prev;
4309 else
4311 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4312 first_parallel = XWINDOW (parent)->hchild;
4315 /* If this level's succession is in the desired dimension,
4316 and this window is the last one, and there is no higher level,
4317 its trailing edge is fixed. */
4318 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4319 && NILP (parent))
4321 Fset_window_configuration (old_config);
4322 error ("Specified window edge is fixed");
4325 /* Don't make this window too small. */
4326 if (XINT (CURSIZE (window)) + delta
4327 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
4329 Fset_window_configuration (old_config);
4330 error ("Cannot adjust window size as specified");
4333 /* Clear out some redisplay caches. */
4334 XSETFASTINT (p->last_modified, 0);
4335 XSETFASTINT (p->last_overlay_modified, 0);
4337 /* Adjust this window's edge. */
4338 XSETINT (CURSIZE (window),
4339 XINT (CURSIZE (window)) + delta);
4341 /* If this window has following siblings in the desired dimension,
4342 make them smaller, and exit the loop.
4344 (If we reach the top of the tree and can never do this,
4345 we will fail and report an error, above.) */
4346 if (NILP (first_parallel))
4348 if (!NILP (p->next))
4350 /* This may happen for the minibuffer. In that case
4351 the window_deletion_count check below does not work. */
4352 if (XINT (CURSIZE (p->next)) - delta <= 0)
4354 Fset_window_configuration (old_config);
4355 error ("Cannot adjust window size as specified");
4358 XSETINT (CURBEG (p->next),
4359 XINT (CURBEG (p->next)) + delta);
4360 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4361 horiz_flag, 0, 1, 0);
4362 break;
4365 else
4366 /* Here we have a chain of parallel siblings, in the other dimension.
4367 Change the size of the other siblings. */
4368 for (child = first_parallel;
4369 ! NILP (child);
4370 child = XWINDOW (child)->next)
4371 if (! EQ (child, window))
4372 size_window (child, XINT (CURSIZE (child)) + delta,
4373 horiz_flag, 0, 0, 1);
4375 window = parent;
4378 /* If we made a window so small it got deleted,
4379 we failed. Report failure. */
4380 if (delcount != window_deletion_count)
4382 Fset_window_configuration (old_config);
4383 error ("Cannot adjust window size as specified");
4386 /* Adjust glyph matrices. */
4387 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4390 #undef CURBEG
4391 #undef CURSIZE
4393 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4394 Sadjust_window_trailing_edge, 3, 3, 0,
4395 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4396 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4397 Otherwise, adjust the height, moving the bottom edge.
4399 Following siblings of the selected window are resized to fulfill
4400 the size request. If they become too small in the process, they
4401 are not deleted; instead, we signal an error. */)
4402 (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal)
4404 CHECK_NUMBER (delta);
4405 if (NILP (window))
4406 window = selected_window;
4407 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4409 run_window_configuration_change_hook
4410 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4412 return Qnil;
4417 /***********************************************************************
4418 Resizing Mini-Windows
4419 ***********************************************************************/
4421 static void shrink_window_lowest_first (struct window *, int);
4423 enum save_restore_action
4425 CHECK_ORIG_SIZES,
4426 SAVE_ORIG_SIZES,
4427 RESTORE_ORIG_SIZES
4430 static int save_restore_orig_size (struct window *,
4431 enum save_restore_action);
4433 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4434 from lowest windows first. */
4436 static void
4437 shrink_window_lowest_first (struct window *w, int height)
4439 struct window *c;
4440 Lisp_Object child;
4441 int old_height;
4443 xassert (!MINI_WINDOW_P (w));
4445 /* Set redisplay hints. */
4446 XSETFASTINT (w->last_modified, 0);
4447 XSETFASTINT (w->last_overlay_modified, 0);
4448 windows_or_buffers_changed++;
4449 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4451 old_height = XFASTINT (w->total_lines);
4452 XSETFASTINT (w->total_lines, height);
4454 if (!NILP (w->hchild))
4456 for (child = w->hchild; !NILP (child); child = c->next)
4458 c = XWINDOW (child);
4459 c->top_line = w->top_line;
4460 shrink_window_lowest_first (c, height);
4463 else if (!NILP (w->vchild))
4465 Lisp_Object last_child;
4466 int delta = old_height - height;
4467 int last_top;
4469 last_child = Qnil;
4471 /* Find the last child. We are taking space from lowest windows
4472 first, so we iterate over children from the last child
4473 backwards. */
4474 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
4475 last_child = child;
4477 /* Size children down to their safe heights. */
4478 for (child = last_child; delta && !NILP (child); child = c->prev)
4480 int this_one;
4482 c = XWINDOW (child);
4483 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
4485 if (this_one > delta)
4486 this_one = delta;
4488 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4489 delta -= this_one;
4492 /* Compute new positions. */
4493 last_top = XINT (w->top_line);
4494 for (child = w->vchild; !NILP (child); child = c->next)
4496 c = XWINDOW (child);
4497 c->top_line = make_number (last_top);
4498 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4499 last_top += XFASTINT (c->total_lines);
4505 /* Save, restore, or check positions and sizes in the window tree
4506 rooted at W. ACTION says what to do.
4508 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4509 orig_total_lines members are valid for all windows in the window
4510 tree. Value is non-zero if they are valid.
4512 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4513 orig_top_line and orig_total_lines for all windows in the tree.
4515 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4516 stored in orig_top_line and orig_total_lines for all windows. */
4518 static int
4519 save_restore_orig_size (struct window *w, enum save_restore_action action)
4521 int success_p = 1;
4523 while (w)
4525 if (!NILP (w->hchild))
4527 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4528 success_p = 0;
4530 else if (!NILP (w->vchild))
4532 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4533 success_p = 0;
4536 switch (action)
4538 case CHECK_ORIG_SIZES:
4539 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4540 return 0;
4541 break;
4543 case SAVE_ORIG_SIZES:
4544 w->orig_top_line = w->top_line;
4545 w->orig_total_lines = w->total_lines;
4546 XSETFASTINT (w->last_modified, 0);
4547 XSETFASTINT (w->last_overlay_modified, 0);
4548 break;
4550 case RESTORE_ORIG_SIZES:
4551 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4552 w->top_line = w->orig_top_line;
4553 w->total_lines = w->orig_total_lines;
4554 w->orig_total_lines = w->orig_top_line = Qnil;
4555 XSETFASTINT (w->last_modified, 0);
4556 XSETFASTINT (w->last_overlay_modified, 0);
4557 break;
4559 default:
4560 abort ();
4563 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4566 return success_p;
4570 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4571 without deleting other windows. */
4573 void
4574 grow_mini_window (struct window *w, int delta)
4576 struct frame *f = XFRAME (w->frame);
4577 struct window *root;
4579 xassert (MINI_WINDOW_P (w));
4580 /* Commenting out the following assertion goes against the stated interface
4581 of the function, but it currently does not seem to do anything useful.
4582 See discussion of this issue in the thread for bug#4534.
4583 xassert (delta >= 0); */
4585 /* Compute how much we can enlarge the mini-window without deleting
4586 other windows. */
4587 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4588 if (delta > 0)
4590 int min_height = window_min_size (root, 0, 0, 0, 0);
4591 if (XFASTINT (root->total_lines) - delta < min_height)
4592 /* Note that the root window may already be smaller than
4593 min_height. */
4594 delta = max (0, XFASTINT (root->total_lines) - min_height);
4597 if (delta)
4599 /* Save original window sizes and positions, if not already done. */
4600 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4601 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4603 /* Shrink other windows. */
4604 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4606 /* Grow the mini-window. */
4607 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4608 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4609 XSETFASTINT (w->last_modified, 0);
4610 XSETFASTINT (w->last_overlay_modified, 0);
4612 adjust_glyphs (f);
4617 /* Shrink mini-window W. If there is recorded info about window sizes
4618 before a call to grow_mini_window, restore recorded window sizes.
4619 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4620 line. */
4622 void
4623 shrink_mini_window (struct window *w)
4625 struct frame *f = XFRAME (w->frame);
4626 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4628 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4630 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4631 adjust_glyphs (f);
4632 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4633 windows_or_buffers_changed = 1;
4635 else if (XFASTINT (w->total_lines) > 1)
4637 /* Distribute the additional lines of the mini-window
4638 among the other windows. */
4639 Lisp_Object window;
4640 XSETWINDOW (window, w);
4641 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4647 /* Mark window cursors off for all windows in the window tree rooted
4648 at W by setting their phys_cursor_on_p flag to zero. Called from
4649 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4650 the frame are cleared. */
4652 void
4653 mark_window_cursors_off (struct window *w)
4655 while (w)
4657 if (!NILP (w->hchild))
4658 mark_window_cursors_off (XWINDOW (w->hchild));
4659 else if (!NILP (w->vchild))
4660 mark_window_cursors_off (XWINDOW (w->vchild));
4661 else
4662 w->phys_cursor_on_p = 0;
4664 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4669 /* Return number of lines of text (not counting mode lines) in W. */
4672 window_internal_height (struct window *w)
4674 int ht = XFASTINT (w->total_lines);
4676 if (!MINI_WINDOW_P (w))
4678 if (!NILP (w->parent)
4679 || !NILP (w->vchild)
4680 || !NILP (w->hchild)
4681 || !NILP (w->next)
4682 || !NILP (w->prev)
4683 || WINDOW_WANTS_MODELINE_P (w))
4684 --ht;
4686 if (WINDOW_WANTS_HEADER_LINE_P (w))
4687 --ht;
4690 return ht;
4694 /* Return the number of columns in W.
4695 Don't count columns occupied by scroll bars or the vertical bar
4696 separating W from the sibling to its right. */
4699 window_box_text_cols (struct window *w)
4701 struct frame *f = XFRAME (WINDOW_FRAME (w));
4702 int width = XINT (w->total_cols);
4704 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4705 /* Scroll bars occupy a few columns. */
4706 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4707 else if (!FRAME_WINDOW_P (f)
4708 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4709 /* The column of `|' characters separating side-by-side windows
4710 occupies one column only. */
4711 width -= 1;
4713 if (FRAME_WINDOW_P (f))
4714 /* On window-systems, fringes and display margins cannot be
4715 used for normal text. */
4716 width -= (WINDOW_FRINGE_COLS (w)
4717 + WINDOW_LEFT_MARGIN_COLS (w)
4718 + WINDOW_RIGHT_MARGIN_COLS (w));
4720 return width;
4724 /************************************************************************
4725 Window Scrolling
4726 ***********************************************************************/
4728 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4729 N screen-fulls, which is defined as the height of the window minus
4730 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4731 instead. Negative values of N mean scroll down. NOERROR non-zero
4732 means don't signal an error if we try to move over BEGV or ZV,
4733 respectively. */
4735 static void
4736 window_scroll (Lisp_Object window, int n, int whole, int noerror)
4738 immediate_quit = 1;
4740 /* If we must, use the pixel-based version which is much slower than
4741 the line-based one but can handle varying line heights. */
4742 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4743 window_scroll_pixel_based (window, n, whole, noerror);
4744 else
4745 window_scroll_line_based (window, n, whole, noerror);
4747 immediate_quit = 0;
4751 /* Implementation of window_scroll that works based on pixel line
4752 heights. See the comment of window_scroll for parameter
4753 descriptions. */
4755 static void
4756 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4758 struct it it;
4759 struct window *w = XWINDOW (window);
4760 struct text_pos start;
4761 int this_scroll_margin;
4762 /* True if we fiddled the window vscroll field without really scrolling. */
4763 int vscrolled = 0;
4764 int x, y, rtop, rbot, rowh, vpos;
4766 SET_TEXT_POS_FROM_MARKER (start, w->start);
4768 /* If PT is not visible in WINDOW, move back one half of
4769 the screen. Allow PT to be partially visible, otherwise
4770 something like (scroll-down 1) with PT in the line before
4771 the partially visible one would recenter. */
4773 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4775 /* Move backward half the height of the window. Performance note:
4776 vmotion used here is about 10% faster, but would give wrong
4777 results for variable height lines. */
4778 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4779 it.current_y = it.last_visible_y;
4780 move_it_vertically_backward (&it, window_box_height (w) / 2);
4782 /* The function move_iterator_vertically may move over more than
4783 the specified y-distance. If it->w is small, e.g. a
4784 mini-buffer window, we may end up in front of the window's
4785 display area. This is the case when Start displaying at the
4786 start of the line containing PT in this case. */
4787 if (it.current_y <= 0)
4789 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4790 move_it_vertically_backward (&it, 0);
4791 it.current_y = 0;
4794 start = it.current.pos;
4796 else if (auto_window_vscroll_p)
4798 if (rtop || rbot) /* partially visible */
4800 int px;
4801 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4802 if (whole)
4803 dy = max ((window_box_height (w)
4804 - next_screen_context_lines * dy),
4805 dy);
4806 dy *= n;
4808 if (n < 0)
4810 /* Only vscroll backwards if already vscrolled forwards. */
4811 if (w->vscroll < 0 && rtop > 0)
4813 px = max (0, -w->vscroll - min (rtop, -dy));
4814 Fset_window_vscroll (window, make_number (px), Qt);
4815 return;
4818 if (n > 0)
4820 /* Do vscroll if already vscrolled or only display line. */
4821 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4823 px = max (0, -w->vscroll + min (rbot, dy));
4824 Fset_window_vscroll (window, make_number (px), Qt);
4825 return;
4828 /* Maybe modify window start instead of scrolling. */
4829 if (rbot > 0 || w->vscroll < 0)
4831 EMACS_INT spos;
4833 Fset_window_vscroll (window, make_number (0), Qt);
4834 /* If there are other text lines above the current row,
4835 move window start to current row. Else to next row. */
4836 if (rbot > 0)
4837 spos = XINT (Fline_beginning_position (Qnil));
4838 else
4839 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4840 set_marker_restricted (w->start, make_number (spos),
4841 w->buffer);
4842 w->start_at_line_beg = Qt;
4843 w->update_mode_line = Qt;
4844 XSETFASTINT (w->last_modified, 0);
4845 XSETFASTINT (w->last_overlay_modified, 0);
4846 /* Set force_start so that redisplay_window will run the
4847 window-scroll-functions. */
4848 w->force_start = Qt;
4849 return;
4853 /* Cancel previous vscroll. */
4854 Fset_window_vscroll (window, make_number (0), Qt);
4857 /* If scroll_preserve_screen_position is non-nil, we try to set
4858 point in the same window line as it is now, so get that line. */
4859 if (!NILP (Vscroll_preserve_screen_position))
4861 /* We preserve the goal pixel coordinate across consecutive
4862 calls to scroll-up, scroll-down and other commands that
4863 have the `scroll-command' property. This avoids the
4864 possibility of point becoming "stuck" on a tall line when
4865 scrolling by one line. */
4866 if (window_scroll_pixel_based_preserve_y < 0
4867 || !SYMBOLP (current_kboard->Vlast_command)
4868 || NILP (Fget (current_kboard->Vlast_command, Qscroll_command)))
4870 start_display (&it, w, start);
4871 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4872 window_scroll_pixel_based_preserve_y = it.current_y;
4873 window_scroll_pixel_based_preserve_x = it.current_x;
4876 else
4877 window_scroll_pixel_based_preserve_y
4878 = window_scroll_pixel_based_preserve_x = -1;
4880 /* Move iterator it from start the specified distance forward or
4881 backward. The result is the new window start. */
4882 start_display (&it, w, start);
4883 if (whole)
4885 EMACS_INT start_pos = IT_CHARPOS (it);
4886 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4887 dy = max ((window_box_height (w)
4888 - next_screen_context_lines * dy),
4889 dy) * n;
4891 /* Note that move_it_vertically always moves the iterator to the
4892 start of a line. So, if the last line doesn't have a newline,
4893 we would end up at the start of the line ending at ZV. */
4894 if (dy <= 0)
4896 move_it_vertically_backward (&it, -dy);
4897 /* Ensure we actually do move, e.g. in case we are currently
4898 looking at an image that is taller that the window height. */
4899 while (start_pos == IT_CHARPOS (it)
4900 && start_pos > BEGV)
4901 move_it_by_lines (&it, -1, 1);
4903 else if (dy > 0)
4905 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4906 MOVE_TO_POS | MOVE_TO_Y);
4907 /* Ensure we actually do move, e.g. in case we are currently
4908 looking at an image that is taller that the window height. */
4909 while (start_pos == IT_CHARPOS (it)
4910 && start_pos < ZV)
4911 move_it_by_lines (&it, 1, 1);
4914 else
4915 move_it_by_lines (&it, n, 1);
4917 /* We failed if we find ZV is already on the screen (scrolling up,
4918 means there's nothing past the end), or if we can't start any
4919 earlier (scrolling down, means there's nothing past the top). */
4920 if ((n > 0 && IT_CHARPOS (it) == ZV)
4921 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4923 if (IT_CHARPOS (it) == ZV)
4925 if (it.current_y < it.last_visible_y
4926 && (it.current_y + it.max_ascent + it.max_descent
4927 > it.last_visible_y))
4929 /* The last line was only partially visible, make it fully
4930 visible. */
4931 w->vscroll = (it.last_visible_y
4932 - it.current_y + it.max_ascent + it.max_descent);
4933 adjust_glyphs (it.f);
4935 else if (noerror)
4936 return;
4937 else if (n < 0) /* could happen with empty buffers */
4938 xsignal0 (Qbeginning_of_buffer);
4939 else
4940 xsignal0 (Qend_of_buffer);
4942 else
4944 if (w->vscroll != 0)
4945 /* The first line was only partially visible, make it fully
4946 visible. */
4947 w->vscroll = 0;
4948 else if (noerror)
4949 return;
4950 else
4951 xsignal0 (Qbeginning_of_buffer);
4954 /* If control gets here, then we vscrolled. */
4956 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4958 /* Don't try to change the window start below. */
4959 vscrolled = 1;
4962 if (! vscrolled)
4964 EMACS_INT pos = IT_CHARPOS (it);
4965 EMACS_INT bytepos;
4967 /* If in the middle of a multi-glyph character move forward to
4968 the next character. */
4969 if (in_display_vector_p (&it))
4971 ++pos;
4972 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4975 /* Set the window start, and set up the window for redisplay. */
4976 set_marker_restricted (w->start, make_number (pos),
4977 w->buffer);
4978 bytepos = XMARKER (w->start)->bytepos;
4979 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4980 ? Qt : Qnil);
4981 w->update_mode_line = Qt;
4982 XSETFASTINT (w->last_modified, 0);
4983 XSETFASTINT (w->last_overlay_modified, 0);
4984 /* Set force_start so that redisplay_window will run the
4985 window-scroll-functions. */
4986 w->force_start = Qt;
4989 /* The rest of this function uses current_y in a nonstandard way,
4990 not including the height of the header line if any. */
4991 it.current_y = it.vpos = 0;
4993 /* Move PT out of scroll margins.
4994 This code wants current_y to be zero at the window start position
4995 even if there is a header line. */
4996 this_scroll_margin = max (0, scroll_margin);
4997 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4998 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5000 if (n > 0)
5002 /* We moved the window start towards ZV, so PT may be now
5003 in the scroll margin at the top. */
5004 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5005 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5006 && (NILP (Vscroll_preserve_screen_position)
5007 || EQ (Vscroll_preserve_screen_position, Qt)))
5008 /* We found PT at a legitimate height. Leave it alone. */
5010 else if (window_scroll_pixel_based_preserve_y >= 0)
5012 /* If we have a header line, take account of it.
5013 This is necessary because we set it.current_y to 0, above. */
5014 move_it_to (&it, -1,
5015 window_scroll_pixel_based_preserve_x,
5016 window_scroll_pixel_based_preserve_y
5017 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5018 -1, MOVE_TO_Y | MOVE_TO_X);
5019 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5021 else
5023 while (it.current_y < this_scroll_margin)
5025 int prev = it.current_y;
5026 move_it_by_lines (&it, 1, 1);
5027 if (prev == it.current_y)
5028 break;
5030 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5033 else if (n < 0)
5035 EMACS_INT charpos, bytepos;
5036 int partial_p;
5038 /* Save our position, for the
5039 window_scroll_pixel_based_preserve_y case. */
5040 charpos = IT_CHARPOS (it);
5041 bytepos = IT_BYTEPOS (it);
5043 /* We moved the window start towards BEGV, so PT may be now
5044 in the scroll margin at the bottom. */
5045 move_it_to (&it, PT, -1,
5046 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5047 - this_scroll_margin - 1),
5049 MOVE_TO_POS | MOVE_TO_Y);
5051 /* Save our position, in case it's correct. */
5052 charpos = IT_CHARPOS (it);
5053 bytepos = IT_BYTEPOS (it);
5055 /* See if point is on a partially visible line at the end. */
5056 if (it.what == IT_EOB)
5057 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5058 else
5060 move_it_by_lines (&it, 1, 1);
5061 partial_p = it.current_y > it.last_visible_y;
5064 if (charpos == PT && !partial_p
5065 && (NILP (Vscroll_preserve_screen_position)
5066 || EQ (Vscroll_preserve_screen_position, Qt)))
5067 /* We found PT before we found the display margin, so PT is ok. */
5069 else if (window_scroll_pixel_based_preserve_y >= 0)
5071 SET_TEXT_POS_FROM_MARKER (start, w->start);
5072 start_display (&it, w, start);
5073 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5074 here because we called start_display again and did not
5075 alter it.current_y this time. */
5076 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5077 window_scroll_pixel_based_preserve_y, -1,
5078 MOVE_TO_Y | MOVE_TO_X);
5079 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5081 else
5083 if (partial_p)
5084 /* The last line was only partially visible, so back up two
5085 lines to make sure we're on a fully visible line. */
5087 move_it_by_lines (&it, -2, 0);
5088 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5090 else
5091 /* No, the position we saved is OK, so use it. */
5092 SET_PT_BOTH (charpos, bytepos);
5098 /* Implementation of window_scroll that works based on screen lines.
5099 See the comment of window_scroll for parameter descriptions. */
5101 static void
5102 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
5104 register struct window *w = XWINDOW (window);
5105 register EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
5106 register EMACS_INT pos, pos_byte;
5107 register int ht = window_internal_height (w);
5108 register Lisp_Object tem;
5109 int lose;
5110 Lisp_Object bolp;
5111 EMACS_INT startpos;
5112 Lisp_Object original_pos = Qnil;
5114 /* If scrolling screen-fulls, compute the number of lines to
5115 scroll from the window's height. */
5116 if (whole)
5117 n *= max (1, ht - next_screen_context_lines);
5119 startpos = marker_position (w->start);
5121 if (!NILP (Vscroll_preserve_screen_position))
5123 if (window_scroll_preserve_vpos <= 0
5124 || !SYMBOLP (current_kboard->Vlast_command)
5125 || NILP (Fget (current_kboard->Vlast_command, Qscroll_command)))
5127 struct position posit
5128 = *compute_motion (startpos, 0, 0, 0,
5129 PT, ht, 0,
5130 -1, XINT (w->hscroll),
5131 0, w);
5132 window_scroll_preserve_vpos = posit.vpos;
5133 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
5136 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5137 make_number (window_scroll_preserve_vpos));
5140 XSETFASTINT (tem, PT);
5141 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5143 if (NILP (tem))
5145 Fvertical_motion (make_number (- (ht / 2)), window);
5146 startpos = PT;
5149 SET_PT (startpos);
5150 lose = n < 0 && PT == BEGV;
5151 Fvertical_motion (make_number (n), window);
5152 pos = PT;
5153 pos_byte = PT_BYTE;
5154 bolp = Fbolp ();
5155 SET_PT_BOTH (opoint, opoint_byte);
5157 if (lose)
5159 if (noerror)
5160 return;
5161 else
5162 xsignal0 (Qbeginning_of_buffer);
5165 if (pos < ZV)
5167 int this_scroll_margin = scroll_margin;
5169 /* Don't use a scroll margin that is negative or too large. */
5170 if (this_scroll_margin < 0)
5171 this_scroll_margin = 0;
5173 if (XINT (w->total_lines) < 4 * scroll_margin)
5174 this_scroll_margin = XINT (w->total_lines) / 4;
5176 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5177 w->start_at_line_beg = bolp;
5178 w->update_mode_line = Qt;
5179 XSETFASTINT (w->last_modified, 0);
5180 XSETFASTINT (w->last_overlay_modified, 0);
5181 /* Set force_start so that redisplay_window will run
5182 the window-scroll-functions. */
5183 w->force_start = Qt;
5185 if (!NILP (Vscroll_preserve_screen_position)
5186 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5188 SET_PT_BOTH (pos, pos_byte);
5189 Fvertical_motion (original_pos, window);
5191 /* If we scrolled forward, put point enough lines down
5192 that it is outside the scroll margin. */
5193 else if (n > 0)
5195 int top_margin;
5197 if (this_scroll_margin > 0)
5199 SET_PT_BOTH (pos, pos_byte);
5200 Fvertical_motion (make_number (this_scroll_margin), window);
5201 top_margin = PT;
5203 else
5204 top_margin = pos;
5206 if (top_margin <= opoint)
5207 SET_PT_BOTH (opoint, opoint_byte);
5208 else if (!NILP (Vscroll_preserve_screen_position))
5210 SET_PT_BOTH (pos, pos_byte);
5211 Fvertical_motion (original_pos, window);
5213 else
5214 SET_PT (top_margin);
5216 else if (n < 0)
5218 int bottom_margin;
5220 /* If we scrolled backward, put point near the end of the window
5221 but not within the scroll margin. */
5222 SET_PT_BOTH (pos, pos_byte);
5223 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5224 if (XFASTINT (tem) == ht - this_scroll_margin)
5225 bottom_margin = PT;
5226 else
5227 bottom_margin = PT + 1;
5229 if (bottom_margin > opoint)
5230 SET_PT_BOTH (opoint, opoint_byte);
5231 else
5233 if (!NILP (Vscroll_preserve_screen_position))
5235 SET_PT_BOTH (pos, pos_byte);
5236 Fvertical_motion (original_pos, window);
5238 else
5239 Fvertical_motion (make_number (-1), window);
5243 else
5245 if (noerror)
5246 return;
5247 else
5248 xsignal0 (Qend_of_buffer);
5253 /* Scroll selected_window up or down. If N is nil, scroll a
5254 screen-full which is defined as the height of the window minus
5255 next_screen_context_lines. If N is the symbol `-', scroll.
5256 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5257 up. This is the guts of Fscroll_up and Fscroll_down. */
5259 static void
5260 scroll_command (Lisp_Object n, int direction)
5262 int count = SPECPDL_INDEX ();
5264 xassert (eabs (direction) == 1);
5266 /* If selected window's buffer isn't current, make it current for
5267 the moment. But don't screw up if window_scroll gets an error. */
5268 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5270 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5271 Fset_buffer (XWINDOW (selected_window)->buffer);
5273 /* Make redisplay consider other windows than just selected_window. */
5274 ++windows_or_buffers_changed;
5277 if (NILP (n))
5278 window_scroll (selected_window, direction, 1, 0);
5279 else if (EQ (n, Qminus))
5280 window_scroll (selected_window, -direction, 1, 0);
5281 else
5283 n = Fprefix_numeric_value (n);
5284 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5287 unbind_to (count, Qnil);
5290 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5291 doc: /* Scroll text of selected window upward ARG lines.
5292 If ARG is omitted or nil, scroll upward by a near full screen.
5293 A near full screen is `next-screen-context-lines' less than a full screen.
5294 Negative ARG means scroll downward.
5295 If ARG is the atom `-', scroll downward by nearly full screen.
5296 When calling from a program, supply as argument a number, nil, or `-'. */)
5297 (Lisp_Object arg)
5299 scroll_command (arg, 1);
5300 return Qnil;
5303 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5304 doc: /* Scroll text of selected window down ARG lines.
5305 If ARG is omitted or nil, scroll down by a near full screen.
5306 A near full screen is `next-screen-context-lines' less than a full screen.
5307 Negative ARG means scroll upward.
5308 If ARG is the atom `-', scroll upward by nearly full screen.
5309 When calling from a program, supply as argument a number, nil, or `-'. */)
5310 (Lisp_Object arg)
5312 scroll_command (arg, -1);
5313 return Qnil;
5316 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5317 doc: /* Return the other window for \"other window scroll\" commands.
5318 If `other-window-scroll-buffer' is non-nil, a window
5319 showing that buffer is used.
5320 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5321 specifies the window. This takes precedence over
5322 `other-window-scroll-buffer'. */)
5323 (void)
5325 Lisp_Object window;
5327 if (MINI_WINDOW_P (XWINDOW (selected_window))
5328 && !NILP (Vminibuf_scroll_window))
5329 window = Vminibuf_scroll_window;
5330 /* If buffer is specified, scroll that buffer. */
5331 else if (!NILP (Vother_window_scroll_buffer))
5333 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5334 if (NILP (window))
5335 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5337 else
5339 /* Nothing specified; look for a neighboring window on the same
5340 frame. */
5341 window = Fnext_window (selected_window, Qnil, Qnil);
5343 if (EQ (window, selected_window))
5344 /* That didn't get us anywhere; look for a window on another
5345 visible frame. */
5347 window = Fnext_window (window, Qnil, Qt);
5348 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5349 && ! EQ (window, selected_window));
5352 CHECK_LIVE_WINDOW (window);
5354 if (EQ (window, selected_window))
5355 error ("There is no other window");
5357 return window;
5360 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5361 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5362 A near full screen is `next-screen-context-lines' less than a full screen.
5363 The next window is the one below the current one; or the one at the top
5364 if the current one is at the bottom. Negative ARG means scroll downward.
5365 If ARG is the atom `-', scroll downward by nearly full screen.
5366 When calling from a program, supply as argument a number, nil, or `-'.
5368 If `other-window-scroll-buffer' is non-nil, scroll the window
5369 showing that buffer, popping the buffer up if necessary.
5370 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5371 specifies the window to scroll. This takes precedence over
5372 `other-window-scroll-buffer'. */)
5373 (Lisp_Object arg)
5375 Lisp_Object window;
5376 struct window *w;
5377 int count = SPECPDL_INDEX ();
5379 window = Fother_window_for_scrolling ();
5380 w = XWINDOW (window);
5382 /* Don't screw up if window_scroll gets an error. */
5383 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5384 ++windows_or_buffers_changed;
5386 Fset_buffer (w->buffer);
5387 SET_PT (marker_position (w->pointm));
5389 if (NILP (arg))
5390 window_scroll (window, 1, 1, 1);
5391 else if (EQ (arg, Qminus))
5392 window_scroll (window, -1, 1, 1);
5393 else
5395 if (CONSP (arg))
5396 arg = Fcar (arg);
5397 CHECK_NUMBER (arg);
5398 window_scroll (window, XINT (arg), 0, 1);
5401 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5402 unbind_to (count, Qnil);
5404 return Qnil;
5407 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5408 doc: /* Scroll selected window display ARG columns left.
5409 Default for ARG is window width minus 2.
5410 Value is the total amount of leftward horizontal scrolling in
5411 effect after the change.
5412 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5413 lower bound for automatic scrolling, i.e. automatic scrolling
5414 will not scroll a window to a column less than the value returned
5415 by this function. This happens in an interactive call. */)
5416 (register Lisp_Object arg, Lisp_Object set_minimum)
5418 Lisp_Object result;
5419 int hscroll;
5420 struct window *w = XWINDOW (selected_window);
5422 if (NILP (arg))
5423 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5424 else
5425 arg = Fprefix_numeric_value (arg);
5427 hscroll = XINT (w->hscroll) + XINT (arg);
5428 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5430 if (!NILP (set_minimum))
5431 w->min_hscroll = w->hscroll;
5433 return result;
5436 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5437 doc: /* Scroll selected window display ARG columns right.
5438 Default for ARG is window width minus 2.
5439 Value is the total amount of leftward horizontal scrolling in
5440 effect after the change.
5441 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5442 lower bound for automatic scrolling, i.e. automatic scrolling
5443 will not scroll a window to a column less than the value returned
5444 by this function. This happens in an interactive call. */)
5445 (register Lisp_Object arg, Lisp_Object set_minimum)
5447 Lisp_Object result;
5448 int hscroll;
5449 struct window *w = XWINDOW (selected_window);
5451 if (NILP (arg))
5452 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5453 else
5454 arg = Fprefix_numeric_value (arg);
5456 hscroll = XINT (w->hscroll) - XINT (arg);
5457 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5459 if (!NILP (set_minimum))
5460 w->min_hscroll = w->hscroll;
5462 return result;
5465 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5466 doc: /* Return the window which was selected when entering the minibuffer.
5467 Returns nil, if selected window is not a minibuffer window. */)
5468 (void)
5470 if (minibuf_level > 0
5471 && MINI_WINDOW_P (XWINDOW (selected_window))
5472 && WINDOW_LIVE_P (minibuf_selected_window))
5473 return minibuf_selected_window;
5475 return Qnil;
5478 /* Value is the number of lines actually displayed in window W,
5479 as opposed to its height. */
5481 static int
5482 displayed_window_lines (struct window *w)
5484 struct it it;
5485 struct text_pos start;
5486 int height = window_box_height (w);
5487 struct buffer *old_buffer;
5488 int bottom_y;
5490 if (XBUFFER (w->buffer) != current_buffer)
5492 old_buffer = current_buffer;
5493 set_buffer_internal (XBUFFER (w->buffer));
5495 else
5496 old_buffer = NULL;
5498 /* In case W->start is out of the accessible range, do something
5499 reasonable. This happens in Info mode when Info-scroll-down
5500 calls (recenter -1) while W->start is 1. */
5501 if (XMARKER (w->start)->charpos < BEGV)
5502 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5503 else if (XMARKER (w->start)->charpos > ZV)
5504 SET_TEXT_POS (start, ZV, ZV_BYTE);
5505 else
5506 SET_TEXT_POS_FROM_MARKER (start, w->start);
5508 start_display (&it, w, start);
5509 move_it_vertically (&it, height);
5510 bottom_y = line_bottom_y (&it);
5512 /* rms: On a non-window display,
5513 the value of it.vpos at the bottom of the screen
5514 seems to be 1 larger than window_box_height (w).
5515 This kludge fixes a bug whereby (move-to-window-line -1)
5516 when ZV is on the last screen line
5517 moves to the previous screen line instead of the last one. */
5518 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5519 height++;
5521 /* Add in empty lines at the bottom of the window. */
5522 if (bottom_y < height)
5524 int uy = FRAME_LINE_HEIGHT (it.f);
5525 it.vpos += (height - bottom_y + uy - 1) / uy;
5528 if (old_buffer)
5529 set_buffer_internal (old_buffer);
5531 return it.vpos;
5535 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5536 doc: /* Center point in selected window and maybe redisplay frame.
5537 With prefix argument ARG, recenter putting point on screen line ARG
5538 relative to the selected window. If ARG is negative, it counts up from the
5539 bottom of the window. (ARG should be less than the height of the window.)
5541 If ARG is omitted or nil, then recenter with point on the middle line of
5542 the selected window; if the variable `recenter-redisplay' is non-nil,
5543 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5544 is set to `grow-only', this resets the tool-bar's height to the minimum
5545 height needed); if `recenter-redisplay' has the special value `tty',
5546 then only tty frame are redrawn.
5548 Just C-u as prefix means put point in the center of the window
5549 and redisplay normally--don't erase and redraw the frame. */)
5550 (register Lisp_Object arg)
5552 struct window *w = XWINDOW (selected_window);
5553 struct buffer *buf = XBUFFER (w->buffer);
5554 struct buffer *obuf = current_buffer;
5555 int center_p = 0;
5556 EMACS_INT charpos, bytepos;
5557 int iarg;
5558 int this_scroll_margin;
5560 /* If redisplay is suppressed due to an error, try again. */
5561 obuf->display_error_modiff = 0;
5563 if (NILP (arg))
5565 if (!NILP (Vrecenter_redisplay)
5566 && (!EQ (Vrecenter_redisplay, Qtty)
5567 || !NILP (Ftty_type (selected_frame))))
5569 int i;
5571 /* Invalidate pixel data calculated for all compositions. */
5572 for (i = 0; i < n_compositions; i++)
5573 composition_table[i]->font = NULL;
5575 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5577 Fredraw_frame (WINDOW_FRAME (w));
5578 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5581 center_p = 1;
5583 else if (CONSP (arg)) /* Just C-u. */
5584 center_p = 1;
5585 else
5587 arg = Fprefix_numeric_value (arg);
5588 CHECK_NUMBER (arg);
5589 iarg = XINT (arg);
5592 set_buffer_internal (buf);
5594 /* Do this after making BUF current
5595 in case scroll_margin is buffer-local. */
5596 this_scroll_margin = max (0, scroll_margin);
5597 this_scroll_margin = min (this_scroll_margin,
5598 XFASTINT (w->total_lines) / 4);
5600 /* Handle centering on a graphical frame specially. Such frames can
5601 have variable-height lines and centering point on the basis of
5602 line counts would lead to strange effects. */
5603 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5605 if (center_p)
5607 struct it it;
5608 struct text_pos pt;
5610 SET_TEXT_POS (pt, PT, PT_BYTE);
5611 start_display (&it, w, pt);
5612 move_it_vertically_backward (&it, window_box_height (w) / 2);
5613 charpos = IT_CHARPOS (it);
5614 bytepos = IT_BYTEPOS (it);
5616 else if (iarg < 0)
5618 struct it it;
5619 struct text_pos pt;
5620 int nlines = -iarg;
5621 int extra_line_spacing;
5622 int h = window_box_height (w);
5624 iarg = - max (-iarg, this_scroll_margin);
5626 SET_TEXT_POS (pt, PT, PT_BYTE);
5627 start_display (&it, w, pt);
5629 /* Be sure we have the exact height of the full line containing PT. */
5630 move_it_by_lines (&it, 0, 1);
5632 /* The amount of pixels we have to move back is the window
5633 height minus what's displayed in the line containing PT,
5634 and the lines below. */
5635 it.current_y = 0;
5636 it.vpos = 0;
5637 move_it_by_lines (&it, nlines, 1);
5639 if (it.vpos == nlines)
5640 h -= it.current_y;
5641 else
5643 /* Last line has no newline */
5644 h -= line_bottom_y (&it);
5645 it.vpos++;
5648 /* Don't reserve space for extra line spacing of last line. */
5649 extra_line_spacing = it.max_extra_line_spacing;
5651 /* If we can't move down NLINES lines because we hit
5652 the end of the buffer, count in some empty lines. */
5653 if (it.vpos < nlines)
5655 nlines -= it.vpos;
5656 extra_line_spacing = it.extra_line_spacing;
5657 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5659 if (h <= 0)
5660 return Qnil;
5662 /* Now find the new top line (starting position) of the window. */
5663 start_display (&it, w, pt);
5664 it.current_y = 0;
5665 move_it_vertically_backward (&it, h);
5667 /* If extra line spacing is present, we may move too far
5668 back. This causes the last line to be only partially
5669 visible (which triggers redisplay to recenter that line
5670 in the middle), so move forward.
5671 But ignore extra line spacing on last line, as it is not
5672 considered to be part of the visible height of the line.
5674 h += extra_line_spacing;
5675 while (-it.current_y > h)
5676 move_it_by_lines (&it, 1, 1);
5678 charpos = IT_CHARPOS (it);
5679 bytepos = IT_BYTEPOS (it);
5681 else
5683 struct position pos;
5685 iarg = max (iarg, this_scroll_margin);
5687 pos = *vmotion (PT, -iarg, w);
5688 charpos = pos.bufpos;
5689 bytepos = pos.bytepos;
5692 else
5694 struct position pos;
5695 int ht = window_internal_height (w);
5697 if (center_p)
5698 iarg = ht / 2;
5699 else if (iarg < 0)
5700 iarg += ht;
5702 /* Don't let it get into the margin at either top or bottom. */
5703 iarg = max (iarg, this_scroll_margin);
5704 iarg = min (iarg, ht - this_scroll_margin - 1);
5706 pos = *vmotion (PT, - iarg, w);
5707 charpos = pos.bufpos;
5708 bytepos = pos.bytepos;
5711 /* Set the new window start. */
5712 set_marker_both (w->start, w->buffer, charpos, bytepos);
5713 w->window_end_valid = Qnil;
5715 w->optional_new_start = Qt;
5717 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5718 w->start_at_line_beg = Qt;
5719 else
5720 w->start_at_line_beg = Qnil;
5722 set_buffer_internal (obuf);
5723 return Qnil;
5727 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5728 0, 1, 0,
5729 doc: /* Return the height in lines of the text display area of WINDOW.
5730 WINDOW defaults to the selected window.
5732 The return value does not include the mode line, any header line, nor
5733 any partial-height lines in the text display area. */)
5734 (Lisp_Object window)
5736 struct window *w = decode_window (window);
5737 int pixel_height = window_box_height (w);
5738 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5739 return make_number (line_height);
5744 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5745 1, 1, "P",
5746 doc: /* Position point relative to window.
5747 With no argument, position point at center of window.
5748 An argument specifies vertical position within the window;
5749 zero means top of window, negative means relative to bottom of window. */)
5750 (Lisp_Object arg)
5752 struct window *w = XWINDOW (selected_window);
5753 int lines, start;
5754 Lisp_Object window;
5755 #if 0
5756 int this_scroll_margin;
5757 #endif
5759 window = selected_window;
5760 start = marker_position (w->start);
5761 if (start < BEGV || start > ZV)
5763 int height = window_internal_height (w);
5764 Fvertical_motion (make_number (- (height / 2)), window);
5765 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5766 w->start_at_line_beg = Fbolp ();
5767 w->force_start = Qt;
5769 else
5770 Fgoto_char (w->start);
5772 lines = displayed_window_lines (w);
5774 #if 0
5775 this_scroll_margin = max (0, scroll_margin);
5776 this_scroll_margin = min (this_scroll_margin, lines / 4);
5777 #endif
5779 if (NILP (arg))
5780 XSETFASTINT (arg, lines / 2);
5781 else
5783 int iarg = XINT (Fprefix_numeric_value (arg));
5785 if (iarg < 0)
5786 iarg = iarg + lines;
5788 #if 0 /* This code would prevent move-to-window-line from moving point
5789 to a place inside the scroll margins (which would cause the
5790 next redisplay to scroll). I wrote this code, but then concluded
5791 it is probably better not to install it. However, it is here
5792 inside #if 0 so as not to lose it. -- rms. */
5794 /* Don't let it get into the margin at either top or bottom. */
5795 iarg = max (iarg, this_scroll_margin);
5796 iarg = min (iarg, lines - this_scroll_margin - 1);
5797 #endif
5799 arg = make_number (iarg);
5802 /* Skip past a partially visible first line. */
5803 if (w->vscroll)
5804 XSETINT (arg, XINT (arg) + 1);
5806 return Fvertical_motion (arg, window);
5811 /***********************************************************************
5812 Window Configuration
5813 ***********************************************************************/
5815 struct save_window_data
5817 EMACS_UINT size;
5818 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5819 Lisp_Object selected_frame;
5820 Lisp_Object current_window;
5821 Lisp_Object current_buffer;
5822 Lisp_Object minibuf_scroll_window;
5823 Lisp_Object minibuf_selected_window;
5824 Lisp_Object root_window;
5825 Lisp_Object focus_frame;
5826 /* A vector, each of whose elements is a struct saved_window
5827 for one window. */
5828 Lisp_Object saved_windows;
5830 /* All fields above are traced by the GC.
5831 From `fame-cols' down, the fields are ignored by the GC. */
5833 int frame_cols, frame_lines, frame_menu_bar_lines;
5834 int frame_tool_bar_lines;
5837 /* This is saved as a Lisp_Vector */
5838 struct saved_window
5840 /* these first two must agree with struct Lisp_Vector in lisp.h */
5841 EMACS_UINT size;
5842 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5844 Lisp_Object window;
5845 Lisp_Object buffer, start, pointm, mark;
5846 Lisp_Object left_col, top_line, total_cols, total_lines;
5847 Lisp_Object hscroll, min_hscroll;
5848 Lisp_Object parent, prev;
5849 Lisp_Object start_at_line_beg;
5850 Lisp_Object display_table;
5851 Lisp_Object orig_top_line, orig_total_lines;
5852 Lisp_Object left_margin_cols, right_margin_cols;
5853 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5854 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5855 Lisp_Object dedicated, resize_proportionally;
5858 #define SAVED_WINDOW_N(swv,n) \
5859 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5861 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5862 doc: /* Return t if OBJECT is a window-configuration object. */)
5863 (Lisp_Object object)
5865 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5868 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5869 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5870 (Lisp_Object config)
5872 register struct save_window_data *data;
5873 struct Lisp_Vector *saved_windows;
5875 CHECK_WINDOW_CONFIGURATION (config);
5877 data = (struct save_window_data *) XVECTOR (config);
5878 saved_windows = XVECTOR (data->saved_windows);
5879 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5882 DEFUN ("set-window-configuration", Fset_window_configuration,
5883 Sset_window_configuration, 1, 1, 0,
5884 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5885 CONFIGURATION must be a value previously returned
5886 by `current-window-configuration' (which see).
5887 If CONFIGURATION was made from a frame that is now deleted,
5888 only frame-independent values can be restored. In this case,
5889 the return value is nil. Otherwise the value is t. */)
5890 (Lisp_Object configuration)
5892 register struct save_window_data *data;
5893 struct Lisp_Vector *saved_windows;
5894 Lisp_Object new_current_buffer;
5895 Lisp_Object frame;
5896 FRAME_PTR f;
5897 EMACS_INT old_point = -1;
5899 CHECK_WINDOW_CONFIGURATION (configuration);
5901 data = (struct save_window_data *) XVECTOR (configuration);
5902 saved_windows = XVECTOR (data->saved_windows);
5904 new_current_buffer = data->current_buffer;
5905 if (NILP (XBUFFER (new_current_buffer)->name))
5906 new_current_buffer = Qnil;
5907 else
5909 if (XBUFFER (new_current_buffer) == current_buffer)
5910 /* The code further down "preserves point" by saving here PT in
5911 old_point and then setting it later back into PT. When the
5912 current-selected-window and the final-selected-window both show
5913 the current buffer, this suffers from the problem that the
5914 current PT is the window-point of the current-selected-window,
5915 while the final PT is the point of the final-selected-window, so
5916 this copy from one PT to the other would end up moving the
5917 window-point of the final-selected-window to the window-point of
5918 the current-selected-window. So we have to be careful which
5919 point of the current-buffer we copy into old_point. */
5920 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5921 && WINDOWP (selected_window)
5922 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5923 && !EQ (selected_window, data->current_window))
5924 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5925 else
5926 old_point = PT;
5927 else
5928 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5929 point in new_current_buffer as of the last time this buffer was
5930 used. This can be non-deterministic since it can be changed by
5931 things like jit-lock by mere temporary selection of some random
5932 window that happens to show this buffer.
5933 So if possible we want this arbitrary choice of "which point" to
5934 be the one from the to-be-selected-window so as to prevent this
5935 window's cursor from being copied from another window. */
5936 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5937 /* If current_window = selected_window, its point is in BUF_PT. */
5938 && !EQ (selected_window, data->current_window))
5939 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5940 else
5941 old_point = BUF_PT (XBUFFER (new_current_buffer));
5944 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5945 f = XFRAME (frame);
5947 /* If f is a dead frame, don't bother rebuilding its window tree.
5948 However, there is other stuff we should still try to do below. */
5949 if (FRAME_LIVE_P (f))
5951 register struct window *w;
5952 register struct saved_window *p;
5953 struct window *root_window;
5954 struct window **leaf_windows;
5955 int n_leaf_windows;
5956 int k, i, n;
5958 /* If the frame has been resized since this window configuration was
5959 made, we change the frame to the size specified in the
5960 configuration, restore the configuration, and then resize it
5961 back. We keep track of the prevailing height in these variables. */
5962 int previous_frame_lines = FRAME_LINES (f);
5963 int previous_frame_cols = FRAME_COLS (f);
5964 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5965 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5967 /* The mouse highlighting code could get screwed up
5968 if it runs during this. */
5969 BLOCK_INPUT;
5971 if (data->frame_lines != previous_frame_lines
5972 || data->frame_cols != previous_frame_cols)
5973 change_frame_size (f, data->frame_lines,
5974 data->frame_cols, 0, 0, 0);
5975 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5976 if (data->frame_menu_bar_lines
5977 != previous_frame_menu_bar_lines)
5978 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5979 make_number (0));
5980 #ifdef HAVE_WINDOW_SYSTEM
5981 if (data->frame_tool_bar_lines
5982 != previous_frame_tool_bar_lines)
5983 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5984 make_number (0));
5985 #endif
5986 #endif
5988 /* "Swap out" point from the selected window's buffer
5989 into the window itself. (Normally the pointm of the selected
5990 window holds garbage.) We do this now, before
5991 restoring the window contents, and prevent it from
5992 being done later on when we select a new window. */
5993 if (! NILP (XWINDOW (selected_window)->buffer))
5995 w = XWINDOW (selected_window);
5996 set_marker_both (w->pointm,
5997 w->buffer,
5998 BUF_PT (XBUFFER (w->buffer)),
5999 BUF_PT_BYTE (XBUFFER (w->buffer)));
6002 windows_or_buffers_changed++;
6003 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6005 /* Problem: Freeing all matrices and later allocating them again
6006 is a serious redisplay flickering problem. What we would
6007 really like to do is to free only those matrices not reused
6008 below. */
6009 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6010 leaf_windows
6011 = (struct window **) alloca (count_windows (root_window)
6012 * sizeof (struct window *));
6013 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6015 /* Kludge Alert!
6016 Mark all windows now on frame as "deleted".
6017 Restoring the new configuration "undeletes" any that are in it.
6019 Save their current buffers in their height fields, since we may
6020 need it later, if a buffer saved in the configuration is now
6021 dead. */
6022 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6024 for (k = 0; k < saved_windows->size; k++)
6026 p = SAVED_WINDOW_N (saved_windows, k);
6027 w = XWINDOW (p->window);
6028 w->next = Qnil;
6030 if (!NILP (p->parent))
6031 w->parent = SAVED_WINDOW_N (saved_windows,
6032 XFASTINT (p->parent))->window;
6033 else
6034 w->parent = Qnil;
6036 if (!NILP (p->prev))
6038 w->prev = SAVED_WINDOW_N (saved_windows,
6039 XFASTINT (p->prev))->window;
6040 XWINDOW (w->prev)->next = p->window;
6042 else
6044 w->prev = Qnil;
6045 if (!NILP (w->parent))
6047 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6049 XWINDOW (w->parent)->vchild = p->window;
6050 XWINDOW (w->parent)->hchild = Qnil;
6052 else
6054 XWINDOW (w->parent)->hchild = p->window;
6055 XWINDOW (w->parent)->vchild = Qnil;
6060 /* If we squirreled away the buffer in the window's height,
6061 restore it now. */
6062 if (BUFFERP (w->total_lines))
6063 w->buffer = w->total_lines;
6064 w->left_col = p->left_col;
6065 w->top_line = p->top_line;
6066 w->total_cols = p->total_cols;
6067 w->total_lines = p->total_lines;
6068 w->hscroll = p->hscroll;
6069 w->min_hscroll = p->min_hscroll;
6070 w->display_table = p->display_table;
6071 w->orig_top_line = p->orig_top_line;
6072 w->orig_total_lines = p->orig_total_lines;
6073 w->left_margin_cols = p->left_margin_cols;
6074 w->right_margin_cols = p->right_margin_cols;
6075 w->left_fringe_width = p->left_fringe_width;
6076 w->right_fringe_width = p->right_fringe_width;
6077 w->fringes_outside_margins = p->fringes_outside_margins;
6078 w->scroll_bar_width = p->scroll_bar_width;
6079 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6080 w->dedicated = p->dedicated;
6081 w->resize_proportionally = p->resize_proportionally;
6082 XSETFASTINT (w->last_modified, 0);
6083 XSETFASTINT (w->last_overlay_modified, 0);
6085 /* Reinstall the saved buffer and pointers into it. */
6086 if (NILP (p->buffer))
6087 w->buffer = p->buffer;
6088 else
6090 if (!NILP (XBUFFER (p->buffer)->name))
6091 /* If saved buffer is alive, install it. */
6093 w->buffer = p->buffer;
6094 w->start_at_line_beg = p->start_at_line_beg;
6095 set_marker_restricted (w->start, p->start, w->buffer);
6096 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6097 Fset_marker (XBUFFER (w->buffer)->mark,
6098 p->mark, w->buffer);
6100 /* As documented in Fcurrent_window_configuration, don't
6101 restore the location of point in the buffer which was
6102 current when the window configuration was recorded. */
6103 if (!EQ (p->buffer, new_current_buffer)
6104 && XBUFFER (p->buffer) == current_buffer)
6105 Fgoto_char (w->pointm);
6107 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
6108 /* Else unless window has a live buffer, get one. */
6110 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6111 /* This will set the markers to beginning of visible
6112 range. */
6113 set_marker_restricted (w->start, make_number (0), w->buffer);
6114 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6115 w->start_at_line_beg = Qt;
6117 else
6118 /* Keeping window's old buffer; make sure the markers
6119 are real. */
6121 /* Set window markers at start of visible range. */
6122 if (XMARKER (w->start)->buffer == 0)
6123 set_marker_restricted (w->start, make_number (0),
6124 w->buffer);
6125 if (XMARKER (w->pointm)->buffer == 0)
6126 set_marker_restricted_both (w->pointm, w->buffer,
6127 BUF_PT (XBUFFER (w->buffer)),
6128 BUF_PT_BYTE (XBUFFER (w->buffer)));
6129 w->start_at_line_beg = Qt;
6134 FRAME_ROOT_WINDOW (f) = data->root_window;
6135 /* Prevent "swapping out point" in the old selected window
6136 using the buffer that has been restored into it.
6137 We already swapped out point that from that window's old buffer. */
6138 selected_window = Qnil;
6140 /* Arrange *not* to restore point in the buffer that was
6141 current when the window configuration was saved. */
6142 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6143 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6144 make_number (old_point),
6145 XWINDOW (data->current_window)->buffer);
6147 Fselect_window (data->current_window, Qnil);
6148 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
6149 = selected_window;
6151 if (NILP (data->focus_frame)
6152 || (FRAMEP (data->focus_frame)
6153 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6154 Fredirect_frame_focus (frame, data->focus_frame);
6156 #if 0 /* I don't understand why this is needed, and it causes problems
6157 when the frame's old selected window has been deleted. */
6158 if (f != selected_frame && FRAME_WINDOW_P (f))
6159 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
6160 0, 0, Qnil);
6161 #endif
6163 /* Set the screen height to the value it had before this function. */
6164 if (previous_frame_lines != FRAME_LINES (f)
6165 || previous_frame_cols != FRAME_COLS (f))
6166 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6167 0, 0, 0);
6168 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6169 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6170 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6171 make_number (0));
6172 #ifdef HAVE_WINDOW_SYSTEM
6173 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6174 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6175 make_number (0));
6176 #endif
6177 #endif
6179 /* Now, free glyph matrices in windows that were not reused. */
6180 for (i = n = 0; i < n_leaf_windows; ++i)
6182 if (NILP (leaf_windows[i]->buffer))
6184 /* Assert it's not reused as a combination. */
6185 xassert (NILP (leaf_windows[i]->hchild)
6186 && NILP (leaf_windows[i]->vchild));
6187 free_window_matrices (leaf_windows[i]);
6189 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6190 ++n;
6193 adjust_glyphs (f);
6195 UNBLOCK_INPUT;
6197 /* Fselect_window will have made f the selected frame, so we
6198 reselect the proper frame here. Fhandle_switch_frame will change the
6199 selected window too, but that doesn't make the call to
6200 Fselect_window above totally superfluous; it still sets f's
6201 selected window. */
6202 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6203 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6205 run_window_configuration_change_hook (f);
6208 if (!NILP (new_current_buffer))
6209 Fset_buffer (new_current_buffer);
6211 Vminibuf_scroll_window = data->minibuf_scroll_window;
6212 minibuf_selected_window = data->minibuf_selected_window;
6214 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6217 /* Mark all windows now on frame as deleted
6218 by setting their buffers to nil. */
6220 void
6221 delete_all_subwindows (register struct window *w)
6223 if (!NILP (w->next))
6224 delete_all_subwindows (XWINDOW (w->next));
6225 if (!NILP (w->vchild))
6226 delete_all_subwindows (XWINDOW (w->vchild));
6227 if (!NILP (w->hchild))
6228 delete_all_subwindows (XWINDOW (w->hchild));
6230 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6232 if (!NILP (w->buffer))
6233 unshow_buffer (w);
6235 /* We set all three of these fields to nil, to make sure that we can
6236 distinguish this dead window from any live window. Live leaf
6237 windows will have buffer set, and combination windows will have
6238 vchild or hchild set. */
6239 w->buffer = Qnil;
6240 w->vchild = Qnil;
6241 w->hchild = Qnil;
6243 Vwindow_list = Qnil;
6246 static int
6247 count_windows (register struct window *window)
6249 register int count = 1;
6250 if (!NILP (window->next))
6251 count += count_windows (XWINDOW (window->next));
6252 if (!NILP (window->vchild))
6253 count += count_windows (XWINDOW (window->vchild));
6254 if (!NILP (window->hchild))
6255 count += count_windows (XWINDOW (window->hchild));
6256 return count;
6260 /* Fill vector FLAT with leaf windows under W, starting at index I.
6261 Value is last index + 1. */
6263 static int
6264 get_leaf_windows (struct window *w, struct window **flat, int i)
6266 while (w)
6268 if (!NILP (w->hchild))
6269 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6270 else if (!NILP (w->vchild))
6271 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6272 else
6273 flat[i++] = w;
6275 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6278 return i;
6282 /* Return a pointer to the glyph W's physical cursor is on. Value is
6283 null if W's current matrix is invalid, so that no meaningfull glyph
6284 can be returned. */
6286 struct glyph *
6287 get_phys_cursor_glyph (struct window *w)
6289 struct glyph_row *row;
6290 struct glyph *glyph;
6292 if (w->phys_cursor.vpos >= 0
6293 && w->phys_cursor.vpos < w->current_matrix->nrows
6294 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6295 row->enabled_p)
6296 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6297 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6298 else
6299 glyph = NULL;
6301 return glyph;
6305 static int
6306 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6308 register struct saved_window *p;
6309 register struct window *w;
6310 register Lisp_Object tem;
6312 for (;!NILP (window); window = w->next)
6314 p = SAVED_WINDOW_N (vector, i);
6315 w = XWINDOW (window);
6317 XSETFASTINT (w->temslot, i); i++;
6318 p->window = window;
6319 p->buffer = w->buffer;
6320 p->left_col = w->left_col;
6321 p->top_line = w->top_line;
6322 p->total_cols = w->total_cols;
6323 p->total_lines = w->total_lines;
6324 p->hscroll = w->hscroll;
6325 p->min_hscroll = w->min_hscroll;
6326 p->display_table = w->display_table;
6327 p->orig_top_line = w->orig_top_line;
6328 p->orig_total_lines = w->orig_total_lines;
6329 p->left_margin_cols = w->left_margin_cols;
6330 p->right_margin_cols = w->right_margin_cols;
6331 p->left_fringe_width = w->left_fringe_width;
6332 p->right_fringe_width = w->right_fringe_width;
6333 p->fringes_outside_margins = w->fringes_outside_margins;
6334 p->scroll_bar_width = w->scroll_bar_width;
6335 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6336 p->dedicated = w->dedicated;
6337 p->resize_proportionally = w->resize_proportionally;
6338 if (!NILP (w->buffer))
6340 /* Save w's value of point in the window configuration.
6341 If w is the selected window, then get the value of point
6342 from the buffer; pointm is garbage in the selected window. */
6343 if (EQ (window, selected_window))
6345 p->pointm = Fmake_marker ();
6346 set_marker_both (p->pointm, w->buffer,
6347 BUF_PT (XBUFFER (w->buffer)),
6348 BUF_PT_BYTE (XBUFFER (w->buffer)));
6350 else
6351 p->pointm = Fcopy_marker (w->pointm, Qnil);
6353 p->start = Fcopy_marker (w->start, Qnil);
6354 p->start_at_line_beg = w->start_at_line_beg;
6356 tem = XBUFFER (w->buffer)->mark;
6357 p->mark = Fcopy_marker (tem, Qnil);
6359 else
6361 p->pointm = Qnil;
6362 p->start = Qnil;
6363 p->mark = Qnil;
6364 p->start_at_line_beg = Qnil;
6367 if (NILP (w->parent))
6368 p->parent = Qnil;
6369 else
6370 p->parent = XWINDOW (w->parent)->temslot;
6372 if (NILP (w->prev))
6373 p->prev = Qnil;
6374 else
6375 p->prev = XWINDOW (w->prev)->temslot;
6377 if (!NILP (w->vchild))
6378 i = save_window_save (w->vchild, vector, i);
6379 if (!NILP (w->hchild))
6380 i = save_window_save (w->hchild, vector, i);
6383 return i;
6386 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6387 Scurrent_window_configuration, 0, 1, 0,
6388 doc: /* Return an object representing the current window configuration of FRAME.
6389 If FRAME is nil or omitted, use the selected frame.
6390 This describes the number of windows, their sizes and current buffers,
6391 and for each displayed buffer, where display starts, and the positions of
6392 point and mark. An exception is made for point in the current buffer:
6393 its value is -not- saved.
6394 This also records the currently selected frame, and FRAME's focus
6395 redirection (see `redirect-frame-focus'). */)
6396 (Lisp_Object frame)
6398 register Lisp_Object tem;
6399 register int n_windows;
6400 register struct save_window_data *data;
6401 register int i;
6402 FRAME_PTR f;
6404 if (NILP (frame))
6405 frame = selected_frame;
6406 CHECK_LIVE_FRAME (frame);
6407 f = XFRAME (frame);
6409 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6410 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6411 PVEC_WINDOW_CONFIGURATION);
6413 data->frame_cols = FRAME_COLS (f);
6414 data->frame_lines = FRAME_LINES (f);
6415 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6416 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6417 data->selected_frame = selected_frame;
6418 data->current_window = FRAME_SELECTED_WINDOW (f);
6419 XSETBUFFER (data->current_buffer, current_buffer);
6420 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6421 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6422 data->root_window = FRAME_ROOT_WINDOW (f);
6423 data->focus_frame = FRAME_FOCUS_FRAME (f);
6424 tem = Fmake_vector (make_number (n_windows), Qnil);
6425 data->saved_windows = tem;
6426 for (i = 0; i < n_windows; i++)
6427 XVECTOR (tem)->contents[i]
6428 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6429 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6430 XSETWINDOW_CONFIGURATION (tem, data);
6431 return (tem);
6434 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
6435 0, UNEVALLED, 0,
6436 doc: /* Execute BODY, preserving window sizes and contents.
6437 Return the value of the last form in BODY.
6438 Restore which buffer appears in which window, where display starts,
6439 and the value of point and mark for each window.
6440 Also restore the choice of selected window.
6441 Also restore which buffer is current.
6442 Does not restore the value of point in current buffer.
6443 usage: (save-window-excursion BODY...) */)
6444 (Lisp_Object args)
6446 register Lisp_Object val;
6447 register int count = SPECPDL_INDEX ();
6449 record_unwind_protect (Fset_window_configuration,
6450 Fcurrent_window_configuration (Qnil));
6451 val = Fprogn (args);
6452 return unbind_to (count, val);
6457 /***********************************************************************
6458 Window Split Tree
6459 ***********************************************************************/
6461 static Lisp_Object
6462 window_tree (struct window *w)
6464 Lisp_Object tail = Qnil;
6465 Lisp_Object result = Qnil;
6467 while (w)
6469 Lisp_Object wn;
6471 XSETWINDOW (wn, w);
6472 if (!NILP (w->hchild))
6473 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6474 window_tree (XWINDOW (w->hchild))));
6475 else if (!NILP (w->vchild))
6476 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6477 window_tree (XWINDOW (w->vchild))));
6479 if (NILP (result))
6481 result = tail = Fcons (wn, Qnil);
6483 else
6485 XSETCDR (tail, Fcons (wn, Qnil));
6486 tail = XCDR (tail);
6489 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6492 return result;
6497 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6498 0, 1, 0,
6499 doc: /* Return the window tree for frame FRAME.
6501 The return value is a list of the form (ROOT MINI), where ROOT
6502 represents the window tree of the frame's root window, and MINI
6503 is the frame's minibuffer window.
6505 If the root window is not split, ROOT is the root window itself.
6506 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6507 horizontal split, and t for a vertical split, EDGES gives the combined
6508 size and position of the subwindows in the split, and the rest of the
6509 elements are the subwindows in the split. Each of the subwindows may
6510 again be a window or a list representing a window split, and so on.
6511 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6513 If FRAME is nil or omitted, return information on the currently
6514 selected frame. */)
6515 (Lisp_Object frame)
6517 FRAME_PTR f;
6519 if (NILP (frame))
6520 frame = selected_frame;
6522 CHECK_FRAME (frame);
6523 f = XFRAME (frame);
6525 if (!FRAME_LIVE_P (f))
6526 return Qnil;
6528 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6532 /***********************************************************************
6533 Marginal Areas
6534 ***********************************************************************/
6536 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6537 2, 3, 0,
6538 doc: /* Set width of marginal areas of window WINDOW.
6539 If WINDOW is nil, set margins of the currently selected window.
6540 Second arg LEFT-WIDTH specifies the number of character cells to
6541 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6542 does the same for the right marginal area. A nil width parameter
6543 means no margin. */)
6544 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6546 struct window *w = decode_window (window);
6548 /* Translate negative or zero widths to nil.
6549 Margins that are too wide have to be checked elsewhere. */
6551 if (!NILP (left_width))
6553 CHECK_NUMBER (left_width);
6554 if (XINT (left_width) <= 0)
6555 left_width = Qnil;
6558 if (!NILP (right_width))
6560 CHECK_NUMBER (right_width);
6561 if (XINT (right_width) <= 0)
6562 right_width = Qnil;
6565 if (!EQ (w->left_margin_cols, left_width)
6566 || !EQ (w->right_margin_cols, right_width))
6568 w->left_margin_cols = left_width;
6569 w->right_margin_cols = right_width;
6571 adjust_window_margins (w);
6573 ++windows_or_buffers_changed;
6574 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6577 return Qnil;
6581 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6582 0, 1, 0,
6583 doc: /* Get width of marginal areas of window WINDOW.
6584 If WINDOW is omitted or nil, use the currently selected window.
6585 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6586 If a marginal area does not exist, its width will be returned
6587 as nil. */)
6588 (Lisp_Object window)
6590 struct window *w = decode_window (window);
6591 return Fcons (w->left_margin_cols, w->right_margin_cols);
6596 /***********************************************************************
6597 Fringes
6598 ***********************************************************************/
6600 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6601 2, 4, 0,
6602 doc: /* Set the fringe widths of window WINDOW.
6603 If WINDOW is nil, set the fringe widths of the currently selected
6604 window.
6605 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6606 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6607 fringe width. If a fringe width arg is nil, that means to use the
6608 frame's default fringe width. Default fringe widths can be set with
6609 the command `set-fringe-style'.
6610 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6611 outside of the display margins. By default, fringes are drawn between
6612 display marginal areas and the text area. */)
6613 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6615 struct window *w = decode_window (window);
6617 if (!NILP (left_width))
6618 CHECK_NATNUM (left_width);
6619 if (!NILP (right_width))
6620 CHECK_NATNUM (right_width);
6622 /* Do nothing on a tty. */
6623 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6624 && (!EQ (w->left_fringe_width, left_width)
6625 || !EQ (w->right_fringe_width, right_width)
6626 || !EQ (w->fringes_outside_margins, outside_margins)))
6628 w->left_fringe_width = left_width;
6629 w->right_fringe_width = right_width;
6630 w->fringes_outside_margins = outside_margins;
6632 adjust_window_margins (w);
6634 clear_glyph_matrix (w->current_matrix);
6635 w->window_end_valid = Qnil;
6637 ++windows_or_buffers_changed;
6638 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6641 return Qnil;
6645 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6646 0, 1, 0,
6647 doc: /* Get width of fringes of window WINDOW.
6648 If WINDOW is omitted or nil, use the currently selected window.
6649 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6650 (Lisp_Object window)
6652 struct window *w = decode_window (window);
6654 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6655 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6656 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6657 ? Qt : Qnil), Qnil)));
6662 /***********************************************************************
6663 Scroll bars
6664 ***********************************************************************/
6666 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6667 2, 4, 0,
6668 doc: /* Set width and type of scroll bars of window WINDOW.
6669 If window is nil, set scroll bars of the currently selected window.
6670 Second parameter WIDTH specifies the pixel width for the scroll bar;
6671 this is automatically adjusted to a multiple of the frame column width.
6672 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6673 bar: left, right, or nil.
6674 If WIDTH is nil, use the frame's scroll-bar width.
6675 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6676 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6677 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6679 struct window *w = decode_window (window);
6681 if (!NILP (width))
6683 CHECK_NATNUM (width);
6685 if (XINT (width) == 0)
6686 vertical_type = Qnil;
6689 if (!(NILP (vertical_type)
6690 || EQ (vertical_type, Qleft)
6691 || EQ (vertical_type, Qright)
6692 || EQ (vertical_type, Qt)))
6693 error ("Invalid type of vertical scroll bar");
6695 if (!EQ (w->scroll_bar_width, width)
6696 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6698 w->scroll_bar_width = width;
6699 w->vertical_scroll_bar_type = vertical_type;
6701 adjust_window_margins (w);
6703 clear_glyph_matrix (w->current_matrix);
6704 w->window_end_valid = Qnil;
6706 ++windows_or_buffers_changed;
6707 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6710 return Qnil;
6714 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6715 0, 1, 0,
6716 doc: /* Get width and type of scroll bars of window WINDOW.
6717 If WINDOW is omitted or nil, use the currently selected window.
6718 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6719 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6720 value. */)
6721 (Lisp_Object window)
6723 struct window *w = decode_window (window);
6724 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6725 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6726 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6727 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6728 Fcons (w->vertical_scroll_bar_type,
6729 Fcons (Qnil, Qnil))));
6734 /***********************************************************************
6735 Smooth scrolling
6736 ***********************************************************************/
6738 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6739 doc: /* Return the amount by which WINDOW is scrolled vertically.
6740 Use the selected window if WINDOW is nil or omitted.
6741 Normally, value is a multiple of the canonical character height of WINDOW;
6742 optional second arg PIXELS-P means value is measured in pixels. */)
6743 (Lisp_Object window, Lisp_Object pixels_p)
6745 Lisp_Object result;
6746 struct frame *f;
6747 struct window *w;
6749 if (NILP (window))
6750 window = selected_window;
6751 else
6752 CHECK_WINDOW (window);
6753 w = XWINDOW (window);
6754 f = XFRAME (w->frame);
6756 if (FRAME_WINDOW_P (f))
6757 result = (NILP (pixels_p)
6758 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6759 : make_number (-w->vscroll));
6760 else
6761 result = make_number (0);
6762 return result;
6766 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6767 2, 3, 0,
6768 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6769 WINDOW nil means use the selected window. Normally, VSCROLL is a
6770 non-negative multiple of the canonical character height of WINDOW;
6771 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6772 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6773 corresponds to an integral number of pixels. The return value is the
6774 result of this rounding.
6775 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6776 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6778 struct window *w;
6779 struct frame *f;
6781 if (NILP (window))
6782 window = selected_window;
6783 else
6784 CHECK_WINDOW (window);
6785 CHECK_NUMBER_OR_FLOAT (vscroll);
6787 w = XWINDOW (window);
6788 f = XFRAME (w->frame);
6790 if (FRAME_WINDOW_P (f))
6792 int old_dy = w->vscroll;
6794 w->vscroll = - (NILP (pixels_p)
6795 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6796 : XFLOATINT (vscroll));
6797 w->vscroll = min (w->vscroll, 0);
6799 if (w->vscroll != old_dy)
6801 /* Adjust glyph matrix of the frame if the virtual display
6802 area becomes larger than before. */
6803 if (w->vscroll < 0 && w->vscroll < old_dy)
6804 adjust_glyphs (f);
6806 /* Prevent redisplay shortcuts. */
6807 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6811 return Fwindow_vscroll (window, pixels_p);
6815 /* Call FN for all leaf windows on frame F. FN is called with the
6816 first argument being a pointer to the leaf window, and with
6817 additional argument USER_DATA. Stops when FN returns 0. */
6819 void
6820 foreach_window (struct frame *f, int (*fn) (struct window *, void *), void *user_data)
6822 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6823 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6824 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6828 /* Helper function for foreach_window. Call FN for all leaf windows
6829 reachable from W. FN is called with the first argument being a
6830 pointer to the leaf window, and with additional argument USER_DATA.
6831 Stop when FN returns 0. Value is 0 if stopped by FN. */
6833 static int
6834 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6836 int cont;
6838 for (cont = 1; w && cont;)
6840 if (!NILP (w->hchild))
6841 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6842 else if (!NILP (w->vchild))
6843 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6844 else
6845 cont = fn (w, user_data);
6847 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6850 return cont;
6854 /* Freeze or unfreeze the window start of W unless it is a
6855 mini-window or the selected window. FREEZE_P non-null means freeze
6856 the window start. */
6858 static int
6859 freeze_window_start (struct window *w, void *freeze_p)
6861 if (MINI_WINDOW_P (w)
6862 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6863 && (w == XWINDOW (selected_window)
6864 || (MINI_WINDOW_P (XWINDOW (selected_window))
6865 && ! NILP (Vminibuf_scroll_window)
6866 && w == XWINDOW (Vminibuf_scroll_window)))))
6867 freeze_p = NULL;
6869 w->frozen_window_start_p = freeze_p != NULL;
6870 return 1;
6874 /* Freeze or unfreeze the window starts of all leaf windows on frame
6875 F, except the selected window and a mini-window. FREEZE_P non-zero
6876 means freeze the window start. */
6878 void
6879 freeze_window_starts (struct frame *f, int freeze_p)
6881 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6885 /***********************************************************************
6886 Initialization
6887 ***********************************************************************/
6889 /* Return 1 if window configurations C1 and C2
6890 describe the same state of affairs. This is used by Fequal. */
6893 compare_window_configurations (Lisp_Object c1, Lisp_Object c2, int ignore_positions)
6895 register struct save_window_data *d1, *d2;
6896 struct Lisp_Vector *sw1, *sw2;
6897 int i;
6899 CHECK_WINDOW_CONFIGURATION (c1);
6900 CHECK_WINDOW_CONFIGURATION (c2);
6902 d1 = (struct save_window_data *) XVECTOR (c1);
6903 d2 = (struct save_window_data *) XVECTOR (c2);
6904 sw1 = XVECTOR (d1->saved_windows);
6905 sw2 = XVECTOR (d2->saved_windows);
6907 if (d1->frame_cols != d2->frame_cols)
6908 return 0;
6909 if (d1->frame_lines != d2->frame_lines)
6910 return 0;
6911 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
6912 return 0;
6913 if (! EQ (d1->selected_frame, d2->selected_frame))
6914 return 0;
6915 /* Don't compare the current_window field directly.
6916 Instead see w1_is_current and w2_is_current, below. */
6917 if (! EQ (d1->current_buffer, d2->current_buffer))
6918 return 0;
6919 if (! ignore_positions)
6921 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6922 return 0;
6923 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6924 return 0;
6926 /* Don't compare the root_window field.
6927 We don't require the two configurations
6928 to use the same window object,
6929 and the two root windows must be equivalent
6930 if everything else compares equal. */
6931 if (! EQ (d1->focus_frame, d2->focus_frame))
6932 return 0;
6934 /* Verify that the two confis have the same number of windows. */
6935 if (sw1->size != sw2->size)
6936 return 0;
6938 for (i = 0; i < sw1->size; i++)
6940 struct saved_window *p1, *p2;
6941 int w1_is_current, w2_is_current;
6943 p1 = SAVED_WINDOW_N (sw1, i);
6944 p2 = SAVED_WINDOW_N (sw2, i);
6946 /* Verify that the current windows in the two
6947 configurations correspond to each other. */
6948 w1_is_current = EQ (d1->current_window, p1->window);
6949 w2_is_current = EQ (d2->current_window, p2->window);
6951 if (w1_is_current != w2_is_current)
6952 return 0;
6954 /* Verify that the corresponding windows do match. */
6955 if (! EQ (p1->buffer, p2->buffer))
6956 return 0;
6957 if (! EQ (p1->left_col, p2->left_col))
6958 return 0;
6959 if (! EQ (p1->top_line, p2->top_line))
6960 return 0;
6961 if (! EQ (p1->total_cols, p2->total_cols))
6962 return 0;
6963 if (! EQ (p1->total_lines, p2->total_lines))
6964 return 0;
6965 if (! EQ (p1->display_table, p2->display_table))
6966 return 0;
6967 if (! EQ (p1->parent, p2->parent))
6968 return 0;
6969 if (! EQ (p1->prev, p2->prev))
6970 return 0;
6971 if (! ignore_positions)
6973 if (! EQ (p1->hscroll, p2->hscroll))
6974 return 0;
6975 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6976 return 0;
6977 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6978 return 0;
6979 if (NILP (Fequal (p1->start, p2->start)))
6980 return 0;
6981 if (NILP (Fequal (p1->pointm, p2->pointm)))
6982 return 0;
6983 if (NILP (Fequal (p1->mark, p2->mark)))
6984 return 0;
6986 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6987 return 0;
6988 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6989 return 0;
6990 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6991 return 0;
6992 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6993 return 0;
6994 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6995 return 0;
6996 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6997 return 0;
6998 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6999 return 0;
7002 return 1;
7005 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7006 Scompare_window_configurations, 2, 2, 0,
7007 doc: /* Compare two window configurations as regards the structure of windows.
7008 This function ignores details such as the values of point and mark
7009 and scrolling positions. */)
7010 (Lisp_Object x, Lisp_Object y)
7012 if (compare_window_configurations (x, y, 1))
7013 return Qt;
7014 return Qnil;
7017 void
7018 init_window_once (void)
7020 struct frame *f = make_initial_frame ();
7021 XSETFRAME (selected_frame, f);
7022 Vterminal_frame = selected_frame;
7023 minibuf_window = f->minibuffer_window;
7024 selected_window = f->selected_window;
7025 last_nonminibuf_frame = f;
7027 window_initialized = 1;
7030 void
7031 init_window (void)
7033 Vwindow_list = Qnil;
7036 void
7037 syms_of_window (void)
7039 Qscroll_up = intern_c_string ("scroll-up");
7040 staticpro (&Qscroll_up);
7042 Qscroll_down = intern_c_string ("scroll-down");
7043 staticpro (&Qscroll_down);
7045 Qscroll_command = intern_c_string ("scroll-command");
7046 staticpro (&Qscroll_command);
7048 Fput (Qscroll_up, Qscroll_command, Qt);
7049 Fput (Qscroll_down, Qscroll_command, Qt);
7051 Qwindow_size_fixed = intern_c_string ("window-size-fixed");
7052 staticpro (&Qwindow_size_fixed);
7053 Fset (Qwindow_size_fixed, Qnil);
7055 staticpro (&Qwindow_configuration_change_hook);
7056 Qwindow_configuration_change_hook
7057 = intern_c_string ("window-configuration-change-hook");
7059 Qwindowp = intern_c_string ("windowp");
7060 staticpro (&Qwindowp);
7062 Qwindow_configuration_p = intern_c_string ("window-configuration-p");
7063 staticpro (&Qwindow_configuration_p);
7065 Qwindow_live_p = intern_c_string ("window-live-p");
7066 staticpro (&Qwindow_live_p);
7068 Qdisplay_buffer = intern_c_string ("display-buffer");
7069 staticpro (&Qdisplay_buffer);
7071 Qtemp_buffer_show_hook = intern_c_string ("temp-buffer-show-hook");
7072 staticpro (&Qtemp_buffer_show_hook);
7074 staticpro (&Vwindow_list);
7076 minibuf_selected_window = Qnil;
7077 staticpro (&minibuf_selected_window);
7079 window_scroll_pixel_based_preserve_x = -1;
7080 window_scroll_pixel_based_preserve_y = -1;
7081 window_scroll_preserve_hpos = -1;
7082 window_scroll_preserve_vpos = -1;
7084 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
7085 doc: /* Non-nil means call as function to display a help buffer.
7086 The function is called with one argument, the buffer to be displayed.
7087 Used by `with-output-to-temp-buffer'.
7088 If this function is used, then it must do the entire job of showing
7089 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7090 Vtemp_buffer_show_function = Qnil;
7092 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
7093 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7094 Vminibuf_scroll_window = Qnil;
7096 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
7097 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7098 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7099 is displayed in the `mode-line' face. */);
7100 mode_line_in_non_selected_windows = 1;
7102 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
7103 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7104 Vother_window_scroll_buffer = Qnil;
7106 DEFVAR_BOOL ("auto-window-vscroll", &auto_window_vscroll_p,
7107 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7108 auto_window_vscroll_p = 1;
7110 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
7111 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7112 next_screen_context_lines = 2;
7114 DEFVAR_INT ("window-min-height", &window_min_height,
7115 doc: /* Allow deleting windows less than this tall.
7116 The value is measured in line units. If a window wants a modeline it
7117 is counted as one line.
7119 Emacs honors settings of this variable when enlarging or shrinking
7120 windows vertically. A value less than 1 is invalid. */);
7121 window_min_height = 4;
7123 DEFVAR_INT ("window-min-width", &window_min_width,
7124 doc: /* Allow deleting windows less than this wide.
7125 The value is measured in characters and includes any fringes or
7126 the scrollbar.
7128 Emacs honors settings of this variable when enlarging or shrinking
7129 windows horizontally. A value less than 2 is invalid. */);
7130 window_min_width = 10;
7132 DEFVAR_LISP ("scroll-preserve-screen-position",
7133 &Vscroll_preserve_screen_position,
7134 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
7135 A value of nil means point does not keep its screen position except
7136 at the scroll margin or window boundary respectively.
7137 A value of t means point keeps its screen position if the scroll
7138 command moved it vertically out of the window, e.g. when scrolling
7139 by full screens.
7140 Any other value means point always keeps its screen position.
7141 Scroll commands should have the `scroll-command' property
7142 on their symbols to be controlled by this variable. */);
7143 Vscroll_preserve_screen_position = Qnil;
7145 DEFVAR_LISP ("window-point-insertion-type", &Vwindow_point_insertion_type,
7146 doc: /* Type of marker to use for `window-point'. */);
7147 Vwindow_point_insertion_type = Qnil;
7149 DEFVAR_LISP ("window-configuration-change-hook",
7150 &Vwindow_configuration_change_hook,
7151 doc: /* Functions to call when window configuration changes.
7152 The buffer-local part is run once per window, with the relevant window
7153 selected; while the global part is run only once for the modified frame,
7154 with the relevant frame selected. */);
7155 Vwindow_configuration_change_hook = Qnil;
7157 DEFVAR_LISP ("recenter-redisplay", &Vrecenter_redisplay,
7158 doc: /* If non-nil, then the `recenter' command with a nil argument
7159 will redraw the entire frame; the special value `tty' causes the
7160 frame to be redrawn only if it is a tty frame. */);
7161 Vrecenter_redisplay = Qtty;
7164 defsubr (&Sselected_window);
7165 defsubr (&Sminibuffer_window);
7166 defsubr (&Swindow_minibuffer_p);
7167 defsubr (&Swindowp);
7168 defsubr (&Swindow_live_p);
7169 defsubr (&Spos_visible_in_window_p);
7170 defsubr (&Swindow_line_height);
7171 defsubr (&Swindow_buffer);
7172 defsubr (&Swindow_height);
7173 defsubr (&Swindow_width);
7174 defsubr (&Swindow_full_width_p);
7175 defsubr (&Swindow_hscroll);
7176 defsubr (&Sset_window_hscroll);
7177 defsubr (&Swindow_redisplay_end_trigger);
7178 defsubr (&Sset_window_redisplay_end_trigger);
7179 defsubr (&Swindow_edges);
7180 defsubr (&Swindow_pixel_edges);
7181 defsubr (&Swindow_absolute_pixel_edges);
7182 defsubr (&Swindow_inside_edges);
7183 defsubr (&Swindow_inside_pixel_edges);
7184 defsubr (&Swindow_inside_absolute_pixel_edges);
7185 defsubr (&Scoordinates_in_window_p);
7186 defsubr (&Swindow_at);
7187 defsubr (&Swindow_point);
7188 defsubr (&Swindow_start);
7189 defsubr (&Swindow_end);
7190 defsubr (&Sset_window_point);
7191 defsubr (&Sset_window_start);
7192 defsubr (&Swindow_dedicated_p);
7193 defsubr (&Sset_window_dedicated_p);
7194 defsubr (&Swindow_display_table);
7195 defsubr (&Sset_window_display_table);
7196 defsubr (&Snext_window);
7197 defsubr (&Sprevious_window);
7198 defsubr (&Sother_window);
7199 defsubr (&Sget_lru_window);
7200 defsubr (&Sget_largest_window);
7201 defsubr (&Sget_buffer_window);
7202 defsubr (&Sdelete_other_windows);
7203 defsubr (&Sdelete_windows_on);
7204 defsubr (&Sreplace_buffer_in_windows);
7205 defsubr (&Sdelete_window);
7206 defsubr (&Sset_window_buffer);
7207 defsubr (&Sselect_window);
7208 defsubr (&Sforce_window_update);
7209 defsubr (&Ssplit_window);
7210 defsubr (&Senlarge_window);
7211 defsubr (&Sshrink_window);
7212 defsubr (&Sadjust_window_trailing_edge);
7213 defsubr (&Sscroll_up);
7214 defsubr (&Sscroll_down);
7215 defsubr (&Sscroll_left);
7216 defsubr (&Sscroll_right);
7217 defsubr (&Sother_window_for_scrolling);
7218 defsubr (&Sscroll_other_window);
7219 defsubr (&Sminibuffer_selected_window);
7220 defsubr (&Srecenter);
7221 defsubr (&Swindow_text_height);
7222 defsubr (&Smove_to_window_line);
7223 defsubr (&Swindow_configuration_p);
7224 defsubr (&Swindow_configuration_frame);
7225 defsubr (&Sset_window_configuration);
7226 defsubr (&Scurrent_window_configuration);
7227 defsubr (&Ssave_window_excursion);
7228 defsubr (&Swindow_tree);
7229 defsubr (&Sset_window_margins);
7230 defsubr (&Swindow_margins);
7231 defsubr (&Sset_window_fringes);
7232 defsubr (&Swindow_fringes);
7233 defsubr (&Sset_window_scroll_bars);
7234 defsubr (&Swindow_scroll_bars);
7235 defsubr (&Swindow_vscroll);
7236 defsubr (&Sset_window_vscroll);
7237 defsubr (&Scompare_window_configurations);
7238 defsubr (&Swindow_list);
7239 defsubr (&Swindow_parameters);
7240 defsubr (&Swindow_parameter);
7241 defsubr (&Sset_window_parameter);
7245 void
7246 keys_of_window (void)
7248 initial_define_key (control_x_map, '1', "delete-other-windows");
7249 initial_define_key (control_x_map, '2', "split-window");
7250 initial_define_key (control_x_map, '0', "delete-window");
7251 initial_define_key (control_x_map, 'o', "other-window");
7252 initial_define_key (control_x_map, '^', "enlarge-window");
7253 initial_define_key (control_x_map, '<', "scroll-left");
7254 initial_define_key (control_x_map, '>', "scroll-right");
7256 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7257 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7258 initial_define_key (meta_map, 'v', "scroll-down-command");
7261 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
7262 (do not change this comment) */