xref: /vim-8.2.3635/src/window.c (revision d09a206e)
1 /* vi:set ts=8 sts=4 sw=4 noet:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read a list of people who contributed.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 #include "vim.h"
11 
12 static int path_is_url(char_u *p);
13 static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
14 static void win_init(win_T *newp, win_T *oldp, int flags);
15 static void win_init_some(win_T *newp, win_T *oldp);
16 static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
17 static void frame_setheight(frame_T *curfrp, int height);
18 static void frame_setwidth(frame_T *curfrp, int width);
19 static void win_exchange(long);
20 static void win_rotate(int, int);
21 static void win_totop(int size, int flags);
22 static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
23 static int last_window(void);
24 static int close_last_window_tabpage(win_T *win, int free_buf, tabpage_T *prev_curtab);
25 static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
26 static frame_T *win_altframe(win_T *win, tabpage_T *tp);
27 static tabpage_T *alt_tabpage(void);
28 static win_T *frame2win(frame_T *frp);
29 static int frame_has_win(frame_T *frp, win_T *wp);
30 static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
31 static int frame_fixed_height(frame_T *frp);
32 static int frame_fixed_width(frame_T *frp);
33 static void frame_add_statusline(frame_T *frp);
34 static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
35 static void frame_add_vsep(frame_T *frp);
36 static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
37 static void frame_fix_width(win_T *wp);
38 static int win_alloc_firstwin(win_T *oldwin);
39 static void new_frame(win_T *wp);
40 static tabpage_T *alloc_tabpage(void);
41 static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
42 static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
43 static void frame_fix_height(win_T *wp);
44 static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
45 static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_new_autocmds, int trigger_enter_autocmds, int trigger_leave_autocmds);
46 static void win_free(win_T *wp, tabpage_T *tp);
47 static void frame_append(frame_T *after, frame_T *frp);
48 static void frame_insert(frame_T *before, frame_T *frp);
49 static void frame_remove(frame_T *frp);
50 static void win_goto_ver(int up, long count);
51 static void win_goto_hor(int left, long count);
52 static void frame_add_height(frame_T *frp, int n);
53 static void last_status_rec(frame_T *fr, int statusline);
54 
55 static void make_snapshot_rec(frame_T *fr, frame_T **frp);
56 static void clear_snapshot(tabpage_T *tp, int idx);
57 static void clear_snapshot_rec(frame_T *fr);
58 static int check_snapshot_rec(frame_T *sn, frame_T *fr);
59 static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
60 
61 static int frame_check_height(frame_T *topfrp, int height);
62 static int frame_check_width(frame_T *topfrp, int width);
63 
64 static win_T *win_alloc(win_T *after, int hidden);
65 
66 #define URL_SLASH	1		/* path_is_url() has found "://" */
67 #define URL_BACKSLASH	2		/* path_is_url() has found ":\\" */
68 
69 #define NOWIN		(win_T *)-1	/* non-existing window */
70 
71 #define ROWS_AVAIL (Rows - p_ch - tabline_height())
72 
73 static char *m_onlyone = N_("Already only one window");
74 
75 /*
76  * all CTRL-W window commands are handled here, called from normal_cmd().
77  */
78     void
79 do_window(
80     int		nchar,
81     long	Prenum,
82     int		xchar)	    /* extra char from ":wincmd gx" or NUL */
83 {
84     long	Prenum1;
85     win_T	*wp;
86 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
87     char_u	*ptr;
88     linenr_T    lnum = -1;
89 #endif
90 #ifdef FEAT_FIND_ID
91     int		type = FIND_DEFINE;
92     int		len;
93 #endif
94     char_u	cbuf[40];
95 
96     if (Prenum == 0)
97 	Prenum1 = 1;
98     else
99 	Prenum1 = Prenum;
100 
101 #ifdef FEAT_CMDWIN
102 # define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
103 #else
104 # define CHECK_CMDWIN
105 #endif
106 
107     switch (nchar)
108     {
109 /* split current window in two parts, horizontally */
110     case 'S':
111     case Ctrl_S:
112     case 's':
113 		CHECK_CMDWIN
114 		reset_VIsual_and_resel();	/* stop Visual mode */
115 #ifdef FEAT_QUICKFIX
116 		/* When splitting the quickfix window open a new buffer in it,
117 		 * don't replicate the quickfix buffer. */
118 		if (bt_quickfix(curbuf))
119 		    goto newwindow;
120 #endif
121 #ifdef FEAT_GUI
122 		need_mouse_correct = TRUE;
123 #endif
124 		(void)win_split((int)Prenum, 0);
125 		break;
126 
127 /* split current window in two parts, vertically */
128     case Ctrl_V:
129     case 'v':
130 		CHECK_CMDWIN
131 		reset_VIsual_and_resel();	/* stop Visual mode */
132 #ifdef FEAT_QUICKFIX
133 		/* When splitting the quickfix window open a new buffer in it,
134 		 * don't replicate the quickfix buffer. */
135 		if (bt_quickfix(curbuf))
136 		    goto newwindow;
137 #endif
138 #ifdef FEAT_GUI
139 		need_mouse_correct = TRUE;
140 #endif
141 		(void)win_split((int)Prenum, WSP_VERT);
142 		break;
143 
144 /* split current window and edit alternate file */
145     case Ctrl_HAT:
146     case '^':
147 		CHECK_CMDWIN
148 		reset_VIsual_and_resel();	/* stop Visual mode */
149 		cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
150 		do_cmdline_cmd(cbuf);
151 		break;
152 
153 /* open new window */
154     case Ctrl_N:
155     case 'n':
156 		CHECK_CMDWIN
157 		reset_VIsual_and_resel();	/* stop Visual mode */
158 #ifdef FEAT_QUICKFIX
159 newwindow:
160 #endif
161 		if (Prenum)
162 		    /* window height */
163 		    vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
164 		else
165 		    cbuf[0] = NUL;
166 #if defined(FEAT_QUICKFIX)
167 		if (nchar == 'v' || nchar == Ctrl_V)
168 		    STRCAT(cbuf, "v");
169 #endif
170 		STRCAT(cbuf, "new");
171 		do_cmdline_cmd(cbuf);
172 		break;
173 
174 /* quit current window */
175     case Ctrl_Q:
176     case 'q':
177 		reset_VIsual_and_resel();	/* stop Visual mode */
178 		cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
179 		do_cmdline_cmd(cbuf);
180 		break;
181 
182 /* close current window */
183     case Ctrl_C:
184     case 'c':
185 		reset_VIsual_and_resel();	/* stop Visual mode */
186 		cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
187 		do_cmdline_cmd(cbuf);
188 		break;
189 
190 #if defined(FEAT_QUICKFIX)
191 /* close preview window */
192     case Ctrl_Z:
193     case 'z':
194 		CHECK_CMDWIN
195 		reset_VIsual_and_resel();	/* stop Visual mode */
196 		do_cmdline_cmd((char_u *)"pclose");
197 		break;
198 
199 /* cursor to preview window */
200     case 'P':
201 		FOR_ALL_WINDOWS(wp)
202 		    if (wp->w_p_pvw)
203 			break;
204 		if (wp == NULL)
205 		    EMSG(_("E441: There is no preview window"));
206 		else
207 		    win_goto(wp);
208 		break;
209 #endif
210 
211 /* close all but current window */
212     case Ctrl_O:
213     case 'o':
214 		CHECK_CMDWIN
215 		reset_VIsual_and_resel();	/* stop Visual mode */
216 		cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
217 		do_cmdline_cmd(cbuf);
218 		break;
219 
220 /* cursor to next window with wrap around */
221     case Ctrl_W:
222     case 'w':
223 /* cursor to previous window with wrap around */
224     case 'W':
225 		CHECK_CMDWIN
226 		if (ONE_WINDOW && Prenum != 1)	/* just one window */
227 		    beep_flush();
228 		else
229 		{
230 		    if (Prenum)			/* go to specified window */
231 		    {
232 			for (wp = firstwin; --Prenum > 0; )
233 			{
234 			    if (wp->w_next == NULL)
235 				break;
236 			    else
237 				wp = wp->w_next;
238 			}
239 		    }
240 		    else
241 		    {
242 			if (nchar == 'W')	    /* go to previous window */
243 			{
244 			    wp = curwin->w_prev;
245 			    if (wp == NULL)
246 				wp = lastwin;	    /* wrap around */
247 			}
248 			else			    /* go to next window */
249 			{
250 			    wp = curwin->w_next;
251 			    if (wp == NULL)
252 				wp = firstwin;	    /* wrap around */
253 			}
254 		    }
255 		    win_goto(wp);
256 		}
257 		break;
258 
259 /* cursor to window below */
260     case 'j':
261     case K_DOWN:
262     case Ctrl_J:
263 		CHECK_CMDWIN
264 		win_goto_ver(FALSE, Prenum1);
265 		break;
266 
267 /* cursor to window above */
268     case 'k':
269     case K_UP:
270     case Ctrl_K:
271 		CHECK_CMDWIN
272 		win_goto_ver(TRUE, Prenum1);
273 		break;
274 
275 /* cursor to left window */
276     case 'h':
277     case K_LEFT:
278     case Ctrl_H:
279     case K_BS:
280 		CHECK_CMDWIN
281 		win_goto_hor(TRUE, Prenum1);
282 		break;
283 
284 /* cursor to right window */
285     case 'l':
286     case K_RIGHT:
287     case Ctrl_L:
288 		CHECK_CMDWIN
289 		win_goto_hor(FALSE, Prenum1);
290 		break;
291 
292 /* move window to new tab page */
293     case 'T':
294 		if (one_window())
295 		    MSG(_(m_onlyone));
296 		else
297 		{
298 		    tabpage_T	*oldtab = curtab;
299 		    tabpage_T	*newtab;
300 
301 		    /* First create a new tab with the window, then go back to
302 		     * the old tab and close the window there. */
303 		    wp = curwin;
304 		    if (win_new_tabpage((int)Prenum) == OK
305 						     && valid_tabpage(oldtab))
306 		    {
307 			newtab = curtab;
308 			goto_tabpage_tp(oldtab, TRUE, TRUE);
309 			if (curwin == wp)
310 			    win_close(curwin, FALSE);
311 			if (valid_tabpage(newtab))
312 			    goto_tabpage_tp(newtab, TRUE, TRUE);
313 		    }
314 		}
315 		break;
316 
317 /* cursor to top-left window */
318     case 't':
319     case Ctrl_T:
320 		win_goto(firstwin);
321 		break;
322 
323 /* cursor to bottom-right window */
324     case 'b':
325     case Ctrl_B:
326 		win_goto(lastwin);
327 		break;
328 
329 /* cursor to last accessed (previous) window */
330     case 'p':
331     case Ctrl_P:
332 		if (!win_valid(prevwin))
333 		    beep_flush();
334 		else
335 		    win_goto(prevwin);
336 		break;
337 
338 /* exchange current and next window */
339     case 'x':
340     case Ctrl_X:
341 		CHECK_CMDWIN
342 		win_exchange(Prenum);
343 		break;
344 
345 /* rotate windows downwards */
346     case Ctrl_R:
347     case 'r':
348 		CHECK_CMDWIN
349 		reset_VIsual_and_resel();	/* stop Visual mode */
350 		win_rotate(FALSE, (int)Prenum1);    /* downwards */
351 		break;
352 
353 /* rotate windows upwards */
354     case 'R':
355 		CHECK_CMDWIN
356 		reset_VIsual_and_resel();	/* stop Visual mode */
357 		win_rotate(TRUE, (int)Prenum1);	    /* upwards */
358 		break;
359 
360 /* move window to the very top/bottom/left/right */
361     case 'K':
362     case 'J':
363     case 'H':
364     case 'L':
365 		CHECK_CMDWIN
366 		win_totop((int)Prenum,
367 			((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
368 			| ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
369 		break;
370 
371 /* make all windows the same height */
372     case '=':
373 #ifdef FEAT_GUI
374 		need_mouse_correct = TRUE;
375 #endif
376 		win_equal(NULL, FALSE, 'b');
377 		break;
378 
379 /* increase current window height */
380     case '+':
381 #ifdef FEAT_GUI
382 		need_mouse_correct = TRUE;
383 #endif
384 		win_setheight(curwin->w_height + (int)Prenum1);
385 		break;
386 
387 /* decrease current window height */
388     case '-':
389 #ifdef FEAT_GUI
390 		need_mouse_correct = TRUE;
391 #endif
392 		win_setheight(curwin->w_height - (int)Prenum1);
393 		break;
394 
395 /* set current window height */
396     case Ctrl__:
397     case '_':
398 #ifdef FEAT_GUI
399 		need_mouse_correct = TRUE;
400 #endif
401 		win_setheight(Prenum ? (int)Prenum : 9999);
402 		break;
403 
404 /* increase current window width */
405     case '>':
406 #ifdef FEAT_GUI
407 		need_mouse_correct = TRUE;
408 #endif
409 		win_setwidth(curwin->w_width + (int)Prenum1);
410 		break;
411 
412 /* decrease current window width */
413     case '<':
414 #ifdef FEAT_GUI
415 		need_mouse_correct = TRUE;
416 #endif
417 		win_setwidth(curwin->w_width - (int)Prenum1);
418 		break;
419 
420 /* set current window width */
421     case '|':
422 #ifdef FEAT_GUI
423 		need_mouse_correct = TRUE;
424 #endif
425 		win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
426 		break;
427 
428 /* jump to tag and split window if tag exists (in preview window) */
429 #if defined(FEAT_QUICKFIX)
430     case '}':
431 		CHECK_CMDWIN
432 		if (Prenum)
433 		    g_do_tagpreview = Prenum;
434 		else
435 		    g_do_tagpreview = p_pvh;
436 #endif
437 		/* FALLTHROUGH */
438     case ']':
439     case Ctrl_RSB:
440 		CHECK_CMDWIN
441 		/* keep Visual mode, can select words to use as a tag */
442 		if (Prenum)
443 		    postponed_split = Prenum;
444 		else
445 		    postponed_split = -1;
446 #ifdef FEAT_QUICKFIX
447 		if (nchar != '}')
448 		    g_do_tagpreview = 0;
449 #endif
450 
451 		/* Execute the command right here, required when "wincmd ]"
452 		 * was used in a function. */
453 		do_nv_ident(Ctrl_RSB, NUL);
454 		break;
455 
456 #ifdef FEAT_SEARCHPATH
457 /* edit file name under cursor in a new window */
458     case 'f':
459     case 'F':
460     case Ctrl_F:
461 wingotofile:
462 		CHECK_CMDWIN
463 
464 		ptr = grab_file_name(Prenum1, &lnum);
465 		if (ptr != NULL)
466 		{
467 		    tabpage_T	*oldtab = curtab;
468 		    win_T	*oldwin = curwin;
469 # ifdef FEAT_GUI
470 		    need_mouse_correct = TRUE;
471 # endif
472 		    setpcmark();
473 		    if (win_split(0, 0) == OK)
474 		    {
475 			RESET_BINDING(curwin);
476 			if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
477 						   ECMD_HIDE, NULL) == FAIL)
478 			{
479 			    /* Failed to open the file, close the window
480 			     * opened for it. */
481 			    win_close(curwin, FALSE);
482 			    goto_tabpage_win(oldtab, oldwin);
483 			}
484 			else if (nchar == 'F' && lnum >= 0)
485 			{
486 			    curwin->w_cursor.lnum = lnum;
487 			    check_cursor_lnum();
488 			    beginline(BL_SOL | BL_FIX);
489 			}
490 		    }
491 		    vim_free(ptr);
492 		}
493 		break;
494 #endif
495 
496 #ifdef FEAT_FIND_ID
497 /* Go to the first occurrence of the identifier under cursor along path in a
498  * new window -- webb
499  */
500     case 'i':			    /* Go to any match */
501     case Ctrl_I:
502 		type = FIND_ANY;
503 		/* FALLTHROUGH */
504     case 'd':			    /* Go to definition, using 'define' */
505     case Ctrl_D:
506 		CHECK_CMDWIN
507 		if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
508 		    break;
509 		find_pattern_in_path(ptr, 0, len, TRUE,
510 			Prenum == 0 ? TRUE : FALSE, type,
511 			Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
512 		curwin->w_set_curswant = TRUE;
513 		break;
514 #endif
515 
516     case K_KENTER:
517     case CAR:
518 #if defined(FEAT_QUICKFIX)
519 		/*
520 		 * In a quickfix window a <CR> jumps to the error under the
521 		 * cursor in a new window.
522 		 */
523 		if (bt_quickfix(curbuf))
524 		{
525 		    sprintf((char *)cbuf, "split +%ld%s",
526 				(long)curwin->w_cursor.lnum,
527 				(curwin->w_llist_ref == NULL) ? "cc" : "ll");
528 		    do_cmdline_cmd(cbuf);
529 		}
530 #endif
531 		break;
532 
533 
534 /* CTRL-W g  extended commands */
535     case 'g':
536     case Ctrl_G:
537 		CHECK_CMDWIN
538 #ifdef USE_ON_FLY_SCROLL
539 		dont_scroll = TRUE;		/* disallow scrolling here */
540 #endif
541 		++no_mapping;
542 		++allow_keys;   /* no mapping for xchar, but allow key codes */
543 		if (xchar == NUL)
544 		    xchar = plain_vgetc();
545 		LANGMAP_ADJUST(xchar, TRUE);
546 		--no_mapping;
547 		--allow_keys;
548 #ifdef FEAT_CMDL_INFO
549 		(void)add_to_showcmd(xchar);
550 #endif
551 		switch (xchar)
552 		{
553 #if defined(FEAT_QUICKFIX)
554 		    case '}':
555 			xchar = Ctrl_RSB;
556 			if (Prenum)
557 			    g_do_tagpreview = Prenum;
558 			else
559 			    g_do_tagpreview = p_pvh;
560 #endif
561 			/* FALLTHROUGH */
562 		    case ']':
563 		    case Ctrl_RSB:
564 			/* keep Visual mode, can select words to use as a tag */
565 			if (Prenum)
566 			    postponed_split = Prenum;
567 			else
568 			    postponed_split = -1;
569 
570 			/* Execute the command right here, required when
571 			 * "wincmd g}" was used in a function. */
572 			do_nv_ident('g', xchar);
573 			break;
574 
575 #ifdef FEAT_SEARCHPATH
576 		    case 'f':	    /* CTRL-W gf: "gf" in a new tab page */
577 		    case 'F':	    /* CTRL-W gF: "gF" in a new tab page */
578 			cmdmod.tab = tabpage_index(curtab) + 1;
579 			nchar = xchar;
580 			goto wingotofile;
581 #endif
582 		    default:
583 			beep_flush();
584 			break;
585 		}
586 		break;
587 
588     default:	beep_flush();
589 		break;
590     }
591 }
592 
593 /*
594  * Figure out the address type for ":wnncmd".
595  */
596     void
597 get_wincmd_addr_type(char_u *arg, exarg_T *eap)
598 {
599     switch (*arg)
600     {
601     case 'S':
602     case Ctrl_S:
603     case 's':
604     case Ctrl_N:
605     case 'n':
606     case 'j':
607     case Ctrl_J:
608     case 'k':
609     case Ctrl_K:
610     case 'T':
611     case Ctrl_R:
612     case 'r':
613     case 'R':
614     case 'K':
615     case 'J':
616     case '+':
617     case '-':
618     case Ctrl__:
619     case '_':
620     case '|':
621     case ']':
622     case Ctrl_RSB:
623     case 'g':
624     case Ctrl_G:
625     case Ctrl_V:
626     case 'v':
627     case 'h':
628     case Ctrl_H:
629     case 'l':
630     case Ctrl_L:
631     case 'H':
632     case 'L':
633     case '>':
634     case '<':
635 #if defined(FEAT_QUICKFIX)
636     case '}':
637 #endif
638 #ifdef FEAT_SEARCHPATH
639     case 'f':
640     case 'F':
641     case Ctrl_F:
642 #endif
643 #ifdef FEAT_FIND_ID
644     case 'i':
645     case Ctrl_I:
646     case 'd':
647     case Ctrl_D:
648 #endif
649 		/* window size or any count */
650 		eap->addr_type = ADDR_LINES;
651 		break;
652 
653     case Ctrl_HAT:
654     case '^':
655 		/* buffer number */
656 		eap->addr_type = ADDR_BUFFERS;
657 		break;
658 
659     case Ctrl_Q:
660     case 'q':
661     case Ctrl_C:
662     case 'c':
663     case Ctrl_O:
664     case 'o':
665     case Ctrl_W:
666     case 'w':
667     case 'W':
668     case 'x':
669     case Ctrl_X:
670 		/* window number */
671 		eap->addr_type = ADDR_WINDOWS;
672 		break;
673 
674 #if defined(FEAT_QUICKFIX)
675     case Ctrl_Z:
676     case 'z':
677     case 'P':
678 #endif
679     case 't':
680     case Ctrl_T:
681     case 'b':
682     case Ctrl_B:
683     case 'p':
684     case Ctrl_P:
685     case '=':
686     case CAR:
687 		/* no count */
688 		eap->addr_type = 0;
689 		break;
690     }
691 }
692 
693     static void
694 cmd_with_count(
695     char	*cmd,
696     char_u	*bufp,
697     size_t	bufsize,
698     long	Prenum)
699 {
700     size_t	len = STRLEN(cmd);
701 
702     STRCPY(bufp, cmd);
703     if (Prenum > 0)
704 	vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
705 }
706 
707 /*
708  * split the current window, implements CTRL-W s and :split
709  *
710  * "size" is the height or width for the new window, 0 to use half of current
711  * height or width.
712  *
713  * "flags":
714  * WSP_ROOM: require enough room for new window
715  * WSP_VERT: vertical split.
716  * WSP_TOP:  open window at the top-left of the shell (help window).
717  * WSP_BOT:  open window at the bottom-right of the shell (quickfix window).
718  * WSP_HELP: creating the help window, keep layout snapshot
719  *
720  * return FAIL for failure, OK otherwise
721  */
722     int
723 win_split(int size, int flags)
724 {
725     /* When the ":tab" modifier was used open a new tab page instead. */
726     if (may_open_tabpage() == OK)
727 	return OK;
728 
729     /* Add flags from ":vertical", ":topleft" and ":botright". */
730     flags |= cmdmod.split;
731     if ((flags & WSP_TOP) && (flags & WSP_BOT))
732     {
733 	EMSG(_("E442: Can't split topleft and botright at the same time"));
734 	return FAIL;
735     }
736 
737     /* When creating the help window make a snapshot of the window layout.
738      * Otherwise clear the snapshot, it's now invalid. */
739     if (flags & WSP_HELP)
740 	make_snapshot(SNAP_HELP_IDX);
741     else
742 	clear_snapshot(curtab, SNAP_HELP_IDX);
743 
744     return win_split_ins(size, flags, NULL, 0);
745 }
746 
747 /*
748  * When "new_wp" is NULL: split the current window in two.
749  * When "new_wp" is not NULL: insert this window at the far
750  * top/left/right/bottom.
751  * return FAIL for failure, OK otherwise
752  */
753     int
754 win_split_ins(
755     int		size,
756     int		flags,
757     win_T	*new_wp,
758     int		dir)
759 {
760     win_T	*wp = new_wp;
761     win_T	*oldwin;
762     int		new_size = size;
763     int		i;
764     int		need_status = 0;
765     int		do_equal = FALSE;
766     int		needed;
767     int		available;
768     int		oldwin_height = 0;
769     int		layout;
770     frame_T	*frp, *curfrp, *frp2, *prevfrp;
771     int		before;
772     int		minheight;
773     int		wmh1;
774 
775     if (flags & WSP_TOP)
776 	oldwin = firstwin;
777     else if (flags & WSP_BOT)
778 	oldwin = lastwin;
779     else
780 	oldwin = curwin;
781 
782     /* add a status line when p_ls == 1 and splitting the first window */
783     if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
784     {
785 	if (oldwin->w_height <= p_wmh && new_wp == NULL)
786 	{
787 	    EMSG(_(e_noroom));
788 	    return FAIL;
789 	}
790 	need_status = STATUS_HEIGHT;
791     }
792 
793 #ifdef FEAT_GUI
794     /* May be needed for the scrollbars that are going to change. */
795     if (gui.in_use)
796 	out_flush();
797 #endif
798 
799     if (flags & WSP_VERT)
800     {
801 	int	wmw1;
802 	int	minwidth;
803 
804 	layout = FR_ROW;
805 
806 	/*
807 	 * Check if we are able to split the current window and compute its
808 	 * width.
809 	 */
810 	/* Current window requires at least 1 space. */
811 	wmw1 = (p_wmw == 0 ? 1 : p_wmw);
812 	needed = wmw1 + 1;
813 	if (flags & WSP_ROOM)
814 	    needed += p_wiw - wmw1;
815 	if (flags & (WSP_BOT | WSP_TOP))
816 	{
817 	    minwidth = frame_minwidth(topframe, NOWIN);
818 	    available = topframe->fr_width;
819 	    needed += minwidth;
820 	}
821 	else if (p_ea)
822 	{
823 	    minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
824 	    prevfrp = oldwin->w_frame;
825 	    for (frp = oldwin->w_frame->fr_parent; frp != NULL;
826 							frp = frp->fr_parent)
827 	    {
828 		if (frp->fr_layout == FR_ROW)
829 		    for (frp2 = frp->fr_child; frp2 != NULL;
830 							frp2 = frp2->fr_next)
831 			if (frp2 != prevfrp)
832 			    minwidth += frame_minwidth(frp2, NOWIN);
833 		prevfrp = frp;
834 	    }
835 	    available = topframe->fr_width;
836 	    needed += minwidth;
837 	}
838 	else
839 	{
840 	    minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
841 	    available = oldwin->w_frame->fr_width;
842 	    needed += minwidth;
843 	}
844 	if (available < needed && new_wp == NULL)
845 	{
846 	    EMSG(_(e_noroom));
847 	    return FAIL;
848 	}
849 	if (new_size == 0)
850 	    new_size = oldwin->w_width / 2;
851 	if (new_size > available - minwidth - 1)
852 	    new_size = available - minwidth - 1;
853 	if (new_size < wmw1)
854 	    new_size = wmw1;
855 
856 	/* if it doesn't fit in the current window, need win_equal() */
857 	if (oldwin->w_width - new_size - 1 < p_wmw)
858 	    do_equal = TRUE;
859 
860 	/* We don't like to take lines for the new window from a
861 	 * 'winfixwidth' window.  Take them from a window to the left or right
862 	 * instead, if possible. Add one for the separator. */
863 	if (oldwin->w_p_wfw)
864 	    win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
865 
866 	/* Only make all windows the same width if one of them (except oldwin)
867 	 * is wider than one of the split windows. */
868 	if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
869 	   && oldwin->w_frame->fr_parent != NULL)
870 	{
871 	    frp = oldwin->w_frame->fr_parent->fr_child;
872 	    while (frp != NULL)
873 	    {
874 		if (frp->fr_win != oldwin && frp->fr_win != NULL
875 			&& (frp->fr_win->w_width > new_size
876 			    || frp->fr_win->w_width > oldwin->w_width
877 							      - new_size - 1))
878 		{
879 		    do_equal = TRUE;
880 		    break;
881 		}
882 		frp = frp->fr_next;
883 	    }
884 	}
885     }
886     else
887     {
888 	layout = FR_COL;
889 
890 	/*
891 	 * Check if we are able to split the current window and compute its
892 	 * height.
893 	 */
894 	/* Current window requires at least 1 space. */
895 	wmh1 = (p_wmh == 0 ? 1 : p_wmh);
896 	needed = wmh1 + STATUS_HEIGHT;
897 	if (flags & WSP_ROOM)
898 	    needed += p_wh - wmh1;
899 	if (flags & (WSP_BOT | WSP_TOP))
900 	{
901 	    minheight = frame_minheight(topframe, NOWIN) + need_status;
902 	    available = topframe->fr_height;
903 	    needed += minheight;
904 	}
905 	else if (p_ea)
906 	{
907 	    minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
908 	    prevfrp = oldwin->w_frame;
909 	    for (frp = oldwin->w_frame->fr_parent; frp != NULL;
910 							frp = frp->fr_parent)
911 	    {
912 		if (frp->fr_layout == FR_COL)
913 		    for (frp2 = frp->fr_child; frp2 != NULL;
914 							frp2 = frp2->fr_next)
915 			if (frp2 != prevfrp)
916 			    minheight += frame_minheight(frp2, NOWIN);
917 		prevfrp = frp;
918 	    }
919 	    available = topframe->fr_height;
920 	    needed += minheight;
921 	}
922 	else
923 	{
924 	    minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
925 	    available = oldwin->w_frame->fr_height;
926 	    needed += minheight;
927 	}
928 	if (available < needed && new_wp == NULL)
929 	{
930 	    EMSG(_(e_noroom));
931 	    return FAIL;
932 	}
933 	oldwin_height = oldwin->w_height;
934 	if (need_status)
935 	{
936 	    oldwin->w_status_height = STATUS_HEIGHT;
937 	    oldwin_height -= STATUS_HEIGHT;
938 	}
939 	if (new_size == 0)
940 	    new_size = oldwin_height / 2;
941 	if (new_size > available - minheight - STATUS_HEIGHT)
942 	    new_size = available - minheight - STATUS_HEIGHT;
943 	if (new_size < wmh1)
944 	    new_size = wmh1;
945 
946 	/* if it doesn't fit in the current window, need win_equal() */
947 	if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
948 	    do_equal = TRUE;
949 
950 	/* We don't like to take lines for the new window from a
951 	 * 'winfixheight' window.  Take them from a window above or below
952 	 * instead, if possible. */
953 	if (oldwin->w_p_wfh)
954 	{
955 	    win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
956 								      oldwin);
957 	    oldwin_height = oldwin->w_height;
958 	    if (need_status)
959 		oldwin_height -= STATUS_HEIGHT;
960 	}
961 
962 	/* Only make all windows the same height if one of them (except oldwin)
963 	 * is higher than one of the split windows. */
964 	if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
965 	   && oldwin->w_frame->fr_parent != NULL)
966 	{
967 	    frp = oldwin->w_frame->fr_parent->fr_child;
968 	    while (frp != NULL)
969 	    {
970 		if (frp->fr_win != oldwin && frp->fr_win != NULL
971 			&& (frp->fr_win->w_height > new_size
972 			    || frp->fr_win->w_height > oldwin_height - new_size
973 							      - STATUS_HEIGHT))
974 		{
975 		    do_equal = TRUE;
976 		    break;
977 		}
978 		frp = frp->fr_next;
979 	    }
980 	}
981     }
982 
983     /*
984      * allocate new window structure and link it in the window list
985      */
986     if ((flags & WSP_TOP) == 0
987 	    && ((flags & WSP_BOT)
988 		|| (flags & WSP_BELOW)
989 		|| (!(flags & WSP_ABOVE)
990 		    && ( (flags & WSP_VERT) ? p_spr : p_sb))))
991     {
992 	/* new window below/right of current one */
993 	if (new_wp == NULL)
994 	    wp = win_alloc(oldwin, FALSE);
995 	else
996 	    win_append(oldwin, wp);
997     }
998     else
999     {
1000 	if (new_wp == NULL)
1001 	    wp = win_alloc(oldwin->w_prev, FALSE);
1002 	else
1003 	    win_append(oldwin->w_prev, wp);
1004     }
1005 
1006     if (new_wp == NULL)
1007     {
1008 	if (wp == NULL)
1009 	    return FAIL;
1010 
1011 	new_frame(wp);
1012 	if (wp->w_frame == NULL)
1013 	{
1014 	    win_free(wp, NULL);
1015 	    return FAIL;
1016 	}
1017 
1018 	/* make the contents of the new window the same as the current one */
1019 	win_init(wp, curwin, flags);
1020     }
1021 
1022     /*
1023      * Reorganise the tree of frames to insert the new window.
1024      */
1025     if (flags & (WSP_TOP | WSP_BOT))
1026     {
1027 	if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1028 	    || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
1029 	{
1030 	    curfrp = topframe->fr_child;
1031 	    if (flags & WSP_BOT)
1032 		while (curfrp->fr_next != NULL)
1033 		    curfrp = curfrp->fr_next;
1034 	}
1035 	else
1036 	    curfrp = topframe;
1037 	before = (flags & WSP_TOP);
1038     }
1039     else
1040     {
1041 	curfrp = oldwin->w_frame;
1042 	if (flags & WSP_BELOW)
1043 	    before = FALSE;
1044 	else if (flags & WSP_ABOVE)
1045 	    before = TRUE;
1046 	else if (flags & WSP_VERT)
1047 	    before = !p_spr;
1048 	else
1049 	    before = !p_sb;
1050     }
1051     if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1052     {
1053 	/* Need to create a new frame in the tree to make a branch. */
1054 	frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1055 	*frp = *curfrp;
1056 	curfrp->fr_layout = layout;
1057 	frp->fr_parent = curfrp;
1058 	frp->fr_next = NULL;
1059 	frp->fr_prev = NULL;
1060 	curfrp->fr_child = frp;
1061 	curfrp->fr_win = NULL;
1062 	curfrp = frp;
1063 	if (frp->fr_win != NULL)
1064 	    oldwin->w_frame = frp;
1065 	else
1066 	    for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1067 		frp->fr_parent = curfrp;
1068     }
1069 
1070     if (new_wp == NULL)
1071 	frp = wp->w_frame;
1072     else
1073 	frp = new_wp->w_frame;
1074     frp->fr_parent = curfrp->fr_parent;
1075 
1076     /* Insert the new frame at the right place in the frame list. */
1077     if (before)
1078 	frame_insert(curfrp, frp);
1079     else
1080 	frame_append(curfrp, frp);
1081 
1082     /* Set w_fraction now so that the cursor keeps the same relative
1083      * vertical position. */
1084     if (oldwin->w_height > 0)
1085 	set_fraction(oldwin);
1086     wp->w_fraction = oldwin->w_fraction;
1087 
1088     if (flags & WSP_VERT)
1089     {
1090 	wp->w_p_scr = curwin->w_p_scr;
1091 
1092 	if (need_status)
1093 	{
1094 	    win_new_height(oldwin, oldwin->w_height - 1);
1095 	    oldwin->w_status_height = need_status;
1096 	}
1097 	if (flags & (WSP_TOP | WSP_BOT))
1098 	{
1099 	    /* set height and row of new window to full height */
1100 	    wp->w_winrow = tabline_height();
1101 	    win_new_height(wp, curfrp->fr_height - (p_ls > 0)
1102 #ifdef FEAT_MENU
1103 		    - wp->w_winbar_height
1104 #endif
1105 		    );
1106 	    wp->w_status_height = (p_ls > 0);
1107 	}
1108 	else
1109 	{
1110 	    /* height and row of new window is same as current window */
1111 	    wp->w_winrow = oldwin->w_winrow;
1112 	    win_new_height(wp, oldwin->w_height
1113 #ifdef FEAT_MENU
1114 		    + oldwin->w_winbar_height
1115 #endif
1116 		    );
1117 	    wp->w_status_height = oldwin->w_status_height;
1118 	}
1119 	frp->fr_height = curfrp->fr_height;
1120 
1121 	/* "new_size" of the current window goes to the new window, use
1122 	 * one column for the vertical separator */
1123 	win_new_width(wp, new_size);
1124 	if (before)
1125 	    wp->w_vsep_width = 1;
1126 	else
1127 	{
1128 	    wp->w_vsep_width = oldwin->w_vsep_width;
1129 	    oldwin->w_vsep_width = 1;
1130 	}
1131 	if (flags & (WSP_TOP | WSP_BOT))
1132 	{
1133 	    if (flags & WSP_BOT)
1134 		frame_add_vsep(curfrp);
1135 	    /* Set width of neighbor frame */
1136 	    frame_new_width(curfrp, curfrp->fr_width
1137 		     - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1138 								       FALSE);
1139 	}
1140 	else
1141 	    win_new_width(oldwin, oldwin->w_width - (new_size + 1));
1142 	if (before)	/* new window left of current one */
1143 	{
1144 	    wp->w_wincol = oldwin->w_wincol;
1145 	    oldwin->w_wincol += new_size + 1;
1146 	}
1147 	else		/* new window right of current one */
1148 	    wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1149 	frame_fix_width(oldwin);
1150 	frame_fix_width(wp);
1151     }
1152     else
1153     {
1154 	/* width and column of new window is same as current window */
1155 	if (flags & (WSP_TOP | WSP_BOT))
1156 	{
1157 	    wp->w_wincol = 0;
1158 	    win_new_width(wp, Columns);
1159 	    wp->w_vsep_width = 0;
1160 	}
1161 	else
1162 	{
1163 	    wp->w_wincol = oldwin->w_wincol;
1164 	    win_new_width(wp, oldwin->w_width);
1165 	    wp->w_vsep_width = oldwin->w_vsep_width;
1166 	}
1167 	frp->fr_width = curfrp->fr_width;
1168 
1169 	/* "new_size" of the current window goes to the new window, use
1170 	 * one row for the status line */
1171 	win_new_height(wp, new_size);
1172 	if (flags & (WSP_TOP | WSP_BOT))
1173 	{
1174 	    int new_fr_height = curfrp->fr_height - new_size
1175 #ifdef FEAT_MENU
1176 		+ wp->w_winbar_height
1177 #endif
1178 		;
1179 
1180 	    if (!((flags & WSP_BOT) && p_ls == 0))
1181 		new_fr_height -= STATUS_HEIGHT;
1182 	    frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1183 	}
1184 	else
1185 	    win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1186 	if (before)	/* new window above current one */
1187 	{
1188 	    wp->w_winrow = oldwin->w_winrow;
1189 	    wp->w_status_height = STATUS_HEIGHT;
1190 	    oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1191 	}
1192 	else		/* new window below current one */
1193 	{
1194 	    wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1195 	    wp->w_status_height = oldwin->w_status_height;
1196 	    if (!(flags & WSP_BOT))
1197 		oldwin->w_status_height = STATUS_HEIGHT;
1198 	}
1199 	if (flags & WSP_BOT)
1200 	    frame_add_statusline(curfrp);
1201 	frame_fix_height(wp);
1202 	frame_fix_height(oldwin);
1203     }
1204 
1205     if (flags & (WSP_TOP | WSP_BOT))
1206 	(void)win_comp_pos();
1207 
1208     /*
1209      * Both windows need redrawing
1210      */
1211     redraw_win_later(wp, NOT_VALID);
1212     wp->w_redr_status = TRUE;
1213     redraw_win_later(oldwin, NOT_VALID);
1214     oldwin->w_redr_status = TRUE;
1215 
1216     if (need_status)
1217     {
1218 	msg_row = Rows - 1;
1219 	msg_col = sc_col;
1220 	msg_clr_eos_force();	/* Old command/ruler may still be there */
1221 	comp_col();
1222 	msg_row = Rows - 1;
1223 	msg_col = 0;	/* put position back at start of line */
1224     }
1225 
1226     /*
1227      * equalize the window sizes.
1228      */
1229     if (do_equal || dir != 0)
1230 	win_equal(wp, TRUE,
1231 		(flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1232 		: dir == 'h' ? 'b' : 'v');
1233 
1234     /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1235      * size was given. */
1236     if (flags & WSP_VERT)
1237     {
1238 	i = p_wiw;
1239 	if (size != 0)
1240 	    p_wiw = size;
1241 
1242 # ifdef FEAT_GUI
1243 	/* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1244 	if (gui.in_use)
1245 	    gui_init_which_components(NULL);
1246 # endif
1247     }
1248     else
1249     {
1250 	i = p_wh;
1251 	if (size != 0)
1252 	    p_wh = size;
1253     }
1254 
1255 #ifdef FEAT_JUMPLIST
1256     /* Keep same changelist position in new window. */
1257     wp->w_changelistidx = oldwin->w_changelistidx;
1258 #endif
1259 
1260     /*
1261      * make the new window the current window
1262      */
1263     win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
1264     if (flags & WSP_VERT)
1265 	p_wiw = i;
1266     else
1267 	p_wh = i;
1268 
1269     return OK;
1270 }
1271 
1272 
1273 /*
1274  * Initialize window "newp" from window "oldp".
1275  * Used when splitting a window and when creating a new tab page.
1276  * The windows will both edit the same buffer.
1277  * WSP_NEWLOC may be specified in flags to prevent the location list from
1278  * being copied.
1279  */
1280     static void
1281 win_init(win_T *newp, win_T *oldp, int flags UNUSED)
1282 {
1283     int		i;
1284 
1285     newp->w_buffer = oldp->w_buffer;
1286 #ifdef FEAT_SYN_HL
1287     newp->w_s = &(oldp->w_buffer->b_s);
1288 #endif
1289     oldp->w_buffer->b_nwindows++;
1290     newp->w_cursor = oldp->w_cursor;
1291     newp->w_valid = 0;
1292     newp->w_curswant = oldp->w_curswant;
1293     newp->w_set_curswant = oldp->w_set_curswant;
1294     newp->w_topline = oldp->w_topline;
1295 #ifdef FEAT_DIFF
1296     newp->w_topfill = oldp->w_topfill;
1297 #endif
1298     newp->w_leftcol = oldp->w_leftcol;
1299     newp->w_pcmark = oldp->w_pcmark;
1300     newp->w_prev_pcmark = oldp->w_prev_pcmark;
1301     newp->w_alt_fnum = oldp->w_alt_fnum;
1302     newp->w_wrow = oldp->w_wrow;
1303     newp->w_fraction = oldp->w_fraction;
1304     newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1305 #ifdef FEAT_JUMPLIST
1306     copy_jumplist(oldp, newp);
1307 #endif
1308 #ifdef FEAT_QUICKFIX
1309     if (flags & WSP_NEWLOC)
1310     {
1311 	/* Don't copy the location list.  */
1312 	newp->w_llist = NULL;
1313 	newp->w_llist_ref = NULL;
1314     }
1315     else
1316 	copy_loclist(oldp, newp);
1317 #endif
1318     newp->w_localdir = (oldp->w_localdir == NULL)
1319 				    ? NULL : vim_strsave(oldp->w_localdir);
1320 
1321     /* copy tagstack and folds */
1322     for (i = 0; i < oldp->w_tagstacklen; i++)
1323     {
1324 	newp->w_tagstack[i] = oldp->w_tagstack[i];
1325 	if (newp->w_tagstack[i].tagname != NULL)
1326 	    newp->w_tagstack[i].tagname =
1327 				   vim_strsave(newp->w_tagstack[i].tagname);
1328     }
1329     newp->w_tagstackidx = oldp->w_tagstackidx;
1330     newp->w_tagstacklen = oldp->w_tagstacklen;
1331 #ifdef FEAT_FOLDING
1332     copyFoldingState(oldp, newp);
1333 #endif
1334 
1335     win_init_some(newp, oldp);
1336 
1337 #ifdef FEAT_SYN_HL
1338     check_colorcolumn(newp);
1339 #endif
1340 }
1341 
1342 /*
1343  * Initialize window "newp" from window "old".
1344  * Only the essential things are copied.
1345  */
1346     static void
1347 win_init_some(win_T *newp, win_T *oldp)
1348 {
1349     /* Use the same argument list. */
1350     newp->w_alist = oldp->w_alist;
1351     ++newp->w_alist->al_refcount;
1352     newp->w_arg_idx = oldp->w_arg_idx;
1353 
1354     /* copy options from existing window */
1355     win_copy_options(oldp, newp);
1356 }
1357 
1358 
1359 /*
1360  * Check if "win" is a pointer to an existing window in the current tab page.
1361  */
1362     int
1363 win_valid(win_T *win)
1364 {
1365     win_T	*wp;
1366 
1367     if (win == NULL)
1368 	return FALSE;
1369     FOR_ALL_WINDOWS(wp)
1370 	if (wp == win)
1371 	    return TRUE;
1372     return FALSE;
1373 }
1374 
1375 /*
1376  * Check if "win" is a pointer to an existing window in any tab page.
1377  */
1378     int
1379 win_valid_any_tab(win_T *win)
1380 {
1381     win_T	*wp;
1382     tabpage_T	*tp;
1383 
1384     if (win == NULL)
1385 	return FALSE;
1386     FOR_ALL_TABPAGES(tp)
1387     {
1388 	FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1389 	{
1390 	    if (wp == win)
1391 		return TRUE;
1392 	}
1393     }
1394     return FALSE;
1395 }
1396 
1397 /*
1398  * Return the number of windows.
1399  */
1400     int
1401 win_count(void)
1402 {
1403     win_T	*wp;
1404     int		count = 0;
1405 
1406     FOR_ALL_WINDOWS(wp)
1407 	++count;
1408     return count;
1409 }
1410 
1411 /*
1412  * Make "count" windows on the screen.
1413  * Return actual number of windows on the screen.
1414  * Must be called when there is just one window, filling the whole screen
1415  * (excluding the command line).
1416  */
1417     int
1418 make_windows(
1419     int		count,
1420     int		vertical UNUSED)  /* split windows vertically if TRUE */
1421 {
1422     int		maxcount;
1423     int		todo;
1424 
1425     if (vertical)
1426     {
1427 	/* Each windows needs at least 'winminwidth' lines and a separator
1428 	 * column. */
1429 	maxcount = (curwin->w_width + curwin->w_vsep_width
1430 					     - (p_wiw - p_wmw)) / (p_wmw + 1);
1431     }
1432     else
1433     {
1434 	/* Each window needs at least 'winminheight' lines and a status line. */
1435 	maxcount = (curwin->w_height + curwin->w_status_height
1436 				  - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1437     }
1438 
1439     if (maxcount < 2)
1440 	maxcount = 2;
1441     if (count > maxcount)
1442 	count = maxcount;
1443 
1444     /*
1445      * add status line now, otherwise first window will be too big
1446      */
1447     if (count > 1)
1448 	last_status(TRUE);
1449 
1450 #ifdef FEAT_AUTOCMD
1451     /*
1452      * Don't execute autocommands while creating the windows.  Must do that
1453      * when putting the buffers in the windows.
1454      */
1455     block_autocmds();
1456 #endif
1457 
1458     /* todo is number of windows left to create */
1459     for (todo = count - 1; todo > 0; --todo)
1460 	if (vertical)
1461 	{
1462 	    if (win_split(curwin->w_width - (curwin->w_width - todo)
1463 			/ (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1464 		break;
1465 	}
1466 	else
1467 	{
1468 	    if (win_split(curwin->w_height - (curwin->w_height - todo
1469 			    * STATUS_HEIGHT) / (todo + 1)
1470 			- STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1471 		break;
1472 	}
1473 
1474 #ifdef FEAT_AUTOCMD
1475     unblock_autocmds();
1476 #endif
1477 
1478     /* return actual number of windows */
1479     return (count - todo);
1480 }
1481 
1482 /*
1483  * Exchange current and next window
1484  */
1485     static void
1486 win_exchange(long Prenum)
1487 {
1488     frame_T	*frp;
1489     frame_T	*frp2;
1490     win_T	*wp;
1491     win_T	*wp2;
1492     int		temp;
1493 
1494     if (ONE_WINDOW)	    /* just one window */
1495     {
1496 	beep_flush();
1497 	return;
1498     }
1499 
1500 #ifdef FEAT_GUI
1501     need_mouse_correct = TRUE;
1502 #endif
1503 
1504     /*
1505      * find window to exchange with
1506      */
1507     if (Prenum)
1508     {
1509 	frp = curwin->w_frame->fr_parent->fr_child;
1510 	while (frp != NULL && --Prenum > 0)
1511 	    frp = frp->fr_next;
1512     }
1513     else if (curwin->w_frame->fr_next != NULL)	/* Swap with next */
1514 	frp = curwin->w_frame->fr_next;
1515     else    /* Swap last window in row/col with previous */
1516 	frp = curwin->w_frame->fr_prev;
1517 
1518     /* We can only exchange a window with another window, not with a frame
1519      * containing windows. */
1520     if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1521 	return;
1522     wp = frp->fr_win;
1523 
1524 /*
1525  * 1. remove curwin from the list. Remember after which window it was in wp2
1526  * 2. insert curwin before wp in the list
1527  * if wp != wp2
1528  *    3. remove wp from the list
1529  *    4. insert wp after wp2
1530  * 5. exchange the status line height and vsep width.
1531  */
1532     wp2 = curwin->w_prev;
1533     frp2 = curwin->w_frame->fr_prev;
1534     if (wp->w_prev != curwin)
1535     {
1536 	win_remove(curwin, NULL);
1537 	frame_remove(curwin->w_frame);
1538 	win_append(wp->w_prev, curwin);
1539 	frame_insert(frp, curwin->w_frame);
1540     }
1541     if (wp != wp2)
1542     {
1543 	win_remove(wp, NULL);
1544 	frame_remove(wp->w_frame);
1545 	win_append(wp2, wp);
1546 	if (frp2 == NULL)
1547 	    frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1548 	else
1549 	    frame_append(frp2, wp->w_frame);
1550     }
1551     temp = curwin->w_status_height;
1552     curwin->w_status_height = wp->w_status_height;
1553     wp->w_status_height = temp;
1554     temp = curwin->w_vsep_width;
1555     curwin->w_vsep_width = wp->w_vsep_width;
1556     wp->w_vsep_width = temp;
1557 
1558     /* If the windows are not in the same frame, exchange the sizes to avoid
1559      * messing up the window layout.  Otherwise fix the frame sizes. */
1560     if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1561     {
1562 	temp = curwin->w_height;
1563 	curwin->w_height = wp->w_height;
1564 	wp->w_height = temp;
1565 	temp = curwin->w_width;
1566 	curwin->w_width = wp->w_width;
1567 	wp->w_width = temp;
1568     }
1569     else
1570     {
1571 	frame_fix_height(curwin);
1572 	frame_fix_height(wp);
1573 	frame_fix_width(curwin);
1574 	frame_fix_width(wp);
1575     }
1576 
1577     (void)win_comp_pos();		/* recompute window positions */
1578 
1579     win_enter(wp, TRUE);
1580     redraw_later(CLEAR);
1581 }
1582 
1583 /*
1584  * rotate windows: if upwards TRUE the second window becomes the first one
1585  *		   if upwards FALSE the first window becomes the second one
1586  */
1587     static void
1588 win_rotate(int upwards, int count)
1589 {
1590     win_T	*wp1;
1591     win_T	*wp2;
1592     frame_T	*frp;
1593     int		n;
1594 
1595     if (ONE_WINDOW)		/* nothing to do */
1596     {
1597 	beep_flush();
1598 	return;
1599     }
1600 
1601 #ifdef FEAT_GUI
1602     need_mouse_correct = TRUE;
1603 #endif
1604 
1605     /* Check if all frames in this row/col have one window. */
1606     for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1607 							   frp = frp->fr_next)
1608 	if (frp->fr_win == NULL)
1609 	{
1610 	    EMSG(_("E443: Cannot rotate when another window is split"));
1611 	    return;
1612 	}
1613 
1614     while (count--)
1615     {
1616 	if (upwards)		/* first window becomes last window */
1617 	{
1618 	    /* remove first window/frame from the list */
1619 	    frp = curwin->w_frame->fr_parent->fr_child;
1620 	    wp1 = frp->fr_win;
1621 	    win_remove(wp1, NULL);
1622 	    frame_remove(frp);
1623 
1624 	    /* find last frame and append removed window/frame after it */
1625 	    for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1626 		;
1627 	    win_append(frp->fr_win, wp1);
1628 	    frame_append(frp, wp1->w_frame);
1629 
1630 	    wp2 = frp->fr_win;		/* previously last window */
1631 	}
1632 	else			/* last window becomes first window */
1633 	{
1634 	    /* find last window/frame in the list and remove it */
1635 	    for (frp = curwin->w_frame; frp->fr_next != NULL;
1636 							   frp = frp->fr_next)
1637 		;
1638 	    wp1 = frp->fr_win;
1639 	    wp2 = wp1->w_prev;		    /* will become last window */
1640 	    win_remove(wp1, NULL);
1641 	    frame_remove(frp);
1642 
1643 	    /* append the removed window/frame before the first in the list */
1644 	    win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1645 	    frame_insert(frp->fr_parent->fr_child, frp);
1646 	}
1647 
1648 	/* exchange status height and vsep width of old and new last window */
1649 	n = wp2->w_status_height;
1650 	wp2->w_status_height = wp1->w_status_height;
1651 	wp1->w_status_height = n;
1652 	frame_fix_height(wp1);
1653 	frame_fix_height(wp2);
1654 	n = wp2->w_vsep_width;
1655 	wp2->w_vsep_width = wp1->w_vsep_width;
1656 	wp1->w_vsep_width = n;
1657 	frame_fix_width(wp1);
1658 	frame_fix_width(wp2);
1659 
1660 	/* recompute w_winrow and w_wincol for all windows */
1661 	(void)win_comp_pos();
1662     }
1663 
1664     redraw_later(CLEAR);
1665 }
1666 
1667 /*
1668  * Move the current window to the very top/bottom/left/right of the screen.
1669  */
1670     static void
1671 win_totop(int size, int flags)
1672 {
1673     int		dir;
1674     int		height = curwin->w_height;
1675 
1676     if (ONE_WINDOW)
1677     {
1678 	beep_flush();
1679 	return;
1680     }
1681 
1682     /* Remove the window and frame from the tree of frames. */
1683     (void)winframe_remove(curwin, &dir, NULL);
1684     win_remove(curwin, NULL);
1685     last_status(FALSE);	    /* may need to remove last status line */
1686     (void)win_comp_pos();   /* recompute window positions */
1687 
1688     /* Split a window on the desired side and put the window there. */
1689     (void)win_split_ins(size, flags, curwin, dir);
1690     if (!(flags & WSP_VERT))
1691     {
1692 	win_setheight(height);
1693 	if (p_ea)
1694 	    win_equal(curwin, TRUE, 'v');
1695     }
1696 
1697 #if defined(FEAT_GUI)
1698     /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1699      * scrollbars.  Have to update them anyway. */
1700     gui_may_update_scrollbars();
1701 #endif
1702 }
1703 
1704 /*
1705  * Move window "win1" to below/right of "win2" and make "win1" the current
1706  * window.  Only works within the same frame!
1707  */
1708     void
1709 win_move_after(win_T *win1, win_T *win2)
1710 {
1711     int		height;
1712 
1713     /* check if the arguments are reasonable */
1714     if (win1 == win2)
1715 	return;
1716 
1717     /* check if there is something to do */
1718     if (win2->w_next != win1)
1719     {
1720 	/* may need move the status line/vertical separator of the last window
1721 	 * */
1722 	if (win1 == lastwin)
1723 	{
1724 	    height = win1->w_prev->w_status_height;
1725 	    win1->w_prev->w_status_height = win1->w_status_height;
1726 	    win1->w_status_height = height;
1727 	    if (win1->w_prev->w_vsep_width == 1)
1728 	    {
1729 		/* Remove the vertical separator from the last-but-one window,
1730 		 * add it to the last window.  Adjust the frame widths. */
1731 		win1->w_prev->w_vsep_width = 0;
1732 		win1->w_prev->w_frame->fr_width -= 1;
1733 		win1->w_vsep_width = 1;
1734 		win1->w_frame->fr_width += 1;
1735 	    }
1736 	}
1737 	else if (win2 == lastwin)
1738 	{
1739 	    height = win1->w_status_height;
1740 	    win1->w_status_height = win2->w_status_height;
1741 	    win2->w_status_height = height;
1742 	    if (win1->w_vsep_width == 1)
1743 	    {
1744 		/* Remove the vertical separator from win1, add it to the last
1745 		 * window, win2.  Adjust the frame widths. */
1746 		win2->w_vsep_width = 1;
1747 		win2->w_frame->fr_width += 1;
1748 		win1->w_vsep_width = 0;
1749 		win1->w_frame->fr_width -= 1;
1750 	    }
1751 	}
1752 	win_remove(win1, NULL);
1753 	frame_remove(win1->w_frame);
1754 	win_append(win2, win1);
1755 	frame_append(win2->w_frame, win1->w_frame);
1756 
1757 	(void)win_comp_pos();	/* recompute w_winrow for all windows */
1758 	redraw_later(NOT_VALID);
1759     }
1760     win_enter(win1, FALSE);
1761 }
1762 
1763 /*
1764  * Make all windows the same height.
1765  * 'next_curwin' will soon be the current window, make sure it has enough
1766  * rows.
1767  */
1768     void
1769 win_equal(
1770     win_T	*next_curwin,	/* pointer to current window to be or NULL */
1771     int		current,	/* do only frame with current window */
1772     int		dir)		/* 'v' for vertically, 'h' for horizontally,
1773 				   'b' for both, 0 for using p_ead */
1774 {
1775     if (dir == 0)
1776 	dir = *p_ead;
1777     win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1778 		      topframe, dir, 0, tabline_height(),
1779 					   (int)Columns, topframe->fr_height);
1780 }
1781 
1782 /*
1783  * Set a frame to a new position and height, spreading the available room
1784  * equally over contained frames.
1785  * The window "next_curwin" (if not NULL) should at least get the size from
1786  * 'winheight' and 'winwidth' if possible.
1787  */
1788     static void
1789 win_equal_rec(
1790     win_T	*next_curwin,	/* pointer to current window to be or NULL */
1791     int		current,	/* do only frame with current window */
1792     frame_T	*topfr,		/* frame to set size off */
1793     int		dir,		/* 'v', 'h' or 'b', see win_equal() */
1794     int		col,		/* horizontal position for frame */
1795     int		row,		/* vertical position for frame */
1796     int		width,		/* new width of frame */
1797     int		height)		/* new height of frame */
1798 {
1799     int		n, m;
1800     int		extra_sep = 0;
1801     int		wincount, totwincount = 0;
1802     frame_T	*fr;
1803     int		next_curwin_size = 0;
1804     int		room = 0;
1805     int		new_size;
1806     int		has_next_curwin = 0;
1807     int		hnc;
1808 
1809     if (topfr->fr_layout == FR_LEAF)
1810     {
1811 	/* Set the width/height of this frame.
1812 	 * Redraw when size or position changes */
1813 	if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1814 		|| topfr->fr_width != width || topfr->fr_win->w_wincol != col
1815 	   )
1816 	{
1817 	    topfr->fr_win->w_winrow = row;
1818 	    frame_new_height(topfr, height, FALSE, FALSE);
1819 	    topfr->fr_win->w_wincol = col;
1820 	    frame_new_width(topfr, width, FALSE, FALSE);
1821 	    redraw_all_later(CLEAR);
1822 	}
1823     }
1824     else if (topfr->fr_layout == FR_ROW)
1825     {
1826 	topfr->fr_width = width;
1827 	topfr->fr_height = height;
1828 
1829 	if (dir != 'v')			/* equalize frame widths */
1830 	{
1831 	    /* Compute the maximum number of windows horizontally in this
1832 	     * frame. */
1833 	    n = frame_minwidth(topfr, NOWIN);
1834 	    /* add one for the rightmost window, it doesn't have a separator */
1835 	    if (col + width == Columns)
1836 		extra_sep = 1;
1837 	    else
1838 		extra_sep = 0;
1839 	    totwincount = (n + extra_sep) / (p_wmw + 1);
1840 	    has_next_curwin = frame_has_win(topfr, next_curwin);
1841 
1842 	    /*
1843 	     * Compute width for "next_curwin" window and room available for
1844 	     * other windows.
1845 	     * "m" is the minimal width when counting p_wiw for "next_curwin".
1846 	     */
1847 	    m = frame_minwidth(topfr, next_curwin);
1848 	    room = width - m;
1849 	    if (room < 0)
1850 	    {
1851 		next_curwin_size = p_wiw + room;
1852 		room = 0;
1853 	    }
1854 	    else
1855 	    {
1856 		next_curwin_size = -1;
1857 		for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1858 		{
1859 		    /* If 'winfixwidth' set keep the window width if
1860 		     * possible.
1861 		     * Watch out for this window being the next_curwin. */
1862 		    if (frame_fixed_width(fr))
1863 		    {
1864 			n = frame_minwidth(fr, NOWIN);
1865 			new_size = fr->fr_width;
1866 			if (frame_has_win(fr, next_curwin))
1867 			{
1868 			    room += p_wiw - p_wmw;
1869 			    next_curwin_size = 0;
1870 			    if (new_size < p_wiw)
1871 				new_size = p_wiw;
1872 			}
1873 			else
1874 			    /* These windows don't use up room. */
1875 			    totwincount -= (n + (fr->fr_next == NULL
1876 					      ? extra_sep : 0)) / (p_wmw + 1);
1877 			room -= new_size - n;
1878 			if (room < 0)
1879 			{
1880 			    new_size += room;
1881 			    room = 0;
1882 			}
1883 			fr->fr_newwidth = new_size;
1884 		    }
1885 		}
1886 		if (next_curwin_size == -1)
1887 		{
1888 		    if (!has_next_curwin)
1889 			next_curwin_size = 0;
1890 		    else if (totwincount > 1
1891 			    && (room + (totwincount - 2))
1892 						  / (totwincount - 1) > p_wiw)
1893 		    {
1894 			/* Can make all windows wider than 'winwidth', spread
1895 			 * the room equally. */
1896 			next_curwin_size = (room + p_wiw
1897 					    + (totwincount - 1) * p_wmw
1898 					    + (totwincount - 1)) / totwincount;
1899 			room -= next_curwin_size - p_wiw;
1900 		    }
1901 		    else
1902 			next_curwin_size = p_wiw;
1903 		}
1904 	    }
1905 
1906 	    if (has_next_curwin)
1907 		--totwincount;		/* don't count curwin */
1908 	}
1909 
1910 	for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1911 	{
1912 	    n = m = 0;
1913 	    wincount = 1;
1914 	    if (fr->fr_next == NULL)
1915 		/* last frame gets all that remains (avoid roundoff error) */
1916 		new_size = width;
1917 	    else if (dir == 'v')
1918 		new_size = fr->fr_width;
1919 	    else if (frame_fixed_width(fr))
1920 	    {
1921 		new_size = fr->fr_newwidth;
1922 		wincount = 0;	    /* doesn't count as a sizeable window */
1923 	    }
1924 	    else
1925 	    {
1926 		/* Compute the maximum number of windows horiz. in "fr". */
1927 		n = frame_minwidth(fr, NOWIN);
1928 		wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1929 								/ (p_wmw + 1);
1930 		m = frame_minwidth(fr, next_curwin);
1931 		if (has_next_curwin)
1932 		    hnc = frame_has_win(fr, next_curwin);
1933 		else
1934 		    hnc = FALSE;
1935 		if (hnc)	    /* don't count next_curwin */
1936 		    --wincount;
1937 		if (totwincount == 0)
1938 		    new_size = room;
1939 		else
1940 		    new_size = (wincount * room + ((unsigned)totwincount >> 1))
1941 								/ totwincount;
1942 		if (hnc)	    /* add next_curwin size */
1943 		{
1944 		    next_curwin_size -= p_wiw - (m - n);
1945 		    new_size += next_curwin_size;
1946 		    room -= new_size - next_curwin_size;
1947 		}
1948 		else
1949 		    room -= new_size;
1950 		new_size += n;
1951 	    }
1952 
1953 	    /* Skip frame that is full width when splitting or closing a
1954 	     * window, unless equalizing all frames. */
1955 	    if (!current || dir != 'v' || topfr->fr_parent != NULL
1956 		    || (new_size != fr->fr_width)
1957 		    || frame_has_win(fr, next_curwin))
1958 		win_equal_rec(next_curwin, current, fr, dir, col, row,
1959 							    new_size, height);
1960 	    col += new_size;
1961 	    width -= new_size;
1962 	    totwincount -= wincount;
1963 	}
1964     }
1965     else /* topfr->fr_layout == FR_COL */
1966     {
1967 	topfr->fr_width = width;
1968 	topfr->fr_height = height;
1969 
1970 	if (dir != 'h')			/* equalize frame heights */
1971 	{
1972 	    /* Compute maximum number of windows vertically in this frame. */
1973 	    n = frame_minheight(topfr, NOWIN);
1974 	    /* add one for the bottom window if it doesn't have a statusline */
1975 	    if (row + height == cmdline_row && p_ls == 0)
1976 		extra_sep = 1;
1977 	    else
1978 		extra_sep = 0;
1979 	    totwincount = (n + extra_sep) / (p_wmh + 1);
1980 	    has_next_curwin = frame_has_win(topfr, next_curwin);
1981 
1982 	    /*
1983 	     * Compute height for "next_curwin" window and room available for
1984 	     * other windows.
1985 	     * "m" is the minimal height when counting p_wh for "next_curwin".
1986 	     */
1987 	    m = frame_minheight(topfr, next_curwin);
1988 	    room = height - m;
1989 	    if (room < 0)
1990 	    {
1991 		/* The room is less then 'winheight', use all space for the
1992 		 * current window. */
1993 		next_curwin_size = p_wh + room;
1994 		room = 0;
1995 	    }
1996 	    else
1997 	    {
1998 		next_curwin_size = -1;
1999 		for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2000 		{
2001 		    /* If 'winfixheight' set keep the window height if
2002 		     * possible.
2003 		     * Watch out for this window being the next_curwin. */
2004 		    if (frame_fixed_height(fr))
2005 		    {
2006 			n = frame_minheight(fr, NOWIN);
2007 			new_size = fr->fr_height;
2008 			if (frame_has_win(fr, next_curwin))
2009 			{
2010 			    room += p_wh - p_wmh;
2011 			    next_curwin_size = 0;
2012 			    if (new_size < p_wh)
2013 				new_size = p_wh;
2014 			}
2015 			else
2016 			    /* These windows don't use up room. */
2017 			    totwincount -= (n + (fr->fr_next == NULL
2018 					      ? extra_sep : 0)) / (p_wmh + 1);
2019 			room -= new_size - n;
2020 			if (room < 0)
2021 			{
2022 			    new_size += room;
2023 			    room = 0;
2024 			}
2025 			fr->fr_newheight = new_size;
2026 		    }
2027 		}
2028 		if (next_curwin_size == -1)
2029 		{
2030 		    if (!has_next_curwin)
2031 			next_curwin_size = 0;
2032 		    else if (totwincount > 1
2033 			    && (room + (totwincount - 2))
2034 						   / (totwincount - 1) > p_wh)
2035 		    {
2036 			/* can make all windows higher than 'winheight',
2037 			 * spread the room equally. */
2038 			next_curwin_size = (room + p_wh
2039 					   + (totwincount - 1) * p_wmh
2040 					   + (totwincount - 1)) / totwincount;
2041 			room -= next_curwin_size - p_wh;
2042 		    }
2043 		    else
2044 			next_curwin_size = p_wh;
2045 		}
2046 	    }
2047 
2048 	    if (has_next_curwin)
2049 		--totwincount;		/* don't count curwin */
2050 	}
2051 
2052 	for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2053 	{
2054 	    n = m = 0;
2055 	    wincount = 1;
2056 	    if (fr->fr_next == NULL)
2057 		/* last frame gets all that remains (avoid roundoff error) */
2058 		new_size = height;
2059 	    else if (dir == 'h')
2060 		new_size = fr->fr_height;
2061 	    else if (frame_fixed_height(fr))
2062 	    {
2063 		new_size = fr->fr_newheight;
2064 		wincount = 0;	    /* doesn't count as a sizeable window */
2065 	    }
2066 	    else
2067 	    {
2068 		/* Compute the maximum number of windows vert. in "fr". */
2069 		n = frame_minheight(fr, NOWIN);
2070 		wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2071 								/ (p_wmh + 1);
2072 		m = frame_minheight(fr, next_curwin);
2073 		if (has_next_curwin)
2074 		    hnc = frame_has_win(fr, next_curwin);
2075 		else
2076 		    hnc = FALSE;
2077 		if (hnc)	    /* don't count next_curwin */
2078 		    --wincount;
2079 		if (totwincount == 0)
2080 		    new_size = room;
2081 		else
2082 		    new_size = (wincount * room + ((unsigned)totwincount >> 1))
2083 								/ totwincount;
2084 		if (hnc)	    /* add next_curwin size */
2085 		{
2086 		    next_curwin_size -= p_wh - (m - n);
2087 		    new_size += next_curwin_size;
2088 		    room -= new_size - next_curwin_size;
2089 		}
2090 		else
2091 		    room -= new_size;
2092 		new_size += n;
2093 	    }
2094 	    /* Skip frame that is full width when splitting or closing a
2095 	     * window, unless equalizing all frames. */
2096 	    if (!current || dir != 'h' || topfr->fr_parent != NULL
2097 		    || (new_size != fr->fr_height)
2098 		    || frame_has_win(fr, next_curwin))
2099 		win_equal_rec(next_curwin, current, fr, dir, col, row,
2100 							     width, new_size);
2101 	    row += new_size;
2102 	    height -= new_size;
2103 	    totwincount -= wincount;
2104 	}
2105     }
2106 }
2107 
2108 /*
2109  * Close all windows for buffer "buf".
2110  */
2111     void
2112 close_windows(
2113     buf_T	*buf,
2114     int		keep_curwin)	    /* don't close "curwin" */
2115 {
2116     win_T	*wp;
2117     tabpage_T   *tp, *nexttp;
2118     int		h = tabline_height();
2119 #ifdef FEAT_AUTOCMD
2120     int		count = tabpage_index(NULL);
2121 #endif
2122 
2123     ++RedrawingDisabled;
2124 
2125     for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
2126     {
2127 	if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2128 #ifdef FEAT_AUTOCMD
2129 		&& !(wp->w_closing || wp->w_buffer->b_locked > 0)
2130 #endif
2131 		)
2132 	{
2133 	    if (win_close(wp, FALSE) == FAIL)
2134 		/* If closing the window fails give up, to avoid looping
2135 		 * forever. */
2136 		break;
2137 
2138 	    /* Start all over, autocommands may change the window layout. */
2139 	    wp = firstwin;
2140 	}
2141 	else
2142 	    wp = wp->w_next;
2143     }
2144 
2145     /* Also check windows in other tab pages. */
2146     for (tp = first_tabpage; tp != NULL; tp = nexttp)
2147     {
2148 	nexttp = tp->tp_next;
2149 	if (tp != curtab)
2150 	    for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
2151 		if (wp->w_buffer == buf
2152 #ifdef FEAT_AUTOCMD
2153 		    && !(wp->w_closing || wp->w_buffer->b_locked > 0)
2154 #endif
2155 		    )
2156 		{
2157 		    win_close_othertab(wp, FALSE, tp);
2158 
2159 		    /* Start all over, the tab page may be closed and
2160 		     * autocommands may change the window layout. */
2161 		    nexttp = first_tabpage;
2162 		    break;
2163 		}
2164     }
2165 
2166     --RedrawingDisabled;
2167 
2168 #ifdef FEAT_AUTOCMD
2169     if (count != tabpage_index(NULL))
2170 	apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
2171 #endif
2172 
2173     redraw_tabline = TRUE;
2174     if (h != tabline_height())
2175 	shell_new_rows();
2176 }
2177 
2178 /*
2179  * Return TRUE if the current window is the only window that exists (ignoring
2180  * "aucmd_win").
2181  * Returns FALSE if there is a window, possibly in another tab page.
2182  */
2183     static int
2184 last_window(void)
2185 {
2186     return (one_window() && first_tabpage->tp_next == NULL);
2187 }
2188 
2189 /*
2190  * Return TRUE if there is only one window other than "aucmd_win" in the
2191  * current tab page.
2192  */
2193     int
2194 one_window(void)
2195 {
2196 #ifdef FEAT_AUTOCMD
2197     win_T	*wp;
2198     int		seen_one = FALSE;
2199 
2200     FOR_ALL_WINDOWS(wp)
2201     {
2202 	if (wp != aucmd_win)
2203 	{
2204 	    if (seen_one)
2205 		return FALSE;
2206 	    seen_one = TRUE;
2207 	}
2208     }
2209     return TRUE;
2210 #else
2211     return ONE_WINDOW;
2212 #endif
2213 }
2214 
2215 /*
2216  * Close the possibly last window in a tab page.
2217  * Returns TRUE when the window was closed already.
2218  */
2219     static int
2220 close_last_window_tabpage(
2221     win_T	*win,
2222     int		free_buf,
2223     tabpage_T   *prev_curtab)
2224 {
2225     if (ONE_WINDOW)
2226     {
2227 #ifdef FEAT_AUTOCMD
2228 	buf_T	*old_curbuf = curbuf;
2229 #endif
2230 
2231 	/*
2232 	 * Closing the last window in a tab page.  First go to another tab
2233 	 * page and then close the window and the tab page.  This avoids that
2234 	 * curwin and curtab are invalid while we are freeing memory, they may
2235 	 * be used in GUI events.
2236 	 * Don't trigger autocommands yet, they may use wrong values, so do
2237 	 * that below.
2238 	 */
2239 	goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
2240 	redraw_tabline = TRUE;
2241 
2242 	/* Safety check: Autocommands may have closed the window when jumping
2243 	 * to the other tab page. */
2244 	if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2245 	{
2246 	    int	    h = tabline_height();
2247 
2248 	    win_close_othertab(win, free_buf, prev_curtab);
2249 	    if (h != tabline_height())
2250 		shell_new_rows();
2251 	}
2252 	/* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2253 	 * that now. */
2254 #ifdef FEAT_AUTOCMD
2255 	apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
2256 	apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
2257 	apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2258 	if (old_curbuf != curbuf)
2259 	    apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2260 #endif
2261 	return TRUE;
2262     }
2263     return FALSE;
2264 }
2265 
2266 /*
2267  * Close window "win".  Only works for the current tab page.
2268  * If "free_buf" is TRUE related buffer may be unloaded.
2269  *
2270  * Called by :quit, :close, :xit, :wq and findtag().
2271  * Returns FAIL when the window was not closed.
2272  */
2273     int
2274 win_close(win_T *win, int free_buf)
2275 {
2276     win_T	*wp;
2277 #ifdef FEAT_AUTOCMD
2278     int		other_buffer = FALSE;
2279 #endif
2280     int		close_curwin = FALSE;
2281     int		dir;
2282     int		help_window = FALSE;
2283     tabpage_T   *prev_curtab = curtab;
2284     frame_T	*win_frame = win->w_frame->fr_parent;
2285 
2286     if (last_window())
2287     {
2288 	EMSG(_("E444: Cannot close last window"));
2289 	return FAIL;
2290     }
2291 
2292 #ifdef FEAT_AUTOCMD
2293     if (win->w_closing || (win->w_buffer != NULL
2294 					       && win->w_buffer->b_locked > 0))
2295 	return FAIL; /* window is already being closed */
2296     if (win == aucmd_win)
2297     {
2298 	EMSG(_("E813: Cannot close autocmd window"));
2299 	return FAIL;
2300     }
2301     if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2302     {
2303 	EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2304 	return FAIL;
2305     }
2306 #endif
2307 
2308     /* When closing the last window in a tab page first go to another tab page
2309      * and then close the window and the tab page to avoid that curwin and
2310      * curtab are invalid while we are freeing memory. */
2311     if (close_last_window_tabpage(win, free_buf, prev_curtab))
2312       return FAIL;
2313 
2314     /* When closing the help window, try restoring a snapshot after closing
2315      * the window.  Otherwise clear the snapshot, it's now invalid. */
2316     if (bt_help(win->w_buffer))
2317 	help_window = TRUE;
2318     else
2319 	clear_snapshot(curtab, SNAP_HELP_IDX);
2320 
2321 #ifdef FEAT_AUTOCMD
2322     if (win == curwin)
2323     {
2324 	/*
2325 	 * Guess which window is going to be the new current window.
2326 	 * This may change because of the autocommands (sigh).
2327 	 */
2328 	wp = frame2win(win_altframe(win, NULL));
2329 
2330 	/*
2331 	 * Be careful: If autocommands delete the window or cause this window
2332 	 * to be the last one left, return now.
2333 	 */
2334 	if (wp->w_buffer != curbuf)
2335 	{
2336 	    other_buffer = TRUE;
2337 	    win->w_closing = TRUE;
2338 	    apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
2339 	    if (!win_valid(win))
2340 		return FAIL;
2341 	    win->w_closing = FALSE;
2342 	    if (last_window())
2343 		return FAIL;
2344 	}
2345 	win->w_closing = TRUE;
2346 	apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
2347 	if (!win_valid(win))
2348 	    return FAIL;
2349 	win->w_closing = FALSE;
2350 	if (last_window())
2351 	    return FAIL;
2352 # ifdef FEAT_EVAL
2353 	/* autocmds may abort script processing */
2354 	if (aborting())
2355 	    return FAIL;
2356 # endif
2357     }
2358 #endif
2359 
2360 #ifdef FEAT_GUI
2361     /* Avoid trouble with scrollbars that are going to be deleted in
2362      * win_free(). */
2363     if (gui.in_use)
2364 	out_flush();
2365 #endif
2366 
2367 #ifdef FEAT_SYN_HL
2368     /* Free independent synblock before the buffer is freed. */
2369     if (win->w_buffer != NULL)
2370 	reset_synblock(win);
2371 #endif
2372 
2373     /*
2374      * Close the link to the buffer.
2375      */
2376     if (win->w_buffer != NULL)
2377     {
2378 	bufref_T    bufref;
2379 
2380 	set_bufref(&bufref, curbuf);
2381 #ifdef FEAT_AUTOCMD
2382 	win->w_closing = TRUE;
2383 #endif
2384 	close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
2385 #ifdef FEAT_AUTOCMD
2386 	if (win_valid_any_tab(win))
2387 	    win->w_closing = FALSE;
2388 #endif
2389 	/* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2390 	 * "wipe". */
2391 	if (!bufref_valid(&bufref))
2392 	    curbuf = firstbuf;
2393     }
2394 
2395     if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2396 	    && (last_window() || curtab != prev_curtab
2397 		|| close_last_window_tabpage(win, free_buf, prev_curtab)))
2398     {
2399 	/* Autocommands have closed all windows, quit now.  Restore
2400 	 * curwin->w_buffer, otherwise writing viminfo may fail. */
2401 	if (curwin->w_buffer == NULL)
2402 	    curwin->w_buffer = curbuf;
2403 	getout(0);
2404     }
2405 
2406     /* Autocommands may have moved to another tab page. */
2407     if (curtab != prev_curtab && win_valid_any_tab(win)
2408 						      && win->w_buffer == NULL)
2409     {
2410 	/* Need to close the window anyway, since the buffer is NULL. */
2411 	win_close_othertab(win, FALSE, prev_curtab);
2412 	return FAIL;
2413     }
2414 
2415     /* Autocommands may have closed the window already or closed the only
2416      * other window. */
2417     if (!win_valid(win) || last_window()
2418 	    || close_last_window_tabpage(win, free_buf, prev_curtab))
2419 	return FAIL;
2420 
2421     /* Free the memory used for the window and get the window that received
2422      * the screen space. */
2423     wp = win_free_mem(win, &dir, NULL);
2424 
2425     /* Make sure curwin isn't invalid.  It can cause severe trouble when
2426      * printing an error message.  For win_equal() curbuf needs to be valid
2427      * too. */
2428     if (win == curwin)
2429     {
2430 	curwin = wp;
2431 #ifdef FEAT_QUICKFIX
2432 	if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2433 	{
2434 	    /*
2435 	     * If the cursor goes to the preview or the quickfix window, try
2436 	     * finding another window to go to.
2437 	     */
2438 	    for (;;)
2439 	    {
2440 		if (wp->w_next == NULL)
2441 		    wp = firstwin;
2442 		else
2443 		    wp = wp->w_next;
2444 		if (wp == curwin)
2445 		    break;
2446 		if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2447 		{
2448 		    curwin = wp;
2449 		    break;
2450 		}
2451 	    }
2452 	}
2453 #endif
2454 	curbuf = curwin->w_buffer;
2455 	close_curwin = TRUE;
2456 
2457 	/* The cursor position may be invalid if the buffer changed after last
2458 	 * using the window. */
2459 	check_cursor();
2460     }
2461     if (p_ea && (*p_ead == 'b' || *p_ead == dir))
2462 	/* If the frame of the closed window contains the new current window,
2463 	 * only resize that frame.  Otherwise resize all windows. */
2464 	win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
2465     else
2466 	win_comp_pos();
2467     if (close_curwin)
2468     {
2469 	win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
2470 #ifdef FEAT_AUTOCMD
2471 	if (other_buffer)
2472 	    /* careful: after this wp and win may be invalid! */
2473 	    apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2474 #endif
2475     }
2476 
2477     /*
2478      * If last window has a status line now and we don't want one,
2479      * remove the status line.
2480      */
2481     last_status(FALSE);
2482 
2483     /* After closing the help window, try restoring the window layout from
2484      * before it was opened. */
2485     if (help_window)
2486 	restore_snapshot(SNAP_HELP_IDX, close_curwin);
2487 
2488 #if defined(FEAT_GUI)
2489     /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2490     if (gui.in_use && !win_hasvertsplit())
2491 	gui_init_which_components(NULL);
2492 #endif
2493 
2494     redraw_all_later(NOT_VALID);
2495     return OK;
2496 }
2497 
2498 /*
2499  * Close window "win" in tab page "tp", which is not the current tab page.
2500  * This may be the last window in that tab page and result in closing the tab,
2501  * thus "tp" may become invalid!
2502  * Caller must check if buffer is hidden and whether the tabline needs to be
2503  * updated.
2504  */
2505     void
2506 win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
2507 {
2508     win_T	*wp;
2509     int		dir;
2510     tabpage_T   *ptp = NULL;
2511     int		free_tp = FALSE;
2512 
2513 #ifdef FEAT_AUTOCMD
2514     /* Get here with win->w_buffer == NULL when win_close() detects the tab
2515      * page changed. */
2516     if (win->w_closing || (win->w_buffer != NULL
2517 					       && win->w_buffer->b_locked > 0))
2518 	return; /* window is already being closed */
2519 #endif
2520 
2521     if (win->w_buffer != NULL)
2522 	/* Close the link to the buffer. */
2523 	close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
2524 
2525     /* Careful: Autocommands may have closed the tab page or made it the
2526      * current tab page.  */
2527     for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2528 	;
2529     if (ptp == NULL || tp == curtab)
2530 	return;
2531 
2532     /* Autocommands may have closed the window already. */
2533     for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2534 	;
2535     if (wp == NULL)
2536 	return;
2537 
2538     /* When closing the last window in a tab page remove the tab page. */
2539     if (tp->tp_firstwin == tp->tp_lastwin)
2540     {
2541 	if (tp == first_tabpage)
2542 	    first_tabpage = tp->tp_next;
2543 	else
2544 	{
2545 	    for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2546 							   ptp = ptp->tp_next)
2547 		;
2548 	    if (ptp == NULL)
2549 	    {
2550 		internal_error("win_close_othertab()");
2551 		return;
2552 	    }
2553 	    ptp->tp_next = tp->tp_next;
2554 	}
2555 	free_tp = TRUE;
2556     }
2557 
2558     /* Free the memory used for the window. */
2559     win_free_mem(win, &dir, tp);
2560 
2561     if (free_tp)
2562 	free_tabpage(tp);
2563 }
2564 
2565 /*
2566  * Free the memory used for a window.
2567  * Returns a pointer to the window that got the freed up space.
2568  */
2569     static win_T *
2570 win_free_mem(
2571     win_T	*win,
2572     int		*dirp,		/* set to 'v' or 'h' for direction if 'ea' */
2573     tabpage_T	*tp)		/* tab page "win" is in, NULL for current */
2574 {
2575     frame_T	*frp;
2576     win_T	*wp;
2577 
2578     /* Remove the window and its frame from the tree of frames. */
2579     frp = win->w_frame;
2580     wp = winframe_remove(win, dirp, tp);
2581     vim_free(frp);
2582     win_free(win, tp);
2583 
2584     /* When deleting the current window of another tab page select a new
2585      * current window. */
2586     if (tp != NULL && win == tp->tp_curwin)
2587 	tp->tp_curwin = wp;
2588 
2589     return wp;
2590 }
2591 
2592 #if defined(EXITFREE) || defined(PROTO)
2593     void
2594 win_free_all(void)
2595 {
2596     int		dummy;
2597 
2598     while (first_tabpage->tp_next != NULL)
2599 	tabpage_close(TRUE);
2600 
2601 # ifdef FEAT_AUTOCMD
2602     if (aucmd_win != NULL)
2603     {
2604 	(void)win_free_mem(aucmd_win, &dummy, NULL);
2605 	aucmd_win = NULL;
2606     }
2607 # endif
2608 
2609     while (firstwin != NULL)
2610 	(void)win_free_mem(firstwin, &dummy, NULL);
2611 
2612     /* No window should be used after this. Set curwin to NULL to crash
2613      * instead of using freed memory. */
2614     curwin = NULL;
2615 }
2616 #endif
2617 
2618 /*
2619  * Remove a window and its frame from the tree of frames.
2620  * Returns a pointer to the window that got the freed up space.
2621  */
2622     win_T *
2623 winframe_remove(
2624     win_T	*win,
2625     int		*dirp UNUSED,	/* set to 'v' or 'h' for direction if 'ea' */
2626     tabpage_T	*tp)		/* tab page "win" is in, NULL for current */
2627 {
2628     frame_T	*frp, *frp2, *frp3;
2629     frame_T	*frp_close = win->w_frame;
2630     win_T	*wp;
2631 
2632     /*
2633      * If there is only one window there is nothing to remove.
2634      */
2635     if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
2636 	return NULL;
2637 
2638     /*
2639      * Remove the window from its frame.
2640      */
2641     frp2 = win_altframe(win, tp);
2642     wp = frame2win(frp2);
2643 
2644     /* Remove this frame from the list of frames. */
2645     frame_remove(frp_close);
2646 
2647     if (frp_close->fr_parent->fr_layout == FR_COL)
2648     {
2649 	/* When 'winfixheight' is set, try to find another frame in the column
2650 	 * (as close to the closed frame as possible) to distribute the height
2651 	 * to. */
2652 	if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2653 	{
2654 	    frp = frp_close->fr_prev;
2655 	    frp3 = frp_close->fr_next;
2656 	    while (frp != NULL || frp3 != NULL)
2657 	    {
2658 		if (frp != NULL)
2659 		{
2660 		    if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2661 		    {
2662 			frp2 = frp;
2663 			wp = frp->fr_win;
2664 			break;
2665 		    }
2666 		    frp = frp->fr_prev;
2667 		}
2668 		if (frp3 != NULL)
2669 		{
2670 		    if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2671 		    {
2672 			frp2 = frp3;
2673 			wp = frp3->fr_win;
2674 			break;
2675 		    }
2676 		    frp3 = frp3->fr_next;
2677 		}
2678 	    }
2679 	}
2680 	frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2681 			    frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2682 	*dirp = 'v';
2683     }
2684     else
2685     {
2686 	/* When 'winfixwidth' is set, try to find another frame in the column
2687 	 * (as close to the closed frame as possible) to distribute the width
2688 	 * to. */
2689 	if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2690 	{
2691 	    frp = frp_close->fr_prev;
2692 	    frp3 = frp_close->fr_next;
2693 	    while (frp != NULL || frp3 != NULL)
2694 	    {
2695 		if (frp != NULL)
2696 		{
2697 		    if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2698 		    {
2699 			frp2 = frp;
2700 			wp = frp->fr_win;
2701 			break;
2702 		    }
2703 		    frp = frp->fr_prev;
2704 		}
2705 		if (frp3 != NULL)
2706 		{
2707 		    if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2708 		    {
2709 			frp2 = frp3;
2710 			wp = frp3->fr_win;
2711 			break;
2712 		    }
2713 		    frp3 = frp3->fr_next;
2714 		}
2715 	    }
2716 	}
2717 	frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2718 			    frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2719 	*dirp = 'h';
2720     }
2721 
2722     /* If rows/columns go to a window below/right its positions need to be
2723      * updated.  Can only be done after the sizes have been updated. */
2724     if (frp2 == frp_close->fr_next)
2725     {
2726 	int row = win->w_winrow;
2727 	int col = win->w_wincol;
2728 
2729 	frame_comp_pos(frp2, &row, &col);
2730     }
2731 
2732     if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2733     {
2734 	/* There is no other frame in this list, move its info to the parent
2735 	 * and remove it. */
2736 	frp2->fr_parent->fr_layout = frp2->fr_layout;
2737 	frp2->fr_parent->fr_child = frp2->fr_child;
2738 	for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2739 	    frp->fr_parent = frp2->fr_parent;
2740 	frp2->fr_parent->fr_win = frp2->fr_win;
2741 	if (frp2->fr_win != NULL)
2742 	    frp2->fr_win->w_frame = frp2->fr_parent;
2743 	frp = frp2->fr_parent;
2744 	vim_free(frp2);
2745 
2746 	frp2 = frp->fr_parent;
2747 	if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2748 	{
2749 	    /* The frame above the parent has the same layout, have to merge
2750 	     * the frames into this list. */
2751 	    if (frp2->fr_child == frp)
2752 		frp2->fr_child = frp->fr_child;
2753 	    frp->fr_child->fr_prev = frp->fr_prev;
2754 	    if (frp->fr_prev != NULL)
2755 		frp->fr_prev->fr_next = frp->fr_child;
2756 	    for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2757 	    {
2758 		frp3->fr_parent = frp2;
2759 		if (frp3->fr_next == NULL)
2760 		{
2761 		    frp3->fr_next = frp->fr_next;
2762 		    if (frp->fr_next != NULL)
2763 			frp->fr_next->fr_prev = frp3;
2764 		    break;
2765 		}
2766 	    }
2767 	    vim_free(frp);
2768 	}
2769     }
2770 
2771     return wp;
2772 }
2773 
2774 /*
2775  * Find out which frame is going to get the freed up space when "win" is
2776  * closed.
2777  * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2778  * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2779  * This makes opening a window and closing it immediately keep the same window
2780  * layout.
2781  */
2782     static frame_T *
2783 win_altframe(
2784     win_T	*win,
2785     tabpage_T	*tp)		/* tab page "win" is in, NULL for current */
2786 {
2787     frame_T	*frp;
2788     int		b;
2789 
2790     if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
2791 	/* Last window in this tab page, will go to next tab page. */
2792 	return alt_tabpage()->tp_curwin->w_frame;
2793 
2794     frp = win->w_frame;
2795     if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2796 	b = p_spr;
2797     else
2798 	b = p_sb;
2799     if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2800 	return frp->fr_next;
2801     return frp->fr_prev;
2802 }
2803 
2804 /*
2805  * Return the tabpage that will be used if the current one is closed.
2806  */
2807     static tabpage_T *
2808 alt_tabpage(void)
2809 {
2810     tabpage_T	*tp;
2811 
2812     /* Use the next tab page if possible. */
2813     if (curtab->tp_next != NULL)
2814 	return curtab->tp_next;
2815 
2816     /* Find the last but one tab page. */
2817     for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2818 	;
2819     return tp;
2820 }
2821 
2822 /*
2823  * Find the left-upper window in frame "frp".
2824  */
2825     static win_T *
2826 frame2win(frame_T *frp)
2827 {
2828     while (frp->fr_win == NULL)
2829 	frp = frp->fr_child;
2830     return frp->fr_win;
2831 }
2832 
2833 /*
2834  * Return TRUE if frame "frp" contains window "wp".
2835  */
2836     static int
2837 frame_has_win(frame_T *frp, win_T *wp)
2838 {
2839     frame_T	*p;
2840 
2841     if (frp->fr_layout == FR_LEAF)
2842 	return frp->fr_win == wp;
2843 
2844     for (p = frp->fr_child; p != NULL; p = p->fr_next)
2845 	if (frame_has_win(p, wp))
2846 	    return TRUE;
2847     return FALSE;
2848 }
2849 
2850 /*
2851  * Set a new height for a frame.  Recursively sets the height for contained
2852  * frames and windows.  Caller must take care of positions.
2853  */
2854     static void
2855 frame_new_height(
2856     frame_T	*topfrp,
2857     int		height,
2858     int		topfirst,	/* resize topmost contained frame first */
2859     int		wfh)		/* obey 'winfixheight' when there is a choice;
2860 				   may cause the height not to be set */
2861 {
2862     frame_T	*frp;
2863     int		extra_lines;
2864     int		h;
2865 
2866     if (topfrp->fr_win != NULL)
2867     {
2868 	/* Simple case: just one window. */
2869 	win_new_height(topfrp->fr_win,
2870 				    height - topfrp->fr_win->w_status_height
2871 #ifdef FEAT_MENU
2872 				    - topfrp->fr_win->w_winbar_height
2873 #endif
2874 				    );
2875     }
2876     else if (topfrp->fr_layout == FR_ROW)
2877     {
2878 	do
2879 	{
2880 	    /* All frames in this row get the same new height. */
2881 	    for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2882 	    {
2883 		frame_new_height(frp, height, topfirst, wfh);
2884 		if (frp->fr_height > height)
2885 		{
2886 		    /* Could not fit the windows, make the whole row higher. */
2887 		    height = frp->fr_height;
2888 		    break;
2889 		}
2890 	    }
2891 	}
2892 	while (frp != NULL);
2893     }
2894     else    /* fr_layout == FR_COL */
2895     {
2896 	/* Complicated case: Resize a column of frames.  Resize the bottom
2897 	 * frame first, frames above that when needed. */
2898 
2899 	frp = topfrp->fr_child;
2900 	if (wfh)
2901 	    /* Advance past frames with one window with 'wfh' set. */
2902 	    while (frame_fixed_height(frp))
2903 	    {
2904 		frp = frp->fr_next;
2905 		if (frp == NULL)
2906 		    return;	    /* no frame without 'wfh', give up */
2907 	    }
2908 	if (!topfirst)
2909 	{
2910 	    /* Find the bottom frame of this column */
2911 	    while (frp->fr_next != NULL)
2912 		frp = frp->fr_next;
2913 	    if (wfh)
2914 		/* Advance back for frames with one window with 'wfh' set. */
2915 		while (frame_fixed_height(frp))
2916 		    frp = frp->fr_prev;
2917 	}
2918 
2919 	extra_lines = height - topfrp->fr_height;
2920 	if (extra_lines < 0)
2921 	{
2922 	    /* reduce height of contained frames, bottom or top frame first */
2923 	    while (frp != NULL)
2924 	    {
2925 		h = frame_minheight(frp, NULL);
2926 		if (frp->fr_height + extra_lines < h)
2927 		{
2928 		    extra_lines += frp->fr_height - h;
2929 		    frame_new_height(frp, h, topfirst, wfh);
2930 		}
2931 		else
2932 		{
2933 		    frame_new_height(frp, frp->fr_height + extra_lines,
2934 							       topfirst, wfh);
2935 		    break;
2936 		}
2937 		if (topfirst)
2938 		{
2939 		    do
2940 			frp = frp->fr_next;
2941 		    while (wfh && frp != NULL && frame_fixed_height(frp));
2942 		}
2943 		else
2944 		{
2945 		    do
2946 			frp = frp->fr_prev;
2947 		    while (wfh && frp != NULL && frame_fixed_height(frp));
2948 		}
2949 		/* Increase "height" if we could not reduce enough frames. */
2950 		if (frp == NULL)
2951 		    height -= extra_lines;
2952 	    }
2953 	}
2954 	else if (extra_lines > 0)
2955 	{
2956 	    /* increase height of bottom or top frame */
2957 	    frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2958 	}
2959     }
2960     topfrp->fr_height = height;
2961 }
2962 
2963 /*
2964  * Return TRUE if height of frame "frp" should not be changed because of
2965  * the 'winfixheight' option.
2966  */
2967     static int
2968 frame_fixed_height(frame_T *frp)
2969 {
2970     /* frame with one window: fixed height if 'winfixheight' set. */
2971     if (frp->fr_win != NULL)
2972 	return frp->fr_win->w_p_wfh;
2973 
2974     if (frp->fr_layout == FR_ROW)
2975     {
2976 	/* The frame is fixed height if one of the frames in the row is fixed
2977 	 * height. */
2978 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2979 	    if (frame_fixed_height(frp))
2980 		return TRUE;
2981 	return FALSE;
2982     }
2983 
2984     /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2985      * frames in the row are fixed height. */
2986     for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2987 	if (!frame_fixed_height(frp))
2988 	    return FALSE;
2989     return TRUE;
2990 }
2991 
2992 /*
2993  * Return TRUE if width of frame "frp" should not be changed because of
2994  * the 'winfixwidth' option.
2995  */
2996     static int
2997 frame_fixed_width(frame_T *frp)
2998 {
2999     /* frame with one window: fixed width if 'winfixwidth' set. */
3000     if (frp->fr_win != NULL)
3001 	return frp->fr_win->w_p_wfw;
3002 
3003     if (frp->fr_layout == FR_COL)
3004     {
3005 	/* The frame is fixed width if one of the frames in the row is fixed
3006 	 * width. */
3007 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3008 	    if (frame_fixed_width(frp))
3009 		return TRUE;
3010 	return FALSE;
3011     }
3012 
3013     /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3014      * frames in the row are fixed width. */
3015     for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3016 	if (!frame_fixed_width(frp))
3017 	    return FALSE;
3018     return TRUE;
3019 }
3020 
3021 /*
3022  * Add a status line to windows at the bottom of "frp".
3023  * Note: Does not check if there is room!
3024  */
3025     static void
3026 frame_add_statusline(frame_T *frp)
3027 {
3028     win_T	*wp;
3029 
3030     if (frp->fr_layout == FR_LEAF)
3031     {
3032 	wp = frp->fr_win;
3033 	if (wp->w_status_height == 0)
3034 	{
3035 	    if (wp->w_height > 0)	/* don't make it negative */
3036 		--wp->w_height;
3037 	    wp->w_status_height = STATUS_HEIGHT;
3038 	}
3039     }
3040     else if (frp->fr_layout == FR_ROW)
3041     {
3042 	/* Handle all the frames in the row. */
3043 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3044 	    frame_add_statusline(frp);
3045     }
3046     else /* frp->fr_layout == FR_COL */
3047     {
3048 	/* Only need to handle the last frame in the column. */
3049 	for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3050 	    ;
3051 	frame_add_statusline(frp);
3052     }
3053 }
3054 
3055 /*
3056  * Set width of a frame.  Handles recursively going through contained frames.
3057  * May remove separator line for windows at the right side (for win_close()).
3058  */
3059     static void
3060 frame_new_width(
3061     frame_T	*topfrp,
3062     int		width,
3063     int		leftfirst,	/* resize leftmost contained frame first */
3064     int		wfw)		/* obey 'winfixwidth' when there is a choice;
3065 				   may cause the width not to be set */
3066 {
3067     frame_T	*frp;
3068     int		extra_cols;
3069     int		w;
3070     win_T	*wp;
3071 
3072     if (topfrp->fr_layout == FR_LEAF)
3073     {
3074 	/* Simple case: just one window. */
3075 	wp = topfrp->fr_win;
3076 	/* Find out if there are any windows right of this one. */
3077 	for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3078 	    if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3079 		break;
3080 	if (frp->fr_parent == NULL)
3081 	    wp->w_vsep_width = 0;
3082 	win_new_width(wp, width - wp->w_vsep_width);
3083     }
3084     else if (topfrp->fr_layout == FR_COL)
3085     {
3086 	do
3087 	{
3088 	    /* All frames in this column get the same new width. */
3089 	    for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3090 	    {
3091 		frame_new_width(frp, width, leftfirst, wfw);
3092 		if (frp->fr_width > width)
3093 		{
3094 		    /* Could not fit the windows, make whole column wider. */
3095 		    width = frp->fr_width;
3096 		    break;
3097 		}
3098 	    }
3099 	} while (frp != NULL);
3100     }
3101     else    /* fr_layout == FR_ROW */
3102     {
3103 	/* Complicated case: Resize a row of frames.  Resize the rightmost
3104 	 * frame first, frames left of it when needed. */
3105 
3106 	frp = topfrp->fr_child;
3107 	if (wfw)
3108 	    /* Advance past frames with one window with 'wfw' set. */
3109 	    while (frame_fixed_width(frp))
3110 	    {
3111 		frp = frp->fr_next;
3112 		if (frp == NULL)
3113 		    return;	    /* no frame without 'wfw', give up */
3114 	    }
3115 	if (!leftfirst)
3116 	{
3117 	    /* Find the rightmost frame of this row */
3118 	    while (frp->fr_next != NULL)
3119 		frp = frp->fr_next;
3120 	    if (wfw)
3121 		/* Advance back for frames with one window with 'wfw' set. */
3122 		while (frame_fixed_width(frp))
3123 		    frp = frp->fr_prev;
3124 	}
3125 
3126 	extra_cols = width - topfrp->fr_width;
3127 	if (extra_cols < 0)
3128 	{
3129 	    /* reduce frame width, rightmost frame first */
3130 	    while (frp != NULL)
3131 	    {
3132 		w = frame_minwidth(frp, NULL);
3133 		if (frp->fr_width + extra_cols < w)
3134 		{
3135 		    extra_cols += frp->fr_width - w;
3136 		    frame_new_width(frp, w, leftfirst, wfw);
3137 		}
3138 		else
3139 		{
3140 		    frame_new_width(frp, frp->fr_width + extra_cols,
3141 							      leftfirst, wfw);
3142 		    break;
3143 		}
3144 		if (leftfirst)
3145 		{
3146 		    do
3147 			frp = frp->fr_next;
3148 		    while (wfw && frp != NULL && frame_fixed_width(frp));
3149 		}
3150 		else
3151 		{
3152 		    do
3153 			frp = frp->fr_prev;
3154 		    while (wfw && frp != NULL && frame_fixed_width(frp));
3155 		}
3156 		/* Increase "width" if we could not reduce enough frames. */
3157 		if (frp == NULL)
3158 		    width -= extra_cols;
3159 	    }
3160 	}
3161 	else if (extra_cols > 0)
3162 	{
3163 	    /* increase width of rightmost frame */
3164 	    frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
3165 	}
3166     }
3167     topfrp->fr_width = width;
3168 }
3169 
3170 /*
3171  * Add the vertical separator to windows at the right side of "frp".
3172  * Note: Does not check if there is room!
3173  */
3174     static void
3175 frame_add_vsep(frame_T *frp)
3176 {
3177     win_T	*wp;
3178 
3179     if (frp->fr_layout == FR_LEAF)
3180     {
3181 	wp = frp->fr_win;
3182 	if (wp->w_vsep_width == 0)
3183 	{
3184 	    if (wp->w_width > 0)	/* don't make it negative */
3185 		--wp->w_width;
3186 	    wp->w_vsep_width = 1;
3187 	}
3188     }
3189     else if (frp->fr_layout == FR_COL)
3190     {
3191 	/* Handle all the frames in the column. */
3192 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3193 	    frame_add_vsep(frp);
3194     }
3195     else /* frp->fr_layout == FR_ROW */
3196     {
3197 	/* Only need to handle the last frame in the row. */
3198 	frp = frp->fr_child;
3199 	while (frp->fr_next != NULL)
3200 	    frp = frp->fr_next;
3201 	frame_add_vsep(frp);
3202     }
3203 }
3204 
3205 /*
3206  * Set frame width from the window it contains.
3207  */
3208     static void
3209 frame_fix_width(win_T *wp)
3210 {
3211     wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3212 }
3213 
3214 /*
3215  * Set frame height from the window it contains.
3216  */
3217     static void
3218 frame_fix_height(win_T *wp)
3219 {
3220     wp->w_frame->fr_height = wp->w_height + wp->w_status_height
3221 #ifdef FEAT_MENU
3222 	+ wp->w_winbar_height
3223 #endif
3224 	;
3225 }
3226 
3227 /*
3228  * Compute the minimal height for frame "topfrp".
3229  * Uses the 'winminheight' option.
3230  * When "next_curwin" isn't NULL, use p_wh for this window.
3231  * When "next_curwin" is NOWIN, don't use at least one line for the current
3232  * window.
3233  */
3234     static int
3235 frame_minheight(frame_T *topfrp, win_T *next_curwin)
3236 {
3237     frame_T	*frp;
3238     int		m;
3239     int		n;
3240 
3241     if (topfrp->fr_win != NULL)
3242     {
3243 	if (topfrp->fr_win == next_curwin)
3244 	    m = p_wh + topfrp->fr_win->w_status_height;
3245 	else
3246 	{
3247 	    /* window: minimal height of the window plus status line */
3248 	    m = p_wmh + topfrp->fr_win->w_status_height;
3249 	    /* Current window is minimal one line high */
3250 	    if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3251 		++m;
3252 	}
3253     }
3254     else if (topfrp->fr_layout == FR_ROW)
3255     {
3256 	/* get the minimal height from each frame in this row */
3257 	m = 0;
3258 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3259 	{
3260 	    n = frame_minheight(frp, next_curwin);
3261 	    if (n > m)
3262 		m = n;
3263 	}
3264     }
3265     else
3266     {
3267 	/* Add up the minimal heights for all frames in this column. */
3268 	m = 0;
3269 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3270 	    m += frame_minheight(frp, next_curwin);
3271     }
3272 
3273     return m;
3274 }
3275 
3276 /*
3277  * Compute the minimal width for frame "topfrp".
3278  * When "next_curwin" isn't NULL, use p_wiw for this window.
3279  * When "next_curwin" is NOWIN, don't use at least one column for the current
3280  * window.
3281  */
3282     static int
3283 frame_minwidth(
3284     frame_T	*topfrp,
3285     win_T	*next_curwin)	/* use p_wh and p_wiw for next_curwin */
3286 {
3287     frame_T	*frp;
3288     int		m, n;
3289 
3290     if (topfrp->fr_win != NULL)
3291     {
3292 	if (topfrp->fr_win == next_curwin)
3293 	    m = p_wiw + topfrp->fr_win->w_vsep_width;
3294 	else
3295 	{
3296 	    /* window: minimal width of the window plus separator column */
3297 	    m = p_wmw + topfrp->fr_win->w_vsep_width;
3298 	    /* Current window is minimal one column wide */
3299 	    if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3300 		++m;
3301 	}
3302     }
3303     else if (topfrp->fr_layout == FR_COL)
3304     {
3305 	/* get the minimal width from each frame in this column */
3306 	m = 0;
3307 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3308 	{
3309 	    n = frame_minwidth(frp, next_curwin);
3310 	    if (n > m)
3311 		m = n;
3312 	}
3313     }
3314     else
3315     {
3316 	/* Add up the minimal widths for all frames in this row. */
3317 	m = 0;
3318 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3319 	    m += frame_minwidth(frp, next_curwin);
3320     }
3321 
3322     return m;
3323 }
3324 
3325 
3326 /*
3327  * Try to close all windows except current one.
3328  * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3329  * used and the buffer was modified.
3330  *
3331  * Used by ":bdel" and ":only".
3332  */
3333     void
3334 close_others(
3335     int		message,
3336     int		forceit)	    /* always hide all other windows */
3337 {
3338     win_T	*wp;
3339     win_T	*nextwp;
3340     int		r;
3341 
3342     if (one_window())
3343     {
3344 	if (message
3345 #ifdef FEAT_AUTOCMD
3346 		    && !autocmd_busy
3347 #endif
3348 				    )
3349 	    MSG(_(m_onlyone));
3350 	return;
3351     }
3352 
3353     /* Be very careful here: autocommands may change the window layout. */
3354     for (wp = firstwin; win_valid(wp); wp = nextwp)
3355     {
3356 	nextwp = wp->w_next;
3357 	if (wp != curwin)		/* don't close current window */
3358 	{
3359 
3360 	    /* Check if it's allowed to abandon this window */
3361 	    r = can_abandon(wp->w_buffer, forceit);
3362 #ifdef FEAT_AUTOCMD
3363 	    if (!win_valid(wp))		/* autocommands messed wp up */
3364 	    {
3365 		nextwp = firstwin;
3366 		continue;
3367 	    }
3368 #endif
3369 	    if (!r)
3370 	    {
3371 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3372 		if (message && (p_confirm || cmdmod.confirm) && p_write)
3373 		{
3374 		    dialog_changed(wp->w_buffer, FALSE);
3375 # ifdef FEAT_AUTOCMD
3376 		    if (!win_valid(wp))		/* autocommands messed wp up */
3377 		    {
3378 			nextwp = firstwin;
3379 			continue;
3380 		    }
3381 # endif
3382 		}
3383 		if (bufIsChanged(wp->w_buffer))
3384 #endif
3385 		    continue;
3386 	    }
3387 	    win_close(wp, !buf_hide(wp->w_buffer)
3388 					       && !bufIsChanged(wp->w_buffer));
3389 	}
3390     }
3391 
3392     if (message && !ONE_WINDOW)
3393 	EMSG(_("E445: Other window contains changes"));
3394 }
3395 
3396 /*
3397  * Init the current window "curwin".
3398  * Called when a new file is being edited.
3399  */
3400     void
3401 curwin_init(void)
3402 {
3403     win_init_empty(curwin);
3404 }
3405 
3406     void
3407 win_init_empty(win_T *wp)
3408 {
3409     redraw_win_later(wp, NOT_VALID);
3410     wp->w_lines_valid = 0;
3411     wp->w_cursor.lnum = 1;
3412     wp->w_curswant = wp->w_cursor.col = 0;
3413 #ifdef FEAT_VIRTUALEDIT
3414     wp->w_cursor.coladd = 0;
3415 #endif
3416     wp->w_pcmark.lnum = 1;	/* pcmark not cleared but set to line 1 */
3417     wp->w_pcmark.col = 0;
3418     wp->w_prev_pcmark.lnum = 0;
3419     wp->w_prev_pcmark.col = 0;
3420     wp->w_topline = 1;
3421 #ifdef FEAT_DIFF
3422     wp->w_topfill = 0;
3423 #endif
3424     wp->w_botline = 2;
3425 #ifdef FEAT_FKMAP
3426     if (wp->w_p_rl)
3427 	wp->w_farsi = W_CONV + W_R_L;
3428     else
3429 	wp->w_farsi = W_CONV;
3430 #endif
3431 #ifdef FEAT_SYN_HL
3432     wp->w_s = &wp->w_buffer->b_s;
3433 #endif
3434 }
3435 
3436 /*
3437  * Allocate the first window and put an empty buffer in it.
3438  * Called from main().
3439  * Return FAIL when something goes wrong (out of memory).
3440  */
3441     int
3442 win_alloc_first(void)
3443 {
3444     if (win_alloc_firstwin(NULL) == FAIL)
3445 	return FAIL;
3446 
3447     first_tabpage = alloc_tabpage();
3448     if (first_tabpage == NULL)
3449 	return FAIL;
3450     first_tabpage->tp_topframe = topframe;
3451     curtab = first_tabpage;
3452 
3453     return OK;
3454 }
3455 
3456 #if defined(FEAT_AUTOCMD) || defined(PROTO)
3457 /*
3458  * Init "aucmd_win".  This can only be done after the first
3459  * window is fully initialized, thus it can't be in win_alloc_first().
3460  */
3461     void
3462 win_alloc_aucmd_win(void)
3463 {
3464     aucmd_win = win_alloc(NULL, TRUE);
3465     if (aucmd_win != NULL)
3466     {
3467 	win_init_some(aucmd_win, curwin);
3468 	RESET_BINDING(aucmd_win);
3469 	new_frame(aucmd_win);
3470     }
3471 }
3472 #endif
3473 
3474 /*
3475  * Allocate the first window or the first window in a new tab page.
3476  * When "oldwin" is NULL create an empty buffer for it.
3477  * When "oldwin" is not NULL copy info from it to the new window.
3478  * Return FAIL when something goes wrong (out of memory).
3479  */
3480     static int
3481 win_alloc_firstwin(win_T *oldwin)
3482 {
3483     curwin = win_alloc(NULL, FALSE);
3484     if (oldwin == NULL)
3485     {
3486 	/* Very first window, need to create an empty buffer for it and
3487 	 * initialize from scratch. */
3488 	curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3489 	if (curwin == NULL || curbuf == NULL)
3490 	    return FAIL;
3491 	curwin->w_buffer = curbuf;
3492 #ifdef FEAT_SYN_HL
3493 	curwin->w_s = &(curbuf->b_s);
3494 #endif
3495 	curbuf->b_nwindows = 1;	/* there is one window */
3496 	curwin->w_alist = &global_alist;
3497 	curwin_init();		/* init current window */
3498     }
3499     else
3500     {
3501 	/* First window in new tab page, initialize it from "oldwin". */
3502 	win_init(curwin, oldwin, 0);
3503 
3504 	/* We don't want cursor- and scroll-binding in the first window. */
3505 	RESET_BINDING(curwin);
3506     }
3507 
3508     new_frame(curwin);
3509     if (curwin->w_frame == NULL)
3510 	return FAIL;
3511     topframe = curwin->w_frame;
3512     topframe->fr_width = Columns;
3513     topframe->fr_height = Rows - p_ch;
3514     topframe->fr_win = curwin;
3515 
3516     return OK;
3517 }
3518 
3519 /*
3520  * Create a frame for window "wp".
3521  */
3522     static void
3523 new_frame(win_T *wp)
3524 {
3525     frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3526 
3527     wp->w_frame = frp;
3528     if (frp != NULL)
3529     {
3530 	frp->fr_layout = FR_LEAF;
3531 	frp->fr_win = wp;
3532     }
3533 }
3534 
3535 /*
3536  * Initialize the window and frame size to the maximum.
3537  */
3538     void
3539 win_init_size(void)
3540 {
3541     firstwin->w_height = ROWS_AVAIL;
3542     topframe->fr_height = ROWS_AVAIL;
3543     firstwin->w_width = Columns;
3544     topframe->fr_width = Columns;
3545 }
3546 
3547 /*
3548  * Allocate a new tabpage_T and init the values.
3549  * Returns NULL when out of memory.
3550  */
3551     static tabpage_T *
3552 alloc_tabpage(void)
3553 {
3554     tabpage_T	*tp;
3555 # ifdef FEAT_GUI
3556     int		i;
3557 # endif
3558 
3559 
3560     tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3561     if (tp == NULL)
3562 	return NULL;
3563 
3564 # ifdef FEAT_EVAL
3565     /* init t: variables */
3566     tp->tp_vars = dict_alloc();
3567     if (tp->tp_vars == NULL)
3568     {
3569 	vim_free(tp);
3570 	return NULL;
3571     }
3572     init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3573 # endif
3574 
3575 # ifdef FEAT_GUI
3576     for (i = 0; i < 3; i++)
3577 	tp->tp_prev_which_scrollbars[i] = -1;
3578 # endif
3579 # ifdef FEAT_DIFF
3580     tp->tp_diff_invalid = TRUE;
3581 # endif
3582     tp->tp_ch_used = p_ch;
3583 
3584     return tp;
3585 }
3586 
3587     void
3588 free_tabpage(tabpage_T *tp)
3589 {
3590     int idx;
3591 
3592 # ifdef FEAT_DIFF
3593     diff_clear(tp);
3594 # endif
3595     for (idx = 0; idx < SNAP_COUNT; ++idx)
3596 	clear_snapshot(tp, idx);
3597 #ifdef FEAT_EVAL
3598     vars_clear(&tp->tp_vars->dv_hashtab);	/* free all t: variables */
3599     hash_init(&tp->tp_vars->dv_hashtab);
3600     unref_var_dict(tp->tp_vars);
3601 #endif
3602 
3603 #ifdef FEAT_PYTHON
3604     python_tabpage_free(tp);
3605 #endif
3606 
3607 #ifdef FEAT_PYTHON3
3608     python3_tabpage_free(tp);
3609 #endif
3610 
3611     vim_free(tp);
3612 }
3613 
3614 /*
3615  * Create a new Tab page with one window.
3616  * It will edit the current buffer, like after ":split".
3617  * When "after" is 0 put it just after the current Tab page.
3618  * Otherwise put it just before tab page "after".
3619  * Return FAIL or OK.
3620  */
3621     int
3622 win_new_tabpage(int after)
3623 {
3624     tabpage_T	*tp = curtab;
3625     tabpage_T	*newtp;
3626     int		n;
3627 
3628     newtp = alloc_tabpage();
3629     if (newtp == NULL)
3630 	return FAIL;
3631 
3632     /* Remember the current windows in this Tab page. */
3633     if (leave_tabpage(curbuf, TRUE) == FAIL)
3634     {
3635 	vim_free(newtp);
3636 	return FAIL;
3637     }
3638     curtab = newtp;
3639 
3640     /* Create a new empty window. */
3641     if (win_alloc_firstwin(tp->tp_curwin) == OK)
3642     {
3643 	/* Make the new Tab page the new topframe. */
3644 	if (after == 1)
3645 	{
3646 	    /* New tab page becomes the first one. */
3647 	    newtp->tp_next = first_tabpage;
3648 	    first_tabpage = newtp;
3649 	}
3650 	else
3651 	{
3652 	    if (after > 0)
3653 	    {
3654 		/* Put new tab page before tab page "after". */
3655 		n = 2;
3656 		for (tp = first_tabpage; tp->tp_next != NULL
3657 					       && n < after; tp = tp->tp_next)
3658 		    ++n;
3659 	    }
3660 	    newtp->tp_next = tp->tp_next;
3661 	    tp->tp_next = newtp;
3662 	}
3663 	win_init_size();
3664 	firstwin->w_winrow = tabline_height();
3665 	win_comp_scroll(curwin);
3666 
3667 	newtp->tp_topframe = topframe;
3668 	last_status(FALSE);
3669 
3670 #if defined(FEAT_GUI)
3671 	/* When 'guioptions' includes 'L' or 'R' may have to remove or add
3672 	 * scrollbars.  Have to update them anyway. */
3673 	gui_may_update_scrollbars();
3674 #endif
3675 
3676 	redraw_all_later(CLEAR);
3677 #ifdef FEAT_AUTOCMD
3678 	apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
3679 	apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3680 	apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
3681 	apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3682 #endif
3683 	return OK;
3684     }
3685 
3686     /* Failed, get back the previous Tab page */
3687     enter_tabpage(curtab, curbuf, TRUE, TRUE);
3688     return FAIL;
3689 }
3690 
3691 /*
3692  * Open a new tab page if ":tab cmd" was used.  It will edit the same buffer,
3693  * like with ":split".
3694  * Returns OK if a new tab page was created, FAIL otherwise.
3695  */
3696     int
3697 may_open_tabpage(void)
3698 {
3699     int		n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
3700 
3701     if (n != 0)
3702     {
3703 	cmdmod.tab = 0;	    /* reset it to avoid doing it twice */
3704 	postponed_split_tab = 0;
3705 	return win_new_tabpage(n);
3706     }
3707     return FAIL;
3708 }
3709 
3710 /*
3711  * Create up to "maxcount" tabpages with empty windows.
3712  * Returns the number of resulting tab pages.
3713  */
3714     int
3715 make_tabpages(int maxcount)
3716 {
3717     int		count = maxcount;
3718     int		todo;
3719 
3720     /* Limit to 'tabpagemax' tabs. */
3721     if (count > p_tpm)
3722 	count = p_tpm;
3723 
3724 #ifdef FEAT_AUTOCMD
3725     /*
3726      * Don't execute autocommands while creating the tab pages.  Must do that
3727      * when putting the buffers in the windows.
3728      */
3729     block_autocmds();
3730 #endif
3731 
3732     for (todo = count - 1; todo > 0; --todo)
3733 	if (win_new_tabpage(0) == FAIL)
3734 	    break;
3735 
3736 #ifdef FEAT_AUTOCMD
3737     unblock_autocmds();
3738 #endif
3739 
3740     /* return actual number of tab pages */
3741     return (count - todo);
3742 }
3743 
3744 /*
3745  * Return TRUE when "tpc" points to a valid tab page.
3746  */
3747     int
3748 valid_tabpage(tabpage_T *tpc)
3749 {
3750     tabpage_T	*tp;
3751 
3752     FOR_ALL_TABPAGES(tp)
3753 	if (tp == tpc)
3754 	    return TRUE;
3755     return FALSE;
3756 }
3757 
3758 /*
3759  * Return TRUE when "tpc" points to a valid tab page and at least one window is
3760  * valid.
3761  */
3762     int
3763 valid_tabpage_win(tabpage_T *tpc)
3764 {
3765     tabpage_T	*tp;
3766     win_T	*wp;
3767 
3768     FOR_ALL_TABPAGES(tp)
3769     {
3770 	if (tp == tpc)
3771 	{
3772 	    FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3773 	    {
3774 		if (win_valid_any_tab(wp))
3775 		    return TRUE;
3776 	    }
3777 	    return FALSE;
3778 	}
3779     }
3780     /* shouldn't happen */
3781     return FALSE;
3782 }
3783 
3784 /*
3785  * Close tabpage "tab", assuming it has no windows in it.
3786  * There must be another tabpage or this will crash.
3787  */
3788     void
3789 close_tabpage(tabpage_T *tab)
3790 {
3791     tabpage_T	*ptp;
3792 
3793     if (tab == first_tabpage)
3794     {
3795 	first_tabpage = tab->tp_next;
3796 	ptp = first_tabpage;
3797     }
3798     else
3799     {
3800 	for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3801 							    ptp = ptp->tp_next)
3802 	    ;
3803 	assert(ptp != NULL);
3804 	ptp->tp_next = tab->tp_next;
3805     }
3806 
3807     goto_tabpage_tp(ptp, FALSE, FALSE);
3808     free_tabpage(tab);
3809 }
3810 
3811 /*
3812  * Find tab page "n" (first one is 1).  Returns NULL when not found.
3813  */
3814     tabpage_T *
3815 find_tabpage(int n)
3816 {
3817     tabpage_T	*tp;
3818     int		i = 1;
3819 
3820     for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3821 	++i;
3822     return tp;
3823 }
3824 
3825 /*
3826  * Get index of tab page "tp".  First one has index 1.
3827  * When not found returns number of tab pages plus one.
3828  */
3829     int
3830 tabpage_index(tabpage_T *ftp)
3831 {
3832     int		i = 1;
3833     tabpage_T	*tp;
3834 
3835     for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3836 	++i;
3837     return i;
3838 }
3839 
3840 /*
3841  * Prepare for leaving the current tab page.
3842  * When autocommands change "curtab" we don't leave the tab page and return
3843  * FAIL.
3844  * Careful: When OK is returned need to get a new tab page very very soon!
3845  */
3846     static int
3847 leave_tabpage(
3848     buf_T	*new_curbuf UNUSED,    /* what is going to be the new curbuf,
3849 				       NULL if unknown */
3850     int		trigger_leave_autocmds UNUSED)
3851 {
3852     tabpage_T	*tp = curtab;
3853 
3854     reset_VIsual_and_resel();	/* stop Visual mode */
3855 #ifdef FEAT_AUTOCMD
3856     if (trigger_leave_autocmds)
3857     {
3858 	if (new_curbuf != curbuf)
3859 	{
3860 	    apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3861 	    if (curtab != tp)
3862 		return FAIL;
3863 	}
3864 	apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3865 	if (curtab != tp)
3866 	    return FAIL;
3867 	apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3868 	if (curtab != tp)
3869 	    return FAIL;
3870     }
3871 #endif
3872 #if defined(FEAT_GUI)
3873     /* Remove the scrollbars.  They may be added back later. */
3874     if (gui.in_use)
3875 	gui_remove_scrollbars();
3876 #endif
3877     tp->tp_curwin = curwin;
3878     tp->tp_prevwin = prevwin;
3879     tp->tp_firstwin = firstwin;
3880     tp->tp_lastwin = lastwin;
3881     tp->tp_old_Rows = Rows;
3882     tp->tp_old_Columns = Columns;
3883     firstwin = NULL;
3884     lastwin = NULL;
3885     return OK;
3886 }
3887 
3888 /*
3889  * Start using tab page "tp".
3890  * Only to be used after leave_tabpage() or freeing the current tab page.
3891  * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3892  * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
3893  */
3894     static void
3895 enter_tabpage(
3896     tabpage_T	*tp,
3897     buf_T	*old_curbuf UNUSED,
3898     int		trigger_enter_autocmds,
3899     int		trigger_leave_autocmds)
3900 {
3901     int		old_off = tp->tp_firstwin->w_winrow;
3902     win_T	*next_prevwin = tp->tp_prevwin;
3903 
3904     curtab = tp;
3905     firstwin = tp->tp_firstwin;
3906     lastwin = tp->tp_lastwin;
3907     topframe = tp->tp_topframe;
3908 
3909     /* We would like doing the TabEnter event first, but we don't have a
3910      * valid current window yet, which may break some commands.
3911      * This triggers autocommands, thus may make "tp" invalid. */
3912     win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
3913 			      trigger_enter_autocmds, trigger_leave_autocmds);
3914     prevwin = next_prevwin;
3915 
3916     last_status(FALSE);		/* status line may appear or disappear */
3917     (void)win_comp_pos();	/* recompute w_winrow for all windows */
3918     must_redraw = CLEAR;	/* need to redraw everything */
3919 #ifdef FEAT_DIFF
3920     diff_need_scrollbind = TRUE;
3921 #endif
3922 
3923     /* The tabpage line may have appeared or disappeared, may need to resize
3924      * the frames for that.  When the Vim window was resized need to update
3925      * frame sizes too.  Use the stored value of p_ch, so that it can be
3926      * different for each tab page. */
3927     p_ch = curtab->tp_ch_used;
3928     if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3929 #ifdef FEAT_GUI_TABLINE
3930 			    && !gui_use_tabline()
3931 #endif
3932 		))
3933 	shell_new_rows();
3934     if (curtab->tp_old_Columns != Columns && starting == 0)
3935 	shell_new_columns();	/* update window widths */
3936 
3937 #if defined(FEAT_GUI)
3938     /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3939      * scrollbars.  Have to update them anyway. */
3940     gui_may_update_scrollbars();
3941 #endif
3942 
3943 #ifdef FEAT_AUTOCMD
3944     /* Apply autocommands after updating the display, when 'rows' and
3945      * 'columns' have been set correctly. */
3946     if (trigger_enter_autocmds)
3947     {
3948 	apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3949 	if (old_curbuf != curbuf)
3950 	    apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3951     }
3952 #endif
3953 
3954     redraw_all_later(CLEAR);
3955 }
3956 
3957 /*
3958  * Go to tab page "n".  For ":tab N" and "Ngt".
3959  * When "n" is 9999 go to the last tab page.
3960  */
3961     void
3962 goto_tabpage(int n)
3963 {
3964     tabpage_T	*tp;
3965     tabpage_T	*ttp;
3966     int		i;
3967 
3968     if (text_locked())
3969     {
3970 	/* Not allowed when editing the command line. */
3971 	text_locked_msg();
3972 	return;
3973     }
3974 
3975     /* If there is only one it can't work. */
3976     if (first_tabpage->tp_next == NULL)
3977     {
3978 	if (n > 1)
3979 	    beep_flush();
3980 	return;
3981     }
3982 
3983     if (n == 0)
3984     {
3985 	/* No count, go to next tab page, wrap around end. */
3986 	if (curtab->tp_next == NULL)
3987 	    tp = first_tabpage;
3988 	else
3989 	    tp = curtab->tp_next;
3990     }
3991     else if (n < 0)
3992     {
3993 	/* "gT": go to previous tab page, wrap around end.  "N gT" repeats
3994 	 * this N times. */
3995 	ttp = curtab;
3996 	for (i = n; i < 0; ++i)
3997 	{
3998 	    for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3999 		    tp = tp->tp_next)
4000 		;
4001 	    ttp = tp;
4002 	}
4003     }
4004     else if (n == 9999)
4005     {
4006 	/* Go to last tab page. */
4007 	for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4008 	    ;
4009     }
4010     else
4011     {
4012 	/* Go to tab page "n". */
4013 	tp = find_tabpage(n);
4014 	if (tp == NULL)
4015 	{
4016 	    beep_flush();
4017 	    return;
4018 	}
4019     }
4020 
4021     goto_tabpage_tp(tp, TRUE, TRUE);
4022 
4023 #ifdef FEAT_GUI_TABLINE
4024     if (gui_use_tabline())
4025 	gui_mch_set_curtab(tabpage_index(curtab));
4026 #endif
4027 }
4028 
4029 /*
4030  * Go to tabpage "tp".
4031  * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4032  * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
4033  * Note: doesn't update the GUI tab.
4034  */
4035     void
4036 goto_tabpage_tp(
4037     tabpage_T	*tp,
4038     int		trigger_enter_autocmds,
4039     int		trigger_leave_autocmds)
4040 {
4041     /* Don't repeat a message in another tab page. */
4042     set_keep_msg(NULL, 0);
4043 
4044     if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4045 					trigger_leave_autocmds) == OK)
4046     {
4047 	if (valid_tabpage(tp))
4048 	    enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4049 		    trigger_leave_autocmds);
4050 	else
4051 	    enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4052 		    trigger_leave_autocmds);
4053     }
4054 }
4055 
4056 /*
4057  * Enter window "wp" in tab page "tp".
4058  * Also updates the GUI tab.
4059  */
4060     void
4061 goto_tabpage_win(tabpage_T *tp, win_T *wp)
4062 {
4063     goto_tabpage_tp(tp, TRUE, TRUE);
4064     if (curtab == tp && win_valid(wp))
4065     {
4066 	win_enter(wp, TRUE);
4067 # ifdef FEAT_GUI_TABLINE
4068 	if (gui_use_tabline())
4069 	    gui_mch_set_curtab(tabpage_index(curtab));
4070 # endif
4071     }
4072 }
4073 
4074 /*
4075  * Move the current tab page to after tab page "nr".
4076  */
4077     void
4078 tabpage_move(int nr)
4079 {
4080     int		n = 1;
4081     tabpage_T	*tp, *tp_dst;
4082 
4083     if (first_tabpage->tp_next == NULL)
4084 	return;
4085 
4086     for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4087 	++n;
4088 
4089     if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4090 						    && tp->tp_next == curtab))
4091 	return;
4092 
4093     tp_dst = tp;
4094 
4095     /* Remove the current tab page from the list of tab pages. */
4096     if (curtab == first_tabpage)
4097 	first_tabpage = curtab->tp_next;
4098     else
4099     {
4100 	FOR_ALL_TABPAGES(tp)
4101 	    if (tp->tp_next == curtab)
4102 		break;
4103 	if (tp == NULL)	/* "cannot happen" */
4104 	    return;
4105 	tp->tp_next = curtab->tp_next;
4106     }
4107 
4108     /* Re-insert it at the specified position. */
4109     if (nr <= 0)
4110     {
4111 	curtab->tp_next = first_tabpage;
4112 	first_tabpage = curtab;
4113     }
4114     else
4115     {
4116 	curtab->tp_next = tp_dst->tp_next;
4117 	tp_dst->tp_next = curtab;
4118     }
4119 
4120     /* Need to redraw the tabline.  Tab page contents doesn't change. */
4121     redraw_tabline = TRUE;
4122 }
4123 
4124 
4125 /*
4126  * Go to another window.
4127  * When jumping to another buffer, stop Visual mode.  Do this before
4128  * changing windows so we can yank the selection into the '*' register.
4129  * When jumping to another window on the same buffer, adjust its cursor
4130  * position to keep the same Visual area.
4131  */
4132     void
4133 win_goto(win_T *wp)
4134 {
4135 #ifdef FEAT_CONCEAL
4136     win_T	*owp = curwin;
4137 #endif
4138 
4139     if (text_locked())
4140     {
4141 	beep_flush();
4142 	text_locked_msg();
4143 	return;
4144     }
4145 #ifdef FEAT_AUTOCMD
4146     if (curbuf_locked())
4147 	return;
4148 #endif
4149 
4150     if (wp->w_buffer != curbuf)
4151 	reset_VIsual_and_resel();
4152     else if (VIsual_active)
4153 	wp->w_cursor = curwin->w_cursor;
4154 
4155 #ifdef FEAT_GUI
4156     need_mouse_correct = TRUE;
4157 #endif
4158     win_enter(wp, TRUE);
4159 
4160 #ifdef FEAT_CONCEAL
4161     /* Conceal cursor line in previous window, unconceal in current window. */
4162     if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
4163 	update_single_line(owp, owp->w_cursor.lnum);
4164     if (curwin->w_p_cole > 0 && !msg_scrolled)
4165 	need_cursor_line_redraw = TRUE;
4166 #endif
4167 }
4168 
4169 #if defined(FEAT_PERL) || defined(PROTO)
4170 /*
4171  * Find window number "winnr" (counting top to bottom).
4172  */
4173     win_T *
4174 win_find_nr(int winnr)
4175 {
4176     win_T	*wp;
4177 
4178     FOR_ALL_WINDOWS(wp)
4179 	if (--winnr == 0)
4180 	    break;
4181     return wp;
4182 }
4183 #endif
4184 
4185 #if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
4186 /*
4187  * Find the tabpage for window "win".
4188  */
4189     tabpage_T *
4190 win_find_tabpage(win_T *win)
4191 {
4192     win_T	*wp;
4193     tabpage_T	*tp;
4194 
4195     FOR_ALL_TAB_WINDOWS(tp, wp)
4196 	    if (wp == win)
4197 		return tp;
4198     return NULL;
4199 }
4200 #endif
4201 
4202 /*
4203  * Move to window above or below "count" times.
4204  */
4205     static void
4206 win_goto_ver(
4207     int		up,		/* TRUE to go to win above */
4208     long	count)
4209 {
4210     frame_T	*fr;
4211     frame_T	*nfr;
4212     frame_T	*foundfr;
4213 
4214     foundfr = curwin->w_frame;
4215     while (count--)
4216     {
4217 	/*
4218 	 * First go upwards in the tree of frames until we find a upwards or
4219 	 * downwards neighbor.
4220 	 */
4221 	fr = foundfr;
4222 	for (;;)
4223 	{
4224 	    if (fr == topframe)
4225 		goto end;
4226 	    if (up)
4227 		nfr = fr->fr_prev;
4228 	    else
4229 		nfr = fr->fr_next;
4230 	    if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4231 		break;
4232 	    fr = fr->fr_parent;
4233 	}
4234 
4235 	/*
4236 	 * Now go downwards to find the bottom or top frame in it.
4237 	 */
4238 	for (;;)
4239 	{
4240 	    if (nfr->fr_layout == FR_LEAF)
4241 	    {
4242 		foundfr = nfr;
4243 		break;
4244 	    }
4245 	    fr = nfr->fr_child;
4246 	    if (nfr->fr_layout == FR_ROW)
4247 	    {
4248 		/* Find the frame at the cursor row. */
4249 		while (fr->fr_next != NULL
4250 			&& frame2win(fr)->w_wincol + fr->fr_width
4251 					 <= curwin->w_wincol + curwin->w_wcol)
4252 		    fr = fr->fr_next;
4253 	    }
4254 	    if (nfr->fr_layout == FR_COL && up)
4255 		while (fr->fr_next != NULL)
4256 		    fr = fr->fr_next;
4257 	    nfr = fr;
4258 	}
4259     }
4260 end:
4261     if (foundfr != NULL)
4262 	win_goto(foundfr->fr_win);
4263 }
4264 
4265 /*
4266  * Move to left or right window.
4267  */
4268     static void
4269 win_goto_hor(
4270     int		left,		/* TRUE to go to left win */
4271     long	count)
4272 {
4273     frame_T	*fr;
4274     frame_T	*nfr;
4275     frame_T	*foundfr;
4276 
4277     foundfr = curwin->w_frame;
4278     while (count--)
4279     {
4280 	/*
4281 	 * First go upwards in the tree of frames until we find a left or
4282 	 * right neighbor.
4283 	 */
4284 	fr = foundfr;
4285 	for (;;)
4286 	{
4287 	    if (fr == topframe)
4288 		goto end;
4289 	    if (left)
4290 		nfr = fr->fr_prev;
4291 	    else
4292 		nfr = fr->fr_next;
4293 	    if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4294 		break;
4295 	    fr = fr->fr_parent;
4296 	}
4297 
4298 	/*
4299 	 * Now go downwards to find the leftmost or rightmost frame in it.
4300 	 */
4301 	for (;;)
4302 	{
4303 	    if (nfr->fr_layout == FR_LEAF)
4304 	    {
4305 		foundfr = nfr;
4306 		break;
4307 	    }
4308 	    fr = nfr->fr_child;
4309 	    if (nfr->fr_layout == FR_COL)
4310 	    {
4311 		/* Find the frame at the cursor row. */
4312 		while (fr->fr_next != NULL
4313 			&& frame2win(fr)->w_winrow + fr->fr_height
4314 					 <= curwin->w_winrow + curwin->w_wrow)
4315 		    fr = fr->fr_next;
4316 	    }
4317 	    if (nfr->fr_layout == FR_ROW && left)
4318 		while (fr->fr_next != NULL)
4319 		    fr = fr->fr_next;
4320 	    nfr = fr;
4321 	}
4322     }
4323 end:
4324     if (foundfr != NULL)
4325 	win_goto(foundfr->fr_win);
4326 }
4327 
4328 /*
4329  * Make window "wp" the current window.
4330  */
4331     void
4332 win_enter(win_T *wp, int undo_sync)
4333 {
4334     win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
4335 }
4336 
4337 /*
4338  * Make window wp the current window.
4339  * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4340  * been closed and isn't valid.
4341  */
4342     static void
4343 win_enter_ext(
4344     win_T	*wp,
4345     int		undo_sync,
4346     int		curwin_invalid,
4347     int		trigger_new_autocmds UNUSED,
4348     int		trigger_enter_autocmds UNUSED,
4349     int		trigger_leave_autocmds UNUSED)
4350 {
4351 #ifdef FEAT_AUTOCMD
4352     int		other_buffer = FALSE;
4353 #endif
4354 
4355     if (wp == curwin && !curwin_invalid)	/* nothing to do */
4356 	return;
4357 
4358 #ifdef FEAT_AUTOCMD
4359     if (!curwin_invalid && trigger_leave_autocmds)
4360     {
4361 	/*
4362 	 * Be careful: If autocommands delete the window, return now.
4363 	 */
4364 	if (wp->w_buffer != curbuf)
4365 	{
4366 	    apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4367 	    other_buffer = TRUE;
4368 	    if (!win_valid(wp))
4369 		return;
4370 	}
4371 	apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4372 	if (!win_valid(wp))
4373 	    return;
4374 # ifdef FEAT_EVAL
4375 	/* autocmds may abort script processing */
4376 	if (aborting())
4377 	    return;
4378 # endif
4379     }
4380 #endif
4381 
4382     /* sync undo before leaving the current buffer */
4383     if (undo_sync && curbuf != wp->w_buffer)
4384 	u_sync(FALSE);
4385 
4386     /* Might need to scroll the old window before switching, e.g., when the
4387      * cursor was moved. */
4388     update_topline();
4389 
4390     /* may have to copy the buffer options when 'cpo' contains 'S' */
4391     if (wp->w_buffer != curbuf)
4392 	buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4393     if (!curwin_invalid)
4394     {
4395 	prevwin = curwin;	/* remember for CTRL-W p */
4396 	curwin->w_redr_status = TRUE;
4397     }
4398     curwin = wp;
4399     curbuf = wp->w_buffer;
4400     check_cursor();
4401 #ifdef FEAT_VIRTUALEDIT
4402     if (!virtual_active())
4403 	curwin->w_cursor.coladd = 0;
4404 #endif
4405     changed_line_abv_curs();	/* assume cursor position needs updating */
4406 
4407     if (curwin->w_localdir != NULL)
4408     {
4409 	/* Window has a local directory: Save current directory as global
4410 	 * directory (unless that was done already) and change to the local
4411 	 * directory. */
4412 	if (globaldir == NULL)
4413 	{
4414 	    char_u	cwd[MAXPATHL];
4415 
4416 	    if (mch_dirname(cwd, MAXPATHL) == OK)
4417 		globaldir = vim_strsave(cwd);
4418 	}
4419 	if (mch_chdir((char *)curwin->w_localdir) == 0)
4420 	    shorten_fnames(TRUE);
4421     }
4422     else if (globaldir != NULL)
4423     {
4424 	/* Window doesn't have a local directory and we are not in the global
4425 	 * directory: Change to the global directory. */
4426 	ignored = mch_chdir((char *)globaldir);
4427 	vim_free(globaldir);
4428 	globaldir = NULL;
4429 	shorten_fnames(TRUE);
4430     }
4431 
4432 #ifdef FEAT_AUTOCMD
4433     if (trigger_new_autocmds)
4434 	apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
4435     if (trigger_enter_autocmds)
4436     {
4437 	apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4438 	if (other_buffer)
4439 	    apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4440     }
4441 #endif
4442 
4443 #ifdef FEAT_TITLE
4444     maketitle();
4445 #endif
4446     curwin->w_redr_status = TRUE;
4447     redraw_tabline = TRUE;
4448     if (restart_edit)
4449 	redraw_later(VALID);	/* causes status line redraw */
4450 
4451     /* set window height to desired minimal value */
4452     if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4453 	win_setheight((int)p_wh);
4454     else if (curwin->w_height == 0)
4455 	win_setheight(1);
4456 
4457     /* set window width to desired minimal value */
4458     if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
4459 	win_setwidth((int)p_wiw);
4460 
4461 #ifdef FEAT_MOUSE
4462     setmouse();			/* in case jumped to/from help buffer */
4463 #endif
4464 
4465     /* Change directories when the 'acd' option is set. */
4466     DO_AUTOCHDIR
4467 }
4468 
4469 
4470 /*
4471  * Jump to the first open window that contains buffer "buf", if one exists.
4472  * Returns a pointer to the window found, otherwise NULL.
4473  */
4474     win_T *
4475 buf_jump_open_win(buf_T *buf)
4476 {
4477     win_T	*wp = NULL;
4478 
4479     if (curwin->w_buffer == buf)
4480 	wp = curwin;
4481     else
4482 	FOR_ALL_WINDOWS(wp)
4483 	    if (wp->w_buffer == buf)
4484 		break;
4485     if (wp != NULL)
4486 	win_enter(wp, FALSE);
4487     return wp;
4488 }
4489 
4490 /*
4491  * Jump to the first open window in any tab page that contains buffer "buf",
4492  * if one exists.
4493  * Returns a pointer to the window found, otherwise NULL.
4494  */
4495     win_T *
4496 buf_jump_open_tab(buf_T *buf)
4497 {
4498     win_T	*wp = buf_jump_open_win(buf);
4499     tabpage_T	*tp;
4500 
4501     if (wp != NULL)
4502 	return wp;
4503 
4504     FOR_ALL_TABPAGES(tp)
4505 	if (tp != curtab)
4506 	{
4507 	    for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4508 		if (wp->w_buffer == buf)
4509 		    break;
4510 	    if (wp != NULL)
4511 	    {
4512 		goto_tabpage_win(tp, wp);
4513 		if (curwin != wp)
4514 		    wp = NULL;	/* something went wrong */
4515 		break;
4516 	    }
4517 	}
4518     return wp;
4519 }
4520 
4521 static int last_win_id = LOWEST_WIN_ID - 1;
4522 
4523 /*
4524  * Allocate a window structure and link it in the window list when "hidden" is
4525  * FALSE.
4526  */
4527     static win_T *
4528 win_alloc(win_T *after UNUSED, int hidden UNUSED)
4529 {
4530     win_T	*new_wp;
4531 
4532     /*
4533      * allocate window structure and linesizes arrays
4534      */
4535     new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
4536     if (new_wp == NULL)
4537 	return NULL;
4538 
4539     if (win_alloc_lines(new_wp) == FAIL)
4540     {
4541 	vim_free(new_wp);
4542 	return NULL;
4543     }
4544 
4545     new_wp->w_id = ++last_win_id;
4546 
4547 #ifdef FEAT_EVAL
4548     /* init w: variables */
4549     new_wp->w_vars = dict_alloc();
4550     if (new_wp->w_vars == NULL)
4551     {
4552 	win_free_lsize(new_wp);
4553 	vim_free(new_wp);
4554 	return NULL;
4555     }
4556     init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
4557 #endif
4558 
4559 #ifdef FEAT_AUTOCMD
4560     /* Don't execute autocommands while the window is not properly
4561      * initialized yet.  gui_create_scrollbar() may trigger a FocusGained
4562      * event. */
4563     block_autocmds();
4564 #endif
4565     /*
4566      * link the window in the window list
4567      */
4568     if (!hidden)
4569 	win_append(after, new_wp);
4570     new_wp->w_wincol = 0;
4571     new_wp->w_width = Columns;
4572 
4573     /* position the display and the cursor at the top of the file. */
4574     new_wp->w_topline = 1;
4575 #ifdef FEAT_DIFF
4576     new_wp->w_topfill = 0;
4577 #endif
4578     new_wp->w_botline = 2;
4579     new_wp->w_cursor.lnum = 1;
4580 #ifdef FEAT_SCROLLBIND
4581     new_wp->w_scbind_pos = 1;
4582 #endif
4583 
4584     /* We won't calculate w_fraction until resizing the window */
4585     new_wp->w_fraction = 0;
4586     new_wp->w_prev_fraction_row = -1;
4587 
4588 #ifdef FEAT_GUI
4589     if (gui.in_use)
4590     {
4591 	gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4592 		SBAR_LEFT, new_wp);
4593 	gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4594 		SBAR_RIGHT, new_wp);
4595     }
4596 #endif
4597 #ifdef FEAT_FOLDING
4598     foldInitWin(new_wp);
4599 #endif
4600 #ifdef FEAT_AUTOCMD
4601     unblock_autocmds();
4602 #endif
4603 #ifdef FEAT_SEARCH_EXTRA
4604     new_wp->w_match_head = NULL;
4605     new_wp->w_next_match_id = 4;
4606 #endif
4607     return new_wp;
4608 }
4609 
4610 /*
4611  * Remove window 'wp' from the window list and free the structure.
4612  */
4613     static void
4614 win_free(
4615     win_T	*wp,
4616     tabpage_T	*tp)		/* tab page "win" is in, NULL for current */
4617 {
4618     int		i;
4619     buf_T	*buf;
4620     wininfo_T	*wip;
4621 
4622 #ifdef FEAT_FOLDING
4623     clearFolding(wp);
4624 #endif
4625 
4626     /* reduce the reference count to the argument list. */
4627     alist_unlink(wp->w_alist);
4628 
4629 #ifdef FEAT_AUTOCMD
4630     /* Don't execute autocommands while the window is halfway being deleted.
4631      * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
4632     block_autocmds();
4633 #endif
4634 
4635 #ifdef FEAT_LUA
4636     lua_window_free(wp);
4637 #endif
4638 
4639 #ifdef FEAT_MZSCHEME
4640     mzscheme_window_free(wp);
4641 #endif
4642 
4643 #ifdef FEAT_PERL
4644     perl_win_free(wp);
4645 #endif
4646 
4647 #ifdef FEAT_PYTHON
4648     python_window_free(wp);
4649 #endif
4650 
4651 #ifdef FEAT_PYTHON3
4652     python3_window_free(wp);
4653 #endif
4654 
4655 #ifdef FEAT_TCL
4656     tcl_window_free(wp);
4657 #endif
4658 
4659 #ifdef FEAT_RUBY
4660     ruby_window_free(wp);
4661 #endif
4662 
4663     clear_winopt(&wp->w_onebuf_opt);
4664     clear_winopt(&wp->w_allbuf_opt);
4665 
4666 #ifdef FEAT_EVAL
4667     vars_clear(&wp->w_vars->dv_hashtab);	/* free all w: variables */
4668     hash_init(&wp->w_vars->dv_hashtab);
4669     unref_var_dict(wp->w_vars);
4670 #endif
4671 
4672     {
4673 	tabpage_T	*ttp;
4674 
4675 	if (prevwin == wp)
4676 	    prevwin = NULL;
4677 	FOR_ALL_TABPAGES(ttp)
4678 	    if (ttp->tp_prevwin == wp)
4679 		ttp->tp_prevwin = NULL;
4680     }
4681     win_free_lsize(wp);
4682 
4683     for (i = 0; i < wp->w_tagstacklen; ++i)
4684 	vim_free(wp->w_tagstack[i].tagname);
4685 
4686     vim_free(wp->w_localdir);
4687 
4688     /* Remove the window from the b_wininfo lists, it may happen that the
4689      * freed memory is re-used for another window. */
4690     FOR_ALL_BUFFERS(buf)
4691 	for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4692 	    if (wip->wi_win == wp)
4693 		wip->wi_win = NULL;
4694 
4695 #ifdef FEAT_SEARCH_EXTRA
4696     clear_matches(wp);
4697 #endif
4698 
4699 #ifdef FEAT_JUMPLIST
4700     free_jumplist(wp);
4701 #endif
4702 
4703 #ifdef FEAT_QUICKFIX
4704     qf_free_all(wp);
4705 #endif
4706 
4707 #ifdef FEAT_GUI
4708     if (gui.in_use)
4709     {
4710 	gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4711 	gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4712     }
4713 #endif /* FEAT_GUI */
4714 
4715 #ifdef FEAT_MENU
4716     remove_winbar(wp);
4717 #endif
4718 
4719 #ifdef FEAT_SYN_HL
4720     vim_free(wp->w_p_cc_cols);
4721 #endif
4722 
4723 #ifdef FEAT_AUTOCMD
4724     if (wp != aucmd_win)
4725 #endif
4726 	win_remove(wp, tp);
4727 #ifdef FEAT_AUTOCMD
4728     if (autocmd_busy)
4729     {
4730 	wp->w_next = au_pending_free_win;
4731 	au_pending_free_win = wp;
4732     }
4733     else
4734 #endif
4735 	vim_free(wp);
4736 
4737 #ifdef FEAT_AUTOCMD
4738     unblock_autocmds();
4739 #endif
4740 }
4741 
4742 /*
4743  * Append window "wp" in the window list after window "after".
4744  */
4745     void
4746 win_append(win_T *after, win_T *wp)
4747 {
4748     win_T	*before;
4749 
4750     if (after == NULL)	    /* after NULL is in front of the first */
4751 	before = firstwin;
4752     else
4753 	before = after->w_next;
4754 
4755     wp->w_next = before;
4756     wp->w_prev = after;
4757     if (after == NULL)
4758 	firstwin = wp;
4759     else
4760 	after->w_next = wp;
4761     if (before == NULL)
4762 	lastwin = wp;
4763     else
4764 	before->w_prev = wp;
4765 }
4766 
4767 /*
4768  * Remove a window from the window list.
4769  */
4770     void
4771 win_remove(
4772     win_T	*wp,
4773     tabpage_T	*tp)		/* tab page "win" is in, NULL for current */
4774 {
4775     if (wp->w_prev != NULL)
4776 	wp->w_prev->w_next = wp->w_next;
4777     else if (tp == NULL)
4778 	firstwin = curtab->tp_firstwin = wp->w_next;
4779     else
4780 	tp->tp_firstwin = wp->w_next;
4781 
4782     if (wp->w_next != NULL)
4783 	wp->w_next->w_prev = wp->w_prev;
4784     else if (tp == NULL)
4785 	lastwin = curtab->tp_lastwin = wp->w_prev;
4786     else
4787 	tp->tp_lastwin = wp->w_prev;
4788 }
4789 
4790 /*
4791  * Append frame "frp" in a frame list after frame "after".
4792  */
4793     static void
4794 frame_append(frame_T *after, frame_T *frp)
4795 {
4796     frp->fr_next = after->fr_next;
4797     after->fr_next = frp;
4798     if (frp->fr_next != NULL)
4799 	frp->fr_next->fr_prev = frp;
4800     frp->fr_prev = after;
4801 }
4802 
4803 /*
4804  * Insert frame "frp" in a frame list before frame "before".
4805  */
4806     static void
4807 frame_insert(frame_T *before, frame_T *frp)
4808 {
4809     frp->fr_next = before;
4810     frp->fr_prev = before->fr_prev;
4811     before->fr_prev = frp;
4812     if (frp->fr_prev != NULL)
4813 	frp->fr_prev->fr_next = frp;
4814     else
4815 	frp->fr_parent->fr_child = frp;
4816 }
4817 
4818 /*
4819  * Remove a frame from a frame list.
4820  */
4821     static void
4822 frame_remove(frame_T *frp)
4823 {
4824     if (frp->fr_prev != NULL)
4825 	frp->fr_prev->fr_next = frp->fr_next;
4826     else
4827 	frp->fr_parent->fr_child = frp->fr_next;
4828     if (frp->fr_next != NULL)
4829 	frp->fr_next->fr_prev = frp->fr_prev;
4830 }
4831 
4832 /*
4833  * Allocate w_lines[] for window "wp".
4834  * Return FAIL for failure, OK for success.
4835  */
4836     int
4837 win_alloc_lines(win_T *wp)
4838 {
4839     wp->w_lines_valid = 0;
4840     wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
4841     if (wp->w_lines == NULL)
4842 	return FAIL;
4843     return OK;
4844 }
4845 
4846 /*
4847  * free lsize arrays for a window
4848  */
4849     void
4850 win_free_lsize(win_T *wp)
4851 {
4852     /* TODO: why would wp be NULL here? */
4853     if (wp != NULL)
4854     {
4855 	vim_free(wp->w_lines);
4856 	wp->w_lines = NULL;
4857     }
4858 }
4859 
4860 /*
4861  * Called from win_new_shellsize() after Rows changed.
4862  * This only does the current tab page, others must be done when made active.
4863  */
4864     void
4865 shell_new_rows(void)
4866 {
4867     int		h = (int)ROWS_AVAIL;
4868 
4869     if (firstwin == NULL)	/* not initialized yet */
4870 	return;
4871     if (h < frame_minheight(topframe, NULL))
4872 	h = frame_minheight(topframe, NULL);
4873 
4874     /* First try setting the heights of windows with 'winfixheight'.  If
4875      * that doesn't result in the right height, forget about that option. */
4876     frame_new_height(topframe, h, FALSE, TRUE);
4877     if (!frame_check_height(topframe, h))
4878 	frame_new_height(topframe, h, FALSE, FALSE);
4879 
4880     (void)win_comp_pos();		/* recompute w_winrow and w_wincol */
4881     compute_cmdrow();
4882     curtab->tp_ch_used = p_ch;
4883 
4884 #if 0
4885     /* Disabled: don't want making the screen smaller make a window larger. */
4886     if (p_ea)
4887 	win_equal(curwin, FALSE, 'v');
4888 #endif
4889 }
4890 
4891 /*
4892  * Called from win_new_shellsize() after Columns changed.
4893  */
4894     void
4895 shell_new_columns(void)
4896 {
4897     if (firstwin == NULL)	/* not initialized yet */
4898 	return;
4899 
4900     /* First try setting the widths of windows with 'winfixwidth'.  If that
4901      * doesn't result in the right width, forget about that option. */
4902     frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4903     if (!frame_check_width(topframe, Columns))
4904 	frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4905 
4906     (void)win_comp_pos();		/* recompute w_winrow and w_wincol */
4907 #if 0
4908     /* Disabled: don't want making the screen smaller make a window larger. */
4909     if (p_ea)
4910 	win_equal(curwin, FALSE, 'h');
4911 #endif
4912 }
4913 
4914 #if defined(FEAT_CMDWIN) || defined(PROTO)
4915 /*
4916  * Save the size of all windows in "gap".
4917  */
4918     void
4919 win_size_save(garray_T *gap)
4920 
4921 {
4922     win_T	*wp;
4923 
4924     ga_init2(gap, (int)sizeof(int), 1);
4925     if (ga_grow(gap, win_count() * 2) == OK)
4926 	FOR_ALL_WINDOWS(wp)
4927 	{
4928 	    ((int *)gap->ga_data)[gap->ga_len++] =
4929 					       wp->w_width + wp->w_vsep_width;
4930 	    ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4931 	}
4932 }
4933 
4934 /*
4935  * Restore window sizes, but only if the number of windows is still the same.
4936  * Does not free the growarray.
4937  */
4938     void
4939 win_size_restore(garray_T *gap)
4940 {
4941     win_T	*wp;
4942     int		i, j;
4943 
4944     if (win_count() * 2 == gap->ga_len)
4945     {
4946 	/* The order matters, because frames contain other frames, but it's
4947 	 * difficult to get right. The easy way out is to do it twice. */
4948 	for (j = 0; j < 2; ++j)
4949 	{
4950 	    i = 0;
4951 	    FOR_ALL_WINDOWS(wp)
4952 	    {
4953 		frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4954 		win_setheight_win(((int *)gap->ga_data)[i++], wp);
4955 	    }
4956 	}
4957 	/* recompute the window positions */
4958 	(void)win_comp_pos();
4959     }
4960 }
4961 #endif /* FEAT_CMDWIN */
4962 
4963 /*
4964  * Update the position for all windows, using the width and height of the
4965  * frames.
4966  * Returns the row just after the last window.
4967  */
4968     int
4969 win_comp_pos(void)
4970 {
4971     int		row = tabline_height();
4972     int		col = 0;
4973 
4974     frame_comp_pos(topframe, &row, &col);
4975     return row;
4976 }
4977 
4978 /*
4979  * Update the position of the windows in frame "topfrp", using the width and
4980  * height of the frames.
4981  * "*row" and "*col" are the top-left position of the frame.  They are updated
4982  * to the bottom-right position plus one.
4983  */
4984     static void
4985 frame_comp_pos(frame_T *topfrp, int *row, int *col)
4986 {
4987     win_T	*wp;
4988     frame_T	*frp;
4989     int		startcol;
4990     int		startrow;
4991 
4992     wp = topfrp->fr_win;
4993     if (wp != NULL)
4994     {
4995 	if (wp->w_winrow != *row || wp->w_wincol != *col)
4996 	{
4997 	    /* position changed, redraw */
4998 	    wp->w_winrow = *row;
4999 	    wp->w_wincol = *col;
5000 	    redraw_win_later(wp, NOT_VALID);
5001 	    wp->w_redr_status = TRUE;
5002 	}
5003 	*row += wp->w_height + wp->w_status_height;
5004 	*col += wp->w_width + wp->w_vsep_width;
5005     }
5006     else
5007     {
5008 	startrow = *row;
5009 	startcol = *col;
5010 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5011 	{
5012 	    if (topfrp->fr_layout == FR_ROW)
5013 		*row = startrow;	/* all frames are at the same row */
5014 	    else
5015 		*col = startcol;	/* all frames are at the same col */
5016 	    frame_comp_pos(frp, row, col);
5017 	}
5018     }
5019 }
5020 
5021 /*
5022  * Set current window height and take care of repositioning other windows to
5023  * fit around it.
5024  */
5025     void
5026 win_setheight(int height)
5027 {
5028     win_setheight_win(height, curwin);
5029 }
5030 
5031 /*
5032  * Set the window height of window "win" and take care of repositioning other
5033  * windows to fit around it.
5034  */
5035     void
5036 win_setheight_win(int height, win_T *win)
5037 {
5038     int		row;
5039 
5040     if (win == curwin)
5041     {
5042 	/* Always keep current window at least one line high, even when
5043 	 * 'winminheight' is zero. */
5044 	if (height < p_wmh)
5045 	    height = p_wmh;
5046 	if (height == 0)
5047 	    height = 1;
5048     }
5049 
5050     frame_setheight(win->w_frame, height + win->w_status_height);
5051 
5052     /* recompute the window positions */
5053     row = win_comp_pos();
5054 
5055     /*
5056      * If there is extra space created between the last window and the command
5057      * line, clear it.
5058      */
5059     if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5060 	screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5061     cmdline_row = row;
5062     msg_row = row;
5063     msg_col = 0;
5064 
5065     redraw_all_later(NOT_VALID);
5066 }
5067 
5068 /*
5069  * Set the height of a frame to "height" and take care that all frames and
5070  * windows inside it are resized.  Also resize frames on the left and right if
5071  * the are in the same FR_ROW frame.
5072  *
5073  * Strategy:
5074  * If the frame is part of a FR_COL frame, try fitting the frame in that
5075  * frame.  If that doesn't work (the FR_COL frame is too small), recursively
5076  * go to containing frames to resize them and make room.
5077  * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5078  * Check for the minimal height of the FR_ROW frame.
5079  * At the top level we can also use change the command line height.
5080  */
5081     static void
5082 frame_setheight(frame_T *curfrp, int height)
5083 {
5084     int		room;		/* total number of lines available */
5085     int		take;		/* number of lines taken from other windows */
5086     int		room_cmdline;	/* lines available from cmdline */
5087     int		run;
5088     frame_T	*frp;
5089     int		h;
5090     int		room_reserved;
5091 
5092     /* If the height already is the desired value, nothing to do. */
5093     if (curfrp->fr_height == height)
5094 	return;
5095 
5096     if (curfrp->fr_parent == NULL)
5097     {
5098 	/* topframe: can only change the command line */
5099 	if (height > ROWS_AVAIL)
5100 	    height = ROWS_AVAIL;
5101 	if (height > 0)
5102 	    frame_new_height(curfrp, height, FALSE, FALSE);
5103     }
5104     else if (curfrp->fr_parent->fr_layout == FR_ROW)
5105     {
5106 	/* Row of frames: Also need to resize frames left and right of this
5107 	 * one.  First check for the minimal height of these. */
5108 	h = frame_minheight(curfrp->fr_parent, NULL);
5109 	if (height < h)
5110 	    height = h;
5111 	frame_setheight(curfrp->fr_parent, height);
5112     }
5113     else
5114     {
5115 	/*
5116 	 * Column of frames: try to change only frames in this column.
5117 	 */
5118 	/*
5119 	 * Do this twice:
5120 	 * 1: compute room available, if it's not enough try resizing the
5121 	 *    containing frame.
5122 	 * 2: compute the room available and adjust the height to it.
5123 	 * Try not to reduce the height of a window with 'winfixheight' set.
5124 	 */
5125 	for (run = 1; run <= 2; ++run)
5126 	{
5127 	    room = 0;
5128 	    room_reserved = 0;
5129 	    for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5130 							   frp = frp->fr_next)
5131 	    {
5132 		if (frp != curfrp
5133 			&& frp->fr_win != NULL
5134 			&& frp->fr_win->w_p_wfh)
5135 		    room_reserved += frp->fr_height;
5136 		room += frp->fr_height;
5137 		if (frp != curfrp)
5138 		    room -= frame_minheight(frp, NULL);
5139 	    }
5140 	    if (curfrp->fr_width != Columns)
5141 		room_cmdline = 0;
5142 	    else
5143 	    {
5144 		room_cmdline = Rows - p_ch - (lastwin->w_winrow
5145 			       + lastwin->w_height + lastwin->w_status_height);
5146 		if (room_cmdline < 0)
5147 		    room_cmdline = 0;
5148 	    }
5149 
5150 	    if (height <= room + room_cmdline)
5151 		break;
5152 	    if (run == 2 || curfrp->fr_width == Columns)
5153 	    {
5154 		if (height > room + room_cmdline)
5155 		    height = room + room_cmdline;
5156 		break;
5157 	    }
5158 	    frame_setheight(curfrp->fr_parent, height
5159 		+ frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5160 	}
5161 
5162 	/*
5163 	 * Compute the number of lines we will take from others frames (can be
5164 	 * negative!).
5165 	 */
5166 	take = height - curfrp->fr_height;
5167 
5168 	/* If there is not enough room, also reduce the height of a window
5169 	 * with 'winfixheight' set. */
5170 	if (height > room + room_cmdline - room_reserved)
5171 	    room_reserved = room + room_cmdline - height;
5172 	/* If there is only a 'winfixheight' window and making the
5173 	 * window smaller, need to make the other window taller. */
5174 	if (take < 0 && room - curfrp->fr_height < room_reserved)
5175 	    room_reserved = 0;
5176 
5177 	if (take > 0 && room_cmdline > 0)
5178 	{
5179 	    /* use lines from cmdline first */
5180 	    if (take < room_cmdline)
5181 		room_cmdline = take;
5182 	    take -= room_cmdline;
5183 	    topframe->fr_height += room_cmdline;
5184 	}
5185 
5186 	/*
5187 	 * set the current frame to the new height
5188 	 */
5189 	frame_new_height(curfrp, height, FALSE, FALSE);
5190 
5191 	/*
5192 	 * First take lines from the frames after the current frame.  If
5193 	 * that is not enough, takes lines from frames above the current
5194 	 * frame.
5195 	 */
5196 	for (run = 0; run < 2; ++run)
5197 	{
5198 	    if (run == 0)
5199 		frp = curfrp->fr_next;	/* 1st run: start with next window */
5200 	    else
5201 		frp = curfrp->fr_prev;	/* 2nd run: start with prev window */
5202 	    while (frp != NULL && take != 0)
5203 	    {
5204 		h = frame_minheight(frp, NULL);
5205 		if (room_reserved > 0
5206 			&& frp->fr_win != NULL
5207 			&& frp->fr_win->w_p_wfh)
5208 		{
5209 		    if (room_reserved >= frp->fr_height)
5210 			room_reserved -= frp->fr_height;
5211 		    else
5212 		    {
5213 			if (frp->fr_height - room_reserved > take)
5214 			    room_reserved = frp->fr_height - take;
5215 			take -= frp->fr_height - room_reserved;
5216 			frame_new_height(frp, room_reserved, FALSE, FALSE);
5217 			room_reserved = 0;
5218 		    }
5219 		}
5220 		else
5221 		{
5222 		    if (frp->fr_height - take < h)
5223 		    {
5224 			take -= frp->fr_height - h;
5225 			frame_new_height(frp, h, FALSE, FALSE);
5226 		    }
5227 		    else
5228 		    {
5229 			frame_new_height(frp, frp->fr_height - take,
5230 								FALSE, FALSE);
5231 			take = 0;
5232 		    }
5233 		}
5234 		if (run == 0)
5235 		    frp = frp->fr_next;
5236 		else
5237 		    frp = frp->fr_prev;
5238 	    }
5239 	}
5240     }
5241 }
5242 
5243 /*
5244  * Set current window width and take care of repositioning other windows to
5245  * fit around it.
5246  */
5247     void
5248 win_setwidth(int width)
5249 {
5250     win_setwidth_win(width, curwin);
5251 }
5252 
5253     void
5254 win_setwidth_win(int width, win_T *wp)
5255 {
5256     /* Always keep current window at least one column wide, even when
5257      * 'winminwidth' is zero. */
5258     if (wp == curwin)
5259     {
5260 	if (width < p_wmw)
5261 	    width = p_wmw;
5262 	if (width == 0)
5263 	    width = 1;
5264     }
5265 
5266     frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5267 
5268     /* recompute the window positions */
5269     (void)win_comp_pos();
5270 
5271     redraw_all_later(NOT_VALID);
5272 }
5273 
5274 /*
5275  * Set the width of a frame to "width" and take care that all frames and
5276  * windows inside it are resized.  Also resize frames above and below if the
5277  * are in the same FR_ROW frame.
5278  *
5279  * Strategy is similar to frame_setheight().
5280  */
5281     static void
5282 frame_setwidth(frame_T *curfrp, int width)
5283 {
5284     int		room;		/* total number of lines available */
5285     int		take;		/* number of lines taken from other windows */
5286     int		run;
5287     frame_T	*frp;
5288     int		w;
5289     int		room_reserved;
5290 
5291     /* If the width already is the desired value, nothing to do. */
5292     if (curfrp->fr_width == width)
5293 	return;
5294 
5295     if (curfrp->fr_parent == NULL)
5296 	/* topframe: can't change width */
5297 	return;
5298 
5299     if (curfrp->fr_parent->fr_layout == FR_COL)
5300     {
5301 	/* Column of frames: Also need to resize frames above and below of
5302 	 * this one.  First check for the minimal width of these. */
5303 	w = frame_minwidth(curfrp->fr_parent, NULL);
5304 	if (width < w)
5305 	    width = w;
5306 	frame_setwidth(curfrp->fr_parent, width);
5307     }
5308     else
5309     {
5310 	/*
5311 	 * Row of frames: try to change only frames in this row.
5312 	 *
5313 	 * Do this twice:
5314 	 * 1: compute room available, if it's not enough try resizing the
5315 	 *    containing frame.
5316 	 * 2: compute the room available and adjust the width to it.
5317 	 */
5318 	for (run = 1; run <= 2; ++run)
5319 	{
5320 	    room = 0;
5321 	    room_reserved = 0;
5322 	    for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5323 							   frp = frp->fr_next)
5324 	    {
5325 		if (frp != curfrp
5326 			&& frp->fr_win != NULL
5327 			&& frp->fr_win->w_p_wfw)
5328 		    room_reserved += frp->fr_width;
5329 		room += frp->fr_width;
5330 		if (frp != curfrp)
5331 		    room -= frame_minwidth(frp, NULL);
5332 	    }
5333 
5334 	    if (width <= room)
5335 		break;
5336 	    if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
5337 	    {
5338 		if (width > room)
5339 		    width = room;
5340 		break;
5341 	    }
5342 	    frame_setwidth(curfrp->fr_parent, width
5343 		 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5344 	}
5345 
5346 	/*
5347 	 * Compute the number of lines we will take from others frames (can be
5348 	 * negative!).
5349 	 */
5350 	take = width - curfrp->fr_width;
5351 
5352 	/* If there is not enough room, also reduce the width of a window
5353 	 * with 'winfixwidth' set. */
5354 	if (width > room - room_reserved)
5355 	    room_reserved = room - width;
5356 	/* If there is only a 'winfixwidth' window and making the
5357 	 * window smaller, need to make the other window narrower. */
5358 	if (take < 0 && room - curfrp->fr_width < room_reserved)
5359 	    room_reserved = 0;
5360 
5361 	/*
5362 	 * set the current frame to the new width
5363 	 */
5364 	frame_new_width(curfrp, width, FALSE, FALSE);
5365 
5366 	/*
5367 	 * First take lines from the frames right of the current frame.  If
5368 	 * that is not enough, takes lines from frames left of the current
5369 	 * frame.
5370 	 */
5371 	for (run = 0; run < 2; ++run)
5372 	{
5373 	    if (run == 0)
5374 		frp = curfrp->fr_next;	/* 1st run: start with next window */
5375 	    else
5376 		frp = curfrp->fr_prev;	/* 2nd run: start with prev window */
5377 	    while (frp != NULL && take != 0)
5378 	    {
5379 		w = frame_minwidth(frp, NULL);
5380 		if (room_reserved > 0
5381 			&& frp->fr_win != NULL
5382 			&& frp->fr_win->w_p_wfw)
5383 		{
5384 		    if (room_reserved >= frp->fr_width)
5385 			room_reserved -= frp->fr_width;
5386 		    else
5387 		    {
5388 			if (frp->fr_width - room_reserved > take)
5389 			    room_reserved = frp->fr_width - take;
5390 			take -= frp->fr_width - room_reserved;
5391 			frame_new_width(frp, room_reserved, FALSE, FALSE);
5392 			room_reserved = 0;
5393 		    }
5394 		}
5395 		else
5396 		{
5397 		    if (frp->fr_width - take < w)
5398 		    {
5399 			take -= frp->fr_width - w;
5400 			frame_new_width(frp, w, FALSE, FALSE);
5401 		    }
5402 		    else
5403 		    {
5404 			frame_new_width(frp, frp->fr_width - take,
5405 								FALSE, FALSE);
5406 			take = 0;
5407 		    }
5408 		}
5409 		if (run == 0)
5410 		    frp = frp->fr_next;
5411 		else
5412 		    frp = frp->fr_prev;
5413 	    }
5414 	}
5415     }
5416 }
5417 
5418 /*
5419  * Check 'winminheight' for a valid value.
5420  */
5421     void
5422 win_setminheight(void)
5423 {
5424     int		room;
5425     int		first = TRUE;
5426     win_T	*wp;
5427 
5428     /* loop until there is a 'winminheight' that is possible */
5429     while (p_wmh > 0)
5430     {
5431 	/* TODO: handle vertical splits */
5432 	room = -p_wh;
5433 	FOR_ALL_WINDOWS(wp)
5434 	    room += wp->w_height - p_wmh;
5435 	if (room >= 0)
5436 	    break;
5437 	--p_wmh;
5438 	if (first)
5439 	{
5440 	    EMSG(_(e_noroom));
5441 	    first = FALSE;
5442 	}
5443     }
5444 }
5445 
5446 #if defined(FEAT_MOUSE) || defined(PROTO)
5447 
5448 /*
5449  * Status line of dragwin is dragged "offset" lines down (negative is up).
5450  */
5451     void
5452 win_drag_status_line(win_T *dragwin, int offset)
5453 {
5454     frame_T	*curfr;
5455     frame_T	*fr;
5456     int		room;
5457     int		row;
5458     int		up;	/* if TRUE, drag status line up, otherwise down */
5459     int		n;
5460 
5461     fr = dragwin->w_frame;
5462     curfr = fr;
5463     if (fr != topframe)		/* more than one window */
5464     {
5465 	fr = fr->fr_parent;
5466 	/* When the parent frame is not a column of frames, its parent should
5467 	 * be. */
5468 	if (fr->fr_layout != FR_COL)
5469 	{
5470 	    curfr = fr;
5471 	    if (fr != topframe)	/* only a row of windows, may drag statusline */
5472 		fr = fr->fr_parent;
5473 	}
5474     }
5475 
5476     /* If this is the last frame in a column, may want to resize the parent
5477      * frame instead (go two up to skip a row of frames). */
5478     while (curfr != topframe && curfr->fr_next == NULL)
5479     {
5480 	if (fr != topframe)
5481 	    fr = fr->fr_parent;
5482 	curfr = fr;
5483 	if (fr != topframe)
5484 	    fr = fr->fr_parent;
5485     }
5486 
5487     if (offset < 0) /* drag up */
5488     {
5489 	up = TRUE;
5490 	offset = -offset;
5491 	/* sum up the room of the current frame and above it */
5492 	if (fr == curfr)
5493 	{
5494 	    /* only one window */
5495 	    room = fr->fr_height - frame_minheight(fr, NULL);
5496 	}
5497 	else
5498 	{
5499 	    room = 0;
5500 	    for (fr = fr->fr_child; ; fr = fr->fr_next)
5501 	    {
5502 		room += fr->fr_height - frame_minheight(fr, NULL);
5503 		if (fr == curfr)
5504 		    break;
5505 	    }
5506 	}
5507 	fr = curfr->fr_next;		/* put fr at frame that grows */
5508     }
5509     else    /* drag down */
5510     {
5511 	up = FALSE;
5512 	/*
5513 	 * Only dragging the last status line can reduce p_ch.
5514 	 */
5515 	room = Rows - cmdline_row;
5516 	if (curfr->fr_next == NULL)
5517 	    room -= 1;
5518 	else
5519 	    room -= p_ch;
5520 	if (room < 0)
5521 	    room = 0;
5522 	/* sum up the room of frames below of the current one */
5523 	for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5524 	    room += fr->fr_height - frame_minheight(fr, NULL);
5525 	fr = curfr;			/* put fr at window that grows */
5526     }
5527 
5528     if (room < offset)		/* Not enough room */
5529 	offset = room;		/* Move as far as we can */
5530     if (offset <= 0)
5531 	return;
5532 
5533     /*
5534      * Grow frame fr by "offset" lines.
5535      * Doesn't happen when dragging the last status line up.
5536      */
5537     if (fr != NULL)
5538 	frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5539 
5540     if (up)
5541 	fr = curfr;		/* current frame gets smaller */
5542     else
5543 	fr = curfr->fr_next;	/* next frame gets smaller */
5544 
5545     /*
5546      * Now make the other frames smaller.
5547      */
5548     while (fr != NULL && offset > 0)
5549     {
5550 	n = frame_minheight(fr, NULL);
5551 	if (fr->fr_height - offset <= n)
5552 	{
5553 	    offset -= fr->fr_height - n;
5554 	    frame_new_height(fr, n, !up, FALSE);
5555 	}
5556 	else
5557 	{
5558 	    frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5559 	    break;
5560 	}
5561 	if (up)
5562 	    fr = fr->fr_prev;
5563 	else
5564 	    fr = fr->fr_next;
5565     }
5566     row = win_comp_pos();
5567     screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5568     cmdline_row = row;
5569     p_ch = Rows - cmdline_row;
5570     if (p_ch < 1)
5571 	p_ch = 1;
5572     curtab->tp_ch_used = p_ch;
5573     redraw_all_later(SOME_VALID);
5574     showmode();
5575 }
5576 
5577 /*
5578  * Separator line of dragwin is dragged "offset" lines right (negative is left).
5579  */
5580     void
5581 win_drag_vsep_line(win_T *dragwin, int offset)
5582 {
5583     frame_T	*curfr;
5584     frame_T	*fr;
5585     int		room;
5586     int		left;	/* if TRUE, drag separator line left, otherwise right */
5587     int		n;
5588 
5589     fr = dragwin->w_frame;
5590     if (fr == topframe)		/* only one window (cannot happen?) */
5591 	return;
5592     curfr = fr;
5593     fr = fr->fr_parent;
5594     /* When the parent frame is not a row of frames, its parent should be. */
5595     if (fr->fr_layout != FR_ROW)
5596     {
5597 	if (fr == topframe)	/* only a column of windows (cannot happen?) */
5598 	    return;
5599 	curfr = fr;
5600 	fr = fr->fr_parent;
5601     }
5602 
5603     /* If this is the last frame in a row, may want to resize a parent
5604      * frame instead. */
5605     while (curfr->fr_next == NULL)
5606     {
5607 	if (fr == topframe)
5608 	    break;
5609 	curfr = fr;
5610 	fr = fr->fr_parent;
5611 	if (fr != topframe)
5612 	{
5613 	    curfr = fr;
5614 	    fr = fr->fr_parent;
5615 	}
5616     }
5617 
5618     if (offset < 0) /* drag left */
5619     {
5620 	left = TRUE;
5621 	offset = -offset;
5622 	/* sum up the room of the current frame and left of it */
5623 	room = 0;
5624 	for (fr = fr->fr_child; ; fr = fr->fr_next)
5625 	{
5626 	    room += fr->fr_width - frame_minwidth(fr, NULL);
5627 	    if (fr == curfr)
5628 		break;
5629 	}
5630 	fr = curfr->fr_next;		/* put fr at frame that grows */
5631     }
5632     else    /* drag right */
5633     {
5634 	left = FALSE;
5635 	/* sum up the room of frames right of the current one */
5636 	room = 0;
5637 	for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5638 	    room += fr->fr_width - frame_minwidth(fr, NULL);
5639 	fr = curfr;			/* put fr at window that grows */
5640     }
5641 
5642     if (room < offset)		/* Not enough room */
5643 	offset = room;		/* Move as far as we can */
5644     if (offset <= 0)		/* No room at all, quit. */
5645 	return;
5646     if (fr == NULL)
5647 	return;			/* Safety check, should not happen. */
5648 
5649     /* grow frame fr by offset lines */
5650     frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5651 
5652     /* shrink other frames: current and at the left or at the right */
5653     if (left)
5654 	fr = curfr;		/* current frame gets smaller */
5655     else
5656 	fr = curfr->fr_next;	/* next frame gets smaller */
5657 
5658     while (fr != NULL && offset > 0)
5659     {
5660 	n = frame_minwidth(fr, NULL);
5661 	if (fr->fr_width - offset <= n)
5662 	{
5663 	    offset -= fr->fr_width - n;
5664 	    frame_new_width(fr, n, !left, FALSE);
5665 	}
5666 	else
5667 	{
5668 	    frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5669 	    break;
5670 	}
5671 	if (left)
5672 	    fr = fr->fr_prev;
5673 	else
5674 	    fr = fr->fr_next;
5675     }
5676     (void)win_comp_pos();
5677     redraw_all_later(NOT_VALID);
5678 }
5679 #endif /* FEAT_MOUSE */
5680 
5681 #define FRACTION_MULT	16384L
5682 
5683 /*
5684  * Set wp->w_fraction for the current w_wrow and w_height.
5685  */
5686     void
5687 set_fraction(win_T *wp)
5688 {
5689     wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5690 				    + wp->w_height / 2) / (long)wp->w_height;
5691 }
5692 
5693 /*
5694  * Set the height of a window.
5695  * "height" excludes any window toolbar.
5696  * This takes care of the things inside the window, not what happens to the
5697  * window position, the frame or to other windows.
5698  */
5699     void
5700 win_new_height(win_T *wp, int height)
5701 {
5702     int		prev_height = wp->w_height;
5703 
5704     /* Don't want a negative height.  Happens when splitting a tiny window.
5705      * Will equalize heights soon to fix it. */
5706     if (height < 0)
5707 	height = 0;
5708     if (wp->w_height == height)
5709 	return;	    /* nothing to do */
5710 
5711     if (wp->w_height > 0)
5712     {
5713 	if (wp == curwin)
5714 	    /* w_wrow needs to be valid. When setting 'laststatus' this may
5715 	     * call win_new_height() recursively. */
5716 	    validate_cursor();
5717 	if (wp->w_height != prev_height)
5718 	    return;  /* Recursive call already changed the size, bail out here
5719 			to avoid the following to mess things up. */
5720 	if (wp->w_wrow != wp->w_prev_fraction_row)
5721 	    set_fraction(wp);
5722     }
5723 
5724     wp->w_height = height;
5725     wp->w_skipcol = 0;
5726 
5727     /* There is no point in adjusting the scroll position when exiting.  Some
5728      * values might be invalid. */
5729     if (!exiting)
5730 	scroll_to_fraction(wp, prev_height);
5731 }
5732 
5733     void
5734 scroll_to_fraction(win_T *wp, int prev_height)
5735 {
5736     linenr_T	lnum;
5737     int		sline, line_size;
5738     int		height = wp->w_height;
5739 
5740     /* Don't change w_topline when height is zero.  Don't set w_topline when
5741      * 'scrollbind' is set and this isn't the current window. */
5742     if (height > 0
5743 #ifdef FEAT_SCROLLBIND
5744 	    && (!wp->w_p_scb || wp == curwin)
5745 #endif
5746        )
5747     {
5748 	/*
5749 	 * Find a value for w_topline that shows the cursor at the same
5750 	 * relative position in the window as before (more or less).
5751 	 */
5752 	lnum = wp->w_cursor.lnum;
5753 	if (lnum < 1)		/* can happen when starting up */
5754 	    lnum = 1;
5755 	wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5756 					 + FRACTION_MULT / 2) / FRACTION_MULT;
5757 	line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5758 	sline = wp->w_wrow - line_size;
5759 
5760 	if (sline >= 0)
5761 	{
5762 	    /* Make sure the whole cursor line is visible, if possible. */
5763 	    int rows = plines_win(wp, lnum, FALSE);
5764 
5765 	    if (sline > wp->w_height - rows)
5766 	    {
5767 		sline = wp->w_height - rows;
5768 		wp->w_wrow -= rows - line_size;
5769 	    }
5770 	}
5771 
5772 	if (sline < 0)
5773 	{
5774 	    /*
5775 	     * Cursor line would go off top of screen if w_wrow was this high.
5776 	     * Make cursor line the first line in the window.  If not enough
5777 	     * room use w_skipcol;
5778 	     */
5779 	    wp->w_wrow = line_size;
5780 	    if (wp->w_wrow >= wp->w_height
5781 				       && (wp->w_width - win_col_off(wp)) > 0)
5782 	    {
5783 		wp->w_skipcol += wp->w_width - win_col_off(wp);
5784 		--wp->w_wrow;
5785 		while (wp->w_wrow >= wp->w_height)
5786 		{
5787 		    wp->w_skipcol += wp->w_width - win_col_off(wp)
5788 							   + win_col_off2(wp);
5789 		    --wp->w_wrow;
5790 		}
5791 	    }
5792 	    set_topline(wp, lnum);
5793 	}
5794 	else if (sline > 0)
5795 	{
5796 	    while (sline > 0 && lnum > 1)
5797 	    {
5798 #ifdef FEAT_FOLDING
5799 		hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5800 		if (lnum == 1)
5801 		{
5802 		    /* first line in buffer is folded */
5803 		    line_size = 1;
5804 		    --sline;
5805 		    break;
5806 		}
5807 #endif
5808 		--lnum;
5809 #ifdef FEAT_DIFF
5810 		if (lnum == wp->w_topline)
5811 		    line_size = plines_win_nofill(wp, lnum, TRUE)
5812 							      + wp->w_topfill;
5813 		else
5814 #endif
5815 		    line_size = plines_win(wp, lnum, TRUE);
5816 		sline -= line_size;
5817 	    }
5818 
5819 	    if (sline < 0)
5820 	    {
5821 		/*
5822 		 * Line we want at top would go off top of screen.  Use next
5823 		 * line instead.
5824 		 */
5825 #ifdef FEAT_FOLDING
5826 		hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5827 #endif
5828 		lnum++;
5829 		wp->w_wrow -= line_size + sline;
5830 	    }
5831 	    else if (sline > 0)
5832 	    {
5833 		/* First line of file reached, use that as topline. */
5834 		lnum = 1;
5835 		wp->w_wrow -= sline;
5836 	    }
5837 
5838 	    set_topline(wp, lnum);
5839 	}
5840     }
5841 
5842     if (wp == curwin)
5843     {
5844 	if (p_so)
5845 	    update_topline();
5846 	curs_columns(FALSE);	/* validate w_wrow */
5847     }
5848     if (prev_height > 0)
5849 	wp->w_prev_fraction_row = wp->w_wrow;
5850 
5851     win_comp_scroll(wp);
5852     redraw_win_later(wp, SOME_VALID);
5853     wp->w_redr_status = TRUE;
5854     invalidate_botline_win(wp);
5855 }
5856 
5857 /*
5858  * Set the width of a window.
5859  */
5860     void
5861 win_new_width(win_T *wp, int width)
5862 {
5863     wp->w_width = width;
5864     wp->w_lines_valid = 0;
5865     changed_line_abv_curs_win(wp);
5866     invalidate_botline_win(wp);
5867     if (wp == curwin)
5868     {
5869 	update_topline();
5870 	curs_columns(TRUE);	/* validate w_wrow */
5871     }
5872     redraw_win_later(wp, NOT_VALID);
5873     wp->w_redr_status = TRUE;
5874 }
5875 
5876     void
5877 win_comp_scroll(win_T *wp)
5878 {
5879     wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5880     if (wp->w_p_scr == 0)
5881 	wp->w_p_scr = 1;
5882 }
5883 
5884 /*
5885  * command_height: called whenever p_ch has been changed
5886  */
5887     void
5888 command_height(void)
5889 {
5890     int		h;
5891     frame_T	*frp;
5892     int		old_p_ch = curtab->tp_ch_used;
5893 
5894     /* Use the value of p_ch that we remembered.  This is needed for when the
5895      * GUI starts up, we can't be sure in what order things happen.  And when
5896      * p_ch was changed in another tab page. */
5897     curtab->tp_ch_used = p_ch;
5898 
5899     /* Find bottom frame with width of screen. */
5900     frp = lastwin->w_frame;
5901     while (frp->fr_width != Columns && frp->fr_parent != NULL)
5902 	frp = frp->fr_parent;
5903 
5904     /* Avoid changing the height of a window with 'winfixheight' set. */
5905     while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5906 						      && frp->fr_win->w_p_wfh)
5907 	frp = frp->fr_prev;
5908 
5909     if (starting != NO_SCREEN)
5910     {
5911 	cmdline_row = Rows - p_ch;
5912 
5913 	if (p_ch > old_p_ch)		    /* p_ch got bigger */
5914 	{
5915 	    while (p_ch > old_p_ch)
5916 	    {
5917 		if (frp == NULL)
5918 		{
5919 		    EMSG(_(e_noroom));
5920 		    p_ch = old_p_ch;
5921 		    curtab->tp_ch_used = p_ch;
5922 		    cmdline_row = Rows - p_ch;
5923 		    break;
5924 		}
5925 		h = frp->fr_height - frame_minheight(frp, NULL);
5926 		if (h > p_ch - old_p_ch)
5927 		    h = p_ch - old_p_ch;
5928 		old_p_ch += h;
5929 		frame_add_height(frp, -h);
5930 		frp = frp->fr_prev;
5931 	    }
5932 
5933 	    /* Recompute window positions. */
5934 	    (void)win_comp_pos();
5935 
5936 	    /* clear the lines added to cmdline */
5937 	    if (full_screen)
5938 		screen_fill((int)(cmdline_row), (int)Rows, 0,
5939 						   (int)Columns, ' ', ' ', 0);
5940 	    msg_row = cmdline_row;
5941 	    redraw_cmdline = TRUE;
5942 	    return;
5943 	}
5944 
5945 	if (msg_row < cmdline_row)
5946 	    msg_row = cmdline_row;
5947 	redraw_cmdline = TRUE;
5948     }
5949     frame_add_height(frp, (int)(old_p_ch - p_ch));
5950 
5951     /* Recompute window positions. */
5952     if (frp != lastwin->w_frame)
5953 	(void)win_comp_pos();
5954 }
5955 
5956 /*
5957  * Resize frame "frp" to be "n" lines higher (negative for less high).
5958  * Also resize the frames it is contained in.
5959  */
5960     static void
5961 frame_add_height(frame_T *frp, int n)
5962 {
5963     frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5964     for (;;)
5965     {
5966 	frp = frp->fr_parent;
5967 	if (frp == NULL)
5968 	    break;
5969 	frp->fr_height += n;
5970     }
5971 }
5972 
5973 /*
5974  * Add or remove a status line for the bottom window(s), according to the
5975  * value of 'laststatus'.
5976  */
5977     void
5978 last_status(
5979     int		morewin)	/* pretend there are two or more windows */
5980 {
5981     /* Don't make a difference between horizontal or vertical split. */
5982     last_status_rec(topframe, (p_ls == 2
5983 			  || (p_ls == 1 && (morewin || !ONE_WINDOW))));
5984 }
5985 
5986     static void
5987 last_status_rec(frame_T *fr, int statusline)
5988 {
5989     frame_T	*fp;
5990     win_T	*wp;
5991 
5992     if (fr->fr_layout == FR_LEAF)
5993     {
5994 	wp = fr->fr_win;
5995 	if (wp->w_status_height != 0 && !statusline)
5996 	{
5997 	    /* remove status line */
5998 	    win_new_height(wp, wp->w_height + 1);
5999 	    wp->w_status_height = 0;
6000 	    comp_col();
6001 	}
6002 	else if (wp->w_status_height == 0 && statusline)
6003 	{
6004 	    /* Find a frame to take a line from. */
6005 	    fp = fr;
6006 	    while (fp->fr_height <= frame_minheight(fp, NULL))
6007 	    {
6008 		if (fp == topframe)
6009 		{
6010 		    EMSG(_(e_noroom));
6011 		    return;
6012 		}
6013 		/* In a column of frames: go to frame above.  If already at
6014 		 * the top or in a row of frames: go to parent. */
6015 		if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6016 		    fp = fp->fr_prev;
6017 		else
6018 		    fp = fp->fr_parent;
6019 	    }
6020 	    wp->w_status_height = 1;
6021 	    if (fp != fr)
6022 	    {
6023 		frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6024 		frame_fix_height(wp);
6025 		(void)win_comp_pos();
6026 	    }
6027 	    else
6028 		win_new_height(wp, wp->w_height - 1);
6029 	    comp_col();
6030 	    redraw_all_later(SOME_VALID);
6031 	}
6032     }
6033     else if (fr->fr_layout == FR_ROW)
6034     {
6035 	/* vertically split windows, set status line for each one */
6036 	for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6037 	    last_status_rec(fp, statusline);
6038     }
6039     else
6040     {
6041 	/* horizontally split window, set status line for last one */
6042 	for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6043 	    ;
6044 	last_status_rec(fp, statusline);
6045     }
6046 }
6047 
6048 /*
6049  * Return the number of lines used by the tab page line.
6050  */
6051     int
6052 tabline_height(void)
6053 {
6054 #ifdef FEAT_GUI_TABLINE
6055     /* When the GUI has the tabline then this always returns zero. */
6056     if (gui_use_tabline())
6057 	return 0;
6058 #endif
6059     switch (p_stal)
6060     {
6061 	case 0: return 0;
6062 	case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6063     }
6064     return 1;
6065 }
6066 
6067 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
6068 /*
6069  * Get the file name at the cursor.
6070  * If Visual mode is active, use the selected text if it's in one line.
6071  * Returns the name in allocated memory, NULL for failure.
6072  */
6073     char_u *
6074 grab_file_name(long count, linenr_T *file_lnum)
6075 {
6076     int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6077 
6078     if (VIsual_active)
6079     {
6080 	int	len;
6081 	char_u	*ptr;
6082 
6083 	if (get_visual_text(NULL, &ptr, &len) == FAIL)
6084 	    return NULL;
6085 	return find_file_name_in_path(ptr, len, options,
6086 						     count, curbuf->b_ffname);
6087     }
6088     return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
6089 
6090 }
6091 
6092 /*
6093  * Return the file name under or after the cursor.
6094  *
6095  * The 'path' option is searched if the file name is not absolute.
6096  * The string returned has been alloc'ed and should be freed by the caller.
6097  * NULL is returned if the file name or file is not found.
6098  *
6099  * options:
6100  * FNAME_MESS	    give error messages
6101  * FNAME_EXP	    expand to path
6102  * FNAME_HYP	    check for hypertext link
6103  * FNAME_INCL	    apply "includeexpr"
6104  */
6105     char_u *
6106 file_name_at_cursor(int options, long count, linenr_T *file_lnum)
6107 {
6108     return file_name_in_line(ml_get_curline(),
6109 		      curwin->w_cursor.col, options, count, curbuf->b_ffname,
6110 		      file_lnum);
6111 }
6112 
6113 /*
6114  * Return the name of the file under or after ptr[col].
6115  * Otherwise like file_name_at_cursor().
6116  */
6117     char_u *
6118 file_name_in_line(
6119     char_u	*line,
6120     int		col,
6121     int		options,
6122     long	count,
6123     char_u	*rel_fname,	/* file we are searching relative to */
6124     linenr_T	*file_lnum)	/* line number after the file name */
6125 {
6126     char_u	*ptr;
6127     int		len;
6128 
6129     /*
6130      * search forward for what could be the start of a file name
6131      */
6132     ptr = line + col;
6133     while (*ptr != NUL && !vim_isfilec(*ptr))
6134 	MB_PTR_ADV(ptr);
6135     if (*ptr == NUL)		/* nothing found */
6136     {
6137 	if (options & FNAME_MESS)
6138 	    EMSG(_("E446: No file name under cursor"));
6139 	return NULL;
6140     }
6141 
6142     /*
6143      * Search backward for first char of the file name.
6144      * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6145      */
6146     while (ptr > line)
6147     {
6148 #ifdef FEAT_MBYTE
6149 	if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6150 	    ptr -= len + 1;
6151 	else
6152 #endif
6153 	if (vim_isfilec(ptr[-1])
6154 		|| ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6155 	    --ptr;
6156 	else
6157 	    break;
6158     }
6159 
6160     /*
6161      * Search forward for the last char of the file name.
6162      * Also allow "://" when ':' is not in 'isfname'.
6163      */
6164     len = 0;
6165     while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
6166 			 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6167     {
6168 	if (ptr[len] == '\\')
6169 	    /* Skip over the "\" in "\ ". */
6170 	    ++len;
6171 #ifdef FEAT_MBYTE
6172 	if (has_mbyte)
6173 	    len += (*mb_ptr2len)(ptr + len);
6174 	else
6175 #endif
6176 	    ++len;
6177     }
6178 
6179     /*
6180      * If there is trailing punctuation, remove it.
6181      * But don't remove "..", could be a directory name.
6182      */
6183     if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6184 						       && ptr[len - 2] != '.')
6185 	--len;
6186 
6187     if (file_lnum != NULL)
6188     {
6189 	char_u *p;
6190 
6191 	/* Get the number after the file name and a separator character */
6192 	p = ptr + len;
6193 	p = skipwhite(p);
6194 	if (*p != NUL)
6195 	{
6196 	    if (!isdigit(*p))
6197 		++p;		    /* skip the separator */
6198 	    p = skipwhite(p);
6199 	    if (isdigit(*p))
6200 		*file_lnum = (int)getdigits(&p);
6201 	}
6202     }
6203 
6204     return find_file_name_in_path(ptr, len, options, count, rel_fname);
6205 }
6206 
6207 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6208 static char_u *eval_includeexpr(char_u *ptr, int len);
6209 
6210     static char_u *
6211 eval_includeexpr(char_u *ptr, int len)
6212 {
6213     char_u	*res;
6214 
6215     set_vim_var_string(VV_FNAME, ptr, len);
6216     res = eval_to_string_safe(curbuf->b_p_inex, NULL,
6217 		      was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
6218     set_vim_var_string(VV_FNAME, NULL, 0);
6219     return res;
6220 }
6221 #endif
6222 
6223 /*
6224  * Return the name of the file ptr[len] in 'path'.
6225  * Otherwise like file_name_at_cursor().
6226  */
6227     char_u *
6228 find_file_name_in_path(
6229     char_u	*ptr,
6230     int		len,
6231     int		options,
6232     long	count,
6233     char_u	*rel_fname)	/* file we are searching relative to */
6234 {
6235     char_u	*file_name;
6236     int		c;
6237 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6238     char_u	*tofree = NULL;
6239 
6240     if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6241     {
6242 	tofree = eval_includeexpr(ptr, len);
6243 	if (tofree != NULL)
6244 	{
6245 	    ptr = tofree;
6246 	    len = (int)STRLEN(ptr);
6247 	}
6248     }
6249 # endif
6250 
6251     if (options & FNAME_EXP)
6252     {
6253 	file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6254 							     TRUE, rel_fname);
6255 
6256 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6257 	/*
6258 	 * If the file could not be found in a normal way, try applying
6259 	 * 'includeexpr' (unless done already).
6260 	 */
6261 	if (file_name == NULL
6262 		&& !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6263 	{
6264 	    tofree = eval_includeexpr(ptr, len);
6265 	    if (tofree != NULL)
6266 	    {
6267 		ptr = tofree;
6268 		len = (int)STRLEN(ptr);
6269 		file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6270 							     TRUE, rel_fname);
6271 	    }
6272 	}
6273 # endif
6274 	if (file_name == NULL && (options & FNAME_MESS))
6275 	{
6276 	    c = ptr[len];
6277 	    ptr[len] = NUL;
6278 	    EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6279 	    ptr[len] = c;
6280 	}
6281 
6282 	/* Repeat finding the file "count" times.  This matters when it
6283 	 * appears several times in the path. */
6284 	while (file_name != NULL && --count > 0)
6285 	{
6286 	    vim_free(file_name);
6287 	    file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6288 	}
6289     }
6290     else
6291 	file_name = vim_strnsave(ptr, len);
6292 
6293 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6294     vim_free(tofree);
6295 # endif
6296 
6297     return file_name;
6298 }
6299 #endif /* FEAT_SEARCHPATH */
6300 
6301 /*
6302  * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6303  * Also check for ":\\", which MS Internet Explorer accepts, return
6304  * URL_BACKSLASH.
6305  */
6306     static int
6307 path_is_url(char_u *p)
6308 {
6309     if (STRNCMP(p, "://", (size_t)3) == 0)
6310 	return URL_SLASH;
6311     else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6312 	return URL_BACKSLASH;
6313     return 0;
6314 }
6315 
6316 /*
6317  * Check if "fname" starts with "name://".  Return URL_SLASH if it does.
6318  * Return URL_BACKSLASH for "name:\\".
6319  * Return zero otherwise.
6320  */
6321     int
6322 path_with_url(char_u *fname)
6323 {
6324     char_u *p;
6325 
6326     for (p = fname; isalpha(*p); ++p)
6327 	;
6328     return path_is_url(p);
6329 }
6330 
6331 /*
6332  * Return TRUE if "name" is a full (absolute) path name or URL.
6333  */
6334     int
6335 vim_isAbsName(char_u *name)
6336 {
6337     return (path_with_url(name) != 0 || mch_isFullName(name));
6338 }
6339 
6340 /*
6341  * Get absolute file name into buffer "buf[len]".
6342  *
6343  * return FAIL for failure, OK otherwise
6344  */
6345     int
6346 vim_FullName(
6347     char_u	*fname,
6348     char_u	*buf,
6349     int		len,
6350     int		force)	    /* force expansion even when already absolute */
6351 {
6352     int		retval = OK;
6353     int		url;
6354 
6355     *buf = NUL;
6356     if (fname == NULL)
6357 	return FAIL;
6358 
6359     url = path_with_url(fname);
6360     if (!url)
6361 	retval = mch_FullName(fname, buf, len, force);
6362     if (url || retval == FAIL)
6363     {
6364 	/* something failed; use the file name (truncate when too long) */
6365 	vim_strncpy(buf, fname, len - 1);
6366     }
6367 #if defined(MSWIN)
6368     slash_adjust(buf);
6369 #endif
6370     return retval;
6371 }
6372 
6373 /*
6374  * Return the minimal number of rows that is needed on the screen to display
6375  * the current number of windows.
6376  */
6377     int
6378 min_rows(void)
6379 {
6380     int		total;
6381     tabpage_T	*tp;
6382     int		n;
6383 
6384     if (firstwin == NULL)	/* not initialized yet */
6385 	return MIN_LINES;
6386 
6387     total = 0;
6388     FOR_ALL_TABPAGES(tp)
6389     {
6390 	n = frame_minheight(tp->tp_topframe, NULL);
6391 	if (total < n)
6392 	    total = n;
6393     }
6394     total += tabline_height();
6395     total += 1;		/* count the room for the command line */
6396     return total;
6397 }
6398 
6399 /*
6400  * Return TRUE if there is only one window (in the current tab page), not
6401  * counting a help or preview window, unless it is the current window.
6402  * Does not count "aucmd_win".
6403  */
6404     int
6405 only_one_window(void)
6406 {
6407     int		count = 0;
6408     win_T	*wp;
6409 
6410     /* If there is another tab page there always is another window. */
6411     if (first_tabpage->tp_next != NULL)
6412 	return FALSE;
6413 
6414     FOR_ALL_WINDOWS(wp)
6415 	if (wp->w_buffer != NULL
6416 		&& (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
6417 # ifdef FEAT_QUICKFIX
6418 		    || wp->w_p_pvw
6419 # endif
6420 	     ) || wp == curwin)
6421 # ifdef FEAT_AUTOCMD
6422 		&& wp != aucmd_win
6423 # endif
6424 	   )
6425 	    ++count;
6426     return (count <= 1);
6427 }
6428 
6429 /*
6430  * Correct the cursor line number in other windows.  Used after changing the
6431  * current buffer, and before applying autocommands.
6432  * When "do_curwin" is TRUE, also check current window.
6433  */
6434     void
6435 check_lnums(int do_curwin)
6436 {
6437     win_T	*wp;
6438     tabpage_T	*tp;
6439 
6440     FOR_ALL_TAB_WINDOWS(tp, wp)
6441 	if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6442 	{
6443 	    if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6444 		wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6445 	    if (wp->w_topline > curbuf->b_ml.ml_line_count)
6446 		wp->w_topline = curbuf->b_ml.ml_line_count;
6447 	}
6448 }
6449 
6450 /*
6451  * A snapshot of the window sizes, to restore them after closing the help
6452  * window.
6453  * Only these fields are used:
6454  * fr_layout
6455  * fr_width
6456  * fr_height
6457  * fr_next
6458  * fr_child
6459  * fr_win (only valid for the old curwin, NULL otherwise)
6460  */
6461 
6462 /*
6463  * Create a snapshot of the current frame sizes.
6464  */
6465     void
6466 make_snapshot(int idx)
6467 {
6468     clear_snapshot(curtab, idx);
6469     make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
6470 }
6471 
6472     static void
6473 make_snapshot_rec(frame_T *fr, frame_T **frp)
6474 {
6475     *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6476     if (*frp == NULL)
6477 	return;
6478     (*frp)->fr_layout = fr->fr_layout;
6479     (*frp)->fr_width = fr->fr_width;
6480     (*frp)->fr_height = fr->fr_height;
6481     if (fr->fr_next != NULL)
6482 	make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6483     if (fr->fr_child != NULL)
6484 	make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6485     if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6486 	(*frp)->fr_win = curwin;
6487 }
6488 
6489 /*
6490  * Remove any existing snapshot.
6491  */
6492     static void
6493 clear_snapshot(tabpage_T *tp, int idx)
6494 {
6495     clear_snapshot_rec(tp->tp_snapshot[idx]);
6496     tp->tp_snapshot[idx] = NULL;
6497 }
6498 
6499     static void
6500 clear_snapshot_rec(frame_T *fr)
6501 {
6502     if (fr != NULL)
6503     {
6504 	clear_snapshot_rec(fr->fr_next);
6505 	clear_snapshot_rec(fr->fr_child);
6506 	vim_free(fr);
6507     }
6508 }
6509 
6510 /*
6511  * Restore a previously created snapshot, if there is any.
6512  * This is only done if the screen size didn't change and the window layout is
6513  * still the same.
6514  */
6515     void
6516 restore_snapshot(
6517     int		idx,
6518     int		close_curwin)	    /* closing current window */
6519 {
6520     win_T	*wp;
6521 
6522     if (curtab->tp_snapshot[idx] != NULL
6523 	    && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
6524 	    && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6525 	    && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
6526     {
6527 	wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
6528 	win_comp_pos();
6529 	if (wp != NULL && close_curwin)
6530 	    win_goto(wp);
6531 	redraw_all_later(CLEAR);
6532     }
6533     clear_snapshot(curtab, idx);
6534 }
6535 
6536 /*
6537  * Check if frames "sn" and "fr" have the same layout, same following frames
6538  * and same children.  And the window pointer is valid.
6539  */
6540     static int
6541 check_snapshot_rec(frame_T *sn, frame_T *fr)
6542 {
6543     if (sn->fr_layout != fr->fr_layout
6544 	    || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6545 	    || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6546 	    || (sn->fr_next != NULL
6547 		&& check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6548 	    || (sn->fr_child != NULL
6549 		&& check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
6550 	    || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
6551 	return FAIL;
6552     return OK;
6553 }
6554 
6555 /*
6556  * Copy the size of snapshot frame "sn" to frame "fr".  Do the same for all
6557  * following frames and children.
6558  * Returns a pointer to the old current window, or NULL.
6559  */
6560     static win_T *
6561 restore_snapshot_rec(frame_T *sn, frame_T *fr)
6562 {
6563     win_T	*wp = NULL;
6564     win_T	*wp2;
6565 
6566     fr->fr_height = sn->fr_height;
6567     fr->fr_width = sn->fr_width;
6568     if (fr->fr_layout == FR_LEAF)
6569     {
6570 	frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6571 	frame_new_width(fr, fr->fr_width, FALSE, FALSE);
6572 	wp = sn->fr_win;
6573     }
6574     if (sn->fr_next != NULL)
6575     {
6576 	wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6577 	if (wp2 != NULL)
6578 	    wp = wp2;
6579     }
6580     if (sn->fr_child != NULL)
6581     {
6582 	wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6583 	if (wp2 != NULL)
6584 	    wp = wp2;
6585     }
6586     return wp;
6587 }
6588 
6589 #if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6590 	|| defined(PROTO)
6591 /*
6592  * Set "win" to be the curwin and "tp" to be the current tab page.
6593  * restore_win() MUST be called to undo, also when FAIL is returned.
6594  * No autocommands will be executed until restore_win() is called.
6595  * When "no_display" is TRUE the display won't be affected, no redraw is
6596  * triggered, another tabpage access is limited.
6597  * Returns FAIL if switching to "win" failed.
6598  */
6599     int
6600 switch_win(
6601     win_T	**save_curwin,
6602     tabpage_T	**save_curtab,
6603     win_T	*win,
6604     tabpage_T	*tp,
6605     int		no_display)
6606 {
6607 # ifdef FEAT_AUTOCMD
6608     block_autocmds();
6609 # endif
6610     *save_curwin = curwin;
6611     if (tp != NULL)
6612     {
6613 	*save_curtab = curtab;
6614 	if (no_display)
6615 	{
6616 	    curtab->tp_firstwin = firstwin;
6617 	    curtab->tp_lastwin = lastwin;
6618 	    curtab = tp;
6619 	    firstwin = curtab->tp_firstwin;
6620 	    lastwin = curtab->tp_lastwin;
6621 	}
6622 	else
6623 	    goto_tabpage_tp(tp, FALSE, FALSE);
6624     }
6625     if (!win_valid(win))
6626 	return FAIL;
6627     curwin = win;
6628     curbuf = curwin->w_buffer;
6629     return OK;
6630 }
6631 
6632 /*
6633  * Restore current tabpage and window saved by switch_win(), if still valid.
6634  * When "no_display" is TRUE the display won't be affected, no redraw is
6635  * triggered.
6636  */
6637     void
6638 restore_win(
6639     win_T	*save_curwin UNUSED,
6640     tabpage_T	*save_curtab UNUSED,
6641     int		no_display UNUSED)
6642 {
6643     if (save_curtab != NULL && valid_tabpage(save_curtab))
6644     {
6645 	if (no_display)
6646 	{
6647 	    curtab->tp_firstwin = firstwin;
6648 	    curtab->tp_lastwin = lastwin;
6649 	    curtab = save_curtab;
6650 	    firstwin = curtab->tp_firstwin;
6651 	    lastwin = curtab->tp_lastwin;
6652 	}
6653 	else
6654 	    goto_tabpage_tp(save_curtab, FALSE, FALSE);
6655     }
6656     if (win_valid(save_curwin))
6657     {
6658 	curwin = save_curwin;
6659 	curbuf = curwin->w_buffer;
6660     }
6661 # ifdef FEAT_AUTOCMD
6662     unblock_autocmds();
6663 # endif
6664 }
6665 
6666 /*
6667  * Make "buf" the current buffer.  restore_buffer() MUST be called to undo.
6668  * No autocommands will be executed.  Use aucmd_prepbuf() if there are any.
6669  */
6670     void
6671 switch_buffer(bufref_T *save_curbuf, buf_T *buf)
6672 {
6673 # ifdef FEAT_AUTOCMD
6674     block_autocmds();
6675 # endif
6676     set_bufref(save_curbuf, curbuf);
6677     --curbuf->b_nwindows;
6678     curbuf = buf;
6679     curwin->w_buffer = buf;
6680     ++curbuf->b_nwindows;
6681 }
6682 
6683 /*
6684  * Restore the current buffer after using switch_buffer().
6685  */
6686     void
6687 restore_buffer(bufref_T *save_curbuf)
6688 {
6689 # ifdef FEAT_AUTOCMD
6690     unblock_autocmds();
6691 # endif
6692     /* Check for valid buffer, just in case. */
6693     if (bufref_valid(save_curbuf))
6694     {
6695 	--curbuf->b_nwindows;
6696 	curwin->w_buffer = save_curbuf->br_buf;
6697 	curbuf = save_curbuf->br_buf;
6698 	++curbuf->b_nwindows;
6699     }
6700 }
6701 #endif
6702 
6703 #if defined(FEAT_GUI) || defined(PROTO)
6704 /*
6705  * Return TRUE if there is any vertically split window.
6706  */
6707     int
6708 win_hasvertsplit(void)
6709 {
6710     frame_T	*fr;
6711 
6712     if (topframe->fr_layout == FR_ROW)
6713 	return TRUE;
6714 
6715     if (topframe->fr_layout == FR_COL)
6716 	for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6717 	    if (fr->fr_layout == FR_ROW)
6718 		return TRUE;
6719 
6720     return FALSE;
6721 }
6722 #endif
6723 
6724 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6725 /*
6726  * Add match to the match list of window 'wp'.  The pattern 'pat' will be
6727  * highlighted with the group 'grp' with priority 'prio'.
6728  * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6729  * If no particular ID is desired, -1 must be specified for 'id'.
6730  * Return ID of added match, -1 on failure.
6731  */
6732     int
6733 match_add(
6734     win_T	*wp,
6735     char_u	*grp,
6736     char_u	*pat,
6737     int		prio,
6738     int		id,
6739     list_T	*pos_list,
6740     char_u      *conceal_char UNUSED) /* pointer to conceal replacement char */
6741 {
6742     matchitem_T	*cur;
6743     matchitem_T	*prev;
6744     matchitem_T	*m;
6745     int		hlg_id;
6746     regprog_T	*regprog = NULL;
6747     int		rtype = SOME_VALID;
6748 
6749     if (*grp == NUL || (pat != NULL && *pat == NUL))
6750 	return -1;
6751     if (id < -1 || id == 0)
6752     {
6753 	EMSGN(_("E799: Invalid ID: %ld (must be greater than or equal to 1)"), id);
6754 	return -1;
6755     }
6756     if (id != -1)
6757     {
6758 	cur = wp->w_match_head;
6759 	while (cur != NULL)
6760 	{
6761 	    if (cur->id == id)
6762 	    {
6763 		EMSGN(_("E801: ID already taken: %ld"), id);
6764 		return -1;
6765 	    }
6766 	    cur = cur->next;
6767 	}
6768     }
6769     if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
6770     {
6771 	EMSG2(_(e_nogroup), grp);
6772 	return -1;
6773     }
6774     if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
6775     {
6776 	EMSG2(_(e_invarg2), pat);
6777 	return -1;
6778     }
6779 
6780     /* Find available match ID. */
6781     while (id == -1)
6782     {
6783 	cur = wp->w_match_head;
6784 	while (cur != NULL && cur->id != wp->w_next_match_id)
6785 	    cur = cur->next;
6786 	if (cur == NULL)
6787 	    id = wp->w_next_match_id;
6788 	wp->w_next_match_id++;
6789     }
6790 
6791     /* Build new match. */
6792     m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
6793     m->id = id;
6794     m->priority = prio;
6795     m->pattern = pat == NULL ? NULL : vim_strsave(pat);
6796     m->hlg_id = hlg_id;
6797     m->match.regprog = regprog;
6798     m->match.rmm_ic = FALSE;
6799     m->match.rmm_maxcol = 0;
6800 # if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
6801     m->conceal_char = 0;
6802     if (conceal_char != NULL)
6803 	m->conceal_char = (*mb_ptr2char)(conceal_char);
6804 # endif
6805 
6806     /* Set up position matches */
6807     if (pos_list != NULL)
6808     {
6809 	linenr_T	toplnum = 0;
6810 	linenr_T	botlnum = 0;
6811 	listitem_T	*li;
6812 	int		i;
6813 
6814 	for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
6815 							i++, li = li->li_next)
6816 	{
6817 	    linenr_T	lnum = 0;
6818 	    colnr_T	col = 0;
6819 	    int		len = 1;
6820 	    list_T	*subl;
6821 	    listitem_T	*subli;
6822 	    int		error = FALSE;
6823 
6824 	    if (li->li_tv.v_type == VAR_LIST)
6825 	    {
6826 		subl = li->li_tv.vval.v_list;
6827 		if (subl == NULL)
6828 		    goto fail;
6829 		subli = subl->lv_first;
6830 		if (subli == NULL)
6831 		    goto fail;
6832 		lnum = get_tv_number_chk(&subli->li_tv, &error);
6833 		if (error == TRUE)
6834 		    goto fail;
6835 		if (lnum == 0)
6836 		{
6837 		    --i;
6838 		    continue;
6839 		}
6840 		m->pos.pos[i].lnum = lnum;
6841 		subli = subli->li_next;
6842 		if (subli != NULL)
6843 		{
6844 		    col = get_tv_number_chk(&subli->li_tv, &error);
6845 		    if (error == TRUE)
6846 			goto fail;
6847 		    subli = subli->li_next;
6848 		    if (subli != NULL)
6849 		    {
6850 			len = get_tv_number_chk(&subli->li_tv, &error);
6851 			if (error == TRUE)
6852 			    goto fail;
6853 		    }
6854 		}
6855 		m->pos.pos[i].col = col;
6856 		m->pos.pos[i].len = len;
6857 	    }
6858 	    else if (li->li_tv.v_type == VAR_NUMBER)
6859 	    {
6860 		if (li->li_tv.vval.v_number == 0)
6861 		{
6862 		    --i;
6863 		    continue;
6864 		}
6865 		m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6866 		m->pos.pos[i].col = 0;
6867 		m->pos.pos[i].len = 0;
6868 	    }
6869 	    else
6870 	    {
6871 		EMSG(_("List or number required"));
6872 		goto fail;
6873 	    }
6874 	    if (toplnum == 0 || lnum < toplnum)
6875 		toplnum = lnum;
6876 	    if (botlnum == 0 || lnum >= botlnum)
6877 		botlnum = lnum + 1;
6878 	}
6879 
6880 	/* Calculate top and bottom lines for redrawing area */
6881 	if (toplnum != 0)
6882 	{
6883 	    if (wp->w_buffer->b_mod_set)
6884 	    {
6885 		if (wp->w_buffer->b_mod_top > toplnum)
6886 		    wp->w_buffer->b_mod_top = toplnum;
6887 		if (wp->w_buffer->b_mod_bot < botlnum)
6888 		    wp->w_buffer->b_mod_bot = botlnum;
6889 	    }
6890 	    else
6891 	    {
6892 		wp->w_buffer->b_mod_set = TRUE;
6893 		wp->w_buffer->b_mod_top = toplnum;
6894 		wp->w_buffer->b_mod_bot = botlnum;
6895 		wp->w_buffer->b_mod_xlines = 0;
6896 	    }
6897 	    m->pos.toplnum = toplnum;
6898 	    m->pos.botlnum = botlnum;
6899 	    rtype = VALID;
6900 	}
6901     }
6902 
6903     /* Insert new match.  The match list is in ascending order with regard to
6904      * the match priorities. */
6905     cur = wp->w_match_head;
6906     prev = cur;
6907     while (cur != NULL && prio >= cur->priority)
6908     {
6909 	prev = cur;
6910 	cur = cur->next;
6911     }
6912     if (cur == prev)
6913 	wp->w_match_head = m;
6914     else
6915 	prev->next = m;
6916     m->next = cur;
6917 
6918     redraw_later(rtype);
6919     return id;
6920 
6921 fail:
6922     vim_free(m);
6923     return -1;
6924 }
6925 
6926 /*
6927  * Delete match with ID 'id' in the match list of window 'wp'.
6928  * Print error messages if 'perr' is TRUE.
6929  */
6930     int
6931 match_delete(win_T *wp, int id, int perr)
6932 {
6933     matchitem_T	*cur = wp->w_match_head;
6934     matchitem_T	*prev = cur;
6935     int		rtype = SOME_VALID;
6936 
6937     if (id < 1)
6938     {
6939 	if (perr == TRUE)
6940 	    EMSGN(_("E802: Invalid ID: %ld (must be greater than or equal to 1)"),
6941 									  id);
6942 	return -1;
6943     }
6944     while (cur != NULL && cur->id != id)
6945     {
6946 	prev = cur;
6947 	cur = cur->next;
6948     }
6949     if (cur == NULL)
6950     {
6951 	if (perr == TRUE)
6952 	    EMSGN(_("E803: ID not found: %ld"), id);
6953 	return -1;
6954     }
6955     if (cur == prev)
6956 	wp->w_match_head = cur->next;
6957     else
6958 	prev->next = cur->next;
6959     vim_regfree(cur->match.regprog);
6960     vim_free(cur->pattern);
6961     if (cur->pos.toplnum != 0)
6962     {
6963 	if (wp->w_buffer->b_mod_set)
6964 	{
6965 	    if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6966 		wp->w_buffer->b_mod_top = cur->pos.toplnum;
6967 	    if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6968 		wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6969 	}
6970 	else
6971 	{
6972 	    wp->w_buffer->b_mod_set = TRUE;
6973 	    wp->w_buffer->b_mod_top = cur->pos.toplnum;
6974 	    wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6975 	    wp->w_buffer->b_mod_xlines = 0;
6976 	}
6977 	rtype = VALID;
6978     }
6979     vim_free(cur);
6980     redraw_later(rtype);
6981     return 0;
6982 }
6983 
6984 /*
6985  * Delete all matches in the match list of window 'wp'.
6986  */
6987     void
6988 clear_matches(win_T *wp)
6989 {
6990     matchitem_T *m;
6991 
6992     while (wp->w_match_head != NULL)
6993     {
6994 	m = wp->w_match_head->next;
6995 	vim_regfree(wp->w_match_head->match.regprog);
6996 	vim_free(wp->w_match_head->pattern);
6997 	vim_free(wp->w_match_head);
6998 	wp->w_match_head = m;
6999     }
7000     redraw_later(SOME_VALID);
7001 }
7002 
7003 /*
7004  * Get match from ID 'id' in window 'wp'.
7005  * Return NULL if match not found.
7006  */
7007     matchitem_T *
7008 get_match(win_T *wp, int id)
7009 {
7010     matchitem_T *cur = wp->w_match_head;
7011 
7012     while (cur != NULL && cur->id != id)
7013 	cur = cur->next;
7014     return cur;
7015 }
7016 #endif
7017 
7018 #if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7019     int
7020 get_win_number(win_T *wp, win_T *first_win)
7021 {
7022     int		i = 1;
7023     win_T	*w;
7024 
7025     for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
7026 	++i;
7027 
7028     if (w == NULL)
7029 	return 0;
7030     else
7031 	return i;
7032 }
7033 
7034     int
7035 get_tab_number(tabpage_T *tp UNUSED)
7036 {
7037     int		i = 1;
7038     tabpage_T	*t;
7039 
7040     for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7041 	++i;
7042 
7043     if (t == NULL)
7044 	return 0;
7045     else
7046 	return i;
7047 }
7048 #endif
7049 
7050 /*
7051  * Return TRUE if "topfrp" and its children are at the right height.
7052  */
7053     static int
7054 frame_check_height(frame_T *topfrp, int height)
7055 {
7056     frame_T *frp;
7057 
7058     if (topfrp->fr_height != height)
7059 	return FALSE;
7060 
7061     if (topfrp->fr_layout == FR_ROW)
7062 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7063 	    if (frp->fr_height != height)
7064 		return FALSE;
7065 
7066     return TRUE;
7067 }
7068 
7069 /*
7070  * Return TRUE if "topfrp" and its children are at the right width.
7071  */
7072     static int
7073 frame_check_width(frame_T *topfrp, int width)
7074 {
7075     frame_T *frp;
7076 
7077     if (topfrp->fr_width != width)
7078 	return FALSE;
7079 
7080     if (topfrp->fr_layout == FR_COL)
7081 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7082 	    if (frp->fr_width != width)
7083 		return FALSE;
7084 
7085     return TRUE;
7086 }
7087 
7088 #if defined(FEAT_EVAL) || defined(PROTO)
7089     int
7090 win_getid(typval_T *argvars)
7091 {
7092     int	    winnr;
7093     win_T   *wp;
7094 
7095     if (argvars[0].v_type == VAR_UNKNOWN)
7096 	return curwin->w_id;
7097     winnr = get_tv_number(&argvars[0]);
7098     if (winnr > 0)
7099     {
7100 	if (argvars[1].v_type == VAR_UNKNOWN)
7101 	    wp = firstwin;
7102 	else
7103 	{
7104 	    tabpage_T	*tp;
7105 	    int		tabnr = get_tv_number(&argvars[1]);
7106 
7107 	    FOR_ALL_TABPAGES(tp)
7108 		if (--tabnr == 0)
7109 		    break;
7110 	    if (tp == NULL)
7111 		return -1;
7112 	    if (tp == curtab)
7113 		wp = firstwin;
7114 	    else
7115 		wp = tp->tp_firstwin;
7116 	}
7117 	for ( ; wp != NULL; wp = wp->w_next)
7118 	    if (--winnr == 0)
7119 		return wp->w_id;
7120     }
7121     return 0;
7122 }
7123 
7124     int
7125 win_gotoid(typval_T *argvars)
7126 {
7127     win_T	*wp;
7128     tabpage_T   *tp;
7129     int		id = get_tv_number(&argvars[0]);
7130 
7131     FOR_ALL_TAB_WINDOWS(tp, wp)
7132 	    if (wp->w_id == id)
7133 	    {
7134 		goto_tabpage_win(tp, wp);
7135 		return 1;
7136 	    }
7137     return 0;
7138 }
7139 
7140     void
7141 win_id2tabwin(typval_T *argvars, list_T *list)
7142 {
7143     win_T	*wp;
7144     tabpage_T   *tp;
7145     int		winnr = 1;
7146     int		tabnr = 1;
7147     int		id = get_tv_number(&argvars[0]);
7148 
7149     FOR_ALL_TABPAGES(tp)
7150     {
7151 	FOR_ALL_WINDOWS_IN_TAB(tp, wp)
7152 	{
7153 	    if (wp->w_id == id)
7154 	    {
7155 		list_append_number(list, tabnr);
7156 		list_append_number(list, winnr);
7157 		return;
7158 	    }
7159 	    ++winnr;
7160 	}
7161 	++tabnr;
7162 	winnr = 1;
7163     }
7164     list_append_number(list, 0);
7165     list_append_number(list, 0);
7166 }
7167 
7168     win_T *
7169 win_id2wp(typval_T *argvars)
7170 {
7171     win_T	*wp;
7172     tabpage_T   *tp;
7173     int		id = get_tv_number(&argvars[0]);
7174 
7175     FOR_ALL_TAB_WINDOWS(tp, wp)
7176 	if (wp->w_id == id)
7177 	    return wp;
7178 
7179     return NULL;
7180 }
7181 
7182     int
7183 win_id2win(typval_T *argvars)
7184 {
7185     win_T   *wp;
7186     int	    nr = 1;
7187     int	    id = get_tv_number(&argvars[0]);
7188 
7189     FOR_ALL_WINDOWS(wp)
7190     {
7191 	if (wp->w_id == id)
7192 	    return nr;
7193 	++nr;
7194     }
7195     return 0;
7196 }
7197 
7198     void
7199 win_findbuf(typval_T *argvars, list_T *list)
7200 {
7201     win_T	*wp;
7202     tabpage_T   *tp;
7203     int		bufnr = get_tv_number(&argvars[0]);
7204 
7205     FOR_ALL_TAB_WINDOWS(tp, wp)
7206 	    if (wp->w_buffer->b_fnum == bufnr)
7207 		list_append_number(list, wp->w_id);
7208 }
7209 
7210 #endif
7211