blob: fda9199b2e3802eeaaaefbff6c99317b95e44098 [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 Moolenaar884ae642009-02-22 01:37:59 +000014static void win_init __ARGS((win_T *newp, win_T *oldp, int flags));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000015static void win_init_some __ARGS((win_T *newp, win_T *oldp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
17static void frame_setheight __ARGS((frame_T *curfrp, int height));
18#ifdef FEAT_VERTSPLIT
19static void frame_setwidth __ARGS((frame_T *curfrp, int width));
20#endif
21static void win_exchange __ARGS((long));
22static void win_rotate __ARGS((int, int));
23static void win_totop __ARGS((int size, int flags));
24static 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 +000025static int last_window __ARGS((void));
Bram Moolenaarbef1c362012-05-25 12:39:00 +020026static int close_last_window_tabpage __ARGS((win_T *win, int free_buf, tabpage_T *prev_curtab));
Bram Moolenaarf740b292006-02-16 22:11:02 +000027static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
Bram Moolenaarf740b292006-02-16 22:11:02 +000028static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000029static tabpage_T *alt_tabpage __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000030static win_T *frame2win __ARGS((frame_T *frp));
31static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
32static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
33static int frame_fixed_height __ARGS((frame_T *frp));
34#ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000035static int frame_fixed_width __ARGS((frame_T *frp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000036static void frame_add_statusline __ARGS((frame_T *frp));
Bram Moolenaarbe4d5062006-03-18 21:30:13 +000037static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
Bram Moolenaar071d4272004-06-13 20:20:40 +000038static void frame_add_vsep __ARGS((frame_T *frp));
39static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
40static void frame_fix_width __ARGS((win_T *wp));
41#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +000043static int win_alloc_firstwin __ARGS((win_T *oldwin));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000044static void new_frame __ARGS((win_T *wp));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000045#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000046static tabpage_T *alloc_tabpage __ARGS((void));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020047static int leave_tabpage __ARGS((buf_T *new_curbuf, int trigger_leave_autocmds));
48static 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 +000049static void frame_fix_height __ARGS((win_T *wp));
50static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
Bram Moolenaar49e649f2013-05-06 04:50:35 +020051static 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 +000052static void win_free __ARGS((win_T *wp, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000053static void frame_append __ARGS((frame_T *after, frame_T *frp));
54static void frame_insert __ARGS((frame_T *before, frame_T *frp));
55static void frame_remove __ARGS((frame_T *frp));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020056# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +000057static void win_goto_ver __ARGS((int up, long count));
58static void win_goto_hor __ARGS((int left, long count));
Bram Moolenaarf0327f62013-06-28 20:16:55 +020059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000060static void frame_add_height __ARGS((frame_T *frp, int n));
61static void last_status_rec __ARGS((frame_T *fr, int statusline));
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
Bram Moolenaar746ebd32009-06-16 14:01:43 +000064static void clear_snapshot __ARGS((tabpage_T *tp, int idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static void clear_snapshot_rec __ARGS((frame_T *fr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
67static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
68
Bram Moolenaarb893ac22013-06-26 14:04:47 +020069static int frame_check_height __ARGS((frame_T *topfrp, int height));
70#ifdef FEAT_VERTSPLIT
71static int frame_check_width __ARGS((frame_T *topfrp, int width));
72#endif
73
Bram Moolenaar071d4272004-06-13 20:20:40 +000074#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000075
Bram Moolenaar746ebd32009-06-16 14:01:43 +000076static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010077static void set_fraction __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79#define URL_SLASH 1 /* path_is_url() has found "://" */
80#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
81
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000082#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000085# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000086#else
87# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000088#endif
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000091
92static char *m_onlyone = N_("Already only one window");
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094/*
95 * all CTRL-W window commands are handled here, called from normal_cmd().
96 */
97 void
98do_window(nchar, Prenum, xchar)
99 int nchar;
100 long Prenum;
101 int xchar; /* extra char from ":wincmd gx" or NUL */
102{
103 long Prenum1;
104 win_T *wp;
105#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
106 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000107 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_FIND_ID
110 int type = FIND_DEFINE;
111 int len;
112#endif
113 char_u cbuf[40];
114
115 if (Prenum == 0)
116 Prenum1 = 1;
117 else
118 Prenum1 = Prenum;
119
120#ifdef FEAT_CMDWIN
121# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
122#else
123# define CHECK_CMDWIN
124#endif
125
126 switch (nchar)
127 {
128/* split current window in two parts, horizontally */
129 case 'S':
130 case Ctrl_S:
131 case 's':
132 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000134#ifdef FEAT_QUICKFIX
135 /* When splitting the quickfix window open a new buffer in it,
136 * don't replicate the quickfix buffer. */
137 if (bt_quickfix(curbuf))
138 goto newwindow;
139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_GUI
141 need_mouse_correct = TRUE;
142#endif
143 win_split((int)Prenum, 0);
144 break;
145
146#ifdef FEAT_VERTSPLIT
147/* split current window in two parts, vertically */
148 case Ctrl_V:
149 case 'v':
150 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 win_split((int)Prenum, WSP_VERT);
162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 STRCPY(cbuf, "split #");
171 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000172 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
173 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 do_cmdline_cmd(cbuf);
175 break;
176
177/* open new window */
178 case Ctrl_N:
179 case 'n':
180 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000182#ifdef FEAT_QUICKFIX
183newwindow:
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000186 /* window height */
187 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 else
189 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000190#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
191 if (nchar == 'v' || nchar == Ctrl_V)
192 STRCAT(cbuf, "v");
193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 STRCAT(cbuf, "new");
195 do_cmdline_cmd(cbuf);
196 break;
197
198/* quit current window */
199 case Ctrl_Q:
200 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 do_cmdline_cmd((char_u *)"quit");
203 break;
204
205/* close current window */
206 case Ctrl_C:
207 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 do_cmdline_cmd((char_u *)"close");
210 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 Moolenaar071d4272004-06-13 20:20:40 +0000238 do_cmdline_cmd((char_u *)"only");
239 break;
240
241/* cursor to next window with wrap around */
242 case Ctrl_W:
243 case 'w':
244/* cursor to previous window with wrap around */
245 case 'W':
246 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000247 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 beep_flush();
249 else
250 {
251 if (Prenum) /* go to specified window */
252 {
253 for (wp = firstwin; --Prenum > 0; )
254 {
255 if (wp->w_next == NULL)
256 break;
257 else
258 wp = wp->w_next;
259 }
260 }
261 else
262 {
263 if (nchar == 'W') /* go to previous window */
264 {
265 wp = curwin->w_prev;
266 if (wp == NULL)
267 wp = lastwin; /* wrap around */
268 }
269 else /* go to next window */
270 {
271 wp = curwin->w_next;
272 if (wp == NULL)
273 wp = firstwin; /* wrap around */
274 }
275 }
276 win_goto(wp);
277 }
278 break;
279
280/* cursor to window below */
281 case 'j':
282 case K_DOWN:
283 case Ctrl_J:
284 CHECK_CMDWIN
285#ifdef FEAT_VERTSPLIT
286 win_goto_ver(FALSE, Prenum1);
287#else
288 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
289 wp = wp->w_next)
290 ;
291 win_goto(wp);
292#endif
293 break;
294
295/* cursor to window above */
296 case 'k':
297 case K_UP:
298 case Ctrl_K:
299 CHECK_CMDWIN
300#ifdef FEAT_VERTSPLIT
301 win_goto_ver(TRUE, Prenum1);
302#else
303 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
304 wp = wp->w_prev)
305 ;
306 win_goto(wp);
307#endif
308 break;
309
310#ifdef FEAT_VERTSPLIT
311/* cursor to left window */
312 case 'h':
313 case K_LEFT:
314 case Ctrl_H:
315 case K_BS:
316 CHECK_CMDWIN
317 win_goto_hor(TRUE, Prenum1);
318 break;
319
320/* cursor to right window */
321 case 'l':
322 case K_RIGHT:
323 case Ctrl_L:
324 CHECK_CMDWIN
325 win_goto_hor(FALSE, Prenum1);
326 break;
327#endif
328
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000329/* move window to new tab page */
330 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000331 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000332 MSG(_(m_onlyone));
333 else
334 {
335 tabpage_T *oldtab = curtab;
336 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337
338 /* First create a new tab with the window, then go back to
339 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000340 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341 if (win_new_tabpage((int)Prenum) == OK
342 && valid_tabpage(oldtab))
343 {
344 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200345 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000346 if (curwin == wp)
347 win_close(curwin, FALSE);
348 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200349 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350 }
351 }
352 break;
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/* cursor to top-left window */
355 case 't':
356 case Ctrl_T:
357 win_goto(firstwin);
358 break;
359
360/* cursor to bottom-right window */
361 case 'b':
362 case Ctrl_B:
363 win_goto(lastwin);
364 break;
365
366/* cursor to last accessed (previous) window */
367 case 'p':
368 case Ctrl_P:
369 if (prevwin == NULL)
370 beep_flush();
371 else
372 win_goto(prevwin);
373 break;
374
375/* exchange current and next window */
376 case 'x':
377 case Ctrl_X:
378 CHECK_CMDWIN
379 win_exchange(Prenum);
380 break;
381
382/* rotate windows downwards */
383 case Ctrl_R:
384 case 'r':
385 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 win_rotate(FALSE, (int)Prenum1); /* downwards */
388 break;
389
390/* rotate windows upwards */
391 case 'R':
392 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 win_rotate(TRUE, (int)Prenum1); /* upwards */
395 break;
396
397/* move window to the very top/bottom/left/right */
398 case 'K':
399 case 'J':
400#ifdef FEAT_VERTSPLIT
401 case 'H':
402 case 'L':
403#endif
404 CHECK_CMDWIN
405 win_totop((int)Prenum,
406 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
407 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
408 break;
409
410/* make all windows the same height */
411 case '=':
412#ifdef FEAT_GUI
413 need_mouse_correct = TRUE;
414#endif
415 win_equal(NULL, FALSE, 'b');
416 break;
417
418/* increase current window height */
419 case '+':
420#ifdef FEAT_GUI
421 need_mouse_correct = TRUE;
422#endif
423 win_setheight(curwin->w_height + (int)Prenum1);
424 break;
425
426/* decrease current window height */
427 case '-':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_setheight(curwin->w_height - (int)Prenum1);
432 break;
433
434/* set current window height */
435 case Ctrl__:
436 case '_':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setheight(Prenum ? (int)Prenum : 9999);
441 break;
442
443#ifdef FEAT_VERTSPLIT
444/* increase current window width */
445 case '>':
446#ifdef FEAT_GUI
447 need_mouse_correct = TRUE;
448#endif
449 win_setwidth(curwin->w_width + (int)Prenum1);
450 break;
451
452/* decrease current window width */
453 case '<':
454#ifdef FEAT_GUI
455 need_mouse_correct = TRUE;
456#endif
457 win_setwidth(curwin->w_width - (int)Prenum1);
458 break;
459
460/* set current window width */
461 case '|':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
466 break;
467#endif
468
469/* jump to tag and split window if tag exists (in preview window) */
470#if defined(FEAT_QUICKFIX)
471 case '}':
472 CHECK_CMDWIN
473 if (Prenum)
474 g_do_tagpreview = Prenum;
475 else
476 g_do_tagpreview = p_pvh;
477 /*FALLTHROUGH*/
478#endif
479 case ']':
480 case Ctrl_RSB:
481 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200482 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Prenum)
484 postponed_split = Prenum;
485 else
486 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200487#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200488 if (nchar != '}')
489 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
Bram Moolenaard355c502014-09-23 13:48:43 +0200492 /* Execute the command right here, required when "wincmd ]"
493 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 do_nv_ident(Ctrl_RSB, NUL);
495 break;
496
497#ifdef FEAT_SEARCHPATH
498/* edit file name under cursor in a new window */
499 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000500 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000502wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000504
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000505 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (ptr != NULL)
507 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000508# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 setpcmark();
512 if (win_split(0, 0) == OK)
513 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200514 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000515 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
516 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000517 if (nchar == 'F' && lnum >= 0)
518 {
519 curwin->w_cursor.lnum = lnum;
520 check_cursor_lnum();
521 beginline(BL_SOL | BL_FIX);
522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 }
524 vim_free(ptr);
525 }
526 break;
527#endif
528
529#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000530/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 * new window -- webb
532 */
533 case 'i': /* Go to any match */
534 case Ctrl_I:
535 type = FIND_ANY;
536 /* FALLTHROUGH */
537 case 'd': /* Go to definition, using 'define' */
538 case Ctrl_D:
539 CHECK_CMDWIN
540 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
541 break;
542 find_pattern_in_path(ptr, 0, len, TRUE,
543 Prenum == 0 ? TRUE : FALSE, type,
544 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
545 curwin->w_set_curswant = TRUE;
546 break;
547#endif
548
Bram Moolenaar05159a02005-02-26 23:04:13 +0000549 case K_KENTER:
550 case CAR:
551#if defined(FEAT_QUICKFIX)
552 /*
553 * In a quickfix window a <CR> jumps to the error under the
554 * cursor in a new window.
555 */
556 if (bt_quickfix(curbuf))
557 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000558 sprintf((char *)cbuf, "split +%ld%s",
559 (long)curwin->w_cursor.lnum,
560 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000561 do_cmdline_cmd(cbuf);
562 }
563#endif
564 break;
565
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567/* CTRL-W g extended commands */
568 case 'g':
569 case Ctrl_G:
570 CHECK_CMDWIN
571#ifdef USE_ON_FLY_SCROLL
572 dont_scroll = TRUE; /* disallow scrolling here */
573#endif
574 ++no_mapping;
575 ++allow_keys; /* no mapping for xchar, but allow key codes */
576 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000577 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 --no_mapping;
580 --allow_keys;
581#ifdef FEAT_CMDL_INFO
582 (void)add_to_showcmd(xchar);
583#endif
584 switch (xchar)
585 {
586#if defined(FEAT_QUICKFIX)
587 case '}':
588 xchar = Ctrl_RSB;
589 if (Prenum)
590 g_do_tagpreview = Prenum;
591 else
592 g_do_tagpreview = p_pvh;
593 /*FALLTHROUGH*/
594#endif
595 case ']':
596 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200597 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (Prenum)
599 postponed_split = Prenum;
600 else
601 postponed_split = -1;
602
603 /* Execute the command right here, required when
604 * "wincmd g}" was used in a function. */
605 do_nv_ident('g', xchar);
606 break;
607
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000608#ifdef FEAT_SEARCHPATH
609 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000610 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100611 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000612 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000613 goto wingotofile;
614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 default:
616 beep_flush();
617 break;
618 }
619 break;
620
621 default: beep_flush();
622 break;
623 }
624}
625
626/*
627 * split the current window, implements CTRL-W s and :split
628 *
629 * "size" is the height or width for the new window, 0 to use half of current
630 * height or width.
631 *
632 * "flags":
633 * WSP_ROOM: require enough room for new window
634 * WSP_VERT: vertical split.
635 * WSP_TOP: open window at the top-left of the shell (help window).
636 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
637 * WSP_HELP: creating the help window, keep layout snapshot
638 *
639 * return FAIL for failure, OK otherwise
640 */
641 int
642win_split(size, flags)
643 int size;
644 int flags;
645{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000646 /* When the ":tab" modifier was used open a new tab page instead. */
647 if (may_open_tabpage() == OK)
648 return OK;
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 /* Add flags from ":vertical", ":topleft" and ":botright". */
651 flags |= cmdmod.split;
652 if ((flags & WSP_TOP) && (flags & WSP_BOT))
653 {
654 EMSG(_("E442: Can't split topleft and botright at the same time"));
655 return FAIL;
656 }
657
658 /* When creating the help window make a snapshot of the window layout.
659 * Otherwise clear the snapshot, it's now invalid. */
660 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000661 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000663 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
665 return win_split_ins(size, flags, NULL, 0);
666}
667
668/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100669 * When "new_wp" is NULL: split the current window in two.
670 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 * top/left/right/bottom.
672 * return FAIL for failure, OK otherwise
673 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000674 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100675win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 int size;
677 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100678 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 int dir;
680{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100681 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 win_T *oldwin;
683 int new_size = size;
684 int i;
685 int need_status = 0;
686 int do_equal = FALSE;
687 int needed;
688 int available;
689 int oldwin_height = 0;
690 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200691 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200693 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200694 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696 if (flags & WSP_TOP)
697 oldwin = firstwin;
698 else if (flags & WSP_BOT)
699 oldwin = lastwin;
700 else
701 oldwin = curwin;
702
703 /* add a status line when p_ls == 1 and splitting the first window */
704 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
705 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100706 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 EMSG(_(e_noroom));
709 return FAIL;
710 }
711 need_status = STATUS_HEIGHT;
712 }
713
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000714#ifdef FEAT_GUI
715 /* May be needed for the scrollbars that are going to change. */
716 if (gui.in_use)
717 out_flush();
718#endif
719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720#ifdef FEAT_VERTSPLIT
721 if (flags & WSP_VERT)
722 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200723 int wmw1;
724 int minwidth;
725
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
728 /*
729 * Check if we are able to split the current window and compute its
730 * width.
731 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200732 /* Current window requires at least 1 space. */
733 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
734 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200736 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200737 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200739 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200741 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200743 else if (p_ea)
744 {
745 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
746 prevfrp = oldwin->w_frame;
747 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
748 frp = frp->fr_parent)
749 {
750 if (frp->fr_layout == FR_ROW)
751 for (frp2 = frp->fr_child; frp2 != NULL;
752 frp2 = frp2->fr_next)
753 if (frp2 != prevfrp)
754 minwidth += frame_minwidth(frp2, NOWIN);
755 prevfrp = frp;
756 }
757 available = topframe->fr_width;
758 needed += minwidth;
759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200761 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200762 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
763 available = oldwin->w_frame->fr_width;
764 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200765 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100766 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 {
768 EMSG(_(e_noroom));
769 return FAIL;
770 }
771 if (new_size == 0)
772 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200773 if (new_size > available - minwidth - 1)
774 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200775 if (new_size < wmw1)
776 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
778 /* if it doesn't fit in the current window, need win_equal() */
779 if (oldwin->w_width - new_size - 1 < p_wmw)
780 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000781
782 /* We don't like to take lines for the new window from a
783 * 'winfixwidth' window. Take them from a window to the left or right
784 * instead, if possible. */
785 if (oldwin->w_p_wfw)
786 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000787
788 /* Only make all windows the same width if one of them (except oldwin)
789 * is wider than one of the split windows. */
790 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
791 && oldwin->w_frame->fr_parent != NULL)
792 {
793 frp = oldwin->w_frame->fr_parent->fr_child;
794 while (frp != NULL)
795 {
796 if (frp->fr_win != oldwin && frp->fr_win != NULL
797 && (frp->fr_win->w_width > new_size
798 || frp->fr_win->w_width > oldwin->w_width
799 - new_size - STATUS_HEIGHT))
800 {
801 do_equal = TRUE;
802 break;
803 }
804 frp = frp->fr_next;
805 }
806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
808 else
809#endif
810 {
811 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
813 /*
814 * Check if we are able to split the current window and compute its
815 * height.
816 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200817 /* Current window requires at least 1 space. */
818 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
819 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200821 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200822 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200824 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200826 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200828 else if (p_ea)
829 {
830 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
831 prevfrp = oldwin->w_frame;
832 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
833 frp = frp->fr_parent)
834 {
835 if (frp->fr_layout == FR_COL)
836 for (frp2 = frp->fr_child; frp2 != NULL;
837 frp2 = frp2->fr_next)
838 if (frp2 != prevfrp)
839 minheight += frame_minheight(frp2, NOWIN);
840 prevfrp = frp;
841 }
842 available = topframe->fr_height;
843 needed += minheight;
844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 else
846 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200847 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
848 available = oldwin->w_frame->fr_height;
849 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100851 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
853 EMSG(_(e_noroom));
854 return FAIL;
855 }
856 oldwin_height = oldwin->w_height;
857 if (need_status)
858 {
859 oldwin->w_status_height = STATUS_HEIGHT;
860 oldwin_height -= STATUS_HEIGHT;
861 }
862 if (new_size == 0)
863 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200864 if (new_size > available - minheight - STATUS_HEIGHT)
865 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200866 if (new_size < wmh1)
867 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 /* if it doesn't fit in the current window, need win_equal() */
870 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
871 do_equal = TRUE;
872
873 /* We don't like to take lines for the new window from a
874 * 'winfixheight' window. Take them from a window above or below
875 * instead, if possible. */
876 if (oldwin->w_p_wfh)
877 {
878 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
879 oldwin);
880 oldwin_height = oldwin->w_height;
881 if (need_status)
882 oldwin_height -= STATUS_HEIGHT;
883 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000884
885 /* Only make all windows the same height if one of them (except oldwin)
886 * is higher than one of the split windows. */
887 if (!do_equal && p_ea && size == 0
888#ifdef FEAT_VERTSPLIT
889 && *p_ead != 'h'
890#endif
891 && oldwin->w_frame->fr_parent != NULL)
892 {
893 frp = oldwin->w_frame->fr_parent->fr_child;
894 while (frp != NULL)
895 {
896 if (frp->fr_win != oldwin && frp->fr_win != NULL
897 && (frp->fr_win->w_height > new_size
898 || frp->fr_win->w_height > oldwin_height - new_size
899 - STATUS_HEIGHT))
900 {
901 do_equal = TRUE;
902 break;
903 }
904 frp = frp->fr_next;
905 }
906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
908
909 /*
910 * allocate new window structure and link it in the window list
911 */
912 if ((flags & WSP_TOP) == 0
913 && ((flags & WSP_BOT)
914 || (flags & WSP_BELOW)
915 || (!(flags & WSP_ABOVE)
916 && (
917#ifdef FEAT_VERTSPLIT
918 (flags & WSP_VERT) ? p_spr :
919#endif
920 p_sb))))
921 {
922 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100923 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000924 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else
926 win_append(oldwin, wp);
927 }
928 else
929 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100930 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000931 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 else
933 win_append(oldwin->w_prev, wp);
934 }
935
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100936 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 if (wp == NULL)
939 return FAIL;
940
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000941 new_frame(wp);
942 if (wp->w_frame == NULL)
943 {
944 win_free(wp, NULL);
945 return FAIL;
946 }
947
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000948 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000949 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951
952 /*
953 * Reorganise the tree of frames to insert the new window.
954 */
955 if (flags & (WSP_TOP | WSP_BOT))
956 {
957#ifdef FEAT_VERTSPLIT
958 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
959 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
960#else
961 if (topframe->fr_layout == FR_COL)
962#endif
963 {
964 curfrp = topframe->fr_child;
965 if (flags & WSP_BOT)
966 while (curfrp->fr_next != NULL)
967 curfrp = curfrp->fr_next;
968 }
969 else
970 curfrp = topframe;
971 before = (flags & WSP_TOP);
972 }
973 else
974 {
975 curfrp = oldwin->w_frame;
976 if (flags & WSP_BELOW)
977 before = FALSE;
978 else if (flags & WSP_ABOVE)
979 before = TRUE;
980 else
981#ifdef FEAT_VERTSPLIT
982 if (flags & WSP_VERT)
983 before = !p_spr;
984 else
985#endif
986 before = !p_sb;
987 }
988 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
989 {
990 /* Need to create a new frame in the tree to make a branch. */
991 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
992 *frp = *curfrp;
993 curfrp->fr_layout = layout;
994 frp->fr_parent = curfrp;
995 frp->fr_next = NULL;
996 frp->fr_prev = NULL;
997 curfrp->fr_child = frp;
998 curfrp->fr_win = NULL;
999 curfrp = frp;
1000 if (frp->fr_win != NULL)
1001 oldwin->w_frame = frp;
1002 else
1003 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1004 frp->fr_parent = curfrp;
1005 }
1006
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001007 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001008 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001010 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 frp->fr_parent = curfrp->fr_parent;
1012
1013 /* Insert the new frame at the right place in the frame list. */
1014 if (before)
1015 frame_insert(curfrp, frp);
1016 else
1017 frame_append(curfrp, frp);
1018
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001019 /* Set w_fraction now so that the cursor keeps the same relative
1020 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001021 if (oldwin->w_height > 0)
1022 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001023 wp->w_fraction = oldwin->w_fraction;
1024
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025#ifdef FEAT_VERTSPLIT
1026 if (flags & WSP_VERT)
1027 {
1028 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (need_status)
1031 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001032 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 oldwin->w_status_height = need_status;
1034 }
1035 if (flags & (WSP_TOP | WSP_BOT))
1036 {
1037 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001038 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001039 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 wp->w_status_height = (p_ls > 0);
1041 }
1042 else
1043 {
1044 /* height and row of new window is same as current window */
1045 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001046 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 wp->w_status_height = oldwin->w_status_height;
1048 }
1049 frp->fr_height = curfrp->fr_height;
1050
1051 /* "new_size" of the current window goes to the new window, use
1052 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001053 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (before)
1055 wp->w_vsep_width = 1;
1056 else
1057 {
1058 wp->w_vsep_width = oldwin->w_vsep_width;
1059 oldwin->w_vsep_width = 1;
1060 }
1061 if (flags & (WSP_TOP | WSP_BOT))
1062 {
1063 if (flags & WSP_BOT)
1064 frame_add_vsep(curfrp);
1065 /* Set width of neighbor frame */
1066 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001067 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1068 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
1070 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001071 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (before) /* new window left of current one */
1073 {
1074 wp->w_wincol = oldwin->w_wincol;
1075 oldwin->w_wincol += new_size + 1;
1076 }
1077 else /* new window right of current one */
1078 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1079 frame_fix_width(oldwin);
1080 frame_fix_width(wp);
1081 }
1082 else
1083#endif
1084 {
1085 /* width and column of new window is same as current window */
1086#ifdef FEAT_VERTSPLIT
1087 if (flags & (WSP_TOP | WSP_BOT))
1088 {
1089 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001090 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 wp->w_vsep_width = 0;
1092 }
1093 else
1094 {
1095 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001096 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 wp->w_vsep_width = oldwin->w_vsep_width;
1098 }
1099 frp->fr_width = curfrp->fr_width;
1100#endif
1101
1102 /* "new_size" of the current window goes to the new window, use
1103 * one row for the status line */
1104 win_new_height(wp, new_size);
1105 if (flags & (WSP_TOP | WSP_BOT))
1106 frame_new_height(curfrp, curfrp->fr_height
1107 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1108 else
1109 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1110 if (before) /* new window above current one */
1111 {
1112 wp->w_winrow = oldwin->w_winrow;
1113 wp->w_status_height = STATUS_HEIGHT;
1114 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1115 }
1116 else /* new window below current one */
1117 {
1118 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1119 wp->w_status_height = oldwin->w_status_height;
1120 oldwin->w_status_height = STATUS_HEIGHT;
1121 }
1122#ifdef FEAT_VERTSPLIT
1123 if (flags & WSP_BOT)
1124 frame_add_statusline(curfrp);
1125#endif
1126 frame_fix_height(wp);
1127 frame_fix_height(oldwin);
1128 }
1129
1130 if (flags & (WSP_TOP | WSP_BOT))
1131 (void)win_comp_pos();
1132
1133 /*
1134 * Both windows need redrawing
1135 */
1136 redraw_win_later(wp, NOT_VALID);
1137 wp->w_redr_status = TRUE;
1138 redraw_win_later(oldwin, NOT_VALID);
1139 oldwin->w_redr_status = TRUE;
1140
1141 if (need_status)
1142 {
1143 msg_row = Rows - 1;
1144 msg_col = sc_col;
1145 msg_clr_eos_force(); /* Old command/ruler may still be there */
1146 comp_col();
1147 msg_row = Rows - 1;
1148 msg_col = 0; /* put position back at start of line */
1149 }
1150
1151 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001152 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 */
1154 if (do_equal || dir != 0)
1155 win_equal(wp, TRUE,
1156#ifdef FEAT_VERTSPLIT
1157 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1158 : dir == 'h' ? 'b' :
1159#endif
1160 'v');
1161
1162 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1163 * size was given. */
1164#ifdef FEAT_VERTSPLIT
1165 if (flags & WSP_VERT)
1166 {
1167 i = p_wiw;
1168 if (size != 0)
1169 p_wiw = size;
1170
1171# ifdef FEAT_GUI
1172 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1173 if (gui.in_use)
1174 gui_init_which_components(NULL);
1175# endif
1176 }
1177 else
1178#endif
1179 {
1180 i = p_wh;
1181 if (size != 0)
1182 p_wh = size;
1183 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001184
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001185#ifdef FEAT_JUMPLIST
1186 /* Keep same changelist position in new window. */
1187 wp->w_changelistidx = oldwin->w_changelistidx;
1188#endif
1189
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001190 /*
1191 * make the new window the current window
1192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 win_enter(wp, FALSE);
1194#ifdef FEAT_VERTSPLIT
1195 if (flags & WSP_VERT)
1196 p_wiw = i;
1197 else
1198#endif
1199 p_wh = i;
1200
1201 return OK;
1202}
1203
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001204
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001205/*
1206 * Initialize window "newp" from window "oldp".
1207 * Used when splitting a window and when creating a new tab page.
1208 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001209 * WSP_NEWLOC may be specified in flags to prevent the location list from
1210 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001211 */
1212 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001213win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001214 win_T *newp;
1215 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001216 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001217{
1218 int i;
1219
1220 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001221#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001222 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001223#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001224 oldp->w_buffer->b_nwindows++;
1225 newp->w_cursor = oldp->w_cursor;
1226 newp->w_valid = 0;
1227 newp->w_curswant = oldp->w_curswant;
1228 newp->w_set_curswant = oldp->w_set_curswant;
1229 newp->w_topline = oldp->w_topline;
1230#ifdef FEAT_DIFF
1231 newp->w_topfill = oldp->w_topfill;
1232#endif
1233 newp->w_leftcol = oldp->w_leftcol;
1234 newp->w_pcmark = oldp->w_pcmark;
1235 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1236 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001237 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001238 newp->w_fraction = oldp->w_fraction;
1239 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1240#ifdef FEAT_JUMPLIST
1241 copy_jumplist(oldp, newp);
1242#endif
1243#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001244 if (flags & WSP_NEWLOC)
1245 {
1246 /* Don't copy the location list. */
1247 newp->w_llist = NULL;
1248 newp->w_llist_ref = NULL;
1249 }
1250 else
1251 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001252#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001253 newp->w_localdir = (oldp->w_localdir == NULL)
1254 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001255
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001256 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001257 for (i = 0; i < oldp->w_tagstacklen; i++)
1258 {
1259 newp->w_tagstack[i] = oldp->w_tagstack[i];
1260 if (newp->w_tagstack[i].tagname != NULL)
1261 newp->w_tagstack[i].tagname =
1262 vim_strsave(newp->w_tagstack[i].tagname);
1263 }
1264 newp->w_tagstackidx = oldp->w_tagstackidx;
1265 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001266#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001267 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001268#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001269
1270 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001271
Bram Moolenaara971b822011-09-14 14:43:25 +02001272#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001273 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001274#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001275}
1276
1277/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001278 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001279 * Only the essential things are copied.
1280 */
1281 static void
1282win_init_some(newp, oldp)
1283 win_T *newp;
1284 win_T *oldp;
1285{
1286 /* Use the same argument list. */
1287 newp->w_alist = oldp->w_alist;
1288 ++newp->w_alist->al_refcount;
1289 newp->w_arg_idx = oldp->w_arg_idx;
1290
1291 /* copy options from existing window */
1292 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001293}
1294
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295#endif /* FEAT_WINDOWS */
1296
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297#if defined(FEAT_WINDOWS) || defined(PROTO)
1298/*
1299 * Check if "win" is a pointer to an existing window.
1300 */
1301 int
1302win_valid(win)
1303 win_T *win;
1304{
1305 win_T *wp;
1306
1307 if (win == NULL)
1308 return FALSE;
1309 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1310 if (wp == win)
1311 return TRUE;
1312 return FALSE;
1313}
1314
1315/*
1316 * Return the number of windows.
1317 */
1318 int
1319win_count()
1320{
1321 win_T *wp;
1322 int count = 0;
1323
1324 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1325 ++count;
1326 return count;
1327}
1328
1329/*
1330 * Make "count" windows on the screen.
1331 * Return actual number of windows on the screen.
1332 * Must be called when there is just one window, filling the whole screen
1333 * (excluding the command line).
1334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 int
1336make_windows(count, vertical)
1337 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001338 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 int maxcount;
1341 int todo;
1342
1343#ifdef FEAT_VERTSPLIT
1344 if (vertical)
1345 {
1346 /* Each windows needs at least 'winminwidth' lines and a separator
1347 * column. */
1348 maxcount = (curwin->w_width + curwin->w_vsep_width
1349 - (p_wiw - p_wmw)) / (p_wmw + 1);
1350 }
1351 else
1352#endif
1353 {
1354 /* Each window needs at least 'winminheight' lines and a status line. */
1355 maxcount = (curwin->w_height + curwin->w_status_height
1356 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1357 }
1358
1359 if (maxcount < 2)
1360 maxcount = 2;
1361 if (count > maxcount)
1362 count = maxcount;
1363
1364 /*
1365 * add status line now, otherwise first window will be too big
1366 */
1367 if (count > 1)
1368 last_status(TRUE);
1369
1370#ifdef FEAT_AUTOCMD
1371 /*
1372 * Don't execute autocommands while creating the windows. Must do that
1373 * when putting the buffers in the windows.
1374 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001375 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376#endif
1377
1378 /* todo is number of windows left to create */
1379 for (todo = count - 1; todo > 0; --todo)
1380#ifdef FEAT_VERTSPLIT
1381 if (vertical)
1382 {
1383 if (win_split(curwin->w_width - (curwin->w_width - todo)
1384 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1385 break;
1386 }
1387 else
1388#endif
1389 {
1390 if (win_split(curwin->w_height - (curwin->w_height - todo
1391 * STATUS_HEIGHT) / (todo + 1)
1392 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1393 break;
1394 }
1395
1396#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001397 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#endif
1399
1400 /* return actual number of windows */
1401 return (count - todo);
1402}
1403
1404/*
1405 * Exchange current and next window
1406 */
1407 static void
1408win_exchange(Prenum)
1409 long Prenum;
1410{
1411 frame_T *frp;
1412 frame_T *frp2;
1413 win_T *wp;
1414 win_T *wp2;
1415 int temp;
1416
1417 if (lastwin == firstwin) /* just one window */
1418 {
1419 beep_flush();
1420 return;
1421 }
1422
1423#ifdef FEAT_GUI
1424 need_mouse_correct = TRUE;
1425#endif
1426
1427 /*
1428 * find window to exchange with
1429 */
1430 if (Prenum)
1431 {
1432 frp = curwin->w_frame->fr_parent->fr_child;
1433 while (frp != NULL && --Prenum > 0)
1434 frp = frp->fr_next;
1435 }
1436 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1437 frp = curwin->w_frame->fr_next;
1438 else /* Swap last window in row/col with previous */
1439 frp = curwin->w_frame->fr_prev;
1440
1441 /* We can only exchange a window with another window, not with a frame
1442 * containing windows. */
1443 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1444 return;
1445 wp = frp->fr_win;
1446
1447/*
1448 * 1. remove curwin from the list. Remember after which window it was in wp2
1449 * 2. insert curwin before wp in the list
1450 * if wp != wp2
1451 * 3. remove wp from the list
1452 * 4. insert wp after wp2
1453 * 5. exchange the status line height and vsep width.
1454 */
1455 wp2 = curwin->w_prev;
1456 frp2 = curwin->w_frame->fr_prev;
1457 if (wp->w_prev != curwin)
1458 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001459 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 frame_remove(curwin->w_frame);
1461 win_append(wp->w_prev, curwin);
1462 frame_insert(frp, curwin->w_frame);
1463 }
1464 if (wp != wp2)
1465 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001466 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 frame_remove(wp->w_frame);
1468 win_append(wp2, wp);
1469 if (frp2 == NULL)
1470 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1471 else
1472 frame_append(frp2, wp->w_frame);
1473 }
1474 temp = curwin->w_status_height;
1475 curwin->w_status_height = wp->w_status_height;
1476 wp->w_status_height = temp;
1477#ifdef FEAT_VERTSPLIT
1478 temp = curwin->w_vsep_width;
1479 curwin->w_vsep_width = wp->w_vsep_width;
1480 wp->w_vsep_width = temp;
1481
1482 /* If the windows are not in the same frame, exchange the sizes to avoid
1483 * messing up the window layout. Otherwise fix the frame sizes. */
1484 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1485 {
1486 temp = curwin->w_height;
1487 curwin->w_height = wp->w_height;
1488 wp->w_height = temp;
1489 temp = curwin->w_width;
1490 curwin->w_width = wp->w_width;
1491 wp->w_width = temp;
1492 }
1493 else
1494 {
1495 frame_fix_height(curwin);
1496 frame_fix_height(wp);
1497 frame_fix_width(curwin);
1498 frame_fix_width(wp);
1499 }
1500#endif
1501
1502 (void)win_comp_pos(); /* recompute window positions */
1503
1504 win_enter(wp, TRUE);
1505 redraw_later(CLEAR);
1506}
1507
1508/*
1509 * rotate windows: if upwards TRUE the second window becomes the first one
1510 * if upwards FALSE the first window becomes the second one
1511 */
1512 static void
1513win_rotate(upwards, count)
1514 int upwards;
1515 int count;
1516{
1517 win_T *wp1;
1518 win_T *wp2;
1519 frame_T *frp;
1520 int n;
1521
1522 if (firstwin == lastwin) /* nothing to do */
1523 {
1524 beep_flush();
1525 return;
1526 }
1527
1528#ifdef FEAT_GUI
1529 need_mouse_correct = TRUE;
1530#endif
1531
1532#ifdef FEAT_VERTSPLIT
1533 /* Check if all frames in this row/col have one window. */
1534 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1535 frp = frp->fr_next)
1536 if (frp->fr_win == NULL)
1537 {
1538 EMSG(_("E443: Cannot rotate when another window is split"));
1539 return;
1540 }
1541#endif
1542
1543 while (count--)
1544 {
1545 if (upwards) /* first window becomes last window */
1546 {
1547 /* remove first window/frame from the list */
1548 frp = curwin->w_frame->fr_parent->fr_child;
1549 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001550 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 frame_remove(frp);
1552
1553 /* find last frame and append removed window/frame after it */
1554 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1555 ;
1556 win_append(frp->fr_win, wp1);
1557 frame_append(frp, wp1->w_frame);
1558
1559 wp2 = frp->fr_win; /* previously last window */
1560 }
1561 else /* last window becomes first window */
1562 {
1563 /* find last window/frame in the list and remove it */
1564 for (frp = curwin->w_frame; frp->fr_next != NULL;
1565 frp = frp->fr_next)
1566 ;
1567 wp1 = frp->fr_win;
1568 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001569 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 frame_remove(frp);
1571
1572 /* append the removed window/frame before the first in the list */
1573 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1574 frame_insert(frp->fr_parent->fr_child, frp);
1575 }
1576
1577 /* exchange status height and vsep width of old and new last window */
1578 n = wp2->w_status_height;
1579 wp2->w_status_height = wp1->w_status_height;
1580 wp1->w_status_height = n;
1581 frame_fix_height(wp1);
1582 frame_fix_height(wp2);
1583#ifdef FEAT_VERTSPLIT
1584 n = wp2->w_vsep_width;
1585 wp2->w_vsep_width = wp1->w_vsep_width;
1586 wp1->w_vsep_width = n;
1587 frame_fix_width(wp1);
1588 frame_fix_width(wp2);
1589#endif
1590
1591 /* recompute w_winrow and w_wincol for all windows */
1592 (void)win_comp_pos();
1593 }
1594
1595 redraw_later(CLEAR);
1596}
1597
1598/*
1599 * Move the current window to the very top/bottom/left/right of the screen.
1600 */
1601 static void
1602win_totop(size, flags)
1603 int size;
1604 int flags;
1605{
1606 int dir;
1607 int height = curwin->w_height;
1608
1609 if (lastwin == firstwin)
1610 {
1611 beep_flush();
1612 return;
1613 }
1614
1615 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001616 (void)winframe_remove(curwin, &dir, NULL);
1617 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 last_status(FALSE); /* may need to remove last status line */
1619 (void)win_comp_pos(); /* recompute window positions */
1620
1621 /* Split a window on the desired side and put the window there. */
1622 (void)win_split_ins(size, flags, curwin, dir);
1623 if (!(flags & WSP_VERT))
1624 {
1625 win_setheight(height);
1626 if (p_ea)
1627 win_equal(curwin, TRUE, 'v');
1628 }
1629
1630#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1631 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1632 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001633 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635}
1636
1637/*
1638 * Move window "win1" to below/right of "win2" and make "win1" the current
1639 * window. Only works within the same frame!
1640 */
1641 void
1642win_move_after(win1, win2)
1643 win_T *win1, *win2;
1644{
1645 int height;
1646
1647 /* check if the arguments are reasonable */
1648 if (win1 == win2)
1649 return;
1650
1651 /* check if there is something to do */
1652 if (win2->w_next != win1)
1653 {
1654 /* may need move the status line/vertical separator of the last window
1655 * */
1656 if (win1 == lastwin)
1657 {
1658 height = win1->w_prev->w_status_height;
1659 win1->w_prev->w_status_height = win1->w_status_height;
1660 win1->w_status_height = height;
1661#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001662 if (win1->w_prev->w_vsep_width == 1)
1663 {
1664 /* Remove the vertical separator from the last-but-one window,
1665 * add it to the last window. Adjust the frame widths. */
1666 win1->w_prev->w_vsep_width = 0;
1667 win1->w_prev->w_frame->fr_width -= 1;
1668 win1->w_vsep_width = 1;
1669 win1->w_frame->fr_width += 1;
1670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671#endif
1672 }
1673 else if (win2 == lastwin)
1674 {
1675 height = win1->w_status_height;
1676 win1->w_status_height = win2->w_status_height;
1677 win2->w_status_height = height;
1678#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001679 if (win1->w_vsep_width == 1)
1680 {
1681 /* Remove the vertical separator from win1, add it to the last
1682 * window, win2. Adjust the frame widths. */
1683 win2->w_vsep_width = 1;
1684 win2->w_frame->fr_width += 1;
1685 win1->w_vsep_width = 0;
1686 win1->w_frame->fr_width -= 1;
1687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688#endif
1689 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001690 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 frame_remove(win1->w_frame);
1692 win_append(win2, win1);
1693 frame_append(win2->w_frame, win1->w_frame);
1694
1695 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1696 redraw_later(NOT_VALID);
1697 }
1698 win_enter(win1, FALSE);
1699}
1700
1701/*
1702 * Make all windows the same height.
1703 * 'next_curwin' will soon be the current window, make sure it has enough
1704 * rows.
1705 */
1706 void
1707win_equal(next_curwin, current, dir)
1708 win_T *next_curwin; /* pointer to current window to be or NULL */
1709 int current; /* do only frame with current window */
1710 int dir; /* 'v' for vertically, 'h' for horizontally,
1711 'b' for both, 0 for using p_ead */
1712{
1713 if (dir == 0)
1714#ifdef FEAT_VERTSPLIT
1715 dir = *p_ead;
1716#else
1717 dir = 'b';
1718#endif
1719 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001720 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001721 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722}
1723
1724/*
1725 * Set a frame to a new position and height, spreading the available room
1726 * equally over contained frames.
1727 * The window "next_curwin" (if not NULL) should at least get the size from
1728 * 'winheight' and 'winwidth' if possible.
1729 */
1730 static void
1731win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1732 win_T *next_curwin; /* pointer to current window to be or NULL */
1733 int current; /* do only frame with current window */
1734 frame_T *topfr; /* frame to set size off */
1735 int dir; /* 'v', 'h' or 'b', see win_equal() */
1736 int col; /* horizontal position for frame */
1737 int row; /* vertical position for frame */
1738 int width; /* new width of frame */
1739 int height; /* new height of frame */
1740{
1741 int n, m;
1742 int extra_sep = 0;
1743 int wincount, totwincount = 0;
1744 frame_T *fr;
1745 int next_curwin_size = 0;
1746 int room = 0;
1747 int new_size;
1748 int has_next_curwin = 0;
1749 int hnc;
1750
1751 if (topfr->fr_layout == FR_LEAF)
1752 {
1753 /* Set the width/height of this frame.
1754 * Redraw when size or position changes */
1755 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1756#ifdef FEAT_VERTSPLIT
1757 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1758#endif
1759 )
1760 {
1761 topfr->fr_win->w_winrow = row;
1762 frame_new_height(topfr, height, FALSE, FALSE);
1763#ifdef FEAT_VERTSPLIT
1764 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001765 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#endif
1767 redraw_all_later(CLEAR);
1768 }
1769 }
1770#ifdef FEAT_VERTSPLIT
1771 else if (topfr->fr_layout == FR_ROW)
1772 {
1773 topfr->fr_width = width;
1774 topfr->fr_height = height;
1775
1776 if (dir != 'v') /* equalize frame widths */
1777 {
1778 /* Compute the maximum number of windows horizontally in this
1779 * frame. */
1780 n = frame_minwidth(topfr, NOWIN);
1781 /* add one for the rightmost window, it doesn't have a separator */
1782 if (col + width == Columns)
1783 extra_sep = 1;
1784 else
1785 extra_sep = 0;
1786 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001787 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001789 /*
1790 * Compute width for "next_curwin" window and room available for
1791 * other windows.
1792 * "m" is the minimal width when counting p_wiw for "next_curwin".
1793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 m = frame_minwidth(topfr, next_curwin);
1795 room = width - m;
1796 if (room < 0)
1797 {
1798 next_curwin_size = p_wiw + room;
1799 room = 0;
1800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 else
1802 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001803 next_curwin_size = -1;
1804 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1805 {
1806 /* If 'winfixwidth' set keep the window width if
1807 * possible.
1808 * Watch out for this window being the next_curwin. */
1809 if (frame_fixed_width(fr))
1810 {
1811 n = frame_minwidth(fr, NOWIN);
1812 new_size = fr->fr_width;
1813 if (frame_has_win(fr, next_curwin))
1814 {
1815 room += p_wiw - p_wmw;
1816 next_curwin_size = 0;
1817 if (new_size < p_wiw)
1818 new_size = p_wiw;
1819 }
1820 else
1821 /* These windows don't use up room. */
1822 totwincount -= (n + (fr->fr_next == NULL
1823 ? extra_sep : 0)) / (p_wmw + 1);
1824 room -= new_size - n;
1825 if (room < 0)
1826 {
1827 new_size += room;
1828 room = 0;
1829 }
1830 fr->fr_newwidth = new_size;
1831 }
1832 }
1833 if (next_curwin_size == -1)
1834 {
1835 if (!has_next_curwin)
1836 next_curwin_size = 0;
1837 else if (totwincount > 1
1838 && (room + (totwincount - 2))
1839 / (totwincount - 1) > p_wiw)
1840 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001841 /* Can make all windows wider than 'winwidth', spread
1842 * the room equally. */
1843 next_curwin_size = (room + p_wiw
1844 + (totwincount - 1) * p_wmw
1845 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001846 room -= next_curwin_size - p_wiw;
1847 }
1848 else
1849 next_curwin_size = p_wiw;
1850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001852
1853 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 --totwincount; /* don't count curwin */
1855 }
1856
1857 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1858 {
1859 n = m = 0;
1860 wincount = 1;
1861 if (fr->fr_next == NULL)
1862 /* last frame gets all that remains (avoid roundoff error) */
1863 new_size = width;
1864 else if (dir == 'v')
1865 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001866 else if (frame_fixed_width(fr))
1867 {
1868 new_size = fr->fr_newwidth;
1869 wincount = 0; /* doesn't count as a sizeable window */
1870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 else
1872 {
1873 /* Compute the maximum number of windows horiz. in "fr". */
1874 n = frame_minwidth(fr, NOWIN);
1875 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1876 / (p_wmw + 1);
1877 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001878 if (has_next_curwin)
1879 hnc = frame_has_win(fr, next_curwin);
1880 else
1881 hnc = FALSE;
1882 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001884 if (totwincount == 0)
1885 new_size = room;
1886 else
1887 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001889 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 {
1891 next_curwin_size -= p_wiw - (m - n);
1892 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001893 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001895 else
1896 room -= new_size;
1897 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001900 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 * window, unless equalizing all frames. */
1902 if (!current || dir != 'v' || topfr->fr_parent != NULL
1903 || (new_size != fr->fr_width)
1904 || frame_has_win(fr, next_curwin))
1905 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001906 new_size, height);
1907 col += new_size;
1908 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 totwincount -= wincount;
1910 }
1911 }
1912#endif
1913 else /* topfr->fr_layout == FR_COL */
1914 {
1915#ifdef FEAT_VERTSPLIT
1916 topfr->fr_width = width;
1917#endif
1918 topfr->fr_height = height;
1919
1920 if (dir != 'h') /* equalize frame heights */
1921 {
1922 /* Compute maximum number of windows vertically in this frame. */
1923 n = frame_minheight(topfr, NOWIN);
1924 /* add one for the bottom window if it doesn't have a statusline */
1925 if (row + height == cmdline_row && p_ls == 0)
1926 extra_sep = 1;
1927 else
1928 extra_sep = 0;
1929 totwincount = (n + extra_sep) / (p_wmh + 1);
1930 has_next_curwin = frame_has_win(topfr, next_curwin);
1931
1932 /*
1933 * Compute height for "next_curwin" window and room available for
1934 * other windows.
1935 * "m" is the minimal height when counting p_wh for "next_curwin".
1936 */
1937 m = frame_minheight(topfr, next_curwin);
1938 room = height - m;
1939 if (room < 0)
1940 {
1941 /* The room is less then 'winheight', use all space for the
1942 * current window. */
1943 next_curwin_size = p_wh + room;
1944 room = 0;
1945 }
1946 else
1947 {
1948 next_curwin_size = -1;
1949 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1950 {
1951 /* If 'winfixheight' set keep the window height if
1952 * possible.
1953 * Watch out for this window being the next_curwin. */
1954 if (frame_fixed_height(fr))
1955 {
1956 n = frame_minheight(fr, NOWIN);
1957 new_size = fr->fr_height;
1958 if (frame_has_win(fr, next_curwin))
1959 {
1960 room += p_wh - p_wmh;
1961 next_curwin_size = 0;
1962 if (new_size < p_wh)
1963 new_size = p_wh;
1964 }
1965 else
1966 /* These windows don't use up room. */
1967 totwincount -= (n + (fr->fr_next == NULL
1968 ? extra_sep : 0)) / (p_wmh + 1);
1969 room -= new_size - n;
1970 if (room < 0)
1971 {
1972 new_size += room;
1973 room = 0;
1974 }
1975 fr->fr_newheight = new_size;
1976 }
1977 }
1978 if (next_curwin_size == -1)
1979 {
1980 if (!has_next_curwin)
1981 next_curwin_size = 0;
1982 else if (totwincount > 1
1983 && (room + (totwincount - 2))
1984 / (totwincount - 1) > p_wh)
1985 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001986 /* can make all windows higher than 'winheight',
1987 * spread the room equally. */
1988 next_curwin_size = (room + p_wh
1989 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 + (totwincount - 1)) / totwincount;
1991 room -= next_curwin_size - p_wh;
1992 }
1993 else
1994 next_curwin_size = p_wh;
1995 }
1996 }
1997
1998 if (has_next_curwin)
1999 --totwincount; /* don't count curwin */
2000 }
2001
2002 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2003 {
2004 n = m = 0;
2005 wincount = 1;
2006 if (fr->fr_next == NULL)
2007 /* last frame gets all that remains (avoid roundoff error) */
2008 new_size = height;
2009 else if (dir == 'h')
2010 new_size = fr->fr_height;
2011 else if (frame_fixed_height(fr))
2012 {
2013 new_size = fr->fr_newheight;
2014 wincount = 0; /* doesn't count as a sizeable window */
2015 }
2016 else
2017 {
2018 /* Compute the maximum number of windows vert. in "fr". */
2019 n = frame_minheight(fr, NOWIN);
2020 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2021 / (p_wmh + 1);
2022 m = frame_minheight(fr, next_curwin);
2023 if (has_next_curwin)
2024 hnc = frame_has_win(fr, next_curwin);
2025 else
2026 hnc = FALSE;
2027 if (hnc) /* don't count next_curwin */
2028 --wincount;
2029 if (totwincount == 0)
2030 new_size = room;
2031 else
2032 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2033 / totwincount;
2034 if (hnc) /* add next_curwin size */
2035 {
2036 next_curwin_size -= p_wh - (m - n);
2037 new_size += next_curwin_size;
2038 room -= new_size - next_curwin_size;
2039 }
2040 else
2041 room -= new_size;
2042 new_size += n;
2043 }
2044 /* Skip frame that is full width when splitting or closing a
2045 * window, unless equalizing all frames. */
2046 if (!current || dir != 'h' || topfr->fr_parent != NULL
2047 || (new_size != fr->fr_height)
2048 || frame_has_win(fr, next_curwin))
2049 win_equal_rec(next_curwin, current, fr, dir, col, row,
2050 width, new_size);
2051 row += new_size;
2052 height -= new_size;
2053 totwincount -= wincount;
2054 }
2055 }
2056}
2057
2058/*
2059 * close all windows for buffer 'buf'
2060 */
2061 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002062close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002064 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002066 win_T *wp;
2067 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002068 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002071
2072 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002074 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2075#ifdef FEAT_AUTOCMD
2076 && !(wp->w_closing || wp->w_buffer->b_closing)
2077#endif
2078 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002080 win_close(wp, FALSE);
2081
2082 /* Start all over, autocommands may change the window layout. */
2083 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002086 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002088
2089 /* Also check windows in other tab pages. */
2090 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2091 {
2092 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002093 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002094 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002095 if (wp->w_buffer == buf
2096#ifdef FEAT_AUTOCMD
2097 && !(wp->w_closing || wp->w_buffer->b_closing)
2098#endif
2099 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002100 {
2101 win_close_othertab(wp, FALSE, tp);
2102
2103 /* Start all over, the tab page may be closed and
2104 * autocommands may change the window layout. */
2105 nexttp = first_tabpage;
2106 break;
2107 }
2108 }
2109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002111
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002112 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002113 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002114 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115}
2116
2117/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002118 * Return TRUE if the current window is the only window that exists (ignoring
2119 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002120 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002121 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002122 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002123last_window()
2124{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002125 return (one_window() && first_tabpage->tp_next == NULL);
2126}
2127
2128/*
2129 * Return TRUE if there is only one window other than "aucmd_win" in the
2130 * current tab page.
2131 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002132 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002133one_window()
2134{
2135#ifdef FEAT_AUTOCMD
2136 win_T *wp;
2137 int seen_one = FALSE;
2138
2139 FOR_ALL_WINDOWS(wp)
2140 {
2141 if (wp != aucmd_win)
2142 {
2143 if (seen_one)
2144 return FALSE;
2145 seen_one = TRUE;
2146 }
2147 }
2148 return TRUE;
2149#else
2150 return firstwin == lastwin;
2151#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002152}
2153
2154/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002155 * Close the possibly last window in a tab page.
2156 * Returns TRUE when the window was closed already.
2157 */
2158 static int
2159close_last_window_tabpage(win, free_buf, prev_curtab)
2160 win_T *win;
2161 int free_buf;
2162 tabpage_T *prev_curtab;
2163{
2164 if (firstwin == lastwin)
2165 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002166#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002167 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002168#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002169
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002170 /*
2171 * Closing the last window in a tab page. First go to another tab
2172 * page and then close the window and the tab page. This avoids that
2173 * curwin and curtab are invalid while we are freeing memory, they may
2174 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002175 * Don't trigger autocommands yet, they may use wrong values, so do
2176 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002177 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002178 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002179 redraw_tabline = TRUE;
2180
2181 /* Safety check: Autocommands may have closed the window when jumping
2182 * to the other tab page. */
2183 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2184 {
2185 int h = tabline_height();
2186
2187 win_close_othertab(win, free_buf, prev_curtab);
2188 if (h != tabline_height())
2189 shell_new_rows();
2190 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002191 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2192 * that now. */
2193#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002194 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002195 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2196 if (old_curbuf != curbuf)
2197 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002198#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002199 return TRUE;
2200 }
2201 return FALSE;
2202}
2203
2204/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002205 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 * If "free_buf" is TRUE related buffer may be unloaded.
2207 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002208 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002209 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002211 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212win_close(win, free_buf)
2213 win_T *win;
2214 int free_buf;
2215{
2216 win_T *wp;
2217#ifdef FEAT_AUTOCMD
2218 int other_buffer = FALSE;
2219#endif
2220 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 int dir;
2222 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002223 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002225 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
2227 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002228 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002231#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002232 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002233 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002234 if (win == aucmd_win)
2235 {
2236 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002237 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002238 }
2239 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2240 {
2241 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002242 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002243 }
2244#endif
2245
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002246 /* When closing the last window in a tab page first go to another tab page
2247 * and then close the window and the tab page to avoid that curwin and
2248 * curtab are invalid while we are freeing memory. */
2249 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002250 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002251
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 /* When closing the help window, try restoring a snapshot after closing
2253 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002254 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 help_window = TRUE;
2256 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002257 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259#ifdef FEAT_AUTOCMD
2260 if (win == curwin)
2261 {
2262 /*
2263 * Guess which window is going to be the new current window.
2264 * This may change because of the autocommands (sigh).
2265 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002266 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
2268 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002269 * Be careful: If autocommands delete the window or cause this window
2270 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 */
2272 if (wp->w_buffer != curbuf)
2273 {
2274 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002275 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002277 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002278 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002279 win->w_closing = FALSE;
2280 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002283 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002285 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002286 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002287 win->w_closing = FALSE;
2288 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290# ifdef FEAT_EVAL
2291 /* autocmds may abort script processing */
2292 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002293 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294# endif
2295 }
2296#endif
2297
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002298#ifdef FEAT_GUI
2299 /* Avoid trouble with scrollbars that are going to be deleted in
2300 * win_free(). */
2301 if (gui.in_use)
2302 out_flush();
2303#endif
2304
Bram Moolenaara971b822011-09-14 14:43:25 +02002305#ifdef FEAT_SYN_HL
2306 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002307 if (win->w_buffer != NULL)
2308 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002309#endif
2310
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 /*
2312 * Close the link to the buffer.
2313 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002314 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002315 {
2316#ifdef FEAT_AUTOCMD
2317 win->w_closing = TRUE;
2318#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002319 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002320#ifdef FEAT_AUTOCMD
2321 if (win_valid(win))
2322 win->w_closing = FALSE;
2323#endif
2324 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002325
Bram Moolenaar802418d2013-01-17 14:00:11 +01002326 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2327 && (last_window() || curtab != prev_curtab
2328 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002329 {
2330 /* Autocommands have close all windows, quit now. Restore
2331 * curwin->w_buffer, otherwise writing viminfo may fail. */
2332 if (curwin->w_buffer == NULL)
2333 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002334 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002335 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002336
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002338 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002339 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002340 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002341 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
Bram Moolenaara971b822011-09-14 14:43:25 +02002343 /* Free the memory used for the window and get the window that received
2344 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002345 wp = win_free_mem(win, &dir, NULL);
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 /* Make sure curwin isn't invalid. It can cause severe trouble when
2348 * printing an error message. For win_equal() curbuf needs to be valid
2349 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002350 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
2352 curwin = wp;
2353#ifdef FEAT_QUICKFIX
2354 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2355 {
2356 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002357 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 * finding another window to go to.
2359 */
2360 for (;;)
2361 {
2362 if (wp->w_next == NULL)
2363 wp = firstwin;
2364 else
2365 wp = wp->w_next;
2366 if (wp == curwin)
2367 break;
2368 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2369 {
2370 curwin = wp;
2371 break;
2372 }
2373 }
2374 }
2375#endif
2376 curbuf = curwin->w_buffer;
2377 close_curwin = TRUE;
2378 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002379 if (p_ea
2380#ifdef FEAT_VERTSPLIT
2381 && (*p_ead == 'b' || *p_ead == dir)
2382#endif
2383 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 win_equal(curwin, TRUE,
2385#ifdef FEAT_VERTSPLIT
2386 dir
2387#else
2388 0
2389#endif
2390 );
2391 else
2392 win_comp_pos();
2393 if (close_curwin)
2394 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002395 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396#ifdef FEAT_AUTOCMD
2397 if (other_buffer)
2398 /* careful: after this wp and win may be invalid! */
2399 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2400#endif
2401 }
2402
2403 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002404 * If last window has a status line now and we don't want one,
2405 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 */
2407 last_status(FALSE);
2408
2409 /* After closing the help window, try restoring the window layout from
2410 * before it was opened. */
2411 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002412 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2415 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2416 if (gui.in_use && !win_hasvertsplit())
2417 gui_init_which_components(NULL);
2418#endif
2419
2420 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002421 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422}
2423
2424/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002425 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002426 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002427 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002428 * Caller must check if buffer is hidden and whether the tabline needs to be
2429 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002430 */
2431 void
2432win_close_othertab(win, free_buf, tp)
2433 win_T *win;
2434 int free_buf;
2435 tabpage_T *tp;
2436{
2437 win_T *wp;
2438 int dir;
2439 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002440 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002441
Bram Moolenaar362ce482012-06-06 19:02:45 +02002442#ifdef FEAT_AUTOCMD
2443 if (win->w_closing || win->w_buffer->b_closing)
2444 return; /* window is already being closed */
2445#endif
2446
Bram Moolenaarf740b292006-02-16 22:11:02 +00002447 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002448 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002449
2450 /* Careful: Autocommands may have closed the tab page or made it the
2451 * current tab page. */
2452 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2453 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002454 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002455 return;
2456
2457 /* Autocommands may have closed the window already. */
2458 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2459 ;
2460 if (wp == NULL)
2461 return;
2462
Bram Moolenaarf740b292006-02-16 22:11:02 +00002463 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002464 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002465 {
2466 if (tp == first_tabpage)
2467 first_tabpage = tp->tp_next;
2468 else
2469 {
2470 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2471 ptp = ptp->tp_next)
2472 ;
2473 if (ptp == NULL)
2474 {
2475 EMSG2(_(e_intern2), "win_close_othertab()");
2476 return;
2477 }
2478 ptp->tp_next = tp->tp_next;
2479 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002480 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002481 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002482
2483 /* Free the memory used for the window. */
2484 win_free_mem(win, &dir, tp);
2485
2486 if (free_tp)
2487 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002488}
2489
2490/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002491 * Free the memory used for a window.
2492 * Returns a pointer to the window that got the freed up space.
2493 */
2494 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002495win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002496 win_T *win;
2497 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002498 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002499{
2500 frame_T *frp;
2501 win_T *wp;
2502
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002503 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002504 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002505 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002506 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002507 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002508
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002509 /* When deleting the current window of another tab page select a new
2510 * current window. */
2511 if (tp != NULL && win == tp->tp_curwin)
2512 tp->tp_curwin = wp;
2513
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002514 return wp;
2515}
2516
2517#if defined(EXITFREE) || defined(PROTO)
2518 void
2519win_free_all()
2520{
2521 int dummy;
2522
Bram Moolenaarf740b292006-02-16 22:11:02 +00002523# ifdef FEAT_WINDOWS
2524 while (first_tabpage->tp_next != NULL)
2525 tabpage_close(TRUE);
2526# endif
2527
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002528# ifdef FEAT_AUTOCMD
2529 if (aucmd_win != NULL)
2530 {
2531 (void)win_free_mem(aucmd_win, &dummy, NULL);
2532 aucmd_win = NULL;
2533 }
2534# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002535
2536 while (firstwin != NULL)
2537 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002538
2539 /* No window should be used after this. Set curwin to NULL to crash
2540 * instead of using freed memory. */
2541 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002542}
2543#endif
2544
2545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 * Remove a window and its frame from the tree of frames.
2547 * Returns a pointer to the window that got the freed up space.
2548 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002549 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002550winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002552 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002553 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 frame_T *frp, *frp2, *frp3;
2556 frame_T *frp_close = win->w_frame;
2557 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002560 * If there is only one window there is nothing to remove.
2561 */
2562 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2563 return NULL;
2564
2565 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 * Remove the window from its frame.
2567 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 wp = frame2win(frp2);
2570
2571 /* Remove this frame from the list of frames. */
2572 frame_remove(frp_close);
2573
2574#ifdef FEAT_VERTSPLIT
2575 if (frp_close->fr_parent->fr_layout == FR_COL)
2576 {
2577#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002578 /* When 'winfixheight' is set, try to find another frame in the column
2579 * (as close to the closed frame as possible) to distribute the height
2580 * to. */
2581 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2582 {
2583 frp = frp_close->fr_prev;
2584 frp3 = frp_close->fr_next;
2585 while (frp != NULL || frp3 != NULL)
2586 {
2587 if (frp != NULL)
2588 {
2589 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2590 {
2591 frp2 = frp;
2592 wp = frp->fr_win;
2593 break;
2594 }
2595 frp = frp->fr_prev;
2596 }
2597 if (frp3 != NULL)
2598 {
2599 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2600 {
2601 frp2 = frp3;
2602 wp = frp3->fr_win;
2603 break;
2604 }
2605 frp3 = frp3->fr_next;
2606 }
2607 }
2608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2610 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#ifdef FEAT_VERTSPLIT
2612 *dirp = 'v';
2613 }
2614 else
2615 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002616 /* When 'winfixwidth' is set, try to find another frame in the column
2617 * (as close to the closed frame as possible) to distribute the width
2618 * to. */
2619 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2620 {
2621 frp = frp_close->fr_prev;
2622 frp3 = frp_close->fr_next;
2623 while (frp != NULL || frp3 != NULL)
2624 {
2625 if (frp != NULL)
2626 {
2627 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2628 {
2629 frp2 = frp;
2630 wp = frp->fr_win;
2631 break;
2632 }
2633 frp = frp->fr_prev;
2634 }
2635 if (frp3 != NULL)
2636 {
2637 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2638 {
2639 frp2 = frp3;
2640 wp = frp3->fr_win;
2641 break;
2642 }
2643 frp3 = frp3->fr_next;
2644 }
2645 }
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002648 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 *dirp = 'h';
2650 }
2651#endif
2652
2653 /* If rows/columns go to a window below/right its positions need to be
2654 * updated. Can only be done after the sizes have been updated. */
2655 if (frp2 == frp_close->fr_next)
2656 {
2657 int row = win->w_winrow;
2658 int col = W_WINCOL(win);
2659
2660 frame_comp_pos(frp2, &row, &col);
2661 }
2662
2663 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2664 {
2665 /* There is no other frame in this list, move its info to the parent
2666 * and remove it. */
2667 frp2->fr_parent->fr_layout = frp2->fr_layout;
2668 frp2->fr_parent->fr_child = frp2->fr_child;
2669 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2670 frp->fr_parent = frp2->fr_parent;
2671 frp2->fr_parent->fr_win = frp2->fr_win;
2672 if (frp2->fr_win != NULL)
2673 frp2->fr_win->w_frame = frp2->fr_parent;
2674 frp = frp2->fr_parent;
2675 vim_free(frp2);
2676
2677 frp2 = frp->fr_parent;
2678 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2679 {
2680 /* The frame above the parent has the same layout, have to merge
2681 * the frames into this list. */
2682 if (frp2->fr_child == frp)
2683 frp2->fr_child = frp->fr_child;
2684 frp->fr_child->fr_prev = frp->fr_prev;
2685 if (frp->fr_prev != NULL)
2686 frp->fr_prev->fr_next = frp->fr_child;
2687 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2688 {
2689 frp3->fr_parent = frp2;
2690 if (frp3->fr_next == NULL)
2691 {
2692 frp3->fr_next = frp->fr_next;
2693 if (frp->fr_next != NULL)
2694 frp->fr_next->fr_prev = frp3;
2695 break;
2696 }
2697 }
2698 vim_free(frp);
2699 }
2700 }
2701
2702 return wp;
2703}
2704
2705/*
2706 * Find out which frame is going to get the freed up space when "win" is
2707 * closed.
2708 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2709 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2710 * This makes opening a window and closing it immediately keep the same window
2711 * layout.
2712 */
2713 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002714win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002716 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717{
2718 frame_T *frp;
2719 int b;
2720
Bram Moolenaarf740b292006-02-16 22:11:02 +00002721 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002722 /* Last window in this tab page, will go to next tab page. */
2723 return alt_tabpage()->tp_curwin->w_frame;
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 frp = win->w_frame;
2726#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002727 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 b = p_spr;
2729 else
2730#endif
2731 b = p_sb;
2732 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2733 return frp->fr_next;
2734 return frp->fr_prev;
2735}
2736
2737/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002738 * Return the tabpage that will be used if the current one is closed.
2739 */
2740 static tabpage_T *
2741alt_tabpage()
2742{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002743 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002744
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002745 /* Use the next tab page if possible. */
2746 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002747 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002748
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002749 /* Find the last but one tab page. */
2750 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2751 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002752 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002753}
2754
2755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 * Find the left-upper window in frame "frp".
2757 */
2758 static win_T *
2759frame2win(frp)
2760 frame_T *frp;
2761{
2762 while (frp->fr_win == NULL)
2763 frp = frp->fr_child;
2764 return frp->fr_win;
2765}
2766
2767/*
2768 * Return TRUE if frame "frp" contains window "wp".
2769 */
2770 static int
2771frame_has_win(frp, wp)
2772 frame_T *frp;
2773 win_T *wp;
2774{
2775 frame_T *p;
2776
2777 if (frp->fr_layout == FR_LEAF)
2778 return frp->fr_win == wp;
2779
2780 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2781 if (frame_has_win(p, wp))
2782 return TRUE;
2783 return FALSE;
2784}
2785
2786/*
2787 * Set a new height for a frame. Recursively sets the height for contained
2788 * frames and windows. Caller must take care of positions.
2789 */
2790 static void
2791frame_new_height(topfrp, height, topfirst, wfh)
2792 frame_T *topfrp;
2793 int height;
2794 int topfirst; /* resize topmost contained frame first */
2795 int wfh; /* obey 'winfixheight' when there is a choice;
2796 may cause the height not to be set */
2797{
2798 frame_T *frp;
2799 int extra_lines;
2800 int h;
2801
2802 if (topfrp->fr_win != NULL)
2803 {
2804 /* Simple case: just one window. */
2805 win_new_height(topfrp->fr_win,
2806 height - topfrp->fr_win->w_status_height);
2807 }
2808#ifdef FEAT_VERTSPLIT
2809 else if (topfrp->fr_layout == FR_ROW)
2810 {
2811 do
2812 {
2813 /* All frames in this row get the same new height. */
2814 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2815 {
2816 frame_new_height(frp, height, topfirst, wfh);
2817 if (frp->fr_height > height)
2818 {
2819 /* Could not fit the windows, make the whole row higher. */
2820 height = frp->fr_height;
2821 break;
2822 }
2823 }
2824 }
2825 while (frp != NULL);
2826 }
2827#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002828 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
2830 /* Complicated case: Resize a column of frames. Resize the bottom
2831 * frame first, frames above that when needed. */
2832
2833 frp = topfrp->fr_child;
2834 if (wfh)
2835 /* Advance past frames with one window with 'wfh' set. */
2836 while (frame_fixed_height(frp))
2837 {
2838 frp = frp->fr_next;
2839 if (frp == NULL)
2840 return; /* no frame without 'wfh', give up */
2841 }
2842 if (!topfirst)
2843 {
2844 /* Find the bottom frame of this column */
2845 while (frp->fr_next != NULL)
2846 frp = frp->fr_next;
2847 if (wfh)
2848 /* Advance back for frames with one window with 'wfh' set. */
2849 while (frame_fixed_height(frp))
2850 frp = frp->fr_prev;
2851 }
2852
2853 extra_lines = height - topfrp->fr_height;
2854 if (extra_lines < 0)
2855 {
2856 /* reduce height of contained frames, bottom or top frame first */
2857 while (frp != NULL)
2858 {
2859 h = frame_minheight(frp, NULL);
2860 if (frp->fr_height + extra_lines < h)
2861 {
2862 extra_lines += frp->fr_height - h;
2863 frame_new_height(frp, h, topfirst, wfh);
2864 }
2865 else
2866 {
2867 frame_new_height(frp, frp->fr_height + extra_lines,
2868 topfirst, wfh);
2869 break;
2870 }
2871 if (topfirst)
2872 {
2873 do
2874 frp = frp->fr_next;
2875 while (wfh && frp != NULL && frame_fixed_height(frp));
2876 }
2877 else
2878 {
2879 do
2880 frp = frp->fr_prev;
2881 while (wfh && frp != NULL && frame_fixed_height(frp));
2882 }
2883 /* Increase "height" if we could not reduce enough frames. */
2884 if (frp == NULL)
2885 height -= extra_lines;
2886 }
2887 }
2888 else if (extra_lines > 0)
2889 {
2890 /* increase height of bottom or top frame */
2891 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2892 }
2893 }
2894 topfrp->fr_height = height;
2895}
2896
2897/*
2898 * Return TRUE if height of frame "frp" should not be changed because of
2899 * the 'winfixheight' option.
2900 */
2901 static int
2902frame_fixed_height(frp)
2903 frame_T *frp;
2904{
2905 /* frame with one window: fixed height if 'winfixheight' set. */
2906 if (frp->fr_win != NULL)
2907 return frp->fr_win->w_p_wfh;
2908
2909 if (frp->fr_layout == FR_ROW)
2910 {
2911 /* The frame is fixed height if one of the frames in the row is fixed
2912 * height. */
2913 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2914 if (frame_fixed_height(frp))
2915 return TRUE;
2916 return FALSE;
2917 }
2918
2919 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2920 * frames in the row are fixed height. */
2921 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2922 if (!frame_fixed_height(frp))
2923 return FALSE;
2924 return TRUE;
2925}
2926
2927#ifdef FEAT_VERTSPLIT
2928/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002929 * Return TRUE if width of frame "frp" should not be changed because of
2930 * the 'winfixwidth' option.
2931 */
2932 static int
2933frame_fixed_width(frp)
2934 frame_T *frp;
2935{
2936 /* frame with one window: fixed width if 'winfixwidth' set. */
2937 if (frp->fr_win != NULL)
2938 return frp->fr_win->w_p_wfw;
2939
2940 if (frp->fr_layout == FR_COL)
2941 {
2942 /* The frame is fixed width if one of the frames in the row is fixed
2943 * width. */
2944 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2945 if (frame_fixed_width(frp))
2946 return TRUE;
2947 return FALSE;
2948 }
2949
2950 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2951 * frames in the row are fixed width. */
2952 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2953 if (!frame_fixed_width(frp))
2954 return FALSE;
2955 return TRUE;
2956}
2957
2958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 * Add a status line to windows at the bottom of "frp".
2960 * Note: Does not check if there is room!
2961 */
2962 static void
2963frame_add_statusline(frp)
2964 frame_T *frp;
2965{
2966 win_T *wp;
2967
2968 if (frp->fr_layout == FR_LEAF)
2969 {
2970 wp = frp->fr_win;
2971 if (wp->w_status_height == 0)
2972 {
2973 if (wp->w_height > 0) /* don't make it negative */
2974 --wp->w_height;
2975 wp->w_status_height = STATUS_HEIGHT;
2976 }
2977 }
2978 else if (frp->fr_layout == FR_ROW)
2979 {
2980 /* Handle all the frames in the row. */
2981 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2982 frame_add_statusline(frp);
2983 }
2984 else /* frp->fr_layout == FR_COL */
2985 {
2986 /* Only need to handle the last frame in the column. */
2987 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2988 ;
2989 frame_add_statusline(frp);
2990 }
2991}
2992
2993/*
2994 * Set width of a frame. Handles recursively going through contained frames.
2995 * May remove separator line for windows at the right side (for win_close()).
2996 */
2997 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002998frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 frame_T *topfrp;
3000 int width;
3001 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003002 int wfw; /* obey 'winfixwidth' when there is a choice;
3003 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
3005 frame_T *frp;
3006 int extra_cols;
3007 int w;
3008 win_T *wp;
3009
3010 if (topfrp->fr_layout == FR_LEAF)
3011 {
3012 /* Simple case: just one window. */
3013 wp = topfrp->fr_win;
3014 /* Find out if there are any windows right of this one. */
3015 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3016 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3017 break;
3018 if (frp->fr_parent == NULL)
3019 wp->w_vsep_width = 0;
3020 win_new_width(wp, width - wp->w_vsep_width);
3021 }
3022 else if (topfrp->fr_layout == FR_COL)
3023 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003024 do
3025 {
3026 /* All frames in this column get the same new width. */
3027 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3028 {
3029 frame_new_width(frp, width, leftfirst, wfw);
3030 if (frp->fr_width > width)
3031 {
3032 /* Could not fit the windows, make whole column wider. */
3033 width = frp->fr_width;
3034 break;
3035 }
3036 }
3037 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
3039 else /* fr_layout == FR_ROW */
3040 {
3041 /* Complicated case: Resize a row of frames. Resize the rightmost
3042 * frame first, frames left of it when needed. */
3043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003045 if (wfw)
3046 /* Advance past frames with one window with 'wfw' set. */
3047 while (frame_fixed_width(frp))
3048 {
3049 frp = frp->fr_next;
3050 if (frp == NULL)
3051 return; /* no frame without 'wfw', give up */
3052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003054 {
3055 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 while (frp->fr_next != NULL)
3057 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003058 if (wfw)
3059 /* Advance back for frames with one window with 'wfw' set. */
3060 while (frame_fixed_width(frp))
3061 frp = frp->fr_prev;
3062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
3064 extra_cols = width - topfrp->fr_width;
3065 if (extra_cols < 0)
3066 {
3067 /* reduce frame width, rightmost frame first */
3068 while (frp != NULL)
3069 {
3070 w = frame_minwidth(frp, NULL);
3071 if (frp->fr_width + extra_cols < w)
3072 {
3073 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003074 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076 else
3077 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003078 frame_new_width(frp, frp->fr_width + extra_cols,
3079 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 break;
3081 }
3082 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003083 {
3084 do
3085 frp = frp->fr_next;
3086 while (wfw && frp != NULL && frame_fixed_width(frp));
3087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003089 {
3090 do
3091 frp = frp->fr_prev;
3092 while (wfw && frp != NULL && frame_fixed_width(frp));
3093 }
3094 /* Increase "width" if we could not reduce enough frames. */
3095 if (frp == NULL)
3096 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 }
3098 }
3099 else if (extra_cols > 0)
3100 {
3101 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003102 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104 }
3105 topfrp->fr_width = width;
3106}
3107
3108/*
3109 * Add the vertical separator to windows at the right side of "frp".
3110 * Note: Does not check if there is room!
3111 */
3112 static void
3113frame_add_vsep(frp)
3114 frame_T *frp;
3115{
3116 win_T *wp;
3117
3118 if (frp->fr_layout == FR_LEAF)
3119 {
3120 wp = frp->fr_win;
3121 if (wp->w_vsep_width == 0)
3122 {
3123 if (wp->w_width > 0) /* don't make it negative */
3124 --wp->w_width;
3125 wp->w_vsep_width = 1;
3126 }
3127 }
3128 else if (frp->fr_layout == FR_COL)
3129 {
3130 /* Handle all the frames in the column. */
3131 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3132 frame_add_vsep(frp);
3133 }
3134 else /* frp->fr_layout == FR_ROW */
3135 {
3136 /* Only need to handle the last frame in the row. */
3137 frp = frp->fr_child;
3138 while (frp->fr_next != NULL)
3139 frp = frp->fr_next;
3140 frame_add_vsep(frp);
3141 }
3142}
3143
3144/*
3145 * Set frame width from the window it contains.
3146 */
3147 static void
3148frame_fix_width(wp)
3149 win_T *wp;
3150{
3151 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3152}
3153#endif
3154
3155/*
3156 * Set frame height from the window it contains.
3157 */
3158 static void
3159frame_fix_height(wp)
3160 win_T *wp;
3161{
3162 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3163}
3164
3165/*
3166 * Compute the minimal height for frame "topfrp".
3167 * Uses the 'winminheight' option.
3168 * When "next_curwin" isn't NULL, use p_wh for this window.
3169 * When "next_curwin" is NOWIN, don't use at least one line for the current
3170 * window.
3171 */
3172 static int
3173frame_minheight(topfrp, next_curwin)
3174 frame_T *topfrp;
3175 win_T *next_curwin;
3176{
3177 frame_T *frp;
3178 int m;
3179#ifdef FEAT_VERTSPLIT
3180 int n;
3181#endif
3182
3183 if (topfrp->fr_win != NULL)
3184 {
3185 if (topfrp->fr_win == next_curwin)
3186 m = p_wh + topfrp->fr_win->w_status_height;
3187 else
3188 {
3189 /* window: minimal height of the window plus status line */
3190 m = p_wmh + topfrp->fr_win->w_status_height;
3191 /* Current window is minimal one line high */
3192 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3193 ++m;
3194 }
3195 }
3196#ifdef FEAT_VERTSPLIT
3197 else if (topfrp->fr_layout == FR_ROW)
3198 {
3199 /* get the minimal height from each frame in this row */
3200 m = 0;
3201 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3202 {
3203 n = frame_minheight(frp, next_curwin);
3204 if (n > m)
3205 m = n;
3206 }
3207 }
3208#endif
3209 else
3210 {
3211 /* Add up the minimal heights for all frames in this column. */
3212 m = 0;
3213 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3214 m += frame_minheight(frp, next_curwin);
3215 }
3216
3217 return m;
3218}
3219
3220#ifdef FEAT_VERTSPLIT
3221/*
3222 * Compute the minimal width for frame "topfrp".
3223 * When "next_curwin" isn't NULL, use p_wiw for this window.
3224 * When "next_curwin" is NOWIN, don't use at least one column for the current
3225 * window.
3226 */
3227 static int
3228frame_minwidth(topfrp, next_curwin)
3229 frame_T *topfrp;
3230 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3231{
3232 frame_T *frp;
3233 int m, n;
3234
3235 if (topfrp->fr_win != NULL)
3236 {
3237 if (topfrp->fr_win == next_curwin)
3238 m = p_wiw + topfrp->fr_win->w_vsep_width;
3239 else
3240 {
3241 /* window: minimal width of the window plus separator column */
3242 m = p_wmw + topfrp->fr_win->w_vsep_width;
3243 /* Current window is minimal one column wide */
3244 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3245 ++m;
3246 }
3247 }
3248 else if (topfrp->fr_layout == FR_COL)
3249 {
3250 /* get the minimal width from each frame in this column */
3251 m = 0;
3252 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3253 {
3254 n = frame_minwidth(frp, next_curwin);
3255 if (n > m)
3256 m = n;
3257 }
3258 }
3259 else
3260 {
3261 /* Add up the minimal widths for all frames in this row. */
3262 m = 0;
3263 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3264 m += frame_minwidth(frp, next_curwin);
3265 }
3266
3267 return m;
3268}
3269#endif
3270
3271
3272/*
3273 * Try to close all windows except current one.
3274 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3275 * used and the buffer was modified.
3276 *
3277 * Used by ":bdel" and ":only".
3278 */
3279 void
3280close_others(message, forceit)
3281 int message;
3282 int forceit; /* always hide all other windows */
3283{
3284 win_T *wp;
3285 win_T *nextwp;
3286 int r;
3287
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003288 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 {
3290 if (message
3291#ifdef FEAT_AUTOCMD
3292 && !autocmd_busy
3293#endif
3294 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003295 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 return;
3297 }
3298
3299 /* Be very careful here: autocommands may change the window layout. */
3300 for (wp = firstwin; win_valid(wp); wp = nextwp)
3301 {
3302 nextwp = wp->w_next;
3303 if (wp != curwin) /* don't close current window */
3304 {
3305
3306 /* Check if it's allowed to abandon this window */
3307 r = can_abandon(wp->w_buffer, forceit);
3308#ifdef FEAT_AUTOCMD
3309 if (!win_valid(wp)) /* autocommands messed wp up */
3310 {
3311 nextwp = firstwin;
3312 continue;
3313 }
3314#endif
3315 if (!r)
3316 {
3317#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3318 if (message && (p_confirm || cmdmod.confirm) && p_write)
3319 {
3320 dialog_changed(wp->w_buffer, FALSE);
3321# ifdef FEAT_AUTOCMD
3322 if (!win_valid(wp)) /* autocommands messed wp up */
3323 {
3324 nextwp = firstwin;
3325 continue;
3326 }
3327# endif
3328 }
3329 if (bufIsChanged(wp->w_buffer))
3330#endif
3331 continue;
3332 }
3333 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3334 }
3335 }
3336
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003337 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 EMSG(_("E445: Other window contains changes"));
3339}
3340
3341#endif /* FEAT_WINDOWS */
3342
3343/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003344 * Init the current window "curwin".
3345 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 */
3347 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003348curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003350 win_init_empty(curwin);
3351}
3352
3353 void
3354win_init_empty(wp)
3355 win_T *wp;
3356{
3357 redraw_win_later(wp, NOT_VALID);
3358 wp->w_lines_valid = 0;
3359 wp->w_cursor.lnum = 1;
3360 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003362 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003364 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3365 wp->w_pcmark.col = 0;
3366 wp->w_prev_pcmark.lnum = 0;
3367 wp->w_prev_pcmark.col = 0;
3368 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003370 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003372 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003374 if (wp->w_p_rl)
3375 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003377 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003379#ifdef FEAT_SYN_HL
3380 wp->w_s = &wp->w_buffer->b_s;
3381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382}
3383
3384/*
3385 * Allocate the first window and put an empty buffer in it.
3386 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003387 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003389 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390win_alloc_first()
3391{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003392 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003393 return FAIL;
3394
3395#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003396 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003397 if (first_tabpage == NULL)
3398 return FAIL;
3399 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003400 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003401#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003402
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003403 return OK;
3404}
3405
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003406#if defined(FEAT_AUTOCMD) || defined(PROTO)
3407/*
3408 * Init "aucmd_win". This can only be done after the first
3409 * window is fully initialized, thus it can't be in win_alloc_first().
3410 */
3411 void
3412win_alloc_aucmd_win()
3413{
3414 aucmd_win = win_alloc(NULL, TRUE);
3415 if (aucmd_win != NULL)
3416 {
3417 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003418 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003419 new_frame(aucmd_win);
3420 }
3421}
3422#endif
3423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003424/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003425 * Allocate the first window or the first window in a new tab page.
3426 * When "oldwin" is NULL create an empty buffer for it.
3427 * When "oldwin" is not NULL copy info from it to the new window (only with
3428 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003429 * Return FAIL when something goes wrong (out of memory).
3430 */
3431 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003432win_alloc_firstwin(oldwin)
3433 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003434{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003435 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003436 if (oldwin == NULL)
3437 {
3438 /* Very first window, need to create an empty buffer for it and
3439 * initialize from scratch. */
3440 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3441 if (curwin == NULL || curbuf == NULL)
3442 return FAIL;
3443 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003444#ifdef FEAT_SYN_HL
3445 curwin->w_s = &(curbuf->b_s);
3446#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003447 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003449 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003451 curwin_init(); /* init current window */
3452 }
3453#ifdef FEAT_WINDOWS
3454 else
3455 {
3456 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003457 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003458
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003459 /* We don't want cursor- and scroll-binding in the first window. */
3460 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003461 }
3462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003464 new_frame(curwin);
3465 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003466 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003467 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468#ifdef FEAT_VERTSPLIT
3469 topframe->fr_width = Columns;
3470#endif
3471 topframe->fr_height = Rows - p_ch;
3472 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003473
3474 return OK;
3475}
3476
3477/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003478 * Create a frame for window "wp".
3479 */
3480 static void
3481new_frame(win_T *wp)
3482{
3483 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3484
3485 wp->w_frame = frp;
3486 if (frp != NULL)
3487 {
3488 frp->fr_layout = FR_LEAF;
3489 frp->fr_win = wp;
3490 }
3491}
3492
3493/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003494 * Initialize the window and frame size to the maximum.
3495 */
3496 void
3497win_init_size()
3498{
3499 firstwin->w_height = ROWS_AVAIL;
3500 topframe->fr_height = ROWS_AVAIL;
3501#ifdef FEAT_VERTSPLIT
3502 firstwin->w_width = Columns;
3503 topframe->fr_width = Columns;
3504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505}
3506
3507#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003508
3509/*
3510 * Allocate a new tabpage_T and init the values.
3511 * Returns NULL when out of memory.
3512 */
3513 static tabpage_T *
3514alloc_tabpage()
3515{
3516 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003517# ifdef FEAT_GUI
3518 int i;
3519# endif
3520
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003521
3522 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003523 if (tp == NULL)
3524 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003525
Bram Moolenaar429fa852013-04-15 12:27:36 +02003526# ifdef FEAT_EVAL
3527 /* init t: variables */
3528 tp->tp_vars = dict_alloc();
3529 if (tp->tp_vars == NULL)
3530 {
3531 vim_free(tp);
3532 return NULL;
3533 }
3534 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3535# endif
3536
3537# ifdef FEAT_GUI
3538 for (i = 0; i < 3; i++)
3539 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003540# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003541# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003542 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003543# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003544 tp->tp_ch_used = p_ch;
3545
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003546 return tp;
3547}
3548
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003549 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003550free_tabpage(tp)
3551 tabpage_T *tp;
3552{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003553 int idx;
3554
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003555# ifdef FEAT_DIFF
3556 diff_clear(tp);
3557# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003558 for (idx = 0; idx < SNAP_COUNT; ++idx)
3559 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003560#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003561 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3562 hash_init(&tp->tp_vars->dv_hashtab);
3563 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003564#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003565
3566#ifdef FEAT_PYTHON
3567 python_tabpage_free(tp);
3568#endif
3569
3570#ifdef FEAT_PYTHON3
3571 python3_tabpage_free(tp);
3572#endif
3573
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003574 vim_free(tp);
3575}
3576
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003577/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003578 * Create a new Tab page with one window.
3579 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003580 * When "after" is 0 put it just after the current Tab page.
3581 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582 * Return FAIL or OK.
3583 */
3584 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003585win_new_tabpage(after)
3586 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003587{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003588 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003589 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003590 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003591
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003592 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003593 if (newtp == NULL)
3594 return FAIL;
3595
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003596 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003597 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003598 {
3599 vim_free(newtp);
3600 return FAIL;
3601 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003602 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003603
3604 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003605 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003606 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003607 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003608 if (after == 1)
3609 {
3610 /* New tab page becomes the first one. */
3611 newtp->tp_next = first_tabpage;
3612 first_tabpage = newtp;
3613 }
3614 else
3615 {
3616 if (after > 0)
3617 {
3618 /* Put new tab page before tab page "after". */
3619 n = 2;
3620 for (tp = first_tabpage; tp->tp_next != NULL
3621 && n < after; tp = tp->tp_next)
3622 ++n;
3623 }
3624 newtp->tp_next = tp->tp_next;
3625 tp->tp_next = newtp;
3626 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003627 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003628 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003629 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003630
3631 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003632 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003633
3634#if defined(FEAT_GUI)
3635 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3636 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003637 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003638#endif
3639
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003640 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003641#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003642 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003643 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003644#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645 return OK;
3646 }
3647
3648 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003649 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003650 return FAIL;
3651}
3652
3653/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003654 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3655 * like with ":split".
3656 * Returns OK if a new tab page was created, FAIL otherwise.
3657 */
3658 int
3659may_open_tabpage()
3660{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003661 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003662
Bram Moolenaard326ce82007-03-11 14:48:29 +00003663 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003664 {
3665 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003666 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003667 return win_new_tabpage(n);
3668 }
3669 return FAIL;
3670}
3671
3672/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003673 * Create up to "maxcount" tabpages with empty windows.
3674 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003675 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003676 int
3677make_tabpages(maxcount)
3678 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003679{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003680 int count = maxcount;
3681 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003682
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003683 /* Limit to 'tabpagemax' tabs. */
3684 if (count > p_tpm)
3685 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003686
3687#ifdef FEAT_AUTOCMD
3688 /*
3689 * Don't execute autocommands while creating the tab pages. Must do that
3690 * when putting the buffers in the windows.
3691 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003692 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003693#endif
3694
3695 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003696 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003697 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003698
3699#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003700 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003701#endif
3702
3703 /* return actual number of tab pages */
3704 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705}
3706
3707/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003708 * Return TRUE when "tpc" points to a valid tab page.
3709 */
3710 int
3711valid_tabpage(tpc)
3712 tabpage_T *tpc;
3713{
3714 tabpage_T *tp;
3715
3716 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3717 if (tp == tpc)
3718 return TRUE;
3719 return FALSE;
3720}
3721
3722/*
3723 * Find tab page "n" (first one is 1). Returns NULL when not found.
3724 */
3725 tabpage_T *
3726find_tabpage(n)
3727 int n;
3728{
3729 tabpage_T *tp;
3730 int i = 1;
3731
3732 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3733 ++i;
3734 return tp;
3735}
3736
3737/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003738 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003739 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003740 */
3741 int
3742tabpage_index(ftp)
3743 tabpage_T *ftp;
3744{
3745 int i = 1;
3746 tabpage_T *tp;
3747
3748 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3749 ++i;
3750 return i;
3751}
3752
3753/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003754 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003755 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003756 * FAIL.
3757 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003758 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003759 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003760leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003761 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003762 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003763 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003764{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003765 tabpage_T *tp = curtab;
3766
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003768#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003769 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003770 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003771 if (new_curbuf != curbuf)
3772 {
3773 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3774 if (curtab != tp)
3775 return FAIL;
3776 }
3777 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3778 if (curtab != tp)
3779 return FAIL;
3780 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003781 if (curtab != tp)
3782 return FAIL;
3783 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003784#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003785#if defined(FEAT_GUI)
3786 /* Remove the scrollbars. They may be added back later. */
3787 if (gui.in_use)
3788 gui_remove_scrollbars();
3789#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003790 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003791 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003792 tp->tp_firstwin = firstwin;
3793 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003794 tp->tp_old_Rows = Rows;
3795 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003796 firstwin = NULL;
3797 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003798 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003799}
3800
3801/*
3802 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003803 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003804 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3805 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003806 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003807 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003808enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003809 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003810 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003811 int trigger_enter_autocmds UNUSED;
3812 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003813{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003814 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003815 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003816
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003817 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003818 firstwin = tp->tp_firstwin;
3819 lastwin = tp->tp_lastwin;
3820 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003821
3822 /* We would like doing the TabEnter event first, but we don't have a
3823 * valid current window yet, which may break some commands.
3824 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003825 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3826 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003827 prevwin = next_prevwin;
3828
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003829 last_status(FALSE); /* status line may appear or disappear */
3830 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003831 must_redraw = CLEAR; /* need to redraw everything */
3832#ifdef FEAT_DIFF
3833 diff_need_scrollbind = TRUE;
3834#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003835
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003836 /* The tabpage line may have appeared or disappeared, may need to resize
3837 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003838 * frame sizes too. Use the stored value of p_ch, so that it can be
3839 * different for each tab page. */
3840 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003841 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3842#ifdef FEAT_GUI_TABLINE
3843 && !gui_use_tabline()
3844#endif
3845 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003846 shell_new_rows();
3847#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003848 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003849 shell_new_columns(); /* update window widths */
3850#endif
3851
3852#if defined(FEAT_GUI)
3853 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3854 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003855 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003856#endif
3857
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003858#ifdef FEAT_AUTOCMD
3859 /* Apply autocommands after updating the display, when 'rows' and
3860 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003861 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003862 {
3863 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3864 if (old_curbuf != curbuf)
3865 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3866 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003867#endif
3868
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003869 redraw_all_later(CLEAR);
3870}
3871
3872/*
3873 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003874 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003875 */
3876 void
3877goto_tabpage(n)
3878 int n;
3879{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003880 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003881 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003882 int i;
3883
Bram Moolenaard68071d2006-05-02 22:08:30 +00003884 if (text_locked())
3885 {
3886 /* Not allowed when editing the command line. */
3887#ifdef FEAT_CMDWIN
3888 if (cmdwin_type != 0)
3889 EMSG(_(e_cmdwin));
3890 else
3891#endif
3892 EMSG(_(e_secure));
3893 return;
3894 }
3895
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003896 /* If there is only one it can't work. */
3897 if (first_tabpage->tp_next == NULL)
3898 {
3899 if (n > 1)
3900 beep_flush();
3901 return;
3902 }
3903
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003904 if (n == 0)
3905 {
3906 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003907 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003908 tp = first_tabpage;
3909 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003910 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003911 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003912 else if (n < 0)
3913 {
3914 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3915 * this N times. */
3916 ttp = curtab;
3917 for (i = n; i < 0; ++i)
3918 {
3919 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3920 tp = tp->tp_next)
3921 ;
3922 ttp = tp;
3923 }
3924 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003925 else if (n == 9999)
3926 {
3927 /* Go to last tab page. */
3928 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3929 ;
3930 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003931 else
3932 {
3933 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003934 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003935 if (tp == NULL)
3936 {
3937 beep_flush();
3938 return;
3939 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003940 }
3941
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003942 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003943
3944#ifdef FEAT_GUI_TABLINE
3945 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003946 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003947#endif
3948}
3949
3950/*
3951 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003952 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3953 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003954 * Note: doesn't update the GUI tab.
3955 */
3956 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003957goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003958 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003959 int trigger_enter_autocmds;
3960 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003961{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003962 /* Don't repeat a message in another tab page. */
3963 set_keep_msg(NULL, 0);
3964
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003965 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3966 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003967 {
3968 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003969 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3970 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003971 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003972 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3973 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003974 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003975}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003978 * Enter window "wp" in tab page "tp".
3979 * Also updates the GUI tab.
3980 */
3981 void
3982goto_tabpage_win(tp, wp)
3983 tabpage_T *tp;
3984 win_T *wp;
3985{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003986 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003987 if (curtab == tp && win_valid(wp))
3988 {
3989 win_enter(wp, TRUE);
3990# ifdef FEAT_GUI_TABLINE
3991 if (gui_use_tabline())
3992 gui_mch_set_curtab(tabpage_index(curtab));
3993# endif
3994 }
3995}
3996
3997/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003998 * Move the current tab page to before tab page "nr".
3999 */
4000 void
4001tabpage_move(nr)
4002 int nr;
4003{
4004 int n = nr;
4005 tabpage_T *tp;
4006
4007 if (first_tabpage->tp_next == NULL)
4008 return;
4009
4010 /* Remove the current tab page from the list of tab pages. */
4011 if (curtab == first_tabpage)
4012 first_tabpage = curtab->tp_next;
4013 else
4014 {
4015 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4016 if (tp->tp_next == curtab)
4017 break;
4018 if (tp == NULL) /* "cannot happen" */
4019 return;
4020 tp->tp_next = curtab->tp_next;
4021 }
4022
4023 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02004024 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004025 {
4026 curtab->tp_next = first_tabpage;
4027 first_tabpage = curtab;
4028 }
4029 else
4030 {
4031 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
4032 --n;
4033 curtab->tp_next = tp->tp_next;
4034 tp->tp_next = curtab;
4035 }
4036
4037 /* Need to redraw the tabline. Tab page contents doesn't change. */
4038 redraw_tabline = TRUE;
4039}
4040
4041
4042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 * Go to another window.
4044 * When jumping to another buffer, stop Visual mode. Do this before
4045 * changing windows so we can yank the selection into the '*' register.
4046 * When jumping to another window on the same buffer, adjust its cursor
4047 * position to keep the same Visual area.
4048 */
4049 void
4050win_goto(wp)
4051 win_T *wp;
4052{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004053#ifdef FEAT_CONCEAL
4054 win_T *owp = curwin;
4055#endif
4056
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004057 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
4059 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004060 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return;
4062 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004063#ifdef FEAT_AUTOCMD
4064 if (curbuf_locked())
4065 return;
4066#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004067
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (wp->w_buffer != curbuf)
4069 reset_VIsual_and_resel();
4070 else if (VIsual_active)
4071 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073#ifdef FEAT_GUI
4074 need_mouse_correct = TRUE;
4075#endif
4076 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004077
4078#ifdef FEAT_CONCEAL
4079 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004080 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004081 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004082 if (curwin->w_p_cole > 0 && !msg_scrolled)
4083 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085}
4086
4087#if defined(FEAT_PERL) || defined(PROTO)
4088/*
4089 * Find window number "winnr" (counting top to bottom).
4090 */
4091 win_T *
4092win_find_nr(winnr)
4093 int winnr;
4094{
4095 win_T *wp;
4096
4097# ifdef FEAT_WINDOWS
4098 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4099 if (--winnr == 0)
4100 break;
4101 return wp;
4102# else
4103 return curwin;
4104# endif
4105}
4106#endif
4107
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004108#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4109 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004110/*
4111 * Find the tabpage for window "win".
4112 */
4113 tabpage_T *
4114win_find_tabpage(win)
4115 win_T *win;
4116{
4117 win_T *wp;
4118 tabpage_T *tp;
4119
4120 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004121 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4122 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004123 if (wp == win)
4124 return tp;
4125 return NULL;
4126}
4127#endif
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129#ifdef FEAT_VERTSPLIT
4130/*
4131 * Move to window above or below "count" times.
4132 */
4133 static void
4134win_goto_ver(up, count)
4135 int up; /* TRUE to go to win above */
4136 long count;
4137{
4138 frame_T *fr;
4139 frame_T *nfr;
4140 frame_T *foundfr;
4141
4142 foundfr = curwin->w_frame;
4143 while (count--)
4144 {
4145 /*
4146 * First go upwards in the tree of frames until we find a upwards or
4147 * downwards neighbor.
4148 */
4149 fr = foundfr;
4150 for (;;)
4151 {
4152 if (fr == topframe)
4153 goto end;
4154 if (up)
4155 nfr = fr->fr_prev;
4156 else
4157 nfr = fr->fr_next;
4158 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4159 break;
4160 fr = fr->fr_parent;
4161 }
4162
4163 /*
4164 * Now go downwards to find the bottom or top frame in it.
4165 */
4166 for (;;)
4167 {
4168 if (nfr->fr_layout == FR_LEAF)
4169 {
4170 foundfr = nfr;
4171 break;
4172 }
4173 fr = nfr->fr_child;
4174 if (nfr->fr_layout == FR_ROW)
4175 {
4176 /* Find the frame at the cursor row. */
4177 while (fr->fr_next != NULL
4178 && frame2win(fr)->w_wincol + fr->fr_width
4179 <= curwin->w_wincol + curwin->w_wcol)
4180 fr = fr->fr_next;
4181 }
4182 if (nfr->fr_layout == FR_COL && up)
4183 while (fr->fr_next != NULL)
4184 fr = fr->fr_next;
4185 nfr = fr;
4186 }
4187 }
4188end:
4189 if (foundfr != NULL)
4190 win_goto(foundfr->fr_win);
4191}
4192
4193/*
4194 * Move to left or right window.
4195 */
4196 static void
4197win_goto_hor(left, count)
4198 int left; /* TRUE to go to left win */
4199 long count;
4200{
4201 frame_T *fr;
4202 frame_T *nfr;
4203 frame_T *foundfr;
4204
4205 foundfr = curwin->w_frame;
4206 while (count--)
4207 {
4208 /*
4209 * First go upwards in the tree of frames until we find a left or
4210 * right neighbor.
4211 */
4212 fr = foundfr;
4213 for (;;)
4214 {
4215 if (fr == topframe)
4216 goto end;
4217 if (left)
4218 nfr = fr->fr_prev;
4219 else
4220 nfr = fr->fr_next;
4221 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4222 break;
4223 fr = fr->fr_parent;
4224 }
4225
4226 /*
4227 * Now go downwards to find the leftmost or rightmost frame in it.
4228 */
4229 for (;;)
4230 {
4231 if (nfr->fr_layout == FR_LEAF)
4232 {
4233 foundfr = nfr;
4234 break;
4235 }
4236 fr = nfr->fr_child;
4237 if (nfr->fr_layout == FR_COL)
4238 {
4239 /* Find the frame at the cursor row. */
4240 while (fr->fr_next != NULL
4241 && frame2win(fr)->w_winrow + fr->fr_height
4242 <= curwin->w_winrow + curwin->w_wrow)
4243 fr = fr->fr_next;
4244 }
4245 if (nfr->fr_layout == FR_ROW && left)
4246 while (fr->fr_next != NULL)
4247 fr = fr->fr_next;
4248 nfr = fr;
4249 }
4250 }
4251end:
4252 if (foundfr != NULL)
4253 win_goto(foundfr->fr_win);
4254}
4255#endif
4256
4257/*
4258 * Make window "wp" the current window.
4259 */
4260 void
4261win_enter(wp, undo_sync)
4262 win_T *wp;
4263 int undo_sync;
4264{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004265 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266}
4267
4268/*
4269 * Make window wp the current window.
4270 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4271 * been closed and isn't valid.
4272 */
4273 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004274win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 win_T *wp;
4276 int undo_sync;
4277 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004278 int trigger_enter_autocmds UNUSED;
4279 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280{
4281#ifdef FEAT_AUTOCMD
4282 int other_buffer = FALSE;
4283#endif
4284
4285 if (wp == curwin && !curwin_invalid) /* nothing to do */
4286 return;
4287
4288#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004289 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
4291 /*
4292 * Be careful: If autocommands delete the window, return now.
4293 */
4294 if (wp->w_buffer != curbuf)
4295 {
4296 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4297 other_buffer = TRUE;
4298 if (!win_valid(wp))
4299 return;
4300 }
4301 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4302 if (!win_valid(wp))
4303 return;
4304# ifdef FEAT_EVAL
4305 /* autocmds may abort script processing */
4306 if (aborting())
4307 return;
4308# endif
4309 }
4310#endif
4311
4312 /* sync undo before leaving the current buffer */
4313 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004314 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004315
4316 /* Might need to scroll the old window before switching, e.g., when the
4317 * cursor was moved. */
4318 update_topline();
4319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 /* may have to copy the buffer options when 'cpo' contains 'S' */
4321 if (wp->w_buffer != curbuf)
4322 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4323 if (!curwin_invalid)
4324 {
4325 prevwin = curwin; /* remember for CTRL-W p */
4326 curwin->w_redr_status = TRUE;
4327 }
4328 curwin = wp;
4329 curbuf = wp->w_buffer;
4330 check_cursor();
4331#ifdef FEAT_VIRTUALEDIT
4332 if (!virtual_active())
4333 curwin->w_cursor.coladd = 0;
4334#endif
4335 changed_line_abv_curs(); /* assume cursor position needs updating */
4336
4337 if (curwin->w_localdir != NULL)
4338 {
4339 /* Window has a local directory: Save current directory as global
4340 * directory (unless that was done already) and change to the local
4341 * directory. */
4342 if (globaldir == NULL)
4343 {
4344 char_u cwd[MAXPATHL];
4345
4346 if (mch_dirname(cwd, MAXPATHL) == OK)
4347 globaldir = vim_strsave(cwd);
4348 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004349 if (mch_chdir((char *)curwin->w_localdir) == 0)
4350 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 else if (globaldir != NULL)
4353 {
4354 /* Window doesn't have a local directory and we are not in the global
4355 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004356 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 vim_free(globaldir);
4358 globaldir = NULL;
4359 shorten_fnames(TRUE);
4360 }
4361
4362#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004363 if (trigger_enter_autocmds)
4364 {
4365 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4366 if (other_buffer)
4367 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369#endif
4370
4371#ifdef FEAT_TITLE
4372 maketitle();
4373#endif
4374 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004375 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 if (restart_edit)
4377 redraw_later(VALID); /* causes status line redraw */
4378
4379 /* set window height to desired minimal value */
4380 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4381 win_setheight((int)p_wh);
4382 else if (curwin->w_height == 0)
4383 win_setheight(1);
4384
4385#ifdef FEAT_VERTSPLIT
4386 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004387 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 win_setwidth((int)p_wiw);
4389#endif
4390
4391#ifdef FEAT_MOUSE
4392 setmouse(); /* in case jumped to/from help buffer */
4393#endif
4394
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004395 /* Change directories when the 'acd' option is set. */
4396 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397}
4398
4399#endif /* FEAT_WINDOWS */
4400
4401#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4402/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004403 * Jump to the first open window that contains buffer "buf", if one exists.
4404 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 */
4406 win_T *
4407buf_jump_open_win(buf)
4408 buf_T *buf;
4409{
4410# ifdef FEAT_WINDOWS
4411 win_T *wp;
4412
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004413 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if (wp->w_buffer == buf)
4415 break;
4416 if (wp != NULL)
4417 win_enter(wp, FALSE);
4418 return wp;
4419# else
4420 if (curwin->w_buffer == buf)
4421 return curwin;
4422 return NULL;
4423# endif
4424}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004425
4426/*
4427 * Jump to the first open window in any tab page that contains buffer "buf",
4428 * if one exists.
4429 * Returns a pointer to the window found, otherwise NULL.
4430 */
4431 win_T *
4432buf_jump_open_tab(buf)
4433 buf_T *buf;
4434{
4435# ifdef FEAT_WINDOWS
4436 win_T *wp;
4437 tabpage_T *tp;
4438
4439 /* First try the current tab page. */
4440 wp = buf_jump_open_win(buf);
4441 if (wp != NULL)
4442 return wp;
4443
4444 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4445 if (tp != curtab)
4446 {
4447 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4448 if (wp->w_buffer == buf)
4449 break;
4450 if (wp != NULL)
4451 {
4452 goto_tabpage_win(tp, wp);
4453 if (curwin != wp)
4454 wp = NULL; /* something went wrong */
4455 break;
4456 }
4457 }
4458
4459 return wp;
4460# else
4461 if (curwin->w_buffer == buf)
4462 return curwin;
4463 return NULL;
4464# endif
4465}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466#endif
4467
4468/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004469 * Allocate a window structure and link it in the window list when "hidden" is
4470 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004473win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004474 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004475 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004477 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 /*
4480 * allocate window structure and linesizes arrays
4481 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004482 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004483 if (new_wp == NULL)
4484 return NULL;
4485
4486 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004488 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004489 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491
Bram Moolenaar429fa852013-04-15 12:27:36 +02004492#ifdef FEAT_EVAL
4493 /* init w: variables */
4494 new_wp->w_vars = dict_alloc();
4495 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004497 win_free_lsize(new_wp);
4498 vim_free(new_wp);
4499 return NULL;
4500 }
4501 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004502#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004503
4504#ifdef FEAT_AUTOCMD
4505 /* Don't execute autocommands while the window is not properly
4506 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4507 * event. */
4508 block_autocmds();
4509#endif
4510 /*
4511 * link the window in the window list
4512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004514 if (!hidden)
4515 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516#endif
4517#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004518 new_wp->w_wincol = 0;
4519 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#endif
4521
Bram Moolenaar429fa852013-04-15 12:27:36 +02004522 /* position the display and the cursor at the top of the file. */
4523 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004525 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004527 new_wp->w_botline = 2;
4528 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004530 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531#endif
4532
Bram Moolenaar429fa852013-04-15 12:27:36 +02004533 /* We won't calculate w_fraction until resizing the window */
4534 new_wp->w_fraction = 0;
4535 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004538 if (gui.in_use)
4539 {
4540 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4541 SBAR_LEFT, new_wp);
4542 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4543 SBAR_RIGHT, new_wp);
4544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545#endif
4546#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004547 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004549#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004550 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004551#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004552#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004553 new_wp->w_match_head = NULL;
4554 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004555#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004556 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557}
4558
4559#if defined(FEAT_WINDOWS) || defined(PROTO)
4560
4561/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004562 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 */
4564 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004565win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004567 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568{
4569 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004570 buf_T *buf;
4571 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004573#ifdef FEAT_FOLDING
4574 clearFolding(wp);
4575#endif
4576
4577 /* reduce the reference count to the argument list. */
4578 alist_unlink(wp->w_alist);
4579
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004580#ifdef FEAT_AUTOCMD
4581 /* Don't execute autocommands while the window is halfway being deleted.
4582 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004583 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004584#endif
4585
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004586#ifdef FEAT_LUA
4587 lua_window_free(wp);
4588#endif
4589
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004590#ifdef FEAT_MZSCHEME
4591 mzscheme_window_free(wp);
4592#endif
4593
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#ifdef FEAT_PERL
4595 perl_win_free(wp);
4596#endif
4597
4598#ifdef FEAT_PYTHON
4599 python_window_free(wp);
4600#endif
4601
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004602#ifdef FEAT_PYTHON3
4603 python3_window_free(wp);
4604#endif
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#ifdef FEAT_TCL
4607 tcl_window_free(wp);
4608#endif
4609
4610#ifdef FEAT_RUBY
4611 ruby_window_free(wp);
4612#endif
4613
4614 clear_winopt(&wp->w_onebuf_opt);
4615 clear_winopt(&wp->w_allbuf_opt);
4616
4617#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004618 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4619 hash_init(&wp->w_vars->dv_hashtab);
4620 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621#endif
4622
4623 if (prevwin == wp)
4624 prevwin = NULL;
4625 win_free_lsize(wp);
4626
4627 for (i = 0; i < wp->w_tagstacklen; ++i)
4628 vim_free(wp->w_tagstack[i].tagname);
4629
4630 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004631
Bram Moolenaarff18df02013-07-24 17:51:57 +02004632 /* Remove the window from the b_wininfo lists, it may happen that the
4633 * freed memory is re-used for another window. */
4634 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4635 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4636 if (wip->wi_win == wp)
4637 wip->wi_win = NULL;
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004640 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#ifdef FEAT_JUMPLIST
4644 free_jumplist(wp);
4645#endif
4646
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004647#ifdef FEAT_QUICKFIX
4648 qf_free_all(wp);
4649#endif
4650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651#ifdef FEAT_GUI
4652 if (gui.in_use)
4653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4655 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4656 }
4657#endif /* FEAT_GUI */
4658
Bram Moolenaar860cae12010-06-05 23:22:07 +02004659#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004660 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004661#endif
4662
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004663#ifdef FEAT_AUTOCMD
4664 if (wp != aucmd_win)
4665#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004666 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004667#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004668 if (autocmd_busy)
4669 {
4670 wp->w_next = au_pending_free_win;
4671 au_pending_free_win = wp;
4672 }
4673 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004674#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004675 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004676
4677#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004678 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680}
4681
4682/*
4683 * Append window "wp" in the window list after window "after".
4684 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004685 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686win_append(after, wp)
4687 win_T *after, *wp;
4688{
4689 win_T *before;
4690
4691 if (after == NULL) /* after NULL is in front of the first */
4692 before = firstwin;
4693 else
4694 before = after->w_next;
4695
4696 wp->w_next = before;
4697 wp->w_prev = after;
4698 if (after == NULL)
4699 firstwin = wp;
4700 else
4701 after->w_next = wp;
4702 if (before == NULL)
4703 lastwin = wp;
4704 else
4705 before->w_prev = wp;
4706}
4707
4708/*
4709 * Remove a window from the window list.
4710 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004711 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004712win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004714 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 if (wp->w_prev != NULL)
4717 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004718 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004720 else
4721 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 if (wp->w_next != NULL)
4723 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004724 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004726 else
4727 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728}
4729
4730/*
4731 * Append frame "frp" in a frame list after frame "after".
4732 */
4733 static void
4734frame_append(after, frp)
4735 frame_T *after, *frp;
4736{
4737 frp->fr_next = after->fr_next;
4738 after->fr_next = frp;
4739 if (frp->fr_next != NULL)
4740 frp->fr_next->fr_prev = frp;
4741 frp->fr_prev = after;
4742}
4743
4744/*
4745 * Insert frame "frp" in a frame list before frame "before".
4746 */
4747 static void
4748frame_insert(before, frp)
4749 frame_T *before, *frp;
4750{
4751 frp->fr_next = before;
4752 frp->fr_prev = before->fr_prev;
4753 before->fr_prev = frp;
4754 if (frp->fr_prev != NULL)
4755 frp->fr_prev->fr_next = frp;
4756 else
4757 frp->fr_parent->fr_child = frp;
4758}
4759
4760/*
4761 * Remove a frame from a frame list.
4762 */
4763 static void
4764frame_remove(frp)
4765 frame_T *frp;
4766{
4767 if (frp->fr_prev != NULL)
4768 frp->fr_prev->fr_next = frp->fr_next;
4769 else
4770 frp->fr_parent->fr_child = frp->fr_next;
4771 if (frp->fr_next != NULL)
4772 frp->fr_next->fr_prev = frp->fr_prev;
4773}
4774
4775#endif /* FEAT_WINDOWS */
4776
4777/*
4778 * Allocate w_lines[] for window "wp".
4779 * Return FAIL for failure, OK for success.
4780 */
4781 int
4782win_alloc_lines(wp)
4783 win_T *wp;
4784{
4785 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004786 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (wp->w_lines == NULL)
4788 return FAIL;
4789 return OK;
4790}
4791
4792/*
4793 * free lsize arrays for a window
4794 */
4795 void
4796win_free_lsize(wp)
4797 win_T *wp;
4798{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004799 /* TODO: why would wp be NULL here? */
4800 if (wp != NULL)
4801 {
4802 vim_free(wp->w_lines);
4803 wp->w_lines = NULL;
4804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805}
4806
4807/*
4808 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004809 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 */
4811 void
4812shell_new_rows()
4813{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004814 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
4816 if (firstwin == NULL) /* not initialized yet */
4817 return;
4818#ifdef FEAT_WINDOWS
4819 if (h < frame_minheight(topframe, NULL))
4820 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004821
4822 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 * that doesn't result in the right height, forget about that option. */
4824 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004825 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 frame_new_height(topframe, h, FALSE, FALSE);
4827
4828 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4829#else
4830 if (h < 1)
4831 h = 1;
4832 win_new_height(firstwin, h);
4833#endif
4834 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004835#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004836 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004837#endif
4838
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#if 0
4840 /* Disabled: don't want making the screen smaller make a window larger. */
4841 if (p_ea)
4842 win_equal(curwin, FALSE, 'v');
4843#endif
4844}
4845
4846#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4847/*
4848 * Called from win_new_shellsize() after Columns changed.
4849 */
4850 void
4851shell_new_columns()
4852{
4853 if (firstwin == NULL) /* not initialized yet */
4854 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004855
4856 /* First try setting the widths of windows with 'winfixwidth'. If that
4857 * doesn't result in the right width, forget about that option. */
4858 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004859 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004860 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4863#if 0
4864 /* Disabled: don't want making the screen smaller make a window larger. */
4865 if (p_ea)
4866 win_equal(curwin, FALSE, 'h');
4867#endif
4868}
4869#endif
4870
4871#if defined(FEAT_CMDWIN) || defined(PROTO)
4872/*
4873 * Save the size of all windows in "gap".
4874 */
4875 void
4876win_size_save(gap)
4877 garray_T *gap;
4878
4879{
4880 win_T *wp;
4881
4882 ga_init2(gap, (int)sizeof(int), 1);
4883 if (ga_grow(gap, win_count() * 2) == OK)
4884 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4885 {
4886 ((int *)gap->ga_data)[gap->ga_len++] =
4887 wp->w_width + wp->w_vsep_width;
4888 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4889 }
4890}
4891
4892/*
4893 * Restore window sizes, but only if the number of windows is still the same.
4894 * Does not free the growarray.
4895 */
4896 void
4897win_size_restore(gap)
4898 garray_T *gap;
4899{
4900 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004901 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
4903 if (win_count() * 2 == gap->ga_len)
4904 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004905 /* The order matters, because frames contain other frames, but it's
4906 * difficult to get right. The easy way out is to do it twice. */
4907 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004909 i = 0;
4910 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4911 {
4912 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4913 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 /* recompute the window positions */
4917 (void)win_comp_pos();
4918 }
4919}
4920#endif /* FEAT_CMDWIN */
4921
4922#if defined(FEAT_WINDOWS) || defined(PROTO)
4923/*
4924 * Update the position for all windows, using the width and height of the
4925 * frames.
4926 * Returns the row just after the last window.
4927 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004928 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929win_comp_pos()
4930{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004931 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 int col = 0;
4933
4934 frame_comp_pos(topframe, &row, &col);
4935 return row;
4936}
4937
4938/*
4939 * Update the position of the windows in frame "topfrp", using the width and
4940 * height of the frames.
4941 * "*row" and "*col" are the top-left position of the frame. They are updated
4942 * to the bottom-right position plus one.
4943 */
4944 static void
4945frame_comp_pos(topfrp, row, col)
4946 frame_T *topfrp;
4947 int *row;
4948 int *col;
4949{
4950 win_T *wp;
4951 frame_T *frp;
4952#ifdef FEAT_VERTSPLIT
4953 int startcol;
4954 int startrow;
4955#endif
4956
4957 wp = topfrp->fr_win;
4958 if (wp != NULL)
4959 {
4960 if (wp->w_winrow != *row
4961#ifdef FEAT_VERTSPLIT
4962 || wp->w_wincol != *col
4963#endif
4964 )
4965 {
4966 /* position changed, redraw */
4967 wp->w_winrow = *row;
4968#ifdef FEAT_VERTSPLIT
4969 wp->w_wincol = *col;
4970#endif
4971 redraw_win_later(wp, NOT_VALID);
4972 wp->w_redr_status = TRUE;
4973 }
4974 *row += wp->w_height + wp->w_status_height;
4975#ifdef FEAT_VERTSPLIT
4976 *col += wp->w_width + wp->w_vsep_width;
4977#endif
4978 }
4979 else
4980 {
4981#ifdef FEAT_VERTSPLIT
4982 startrow = *row;
4983 startcol = *col;
4984#endif
4985 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4986 {
4987#ifdef FEAT_VERTSPLIT
4988 if (topfrp->fr_layout == FR_ROW)
4989 *row = startrow; /* all frames are at the same row */
4990 else
4991 *col = startcol; /* all frames are at the same col */
4992#endif
4993 frame_comp_pos(frp, row, col);
4994 }
4995 }
4996}
4997
4998#endif /* FEAT_WINDOWS */
4999
5000/*
5001 * Set current window height and take care of repositioning other windows to
5002 * fit around it.
5003 */
5004 void
5005win_setheight(height)
5006 int height;
5007{
5008 win_setheight_win(height, curwin);
5009}
5010
5011/*
5012 * Set the window height of window "win" and take care of repositioning other
5013 * windows to fit around it.
5014 */
5015 void
5016win_setheight_win(height, win)
5017 int height;
5018 win_T *win;
5019{
5020 int row;
5021
5022 if (win == curwin)
5023 {
5024 /* Always keep current window at least one line high, even when
5025 * 'winminheight' is zero. */
5026#ifdef FEAT_WINDOWS
5027 if (height < p_wmh)
5028 height = p_wmh;
5029#endif
5030 if (height == 0)
5031 height = 1;
5032 }
5033
5034#ifdef FEAT_WINDOWS
5035 frame_setheight(win->w_frame, height + win->w_status_height);
5036
5037 /* recompute the window positions */
5038 row = win_comp_pos();
5039#else
5040 if (height > topframe->fr_height)
5041 height = topframe->fr_height;
5042 win->w_height = height;
5043 row = height;
5044#endif
5045
5046 /*
5047 * If there is extra space created between the last window and the command
5048 * line, clear it.
5049 */
5050 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5051 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5052 cmdline_row = row;
5053 msg_row = row;
5054 msg_col = 0;
5055
5056 redraw_all_later(NOT_VALID);
5057}
5058
5059#if defined(FEAT_WINDOWS) || defined(PROTO)
5060
5061/*
5062 * Set the height of a frame to "height" and take care that all frames and
5063 * windows inside it are resized. Also resize frames on the left and right if
5064 * the are in the same FR_ROW frame.
5065 *
5066 * Strategy:
5067 * If the frame is part of a FR_COL frame, try fitting the frame in that
5068 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5069 * go to containing frames to resize them and make room.
5070 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5071 * Check for the minimal height of the FR_ROW frame.
5072 * At the top level we can also use change the command line height.
5073 */
5074 static void
5075frame_setheight(curfrp, height)
5076 frame_T *curfrp;
5077 int height;
5078{
5079 int room; /* total number of lines available */
5080 int take; /* number of lines taken from other windows */
5081 int room_cmdline; /* lines available from cmdline */
5082 int run;
5083 frame_T *frp;
5084 int h;
5085 int room_reserved;
5086
5087 /* If the height already is the desired value, nothing to do. */
5088 if (curfrp->fr_height == height)
5089 return;
5090
5091 if (curfrp->fr_parent == NULL)
5092 {
5093 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005094 if (height > ROWS_AVAIL)
5095 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 if (height > 0)
5097 frame_new_height(curfrp, height, FALSE, FALSE);
5098 }
5099 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5100 {
5101 /* Row of frames: Also need to resize frames left and right of this
5102 * one. First check for the minimal height of these. */
5103 h = frame_minheight(curfrp->fr_parent, NULL);
5104 if (height < h)
5105 height = h;
5106 frame_setheight(curfrp->fr_parent, height);
5107 }
5108 else
5109 {
5110 /*
5111 * Column of frames: try to change only frames in this column.
5112 */
5113#ifdef FEAT_VERTSPLIT
5114 /*
5115 * Do this twice:
5116 * 1: compute room available, if it's not enough try resizing the
5117 * containing frame.
5118 * 2: compute the room available and adjust the height to it.
5119 * Try not to reduce the height of a window with 'winfixheight' set.
5120 */
5121 for (run = 1; run <= 2; ++run)
5122#else
5123 for (;;)
5124#endif
5125 {
5126 room = 0;
5127 room_reserved = 0;
5128 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5129 frp = frp->fr_next)
5130 {
5131 if (frp != curfrp
5132 && frp->fr_win != NULL
5133 && frp->fr_win->w_p_wfh)
5134 room_reserved += frp->fr_height;
5135 room += frp->fr_height;
5136 if (frp != curfrp)
5137 room -= frame_minheight(frp, NULL);
5138 }
5139#ifdef FEAT_VERTSPLIT
5140 if (curfrp->fr_width != Columns)
5141 room_cmdline = 0;
5142 else
5143#endif
5144 {
5145 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5146 + lastwin->w_height + lastwin->w_status_height);
5147 if (room_cmdline < 0)
5148 room_cmdline = 0;
5149 }
5150
5151 if (height <= room + room_cmdline)
5152 break;
5153#ifdef FEAT_VERTSPLIT
5154 if (run == 2 || curfrp->fr_width == Columns)
5155#endif
5156 {
5157 if (height > room + room_cmdline)
5158 height = room + room_cmdline;
5159 break;
5160 }
5161#ifdef FEAT_VERTSPLIT
5162 frame_setheight(curfrp->fr_parent, height
5163 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5164#endif
5165 /*NOTREACHED*/
5166 }
5167
5168 /*
5169 * Compute the number of lines we will take from others frames (can be
5170 * negative!).
5171 */
5172 take = height - curfrp->fr_height;
5173
5174 /* If there is not enough room, also reduce the height of a window
5175 * with 'winfixheight' set. */
5176 if (height > room + room_cmdline - room_reserved)
5177 room_reserved = room + room_cmdline - height;
5178 /* If there is only a 'winfixheight' window and making the
5179 * window smaller, need to make the other window taller. */
5180 if (take < 0 && room - curfrp->fr_height < room_reserved)
5181 room_reserved = 0;
5182
5183 if (take > 0 && room_cmdline > 0)
5184 {
5185 /* use lines from cmdline first */
5186 if (take < room_cmdline)
5187 room_cmdline = take;
5188 take -= room_cmdline;
5189 topframe->fr_height += room_cmdline;
5190 }
5191
5192 /*
5193 * set the current frame to the new height
5194 */
5195 frame_new_height(curfrp, height, FALSE, FALSE);
5196
5197 /*
5198 * First take lines from the frames after the current frame. If
5199 * that is not enough, takes lines from frames above the current
5200 * frame.
5201 */
5202 for (run = 0; run < 2; ++run)
5203 {
5204 if (run == 0)
5205 frp = curfrp->fr_next; /* 1st run: start with next window */
5206 else
5207 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5208 while (frp != NULL && take != 0)
5209 {
5210 h = frame_minheight(frp, NULL);
5211 if (room_reserved > 0
5212 && frp->fr_win != NULL
5213 && frp->fr_win->w_p_wfh)
5214 {
5215 if (room_reserved >= frp->fr_height)
5216 room_reserved -= frp->fr_height;
5217 else
5218 {
5219 if (frp->fr_height - room_reserved > take)
5220 room_reserved = frp->fr_height - take;
5221 take -= frp->fr_height - room_reserved;
5222 frame_new_height(frp, room_reserved, FALSE, FALSE);
5223 room_reserved = 0;
5224 }
5225 }
5226 else
5227 {
5228 if (frp->fr_height - take < h)
5229 {
5230 take -= frp->fr_height - h;
5231 frame_new_height(frp, h, FALSE, FALSE);
5232 }
5233 else
5234 {
5235 frame_new_height(frp, frp->fr_height - take,
5236 FALSE, FALSE);
5237 take = 0;
5238 }
5239 }
5240 if (run == 0)
5241 frp = frp->fr_next;
5242 else
5243 frp = frp->fr_prev;
5244 }
5245 }
5246 }
5247}
5248
5249#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5250/*
5251 * Set current window width and take care of repositioning other windows to
5252 * fit around it.
5253 */
5254 void
5255win_setwidth(width)
5256 int width;
5257{
5258 win_setwidth_win(width, curwin);
5259}
5260
5261 void
5262win_setwidth_win(width, wp)
5263 int width;
5264 win_T *wp;
5265{
5266 /* Always keep current window at least one column wide, even when
5267 * 'winminwidth' is zero. */
5268 if (wp == curwin)
5269 {
5270 if (width < p_wmw)
5271 width = p_wmw;
5272 if (width == 0)
5273 width = 1;
5274 }
5275
5276 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5277
5278 /* recompute the window positions */
5279 (void)win_comp_pos();
5280
5281 redraw_all_later(NOT_VALID);
5282}
5283
5284/*
5285 * Set the width of a frame to "width" and take care that all frames and
5286 * windows inside it are resized. Also resize frames above and below if the
5287 * are in the same FR_ROW frame.
5288 *
5289 * Strategy is similar to frame_setheight().
5290 */
5291 static void
5292frame_setwidth(curfrp, width)
5293 frame_T *curfrp;
5294 int width;
5295{
5296 int room; /* total number of lines available */
5297 int take; /* number of lines taken from other windows */
5298 int run;
5299 frame_T *frp;
5300 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005301 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303 /* If the width already is the desired value, nothing to do. */
5304 if (curfrp->fr_width == width)
5305 return;
5306
5307 if (curfrp->fr_parent == NULL)
5308 /* topframe: can't change width */
5309 return;
5310
5311 if (curfrp->fr_parent->fr_layout == FR_COL)
5312 {
5313 /* Column of frames: Also need to resize frames above and below of
5314 * this one. First check for the minimal width of these. */
5315 w = frame_minwidth(curfrp->fr_parent, NULL);
5316 if (width < w)
5317 width = w;
5318 frame_setwidth(curfrp->fr_parent, width);
5319 }
5320 else
5321 {
5322 /*
5323 * Row of frames: try to change only frames in this row.
5324 *
5325 * Do this twice:
5326 * 1: compute room available, if it's not enough try resizing the
5327 * containing frame.
5328 * 2: compute the room available and adjust the width to it.
5329 */
5330 for (run = 1; run <= 2; ++run)
5331 {
5332 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005333 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5335 frp = frp->fr_next)
5336 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005337 if (frp != curfrp
5338 && frp->fr_win != NULL
5339 && frp->fr_win->w_p_wfw)
5340 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 room += frp->fr_width;
5342 if (frp != curfrp)
5343 room -= frame_minwidth(frp, NULL);
5344 }
5345
5346 if (width <= room)
5347 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005348 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
5350 if (width > room)
5351 width = room;
5352 break;
5353 }
5354 frame_setwidth(curfrp->fr_parent, width
5355 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5356 }
5357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 /*
5359 * Compute the number of lines we will take from others frames (can be
5360 * negative!).
5361 */
5362 take = width - curfrp->fr_width;
5363
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005364 /* If there is not enough room, also reduce the width of a window
5365 * with 'winfixwidth' set. */
5366 if (width > room - room_reserved)
5367 room_reserved = room - width;
5368 /* If there is only a 'winfixwidth' window and making the
5369 * window smaller, need to make the other window narrower. */
5370 if (take < 0 && room - curfrp->fr_width < room_reserved)
5371 room_reserved = 0;
5372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 /*
5374 * set the current frame to the new width
5375 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005376 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
5378 /*
5379 * First take lines from the frames right of the current frame. If
5380 * that is not enough, takes lines from frames left of the current
5381 * frame.
5382 */
5383 for (run = 0; run < 2; ++run)
5384 {
5385 if (run == 0)
5386 frp = curfrp->fr_next; /* 1st run: start with next window */
5387 else
5388 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5389 while (frp != NULL && take != 0)
5390 {
5391 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005392 if (room_reserved > 0
5393 && frp->fr_win != NULL
5394 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005396 if (room_reserved >= frp->fr_width)
5397 room_reserved -= frp->fr_width;
5398 else
5399 {
5400 if (frp->fr_width - room_reserved > take)
5401 room_reserved = frp->fr_width - take;
5402 take -= frp->fr_width - room_reserved;
5403 frame_new_width(frp, room_reserved, FALSE, FALSE);
5404 room_reserved = 0;
5405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407 else
5408 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005409 if (frp->fr_width - take < w)
5410 {
5411 take -= frp->fr_width - w;
5412 frame_new_width(frp, w, FALSE, FALSE);
5413 }
5414 else
5415 {
5416 frame_new_width(frp, frp->fr_width - take,
5417 FALSE, FALSE);
5418 take = 0;
5419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
5421 if (run == 0)
5422 frp = frp->fr_next;
5423 else
5424 frp = frp->fr_prev;
5425 }
5426 }
5427 }
5428}
5429#endif /* FEAT_VERTSPLIT */
5430
5431/*
5432 * Check 'winminheight' for a valid value.
5433 */
5434 void
5435win_setminheight()
5436{
5437 int room;
5438 int first = TRUE;
5439 win_T *wp;
5440
5441 /* loop until there is a 'winminheight' that is possible */
5442 while (p_wmh > 0)
5443 {
5444 /* TODO: handle vertical splits */
5445 room = -p_wh;
5446 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5447 room += wp->w_height - p_wmh;
5448 if (room >= 0)
5449 break;
5450 --p_wmh;
5451 if (first)
5452 {
5453 EMSG(_(e_noroom));
5454 first = FALSE;
5455 }
5456 }
5457}
5458
5459#ifdef FEAT_MOUSE
5460
5461/*
5462 * Status line of dragwin is dragged "offset" lines down (negative is up).
5463 */
5464 void
5465win_drag_status_line(dragwin, offset)
5466 win_T *dragwin;
5467 int offset;
5468{
5469 frame_T *curfr;
5470 frame_T *fr;
5471 int room;
5472 int row;
5473 int up; /* if TRUE, drag status line up, otherwise down */
5474 int n;
5475
5476 fr = dragwin->w_frame;
5477 curfr = fr;
5478 if (fr != topframe) /* more than one window */
5479 {
5480 fr = fr->fr_parent;
5481 /* When the parent frame is not a column of frames, its parent should
5482 * be. */
5483 if (fr->fr_layout != FR_COL)
5484 {
5485 curfr = fr;
5486 if (fr != topframe) /* only a row of windows, may drag statusline */
5487 fr = fr->fr_parent;
5488 }
5489 }
5490
5491 /* If this is the last frame in a column, may want to resize the parent
5492 * frame instead (go two up to skip a row of frames). */
5493 while (curfr != topframe && curfr->fr_next == NULL)
5494 {
5495 if (fr != topframe)
5496 fr = fr->fr_parent;
5497 curfr = fr;
5498 if (fr != topframe)
5499 fr = fr->fr_parent;
5500 }
5501
5502 if (offset < 0) /* drag up */
5503 {
5504 up = TRUE;
5505 offset = -offset;
5506 /* sum up the room of the current frame and above it */
5507 if (fr == curfr)
5508 {
5509 /* only one window */
5510 room = fr->fr_height - frame_minheight(fr, NULL);
5511 }
5512 else
5513 {
5514 room = 0;
5515 for (fr = fr->fr_child; ; fr = fr->fr_next)
5516 {
5517 room += fr->fr_height - frame_minheight(fr, NULL);
5518 if (fr == curfr)
5519 break;
5520 }
5521 }
5522 fr = curfr->fr_next; /* put fr at frame that grows */
5523 }
5524 else /* drag down */
5525 {
5526 up = FALSE;
5527 /*
5528 * Only dragging the last status line can reduce p_ch.
5529 */
5530 room = Rows - cmdline_row;
5531 if (curfr->fr_next == NULL)
5532 room -= 1;
5533 else
5534 room -= p_ch;
5535 if (room < 0)
5536 room = 0;
5537 /* sum up the room of frames below of the current one */
5538 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5539 room += fr->fr_height - frame_minheight(fr, NULL);
5540 fr = curfr; /* put fr at window that grows */
5541 }
5542
5543 if (room < offset) /* Not enough room */
5544 offset = room; /* Move as far as we can */
5545 if (offset <= 0)
5546 return;
5547
5548 /*
5549 * Grow frame fr by "offset" lines.
5550 * Doesn't happen when dragging the last status line up.
5551 */
5552 if (fr != NULL)
5553 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5554
5555 if (up)
5556 fr = curfr; /* current frame gets smaller */
5557 else
5558 fr = curfr->fr_next; /* next frame gets smaller */
5559
5560 /*
5561 * Now make the other frames smaller.
5562 */
5563 while (fr != NULL && offset > 0)
5564 {
5565 n = frame_minheight(fr, NULL);
5566 if (fr->fr_height - offset <= n)
5567 {
5568 offset -= fr->fr_height - n;
5569 frame_new_height(fr, n, !up, FALSE);
5570 }
5571 else
5572 {
5573 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5574 break;
5575 }
5576 if (up)
5577 fr = fr->fr_prev;
5578 else
5579 fr = fr->fr_next;
5580 }
5581 row = win_comp_pos();
5582 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5583 cmdline_row = row;
5584 p_ch = Rows - cmdline_row;
5585 if (p_ch < 1)
5586 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005587 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005588 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 showmode();
5590}
5591
5592#ifdef FEAT_VERTSPLIT
5593/*
5594 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5595 */
5596 void
5597win_drag_vsep_line(dragwin, offset)
5598 win_T *dragwin;
5599 int offset;
5600{
5601 frame_T *curfr;
5602 frame_T *fr;
5603 int room;
5604 int left; /* if TRUE, drag separator line left, otherwise right */
5605 int n;
5606
5607 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005608 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 return;
5610 curfr = fr;
5611 fr = fr->fr_parent;
5612 /* When the parent frame is not a row of frames, its parent should be. */
5613 if (fr->fr_layout != FR_ROW)
5614 {
5615 if (fr == topframe) /* only a column of windows (cannot happen?) */
5616 return;
5617 curfr = fr;
5618 fr = fr->fr_parent;
5619 }
5620
5621 /* If this is the last frame in a row, may want to resize a parent
5622 * frame instead. */
5623 while (curfr->fr_next == NULL)
5624 {
5625 if (fr == topframe)
5626 break;
5627 curfr = fr;
5628 fr = fr->fr_parent;
5629 if (fr != topframe)
5630 {
5631 curfr = fr;
5632 fr = fr->fr_parent;
5633 }
5634 }
5635
5636 if (offset < 0) /* drag left */
5637 {
5638 left = TRUE;
5639 offset = -offset;
5640 /* sum up the room of the current frame and left of it */
5641 room = 0;
5642 for (fr = fr->fr_child; ; fr = fr->fr_next)
5643 {
5644 room += fr->fr_width - frame_minwidth(fr, NULL);
5645 if (fr == curfr)
5646 break;
5647 }
5648 fr = curfr->fr_next; /* put fr at frame that grows */
5649 }
5650 else /* drag right */
5651 {
5652 left = FALSE;
5653 /* sum up the room of frames right of the current one */
5654 room = 0;
5655 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5656 room += fr->fr_width - frame_minwidth(fr, NULL);
5657 fr = curfr; /* put fr at window that grows */
5658 }
5659
5660 if (room < offset) /* Not enough room */
5661 offset = room; /* Move as far as we can */
5662 if (offset <= 0) /* No room at all, quit. */
5663 return;
5664
5665 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005666 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 /* shrink other frames: current and at the left or at the right */
5669 if (left)
5670 fr = curfr; /* current frame gets smaller */
5671 else
5672 fr = curfr->fr_next; /* next frame gets smaller */
5673
5674 while (fr != NULL && offset > 0)
5675 {
5676 n = frame_minwidth(fr, NULL);
5677 if (fr->fr_width - offset <= n)
5678 {
5679 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005680 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 else
5683 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005684 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 break;
5686 }
5687 if (left)
5688 fr = fr->fr_prev;
5689 else
5690 fr = fr->fr_next;
5691 }
5692 (void)win_comp_pos();
5693 redraw_all_later(NOT_VALID);
5694}
5695#endif /* FEAT_VERTSPLIT */
5696#endif /* FEAT_MOUSE */
5697
5698#endif /* FEAT_WINDOWS */
5699
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005700#define FRACTION_MULT 16384L
5701
5702/*
5703 * Set wp->w_fraction for the current w_wrow and w_height.
5704 */
5705 static void
5706set_fraction(wp)
5707 win_T *wp;
5708{
5709 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005710 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005711}
5712
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713/*
5714 * Set the height of a window.
5715 * This takes care of the things inside the window, not what happens to the
5716 * window position, the frame or to other windows.
5717 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005718 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719win_new_height(wp, height)
5720 win_T *wp;
5721 int height;
5722{
5723 linenr_T lnum;
5724 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005725 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726
5727 /* Don't want a negative height. Happens when splitting a tiny window.
5728 * Will equalize heights soon to fix it. */
5729 if (height < 0)
5730 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005731 if (wp->w_height == height)
5732 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005734 if (wp->w_height > 0)
5735 {
5736 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005737 /* w_wrow needs to be valid. When setting 'laststatus' this may
5738 * call win_new_height() recursively. */
5739 validate_cursor();
5740 if (wp->w_height != prev_height)
5741 return; /* Recursive call already changed the size, bail out here
5742 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005743 if (wp->w_wrow != wp->w_prev_fraction_row)
5744 set_fraction(wp);
5745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
5747 wp->w_height = height;
5748 wp->w_skipcol = 0;
5749
5750 /* Don't change w_topline when height is zero. Don't set w_topline when
5751 * 'scrollbind' is set and this isn't the current window. */
5752 if (height > 0
5753#ifdef FEAT_SCROLLBIND
5754 && (!wp->w_p_scb || wp == curwin)
5755#endif
5756 )
5757 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005758 /*
5759 * Find a value for w_topline that shows the cursor at the same
5760 * relative position in the window as before (more or less).
5761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 lnum = wp->w_cursor.lnum;
5763 if (lnum < 1) /* can happen when starting up */
5764 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005765 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5766 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5768 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005769
5770 if (sline >= 0)
5771 {
5772 /* Make sure the whole cursor line is visible, if possible. */
5773 int rows = plines_win(wp, lnum, FALSE);
5774
5775 if (sline > wp->w_height - rows)
5776 {
5777 sline = wp->w_height - rows;
5778 wp->w_wrow -= rows - line_size;
5779 }
5780 }
5781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 if (sline < 0)
5783 {
5784 /*
5785 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005786 * Make cursor line the first line in the window. If not enough
5787 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 */
5789 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005790 if (wp->w_wrow >= wp->w_height
5791 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5792 {
5793 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5794 --wp->w_wrow;
5795 while (wp->w_wrow >= wp->w_height)
5796 {
5797 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5798 + win_col_off2(wp);
5799 --wp->w_wrow;
5800 }
5801 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005802 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005804 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005806 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
5808#ifdef FEAT_FOLDING
5809 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5810 if (lnum == 1)
5811 {
5812 /* first line in buffer is folded */
5813 line_size = 1;
5814 --sline;
5815 break;
5816 }
5817#endif
5818 --lnum;
5819#ifdef FEAT_DIFF
5820 if (lnum == wp->w_topline)
5821 line_size = plines_win_nofill(wp, lnum, TRUE)
5822 + wp->w_topfill;
5823 else
5824#endif
5825 line_size = plines_win(wp, lnum, TRUE);
5826 sline -= line_size;
5827 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005828
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 if (sline < 0)
5830 {
5831 /*
5832 * Line we want at top would go off top of screen. Use next
5833 * line instead.
5834 */
5835#ifdef FEAT_FOLDING
5836 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5837#endif
5838 lnum++;
5839 wp->w_wrow -= line_size + sline;
5840 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005841 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
5843 /* First line of file reached, use that as topline. */
5844 lnum = 1;
5845 wp->w_wrow -= sline;
5846 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005847
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005848 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
5851
5852 if (wp == curwin)
5853 {
5854 if (p_so)
5855 update_topline();
5856 curs_columns(FALSE); /* validate w_wrow */
5857 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005858 if (prev_height > 0)
5859 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
5861 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005862 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863#ifdef FEAT_WINDOWS
5864 wp->w_redr_status = TRUE;
5865#endif
5866 invalidate_botline_win(wp);
5867}
5868
5869#ifdef FEAT_VERTSPLIT
5870/*
5871 * Set the width of a window.
5872 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005873 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874win_new_width(wp, width)
5875 win_T *wp;
5876 int width;
5877{
5878 wp->w_width = width;
5879 wp->w_lines_valid = 0;
5880 changed_line_abv_curs_win(wp);
5881 invalidate_botline_win(wp);
5882 if (wp == curwin)
5883 {
5884 update_topline();
5885 curs_columns(TRUE); /* validate w_wrow */
5886 }
5887 redraw_win_later(wp, NOT_VALID);
5888 wp->w_redr_status = TRUE;
5889}
5890#endif
5891
5892 void
5893win_comp_scroll(wp)
5894 win_T *wp;
5895{
5896 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5897 if (wp->w_p_scr == 0)
5898 wp->w_p_scr = 1;
5899}
5900
5901/*
5902 * command_height: called whenever p_ch has been changed
5903 */
5904 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005905command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907#ifdef FEAT_WINDOWS
5908 int h;
5909 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005910 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005912 /* Use the value of p_ch that we remembered. This is needed for when the
5913 * GUI starts up, we can't be sure in what order things happen. And when
5914 * p_ch was changed in another tab page. */
5915 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005916
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 /* Find bottom frame with width of screen. */
5918 frp = lastwin->w_frame;
5919# ifdef FEAT_VERTSPLIT
5920 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5921 frp = frp->fr_parent;
5922# endif
5923
5924 /* Avoid changing the height of a window with 'winfixheight' set. */
5925 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5926 && frp->fr_win->w_p_wfh)
5927 frp = frp->fr_prev;
5928
5929 if (starting != NO_SCREEN)
5930 {
5931 cmdline_row = Rows - p_ch;
5932
5933 if (p_ch > old_p_ch) /* p_ch got bigger */
5934 {
5935 while (p_ch > old_p_ch)
5936 {
5937 if (frp == NULL)
5938 {
5939 EMSG(_(e_noroom));
5940 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005941 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 cmdline_row = Rows - p_ch;
5943 break;
5944 }
5945 h = frp->fr_height - frame_minheight(frp, NULL);
5946 if (h > p_ch - old_p_ch)
5947 h = p_ch - old_p_ch;
5948 old_p_ch += h;
5949 frame_add_height(frp, -h);
5950 frp = frp->fr_prev;
5951 }
5952
5953 /* Recompute window positions. */
5954 (void)win_comp_pos();
5955
5956 /* clear the lines added to cmdline */
5957 if (full_screen)
5958 screen_fill((int)(cmdline_row), (int)Rows, 0,
5959 (int)Columns, ' ', ' ', 0);
5960 msg_row = cmdline_row;
5961 redraw_cmdline = TRUE;
5962 return;
5963 }
5964
5965 if (msg_row < cmdline_row)
5966 msg_row = cmdline_row;
5967 redraw_cmdline = TRUE;
5968 }
5969 frame_add_height(frp, (int)(old_p_ch - p_ch));
5970
5971 /* Recompute window positions. */
5972 if (frp != lastwin->w_frame)
5973 (void)win_comp_pos();
5974#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005976 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977#endif
5978}
5979
5980#if defined(FEAT_WINDOWS) || defined(PROTO)
5981/*
5982 * Resize frame "frp" to be "n" lines higher (negative for less high).
5983 * Also resize the frames it is contained in.
5984 */
5985 static void
5986frame_add_height(frp, n)
5987 frame_T *frp;
5988 int n;
5989{
5990 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5991 for (;;)
5992 {
5993 frp = frp->fr_parent;
5994 if (frp == NULL)
5995 break;
5996 frp->fr_height += n;
5997 }
5998}
5999
6000/*
6001 * Add or remove a status line for the bottom window(s), according to the
6002 * value of 'laststatus'.
6003 */
6004 void
6005last_status(morewin)
6006 int morewin; /* pretend there are two or more windows */
6007{
6008 /* Don't make a difference between horizontal or vertical split. */
6009 last_status_rec(topframe, (p_ls == 2
6010 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6011}
6012
6013 static void
6014last_status_rec(fr, statusline)
6015 frame_T *fr;
6016 int statusline;
6017{
6018 frame_T *fp;
6019 win_T *wp;
6020
6021 if (fr->fr_layout == FR_LEAF)
6022 {
6023 wp = fr->fr_win;
6024 if (wp->w_status_height != 0 && !statusline)
6025 {
6026 /* remove status line */
6027 win_new_height(wp, wp->w_height + 1);
6028 wp->w_status_height = 0;
6029 comp_col();
6030 }
6031 else if (wp->w_status_height == 0 && statusline)
6032 {
6033 /* Find a frame to take a line from. */
6034 fp = fr;
6035 while (fp->fr_height <= frame_minheight(fp, NULL))
6036 {
6037 if (fp == topframe)
6038 {
6039 EMSG(_(e_noroom));
6040 return;
6041 }
6042 /* In a column of frames: go to frame above. If already at
6043 * the top or in a row of frames: go to parent. */
6044 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6045 fp = fp->fr_prev;
6046 else
6047 fp = fp->fr_parent;
6048 }
6049 wp->w_status_height = 1;
6050 if (fp != fr)
6051 {
6052 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6053 frame_fix_height(wp);
6054 (void)win_comp_pos();
6055 }
6056 else
6057 win_new_height(wp, wp->w_height - 1);
6058 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006059 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
6061 }
6062#ifdef FEAT_VERTSPLIT
6063 else if (fr->fr_layout == FR_ROW)
6064 {
6065 /* vertically split windows, set status line for each one */
6066 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6067 last_status_rec(fp, statusline);
6068 }
6069#endif
6070 else
6071 {
6072 /* horizontally split window, set status line for last one */
6073 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6074 ;
6075 last_status_rec(fp, statusline);
6076 }
6077}
6078
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006079/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006080 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006081 */
6082 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006083tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006084{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006085#ifdef FEAT_GUI_TABLINE
6086 /* When the GUI has the tabline then this always returns zero. */
6087 if (gui_use_tabline())
6088 return 0;
6089#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006090 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006091 {
6092 case 0: return 0;
6093 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6094 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006095 return 1;
6096}
6097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098#endif /* FEAT_WINDOWS */
6099
6100#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6101/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006102 * Get the file name at the cursor.
6103 * If Visual mode is active, use the selected text if it's in one line.
6104 * Returns the name in allocated memory, NULL for failure.
6105 */
6106 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006107grab_file_name(count, file_lnum)
6108 long count;
6109 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006110{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006111 if (VIsual_active)
6112 {
6113 int len;
6114 char_u *ptr;
6115
6116 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6117 return NULL;
6118 return find_file_name_in_path(ptr, len,
6119 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6120 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006121 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6122 file_lnum);
6123
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006124}
6125
6126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 * Return the file name under or after the cursor.
6128 *
6129 * The 'path' option is searched if the file name is not absolute.
6130 * The string returned has been alloc'ed and should be freed by the caller.
6131 * NULL is returned if the file name or file is not found.
6132 *
6133 * options:
6134 * FNAME_MESS give error messages
6135 * FNAME_EXP expand to path
6136 * FNAME_HYP check for hypertext link
6137 * FNAME_INCL apply "includeexpr"
6138 */
6139 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006140file_name_at_cursor(options, count, file_lnum)
6141 int options;
6142 long count;
6143 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006146 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6147 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148}
6149
6150/*
6151 * Return the name of the file under or after ptr[col].
6152 * Otherwise like file_name_at_cursor().
6153 */
6154 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006155file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 char_u *line;
6157 int col;
6158 int options;
6159 long count;
6160 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006161 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
6163 char_u *ptr;
6164 int len;
6165
6166 /*
6167 * search forward for what could be the start of a file name
6168 */
6169 ptr = line + col;
6170 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006171 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (*ptr == NUL) /* nothing found */
6173 {
6174 if (options & FNAME_MESS)
6175 EMSG(_("E446: No file name under cursor"));
6176 return NULL;
6177 }
6178
6179 /*
6180 * Search backward for first char of the file name.
6181 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6182 */
6183 while (ptr > line)
6184 {
6185#ifdef FEAT_MBYTE
6186 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6187 ptr -= len + 1;
6188 else
6189#endif
6190 if (vim_isfilec(ptr[-1])
6191 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6192 --ptr;
6193 else
6194 break;
6195 }
6196
6197 /*
6198 * Search forward for the last char of the file name.
6199 * Also allow "://" when ':' is not in 'isfname'.
6200 */
6201 len = 0;
6202 while (vim_isfilec(ptr[len])
6203 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6204#ifdef FEAT_MBYTE
6205 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006206 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 else
6208#endif
6209 ++len;
6210
6211 /*
6212 * If there is trailing punctuation, remove it.
6213 * But don't remove "..", could be a directory name.
6214 */
6215 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6216 && ptr[len - 2] != '.')
6217 --len;
6218
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006219 if (file_lnum != NULL)
6220 {
6221 char_u *p;
6222
6223 /* Get the number after the file name and a separator character */
6224 p = ptr + len;
6225 p = skipwhite(p);
6226 if (*p != NUL)
6227 {
6228 if (!isdigit(*p))
6229 ++p; /* skip the separator */
6230 p = skipwhite(p);
6231 if (isdigit(*p))
6232 *file_lnum = (int)getdigits(&p);
6233 }
6234 }
6235
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6237}
6238
6239# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6240static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6241
6242 static char_u *
6243eval_includeexpr(ptr, len)
6244 char_u *ptr;
6245 int len;
6246{
6247 char_u *res;
6248
6249 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006250 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006251 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 set_vim_var_string(VV_FNAME, NULL, 0);
6253 return res;
6254}
6255#endif
6256
6257/*
6258 * Return the name of the file ptr[len] in 'path'.
6259 * Otherwise like file_name_at_cursor().
6260 */
6261 char_u *
6262find_file_name_in_path(ptr, len, options, count, rel_fname)
6263 char_u *ptr;
6264 int len;
6265 int options;
6266 long count;
6267 char_u *rel_fname; /* file we are searching relative to */
6268{
6269 char_u *file_name;
6270 int c;
6271# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6272 char_u *tofree = NULL;
6273
6274 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6275 {
6276 tofree = eval_includeexpr(ptr, len);
6277 if (tofree != NULL)
6278 {
6279 ptr = tofree;
6280 len = (int)STRLEN(ptr);
6281 }
6282 }
6283# endif
6284
6285 if (options & FNAME_EXP)
6286 {
6287 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6288 TRUE, rel_fname);
6289
6290# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6291 /*
6292 * If the file could not be found in a normal way, try applying
6293 * 'includeexpr' (unless done already).
6294 */
6295 if (file_name == NULL
6296 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6297 {
6298 tofree = eval_includeexpr(ptr, len);
6299 if (tofree != NULL)
6300 {
6301 ptr = tofree;
6302 len = (int)STRLEN(ptr);
6303 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6304 TRUE, rel_fname);
6305 }
6306 }
6307# endif
6308 if (file_name == NULL && (options & FNAME_MESS))
6309 {
6310 c = ptr[len];
6311 ptr[len] = NUL;
6312 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6313 ptr[len] = c;
6314 }
6315
6316 /* Repeat finding the file "count" times. This matters when it
6317 * appears several times in the path. */
6318 while (file_name != NULL && --count > 0)
6319 {
6320 vim_free(file_name);
6321 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6322 }
6323 }
6324 else
6325 file_name = vim_strnsave(ptr, len);
6326
6327# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6328 vim_free(tofree);
6329# endif
6330
6331 return file_name;
6332}
6333#endif /* FEAT_SEARCHPATH */
6334
6335/*
6336 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6337 * Also check for ":\\", which MS Internet Explorer accepts, return
6338 * URL_BACKSLASH.
6339 */
6340 static int
6341path_is_url(p)
6342 char_u *p;
6343{
6344 if (STRNCMP(p, "://", (size_t)3) == 0)
6345 return URL_SLASH;
6346 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6347 return URL_BACKSLASH;
6348 return 0;
6349}
6350
6351/*
6352 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6353 * Return URL_BACKSLASH for "name:\\".
6354 * Return zero otherwise.
6355 */
6356 int
6357path_with_url(fname)
6358 char_u *fname;
6359{
6360 char_u *p;
6361
6362 for (p = fname; isalpha(*p); ++p)
6363 ;
6364 return path_is_url(p);
6365}
6366
6367/*
6368 * Return TRUE if "name" is a full (absolute) path name or URL.
6369 */
6370 int
6371vim_isAbsName(name)
6372 char_u *name;
6373{
6374 return (path_with_url(name) != 0 || mch_isFullName(name));
6375}
6376
6377/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006378 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 *
6380 * return FAIL for failure, OK otherwise
6381 */
6382 int
6383vim_FullName(fname, buf, len, force)
6384 char_u *fname, *buf;
6385 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006386 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 int retval = OK;
6389 int url;
6390
6391 *buf = NUL;
6392 if (fname == NULL)
6393 return FAIL;
6394
6395 url = path_with_url(fname);
6396 if (!url)
6397 retval = mch_FullName(fname, buf, len, force);
6398 if (url || retval == FAIL)
6399 {
6400 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006401 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 }
6403#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6404 slash_adjust(buf);
6405#endif
6406 return retval;
6407}
6408
6409/*
6410 * Return the minimal number of rows that is needed on the screen to display
6411 * the current number of windows.
6412 */
6413 int
6414min_rows()
6415{
6416 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006417#ifdef FEAT_WINDOWS
6418 tabpage_T *tp;
6419 int n;
6420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 if (firstwin == NULL) /* not initialized yet */
6423 return MIN_LINES;
6424
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006426 total = 0;
6427 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6428 {
6429 n = frame_minheight(tp->tp_topframe, NULL);
6430 if (total < n)
6431 total = n;
6432 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006433 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006435 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006437 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 return total;
6439}
6440
6441/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006442 * Return TRUE if there is only one window (in the current tab page), not
6443 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006444 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 */
6446 int
6447only_one_window()
6448{
6449#ifdef FEAT_WINDOWS
6450 int count = 0;
6451 win_T *wp;
6452
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453 /* If there is another tab page there always is another window. */
6454 if (first_tabpage->tp_next != NULL)
6455 return FALSE;
6456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006458 if (wp->w_buffer != NULL
6459 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460# ifdef FEAT_QUICKFIX
6461 || wp->w_p_pvw
6462# endif
6463 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006464# ifdef FEAT_AUTOCMD
6465 && wp != aucmd_win
6466# endif
6467 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 ++count;
6469 return (count <= 1);
6470#else
6471 return TRUE;
6472#endif
6473}
6474
6475#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6476/*
6477 * Correct the cursor line number in other windows. Used after changing the
6478 * current buffer, and before applying autocommands.
6479 * When "do_curwin" is TRUE, also check current window.
6480 */
6481 void
6482check_lnums(do_curwin)
6483 int do_curwin;
6484{
6485 win_T *wp;
6486
6487#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006488 tabpage_T *tp;
6489
6490 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6492#else
6493 wp = curwin;
6494 if (do_curwin)
6495#endif
6496 {
6497 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6498 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6499 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6500 wp->w_topline = curbuf->b_ml.ml_line_count;
6501 }
6502}
6503#endif
6504
6505#if defined(FEAT_WINDOWS) || defined(PROTO)
6506
6507/*
6508 * A snapshot of the window sizes, to restore them after closing the help
6509 * window.
6510 * Only these fields are used:
6511 * fr_layout
6512 * fr_width
6513 * fr_height
6514 * fr_next
6515 * fr_child
6516 * fr_win (only valid for the old curwin, NULL otherwise)
6517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
6519/*
6520 * Create a snapshot of the current frame sizes.
6521 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006522 void
6523make_snapshot(idx)
6524 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006526 clear_snapshot(curtab, idx);
6527 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528}
6529
6530 static void
6531make_snapshot_rec(fr, frp)
6532 frame_T *fr;
6533 frame_T **frp;
6534{
6535 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6536 if (*frp == NULL)
6537 return;
6538 (*frp)->fr_layout = fr->fr_layout;
6539# ifdef FEAT_VERTSPLIT
6540 (*frp)->fr_width = fr->fr_width;
6541# endif
6542 (*frp)->fr_height = fr->fr_height;
6543 if (fr->fr_next != NULL)
6544 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6545 if (fr->fr_child != NULL)
6546 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6547 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6548 (*frp)->fr_win = curwin;
6549}
6550
6551/*
6552 * Remove any existing snapshot.
6553 */
6554 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006555clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006556 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006557 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006559 clear_snapshot_rec(tp->tp_snapshot[idx]);
6560 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561}
6562
6563 static void
6564clear_snapshot_rec(fr)
6565 frame_T *fr;
6566{
6567 if (fr != NULL)
6568 {
6569 clear_snapshot_rec(fr->fr_next);
6570 clear_snapshot_rec(fr->fr_child);
6571 vim_free(fr);
6572 }
6573}
6574
6575/*
6576 * Restore a previously created snapshot, if there is any.
6577 * This is only done if the screen size didn't change and the window layout is
6578 * still the same.
6579 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006580 void
6581restore_snapshot(idx, close_curwin)
6582 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 int close_curwin; /* closing current window */
6584{
6585 win_T *wp;
6586
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006587 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006589 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006591 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6592 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006594 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 win_comp_pos();
6596 if (wp != NULL && close_curwin)
6597 win_goto(wp);
6598 redraw_all_later(CLEAR);
6599 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006600 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601}
6602
6603/*
6604 * Check if frames "sn" and "fr" have the same layout, same following frames
6605 * and same children.
6606 */
6607 static int
6608check_snapshot_rec(sn, fr)
6609 frame_T *sn;
6610 frame_T *fr;
6611{
6612 if (sn->fr_layout != fr->fr_layout
6613 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6614 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6615 || (sn->fr_next != NULL
6616 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6617 || (sn->fr_child != NULL
6618 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6619 return FAIL;
6620 return OK;
6621}
6622
6623/*
6624 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6625 * following frames and children.
6626 * Returns a pointer to the old current window, or NULL.
6627 */
6628 static win_T *
6629restore_snapshot_rec(sn, fr)
6630 frame_T *sn;
6631 frame_T *fr;
6632{
6633 win_T *wp = NULL;
6634 win_T *wp2;
6635
6636 fr->fr_height = sn->fr_height;
6637# ifdef FEAT_VERTSPLIT
6638 fr->fr_width = sn->fr_width;
6639# endif
6640 if (fr->fr_layout == FR_LEAF)
6641 {
6642 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6643# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006644 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645# endif
6646 wp = sn->fr_win;
6647 }
6648 if (sn->fr_next != NULL)
6649 {
6650 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6651 if (wp2 != NULL)
6652 wp = wp2;
6653 }
6654 if (sn->fr_child != NULL)
6655 {
6656 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6657 if (wp2 != NULL)
6658 wp = wp2;
6659 }
6660 return wp;
6661}
6662
6663#endif
6664
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006665#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6666 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006667/*
6668 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006669 * restore_win() MUST be called to undo, also when FAIL is returned.
6670 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006671 * When "no_display" is TRUE the display won't be affected, no redraw is
6672 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006673 * Returns FAIL if switching to "win" failed.
6674 */
6675 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006676switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006677 win_T **save_curwin UNUSED;
6678 tabpage_T **save_curtab UNUSED;
6679 win_T *win UNUSED;
6680 tabpage_T *tp UNUSED;
6681 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006682{
6683# ifdef FEAT_AUTOCMD
6684 block_autocmds();
6685# endif
6686# ifdef FEAT_WINDOWS
6687 *save_curwin = curwin;
6688 if (tp != NULL)
6689 {
6690 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006691 if (no_display)
6692 {
6693 curtab->tp_firstwin = firstwin;
6694 curtab->tp_lastwin = lastwin;
6695 curtab = tp;
6696 firstwin = curtab->tp_firstwin;
6697 lastwin = curtab->tp_lastwin;
6698 }
6699 else
6700 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006701 }
6702 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006703 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006704 curwin = win;
6705 curbuf = curwin->w_buffer;
6706# endif
6707 return OK;
6708}
6709
6710/*
6711 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006712 * When "no_display" is TRUE the display won't be affected, no redraw is
6713 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006714 */
6715 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006716restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006717 win_T *save_curwin UNUSED;
6718 tabpage_T *save_curtab UNUSED;
6719 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006720{
6721# ifdef FEAT_WINDOWS
6722 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006723 {
6724 if (no_display)
6725 {
6726 curtab->tp_firstwin = firstwin;
6727 curtab->tp_lastwin = lastwin;
6728 curtab = save_curtab;
6729 firstwin = curtab->tp_firstwin;
6730 lastwin = curtab->tp_lastwin;
6731 }
6732 else
6733 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6734 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006735 if (win_valid(save_curwin))
6736 {
6737 curwin = save_curwin;
6738 curbuf = curwin->w_buffer;
6739 }
6740# endif
6741# ifdef FEAT_AUTOCMD
6742 unblock_autocmds();
6743# endif
6744}
6745
6746/*
6747 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6748 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6749 */
6750 void
6751switch_buffer(save_curbuf, buf)
6752 buf_T *buf;
6753 buf_T **save_curbuf;
6754{
6755# ifdef FEAT_AUTOCMD
6756 block_autocmds();
6757# endif
6758 *save_curbuf = curbuf;
6759 --curbuf->b_nwindows;
6760 curbuf = buf;
6761 curwin->w_buffer = buf;
6762 ++curbuf->b_nwindows;
6763}
6764
6765/*
6766 * Restore the current buffer after using switch_buffer().
6767 */
6768 void
6769restore_buffer(save_curbuf)
6770 buf_T *save_curbuf;
6771{
6772# ifdef FEAT_AUTOCMD
6773 unblock_autocmds();
6774# endif
6775 /* Check for valid buffer, just in case. */
6776 if (buf_valid(save_curbuf))
6777 {
6778 --curbuf->b_nwindows;
6779 curwin->w_buffer = save_curbuf;
6780 curbuf = save_curbuf;
6781 ++curbuf->b_nwindows;
6782 }
6783}
6784#endif
6785
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6787/*
6788 * Return TRUE if there is any vertically split window.
6789 */
6790 int
6791win_hasvertsplit()
6792{
6793 frame_T *fr;
6794
6795 if (topframe->fr_layout == FR_ROW)
6796 return TRUE;
6797
6798 if (topframe->fr_layout == FR_COL)
6799 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6800 if (fr->fr_layout == FR_ROW)
6801 return TRUE;
6802
6803 return FALSE;
6804}
6805#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006806
6807#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6808/*
6809 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006810 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006811 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6812 * If no particular ID is desired, -1 must be specified for 'id'.
6813 * Return ID of added match, -1 on failure.
6814 */
6815 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006816match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006817 win_T *wp;
6818 char_u *grp;
6819 char_u *pat;
6820 int prio;
6821 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006822 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006823{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006824 matchitem_T *cur;
6825 matchitem_T *prev;
6826 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006827 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006828 regprog_T *regprog = NULL;
6829 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006830
Bram Moolenaarb3414592014-06-17 17:48:32 +02006831 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006832 return -1;
6833 if (id < -1 || id == 0)
6834 {
6835 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6836 return -1;
6837 }
6838 if (id != -1)
6839 {
6840 cur = wp->w_match_head;
6841 while (cur != NULL)
6842 {
6843 if (cur->id == id)
6844 {
6845 EMSGN("E801: ID already taken: %ld", id);
6846 return -1;
6847 }
6848 cur = cur->next;
6849 }
6850 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006851 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006852 {
6853 EMSG2(_(e_nogroup), grp);
6854 return -1;
6855 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006856 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006857 {
6858 EMSG2(_(e_invarg2), pat);
6859 return -1;
6860 }
6861
6862 /* Find available match ID. */
6863 while (id == -1)
6864 {
6865 cur = wp->w_match_head;
6866 while (cur != NULL && cur->id != wp->w_next_match_id)
6867 cur = cur->next;
6868 if (cur == NULL)
6869 id = wp->w_next_match_id;
6870 wp->w_next_match_id++;
6871 }
6872
6873 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006874 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006875 m->id = id;
6876 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006877 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006878 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006879 m->match.regprog = regprog;
6880 m->match.rmm_ic = FALSE;
6881 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006882
Bram Moolenaarb3414592014-06-17 17:48:32 +02006883 /* Set up position matches */
6884 if (pos_list != NULL)
6885 {
6886 linenr_T toplnum = 0;
6887 linenr_T botlnum = 0;
6888 listitem_T *li;
6889 int i;
6890
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006891 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006892 i++, li = li->li_next)
6893 {
6894 linenr_T lnum = 0;
6895 colnr_T col = 0;
6896 int len = 1;
6897 list_T *subl;
6898 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006899 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006900
Bram Moolenaarb3414592014-06-17 17:48:32 +02006901 if (li->li_tv.v_type == VAR_LIST)
6902 {
6903 subl = li->li_tv.vval.v_list;
6904 if (subl == NULL)
6905 goto fail;
6906 subli = subl->lv_first;
6907 if (subli == NULL)
6908 goto fail;
6909 lnum = get_tv_number_chk(&subli->li_tv, &error);
6910 if (error == TRUE)
6911 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006912 if (lnum == 0)
6913 {
6914 --i;
6915 continue;
6916 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006917 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006918 subli = subli->li_next;
6919 if (subli != NULL)
6920 {
6921 col = get_tv_number_chk(&subli->li_tv, &error);
6922 if (error == TRUE)
6923 goto fail;
6924 subli = subli->li_next;
6925 if (subli != NULL)
6926 {
6927 len = get_tv_number_chk(&subli->li_tv, &error);
6928 if (error == TRUE)
6929 goto fail;
6930 }
6931 }
6932 m->pos.pos[i].col = col;
6933 m->pos.pos[i].len = len;
6934 }
6935 else if (li->li_tv.v_type == VAR_NUMBER)
6936 {
6937 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006938 {
6939 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006940 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006941 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006942 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6943 m->pos.pos[i].col = 0;
6944 m->pos.pos[i].len = 0;
6945 }
6946 else
6947 {
6948 EMSG(_("List or number required"));
6949 goto fail;
6950 }
6951 if (toplnum == 0 || lnum < toplnum)
6952 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006953 if (botlnum == 0 || lnum >= botlnum)
6954 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006955 }
6956
6957 /* Calculate top and bottom lines for redrawing area */
6958 if (toplnum != 0)
6959 {
6960 if (wp->w_buffer->b_mod_set)
6961 {
6962 if (wp->w_buffer->b_mod_top > toplnum)
6963 wp->w_buffer->b_mod_top = toplnum;
6964 if (wp->w_buffer->b_mod_bot < botlnum)
6965 wp->w_buffer->b_mod_bot = botlnum;
6966 }
6967 else
6968 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006969 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006970 wp->w_buffer->b_mod_top = toplnum;
6971 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006972 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006973 }
6974 m->pos.toplnum = toplnum;
6975 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006976 rtype = VALID;
6977 }
6978 }
6979
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006980 /* Insert new match. The match list is in ascending order with regard to
6981 * the match priorities. */
6982 cur = wp->w_match_head;
6983 prev = cur;
6984 while (cur != NULL && prio >= cur->priority)
6985 {
6986 prev = cur;
6987 cur = cur->next;
6988 }
6989 if (cur == prev)
6990 wp->w_match_head = m;
6991 else
6992 prev->next = m;
6993 m->next = cur;
6994
Bram Moolenaarb3414592014-06-17 17:48:32 +02006995 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006996 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006997
6998fail:
6999 vim_free(m);
7000 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007001}
7002
7003/*
7004 * Delete match with ID 'id' in the match list of window 'wp'.
7005 * Print error messages if 'perr' is TRUE.
7006 */
7007 int
7008match_delete(wp, id, perr)
7009 win_T *wp;
7010 int id;
7011 int perr;
7012{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007013 matchitem_T *cur = wp->w_match_head;
7014 matchitem_T *prev = cur;
7015 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007016
7017 if (id < 1)
7018 {
7019 if (perr == TRUE)
7020 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7021 id);
7022 return -1;
7023 }
7024 while (cur != NULL && cur->id != id)
7025 {
7026 prev = cur;
7027 cur = cur->next;
7028 }
7029 if (cur == NULL)
7030 {
7031 if (perr == TRUE)
7032 EMSGN("E803: ID not found: %ld", id);
7033 return -1;
7034 }
7035 if (cur == prev)
7036 wp->w_match_head = cur->next;
7037 else
7038 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007039 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007040 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007041 if (cur->pos.toplnum != 0)
7042 {
7043 if (wp->w_buffer->b_mod_set)
7044 {
7045 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7046 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7047 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7048 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7049 }
7050 else
7051 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007052 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007053 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7054 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007055 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007056 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007057 rtype = VALID;
7058 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007059 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007060 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007061 return 0;
7062}
7063
7064/*
7065 * Delete all matches in the match list of window 'wp'.
7066 */
7067 void
7068clear_matches(wp)
7069 win_T *wp;
7070{
7071 matchitem_T *m;
7072
7073 while (wp->w_match_head != NULL)
7074 {
7075 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007076 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007077 vim_free(wp->w_match_head->pattern);
7078 vim_free(wp->w_match_head);
7079 wp->w_match_head = m;
7080 }
7081 redraw_later(SOME_VALID);
7082}
7083
7084/*
7085 * Get match from ID 'id' in window 'wp'.
7086 * Return NULL if match not found.
7087 */
7088 matchitem_T *
7089get_match(wp, id)
7090 win_T *wp;
7091 int id;
7092{
7093 matchitem_T *cur = wp->w_match_head;
7094
7095 while (cur != NULL && cur->id != id)
7096 cur = cur->next;
7097 return cur;
7098}
7099#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007100
7101#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7102 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007103get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007104{
7105 int i = 1;
7106 win_T *w;
7107
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007108 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007109 ++i;
7110
7111 if (w == NULL)
7112 return 0;
7113 else
7114 return i;
7115}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007116
7117 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007118get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007119{
7120 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007121# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007122 tabpage_T *t;
7123
7124 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7125 ++i;
7126
7127 if (t == NULL)
7128 return 0;
7129 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007130# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007131 return i;
7132}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007133#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007134
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007135#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007136/*
7137 * Return TRUE if "topfrp" and its children are at the right height.
7138 */
7139 static int
7140frame_check_height(topfrp, height)
7141 frame_T *topfrp;
7142 int height;
7143{
7144 frame_T *frp;
7145
7146 if (topfrp->fr_height != height)
7147 return FALSE;
7148
7149 if (topfrp->fr_layout == FR_ROW)
7150 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7151 if (frp->fr_height != height)
7152 return FALSE;
7153
7154 return TRUE;
7155}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007156#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007157
7158#ifdef FEAT_VERTSPLIT
7159/*
7160 * Return TRUE if "topfrp" and its children are at the right width.
7161 */
7162 static int
7163frame_check_width(topfrp, width)
7164 frame_T *topfrp;
7165 int width;
7166{
7167 frame_T *frp;
7168
7169 if (topfrp->fr_width != width)
7170 return FALSE;
7171
7172 if (topfrp->fr_layout == FR_COL)
7173 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7174 if (frp->fr_width != width)
7175 return FALSE;
7176
7177 return TRUE;
7178}
7179#endif
7180