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