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