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