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