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