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