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