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