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