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