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