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