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