blob: 6490fd3868f8986f30e589e618f1b994920e2cc0 [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
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100799 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000800 {
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{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004410 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004412 if (curwin->w_buffer == buf)
4413 wp = curwin;
4414# ifdef FEAT_WINDOWS
4415 else
4416 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4417 if (wp->w_buffer == buf)
4418 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (wp != NULL)
4420 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004422 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004424
4425/*
4426 * Jump to the first open window in any tab page that contains buffer "buf",
4427 * if one exists.
4428 * Returns a pointer to the window found, otherwise NULL.
4429 */
4430 win_T *
4431buf_jump_open_tab(buf)
4432 buf_T *buf;
4433{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004434 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004435# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004436 tabpage_T *tp;
4437
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004438 if (wp != NULL)
4439 return wp;
4440
4441 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4442 if (tp != curtab)
4443 {
4444 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4445 if (wp->w_buffer == buf)
4446 break;
4447 if (wp != NULL)
4448 {
4449 goto_tabpage_win(tp, wp);
4450 if (curwin != wp)
4451 wp = NULL; /* something went wrong */
4452 break;
4453 }
4454 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004455# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004456 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004457}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458#endif
4459
4460/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004461 * Allocate a window structure and link it in the window list when "hidden" is
4462 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004465win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004466 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004467 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004469 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /*
4472 * allocate window structure and linesizes arrays
4473 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004474 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004475 if (new_wp == NULL)
4476 return NULL;
4477
4478 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004480 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004481 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483
Bram Moolenaar429fa852013-04-15 12:27:36 +02004484#ifdef FEAT_EVAL
4485 /* init w: variables */
4486 new_wp->w_vars = dict_alloc();
4487 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004489 win_free_lsize(new_wp);
4490 vim_free(new_wp);
4491 return NULL;
4492 }
4493 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004494#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004495
4496#ifdef FEAT_AUTOCMD
4497 /* Don't execute autocommands while the window is not properly
4498 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4499 * event. */
4500 block_autocmds();
4501#endif
4502 /*
4503 * link the window in the window list
4504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004506 if (!hidden)
4507 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508#endif
4509#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004510 new_wp->w_wincol = 0;
4511 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512#endif
4513
Bram Moolenaar429fa852013-04-15 12:27:36 +02004514 /* position the display and the cursor at the top of the file. */
4515 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004517 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004519 new_wp->w_botline = 2;
4520 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004522 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523#endif
4524
Bram Moolenaar429fa852013-04-15 12:27:36 +02004525 /* We won't calculate w_fraction until resizing the window */
4526 new_wp->w_fraction = 0;
4527 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
4529#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004530 if (gui.in_use)
4531 {
4532 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4533 SBAR_LEFT, new_wp);
4534 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4535 SBAR_RIGHT, new_wp);
4536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537#endif
4538#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004539 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004541#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004542 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004543#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004544#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004545 new_wp->w_match_head = NULL;
4546 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004547#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004548 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549}
4550
4551#if defined(FEAT_WINDOWS) || defined(PROTO)
4552
4553/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004554 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 */
4556 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004557win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004559 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560{
4561 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004562 buf_T *buf;
4563 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004565#ifdef FEAT_FOLDING
4566 clearFolding(wp);
4567#endif
4568
4569 /* reduce the reference count to the argument list. */
4570 alist_unlink(wp->w_alist);
4571
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004572#ifdef FEAT_AUTOCMD
4573 /* Don't execute autocommands while the window is halfway being deleted.
4574 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004575 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004576#endif
4577
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004578#ifdef FEAT_LUA
4579 lua_window_free(wp);
4580#endif
4581
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004582#ifdef FEAT_MZSCHEME
4583 mzscheme_window_free(wp);
4584#endif
4585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586#ifdef FEAT_PERL
4587 perl_win_free(wp);
4588#endif
4589
4590#ifdef FEAT_PYTHON
4591 python_window_free(wp);
4592#endif
4593
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004594#ifdef FEAT_PYTHON3
4595 python3_window_free(wp);
4596#endif
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#ifdef FEAT_TCL
4599 tcl_window_free(wp);
4600#endif
4601
4602#ifdef FEAT_RUBY
4603 ruby_window_free(wp);
4604#endif
4605
4606 clear_winopt(&wp->w_onebuf_opt);
4607 clear_winopt(&wp->w_allbuf_opt);
4608
4609#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004610 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4611 hash_init(&wp->w_vars->dv_hashtab);
4612 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#endif
4614
4615 if (prevwin == wp)
4616 prevwin = NULL;
4617 win_free_lsize(wp);
4618
4619 for (i = 0; i < wp->w_tagstacklen; ++i)
4620 vim_free(wp->w_tagstack[i].tagname);
4621
4622 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004623
Bram Moolenaarff18df02013-07-24 17:51:57 +02004624 /* Remove the window from the b_wininfo lists, it may happen that the
4625 * freed memory is re-used for another window. */
4626 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4627 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4628 if (wip->wi_win == wp)
4629 wip->wi_win = NULL;
4630
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004632 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#ifdef FEAT_JUMPLIST
4636 free_jumplist(wp);
4637#endif
4638
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004639#ifdef FEAT_QUICKFIX
4640 qf_free_all(wp);
4641#endif
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#ifdef FEAT_GUI
4644 if (gui.in_use)
4645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4647 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4648 }
4649#endif /* FEAT_GUI */
4650
Bram Moolenaar860cae12010-06-05 23:22:07 +02004651#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004652 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004653#endif
4654
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004655#ifdef FEAT_AUTOCMD
4656 if (wp != aucmd_win)
4657#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004658 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004659#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004660 if (autocmd_busy)
4661 {
4662 wp->w_next = au_pending_free_win;
4663 au_pending_free_win = wp;
4664 }
4665 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004666#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004667 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004668
4669#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004670 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672}
4673
4674/*
4675 * Append window "wp" in the window list after window "after".
4676 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004677 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678win_append(after, wp)
4679 win_T *after, *wp;
4680{
4681 win_T *before;
4682
4683 if (after == NULL) /* after NULL is in front of the first */
4684 before = firstwin;
4685 else
4686 before = after->w_next;
4687
4688 wp->w_next = before;
4689 wp->w_prev = after;
4690 if (after == NULL)
4691 firstwin = wp;
4692 else
4693 after->w_next = wp;
4694 if (before == NULL)
4695 lastwin = wp;
4696 else
4697 before->w_prev = wp;
4698}
4699
4700/*
4701 * Remove a window from the window list.
4702 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004703 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004704win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004706 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707{
4708 if (wp->w_prev != NULL)
4709 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004710 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004712 else
4713 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (wp->w_next != NULL)
4715 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004716 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004718 else
4719 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721
4722/*
4723 * Append frame "frp" in a frame list after frame "after".
4724 */
4725 static void
4726frame_append(after, frp)
4727 frame_T *after, *frp;
4728{
4729 frp->fr_next = after->fr_next;
4730 after->fr_next = frp;
4731 if (frp->fr_next != NULL)
4732 frp->fr_next->fr_prev = frp;
4733 frp->fr_prev = after;
4734}
4735
4736/*
4737 * Insert frame "frp" in a frame list before frame "before".
4738 */
4739 static void
4740frame_insert(before, frp)
4741 frame_T *before, *frp;
4742{
4743 frp->fr_next = before;
4744 frp->fr_prev = before->fr_prev;
4745 before->fr_prev = frp;
4746 if (frp->fr_prev != NULL)
4747 frp->fr_prev->fr_next = frp;
4748 else
4749 frp->fr_parent->fr_child = frp;
4750}
4751
4752/*
4753 * Remove a frame from a frame list.
4754 */
4755 static void
4756frame_remove(frp)
4757 frame_T *frp;
4758{
4759 if (frp->fr_prev != NULL)
4760 frp->fr_prev->fr_next = frp->fr_next;
4761 else
4762 frp->fr_parent->fr_child = frp->fr_next;
4763 if (frp->fr_next != NULL)
4764 frp->fr_next->fr_prev = frp->fr_prev;
4765}
4766
4767#endif /* FEAT_WINDOWS */
4768
4769/*
4770 * Allocate w_lines[] for window "wp".
4771 * Return FAIL for failure, OK for success.
4772 */
4773 int
4774win_alloc_lines(wp)
4775 win_T *wp;
4776{
4777 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004778 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 if (wp->w_lines == NULL)
4780 return FAIL;
4781 return OK;
4782}
4783
4784/*
4785 * free lsize arrays for a window
4786 */
4787 void
4788win_free_lsize(wp)
4789 win_T *wp;
4790{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004791 /* TODO: why would wp be NULL here? */
4792 if (wp != NULL)
4793 {
4794 vim_free(wp->w_lines);
4795 wp->w_lines = NULL;
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797}
4798
4799/*
4800 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004801 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
4803 void
4804shell_new_rows()
4805{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004806 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
4808 if (firstwin == NULL) /* not initialized yet */
4809 return;
4810#ifdef FEAT_WINDOWS
4811 if (h < frame_minheight(topframe, NULL))
4812 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004813
4814 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 * that doesn't result in the right height, forget about that option. */
4816 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004817 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 frame_new_height(topframe, h, FALSE, FALSE);
4819
4820 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4821#else
4822 if (h < 1)
4823 h = 1;
4824 win_new_height(firstwin, h);
4825#endif
4826 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004827#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004828 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004829#endif
4830
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831#if 0
4832 /* Disabled: don't want making the screen smaller make a window larger. */
4833 if (p_ea)
4834 win_equal(curwin, FALSE, 'v');
4835#endif
4836}
4837
4838#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4839/*
4840 * Called from win_new_shellsize() after Columns changed.
4841 */
4842 void
4843shell_new_columns()
4844{
4845 if (firstwin == NULL) /* not initialized yet */
4846 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004847
4848 /* First try setting the widths of windows with 'winfixwidth'. If that
4849 * doesn't result in the right width, forget about that option. */
4850 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004851 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004852 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4855#if 0
4856 /* Disabled: don't want making the screen smaller make a window larger. */
4857 if (p_ea)
4858 win_equal(curwin, FALSE, 'h');
4859#endif
4860}
4861#endif
4862
4863#if defined(FEAT_CMDWIN) || defined(PROTO)
4864/*
4865 * Save the size of all windows in "gap".
4866 */
4867 void
4868win_size_save(gap)
4869 garray_T *gap;
4870
4871{
4872 win_T *wp;
4873
4874 ga_init2(gap, (int)sizeof(int), 1);
4875 if (ga_grow(gap, win_count() * 2) == OK)
4876 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4877 {
4878 ((int *)gap->ga_data)[gap->ga_len++] =
4879 wp->w_width + wp->w_vsep_width;
4880 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4881 }
4882}
4883
4884/*
4885 * Restore window sizes, but only if the number of windows is still the same.
4886 * Does not free the growarray.
4887 */
4888 void
4889win_size_restore(gap)
4890 garray_T *gap;
4891{
4892 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004893 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 if (win_count() * 2 == gap->ga_len)
4896 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004897 /* The order matters, because frames contain other frames, but it's
4898 * difficult to get right. The easy way out is to do it twice. */
4899 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004901 i = 0;
4902 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4903 {
4904 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4905 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 /* recompute the window positions */
4909 (void)win_comp_pos();
4910 }
4911}
4912#endif /* FEAT_CMDWIN */
4913
4914#if defined(FEAT_WINDOWS) || defined(PROTO)
4915/*
4916 * Update the position for all windows, using the width and height of the
4917 * frames.
4918 * Returns the row just after the last window.
4919 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004920 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921win_comp_pos()
4922{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004923 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 int col = 0;
4925
4926 frame_comp_pos(topframe, &row, &col);
4927 return row;
4928}
4929
4930/*
4931 * Update the position of the windows in frame "topfrp", using the width and
4932 * height of the frames.
4933 * "*row" and "*col" are the top-left position of the frame. They are updated
4934 * to the bottom-right position plus one.
4935 */
4936 static void
4937frame_comp_pos(topfrp, row, col)
4938 frame_T *topfrp;
4939 int *row;
4940 int *col;
4941{
4942 win_T *wp;
4943 frame_T *frp;
4944#ifdef FEAT_VERTSPLIT
4945 int startcol;
4946 int startrow;
4947#endif
4948
4949 wp = topfrp->fr_win;
4950 if (wp != NULL)
4951 {
4952 if (wp->w_winrow != *row
4953#ifdef FEAT_VERTSPLIT
4954 || wp->w_wincol != *col
4955#endif
4956 )
4957 {
4958 /* position changed, redraw */
4959 wp->w_winrow = *row;
4960#ifdef FEAT_VERTSPLIT
4961 wp->w_wincol = *col;
4962#endif
4963 redraw_win_later(wp, NOT_VALID);
4964 wp->w_redr_status = TRUE;
4965 }
4966 *row += wp->w_height + wp->w_status_height;
4967#ifdef FEAT_VERTSPLIT
4968 *col += wp->w_width + wp->w_vsep_width;
4969#endif
4970 }
4971 else
4972 {
4973#ifdef FEAT_VERTSPLIT
4974 startrow = *row;
4975 startcol = *col;
4976#endif
4977 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4978 {
4979#ifdef FEAT_VERTSPLIT
4980 if (topfrp->fr_layout == FR_ROW)
4981 *row = startrow; /* all frames are at the same row */
4982 else
4983 *col = startcol; /* all frames are at the same col */
4984#endif
4985 frame_comp_pos(frp, row, col);
4986 }
4987 }
4988}
4989
4990#endif /* FEAT_WINDOWS */
4991
4992/*
4993 * Set current window height and take care of repositioning other windows to
4994 * fit around it.
4995 */
4996 void
4997win_setheight(height)
4998 int height;
4999{
5000 win_setheight_win(height, curwin);
5001}
5002
5003/*
5004 * Set the window height of window "win" and take care of repositioning other
5005 * windows to fit around it.
5006 */
5007 void
5008win_setheight_win(height, win)
5009 int height;
5010 win_T *win;
5011{
5012 int row;
5013
5014 if (win == curwin)
5015 {
5016 /* Always keep current window at least one line high, even when
5017 * 'winminheight' is zero. */
5018#ifdef FEAT_WINDOWS
5019 if (height < p_wmh)
5020 height = p_wmh;
5021#endif
5022 if (height == 0)
5023 height = 1;
5024 }
5025
5026#ifdef FEAT_WINDOWS
5027 frame_setheight(win->w_frame, height + win->w_status_height);
5028
5029 /* recompute the window positions */
5030 row = win_comp_pos();
5031#else
5032 if (height > topframe->fr_height)
5033 height = topframe->fr_height;
5034 win->w_height = height;
5035 row = height;
5036#endif
5037
5038 /*
5039 * If there is extra space created between the last window and the command
5040 * line, clear it.
5041 */
5042 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5043 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5044 cmdline_row = row;
5045 msg_row = row;
5046 msg_col = 0;
5047
5048 redraw_all_later(NOT_VALID);
5049}
5050
5051#if defined(FEAT_WINDOWS) || defined(PROTO)
5052
5053/*
5054 * Set the height of a frame to "height" and take care that all frames and
5055 * windows inside it are resized. Also resize frames on the left and right if
5056 * the are in the same FR_ROW frame.
5057 *
5058 * Strategy:
5059 * If the frame is part of a FR_COL frame, try fitting the frame in that
5060 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5061 * go to containing frames to resize them and make room.
5062 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5063 * Check for the minimal height of the FR_ROW frame.
5064 * At the top level we can also use change the command line height.
5065 */
5066 static void
5067frame_setheight(curfrp, height)
5068 frame_T *curfrp;
5069 int height;
5070{
5071 int room; /* total number of lines available */
5072 int take; /* number of lines taken from other windows */
5073 int room_cmdline; /* lines available from cmdline */
5074 int run;
5075 frame_T *frp;
5076 int h;
5077 int room_reserved;
5078
5079 /* If the height already is the desired value, nothing to do. */
5080 if (curfrp->fr_height == height)
5081 return;
5082
5083 if (curfrp->fr_parent == NULL)
5084 {
5085 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005086 if (height > ROWS_AVAIL)
5087 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 if (height > 0)
5089 frame_new_height(curfrp, height, FALSE, FALSE);
5090 }
5091 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5092 {
5093 /* Row of frames: Also need to resize frames left and right of this
5094 * one. First check for the minimal height of these. */
5095 h = frame_minheight(curfrp->fr_parent, NULL);
5096 if (height < h)
5097 height = h;
5098 frame_setheight(curfrp->fr_parent, height);
5099 }
5100 else
5101 {
5102 /*
5103 * Column of frames: try to change only frames in this column.
5104 */
5105#ifdef FEAT_VERTSPLIT
5106 /*
5107 * Do this twice:
5108 * 1: compute room available, if it's not enough try resizing the
5109 * containing frame.
5110 * 2: compute the room available and adjust the height to it.
5111 * Try not to reduce the height of a window with 'winfixheight' set.
5112 */
5113 for (run = 1; run <= 2; ++run)
5114#else
5115 for (;;)
5116#endif
5117 {
5118 room = 0;
5119 room_reserved = 0;
5120 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5121 frp = frp->fr_next)
5122 {
5123 if (frp != curfrp
5124 && frp->fr_win != NULL
5125 && frp->fr_win->w_p_wfh)
5126 room_reserved += frp->fr_height;
5127 room += frp->fr_height;
5128 if (frp != curfrp)
5129 room -= frame_minheight(frp, NULL);
5130 }
5131#ifdef FEAT_VERTSPLIT
5132 if (curfrp->fr_width != Columns)
5133 room_cmdline = 0;
5134 else
5135#endif
5136 {
5137 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5138 + lastwin->w_height + lastwin->w_status_height);
5139 if (room_cmdline < 0)
5140 room_cmdline = 0;
5141 }
5142
5143 if (height <= room + room_cmdline)
5144 break;
5145#ifdef FEAT_VERTSPLIT
5146 if (run == 2 || curfrp->fr_width == Columns)
5147#endif
5148 {
5149 if (height > room + room_cmdline)
5150 height = room + room_cmdline;
5151 break;
5152 }
5153#ifdef FEAT_VERTSPLIT
5154 frame_setheight(curfrp->fr_parent, height
5155 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5156#endif
5157 /*NOTREACHED*/
5158 }
5159
5160 /*
5161 * Compute the number of lines we will take from others frames (can be
5162 * negative!).
5163 */
5164 take = height - curfrp->fr_height;
5165
5166 /* If there is not enough room, also reduce the height of a window
5167 * with 'winfixheight' set. */
5168 if (height > room + room_cmdline - room_reserved)
5169 room_reserved = room + room_cmdline - height;
5170 /* If there is only a 'winfixheight' window and making the
5171 * window smaller, need to make the other window taller. */
5172 if (take < 0 && room - curfrp->fr_height < room_reserved)
5173 room_reserved = 0;
5174
5175 if (take > 0 && room_cmdline > 0)
5176 {
5177 /* use lines from cmdline first */
5178 if (take < room_cmdline)
5179 room_cmdline = take;
5180 take -= room_cmdline;
5181 topframe->fr_height += room_cmdline;
5182 }
5183
5184 /*
5185 * set the current frame to the new height
5186 */
5187 frame_new_height(curfrp, height, FALSE, FALSE);
5188
5189 /*
5190 * First take lines from the frames after the current frame. If
5191 * that is not enough, takes lines from frames above the current
5192 * frame.
5193 */
5194 for (run = 0; run < 2; ++run)
5195 {
5196 if (run == 0)
5197 frp = curfrp->fr_next; /* 1st run: start with next window */
5198 else
5199 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5200 while (frp != NULL && take != 0)
5201 {
5202 h = frame_minheight(frp, NULL);
5203 if (room_reserved > 0
5204 && frp->fr_win != NULL
5205 && frp->fr_win->w_p_wfh)
5206 {
5207 if (room_reserved >= frp->fr_height)
5208 room_reserved -= frp->fr_height;
5209 else
5210 {
5211 if (frp->fr_height - room_reserved > take)
5212 room_reserved = frp->fr_height - take;
5213 take -= frp->fr_height - room_reserved;
5214 frame_new_height(frp, room_reserved, FALSE, FALSE);
5215 room_reserved = 0;
5216 }
5217 }
5218 else
5219 {
5220 if (frp->fr_height - take < h)
5221 {
5222 take -= frp->fr_height - h;
5223 frame_new_height(frp, h, FALSE, FALSE);
5224 }
5225 else
5226 {
5227 frame_new_height(frp, frp->fr_height - take,
5228 FALSE, FALSE);
5229 take = 0;
5230 }
5231 }
5232 if (run == 0)
5233 frp = frp->fr_next;
5234 else
5235 frp = frp->fr_prev;
5236 }
5237 }
5238 }
5239}
5240
5241#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5242/*
5243 * Set current window width and take care of repositioning other windows to
5244 * fit around it.
5245 */
5246 void
5247win_setwidth(width)
5248 int width;
5249{
5250 win_setwidth_win(width, curwin);
5251}
5252
5253 void
5254win_setwidth_win(width, wp)
5255 int width;
5256 win_T *wp;
5257{
5258 /* Always keep current window at least one column wide, even when
5259 * 'winminwidth' is zero. */
5260 if (wp == curwin)
5261 {
5262 if (width < p_wmw)
5263 width = p_wmw;
5264 if (width == 0)
5265 width = 1;
5266 }
5267
5268 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5269
5270 /* recompute the window positions */
5271 (void)win_comp_pos();
5272
5273 redraw_all_later(NOT_VALID);
5274}
5275
5276/*
5277 * Set the width of a frame to "width" and take care that all frames and
5278 * windows inside it are resized. Also resize frames above and below if the
5279 * are in the same FR_ROW frame.
5280 *
5281 * Strategy is similar to frame_setheight().
5282 */
5283 static void
5284frame_setwidth(curfrp, width)
5285 frame_T *curfrp;
5286 int width;
5287{
5288 int room; /* total number of lines available */
5289 int take; /* number of lines taken from other windows */
5290 int run;
5291 frame_T *frp;
5292 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005293 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 /* If the width already is the desired value, nothing to do. */
5296 if (curfrp->fr_width == width)
5297 return;
5298
5299 if (curfrp->fr_parent == NULL)
5300 /* topframe: can't change width */
5301 return;
5302
5303 if (curfrp->fr_parent->fr_layout == FR_COL)
5304 {
5305 /* Column of frames: Also need to resize frames above and below of
5306 * this one. First check for the minimal width of these. */
5307 w = frame_minwidth(curfrp->fr_parent, NULL);
5308 if (width < w)
5309 width = w;
5310 frame_setwidth(curfrp->fr_parent, width);
5311 }
5312 else
5313 {
5314 /*
5315 * Row of frames: try to change only frames in this row.
5316 *
5317 * Do this twice:
5318 * 1: compute room available, if it's not enough try resizing the
5319 * containing frame.
5320 * 2: compute the room available and adjust the width to it.
5321 */
5322 for (run = 1; run <= 2; ++run)
5323 {
5324 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005325 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5327 frp = frp->fr_next)
5328 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005329 if (frp != curfrp
5330 && frp->fr_win != NULL
5331 && frp->fr_win->w_p_wfw)
5332 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 room += frp->fr_width;
5334 if (frp != curfrp)
5335 room -= frame_minwidth(frp, NULL);
5336 }
5337
5338 if (width <= room)
5339 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005340 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 if (width > room)
5343 width = room;
5344 break;
5345 }
5346 frame_setwidth(curfrp->fr_parent, width
5347 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5348 }
5349
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 /*
5351 * Compute the number of lines we will take from others frames (can be
5352 * negative!).
5353 */
5354 take = width - curfrp->fr_width;
5355
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005356 /* If there is not enough room, also reduce the width of a window
5357 * with 'winfixwidth' set. */
5358 if (width > room - room_reserved)
5359 room_reserved = room - width;
5360 /* If there is only a 'winfixwidth' window and making the
5361 * window smaller, need to make the other window narrower. */
5362 if (take < 0 && room - curfrp->fr_width < room_reserved)
5363 room_reserved = 0;
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 /*
5366 * set the current frame to the new width
5367 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005368 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370 /*
5371 * First take lines from the frames right of the current frame. If
5372 * that is not enough, takes lines from frames left of the current
5373 * frame.
5374 */
5375 for (run = 0; run < 2; ++run)
5376 {
5377 if (run == 0)
5378 frp = curfrp->fr_next; /* 1st run: start with next window */
5379 else
5380 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5381 while (frp != NULL && take != 0)
5382 {
5383 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005384 if (room_reserved > 0
5385 && frp->fr_win != NULL
5386 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005388 if (room_reserved >= frp->fr_width)
5389 room_reserved -= frp->fr_width;
5390 else
5391 {
5392 if (frp->fr_width - room_reserved > take)
5393 room_reserved = frp->fr_width - take;
5394 take -= frp->fr_width - room_reserved;
5395 frame_new_width(frp, room_reserved, FALSE, FALSE);
5396 room_reserved = 0;
5397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 }
5399 else
5400 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005401 if (frp->fr_width - take < w)
5402 {
5403 take -= frp->fr_width - w;
5404 frame_new_width(frp, w, FALSE, FALSE);
5405 }
5406 else
5407 {
5408 frame_new_width(frp, frp->fr_width - take,
5409 FALSE, FALSE);
5410 take = 0;
5411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
5413 if (run == 0)
5414 frp = frp->fr_next;
5415 else
5416 frp = frp->fr_prev;
5417 }
5418 }
5419 }
5420}
5421#endif /* FEAT_VERTSPLIT */
5422
5423/*
5424 * Check 'winminheight' for a valid value.
5425 */
5426 void
5427win_setminheight()
5428{
5429 int room;
5430 int first = TRUE;
5431 win_T *wp;
5432
5433 /* loop until there is a 'winminheight' that is possible */
5434 while (p_wmh > 0)
5435 {
5436 /* TODO: handle vertical splits */
5437 room = -p_wh;
5438 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5439 room += wp->w_height - p_wmh;
5440 if (room >= 0)
5441 break;
5442 --p_wmh;
5443 if (first)
5444 {
5445 EMSG(_(e_noroom));
5446 first = FALSE;
5447 }
5448 }
5449}
5450
5451#ifdef FEAT_MOUSE
5452
5453/*
5454 * Status line of dragwin is dragged "offset" lines down (negative is up).
5455 */
5456 void
5457win_drag_status_line(dragwin, offset)
5458 win_T *dragwin;
5459 int offset;
5460{
5461 frame_T *curfr;
5462 frame_T *fr;
5463 int room;
5464 int row;
5465 int up; /* if TRUE, drag status line up, otherwise down */
5466 int n;
5467
5468 fr = dragwin->w_frame;
5469 curfr = fr;
5470 if (fr != topframe) /* more than one window */
5471 {
5472 fr = fr->fr_parent;
5473 /* When the parent frame is not a column of frames, its parent should
5474 * be. */
5475 if (fr->fr_layout != FR_COL)
5476 {
5477 curfr = fr;
5478 if (fr != topframe) /* only a row of windows, may drag statusline */
5479 fr = fr->fr_parent;
5480 }
5481 }
5482
5483 /* If this is the last frame in a column, may want to resize the parent
5484 * frame instead (go two up to skip a row of frames). */
5485 while (curfr != topframe && curfr->fr_next == NULL)
5486 {
5487 if (fr != topframe)
5488 fr = fr->fr_parent;
5489 curfr = fr;
5490 if (fr != topframe)
5491 fr = fr->fr_parent;
5492 }
5493
5494 if (offset < 0) /* drag up */
5495 {
5496 up = TRUE;
5497 offset = -offset;
5498 /* sum up the room of the current frame and above it */
5499 if (fr == curfr)
5500 {
5501 /* only one window */
5502 room = fr->fr_height - frame_minheight(fr, NULL);
5503 }
5504 else
5505 {
5506 room = 0;
5507 for (fr = fr->fr_child; ; fr = fr->fr_next)
5508 {
5509 room += fr->fr_height - frame_minheight(fr, NULL);
5510 if (fr == curfr)
5511 break;
5512 }
5513 }
5514 fr = curfr->fr_next; /* put fr at frame that grows */
5515 }
5516 else /* drag down */
5517 {
5518 up = FALSE;
5519 /*
5520 * Only dragging the last status line can reduce p_ch.
5521 */
5522 room = Rows - cmdline_row;
5523 if (curfr->fr_next == NULL)
5524 room -= 1;
5525 else
5526 room -= p_ch;
5527 if (room < 0)
5528 room = 0;
5529 /* sum up the room of frames below of the current one */
5530 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5531 room += fr->fr_height - frame_minheight(fr, NULL);
5532 fr = curfr; /* put fr at window that grows */
5533 }
5534
5535 if (room < offset) /* Not enough room */
5536 offset = room; /* Move as far as we can */
5537 if (offset <= 0)
5538 return;
5539
5540 /*
5541 * Grow frame fr by "offset" lines.
5542 * Doesn't happen when dragging the last status line up.
5543 */
5544 if (fr != NULL)
5545 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5546
5547 if (up)
5548 fr = curfr; /* current frame gets smaller */
5549 else
5550 fr = curfr->fr_next; /* next frame gets smaller */
5551
5552 /*
5553 * Now make the other frames smaller.
5554 */
5555 while (fr != NULL && offset > 0)
5556 {
5557 n = frame_minheight(fr, NULL);
5558 if (fr->fr_height - offset <= n)
5559 {
5560 offset -= fr->fr_height - n;
5561 frame_new_height(fr, n, !up, FALSE);
5562 }
5563 else
5564 {
5565 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5566 break;
5567 }
5568 if (up)
5569 fr = fr->fr_prev;
5570 else
5571 fr = fr->fr_next;
5572 }
5573 row = win_comp_pos();
5574 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5575 cmdline_row = row;
5576 p_ch = Rows - cmdline_row;
5577 if (p_ch < 1)
5578 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005579 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005580 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 showmode();
5582}
5583
5584#ifdef FEAT_VERTSPLIT
5585/*
5586 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5587 */
5588 void
5589win_drag_vsep_line(dragwin, offset)
5590 win_T *dragwin;
5591 int offset;
5592{
5593 frame_T *curfr;
5594 frame_T *fr;
5595 int room;
5596 int left; /* if TRUE, drag separator line left, otherwise right */
5597 int n;
5598
5599 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005600 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 return;
5602 curfr = fr;
5603 fr = fr->fr_parent;
5604 /* When the parent frame is not a row of frames, its parent should be. */
5605 if (fr->fr_layout != FR_ROW)
5606 {
5607 if (fr == topframe) /* only a column of windows (cannot happen?) */
5608 return;
5609 curfr = fr;
5610 fr = fr->fr_parent;
5611 }
5612
5613 /* If this is the last frame in a row, may want to resize a parent
5614 * frame instead. */
5615 while (curfr->fr_next == NULL)
5616 {
5617 if (fr == topframe)
5618 break;
5619 curfr = fr;
5620 fr = fr->fr_parent;
5621 if (fr != topframe)
5622 {
5623 curfr = fr;
5624 fr = fr->fr_parent;
5625 }
5626 }
5627
5628 if (offset < 0) /* drag left */
5629 {
5630 left = TRUE;
5631 offset = -offset;
5632 /* sum up the room of the current frame and left of it */
5633 room = 0;
5634 for (fr = fr->fr_child; ; fr = fr->fr_next)
5635 {
5636 room += fr->fr_width - frame_minwidth(fr, NULL);
5637 if (fr == curfr)
5638 break;
5639 }
5640 fr = curfr->fr_next; /* put fr at frame that grows */
5641 }
5642 else /* drag right */
5643 {
5644 left = FALSE;
5645 /* sum up the room of frames right of the current one */
5646 room = 0;
5647 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5648 room += fr->fr_width - frame_minwidth(fr, NULL);
5649 fr = curfr; /* put fr at window that grows */
5650 }
5651
5652 if (room < offset) /* Not enough room */
5653 offset = room; /* Move as far as we can */
5654 if (offset <= 0) /* No room at all, quit. */
5655 return;
5656
5657 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005658 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 /* shrink other frames: current and at the left or at the right */
5661 if (left)
5662 fr = curfr; /* current frame gets smaller */
5663 else
5664 fr = curfr->fr_next; /* next frame gets smaller */
5665
5666 while (fr != NULL && offset > 0)
5667 {
5668 n = frame_minwidth(fr, NULL);
5669 if (fr->fr_width - offset <= n)
5670 {
5671 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005672 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
5674 else
5675 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005676 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 break;
5678 }
5679 if (left)
5680 fr = fr->fr_prev;
5681 else
5682 fr = fr->fr_next;
5683 }
5684 (void)win_comp_pos();
5685 redraw_all_later(NOT_VALID);
5686}
5687#endif /* FEAT_VERTSPLIT */
5688#endif /* FEAT_MOUSE */
5689
5690#endif /* FEAT_WINDOWS */
5691
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005692#define FRACTION_MULT 16384L
5693
5694/*
5695 * Set wp->w_fraction for the current w_wrow and w_height.
5696 */
5697 static void
5698set_fraction(wp)
5699 win_T *wp;
5700{
5701 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005702 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005703}
5704
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705/*
5706 * Set the height of a window.
5707 * This takes care of the things inside the window, not what happens to the
5708 * window position, the frame or to other windows.
5709 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005710 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711win_new_height(wp, height)
5712 win_T *wp;
5713 int height;
5714{
5715 linenr_T lnum;
5716 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005717 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
5719 /* Don't want a negative height. Happens when splitting a tiny window.
5720 * Will equalize heights soon to fix it. */
5721 if (height < 0)
5722 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005723 if (wp->w_height == height)
5724 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005726 if (wp->w_height > 0)
5727 {
5728 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005729 /* w_wrow needs to be valid. When setting 'laststatus' this may
5730 * call win_new_height() recursively. */
5731 validate_cursor();
5732 if (wp->w_height != prev_height)
5733 return; /* Recursive call already changed the size, bail out here
5734 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005735 if (wp->w_wrow != wp->w_prev_fraction_row)
5736 set_fraction(wp);
5737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
5739 wp->w_height = height;
5740 wp->w_skipcol = 0;
5741
5742 /* Don't change w_topline when height is zero. Don't set w_topline when
5743 * 'scrollbind' is set and this isn't the current window. */
5744 if (height > 0
5745#ifdef FEAT_SCROLLBIND
5746 && (!wp->w_p_scb || wp == curwin)
5747#endif
5748 )
5749 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005750 /*
5751 * Find a value for w_topline that shows the cursor at the same
5752 * relative position in the window as before (more or less).
5753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 lnum = wp->w_cursor.lnum;
5755 if (lnum < 1) /* can happen when starting up */
5756 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005757 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5758 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5760 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005761
5762 if (sline >= 0)
5763 {
5764 /* Make sure the whole cursor line is visible, if possible. */
5765 int rows = plines_win(wp, lnum, FALSE);
5766
5767 if (sline > wp->w_height - rows)
5768 {
5769 sline = wp->w_height - rows;
5770 wp->w_wrow -= rows - line_size;
5771 }
5772 }
5773
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 if (sline < 0)
5775 {
5776 /*
5777 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005778 * Make cursor line the first line in the window. If not enough
5779 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 */
5781 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005782 if (wp->w_wrow >= wp->w_height
5783 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5784 {
5785 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5786 --wp->w_wrow;
5787 while (wp->w_wrow >= wp->w_height)
5788 {
5789 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5790 + win_col_off2(wp);
5791 --wp->w_wrow;
5792 }
5793 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005794 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005796 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005798 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
5800#ifdef FEAT_FOLDING
5801 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5802 if (lnum == 1)
5803 {
5804 /* first line in buffer is folded */
5805 line_size = 1;
5806 --sline;
5807 break;
5808 }
5809#endif
5810 --lnum;
5811#ifdef FEAT_DIFF
5812 if (lnum == wp->w_topline)
5813 line_size = plines_win_nofill(wp, lnum, TRUE)
5814 + wp->w_topfill;
5815 else
5816#endif
5817 line_size = plines_win(wp, lnum, TRUE);
5818 sline -= line_size;
5819 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (sline < 0)
5822 {
5823 /*
5824 * Line we want at top would go off top of screen. Use next
5825 * line instead.
5826 */
5827#ifdef FEAT_FOLDING
5828 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5829#endif
5830 lnum++;
5831 wp->w_wrow -= line_size + sline;
5832 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005833 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {
5835 /* First line of file reached, use that as topline. */
5836 lnum = 1;
5837 wp->w_wrow -= sline;
5838 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005839
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005840 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
5843
5844 if (wp == curwin)
5845 {
5846 if (p_so)
5847 update_topline();
5848 curs_columns(FALSE); /* validate w_wrow */
5849 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005850 if (prev_height > 0)
5851 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
5853 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005854 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#ifdef FEAT_WINDOWS
5856 wp->w_redr_status = TRUE;
5857#endif
5858 invalidate_botline_win(wp);
5859}
5860
5861#ifdef FEAT_VERTSPLIT
5862/*
5863 * Set the width of a window.
5864 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005865 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866win_new_width(wp, width)
5867 win_T *wp;
5868 int width;
5869{
5870 wp->w_width = width;
5871 wp->w_lines_valid = 0;
5872 changed_line_abv_curs_win(wp);
5873 invalidate_botline_win(wp);
5874 if (wp == curwin)
5875 {
5876 update_topline();
5877 curs_columns(TRUE); /* validate w_wrow */
5878 }
5879 redraw_win_later(wp, NOT_VALID);
5880 wp->w_redr_status = TRUE;
5881}
5882#endif
5883
5884 void
5885win_comp_scroll(wp)
5886 win_T *wp;
5887{
5888 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5889 if (wp->w_p_scr == 0)
5890 wp->w_p_scr = 1;
5891}
5892
5893/*
5894 * command_height: called whenever p_ch has been changed
5895 */
5896 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005897command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899#ifdef FEAT_WINDOWS
5900 int h;
5901 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005902 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005904 /* Use the value of p_ch that we remembered. This is needed for when the
5905 * GUI starts up, we can't be sure in what order things happen. And when
5906 * p_ch was changed in another tab page. */
5907 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 /* Find bottom frame with width of screen. */
5910 frp = lastwin->w_frame;
5911# ifdef FEAT_VERTSPLIT
5912 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5913 frp = frp->fr_parent;
5914# endif
5915
5916 /* Avoid changing the height of a window with 'winfixheight' set. */
5917 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5918 && frp->fr_win->w_p_wfh)
5919 frp = frp->fr_prev;
5920
5921 if (starting != NO_SCREEN)
5922 {
5923 cmdline_row = Rows - p_ch;
5924
5925 if (p_ch > old_p_ch) /* p_ch got bigger */
5926 {
5927 while (p_ch > old_p_ch)
5928 {
5929 if (frp == NULL)
5930 {
5931 EMSG(_(e_noroom));
5932 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005933 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 cmdline_row = Rows - p_ch;
5935 break;
5936 }
5937 h = frp->fr_height - frame_minheight(frp, NULL);
5938 if (h > p_ch - old_p_ch)
5939 h = p_ch - old_p_ch;
5940 old_p_ch += h;
5941 frame_add_height(frp, -h);
5942 frp = frp->fr_prev;
5943 }
5944
5945 /* Recompute window positions. */
5946 (void)win_comp_pos();
5947
5948 /* clear the lines added to cmdline */
5949 if (full_screen)
5950 screen_fill((int)(cmdline_row), (int)Rows, 0,
5951 (int)Columns, ' ', ' ', 0);
5952 msg_row = cmdline_row;
5953 redraw_cmdline = TRUE;
5954 return;
5955 }
5956
5957 if (msg_row < cmdline_row)
5958 msg_row = cmdline_row;
5959 redraw_cmdline = TRUE;
5960 }
5961 frame_add_height(frp, (int)(old_p_ch - p_ch));
5962
5963 /* Recompute window positions. */
5964 if (frp != lastwin->w_frame)
5965 (void)win_comp_pos();
5966#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005968 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969#endif
5970}
5971
5972#if defined(FEAT_WINDOWS) || defined(PROTO)
5973/*
5974 * Resize frame "frp" to be "n" lines higher (negative for less high).
5975 * Also resize the frames it is contained in.
5976 */
5977 static void
5978frame_add_height(frp, n)
5979 frame_T *frp;
5980 int n;
5981{
5982 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5983 for (;;)
5984 {
5985 frp = frp->fr_parent;
5986 if (frp == NULL)
5987 break;
5988 frp->fr_height += n;
5989 }
5990}
5991
5992/*
5993 * Add or remove a status line for the bottom window(s), according to the
5994 * value of 'laststatus'.
5995 */
5996 void
5997last_status(morewin)
5998 int morewin; /* pretend there are two or more windows */
5999{
6000 /* Don't make a difference between horizontal or vertical split. */
6001 last_status_rec(topframe, (p_ls == 2
6002 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6003}
6004
6005 static void
6006last_status_rec(fr, statusline)
6007 frame_T *fr;
6008 int statusline;
6009{
6010 frame_T *fp;
6011 win_T *wp;
6012
6013 if (fr->fr_layout == FR_LEAF)
6014 {
6015 wp = fr->fr_win;
6016 if (wp->w_status_height != 0 && !statusline)
6017 {
6018 /* remove status line */
6019 win_new_height(wp, wp->w_height + 1);
6020 wp->w_status_height = 0;
6021 comp_col();
6022 }
6023 else if (wp->w_status_height == 0 && statusline)
6024 {
6025 /* Find a frame to take a line from. */
6026 fp = fr;
6027 while (fp->fr_height <= frame_minheight(fp, NULL))
6028 {
6029 if (fp == topframe)
6030 {
6031 EMSG(_(e_noroom));
6032 return;
6033 }
6034 /* In a column of frames: go to frame above. If already at
6035 * the top or in a row of frames: go to parent. */
6036 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6037 fp = fp->fr_prev;
6038 else
6039 fp = fp->fr_parent;
6040 }
6041 wp->w_status_height = 1;
6042 if (fp != fr)
6043 {
6044 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6045 frame_fix_height(wp);
6046 (void)win_comp_pos();
6047 }
6048 else
6049 win_new_height(wp, wp->w_height - 1);
6050 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006051 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 }
6053 }
6054#ifdef FEAT_VERTSPLIT
6055 else if (fr->fr_layout == FR_ROW)
6056 {
6057 /* vertically split windows, set status line for each one */
6058 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6059 last_status_rec(fp, statusline);
6060 }
6061#endif
6062 else
6063 {
6064 /* horizontally split window, set status line for last one */
6065 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6066 ;
6067 last_status_rec(fp, statusline);
6068 }
6069}
6070
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006071/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006072 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006073 */
6074 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006075tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006076{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006077#ifdef FEAT_GUI_TABLINE
6078 /* When the GUI has the tabline then this always returns zero. */
6079 if (gui_use_tabline())
6080 return 0;
6081#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006082 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006083 {
6084 case 0: return 0;
6085 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6086 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006087 return 1;
6088}
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090#endif /* FEAT_WINDOWS */
6091
6092#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6093/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006094 * Get the file name at the cursor.
6095 * If Visual mode is active, use the selected text if it's in one line.
6096 * Returns the name in allocated memory, NULL for failure.
6097 */
6098 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006099grab_file_name(count, file_lnum)
6100 long count;
6101 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006102{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006103 if (VIsual_active)
6104 {
6105 int len;
6106 char_u *ptr;
6107
6108 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6109 return NULL;
6110 return find_file_name_in_path(ptr, len,
6111 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6112 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006113 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6114 file_lnum);
6115
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006116}
6117
6118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 * Return the file name under or after the cursor.
6120 *
6121 * The 'path' option is searched if the file name is not absolute.
6122 * The string returned has been alloc'ed and should be freed by the caller.
6123 * NULL is returned if the file name or file is not found.
6124 *
6125 * options:
6126 * FNAME_MESS give error messages
6127 * FNAME_EXP expand to path
6128 * FNAME_HYP check for hypertext link
6129 * FNAME_INCL apply "includeexpr"
6130 */
6131 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006132file_name_at_cursor(options, count, file_lnum)
6133 int options;
6134 long count;
6135 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006138 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6139 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140}
6141
6142/*
6143 * Return the name of the file under or after ptr[col].
6144 * Otherwise like file_name_at_cursor().
6145 */
6146 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006147file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 char_u *line;
6149 int col;
6150 int options;
6151 long count;
6152 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006153 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 char_u *ptr;
6156 int len;
6157
6158 /*
6159 * search forward for what could be the start of a file name
6160 */
6161 ptr = line + col;
6162 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006163 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (*ptr == NUL) /* nothing found */
6165 {
6166 if (options & FNAME_MESS)
6167 EMSG(_("E446: No file name under cursor"));
6168 return NULL;
6169 }
6170
6171 /*
6172 * Search backward for first char of the file name.
6173 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6174 */
6175 while (ptr > line)
6176 {
6177#ifdef FEAT_MBYTE
6178 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6179 ptr -= len + 1;
6180 else
6181#endif
6182 if (vim_isfilec(ptr[-1])
6183 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6184 --ptr;
6185 else
6186 break;
6187 }
6188
6189 /*
6190 * Search forward for the last char of the file name.
6191 * Also allow "://" when ':' is not in 'isfname'.
6192 */
6193 len = 0;
6194 while (vim_isfilec(ptr[len])
6195 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6196#ifdef FEAT_MBYTE
6197 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006198 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 else
6200#endif
6201 ++len;
6202
6203 /*
6204 * If there is trailing punctuation, remove it.
6205 * But don't remove "..", could be a directory name.
6206 */
6207 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6208 && ptr[len - 2] != '.')
6209 --len;
6210
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006211 if (file_lnum != NULL)
6212 {
6213 char_u *p;
6214
6215 /* Get the number after the file name and a separator character */
6216 p = ptr + len;
6217 p = skipwhite(p);
6218 if (*p != NUL)
6219 {
6220 if (!isdigit(*p))
6221 ++p; /* skip the separator */
6222 p = skipwhite(p);
6223 if (isdigit(*p))
6224 *file_lnum = (int)getdigits(&p);
6225 }
6226 }
6227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6229}
6230
6231# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6232static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6233
6234 static char_u *
6235eval_includeexpr(ptr, len)
6236 char_u *ptr;
6237 int len;
6238{
6239 char_u *res;
6240
6241 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006242 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006243 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 set_vim_var_string(VV_FNAME, NULL, 0);
6245 return res;
6246}
6247#endif
6248
6249/*
6250 * Return the name of the file ptr[len] in 'path'.
6251 * Otherwise like file_name_at_cursor().
6252 */
6253 char_u *
6254find_file_name_in_path(ptr, len, options, count, rel_fname)
6255 char_u *ptr;
6256 int len;
6257 int options;
6258 long count;
6259 char_u *rel_fname; /* file we are searching relative to */
6260{
6261 char_u *file_name;
6262 int c;
6263# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6264 char_u *tofree = NULL;
6265
6266 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6267 {
6268 tofree = eval_includeexpr(ptr, len);
6269 if (tofree != NULL)
6270 {
6271 ptr = tofree;
6272 len = (int)STRLEN(ptr);
6273 }
6274 }
6275# endif
6276
6277 if (options & FNAME_EXP)
6278 {
6279 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6280 TRUE, rel_fname);
6281
6282# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6283 /*
6284 * If the file could not be found in a normal way, try applying
6285 * 'includeexpr' (unless done already).
6286 */
6287 if (file_name == NULL
6288 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6289 {
6290 tofree = eval_includeexpr(ptr, len);
6291 if (tofree != NULL)
6292 {
6293 ptr = tofree;
6294 len = (int)STRLEN(ptr);
6295 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6296 TRUE, rel_fname);
6297 }
6298 }
6299# endif
6300 if (file_name == NULL && (options & FNAME_MESS))
6301 {
6302 c = ptr[len];
6303 ptr[len] = NUL;
6304 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6305 ptr[len] = c;
6306 }
6307
6308 /* Repeat finding the file "count" times. This matters when it
6309 * appears several times in the path. */
6310 while (file_name != NULL && --count > 0)
6311 {
6312 vim_free(file_name);
6313 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6314 }
6315 }
6316 else
6317 file_name = vim_strnsave(ptr, len);
6318
6319# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6320 vim_free(tofree);
6321# endif
6322
6323 return file_name;
6324}
6325#endif /* FEAT_SEARCHPATH */
6326
6327/*
6328 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6329 * Also check for ":\\", which MS Internet Explorer accepts, return
6330 * URL_BACKSLASH.
6331 */
6332 static int
6333path_is_url(p)
6334 char_u *p;
6335{
6336 if (STRNCMP(p, "://", (size_t)3) == 0)
6337 return URL_SLASH;
6338 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6339 return URL_BACKSLASH;
6340 return 0;
6341}
6342
6343/*
6344 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6345 * Return URL_BACKSLASH for "name:\\".
6346 * Return zero otherwise.
6347 */
6348 int
6349path_with_url(fname)
6350 char_u *fname;
6351{
6352 char_u *p;
6353
6354 for (p = fname; isalpha(*p); ++p)
6355 ;
6356 return path_is_url(p);
6357}
6358
6359/*
6360 * Return TRUE if "name" is a full (absolute) path name or URL.
6361 */
6362 int
6363vim_isAbsName(name)
6364 char_u *name;
6365{
6366 return (path_with_url(name) != 0 || mch_isFullName(name));
6367}
6368
6369/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006370 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 *
6372 * return FAIL for failure, OK otherwise
6373 */
6374 int
6375vim_FullName(fname, buf, len, force)
6376 char_u *fname, *buf;
6377 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006378 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 int retval = OK;
6381 int url;
6382
6383 *buf = NUL;
6384 if (fname == NULL)
6385 return FAIL;
6386
6387 url = path_with_url(fname);
6388 if (!url)
6389 retval = mch_FullName(fname, buf, len, force);
6390 if (url || retval == FAIL)
6391 {
6392 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006393 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 }
6395#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6396 slash_adjust(buf);
6397#endif
6398 return retval;
6399}
6400
6401/*
6402 * Return the minimal number of rows that is needed on the screen to display
6403 * the current number of windows.
6404 */
6405 int
6406min_rows()
6407{
6408 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006409#ifdef FEAT_WINDOWS
6410 tabpage_T *tp;
6411 int n;
6412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
6414 if (firstwin == NULL) /* not initialized yet */
6415 return MIN_LINES;
6416
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006418 total = 0;
6419 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6420 {
6421 n = frame_minheight(tp->tp_topframe, NULL);
6422 if (total < n)
6423 total = n;
6424 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006425 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006427 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006429 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 return total;
6431}
6432
6433/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006434 * Return TRUE if there is only one window (in the current tab page), not
6435 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006436 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 */
6438 int
6439only_one_window()
6440{
6441#ifdef FEAT_WINDOWS
6442 int count = 0;
6443 win_T *wp;
6444
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006445 /* If there is another tab page there always is another window. */
6446 if (first_tabpage->tp_next != NULL)
6447 return FALSE;
6448
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006450 if (wp->w_buffer != NULL
6451 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452# ifdef FEAT_QUICKFIX
6453 || wp->w_p_pvw
6454# endif
6455 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006456# ifdef FEAT_AUTOCMD
6457 && wp != aucmd_win
6458# endif
6459 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 ++count;
6461 return (count <= 1);
6462#else
6463 return TRUE;
6464#endif
6465}
6466
6467#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6468/*
6469 * Correct the cursor line number in other windows. Used after changing the
6470 * current buffer, and before applying autocommands.
6471 * When "do_curwin" is TRUE, also check current window.
6472 */
6473 void
6474check_lnums(do_curwin)
6475 int do_curwin;
6476{
6477 win_T *wp;
6478
6479#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006480 tabpage_T *tp;
6481
6482 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6484#else
6485 wp = curwin;
6486 if (do_curwin)
6487#endif
6488 {
6489 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6490 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6491 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6492 wp->w_topline = curbuf->b_ml.ml_line_count;
6493 }
6494}
6495#endif
6496
6497#if defined(FEAT_WINDOWS) || defined(PROTO)
6498
6499/*
6500 * A snapshot of the window sizes, to restore them after closing the help
6501 * window.
6502 * Only these fields are used:
6503 * fr_layout
6504 * fr_width
6505 * fr_height
6506 * fr_next
6507 * fr_child
6508 * fr_win (only valid for the old curwin, NULL otherwise)
6509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511/*
6512 * Create a snapshot of the current frame sizes.
6513 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006514 void
6515make_snapshot(idx)
6516 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006518 clear_snapshot(curtab, idx);
6519 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520}
6521
6522 static void
6523make_snapshot_rec(fr, frp)
6524 frame_T *fr;
6525 frame_T **frp;
6526{
6527 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6528 if (*frp == NULL)
6529 return;
6530 (*frp)->fr_layout = fr->fr_layout;
6531# ifdef FEAT_VERTSPLIT
6532 (*frp)->fr_width = fr->fr_width;
6533# endif
6534 (*frp)->fr_height = fr->fr_height;
6535 if (fr->fr_next != NULL)
6536 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6537 if (fr->fr_child != NULL)
6538 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6539 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6540 (*frp)->fr_win = curwin;
6541}
6542
6543/*
6544 * Remove any existing snapshot.
6545 */
6546 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006547clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006548 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006549 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006551 clear_snapshot_rec(tp->tp_snapshot[idx]);
6552 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553}
6554
6555 static void
6556clear_snapshot_rec(fr)
6557 frame_T *fr;
6558{
6559 if (fr != NULL)
6560 {
6561 clear_snapshot_rec(fr->fr_next);
6562 clear_snapshot_rec(fr->fr_child);
6563 vim_free(fr);
6564 }
6565}
6566
6567/*
6568 * Restore a previously created snapshot, if there is any.
6569 * This is only done if the screen size didn't change and the window layout is
6570 * still the same.
6571 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006572 void
6573restore_snapshot(idx, close_curwin)
6574 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 int close_curwin; /* closing current window */
6576{
6577 win_T *wp;
6578
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006579 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006581 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006583 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6584 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006586 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 win_comp_pos();
6588 if (wp != NULL && close_curwin)
6589 win_goto(wp);
6590 redraw_all_later(CLEAR);
6591 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006592 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593}
6594
6595/*
6596 * Check if frames "sn" and "fr" have the same layout, same following frames
6597 * and same children.
6598 */
6599 static int
6600check_snapshot_rec(sn, fr)
6601 frame_T *sn;
6602 frame_T *fr;
6603{
6604 if (sn->fr_layout != fr->fr_layout
6605 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6606 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6607 || (sn->fr_next != NULL
6608 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6609 || (sn->fr_child != NULL
6610 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6611 return FAIL;
6612 return OK;
6613}
6614
6615/*
6616 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6617 * following frames and children.
6618 * Returns a pointer to the old current window, or NULL.
6619 */
6620 static win_T *
6621restore_snapshot_rec(sn, fr)
6622 frame_T *sn;
6623 frame_T *fr;
6624{
6625 win_T *wp = NULL;
6626 win_T *wp2;
6627
6628 fr->fr_height = sn->fr_height;
6629# ifdef FEAT_VERTSPLIT
6630 fr->fr_width = sn->fr_width;
6631# endif
6632 if (fr->fr_layout == FR_LEAF)
6633 {
6634 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6635# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006636 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637# endif
6638 wp = sn->fr_win;
6639 }
6640 if (sn->fr_next != NULL)
6641 {
6642 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6643 if (wp2 != NULL)
6644 wp = wp2;
6645 }
6646 if (sn->fr_child != NULL)
6647 {
6648 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6649 if (wp2 != NULL)
6650 wp = wp2;
6651 }
6652 return wp;
6653}
6654
6655#endif
6656
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006657#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6658 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006659/*
6660 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006661 * restore_win() MUST be called to undo, also when FAIL is returned.
6662 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006663 * When "no_display" is TRUE the display won't be affected, no redraw is
6664 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006665 * Returns FAIL if switching to "win" failed.
6666 */
6667 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006668switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006669 win_T **save_curwin UNUSED;
6670 tabpage_T **save_curtab UNUSED;
6671 win_T *win UNUSED;
6672 tabpage_T *tp UNUSED;
6673 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006674{
6675# ifdef FEAT_AUTOCMD
6676 block_autocmds();
6677# endif
6678# ifdef FEAT_WINDOWS
6679 *save_curwin = curwin;
6680 if (tp != NULL)
6681 {
6682 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006683 if (no_display)
6684 {
6685 curtab->tp_firstwin = firstwin;
6686 curtab->tp_lastwin = lastwin;
6687 curtab = tp;
6688 firstwin = curtab->tp_firstwin;
6689 lastwin = curtab->tp_lastwin;
6690 }
6691 else
6692 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006693 }
6694 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006695 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006696 curwin = win;
6697 curbuf = curwin->w_buffer;
6698# endif
6699 return OK;
6700}
6701
6702/*
6703 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006704 * When "no_display" is TRUE the display won't be affected, no redraw is
6705 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006706 */
6707 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006708restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006709 win_T *save_curwin UNUSED;
6710 tabpage_T *save_curtab UNUSED;
6711 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006712{
6713# ifdef FEAT_WINDOWS
6714 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006715 {
6716 if (no_display)
6717 {
6718 curtab->tp_firstwin = firstwin;
6719 curtab->tp_lastwin = lastwin;
6720 curtab = save_curtab;
6721 firstwin = curtab->tp_firstwin;
6722 lastwin = curtab->tp_lastwin;
6723 }
6724 else
6725 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6726 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006727 if (win_valid(save_curwin))
6728 {
6729 curwin = save_curwin;
6730 curbuf = curwin->w_buffer;
6731 }
6732# endif
6733# ifdef FEAT_AUTOCMD
6734 unblock_autocmds();
6735# endif
6736}
6737
6738/*
6739 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6740 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6741 */
6742 void
6743switch_buffer(save_curbuf, buf)
6744 buf_T *buf;
6745 buf_T **save_curbuf;
6746{
6747# ifdef FEAT_AUTOCMD
6748 block_autocmds();
6749# endif
6750 *save_curbuf = curbuf;
6751 --curbuf->b_nwindows;
6752 curbuf = buf;
6753 curwin->w_buffer = buf;
6754 ++curbuf->b_nwindows;
6755}
6756
6757/*
6758 * Restore the current buffer after using switch_buffer().
6759 */
6760 void
6761restore_buffer(save_curbuf)
6762 buf_T *save_curbuf;
6763{
6764# ifdef FEAT_AUTOCMD
6765 unblock_autocmds();
6766# endif
6767 /* Check for valid buffer, just in case. */
6768 if (buf_valid(save_curbuf))
6769 {
6770 --curbuf->b_nwindows;
6771 curwin->w_buffer = save_curbuf;
6772 curbuf = save_curbuf;
6773 ++curbuf->b_nwindows;
6774 }
6775}
6776#endif
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6779/*
6780 * Return TRUE if there is any vertically split window.
6781 */
6782 int
6783win_hasvertsplit()
6784{
6785 frame_T *fr;
6786
6787 if (topframe->fr_layout == FR_ROW)
6788 return TRUE;
6789
6790 if (topframe->fr_layout == FR_COL)
6791 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6792 if (fr->fr_layout == FR_ROW)
6793 return TRUE;
6794
6795 return FALSE;
6796}
6797#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006798
6799#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6800/*
6801 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006802 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006803 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6804 * If no particular ID is desired, -1 must be specified for 'id'.
6805 * Return ID of added match, -1 on failure.
6806 */
6807 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006808match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006809 win_T *wp;
6810 char_u *grp;
6811 char_u *pat;
6812 int prio;
6813 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006814 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006815{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006816 matchitem_T *cur;
6817 matchitem_T *prev;
6818 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006819 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006820 regprog_T *regprog = NULL;
6821 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006822
Bram Moolenaarb3414592014-06-17 17:48:32 +02006823 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006824 return -1;
6825 if (id < -1 || id == 0)
6826 {
6827 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6828 return -1;
6829 }
6830 if (id != -1)
6831 {
6832 cur = wp->w_match_head;
6833 while (cur != NULL)
6834 {
6835 if (cur->id == id)
6836 {
6837 EMSGN("E801: ID already taken: %ld", id);
6838 return -1;
6839 }
6840 cur = cur->next;
6841 }
6842 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006843 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006844 {
6845 EMSG2(_(e_nogroup), grp);
6846 return -1;
6847 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006848 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006849 {
6850 EMSG2(_(e_invarg2), pat);
6851 return -1;
6852 }
6853
6854 /* Find available match ID. */
6855 while (id == -1)
6856 {
6857 cur = wp->w_match_head;
6858 while (cur != NULL && cur->id != wp->w_next_match_id)
6859 cur = cur->next;
6860 if (cur == NULL)
6861 id = wp->w_next_match_id;
6862 wp->w_next_match_id++;
6863 }
6864
6865 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006866 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006867 m->id = id;
6868 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006869 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006870 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006871 m->match.regprog = regprog;
6872 m->match.rmm_ic = FALSE;
6873 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006874
Bram Moolenaarb3414592014-06-17 17:48:32 +02006875 /* Set up position matches */
6876 if (pos_list != NULL)
6877 {
6878 linenr_T toplnum = 0;
6879 linenr_T botlnum = 0;
6880 listitem_T *li;
6881 int i;
6882
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006883 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006884 i++, li = li->li_next)
6885 {
6886 linenr_T lnum = 0;
6887 colnr_T col = 0;
6888 int len = 1;
6889 list_T *subl;
6890 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006891 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006892
Bram Moolenaarb3414592014-06-17 17:48:32 +02006893 if (li->li_tv.v_type == VAR_LIST)
6894 {
6895 subl = li->li_tv.vval.v_list;
6896 if (subl == NULL)
6897 goto fail;
6898 subli = subl->lv_first;
6899 if (subli == NULL)
6900 goto fail;
6901 lnum = get_tv_number_chk(&subli->li_tv, &error);
6902 if (error == TRUE)
6903 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006904 if (lnum == 0)
6905 {
6906 --i;
6907 continue;
6908 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006909 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006910 subli = subli->li_next;
6911 if (subli != NULL)
6912 {
6913 col = get_tv_number_chk(&subli->li_tv, &error);
6914 if (error == TRUE)
6915 goto fail;
6916 subli = subli->li_next;
6917 if (subli != NULL)
6918 {
6919 len = get_tv_number_chk(&subli->li_tv, &error);
6920 if (error == TRUE)
6921 goto fail;
6922 }
6923 }
6924 m->pos.pos[i].col = col;
6925 m->pos.pos[i].len = len;
6926 }
6927 else if (li->li_tv.v_type == VAR_NUMBER)
6928 {
6929 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006930 {
6931 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006932 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006933 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006934 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6935 m->pos.pos[i].col = 0;
6936 m->pos.pos[i].len = 0;
6937 }
6938 else
6939 {
6940 EMSG(_("List or number required"));
6941 goto fail;
6942 }
6943 if (toplnum == 0 || lnum < toplnum)
6944 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006945 if (botlnum == 0 || lnum >= botlnum)
6946 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006947 }
6948
6949 /* Calculate top and bottom lines for redrawing area */
6950 if (toplnum != 0)
6951 {
6952 if (wp->w_buffer->b_mod_set)
6953 {
6954 if (wp->w_buffer->b_mod_top > toplnum)
6955 wp->w_buffer->b_mod_top = toplnum;
6956 if (wp->w_buffer->b_mod_bot < botlnum)
6957 wp->w_buffer->b_mod_bot = botlnum;
6958 }
6959 else
6960 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006961 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006962 wp->w_buffer->b_mod_top = toplnum;
6963 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006964 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006965 }
6966 m->pos.toplnum = toplnum;
6967 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006968 rtype = VALID;
6969 }
6970 }
6971
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006972 /* Insert new match. The match list is in ascending order with regard to
6973 * the match priorities. */
6974 cur = wp->w_match_head;
6975 prev = cur;
6976 while (cur != NULL && prio >= cur->priority)
6977 {
6978 prev = cur;
6979 cur = cur->next;
6980 }
6981 if (cur == prev)
6982 wp->w_match_head = m;
6983 else
6984 prev->next = m;
6985 m->next = cur;
6986
Bram Moolenaarb3414592014-06-17 17:48:32 +02006987 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006988 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006989
6990fail:
6991 vim_free(m);
6992 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006993}
6994
6995/*
6996 * Delete match with ID 'id' in the match list of window 'wp'.
6997 * Print error messages if 'perr' is TRUE.
6998 */
6999 int
7000match_delete(wp, id, perr)
7001 win_T *wp;
7002 int id;
7003 int perr;
7004{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007005 matchitem_T *cur = wp->w_match_head;
7006 matchitem_T *prev = cur;
7007 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007008
7009 if (id < 1)
7010 {
7011 if (perr == TRUE)
7012 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7013 id);
7014 return -1;
7015 }
7016 while (cur != NULL && cur->id != id)
7017 {
7018 prev = cur;
7019 cur = cur->next;
7020 }
7021 if (cur == NULL)
7022 {
7023 if (perr == TRUE)
7024 EMSGN("E803: ID not found: %ld", id);
7025 return -1;
7026 }
7027 if (cur == prev)
7028 wp->w_match_head = cur->next;
7029 else
7030 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007031 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007032 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007033 if (cur->pos.toplnum != 0)
7034 {
7035 if (wp->w_buffer->b_mod_set)
7036 {
7037 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7038 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7039 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7040 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7041 }
7042 else
7043 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007044 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007045 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7046 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007047 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007048 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007049 rtype = VALID;
7050 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007051 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007052 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007053 return 0;
7054}
7055
7056/*
7057 * Delete all matches in the match list of window 'wp'.
7058 */
7059 void
7060clear_matches(wp)
7061 win_T *wp;
7062{
7063 matchitem_T *m;
7064
7065 while (wp->w_match_head != NULL)
7066 {
7067 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007068 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007069 vim_free(wp->w_match_head->pattern);
7070 vim_free(wp->w_match_head);
7071 wp->w_match_head = m;
7072 }
7073 redraw_later(SOME_VALID);
7074}
7075
7076/*
7077 * Get match from ID 'id' in window 'wp'.
7078 * Return NULL if match not found.
7079 */
7080 matchitem_T *
7081get_match(wp, id)
7082 win_T *wp;
7083 int id;
7084{
7085 matchitem_T *cur = wp->w_match_head;
7086
7087 while (cur != NULL && cur->id != id)
7088 cur = cur->next;
7089 return cur;
7090}
7091#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007092
7093#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7094 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007095get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007096{
7097 int i = 1;
7098 win_T *w;
7099
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007100 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007101 ++i;
7102
7103 if (w == NULL)
7104 return 0;
7105 else
7106 return i;
7107}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007108
7109 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007110get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007111{
7112 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007113# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007114 tabpage_T *t;
7115
7116 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7117 ++i;
7118
7119 if (t == NULL)
7120 return 0;
7121 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007122# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007123 return i;
7124}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007125#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007126
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007127#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007128/*
7129 * Return TRUE if "topfrp" and its children are at the right height.
7130 */
7131 static int
7132frame_check_height(topfrp, height)
7133 frame_T *topfrp;
7134 int height;
7135{
7136 frame_T *frp;
7137
7138 if (topfrp->fr_height != height)
7139 return FALSE;
7140
7141 if (topfrp->fr_layout == FR_ROW)
7142 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7143 if (frp->fr_height != height)
7144 return FALSE;
7145
7146 return TRUE;
7147}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007148#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007149
7150#ifdef FEAT_VERTSPLIT
7151/*
7152 * Return TRUE if "topfrp" and its children are at the right width.
7153 */
7154 static int
7155frame_check_width(topfrp, width)
7156 frame_T *topfrp;
7157 int width;
7158{
7159 frame_T *frp;
7160
7161 if (topfrp->fr_width != width)
7162 return FALSE;
7163
7164 if (topfrp->fr_layout == FR_COL)
7165 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7166 if (frp->fr_width != width)
7167 return FALSE;
7168
7169 return TRUE;
7170}
7171#endif
7172