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