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