blob: 79c97d8a9e8efe26d2c59381b6a12752d88926a8 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
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
Bram Moolenaar071d4272004-06-13 20:20:40 +000012static int path_is_url __ARGS((char_u *p));
13#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +010014static void cmd_with_count __ARGS((char *cmd, char_u *bufp, size_t bufsize, long Prenum));
Bram Moolenaar884ae642009-02-22 01:37:59 +000015static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000016static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
18static void frame_setheight __ARGS((frame_T *curfrp, int height));
19#ifdef FEAT_VERTSPLIT
20static void frame_setwidth __ARGS((frame_T *curfrp, int width));
21#endif
22static void win_exchange __ARGS((long));
23static void win_rotate __ARGS((int, int));
24static void win_totop __ARGS((int size, int flags));
25static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000026static int last_window __ARGS((void));
Bram Moolenaarbef1c362012-05-25 12:39:00 +020027static int close_last_window_tabpage __ARGS((win_T *win, int free_buf, tabpage_T *prev_curtab));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000029static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000030static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000031static win_T *frame2win __ARGS((frame_T *frp));
32static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
33static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
34static int frame_fixed_height __ARGS((frame_T *frp));
35#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000036static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000038static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static void frame_add_vsep __ARGS((frame_T *frp));
40static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
41static void frame_fix_width __ARGS((win_T *wp));
42#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000043#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000044static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000045static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000047static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020048static int leave_tabpage __ARGS((buf_T *new_curbuf, int trigger_leave_autocmds));
49static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds));
Bram Moolenaar071d4272004-06-13 20:20:40 +000050static void frame_fix_height __ARGS((win_T *wp));
51static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020052static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin, int trigger_enter_autocmds, int trigger_leave_autocmds));
Bram Moolenaarf740b292006-02-16 22:11:02 +000053static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000054static void frame_append __ARGS((frame_T *after, frame_T *frp));
55static void frame_insert __ARGS((frame_T *before, frame_T *frp));
56static void frame_remove __ARGS((frame_T *frp));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020057# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +000058static void win_goto_ver __ARGS((int up, long count));
59static void win_goto_hor __ARGS((int left, long count));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020060# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000061static void frame_add_height __ARGS((frame_T *frp, int n));
62static void last_status_rec __ARGS((frame_T *fr, int statusline));
63
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000065static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000067static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
69
Bram Moolenaarb893ac22013-06-26 14:04:47 +020070static int frame_check_height __ARGS((frame_T *topfrp, int height));
71#ifdef FEAT_VERTSPLIT
72static int frame_check_width __ARGS((frame_T *topfrp, int width));
73#endif
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000076
Bram Moolenaar746ebd32009-06-16 14:01:43 +000077static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010078static void set_fraction __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
80#define URL_SLASH 1 /* path_is_url() has found "://" */
81#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
82
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000083#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084
Bram Moolenaar05159a02005-02-26 23:04:13 +000085#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000086# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000087#else
88# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000089#endif
90
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000092
93static char *m_onlyone = N_("Already only one window");
94
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * all CTRL-W window commands are handled here, called from normal_cmd().
97 */
98 void
99do_window(nchar, Prenum, xchar)
100 int nchar;
101 long Prenum;
102 int xchar; /* extra char from ":wincmd gx" or NUL */
103{
104 long Prenum1;
105 win_T *wp;
106#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
107 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000108 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109#endif
110#ifdef FEAT_FIND_ID
111 int type = FIND_DEFINE;
112 int len;
113#endif
114 char_u cbuf[40];
115
116 if (Prenum == 0)
117 Prenum1 = 1;
118 else
119 Prenum1 = Prenum;
120
121#ifdef FEAT_CMDWIN
122# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
123#else
124# define CHECK_CMDWIN
125#endif
126
127 switch (nchar)
128 {
129/* split current window in two parts, horizontally */
130 case 'S':
131 case Ctrl_S:
132 case 's':
133 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000135#ifdef FEAT_QUICKFIX
136 /* When splitting the quickfix window open a new buffer in it,
137 * don't replicate the quickfix buffer. */
138 if (bt_quickfix(curbuf))
139 goto newwindow;
140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_GUI
142 need_mouse_correct = TRUE;
143#endif
144 win_split((int)Prenum, 0);
145 break;
146
147#ifdef FEAT_VERTSPLIT
148/* split current window in two parts, vertically */
149 case Ctrl_V:
150 case 'v':
151 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000153# ifdef FEAT_QUICKFIX
154 /* When splitting the quickfix window open a new buffer in it,
155 * don't replicate the quickfix buffer. */
156 if (bt_quickfix(curbuf))
157 goto newwindow;
158# endif
159# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 win_split((int)Prenum, WSP_VERT);
163 break;
164#endif
165
166/* split current window and edit alternate file */
167 case Ctrl_HAT:
168 case '^':
169 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100171 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 do_cmdline_cmd(cbuf);
173 break;
174
175/* open new window */
176 case Ctrl_N:
177 case 'n':
178 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000180#ifdef FEAT_QUICKFIX
181newwindow:
182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000184 /* window height */
185 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 else
187 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000188#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
189 if (nchar == 'v' || nchar == Ctrl_V)
190 STRCAT(cbuf, "v");
191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 STRCAT(cbuf, "new");
193 do_cmdline_cmd(cbuf);
194 break;
195
196/* quit current window */
197 case Ctrl_Q:
198 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100200 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100201 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 break;
203
204/* close current window */
205 case Ctrl_C:
206 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100208 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100209 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 break;
211
212#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
213/* close preview window */
214 case Ctrl_Z:
215 case 'z':
216 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 do_cmdline_cmd((char_u *)"pclose");
219 break;
220
221/* cursor to preview window */
222 case 'P':
223 for (wp = firstwin; wp != NULL; wp = wp->w_next)
224 if (wp->w_p_pvw)
225 break;
226 if (wp == NULL)
227 EMSG(_("E441: There is no preview window"));
228 else
229 win_goto(wp);
230 break;
231#endif
232
233/* close all but current window */
234 case Ctrl_O:
235 case 'o':
236 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100238 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100239 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 break;
241
242/* cursor to next window with wrap around */
243 case Ctrl_W:
244 case 'w':
245/* cursor to previous window with wrap around */
246 case 'W':
247 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000248 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 beep_flush();
250 else
251 {
252 if (Prenum) /* go to specified window */
253 {
254 for (wp = firstwin; --Prenum > 0; )
255 {
256 if (wp->w_next == NULL)
257 break;
258 else
259 wp = wp->w_next;
260 }
261 }
262 else
263 {
264 if (nchar == 'W') /* go to previous window */
265 {
266 wp = curwin->w_prev;
267 if (wp == NULL)
268 wp = lastwin; /* wrap around */
269 }
270 else /* go to next window */
271 {
272 wp = curwin->w_next;
273 if (wp == NULL)
274 wp = firstwin; /* wrap around */
275 }
276 }
277 win_goto(wp);
278 }
279 break;
280
281/* cursor to window below */
282 case 'j':
283 case K_DOWN:
284 case Ctrl_J:
285 CHECK_CMDWIN
286#ifdef FEAT_VERTSPLIT
287 win_goto_ver(FALSE, Prenum1);
288#else
289 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
290 wp = wp->w_next)
291 ;
292 win_goto(wp);
293#endif
294 break;
295
296/* cursor to window above */
297 case 'k':
298 case K_UP:
299 case Ctrl_K:
300 CHECK_CMDWIN
301#ifdef FEAT_VERTSPLIT
302 win_goto_ver(TRUE, Prenum1);
303#else
304 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
305 wp = wp->w_prev)
306 ;
307 win_goto(wp);
308#endif
309 break;
310
311#ifdef FEAT_VERTSPLIT
312/* cursor to left window */
313 case 'h':
314 case K_LEFT:
315 case Ctrl_H:
316 case K_BS:
317 CHECK_CMDWIN
318 win_goto_hor(TRUE, Prenum1);
319 break;
320
321/* cursor to right window */
322 case 'l':
323 case K_RIGHT:
324 case Ctrl_L:
325 CHECK_CMDWIN
326 win_goto_hor(FALSE, Prenum1);
327 break;
328#endif
329
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000330/* move window to new tab page */
331 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000332 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000333 MSG(_(m_onlyone));
334 else
335 {
336 tabpage_T *oldtab = curtab;
337 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000338
339 /* First create a new tab with the window, then go back to
340 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000341 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000342 if (win_new_tabpage((int)Prenum) == OK
343 && valid_tabpage(oldtab))
344 {
345 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200346 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000347 if (curwin == wp)
348 win_close(curwin, FALSE);
349 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200350 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000351 }
352 }
353 break;
354
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355/* cursor to top-left window */
356 case 't':
357 case Ctrl_T:
358 win_goto(firstwin);
359 break;
360
361/* cursor to bottom-right window */
362 case 'b':
363 case Ctrl_B:
364 win_goto(lastwin);
365 break;
366
367/* cursor to last accessed (previous) window */
368 case 'p':
369 case Ctrl_P:
370 if (prevwin == NULL)
371 beep_flush();
372 else
373 win_goto(prevwin);
374 break;
375
376/* exchange current and next window */
377 case 'x':
378 case Ctrl_X:
379 CHECK_CMDWIN
380 win_exchange(Prenum);
381 break;
382
383/* rotate windows downwards */
384 case Ctrl_R:
385 case 'r':
386 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 win_rotate(FALSE, (int)Prenum1); /* downwards */
389 break;
390
391/* rotate windows upwards */
392 case 'R':
393 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 win_rotate(TRUE, (int)Prenum1); /* upwards */
396 break;
397
398/* move window to the very top/bottom/left/right */
399 case 'K':
400 case 'J':
401#ifdef FEAT_VERTSPLIT
402 case 'H':
403 case 'L':
404#endif
405 CHECK_CMDWIN
406 win_totop((int)Prenum,
407 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
408 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
409 break;
410
411/* make all windows the same height */
412 case '=':
413#ifdef FEAT_GUI
414 need_mouse_correct = TRUE;
415#endif
416 win_equal(NULL, FALSE, 'b');
417 break;
418
419/* increase current window height */
420 case '+':
421#ifdef FEAT_GUI
422 need_mouse_correct = TRUE;
423#endif
424 win_setheight(curwin->w_height + (int)Prenum1);
425 break;
426
427/* decrease current window height */
428 case '-':
429#ifdef FEAT_GUI
430 need_mouse_correct = TRUE;
431#endif
432 win_setheight(curwin->w_height - (int)Prenum1);
433 break;
434
435/* set current window height */
436 case Ctrl__:
437 case '_':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(Prenum ? (int)Prenum : 9999);
442 break;
443
444#ifdef FEAT_VERTSPLIT
445/* increase current window width */
446 case '>':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setwidth(curwin->w_width + (int)Prenum1);
451 break;
452
453/* decrease current window width */
454 case '<':
455#ifdef FEAT_GUI
456 need_mouse_correct = TRUE;
457#endif
458 win_setwidth(curwin->w_width - (int)Prenum1);
459 break;
460
461/* set current window width */
462 case '|':
463#ifdef FEAT_GUI
464 need_mouse_correct = TRUE;
465#endif
466 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
467 break;
468#endif
469
470/* jump to tag and split window if tag exists (in preview window) */
471#if defined(FEAT_QUICKFIX)
472 case '}':
473 CHECK_CMDWIN
474 if (Prenum)
475 g_do_tagpreview = Prenum;
476 else
477 g_do_tagpreview = p_pvh;
478 /*FALLTHROUGH*/
479#endif
480 case ']':
481 case Ctrl_RSB:
482 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200483 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 if (Prenum)
485 postponed_split = Prenum;
486 else
487 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200488#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200489 if (nchar != '}')
490 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200491#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
Bram Moolenaard355c502014-09-23 13:48:43 +0200493 /* Execute the command right here, required when "wincmd ]"
494 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 do_nv_ident(Ctrl_RSB, NUL);
496 break;
497
498#ifdef FEAT_SEARCHPATH
499/* edit file name under cursor in a new window */
500 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000501 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000503wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000505
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000506 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 if (ptr != NULL)
508 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000509# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 setpcmark();
513 if (win_split(0, 0) == OK)
514 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200515 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000516 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
517 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000518 if (nchar == 'F' && lnum >= 0)
519 {
520 curwin->w_cursor.lnum = lnum;
521 check_cursor_lnum();
522 beginline(BL_SOL | BL_FIX);
523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525 vim_free(ptr);
526 }
527 break;
528#endif
529
530#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000531/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 * new window -- webb
533 */
534 case 'i': /* Go to any match */
535 case Ctrl_I:
536 type = FIND_ANY;
537 /* FALLTHROUGH */
538 case 'd': /* Go to definition, using 'define' */
539 case Ctrl_D:
540 CHECK_CMDWIN
541 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
542 break;
543 find_pattern_in_path(ptr, 0, len, TRUE,
544 Prenum == 0 ? TRUE : FALSE, type,
545 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
546 curwin->w_set_curswant = TRUE;
547 break;
548#endif
549
Bram Moolenaar05159a02005-02-26 23:04:13 +0000550 case K_KENTER:
551 case CAR:
552#if defined(FEAT_QUICKFIX)
553 /*
554 * In a quickfix window a <CR> jumps to the error under the
555 * cursor in a new window.
556 */
557 if (bt_quickfix(curbuf))
558 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000559 sprintf((char *)cbuf, "split +%ld%s",
560 (long)curwin->w_cursor.lnum,
561 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000562 do_cmdline_cmd(cbuf);
563 }
564#endif
565 break;
566
567
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568/* CTRL-W g extended commands */
569 case 'g':
570 case Ctrl_G:
571 CHECK_CMDWIN
572#ifdef USE_ON_FLY_SCROLL
573 dont_scroll = TRUE; /* disallow scrolling here */
574#endif
575 ++no_mapping;
576 ++allow_keys; /* no mapping for xchar, but allow key codes */
577 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000578 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 --no_mapping;
581 --allow_keys;
582#ifdef FEAT_CMDL_INFO
583 (void)add_to_showcmd(xchar);
584#endif
585 switch (xchar)
586 {
587#if defined(FEAT_QUICKFIX)
588 case '}':
589 xchar = Ctrl_RSB;
590 if (Prenum)
591 g_do_tagpreview = Prenum;
592 else
593 g_do_tagpreview = p_pvh;
594 /*FALLTHROUGH*/
595#endif
596 case ']':
597 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200598 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (Prenum)
600 postponed_split = Prenum;
601 else
602 postponed_split = -1;
603
604 /* Execute the command right here, required when
605 * "wincmd g}" was used in a function. */
606 do_nv_ident('g', xchar);
607 break;
608
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000609#ifdef FEAT_SEARCHPATH
610 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000611 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100612 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000613 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000614 goto wingotofile;
615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 default:
617 beep_flush();
618 break;
619 }
620 break;
621
622 default: beep_flush();
623 break;
624 }
625}
626
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100627 static void
628cmd_with_count(cmd, bufp, bufsize, Prenum)
629 char *cmd;
630 char_u *bufp;
631 size_t bufsize;
632 long Prenum;
633{
634 size_t len = STRLEN(cmd);
635
636 STRCPY(bufp, cmd);
637 if (Prenum > 0)
638 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
639}
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641/*
642 * split the current window, implements CTRL-W s and :split
643 *
644 * "size" is the height or width for the new window, 0 to use half of current
645 * height or width.
646 *
647 * "flags":
648 * WSP_ROOM: require enough room for new window
649 * WSP_VERT: vertical split.
650 * WSP_TOP: open window at the top-left of the shell (help window).
651 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
652 * WSP_HELP: creating the help window, keep layout snapshot
653 *
654 * return FAIL for failure, OK otherwise
655 */
656 int
657win_split(size, flags)
658 int size;
659 int flags;
660{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000661 /* When the ":tab" modifier was used open a new tab page instead. */
662 if (may_open_tabpage() == OK)
663 return OK;
664
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 /* Add flags from ":vertical", ":topleft" and ":botright". */
666 flags |= cmdmod.split;
667 if ((flags & WSP_TOP) && (flags & WSP_BOT))
668 {
669 EMSG(_("E442: Can't split topleft and botright at the same time"));
670 return FAIL;
671 }
672
673 /* When creating the help window make a snapshot of the window layout.
674 * Otherwise clear the snapshot, it's now invalid. */
675 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000676 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000678 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 return win_split_ins(size, flags, NULL, 0);
681}
682
683/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100684 * When "new_wp" is NULL: split the current window in two.
685 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 * top/left/right/bottom.
687 * return FAIL for failure, OK otherwise
688 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000689 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100690win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 int size;
692 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100693 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 int dir;
695{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100696 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 win_T *oldwin;
698 int new_size = size;
699 int i;
700 int need_status = 0;
701 int do_equal = FALSE;
702 int needed;
703 int available;
704 int oldwin_height = 0;
705 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200706 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200708 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200709 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
711 if (flags & WSP_TOP)
712 oldwin = firstwin;
713 else if (flags & WSP_BOT)
714 oldwin = lastwin;
715 else
716 oldwin = curwin;
717
718 /* add a status line when p_ls == 1 and splitting the first window */
719 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
720 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100721 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 EMSG(_(e_noroom));
724 return FAIL;
725 }
726 need_status = STATUS_HEIGHT;
727 }
728
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000729#ifdef FEAT_GUI
730 /* May be needed for the scrollbars that are going to change. */
731 if (gui.in_use)
732 out_flush();
733#endif
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#ifdef FEAT_VERTSPLIT
736 if (flags & WSP_VERT)
737 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200738 int wmw1;
739 int minwidth;
740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
743 /*
744 * Check if we are able to split the current window and compute its
745 * width.
746 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200747 /* Current window requires at least 1 space. */
748 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
749 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200751 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200752 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200754 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200756 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200758 else if (p_ea)
759 {
760 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
761 prevfrp = oldwin->w_frame;
762 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
763 frp = frp->fr_parent)
764 {
765 if (frp->fr_layout == FR_ROW)
766 for (frp2 = frp->fr_child; frp2 != NULL;
767 frp2 = frp2->fr_next)
768 if (frp2 != prevfrp)
769 minwidth += frame_minwidth(frp2, NOWIN);
770 prevfrp = frp;
771 }
772 available = topframe->fr_width;
773 needed += minwidth;
774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200776 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200777 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
778 available = oldwin->w_frame->fr_width;
779 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200780 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100781 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {
783 EMSG(_(e_noroom));
784 return FAIL;
785 }
786 if (new_size == 0)
787 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200788 if (new_size > available - minwidth - 1)
789 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200790 if (new_size < wmw1)
791 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793 /* if it doesn't fit in the current window, need win_equal() */
794 if (oldwin->w_width - new_size - 1 < p_wmw)
795 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000796
797 /* We don't like to take lines for the new window from a
798 * 'winfixwidth' window. Take them from a window to the left or right
799 * instead, if possible. */
800 if (oldwin->w_p_wfw)
801 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000802
803 /* Only make all windows the same width if one of them (except oldwin)
804 * is wider than one of the split windows. */
805 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
806 && oldwin->w_frame->fr_parent != NULL)
807 {
808 frp = oldwin->w_frame->fr_parent->fr_child;
809 while (frp != NULL)
810 {
811 if (frp->fr_win != oldwin && frp->fr_win != NULL
812 && (frp->fr_win->w_width > new_size
813 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100814 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000815 {
816 do_equal = TRUE;
817 break;
818 }
819 frp = frp->fr_next;
820 }
821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 }
823 else
824#endif
825 {
826 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827
828 /*
829 * Check if we are able to split the current window and compute its
830 * height.
831 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200832 /* Current window requires at least 1 space. */
833 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
834 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200836 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200837 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200839 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200841 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200843 else if (p_ea)
844 {
845 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
846 prevfrp = oldwin->w_frame;
847 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
848 frp = frp->fr_parent)
849 {
850 if (frp->fr_layout == FR_COL)
851 for (frp2 = frp->fr_child; frp2 != NULL;
852 frp2 = frp2->fr_next)
853 if (frp2 != prevfrp)
854 minheight += frame_minheight(frp2, NOWIN);
855 prevfrp = frp;
856 }
857 available = topframe->fr_height;
858 needed += minheight;
859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 else
861 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200862 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
863 available = oldwin->w_frame->fr_height;
864 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100866 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {
868 EMSG(_(e_noroom));
869 return FAIL;
870 }
871 oldwin_height = oldwin->w_height;
872 if (need_status)
873 {
874 oldwin->w_status_height = STATUS_HEIGHT;
875 oldwin_height -= STATUS_HEIGHT;
876 }
877 if (new_size == 0)
878 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200879 if (new_size > available - minheight - STATUS_HEIGHT)
880 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200881 if (new_size < wmh1)
882 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884 /* if it doesn't fit in the current window, need win_equal() */
885 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
886 do_equal = TRUE;
887
888 /* We don't like to take lines for the new window from a
889 * 'winfixheight' window. Take them from a window above or below
890 * instead, if possible. */
891 if (oldwin->w_p_wfh)
892 {
893 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
894 oldwin);
895 oldwin_height = oldwin->w_height;
896 if (need_status)
897 oldwin_height -= STATUS_HEIGHT;
898 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000899
900 /* Only make all windows the same height if one of them (except oldwin)
901 * is higher than one of the split windows. */
902 if (!do_equal && p_ea && size == 0
903#ifdef FEAT_VERTSPLIT
904 && *p_ead != 'h'
905#endif
906 && oldwin->w_frame->fr_parent != NULL)
907 {
908 frp = oldwin->w_frame->fr_parent->fr_child;
909 while (frp != NULL)
910 {
911 if (frp->fr_win != oldwin && frp->fr_win != NULL
912 && (frp->fr_win->w_height > new_size
913 || frp->fr_win->w_height > oldwin_height - new_size
914 - STATUS_HEIGHT))
915 {
916 do_equal = TRUE;
917 break;
918 }
919 frp = frp->fr_next;
920 }
921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923
924 /*
925 * allocate new window structure and link it in the window list
926 */
927 if ((flags & WSP_TOP) == 0
928 && ((flags & WSP_BOT)
929 || (flags & WSP_BELOW)
930 || (!(flags & WSP_ABOVE)
931 && (
932#ifdef FEAT_VERTSPLIT
933 (flags & WSP_VERT) ? p_spr :
934#endif
935 p_sb))))
936 {
937 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100938 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000939 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 else
941 win_append(oldwin, wp);
942 }
943 else
944 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100945 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000946 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 else
948 win_append(oldwin->w_prev, wp);
949 }
950
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100951 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 {
953 if (wp == NULL)
954 return FAIL;
955
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000956 new_frame(wp);
957 if (wp->w_frame == NULL)
958 {
959 win_free(wp, NULL);
960 return FAIL;
961 }
962
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000963 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000964 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966
967 /*
968 * Reorganise the tree of frames to insert the new window.
969 */
970 if (flags & (WSP_TOP | WSP_BOT))
971 {
972#ifdef FEAT_VERTSPLIT
973 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
974 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
975#else
976 if (topframe->fr_layout == FR_COL)
977#endif
978 {
979 curfrp = topframe->fr_child;
980 if (flags & WSP_BOT)
981 while (curfrp->fr_next != NULL)
982 curfrp = curfrp->fr_next;
983 }
984 else
985 curfrp = topframe;
986 before = (flags & WSP_TOP);
987 }
988 else
989 {
990 curfrp = oldwin->w_frame;
991 if (flags & WSP_BELOW)
992 before = FALSE;
993 else if (flags & WSP_ABOVE)
994 before = TRUE;
995 else
996#ifdef FEAT_VERTSPLIT
997 if (flags & WSP_VERT)
998 before = !p_spr;
999 else
1000#endif
1001 before = !p_sb;
1002 }
1003 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1004 {
1005 /* Need to create a new frame in the tree to make a branch. */
1006 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1007 *frp = *curfrp;
1008 curfrp->fr_layout = layout;
1009 frp->fr_parent = curfrp;
1010 frp->fr_next = NULL;
1011 frp->fr_prev = NULL;
1012 curfrp->fr_child = frp;
1013 curfrp->fr_win = NULL;
1014 curfrp = frp;
1015 if (frp->fr_win != NULL)
1016 oldwin->w_frame = frp;
1017 else
1018 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1019 frp->fr_parent = curfrp;
1020 }
1021
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001022 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001023 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001025 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 frp->fr_parent = curfrp->fr_parent;
1027
1028 /* Insert the new frame at the right place in the frame list. */
1029 if (before)
1030 frame_insert(curfrp, frp);
1031 else
1032 frame_append(curfrp, frp);
1033
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001034 /* Set w_fraction now so that the cursor keeps the same relative
1035 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001036 if (oldwin->w_height > 0)
1037 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001038 wp->w_fraction = oldwin->w_fraction;
1039
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#ifdef FEAT_VERTSPLIT
1041 if (flags & WSP_VERT)
1042 {
1043 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (need_status)
1046 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001047 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 oldwin->w_status_height = need_status;
1049 }
1050 if (flags & (WSP_TOP | WSP_BOT))
1051 {
1052 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001053 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001054 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 wp->w_status_height = (p_ls > 0);
1056 }
1057 else
1058 {
1059 /* height and row of new window is same as current window */
1060 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001061 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 wp->w_status_height = oldwin->w_status_height;
1063 }
1064 frp->fr_height = curfrp->fr_height;
1065
1066 /* "new_size" of the current window goes to the new window, use
1067 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001068 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (before)
1070 wp->w_vsep_width = 1;
1071 else
1072 {
1073 wp->w_vsep_width = oldwin->w_vsep_width;
1074 oldwin->w_vsep_width = 1;
1075 }
1076 if (flags & (WSP_TOP | WSP_BOT))
1077 {
1078 if (flags & WSP_BOT)
1079 frame_add_vsep(curfrp);
1080 /* Set width of neighbor frame */
1081 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001082 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1083 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001086 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (before) /* new window left of current one */
1088 {
1089 wp->w_wincol = oldwin->w_wincol;
1090 oldwin->w_wincol += new_size + 1;
1091 }
1092 else /* new window right of current one */
1093 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1094 frame_fix_width(oldwin);
1095 frame_fix_width(wp);
1096 }
1097 else
1098#endif
1099 {
1100 /* width and column of new window is same as current window */
1101#ifdef FEAT_VERTSPLIT
1102 if (flags & (WSP_TOP | WSP_BOT))
1103 {
1104 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001105 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 wp->w_vsep_width = 0;
1107 }
1108 else
1109 {
1110 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001111 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 wp->w_vsep_width = oldwin->w_vsep_width;
1113 }
1114 frp->fr_width = curfrp->fr_width;
1115#endif
1116
1117 /* "new_size" of the current window goes to the new window, use
1118 * one row for the status line */
1119 win_new_height(wp, new_size);
1120 if (flags & (WSP_TOP | WSP_BOT))
1121 frame_new_height(curfrp, curfrp->fr_height
1122 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1123 else
1124 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1125 if (before) /* new window above current one */
1126 {
1127 wp->w_winrow = oldwin->w_winrow;
1128 wp->w_status_height = STATUS_HEIGHT;
1129 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1130 }
1131 else /* new window below current one */
1132 {
1133 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1134 wp->w_status_height = oldwin->w_status_height;
1135 oldwin->w_status_height = STATUS_HEIGHT;
1136 }
1137#ifdef FEAT_VERTSPLIT
1138 if (flags & WSP_BOT)
1139 frame_add_statusline(curfrp);
1140#endif
1141 frame_fix_height(wp);
1142 frame_fix_height(oldwin);
1143 }
1144
1145 if (flags & (WSP_TOP | WSP_BOT))
1146 (void)win_comp_pos();
1147
1148 /*
1149 * Both windows need redrawing
1150 */
1151 redraw_win_later(wp, NOT_VALID);
1152 wp->w_redr_status = TRUE;
1153 redraw_win_later(oldwin, NOT_VALID);
1154 oldwin->w_redr_status = TRUE;
1155
1156 if (need_status)
1157 {
1158 msg_row = Rows - 1;
1159 msg_col = sc_col;
1160 msg_clr_eos_force(); /* Old command/ruler may still be there */
1161 comp_col();
1162 msg_row = Rows - 1;
1163 msg_col = 0; /* put position back at start of line */
1164 }
1165
1166 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001167 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 */
1169 if (do_equal || dir != 0)
1170 win_equal(wp, TRUE,
1171#ifdef FEAT_VERTSPLIT
1172 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1173 : dir == 'h' ? 'b' :
1174#endif
1175 'v');
1176
1177 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1178 * size was given. */
1179#ifdef FEAT_VERTSPLIT
1180 if (flags & WSP_VERT)
1181 {
1182 i = p_wiw;
1183 if (size != 0)
1184 p_wiw = size;
1185
1186# ifdef FEAT_GUI
1187 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1188 if (gui.in_use)
1189 gui_init_which_components(NULL);
1190# endif
1191 }
1192 else
1193#endif
1194 {
1195 i = p_wh;
1196 if (size != 0)
1197 p_wh = size;
1198 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001199
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001200#ifdef FEAT_JUMPLIST
1201 /* Keep same changelist position in new window. */
1202 wp->w_changelistidx = oldwin->w_changelistidx;
1203#endif
1204
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001205 /*
1206 * make the new window the current window
1207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 win_enter(wp, FALSE);
1209#ifdef FEAT_VERTSPLIT
1210 if (flags & WSP_VERT)
1211 p_wiw = i;
1212 else
1213#endif
1214 p_wh = i;
1215
1216 return OK;
1217}
1218
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001219
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001220/*
1221 * Initialize window "newp" from window "oldp".
1222 * Used when splitting a window and when creating a new tab page.
1223 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001224 * WSP_NEWLOC may be specified in flags to prevent the location list from
1225 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001226 */
1227 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001228win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001229 win_T *newp;
1230 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001231 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001232{
1233 int i;
1234
1235 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001236#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001237 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001238#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001239 oldp->w_buffer->b_nwindows++;
1240 newp->w_cursor = oldp->w_cursor;
1241 newp->w_valid = 0;
1242 newp->w_curswant = oldp->w_curswant;
1243 newp->w_set_curswant = oldp->w_set_curswant;
1244 newp->w_topline = oldp->w_topline;
1245#ifdef FEAT_DIFF
1246 newp->w_topfill = oldp->w_topfill;
1247#endif
1248 newp->w_leftcol = oldp->w_leftcol;
1249 newp->w_pcmark = oldp->w_pcmark;
1250 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1251 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001252 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001253 newp->w_fraction = oldp->w_fraction;
1254 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1255#ifdef FEAT_JUMPLIST
1256 copy_jumplist(oldp, newp);
1257#endif
1258#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001259 if (flags & WSP_NEWLOC)
1260 {
1261 /* Don't copy the location list. */
1262 newp->w_llist = NULL;
1263 newp->w_llist_ref = NULL;
1264 }
1265 else
1266 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001267#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001268 newp->w_localdir = (oldp->w_localdir == NULL)
1269 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001270
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001271 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001272 for (i = 0; i < oldp->w_tagstacklen; i++)
1273 {
1274 newp->w_tagstack[i] = oldp->w_tagstack[i];
1275 if (newp->w_tagstack[i].tagname != NULL)
1276 newp->w_tagstack[i].tagname =
1277 vim_strsave(newp->w_tagstack[i].tagname);
1278 }
1279 newp->w_tagstackidx = oldp->w_tagstackidx;
1280 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001281#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001282 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001283#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001284
1285 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001286
Bram Moolenaara971b822011-09-14 14:43:25 +02001287#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001288 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001289#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001290}
1291
1292/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001293 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001294 * Only the essential things are copied.
1295 */
1296 static void
1297win_init_some(newp, oldp)
1298 win_T *newp;
1299 win_T *oldp;
1300{
1301 /* Use the same argument list. */
1302 newp->w_alist = oldp->w_alist;
1303 ++newp->w_alist->al_refcount;
1304 newp->w_arg_idx = oldp->w_arg_idx;
1305
1306 /* copy options from existing window */
1307 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001308}
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#endif /* FEAT_WINDOWS */
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312#if defined(FEAT_WINDOWS) || defined(PROTO)
1313/*
1314 * Check if "win" is a pointer to an existing window.
1315 */
1316 int
1317win_valid(win)
1318 win_T *win;
1319{
1320 win_T *wp;
1321
1322 if (win == NULL)
1323 return FALSE;
1324 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1325 if (wp == win)
1326 return TRUE;
1327 return FALSE;
1328}
1329
1330/*
1331 * Return the number of windows.
1332 */
1333 int
1334win_count()
1335{
1336 win_T *wp;
1337 int count = 0;
1338
1339 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1340 ++count;
1341 return count;
1342}
1343
1344/*
1345 * Make "count" windows on the screen.
1346 * Return actual number of windows on the screen.
1347 * Must be called when there is just one window, filling the whole screen
1348 * (excluding the command line).
1349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 int
1351make_windows(count, vertical)
1352 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001353 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 int maxcount;
1356 int todo;
1357
1358#ifdef FEAT_VERTSPLIT
1359 if (vertical)
1360 {
1361 /* Each windows needs at least 'winminwidth' lines and a separator
1362 * column. */
1363 maxcount = (curwin->w_width + curwin->w_vsep_width
1364 - (p_wiw - p_wmw)) / (p_wmw + 1);
1365 }
1366 else
1367#endif
1368 {
1369 /* Each window needs at least 'winminheight' lines and a status line. */
1370 maxcount = (curwin->w_height + curwin->w_status_height
1371 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1372 }
1373
1374 if (maxcount < 2)
1375 maxcount = 2;
1376 if (count > maxcount)
1377 count = maxcount;
1378
1379 /*
1380 * add status line now, otherwise first window will be too big
1381 */
1382 if (count > 1)
1383 last_status(TRUE);
1384
1385#ifdef FEAT_AUTOCMD
1386 /*
1387 * Don't execute autocommands while creating the windows. Must do that
1388 * when putting the buffers in the windows.
1389 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001390 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391#endif
1392
1393 /* todo is number of windows left to create */
1394 for (todo = count - 1; todo > 0; --todo)
1395#ifdef FEAT_VERTSPLIT
1396 if (vertical)
1397 {
1398 if (win_split(curwin->w_width - (curwin->w_width - todo)
1399 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1400 break;
1401 }
1402 else
1403#endif
1404 {
1405 if (win_split(curwin->w_height - (curwin->w_height - todo
1406 * STATUS_HEIGHT) / (todo + 1)
1407 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1408 break;
1409 }
1410
1411#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001412 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#endif
1414
1415 /* return actual number of windows */
1416 return (count - todo);
1417}
1418
1419/*
1420 * Exchange current and next window
1421 */
1422 static void
1423win_exchange(Prenum)
1424 long Prenum;
1425{
1426 frame_T *frp;
1427 frame_T *frp2;
1428 win_T *wp;
1429 win_T *wp2;
1430 int temp;
1431
1432 if (lastwin == firstwin) /* just one window */
1433 {
1434 beep_flush();
1435 return;
1436 }
1437
1438#ifdef FEAT_GUI
1439 need_mouse_correct = TRUE;
1440#endif
1441
1442 /*
1443 * find window to exchange with
1444 */
1445 if (Prenum)
1446 {
1447 frp = curwin->w_frame->fr_parent->fr_child;
1448 while (frp != NULL && --Prenum > 0)
1449 frp = frp->fr_next;
1450 }
1451 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1452 frp = curwin->w_frame->fr_next;
1453 else /* Swap last window in row/col with previous */
1454 frp = curwin->w_frame->fr_prev;
1455
1456 /* We can only exchange a window with another window, not with a frame
1457 * containing windows. */
1458 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1459 return;
1460 wp = frp->fr_win;
1461
1462/*
1463 * 1. remove curwin from the list. Remember after which window it was in wp2
1464 * 2. insert curwin before wp in the list
1465 * if wp != wp2
1466 * 3. remove wp from the list
1467 * 4. insert wp after wp2
1468 * 5. exchange the status line height and vsep width.
1469 */
1470 wp2 = curwin->w_prev;
1471 frp2 = curwin->w_frame->fr_prev;
1472 if (wp->w_prev != curwin)
1473 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001474 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 frame_remove(curwin->w_frame);
1476 win_append(wp->w_prev, curwin);
1477 frame_insert(frp, curwin->w_frame);
1478 }
1479 if (wp != wp2)
1480 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001481 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 frame_remove(wp->w_frame);
1483 win_append(wp2, wp);
1484 if (frp2 == NULL)
1485 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1486 else
1487 frame_append(frp2, wp->w_frame);
1488 }
1489 temp = curwin->w_status_height;
1490 curwin->w_status_height = wp->w_status_height;
1491 wp->w_status_height = temp;
1492#ifdef FEAT_VERTSPLIT
1493 temp = curwin->w_vsep_width;
1494 curwin->w_vsep_width = wp->w_vsep_width;
1495 wp->w_vsep_width = temp;
1496
1497 /* If the windows are not in the same frame, exchange the sizes to avoid
1498 * messing up the window layout. Otherwise fix the frame sizes. */
1499 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1500 {
1501 temp = curwin->w_height;
1502 curwin->w_height = wp->w_height;
1503 wp->w_height = temp;
1504 temp = curwin->w_width;
1505 curwin->w_width = wp->w_width;
1506 wp->w_width = temp;
1507 }
1508 else
1509 {
1510 frame_fix_height(curwin);
1511 frame_fix_height(wp);
1512 frame_fix_width(curwin);
1513 frame_fix_width(wp);
1514 }
1515#endif
1516
1517 (void)win_comp_pos(); /* recompute window positions */
1518
1519 win_enter(wp, TRUE);
1520 redraw_later(CLEAR);
1521}
1522
1523/*
1524 * rotate windows: if upwards TRUE the second window becomes the first one
1525 * if upwards FALSE the first window becomes the second one
1526 */
1527 static void
1528win_rotate(upwards, count)
1529 int upwards;
1530 int count;
1531{
1532 win_T *wp1;
1533 win_T *wp2;
1534 frame_T *frp;
1535 int n;
1536
1537 if (firstwin == lastwin) /* nothing to do */
1538 {
1539 beep_flush();
1540 return;
1541 }
1542
1543#ifdef FEAT_GUI
1544 need_mouse_correct = TRUE;
1545#endif
1546
1547#ifdef FEAT_VERTSPLIT
1548 /* Check if all frames in this row/col have one window. */
1549 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1550 frp = frp->fr_next)
1551 if (frp->fr_win == NULL)
1552 {
1553 EMSG(_("E443: Cannot rotate when another window is split"));
1554 return;
1555 }
1556#endif
1557
1558 while (count--)
1559 {
1560 if (upwards) /* first window becomes last window */
1561 {
1562 /* remove first window/frame from the list */
1563 frp = curwin->w_frame->fr_parent->fr_child;
1564 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001565 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 frame_remove(frp);
1567
1568 /* find last frame and append removed window/frame after it */
1569 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1570 ;
1571 win_append(frp->fr_win, wp1);
1572 frame_append(frp, wp1->w_frame);
1573
1574 wp2 = frp->fr_win; /* previously last window */
1575 }
1576 else /* last window becomes first window */
1577 {
1578 /* find last window/frame in the list and remove it */
1579 for (frp = curwin->w_frame; frp->fr_next != NULL;
1580 frp = frp->fr_next)
1581 ;
1582 wp1 = frp->fr_win;
1583 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001584 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 frame_remove(frp);
1586
1587 /* append the removed window/frame before the first in the list */
1588 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1589 frame_insert(frp->fr_parent->fr_child, frp);
1590 }
1591
1592 /* exchange status height and vsep width of old and new last window */
1593 n = wp2->w_status_height;
1594 wp2->w_status_height = wp1->w_status_height;
1595 wp1->w_status_height = n;
1596 frame_fix_height(wp1);
1597 frame_fix_height(wp2);
1598#ifdef FEAT_VERTSPLIT
1599 n = wp2->w_vsep_width;
1600 wp2->w_vsep_width = wp1->w_vsep_width;
1601 wp1->w_vsep_width = n;
1602 frame_fix_width(wp1);
1603 frame_fix_width(wp2);
1604#endif
1605
1606 /* recompute w_winrow and w_wincol for all windows */
1607 (void)win_comp_pos();
1608 }
1609
1610 redraw_later(CLEAR);
1611}
1612
1613/*
1614 * Move the current window to the very top/bottom/left/right of the screen.
1615 */
1616 static void
1617win_totop(size, flags)
1618 int size;
1619 int flags;
1620{
1621 int dir;
1622 int height = curwin->w_height;
1623
1624 if (lastwin == firstwin)
1625 {
1626 beep_flush();
1627 return;
1628 }
1629
1630 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001631 (void)winframe_remove(curwin, &dir, NULL);
1632 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 last_status(FALSE); /* may need to remove last status line */
1634 (void)win_comp_pos(); /* recompute window positions */
1635
1636 /* Split a window on the desired side and put the window there. */
1637 (void)win_split_ins(size, flags, curwin, dir);
1638 if (!(flags & WSP_VERT))
1639 {
1640 win_setheight(height);
1641 if (p_ea)
1642 win_equal(curwin, TRUE, 'v');
1643 }
1644
1645#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1646 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1647 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001648 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650}
1651
1652/*
1653 * Move window "win1" to below/right of "win2" and make "win1" the current
1654 * window. Only works within the same frame!
1655 */
1656 void
1657win_move_after(win1, win2)
1658 win_T *win1, *win2;
1659{
1660 int height;
1661
1662 /* check if the arguments are reasonable */
1663 if (win1 == win2)
1664 return;
1665
1666 /* check if there is something to do */
1667 if (win2->w_next != win1)
1668 {
1669 /* may need move the status line/vertical separator of the last window
1670 * */
1671 if (win1 == lastwin)
1672 {
1673 height = win1->w_prev->w_status_height;
1674 win1->w_prev->w_status_height = win1->w_status_height;
1675 win1->w_status_height = height;
1676#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001677 if (win1->w_prev->w_vsep_width == 1)
1678 {
1679 /* Remove the vertical separator from the last-but-one window,
1680 * add it to the last window. Adjust the frame widths. */
1681 win1->w_prev->w_vsep_width = 0;
1682 win1->w_prev->w_frame->fr_width -= 1;
1683 win1->w_vsep_width = 1;
1684 win1->w_frame->fr_width += 1;
1685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
1687 }
1688 else if (win2 == lastwin)
1689 {
1690 height = win1->w_status_height;
1691 win1->w_status_height = win2->w_status_height;
1692 win2->w_status_height = height;
1693#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001694 if (win1->w_vsep_width == 1)
1695 {
1696 /* Remove the vertical separator from win1, add it to the last
1697 * window, win2. Adjust the frame widths. */
1698 win2->w_vsep_width = 1;
1699 win2->w_frame->fr_width += 1;
1700 win1->w_vsep_width = 0;
1701 win1->w_frame->fr_width -= 1;
1702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
1704 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001705 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 frame_remove(win1->w_frame);
1707 win_append(win2, win1);
1708 frame_append(win2->w_frame, win1->w_frame);
1709
1710 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1711 redraw_later(NOT_VALID);
1712 }
1713 win_enter(win1, FALSE);
1714}
1715
1716/*
1717 * Make all windows the same height.
1718 * 'next_curwin' will soon be the current window, make sure it has enough
1719 * rows.
1720 */
1721 void
1722win_equal(next_curwin, current, dir)
1723 win_T *next_curwin; /* pointer to current window to be or NULL */
1724 int current; /* do only frame with current window */
1725 int dir; /* 'v' for vertically, 'h' for horizontally,
1726 'b' for both, 0 for using p_ead */
1727{
1728 if (dir == 0)
1729#ifdef FEAT_VERTSPLIT
1730 dir = *p_ead;
1731#else
1732 dir = 'b';
1733#endif
1734 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001735 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001736 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737}
1738
1739/*
1740 * Set a frame to a new position and height, spreading the available room
1741 * equally over contained frames.
1742 * The window "next_curwin" (if not NULL) should at least get the size from
1743 * 'winheight' and 'winwidth' if possible.
1744 */
1745 static void
1746win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1747 win_T *next_curwin; /* pointer to current window to be or NULL */
1748 int current; /* do only frame with current window */
1749 frame_T *topfr; /* frame to set size off */
1750 int dir; /* 'v', 'h' or 'b', see win_equal() */
1751 int col; /* horizontal position for frame */
1752 int row; /* vertical position for frame */
1753 int width; /* new width of frame */
1754 int height; /* new height of frame */
1755{
1756 int n, m;
1757 int extra_sep = 0;
1758 int wincount, totwincount = 0;
1759 frame_T *fr;
1760 int next_curwin_size = 0;
1761 int room = 0;
1762 int new_size;
1763 int has_next_curwin = 0;
1764 int hnc;
1765
1766 if (topfr->fr_layout == FR_LEAF)
1767 {
1768 /* Set the width/height of this frame.
1769 * Redraw when size or position changes */
1770 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1771#ifdef FEAT_VERTSPLIT
1772 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1773#endif
1774 )
1775 {
1776 topfr->fr_win->w_winrow = row;
1777 frame_new_height(topfr, height, FALSE, FALSE);
1778#ifdef FEAT_VERTSPLIT
1779 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001780 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781#endif
1782 redraw_all_later(CLEAR);
1783 }
1784 }
1785#ifdef FEAT_VERTSPLIT
1786 else if (topfr->fr_layout == FR_ROW)
1787 {
1788 topfr->fr_width = width;
1789 topfr->fr_height = height;
1790
1791 if (dir != 'v') /* equalize frame widths */
1792 {
1793 /* Compute the maximum number of windows horizontally in this
1794 * frame. */
1795 n = frame_minwidth(topfr, NOWIN);
1796 /* add one for the rightmost window, it doesn't have a separator */
1797 if (col + width == Columns)
1798 extra_sep = 1;
1799 else
1800 extra_sep = 0;
1801 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001802 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001804 /*
1805 * Compute width for "next_curwin" window and room available for
1806 * other windows.
1807 * "m" is the minimal width when counting p_wiw for "next_curwin".
1808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 m = frame_minwidth(topfr, next_curwin);
1810 room = width - m;
1811 if (room < 0)
1812 {
1813 next_curwin_size = p_wiw + room;
1814 room = 0;
1815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 else
1817 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001818 next_curwin_size = -1;
1819 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1820 {
1821 /* If 'winfixwidth' set keep the window width if
1822 * possible.
1823 * Watch out for this window being the next_curwin. */
1824 if (frame_fixed_width(fr))
1825 {
1826 n = frame_minwidth(fr, NOWIN);
1827 new_size = fr->fr_width;
1828 if (frame_has_win(fr, next_curwin))
1829 {
1830 room += p_wiw - p_wmw;
1831 next_curwin_size = 0;
1832 if (new_size < p_wiw)
1833 new_size = p_wiw;
1834 }
1835 else
1836 /* These windows don't use up room. */
1837 totwincount -= (n + (fr->fr_next == NULL
1838 ? extra_sep : 0)) / (p_wmw + 1);
1839 room -= new_size - n;
1840 if (room < 0)
1841 {
1842 new_size += room;
1843 room = 0;
1844 }
1845 fr->fr_newwidth = new_size;
1846 }
1847 }
1848 if (next_curwin_size == -1)
1849 {
1850 if (!has_next_curwin)
1851 next_curwin_size = 0;
1852 else if (totwincount > 1
1853 && (room + (totwincount - 2))
1854 / (totwincount - 1) > p_wiw)
1855 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001856 /* Can make all windows wider than 'winwidth', spread
1857 * the room equally. */
1858 next_curwin_size = (room + p_wiw
1859 + (totwincount - 1) * p_wmw
1860 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001861 room -= next_curwin_size - p_wiw;
1862 }
1863 else
1864 next_curwin_size = p_wiw;
1865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001867
1868 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 --totwincount; /* don't count curwin */
1870 }
1871
1872 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1873 {
1874 n = m = 0;
1875 wincount = 1;
1876 if (fr->fr_next == NULL)
1877 /* last frame gets all that remains (avoid roundoff error) */
1878 new_size = width;
1879 else if (dir == 'v')
1880 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001881 else if (frame_fixed_width(fr))
1882 {
1883 new_size = fr->fr_newwidth;
1884 wincount = 0; /* doesn't count as a sizeable window */
1885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 else
1887 {
1888 /* Compute the maximum number of windows horiz. in "fr". */
1889 n = frame_minwidth(fr, NOWIN);
1890 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1891 / (p_wmw + 1);
1892 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001893 if (has_next_curwin)
1894 hnc = frame_has_win(fr, next_curwin);
1895 else
1896 hnc = FALSE;
1897 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001899 if (totwincount == 0)
1900 new_size = room;
1901 else
1902 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001904 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 next_curwin_size -= p_wiw - (m - n);
1907 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001908 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001910 else
1911 room -= new_size;
1912 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 }
1914
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001915 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 * window, unless equalizing all frames. */
1917 if (!current || dir != 'v' || topfr->fr_parent != NULL
1918 || (new_size != fr->fr_width)
1919 || frame_has_win(fr, next_curwin))
1920 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001921 new_size, height);
1922 col += new_size;
1923 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 totwincount -= wincount;
1925 }
1926 }
1927#endif
1928 else /* topfr->fr_layout == FR_COL */
1929 {
1930#ifdef FEAT_VERTSPLIT
1931 topfr->fr_width = width;
1932#endif
1933 topfr->fr_height = height;
1934
1935 if (dir != 'h') /* equalize frame heights */
1936 {
1937 /* Compute maximum number of windows vertically in this frame. */
1938 n = frame_minheight(topfr, NOWIN);
1939 /* add one for the bottom window if it doesn't have a statusline */
1940 if (row + height == cmdline_row && p_ls == 0)
1941 extra_sep = 1;
1942 else
1943 extra_sep = 0;
1944 totwincount = (n + extra_sep) / (p_wmh + 1);
1945 has_next_curwin = frame_has_win(topfr, next_curwin);
1946
1947 /*
1948 * Compute height for "next_curwin" window and room available for
1949 * other windows.
1950 * "m" is the minimal height when counting p_wh for "next_curwin".
1951 */
1952 m = frame_minheight(topfr, next_curwin);
1953 room = height - m;
1954 if (room < 0)
1955 {
1956 /* The room is less then 'winheight', use all space for the
1957 * current window. */
1958 next_curwin_size = p_wh + room;
1959 room = 0;
1960 }
1961 else
1962 {
1963 next_curwin_size = -1;
1964 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1965 {
1966 /* If 'winfixheight' set keep the window height if
1967 * possible.
1968 * Watch out for this window being the next_curwin. */
1969 if (frame_fixed_height(fr))
1970 {
1971 n = frame_minheight(fr, NOWIN);
1972 new_size = fr->fr_height;
1973 if (frame_has_win(fr, next_curwin))
1974 {
1975 room += p_wh - p_wmh;
1976 next_curwin_size = 0;
1977 if (new_size < p_wh)
1978 new_size = p_wh;
1979 }
1980 else
1981 /* These windows don't use up room. */
1982 totwincount -= (n + (fr->fr_next == NULL
1983 ? extra_sep : 0)) / (p_wmh + 1);
1984 room -= new_size - n;
1985 if (room < 0)
1986 {
1987 new_size += room;
1988 room = 0;
1989 }
1990 fr->fr_newheight = new_size;
1991 }
1992 }
1993 if (next_curwin_size == -1)
1994 {
1995 if (!has_next_curwin)
1996 next_curwin_size = 0;
1997 else if (totwincount > 1
1998 && (room + (totwincount - 2))
1999 / (totwincount - 1) > p_wh)
2000 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002001 /* can make all windows higher than 'winheight',
2002 * spread the room equally. */
2003 next_curwin_size = (room + p_wh
2004 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 + (totwincount - 1)) / totwincount;
2006 room -= next_curwin_size - p_wh;
2007 }
2008 else
2009 next_curwin_size = p_wh;
2010 }
2011 }
2012
2013 if (has_next_curwin)
2014 --totwincount; /* don't count curwin */
2015 }
2016
2017 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2018 {
2019 n = m = 0;
2020 wincount = 1;
2021 if (fr->fr_next == NULL)
2022 /* last frame gets all that remains (avoid roundoff error) */
2023 new_size = height;
2024 else if (dir == 'h')
2025 new_size = fr->fr_height;
2026 else if (frame_fixed_height(fr))
2027 {
2028 new_size = fr->fr_newheight;
2029 wincount = 0; /* doesn't count as a sizeable window */
2030 }
2031 else
2032 {
2033 /* Compute the maximum number of windows vert. in "fr". */
2034 n = frame_minheight(fr, NOWIN);
2035 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2036 / (p_wmh + 1);
2037 m = frame_minheight(fr, next_curwin);
2038 if (has_next_curwin)
2039 hnc = frame_has_win(fr, next_curwin);
2040 else
2041 hnc = FALSE;
2042 if (hnc) /* don't count next_curwin */
2043 --wincount;
2044 if (totwincount == 0)
2045 new_size = room;
2046 else
2047 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2048 / totwincount;
2049 if (hnc) /* add next_curwin size */
2050 {
2051 next_curwin_size -= p_wh - (m - n);
2052 new_size += next_curwin_size;
2053 room -= new_size - next_curwin_size;
2054 }
2055 else
2056 room -= new_size;
2057 new_size += n;
2058 }
2059 /* Skip frame that is full width when splitting or closing a
2060 * window, unless equalizing all frames. */
2061 if (!current || dir != 'h' || topfr->fr_parent != NULL
2062 || (new_size != fr->fr_height)
2063 || frame_has_win(fr, next_curwin))
2064 win_equal_rec(next_curwin, current, fr, dir, col, row,
2065 width, new_size);
2066 row += new_size;
2067 height -= new_size;
2068 totwincount -= wincount;
2069 }
2070 }
2071}
2072
2073/*
2074 * close all windows for buffer 'buf'
2075 */
2076 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002077close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002079 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002081 win_T *wp;
2082 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002083 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002086
2087 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002089 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2090#ifdef FEAT_AUTOCMD
2091 && !(wp->w_closing || wp->w_buffer->b_closing)
2092#endif
2093 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002095 win_close(wp, FALSE);
2096
2097 /* Start all over, autocommands may change the window layout. */
2098 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 }
2100 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002101 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002103
2104 /* Also check windows in other tab pages. */
2105 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2106 {
2107 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002108 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002109 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002110 if (wp->w_buffer == buf
2111#ifdef FEAT_AUTOCMD
2112 && !(wp->w_closing || wp->w_buffer->b_closing)
2113#endif
2114 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002115 {
2116 win_close_othertab(wp, FALSE, tp);
2117
2118 /* Start all over, the tab page may be closed and
2119 * autocommands may change the window layout. */
2120 nexttp = first_tabpage;
2121 break;
2122 }
2123 }
2124
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002126
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002127 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002128 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002129 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130}
2131
2132/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002133 * Return TRUE if the current window is the only window that exists (ignoring
2134 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002135 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002136 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002137 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002138last_window()
2139{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002140 return (one_window() && first_tabpage->tp_next == NULL);
2141}
2142
2143/*
2144 * Return TRUE if there is only one window other than "aucmd_win" in the
2145 * current tab page.
2146 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002147 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002148one_window()
2149{
2150#ifdef FEAT_AUTOCMD
2151 win_T *wp;
2152 int seen_one = FALSE;
2153
2154 FOR_ALL_WINDOWS(wp)
2155 {
2156 if (wp != aucmd_win)
2157 {
2158 if (seen_one)
2159 return FALSE;
2160 seen_one = TRUE;
2161 }
2162 }
2163 return TRUE;
2164#else
2165 return firstwin == lastwin;
2166#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002167}
2168
2169/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002170 * Close the possibly last window in a tab page.
2171 * Returns TRUE when the window was closed already.
2172 */
2173 static int
2174close_last_window_tabpage(win, free_buf, prev_curtab)
2175 win_T *win;
2176 int free_buf;
2177 tabpage_T *prev_curtab;
2178{
2179 if (firstwin == lastwin)
2180 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002181#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002182 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002183#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002184
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002185 /*
2186 * Closing the last window in a tab page. First go to another tab
2187 * page and then close the window and the tab page. This avoids that
2188 * curwin and curtab are invalid while we are freeing memory, they may
2189 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002190 * Don't trigger autocommands yet, they may use wrong values, so do
2191 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002192 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002193 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002194 redraw_tabline = TRUE;
2195
2196 /* Safety check: Autocommands may have closed the window when jumping
2197 * to the other tab page. */
2198 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2199 {
2200 int h = tabline_height();
2201
2202 win_close_othertab(win, free_buf, prev_curtab);
2203 if (h != tabline_height())
2204 shell_new_rows();
2205 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002206 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2207 * that now. */
2208#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002209 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002210 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2211 if (old_curbuf != curbuf)
2212 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002213#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002214 return TRUE;
2215 }
2216 return FALSE;
2217}
2218
2219/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002220 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 * If "free_buf" is TRUE related buffer may be unloaded.
2222 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002223 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002224 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002226 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227win_close(win, free_buf)
2228 win_T *win;
2229 int free_buf;
2230{
2231 win_T *wp;
2232#ifdef FEAT_AUTOCMD
2233 int other_buffer = FALSE;
2234#endif
2235 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 int dir;
2237 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002238 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002240 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
2242 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002246#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002247 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002248 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002249 if (win == aucmd_win)
2250 {
2251 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002252 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002253 }
2254 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2255 {
2256 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002257 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002258 }
2259#endif
2260
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002261 /* When closing the last window in a tab page first go to another tab page
2262 * and then close the window and the tab page to avoid that curwin and
2263 * curtab are invalid while we are freeing memory. */
2264 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002265 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 /* When closing the help window, try restoring a snapshot after closing
2268 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002269 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 help_window = TRUE;
2271 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002272 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
2274#ifdef FEAT_AUTOCMD
2275 if (win == curwin)
2276 {
2277 /*
2278 * Guess which window is going to be the new current window.
2279 * This may change because of the autocommands (sigh).
2280 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002281 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002284 * Be careful: If autocommands delete the window or cause this window
2285 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 */
2287 if (wp->w_buffer != curbuf)
2288 {
2289 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002290 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002292 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002293 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002294 win->w_closing = FALSE;
2295 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002296 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002298 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002300 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002301 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002302 win->w_closing = FALSE;
2303 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305# ifdef FEAT_EVAL
2306 /* autocmds may abort script processing */
2307 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002308 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309# endif
2310 }
2311#endif
2312
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002313#ifdef FEAT_GUI
2314 /* Avoid trouble with scrollbars that are going to be deleted in
2315 * win_free(). */
2316 if (gui.in_use)
2317 out_flush();
2318#endif
2319
Bram Moolenaara971b822011-09-14 14:43:25 +02002320#ifdef FEAT_SYN_HL
2321 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002322 if (win->w_buffer != NULL)
2323 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002324#endif
2325
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 /*
2327 * Close the link to the buffer.
2328 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002329 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002330 {
2331#ifdef FEAT_AUTOCMD
2332 win->w_closing = TRUE;
2333#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002334 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002335#ifdef FEAT_AUTOCMD
2336 if (win_valid(win))
2337 win->w_closing = FALSE;
2338#endif
2339 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002340
Bram Moolenaar802418d2013-01-17 14:00:11 +01002341 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2342 && (last_window() || curtab != prev_curtab
2343 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002344 {
2345 /* Autocommands have close all windows, quit now. Restore
2346 * curwin->w_buffer, otherwise writing viminfo may fail. */
2347 if (curwin->w_buffer == NULL)
2348 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002349 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002350 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002353 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002354 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002355 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002356 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
Bram Moolenaara971b822011-09-14 14:43:25 +02002358 /* Free the memory used for the window and get the window that received
2359 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002360 wp = win_free_mem(win, &dir, NULL);
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 /* Make sure curwin isn't invalid. It can cause severe trouble when
2363 * printing an error message. For win_equal() curbuf needs to be valid
2364 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002365 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
2367 curwin = wp;
2368#ifdef FEAT_QUICKFIX
2369 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2370 {
2371 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002372 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * finding another window to go to.
2374 */
2375 for (;;)
2376 {
2377 if (wp->w_next == NULL)
2378 wp = firstwin;
2379 else
2380 wp = wp->w_next;
2381 if (wp == curwin)
2382 break;
2383 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2384 {
2385 curwin = wp;
2386 break;
2387 }
2388 }
2389 }
2390#endif
2391 curbuf = curwin->w_buffer;
2392 close_curwin = TRUE;
2393 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002394 if (p_ea
2395#ifdef FEAT_VERTSPLIT
2396 && (*p_ead == 'b' || *p_ead == dir)
2397#endif
2398 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 win_equal(curwin, TRUE,
2400#ifdef FEAT_VERTSPLIT
2401 dir
2402#else
2403 0
2404#endif
2405 );
2406 else
2407 win_comp_pos();
2408 if (close_curwin)
2409 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002410 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411#ifdef FEAT_AUTOCMD
2412 if (other_buffer)
2413 /* careful: after this wp and win may be invalid! */
2414 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2415#endif
2416 }
2417
2418 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002419 * If last window has a status line now and we don't want one,
2420 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 */
2422 last_status(FALSE);
2423
2424 /* After closing the help window, try restoring the window layout from
2425 * before it was opened. */
2426 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002427 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2430 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2431 if (gui.in_use && !win_hasvertsplit())
2432 gui_init_which_components(NULL);
2433#endif
2434
2435 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002436 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437}
2438
2439/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002440 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002441 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002442 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002443 * Caller must check if buffer is hidden and whether the tabline needs to be
2444 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002445 */
2446 void
2447win_close_othertab(win, free_buf, tp)
2448 win_T *win;
2449 int free_buf;
2450 tabpage_T *tp;
2451{
2452 win_T *wp;
2453 int dir;
2454 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002455 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002456
Bram Moolenaar362ce482012-06-06 19:02:45 +02002457#ifdef FEAT_AUTOCMD
2458 if (win->w_closing || win->w_buffer->b_closing)
2459 return; /* window is already being closed */
2460#endif
2461
Bram Moolenaarf740b292006-02-16 22:11:02 +00002462 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002463 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002464
2465 /* Careful: Autocommands may have closed the tab page or made it the
2466 * current tab page. */
2467 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2468 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002469 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002470 return;
2471
2472 /* Autocommands may have closed the window already. */
2473 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2474 ;
2475 if (wp == NULL)
2476 return;
2477
Bram Moolenaarf740b292006-02-16 22:11:02 +00002478 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002479 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002480 {
2481 if (tp == first_tabpage)
2482 first_tabpage = tp->tp_next;
2483 else
2484 {
2485 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2486 ptp = ptp->tp_next)
2487 ;
2488 if (ptp == NULL)
2489 {
2490 EMSG2(_(e_intern2), "win_close_othertab()");
2491 return;
2492 }
2493 ptp->tp_next = tp->tp_next;
2494 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002495 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002496 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002497
2498 /* Free the memory used for the window. */
2499 win_free_mem(win, &dir, tp);
2500
2501 if (free_tp)
2502 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002503}
2504
2505/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002506 * Free the memory used for a window.
2507 * Returns a pointer to the window that got the freed up space.
2508 */
2509 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002510win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002511 win_T *win;
2512 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002513 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002514{
2515 frame_T *frp;
2516 win_T *wp;
2517
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002518 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002519 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002520 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002521 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002522 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002523
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002524 /* When deleting the current window of another tab page select a new
2525 * current window. */
2526 if (tp != NULL && win == tp->tp_curwin)
2527 tp->tp_curwin = wp;
2528
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002529 return wp;
2530}
2531
2532#if defined(EXITFREE) || defined(PROTO)
2533 void
2534win_free_all()
2535{
2536 int dummy;
2537
Bram Moolenaarf740b292006-02-16 22:11:02 +00002538# ifdef FEAT_WINDOWS
2539 while (first_tabpage->tp_next != NULL)
2540 tabpage_close(TRUE);
2541# endif
2542
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002543# ifdef FEAT_AUTOCMD
2544 if (aucmd_win != NULL)
2545 {
2546 (void)win_free_mem(aucmd_win, &dummy, NULL);
2547 aucmd_win = NULL;
2548 }
2549# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002550
2551 while (firstwin != NULL)
2552 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002553
2554 /* No window should be used after this. Set curwin to NULL to crash
2555 * instead of using freed memory. */
2556 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002557}
2558#endif
2559
2560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 * Remove a window and its frame from the tree of frames.
2562 * Returns a pointer to the window that got the freed up space.
2563 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002564 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002565winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002567 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 frame_T *frp, *frp2, *frp3;
2571 frame_T *frp_close = win->w_frame;
2572 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002575 * If there is only one window there is nothing to remove.
2576 */
2577 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2578 return NULL;
2579
2580 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 * Remove the window from its frame.
2582 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 wp = frame2win(frp2);
2585
2586 /* Remove this frame from the list of frames. */
2587 frame_remove(frp_close);
2588
2589#ifdef FEAT_VERTSPLIT
2590 if (frp_close->fr_parent->fr_layout == FR_COL)
2591 {
2592#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002593 /* When 'winfixheight' is set, try to find another frame in the column
2594 * (as close to the closed frame as possible) to distribute the height
2595 * to. */
2596 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2597 {
2598 frp = frp_close->fr_prev;
2599 frp3 = frp_close->fr_next;
2600 while (frp != NULL || frp3 != NULL)
2601 {
2602 if (frp != NULL)
2603 {
2604 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2605 {
2606 frp2 = frp;
2607 wp = frp->fr_win;
2608 break;
2609 }
2610 frp = frp->fr_prev;
2611 }
2612 if (frp3 != NULL)
2613 {
2614 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2615 {
2616 frp2 = frp3;
2617 wp = frp3->fr_win;
2618 break;
2619 }
2620 frp3 = frp3->fr_next;
2621 }
2622 }
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2625 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626#ifdef FEAT_VERTSPLIT
2627 *dirp = 'v';
2628 }
2629 else
2630 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002631 /* When 'winfixwidth' is set, try to find another frame in the column
2632 * (as close to the closed frame as possible) to distribute the width
2633 * to. */
2634 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2635 {
2636 frp = frp_close->fr_prev;
2637 frp3 = frp_close->fr_next;
2638 while (frp != NULL || frp3 != NULL)
2639 {
2640 if (frp != NULL)
2641 {
2642 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2643 {
2644 frp2 = frp;
2645 wp = frp->fr_win;
2646 break;
2647 }
2648 frp = frp->fr_prev;
2649 }
2650 if (frp3 != NULL)
2651 {
2652 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2653 {
2654 frp2 = frp3;
2655 wp = frp3->fr_win;
2656 break;
2657 }
2658 frp3 = frp3->fr_next;
2659 }
2660 }
2661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002663 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 *dirp = 'h';
2665 }
2666#endif
2667
2668 /* If rows/columns go to a window below/right its positions need to be
2669 * updated. Can only be done after the sizes have been updated. */
2670 if (frp2 == frp_close->fr_next)
2671 {
2672 int row = win->w_winrow;
2673 int col = W_WINCOL(win);
2674
2675 frame_comp_pos(frp2, &row, &col);
2676 }
2677
2678 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2679 {
2680 /* There is no other frame in this list, move its info to the parent
2681 * and remove it. */
2682 frp2->fr_parent->fr_layout = frp2->fr_layout;
2683 frp2->fr_parent->fr_child = frp2->fr_child;
2684 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2685 frp->fr_parent = frp2->fr_parent;
2686 frp2->fr_parent->fr_win = frp2->fr_win;
2687 if (frp2->fr_win != NULL)
2688 frp2->fr_win->w_frame = frp2->fr_parent;
2689 frp = frp2->fr_parent;
2690 vim_free(frp2);
2691
2692 frp2 = frp->fr_parent;
2693 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2694 {
2695 /* The frame above the parent has the same layout, have to merge
2696 * the frames into this list. */
2697 if (frp2->fr_child == frp)
2698 frp2->fr_child = frp->fr_child;
2699 frp->fr_child->fr_prev = frp->fr_prev;
2700 if (frp->fr_prev != NULL)
2701 frp->fr_prev->fr_next = frp->fr_child;
2702 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2703 {
2704 frp3->fr_parent = frp2;
2705 if (frp3->fr_next == NULL)
2706 {
2707 frp3->fr_next = frp->fr_next;
2708 if (frp->fr_next != NULL)
2709 frp->fr_next->fr_prev = frp3;
2710 break;
2711 }
2712 }
2713 vim_free(frp);
2714 }
2715 }
2716
2717 return wp;
2718}
2719
2720/*
2721 * Find out which frame is going to get the freed up space when "win" is
2722 * closed.
2723 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2724 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2725 * This makes opening a window and closing it immediately keep the same window
2726 * layout.
2727 */
2728 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002729win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002731 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
2733 frame_T *frp;
2734 int b;
2735
Bram Moolenaarf740b292006-02-16 22:11:02 +00002736 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002737 /* Last window in this tab page, will go to next tab page. */
2738 return alt_tabpage()->tp_curwin->w_frame;
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 frp = win->w_frame;
2741#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002742 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 b = p_spr;
2744 else
2745#endif
2746 b = p_sb;
2747 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2748 return frp->fr_next;
2749 return frp->fr_prev;
2750}
2751
2752/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002753 * Return the tabpage that will be used if the current one is closed.
2754 */
2755 static tabpage_T *
2756alt_tabpage()
2757{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002758 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002759
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002760 /* Use the next tab page if possible. */
2761 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002762 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002763
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002764 /* Find the last but one tab page. */
2765 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2766 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002767 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002768}
2769
2770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 * Find the left-upper window in frame "frp".
2772 */
2773 static win_T *
2774frame2win(frp)
2775 frame_T *frp;
2776{
2777 while (frp->fr_win == NULL)
2778 frp = frp->fr_child;
2779 return frp->fr_win;
2780}
2781
2782/*
2783 * Return TRUE if frame "frp" contains window "wp".
2784 */
2785 static int
2786frame_has_win(frp, wp)
2787 frame_T *frp;
2788 win_T *wp;
2789{
2790 frame_T *p;
2791
2792 if (frp->fr_layout == FR_LEAF)
2793 return frp->fr_win == wp;
2794
2795 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2796 if (frame_has_win(p, wp))
2797 return TRUE;
2798 return FALSE;
2799}
2800
2801/*
2802 * Set a new height for a frame. Recursively sets the height for contained
2803 * frames and windows. Caller must take care of positions.
2804 */
2805 static void
2806frame_new_height(topfrp, height, topfirst, wfh)
2807 frame_T *topfrp;
2808 int height;
2809 int topfirst; /* resize topmost contained frame first */
2810 int wfh; /* obey 'winfixheight' when there is a choice;
2811 may cause the height not to be set */
2812{
2813 frame_T *frp;
2814 int extra_lines;
2815 int h;
2816
2817 if (topfrp->fr_win != NULL)
2818 {
2819 /* Simple case: just one window. */
2820 win_new_height(topfrp->fr_win,
2821 height - topfrp->fr_win->w_status_height);
2822 }
2823#ifdef FEAT_VERTSPLIT
2824 else if (topfrp->fr_layout == FR_ROW)
2825 {
2826 do
2827 {
2828 /* All frames in this row get the same new height. */
2829 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2830 {
2831 frame_new_height(frp, height, topfirst, wfh);
2832 if (frp->fr_height > height)
2833 {
2834 /* Could not fit the windows, make the whole row higher. */
2835 height = frp->fr_height;
2836 break;
2837 }
2838 }
2839 }
2840 while (frp != NULL);
2841 }
2842#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002843 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 {
2845 /* Complicated case: Resize a column of frames. Resize the bottom
2846 * frame first, frames above that when needed. */
2847
2848 frp = topfrp->fr_child;
2849 if (wfh)
2850 /* Advance past frames with one window with 'wfh' set. */
2851 while (frame_fixed_height(frp))
2852 {
2853 frp = frp->fr_next;
2854 if (frp == NULL)
2855 return; /* no frame without 'wfh', give up */
2856 }
2857 if (!topfirst)
2858 {
2859 /* Find the bottom frame of this column */
2860 while (frp->fr_next != NULL)
2861 frp = frp->fr_next;
2862 if (wfh)
2863 /* Advance back for frames with one window with 'wfh' set. */
2864 while (frame_fixed_height(frp))
2865 frp = frp->fr_prev;
2866 }
2867
2868 extra_lines = height - topfrp->fr_height;
2869 if (extra_lines < 0)
2870 {
2871 /* reduce height of contained frames, bottom or top frame first */
2872 while (frp != NULL)
2873 {
2874 h = frame_minheight(frp, NULL);
2875 if (frp->fr_height + extra_lines < h)
2876 {
2877 extra_lines += frp->fr_height - h;
2878 frame_new_height(frp, h, topfirst, wfh);
2879 }
2880 else
2881 {
2882 frame_new_height(frp, frp->fr_height + extra_lines,
2883 topfirst, wfh);
2884 break;
2885 }
2886 if (topfirst)
2887 {
2888 do
2889 frp = frp->fr_next;
2890 while (wfh && frp != NULL && frame_fixed_height(frp));
2891 }
2892 else
2893 {
2894 do
2895 frp = frp->fr_prev;
2896 while (wfh && frp != NULL && frame_fixed_height(frp));
2897 }
2898 /* Increase "height" if we could not reduce enough frames. */
2899 if (frp == NULL)
2900 height -= extra_lines;
2901 }
2902 }
2903 else if (extra_lines > 0)
2904 {
2905 /* increase height of bottom or top frame */
2906 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2907 }
2908 }
2909 topfrp->fr_height = height;
2910}
2911
2912/*
2913 * Return TRUE if height of frame "frp" should not be changed because of
2914 * the 'winfixheight' option.
2915 */
2916 static int
2917frame_fixed_height(frp)
2918 frame_T *frp;
2919{
2920 /* frame with one window: fixed height if 'winfixheight' set. */
2921 if (frp->fr_win != NULL)
2922 return frp->fr_win->w_p_wfh;
2923
2924 if (frp->fr_layout == FR_ROW)
2925 {
2926 /* The frame is fixed height if one of the frames in the row is fixed
2927 * height. */
2928 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2929 if (frame_fixed_height(frp))
2930 return TRUE;
2931 return FALSE;
2932 }
2933
2934 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2935 * frames in the row are fixed height. */
2936 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2937 if (!frame_fixed_height(frp))
2938 return FALSE;
2939 return TRUE;
2940}
2941
2942#ifdef FEAT_VERTSPLIT
2943/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002944 * Return TRUE if width of frame "frp" should not be changed because of
2945 * the 'winfixwidth' option.
2946 */
2947 static int
2948frame_fixed_width(frp)
2949 frame_T *frp;
2950{
2951 /* frame with one window: fixed width if 'winfixwidth' set. */
2952 if (frp->fr_win != NULL)
2953 return frp->fr_win->w_p_wfw;
2954
2955 if (frp->fr_layout == FR_COL)
2956 {
2957 /* The frame is fixed width if one of the frames in the row is fixed
2958 * width. */
2959 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2960 if (frame_fixed_width(frp))
2961 return TRUE;
2962 return FALSE;
2963 }
2964
2965 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2966 * frames in the row are fixed width. */
2967 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2968 if (!frame_fixed_width(frp))
2969 return FALSE;
2970 return TRUE;
2971}
2972
2973/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 * Add a status line to windows at the bottom of "frp".
2975 * Note: Does not check if there is room!
2976 */
2977 static void
2978frame_add_statusline(frp)
2979 frame_T *frp;
2980{
2981 win_T *wp;
2982
2983 if (frp->fr_layout == FR_LEAF)
2984 {
2985 wp = frp->fr_win;
2986 if (wp->w_status_height == 0)
2987 {
2988 if (wp->w_height > 0) /* don't make it negative */
2989 --wp->w_height;
2990 wp->w_status_height = STATUS_HEIGHT;
2991 }
2992 }
2993 else if (frp->fr_layout == FR_ROW)
2994 {
2995 /* Handle all the frames in the row. */
2996 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2997 frame_add_statusline(frp);
2998 }
2999 else /* frp->fr_layout == FR_COL */
3000 {
3001 /* Only need to handle the last frame in the column. */
3002 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3003 ;
3004 frame_add_statusline(frp);
3005 }
3006}
3007
3008/*
3009 * Set width of a frame. Handles recursively going through contained frames.
3010 * May remove separator line for windows at the right side (for win_close()).
3011 */
3012 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003013frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 frame_T *topfrp;
3015 int width;
3016 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003017 int wfw; /* obey 'winfixwidth' when there is a choice;
3018 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019{
3020 frame_T *frp;
3021 int extra_cols;
3022 int w;
3023 win_T *wp;
3024
3025 if (topfrp->fr_layout == FR_LEAF)
3026 {
3027 /* Simple case: just one window. */
3028 wp = topfrp->fr_win;
3029 /* Find out if there are any windows right of this one. */
3030 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3031 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3032 break;
3033 if (frp->fr_parent == NULL)
3034 wp->w_vsep_width = 0;
3035 win_new_width(wp, width - wp->w_vsep_width);
3036 }
3037 else if (topfrp->fr_layout == FR_COL)
3038 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003039 do
3040 {
3041 /* All frames in this column get the same new width. */
3042 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3043 {
3044 frame_new_width(frp, width, leftfirst, wfw);
3045 if (frp->fr_width > width)
3046 {
3047 /* Could not fit the windows, make whole column wider. */
3048 width = frp->fr_width;
3049 break;
3050 }
3051 }
3052 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054 else /* fr_layout == FR_ROW */
3055 {
3056 /* Complicated case: Resize a row of frames. Resize the rightmost
3057 * frame first, frames left of it when needed. */
3058
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003060 if (wfw)
3061 /* Advance past frames with one window with 'wfw' set. */
3062 while (frame_fixed_width(frp))
3063 {
3064 frp = frp->fr_next;
3065 if (frp == NULL)
3066 return; /* no frame without 'wfw', give up */
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003069 {
3070 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 while (frp->fr_next != NULL)
3072 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003073 if (wfw)
3074 /* Advance back for frames with one window with 'wfw' set. */
3075 while (frame_fixed_width(frp))
3076 frp = frp->fr_prev;
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079 extra_cols = width - topfrp->fr_width;
3080 if (extra_cols < 0)
3081 {
3082 /* reduce frame width, rightmost frame first */
3083 while (frp != NULL)
3084 {
3085 w = frame_minwidth(frp, NULL);
3086 if (frp->fr_width + extra_cols < w)
3087 {
3088 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003089 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091 else
3092 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003093 frame_new_width(frp, frp->fr_width + extra_cols,
3094 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 break;
3096 }
3097 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003098 {
3099 do
3100 frp = frp->fr_next;
3101 while (wfw && frp != NULL && frame_fixed_width(frp));
3102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003104 {
3105 do
3106 frp = frp->fr_prev;
3107 while (wfw && frp != NULL && frame_fixed_width(frp));
3108 }
3109 /* Increase "width" if we could not reduce enough frames. */
3110 if (frp == NULL)
3111 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113 }
3114 else if (extra_cols > 0)
3115 {
3116 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003117 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119 }
3120 topfrp->fr_width = width;
3121}
3122
3123/*
3124 * Add the vertical separator to windows at the right side of "frp".
3125 * Note: Does not check if there is room!
3126 */
3127 static void
3128frame_add_vsep(frp)
3129 frame_T *frp;
3130{
3131 win_T *wp;
3132
3133 if (frp->fr_layout == FR_LEAF)
3134 {
3135 wp = frp->fr_win;
3136 if (wp->w_vsep_width == 0)
3137 {
3138 if (wp->w_width > 0) /* don't make it negative */
3139 --wp->w_width;
3140 wp->w_vsep_width = 1;
3141 }
3142 }
3143 else if (frp->fr_layout == FR_COL)
3144 {
3145 /* Handle all the frames in the column. */
3146 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3147 frame_add_vsep(frp);
3148 }
3149 else /* frp->fr_layout == FR_ROW */
3150 {
3151 /* Only need to handle the last frame in the row. */
3152 frp = frp->fr_child;
3153 while (frp->fr_next != NULL)
3154 frp = frp->fr_next;
3155 frame_add_vsep(frp);
3156 }
3157}
3158
3159/*
3160 * Set frame width from the window it contains.
3161 */
3162 static void
3163frame_fix_width(wp)
3164 win_T *wp;
3165{
3166 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3167}
3168#endif
3169
3170/*
3171 * Set frame height from the window it contains.
3172 */
3173 static void
3174frame_fix_height(wp)
3175 win_T *wp;
3176{
3177 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3178}
3179
3180/*
3181 * Compute the minimal height for frame "topfrp".
3182 * Uses the 'winminheight' option.
3183 * When "next_curwin" isn't NULL, use p_wh for this window.
3184 * When "next_curwin" is NOWIN, don't use at least one line for the current
3185 * window.
3186 */
3187 static int
3188frame_minheight(topfrp, next_curwin)
3189 frame_T *topfrp;
3190 win_T *next_curwin;
3191{
3192 frame_T *frp;
3193 int m;
3194#ifdef FEAT_VERTSPLIT
3195 int n;
3196#endif
3197
3198 if (topfrp->fr_win != NULL)
3199 {
3200 if (topfrp->fr_win == next_curwin)
3201 m = p_wh + topfrp->fr_win->w_status_height;
3202 else
3203 {
3204 /* window: minimal height of the window plus status line */
3205 m = p_wmh + topfrp->fr_win->w_status_height;
3206 /* Current window is minimal one line high */
3207 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3208 ++m;
3209 }
3210 }
3211#ifdef FEAT_VERTSPLIT
3212 else if (topfrp->fr_layout == FR_ROW)
3213 {
3214 /* get the minimal height from each frame in this row */
3215 m = 0;
3216 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3217 {
3218 n = frame_minheight(frp, next_curwin);
3219 if (n > m)
3220 m = n;
3221 }
3222 }
3223#endif
3224 else
3225 {
3226 /* Add up the minimal heights for all frames in this column. */
3227 m = 0;
3228 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3229 m += frame_minheight(frp, next_curwin);
3230 }
3231
3232 return m;
3233}
3234
3235#ifdef FEAT_VERTSPLIT
3236/*
3237 * Compute the minimal width for frame "topfrp".
3238 * When "next_curwin" isn't NULL, use p_wiw for this window.
3239 * When "next_curwin" is NOWIN, don't use at least one column for the current
3240 * window.
3241 */
3242 static int
3243frame_minwidth(topfrp, next_curwin)
3244 frame_T *topfrp;
3245 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3246{
3247 frame_T *frp;
3248 int m, n;
3249
3250 if (topfrp->fr_win != NULL)
3251 {
3252 if (topfrp->fr_win == next_curwin)
3253 m = p_wiw + topfrp->fr_win->w_vsep_width;
3254 else
3255 {
3256 /* window: minimal width of the window plus separator column */
3257 m = p_wmw + topfrp->fr_win->w_vsep_width;
3258 /* Current window is minimal one column wide */
3259 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3260 ++m;
3261 }
3262 }
3263 else if (topfrp->fr_layout == FR_COL)
3264 {
3265 /* get the minimal width from each frame in this column */
3266 m = 0;
3267 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3268 {
3269 n = frame_minwidth(frp, next_curwin);
3270 if (n > m)
3271 m = n;
3272 }
3273 }
3274 else
3275 {
3276 /* Add up the minimal widths for all frames in this row. */
3277 m = 0;
3278 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3279 m += frame_minwidth(frp, next_curwin);
3280 }
3281
3282 return m;
3283}
3284#endif
3285
3286
3287/*
3288 * Try to close all windows except current one.
3289 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3290 * used and the buffer was modified.
3291 *
3292 * Used by ":bdel" and ":only".
3293 */
3294 void
3295close_others(message, forceit)
3296 int message;
3297 int forceit; /* always hide all other windows */
3298{
3299 win_T *wp;
3300 win_T *nextwp;
3301 int r;
3302
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003303 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 if (message
3306#ifdef FEAT_AUTOCMD
3307 && !autocmd_busy
3308#endif
3309 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003310 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return;
3312 }
3313
3314 /* Be very careful here: autocommands may change the window layout. */
3315 for (wp = firstwin; win_valid(wp); wp = nextwp)
3316 {
3317 nextwp = wp->w_next;
3318 if (wp != curwin) /* don't close current window */
3319 {
3320
3321 /* Check if it's allowed to abandon this window */
3322 r = can_abandon(wp->w_buffer, forceit);
3323#ifdef FEAT_AUTOCMD
3324 if (!win_valid(wp)) /* autocommands messed wp up */
3325 {
3326 nextwp = firstwin;
3327 continue;
3328 }
3329#endif
3330 if (!r)
3331 {
3332#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3333 if (message && (p_confirm || cmdmod.confirm) && p_write)
3334 {
3335 dialog_changed(wp->w_buffer, FALSE);
3336# ifdef FEAT_AUTOCMD
3337 if (!win_valid(wp)) /* autocommands messed wp up */
3338 {
3339 nextwp = firstwin;
3340 continue;
3341 }
3342# endif
3343 }
3344 if (bufIsChanged(wp->w_buffer))
3345#endif
3346 continue;
3347 }
3348 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3349 }
3350 }
3351
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003352 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 EMSG(_("E445: Other window contains changes"));
3354}
3355
3356#endif /* FEAT_WINDOWS */
3357
3358/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003359 * Init the current window "curwin".
3360 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 */
3362 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003363curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003365 win_init_empty(curwin);
3366}
3367
3368 void
3369win_init_empty(wp)
3370 win_T *wp;
3371{
3372 redraw_win_later(wp, NOT_VALID);
3373 wp->w_lines_valid = 0;
3374 wp->w_cursor.lnum = 1;
3375 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003377 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003379 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3380 wp->w_pcmark.col = 0;
3381 wp->w_prev_pcmark.lnum = 0;
3382 wp->w_prev_pcmark.col = 0;
3383 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003385 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003387 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003389 if (wp->w_p_rl)
3390 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003392 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003394#ifdef FEAT_SYN_HL
3395 wp->w_s = &wp->w_buffer->b_s;
3396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397}
3398
3399/*
3400 * Allocate the first window and put an empty buffer in it.
3401 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003402 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003404 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405win_alloc_first()
3406{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003407 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003408 return FAIL;
3409
3410#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003411 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003412 if (first_tabpage == NULL)
3413 return FAIL;
3414 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003415 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003416#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003417
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003418 return OK;
3419}
3420
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003421#if defined(FEAT_AUTOCMD) || defined(PROTO)
3422/*
3423 * Init "aucmd_win". This can only be done after the first
3424 * window is fully initialized, thus it can't be in win_alloc_first().
3425 */
3426 void
3427win_alloc_aucmd_win()
3428{
3429 aucmd_win = win_alloc(NULL, TRUE);
3430 if (aucmd_win != NULL)
3431 {
3432 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003433 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003434 new_frame(aucmd_win);
3435 }
3436}
3437#endif
3438
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003439/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003440 * Allocate the first window or the first window in a new tab page.
3441 * When "oldwin" is NULL create an empty buffer for it.
3442 * When "oldwin" is not NULL copy info from it to the new window (only with
3443 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003444 * Return FAIL when something goes wrong (out of memory).
3445 */
3446 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003447win_alloc_firstwin(oldwin)
3448 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003449{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003450 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003451 if (oldwin == NULL)
3452 {
3453 /* Very first window, need to create an empty buffer for it and
3454 * initialize from scratch. */
3455 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3456 if (curwin == NULL || curbuf == NULL)
3457 return FAIL;
3458 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003459#ifdef FEAT_SYN_HL
3460 curwin->w_s = &(curbuf->b_s);
3461#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003462 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003464 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003466 curwin_init(); /* init current window */
3467 }
3468#ifdef FEAT_WINDOWS
3469 else
3470 {
3471 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003472 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003473
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003474 /* We don't want cursor- and scroll-binding in the first window. */
3475 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003476 }
3477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003479 new_frame(curwin);
3480 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003481 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003482 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483#ifdef FEAT_VERTSPLIT
3484 topframe->fr_width = Columns;
3485#endif
3486 topframe->fr_height = Rows - p_ch;
3487 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003488
3489 return OK;
3490}
3491
3492/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003493 * Create a frame for window "wp".
3494 */
3495 static void
3496new_frame(win_T *wp)
3497{
3498 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3499
3500 wp->w_frame = frp;
3501 if (frp != NULL)
3502 {
3503 frp->fr_layout = FR_LEAF;
3504 frp->fr_win = wp;
3505 }
3506}
3507
3508/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003509 * Initialize the window and frame size to the maximum.
3510 */
3511 void
3512win_init_size()
3513{
3514 firstwin->w_height = ROWS_AVAIL;
3515 topframe->fr_height = ROWS_AVAIL;
3516#ifdef FEAT_VERTSPLIT
3517 firstwin->w_width = Columns;
3518 topframe->fr_width = Columns;
3519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520}
3521
3522#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003523
3524/*
3525 * Allocate a new tabpage_T and init the values.
3526 * Returns NULL when out of memory.
3527 */
3528 static tabpage_T *
3529alloc_tabpage()
3530{
3531 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003532# ifdef FEAT_GUI
3533 int i;
3534# endif
3535
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003536
3537 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003538 if (tp == NULL)
3539 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003540
Bram Moolenaar429fa852013-04-15 12:27:36 +02003541# ifdef FEAT_EVAL
3542 /* init t: variables */
3543 tp->tp_vars = dict_alloc();
3544 if (tp->tp_vars == NULL)
3545 {
3546 vim_free(tp);
3547 return NULL;
3548 }
3549 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3550# endif
3551
3552# ifdef FEAT_GUI
3553 for (i = 0; i < 3; i++)
3554 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003555# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003556# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003557 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003558# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003559 tp->tp_ch_used = p_ch;
3560
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003561 return tp;
3562}
3563
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003564 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003565free_tabpage(tp)
3566 tabpage_T *tp;
3567{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003568 int idx;
3569
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003570# ifdef FEAT_DIFF
3571 diff_clear(tp);
3572# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003573 for (idx = 0; idx < SNAP_COUNT; ++idx)
3574 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003575#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003576 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3577 hash_init(&tp->tp_vars->dv_hashtab);
3578 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003579#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003580
3581#ifdef FEAT_PYTHON
3582 python_tabpage_free(tp);
3583#endif
3584
3585#ifdef FEAT_PYTHON3
3586 python3_tabpage_free(tp);
3587#endif
3588
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003589 vim_free(tp);
3590}
3591
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003593 * Create a new Tab page with one window.
3594 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003595 * When "after" is 0 put it just after the current Tab page.
3596 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003597 * Return FAIL or OK.
3598 */
3599 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003600win_new_tabpage(after)
3601 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003602{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003603 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003604 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003605 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003606
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003607 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003608 if (newtp == NULL)
3609 return FAIL;
3610
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003611 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003612 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003613 {
3614 vim_free(newtp);
3615 return FAIL;
3616 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003617 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003618
3619 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003620 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003621 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003622 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003623 if (after == 1)
3624 {
3625 /* New tab page becomes the first one. */
3626 newtp->tp_next = first_tabpage;
3627 first_tabpage = newtp;
3628 }
3629 else
3630 {
3631 if (after > 0)
3632 {
3633 /* Put new tab page before tab page "after". */
3634 n = 2;
3635 for (tp = first_tabpage; tp->tp_next != NULL
3636 && n < after; tp = tp->tp_next)
3637 ++n;
3638 }
3639 newtp->tp_next = tp->tp_next;
3640 tp->tp_next = newtp;
3641 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003643 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003644 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645
3646 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003647 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003648
3649#if defined(FEAT_GUI)
3650 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3651 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003652 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003653#endif
3654
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003655 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003656#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003657 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003658 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003659#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003660 return OK;
3661 }
3662
3663 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003664 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003665 return FAIL;
3666}
3667
3668/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003669 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3670 * like with ":split".
3671 * Returns OK if a new tab page was created, FAIL otherwise.
3672 */
3673 int
3674may_open_tabpage()
3675{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003676 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003677
Bram Moolenaard326ce82007-03-11 14:48:29 +00003678 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003679 {
3680 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003681 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003682 return win_new_tabpage(n);
3683 }
3684 return FAIL;
3685}
3686
3687/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003688 * Create up to "maxcount" tabpages with empty windows.
3689 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003690 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003691 int
3692make_tabpages(maxcount)
3693 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003694{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003695 int count = maxcount;
3696 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003697
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003698 /* Limit to 'tabpagemax' tabs. */
3699 if (count > p_tpm)
3700 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003701
3702#ifdef FEAT_AUTOCMD
3703 /*
3704 * Don't execute autocommands while creating the tab pages. Must do that
3705 * when putting the buffers in the windows.
3706 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003707 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003708#endif
3709
3710 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003711 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003713
3714#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003715 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003716#endif
3717
3718 /* return actual number of tab pages */
3719 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003720}
3721
3722/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003723 * Return TRUE when "tpc" points to a valid tab page.
3724 */
3725 int
3726valid_tabpage(tpc)
3727 tabpage_T *tpc;
3728{
3729 tabpage_T *tp;
3730
3731 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3732 if (tp == tpc)
3733 return TRUE;
3734 return FALSE;
3735}
3736
3737/*
3738 * Find tab page "n" (first one is 1). Returns NULL when not found.
3739 */
3740 tabpage_T *
3741find_tabpage(n)
3742 int n;
3743{
3744 tabpage_T *tp;
3745 int i = 1;
3746
3747 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3748 ++i;
3749 return tp;
3750}
3751
3752/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003753 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003754 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003755 */
3756 int
3757tabpage_index(ftp)
3758 tabpage_T *ftp;
3759{
3760 int i = 1;
3761 tabpage_T *tp;
3762
3763 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3764 ++i;
3765 return i;
3766}
3767
3768/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003769 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003770 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003771 * FAIL.
3772 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003773 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003774 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003775leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003776 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003777 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003778 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003779{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003780 tabpage_T *tp = curtab;
3781
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003782 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003783#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003784 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003785 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003786 if (new_curbuf != curbuf)
3787 {
3788 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3789 if (curtab != tp)
3790 return FAIL;
3791 }
3792 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3793 if (curtab != tp)
3794 return FAIL;
3795 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003796 if (curtab != tp)
3797 return FAIL;
3798 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003799#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003800#if defined(FEAT_GUI)
3801 /* Remove the scrollbars. They may be added back later. */
3802 if (gui.in_use)
3803 gui_remove_scrollbars();
3804#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003805 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003806 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003807 tp->tp_firstwin = firstwin;
3808 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003809 tp->tp_old_Rows = Rows;
3810 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003811 firstwin = NULL;
3812 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003813 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003814}
3815
3816/*
3817 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003818 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003819 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3820 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003821 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003822 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003823enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003824 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003825 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003826 int trigger_enter_autocmds UNUSED;
3827 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003828{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003829 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003830 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003831
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003832 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003833 firstwin = tp->tp_firstwin;
3834 lastwin = tp->tp_lastwin;
3835 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003836
3837 /* We would like doing the TabEnter event first, but we don't have a
3838 * valid current window yet, which may break some commands.
3839 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003840 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3841 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003842 prevwin = next_prevwin;
3843
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003844 last_status(FALSE); /* status line may appear or disappear */
3845 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003846 must_redraw = CLEAR; /* need to redraw everything */
3847#ifdef FEAT_DIFF
3848 diff_need_scrollbind = TRUE;
3849#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003850
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003851 /* The tabpage line may have appeared or disappeared, may need to resize
3852 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003853 * frame sizes too. Use the stored value of p_ch, so that it can be
3854 * different for each tab page. */
3855 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003856 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3857#ifdef FEAT_GUI_TABLINE
3858 && !gui_use_tabline()
3859#endif
3860 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003861 shell_new_rows();
3862#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003863 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003864 shell_new_columns(); /* update window widths */
3865#endif
3866
3867#if defined(FEAT_GUI)
3868 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3869 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003870 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003871#endif
3872
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003873#ifdef FEAT_AUTOCMD
3874 /* Apply autocommands after updating the display, when 'rows' and
3875 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003876 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003877 {
3878 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3879 if (old_curbuf != curbuf)
3880 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3881 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003882#endif
3883
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003884 redraw_all_later(CLEAR);
3885}
3886
3887/*
3888 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003889 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003890 */
3891 void
3892goto_tabpage(n)
3893 int n;
3894{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003895 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003896 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003897 int i;
3898
Bram Moolenaard68071d2006-05-02 22:08:30 +00003899 if (text_locked())
3900 {
3901 /* Not allowed when editing the command line. */
3902#ifdef FEAT_CMDWIN
3903 if (cmdwin_type != 0)
3904 EMSG(_(e_cmdwin));
3905 else
3906#endif
3907 EMSG(_(e_secure));
3908 return;
3909 }
3910
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003911 /* If there is only one it can't work. */
3912 if (first_tabpage->tp_next == NULL)
3913 {
3914 if (n > 1)
3915 beep_flush();
3916 return;
3917 }
3918
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003919 if (n == 0)
3920 {
3921 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003922 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003923 tp = first_tabpage;
3924 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003925 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003926 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003927 else if (n < 0)
3928 {
3929 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3930 * this N times. */
3931 ttp = curtab;
3932 for (i = n; i < 0; ++i)
3933 {
3934 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3935 tp = tp->tp_next)
3936 ;
3937 ttp = tp;
3938 }
3939 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003940 else if (n == 9999)
3941 {
3942 /* Go to last tab page. */
3943 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3944 ;
3945 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003946 else
3947 {
3948 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003949 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003950 if (tp == NULL)
3951 {
3952 beep_flush();
3953 return;
3954 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003955 }
3956
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003957 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003958
3959#ifdef FEAT_GUI_TABLINE
3960 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003961 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003962#endif
3963}
3964
3965/*
3966 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003967 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3968 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003969 * Note: doesn't update the GUI tab.
3970 */
3971 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003972goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003973 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003974 int trigger_enter_autocmds;
3975 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003976{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003977 /* Don't repeat a message in another tab page. */
3978 set_keep_msg(NULL, 0);
3979
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003980 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3981 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003982 {
3983 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003984 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3985 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003986 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003987 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3988 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003989 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003990}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
3992/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003993 * Enter window "wp" in tab page "tp".
3994 * Also updates the GUI tab.
3995 */
3996 void
3997goto_tabpage_win(tp, wp)
3998 tabpage_T *tp;
3999 win_T *wp;
4000{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004001 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004002 if (curtab == tp && win_valid(wp))
4003 {
4004 win_enter(wp, TRUE);
4005# ifdef FEAT_GUI_TABLINE
4006 if (gui_use_tabline())
4007 gui_mch_set_curtab(tabpage_index(curtab));
4008# endif
4009 }
4010}
4011
4012/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004013 * Move the current tab page to before tab page "nr".
4014 */
4015 void
4016tabpage_move(nr)
4017 int nr;
4018{
4019 int n = nr;
4020 tabpage_T *tp;
4021
4022 if (first_tabpage->tp_next == NULL)
4023 return;
4024
4025 /* Remove the current tab page from the list of tab pages. */
4026 if (curtab == first_tabpage)
4027 first_tabpage = curtab->tp_next;
4028 else
4029 {
4030 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4031 if (tp->tp_next == curtab)
4032 break;
4033 if (tp == NULL) /* "cannot happen" */
4034 return;
4035 tp->tp_next = curtab->tp_next;
4036 }
4037
4038 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02004039 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004040 {
4041 curtab->tp_next = first_tabpage;
4042 first_tabpage = curtab;
4043 }
4044 else
4045 {
4046 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
4047 --n;
4048 curtab->tp_next = tp->tp_next;
4049 tp->tp_next = curtab;
4050 }
4051
4052 /* Need to redraw the tabline. Tab page contents doesn't change. */
4053 redraw_tabline = TRUE;
4054}
4055
4056
4057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 * Go to another window.
4059 * When jumping to another buffer, stop Visual mode. Do this before
4060 * changing windows so we can yank the selection into the '*' register.
4061 * When jumping to another window on the same buffer, adjust its cursor
4062 * position to keep the same Visual area.
4063 */
4064 void
4065win_goto(wp)
4066 win_T *wp;
4067{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004068#ifdef FEAT_CONCEAL
4069 win_T *owp = curwin;
4070#endif
4071
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004072 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 {
4074 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004075 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 return;
4077 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078#ifdef FEAT_AUTOCMD
4079 if (curbuf_locked())
4080 return;
4081#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004082
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (wp->w_buffer != curbuf)
4084 reset_VIsual_and_resel();
4085 else if (VIsual_active)
4086 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088#ifdef FEAT_GUI
4089 need_mouse_correct = TRUE;
4090#endif
4091 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004092
4093#ifdef FEAT_CONCEAL
4094 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004095 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004096 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004097 if (curwin->w_p_cole > 0 && !msg_scrolled)
4098 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100}
4101
4102#if defined(FEAT_PERL) || defined(PROTO)
4103/*
4104 * Find window number "winnr" (counting top to bottom).
4105 */
4106 win_T *
4107win_find_nr(winnr)
4108 int winnr;
4109{
4110 win_T *wp;
4111
4112# ifdef FEAT_WINDOWS
4113 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4114 if (--winnr == 0)
4115 break;
4116 return wp;
4117# else
4118 return curwin;
4119# endif
4120}
4121#endif
4122
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004123#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4124 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004125/*
4126 * Find the tabpage for window "win".
4127 */
4128 tabpage_T *
4129win_find_tabpage(win)
4130 win_T *win;
4131{
4132 win_T *wp;
4133 tabpage_T *tp;
4134
4135 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004136 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4137 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004138 if (wp == win)
4139 return tp;
4140 return NULL;
4141}
4142#endif
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144#ifdef FEAT_VERTSPLIT
4145/*
4146 * Move to window above or below "count" times.
4147 */
4148 static void
4149win_goto_ver(up, count)
4150 int up; /* TRUE to go to win above */
4151 long count;
4152{
4153 frame_T *fr;
4154 frame_T *nfr;
4155 frame_T *foundfr;
4156
4157 foundfr = curwin->w_frame;
4158 while (count--)
4159 {
4160 /*
4161 * First go upwards in the tree of frames until we find a upwards or
4162 * downwards neighbor.
4163 */
4164 fr = foundfr;
4165 for (;;)
4166 {
4167 if (fr == topframe)
4168 goto end;
4169 if (up)
4170 nfr = fr->fr_prev;
4171 else
4172 nfr = fr->fr_next;
4173 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4174 break;
4175 fr = fr->fr_parent;
4176 }
4177
4178 /*
4179 * Now go downwards to find the bottom or top frame in it.
4180 */
4181 for (;;)
4182 {
4183 if (nfr->fr_layout == FR_LEAF)
4184 {
4185 foundfr = nfr;
4186 break;
4187 }
4188 fr = nfr->fr_child;
4189 if (nfr->fr_layout == FR_ROW)
4190 {
4191 /* Find the frame at the cursor row. */
4192 while (fr->fr_next != NULL
4193 && frame2win(fr)->w_wincol + fr->fr_width
4194 <= curwin->w_wincol + curwin->w_wcol)
4195 fr = fr->fr_next;
4196 }
4197 if (nfr->fr_layout == FR_COL && up)
4198 while (fr->fr_next != NULL)
4199 fr = fr->fr_next;
4200 nfr = fr;
4201 }
4202 }
4203end:
4204 if (foundfr != NULL)
4205 win_goto(foundfr->fr_win);
4206}
4207
4208/*
4209 * Move to left or right window.
4210 */
4211 static void
4212win_goto_hor(left, count)
4213 int left; /* TRUE to go to left win */
4214 long count;
4215{
4216 frame_T *fr;
4217 frame_T *nfr;
4218 frame_T *foundfr;
4219
4220 foundfr = curwin->w_frame;
4221 while (count--)
4222 {
4223 /*
4224 * First go upwards in the tree of frames until we find a left or
4225 * right neighbor.
4226 */
4227 fr = foundfr;
4228 for (;;)
4229 {
4230 if (fr == topframe)
4231 goto end;
4232 if (left)
4233 nfr = fr->fr_prev;
4234 else
4235 nfr = fr->fr_next;
4236 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4237 break;
4238 fr = fr->fr_parent;
4239 }
4240
4241 /*
4242 * Now go downwards to find the leftmost or rightmost frame in it.
4243 */
4244 for (;;)
4245 {
4246 if (nfr->fr_layout == FR_LEAF)
4247 {
4248 foundfr = nfr;
4249 break;
4250 }
4251 fr = nfr->fr_child;
4252 if (nfr->fr_layout == FR_COL)
4253 {
4254 /* Find the frame at the cursor row. */
4255 while (fr->fr_next != NULL
4256 && frame2win(fr)->w_winrow + fr->fr_height
4257 <= curwin->w_winrow + curwin->w_wrow)
4258 fr = fr->fr_next;
4259 }
4260 if (nfr->fr_layout == FR_ROW && left)
4261 while (fr->fr_next != NULL)
4262 fr = fr->fr_next;
4263 nfr = fr;
4264 }
4265 }
4266end:
4267 if (foundfr != NULL)
4268 win_goto(foundfr->fr_win);
4269}
4270#endif
4271
4272/*
4273 * Make window "wp" the current window.
4274 */
4275 void
4276win_enter(wp, undo_sync)
4277 win_T *wp;
4278 int undo_sync;
4279{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004280 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281}
4282
4283/*
4284 * Make window wp the current window.
4285 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4286 * been closed and isn't valid.
4287 */
4288 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004289win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 win_T *wp;
4291 int undo_sync;
4292 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004293 int trigger_enter_autocmds UNUSED;
4294 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295{
4296#ifdef FEAT_AUTOCMD
4297 int other_buffer = FALSE;
4298#endif
4299
4300 if (wp == curwin && !curwin_invalid) /* nothing to do */
4301 return;
4302
4303#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004304 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 {
4306 /*
4307 * Be careful: If autocommands delete the window, return now.
4308 */
4309 if (wp->w_buffer != curbuf)
4310 {
4311 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4312 other_buffer = TRUE;
4313 if (!win_valid(wp))
4314 return;
4315 }
4316 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4317 if (!win_valid(wp))
4318 return;
4319# ifdef FEAT_EVAL
4320 /* autocmds may abort script processing */
4321 if (aborting())
4322 return;
4323# endif
4324 }
4325#endif
4326
4327 /* sync undo before leaving the current buffer */
4328 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004329 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004330
4331 /* Might need to scroll the old window before switching, e.g., when the
4332 * cursor was moved. */
4333 update_topline();
4334
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 /* may have to copy the buffer options when 'cpo' contains 'S' */
4336 if (wp->w_buffer != curbuf)
4337 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4338 if (!curwin_invalid)
4339 {
4340 prevwin = curwin; /* remember for CTRL-W p */
4341 curwin->w_redr_status = TRUE;
4342 }
4343 curwin = wp;
4344 curbuf = wp->w_buffer;
4345 check_cursor();
4346#ifdef FEAT_VIRTUALEDIT
4347 if (!virtual_active())
4348 curwin->w_cursor.coladd = 0;
4349#endif
4350 changed_line_abv_curs(); /* assume cursor position needs updating */
4351
4352 if (curwin->w_localdir != NULL)
4353 {
4354 /* Window has a local directory: Save current directory as global
4355 * directory (unless that was done already) and change to the local
4356 * directory. */
4357 if (globaldir == NULL)
4358 {
4359 char_u cwd[MAXPATHL];
4360
4361 if (mch_dirname(cwd, MAXPATHL) == OK)
4362 globaldir = vim_strsave(cwd);
4363 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004364 if (mch_chdir((char *)curwin->w_localdir) == 0)
4365 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 else if (globaldir != NULL)
4368 {
4369 /* Window doesn't have a local directory and we are not in the global
4370 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004371 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 vim_free(globaldir);
4373 globaldir = NULL;
4374 shorten_fnames(TRUE);
4375 }
4376
4377#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004378 if (trigger_enter_autocmds)
4379 {
4380 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4381 if (other_buffer)
4382 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#endif
4385
4386#ifdef FEAT_TITLE
4387 maketitle();
4388#endif
4389 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004390 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 if (restart_edit)
4392 redraw_later(VALID); /* causes status line redraw */
4393
4394 /* set window height to desired minimal value */
4395 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4396 win_setheight((int)p_wh);
4397 else if (curwin->w_height == 0)
4398 win_setheight(1);
4399
4400#ifdef FEAT_VERTSPLIT
4401 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004402 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 win_setwidth((int)p_wiw);
4404#endif
4405
4406#ifdef FEAT_MOUSE
4407 setmouse(); /* in case jumped to/from help buffer */
4408#endif
4409
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004410 /* Change directories when the 'acd' option is set. */
4411 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412}
4413
4414#endif /* FEAT_WINDOWS */
4415
4416#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4417/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004418 * Jump to the first open window that contains buffer "buf", if one exists.
4419 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 */
4421 win_T *
4422buf_jump_open_win(buf)
4423 buf_T *buf;
4424{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004425 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004427 if (curwin->w_buffer == buf)
4428 wp = curwin;
4429# ifdef FEAT_WINDOWS
4430 else
4431 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4432 if (wp->w_buffer == buf)
4433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (wp != NULL)
4435 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004437 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004439
4440/*
4441 * Jump to the first open window in any tab page that contains buffer "buf",
4442 * if one exists.
4443 * Returns a pointer to the window found, otherwise NULL.
4444 */
4445 win_T *
4446buf_jump_open_tab(buf)
4447 buf_T *buf;
4448{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004449 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004450# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004451 tabpage_T *tp;
4452
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004453 if (wp != NULL)
4454 return wp;
4455
4456 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4457 if (tp != curtab)
4458 {
4459 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4460 if (wp->w_buffer == buf)
4461 break;
4462 if (wp != NULL)
4463 {
4464 goto_tabpage_win(tp, wp);
4465 if (curwin != wp)
4466 wp = NULL; /* something went wrong */
4467 break;
4468 }
4469 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004470# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004471 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004472}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
4474
4475/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004476 * Allocate a window structure and link it in the window list when "hidden" is
4477 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004480win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004481 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004482 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004484 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 /*
4487 * allocate window structure and linesizes arrays
4488 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004489 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004490 if (new_wp == NULL)
4491 return NULL;
4492
4493 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004495 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004496 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498
Bram Moolenaar429fa852013-04-15 12:27:36 +02004499#ifdef FEAT_EVAL
4500 /* init w: variables */
4501 new_wp->w_vars = dict_alloc();
4502 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004504 win_free_lsize(new_wp);
4505 vim_free(new_wp);
4506 return NULL;
4507 }
4508 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004509#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004510
4511#ifdef FEAT_AUTOCMD
4512 /* Don't execute autocommands while the window is not properly
4513 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4514 * event. */
4515 block_autocmds();
4516#endif
4517 /*
4518 * link the window in the window list
4519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004521 if (!hidden)
4522 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523#endif
4524#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004525 new_wp->w_wincol = 0;
4526 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#endif
4528
Bram Moolenaar429fa852013-04-15 12:27:36 +02004529 /* position the display and the cursor at the top of the file. */
4530 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004532 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004534 new_wp->w_botline = 2;
4535 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004537 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#endif
4539
Bram Moolenaar429fa852013-04-15 12:27:36 +02004540 /* We won't calculate w_fraction until resizing the window */
4541 new_wp->w_fraction = 0;
4542 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
4544#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004545 if (gui.in_use)
4546 {
4547 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4548 SBAR_LEFT, new_wp);
4549 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4550 SBAR_RIGHT, new_wp);
4551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#endif
4553#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004554 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004556#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004557 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004558#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004559#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004560 new_wp->w_match_head = NULL;
4561 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004562#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004563 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564}
4565
4566#if defined(FEAT_WINDOWS) || defined(PROTO)
4567
4568/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004569 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 */
4571 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004572win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004574 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575{
4576 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004577 buf_T *buf;
4578 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004580#ifdef FEAT_FOLDING
4581 clearFolding(wp);
4582#endif
4583
4584 /* reduce the reference count to the argument list. */
4585 alist_unlink(wp->w_alist);
4586
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004587#ifdef FEAT_AUTOCMD
4588 /* Don't execute autocommands while the window is halfway being deleted.
4589 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004590 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004591#endif
4592
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004593#ifdef FEAT_LUA
4594 lua_window_free(wp);
4595#endif
4596
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004597#ifdef FEAT_MZSCHEME
4598 mzscheme_window_free(wp);
4599#endif
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#ifdef FEAT_PERL
4602 perl_win_free(wp);
4603#endif
4604
4605#ifdef FEAT_PYTHON
4606 python_window_free(wp);
4607#endif
4608
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004609#ifdef FEAT_PYTHON3
4610 python3_window_free(wp);
4611#endif
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#ifdef FEAT_TCL
4614 tcl_window_free(wp);
4615#endif
4616
4617#ifdef FEAT_RUBY
4618 ruby_window_free(wp);
4619#endif
4620
4621 clear_winopt(&wp->w_onebuf_opt);
4622 clear_winopt(&wp->w_allbuf_opt);
4623
4624#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004625 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4626 hash_init(&wp->w_vars->dv_hashtab);
4627 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628#endif
4629
4630 if (prevwin == wp)
4631 prevwin = NULL;
4632 win_free_lsize(wp);
4633
4634 for (i = 0; i < wp->w_tagstacklen; ++i)
4635 vim_free(wp->w_tagstack[i].tagname);
4636
4637 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004638
Bram Moolenaarff18df02013-07-24 17:51:57 +02004639 /* Remove the window from the b_wininfo lists, it may happen that the
4640 * freed memory is re-used for another window. */
4641 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4642 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4643 if (wip->wi_win == wp)
4644 wip->wi_win = NULL;
4645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004647 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650#ifdef FEAT_JUMPLIST
4651 free_jumplist(wp);
4652#endif
4653
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004654#ifdef FEAT_QUICKFIX
4655 qf_free_all(wp);
4656#endif
4657
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#ifdef FEAT_GUI
4659 if (gui.in_use)
4660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4662 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4663 }
4664#endif /* FEAT_GUI */
4665
Bram Moolenaar860cae12010-06-05 23:22:07 +02004666#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004667 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004668#endif
4669
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004670#ifdef FEAT_AUTOCMD
4671 if (wp != aucmd_win)
4672#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004673 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004674#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004675 if (autocmd_busy)
4676 {
4677 wp->w_next = au_pending_free_win;
4678 au_pending_free_win = wp;
4679 }
4680 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004681#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004682 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004683
4684#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004685 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687}
4688
4689/*
4690 * Append window "wp" in the window list after window "after".
4691 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004692 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693win_append(after, wp)
4694 win_T *after, *wp;
4695{
4696 win_T *before;
4697
4698 if (after == NULL) /* after NULL is in front of the first */
4699 before = firstwin;
4700 else
4701 before = after->w_next;
4702
4703 wp->w_next = before;
4704 wp->w_prev = after;
4705 if (after == NULL)
4706 firstwin = wp;
4707 else
4708 after->w_next = wp;
4709 if (before == NULL)
4710 lastwin = wp;
4711 else
4712 before->w_prev = wp;
4713}
4714
4715/*
4716 * Remove a window from the window list.
4717 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004718 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004719win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004721 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
4723 if (wp->w_prev != NULL)
4724 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004725 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004727 else
4728 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (wp->w_next != NULL)
4730 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004731 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004733 else
4734 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735}
4736
4737/*
4738 * Append frame "frp" in a frame list after frame "after".
4739 */
4740 static void
4741frame_append(after, frp)
4742 frame_T *after, *frp;
4743{
4744 frp->fr_next = after->fr_next;
4745 after->fr_next = frp;
4746 if (frp->fr_next != NULL)
4747 frp->fr_next->fr_prev = frp;
4748 frp->fr_prev = after;
4749}
4750
4751/*
4752 * Insert frame "frp" in a frame list before frame "before".
4753 */
4754 static void
4755frame_insert(before, frp)
4756 frame_T *before, *frp;
4757{
4758 frp->fr_next = before;
4759 frp->fr_prev = before->fr_prev;
4760 before->fr_prev = frp;
4761 if (frp->fr_prev != NULL)
4762 frp->fr_prev->fr_next = frp;
4763 else
4764 frp->fr_parent->fr_child = frp;
4765}
4766
4767/*
4768 * Remove a frame from a frame list.
4769 */
4770 static void
4771frame_remove(frp)
4772 frame_T *frp;
4773{
4774 if (frp->fr_prev != NULL)
4775 frp->fr_prev->fr_next = frp->fr_next;
4776 else
4777 frp->fr_parent->fr_child = frp->fr_next;
4778 if (frp->fr_next != NULL)
4779 frp->fr_next->fr_prev = frp->fr_prev;
4780}
4781
4782#endif /* FEAT_WINDOWS */
4783
4784/*
4785 * Allocate w_lines[] for window "wp".
4786 * Return FAIL for failure, OK for success.
4787 */
4788 int
4789win_alloc_lines(wp)
4790 win_T *wp;
4791{
4792 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004793 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 if (wp->w_lines == NULL)
4795 return FAIL;
4796 return OK;
4797}
4798
4799/*
4800 * free lsize arrays for a window
4801 */
4802 void
4803win_free_lsize(wp)
4804 win_T *wp;
4805{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004806 /* TODO: why would wp be NULL here? */
4807 if (wp != NULL)
4808 {
4809 vim_free(wp->w_lines);
4810 wp->w_lines = NULL;
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812}
4813
4814/*
4815 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004816 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 */
4818 void
4819shell_new_rows()
4820{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004821 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822
4823 if (firstwin == NULL) /* not initialized yet */
4824 return;
4825#ifdef FEAT_WINDOWS
4826 if (h < frame_minheight(topframe, NULL))
4827 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004828
4829 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 * that doesn't result in the right height, forget about that option. */
4831 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004832 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 frame_new_height(topframe, h, FALSE, FALSE);
4834
4835 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4836#else
4837 if (h < 1)
4838 h = 1;
4839 win_new_height(firstwin, h);
4840#endif
4841 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004842#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004843 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004844#endif
4845
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846#if 0
4847 /* Disabled: don't want making the screen smaller make a window larger. */
4848 if (p_ea)
4849 win_equal(curwin, FALSE, 'v');
4850#endif
4851}
4852
4853#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4854/*
4855 * Called from win_new_shellsize() after Columns changed.
4856 */
4857 void
4858shell_new_columns()
4859{
4860 if (firstwin == NULL) /* not initialized yet */
4861 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004862
4863 /* First try setting the widths of windows with 'winfixwidth'. If that
4864 * doesn't result in the right width, forget about that option. */
4865 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004866 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004867 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4868
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4870#if 0
4871 /* Disabled: don't want making the screen smaller make a window larger. */
4872 if (p_ea)
4873 win_equal(curwin, FALSE, 'h');
4874#endif
4875}
4876#endif
4877
4878#if defined(FEAT_CMDWIN) || defined(PROTO)
4879/*
4880 * Save the size of all windows in "gap".
4881 */
4882 void
4883win_size_save(gap)
4884 garray_T *gap;
4885
4886{
4887 win_T *wp;
4888
4889 ga_init2(gap, (int)sizeof(int), 1);
4890 if (ga_grow(gap, win_count() * 2) == OK)
4891 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4892 {
4893 ((int *)gap->ga_data)[gap->ga_len++] =
4894 wp->w_width + wp->w_vsep_width;
4895 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4896 }
4897}
4898
4899/*
4900 * Restore window sizes, but only if the number of windows is still the same.
4901 * Does not free the growarray.
4902 */
4903 void
4904win_size_restore(gap)
4905 garray_T *gap;
4906{
4907 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004908 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909
4910 if (win_count() * 2 == gap->ga_len)
4911 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004912 /* The order matters, because frames contain other frames, but it's
4913 * difficult to get right. The easy way out is to do it twice. */
4914 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004916 i = 0;
4917 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4918 {
4919 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4920 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 /* recompute the window positions */
4924 (void)win_comp_pos();
4925 }
4926}
4927#endif /* FEAT_CMDWIN */
4928
4929#if defined(FEAT_WINDOWS) || defined(PROTO)
4930/*
4931 * Update the position for all windows, using the width and height of the
4932 * frames.
4933 * Returns the row just after the last window.
4934 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004935 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936win_comp_pos()
4937{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004938 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 int col = 0;
4940
4941 frame_comp_pos(topframe, &row, &col);
4942 return row;
4943}
4944
4945/*
4946 * Update the position of the windows in frame "topfrp", using the width and
4947 * height of the frames.
4948 * "*row" and "*col" are the top-left position of the frame. They are updated
4949 * to the bottom-right position plus one.
4950 */
4951 static void
4952frame_comp_pos(topfrp, row, col)
4953 frame_T *topfrp;
4954 int *row;
4955 int *col;
4956{
4957 win_T *wp;
4958 frame_T *frp;
4959#ifdef FEAT_VERTSPLIT
4960 int startcol;
4961 int startrow;
4962#endif
4963
4964 wp = topfrp->fr_win;
4965 if (wp != NULL)
4966 {
4967 if (wp->w_winrow != *row
4968#ifdef FEAT_VERTSPLIT
4969 || wp->w_wincol != *col
4970#endif
4971 )
4972 {
4973 /* position changed, redraw */
4974 wp->w_winrow = *row;
4975#ifdef FEAT_VERTSPLIT
4976 wp->w_wincol = *col;
4977#endif
4978 redraw_win_later(wp, NOT_VALID);
4979 wp->w_redr_status = TRUE;
4980 }
4981 *row += wp->w_height + wp->w_status_height;
4982#ifdef FEAT_VERTSPLIT
4983 *col += wp->w_width + wp->w_vsep_width;
4984#endif
4985 }
4986 else
4987 {
4988#ifdef FEAT_VERTSPLIT
4989 startrow = *row;
4990 startcol = *col;
4991#endif
4992 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4993 {
4994#ifdef FEAT_VERTSPLIT
4995 if (topfrp->fr_layout == FR_ROW)
4996 *row = startrow; /* all frames are at the same row */
4997 else
4998 *col = startcol; /* all frames are at the same col */
4999#endif
5000 frame_comp_pos(frp, row, col);
5001 }
5002 }
5003}
5004
5005#endif /* FEAT_WINDOWS */
5006
5007/*
5008 * Set current window height and take care of repositioning other windows to
5009 * fit around it.
5010 */
5011 void
5012win_setheight(height)
5013 int height;
5014{
5015 win_setheight_win(height, curwin);
5016}
5017
5018/*
5019 * Set the window height of window "win" and take care of repositioning other
5020 * windows to fit around it.
5021 */
5022 void
5023win_setheight_win(height, win)
5024 int height;
5025 win_T *win;
5026{
5027 int row;
5028
5029 if (win == curwin)
5030 {
5031 /* Always keep current window at least one line high, even when
5032 * 'winminheight' is zero. */
5033#ifdef FEAT_WINDOWS
5034 if (height < p_wmh)
5035 height = p_wmh;
5036#endif
5037 if (height == 0)
5038 height = 1;
5039 }
5040
5041#ifdef FEAT_WINDOWS
5042 frame_setheight(win->w_frame, height + win->w_status_height);
5043
5044 /* recompute the window positions */
5045 row = win_comp_pos();
5046#else
5047 if (height > topframe->fr_height)
5048 height = topframe->fr_height;
5049 win->w_height = height;
5050 row = height;
5051#endif
5052
5053 /*
5054 * If there is extra space created between the last window and the command
5055 * line, clear it.
5056 */
5057 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5058 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5059 cmdline_row = row;
5060 msg_row = row;
5061 msg_col = 0;
5062
5063 redraw_all_later(NOT_VALID);
5064}
5065
5066#if defined(FEAT_WINDOWS) || defined(PROTO)
5067
5068/*
5069 * Set the height of a frame to "height" and take care that all frames and
5070 * windows inside it are resized. Also resize frames on the left and right if
5071 * the are in the same FR_ROW frame.
5072 *
5073 * Strategy:
5074 * If the frame is part of a FR_COL frame, try fitting the frame in that
5075 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5076 * go to containing frames to resize them and make room.
5077 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5078 * Check for the minimal height of the FR_ROW frame.
5079 * At the top level we can also use change the command line height.
5080 */
5081 static void
5082frame_setheight(curfrp, height)
5083 frame_T *curfrp;
5084 int height;
5085{
5086 int room; /* total number of lines available */
5087 int take; /* number of lines taken from other windows */
5088 int room_cmdline; /* lines available from cmdline */
5089 int run;
5090 frame_T *frp;
5091 int h;
5092 int room_reserved;
5093
5094 /* If the height already is the desired value, nothing to do. */
5095 if (curfrp->fr_height == height)
5096 return;
5097
5098 if (curfrp->fr_parent == NULL)
5099 {
5100 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005101 if (height > ROWS_AVAIL)
5102 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (height > 0)
5104 frame_new_height(curfrp, height, FALSE, FALSE);
5105 }
5106 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5107 {
5108 /* Row of frames: Also need to resize frames left and right of this
5109 * one. First check for the minimal height of these. */
5110 h = frame_minheight(curfrp->fr_parent, NULL);
5111 if (height < h)
5112 height = h;
5113 frame_setheight(curfrp->fr_parent, height);
5114 }
5115 else
5116 {
5117 /*
5118 * Column of frames: try to change only frames in this column.
5119 */
5120#ifdef FEAT_VERTSPLIT
5121 /*
5122 * Do this twice:
5123 * 1: compute room available, if it's not enough try resizing the
5124 * containing frame.
5125 * 2: compute the room available and adjust the height to it.
5126 * Try not to reduce the height of a window with 'winfixheight' set.
5127 */
5128 for (run = 1; run <= 2; ++run)
5129#else
5130 for (;;)
5131#endif
5132 {
5133 room = 0;
5134 room_reserved = 0;
5135 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5136 frp = frp->fr_next)
5137 {
5138 if (frp != curfrp
5139 && frp->fr_win != NULL
5140 && frp->fr_win->w_p_wfh)
5141 room_reserved += frp->fr_height;
5142 room += frp->fr_height;
5143 if (frp != curfrp)
5144 room -= frame_minheight(frp, NULL);
5145 }
5146#ifdef FEAT_VERTSPLIT
5147 if (curfrp->fr_width != Columns)
5148 room_cmdline = 0;
5149 else
5150#endif
5151 {
5152 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5153 + lastwin->w_height + lastwin->w_status_height);
5154 if (room_cmdline < 0)
5155 room_cmdline = 0;
5156 }
5157
5158 if (height <= room + room_cmdline)
5159 break;
5160#ifdef FEAT_VERTSPLIT
5161 if (run == 2 || curfrp->fr_width == Columns)
5162#endif
5163 {
5164 if (height > room + room_cmdline)
5165 height = room + room_cmdline;
5166 break;
5167 }
5168#ifdef FEAT_VERTSPLIT
5169 frame_setheight(curfrp->fr_parent, height
5170 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5171#endif
5172 /*NOTREACHED*/
5173 }
5174
5175 /*
5176 * Compute the number of lines we will take from others frames (can be
5177 * negative!).
5178 */
5179 take = height - curfrp->fr_height;
5180
5181 /* If there is not enough room, also reduce the height of a window
5182 * with 'winfixheight' set. */
5183 if (height > room + room_cmdline - room_reserved)
5184 room_reserved = room + room_cmdline - height;
5185 /* If there is only a 'winfixheight' window and making the
5186 * window smaller, need to make the other window taller. */
5187 if (take < 0 && room - curfrp->fr_height < room_reserved)
5188 room_reserved = 0;
5189
5190 if (take > 0 && room_cmdline > 0)
5191 {
5192 /* use lines from cmdline first */
5193 if (take < room_cmdline)
5194 room_cmdline = take;
5195 take -= room_cmdline;
5196 topframe->fr_height += room_cmdline;
5197 }
5198
5199 /*
5200 * set the current frame to the new height
5201 */
5202 frame_new_height(curfrp, height, FALSE, FALSE);
5203
5204 /*
5205 * First take lines from the frames after the current frame. If
5206 * that is not enough, takes lines from frames above the current
5207 * frame.
5208 */
5209 for (run = 0; run < 2; ++run)
5210 {
5211 if (run == 0)
5212 frp = curfrp->fr_next; /* 1st run: start with next window */
5213 else
5214 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5215 while (frp != NULL && take != 0)
5216 {
5217 h = frame_minheight(frp, NULL);
5218 if (room_reserved > 0
5219 && frp->fr_win != NULL
5220 && frp->fr_win->w_p_wfh)
5221 {
5222 if (room_reserved >= frp->fr_height)
5223 room_reserved -= frp->fr_height;
5224 else
5225 {
5226 if (frp->fr_height - room_reserved > take)
5227 room_reserved = frp->fr_height - take;
5228 take -= frp->fr_height - room_reserved;
5229 frame_new_height(frp, room_reserved, FALSE, FALSE);
5230 room_reserved = 0;
5231 }
5232 }
5233 else
5234 {
5235 if (frp->fr_height - take < h)
5236 {
5237 take -= frp->fr_height - h;
5238 frame_new_height(frp, h, FALSE, FALSE);
5239 }
5240 else
5241 {
5242 frame_new_height(frp, frp->fr_height - take,
5243 FALSE, FALSE);
5244 take = 0;
5245 }
5246 }
5247 if (run == 0)
5248 frp = frp->fr_next;
5249 else
5250 frp = frp->fr_prev;
5251 }
5252 }
5253 }
5254}
5255
5256#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5257/*
5258 * Set current window width and take care of repositioning other windows to
5259 * fit around it.
5260 */
5261 void
5262win_setwidth(width)
5263 int width;
5264{
5265 win_setwidth_win(width, curwin);
5266}
5267
5268 void
5269win_setwidth_win(width, wp)
5270 int width;
5271 win_T *wp;
5272{
5273 /* Always keep current window at least one column wide, even when
5274 * 'winminwidth' is zero. */
5275 if (wp == curwin)
5276 {
5277 if (width < p_wmw)
5278 width = p_wmw;
5279 if (width == 0)
5280 width = 1;
5281 }
5282
5283 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5284
5285 /* recompute the window positions */
5286 (void)win_comp_pos();
5287
5288 redraw_all_later(NOT_VALID);
5289}
5290
5291/*
5292 * Set the width of a frame to "width" and take care that all frames and
5293 * windows inside it are resized. Also resize frames above and below if the
5294 * are in the same FR_ROW frame.
5295 *
5296 * Strategy is similar to frame_setheight().
5297 */
5298 static void
5299frame_setwidth(curfrp, width)
5300 frame_T *curfrp;
5301 int width;
5302{
5303 int room; /* total number of lines available */
5304 int take; /* number of lines taken from other windows */
5305 int run;
5306 frame_T *frp;
5307 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005308 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
5310 /* If the width already is the desired value, nothing to do. */
5311 if (curfrp->fr_width == width)
5312 return;
5313
5314 if (curfrp->fr_parent == NULL)
5315 /* topframe: can't change width */
5316 return;
5317
5318 if (curfrp->fr_parent->fr_layout == FR_COL)
5319 {
5320 /* Column of frames: Also need to resize frames above and below of
5321 * this one. First check for the minimal width of these. */
5322 w = frame_minwidth(curfrp->fr_parent, NULL);
5323 if (width < w)
5324 width = w;
5325 frame_setwidth(curfrp->fr_parent, width);
5326 }
5327 else
5328 {
5329 /*
5330 * Row of frames: try to change only frames in this row.
5331 *
5332 * Do this twice:
5333 * 1: compute room available, if it's not enough try resizing the
5334 * containing frame.
5335 * 2: compute the room available and adjust the width to it.
5336 */
5337 for (run = 1; run <= 2; ++run)
5338 {
5339 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005340 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5342 frp = frp->fr_next)
5343 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005344 if (frp != curfrp
5345 && frp->fr_win != NULL
5346 && frp->fr_win->w_p_wfw)
5347 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 room += frp->fr_width;
5349 if (frp != curfrp)
5350 room -= frame_minwidth(frp, NULL);
5351 }
5352
5353 if (width <= room)
5354 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005355 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 {
5357 if (width > room)
5358 width = room;
5359 break;
5360 }
5361 frame_setwidth(curfrp->fr_parent, width
5362 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5363 }
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 /*
5366 * Compute the number of lines we will take from others frames (can be
5367 * negative!).
5368 */
5369 take = width - curfrp->fr_width;
5370
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005371 /* If there is not enough room, also reduce the width of a window
5372 * with 'winfixwidth' set. */
5373 if (width > room - room_reserved)
5374 room_reserved = room - width;
5375 /* If there is only a 'winfixwidth' window and making the
5376 * window smaller, need to make the other window narrower. */
5377 if (take < 0 && room - curfrp->fr_width < room_reserved)
5378 room_reserved = 0;
5379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 /*
5381 * set the current frame to the new width
5382 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005383 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385 /*
5386 * First take lines from the frames right of the current frame. If
5387 * that is not enough, takes lines from frames left of the current
5388 * frame.
5389 */
5390 for (run = 0; run < 2; ++run)
5391 {
5392 if (run == 0)
5393 frp = curfrp->fr_next; /* 1st run: start with next window */
5394 else
5395 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5396 while (frp != NULL && take != 0)
5397 {
5398 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005399 if (room_reserved > 0
5400 && frp->fr_win != NULL
5401 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005403 if (room_reserved >= frp->fr_width)
5404 room_reserved -= frp->fr_width;
5405 else
5406 {
5407 if (frp->fr_width - room_reserved > take)
5408 room_reserved = frp->fr_width - take;
5409 take -= frp->fr_width - room_reserved;
5410 frame_new_width(frp, room_reserved, FALSE, FALSE);
5411 room_reserved = 0;
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 }
5414 else
5415 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005416 if (frp->fr_width - take < w)
5417 {
5418 take -= frp->fr_width - w;
5419 frame_new_width(frp, w, FALSE, FALSE);
5420 }
5421 else
5422 {
5423 frame_new_width(frp, frp->fr_width - take,
5424 FALSE, FALSE);
5425 take = 0;
5426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
5428 if (run == 0)
5429 frp = frp->fr_next;
5430 else
5431 frp = frp->fr_prev;
5432 }
5433 }
5434 }
5435}
5436#endif /* FEAT_VERTSPLIT */
5437
5438/*
5439 * Check 'winminheight' for a valid value.
5440 */
5441 void
5442win_setminheight()
5443{
5444 int room;
5445 int first = TRUE;
5446 win_T *wp;
5447
5448 /* loop until there is a 'winminheight' that is possible */
5449 while (p_wmh > 0)
5450 {
5451 /* TODO: handle vertical splits */
5452 room = -p_wh;
5453 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5454 room += wp->w_height - p_wmh;
5455 if (room >= 0)
5456 break;
5457 --p_wmh;
5458 if (first)
5459 {
5460 EMSG(_(e_noroom));
5461 first = FALSE;
5462 }
5463 }
5464}
5465
5466#ifdef FEAT_MOUSE
5467
5468/*
5469 * Status line of dragwin is dragged "offset" lines down (negative is up).
5470 */
5471 void
5472win_drag_status_line(dragwin, offset)
5473 win_T *dragwin;
5474 int offset;
5475{
5476 frame_T *curfr;
5477 frame_T *fr;
5478 int room;
5479 int row;
5480 int up; /* if TRUE, drag status line up, otherwise down */
5481 int n;
5482
5483 fr = dragwin->w_frame;
5484 curfr = fr;
5485 if (fr != topframe) /* more than one window */
5486 {
5487 fr = fr->fr_parent;
5488 /* When the parent frame is not a column of frames, its parent should
5489 * be. */
5490 if (fr->fr_layout != FR_COL)
5491 {
5492 curfr = fr;
5493 if (fr != topframe) /* only a row of windows, may drag statusline */
5494 fr = fr->fr_parent;
5495 }
5496 }
5497
5498 /* If this is the last frame in a column, may want to resize the parent
5499 * frame instead (go two up to skip a row of frames). */
5500 while (curfr != topframe && curfr->fr_next == NULL)
5501 {
5502 if (fr != topframe)
5503 fr = fr->fr_parent;
5504 curfr = fr;
5505 if (fr != topframe)
5506 fr = fr->fr_parent;
5507 }
5508
5509 if (offset < 0) /* drag up */
5510 {
5511 up = TRUE;
5512 offset = -offset;
5513 /* sum up the room of the current frame and above it */
5514 if (fr == curfr)
5515 {
5516 /* only one window */
5517 room = fr->fr_height - frame_minheight(fr, NULL);
5518 }
5519 else
5520 {
5521 room = 0;
5522 for (fr = fr->fr_child; ; fr = fr->fr_next)
5523 {
5524 room += fr->fr_height - frame_minheight(fr, NULL);
5525 if (fr == curfr)
5526 break;
5527 }
5528 }
5529 fr = curfr->fr_next; /* put fr at frame that grows */
5530 }
5531 else /* drag down */
5532 {
5533 up = FALSE;
5534 /*
5535 * Only dragging the last status line can reduce p_ch.
5536 */
5537 room = Rows - cmdline_row;
5538 if (curfr->fr_next == NULL)
5539 room -= 1;
5540 else
5541 room -= p_ch;
5542 if (room < 0)
5543 room = 0;
5544 /* sum up the room of frames below of the current one */
5545 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5546 room += fr->fr_height - frame_minheight(fr, NULL);
5547 fr = curfr; /* put fr at window that grows */
5548 }
5549
5550 if (room < offset) /* Not enough room */
5551 offset = room; /* Move as far as we can */
5552 if (offset <= 0)
5553 return;
5554
5555 /*
5556 * Grow frame fr by "offset" lines.
5557 * Doesn't happen when dragging the last status line up.
5558 */
5559 if (fr != NULL)
5560 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5561
5562 if (up)
5563 fr = curfr; /* current frame gets smaller */
5564 else
5565 fr = curfr->fr_next; /* next frame gets smaller */
5566
5567 /*
5568 * Now make the other frames smaller.
5569 */
5570 while (fr != NULL && offset > 0)
5571 {
5572 n = frame_minheight(fr, NULL);
5573 if (fr->fr_height - offset <= n)
5574 {
5575 offset -= fr->fr_height - n;
5576 frame_new_height(fr, n, !up, FALSE);
5577 }
5578 else
5579 {
5580 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5581 break;
5582 }
5583 if (up)
5584 fr = fr->fr_prev;
5585 else
5586 fr = fr->fr_next;
5587 }
5588 row = win_comp_pos();
5589 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5590 cmdline_row = row;
5591 p_ch = Rows - cmdline_row;
5592 if (p_ch < 1)
5593 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005594 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005595 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 showmode();
5597}
5598
5599#ifdef FEAT_VERTSPLIT
5600/*
5601 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5602 */
5603 void
5604win_drag_vsep_line(dragwin, offset)
5605 win_T *dragwin;
5606 int offset;
5607{
5608 frame_T *curfr;
5609 frame_T *fr;
5610 int room;
5611 int left; /* if TRUE, drag separator line left, otherwise right */
5612 int n;
5613
5614 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005615 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 return;
5617 curfr = fr;
5618 fr = fr->fr_parent;
5619 /* When the parent frame is not a row of frames, its parent should be. */
5620 if (fr->fr_layout != FR_ROW)
5621 {
5622 if (fr == topframe) /* only a column of windows (cannot happen?) */
5623 return;
5624 curfr = fr;
5625 fr = fr->fr_parent;
5626 }
5627
5628 /* If this is the last frame in a row, may want to resize a parent
5629 * frame instead. */
5630 while (curfr->fr_next == NULL)
5631 {
5632 if (fr == topframe)
5633 break;
5634 curfr = fr;
5635 fr = fr->fr_parent;
5636 if (fr != topframe)
5637 {
5638 curfr = fr;
5639 fr = fr->fr_parent;
5640 }
5641 }
5642
5643 if (offset < 0) /* drag left */
5644 {
5645 left = TRUE;
5646 offset = -offset;
5647 /* sum up the room of the current frame and left of it */
5648 room = 0;
5649 for (fr = fr->fr_child; ; fr = fr->fr_next)
5650 {
5651 room += fr->fr_width - frame_minwidth(fr, NULL);
5652 if (fr == curfr)
5653 break;
5654 }
5655 fr = curfr->fr_next; /* put fr at frame that grows */
5656 }
5657 else /* drag right */
5658 {
5659 left = FALSE;
5660 /* sum up the room of frames right of the current one */
5661 room = 0;
5662 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5663 room += fr->fr_width - frame_minwidth(fr, NULL);
5664 fr = curfr; /* put fr at window that grows */
5665 }
5666
5667 if (room < offset) /* Not enough room */
5668 offset = room; /* Move as far as we can */
5669 if (offset <= 0) /* No room at all, quit. */
5670 return;
5671
5672 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005673 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675 /* shrink other frames: current and at the left or at the right */
5676 if (left)
5677 fr = curfr; /* current frame gets smaller */
5678 else
5679 fr = curfr->fr_next; /* next frame gets smaller */
5680
5681 while (fr != NULL && offset > 0)
5682 {
5683 n = frame_minwidth(fr, NULL);
5684 if (fr->fr_width - offset <= n)
5685 {
5686 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005687 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 else
5690 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005691 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 break;
5693 }
5694 if (left)
5695 fr = fr->fr_prev;
5696 else
5697 fr = fr->fr_next;
5698 }
5699 (void)win_comp_pos();
5700 redraw_all_later(NOT_VALID);
5701}
5702#endif /* FEAT_VERTSPLIT */
5703#endif /* FEAT_MOUSE */
5704
5705#endif /* FEAT_WINDOWS */
5706
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005707#define FRACTION_MULT 16384L
5708
5709/*
5710 * Set wp->w_fraction for the current w_wrow and w_height.
5711 */
5712 static void
5713set_fraction(wp)
5714 win_T *wp;
5715{
5716 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005717 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005718}
5719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720/*
5721 * Set the height of a window.
5722 * This takes care of the things inside the window, not what happens to the
5723 * window position, the frame or to other windows.
5724 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005725 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726win_new_height(wp, height)
5727 win_T *wp;
5728 int height;
5729{
5730 linenr_T lnum;
5731 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005732 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 /* Don't want a negative height. Happens when splitting a tiny window.
5735 * Will equalize heights soon to fix it. */
5736 if (height < 0)
5737 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005738 if (wp->w_height == height)
5739 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005741 if (wp->w_height > 0)
5742 {
5743 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005744 /* w_wrow needs to be valid. When setting 'laststatus' this may
5745 * call win_new_height() recursively. */
5746 validate_cursor();
5747 if (wp->w_height != prev_height)
5748 return; /* Recursive call already changed the size, bail out here
5749 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005750 if (wp->w_wrow != wp->w_prev_fraction_row)
5751 set_fraction(wp);
5752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
5754 wp->w_height = height;
5755 wp->w_skipcol = 0;
5756
5757 /* Don't change w_topline when height is zero. Don't set w_topline when
5758 * 'scrollbind' is set and this isn't the current window. */
5759 if (height > 0
5760#ifdef FEAT_SCROLLBIND
5761 && (!wp->w_p_scb || wp == curwin)
5762#endif
5763 )
5764 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005765 /*
5766 * Find a value for w_topline that shows the cursor at the same
5767 * relative position in the window as before (more or less).
5768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 lnum = wp->w_cursor.lnum;
5770 if (lnum < 1) /* can happen when starting up */
5771 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005772 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5773 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5775 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005776
5777 if (sline >= 0)
5778 {
5779 /* Make sure the whole cursor line is visible, if possible. */
5780 int rows = plines_win(wp, lnum, FALSE);
5781
5782 if (sline > wp->w_height - rows)
5783 {
5784 sline = wp->w_height - rows;
5785 wp->w_wrow -= rows - line_size;
5786 }
5787 }
5788
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 if (sline < 0)
5790 {
5791 /*
5792 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005793 * Make cursor line the first line in the window. If not enough
5794 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 */
5796 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005797 if (wp->w_wrow >= wp->w_height
5798 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5799 {
5800 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5801 --wp->w_wrow;
5802 while (wp->w_wrow >= wp->w_height)
5803 {
5804 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5805 + win_col_off2(wp);
5806 --wp->w_wrow;
5807 }
5808 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005809 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005811 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005813 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
5815#ifdef FEAT_FOLDING
5816 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5817 if (lnum == 1)
5818 {
5819 /* first line in buffer is folded */
5820 line_size = 1;
5821 --sline;
5822 break;
5823 }
5824#endif
5825 --lnum;
5826#ifdef FEAT_DIFF
5827 if (lnum == wp->w_topline)
5828 line_size = plines_win_nofill(wp, lnum, TRUE)
5829 + wp->w_topfill;
5830 else
5831#endif
5832 line_size = plines_win(wp, lnum, TRUE);
5833 sline -= line_size;
5834 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 if (sline < 0)
5837 {
5838 /*
5839 * Line we want at top would go off top of screen. Use next
5840 * line instead.
5841 */
5842#ifdef FEAT_FOLDING
5843 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5844#endif
5845 lnum++;
5846 wp->w_wrow -= line_size + sline;
5847 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005848 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
5850 /* First line of file reached, use that as topline. */
5851 lnum = 1;
5852 wp->w_wrow -= sline;
5853 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005854
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005855 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 }
5858
5859 if (wp == curwin)
5860 {
5861 if (p_so)
5862 update_topline();
5863 curs_columns(FALSE); /* validate w_wrow */
5864 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005865 if (prev_height > 0)
5866 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005869 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870#ifdef FEAT_WINDOWS
5871 wp->w_redr_status = TRUE;
5872#endif
5873 invalidate_botline_win(wp);
5874}
5875
5876#ifdef FEAT_VERTSPLIT
5877/*
5878 * Set the width of a window.
5879 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005880 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881win_new_width(wp, width)
5882 win_T *wp;
5883 int width;
5884{
5885 wp->w_width = width;
5886 wp->w_lines_valid = 0;
5887 changed_line_abv_curs_win(wp);
5888 invalidate_botline_win(wp);
5889 if (wp == curwin)
5890 {
5891 update_topline();
5892 curs_columns(TRUE); /* validate w_wrow */
5893 }
5894 redraw_win_later(wp, NOT_VALID);
5895 wp->w_redr_status = TRUE;
5896}
5897#endif
5898
5899 void
5900win_comp_scroll(wp)
5901 win_T *wp;
5902{
5903 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5904 if (wp->w_p_scr == 0)
5905 wp->w_p_scr = 1;
5906}
5907
5908/*
5909 * command_height: called whenever p_ch has been changed
5910 */
5911 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005912command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914#ifdef FEAT_WINDOWS
5915 int h;
5916 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005917 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005919 /* Use the value of p_ch that we remembered. This is needed for when the
5920 * GUI starts up, we can't be sure in what order things happen. And when
5921 * p_ch was changed in another tab page. */
5922 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005923
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 /* Find bottom frame with width of screen. */
5925 frp = lastwin->w_frame;
5926# ifdef FEAT_VERTSPLIT
5927 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5928 frp = frp->fr_parent;
5929# endif
5930
5931 /* Avoid changing the height of a window with 'winfixheight' set. */
5932 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5933 && frp->fr_win->w_p_wfh)
5934 frp = frp->fr_prev;
5935
5936 if (starting != NO_SCREEN)
5937 {
5938 cmdline_row = Rows - p_ch;
5939
5940 if (p_ch > old_p_ch) /* p_ch got bigger */
5941 {
5942 while (p_ch > old_p_ch)
5943 {
5944 if (frp == NULL)
5945 {
5946 EMSG(_(e_noroom));
5947 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005948 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 cmdline_row = Rows - p_ch;
5950 break;
5951 }
5952 h = frp->fr_height - frame_minheight(frp, NULL);
5953 if (h > p_ch - old_p_ch)
5954 h = p_ch - old_p_ch;
5955 old_p_ch += h;
5956 frame_add_height(frp, -h);
5957 frp = frp->fr_prev;
5958 }
5959
5960 /* Recompute window positions. */
5961 (void)win_comp_pos();
5962
5963 /* clear the lines added to cmdline */
5964 if (full_screen)
5965 screen_fill((int)(cmdline_row), (int)Rows, 0,
5966 (int)Columns, ' ', ' ', 0);
5967 msg_row = cmdline_row;
5968 redraw_cmdline = TRUE;
5969 return;
5970 }
5971
5972 if (msg_row < cmdline_row)
5973 msg_row = cmdline_row;
5974 redraw_cmdline = TRUE;
5975 }
5976 frame_add_height(frp, (int)(old_p_ch - p_ch));
5977
5978 /* Recompute window positions. */
5979 if (frp != lastwin->w_frame)
5980 (void)win_comp_pos();
5981#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005983 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984#endif
5985}
5986
5987#if defined(FEAT_WINDOWS) || defined(PROTO)
5988/*
5989 * Resize frame "frp" to be "n" lines higher (negative for less high).
5990 * Also resize the frames it is contained in.
5991 */
5992 static void
5993frame_add_height(frp, n)
5994 frame_T *frp;
5995 int n;
5996{
5997 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5998 for (;;)
5999 {
6000 frp = frp->fr_parent;
6001 if (frp == NULL)
6002 break;
6003 frp->fr_height += n;
6004 }
6005}
6006
6007/*
6008 * Add or remove a status line for the bottom window(s), according to the
6009 * value of 'laststatus'.
6010 */
6011 void
6012last_status(morewin)
6013 int morewin; /* pretend there are two or more windows */
6014{
6015 /* Don't make a difference between horizontal or vertical split. */
6016 last_status_rec(topframe, (p_ls == 2
6017 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6018}
6019
6020 static void
6021last_status_rec(fr, statusline)
6022 frame_T *fr;
6023 int statusline;
6024{
6025 frame_T *fp;
6026 win_T *wp;
6027
6028 if (fr->fr_layout == FR_LEAF)
6029 {
6030 wp = fr->fr_win;
6031 if (wp->w_status_height != 0 && !statusline)
6032 {
6033 /* remove status line */
6034 win_new_height(wp, wp->w_height + 1);
6035 wp->w_status_height = 0;
6036 comp_col();
6037 }
6038 else if (wp->w_status_height == 0 && statusline)
6039 {
6040 /* Find a frame to take a line from. */
6041 fp = fr;
6042 while (fp->fr_height <= frame_minheight(fp, NULL))
6043 {
6044 if (fp == topframe)
6045 {
6046 EMSG(_(e_noroom));
6047 return;
6048 }
6049 /* In a column of frames: go to frame above. If already at
6050 * the top or in a row of frames: go to parent. */
6051 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6052 fp = fp->fr_prev;
6053 else
6054 fp = fp->fr_parent;
6055 }
6056 wp->w_status_height = 1;
6057 if (fp != fr)
6058 {
6059 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6060 frame_fix_height(wp);
6061 (void)win_comp_pos();
6062 }
6063 else
6064 win_new_height(wp, wp->w_height - 1);
6065 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006066 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 }
6068 }
6069#ifdef FEAT_VERTSPLIT
6070 else if (fr->fr_layout == FR_ROW)
6071 {
6072 /* vertically split windows, set status line for each one */
6073 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6074 last_status_rec(fp, statusline);
6075 }
6076#endif
6077 else
6078 {
6079 /* horizontally split window, set status line for last one */
6080 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6081 ;
6082 last_status_rec(fp, statusline);
6083 }
6084}
6085
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006086/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006087 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006088 */
6089 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006090tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006091{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006092#ifdef FEAT_GUI_TABLINE
6093 /* When the GUI has the tabline then this always returns zero. */
6094 if (gui_use_tabline())
6095 return 0;
6096#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006097 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006098 {
6099 case 0: return 0;
6100 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6101 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006102 return 1;
6103}
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105#endif /* FEAT_WINDOWS */
6106
6107#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6108/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006109 * Get the file name at the cursor.
6110 * If Visual mode is active, use the selected text if it's in one line.
6111 * Returns the name in allocated memory, NULL for failure.
6112 */
6113 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006114grab_file_name(count, file_lnum)
6115 long count;
6116 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006117{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006118 if (VIsual_active)
6119 {
6120 int len;
6121 char_u *ptr;
6122
6123 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6124 return NULL;
6125 return find_file_name_in_path(ptr, len,
6126 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6127 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006128 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6129 file_lnum);
6130
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006131}
6132
6133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 * Return the file name under or after the cursor.
6135 *
6136 * The 'path' option is searched if the file name is not absolute.
6137 * The string returned has been alloc'ed and should be freed by the caller.
6138 * NULL is returned if the file name or file is not found.
6139 *
6140 * options:
6141 * FNAME_MESS give error messages
6142 * FNAME_EXP expand to path
6143 * FNAME_HYP check for hypertext link
6144 * FNAME_INCL apply "includeexpr"
6145 */
6146 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006147file_name_at_cursor(options, count, file_lnum)
6148 int options;
6149 long count;
6150 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
6152 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006153 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6154 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155}
6156
6157/*
6158 * Return the name of the file under or after ptr[col].
6159 * Otherwise like file_name_at_cursor().
6160 */
6161 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006162file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 char_u *line;
6164 int col;
6165 int options;
6166 long count;
6167 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006168 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
6170 char_u *ptr;
6171 int len;
6172
6173 /*
6174 * search forward for what could be the start of a file name
6175 */
6176 ptr = line + col;
6177 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006178 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (*ptr == NUL) /* nothing found */
6180 {
6181 if (options & FNAME_MESS)
6182 EMSG(_("E446: No file name under cursor"));
6183 return NULL;
6184 }
6185
6186 /*
6187 * Search backward for first char of the file name.
6188 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6189 */
6190 while (ptr > line)
6191 {
6192#ifdef FEAT_MBYTE
6193 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6194 ptr -= len + 1;
6195 else
6196#endif
6197 if (vim_isfilec(ptr[-1])
6198 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6199 --ptr;
6200 else
6201 break;
6202 }
6203
6204 /*
6205 * Search forward for the last char of the file name.
6206 * Also allow "://" when ':' is not in 'isfname'.
6207 */
6208 len = 0;
6209 while (vim_isfilec(ptr[len])
6210 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6211#ifdef FEAT_MBYTE
6212 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006213 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 else
6215#endif
6216 ++len;
6217
6218 /*
6219 * If there is trailing punctuation, remove it.
6220 * But don't remove "..", could be a directory name.
6221 */
6222 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6223 && ptr[len - 2] != '.')
6224 --len;
6225
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006226 if (file_lnum != NULL)
6227 {
6228 char_u *p;
6229
6230 /* Get the number after the file name and a separator character */
6231 p = ptr + len;
6232 p = skipwhite(p);
6233 if (*p != NUL)
6234 {
6235 if (!isdigit(*p))
6236 ++p; /* skip the separator */
6237 p = skipwhite(p);
6238 if (isdigit(*p))
6239 *file_lnum = (int)getdigits(&p);
6240 }
6241 }
6242
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6244}
6245
6246# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6247static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6248
6249 static char_u *
6250eval_includeexpr(ptr, len)
6251 char_u *ptr;
6252 int len;
6253{
6254 char_u *res;
6255
6256 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006257 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006258 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 set_vim_var_string(VV_FNAME, NULL, 0);
6260 return res;
6261}
6262#endif
6263
6264/*
6265 * Return the name of the file ptr[len] in 'path'.
6266 * Otherwise like file_name_at_cursor().
6267 */
6268 char_u *
6269find_file_name_in_path(ptr, len, options, count, rel_fname)
6270 char_u *ptr;
6271 int len;
6272 int options;
6273 long count;
6274 char_u *rel_fname; /* file we are searching relative to */
6275{
6276 char_u *file_name;
6277 int c;
6278# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6279 char_u *tofree = NULL;
6280
6281 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6282 {
6283 tofree = eval_includeexpr(ptr, len);
6284 if (tofree != NULL)
6285 {
6286 ptr = tofree;
6287 len = (int)STRLEN(ptr);
6288 }
6289 }
6290# endif
6291
6292 if (options & FNAME_EXP)
6293 {
6294 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6295 TRUE, rel_fname);
6296
6297# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6298 /*
6299 * If the file could not be found in a normal way, try applying
6300 * 'includeexpr' (unless done already).
6301 */
6302 if (file_name == NULL
6303 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6304 {
6305 tofree = eval_includeexpr(ptr, len);
6306 if (tofree != NULL)
6307 {
6308 ptr = tofree;
6309 len = (int)STRLEN(ptr);
6310 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6311 TRUE, rel_fname);
6312 }
6313 }
6314# endif
6315 if (file_name == NULL && (options & FNAME_MESS))
6316 {
6317 c = ptr[len];
6318 ptr[len] = NUL;
6319 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6320 ptr[len] = c;
6321 }
6322
6323 /* Repeat finding the file "count" times. This matters when it
6324 * appears several times in the path. */
6325 while (file_name != NULL && --count > 0)
6326 {
6327 vim_free(file_name);
6328 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6329 }
6330 }
6331 else
6332 file_name = vim_strnsave(ptr, len);
6333
6334# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6335 vim_free(tofree);
6336# endif
6337
6338 return file_name;
6339}
6340#endif /* FEAT_SEARCHPATH */
6341
6342/*
6343 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6344 * Also check for ":\\", which MS Internet Explorer accepts, return
6345 * URL_BACKSLASH.
6346 */
6347 static int
6348path_is_url(p)
6349 char_u *p;
6350{
6351 if (STRNCMP(p, "://", (size_t)3) == 0)
6352 return URL_SLASH;
6353 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6354 return URL_BACKSLASH;
6355 return 0;
6356}
6357
6358/*
6359 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6360 * Return URL_BACKSLASH for "name:\\".
6361 * Return zero otherwise.
6362 */
6363 int
6364path_with_url(fname)
6365 char_u *fname;
6366{
6367 char_u *p;
6368
6369 for (p = fname; isalpha(*p); ++p)
6370 ;
6371 return path_is_url(p);
6372}
6373
6374/*
6375 * Return TRUE if "name" is a full (absolute) path name or URL.
6376 */
6377 int
6378vim_isAbsName(name)
6379 char_u *name;
6380{
6381 return (path_with_url(name) != 0 || mch_isFullName(name));
6382}
6383
6384/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006385 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 *
6387 * return FAIL for failure, OK otherwise
6388 */
6389 int
6390vim_FullName(fname, buf, len, force)
6391 char_u *fname, *buf;
6392 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006393 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 int retval = OK;
6396 int url;
6397
6398 *buf = NUL;
6399 if (fname == NULL)
6400 return FAIL;
6401
6402 url = path_with_url(fname);
6403 if (!url)
6404 retval = mch_FullName(fname, buf, len, force);
6405 if (url || retval == FAIL)
6406 {
6407 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006408 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 }
6410#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6411 slash_adjust(buf);
6412#endif
6413 return retval;
6414}
6415
6416/*
6417 * Return the minimal number of rows that is needed on the screen to display
6418 * the current number of windows.
6419 */
6420 int
6421min_rows()
6422{
6423 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006424#ifdef FEAT_WINDOWS
6425 tabpage_T *tp;
6426 int n;
6427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
6429 if (firstwin == NULL) /* not initialized yet */
6430 return MIN_LINES;
6431
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006433 total = 0;
6434 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6435 {
6436 n = frame_minheight(tp->tp_topframe, NULL);
6437 if (total < n)
6438 total = n;
6439 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006440 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006442 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006444 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 return total;
6446}
6447
6448/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006449 * Return TRUE if there is only one window (in the current tab page), not
6450 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006451 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 */
6453 int
6454only_one_window()
6455{
6456#ifdef FEAT_WINDOWS
6457 int count = 0;
6458 win_T *wp;
6459
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460 /* If there is another tab page there always is another window. */
6461 if (first_tabpage->tp_next != NULL)
6462 return FALSE;
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006465 if (wp->w_buffer != NULL
6466 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467# ifdef FEAT_QUICKFIX
6468 || wp->w_p_pvw
6469# endif
6470 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006471# ifdef FEAT_AUTOCMD
6472 && wp != aucmd_win
6473# endif
6474 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 ++count;
6476 return (count <= 1);
6477#else
6478 return TRUE;
6479#endif
6480}
6481
6482#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6483/*
6484 * Correct the cursor line number in other windows. Used after changing the
6485 * current buffer, and before applying autocommands.
6486 * When "do_curwin" is TRUE, also check current window.
6487 */
6488 void
6489check_lnums(do_curwin)
6490 int do_curwin;
6491{
6492 win_T *wp;
6493
6494#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006495 tabpage_T *tp;
6496
6497 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6499#else
6500 wp = curwin;
6501 if (do_curwin)
6502#endif
6503 {
6504 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6505 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6506 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6507 wp->w_topline = curbuf->b_ml.ml_line_count;
6508 }
6509}
6510#endif
6511
6512#if defined(FEAT_WINDOWS) || defined(PROTO)
6513
6514/*
6515 * A snapshot of the window sizes, to restore them after closing the help
6516 * window.
6517 * Only these fields are used:
6518 * fr_layout
6519 * fr_width
6520 * fr_height
6521 * fr_next
6522 * fr_child
6523 * fr_win (only valid for the old curwin, NULL otherwise)
6524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525
6526/*
6527 * Create a snapshot of the current frame sizes.
6528 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006529 void
6530make_snapshot(idx)
6531 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006533 clear_snapshot(curtab, idx);
6534 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535}
6536
6537 static void
6538make_snapshot_rec(fr, frp)
6539 frame_T *fr;
6540 frame_T **frp;
6541{
6542 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6543 if (*frp == NULL)
6544 return;
6545 (*frp)->fr_layout = fr->fr_layout;
6546# ifdef FEAT_VERTSPLIT
6547 (*frp)->fr_width = fr->fr_width;
6548# endif
6549 (*frp)->fr_height = fr->fr_height;
6550 if (fr->fr_next != NULL)
6551 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6552 if (fr->fr_child != NULL)
6553 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6554 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6555 (*frp)->fr_win = curwin;
6556}
6557
6558/*
6559 * Remove any existing snapshot.
6560 */
6561 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006562clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006563 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006564 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006566 clear_snapshot_rec(tp->tp_snapshot[idx]);
6567 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568}
6569
6570 static void
6571clear_snapshot_rec(fr)
6572 frame_T *fr;
6573{
6574 if (fr != NULL)
6575 {
6576 clear_snapshot_rec(fr->fr_next);
6577 clear_snapshot_rec(fr->fr_child);
6578 vim_free(fr);
6579 }
6580}
6581
6582/*
6583 * Restore a previously created snapshot, if there is any.
6584 * This is only done if the screen size didn't change and the window layout is
6585 * still the same.
6586 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006587 void
6588restore_snapshot(idx, close_curwin)
6589 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 int close_curwin; /* closing current window */
6591{
6592 win_T *wp;
6593
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006594 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006596 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006598 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6599 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006601 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 win_comp_pos();
6603 if (wp != NULL && close_curwin)
6604 win_goto(wp);
6605 redraw_all_later(CLEAR);
6606 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006607 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608}
6609
6610/*
6611 * Check if frames "sn" and "fr" have the same layout, same following frames
6612 * and same children.
6613 */
6614 static int
6615check_snapshot_rec(sn, fr)
6616 frame_T *sn;
6617 frame_T *fr;
6618{
6619 if (sn->fr_layout != fr->fr_layout
6620 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6621 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6622 || (sn->fr_next != NULL
6623 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6624 || (sn->fr_child != NULL
6625 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6626 return FAIL;
6627 return OK;
6628}
6629
6630/*
6631 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6632 * following frames and children.
6633 * Returns a pointer to the old current window, or NULL.
6634 */
6635 static win_T *
6636restore_snapshot_rec(sn, fr)
6637 frame_T *sn;
6638 frame_T *fr;
6639{
6640 win_T *wp = NULL;
6641 win_T *wp2;
6642
6643 fr->fr_height = sn->fr_height;
6644# ifdef FEAT_VERTSPLIT
6645 fr->fr_width = sn->fr_width;
6646# endif
6647 if (fr->fr_layout == FR_LEAF)
6648 {
6649 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6650# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006651 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652# endif
6653 wp = sn->fr_win;
6654 }
6655 if (sn->fr_next != NULL)
6656 {
6657 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6658 if (wp2 != NULL)
6659 wp = wp2;
6660 }
6661 if (sn->fr_child != NULL)
6662 {
6663 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6664 if (wp2 != NULL)
6665 wp = wp2;
6666 }
6667 return wp;
6668}
6669
6670#endif
6671
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006672#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6673 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006674/*
6675 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006676 * restore_win() MUST be called to undo, also when FAIL is returned.
6677 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006678 * When "no_display" is TRUE the display won't be affected, no redraw is
6679 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006680 * Returns FAIL if switching to "win" failed.
6681 */
6682 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006683switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006684 win_T **save_curwin UNUSED;
6685 tabpage_T **save_curtab UNUSED;
6686 win_T *win UNUSED;
6687 tabpage_T *tp UNUSED;
6688 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006689{
6690# ifdef FEAT_AUTOCMD
6691 block_autocmds();
6692# endif
6693# ifdef FEAT_WINDOWS
6694 *save_curwin = curwin;
6695 if (tp != NULL)
6696 {
6697 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006698 if (no_display)
6699 {
6700 curtab->tp_firstwin = firstwin;
6701 curtab->tp_lastwin = lastwin;
6702 curtab = tp;
6703 firstwin = curtab->tp_firstwin;
6704 lastwin = curtab->tp_lastwin;
6705 }
6706 else
6707 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006708 }
6709 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006710 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006711 curwin = win;
6712 curbuf = curwin->w_buffer;
6713# endif
6714 return OK;
6715}
6716
6717/*
6718 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006719 * When "no_display" is TRUE the display won't be affected, no redraw is
6720 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006721 */
6722 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006723restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006724 win_T *save_curwin UNUSED;
6725 tabpage_T *save_curtab UNUSED;
6726 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006727{
6728# ifdef FEAT_WINDOWS
6729 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006730 {
6731 if (no_display)
6732 {
6733 curtab->tp_firstwin = firstwin;
6734 curtab->tp_lastwin = lastwin;
6735 curtab = save_curtab;
6736 firstwin = curtab->tp_firstwin;
6737 lastwin = curtab->tp_lastwin;
6738 }
6739 else
6740 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6741 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006742 if (win_valid(save_curwin))
6743 {
6744 curwin = save_curwin;
6745 curbuf = curwin->w_buffer;
6746 }
6747# endif
6748# ifdef FEAT_AUTOCMD
6749 unblock_autocmds();
6750# endif
6751}
6752
6753/*
6754 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6755 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6756 */
6757 void
6758switch_buffer(save_curbuf, buf)
6759 buf_T *buf;
6760 buf_T **save_curbuf;
6761{
6762# ifdef FEAT_AUTOCMD
6763 block_autocmds();
6764# endif
6765 *save_curbuf = curbuf;
6766 --curbuf->b_nwindows;
6767 curbuf = buf;
6768 curwin->w_buffer = buf;
6769 ++curbuf->b_nwindows;
6770}
6771
6772/*
6773 * Restore the current buffer after using switch_buffer().
6774 */
6775 void
6776restore_buffer(save_curbuf)
6777 buf_T *save_curbuf;
6778{
6779# ifdef FEAT_AUTOCMD
6780 unblock_autocmds();
6781# endif
6782 /* Check for valid buffer, just in case. */
6783 if (buf_valid(save_curbuf))
6784 {
6785 --curbuf->b_nwindows;
6786 curwin->w_buffer = save_curbuf;
6787 curbuf = save_curbuf;
6788 ++curbuf->b_nwindows;
6789 }
6790}
6791#endif
6792
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6794/*
6795 * Return TRUE if there is any vertically split window.
6796 */
6797 int
6798win_hasvertsplit()
6799{
6800 frame_T *fr;
6801
6802 if (topframe->fr_layout == FR_ROW)
6803 return TRUE;
6804
6805 if (topframe->fr_layout == FR_COL)
6806 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6807 if (fr->fr_layout == FR_ROW)
6808 return TRUE;
6809
6810 return FALSE;
6811}
6812#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006813
6814#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6815/*
6816 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006817 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006818 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6819 * If no particular ID is desired, -1 must be specified for 'id'.
6820 * Return ID of added match, -1 on failure.
6821 */
6822 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006823match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006824 win_T *wp;
6825 char_u *grp;
6826 char_u *pat;
6827 int prio;
6828 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006829 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006830{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006831 matchitem_T *cur;
6832 matchitem_T *prev;
6833 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006834 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006835 regprog_T *regprog = NULL;
6836 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006837
Bram Moolenaarb3414592014-06-17 17:48:32 +02006838 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006839 return -1;
6840 if (id < -1 || id == 0)
6841 {
6842 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6843 return -1;
6844 }
6845 if (id != -1)
6846 {
6847 cur = wp->w_match_head;
6848 while (cur != NULL)
6849 {
6850 if (cur->id == id)
6851 {
6852 EMSGN("E801: ID already taken: %ld", id);
6853 return -1;
6854 }
6855 cur = cur->next;
6856 }
6857 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006858 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006859 {
6860 EMSG2(_(e_nogroup), grp);
6861 return -1;
6862 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006863 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006864 {
6865 EMSG2(_(e_invarg2), pat);
6866 return -1;
6867 }
6868
6869 /* Find available match ID. */
6870 while (id == -1)
6871 {
6872 cur = wp->w_match_head;
6873 while (cur != NULL && cur->id != wp->w_next_match_id)
6874 cur = cur->next;
6875 if (cur == NULL)
6876 id = wp->w_next_match_id;
6877 wp->w_next_match_id++;
6878 }
6879
6880 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006881 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006882 m->id = id;
6883 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006884 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006885 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006886 m->match.regprog = regprog;
6887 m->match.rmm_ic = FALSE;
6888 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006889
Bram Moolenaarb3414592014-06-17 17:48:32 +02006890 /* Set up position matches */
6891 if (pos_list != NULL)
6892 {
6893 linenr_T toplnum = 0;
6894 linenr_T botlnum = 0;
6895 listitem_T *li;
6896 int i;
6897
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006898 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006899 i++, li = li->li_next)
6900 {
6901 linenr_T lnum = 0;
6902 colnr_T col = 0;
6903 int len = 1;
6904 list_T *subl;
6905 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006906 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006907
Bram Moolenaarb3414592014-06-17 17:48:32 +02006908 if (li->li_tv.v_type == VAR_LIST)
6909 {
6910 subl = li->li_tv.vval.v_list;
6911 if (subl == NULL)
6912 goto fail;
6913 subli = subl->lv_first;
6914 if (subli == NULL)
6915 goto fail;
6916 lnum = get_tv_number_chk(&subli->li_tv, &error);
6917 if (error == TRUE)
6918 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006919 if (lnum == 0)
6920 {
6921 --i;
6922 continue;
6923 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006924 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006925 subli = subli->li_next;
6926 if (subli != NULL)
6927 {
6928 col = get_tv_number_chk(&subli->li_tv, &error);
6929 if (error == TRUE)
6930 goto fail;
6931 subli = subli->li_next;
6932 if (subli != NULL)
6933 {
6934 len = get_tv_number_chk(&subli->li_tv, &error);
6935 if (error == TRUE)
6936 goto fail;
6937 }
6938 }
6939 m->pos.pos[i].col = col;
6940 m->pos.pos[i].len = len;
6941 }
6942 else if (li->li_tv.v_type == VAR_NUMBER)
6943 {
6944 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006945 {
6946 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006947 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006948 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006949 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6950 m->pos.pos[i].col = 0;
6951 m->pos.pos[i].len = 0;
6952 }
6953 else
6954 {
6955 EMSG(_("List or number required"));
6956 goto fail;
6957 }
6958 if (toplnum == 0 || lnum < toplnum)
6959 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006960 if (botlnum == 0 || lnum >= botlnum)
6961 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006962 }
6963
6964 /* Calculate top and bottom lines for redrawing area */
6965 if (toplnum != 0)
6966 {
6967 if (wp->w_buffer->b_mod_set)
6968 {
6969 if (wp->w_buffer->b_mod_top > toplnum)
6970 wp->w_buffer->b_mod_top = toplnum;
6971 if (wp->w_buffer->b_mod_bot < botlnum)
6972 wp->w_buffer->b_mod_bot = botlnum;
6973 }
6974 else
6975 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006976 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006977 wp->w_buffer->b_mod_top = toplnum;
6978 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006979 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006980 }
6981 m->pos.toplnum = toplnum;
6982 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006983 rtype = VALID;
6984 }
6985 }
6986
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006987 /* Insert new match. The match list is in ascending order with regard to
6988 * the match priorities. */
6989 cur = wp->w_match_head;
6990 prev = cur;
6991 while (cur != NULL && prio >= cur->priority)
6992 {
6993 prev = cur;
6994 cur = cur->next;
6995 }
6996 if (cur == prev)
6997 wp->w_match_head = m;
6998 else
6999 prev->next = m;
7000 m->next = cur;
7001
Bram Moolenaarb3414592014-06-17 17:48:32 +02007002 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007003 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007004
7005fail:
7006 vim_free(m);
7007 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007008}
7009
7010/*
7011 * Delete match with ID 'id' in the match list of window 'wp'.
7012 * Print error messages if 'perr' is TRUE.
7013 */
7014 int
7015match_delete(wp, id, perr)
7016 win_T *wp;
7017 int id;
7018 int perr;
7019{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007020 matchitem_T *cur = wp->w_match_head;
7021 matchitem_T *prev = cur;
7022 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007023
7024 if (id < 1)
7025 {
7026 if (perr == TRUE)
7027 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7028 id);
7029 return -1;
7030 }
7031 while (cur != NULL && cur->id != id)
7032 {
7033 prev = cur;
7034 cur = cur->next;
7035 }
7036 if (cur == NULL)
7037 {
7038 if (perr == TRUE)
7039 EMSGN("E803: ID not found: %ld", id);
7040 return -1;
7041 }
7042 if (cur == prev)
7043 wp->w_match_head = cur->next;
7044 else
7045 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007046 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007047 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007048 if (cur->pos.toplnum != 0)
7049 {
7050 if (wp->w_buffer->b_mod_set)
7051 {
7052 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7053 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7054 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7055 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7056 }
7057 else
7058 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007059 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007060 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7061 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007062 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007063 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007064 rtype = VALID;
7065 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007066 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007067 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007068 return 0;
7069}
7070
7071/*
7072 * Delete all matches in the match list of window 'wp'.
7073 */
7074 void
7075clear_matches(wp)
7076 win_T *wp;
7077{
7078 matchitem_T *m;
7079
7080 while (wp->w_match_head != NULL)
7081 {
7082 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007083 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007084 vim_free(wp->w_match_head->pattern);
7085 vim_free(wp->w_match_head);
7086 wp->w_match_head = m;
7087 }
7088 redraw_later(SOME_VALID);
7089}
7090
7091/*
7092 * Get match from ID 'id' in window 'wp'.
7093 * Return NULL if match not found.
7094 */
7095 matchitem_T *
7096get_match(wp, id)
7097 win_T *wp;
7098 int id;
7099{
7100 matchitem_T *cur = wp->w_match_head;
7101
7102 while (cur != NULL && cur->id != id)
7103 cur = cur->next;
7104 return cur;
7105}
7106#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007107
7108#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7109 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007110get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007111{
7112 int i = 1;
7113 win_T *w;
7114
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007115 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007116 ++i;
7117
7118 if (w == NULL)
7119 return 0;
7120 else
7121 return i;
7122}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007123
7124 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007125get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007126{
7127 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007128# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007129 tabpage_T *t;
7130
7131 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7132 ++i;
7133
7134 if (t == NULL)
7135 return 0;
7136 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007137# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007138 return i;
7139}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007140#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007141
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007142#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007143/*
7144 * Return TRUE if "topfrp" and its children are at the right height.
7145 */
7146 static int
7147frame_check_height(topfrp, height)
7148 frame_T *topfrp;
7149 int height;
7150{
7151 frame_T *frp;
7152
7153 if (topfrp->fr_height != height)
7154 return FALSE;
7155
7156 if (topfrp->fr_layout == FR_ROW)
7157 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7158 if (frp->fr_height != height)
7159 return FALSE;
7160
7161 return TRUE;
7162}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007163#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007164
7165#ifdef FEAT_VERTSPLIT
7166/*
7167 * Return TRUE if "topfrp" and its children are at the right width.
7168 */
7169 static int
7170frame_check_width(topfrp, width)
7171 frame_T *topfrp;
7172 int width;
7173{
7174 frame_T *frp;
7175
7176 if (topfrp->fr_width != width)
7177 return FALSE;
7178
7179 if (topfrp->fr_layout == FR_COL)
7180 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7181 if (frp->fr_width != width)
7182 return FALSE;
7183
7184 return TRUE;
7185}
7186#endif
7187