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