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