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