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