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