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