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