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