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