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