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