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