blob: 4bc2e6c4129d5389ac06c3369fc972dae35984fb [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 Moolenaard355c502014-09-23 13:48:43 +0200487 g_do_tagpreview = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
Bram Moolenaard355c502014-09-23 13:48:43 +0200489 /* Execute the command right here, required when "wincmd ]"
490 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 do_nv_ident(Ctrl_RSB, NUL);
492 break;
493
494#ifdef FEAT_SEARCHPATH
495/* edit file name under cursor in a new window */
496 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000497 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000499wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000501
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000502 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 if (ptr != NULL)
504 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000505# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 setpcmark();
509 if (win_split(0, 0) == OK)
510 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200511 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000512 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
513 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000514 if (nchar == 'F' && lnum >= 0)
515 {
516 curwin->w_cursor.lnum = lnum;
517 check_cursor_lnum();
518 beginline(BL_SOL | BL_FIX);
519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 }
521 vim_free(ptr);
522 }
523 break;
524#endif
525
526#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000527/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 * new window -- webb
529 */
530 case 'i': /* Go to any match */
531 case Ctrl_I:
532 type = FIND_ANY;
533 /* FALLTHROUGH */
534 case 'd': /* Go to definition, using 'define' */
535 case Ctrl_D:
536 CHECK_CMDWIN
537 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
538 break;
539 find_pattern_in_path(ptr, 0, len, TRUE,
540 Prenum == 0 ? TRUE : FALSE, type,
541 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
542 curwin->w_set_curswant = TRUE;
543 break;
544#endif
545
Bram Moolenaar05159a02005-02-26 23:04:13 +0000546 case K_KENTER:
547 case CAR:
548#if defined(FEAT_QUICKFIX)
549 /*
550 * In a quickfix window a <CR> jumps to the error under the
551 * cursor in a new window.
552 */
553 if (bt_quickfix(curbuf))
554 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000555 sprintf((char *)cbuf, "split +%ld%s",
556 (long)curwin->w_cursor.lnum,
557 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000558 do_cmdline_cmd(cbuf);
559 }
560#endif
561 break;
562
563
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564/* CTRL-W g extended commands */
565 case 'g':
566 case Ctrl_G:
567 CHECK_CMDWIN
568#ifdef USE_ON_FLY_SCROLL
569 dont_scroll = TRUE; /* disallow scrolling here */
570#endif
571 ++no_mapping;
572 ++allow_keys; /* no mapping for xchar, but allow key codes */
573 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000574 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 --no_mapping;
577 --allow_keys;
578#ifdef FEAT_CMDL_INFO
579 (void)add_to_showcmd(xchar);
580#endif
581 switch (xchar)
582 {
583#if defined(FEAT_QUICKFIX)
584 case '}':
585 xchar = Ctrl_RSB;
586 if (Prenum)
587 g_do_tagpreview = Prenum;
588 else
589 g_do_tagpreview = p_pvh;
590 /*FALLTHROUGH*/
591#endif
592 case ']':
593 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200594 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (Prenum)
596 postponed_split = Prenum;
597 else
598 postponed_split = -1;
599
600 /* Execute the command right here, required when
601 * "wincmd g}" was used in a function. */
602 do_nv_ident('g', xchar);
603 break;
604
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000605#ifdef FEAT_SEARCHPATH
606 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000607 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100608 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000609 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000610 goto wingotofile;
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 default:
613 beep_flush();
614 break;
615 }
616 break;
617
618 default: beep_flush();
619 break;
620 }
621}
622
623/*
624 * split the current window, implements CTRL-W s and :split
625 *
626 * "size" is the height or width for the new window, 0 to use half of current
627 * height or width.
628 *
629 * "flags":
630 * WSP_ROOM: require enough room for new window
631 * WSP_VERT: vertical split.
632 * WSP_TOP: open window at the top-left of the shell (help window).
633 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
634 * WSP_HELP: creating the help window, keep layout snapshot
635 *
636 * return FAIL for failure, OK otherwise
637 */
638 int
639win_split(size, flags)
640 int size;
641 int flags;
642{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000643 /* When the ":tab" modifier was used open a new tab page instead. */
644 if (may_open_tabpage() == OK)
645 return OK;
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 /* Add flags from ":vertical", ":topleft" and ":botright". */
648 flags |= cmdmod.split;
649 if ((flags & WSP_TOP) && (flags & WSP_BOT))
650 {
651 EMSG(_("E442: Can't split topleft and botright at the same time"));
652 return FAIL;
653 }
654
655 /* When creating the help window make a snapshot of the window layout.
656 * Otherwise clear the snapshot, it's now invalid. */
657 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000658 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000660 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 return win_split_ins(size, flags, NULL, 0);
663}
664
665/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100666 * When "new_wp" is NULL: split the current window in two.
667 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 * top/left/right/bottom.
669 * return FAIL for failure, OK otherwise
670 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000671 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100672win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 int size;
674 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100675 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 int dir;
677{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100678 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 win_T *oldwin;
680 int new_size = size;
681 int i;
682 int need_status = 0;
683 int do_equal = FALSE;
684 int needed;
685 int available;
686 int oldwin_height = 0;
687 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200688 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200690 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200691 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693 if (flags & WSP_TOP)
694 oldwin = firstwin;
695 else if (flags & WSP_BOT)
696 oldwin = lastwin;
697 else
698 oldwin = curwin;
699
700 /* add a status line when p_ls == 1 and splitting the first window */
701 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
702 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100703 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 EMSG(_(e_noroom));
706 return FAIL;
707 }
708 need_status = STATUS_HEIGHT;
709 }
710
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000711#ifdef FEAT_GUI
712 /* May be needed for the scrollbars that are going to change. */
713 if (gui.in_use)
714 out_flush();
715#endif
716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#ifdef FEAT_VERTSPLIT
718 if (flags & WSP_VERT)
719 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200720 int wmw1;
721 int minwidth;
722
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
725 /*
726 * Check if we are able to split the current window and compute its
727 * width.
728 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200729 /* Current window requires at least 1 space. */
730 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
731 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200733 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200734 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200736 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200738 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200740 else if (p_ea)
741 {
742 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
743 prevfrp = oldwin->w_frame;
744 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
745 frp = frp->fr_parent)
746 {
747 if (frp->fr_layout == FR_ROW)
748 for (frp2 = frp->fr_child; frp2 != NULL;
749 frp2 = frp2->fr_next)
750 if (frp2 != prevfrp)
751 minwidth += frame_minwidth(frp2, NOWIN);
752 prevfrp = frp;
753 }
754 available = topframe->fr_width;
755 needed += minwidth;
756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200758 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200759 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
760 available = oldwin->w_frame->fr_width;
761 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200762 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100763 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
765 EMSG(_(e_noroom));
766 return FAIL;
767 }
768 if (new_size == 0)
769 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200770 if (new_size > available - minwidth - 1)
771 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200772 if (new_size < wmw1)
773 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 /* if it doesn't fit in the current window, need win_equal() */
776 if (oldwin->w_width - new_size - 1 < p_wmw)
777 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000778
779 /* We don't like to take lines for the new window from a
780 * 'winfixwidth' window. Take them from a window to the left or right
781 * instead, if possible. */
782 if (oldwin->w_p_wfw)
783 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000784
785 /* Only make all windows the same width if one of them (except oldwin)
786 * is wider than one of the split windows. */
787 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
788 && oldwin->w_frame->fr_parent != NULL)
789 {
790 frp = oldwin->w_frame->fr_parent->fr_child;
791 while (frp != NULL)
792 {
793 if (frp->fr_win != oldwin && frp->fr_win != NULL
794 && (frp->fr_win->w_width > new_size
795 || frp->fr_win->w_width > oldwin->w_width
796 - new_size - STATUS_HEIGHT))
797 {
798 do_equal = TRUE;
799 break;
800 }
801 frp = frp->fr_next;
802 }
803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805 else
806#endif
807 {
808 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /*
811 * Check if we are able to split the current window and compute its
812 * height.
813 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200814 /* Current window requires at least 1 space. */
815 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
816 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200818 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200819 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200821 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200823 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200825 else if (p_ea)
826 {
827 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
828 prevfrp = oldwin->w_frame;
829 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
830 frp = frp->fr_parent)
831 {
832 if (frp->fr_layout == FR_COL)
833 for (frp2 = frp->fr_child; frp2 != NULL;
834 frp2 = frp2->fr_next)
835 if (frp2 != prevfrp)
836 minheight += frame_minheight(frp2, NOWIN);
837 prevfrp = frp;
838 }
839 available = topframe->fr_height;
840 needed += minheight;
841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else
843 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200844 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
845 available = oldwin->w_frame->fr_height;
846 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100848 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
850 EMSG(_(e_noroom));
851 return FAIL;
852 }
853 oldwin_height = oldwin->w_height;
854 if (need_status)
855 {
856 oldwin->w_status_height = STATUS_HEIGHT;
857 oldwin_height -= STATUS_HEIGHT;
858 }
859 if (new_size == 0)
860 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200861 if (new_size > available - minheight - STATUS_HEIGHT)
862 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200863 if (new_size < wmh1)
864 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 /* if it doesn't fit in the current window, need win_equal() */
867 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
868 do_equal = TRUE;
869
870 /* We don't like to take lines for the new window from a
871 * 'winfixheight' window. Take them from a window above or below
872 * instead, if possible. */
873 if (oldwin->w_p_wfh)
874 {
875 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
876 oldwin);
877 oldwin_height = oldwin->w_height;
878 if (need_status)
879 oldwin_height -= STATUS_HEIGHT;
880 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000881
882 /* Only make all windows the same height if one of them (except oldwin)
883 * is higher than one of the split windows. */
884 if (!do_equal && p_ea && size == 0
885#ifdef FEAT_VERTSPLIT
886 && *p_ead != 'h'
887#endif
888 && oldwin->w_frame->fr_parent != NULL)
889 {
890 frp = oldwin->w_frame->fr_parent->fr_child;
891 while (frp != NULL)
892 {
893 if (frp->fr_win != oldwin && frp->fr_win != NULL
894 && (frp->fr_win->w_height > new_size
895 || frp->fr_win->w_height > oldwin_height - new_size
896 - STATUS_HEIGHT))
897 {
898 do_equal = TRUE;
899 break;
900 }
901 frp = frp->fr_next;
902 }
903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905
906 /*
907 * allocate new window structure and link it in the window list
908 */
909 if ((flags & WSP_TOP) == 0
910 && ((flags & WSP_BOT)
911 || (flags & WSP_BELOW)
912 || (!(flags & WSP_ABOVE)
913 && (
914#ifdef FEAT_VERTSPLIT
915 (flags & WSP_VERT) ? p_spr :
916#endif
917 p_sb))))
918 {
919 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100920 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000921 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else
923 win_append(oldwin, wp);
924 }
925 else
926 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100927 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000928 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 else
930 win_append(oldwin->w_prev, wp);
931 }
932
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100933 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 {
935 if (wp == NULL)
936 return FAIL;
937
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000938 new_frame(wp);
939 if (wp->w_frame == NULL)
940 {
941 win_free(wp, NULL);
942 return FAIL;
943 }
944
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000945 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000946 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
948
949 /*
950 * Reorganise the tree of frames to insert the new window.
951 */
952 if (flags & (WSP_TOP | WSP_BOT))
953 {
954#ifdef FEAT_VERTSPLIT
955 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
956 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
957#else
958 if (topframe->fr_layout == FR_COL)
959#endif
960 {
961 curfrp = topframe->fr_child;
962 if (flags & WSP_BOT)
963 while (curfrp->fr_next != NULL)
964 curfrp = curfrp->fr_next;
965 }
966 else
967 curfrp = topframe;
968 before = (flags & WSP_TOP);
969 }
970 else
971 {
972 curfrp = oldwin->w_frame;
973 if (flags & WSP_BELOW)
974 before = FALSE;
975 else if (flags & WSP_ABOVE)
976 before = TRUE;
977 else
978#ifdef FEAT_VERTSPLIT
979 if (flags & WSP_VERT)
980 before = !p_spr;
981 else
982#endif
983 before = !p_sb;
984 }
985 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
986 {
987 /* Need to create a new frame in the tree to make a branch. */
988 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
989 *frp = *curfrp;
990 curfrp->fr_layout = layout;
991 frp->fr_parent = curfrp;
992 frp->fr_next = NULL;
993 frp->fr_prev = NULL;
994 curfrp->fr_child = frp;
995 curfrp->fr_win = NULL;
996 curfrp = frp;
997 if (frp->fr_win != NULL)
998 oldwin->w_frame = frp;
999 else
1000 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1001 frp->fr_parent = curfrp;
1002 }
1003
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001004 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001005 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001007 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 frp->fr_parent = curfrp->fr_parent;
1009
1010 /* Insert the new frame at the right place in the frame list. */
1011 if (before)
1012 frame_insert(curfrp, frp);
1013 else
1014 frame_append(curfrp, frp);
1015
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001016 /* Set w_fraction now so that the cursor keeps the same relative
1017 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001018 if (oldwin->w_height > 0)
1019 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001020 wp->w_fraction = oldwin->w_fraction;
1021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022#ifdef FEAT_VERTSPLIT
1023 if (flags & WSP_VERT)
1024 {
1025 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (need_status)
1028 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001029 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 oldwin->w_status_height = need_status;
1031 }
1032 if (flags & (WSP_TOP | WSP_BOT))
1033 {
1034 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001035 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001036 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 wp->w_status_height = (p_ls > 0);
1038 }
1039 else
1040 {
1041 /* height and row of new window is same as current window */
1042 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001043 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 wp->w_status_height = oldwin->w_status_height;
1045 }
1046 frp->fr_height = curfrp->fr_height;
1047
1048 /* "new_size" of the current window goes to the new window, use
1049 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001050 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (before)
1052 wp->w_vsep_width = 1;
1053 else
1054 {
1055 wp->w_vsep_width = oldwin->w_vsep_width;
1056 oldwin->w_vsep_width = 1;
1057 }
1058 if (flags & (WSP_TOP | WSP_BOT))
1059 {
1060 if (flags & WSP_BOT)
1061 frame_add_vsep(curfrp);
1062 /* Set width of neighbor frame */
1063 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001064 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1065 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 }
1067 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001068 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (before) /* new window left of current one */
1070 {
1071 wp->w_wincol = oldwin->w_wincol;
1072 oldwin->w_wincol += new_size + 1;
1073 }
1074 else /* new window right of current one */
1075 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1076 frame_fix_width(oldwin);
1077 frame_fix_width(wp);
1078 }
1079 else
1080#endif
1081 {
1082 /* width and column of new window is same as current window */
1083#ifdef FEAT_VERTSPLIT
1084 if (flags & (WSP_TOP | WSP_BOT))
1085 {
1086 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001087 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 wp->w_vsep_width = 0;
1089 }
1090 else
1091 {
1092 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001093 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 wp->w_vsep_width = oldwin->w_vsep_width;
1095 }
1096 frp->fr_width = curfrp->fr_width;
1097#endif
1098
1099 /* "new_size" of the current window goes to the new window, use
1100 * one row for the status line */
1101 win_new_height(wp, new_size);
1102 if (flags & (WSP_TOP | WSP_BOT))
1103 frame_new_height(curfrp, curfrp->fr_height
1104 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1105 else
1106 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1107 if (before) /* new window above current one */
1108 {
1109 wp->w_winrow = oldwin->w_winrow;
1110 wp->w_status_height = STATUS_HEIGHT;
1111 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1112 }
1113 else /* new window below current one */
1114 {
1115 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1116 wp->w_status_height = oldwin->w_status_height;
1117 oldwin->w_status_height = STATUS_HEIGHT;
1118 }
1119#ifdef FEAT_VERTSPLIT
1120 if (flags & WSP_BOT)
1121 frame_add_statusline(curfrp);
1122#endif
1123 frame_fix_height(wp);
1124 frame_fix_height(oldwin);
1125 }
1126
1127 if (flags & (WSP_TOP | WSP_BOT))
1128 (void)win_comp_pos();
1129
1130 /*
1131 * Both windows need redrawing
1132 */
1133 redraw_win_later(wp, NOT_VALID);
1134 wp->w_redr_status = TRUE;
1135 redraw_win_later(oldwin, NOT_VALID);
1136 oldwin->w_redr_status = TRUE;
1137
1138 if (need_status)
1139 {
1140 msg_row = Rows - 1;
1141 msg_col = sc_col;
1142 msg_clr_eos_force(); /* Old command/ruler may still be there */
1143 comp_col();
1144 msg_row = Rows - 1;
1145 msg_col = 0; /* put position back at start of line */
1146 }
1147
1148 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001149 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 */
1151 if (do_equal || dir != 0)
1152 win_equal(wp, TRUE,
1153#ifdef FEAT_VERTSPLIT
1154 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1155 : dir == 'h' ? 'b' :
1156#endif
1157 'v');
1158
1159 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1160 * size was given. */
1161#ifdef FEAT_VERTSPLIT
1162 if (flags & WSP_VERT)
1163 {
1164 i = p_wiw;
1165 if (size != 0)
1166 p_wiw = size;
1167
1168# ifdef FEAT_GUI
1169 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1170 if (gui.in_use)
1171 gui_init_which_components(NULL);
1172# endif
1173 }
1174 else
1175#endif
1176 {
1177 i = p_wh;
1178 if (size != 0)
1179 p_wh = size;
1180 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001181
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001182#ifdef FEAT_JUMPLIST
1183 /* Keep same changelist position in new window. */
1184 wp->w_changelistidx = oldwin->w_changelistidx;
1185#endif
1186
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001187 /*
1188 * make the new window the current window
1189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 win_enter(wp, FALSE);
1191#ifdef FEAT_VERTSPLIT
1192 if (flags & WSP_VERT)
1193 p_wiw = i;
1194 else
1195#endif
1196 p_wh = i;
1197
1198 return OK;
1199}
1200
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001201
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001202/*
1203 * Initialize window "newp" from window "oldp".
1204 * Used when splitting a window and when creating a new tab page.
1205 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001206 * WSP_NEWLOC may be specified in flags to prevent the location list from
1207 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001208 */
1209 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001210win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001211 win_T *newp;
1212 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001213 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001214{
1215 int i;
1216
1217 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001218#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001219 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001220#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001221 oldp->w_buffer->b_nwindows++;
1222 newp->w_cursor = oldp->w_cursor;
1223 newp->w_valid = 0;
1224 newp->w_curswant = oldp->w_curswant;
1225 newp->w_set_curswant = oldp->w_set_curswant;
1226 newp->w_topline = oldp->w_topline;
1227#ifdef FEAT_DIFF
1228 newp->w_topfill = oldp->w_topfill;
1229#endif
1230 newp->w_leftcol = oldp->w_leftcol;
1231 newp->w_pcmark = oldp->w_pcmark;
1232 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1233 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001234 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001235 newp->w_fraction = oldp->w_fraction;
1236 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1237#ifdef FEAT_JUMPLIST
1238 copy_jumplist(oldp, newp);
1239#endif
1240#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001241 if (flags & WSP_NEWLOC)
1242 {
1243 /* Don't copy the location list. */
1244 newp->w_llist = NULL;
1245 newp->w_llist_ref = NULL;
1246 }
1247 else
1248 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001249#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001250 newp->w_localdir = (oldp->w_localdir == NULL)
1251 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001252
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001253 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001254 for (i = 0; i < oldp->w_tagstacklen; i++)
1255 {
1256 newp->w_tagstack[i] = oldp->w_tagstack[i];
1257 if (newp->w_tagstack[i].tagname != NULL)
1258 newp->w_tagstack[i].tagname =
1259 vim_strsave(newp->w_tagstack[i].tagname);
1260 }
1261 newp->w_tagstackidx = oldp->w_tagstackidx;
1262 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001263#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001264 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001265#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001266
1267 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001268
Bram Moolenaara971b822011-09-14 14:43:25 +02001269#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001270 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001271#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001272}
1273
1274/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001275 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001276 * Only the essential things are copied.
1277 */
1278 static void
1279win_init_some(newp, oldp)
1280 win_T *newp;
1281 win_T *oldp;
1282{
1283 /* Use the same argument list. */
1284 newp->w_alist = oldp->w_alist;
1285 ++newp->w_alist->al_refcount;
1286 newp->w_arg_idx = oldp->w_arg_idx;
1287
1288 /* copy options from existing window */
1289 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001290}
1291
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292#endif /* FEAT_WINDOWS */
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294#if defined(FEAT_WINDOWS) || defined(PROTO)
1295/*
1296 * Check if "win" is a pointer to an existing window.
1297 */
1298 int
1299win_valid(win)
1300 win_T *win;
1301{
1302 win_T *wp;
1303
1304 if (win == NULL)
1305 return FALSE;
1306 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1307 if (wp == win)
1308 return TRUE;
1309 return FALSE;
1310}
1311
1312/*
1313 * Return the number of windows.
1314 */
1315 int
1316win_count()
1317{
1318 win_T *wp;
1319 int count = 0;
1320
1321 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1322 ++count;
1323 return count;
1324}
1325
1326/*
1327 * Make "count" windows on the screen.
1328 * Return actual number of windows on the screen.
1329 * Must be called when there is just one window, filling the whole screen
1330 * (excluding the command line).
1331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 int
1333make_windows(count, vertical)
1334 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001335 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336{
1337 int maxcount;
1338 int todo;
1339
1340#ifdef FEAT_VERTSPLIT
1341 if (vertical)
1342 {
1343 /* Each windows needs at least 'winminwidth' lines and a separator
1344 * column. */
1345 maxcount = (curwin->w_width + curwin->w_vsep_width
1346 - (p_wiw - p_wmw)) / (p_wmw + 1);
1347 }
1348 else
1349#endif
1350 {
1351 /* Each window needs at least 'winminheight' lines and a status line. */
1352 maxcount = (curwin->w_height + curwin->w_status_height
1353 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1354 }
1355
1356 if (maxcount < 2)
1357 maxcount = 2;
1358 if (count > maxcount)
1359 count = maxcount;
1360
1361 /*
1362 * add status line now, otherwise first window will be too big
1363 */
1364 if (count > 1)
1365 last_status(TRUE);
1366
1367#ifdef FEAT_AUTOCMD
1368 /*
1369 * Don't execute autocommands while creating the windows. Must do that
1370 * when putting the buffers in the windows.
1371 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001372 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373#endif
1374
1375 /* todo is number of windows left to create */
1376 for (todo = count - 1; todo > 0; --todo)
1377#ifdef FEAT_VERTSPLIT
1378 if (vertical)
1379 {
1380 if (win_split(curwin->w_width - (curwin->w_width - todo)
1381 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1382 break;
1383 }
1384 else
1385#endif
1386 {
1387 if (win_split(curwin->w_height - (curwin->w_height - todo
1388 * STATUS_HEIGHT) / (todo + 1)
1389 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1390 break;
1391 }
1392
1393#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001394 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#endif
1396
1397 /* return actual number of windows */
1398 return (count - todo);
1399}
1400
1401/*
1402 * Exchange current and next window
1403 */
1404 static void
1405win_exchange(Prenum)
1406 long Prenum;
1407{
1408 frame_T *frp;
1409 frame_T *frp2;
1410 win_T *wp;
1411 win_T *wp2;
1412 int temp;
1413
1414 if (lastwin == firstwin) /* just one window */
1415 {
1416 beep_flush();
1417 return;
1418 }
1419
1420#ifdef FEAT_GUI
1421 need_mouse_correct = TRUE;
1422#endif
1423
1424 /*
1425 * find window to exchange with
1426 */
1427 if (Prenum)
1428 {
1429 frp = curwin->w_frame->fr_parent->fr_child;
1430 while (frp != NULL && --Prenum > 0)
1431 frp = frp->fr_next;
1432 }
1433 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1434 frp = curwin->w_frame->fr_next;
1435 else /* Swap last window in row/col with previous */
1436 frp = curwin->w_frame->fr_prev;
1437
1438 /* We can only exchange a window with another window, not with a frame
1439 * containing windows. */
1440 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1441 return;
1442 wp = frp->fr_win;
1443
1444/*
1445 * 1. remove curwin from the list. Remember after which window it was in wp2
1446 * 2. insert curwin before wp in the list
1447 * if wp != wp2
1448 * 3. remove wp from the list
1449 * 4. insert wp after wp2
1450 * 5. exchange the status line height and vsep width.
1451 */
1452 wp2 = curwin->w_prev;
1453 frp2 = curwin->w_frame->fr_prev;
1454 if (wp->w_prev != curwin)
1455 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001456 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 frame_remove(curwin->w_frame);
1458 win_append(wp->w_prev, curwin);
1459 frame_insert(frp, curwin->w_frame);
1460 }
1461 if (wp != wp2)
1462 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001463 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 frame_remove(wp->w_frame);
1465 win_append(wp2, wp);
1466 if (frp2 == NULL)
1467 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1468 else
1469 frame_append(frp2, wp->w_frame);
1470 }
1471 temp = curwin->w_status_height;
1472 curwin->w_status_height = wp->w_status_height;
1473 wp->w_status_height = temp;
1474#ifdef FEAT_VERTSPLIT
1475 temp = curwin->w_vsep_width;
1476 curwin->w_vsep_width = wp->w_vsep_width;
1477 wp->w_vsep_width = temp;
1478
1479 /* If the windows are not in the same frame, exchange the sizes to avoid
1480 * messing up the window layout. Otherwise fix the frame sizes. */
1481 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1482 {
1483 temp = curwin->w_height;
1484 curwin->w_height = wp->w_height;
1485 wp->w_height = temp;
1486 temp = curwin->w_width;
1487 curwin->w_width = wp->w_width;
1488 wp->w_width = temp;
1489 }
1490 else
1491 {
1492 frame_fix_height(curwin);
1493 frame_fix_height(wp);
1494 frame_fix_width(curwin);
1495 frame_fix_width(wp);
1496 }
1497#endif
1498
1499 (void)win_comp_pos(); /* recompute window positions */
1500
1501 win_enter(wp, TRUE);
1502 redraw_later(CLEAR);
1503}
1504
1505/*
1506 * rotate windows: if upwards TRUE the second window becomes the first one
1507 * if upwards FALSE the first window becomes the second one
1508 */
1509 static void
1510win_rotate(upwards, count)
1511 int upwards;
1512 int count;
1513{
1514 win_T *wp1;
1515 win_T *wp2;
1516 frame_T *frp;
1517 int n;
1518
1519 if (firstwin == lastwin) /* nothing to do */
1520 {
1521 beep_flush();
1522 return;
1523 }
1524
1525#ifdef FEAT_GUI
1526 need_mouse_correct = TRUE;
1527#endif
1528
1529#ifdef FEAT_VERTSPLIT
1530 /* Check if all frames in this row/col have one window. */
1531 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1532 frp = frp->fr_next)
1533 if (frp->fr_win == NULL)
1534 {
1535 EMSG(_("E443: Cannot rotate when another window is split"));
1536 return;
1537 }
1538#endif
1539
1540 while (count--)
1541 {
1542 if (upwards) /* first window becomes last window */
1543 {
1544 /* remove first window/frame from the list */
1545 frp = curwin->w_frame->fr_parent->fr_child;
1546 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001547 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 frame_remove(frp);
1549
1550 /* find last frame and append removed window/frame after it */
1551 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1552 ;
1553 win_append(frp->fr_win, wp1);
1554 frame_append(frp, wp1->w_frame);
1555
1556 wp2 = frp->fr_win; /* previously last window */
1557 }
1558 else /* last window becomes first window */
1559 {
1560 /* find last window/frame in the list and remove it */
1561 for (frp = curwin->w_frame; frp->fr_next != NULL;
1562 frp = frp->fr_next)
1563 ;
1564 wp1 = frp->fr_win;
1565 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001566 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 frame_remove(frp);
1568
1569 /* append the removed window/frame before the first in the list */
1570 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1571 frame_insert(frp->fr_parent->fr_child, frp);
1572 }
1573
1574 /* exchange status height and vsep width of old and new last window */
1575 n = wp2->w_status_height;
1576 wp2->w_status_height = wp1->w_status_height;
1577 wp1->w_status_height = n;
1578 frame_fix_height(wp1);
1579 frame_fix_height(wp2);
1580#ifdef FEAT_VERTSPLIT
1581 n = wp2->w_vsep_width;
1582 wp2->w_vsep_width = wp1->w_vsep_width;
1583 wp1->w_vsep_width = n;
1584 frame_fix_width(wp1);
1585 frame_fix_width(wp2);
1586#endif
1587
1588 /* recompute w_winrow and w_wincol for all windows */
1589 (void)win_comp_pos();
1590 }
1591
1592 redraw_later(CLEAR);
1593}
1594
1595/*
1596 * Move the current window to the very top/bottom/left/right of the screen.
1597 */
1598 static void
1599win_totop(size, flags)
1600 int size;
1601 int flags;
1602{
1603 int dir;
1604 int height = curwin->w_height;
1605
1606 if (lastwin == firstwin)
1607 {
1608 beep_flush();
1609 return;
1610 }
1611
1612 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001613 (void)winframe_remove(curwin, &dir, NULL);
1614 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 last_status(FALSE); /* may need to remove last status line */
1616 (void)win_comp_pos(); /* recompute window positions */
1617
1618 /* Split a window on the desired side and put the window there. */
1619 (void)win_split_ins(size, flags, curwin, dir);
1620 if (!(flags & WSP_VERT))
1621 {
1622 win_setheight(height);
1623 if (p_ea)
1624 win_equal(curwin, TRUE, 'v');
1625 }
1626
1627#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1628 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1629 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001630 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632}
1633
1634/*
1635 * Move window "win1" to below/right of "win2" and make "win1" the current
1636 * window. Only works within the same frame!
1637 */
1638 void
1639win_move_after(win1, win2)
1640 win_T *win1, *win2;
1641{
1642 int height;
1643
1644 /* check if the arguments are reasonable */
1645 if (win1 == win2)
1646 return;
1647
1648 /* check if there is something to do */
1649 if (win2->w_next != win1)
1650 {
1651 /* may need move the status line/vertical separator of the last window
1652 * */
1653 if (win1 == lastwin)
1654 {
1655 height = win1->w_prev->w_status_height;
1656 win1->w_prev->w_status_height = win1->w_status_height;
1657 win1->w_status_height = height;
1658#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001659 if (win1->w_prev->w_vsep_width == 1)
1660 {
1661 /* Remove the vertical separator from the last-but-one window,
1662 * add it to the last window. Adjust the frame widths. */
1663 win1->w_prev->w_vsep_width = 0;
1664 win1->w_prev->w_frame->fr_width -= 1;
1665 win1->w_vsep_width = 1;
1666 win1->w_frame->fr_width += 1;
1667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668#endif
1669 }
1670 else if (win2 == lastwin)
1671 {
1672 height = win1->w_status_height;
1673 win1->w_status_height = win2->w_status_height;
1674 win2->w_status_height = height;
1675#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001676 if (win1->w_vsep_width == 1)
1677 {
1678 /* Remove the vertical separator from win1, add it to the last
1679 * window, win2. Adjust the frame widths. */
1680 win2->w_vsep_width = 1;
1681 win2->w_frame->fr_width += 1;
1682 win1->w_vsep_width = 0;
1683 win1->w_frame->fr_width -= 1;
1684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#endif
1686 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001687 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 frame_remove(win1->w_frame);
1689 win_append(win2, win1);
1690 frame_append(win2->w_frame, win1->w_frame);
1691
1692 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1693 redraw_later(NOT_VALID);
1694 }
1695 win_enter(win1, FALSE);
1696}
1697
1698/*
1699 * Make all windows the same height.
1700 * 'next_curwin' will soon be the current window, make sure it has enough
1701 * rows.
1702 */
1703 void
1704win_equal(next_curwin, current, dir)
1705 win_T *next_curwin; /* pointer to current window to be or NULL */
1706 int current; /* do only frame with current window */
1707 int dir; /* 'v' for vertically, 'h' for horizontally,
1708 'b' for both, 0 for using p_ead */
1709{
1710 if (dir == 0)
1711#ifdef FEAT_VERTSPLIT
1712 dir = *p_ead;
1713#else
1714 dir = 'b';
1715#endif
1716 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001717 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001718 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719}
1720
1721/*
1722 * Set a frame to a new position and height, spreading the available room
1723 * equally over contained frames.
1724 * The window "next_curwin" (if not NULL) should at least get the size from
1725 * 'winheight' and 'winwidth' if possible.
1726 */
1727 static void
1728win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1729 win_T *next_curwin; /* pointer to current window to be or NULL */
1730 int current; /* do only frame with current window */
1731 frame_T *topfr; /* frame to set size off */
1732 int dir; /* 'v', 'h' or 'b', see win_equal() */
1733 int col; /* horizontal position for frame */
1734 int row; /* vertical position for frame */
1735 int width; /* new width of frame */
1736 int height; /* new height of frame */
1737{
1738 int n, m;
1739 int extra_sep = 0;
1740 int wincount, totwincount = 0;
1741 frame_T *fr;
1742 int next_curwin_size = 0;
1743 int room = 0;
1744 int new_size;
1745 int has_next_curwin = 0;
1746 int hnc;
1747
1748 if (topfr->fr_layout == FR_LEAF)
1749 {
1750 /* Set the width/height of this frame.
1751 * Redraw when size or position changes */
1752 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1753#ifdef FEAT_VERTSPLIT
1754 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1755#endif
1756 )
1757 {
1758 topfr->fr_win->w_winrow = row;
1759 frame_new_height(topfr, height, FALSE, FALSE);
1760#ifdef FEAT_VERTSPLIT
1761 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001762 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#endif
1764 redraw_all_later(CLEAR);
1765 }
1766 }
1767#ifdef FEAT_VERTSPLIT
1768 else if (topfr->fr_layout == FR_ROW)
1769 {
1770 topfr->fr_width = width;
1771 topfr->fr_height = height;
1772
1773 if (dir != 'v') /* equalize frame widths */
1774 {
1775 /* Compute the maximum number of windows horizontally in this
1776 * frame. */
1777 n = frame_minwidth(topfr, NOWIN);
1778 /* add one for the rightmost window, it doesn't have a separator */
1779 if (col + width == Columns)
1780 extra_sep = 1;
1781 else
1782 extra_sep = 0;
1783 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001784 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001786 /*
1787 * Compute width for "next_curwin" window and room available for
1788 * other windows.
1789 * "m" is the minimal width when counting p_wiw for "next_curwin".
1790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 m = frame_minwidth(topfr, next_curwin);
1792 room = width - m;
1793 if (room < 0)
1794 {
1795 next_curwin_size = p_wiw + room;
1796 room = 0;
1797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 else
1799 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001800 next_curwin_size = -1;
1801 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1802 {
1803 /* If 'winfixwidth' set keep the window width if
1804 * possible.
1805 * Watch out for this window being the next_curwin. */
1806 if (frame_fixed_width(fr))
1807 {
1808 n = frame_minwidth(fr, NOWIN);
1809 new_size = fr->fr_width;
1810 if (frame_has_win(fr, next_curwin))
1811 {
1812 room += p_wiw - p_wmw;
1813 next_curwin_size = 0;
1814 if (new_size < p_wiw)
1815 new_size = p_wiw;
1816 }
1817 else
1818 /* These windows don't use up room. */
1819 totwincount -= (n + (fr->fr_next == NULL
1820 ? extra_sep : 0)) / (p_wmw + 1);
1821 room -= new_size - n;
1822 if (room < 0)
1823 {
1824 new_size += room;
1825 room = 0;
1826 }
1827 fr->fr_newwidth = new_size;
1828 }
1829 }
1830 if (next_curwin_size == -1)
1831 {
1832 if (!has_next_curwin)
1833 next_curwin_size = 0;
1834 else if (totwincount > 1
1835 && (room + (totwincount - 2))
1836 / (totwincount - 1) > p_wiw)
1837 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001838 /* Can make all windows wider than 'winwidth', spread
1839 * the room equally. */
1840 next_curwin_size = (room + p_wiw
1841 + (totwincount - 1) * p_wmw
1842 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001843 room -= next_curwin_size - p_wiw;
1844 }
1845 else
1846 next_curwin_size = p_wiw;
1847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001849
1850 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 --totwincount; /* don't count curwin */
1852 }
1853
1854 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1855 {
1856 n = m = 0;
1857 wincount = 1;
1858 if (fr->fr_next == NULL)
1859 /* last frame gets all that remains (avoid roundoff error) */
1860 new_size = width;
1861 else if (dir == 'v')
1862 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001863 else if (frame_fixed_width(fr))
1864 {
1865 new_size = fr->fr_newwidth;
1866 wincount = 0; /* doesn't count as a sizeable window */
1867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 else
1869 {
1870 /* Compute the maximum number of windows horiz. in "fr". */
1871 n = frame_minwidth(fr, NOWIN);
1872 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1873 / (p_wmw + 1);
1874 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001875 if (has_next_curwin)
1876 hnc = frame_has_win(fr, next_curwin);
1877 else
1878 hnc = FALSE;
1879 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001881 if (totwincount == 0)
1882 new_size = room;
1883 else
1884 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001886 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 next_curwin_size -= p_wiw - (m - n);
1889 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001890 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001892 else
1893 room -= new_size;
1894 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001897 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 * window, unless equalizing all frames. */
1899 if (!current || dir != 'v' || topfr->fr_parent != NULL
1900 || (new_size != fr->fr_width)
1901 || frame_has_win(fr, next_curwin))
1902 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001903 new_size, height);
1904 col += new_size;
1905 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 totwincount -= wincount;
1907 }
1908 }
1909#endif
1910 else /* topfr->fr_layout == FR_COL */
1911 {
1912#ifdef FEAT_VERTSPLIT
1913 topfr->fr_width = width;
1914#endif
1915 topfr->fr_height = height;
1916
1917 if (dir != 'h') /* equalize frame heights */
1918 {
1919 /* Compute maximum number of windows vertically in this frame. */
1920 n = frame_minheight(topfr, NOWIN);
1921 /* add one for the bottom window if it doesn't have a statusline */
1922 if (row + height == cmdline_row && p_ls == 0)
1923 extra_sep = 1;
1924 else
1925 extra_sep = 0;
1926 totwincount = (n + extra_sep) / (p_wmh + 1);
1927 has_next_curwin = frame_has_win(topfr, next_curwin);
1928
1929 /*
1930 * Compute height for "next_curwin" window and room available for
1931 * other windows.
1932 * "m" is the minimal height when counting p_wh for "next_curwin".
1933 */
1934 m = frame_minheight(topfr, next_curwin);
1935 room = height - m;
1936 if (room < 0)
1937 {
1938 /* The room is less then 'winheight', use all space for the
1939 * current window. */
1940 next_curwin_size = p_wh + room;
1941 room = 0;
1942 }
1943 else
1944 {
1945 next_curwin_size = -1;
1946 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1947 {
1948 /* If 'winfixheight' set keep the window height if
1949 * possible.
1950 * Watch out for this window being the next_curwin. */
1951 if (frame_fixed_height(fr))
1952 {
1953 n = frame_minheight(fr, NOWIN);
1954 new_size = fr->fr_height;
1955 if (frame_has_win(fr, next_curwin))
1956 {
1957 room += p_wh - p_wmh;
1958 next_curwin_size = 0;
1959 if (new_size < p_wh)
1960 new_size = p_wh;
1961 }
1962 else
1963 /* These windows don't use up room. */
1964 totwincount -= (n + (fr->fr_next == NULL
1965 ? extra_sep : 0)) / (p_wmh + 1);
1966 room -= new_size - n;
1967 if (room < 0)
1968 {
1969 new_size += room;
1970 room = 0;
1971 }
1972 fr->fr_newheight = new_size;
1973 }
1974 }
1975 if (next_curwin_size == -1)
1976 {
1977 if (!has_next_curwin)
1978 next_curwin_size = 0;
1979 else if (totwincount > 1
1980 && (room + (totwincount - 2))
1981 / (totwincount - 1) > p_wh)
1982 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001983 /* can make all windows higher than 'winheight',
1984 * spread the room equally. */
1985 next_curwin_size = (room + p_wh
1986 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 + (totwincount - 1)) / totwincount;
1988 room -= next_curwin_size - p_wh;
1989 }
1990 else
1991 next_curwin_size = p_wh;
1992 }
1993 }
1994
1995 if (has_next_curwin)
1996 --totwincount; /* don't count curwin */
1997 }
1998
1999 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2000 {
2001 n = m = 0;
2002 wincount = 1;
2003 if (fr->fr_next == NULL)
2004 /* last frame gets all that remains (avoid roundoff error) */
2005 new_size = height;
2006 else if (dir == 'h')
2007 new_size = fr->fr_height;
2008 else if (frame_fixed_height(fr))
2009 {
2010 new_size = fr->fr_newheight;
2011 wincount = 0; /* doesn't count as a sizeable window */
2012 }
2013 else
2014 {
2015 /* Compute the maximum number of windows vert. in "fr". */
2016 n = frame_minheight(fr, NOWIN);
2017 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2018 / (p_wmh + 1);
2019 m = frame_minheight(fr, next_curwin);
2020 if (has_next_curwin)
2021 hnc = frame_has_win(fr, next_curwin);
2022 else
2023 hnc = FALSE;
2024 if (hnc) /* don't count next_curwin */
2025 --wincount;
2026 if (totwincount == 0)
2027 new_size = room;
2028 else
2029 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2030 / totwincount;
2031 if (hnc) /* add next_curwin size */
2032 {
2033 next_curwin_size -= p_wh - (m - n);
2034 new_size += next_curwin_size;
2035 room -= new_size - next_curwin_size;
2036 }
2037 else
2038 room -= new_size;
2039 new_size += n;
2040 }
2041 /* Skip frame that is full width when splitting or closing a
2042 * window, unless equalizing all frames. */
2043 if (!current || dir != 'h' || topfr->fr_parent != NULL
2044 || (new_size != fr->fr_height)
2045 || frame_has_win(fr, next_curwin))
2046 win_equal_rec(next_curwin, current, fr, dir, col, row,
2047 width, new_size);
2048 row += new_size;
2049 height -= new_size;
2050 totwincount -= wincount;
2051 }
2052 }
2053}
2054
2055/*
2056 * close all windows for buffer 'buf'
2057 */
2058 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002059close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002061 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002063 win_T *wp;
2064 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002065 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002068
2069 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002071 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2072#ifdef FEAT_AUTOCMD
2073 && !(wp->w_closing || wp->w_buffer->b_closing)
2074#endif
2075 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002077 win_close(wp, FALSE);
2078
2079 /* Start all over, autocommands may change the window layout. */
2080 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002083 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002085
2086 /* Also check windows in other tab pages. */
2087 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2088 {
2089 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002090 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002091 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002092 if (wp->w_buffer == buf
2093#ifdef FEAT_AUTOCMD
2094 && !(wp->w_closing || wp->w_buffer->b_closing)
2095#endif
2096 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002097 {
2098 win_close_othertab(wp, FALSE, tp);
2099
2100 /* Start all over, the tab page may be closed and
2101 * autocommands may change the window layout. */
2102 nexttp = first_tabpage;
2103 break;
2104 }
2105 }
2106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002108
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002109 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002110 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002111 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112}
2113
2114/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002115 * Return TRUE if the current window is the only window that exists (ignoring
2116 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002117 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002118 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002119 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002120last_window()
2121{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002122 return (one_window() && first_tabpage->tp_next == NULL);
2123}
2124
2125/*
2126 * Return TRUE if there is only one window other than "aucmd_win" in the
2127 * current tab page.
2128 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002129 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002130one_window()
2131{
2132#ifdef FEAT_AUTOCMD
2133 win_T *wp;
2134 int seen_one = FALSE;
2135
2136 FOR_ALL_WINDOWS(wp)
2137 {
2138 if (wp != aucmd_win)
2139 {
2140 if (seen_one)
2141 return FALSE;
2142 seen_one = TRUE;
2143 }
2144 }
2145 return TRUE;
2146#else
2147 return firstwin == lastwin;
2148#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002149}
2150
2151/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002152 * Close the possibly last window in a tab page.
2153 * Returns TRUE when the window was closed already.
2154 */
2155 static int
2156close_last_window_tabpage(win, free_buf, prev_curtab)
2157 win_T *win;
2158 int free_buf;
2159 tabpage_T *prev_curtab;
2160{
2161 if (firstwin == lastwin)
2162 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002163#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002164 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002165#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002166
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002167 /*
2168 * Closing the last window in a tab page. First go to another tab
2169 * page and then close the window and the tab page. This avoids that
2170 * curwin and curtab are invalid while we are freeing memory, they may
2171 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002172 * Don't trigger autocommands yet, they may use wrong values, so do
2173 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002174 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002175 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002176 redraw_tabline = TRUE;
2177
2178 /* Safety check: Autocommands may have closed the window when jumping
2179 * to the other tab page. */
2180 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2181 {
2182 int h = tabline_height();
2183
2184 win_close_othertab(win, free_buf, prev_curtab);
2185 if (h != tabline_height())
2186 shell_new_rows();
2187 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002188 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2189 * that now. */
2190#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002191 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002192 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2193 if (old_curbuf != curbuf)
2194 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002195#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002196 return TRUE;
2197 }
2198 return FALSE;
2199}
2200
2201/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002202 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 * If "free_buf" is TRUE related buffer may be unloaded.
2204 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002205 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002206 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002208 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209win_close(win, free_buf)
2210 win_T *win;
2211 int free_buf;
2212{
2213 win_T *wp;
2214#ifdef FEAT_AUTOCMD
2215 int other_buffer = FALSE;
2216#endif
2217 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 int dir;
2219 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002220 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002222 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002225 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002228#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002229 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002230 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002231 if (win == aucmd_win)
2232 {
2233 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002234 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002235 }
2236 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2237 {
2238 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002239 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002240 }
2241#endif
2242
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002243 /* When closing the last window in a tab page first go to another tab page
2244 * and then close the window and the tab page to avoid that curwin and
2245 * curtab are invalid while we are freeing memory. */
2246 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002247 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 /* When closing the help window, try restoring a snapshot after closing
2250 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002251 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 help_window = TRUE;
2253 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002254 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256#ifdef FEAT_AUTOCMD
2257 if (win == curwin)
2258 {
2259 /*
2260 * Guess which window is going to be the new current window.
2261 * This may change because of the autocommands (sigh).
2262 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002263 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
2265 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002266 * Be careful: If autocommands delete the window or cause this window
2267 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 */
2269 if (wp->w_buffer != curbuf)
2270 {
2271 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002272 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002274 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002275 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002276 win->w_closing = FALSE;
2277 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002278 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002280 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002282 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002283 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002284 win->w_closing = FALSE;
2285 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002286 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287# ifdef FEAT_EVAL
2288 /* autocmds may abort script processing */
2289 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291# endif
2292 }
2293#endif
2294
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002295#ifdef FEAT_GUI
2296 /* Avoid trouble with scrollbars that are going to be deleted in
2297 * win_free(). */
2298 if (gui.in_use)
2299 out_flush();
2300#endif
2301
Bram Moolenaara971b822011-09-14 14:43:25 +02002302#ifdef FEAT_SYN_HL
2303 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002304 if (win->w_buffer != NULL)
2305 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002306#endif
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 /*
2309 * Close the link to the buffer.
2310 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002311 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002312 {
2313#ifdef FEAT_AUTOCMD
2314 win->w_closing = TRUE;
2315#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002316 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002317#ifdef FEAT_AUTOCMD
2318 if (win_valid(win))
2319 win->w_closing = FALSE;
2320#endif
2321 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002322
Bram Moolenaar802418d2013-01-17 14:00:11 +01002323 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2324 && (last_window() || curtab != prev_curtab
2325 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002326 {
2327 /* Autocommands have close all windows, quit now. Restore
2328 * curwin->w_buffer, otherwise writing viminfo may fail. */
2329 if (curwin->w_buffer == NULL)
2330 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002331 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002332 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002335 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002336 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002337 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
Bram Moolenaara971b822011-09-14 14:43:25 +02002340 /* Free the memory used for the window and get the window that received
2341 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002342 wp = win_free_mem(win, &dir, NULL);
2343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 /* Make sure curwin isn't invalid. It can cause severe trouble when
2345 * printing an error message. For win_equal() curbuf needs to be valid
2346 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002347 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
2349 curwin = wp;
2350#ifdef FEAT_QUICKFIX
2351 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2352 {
2353 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002354 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 * finding another window to go to.
2356 */
2357 for (;;)
2358 {
2359 if (wp->w_next == NULL)
2360 wp = firstwin;
2361 else
2362 wp = wp->w_next;
2363 if (wp == curwin)
2364 break;
2365 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2366 {
2367 curwin = wp;
2368 break;
2369 }
2370 }
2371 }
2372#endif
2373 curbuf = curwin->w_buffer;
2374 close_curwin = TRUE;
2375 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002376 if (p_ea
2377#ifdef FEAT_VERTSPLIT
2378 && (*p_ead == 'b' || *p_ead == dir)
2379#endif
2380 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 win_equal(curwin, TRUE,
2382#ifdef FEAT_VERTSPLIT
2383 dir
2384#else
2385 0
2386#endif
2387 );
2388 else
2389 win_comp_pos();
2390 if (close_curwin)
2391 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002392 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393#ifdef FEAT_AUTOCMD
2394 if (other_buffer)
2395 /* careful: after this wp and win may be invalid! */
2396 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2397#endif
2398 }
2399
2400 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002401 * If last window has a status line now and we don't want one,
2402 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 */
2404 last_status(FALSE);
2405
2406 /* After closing the help window, try restoring the window layout from
2407 * before it was opened. */
2408 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002409 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2412 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2413 if (gui.in_use && !win_hasvertsplit())
2414 gui_init_which_components(NULL);
2415#endif
2416
2417 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002418 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419}
2420
2421/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002422 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002423 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002424 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002425 * Caller must check if buffer is hidden and whether the tabline needs to be
2426 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002427 */
2428 void
2429win_close_othertab(win, free_buf, tp)
2430 win_T *win;
2431 int free_buf;
2432 tabpage_T *tp;
2433{
2434 win_T *wp;
2435 int dir;
2436 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002437 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002438
Bram Moolenaar362ce482012-06-06 19:02:45 +02002439#ifdef FEAT_AUTOCMD
2440 if (win->w_closing || win->w_buffer->b_closing)
2441 return; /* window is already being closed */
2442#endif
2443
Bram Moolenaarf740b292006-02-16 22:11:02 +00002444 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002445 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002446
2447 /* Careful: Autocommands may have closed the tab page or made it the
2448 * current tab page. */
2449 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2450 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002451 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002452 return;
2453
2454 /* Autocommands may have closed the window already. */
2455 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2456 ;
2457 if (wp == NULL)
2458 return;
2459
Bram Moolenaarf740b292006-02-16 22:11:02 +00002460 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002461 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002462 {
2463 if (tp == first_tabpage)
2464 first_tabpage = tp->tp_next;
2465 else
2466 {
2467 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2468 ptp = ptp->tp_next)
2469 ;
2470 if (ptp == NULL)
2471 {
2472 EMSG2(_(e_intern2), "win_close_othertab()");
2473 return;
2474 }
2475 ptp->tp_next = tp->tp_next;
2476 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002477 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002478 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002479
2480 /* Free the memory used for the window. */
2481 win_free_mem(win, &dir, tp);
2482
2483 if (free_tp)
2484 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002485}
2486
2487/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002488 * Free the memory used for a window.
2489 * Returns a pointer to the window that got the freed up space.
2490 */
2491 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002492win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002493 win_T *win;
2494 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002495 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002496{
2497 frame_T *frp;
2498 win_T *wp;
2499
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002500 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002501 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002502 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002503 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002504 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002505
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002506 /* When deleting the current window of another tab page select a new
2507 * current window. */
2508 if (tp != NULL && win == tp->tp_curwin)
2509 tp->tp_curwin = wp;
2510
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002511 return wp;
2512}
2513
2514#if defined(EXITFREE) || defined(PROTO)
2515 void
2516win_free_all()
2517{
2518 int dummy;
2519
Bram Moolenaarf740b292006-02-16 22:11:02 +00002520# ifdef FEAT_WINDOWS
2521 while (first_tabpage->tp_next != NULL)
2522 tabpage_close(TRUE);
2523# endif
2524
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002525# ifdef FEAT_AUTOCMD
2526 if (aucmd_win != NULL)
2527 {
2528 (void)win_free_mem(aucmd_win, &dummy, NULL);
2529 aucmd_win = NULL;
2530 }
2531# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002532
2533 while (firstwin != NULL)
2534 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002535
2536 /* No window should be used after this. Set curwin to NULL to crash
2537 * instead of using freed memory. */
2538 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002539}
2540#endif
2541
2542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 * Remove a window and its frame from the tree of frames.
2544 * Returns a pointer to the window that got the freed up space.
2545 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002546 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002547winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002549 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002550 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 frame_T *frp, *frp2, *frp3;
2553 frame_T *frp_close = win->w_frame;
2554 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
2556 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002557 * If there is only one window there is nothing to remove.
2558 */
2559 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2560 return NULL;
2561
2562 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 * Remove the window from its frame.
2564 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002565 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 wp = frame2win(frp2);
2567
2568 /* Remove this frame from the list of frames. */
2569 frame_remove(frp_close);
2570
2571#ifdef FEAT_VERTSPLIT
2572 if (frp_close->fr_parent->fr_layout == FR_COL)
2573 {
2574#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002575 /* When 'winfixheight' is set, try to find another frame in the column
2576 * (as close to the closed frame as possible) to distribute the height
2577 * to. */
2578 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2579 {
2580 frp = frp_close->fr_prev;
2581 frp3 = frp_close->fr_next;
2582 while (frp != NULL || frp3 != NULL)
2583 {
2584 if (frp != NULL)
2585 {
2586 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2587 {
2588 frp2 = frp;
2589 wp = frp->fr_win;
2590 break;
2591 }
2592 frp = frp->fr_prev;
2593 }
2594 if (frp3 != NULL)
2595 {
2596 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2597 {
2598 frp2 = frp3;
2599 wp = frp3->fr_win;
2600 break;
2601 }
2602 frp3 = frp3->fr_next;
2603 }
2604 }
2605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2607 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608#ifdef FEAT_VERTSPLIT
2609 *dirp = 'v';
2610 }
2611 else
2612 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002613 /* When 'winfixwidth' is set, try to find another frame in the column
2614 * (as close to the closed frame as possible) to distribute the width
2615 * to. */
2616 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2617 {
2618 frp = frp_close->fr_prev;
2619 frp3 = frp_close->fr_next;
2620 while (frp != NULL || frp3 != NULL)
2621 {
2622 if (frp != NULL)
2623 {
2624 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2625 {
2626 frp2 = frp;
2627 wp = frp->fr_win;
2628 break;
2629 }
2630 frp = frp->fr_prev;
2631 }
2632 if (frp3 != NULL)
2633 {
2634 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2635 {
2636 frp2 = frp3;
2637 wp = frp3->fr_win;
2638 break;
2639 }
2640 frp3 = frp3->fr_next;
2641 }
2642 }
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002645 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 *dirp = 'h';
2647 }
2648#endif
2649
2650 /* If rows/columns go to a window below/right its positions need to be
2651 * updated. Can only be done after the sizes have been updated. */
2652 if (frp2 == frp_close->fr_next)
2653 {
2654 int row = win->w_winrow;
2655 int col = W_WINCOL(win);
2656
2657 frame_comp_pos(frp2, &row, &col);
2658 }
2659
2660 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2661 {
2662 /* There is no other frame in this list, move its info to the parent
2663 * and remove it. */
2664 frp2->fr_parent->fr_layout = frp2->fr_layout;
2665 frp2->fr_parent->fr_child = frp2->fr_child;
2666 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2667 frp->fr_parent = frp2->fr_parent;
2668 frp2->fr_parent->fr_win = frp2->fr_win;
2669 if (frp2->fr_win != NULL)
2670 frp2->fr_win->w_frame = frp2->fr_parent;
2671 frp = frp2->fr_parent;
2672 vim_free(frp2);
2673
2674 frp2 = frp->fr_parent;
2675 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2676 {
2677 /* The frame above the parent has the same layout, have to merge
2678 * the frames into this list. */
2679 if (frp2->fr_child == frp)
2680 frp2->fr_child = frp->fr_child;
2681 frp->fr_child->fr_prev = frp->fr_prev;
2682 if (frp->fr_prev != NULL)
2683 frp->fr_prev->fr_next = frp->fr_child;
2684 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2685 {
2686 frp3->fr_parent = frp2;
2687 if (frp3->fr_next == NULL)
2688 {
2689 frp3->fr_next = frp->fr_next;
2690 if (frp->fr_next != NULL)
2691 frp->fr_next->fr_prev = frp3;
2692 break;
2693 }
2694 }
2695 vim_free(frp);
2696 }
2697 }
2698
2699 return wp;
2700}
2701
2702/*
2703 * Find out which frame is going to get the freed up space when "win" is
2704 * closed.
2705 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2706 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2707 * This makes opening a window and closing it immediately keep the same window
2708 * layout.
2709 */
2710 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002711win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002713 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
2715 frame_T *frp;
2716 int b;
2717
Bram Moolenaarf740b292006-02-16 22:11:02 +00002718 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002719 /* Last window in this tab page, will go to next tab page. */
2720 return alt_tabpage()->tp_curwin->w_frame;
2721
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 frp = win->w_frame;
2723#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002724 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 b = p_spr;
2726 else
2727#endif
2728 b = p_sb;
2729 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2730 return frp->fr_next;
2731 return frp->fr_prev;
2732}
2733
2734/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002735 * Return the tabpage that will be used if the current one is closed.
2736 */
2737 static tabpage_T *
2738alt_tabpage()
2739{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002740 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002741
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002742 /* Use the next tab page if possible. */
2743 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002744 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002745
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002746 /* Find the last but one tab page. */
2747 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2748 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002749 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002750}
2751
2752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 * Find the left-upper window in frame "frp".
2754 */
2755 static win_T *
2756frame2win(frp)
2757 frame_T *frp;
2758{
2759 while (frp->fr_win == NULL)
2760 frp = frp->fr_child;
2761 return frp->fr_win;
2762}
2763
2764/*
2765 * Return TRUE if frame "frp" contains window "wp".
2766 */
2767 static int
2768frame_has_win(frp, wp)
2769 frame_T *frp;
2770 win_T *wp;
2771{
2772 frame_T *p;
2773
2774 if (frp->fr_layout == FR_LEAF)
2775 return frp->fr_win == wp;
2776
2777 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2778 if (frame_has_win(p, wp))
2779 return TRUE;
2780 return FALSE;
2781}
2782
2783/*
2784 * Set a new height for a frame. Recursively sets the height for contained
2785 * frames and windows. Caller must take care of positions.
2786 */
2787 static void
2788frame_new_height(topfrp, height, topfirst, wfh)
2789 frame_T *topfrp;
2790 int height;
2791 int topfirst; /* resize topmost contained frame first */
2792 int wfh; /* obey 'winfixheight' when there is a choice;
2793 may cause the height not to be set */
2794{
2795 frame_T *frp;
2796 int extra_lines;
2797 int h;
2798
2799 if (topfrp->fr_win != NULL)
2800 {
2801 /* Simple case: just one window. */
2802 win_new_height(topfrp->fr_win,
2803 height - topfrp->fr_win->w_status_height);
2804 }
2805#ifdef FEAT_VERTSPLIT
2806 else if (topfrp->fr_layout == FR_ROW)
2807 {
2808 do
2809 {
2810 /* All frames in this row get the same new height. */
2811 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2812 {
2813 frame_new_height(frp, height, topfirst, wfh);
2814 if (frp->fr_height > height)
2815 {
2816 /* Could not fit the windows, make the whole row higher. */
2817 height = frp->fr_height;
2818 break;
2819 }
2820 }
2821 }
2822 while (frp != NULL);
2823 }
2824#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002825 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
2827 /* Complicated case: Resize a column of frames. Resize the bottom
2828 * frame first, frames above that when needed. */
2829
2830 frp = topfrp->fr_child;
2831 if (wfh)
2832 /* Advance past frames with one window with 'wfh' set. */
2833 while (frame_fixed_height(frp))
2834 {
2835 frp = frp->fr_next;
2836 if (frp == NULL)
2837 return; /* no frame without 'wfh', give up */
2838 }
2839 if (!topfirst)
2840 {
2841 /* Find the bottom frame of this column */
2842 while (frp->fr_next != NULL)
2843 frp = frp->fr_next;
2844 if (wfh)
2845 /* Advance back for frames with one window with 'wfh' set. */
2846 while (frame_fixed_height(frp))
2847 frp = frp->fr_prev;
2848 }
2849
2850 extra_lines = height - topfrp->fr_height;
2851 if (extra_lines < 0)
2852 {
2853 /* reduce height of contained frames, bottom or top frame first */
2854 while (frp != NULL)
2855 {
2856 h = frame_minheight(frp, NULL);
2857 if (frp->fr_height + extra_lines < h)
2858 {
2859 extra_lines += frp->fr_height - h;
2860 frame_new_height(frp, h, topfirst, wfh);
2861 }
2862 else
2863 {
2864 frame_new_height(frp, frp->fr_height + extra_lines,
2865 topfirst, wfh);
2866 break;
2867 }
2868 if (topfirst)
2869 {
2870 do
2871 frp = frp->fr_next;
2872 while (wfh && frp != NULL && frame_fixed_height(frp));
2873 }
2874 else
2875 {
2876 do
2877 frp = frp->fr_prev;
2878 while (wfh && frp != NULL && frame_fixed_height(frp));
2879 }
2880 /* Increase "height" if we could not reduce enough frames. */
2881 if (frp == NULL)
2882 height -= extra_lines;
2883 }
2884 }
2885 else if (extra_lines > 0)
2886 {
2887 /* increase height of bottom or top frame */
2888 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2889 }
2890 }
2891 topfrp->fr_height = height;
2892}
2893
2894/*
2895 * Return TRUE if height of frame "frp" should not be changed because of
2896 * the 'winfixheight' option.
2897 */
2898 static int
2899frame_fixed_height(frp)
2900 frame_T *frp;
2901{
2902 /* frame with one window: fixed height if 'winfixheight' set. */
2903 if (frp->fr_win != NULL)
2904 return frp->fr_win->w_p_wfh;
2905
2906 if (frp->fr_layout == FR_ROW)
2907 {
2908 /* The frame is fixed height if one of the frames in the row is fixed
2909 * height. */
2910 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2911 if (frame_fixed_height(frp))
2912 return TRUE;
2913 return FALSE;
2914 }
2915
2916 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2917 * frames in the row are fixed height. */
2918 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2919 if (!frame_fixed_height(frp))
2920 return FALSE;
2921 return TRUE;
2922}
2923
2924#ifdef FEAT_VERTSPLIT
2925/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002926 * Return TRUE if width of frame "frp" should not be changed because of
2927 * the 'winfixwidth' option.
2928 */
2929 static int
2930frame_fixed_width(frp)
2931 frame_T *frp;
2932{
2933 /* frame with one window: fixed width if 'winfixwidth' set. */
2934 if (frp->fr_win != NULL)
2935 return frp->fr_win->w_p_wfw;
2936
2937 if (frp->fr_layout == FR_COL)
2938 {
2939 /* The frame is fixed width if one of the frames in the row is fixed
2940 * width. */
2941 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2942 if (frame_fixed_width(frp))
2943 return TRUE;
2944 return FALSE;
2945 }
2946
2947 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2948 * frames in the row are fixed width. */
2949 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2950 if (!frame_fixed_width(frp))
2951 return FALSE;
2952 return TRUE;
2953}
2954
2955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 * Add a status line to windows at the bottom of "frp".
2957 * Note: Does not check if there is room!
2958 */
2959 static void
2960frame_add_statusline(frp)
2961 frame_T *frp;
2962{
2963 win_T *wp;
2964
2965 if (frp->fr_layout == FR_LEAF)
2966 {
2967 wp = frp->fr_win;
2968 if (wp->w_status_height == 0)
2969 {
2970 if (wp->w_height > 0) /* don't make it negative */
2971 --wp->w_height;
2972 wp->w_status_height = STATUS_HEIGHT;
2973 }
2974 }
2975 else if (frp->fr_layout == FR_ROW)
2976 {
2977 /* Handle all the frames in the row. */
2978 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2979 frame_add_statusline(frp);
2980 }
2981 else /* frp->fr_layout == FR_COL */
2982 {
2983 /* Only need to handle the last frame in the column. */
2984 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2985 ;
2986 frame_add_statusline(frp);
2987 }
2988}
2989
2990/*
2991 * Set width of a frame. Handles recursively going through contained frames.
2992 * May remove separator line for windows at the right side (for win_close()).
2993 */
2994 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002995frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 frame_T *topfrp;
2997 int width;
2998 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002999 int wfw; /* obey 'winfixwidth' when there is a choice;
3000 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 frame_T *frp;
3003 int extra_cols;
3004 int w;
3005 win_T *wp;
3006
3007 if (topfrp->fr_layout == FR_LEAF)
3008 {
3009 /* Simple case: just one window. */
3010 wp = topfrp->fr_win;
3011 /* Find out if there are any windows right of this one. */
3012 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3013 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3014 break;
3015 if (frp->fr_parent == NULL)
3016 wp->w_vsep_width = 0;
3017 win_new_width(wp, width - wp->w_vsep_width);
3018 }
3019 else if (topfrp->fr_layout == FR_COL)
3020 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003021 do
3022 {
3023 /* All frames in this column get the same new width. */
3024 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3025 {
3026 frame_new_width(frp, width, leftfirst, wfw);
3027 if (frp->fr_width > width)
3028 {
3029 /* Could not fit the windows, make whole column wider. */
3030 width = frp->fr_width;
3031 break;
3032 }
3033 }
3034 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036 else /* fr_layout == FR_ROW */
3037 {
3038 /* Complicated case: Resize a row of frames. Resize the rightmost
3039 * frame first, frames left of it when needed. */
3040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003042 if (wfw)
3043 /* Advance past frames with one window with 'wfw' set. */
3044 while (frame_fixed_width(frp))
3045 {
3046 frp = frp->fr_next;
3047 if (frp == NULL)
3048 return; /* no frame without 'wfw', give up */
3049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003051 {
3052 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 while (frp->fr_next != NULL)
3054 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003055 if (wfw)
3056 /* Advance back for frames with one window with 'wfw' set. */
3057 while (frame_fixed_width(frp))
3058 frp = frp->fr_prev;
3059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
3061 extra_cols = width - topfrp->fr_width;
3062 if (extra_cols < 0)
3063 {
3064 /* reduce frame width, rightmost frame first */
3065 while (frp != NULL)
3066 {
3067 w = frame_minwidth(frp, NULL);
3068 if (frp->fr_width + extra_cols < w)
3069 {
3070 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003071 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
3073 else
3074 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003075 frame_new_width(frp, frp->fr_width + extra_cols,
3076 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 break;
3078 }
3079 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003080 {
3081 do
3082 frp = frp->fr_next;
3083 while (wfw && frp != NULL && frame_fixed_width(frp));
3084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003086 {
3087 do
3088 frp = frp->fr_prev;
3089 while (wfw && frp != NULL && frame_fixed_width(frp));
3090 }
3091 /* Increase "width" if we could not reduce enough frames. */
3092 if (frp == NULL)
3093 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 }
3095 }
3096 else if (extra_cols > 0)
3097 {
3098 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003099 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101 }
3102 topfrp->fr_width = width;
3103}
3104
3105/*
3106 * Add the vertical separator to windows at the right side of "frp".
3107 * Note: Does not check if there is room!
3108 */
3109 static void
3110frame_add_vsep(frp)
3111 frame_T *frp;
3112{
3113 win_T *wp;
3114
3115 if (frp->fr_layout == FR_LEAF)
3116 {
3117 wp = frp->fr_win;
3118 if (wp->w_vsep_width == 0)
3119 {
3120 if (wp->w_width > 0) /* don't make it negative */
3121 --wp->w_width;
3122 wp->w_vsep_width = 1;
3123 }
3124 }
3125 else if (frp->fr_layout == FR_COL)
3126 {
3127 /* Handle all the frames in the column. */
3128 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3129 frame_add_vsep(frp);
3130 }
3131 else /* frp->fr_layout == FR_ROW */
3132 {
3133 /* Only need to handle the last frame in the row. */
3134 frp = frp->fr_child;
3135 while (frp->fr_next != NULL)
3136 frp = frp->fr_next;
3137 frame_add_vsep(frp);
3138 }
3139}
3140
3141/*
3142 * Set frame width from the window it contains.
3143 */
3144 static void
3145frame_fix_width(wp)
3146 win_T *wp;
3147{
3148 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3149}
3150#endif
3151
3152/*
3153 * Set frame height from the window it contains.
3154 */
3155 static void
3156frame_fix_height(wp)
3157 win_T *wp;
3158{
3159 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3160}
3161
3162/*
3163 * Compute the minimal height for frame "topfrp".
3164 * Uses the 'winminheight' option.
3165 * When "next_curwin" isn't NULL, use p_wh for this window.
3166 * When "next_curwin" is NOWIN, don't use at least one line for the current
3167 * window.
3168 */
3169 static int
3170frame_minheight(topfrp, next_curwin)
3171 frame_T *topfrp;
3172 win_T *next_curwin;
3173{
3174 frame_T *frp;
3175 int m;
3176#ifdef FEAT_VERTSPLIT
3177 int n;
3178#endif
3179
3180 if (topfrp->fr_win != NULL)
3181 {
3182 if (topfrp->fr_win == next_curwin)
3183 m = p_wh + topfrp->fr_win->w_status_height;
3184 else
3185 {
3186 /* window: minimal height of the window plus status line */
3187 m = p_wmh + topfrp->fr_win->w_status_height;
3188 /* Current window is minimal one line high */
3189 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3190 ++m;
3191 }
3192 }
3193#ifdef FEAT_VERTSPLIT
3194 else if (topfrp->fr_layout == FR_ROW)
3195 {
3196 /* get the minimal height from each frame in this row */
3197 m = 0;
3198 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3199 {
3200 n = frame_minheight(frp, next_curwin);
3201 if (n > m)
3202 m = n;
3203 }
3204 }
3205#endif
3206 else
3207 {
3208 /* Add up the minimal heights for all frames in this column. */
3209 m = 0;
3210 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3211 m += frame_minheight(frp, next_curwin);
3212 }
3213
3214 return m;
3215}
3216
3217#ifdef FEAT_VERTSPLIT
3218/*
3219 * Compute the minimal width for frame "topfrp".
3220 * When "next_curwin" isn't NULL, use p_wiw for this window.
3221 * When "next_curwin" is NOWIN, don't use at least one column for the current
3222 * window.
3223 */
3224 static int
3225frame_minwidth(topfrp, next_curwin)
3226 frame_T *topfrp;
3227 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3228{
3229 frame_T *frp;
3230 int m, n;
3231
3232 if (topfrp->fr_win != NULL)
3233 {
3234 if (topfrp->fr_win == next_curwin)
3235 m = p_wiw + topfrp->fr_win->w_vsep_width;
3236 else
3237 {
3238 /* window: minimal width of the window plus separator column */
3239 m = p_wmw + topfrp->fr_win->w_vsep_width;
3240 /* Current window is minimal one column wide */
3241 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3242 ++m;
3243 }
3244 }
3245 else if (topfrp->fr_layout == FR_COL)
3246 {
3247 /* get the minimal width from each frame in this column */
3248 m = 0;
3249 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3250 {
3251 n = frame_minwidth(frp, next_curwin);
3252 if (n > m)
3253 m = n;
3254 }
3255 }
3256 else
3257 {
3258 /* Add up the minimal widths for all frames in this row. */
3259 m = 0;
3260 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3261 m += frame_minwidth(frp, next_curwin);
3262 }
3263
3264 return m;
3265}
3266#endif
3267
3268
3269/*
3270 * Try to close all windows except current one.
3271 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3272 * used and the buffer was modified.
3273 *
3274 * Used by ":bdel" and ":only".
3275 */
3276 void
3277close_others(message, forceit)
3278 int message;
3279 int forceit; /* always hide all other windows */
3280{
3281 win_T *wp;
3282 win_T *nextwp;
3283 int r;
3284
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003285 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 if (message
3288#ifdef FEAT_AUTOCMD
3289 && !autocmd_busy
3290#endif
3291 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003292 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 return;
3294 }
3295
3296 /* Be very careful here: autocommands may change the window layout. */
3297 for (wp = firstwin; win_valid(wp); wp = nextwp)
3298 {
3299 nextwp = wp->w_next;
3300 if (wp != curwin) /* don't close current window */
3301 {
3302
3303 /* Check if it's allowed to abandon this window */
3304 r = can_abandon(wp->w_buffer, forceit);
3305#ifdef FEAT_AUTOCMD
3306 if (!win_valid(wp)) /* autocommands messed wp up */
3307 {
3308 nextwp = firstwin;
3309 continue;
3310 }
3311#endif
3312 if (!r)
3313 {
3314#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3315 if (message && (p_confirm || cmdmod.confirm) && p_write)
3316 {
3317 dialog_changed(wp->w_buffer, FALSE);
3318# ifdef FEAT_AUTOCMD
3319 if (!win_valid(wp)) /* autocommands messed wp up */
3320 {
3321 nextwp = firstwin;
3322 continue;
3323 }
3324# endif
3325 }
3326 if (bufIsChanged(wp->w_buffer))
3327#endif
3328 continue;
3329 }
3330 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3331 }
3332 }
3333
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003334 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 EMSG(_("E445: Other window contains changes"));
3336}
3337
3338#endif /* FEAT_WINDOWS */
3339
3340/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003341 * Init the current window "curwin".
3342 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 */
3344 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003345curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003347 win_init_empty(curwin);
3348}
3349
3350 void
3351win_init_empty(wp)
3352 win_T *wp;
3353{
3354 redraw_win_later(wp, NOT_VALID);
3355 wp->w_lines_valid = 0;
3356 wp->w_cursor.lnum = 1;
3357 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003359 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003361 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3362 wp->w_pcmark.col = 0;
3363 wp->w_prev_pcmark.lnum = 0;
3364 wp->w_prev_pcmark.col = 0;
3365 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003367 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003369 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003371 if (wp->w_p_rl)
3372 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003374 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003376#ifdef FEAT_SYN_HL
3377 wp->w_s = &wp->w_buffer->b_s;
3378#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380
3381/*
3382 * Allocate the first window and put an empty buffer in it.
3383 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003384 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003386 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387win_alloc_first()
3388{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003389 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003390 return FAIL;
3391
3392#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003393 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003394 if (first_tabpage == NULL)
3395 return FAIL;
3396 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003397 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003398#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003399
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003400 return OK;
3401}
3402
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003403#if defined(FEAT_AUTOCMD) || defined(PROTO)
3404/*
3405 * Init "aucmd_win". This can only be done after the first
3406 * window is fully initialized, thus it can't be in win_alloc_first().
3407 */
3408 void
3409win_alloc_aucmd_win()
3410{
3411 aucmd_win = win_alloc(NULL, TRUE);
3412 if (aucmd_win != NULL)
3413 {
3414 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003415 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003416 new_frame(aucmd_win);
3417 }
3418}
3419#endif
3420
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003422 * Allocate the first window or the first window in a new tab page.
3423 * When "oldwin" is NULL create an empty buffer for it.
3424 * When "oldwin" is not NULL copy info from it to the new window (only with
3425 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426 * Return FAIL when something goes wrong (out of memory).
3427 */
3428 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003429win_alloc_firstwin(oldwin)
3430 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003431{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003432 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003433 if (oldwin == NULL)
3434 {
3435 /* Very first window, need to create an empty buffer for it and
3436 * initialize from scratch. */
3437 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3438 if (curwin == NULL || curbuf == NULL)
3439 return FAIL;
3440 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003441#ifdef FEAT_SYN_HL
3442 curwin->w_s = &(curbuf->b_s);
3443#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003444 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003446 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003448 curwin_init(); /* init current window */
3449 }
3450#ifdef FEAT_WINDOWS
3451 else
3452 {
3453 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003454 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003455
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003456 /* We don't want cursor- and scroll-binding in the first window. */
3457 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003458 }
3459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003461 new_frame(curwin);
3462 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003463 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003464 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465#ifdef FEAT_VERTSPLIT
3466 topframe->fr_width = Columns;
3467#endif
3468 topframe->fr_height = Rows - p_ch;
3469 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003470
3471 return OK;
3472}
3473
3474/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003475 * Create a frame for window "wp".
3476 */
3477 static void
3478new_frame(win_T *wp)
3479{
3480 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3481
3482 wp->w_frame = frp;
3483 if (frp != NULL)
3484 {
3485 frp->fr_layout = FR_LEAF;
3486 frp->fr_win = wp;
3487 }
3488}
3489
3490/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003491 * Initialize the window and frame size to the maximum.
3492 */
3493 void
3494win_init_size()
3495{
3496 firstwin->w_height = ROWS_AVAIL;
3497 topframe->fr_height = ROWS_AVAIL;
3498#ifdef FEAT_VERTSPLIT
3499 firstwin->w_width = Columns;
3500 topframe->fr_width = Columns;
3501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502}
3503
3504#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003505
3506/*
3507 * Allocate a new tabpage_T and init the values.
3508 * Returns NULL when out of memory.
3509 */
3510 static tabpage_T *
3511alloc_tabpage()
3512{
3513 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003514# ifdef FEAT_GUI
3515 int i;
3516# endif
3517
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003518
3519 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003520 if (tp == NULL)
3521 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003522
Bram Moolenaar429fa852013-04-15 12:27:36 +02003523# ifdef FEAT_EVAL
3524 /* init t: variables */
3525 tp->tp_vars = dict_alloc();
3526 if (tp->tp_vars == NULL)
3527 {
3528 vim_free(tp);
3529 return NULL;
3530 }
3531 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3532# endif
3533
3534# ifdef FEAT_GUI
3535 for (i = 0; i < 3; i++)
3536 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003537# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003538# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003539 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003540# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003541 tp->tp_ch_used = p_ch;
3542
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003543 return tp;
3544}
3545
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003546 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003547free_tabpage(tp)
3548 tabpage_T *tp;
3549{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003550 int idx;
3551
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003552# ifdef FEAT_DIFF
3553 diff_clear(tp);
3554# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003555 for (idx = 0; idx < SNAP_COUNT; ++idx)
3556 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003557#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003558 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3559 hash_init(&tp->tp_vars->dv_hashtab);
3560 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003561#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003562
3563#ifdef FEAT_PYTHON
3564 python_tabpage_free(tp);
3565#endif
3566
3567#ifdef FEAT_PYTHON3
3568 python3_tabpage_free(tp);
3569#endif
3570
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003571 vim_free(tp);
3572}
3573
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003574/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003575 * Create a new Tab page with one window.
3576 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003577 * When "after" is 0 put it just after the current Tab page.
3578 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003579 * Return FAIL or OK.
3580 */
3581 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003582win_new_tabpage(after)
3583 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003584{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003585 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003586 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003587 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003588
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003589 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003590 if (newtp == NULL)
3591 return FAIL;
3592
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003593 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003594 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003595 {
3596 vim_free(newtp);
3597 return FAIL;
3598 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003599 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600
3601 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003602 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003603 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003604 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003605 if (after == 1)
3606 {
3607 /* New tab page becomes the first one. */
3608 newtp->tp_next = first_tabpage;
3609 first_tabpage = newtp;
3610 }
3611 else
3612 {
3613 if (after > 0)
3614 {
3615 /* Put new tab page before tab page "after". */
3616 n = 2;
3617 for (tp = first_tabpage; tp->tp_next != NULL
3618 && n < after; tp = tp->tp_next)
3619 ++n;
3620 }
3621 newtp->tp_next = tp->tp_next;
3622 tp->tp_next = newtp;
3623 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003624 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003625 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003626 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003627
3628 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003629 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003630
3631#if defined(FEAT_GUI)
3632 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3633 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003634 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003635#endif
3636
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003637 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003638#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003639 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003640 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003641#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642 return OK;
3643 }
3644
3645 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003646 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003647 return FAIL;
3648}
3649
3650/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003651 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3652 * like with ":split".
3653 * Returns OK if a new tab page was created, FAIL otherwise.
3654 */
3655 int
3656may_open_tabpage()
3657{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003658 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003659
Bram Moolenaard326ce82007-03-11 14:48:29 +00003660 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003661 {
3662 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003663 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003664 return win_new_tabpage(n);
3665 }
3666 return FAIL;
3667}
3668
3669/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003670 * Create up to "maxcount" tabpages with empty windows.
3671 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003672 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003673 int
3674make_tabpages(maxcount)
3675 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003676{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003677 int count = maxcount;
3678 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003679
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003680 /* Limit to 'tabpagemax' tabs. */
3681 if (count > p_tpm)
3682 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003683
3684#ifdef FEAT_AUTOCMD
3685 /*
3686 * Don't execute autocommands while creating the tab pages. Must do that
3687 * when putting the buffers in the windows.
3688 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003689 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003690#endif
3691
3692 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003693 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003694 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003695
3696#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003697 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003698#endif
3699
3700 /* return actual number of tab pages */
3701 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003702}
3703
3704/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003705 * Return TRUE when "tpc" points to a valid tab page.
3706 */
3707 int
3708valid_tabpage(tpc)
3709 tabpage_T *tpc;
3710{
3711 tabpage_T *tp;
3712
3713 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3714 if (tp == tpc)
3715 return TRUE;
3716 return FALSE;
3717}
3718
3719/*
3720 * Find tab page "n" (first one is 1). Returns NULL when not found.
3721 */
3722 tabpage_T *
3723find_tabpage(n)
3724 int n;
3725{
3726 tabpage_T *tp;
3727 int i = 1;
3728
3729 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3730 ++i;
3731 return tp;
3732}
3733
3734/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003735 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003736 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003737 */
3738 int
3739tabpage_index(ftp)
3740 tabpage_T *ftp;
3741{
3742 int i = 1;
3743 tabpage_T *tp;
3744
3745 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3746 ++i;
3747 return i;
3748}
3749
3750/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003751 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003752 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003753 * FAIL.
3754 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003755 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003756 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003757leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003758 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003759 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003760 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003761{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003762 tabpage_T *tp = curtab;
3763
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003764 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003765#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003766 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003767 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003768 if (new_curbuf != curbuf)
3769 {
3770 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3771 if (curtab != tp)
3772 return FAIL;
3773 }
3774 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3775 if (curtab != tp)
3776 return FAIL;
3777 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003778 if (curtab != tp)
3779 return FAIL;
3780 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003781#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003782#if defined(FEAT_GUI)
3783 /* Remove the scrollbars. They may be added back later. */
3784 if (gui.in_use)
3785 gui_remove_scrollbars();
3786#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003787 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003788 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003789 tp->tp_firstwin = firstwin;
3790 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003791 tp->tp_old_Rows = Rows;
3792 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003793 firstwin = NULL;
3794 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003795 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003796}
3797
3798/*
3799 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003800 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003801 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3802 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003803 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003804 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003805enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003806 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003807 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003808 int trigger_enter_autocmds UNUSED;
3809 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003811 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003812 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003813
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003814 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003815 firstwin = tp->tp_firstwin;
3816 lastwin = tp->tp_lastwin;
3817 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003818
3819 /* We would like doing the TabEnter event first, but we don't have a
3820 * valid current window yet, which may break some commands.
3821 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003822 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3823 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003824 prevwin = next_prevwin;
3825
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003826 last_status(FALSE); /* status line may appear or disappear */
3827 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003828 must_redraw = CLEAR; /* need to redraw everything */
3829#ifdef FEAT_DIFF
3830 diff_need_scrollbind = TRUE;
3831#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003832
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003833 /* The tabpage line may have appeared or disappeared, may need to resize
3834 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003835 * frame sizes too. Use the stored value of p_ch, so that it can be
3836 * different for each tab page. */
3837 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003838 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3839#ifdef FEAT_GUI_TABLINE
3840 && !gui_use_tabline()
3841#endif
3842 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003843 shell_new_rows();
3844#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003845 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003846 shell_new_columns(); /* update window widths */
3847#endif
3848
3849#if defined(FEAT_GUI)
3850 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3851 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003852 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003853#endif
3854
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003855#ifdef FEAT_AUTOCMD
3856 /* Apply autocommands after updating the display, when 'rows' and
3857 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003858 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003859 {
3860 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3861 if (old_curbuf != curbuf)
3862 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3863 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003864#endif
3865
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003866 redraw_all_later(CLEAR);
3867}
3868
3869/*
3870 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003871 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003872 */
3873 void
3874goto_tabpage(n)
3875 int n;
3876{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003877 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003878 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003879 int i;
3880
Bram Moolenaard68071d2006-05-02 22:08:30 +00003881 if (text_locked())
3882 {
3883 /* Not allowed when editing the command line. */
3884#ifdef FEAT_CMDWIN
3885 if (cmdwin_type != 0)
3886 EMSG(_(e_cmdwin));
3887 else
3888#endif
3889 EMSG(_(e_secure));
3890 return;
3891 }
3892
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003893 /* If there is only one it can't work. */
3894 if (first_tabpage->tp_next == NULL)
3895 {
3896 if (n > 1)
3897 beep_flush();
3898 return;
3899 }
3900
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003901 if (n == 0)
3902 {
3903 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003904 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003905 tp = first_tabpage;
3906 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003907 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003908 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003909 else if (n < 0)
3910 {
3911 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3912 * this N times. */
3913 ttp = curtab;
3914 for (i = n; i < 0; ++i)
3915 {
3916 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3917 tp = tp->tp_next)
3918 ;
3919 ttp = tp;
3920 }
3921 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003922 else if (n == 9999)
3923 {
3924 /* Go to last tab page. */
3925 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3926 ;
3927 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003928 else
3929 {
3930 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003931 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003932 if (tp == NULL)
3933 {
3934 beep_flush();
3935 return;
3936 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003937 }
3938
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003939 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003940
3941#ifdef FEAT_GUI_TABLINE
3942 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003943 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003944#endif
3945}
3946
3947/*
3948 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003949 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3950 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003951 * Note: doesn't update the GUI tab.
3952 */
3953 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003954goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003955 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003956 int trigger_enter_autocmds;
3957 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003958{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003959 /* Don't repeat a message in another tab page. */
3960 set_keep_msg(NULL, 0);
3961
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003962 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3963 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003964 {
3965 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003966 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3967 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003968 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003969 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3970 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003971 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
3974/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003975 * Enter window "wp" in tab page "tp".
3976 * Also updates the GUI tab.
3977 */
3978 void
3979goto_tabpage_win(tp, wp)
3980 tabpage_T *tp;
3981 win_T *wp;
3982{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003983 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003984 if (curtab == tp && win_valid(wp))
3985 {
3986 win_enter(wp, TRUE);
3987# ifdef FEAT_GUI_TABLINE
3988 if (gui_use_tabline())
3989 gui_mch_set_curtab(tabpage_index(curtab));
3990# endif
3991 }
3992}
3993
3994/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003995 * Move the current tab page to before tab page "nr".
3996 */
3997 void
3998tabpage_move(nr)
3999 int nr;
4000{
4001 int n = nr;
4002 tabpage_T *tp;
4003
4004 if (first_tabpage->tp_next == NULL)
4005 return;
4006
4007 /* Remove the current tab page from the list of tab pages. */
4008 if (curtab == first_tabpage)
4009 first_tabpage = curtab->tp_next;
4010 else
4011 {
4012 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4013 if (tp->tp_next == curtab)
4014 break;
4015 if (tp == NULL) /* "cannot happen" */
4016 return;
4017 tp->tp_next = curtab->tp_next;
4018 }
4019
4020 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02004021 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004022 {
4023 curtab->tp_next = first_tabpage;
4024 first_tabpage = curtab;
4025 }
4026 else
4027 {
4028 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
4029 --n;
4030 curtab->tp_next = tp->tp_next;
4031 tp->tp_next = curtab;
4032 }
4033
4034 /* Need to redraw the tabline. Tab page contents doesn't change. */
4035 redraw_tabline = TRUE;
4036}
4037
4038
4039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 * Go to another window.
4041 * When jumping to another buffer, stop Visual mode. Do this before
4042 * changing windows so we can yank the selection into the '*' register.
4043 * When jumping to another window on the same buffer, adjust its cursor
4044 * position to keep the same Visual area.
4045 */
4046 void
4047win_goto(wp)
4048 win_T *wp;
4049{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004050#ifdef FEAT_CONCEAL
4051 win_T *owp = curwin;
4052#endif
4053
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004054 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
4056 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004057 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 return;
4059 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_AUTOCMD
4061 if (curbuf_locked())
4062 return;
4063#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (wp->w_buffer != curbuf)
4066 reset_VIsual_and_resel();
4067 else if (VIsual_active)
4068 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070#ifdef FEAT_GUI
4071 need_mouse_correct = TRUE;
4072#endif
4073 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004074
4075#ifdef FEAT_CONCEAL
4076 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004077 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004078 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004079 if (curwin->w_p_cole > 0 && !msg_scrolled)
4080 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082}
4083
4084#if defined(FEAT_PERL) || defined(PROTO)
4085/*
4086 * Find window number "winnr" (counting top to bottom).
4087 */
4088 win_T *
4089win_find_nr(winnr)
4090 int winnr;
4091{
4092 win_T *wp;
4093
4094# ifdef FEAT_WINDOWS
4095 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4096 if (--winnr == 0)
4097 break;
4098 return wp;
4099# else
4100 return curwin;
4101# endif
4102}
4103#endif
4104
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004105#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4106 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004107/*
4108 * Find the tabpage for window "win".
4109 */
4110 tabpage_T *
4111win_find_tabpage(win)
4112 win_T *win;
4113{
4114 win_T *wp;
4115 tabpage_T *tp;
4116
4117 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004118 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4119 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004120 if (wp == win)
4121 return tp;
4122 return NULL;
4123}
4124#endif
4125
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126#ifdef FEAT_VERTSPLIT
4127/*
4128 * Move to window above or below "count" times.
4129 */
4130 static void
4131win_goto_ver(up, count)
4132 int up; /* TRUE to go to win above */
4133 long count;
4134{
4135 frame_T *fr;
4136 frame_T *nfr;
4137 frame_T *foundfr;
4138
4139 foundfr = curwin->w_frame;
4140 while (count--)
4141 {
4142 /*
4143 * First go upwards in the tree of frames until we find a upwards or
4144 * downwards neighbor.
4145 */
4146 fr = foundfr;
4147 for (;;)
4148 {
4149 if (fr == topframe)
4150 goto end;
4151 if (up)
4152 nfr = fr->fr_prev;
4153 else
4154 nfr = fr->fr_next;
4155 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4156 break;
4157 fr = fr->fr_parent;
4158 }
4159
4160 /*
4161 * Now go downwards to find the bottom or top frame in it.
4162 */
4163 for (;;)
4164 {
4165 if (nfr->fr_layout == FR_LEAF)
4166 {
4167 foundfr = nfr;
4168 break;
4169 }
4170 fr = nfr->fr_child;
4171 if (nfr->fr_layout == FR_ROW)
4172 {
4173 /* Find the frame at the cursor row. */
4174 while (fr->fr_next != NULL
4175 && frame2win(fr)->w_wincol + fr->fr_width
4176 <= curwin->w_wincol + curwin->w_wcol)
4177 fr = fr->fr_next;
4178 }
4179 if (nfr->fr_layout == FR_COL && up)
4180 while (fr->fr_next != NULL)
4181 fr = fr->fr_next;
4182 nfr = fr;
4183 }
4184 }
4185end:
4186 if (foundfr != NULL)
4187 win_goto(foundfr->fr_win);
4188}
4189
4190/*
4191 * Move to left or right window.
4192 */
4193 static void
4194win_goto_hor(left, count)
4195 int left; /* TRUE to go to left win */
4196 long count;
4197{
4198 frame_T *fr;
4199 frame_T *nfr;
4200 frame_T *foundfr;
4201
4202 foundfr = curwin->w_frame;
4203 while (count--)
4204 {
4205 /*
4206 * First go upwards in the tree of frames until we find a left or
4207 * right neighbor.
4208 */
4209 fr = foundfr;
4210 for (;;)
4211 {
4212 if (fr == topframe)
4213 goto end;
4214 if (left)
4215 nfr = fr->fr_prev;
4216 else
4217 nfr = fr->fr_next;
4218 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4219 break;
4220 fr = fr->fr_parent;
4221 }
4222
4223 /*
4224 * Now go downwards to find the leftmost or rightmost frame in it.
4225 */
4226 for (;;)
4227 {
4228 if (nfr->fr_layout == FR_LEAF)
4229 {
4230 foundfr = nfr;
4231 break;
4232 }
4233 fr = nfr->fr_child;
4234 if (nfr->fr_layout == FR_COL)
4235 {
4236 /* Find the frame at the cursor row. */
4237 while (fr->fr_next != NULL
4238 && frame2win(fr)->w_winrow + fr->fr_height
4239 <= curwin->w_winrow + curwin->w_wrow)
4240 fr = fr->fr_next;
4241 }
4242 if (nfr->fr_layout == FR_ROW && left)
4243 while (fr->fr_next != NULL)
4244 fr = fr->fr_next;
4245 nfr = fr;
4246 }
4247 }
4248end:
4249 if (foundfr != NULL)
4250 win_goto(foundfr->fr_win);
4251}
4252#endif
4253
4254/*
4255 * Make window "wp" the current window.
4256 */
4257 void
4258win_enter(wp, undo_sync)
4259 win_T *wp;
4260 int undo_sync;
4261{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004262 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263}
4264
4265/*
4266 * Make window wp the current window.
4267 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4268 * been closed and isn't valid.
4269 */
4270 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004271win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 win_T *wp;
4273 int undo_sync;
4274 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004275 int trigger_enter_autocmds UNUSED;
4276 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
4278#ifdef FEAT_AUTOCMD
4279 int other_buffer = FALSE;
4280#endif
4281
4282 if (wp == curwin && !curwin_invalid) /* nothing to do */
4283 return;
4284
4285#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004286 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
4288 /*
4289 * Be careful: If autocommands delete the window, return now.
4290 */
4291 if (wp->w_buffer != curbuf)
4292 {
4293 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4294 other_buffer = TRUE;
4295 if (!win_valid(wp))
4296 return;
4297 }
4298 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4299 if (!win_valid(wp))
4300 return;
4301# ifdef FEAT_EVAL
4302 /* autocmds may abort script processing */
4303 if (aborting())
4304 return;
4305# endif
4306 }
4307#endif
4308
4309 /* sync undo before leaving the current buffer */
4310 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004311 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004312
4313 /* Might need to scroll the old window before switching, e.g., when the
4314 * cursor was moved. */
4315 update_topline();
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 /* may have to copy the buffer options when 'cpo' contains 'S' */
4318 if (wp->w_buffer != curbuf)
4319 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4320 if (!curwin_invalid)
4321 {
4322 prevwin = curwin; /* remember for CTRL-W p */
4323 curwin->w_redr_status = TRUE;
4324 }
4325 curwin = wp;
4326 curbuf = wp->w_buffer;
4327 check_cursor();
4328#ifdef FEAT_VIRTUALEDIT
4329 if (!virtual_active())
4330 curwin->w_cursor.coladd = 0;
4331#endif
4332 changed_line_abv_curs(); /* assume cursor position needs updating */
4333
4334 if (curwin->w_localdir != NULL)
4335 {
4336 /* Window has a local directory: Save current directory as global
4337 * directory (unless that was done already) and change to the local
4338 * directory. */
4339 if (globaldir == NULL)
4340 {
4341 char_u cwd[MAXPATHL];
4342
4343 if (mch_dirname(cwd, MAXPATHL) == OK)
4344 globaldir = vim_strsave(cwd);
4345 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004346 if (mch_chdir((char *)curwin->w_localdir) == 0)
4347 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 else if (globaldir != NULL)
4350 {
4351 /* Window doesn't have a local directory and we are not in the global
4352 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004353 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 vim_free(globaldir);
4355 globaldir = NULL;
4356 shorten_fnames(TRUE);
4357 }
4358
4359#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004360 if (trigger_enter_autocmds)
4361 {
4362 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4363 if (other_buffer)
4364 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366#endif
4367
4368#ifdef FEAT_TITLE
4369 maketitle();
4370#endif
4371 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004372 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (restart_edit)
4374 redraw_later(VALID); /* causes status line redraw */
4375
4376 /* set window height to desired minimal value */
4377 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4378 win_setheight((int)p_wh);
4379 else if (curwin->w_height == 0)
4380 win_setheight(1);
4381
4382#ifdef FEAT_VERTSPLIT
4383 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004384 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 win_setwidth((int)p_wiw);
4386#endif
4387
4388#ifdef FEAT_MOUSE
4389 setmouse(); /* in case jumped to/from help buffer */
4390#endif
4391
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004392 /* Change directories when the 'acd' option is set. */
4393 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394}
4395
4396#endif /* FEAT_WINDOWS */
4397
4398#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4399/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004400 * Jump to the first open window that contains buffer "buf", if one exists.
4401 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 */
4403 win_T *
4404buf_jump_open_win(buf)
4405 buf_T *buf;
4406{
4407# ifdef FEAT_WINDOWS
4408 win_T *wp;
4409
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004410 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 if (wp->w_buffer == buf)
4412 break;
4413 if (wp != NULL)
4414 win_enter(wp, FALSE);
4415 return wp;
4416# else
4417 if (curwin->w_buffer == buf)
4418 return curwin;
4419 return NULL;
4420# endif
4421}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004422
4423/*
4424 * Jump to the first open window in any tab page that contains buffer "buf",
4425 * if one exists.
4426 * Returns a pointer to the window found, otherwise NULL.
4427 */
4428 win_T *
4429buf_jump_open_tab(buf)
4430 buf_T *buf;
4431{
4432# ifdef FEAT_WINDOWS
4433 win_T *wp;
4434 tabpage_T *tp;
4435
4436 /* First try the current tab page. */
4437 wp = buf_jump_open_win(buf);
4438 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 }
4455
4456 return wp;
4457# else
4458 if (curwin->w_buffer == buf)
4459 return curwin;
4460 return NULL;
4461# endif
4462}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463#endif
4464
4465/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004466 * Allocate a window structure and link it in the window list when "hidden" is
4467 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004470win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004471 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004472 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004474 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
4476 /*
4477 * allocate window structure and linesizes arrays
4478 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004479 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004480 if (new_wp == NULL)
4481 return NULL;
4482
4483 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004485 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004486 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488
Bram Moolenaar429fa852013-04-15 12:27:36 +02004489#ifdef FEAT_EVAL
4490 /* init w: variables */
4491 new_wp->w_vars = dict_alloc();
4492 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004494 win_free_lsize(new_wp);
4495 vim_free(new_wp);
4496 return NULL;
4497 }
4498 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004499#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004500
4501#ifdef FEAT_AUTOCMD
4502 /* Don't execute autocommands while the window is not properly
4503 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4504 * event. */
4505 block_autocmds();
4506#endif
4507 /*
4508 * link the window in the window list
4509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004511 if (!hidden)
4512 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513#endif
4514#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004515 new_wp->w_wincol = 0;
4516 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#endif
4518
Bram Moolenaar429fa852013-04-15 12:27:36 +02004519 /* position the display and the cursor at the top of the file. */
4520 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004522 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004524 new_wp->w_botline = 2;
4525 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004527 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528#endif
4529
Bram Moolenaar429fa852013-04-15 12:27:36 +02004530 /* We won't calculate w_fraction until resizing the window */
4531 new_wp->w_fraction = 0;
4532 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004535 if (gui.in_use)
4536 {
4537 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4538 SBAR_LEFT, new_wp);
4539 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4540 SBAR_RIGHT, new_wp);
4541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542#endif
4543#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004544 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004546#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004547 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004548#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004549#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004550 new_wp->w_match_head = NULL;
4551 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004552#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004553 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554}
4555
4556#if defined(FEAT_WINDOWS) || defined(PROTO)
4557
4558/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004559 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 */
4561 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004562win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004564 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565{
4566 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004567 buf_T *buf;
4568 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004570#ifdef FEAT_FOLDING
4571 clearFolding(wp);
4572#endif
4573
4574 /* reduce the reference count to the argument list. */
4575 alist_unlink(wp->w_alist);
4576
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004577#ifdef FEAT_AUTOCMD
4578 /* Don't execute autocommands while the window is halfway being deleted.
4579 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004580 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004581#endif
4582
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004583#ifdef FEAT_LUA
4584 lua_window_free(wp);
4585#endif
4586
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004587#ifdef FEAT_MZSCHEME
4588 mzscheme_window_free(wp);
4589#endif
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591#ifdef FEAT_PERL
4592 perl_win_free(wp);
4593#endif
4594
4595#ifdef FEAT_PYTHON
4596 python_window_free(wp);
4597#endif
4598
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004599#ifdef FEAT_PYTHON3
4600 python3_window_free(wp);
4601#endif
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603#ifdef FEAT_TCL
4604 tcl_window_free(wp);
4605#endif
4606
4607#ifdef FEAT_RUBY
4608 ruby_window_free(wp);
4609#endif
4610
4611 clear_winopt(&wp->w_onebuf_opt);
4612 clear_winopt(&wp->w_allbuf_opt);
4613
4614#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004615 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4616 hash_init(&wp->w_vars->dv_hashtab);
4617 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618#endif
4619
4620 if (prevwin == wp)
4621 prevwin = NULL;
4622 win_free_lsize(wp);
4623
4624 for (i = 0; i < wp->w_tagstacklen; ++i)
4625 vim_free(wp->w_tagstack[i].tagname);
4626
4627 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004628
Bram Moolenaarff18df02013-07-24 17:51:57 +02004629 /* Remove the window from the b_wininfo lists, it may happen that the
4630 * freed memory is re-used for another window. */
4631 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4632 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4633 if (wip->wi_win == wp)
4634 wip->wi_win = NULL;
4635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004637 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004639
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#ifdef FEAT_JUMPLIST
4641 free_jumplist(wp);
4642#endif
4643
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004644#ifdef FEAT_QUICKFIX
4645 qf_free_all(wp);
4646#endif
4647
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648#ifdef FEAT_GUI
4649 if (gui.in_use)
4650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4652 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4653 }
4654#endif /* FEAT_GUI */
4655
Bram Moolenaar860cae12010-06-05 23:22:07 +02004656#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004657 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004658#endif
4659
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004660#ifdef FEAT_AUTOCMD
4661 if (wp != aucmd_win)
4662#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004663 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004664#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004665 if (autocmd_busy)
4666 {
4667 wp->w_next = au_pending_free_win;
4668 au_pending_free_win = wp;
4669 }
4670 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004671#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004672 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004673
4674#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004675 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677}
4678
4679/*
4680 * Append window "wp" in the window list after window "after".
4681 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004682 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683win_append(after, wp)
4684 win_T *after, *wp;
4685{
4686 win_T *before;
4687
4688 if (after == NULL) /* after NULL is in front of the first */
4689 before = firstwin;
4690 else
4691 before = after->w_next;
4692
4693 wp->w_next = before;
4694 wp->w_prev = after;
4695 if (after == NULL)
4696 firstwin = wp;
4697 else
4698 after->w_next = wp;
4699 if (before == NULL)
4700 lastwin = wp;
4701 else
4702 before->w_prev = wp;
4703}
4704
4705/*
4706 * Remove a window from the window list.
4707 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004708 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004709win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004711 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712{
4713 if (wp->w_prev != NULL)
4714 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004715 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004717 else
4718 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 if (wp->w_next != NULL)
4720 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004721 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004723 else
4724 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725}
4726
4727/*
4728 * Append frame "frp" in a frame list after frame "after".
4729 */
4730 static void
4731frame_append(after, frp)
4732 frame_T *after, *frp;
4733{
4734 frp->fr_next = after->fr_next;
4735 after->fr_next = frp;
4736 if (frp->fr_next != NULL)
4737 frp->fr_next->fr_prev = frp;
4738 frp->fr_prev = after;
4739}
4740
4741/*
4742 * Insert frame "frp" in a frame list before frame "before".
4743 */
4744 static void
4745frame_insert(before, frp)
4746 frame_T *before, *frp;
4747{
4748 frp->fr_next = before;
4749 frp->fr_prev = before->fr_prev;
4750 before->fr_prev = frp;
4751 if (frp->fr_prev != NULL)
4752 frp->fr_prev->fr_next = frp;
4753 else
4754 frp->fr_parent->fr_child = frp;
4755}
4756
4757/*
4758 * Remove a frame from a frame list.
4759 */
4760 static void
4761frame_remove(frp)
4762 frame_T *frp;
4763{
4764 if (frp->fr_prev != NULL)
4765 frp->fr_prev->fr_next = frp->fr_next;
4766 else
4767 frp->fr_parent->fr_child = frp->fr_next;
4768 if (frp->fr_next != NULL)
4769 frp->fr_next->fr_prev = frp->fr_prev;
4770}
4771
4772#endif /* FEAT_WINDOWS */
4773
4774/*
4775 * Allocate w_lines[] for window "wp".
4776 * Return FAIL for failure, OK for success.
4777 */
4778 int
4779win_alloc_lines(wp)
4780 win_T *wp;
4781{
4782 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004783 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (wp->w_lines == NULL)
4785 return FAIL;
4786 return OK;
4787}
4788
4789/*
4790 * free lsize arrays for a window
4791 */
4792 void
4793win_free_lsize(wp)
4794 win_T *wp;
4795{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004796 /* TODO: why would wp be NULL here? */
4797 if (wp != NULL)
4798 {
4799 vim_free(wp->w_lines);
4800 wp->w_lines = NULL;
4801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802}
4803
4804/*
4805 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004806 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 */
4808 void
4809shell_new_rows()
4810{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004811 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 if (firstwin == NULL) /* not initialized yet */
4814 return;
4815#ifdef FEAT_WINDOWS
4816 if (h < frame_minheight(topframe, NULL))
4817 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004818
4819 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 * that doesn't result in the right height, forget about that option. */
4821 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004822 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 frame_new_height(topframe, h, FALSE, FALSE);
4824
4825 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4826#else
4827 if (h < 1)
4828 h = 1;
4829 win_new_height(firstwin, h);
4830#endif
4831 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004832#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004833 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004834#endif
4835
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836#if 0
4837 /* Disabled: don't want making the screen smaller make a window larger. */
4838 if (p_ea)
4839 win_equal(curwin, FALSE, 'v');
4840#endif
4841}
4842
4843#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4844/*
4845 * Called from win_new_shellsize() after Columns changed.
4846 */
4847 void
4848shell_new_columns()
4849{
4850 if (firstwin == NULL) /* not initialized yet */
4851 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004852
4853 /* First try setting the widths of windows with 'winfixwidth'. If that
4854 * doesn't result in the right width, forget about that option. */
4855 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004856 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004857 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4858
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4860#if 0
4861 /* Disabled: don't want making the screen smaller make a window larger. */
4862 if (p_ea)
4863 win_equal(curwin, FALSE, 'h');
4864#endif
4865}
4866#endif
4867
4868#if defined(FEAT_CMDWIN) || defined(PROTO)
4869/*
4870 * Save the size of all windows in "gap".
4871 */
4872 void
4873win_size_save(gap)
4874 garray_T *gap;
4875
4876{
4877 win_T *wp;
4878
4879 ga_init2(gap, (int)sizeof(int), 1);
4880 if (ga_grow(gap, win_count() * 2) == OK)
4881 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4882 {
4883 ((int *)gap->ga_data)[gap->ga_len++] =
4884 wp->w_width + wp->w_vsep_width;
4885 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4886 }
4887}
4888
4889/*
4890 * Restore window sizes, but only if the number of windows is still the same.
4891 * Does not free the growarray.
4892 */
4893 void
4894win_size_restore(gap)
4895 garray_T *gap;
4896{
4897 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004898 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900 if (win_count() * 2 == gap->ga_len)
4901 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004902 /* The order matters, because frames contain other frames, but it's
4903 * difficult to get right. The easy way out is to do it twice. */
4904 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004906 i = 0;
4907 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4908 {
4909 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4910 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 /* recompute the window positions */
4914 (void)win_comp_pos();
4915 }
4916}
4917#endif /* FEAT_CMDWIN */
4918
4919#if defined(FEAT_WINDOWS) || defined(PROTO)
4920/*
4921 * Update the position for all windows, using the width and height of the
4922 * frames.
4923 * Returns the row just after the last window.
4924 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004925 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926win_comp_pos()
4927{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004928 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 int col = 0;
4930
4931 frame_comp_pos(topframe, &row, &col);
4932 return row;
4933}
4934
4935/*
4936 * Update the position of the windows in frame "topfrp", using the width and
4937 * height of the frames.
4938 * "*row" and "*col" are the top-left position of the frame. They are updated
4939 * to the bottom-right position plus one.
4940 */
4941 static void
4942frame_comp_pos(topfrp, row, col)
4943 frame_T *topfrp;
4944 int *row;
4945 int *col;
4946{
4947 win_T *wp;
4948 frame_T *frp;
4949#ifdef FEAT_VERTSPLIT
4950 int startcol;
4951 int startrow;
4952#endif
4953
4954 wp = topfrp->fr_win;
4955 if (wp != NULL)
4956 {
4957 if (wp->w_winrow != *row
4958#ifdef FEAT_VERTSPLIT
4959 || wp->w_wincol != *col
4960#endif
4961 )
4962 {
4963 /* position changed, redraw */
4964 wp->w_winrow = *row;
4965#ifdef FEAT_VERTSPLIT
4966 wp->w_wincol = *col;
4967#endif
4968 redraw_win_later(wp, NOT_VALID);
4969 wp->w_redr_status = TRUE;
4970 }
4971 *row += wp->w_height + wp->w_status_height;
4972#ifdef FEAT_VERTSPLIT
4973 *col += wp->w_width + wp->w_vsep_width;
4974#endif
4975 }
4976 else
4977 {
4978#ifdef FEAT_VERTSPLIT
4979 startrow = *row;
4980 startcol = *col;
4981#endif
4982 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4983 {
4984#ifdef FEAT_VERTSPLIT
4985 if (topfrp->fr_layout == FR_ROW)
4986 *row = startrow; /* all frames are at the same row */
4987 else
4988 *col = startcol; /* all frames are at the same col */
4989#endif
4990 frame_comp_pos(frp, row, col);
4991 }
4992 }
4993}
4994
4995#endif /* FEAT_WINDOWS */
4996
4997/*
4998 * Set current window height and take care of repositioning other windows to
4999 * fit around it.
5000 */
5001 void
5002win_setheight(height)
5003 int height;
5004{
5005 win_setheight_win(height, curwin);
5006}
5007
5008/*
5009 * Set the window height of window "win" and take care of repositioning other
5010 * windows to fit around it.
5011 */
5012 void
5013win_setheight_win(height, win)
5014 int height;
5015 win_T *win;
5016{
5017 int row;
5018
5019 if (win == curwin)
5020 {
5021 /* Always keep current window at least one line high, even when
5022 * 'winminheight' is zero. */
5023#ifdef FEAT_WINDOWS
5024 if (height < p_wmh)
5025 height = p_wmh;
5026#endif
5027 if (height == 0)
5028 height = 1;
5029 }
5030
5031#ifdef FEAT_WINDOWS
5032 frame_setheight(win->w_frame, height + win->w_status_height);
5033
5034 /* recompute the window positions */
5035 row = win_comp_pos();
5036#else
5037 if (height > topframe->fr_height)
5038 height = topframe->fr_height;
5039 win->w_height = height;
5040 row = height;
5041#endif
5042
5043 /*
5044 * If there is extra space created between the last window and the command
5045 * line, clear it.
5046 */
5047 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5048 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5049 cmdline_row = row;
5050 msg_row = row;
5051 msg_col = 0;
5052
5053 redraw_all_later(NOT_VALID);
5054}
5055
5056#if defined(FEAT_WINDOWS) || defined(PROTO)
5057
5058/*
5059 * Set the height of a frame to "height" and take care that all frames and
5060 * windows inside it are resized. Also resize frames on the left and right if
5061 * the are in the same FR_ROW frame.
5062 *
5063 * Strategy:
5064 * If the frame is part of a FR_COL frame, try fitting the frame in that
5065 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5066 * go to containing frames to resize them and make room.
5067 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5068 * Check for the minimal height of the FR_ROW frame.
5069 * At the top level we can also use change the command line height.
5070 */
5071 static void
5072frame_setheight(curfrp, height)
5073 frame_T *curfrp;
5074 int height;
5075{
5076 int room; /* total number of lines available */
5077 int take; /* number of lines taken from other windows */
5078 int room_cmdline; /* lines available from cmdline */
5079 int run;
5080 frame_T *frp;
5081 int h;
5082 int room_reserved;
5083
5084 /* If the height already is the desired value, nothing to do. */
5085 if (curfrp->fr_height == height)
5086 return;
5087
5088 if (curfrp->fr_parent == NULL)
5089 {
5090 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005091 if (height > ROWS_AVAIL)
5092 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 if (height > 0)
5094 frame_new_height(curfrp, height, FALSE, FALSE);
5095 }
5096 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5097 {
5098 /* Row of frames: Also need to resize frames left and right of this
5099 * one. First check for the minimal height of these. */
5100 h = frame_minheight(curfrp->fr_parent, NULL);
5101 if (height < h)
5102 height = h;
5103 frame_setheight(curfrp->fr_parent, height);
5104 }
5105 else
5106 {
5107 /*
5108 * Column of frames: try to change only frames in this column.
5109 */
5110#ifdef FEAT_VERTSPLIT
5111 /*
5112 * Do this twice:
5113 * 1: compute room available, if it's not enough try resizing the
5114 * containing frame.
5115 * 2: compute the room available and adjust the height to it.
5116 * Try not to reduce the height of a window with 'winfixheight' set.
5117 */
5118 for (run = 1; run <= 2; ++run)
5119#else
5120 for (;;)
5121#endif
5122 {
5123 room = 0;
5124 room_reserved = 0;
5125 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5126 frp = frp->fr_next)
5127 {
5128 if (frp != curfrp
5129 && frp->fr_win != NULL
5130 && frp->fr_win->w_p_wfh)
5131 room_reserved += frp->fr_height;
5132 room += frp->fr_height;
5133 if (frp != curfrp)
5134 room -= frame_minheight(frp, NULL);
5135 }
5136#ifdef FEAT_VERTSPLIT
5137 if (curfrp->fr_width != Columns)
5138 room_cmdline = 0;
5139 else
5140#endif
5141 {
5142 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5143 + lastwin->w_height + lastwin->w_status_height);
5144 if (room_cmdline < 0)
5145 room_cmdline = 0;
5146 }
5147
5148 if (height <= room + room_cmdline)
5149 break;
5150#ifdef FEAT_VERTSPLIT
5151 if (run == 2 || curfrp->fr_width == Columns)
5152#endif
5153 {
5154 if (height > room + room_cmdline)
5155 height = room + room_cmdline;
5156 break;
5157 }
5158#ifdef FEAT_VERTSPLIT
5159 frame_setheight(curfrp->fr_parent, height
5160 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5161#endif
5162 /*NOTREACHED*/
5163 }
5164
5165 /*
5166 * Compute the number of lines we will take from others frames (can be
5167 * negative!).
5168 */
5169 take = height - curfrp->fr_height;
5170
5171 /* If there is not enough room, also reduce the height of a window
5172 * with 'winfixheight' set. */
5173 if (height > room + room_cmdline - room_reserved)
5174 room_reserved = room + room_cmdline - height;
5175 /* If there is only a 'winfixheight' window and making the
5176 * window smaller, need to make the other window taller. */
5177 if (take < 0 && room - curfrp->fr_height < room_reserved)
5178 room_reserved = 0;
5179
5180 if (take > 0 && room_cmdline > 0)
5181 {
5182 /* use lines from cmdline first */
5183 if (take < room_cmdline)
5184 room_cmdline = take;
5185 take -= room_cmdline;
5186 topframe->fr_height += room_cmdline;
5187 }
5188
5189 /*
5190 * set the current frame to the new height
5191 */
5192 frame_new_height(curfrp, height, FALSE, FALSE);
5193
5194 /*
5195 * First take lines from the frames after the current frame. If
5196 * that is not enough, takes lines from frames above the current
5197 * frame.
5198 */
5199 for (run = 0; run < 2; ++run)
5200 {
5201 if (run == 0)
5202 frp = curfrp->fr_next; /* 1st run: start with next window */
5203 else
5204 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5205 while (frp != NULL && take != 0)
5206 {
5207 h = frame_minheight(frp, NULL);
5208 if (room_reserved > 0
5209 && frp->fr_win != NULL
5210 && frp->fr_win->w_p_wfh)
5211 {
5212 if (room_reserved >= frp->fr_height)
5213 room_reserved -= frp->fr_height;
5214 else
5215 {
5216 if (frp->fr_height - room_reserved > take)
5217 room_reserved = frp->fr_height - take;
5218 take -= frp->fr_height - room_reserved;
5219 frame_new_height(frp, room_reserved, FALSE, FALSE);
5220 room_reserved = 0;
5221 }
5222 }
5223 else
5224 {
5225 if (frp->fr_height - take < h)
5226 {
5227 take -= frp->fr_height - h;
5228 frame_new_height(frp, h, FALSE, FALSE);
5229 }
5230 else
5231 {
5232 frame_new_height(frp, frp->fr_height - take,
5233 FALSE, FALSE);
5234 take = 0;
5235 }
5236 }
5237 if (run == 0)
5238 frp = frp->fr_next;
5239 else
5240 frp = frp->fr_prev;
5241 }
5242 }
5243 }
5244}
5245
5246#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5247/*
5248 * Set current window width and take care of repositioning other windows to
5249 * fit around it.
5250 */
5251 void
5252win_setwidth(width)
5253 int width;
5254{
5255 win_setwidth_win(width, curwin);
5256}
5257
5258 void
5259win_setwidth_win(width, wp)
5260 int width;
5261 win_T *wp;
5262{
5263 /* Always keep current window at least one column wide, even when
5264 * 'winminwidth' is zero. */
5265 if (wp == curwin)
5266 {
5267 if (width < p_wmw)
5268 width = p_wmw;
5269 if (width == 0)
5270 width = 1;
5271 }
5272
5273 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5274
5275 /* recompute the window positions */
5276 (void)win_comp_pos();
5277
5278 redraw_all_later(NOT_VALID);
5279}
5280
5281/*
5282 * Set the width of a frame to "width" and take care that all frames and
5283 * windows inside it are resized. Also resize frames above and below if the
5284 * are in the same FR_ROW frame.
5285 *
5286 * Strategy is similar to frame_setheight().
5287 */
5288 static void
5289frame_setwidth(curfrp, width)
5290 frame_T *curfrp;
5291 int width;
5292{
5293 int room; /* total number of lines available */
5294 int take; /* number of lines taken from other windows */
5295 int run;
5296 frame_T *frp;
5297 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005298 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
5300 /* If the width already is the desired value, nothing to do. */
5301 if (curfrp->fr_width == width)
5302 return;
5303
5304 if (curfrp->fr_parent == NULL)
5305 /* topframe: can't change width */
5306 return;
5307
5308 if (curfrp->fr_parent->fr_layout == FR_COL)
5309 {
5310 /* Column of frames: Also need to resize frames above and below of
5311 * this one. First check for the minimal width of these. */
5312 w = frame_minwidth(curfrp->fr_parent, NULL);
5313 if (width < w)
5314 width = w;
5315 frame_setwidth(curfrp->fr_parent, width);
5316 }
5317 else
5318 {
5319 /*
5320 * Row of frames: try to change only frames in this row.
5321 *
5322 * Do this twice:
5323 * 1: compute room available, if it's not enough try resizing the
5324 * containing frame.
5325 * 2: compute the room available and adjust the width to it.
5326 */
5327 for (run = 1; run <= 2; ++run)
5328 {
5329 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005330 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5332 frp = frp->fr_next)
5333 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005334 if (frp != curfrp
5335 && frp->fr_win != NULL
5336 && frp->fr_win->w_p_wfw)
5337 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 room += frp->fr_width;
5339 if (frp != curfrp)
5340 room -= frame_minwidth(frp, NULL);
5341 }
5342
5343 if (width <= room)
5344 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005345 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 if (width > room)
5348 width = room;
5349 break;
5350 }
5351 frame_setwidth(curfrp->fr_parent, width
5352 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5353 }
5354
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 /*
5356 * Compute the number of lines we will take from others frames (can be
5357 * negative!).
5358 */
5359 take = width - curfrp->fr_width;
5360
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005361 /* If there is not enough room, also reduce the width of a window
5362 * with 'winfixwidth' set. */
5363 if (width > room - room_reserved)
5364 room_reserved = room - width;
5365 /* If there is only a 'winfixwidth' window and making the
5366 * window smaller, need to make the other window narrower. */
5367 if (take < 0 && room - curfrp->fr_width < room_reserved)
5368 room_reserved = 0;
5369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 /*
5371 * set the current frame to the new width
5372 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005373 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
5375 /*
5376 * First take lines from the frames right of the current frame. If
5377 * that is not enough, takes lines from frames left of the current
5378 * frame.
5379 */
5380 for (run = 0; run < 2; ++run)
5381 {
5382 if (run == 0)
5383 frp = curfrp->fr_next; /* 1st run: start with next window */
5384 else
5385 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5386 while (frp != NULL && take != 0)
5387 {
5388 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005389 if (room_reserved > 0
5390 && frp->fr_win != NULL
5391 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005393 if (room_reserved >= frp->fr_width)
5394 room_reserved -= frp->fr_width;
5395 else
5396 {
5397 if (frp->fr_width - room_reserved > take)
5398 room_reserved = frp->fr_width - take;
5399 take -= frp->fr_width - room_reserved;
5400 frame_new_width(frp, room_reserved, FALSE, FALSE);
5401 room_reserved = 0;
5402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404 else
5405 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005406 if (frp->fr_width - take < w)
5407 {
5408 take -= frp->fr_width - w;
5409 frame_new_width(frp, w, FALSE, FALSE);
5410 }
5411 else
5412 {
5413 frame_new_width(frp, frp->fr_width - take,
5414 FALSE, FALSE);
5415 take = 0;
5416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 if (run == 0)
5419 frp = frp->fr_next;
5420 else
5421 frp = frp->fr_prev;
5422 }
5423 }
5424 }
5425}
5426#endif /* FEAT_VERTSPLIT */
5427
5428/*
5429 * Check 'winminheight' for a valid value.
5430 */
5431 void
5432win_setminheight()
5433{
5434 int room;
5435 int first = TRUE;
5436 win_T *wp;
5437
5438 /* loop until there is a 'winminheight' that is possible */
5439 while (p_wmh > 0)
5440 {
5441 /* TODO: handle vertical splits */
5442 room = -p_wh;
5443 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5444 room += wp->w_height - p_wmh;
5445 if (room >= 0)
5446 break;
5447 --p_wmh;
5448 if (first)
5449 {
5450 EMSG(_(e_noroom));
5451 first = FALSE;
5452 }
5453 }
5454}
5455
5456#ifdef FEAT_MOUSE
5457
5458/*
5459 * Status line of dragwin is dragged "offset" lines down (negative is up).
5460 */
5461 void
5462win_drag_status_line(dragwin, offset)
5463 win_T *dragwin;
5464 int offset;
5465{
5466 frame_T *curfr;
5467 frame_T *fr;
5468 int room;
5469 int row;
5470 int up; /* if TRUE, drag status line up, otherwise down */
5471 int n;
5472
5473 fr = dragwin->w_frame;
5474 curfr = fr;
5475 if (fr != topframe) /* more than one window */
5476 {
5477 fr = fr->fr_parent;
5478 /* When the parent frame is not a column of frames, its parent should
5479 * be. */
5480 if (fr->fr_layout != FR_COL)
5481 {
5482 curfr = fr;
5483 if (fr != topframe) /* only a row of windows, may drag statusline */
5484 fr = fr->fr_parent;
5485 }
5486 }
5487
5488 /* If this is the last frame in a column, may want to resize the parent
5489 * frame instead (go two up to skip a row of frames). */
5490 while (curfr != topframe && curfr->fr_next == NULL)
5491 {
5492 if (fr != topframe)
5493 fr = fr->fr_parent;
5494 curfr = fr;
5495 if (fr != topframe)
5496 fr = fr->fr_parent;
5497 }
5498
5499 if (offset < 0) /* drag up */
5500 {
5501 up = TRUE;
5502 offset = -offset;
5503 /* sum up the room of the current frame and above it */
5504 if (fr == curfr)
5505 {
5506 /* only one window */
5507 room = fr->fr_height - frame_minheight(fr, NULL);
5508 }
5509 else
5510 {
5511 room = 0;
5512 for (fr = fr->fr_child; ; fr = fr->fr_next)
5513 {
5514 room += fr->fr_height - frame_minheight(fr, NULL);
5515 if (fr == curfr)
5516 break;
5517 }
5518 }
5519 fr = curfr->fr_next; /* put fr at frame that grows */
5520 }
5521 else /* drag down */
5522 {
5523 up = FALSE;
5524 /*
5525 * Only dragging the last status line can reduce p_ch.
5526 */
5527 room = Rows - cmdline_row;
5528 if (curfr->fr_next == NULL)
5529 room -= 1;
5530 else
5531 room -= p_ch;
5532 if (room < 0)
5533 room = 0;
5534 /* sum up the room of frames below of the current one */
5535 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5536 room += fr->fr_height - frame_minheight(fr, NULL);
5537 fr = curfr; /* put fr at window that grows */
5538 }
5539
5540 if (room < offset) /* Not enough room */
5541 offset = room; /* Move as far as we can */
5542 if (offset <= 0)
5543 return;
5544
5545 /*
5546 * Grow frame fr by "offset" lines.
5547 * Doesn't happen when dragging the last status line up.
5548 */
5549 if (fr != NULL)
5550 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5551
5552 if (up)
5553 fr = curfr; /* current frame gets smaller */
5554 else
5555 fr = curfr->fr_next; /* next frame gets smaller */
5556
5557 /*
5558 * Now make the other frames smaller.
5559 */
5560 while (fr != NULL && offset > 0)
5561 {
5562 n = frame_minheight(fr, NULL);
5563 if (fr->fr_height - offset <= n)
5564 {
5565 offset -= fr->fr_height - n;
5566 frame_new_height(fr, n, !up, FALSE);
5567 }
5568 else
5569 {
5570 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5571 break;
5572 }
5573 if (up)
5574 fr = fr->fr_prev;
5575 else
5576 fr = fr->fr_next;
5577 }
5578 row = win_comp_pos();
5579 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5580 cmdline_row = row;
5581 p_ch = Rows - cmdline_row;
5582 if (p_ch < 1)
5583 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005584 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005585 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 showmode();
5587}
5588
5589#ifdef FEAT_VERTSPLIT
5590/*
5591 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5592 */
5593 void
5594win_drag_vsep_line(dragwin, offset)
5595 win_T *dragwin;
5596 int offset;
5597{
5598 frame_T *curfr;
5599 frame_T *fr;
5600 int room;
5601 int left; /* if TRUE, drag separator line left, otherwise right */
5602 int n;
5603
5604 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005605 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 return;
5607 curfr = fr;
5608 fr = fr->fr_parent;
5609 /* When the parent frame is not a row of frames, its parent should be. */
5610 if (fr->fr_layout != FR_ROW)
5611 {
5612 if (fr == topframe) /* only a column of windows (cannot happen?) */
5613 return;
5614 curfr = fr;
5615 fr = fr->fr_parent;
5616 }
5617
5618 /* If this is the last frame in a row, may want to resize a parent
5619 * frame instead. */
5620 while (curfr->fr_next == NULL)
5621 {
5622 if (fr == topframe)
5623 break;
5624 curfr = fr;
5625 fr = fr->fr_parent;
5626 if (fr != topframe)
5627 {
5628 curfr = fr;
5629 fr = fr->fr_parent;
5630 }
5631 }
5632
5633 if (offset < 0) /* drag left */
5634 {
5635 left = TRUE;
5636 offset = -offset;
5637 /* sum up the room of the current frame and left of it */
5638 room = 0;
5639 for (fr = fr->fr_child; ; fr = fr->fr_next)
5640 {
5641 room += fr->fr_width - frame_minwidth(fr, NULL);
5642 if (fr == curfr)
5643 break;
5644 }
5645 fr = curfr->fr_next; /* put fr at frame that grows */
5646 }
5647 else /* drag right */
5648 {
5649 left = FALSE;
5650 /* sum up the room of frames right of the current one */
5651 room = 0;
5652 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5653 room += fr->fr_width - frame_minwidth(fr, NULL);
5654 fr = curfr; /* put fr at window that grows */
5655 }
5656
5657 if (room < offset) /* Not enough room */
5658 offset = room; /* Move as far as we can */
5659 if (offset <= 0) /* No room at all, quit. */
5660 return;
5661
5662 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005663 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
5665 /* shrink other frames: current and at the left or at the right */
5666 if (left)
5667 fr = curfr; /* current frame gets smaller */
5668 else
5669 fr = curfr->fr_next; /* next frame gets smaller */
5670
5671 while (fr != NULL && offset > 0)
5672 {
5673 n = frame_minwidth(fr, NULL);
5674 if (fr->fr_width - offset <= n)
5675 {
5676 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005677 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else
5680 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005681 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683 }
5684 if (left)
5685 fr = fr->fr_prev;
5686 else
5687 fr = fr->fr_next;
5688 }
5689 (void)win_comp_pos();
5690 redraw_all_later(NOT_VALID);
5691}
5692#endif /* FEAT_VERTSPLIT */
5693#endif /* FEAT_MOUSE */
5694
5695#endif /* FEAT_WINDOWS */
5696
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005697#define FRACTION_MULT 16384L
5698
5699/*
5700 * Set wp->w_fraction for the current w_wrow and w_height.
5701 */
5702 static void
5703set_fraction(wp)
5704 win_T *wp;
5705{
5706 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005707 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005708}
5709
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710/*
5711 * Set the height of a window.
5712 * This takes care of the things inside the window, not what happens to the
5713 * window position, the frame or to other windows.
5714 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005715 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716win_new_height(wp, height)
5717 win_T *wp;
5718 int height;
5719{
5720 linenr_T lnum;
5721 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005722 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724 /* Don't want a negative height. Happens when splitting a tiny window.
5725 * Will equalize heights soon to fix it. */
5726 if (height < 0)
5727 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005728 if (wp->w_height == height)
5729 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005731 if (wp->w_height > 0)
5732 {
5733 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005734 /* w_wrow needs to be valid. When setting 'laststatus' this may
5735 * call win_new_height() recursively. */
5736 validate_cursor();
5737 if (wp->w_height != prev_height)
5738 return; /* Recursive call already changed the size, bail out here
5739 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005740 if (wp->w_wrow != wp->w_prev_fraction_row)
5741 set_fraction(wp);
5742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
5744 wp->w_height = height;
5745 wp->w_skipcol = 0;
5746
5747 /* Don't change w_topline when height is zero. Don't set w_topline when
5748 * 'scrollbind' is set and this isn't the current window. */
5749 if (height > 0
5750#ifdef FEAT_SCROLLBIND
5751 && (!wp->w_p_scb || wp == curwin)
5752#endif
5753 )
5754 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005755 /*
5756 * Find a value for w_topline that shows the cursor at the same
5757 * relative position in the window as before (more or less).
5758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 lnum = wp->w_cursor.lnum;
5760 if (lnum < 1) /* can happen when starting up */
5761 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005762 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5763 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5765 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005766
5767 if (sline >= 0)
5768 {
5769 /* Make sure the whole cursor line is visible, if possible. */
5770 int rows = plines_win(wp, lnum, FALSE);
5771
5772 if (sline > wp->w_height - rows)
5773 {
5774 sline = wp->w_height - rows;
5775 wp->w_wrow -= rows - line_size;
5776 }
5777 }
5778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 if (sline < 0)
5780 {
5781 /*
5782 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005783 * Make cursor line the first line in the window. If not enough
5784 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 */
5786 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005787 if (wp->w_wrow >= wp->w_height
5788 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5789 {
5790 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5791 --wp->w_wrow;
5792 while (wp->w_wrow >= wp->w_height)
5793 {
5794 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5795 + win_col_off2(wp);
5796 --wp->w_wrow;
5797 }
5798 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005799 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005801 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005803 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {
5805#ifdef FEAT_FOLDING
5806 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5807 if (lnum == 1)
5808 {
5809 /* first line in buffer is folded */
5810 line_size = 1;
5811 --sline;
5812 break;
5813 }
5814#endif
5815 --lnum;
5816#ifdef FEAT_DIFF
5817 if (lnum == wp->w_topline)
5818 line_size = plines_win_nofill(wp, lnum, TRUE)
5819 + wp->w_topfill;
5820 else
5821#endif
5822 line_size = plines_win(wp, lnum, TRUE);
5823 sline -= line_size;
5824 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 if (sline < 0)
5827 {
5828 /*
5829 * Line we want at top would go off top of screen. Use next
5830 * line instead.
5831 */
5832#ifdef FEAT_FOLDING
5833 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5834#endif
5835 lnum++;
5836 wp->w_wrow -= line_size + sline;
5837 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005838 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
5840 /* First line of file reached, use that as topline. */
5841 lnum = 1;
5842 wp->w_wrow -= sline;
5843 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005844
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005845 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 }
5848
5849 if (wp == curwin)
5850 {
5851 if (p_so)
5852 update_topline();
5853 curs_columns(FALSE); /* validate w_wrow */
5854 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005855 if (prev_height > 0)
5856 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
5858 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005859 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860#ifdef FEAT_WINDOWS
5861 wp->w_redr_status = TRUE;
5862#endif
5863 invalidate_botline_win(wp);
5864}
5865
5866#ifdef FEAT_VERTSPLIT
5867/*
5868 * Set the width of a window.
5869 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005870 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871win_new_width(wp, width)
5872 win_T *wp;
5873 int width;
5874{
5875 wp->w_width = width;
5876 wp->w_lines_valid = 0;
5877 changed_line_abv_curs_win(wp);
5878 invalidate_botline_win(wp);
5879 if (wp == curwin)
5880 {
5881 update_topline();
5882 curs_columns(TRUE); /* validate w_wrow */
5883 }
5884 redraw_win_later(wp, NOT_VALID);
5885 wp->w_redr_status = TRUE;
5886}
5887#endif
5888
5889 void
5890win_comp_scroll(wp)
5891 win_T *wp;
5892{
5893 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5894 if (wp->w_p_scr == 0)
5895 wp->w_p_scr = 1;
5896}
5897
5898/*
5899 * command_height: called whenever p_ch has been changed
5900 */
5901 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005902command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
5904#ifdef FEAT_WINDOWS
5905 int h;
5906 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005907 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005909 /* Use the value of p_ch that we remembered. This is needed for when the
5910 * GUI starts up, we can't be sure in what order things happen. And when
5911 * p_ch was changed in another tab page. */
5912 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005913
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 /* Find bottom frame with width of screen. */
5915 frp = lastwin->w_frame;
5916# ifdef FEAT_VERTSPLIT
5917 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5918 frp = frp->fr_parent;
5919# endif
5920
5921 /* Avoid changing the height of a window with 'winfixheight' set. */
5922 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5923 && frp->fr_win->w_p_wfh)
5924 frp = frp->fr_prev;
5925
5926 if (starting != NO_SCREEN)
5927 {
5928 cmdline_row = Rows - p_ch;
5929
5930 if (p_ch > old_p_ch) /* p_ch got bigger */
5931 {
5932 while (p_ch > old_p_ch)
5933 {
5934 if (frp == NULL)
5935 {
5936 EMSG(_(e_noroom));
5937 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005938 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 cmdline_row = Rows - p_ch;
5940 break;
5941 }
5942 h = frp->fr_height - frame_minheight(frp, NULL);
5943 if (h > p_ch - old_p_ch)
5944 h = p_ch - old_p_ch;
5945 old_p_ch += h;
5946 frame_add_height(frp, -h);
5947 frp = frp->fr_prev;
5948 }
5949
5950 /* Recompute window positions. */
5951 (void)win_comp_pos();
5952
5953 /* clear the lines added to cmdline */
5954 if (full_screen)
5955 screen_fill((int)(cmdline_row), (int)Rows, 0,
5956 (int)Columns, ' ', ' ', 0);
5957 msg_row = cmdline_row;
5958 redraw_cmdline = TRUE;
5959 return;
5960 }
5961
5962 if (msg_row < cmdline_row)
5963 msg_row = cmdline_row;
5964 redraw_cmdline = TRUE;
5965 }
5966 frame_add_height(frp, (int)(old_p_ch - p_ch));
5967
5968 /* Recompute window positions. */
5969 if (frp != lastwin->w_frame)
5970 (void)win_comp_pos();
5971#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005973 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974#endif
5975}
5976
5977#if defined(FEAT_WINDOWS) || defined(PROTO)
5978/*
5979 * Resize frame "frp" to be "n" lines higher (negative for less high).
5980 * Also resize the frames it is contained in.
5981 */
5982 static void
5983frame_add_height(frp, n)
5984 frame_T *frp;
5985 int n;
5986{
5987 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5988 for (;;)
5989 {
5990 frp = frp->fr_parent;
5991 if (frp == NULL)
5992 break;
5993 frp->fr_height += n;
5994 }
5995}
5996
5997/*
5998 * Add or remove a status line for the bottom window(s), according to the
5999 * value of 'laststatus'.
6000 */
6001 void
6002last_status(morewin)
6003 int morewin; /* pretend there are two or more windows */
6004{
6005 /* Don't make a difference between horizontal or vertical split. */
6006 last_status_rec(topframe, (p_ls == 2
6007 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6008}
6009
6010 static void
6011last_status_rec(fr, statusline)
6012 frame_T *fr;
6013 int statusline;
6014{
6015 frame_T *fp;
6016 win_T *wp;
6017
6018 if (fr->fr_layout == FR_LEAF)
6019 {
6020 wp = fr->fr_win;
6021 if (wp->w_status_height != 0 && !statusline)
6022 {
6023 /* remove status line */
6024 win_new_height(wp, wp->w_height + 1);
6025 wp->w_status_height = 0;
6026 comp_col();
6027 }
6028 else if (wp->w_status_height == 0 && statusline)
6029 {
6030 /* Find a frame to take a line from. */
6031 fp = fr;
6032 while (fp->fr_height <= frame_minheight(fp, NULL))
6033 {
6034 if (fp == topframe)
6035 {
6036 EMSG(_(e_noroom));
6037 return;
6038 }
6039 /* In a column of frames: go to frame above. If already at
6040 * the top or in a row of frames: go to parent. */
6041 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6042 fp = fp->fr_prev;
6043 else
6044 fp = fp->fr_parent;
6045 }
6046 wp->w_status_height = 1;
6047 if (fp != fr)
6048 {
6049 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6050 frame_fix_height(wp);
6051 (void)win_comp_pos();
6052 }
6053 else
6054 win_new_height(wp, wp->w_height - 1);
6055 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006056 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 }
6058 }
6059#ifdef FEAT_VERTSPLIT
6060 else if (fr->fr_layout == FR_ROW)
6061 {
6062 /* vertically split windows, set status line for each one */
6063 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6064 last_status_rec(fp, statusline);
6065 }
6066#endif
6067 else
6068 {
6069 /* horizontally split window, set status line for last one */
6070 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6071 ;
6072 last_status_rec(fp, statusline);
6073 }
6074}
6075
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006076/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006077 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006078 */
6079 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006080tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006081{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006082#ifdef FEAT_GUI_TABLINE
6083 /* When the GUI has the tabline then this always returns zero. */
6084 if (gui_use_tabline())
6085 return 0;
6086#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006087 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006088 {
6089 case 0: return 0;
6090 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6091 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006092 return 1;
6093}
6094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095#endif /* FEAT_WINDOWS */
6096
6097#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6098/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006099 * Get the file name at the cursor.
6100 * If Visual mode is active, use the selected text if it's in one line.
6101 * Returns the name in allocated memory, NULL for failure.
6102 */
6103 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006104grab_file_name(count, file_lnum)
6105 long count;
6106 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006107{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006108 if (VIsual_active)
6109 {
6110 int len;
6111 char_u *ptr;
6112
6113 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6114 return NULL;
6115 return find_file_name_in_path(ptr, len,
6116 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6117 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006118 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6119 file_lnum);
6120
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006121}
6122
6123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 * Return the file name under or after the cursor.
6125 *
6126 * The 'path' option is searched if the file name is not absolute.
6127 * The string returned has been alloc'ed and should be freed by the caller.
6128 * NULL is returned if the file name or file is not found.
6129 *
6130 * options:
6131 * FNAME_MESS give error messages
6132 * FNAME_EXP expand to path
6133 * FNAME_HYP check for hypertext link
6134 * FNAME_INCL apply "includeexpr"
6135 */
6136 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006137file_name_at_cursor(options, count, file_lnum)
6138 int options;
6139 long count;
6140 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
6142 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006143 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6144 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145}
6146
6147/*
6148 * Return the name of the file under or after ptr[col].
6149 * Otherwise like file_name_at_cursor().
6150 */
6151 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006152file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 char_u *line;
6154 int col;
6155 int options;
6156 long count;
6157 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006158 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
6160 char_u *ptr;
6161 int len;
6162
6163 /*
6164 * search forward for what could be the start of a file name
6165 */
6166 ptr = line + col;
6167 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006168 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (*ptr == NUL) /* nothing found */
6170 {
6171 if (options & FNAME_MESS)
6172 EMSG(_("E446: No file name under cursor"));
6173 return NULL;
6174 }
6175
6176 /*
6177 * Search backward for first char of the file name.
6178 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6179 */
6180 while (ptr > line)
6181 {
6182#ifdef FEAT_MBYTE
6183 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6184 ptr -= len + 1;
6185 else
6186#endif
6187 if (vim_isfilec(ptr[-1])
6188 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6189 --ptr;
6190 else
6191 break;
6192 }
6193
6194 /*
6195 * Search forward for the last char of the file name.
6196 * Also allow "://" when ':' is not in 'isfname'.
6197 */
6198 len = 0;
6199 while (vim_isfilec(ptr[len])
6200 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6201#ifdef FEAT_MBYTE
6202 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006203 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 else
6205#endif
6206 ++len;
6207
6208 /*
6209 * If there is trailing punctuation, remove it.
6210 * But don't remove "..", could be a directory name.
6211 */
6212 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6213 && ptr[len - 2] != '.')
6214 --len;
6215
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006216 if (file_lnum != NULL)
6217 {
6218 char_u *p;
6219
6220 /* Get the number after the file name and a separator character */
6221 p = ptr + len;
6222 p = skipwhite(p);
6223 if (*p != NUL)
6224 {
6225 if (!isdigit(*p))
6226 ++p; /* skip the separator */
6227 p = skipwhite(p);
6228 if (isdigit(*p))
6229 *file_lnum = (int)getdigits(&p);
6230 }
6231 }
6232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6234}
6235
6236# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6237static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6238
6239 static char_u *
6240eval_includeexpr(ptr, len)
6241 char_u *ptr;
6242 int len;
6243{
6244 char_u *res;
6245
6246 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006247 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006248 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 set_vim_var_string(VV_FNAME, NULL, 0);
6250 return res;
6251}
6252#endif
6253
6254/*
6255 * Return the name of the file ptr[len] in 'path'.
6256 * Otherwise like file_name_at_cursor().
6257 */
6258 char_u *
6259find_file_name_in_path(ptr, len, options, count, rel_fname)
6260 char_u *ptr;
6261 int len;
6262 int options;
6263 long count;
6264 char_u *rel_fname; /* file we are searching relative to */
6265{
6266 char_u *file_name;
6267 int c;
6268# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6269 char_u *tofree = NULL;
6270
6271 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6272 {
6273 tofree = eval_includeexpr(ptr, len);
6274 if (tofree != NULL)
6275 {
6276 ptr = tofree;
6277 len = (int)STRLEN(ptr);
6278 }
6279 }
6280# endif
6281
6282 if (options & FNAME_EXP)
6283 {
6284 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6285 TRUE, rel_fname);
6286
6287# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6288 /*
6289 * If the file could not be found in a normal way, try applying
6290 * 'includeexpr' (unless done already).
6291 */
6292 if (file_name == NULL
6293 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6294 {
6295 tofree = eval_includeexpr(ptr, len);
6296 if (tofree != NULL)
6297 {
6298 ptr = tofree;
6299 len = (int)STRLEN(ptr);
6300 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6301 TRUE, rel_fname);
6302 }
6303 }
6304# endif
6305 if (file_name == NULL && (options & FNAME_MESS))
6306 {
6307 c = ptr[len];
6308 ptr[len] = NUL;
6309 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6310 ptr[len] = c;
6311 }
6312
6313 /* Repeat finding the file "count" times. This matters when it
6314 * appears several times in the path. */
6315 while (file_name != NULL && --count > 0)
6316 {
6317 vim_free(file_name);
6318 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6319 }
6320 }
6321 else
6322 file_name = vim_strnsave(ptr, len);
6323
6324# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6325 vim_free(tofree);
6326# endif
6327
6328 return file_name;
6329}
6330#endif /* FEAT_SEARCHPATH */
6331
6332/*
6333 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6334 * Also check for ":\\", which MS Internet Explorer accepts, return
6335 * URL_BACKSLASH.
6336 */
6337 static int
6338path_is_url(p)
6339 char_u *p;
6340{
6341 if (STRNCMP(p, "://", (size_t)3) == 0)
6342 return URL_SLASH;
6343 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6344 return URL_BACKSLASH;
6345 return 0;
6346}
6347
6348/*
6349 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6350 * Return URL_BACKSLASH for "name:\\".
6351 * Return zero otherwise.
6352 */
6353 int
6354path_with_url(fname)
6355 char_u *fname;
6356{
6357 char_u *p;
6358
6359 for (p = fname; isalpha(*p); ++p)
6360 ;
6361 return path_is_url(p);
6362}
6363
6364/*
6365 * Return TRUE if "name" is a full (absolute) path name or URL.
6366 */
6367 int
6368vim_isAbsName(name)
6369 char_u *name;
6370{
6371 return (path_with_url(name) != 0 || mch_isFullName(name));
6372}
6373
6374/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006375 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 *
6377 * return FAIL for failure, OK otherwise
6378 */
6379 int
6380vim_FullName(fname, buf, len, force)
6381 char_u *fname, *buf;
6382 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006383 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 int retval = OK;
6386 int url;
6387
6388 *buf = NUL;
6389 if (fname == NULL)
6390 return FAIL;
6391
6392 url = path_with_url(fname);
6393 if (!url)
6394 retval = mch_FullName(fname, buf, len, force);
6395 if (url || retval == FAIL)
6396 {
6397 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006398 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
6400#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6401 slash_adjust(buf);
6402#endif
6403 return retval;
6404}
6405
6406/*
6407 * Return the minimal number of rows that is needed on the screen to display
6408 * the current number of windows.
6409 */
6410 int
6411min_rows()
6412{
6413 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006414#ifdef FEAT_WINDOWS
6415 tabpage_T *tp;
6416 int n;
6417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
6419 if (firstwin == NULL) /* not initialized yet */
6420 return MIN_LINES;
6421
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006423 total = 0;
6424 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6425 {
6426 n = frame_minheight(tp->tp_topframe, NULL);
6427 if (total < n)
6428 total = n;
6429 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006430 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006432 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006434 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 return total;
6436}
6437
6438/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006439 * Return TRUE if there is only one window (in the current tab page), not
6440 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006441 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 */
6443 int
6444only_one_window()
6445{
6446#ifdef FEAT_WINDOWS
6447 int count = 0;
6448 win_T *wp;
6449
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006450 /* If there is another tab page there always is another window. */
6451 if (first_tabpage->tp_next != NULL)
6452 return FALSE;
6453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006455 if (wp->w_buffer != NULL
6456 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457# ifdef FEAT_QUICKFIX
6458 || wp->w_p_pvw
6459# endif
6460 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006461# ifdef FEAT_AUTOCMD
6462 && wp != aucmd_win
6463# endif
6464 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 ++count;
6466 return (count <= 1);
6467#else
6468 return TRUE;
6469#endif
6470}
6471
6472#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6473/*
6474 * Correct the cursor line number in other windows. Used after changing the
6475 * current buffer, and before applying autocommands.
6476 * When "do_curwin" is TRUE, also check current window.
6477 */
6478 void
6479check_lnums(do_curwin)
6480 int do_curwin;
6481{
6482 win_T *wp;
6483
6484#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006485 tabpage_T *tp;
6486
6487 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6489#else
6490 wp = curwin;
6491 if (do_curwin)
6492#endif
6493 {
6494 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6495 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6496 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6497 wp->w_topline = curbuf->b_ml.ml_line_count;
6498 }
6499}
6500#endif
6501
6502#if defined(FEAT_WINDOWS) || defined(PROTO)
6503
6504/*
6505 * A snapshot of the window sizes, to restore them after closing the help
6506 * window.
6507 * Only these fields are used:
6508 * fr_layout
6509 * fr_width
6510 * fr_height
6511 * fr_next
6512 * fr_child
6513 * fr_win (only valid for the old curwin, NULL otherwise)
6514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515
6516/*
6517 * Create a snapshot of the current frame sizes.
6518 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006519 void
6520make_snapshot(idx)
6521 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006523 clear_snapshot(curtab, idx);
6524 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525}
6526
6527 static void
6528make_snapshot_rec(fr, frp)
6529 frame_T *fr;
6530 frame_T **frp;
6531{
6532 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6533 if (*frp == NULL)
6534 return;
6535 (*frp)->fr_layout = fr->fr_layout;
6536# ifdef FEAT_VERTSPLIT
6537 (*frp)->fr_width = fr->fr_width;
6538# endif
6539 (*frp)->fr_height = fr->fr_height;
6540 if (fr->fr_next != NULL)
6541 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6542 if (fr->fr_child != NULL)
6543 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6544 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6545 (*frp)->fr_win = curwin;
6546}
6547
6548/*
6549 * Remove any existing snapshot.
6550 */
6551 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006552clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006553 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006554 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006556 clear_snapshot_rec(tp->tp_snapshot[idx]);
6557 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558}
6559
6560 static void
6561clear_snapshot_rec(fr)
6562 frame_T *fr;
6563{
6564 if (fr != NULL)
6565 {
6566 clear_snapshot_rec(fr->fr_next);
6567 clear_snapshot_rec(fr->fr_child);
6568 vim_free(fr);
6569 }
6570}
6571
6572/*
6573 * Restore a previously created snapshot, if there is any.
6574 * This is only done if the screen size didn't change and the window layout is
6575 * still the same.
6576 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006577 void
6578restore_snapshot(idx, close_curwin)
6579 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 int close_curwin; /* closing current window */
6581{
6582 win_T *wp;
6583
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006584 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006586 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006588 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6589 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006591 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 win_comp_pos();
6593 if (wp != NULL && close_curwin)
6594 win_goto(wp);
6595 redraw_all_later(CLEAR);
6596 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006597 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598}
6599
6600/*
6601 * Check if frames "sn" and "fr" have the same layout, same following frames
6602 * and same children.
6603 */
6604 static int
6605check_snapshot_rec(sn, fr)
6606 frame_T *sn;
6607 frame_T *fr;
6608{
6609 if (sn->fr_layout != fr->fr_layout
6610 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6611 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6612 || (sn->fr_next != NULL
6613 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6614 || (sn->fr_child != NULL
6615 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6616 return FAIL;
6617 return OK;
6618}
6619
6620/*
6621 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6622 * following frames and children.
6623 * Returns a pointer to the old current window, or NULL.
6624 */
6625 static win_T *
6626restore_snapshot_rec(sn, fr)
6627 frame_T *sn;
6628 frame_T *fr;
6629{
6630 win_T *wp = NULL;
6631 win_T *wp2;
6632
6633 fr->fr_height = sn->fr_height;
6634# ifdef FEAT_VERTSPLIT
6635 fr->fr_width = sn->fr_width;
6636# endif
6637 if (fr->fr_layout == FR_LEAF)
6638 {
6639 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6640# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006641 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642# endif
6643 wp = sn->fr_win;
6644 }
6645 if (sn->fr_next != NULL)
6646 {
6647 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6648 if (wp2 != NULL)
6649 wp = wp2;
6650 }
6651 if (sn->fr_child != NULL)
6652 {
6653 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6654 if (wp2 != NULL)
6655 wp = wp2;
6656 }
6657 return wp;
6658}
6659
6660#endif
6661
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006662#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6663 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006664/*
6665 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006666 * restore_win() MUST be called to undo, also when FAIL is returned.
6667 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006668 * When "no_display" is TRUE the display won't be affected, no redraw is
6669 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006670 * Returns FAIL if switching to "win" failed.
6671 */
6672 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006673switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006674 win_T **save_curwin UNUSED;
6675 tabpage_T **save_curtab UNUSED;
6676 win_T *win UNUSED;
6677 tabpage_T *tp UNUSED;
6678 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006679{
6680# ifdef FEAT_AUTOCMD
6681 block_autocmds();
6682# endif
6683# ifdef FEAT_WINDOWS
6684 *save_curwin = curwin;
6685 if (tp != NULL)
6686 {
6687 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006688 if (no_display)
6689 {
6690 curtab->tp_firstwin = firstwin;
6691 curtab->tp_lastwin = lastwin;
6692 curtab = tp;
6693 firstwin = curtab->tp_firstwin;
6694 lastwin = curtab->tp_lastwin;
6695 }
6696 else
6697 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006698 }
6699 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006700 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006701 curwin = win;
6702 curbuf = curwin->w_buffer;
6703# endif
6704 return OK;
6705}
6706
6707/*
6708 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006709 * When "no_display" is TRUE the display won't be affected, no redraw is
6710 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006711 */
6712 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006713restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006714 win_T *save_curwin UNUSED;
6715 tabpage_T *save_curtab UNUSED;
6716 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006717{
6718# ifdef FEAT_WINDOWS
6719 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006720 {
6721 if (no_display)
6722 {
6723 curtab->tp_firstwin = firstwin;
6724 curtab->tp_lastwin = lastwin;
6725 curtab = save_curtab;
6726 firstwin = curtab->tp_firstwin;
6727 lastwin = curtab->tp_lastwin;
6728 }
6729 else
6730 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6731 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006732 if (win_valid(save_curwin))
6733 {
6734 curwin = save_curwin;
6735 curbuf = curwin->w_buffer;
6736 }
6737# endif
6738# ifdef FEAT_AUTOCMD
6739 unblock_autocmds();
6740# endif
6741}
6742
6743/*
6744 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6745 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6746 */
6747 void
6748switch_buffer(save_curbuf, buf)
6749 buf_T *buf;
6750 buf_T **save_curbuf;
6751{
6752# ifdef FEAT_AUTOCMD
6753 block_autocmds();
6754# endif
6755 *save_curbuf = curbuf;
6756 --curbuf->b_nwindows;
6757 curbuf = buf;
6758 curwin->w_buffer = buf;
6759 ++curbuf->b_nwindows;
6760}
6761
6762/*
6763 * Restore the current buffer after using switch_buffer().
6764 */
6765 void
6766restore_buffer(save_curbuf)
6767 buf_T *save_curbuf;
6768{
6769# ifdef FEAT_AUTOCMD
6770 unblock_autocmds();
6771# endif
6772 /* Check for valid buffer, just in case. */
6773 if (buf_valid(save_curbuf))
6774 {
6775 --curbuf->b_nwindows;
6776 curwin->w_buffer = save_curbuf;
6777 curbuf = save_curbuf;
6778 ++curbuf->b_nwindows;
6779 }
6780}
6781#endif
6782
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6784/*
6785 * Return TRUE if there is any vertically split window.
6786 */
6787 int
6788win_hasvertsplit()
6789{
6790 frame_T *fr;
6791
6792 if (topframe->fr_layout == FR_ROW)
6793 return TRUE;
6794
6795 if (topframe->fr_layout == FR_COL)
6796 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6797 if (fr->fr_layout == FR_ROW)
6798 return TRUE;
6799
6800 return FALSE;
6801}
6802#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006803
6804#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6805/*
6806 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006807 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006808 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6809 * If no particular ID is desired, -1 must be specified for 'id'.
6810 * Return ID of added match, -1 on failure.
6811 */
6812 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006813match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006814 win_T *wp;
6815 char_u *grp;
6816 char_u *pat;
6817 int prio;
6818 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006819 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006820{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006821 matchitem_T *cur;
6822 matchitem_T *prev;
6823 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006824 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006825 regprog_T *regprog = NULL;
6826 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006827
Bram Moolenaarb3414592014-06-17 17:48:32 +02006828 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006829 return -1;
6830 if (id < -1 || id == 0)
6831 {
6832 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6833 return -1;
6834 }
6835 if (id != -1)
6836 {
6837 cur = wp->w_match_head;
6838 while (cur != NULL)
6839 {
6840 if (cur->id == id)
6841 {
6842 EMSGN("E801: ID already taken: %ld", id);
6843 return -1;
6844 }
6845 cur = cur->next;
6846 }
6847 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006848 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006849 {
6850 EMSG2(_(e_nogroup), grp);
6851 return -1;
6852 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006853 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006854 {
6855 EMSG2(_(e_invarg2), pat);
6856 return -1;
6857 }
6858
6859 /* Find available match ID. */
6860 while (id == -1)
6861 {
6862 cur = wp->w_match_head;
6863 while (cur != NULL && cur->id != wp->w_next_match_id)
6864 cur = cur->next;
6865 if (cur == NULL)
6866 id = wp->w_next_match_id;
6867 wp->w_next_match_id++;
6868 }
6869
6870 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006871 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006872 m->id = id;
6873 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006874 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006875 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006876 m->match.regprog = regprog;
6877 m->match.rmm_ic = FALSE;
6878 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006879
Bram Moolenaarb3414592014-06-17 17:48:32 +02006880 /* Set up position matches */
6881 if (pos_list != NULL)
6882 {
6883 linenr_T toplnum = 0;
6884 linenr_T botlnum = 0;
6885 listitem_T *li;
6886 int i;
6887
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006888 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006889 i++, li = li->li_next)
6890 {
6891 linenr_T lnum = 0;
6892 colnr_T col = 0;
6893 int len = 1;
6894 list_T *subl;
6895 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006896 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006897
Bram Moolenaarb3414592014-06-17 17:48:32 +02006898 if (li->li_tv.v_type == VAR_LIST)
6899 {
6900 subl = li->li_tv.vval.v_list;
6901 if (subl == NULL)
6902 goto fail;
6903 subli = subl->lv_first;
6904 if (subli == NULL)
6905 goto fail;
6906 lnum = get_tv_number_chk(&subli->li_tv, &error);
6907 if (error == TRUE)
6908 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006909 if (lnum == 0)
6910 {
6911 --i;
6912 continue;
6913 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006914 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006915 subli = subli->li_next;
6916 if (subli != NULL)
6917 {
6918 col = get_tv_number_chk(&subli->li_tv, &error);
6919 if (error == TRUE)
6920 goto fail;
6921 subli = subli->li_next;
6922 if (subli != NULL)
6923 {
6924 len = get_tv_number_chk(&subli->li_tv, &error);
6925 if (error == TRUE)
6926 goto fail;
6927 }
6928 }
6929 m->pos.pos[i].col = col;
6930 m->pos.pos[i].len = len;
6931 }
6932 else if (li->li_tv.v_type == VAR_NUMBER)
6933 {
6934 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006935 {
6936 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006937 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006938 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006939 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6940 m->pos.pos[i].col = 0;
6941 m->pos.pos[i].len = 0;
6942 }
6943 else
6944 {
6945 EMSG(_("List or number required"));
6946 goto fail;
6947 }
6948 if (toplnum == 0 || lnum < toplnum)
6949 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006950 if (botlnum == 0 || lnum >= botlnum)
6951 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006952 }
6953
6954 /* Calculate top and bottom lines for redrawing area */
6955 if (toplnum != 0)
6956 {
6957 if (wp->w_buffer->b_mod_set)
6958 {
6959 if (wp->w_buffer->b_mod_top > toplnum)
6960 wp->w_buffer->b_mod_top = toplnum;
6961 if (wp->w_buffer->b_mod_bot < botlnum)
6962 wp->w_buffer->b_mod_bot = botlnum;
6963 }
6964 else
6965 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006966 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006967 wp->w_buffer->b_mod_top = toplnum;
6968 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006969 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006970 }
6971 m->pos.toplnum = toplnum;
6972 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006973 rtype = VALID;
6974 }
6975 }
6976
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006977 /* Insert new match. The match list is in ascending order with regard to
6978 * the match priorities. */
6979 cur = wp->w_match_head;
6980 prev = cur;
6981 while (cur != NULL && prio >= cur->priority)
6982 {
6983 prev = cur;
6984 cur = cur->next;
6985 }
6986 if (cur == prev)
6987 wp->w_match_head = m;
6988 else
6989 prev->next = m;
6990 m->next = cur;
6991
Bram Moolenaarb3414592014-06-17 17:48:32 +02006992 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006993 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006994
6995fail:
6996 vim_free(m);
6997 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006998}
6999
7000/*
7001 * Delete match with ID 'id' in the match list of window 'wp'.
7002 * Print error messages if 'perr' is TRUE.
7003 */
7004 int
7005match_delete(wp, id, perr)
7006 win_T *wp;
7007 int id;
7008 int perr;
7009{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007010 matchitem_T *cur = wp->w_match_head;
7011 matchitem_T *prev = cur;
7012 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007013
7014 if (id < 1)
7015 {
7016 if (perr == TRUE)
7017 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7018 id);
7019 return -1;
7020 }
7021 while (cur != NULL && cur->id != id)
7022 {
7023 prev = cur;
7024 cur = cur->next;
7025 }
7026 if (cur == NULL)
7027 {
7028 if (perr == TRUE)
7029 EMSGN("E803: ID not found: %ld", id);
7030 return -1;
7031 }
7032 if (cur == prev)
7033 wp->w_match_head = cur->next;
7034 else
7035 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007036 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007037 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007038 if (cur->pos.toplnum != 0)
7039 {
7040 if (wp->w_buffer->b_mod_set)
7041 {
7042 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7043 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7044 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7045 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7046 }
7047 else
7048 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007049 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007050 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7051 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007052 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007053 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007054 rtype = VALID;
7055 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007056 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007057 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007058 return 0;
7059}
7060
7061/*
7062 * Delete all matches in the match list of window 'wp'.
7063 */
7064 void
7065clear_matches(wp)
7066 win_T *wp;
7067{
7068 matchitem_T *m;
7069
7070 while (wp->w_match_head != NULL)
7071 {
7072 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007073 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007074 vim_free(wp->w_match_head->pattern);
7075 vim_free(wp->w_match_head);
7076 wp->w_match_head = m;
7077 }
7078 redraw_later(SOME_VALID);
7079}
7080
7081/*
7082 * Get match from ID 'id' in window 'wp'.
7083 * Return NULL if match not found.
7084 */
7085 matchitem_T *
7086get_match(wp, id)
7087 win_T *wp;
7088 int id;
7089{
7090 matchitem_T *cur = wp->w_match_head;
7091
7092 while (cur != NULL && cur->id != id)
7093 cur = cur->next;
7094 return cur;
7095}
7096#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007097
7098#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7099 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007100get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007101{
7102 int i = 1;
7103 win_T *w;
7104
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007105 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007106 ++i;
7107
7108 if (w == NULL)
7109 return 0;
7110 else
7111 return i;
7112}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007113
7114 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007115get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007116{
7117 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007118# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007119 tabpage_T *t;
7120
7121 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7122 ++i;
7123
7124 if (t == NULL)
7125 return 0;
7126 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007127# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007128 return i;
7129}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007130#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007131
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007132#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007133/*
7134 * Return TRUE if "topfrp" and its children are at the right height.
7135 */
7136 static int
7137frame_check_height(topfrp, height)
7138 frame_T *topfrp;
7139 int height;
7140{
7141 frame_T *frp;
7142
7143 if (topfrp->fr_height != height)
7144 return FALSE;
7145
7146 if (topfrp->fr_layout == FR_ROW)
7147 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7148 if (frp->fr_height != height)
7149 return FALSE;
7150
7151 return TRUE;
7152}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007153#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007154
7155#ifdef FEAT_VERTSPLIT
7156/*
7157 * Return TRUE if "topfrp" and its children are at the right width.
7158 */
7159 static int
7160frame_check_width(topfrp, width)
7161 frame_T *topfrp;
7162 int width;
7163{
7164 frame_T *frp;
7165
7166 if (topfrp->fr_width != width)
7167 return FALSE;
7168
7169 if (topfrp->fr_layout == FR_COL)
7170 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7171 if (frp->fr_width != width)
7172 return FALSE;
7173
7174 return TRUE;
7175}
7176#endif
7177