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