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