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