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