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