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