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