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