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