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