blob: 5012427fae37255016e267b889c4b3bcfba056e1 [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 Moolenaar071d4272004-06-13 20:20:40 +0000482 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Prenum)
484 postponed_split = Prenum;
485 else
486 postponed_split = -1;
487
488 /* Execute the command right here, required when
489 * "wincmd ]" was used in a function. */
490 do_nv_ident(Ctrl_RSB, NUL);
491 break;
492
493#ifdef FEAT_SEARCHPATH
494/* edit file name under cursor in a new window */
495 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000496 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000498wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000500
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000501 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 if (ptr != NULL)
503 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000504# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 setpcmark();
508 if (win_split(0, 0) == OK)
509 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200510 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000511 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
512 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 if (nchar == 'F' && lnum >= 0)
514 {
515 curwin->w_cursor.lnum = lnum;
516 check_cursor_lnum();
517 beginline(BL_SOL | BL_FIX);
518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 }
520 vim_free(ptr);
521 }
522 break;
523#endif
524
525#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000526/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 * new window -- webb
528 */
529 case 'i': /* Go to any match */
530 case Ctrl_I:
531 type = FIND_ANY;
532 /* FALLTHROUGH */
533 case 'd': /* Go to definition, using 'define' */
534 case Ctrl_D:
535 CHECK_CMDWIN
536 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
537 break;
538 find_pattern_in_path(ptr, 0, len, TRUE,
539 Prenum == 0 ? TRUE : FALSE, type,
540 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
541 curwin->w_set_curswant = TRUE;
542 break;
543#endif
544
Bram Moolenaar05159a02005-02-26 23:04:13 +0000545 case K_KENTER:
546 case CAR:
547#if defined(FEAT_QUICKFIX)
548 /*
549 * In a quickfix window a <CR> jumps to the error under the
550 * cursor in a new window.
551 */
552 if (bt_quickfix(curbuf))
553 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000554 sprintf((char *)cbuf, "split +%ld%s",
555 (long)curwin->w_cursor.lnum,
556 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000557 do_cmdline_cmd(cbuf);
558 }
559#endif
560 break;
561
562
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563/* CTRL-W g extended commands */
564 case 'g':
565 case Ctrl_G:
566 CHECK_CMDWIN
567#ifdef USE_ON_FLY_SCROLL
568 dont_scroll = TRUE; /* disallow scrolling here */
569#endif
570 ++no_mapping;
571 ++allow_keys; /* no mapping for xchar, but allow key codes */
572 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000573 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 --no_mapping;
576 --allow_keys;
577#ifdef FEAT_CMDL_INFO
578 (void)add_to_showcmd(xchar);
579#endif
580 switch (xchar)
581 {
582#if defined(FEAT_QUICKFIX)
583 case '}':
584 xchar = Ctrl_RSB;
585 if (Prenum)
586 g_do_tagpreview = Prenum;
587 else
588 g_do_tagpreview = p_pvh;
589 /*FALLTHROUGH*/
590#endif
591 case ']':
592 case Ctrl_RSB:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (Prenum)
595 postponed_split = Prenum;
596 else
597 postponed_split = -1;
598
599 /* Execute the command right here, required when
600 * "wincmd g}" was used in a function. */
601 do_nv_ident('g', xchar);
602 break;
603
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000604#ifdef FEAT_SEARCHPATH
605 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000606 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100607 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000608 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000609 goto wingotofile;
610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 default:
612 beep_flush();
613 break;
614 }
615 break;
616
617 default: beep_flush();
618 break;
619 }
620}
621
622/*
623 * split the current window, implements CTRL-W s and :split
624 *
625 * "size" is the height or width for the new window, 0 to use half of current
626 * height or width.
627 *
628 * "flags":
629 * WSP_ROOM: require enough room for new window
630 * WSP_VERT: vertical split.
631 * WSP_TOP: open window at the top-left of the shell (help window).
632 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
633 * WSP_HELP: creating the help window, keep layout snapshot
634 *
635 * return FAIL for failure, OK otherwise
636 */
637 int
638win_split(size, flags)
639 int size;
640 int flags;
641{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000642 /* When the ":tab" modifier was used open a new tab page instead. */
643 if (may_open_tabpage() == OK)
644 return OK;
645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /* Add flags from ":vertical", ":topleft" and ":botright". */
647 flags |= cmdmod.split;
648 if ((flags & WSP_TOP) && (flags & WSP_BOT))
649 {
650 EMSG(_("E442: Can't split topleft and botright at the same time"));
651 return FAIL;
652 }
653
654 /* When creating the help window make a snapshot of the window layout.
655 * Otherwise clear the snapshot, it's now invalid. */
656 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000657 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000659 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 return win_split_ins(size, flags, NULL, 0);
662}
663
664/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100665 * When "new_wp" is NULL: split the current window in two.
666 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 * top/left/right/bottom.
668 * return FAIL for failure, OK otherwise
669 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000670 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100671win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 int size;
673 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100674 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 int dir;
676{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100677 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 win_T *oldwin;
679 int new_size = size;
680 int i;
681 int need_status = 0;
682 int do_equal = FALSE;
683 int needed;
684 int available;
685 int oldwin_height = 0;
686 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200687 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200689 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200690 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 if (flags & WSP_TOP)
693 oldwin = firstwin;
694 else if (flags & WSP_BOT)
695 oldwin = lastwin;
696 else
697 oldwin = curwin;
698
699 /* add a status line when p_ls == 1 and splitting the first window */
700 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
701 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100702 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 EMSG(_(e_noroom));
705 return FAIL;
706 }
707 need_status = STATUS_HEIGHT;
708 }
709
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000710#ifdef FEAT_GUI
711 /* May be needed for the scrollbars that are going to change. */
712 if (gui.in_use)
713 out_flush();
714#endif
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_VERTSPLIT
717 if (flags & WSP_VERT)
718 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200719 int wmw1;
720 int minwidth;
721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
724 /*
725 * Check if we are able to split the current window and compute its
726 * width.
727 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200728 /* Current window requires at least 1 space. */
729 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
730 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200732 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200733 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200735 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200737 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200739 else if (p_ea)
740 {
741 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
742 prevfrp = oldwin->w_frame;
743 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
744 frp = frp->fr_parent)
745 {
746 if (frp->fr_layout == FR_ROW)
747 for (frp2 = frp->fr_child; frp2 != NULL;
748 frp2 = frp2->fr_next)
749 if (frp2 != prevfrp)
750 minwidth += frame_minwidth(frp2, NOWIN);
751 prevfrp = frp;
752 }
753 available = topframe->fr_width;
754 needed += minwidth;
755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200757 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200758 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
759 available = oldwin->w_frame->fr_width;
760 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200761 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100762 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {
764 EMSG(_(e_noroom));
765 return FAIL;
766 }
767 if (new_size == 0)
768 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200769 if (new_size > available - minwidth - 1)
770 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200771 if (new_size < wmw1)
772 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 /* if it doesn't fit in the current window, need win_equal() */
775 if (oldwin->w_width - new_size - 1 < p_wmw)
776 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000777
778 /* We don't like to take lines for the new window from a
779 * 'winfixwidth' window. Take them from a window to the left or right
780 * instead, if possible. */
781 if (oldwin->w_p_wfw)
782 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000783
784 /* Only make all windows the same width if one of them (except oldwin)
785 * is wider than one of the split windows. */
786 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
787 && oldwin->w_frame->fr_parent != NULL)
788 {
789 frp = oldwin->w_frame->fr_parent->fr_child;
790 while (frp != NULL)
791 {
792 if (frp->fr_win != oldwin && frp->fr_win != NULL
793 && (frp->fr_win->w_width > new_size
794 || frp->fr_win->w_width > oldwin->w_width
795 - new_size - STATUS_HEIGHT))
796 {
797 do_equal = TRUE;
798 break;
799 }
800 frp = frp->fr_next;
801 }
802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
804 else
805#endif
806 {
807 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 /*
810 * Check if we are able to split the current window and compute its
811 * height.
812 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200813 /* Current window requires at least 1 space. */
814 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
815 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200817 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200818 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200820 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200822 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200824 else if (p_ea)
825 {
826 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
827 prevfrp = oldwin->w_frame;
828 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
829 frp = frp->fr_parent)
830 {
831 if (frp->fr_layout == FR_COL)
832 for (frp2 = frp->fr_child; frp2 != NULL;
833 frp2 = frp2->fr_next)
834 if (frp2 != prevfrp)
835 minheight += frame_minheight(frp2, NOWIN);
836 prevfrp = frp;
837 }
838 available = topframe->fr_height;
839 needed += minheight;
840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 else
842 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200843 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
844 available = oldwin->w_frame->fr_height;
845 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100847 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
849 EMSG(_(e_noroom));
850 return FAIL;
851 }
852 oldwin_height = oldwin->w_height;
853 if (need_status)
854 {
855 oldwin->w_status_height = STATUS_HEIGHT;
856 oldwin_height -= STATUS_HEIGHT;
857 }
858 if (new_size == 0)
859 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200860 if (new_size > available - minheight - STATUS_HEIGHT)
861 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200862 if (new_size < wmh1)
863 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 /* if it doesn't fit in the current window, need win_equal() */
866 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
867 do_equal = TRUE;
868
869 /* We don't like to take lines for the new window from a
870 * 'winfixheight' window. Take them from a window above or below
871 * instead, if possible. */
872 if (oldwin->w_p_wfh)
873 {
874 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
875 oldwin);
876 oldwin_height = oldwin->w_height;
877 if (need_status)
878 oldwin_height -= STATUS_HEIGHT;
879 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000880
881 /* Only make all windows the same height if one of them (except oldwin)
882 * is higher than one of the split windows. */
883 if (!do_equal && p_ea && size == 0
884#ifdef FEAT_VERTSPLIT
885 && *p_ead != 'h'
886#endif
887 && oldwin->w_frame->fr_parent != NULL)
888 {
889 frp = oldwin->w_frame->fr_parent->fr_child;
890 while (frp != NULL)
891 {
892 if (frp->fr_win != oldwin && frp->fr_win != NULL
893 && (frp->fr_win->w_height > new_size
894 || frp->fr_win->w_height > oldwin_height - new_size
895 - STATUS_HEIGHT))
896 {
897 do_equal = TRUE;
898 break;
899 }
900 frp = frp->fr_next;
901 }
902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 }
904
905 /*
906 * allocate new window structure and link it in the window list
907 */
908 if ((flags & WSP_TOP) == 0
909 && ((flags & WSP_BOT)
910 || (flags & WSP_BELOW)
911 || (!(flags & WSP_ABOVE)
912 && (
913#ifdef FEAT_VERTSPLIT
914 (flags & WSP_VERT) ? p_spr :
915#endif
916 p_sb))))
917 {
918 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100919 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000920 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 else
922 win_append(oldwin, wp);
923 }
924 else
925 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100926 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000927 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 else
929 win_append(oldwin->w_prev, wp);
930 }
931
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100932 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 {
934 if (wp == NULL)
935 return FAIL;
936
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000937 new_frame(wp);
938 if (wp->w_frame == NULL)
939 {
940 win_free(wp, NULL);
941 return FAIL;
942 }
943
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000944 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000945 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947
948 /*
949 * Reorganise the tree of frames to insert the new window.
950 */
951 if (flags & (WSP_TOP | WSP_BOT))
952 {
953#ifdef FEAT_VERTSPLIT
954 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
955 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
956#else
957 if (topframe->fr_layout == FR_COL)
958#endif
959 {
960 curfrp = topframe->fr_child;
961 if (flags & WSP_BOT)
962 while (curfrp->fr_next != NULL)
963 curfrp = curfrp->fr_next;
964 }
965 else
966 curfrp = topframe;
967 before = (flags & WSP_TOP);
968 }
969 else
970 {
971 curfrp = oldwin->w_frame;
972 if (flags & WSP_BELOW)
973 before = FALSE;
974 else if (flags & WSP_ABOVE)
975 before = TRUE;
976 else
977#ifdef FEAT_VERTSPLIT
978 if (flags & WSP_VERT)
979 before = !p_spr;
980 else
981#endif
982 before = !p_sb;
983 }
984 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
985 {
986 /* Need to create a new frame in the tree to make a branch. */
987 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
988 *frp = *curfrp;
989 curfrp->fr_layout = layout;
990 frp->fr_parent = curfrp;
991 frp->fr_next = NULL;
992 frp->fr_prev = NULL;
993 curfrp->fr_child = frp;
994 curfrp->fr_win = NULL;
995 curfrp = frp;
996 if (frp->fr_win != NULL)
997 oldwin->w_frame = frp;
998 else
999 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1000 frp->fr_parent = curfrp;
1001 }
1002
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001003 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001004 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001006 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 frp->fr_parent = curfrp->fr_parent;
1008
1009 /* Insert the new frame at the right place in the frame list. */
1010 if (before)
1011 frame_insert(curfrp, frp);
1012 else
1013 frame_append(curfrp, frp);
1014
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001015 /* Set w_fraction now so that the cursor keeps the same relative
1016 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001017 if (oldwin->w_height > 0)
1018 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001019 wp->w_fraction = oldwin->w_fraction;
1020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#ifdef FEAT_VERTSPLIT
1022 if (flags & WSP_VERT)
1023 {
1024 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001025
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (need_status)
1027 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001028 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 oldwin->w_status_height = need_status;
1030 }
1031 if (flags & (WSP_TOP | WSP_BOT))
1032 {
1033 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001034 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001035 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 wp->w_status_height = (p_ls > 0);
1037 }
1038 else
1039 {
1040 /* height and row of new window is same as current window */
1041 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001042 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 wp->w_status_height = oldwin->w_status_height;
1044 }
1045 frp->fr_height = curfrp->fr_height;
1046
1047 /* "new_size" of the current window goes to the new window, use
1048 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001049 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (before)
1051 wp->w_vsep_width = 1;
1052 else
1053 {
1054 wp->w_vsep_width = oldwin->w_vsep_width;
1055 oldwin->w_vsep_width = 1;
1056 }
1057 if (flags & (WSP_TOP | WSP_BOT))
1058 {
1059 if (flags & WSP_BOT)
1060 frame_add_vsep(curfrp);
1061 /* Set width of neighbor frame */
1062 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001063 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1064 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001067 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (before) /* new window left of current one */
1069 {
1070 wp->w_wincol = oldwin->w_wincol;
1071 oldwin->w_wincol += new_size + 1;
1072 }
1073 else /* new window right of current one */
1074 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1075 frame_fix_width(oldwin);
1076 frame_fix_width(wp);
1077 }
1078 else
1079#endif
1080 {
1081 /* width and column of new window is same as current window */
1082#ifdef FEAT_VERTSPLIT
1083 if (flags & (WSP_TOP | WSP_BOT))
1084 {
1085 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001086 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 wp->w_vsep_width = 0;
1088 }
1089 else
1090 {
1091 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001092 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 wp->w_vsep_width = oldwin->w_vsep_width;
1094 }
1095 frp->fr_width = curfrp->fr_width;
1096#endif
1097
1098 /* "new_size" of the current window goes to the new window, use
1099 * one row for the status line */
1100 win_new_height(wp, new_size);
1101 if (flags & (WSP_TOP | WSP_BOT))
1102 frame_new_height(curfrp, curfrp->fr_height
1103 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1104 else
1105 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1106 if (before) /* new window above current one */
1107 {
1108 wp->w_winrow = oldwin->w_winrow;
1109 wp->w_status_height = STATUS_HEIGHT;
1110 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1111 }
1112 else /* new window below current one */
1113 {
1114 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1115 wp->w_status_height = oldwin->w_status_height;
1116 oldwin->w_status_height = STATUS_HEIGHT;
1117 }
1118#ifdef FEAT_VERTSPLIT
1119 if (flags & WSP_BOT)
1120 frame_add_statusline(curfrp);
1121#endif
1122 frame_fix_height(wp);
1123 frame_fix_height(oldwin);
1124 }
1125
1126 if (flags & (WSP_TOP | WSP_BOT))
1127 (void)win_comp_pos();
1128
1129 /*
1130 * Both windows need redrawing
1131 */
1132 redraw_win_later(wp, NOT_VALID);
1133 wp->w_redr_status = TRUE;
1134 redraw_win_later(oldwin, NOT_VALID);
1135 oldwin->w_redr_status = TRUE;
1136
1137 if (need_status)
1138 {
1139 msg_row = Rows - 1;
1140 msg_col = sc_col;
1141 msg_clr_eos_force(); /* Old command/ruler may still be there */
1142 comp_col();
1143 msg_row = Rows - 1;
1144 msg_col = 0; /* put position back at start of line */
1145 }
1146
1147 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001148 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 */
1150 if (do_equal || dir != 0)
1151 win_equal(wp, TRUE,
1152#ifdef FEAT_VERTSPLIT
1153 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1154 : dir == 'h' ? 'b' :
1155#endif
1156 'v');
1157
1158 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1159 * size was given. */
1160#ifdef FEAT_VERTSPLIT
1161 if (flags & WSP_VERT)
1162 {
1163 i = p_wiw;
1164 if (size != 0)
1165 p_wiw = size;
1166
1167# ifdef FEAT_GUI
1168 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1169 if (gui.in_use)
1170 gui_init_which_components(NULL);
1171# endif
1172 }
1173 else
1174#endif
1175 {
1176 i = p_wh;
1177 if (size != 0)
1178 p_wh = size;
1179 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001180
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001181#ifdef FEAT_JUMPLIST
1182 /* Keep same changelist position in new window. */
1183 wp->w_changelistidx = oldwin->w_changelistidx;
1184#endif
1185
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001186 /*
1187 * make the new window the current window
1188 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 win_enter(wp, FALSE);
1190#ifdef FEAT_VERTSPLIT
1191 if (flags & WSP_VERT)
1192 p_wiw = i;
1193 else
1194#endif
1195 p_wh = i;
1196
1197 return OK;
1198}
1199
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001200
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001201/*
1202 * Initialize window "newp" from window "oldp".
1203 * Used when splitting a window and when creating a new tab page.
1204 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001205 * WSP_NEWLOC may be specified in flags to prevent the location list from
1206 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001207 */
1208 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001209win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001210 win_T *newp;
1211 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001212 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001213{
1214 int i;
1215
1216 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001217#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001218 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001219#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001220 oldp->w_buffer->b_nwindows++;
1221 newp->w_cursor = oldp->w_cursor;
1222 newp->w_valid = 0;
1223 newp->w_curswant = oldp->w_curswant;
1224 newp->w_set_curswant = oldp->w_set_curswant;
1225 newp->w_topline = oldp->w_topline;
1226#ifdef FEAT_DIFF
1227 newp->w_topfill = oldp->w_topfill;
1228#endif
1229 newp->w_leftcol = oldp->w_leftcol;
1230 newp->w_pcmark = oldp->w_pcmark;
1231 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1232 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001233 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001234 newp->w_fraction = oldp->w_fraction;
1235 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1236#ifdef FEAT_JUMPLIST
1237 copy_jumplist(oldp, newp);
1238#endif
1239#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001240 if (flags & WSP_NEWLOC)
1241 {
1242 /* Don't copy the location list. */
1243 newp->w_llist = NULL;
1244 newp->w_llist_ref = NULL;
1245 }
1246 else
1247 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001248#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001249 newp->w_localdir = (oldp->w_localdir == NULL)
1250 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001251
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001252 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001253 for (i = 0; i < oldp->w_tagstacklen; i++)
1254 {
1255 newp->w_tagstack[i] = oldp->w_tagstack[i];
1256 if (newp->w_tagstack[i].tagname != NULL)
1257 newp->w_tagstack[i].tagname =
1258 vim_strsave(newp->w_tagstack[i].tagname);
1259 }
1260 newp->w_tagstackidx = oldp->w_tagstackidx;
1261 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001262#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001263 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001264#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001265
1266 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001267
Bram Moolenaara971b822011-09-14 14:43:25 +02001268#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001269 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001270#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001271}
1272
1273/*
1274 * Initialize window "newp" from window"old".
1275 * Only the essential things are copied.
1276 */
1277 static void
1278win_init_some(newp, oldp)
1279 win_T *newp;
1280 win_T *oldp;
1281{
1282 /* Use the same argument list. */
1283 newp->w_alist = oldp->w_alist;
1284 ++newp->w_alist->al_refcount;
1285 newp->w_arg_idx = oldp->w_arg_idx;
1286
1287 /* copy options from existing window */
1288 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001289}
1290
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291#endif /* FEAT_WINDOWS */
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293#if defined(FEAT_WINDOWS) || defined(PROTO)
1294/*
1295 * Check if "win" is a pointer to an existing window.
1296 */
1297 int
1298win_valid(win)
1299 win_T *win;
1300{
1301 win_T *wp;
1302
1303 if (win == NULL)
1304 return FALSE;
1305 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1306 if (wp == win)
1307 return TRUE;
1308 return FALSE;
1309}
1310
1311/*
1312 * Return the number of windows.
1313 */
1314 int
1315win_count()
1316{
1317 win_T *wp;
1318 int count = 0;
1319
1320 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1321 ++count;
1322 return count;
1323}
1324
1325/*
1326 * Make "count" windows on the screen.
1327 * Return actual number of windows on the screen.
1328 * Must be called when there is just one window, filling the whole screen
1329 * (excluding the command line).
1330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 int
1332make_windows(count, vertical)
1333 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001334 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
1336 int maxcount;
1337 int todo;
1338
1339#ifdef FEAT_VERTSPLIT
1340 if (vertical)
1341 {
1342 /* Each windows needs at least 'winminwidth' lines and a separator
1343 * column. */
1344 maxcount = (curwin->w_width + curwin->w_vsep_width
1345 - (p_wiw - p_wmw)) / (p_wmw + 1);
1346 }
1347 else
1348#endif
1349 {
1350 /* Each window needs at least 'winminheight' lines and a status line. */
1351 maxcount = (curwin->w_height + curwin->w_status_height
1352 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1353 }
1354
1355 if (maxcount < 2)
1356 maxcount = 2;
1357 if (count > maxcount)
1358 count = maxcount;
1359
1360 /*
1361 * add status line now, otherwise first window will be too big
1362 */
1363 if (count > 1)
1364 last_status(TRUE);
1365
1366#ifdef FEAT_AUTOCMD
1367 /*
1368 * Don't execute autocommands while creating the windows. Must do that
1369 * when putting the buffers in the windows.
1370 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001371 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372#endif
1373
1374 /* todo is number of windows left to create */
1375 for (todo = count - 1; todo > 0; --todo)
1376#ifdef FEAT_VERTSPLIT
1377 if (vertical)
1378 {
1379 if (win_split(curwin->w_width - (curwin->w_width - todo)
1380 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1381 break;
1382 }
1383 else
1384#endif
1385 {
1386 if (win_split(curwin->w_height - (curwin->w_height - todo
1387 * STATUS_HEIGHT) / (todo + 1)
1388 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1389 break;
1390 }
1391
1392#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001393 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#endif
1395
1396 /* return actual number of windows */
1397 return (count - todo);
1398}
1399
1400/*
1401 * Exchange current and next window
1402 */
1403 static void
1404win_exchange(Prenum)
1405 long Prenum;
1406{
1407 frame_T *frp;
1408 frame_T *frp2;
1409 win_T *wp;
1410 win_T *wp2;
1411 int temp;
1412
1413 if (lastwin == firstwin) /* just one window */
1414 {
1415 beep_flush();
1416 return;
1417 }
1418
1419#ifdef FEAT_GUI
1420 need_mouse_correct = TRUE;
1421#endif
1422
1423 /*
1424 * find window to exchange with
1425 */
1426 if (Prenum)
1427 {
1428 frp = curwin->w_frame->fr_parent->fr_child;
1429 while (frp != NULL && --Prenum > 0)
1430 frp = frp->fr_next;
1431 }
1432 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1433 frp = curwin->w_frame->fr_next;
1434 else /* Swap last window in row/col with previous */
1435 frp = curwin->w_frame->fr_prev;
1436
1437 /* We can only exchange a window with another window, not with a frame
1438 * containing windows. */
1439 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1440 return;
1441 wp = frp->fr_win;
1442
1443/*
1444 * 1. remove curwin from the list. Remember after which window it was in wp2
1445 * 2. insert curwin before wp in the list
1446 * if wp != wp2
1447 * 3. remove wp from the list
1448 * 4. insert wp after wp2
1449 * 5. exchange the status line height and vsep width.
1450 */
1451 wp2 = curwin->w_prev;
1452 frp2 = curwin->w_frame->fr_prev;
1453 if (wp->w_prev != curwin)
1454 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001455 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 frame_remove(curwin->w_frame);
1457 win_append(wp->w_prev, curwin);
1458 frame_insert(frp, curwin->w_frame);
1459 }
1460 if (wp != wp2)
1461 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001462 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 frame_remove(wp->w_frame);
1464 win_append(wp2, wp);
1465 if (frp2 == NULL)
1466 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1467 else
1468 frame_append(frp2, wp->w_frame);
1469 }
1470 temp = curwin->w_status_height;
1471 curwin->w_status_height = wp->w_status_height;
1472 wp->w_status_height = temp;
1473#ifdef FEAT_VERTSPLIT
1474 temp = curwin->w_vsep_width;
1475 curwin->w_vsep_width = wp->w_vsep_width;
1476 wp->w_vsep_width = temp;
1477
1478 /* If the windows are not in the same frame, exchange the sizes to avoid
1479 * messing up the window layout. Otherwise fix the frame sizes. */
1480 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1481 {
1482 temp = curwin->w_height;
1483 curwin->w_height = wp->w_height;
1484 wp->w_height = temp;
1485 temp = curwin->w_width;
1486 curwin->w_width = wp->w_width;
1487 wp->w_width = temp;
1488 }
1489 else
1490 {
1491 frame_fix_height(curwin);
1492 frame_fix_height(wp);
1493 frame_fix_width(curwin);
1494 frame_fix_width(wp);
1495 }
1496#endif
1497
1498 (void)win_comp_pos(); /* recompute window positions */
1499
1500 win_enter(wp, TRUE);
1501 redraw_later(CLEAR);
1502}
1503
1504/*
1505 * rotate windows: if upwards TRUE the second window becomes the first one
1506 * if upwards FALSE the first window becomes the second one
1507 */
1508 static void
1509win_rotate(upwards, count)
1510 int upwards;
1511 int count;
1512{
1513 win_T *wp1;
1514 win_T *wp2;
1515 frame_T *frp;
1516 int n;
1517
1518 if (firstwin == lastwin) /* nothing to do */
1519 {
1520 beep_flush();
1521 return;
1522 }
1523
1524#ifdef FEAT_GUI
1525 need_mouse_correct = TRUE;
1526#endif
1527
1528#ifdef FEAT_VERTSPLIT
1529 /* Check if all frames in this row/col have one window. */
1530 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1531 frp = frp->fr_next)
1532 if (frp->fr_win == NULL)
1533 {
1534 EMSG(_("E443: Cannot rotate when another window is split"));
1535 return;
1536 }
1537#endif
1538
1539 while (count--)
1540 {
1541 if (upwards) /* first window becomes last window */
1542 {
1543 /* remove first window/frame from the list */
1544 frp = curwin->w_frame->fr_parent->fr_child;
1545 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001546 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 frame_remove(frp);
1548
1549 /* find last frame and append removed window/frame after it */
1550 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1551 ;
1552 win_append(frp->fr_win, wp1);
1553 frame_append(frp, wp1->w_frame);
1554
1555 wp2 = frp->fr_win; /* previously last window */
1556 }
1557 else /* last window becomes first window */
1558 {
1559 /* find last window/frame in the list and remove it */
1560 for (frp = curwin->w_frame; frp->fr_next != NULL;
1561 frp = frp->fr_next)
1562 ;
1563 wp1 = frp->fr_win;
1564 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001565 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 frame_remove(frp);
1567
1568 /* append the removed window/frame before the first in the list */
1569 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1570 frame_insert(frp->fr_parent->fr_child, frp);
1571 }
1572
1573 /* exchange status height and vsep width of old and new last window */
1574 n = wp2->w_status_height;
1575 wp2->w_status_height = wp1->w_status_height;
1576 wp1->w_status_height = n;
1577 frame_fix_height(wp1);
1578 frame_fix_height(wp2);
1579#ifdef FEAT_VERTSPLIT
1580 n = wp2->w_vsep_width;
1581 wp2->w_vsep_width = wp1->w_vsep_width;
1582 wp1->w_vsep_width = n;
1583 frame_fix_width(wp1);
1584 frame_fix_width(wp2);
1585#endif
1586
1587 /* recompute w_winrow and w_wincol for all windows */
1588 (void)win_comp_pos();
1589 }
1590
1591 redraw_later(CLEAR);
1592}
1593
1594/*
1595 * Move the current window to the very top/bottom/left/right of the screen.
1596 */
1597 static void
1598win_totop(size, flags)
1599 int size;
1600 int flags;
1601{
1602 int dir;
1603 int height = curwin->w_height;
1604
1605 if (lastwin == firstwin)
1606 {
1607 beep_flush();
1608 return;
1609 }
1610
1611 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001612 (void)winframe_remove(curwin, &dir, NULL);
1613 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 last_status(FALSE); /* may need to remove last status line */
1615 (void)win_comp_pos(); /* recompute window positions */
1616
1617 /* Split a window on the desired side and put the window there. */
1618 (void)win_split_ins(size, flags, curwin, dir);
1619 if (!(flags & WSP_VERT))
1620 {
1621 win_setheight(height);
1622 if (p_ea)
1623 win_equal(curwin, TRUE, 'v');
1624 }
1625
1626#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1627 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1628 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001629 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631}
1632
1633/*
1634 * Move window "win1" to below/right of "win2" and make "win1" the current
1635 * window. Only works within the same frame!
1636 */
1637 void
1638win_move_after(win1, win2)
1639 win_T *win1, *win2;
1640{
1641 int height;
1642
1643 /* check if the arguments are reasonable */
1644 if (win1 == win2)
1645 return;
1646
1647 /* check if there is something to do */
1648 if (win2->w_next != win1)
1649 {
1650 /* may need move the status line/vertical separator of the last window
1651 * */
1652 if (win1 == lastwin)
1653 {
1654 height = win1->w_prev->w_status_height;
1655 win1->w_prev->w_status_height = win1->w_status_height;
1656 win1->w_status_height = height;
1657#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001658 if (win1->w_prev->w_vsep_width == 1)
1659 {
1660 /* Remove the vertical separator from the last-but-one window,
1661 * add it to the last window. Adjust the frame widths. */
1662 win1->w_prev->w_vsep_width = 0;
1663 win1->w_prev->w_frame->fr_width -= 1;
1664 win1->w_vsep_width = 1;
1665 win1->w_frame->fr_width += 1;
1666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
1668 }
1669 else if (win2 == lastwin)
1670 {
1671 height = win1->w_status_height;
1672 win1->w_status_height = win2->w_status_height;
1673 win2->w_status_height = height;
1674#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001675 if (win1->w_vsep_width == 1)
1676 {
1677 /* Remove the vertical separator from win1, add it to the last
1678 * window, win2. Adjust the frame widths. */
1679 win2->w_vsep_width = 1;
1680 win2->w_frame->fr_width += 1;
1681 win1->w_vsep_width = 0;
1682 win1->w_frame->fr_width -= 1;
1683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#endif
1685 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001686 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 frame_remove(win1->w_frame);
1688 win_append(win2, win1);
1689 frame_append(win2->w_frame, win1->w_frame);
1690
1691 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1692 redraw_later(NOT_VALID);
1693 }
1694 win_enter(win1, FALSE);
1695}
1696
1697/*
1698 * Make all windows the same height.
1699 * 'next_curwin' will soon be the current window, make sure it has enough
1700 * rows.
1701 */
1702 void
1703win_equal(next_curwin, current, dir)
1704 win_T *next_curwin; /* pointer to current window to be or NULL */
1705 int current; /* do only frame with current window */
1706 int dir; /* 'v' for vertically, 'h' for horizontally,
1707 'b' for both, 0 for using p_ead */
1708{
1709 if (dir == 0)
1710#ifdef FEAT_VERTSPLIT
1711 dir = *p_ead;
1712#else
1713 dir = 'b';
1714#endif
1715 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001716 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001717 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718}
1719
1720/*
1721 * Set a frame to a new position and height, spreading the available room
1722 * equally over contained frames.
1723 * The window "next_curwin" (if not NULL) should at least get the size from
1724 * 'winheight' and 'winwidth' if possible.
1725 */
1726 static void
1727win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1728 win_T *next_curwin; /* pointer to current window to be or NULL */
1729 int current; /* do only frame with current window */
1730 frame_T *topfr; /* frame to set size off */
1731 int dir; /* 'v', 'h' or 'b', see win_equal() */
1732 int col; /* horizontal position for frame */
1733 int row; /* vertical position for frame */
1734 int width; /* new width of frame */
1735 int height; /* new height of frame */
1736{
1737 int n, m;
1738 int extra_sep = 0;
1739 int wincount, totwincount = 0;
1740 frame_T *fr;
1741 int next_curwin_size = 0;
1742 int room = 0;
1743 int new_size;
1744 int has_next_curwin = 0;
1745 int hnc;
1746
1747 if (topfr->fr_layout == FR_LEAF)
1748 {
1749 /* Set the width/height of this frame.
1750 * Redraw when size or position changes */
1751 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1752#ifdef FEAT_VERTSPLIT
1753 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1754#endif
1755 )
1756 {
1757 topfr->fr_win->w_winrow = row;
1758 frame_new_height(topfr, height, FALSE, FALSE);
1759#ifdef FEAT_VERTSPLIT
1760 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001761 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762#endif
1763 redraw_all_later(CLEAR);
1764 }
1765 }
1766#ifdef FEAT_VERTSPLIT
1767 else if (topfr->fr_layout == FR_ROW)
1768 {
1769 topfr->fr_width = width;
1770 topfr->fr_height = height;
1771
1772 if (dir != 'v') /* equalize frame widths */
1773 {
1774 /* Compute the maximum number of windows horizontally in this
1775 * frame. */
1776 n = frame_minwidth(topfr, NOWIN);
1777 /* add one for the rightmost window, it doesn't have a separator */
1778 if (col + width == Columns)
1779 extra_sep = 1;
1780 else
1781 extra_sep = 0;
1782 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001783 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001785 /*
1786 * Compute width for "next_curwin" window and room available for
1787 * other windows.
1788 * "m" is the minimal width when counting p_wiw for "next_curwin".
1789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 m = frame_minwidth(topfr, next_curwin);
1791 room = width - m;
1792 if (room < 0)
1793 {
1794 next_curwin_size = p_wiw + room;
1795 room = 0;
1796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 else
1798 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001799 next_curwin_size = -1;
1800 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1801 {
1802 /* If 'winfixwidth' set keep the window width if
1803 * possible.
1804 * Watch out for this window being the next_curwin. */
1805 if (frame_fixed_width(fr))
1806 {
1807 n = frame_minwidth(fr, NOWIN);
1808 new_size = fr->fr_width;
1809 if (frame_has_win(fr, next_curwin))
1810 {
1811 room += p_wiw - p_wmw;
1812 next_curwin_size = 0;
1813 if (new_size < p_wiw)
1814 new_size = p_wiw;
1815 }
1816 else
1817 /* These windows don't use up room. */
1818 totwincount -= (n + (fr->fr_next == NULL
1819 ? extra_sep : 0)) / (p_wmw + 1);
1820 room -= new_size - n;
1821 if (room < 0)
1822 {
1823 new_size += room;
1824 room = 0;
1825 }
1826 fr->fr_newwidth = new_size;
1827 }
1828 }
1829 if (next_curwin_size == -1)
1830 {
1831 if (!has_next_curwin)
1832 next_curwin_size = 0;
1833 else if (totwincount > 1
1834 && (room + (totwincount - 2))
1835 / (totwincount - 1) > p_wiw)
1836 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001837 /* Can make all windows wider than 'winwidth', spread
1838 * the room equally. */
1839 next_curwin_size = (room + p_wiw
1840 + (totwincount - 1) * p_wmw
1841 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001842 room -= next_curwin_size - p_wiw;
1843 }
1844 else
1845 next_curwin_size = p_wiw;
1846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001848
1849 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 --totwincount; /* don't count curwin */
1851 }
1852
1853 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1854 {
1855 n = m = 0;
1856 wincount = 1;
1857 if (fr->fr_next == NULL)
1858 /* last frame gets all that remains (avoid roundoff error) */
1859 new_size = width;
1860 else if (dir == 'v')
1861 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001862 else if (frame_fixed_width(fr))
1863 {
1864 new_size = fr->fr_newwidth;
1865 wincount = 0; /* doesn't count as a sizeable window */
1866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 else
1868 {
1869 /* Compute the maximum number of windows horiz. in "fr". */
1870 n = frame_minwidth(fr, NOWIN);
1871 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1872 / (p_wmw + 1);
1873 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001874 if (has_next_curwin)
1875 hnc = frame_has_win(fr, next_curwin);
1876 else
1877 hnc = FALSE;
1878 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001880 if (totwincount == 0)
1881 new_size = room;
1882 else
1883 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001885 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 {
1887 next_curwin_size -= p_wiw - (m - n);
1888 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001889 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001891 else
1892 room -= new_size;
1893 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001896 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 * window, unless equalizing all frames. */
1898 if (!current || dir != 'v' || topfr->fr_parent != NULL
1899 || (new_size != fr->fr_width)
1900 || frame_has_win(fr, next_curwin))
1901 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001902 new_size, height);
1903 col += new_size;
1904 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 totwincount -= wincount;
1906 }
1907 }
1908#endif
1909 else /* topfr->fr_layout == FR_COL */
1910 {
1911#ifdef FEAT_VERTSPLIT
1912 topfr->fr_width = width;
1913#endif
1914 topfr->fr_height = height;
1915
1916 if (dir != 'h') /* equalize frame heights */
1917 {
1918 /* Compute maximum number of windows vertically in this frame. */
1919 n = frame_minheight(topfr, NOWIN);
1920 /* add one for the bottom window if it doesn't have a statusline */
1921 if (row + height == cmdline_row && p_ls == 0)
1922 extra_sep = 1;
1923 else
1924 extra_sep = 0;
1925 totwincount = (n + extra_sep) / (p_wmh + 1);
1926 has_next_curwin = frame_has_win(topfr, next_curwin);
1927
1928 /*
1929 * Compute height for "next_curwin" window and room available for
1930 * other windows.
1931 * "m" is the minimal height when counting p_wh for "next_curwin".
1932 */
1933 m = frame_minheight(topfr, next_curwin);
1934 room = height - m;
1935 if (room < 0)
1936 {
1937 /* The room is less then 'winheight', use all space for the
1938 * current window. */
1939 next_curwin_size = p_wh + room;
1940 room = 0;
1941 }
1942 else
1943 {
1944 next_curwin_size = -1;
1945 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1946 {
1947 /* If 'winfixheight' set keep the window height if
1948 * possible.
1949 * Watch out for this window being the next_curwin. */
1950 if (frame_fixed_height(fr))
1951 {
1952 n = frame_minheight(fr, NOWIN);
1953 new_size = fr->fr_height;
1954 if (frame_has_win(fr, next_curwin))
1955 {
1956 room += p_wh - p_wmh;
1957 next_curwin_size = 0;
1958 if (new_size < p_wh)
1959 new_size = p_wh;
1960 }
1961 else
1962 /* These windows don't use up room. */
1963 totwincount -= (n + (fr->fr_next == NULL
1964 ? extra_sep : 0)) / (p_wmh + 1);
1965 room -= new_size - n;
1966 if (room < 0)
1967 {
1968 new_size += room;
1969 room = 0;
1970 }
1971 fr->fr_newheight = new_size;
1972 }
1973 }
1974 if (next_curwin_size == -1)
1975 {
1976 if (!has_next_curwin)
1977 next_curwin_size = 0;
1978 else if (totwincount > 1
1979 && (room + (totwincount - 2))
1980 / (totwincount - 1) > p_wh)
1981 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001982 /* can make all windows higher than 'winheight',
1983 * spread the room equally. */
1984 next_curwin_size = (room + p_wh
1985 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 + (totwincount - 1)) / totwincount;
1987 room -= next_curwin_size - p_wh;
1988 }
1989 else
1990 next_curwin_size = p_wh;
1991 }
1992 }
1993
1994 if (has_next_curwin)
1995 --totwincount; /* don't count curwin */
1996 }
1997
1998 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1999 {
2000 n = m = 0;
2001 wincount = 1;
2002 if (fr->fr_next == NULL)
2003 /* last frame gets all that remains (avoid roundoff error) */
2004 new_size = height;
2005 else if (dir == 'h')
2006 new_size = fr->fr_height;
2007 else if (frame_fixed_height(fr))
2008 {
2009 new_size = fr->fr_newheight;
2010 wincount = 0; /* doesn't count as a sizeable window */
2011 }
2012 else
2013 {
2014 /* Compute the maximum number of windows vert. in "fr". */
2015 n = frame_minheight(fr, NOWIN);
2016 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2017 / (p_wmh + 1);
2018 m = frame_minheight(fr, next_curwin);
2019 if (has_next_curwin)
2020 hnc = frame_has_win(fr, next_curwin);
2021 else
2022 hnc = FALSE;
2023 if (hnc) /* don't count next_curwin */
2024 --wincount;
2025 if (totwincount == 0)
2026 new_size = room;
2027 else
2028 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2029 / totwincount;
2030 if (hnc) /* add next_curwin size */
2031 {
2032 next_curwin_size -= p_wh - (m - n);
2033 new_size += next_curwin_size;
2034 room -= new_size - next_curwin_size;
2035 }
2036 else
2037 room -= new_size;
2038 new_size += n;
2039 }
2040 /* Skip frame that is full width when splitting or closing a
2041 * window, unless equalizing all frames. */
2042 if (!current || dir != 'h' || topfr->fr_parent != NULL
2043 || (new_size != fr->fr_height)
2044 || frame_has_win(fr, next_curwin))
2045 win_equal_rec(next_curwin, current, fr, dir, col, row,
2046 width, new_size);
2047 row += new_size;
2048 height -= new_size;
2049 totwincount -= wincount;
2050 }
2051 }
2052}
2053
2054/*
2055 * close all windows for buffer 'buf'
2056 */
2057 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002058close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002060 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002062 win_T *wp;
2063 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002064 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002067
2068 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002070 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2071#ifdef FEAT_AUTOCMD
2072 && !(wp->w_closing || wp->w_buffer->b_closing)
2073#endif
2074 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002076 win_close(wp, FALSE);
2077
2078 /* Start all over, autocommands may change the window layout. */
2079 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002082 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002084
2085 /* Also check windows in other tab pages. */
2086 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2087 {
2088 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002089 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002090 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002091 if (wp->w_buffer == buf
2092#ifdef FEAT_AUTOCMD
2093 && !(wp->w_closing || wp->w_buffer->b_closing)
2094#endif
2095 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002096 {
2097 win_close_othertab(wp, FALSE, tp);
2098
2099 /* Start all over, the tab page may be closed and
2100 * autocommands may change the window layout. */
2101 nexttp = first_tabpage;
2102 break;
2103 }
2104 }
2105
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002107
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002108 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002109 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002110 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112
2113/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002114 * Return TRUE if the current window is the only window that exists (ignoring
2115 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002116 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002117 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002118 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002119last_window()
2120{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002121 return (one_window() && first_tabpage->tp_next == NULL);
2122}
2123
2124/*
2125 * Return TRUE if there is only one window other than "aucmd_win" in the
2126 * current tab page.
2127 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002128 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002129one_window()
2130{
2131#ifdef FEAT_AUTOCMD
2132 win_T *wp;
2133 int seen_one = FALSE;
2134
2135 FOR_ALL_WINDOWS(wp)
2136 {
2137 if (wp != aucmd_win)
2138 {
2139 if (seen_one)
2140 return FALSE;
2141 seen_one = TRUE;
2142 }
2143 }
2144 return TRUE;
2145#else
2146 return firstwin == lastwin;
2147#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002148}
2149
2150/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002151 * Close the possibly last window in a tab page.
2152 * Returns TRUE when the window was closed already.
2153 */
2154 static int
2155close_last_window_tabpage(win, free_buf, prev_curtab)
2156 win_T *win;
2157 int free_buf;
2158 tabpage_T *prev_curtab;
2159{
2160 if (firstwin == lastwin)
2161 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002162#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002163 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002164#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002165
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002166 /*
2167 * Closing the last window in a tab page. First go to another tab
2168 * page and then close the window and the tab page. This avoids that
2169 * curwin and curtab are invalid while we are freeing memory, they may
2170 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002171 * Don't trigger autocommands yet, they may use wrong values, so do
2172 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002173 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002174 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002175 redraw_tabline = TRUE;
2176
2177 /* Safety check: Autocommands may have closed the window when jumping
2178 * to the other tab page. */
2179 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2180 {
2181 int h = tabline_height();
2182
2183 win_close_othertab(win, free_buf, prev_curtab);
2184 if (h != tabline_height())
2185 shell_new_rows();
2186 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002187 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2188 * that now. */
2189#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002190 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002191 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2192 if (old_curbuf != curbuf)
2193 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002194#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002195 return TRUE;
2196 }
2197 return FALSE;
2198}
2199
2200/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 * If "free_buf" is TRUE related buffer may be unloaded.
2203 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002204 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002205 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002207 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208win_close(win, free_buf)
2209 win_T *win;
2210 int free_buf;
2211{
2212 win_T *wp;
2213#ifdef FEAT_AUTOCMD
2214 int other_buffer = FALSE;
2215#endif
2216 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 int dir;
2218 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002219 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002221 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 }
2226
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002227#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002228 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002229 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002230 if (win == aucmd_win)
2231 {
2232 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002233 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002234 }
2235 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2236 {
2237 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002238 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002239 }
2240#endif
2241
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002242 /* When closing the last window in a tab page first go to another tab page
2243 * and then close the window and the tab page to avoid that curwin and
2244 * curtab are invalid while we are freeing memory. */
2245 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002246 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 /* When closing the help window, try restoring a snapshot after closing
2249 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002250 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 help_window = TRUE;
2252 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002253 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
2255#ifdef FEAT_AUTOCMD
2256 if (win == curwin)
2257 {
2258 /*
2259 * Guess which window is going to be the new current window.
2260 * This may change because of the autocommands (sigh).
2261 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002262 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002265 * Be careful: If autocommands delete the window or cause this window
2266 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 */
2268 if (wp->w_buffer != curbuf)
2269 {
2270 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002271 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002273 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002274 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002275 win->w_closing = FALSE;
2276 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002279 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002281 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002282 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002283 win->w_closing = FALSE;
2284 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286# ifdef FEAT_EVAL
2287 /* autocmds may abort script processing */
2288 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290# endif
2291 }
2292#endif
2293
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002294#ifdef FEAT_GUI
2295 /* Avoid trouble with scrollbars that are going to be deleted in
2296 * win_free(). */
2297 if (gui.in_use)
2298 out_flush();
2299#endif
2300
Bram Moolenaara971b822011-09-14 14:43:25 +02002301#ifdef FEAT_SYN_HL
2302 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002303 if (win->w_buffer != NULL)
2304 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002305#endif
2306
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 /*
2308 * Close the link to the buffer.
2309 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002310 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002311 {
2312#ifdef FEAT_AUTOCMD
2313 win->w_closing = TRUE;
2314#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002315 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002316#ifdef FEAT_AUTOCMD
2317 if (win_valid(win))
2318 win->w_closing = FALSE;
2319#endif
2320 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002321
Bram Moolenaar802418d2013-01-17 14:00:11 +01002322 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2323 && (last_window() || curtab != prev_curtab
2324 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002325 {
2326 /* Autocommands have close all windows, quit now. Restore
2327 * curwin->w_buffer, otherwise writing viminfo may fail. */
2328 if (curwin->w_buffer == NULL)
2329 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002330 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002331 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002332
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002334 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002335 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002336 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002337 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaara971b822011-09-14 14:43:25 +02002339 /* Free the memory used for the window and get the window that received
2340 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002341 wp = win_free_mem(win, &dir, NULL);
2342
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 /* Make sure curwin isn't invalid. It can cause severe trouble when
2344 * printing an error message. For win_equal() curbuf needs to be valid
2345 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002346 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
2348 curwin = wp;
2349#ifdef FEAT_QUICKFIX
2350 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2351 {
2352 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002353 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 * finding another window to go to.
2355 */
2356 for (;;)
2357 {
2358 if (wp->w_next == NULL)
2359 wp = firstwin;
2360 else
2361 wp = wp->w_next;
2362 if (wp == curwin)
2363 break;
2364 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2365 {
2366 curwin = wp;
2367 break;
2368 }
2369 }
2370 }
2371#endif
2372 curbuf = curwin->w_buffer;
2373 close_curwin = TRUE;
2374 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002375 if (p_ea
2376#ifdef FEAT_VERTSPLIT
2377 && (*p_ead == 'b' || *p_ead == dir)
2378#endif
2379 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 win_equal(curwin, TRUE,
2381#ifdef FEAT_VERTSPLIT
2382 dir
2383#else
2384 0
2385#endif
2386 );
2387 else
2388 win_comp_pos();
2389 if (close_curwin)
2390 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002391 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef FEAT_AUTOCMD
2393 if (other_buffer)
2394 /* careful: after this wp and win may be invalid! */
2395 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2396#endif
2397 }
2398
2399 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002400 * If last window has a status line now and we don't want one,
2401 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 */
2403 last_status(FALSE);
2404
2405 /* After closing the help window, try restoring the window layout from
2406 * before it was opened. */
2407 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002408 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2411 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2412 if (gui.in_use && !win_hasvertsplit())
2413 gui_init_which_components(NULL);
2414#endif
2415
2416 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002417 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418}
2419
2420/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002421 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002422 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002423 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002424 * Caller must check if buffer is hidden and whether the tabline needs to be
2425 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002426 */
2427 void
2428win_close_othertab(win, free_buf, tp)
2429 win_T *win;
2430 int free_buf;
2431 tabpage_T *tp;
2432{
2433 win_T *wp;
2434 int dir;
2435 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002436 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002437
Bram Moolenaar362ce482012-06-06 19:02:45 +02002438#ifdef FEAT_AUTOCMD
2439 if (win->w_closing || win->w_buffer->b_closing)
2440 return; /* window is already being closed */
2441#endif
2442
Bram Moolenaarf740b292006-02-16 22:11:02 +00002443 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002444 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002445
2446 /* Careful: Autocommands may have closed the tab page or made it the
2447 * current tab page. */
2448 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2449 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002450 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002451 return;
2452
2453 /* Autocommands may have closed the window already. */
2454 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2455 ;
2456 if (wp == NULL)
2457 return;
2458
Bram Moolenaarf740b292006-02-16 22:11:02 +00002459 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002460 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002461 {
2462 if (tp == first_tabpage)
2463 first_tabpage = tp->tp_next;
2464 else
2465 {
2466 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2467 ptp = ptp->tp_next)
2468 ;
2469 if (ptp == NULL)
2470 {
2471 EMSG2(_(e_intern2), "win_close_othertab()");
2472 return;
2473 }
2474 ptp->tp_next = tp->tp_next;
2475 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002476 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002477 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002478
2479 /* Free the memory used for the window. */
2480 win_free_mem(win, &dir, tp);
2481
2482 if (free_tp)
2483 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002484}
2485
2486/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002487 * Free the memory used for a window.
2488 * Returns a pointer to the window that got the freed up space.
2489 */
2490 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002491win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002492 win_T *win;
2493 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002494 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002495{
2496 frame_T *frp;
2497 win_T *wp;
2498
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002499 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002500 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002501 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002502 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002503 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002504
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002505 /* When deleting the current window of another tab page select a new
2506 * current window. */
2507 if (tp != NULL && win == tp->tp_curwin)
2508 tp->tp_curwin = wp;
2509
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002510 return wp;
2511}
2512
2513#if defined(EXITFREE) || defined(PROTO)
2514 void
2515win_free_all()
2516{
2517 int dummy;
2518
Bram Moolenaarf740b292006-02-16 22:11:02 +00002519# ifdef FEAT_WINDOWS
2520 while (first_tabpage->tp_next != NULL)
2521 tabpage_close(TRUE);
2522# endif
2523
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002524# ifdef FEAT_AUTOCMD
2525 if (aucmd_win != NULL)
2526 {
2527 (void)win_free_mem(aucmd_win, &dummy, NULL);
2528 aucmd_win = NULL;
2529 }
2530# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002531
2532 while (firstwin != NULL)
2533 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002534
2535 /* No window should be used after this. Set curwin to NULL to crash
2536 * instead of using freed memory. */
2537 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002538}
2539#endif
2540
2541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 * Remove a window and its frame from the tree of frames.
2543 * Returns a pointer to the window that got the freed up space.
2544 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002545 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002546winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002548 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002549 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 frame_T *frp, *frp2, *frp3;
2552 frame_T *frp_close = win->w_frame;
2553 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002556 * If there is only one window there is nothing to remove.
2557 */
2558 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2559 return NULL;
2560
2561 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 * Remove the window from its frame.
2563 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002564 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 wp = frame2win(frp2);
2566
2567 /* Remove this frame from the list of frames. */
2568 frame_remove(frp_close);
2569
2570#ifdef FEAT_VERTSPLIT
2571 if (frp_close->fr_parent->fr_layout == FR_COL)
2572 {
2573#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002574 /* When 'winfixheight' is set, try to find another frame in the column
2575 * (as close to the closed frame as possible) to distribute the height
2576 * to. */
2577 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2578 {
2579 frp = frp_close->fr_prev;
2580 frp3 = frp_close->fr_next;
2581 while (frp != NULL || frp3 != NULL)
2582 {
2583 if (frp != NULL)
2584 {
2585 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2586 {
2587 frp2 = frp;
2588 wp = frp->fr_win;
2589 break;
2590 }
2591 frp = frp->fr_prev;
2592 }
2593 if (frp3 != NULL)
2594 {
2595 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2596 {
2597 frp2 = frp3;
2598 wp = frp3->fr_win;
2599 break;
2600 }
2601 frp3 = frp3->fr_next;
2602 }
2603 }
2604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2606 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607#ifdef FEAT_VERTSPLIT
2608 *dirp = 'v';
2609 }
2610 else
2611 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002612 /* When 'winfixwidth' is set, try to find another frame in the column
2613 * (as close to the closed frame as possible) to distribute the width
2614 * to. */
2615 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2616 {
2617 frp = frp_close->fr_prev;
2618 frp3 = frp_close->fr_next;
2619 while (frp != NULL || frp3 != NULL)
2620 {
2621 if (frp != NULL)
2622 {
2623 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2624 {
2625 frp2 = frp;
2626 wp = frp->fr_win;
2627 break;
2628 }
2629 frp = frp->fr_prev;
2630 }
2631 if (frp3 != NULL)
2632 {
2633 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2634 {
2635 frp2 = frp3;
2636 wp = frp3->fr_win;
2637 break;
2638 }
2639 frp3 = frp3->fr_next;
2640 }
2641 }
2642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002644 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 *dirp = 'h';
2646 }
2647#endif
2648
2649 /* If rows/columns go to a window below/right its positions need to be
2650 * updated. Can only be done after the sizes have been updated. */
2651 if (frp2 == frp_close->fr_next)
2652 {
2653 int row = win->w_winrow;
2654 int col = W_WINCOL(win);
2655
2656 frame_comp_pos(frp2, &row, &col);
2657 }
2658
2659 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2660 {
2661 /* There is no other frame in this list, move its info to the parent
2662 * and remove it. */
2663 frp2->fr_parent->fr_layout = frp2->fr_layout;
2664 frp2->fr_parent->fr_child = frp2->fr_child;
2665 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2666 frp->fr_parent = frp2->fr_parent;
2667 frp2->fr_parent->fr_win = frp2->fr_win;
2668 if (frp2->fr_win != NULL)
2669 frp2->fr_win->w_frame = frp2->fr_parent;
2670 frp = frp2->fr_parent;
2671 vim_free(frp2);
2672
2673 frp2 = frp->fr_parent;
2674 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2675 {
2676 /* The frame above the parent has the same layout, have to merge
2677 * the frames into this list. */
2678 if (frp2->fr_child == frp)
2679 frp2->fr_child = frp->fr_child;
2680 frp->fr_child->fr_prev = frp->fr_prev;
2681 if (frp->fr_prev != NULL)
2682 frp->fr_prev->fr_next = frp->fr_child;
2683 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2684 {
2685 frp3->fr_parent = frp2;
2686 if (frp3->fr_next == NULL)
2687 {
2688 frp3->fr_next = frp->fr_next;
2689 if (frp->fr_next != NULL)
2690 frp->fr_next->fr_prev = frp3;
2691 break;
2692 }
2693 }
2694 vim_free(frp);
2695 }
2696 }
2697
2698 return wp;
2699}
2700
2701/*
2702 * Find out which frame is going to get the freed up space when "win" is
2703 * closed.
2704 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2705 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2706 * This makes opening a window and closing it immediately keep the same window
2707 * layout.
2708 */
2709 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002710win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002712 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 frame_T *frp;
2715 int b;
2716
Bram Moolenaarf740b292006-02-16 22:11:02 +00002717 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002718 /* Last window in this tab page, will go to next tab page. */
2719 return alt_tabpage()->tp_curwin->w_frame;
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 frp = win->w_frame;
2722#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002723 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 b = p_spr;
2725 else
2726#endif
2727 b = p_sb;
2728 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2729 return frp->fr_next;
2730 return frp->fr_prev;
2731}
2732
2733/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002734 * Return the tabpage that will be used if the current one is closed.
2735 */
2736 static tabpage_T *
2737alt_tabpage()
2738{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002739 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002740
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002741 /* Use the next tab page if possible. */
2742 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002743 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002744
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002745 /* Find the last but one tab page. */
2746 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2747 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002748 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002749}
2750
2751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 * Find the left-upper window in frame "frp".
2753 */
2754 static win_T *
2755frame2win(frp)
2756 frame_T *frp;
2757{
2758 while (frp->fr_win == NULL)
2759 frp = frp->fr_child;
2760 return frp->fr_win;
2761}
2762
2763/*
2764 * Return TRUE if frame "frp" contains window "wp".
2765 */
2766 static int
2767frame_has_win(frp, wp)
2768 frame_T *frp;
2769 win_T *wp;
2770{
2771 frame_T *p;
2772
2773 if (frp->fr_layout == FR_LEAF)
2774 return frp->fr_win == wp;
2775
2776 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2777 if (frame_has_win(p, wp))
2778 return TRUE;
2779 return FALSE;
2780}
2781
2782/*
2783 * Set a new height for a frame. Recursively sets the height for contained
2784 * frames and windows. Caller must take care of positions.
2785 */
2786 static void
2787frame_new_height(topfrp, height, topfirst, wfh)
2788 frame_T *topfrp;
2789 int height;
2790 int topfirst; /* resize topmost contained frame first */
2791 int wfh; /* obey 'winfixheight' when there is a choice;
2792 may cause the height not to be set */
2793{
2794 frame_T *frp;
2795 int extra_lines;
2796 int h;
2797
2798 if (topfrp->fr_win != NULL)
2799 {
2800 /* Simple case: just one window. */
2801 win_new_height(topfrp->fr_win,
2802 height - topfrp->fr_win->w_status_height);
2803 }
2804#ifdef FEAT_VERTSPLIT
2805 else if (topfrp->fr_layout == FR_ROW)
2806 {
2807 do
2808 {
2809 /* All frames in this row get the same new height. */
2810 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2811 {
2812 frame_new_height(frp, height, topfirst, wfh);
2813 if (frp->fr_height > height)
2814 {
2815 /* Could not fit the windows, make the whole row higher. */
2816 height = frp->fr_height;
2817 break;
2818 }
2819 }
2820 }
2821 while (frp != NULL);
2822 }
2823#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002824 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
2826 /* Complicated case: Resize a column of frames. Resize the bottom
2827 * frame first, frames above that when needed. */
2828
2829 frp = topfrp->fr_child;
2830 if (wfh)
2831 /* Advance past frames with one window with 'wfh' set. */
2832 while (frame_fixed_height(frp))
2833 {
2834 frp = frp->fr_next;
2835 if (frp == NULL)
2836 return; /* no frame without 'wfh', give up */
2837 }
2838 if (!topfirst)
2839 {
2840 /* Find the bottom frame of this column */
2841 while (frp->fr_next != NULL)
2842 frp = frp->fr_next;
2843 if (wfh)
2844 /* Advance back for frames with one window with 'wfh' set. */
2845 while (frame_fixed_height(frp))
2846 frp = frp->fr_prev;
2847 }
2848
2849 extra_lines = height - topfrp->fr_height;
2850 if (extra_lines < 0)
2851 {
2852 /* reduce height of contained frames, bottom or top frame first */
2853 while (frp != NULL)
2854 {
2855 h = frame_minheight(frp, NULL);
2856 if (frp->fr_height + extra_lines < h)
2857 {
2858 extra_lines += frp->fr_height - h;
2859 frame_new_height(frp, h, topfirst, wfh);
2860 }
2861 else
2862 {
2863 frame_new_height(frp, frp->fr_height + extra_lines,
2864 topfirst, wfh);
2865 break;
2866 }
2867 if (topfirst)
2868 {
2869 do
2870 frp = frp->fr_next;
2871 while (wfh && frp != NULL && frame_fixed_height(frp));
2872 }
2873 else
2874 {
2875 do
2876 frp = frp->fr_prev;
2877 while (wfh && frp != NULL && frame_fixed_height(frp));
2878 }
2879 /* Increase "height" if we could not reduce enough frames. */
2880 if (frp == NULL)
2881 height -= extra_lines;
2882 }
2883 }
2884 else if (extra_lines > 0)
2885 {
2886 /* increase height of bottom or top frame */
2887 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2888 }
2889 }
2890 topfrp->fr_height = height;
2891}
2892
2893/*
2894 * Return TRUE if height of frame "frp" should not be changed because of
2895 * the 'winfixheight' option.
2896 */
2897 static int
2898frame_fixed_height(frp)
2899 frame_T *frp;
2900{
2901 /* frame with one window: fixed height if 'winfixheight' set. */
2902 if (frp->fr_win != NULL)
2903 return frp->fr_win->w_p_wfh;
2904
2905 if (frp->fr_layout == FR_ROW)
2906 {
2907 /* The frame is fixed height if one of the frames in the row is fixed
2908 * height. */
2909 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2910 if (frame_fixed_height(frp))
2911 return TRUE;
2912 return FALSE;
2913 }
2914
2915 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2916 * frames in the row are fixed height. */
2917 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2918 if (!frame_fixed_height(frp))
2919 return FALSE;
2920 return TRUE;
2921}
2922
2923#ifdef FEAT_VERTSPLIT
2924/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002925 * Return TRUE if width of frame "frp" should not be changed because of
2926 * the 'winfixwidth' option.
2927 */
2928 static int
2929frame_fixed_width(frp)
2930 frame_T *frp;
2931{
2932 /* frame with one window: fixed width if 'winfixwidth' set. */
2933 if (frp->fr_win != NULL)
2934 return frp->fr_win->w_p_wfw;
2935
2936 if (frp->fr_layout == FR_COL)
2937 {
2938 /* The frame is fixed width if one of the frames in the row is fixed
2939 * width. */
2940 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2941 if (frame_fixed_width(frp))
2942 return TRUE;
2943 return FALSE;
2944 }
2945
2946 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2947 * frames in the row are fixed width. */
2948 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2949 if (!frame_fixed_width(frp))
2950 return FALSE;
2951 return TRUE;
2952}
2953
2954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 * Add a status line to windows at the bottom of "frp".
2956 * Note: Does not check if there is room!
2957 */
2958 static void
2959frame_add_statusline(frp)
2960 frame_T *frp;
2961{
2962 win_T *wp;
2963
2964 if (frp->fr_layout == FR_LEAF)
2965 {
2966 wp = frp->fr_win;
2967 if (wp->w_status_height == 0)
2968 {
2969 if (wp->w_height > 0) /* don't make it negative */
2970 --wp->w_height;
2971 wp->w_status_height = STATUS_HEIGHT;
2972 }
2973 }
2974 else if (frp->fr_layout == FR_ROW)
2975 {
2976 /* Handle all the frames in the row. */
2977 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2978 frame_add_statusline(frp);
2979 }
2980 else /* frp->fr_layout == FR_COL */
2981 {
2982 /* Only need to handle the last frame in the column. */
2983 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2984 ;
2985 frame_add_statusline(frp);
2986 }
2987}
2988
2989/*
2990 * Set width of a frame. Handles recursively going through contained frames.
2991 * May remove separator line for windows at the right side (for win_close()).
2992 */
2993 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002994frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 frame_T *topfrp;
2996 int width;
2997 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002998 int wfw; /* obey 'winfixwidth' when there is a choice;
2999 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 frame_T *frp;
3002 int extra_cols;
3003 int w;
3004 win_T *wp;
3005
3006 if (topfrp->fr_layout == FR_LEAF)
3007 {
3008 /* Simple case: just one window. */
3009 wp = topfrp->fr_win;
3010 /* Find out if there are any windows right of this one. */
3011 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3012 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3013 break;
3014 if (frp->fr_parent == NULL)
3015 wp->w_vsep_width = 0;
3016 win_new_width(wp, width - wp->w_vsep_width);
3017 }
3018 else if (topfrp->fr_layout == FR_COL)
3019 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003020 do
3021 {
3022 /* All frames in this column get the same new width. */
3023 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3024 {
3025 frame_new_width(frp, width, leftfirst, wfw);
3026 if (frp->fr_width > width)
3027 {
3028 /* Could not fit the windows, make whole column wider. */
3029 width = frp->fr_width;
3030 break;
3031 }
3032 }
3033 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 }
3035 else /* fr_layout == FR_ROW */
3036 {
3037 /* Complicated case: Resize a row of frames. Resize the rightmost
3038 * frame first, frames left of it when needed. */
3039
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003041 if (wfw)
3042 /* Advance past frames with one window with 'wfw' set. */
3043 while (frame_fixed_width(frp))
3044 {
3045 frp = frp->fr_next;
3046 if (frp == NULL)
3047 return; /* no frame without 'wfw', give up */
3048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003050 {
3051 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 while (frp->fr_next != NULL)
3053 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003054 if (wfw)
3055 /* Advance back for frames with one window with 'wfw' set. */
3056 while (frame_fixed_width(frp))
3057 frp = frp->fr_prev;
3058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059
3060 extra_cols = width - topfrp->fr_width;
3061 if (extra_cols < 0)
3062 {
3063 /* reduce frame width, rightmost frame first */
3064 while (frp != NULL)
3065 {
3066 w = frame_minwidth(frp, NULL);
3067 if (frp->fr_width + extra_cols < w)
3068 {
3069 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003070 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072 else
3073 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003074 frame_new_width(frp, frp->fr_width + extra_cols,
3075 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 break;
3077 }
3078 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003079 {
3080 do
3081 frp = frp->fr_next;
3082 while (wfw && frp != NULL && frame_fixed_width(frp));
3083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003085 {
3086 do
3087 frp = frp->fr_prev;
3088 while (wfw && frp != NULL && frame_fixed_width(frp));
3089 }
3090 /* Increase "width" if we could not reduce enough frames. */
3091 if (frp == NULL)
3092 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
3094 }
3095 else if (extra_cols > 0)
3096 {
3097 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003098 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100 }
3101 topfrp->fr_width = width;
3102}
3103
3104/*
3105 * Add the vertical separator to windows at the right side of "frp".
3106 * Note: Does not check if there is room!
3107 */
3108 static void
3109frame_add_vsep(frp)
3110 frame_T *frp;
3111{
3112 win_T *wp;
3113
3114 if (frp->fr_layout == FR_LEAF)
3115 {
3116 wp = frp->fr_win;
3117 if (wp->w_vsep_width == 0)
3118 {
3119 if (wp->w_width > 0) /* don't make it negative */
3120 --wp->w_width;
3121 wp->w_vsep_width = 1;
3122 }
3123 }
3124 else if (frp->fr_layout == FR_COL)
3125 {
3126 /* Handle all the frames in the column. */
3127 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3128 frame_add_vsep(frp);
3129 }
3130 else /* frp->fr_layout == FR_ROW */
3131 {
3132 /* Only need to handle the last frame in the row. */
3133 frp = frp->fr_child;
3134 while (frp->fr_next != NULL)
3135 frp = frp->fr_next;
3136 frame_add_vsep(frp);
3137 }
3138}
3139
3140/*
3141 * Set frame width from the window it contains.
3142 */
3143 static void
3144frame_fix_width(wp)
3145 win_T *wp;
3146{
3147 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3148}
3149#endif
3150
3151/*
3152 * Set frame height from the window it contains.
3153 */
3154 static void
3155frame_fix_height(wp)
3156 win_T *wp;
3157{
3158 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3159}
3160
3161/*
3162 * Compute the minimal height for frame "topfrp".
3163 * Uses the 'winminheight' option.
3164 * When "next_curwin" isn't NULL, use p_wh for this window.
3165 * When "next_curwin" is NOWIN, don't use at least one line for the current
3166 * window.
3167 */
3168 static int
3169frame_minheight(topfrp, next_curwin)
3170 frame_T *topfrp;
3171 win_T *next_curwin;
3172{
3173 frame_T *frp;
3174 int m;
3175#ifdef FEAT_VERTSPLIT
3176 int n;
3177#endif
3178
3179 if (topfrp->fr_win != NULL)
3180 {
3181 if (topfrp->fr_win == next_curwin)
3182 m = p_wh + topfrp->fr_win->w_status_height;
3183 else
3184 {
3185 /* window: minimal height of the window plus status line */
3186 m = p_wmh + topfrp->fr_win->w_status_height;
3187 /* Current window is minimal one line high */
3188 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3189 ++m;
3190 }
3191 }
3192#ifdef FEAT_VERTSPLIT
3193 else if (topfrp->fr_layout == FR_ROW)
3194 {
3195 /* get the minimal height from each frame in this row */
3196 m = 0;
3197 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3198 {
3199 n = frame_minheight(frp, next_curwin);
3200 if (n > m)
3201 m = n;
3202 }
3203 }
3204#endif
3205 else
3206 {
3207 /* Add up the minimal heights for all frames in this column. */
3208 m = 0;
3209 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3210 m += frame_minheight(frp, next_curwin);
3211 }
3212
3213 return m;
3214}
3215
3216#ifdef FEAT_VERTSPLIT
3217/*
3218 * Compute the minimal width for frame "topfrp".
3219 * When "next_curwin" isn't NULL, use p_wiw for this window.
3220 * When "next_curwin" is NOWIN, don't use at least one column for the current
3221 * window.
3222 */
3223 static int
3224frame_minwidth(topfrp, next_curwin)
3225 frame_T *topfrp;
3226 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3227{
3228 frame_T *frp;
3229 int m, n;
3230
3231 if (topfrp->fr_win != NULL)
3232 {
3233 if (topfrp->fr_win == next_curwin)
3234 m = p_wiw + topfrp->fr_win->w_vsep_width;
3235 else
3236 {
3237 /* window: minimal width of the window plus separator column */
3238 m = p_wmw + topfrp->fr_win->w_vsep_width;
3239 /* Current window is minimal one column wide */
3240 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3241 ++m;
3242 }
3243 }
3244 else if (topfrp->fr_layout == FR_COL)
3245 {
3246 /* get the minimal width from each frame in this column */
3247 m = 0;
3248 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3249 {
3250 n = frame_minwidth(frp, next_curwin);
3251 if (n > m)
3252 m = n;
3253 }
3254 }
3255 else
3256 {
3257 /* Add up the minimal widths for all frames in this row. */
3258 m = 0;
3259 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3260 m += frame_minwidth(frp, next_curwin);
3261 }
3262
3263 return m;
3264}
3265#endif
3266
3267
3268/*
3269 * Try to close all windows except current one.
3270 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3271 * used and the buffer was modified.
3272 *
3273 * Used by ":bdel" and ":only".
3274 */
3275 void
3276close_others(message, forceit)
3277 int message;
3278 int forceit; /* always hide all other windows */
3279{
3280 win_T *wp;
3281 win_T *nextwp;
3282 int r;
3283
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003284 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
3286 if (message
3287#ifdef FEAT_AUTOCMD
3288 && !autocmd_busy
3289#endif
3290 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003291 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return;
3293 }
3294
3295 /* Be very careful here: autocommands may change the window layout. */
3296 for (wp = firstwin; win_valid(wp); wp = nextwp)
3297 {
3298 nextwp = wp->w_next;
3299 if (wp != curwin) /* don't close current window */
3300 {
3301
3302 /* Check if it's allowed to abandon this window */
3303 r = can_abandon(wp->w_buffer, forceit);
3304#ifdef FEAT_AUTOCMD
3305 if (!win_valid(wp)) /* autocommands messed wp up */
3306 {
3307 nextwp = firstwin;
3308 continue;
3309 }
3310#endif
3311 if (!r)
3312 {
3313#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3314 if (message && (p_confirm || cmdmod.confirm) && p_write)
3315 {
3316 dialog_changed(wp->w_buffer, FALSE);
3317# ifdef FEAT_AUTOCMD
3318 if (!win_valid(wp)) /* autocommands messed wp up */
3319 {
3320 nextwp = firstwin;
3321 continue;
3322 }
3323# endif
3324 }
3325 if (bufIsChanged(wp->w_buffer))
3326#endif
3327 continue;
3328 }
3329 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3330 }
3331 }
3332
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003333 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 EMSG(_("E445: Other window contains changes"));
3335}
3336
3337#endif /* FEAT_WINDOWS */
3338
3339/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003340 * Init the current window "curwin".
3341 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 */
3343 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003344curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003346 win_init_empty(curwin);
3347}
3348
3349 void
3350win_init_empty(wp)
3351 win_T *wp;
3352{
3353 redraw_win_later(wp, NOT_VALID);
3354 wp->w_lines_valid = 0;
3355 wp->w_cursor.lnum = 1;
3356 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003358 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003360 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3361 wp->w_pcmark.col = 0;
3362 wp->w_prev_pcmark.lnum = 0;
3363 wp->w_prev_pcmark.col = 0;
3364 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003366 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003368 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003370 if (wp->w_p_rl)
3371 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003373 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003375#ifdef FEAT_SYN_HL
3376 wp->w_s = &wp->w_buffer->b_s;
3377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378}
3379
3380/*
3381 * Allocate the first window and put an empty buffer in it.
3382 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003383 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003385 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386win_alloc_first()
3387{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003388 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003389 return FAIL;
3390
3391#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003392 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003393 if (first_tabpage == NULL)
3394 return FAIL;
3395 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003396 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003397#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003398
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003399 return OK;
3400}
3401
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003402#if defined(FEAT_AUTOCMD) || defined(PROTO)
3403/*
3404 * Init "aucmd_win". This can only be done after the first
3405 * window is fully initialized, thus it can't be in win_alloc_first().
3406 */
3407 void
3408win_alloc_aucmd_win()
3409{
3410 aucmd_win = win_alloc(NULL, TRUE);
3411 if (aucmd_win != NULL)
3412 {
3413 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003414 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003415 new_frame(aucmd_win);
3416 }
3417}
3418#endif
3419
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003421 * Allocate the first window or the first window in a new tab page.
3422 * When "oldwin" is NULL create an empty buffer for it.
3423 * When "oldwin" is not NULL copy info from it to the new window (only with
3424 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003425 * Return FAIL when something goes wrong (out of memory).
3426 */
3427 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003428win_alloc_firstwin(oldwin)
3429 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003431 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003432 if (oldwin == NULL)
3433 {
3434 /* Very first window, need to create an empty buffer for it and
3435 * initialize from scratch. */
3436 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3437 if (curwin == NULL || curbuf == NULL)
3438 return FAIL;
3439 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003440#ifdef FEAT_SYN_HL
3441 curwin->w_s = &(curbuf->b_s);
3442#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003443 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003445 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003447 curwin_init(); /* init current window */
3448 }
3449#ifdef FEAT_WINDOWS
3450 else
3451 {
3452 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003453 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003454
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003455 /* We don't want cursor- and scroll-binding in the first window. */
3456 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003457 }
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003460 new_frame(curwin);
3461 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003462 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003463 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464#ifdef FEAT_VERTSPLIT
3465 topframe->fr_width = Columns;
3466#endif
3467 topframe->fr_height = Rows - p_ch;
3468 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003469
3470 return OK;
3471}
3472
3473/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003474 * Create a frame for window "wp".
3475 */
3476 static void
3477new_frame(win_T *wp)
3478{
3479 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3480
3481 wp->w_frame = frp;
3482 if (frp != NULL)
3483 {
3484 frp->fr_layout = FR_LEAF;
3485 frp->fr_win = wp;
3486 }
3487}
3488
3489/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003490 * Initialize the window and frame size to the maximum.
3491 */
3492 void
3493win_init_size()
3494{
3495 firstwin->w_height = ROWS_AVAIL;
3496 topframe->fr_height = ROWS_AVAIL;
3497#ifdef FEAT_VERTSPLIT
3498 firstwin->w_width = Columns;
3499 topframe->fr_width = Columns;
3500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501}
3502
3503#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003504
3505/*
3506 * Allocate a new tabpage_T and init the values.
3507 * Returns NULL when out of memory.
3508 */
3509 static tabpage_T *
3510alloc_tabpage()
3511{
3512 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003513# ifdef FEAT_GUI
3514 int i;
3515# endif
3516
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003517
3518 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003519 if (tp == NULL)
3520 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003521
Bram Moolenaar429fa852013-04-15 12:27:36 +02003522# ifdef FEAT_EVAL
3523 /* init t: variables */
3524 tp->tp_vars = dict_alloc();
3525 if (tp->tp_vars == NULL)
3526 {
3527 vim_free(tp);
3528 return NULL;
3529 }
3530 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3531# endif
3532
3533# ifdef FEAT_GUI
3534 for (i = 0; i < 3; i++)
3535 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003536# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003537# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003538 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003539# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003540 tp->tp_ch_used = p_ch;
3541
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003542 return tp;
3543}
3544
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003545 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003546free_tabpage(tp)
3547 tabpage_T *tp;
3548{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003549 int idx;
3550
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003551# ifdef FEAT_DIFF
3552 diff_clear(tp);
3553# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003554 for (idx = 0; idx < SNAP_COUNT; ++idx)
3555 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003556#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003557 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3558 hash_init(&tp->tp_vars->dv_hashtab);
3559 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003560#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003561
3562#ifdef FEAT_PYTHON
3563 python_tabpage_free(tp);
3564#endif
3565
3566#ifdef FEAT_PYTHON3
3567 python3_tabpage_free(tp);
3568#endif
3569
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003570 vim_free(tp);
3571}
3572
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003573/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003574 * Create a new Tab page with one window.
3575 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003576 * When "after" is 0 put it just after the current Tab page.
3577 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003578 * Return FAIL or OK.
3579 */
3580 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003581win_new_tabpage(after)
3582 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003583{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003584 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003585 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003586 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003587
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003588 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003589 if (newtp == NULL)
3590 return FAIL;
3591
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003593 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003594 {
3595 vim_free(newtp);
3596 return FAIL;
3597 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003598 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003599
3600 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003601 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003602 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003603 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003604 if (after == 1)
3605 {
3606 /* New tab page becomes the first one. */
3607 newtp->tp_next = first_tabpage;
3608 first_tabpage = newtp;
3609 }
3610 else
3611 {
3612 if (after > 0)
3613 {
3614 /* Put new tab page before tab page "after". */
3615 n = 2;
3616 for (tp = first_tabpage; tp->tp_next != NULL
3617 && n < after; tp = tp->tp_next)
3618 ++n;
3619 }
3620 newtp->tp_next = tp->tp_next;
3621 tp->tp_next = newtp;
3622 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003623 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003624 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003625 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003626
3627 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003628 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003629
3630#if defined(FEAT_GUI)
3631 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3632 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003633 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003634#endif
3635
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003636 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003637#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003638 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003639 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003640#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003641 return OK;
3642 }
3643
3644 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003645 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003646 return FAIL;
3647}
3648
3649/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003650 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3651 * like with ":split".
3652 * Returns OK if a new tab page was created, FAIL otherwise.
3653 */
3654 int
3655may_open_tabpage()
3656{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003657 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003658
Bram Moolenaard326ce82007-03-11 14:48:29 +00003659 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003660 {
3661 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003662 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003663 return win_new_tabpage(n);
3664 }
3665 return FAIL;
3666}
3667
3668/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003669 * Create up to "maxcount" tabpages with empty windows.
3670 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003672 int
3673make_tabpages(maxcount)
3674 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003675{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003676 int count = maxcount;
3677 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003678
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003679 /* Limit to 'tabpagemax' tabs. */
3680 if (count > p_tpm)
3681 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003682
3683#ifdef FEAT_AUTOCMD
3684 /*
3685 * Don't execute autocommands while creating the tab pages. Must do that
3686 * when putting the buffers in the windows.
3687 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003688 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003689#endif
3690
3691 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003692 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003693 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003694
3695#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003696 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003697#endif
3698
3699 /* return actual number of tab pages */
3700 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003701}
3702
3703/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003704 * Return TRUE when "tpc" points to a valid tab page.
3705 */
3706 int
3707valid_tabpage(tpc)
3708 tabpage_T *tpc;
3709{
3710 tabpage_T *tp;
3711
3712 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3713 if (tp == tpc)
3714 return TRUE;
3715 return FALSE;
3716}
3717
3718/*
3719 * Find tab page "n" (first one is 1). Returns NULL when not found.
3720 */
3721 tabpage_T *
3722find_tabpage(n)
3723 int n;
3724{
3725 tabpage_T *tp;
3726 int i = 1;
3727
3728 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3729 ++i;
3730 return tp;
3731}
3732
3733/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003734 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003735 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003736 */
3737 int
3738tabpage_index(ftp)
3739 tabpage_T *ftp;
3740{
3741 int i = 1;
3742 tabpage_T *tp;
3743
3744 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3745 ++i;
3746 return i;
3747}
3748
3749/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003750 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003751 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003752 * FAIL.
3753 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003754 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003755 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003756leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003757 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003758 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003759 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003760{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003761 tabpage_T *tp = curtab;
3762
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003763 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003764#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003765 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003766 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003767 if (new_curbuf != curbuf)
3768 {
3769 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3770 if (curtab != tp)
3771 return FAIL;
3772 }
3773 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3774 if (curtab != tp)
3775 return FAIL;
3776 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003777 if (curtab != tp)
3778 return FAIL;
3779 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003780#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003781#if defined(FEAT_GUI)
3782 /* Remove the scrollbars. They may be added back later. */
3783 if (gui.in_use)
3784 gui_remove_scrollbars();
3785#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003786 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003787 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003788 tp->tp_firstwin = firstwin;
3789 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003790 tp->tp_old_Rows = Rows;
3791 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003792 firstwin = NULL;
3793 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003794 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003795}
3796
3797/*
3798 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003799 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003800 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3801 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003802 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003803 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003804enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003805 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003806 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003807 int trigger_enter_autocmds UNUSED;
3808 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003809{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003810 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003811 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003812
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003813 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003814 firstwin = tp->tp_firstwin;
3815 lastwin = tp->tp_lastwin;
3816 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003817
3818 /* We would like doing the TabEnter event first, but we don't have a
3819 * valid current window yet, which may break some commands.
3820 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003821 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3822 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003823 prevwin = next_prevwin;
3824
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003825 last_status(FALSE); /* status line may appear or disappear */
3826 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003827 must_redraw = CLEAR; /* need to redraw everything */
3828#ifdef FEAT_DIFF
3829 diff_need_scrollbind = TRUE;
3830#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003832 /* The tabpage line may have appeared or disappeared, may need to resize
3833 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003834 * frame sizes too. Use the stored value of p_ch, so that it can be
3835 * different for each tab page. */
3836 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003837 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3838#ifdef FEAT_GUI_TABLINE
3839 && !gui_use_tabline()
3840#endif
3841 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003842 shell_new_rows();
3843#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003844 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003845 shell_new_columns(); /* update window widths */
3846#endif
3847
3848#if defined(FEAT_GUI)
3849 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3850 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003851 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003852#endif
3853
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003854#ifdef FEAT_AUTOCMD
3855 /* Apply autocommands after updating the display, when 'rows' and
3856 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003857 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003858 {
3859 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3860 if (old_curbuf != curbuf)
3861 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3862 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003863#endif
3864
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003865 redraw_all_later(CLEAR);
3866}
3867
3868/*
3869 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003870 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003871 */
3872 void
3873goto_tabpage(n)
3874 int n;
3875{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003876 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003877 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003878 int i;
3879
Bram Moolenaard68071d2006-05-02 22:08:30 +00003880 if (text_locked())
3881 {
3882 /* Not allowed when editing the command line. */
3883#ifdef FEAT_CMDWIN
3884 if (cmdwin_type != 0)
3885 EMSG(_(e_cmdwin));
3886 else
3887#endif
3888 EMSG(_(e_secure));
3889 return;
3890 }
3891
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003892 /* If there is only one it can't work. */
3893 if (first_tabpage->tp_next == NULL)
3894 {
3895 if (n > 1)
3896 beep_flush();
3897 return;
3898 }
3899
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003900 if (n == 0)
3901 {
3902 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003903 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003904 tp = first_tabpage;
3905 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003906 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003907 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003908 else if (n < 0)
3909 {
3910 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3911 * this N times. */
3912 ttp = curtab;
3913 for (i = n; i < 0; ++i)
3914 {
3915 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3916 tp = tp->tp_next)
3917 ;
3918 ttp = tp;
3919 }
3920 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003921 else if (n == 9999)
3922 {
3923 /* Go to last tab page. */
3924 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3925 ;
3926 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003927 else
3928 {
3929 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003930 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003931 if (tp == NULL)
3932 {
3933 beep_flush();
3934 return;
3935 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003936 }
3937
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003938 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003939
3940#ifdef FEAT_GUI_TABLINE
3941 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003942 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003943#endif
3944}
3945
3946/*
3947 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003948 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3949 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003950 * Note: doesn't update the GUI tab.
3951 */
3952 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003953goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003954 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003955 int trigger_enter_autocmds;
3956 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003957{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003958 /* Don't repeat a message in another tab page. */
3959 set_keep_msg(NULL, 0);
3960
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003961 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3962 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003963 {
3964 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003965 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3966 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003967 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003968 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3969 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003970 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003971}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972
3973/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003974 * Enter window "wp" in tab page "tp".
3975 * Also updates the GUI tab.
3976 */
3977 void
3978goto_tabpage_win(tp, wp)
3979 tabpage_T *tp;
3980 win_T *wp;
3981{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003982 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003983 if (curtab == tp && win_valid(wp))
3984 {
3985 win_enter(wp, TRUE);
3986# ifdef FEAT_GUI_TABLINE
3987 if (gui_use_tabline())
3988 gui_mch_set_curtab(tabpage_index(curtab));
3989# endif
3990 }
3991}
3992
3993/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003994 * Move the current tab page to before tab page "nr".
3995 */
3996 void
3997tabpage_move(nr)
3998 int nr;
3999{
4000 int n = nr;
4001 tabpage_T *tp;
4002
4003 if (first_tabpage->tp_next == NULL)
4004 return;
4005
4006 /* Remove the current tab page from the list of tab pages. */
4007 if (curtab == first_tabpage)
4008 first_tabpage = curtab->tp_next;
4009 else
4010 {
4011 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4012 if (tp->tp_next == curtab)
4013 break;
4014 if (tp == NULL) /* "cannot happen" */
4015 return;
4016 tp->tp_next = curtab->tp_next;
4017 }
4018
4019 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02004020 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004021 {
4022 curtab->tp_next = first_tabpage;
4023 first_tabpage = curtab;
4024 }
4025 else
4026 {
4027 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
4028 --n;
4029 curtab->tp_next = tp->tp_next;
4030 tp->tp_next = curtab;
4031 }
4032
4033 /* Need to redraw the tabline. Tab page contents doesn't change. */
4034 redraw_tabline = TRUE;
4035}
4036
4037
4038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 * Go to another window.
4040 * When jumping to another buffer, stop Visual mode. Do this before
4041 * changing windows so we can yank the selection into the '*' register.
4042 * When jumping to another window on the same buffer, adjust its cursor
4043 * position to keep the same Visual area.
4044 */
4045 void
4046win_goto(wp)
4047 win_T *wp;
4048{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004049#ifdef FEAT_CONCEAL
4050 win_T *owp = curwin;
4051#endif
4052
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004053 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
4055 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004056 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 return;
4058 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059#ifdef FEAT_AUTOCMD
4060 if (curbuf_locked())
4061 return;
4062#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004063
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 if (wp->w_buffer != curbuf)
4065 reset_VIsual_and_resel();
4066 else if (VIsual_active)
4067 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069#ifdef FEAT_GUI
4070 need_mouse_correct = TRUE;
4071#endif
4072 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004073
4074#ifdef FEAT_CONCEAL
4075 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004076 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004077 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004078 if (curwin->w_p_cole > 0 && !msg_scrolled)
4079 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081}
4082
4083#if defined(FEAT_PERL) || defined(PROTO)
4084/*
4085 * Find window number "winnr" (counting top to bottom).
4086 */
4087 win_T *
4088win_find_nr(winnr)
4089 int winnr;
4090{
4091 win_T *wp;
4092
4093# ifdef FEAT_WINDOWS
4094 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4095 if (--winnr == 0)
4096 break;
4097 return wp;
4098# else
4099 return curwin;
4100# endif
4101}
4102#endif
4103
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004104#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4105 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004106/*
4107 * Find the tabpage for window "win".
4108 */
4109 tabpage_T *
4110win_find_tabpage(win)
4111 win_T *win;
4112{
4113 win_T *wp;
4114 tabpage_T *tp;
4115
4116 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004117 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4118 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004119 if (wp == win)
4120 return tp;
4121 return NULL;
4122}
4123#endif
4124
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125#ifdef FEAT_VERTSPLIT
4126/*
4127 * Move to window above or below "count" times.
4128 */
4129 static void
4130win_goto_ver(up, count)
4131 int up; /* TRUE to go to win above */
4132 long count;
4133{
4134 frame_T *fr;
4135 frame_T *nfr;
4136 frame_T *foundfr;
4137
4138 foundfr = curwin->w_frame;
4139 while (count--)
4140 {
4141 /*
4142 * First go upwards in the tree of frames until we find a upwards or
4143 * downwards neighbor.
4144 */
4145 fr = foundfr;
4146 for (;;)
4147 {
4148 if (fr == topframe)
4149 goto end;
4150 if (up)
4151 nfr = fr->fr_prev;
4152 else
4153 nfr = fr->fr_next;
4154 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4155 break;
4156 fr = fr->fr_parent;
4157 }
4158
4159 /*
4160 * Now go downwards to find the bottom or top frame in it.
4161 */
4162 for (;;)
4163 {
4164 if (nfr->fr_layout == FR_LEAF)
4165 {
4166 foundfr = nfr;
4167 break;
4168 }
4169 fr = nfr->fr_child;
4170 if (nfr->fr_layout == FR_ROW)
4171 {
4172 /* Find the frame at the cursor row. */
4173 while (fr->fr_next != NULL
4174 && frame2win(fr)->w_wincol + fr->fr_width
4175 <= curwin->w_wincol + curwin->w_wcol)
4176 fr = fr->fr_next;
4177 }
4178 if (nfr->fr_layout == FR_COL && up)
4179 while (fr->fr_next != NULL)
4180 fr = fr->fr_next;
4181 nfr = fr;
4182 }
4183 }
4184end:
4185 if (foundfr != NULL)
4186 win_goto(foundfr->fr_win);
4187}
4188
4189/*
4190 * Move to left or right window.
4191 */
4192 static void
4193win_goto_hor(left, count)
4194 int left; /* TRUE to go to left win */
4195 long count;
4196{
4197 frame_T *fr;
4198 frame_T *nfr;
4199 frame_T *foundfr;
4200
4201 foundfr = curwin->w_frame;
4202 while (count--)
4203 {
4204 /*
4205 * First go upwards in the tree of frames until we find a left or
4206 * right neighbor.
4207 */
4208 fr = foundfr;
4209 for (;;)
4210 {
4211 if (fr == topframe)
4212 goto end;
4213 if (left)
4214 nfr = fr->fr_prev;
4215 else
4216 nfr = fr->fr_next;
4217 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4218 break;
4219 fr = fr->fr_parent;
4220 }
4221
4222 /*
4223 * Now go downwards to find the leftmost or rightmost frame in it.
4224 */
4225 for (;;)
4226 {
4227 if (nfr->fr_layout == FR_LEAF)
4228 {
4229 foundfr = nfr;
4230 break;
4231 }
4232 fr = nfr->fr_child;
4233 if (nfr->fr_layout == FR_COL)
4234 {
4235 /* Find the frame at the cursor row. */
4236 while (fr->fr_next != NULL
4237 && frame2win(fr)->w_winrow + fr->fr_height
4238 <= curwin->w_winrow + curwin->w_wrow)
4239 fr = fr->fr_next;
4240 }
4241 if (nfr->fr_layout == FR_ROW && left)
4242 while (fr->fr_next != NULL)
4243 fr = fr->fr_next;
4244 nfr = fr;
4245 }
4246 }
4247end:
4248 if (foundfr != NULL)
4249 win_goto(foundfr->fr_win);
4250}
4251#endif
4252
4253/*
4254 * Make window "wp" the current window.
4255 */
4256 void
4257win_enter(wp, undo_sync)
4258 win_T *wp;
4259 int undo_sync;
4260{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004261 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262}
4263
4264/*
4265 * Make window wp the current window.
4266 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4267 * been closed and isn't valid.
4268 */
4269 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004270win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 win_T *wp;
4272 int undo_sync;
4273 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004274 int trigger_enter_autocmds UNUSED;
4275 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276{
4277#ifdef FEAT_AUTOCMD
4278 int other_buffer = FALSE;
4279#endif
4280
4281 if (wp == curwin && !curwin_invalid) /* nothing to do */
4282 return;
4283
4284#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004285 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
4287 /*
4288 * Be careful: If autocommands delete the window, return now.
4289 */
4290 if (wp->w_buffer != curbuf)
4291 {
4292 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4293 other_buffer = TRUE;
4294 if (!win_valid(wp))
4295 return;
4296 }
4297 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4298 if (!win_valid(wp))
4299 return;
4300# ifdef FEAT_EVAL
4301 /* autocmds may abort script processing */
4302 if (aborting())
4303 return;
4304# endif
4305 }
4306#endif
4307
4308 /* sync undo before leaving the current buffer */
4309 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004310 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004311
4312 /* Might need to scroll the old window before switching, e.g., when the
4313 * cursor was moved. */
4314 update_topline();
4315
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 /* may have to copy the buffer options when 'cpo' contains 'S' */
4317 if (wp->w_buffer != curbuf)
4318 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4319 if (!curwin_invalid)
4320 {
4321 prevwin = curwin; /* remember for CTRL-W p */
4322 curwin->w_redr_status = TRUE;
4323 }
4324 curwin = wp;
4325 curbuf = wp->w_buffer;
4326 check_cursor();
4327#ifdef FEAT_VIRTUALEDIT
4328 if (!virtual_active())
4329 curwin->w_cursor.coladd = 0;
4330#endif
4331 changed_line_abv_curs(); /* assume cursor position needs updating */
4332
4333 if (curwin->w_localdir != NULL)
4334 {
4335 /* Window has a local directory: Save current directory as global
4336 * directory (unless that was done already) and change to the local
4337 * directory. */
4338 if (globaldir == NULL)
4339 {
4340 char_u cwd[MAXPATHL];
4341
4342 if (mch_dirname(cwd, MAXPATHL) == OK)
4343 globaldir = vim_strsave(cwd);
4344 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004345 if (mch_chdir((char *)curwin->w_localdir) == 0)
4346 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 else if (globaldir != NULL)
4349 {
4350 /* Window doesn't have a local directory and we are not in the global
4351 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004352 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 vim_free(globaldir);
4354 globaldir = NULL;
4355 shorten_fnames(TRUE);
4356 }
4357
4358#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004359 if (trigger_enter_autocmds)
4360 {
4361 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4362 if (other_buffer)
4363 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365#endif
4366
4367#ifdef FEAT_TITLE
4368 maketitle();
4369#endif
4370 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004371 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 if (restart_edit)
4373 redraw_later(VALID); /* causes status line redraw */
4374
4375 /* set window height to desired minimal value */
4376 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4377 win_setheight((int)p_wh);
4378 else if (curwin->w_height == 0)
4379 win_setheight(1);
4380
4381#ifdef FEAT_VERTSPLIT
4382 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004383 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 win_setwidth((int)p_wiw);
4385#endif
4386
4387#ifdef FEAT_MOUSE
4388 setmouse(); /* in case jumped to/from help buffer */
4389#endif
4390
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004391 /* Change directories when the 'acd' option is set. */
4392 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393}
4394
4395#endif /* FEAT_WINDOWS */
4396
4397#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4398/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004399 * Jump to the first open window that contains buffer "buf", if one exists.
4400 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
4402 win_T *
4403buf_jump_open_win(buf)
4404 buf_T *buf;
4405{
4406# ifdef FEAT_WINDOWS
4407 win_T *wp;
4408
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004409 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 if (wp->w_buffer == buf)
4411 break;
4412 if (wp != NULL)
4413 win_enter(wp, FALSE);
4414 return wp;
4415# else
4416 if (curwin->w_buffer == buf)
4417 return curwin;
4418 return NULL;
4419# endif
4420}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004421
4422/*
4423 * Jump to the first open window in any tab page that contains buffer "buf",
4424 * if one exists.
4425 * Returns a pointer to the window found, otherwise NULL.
4426 */
4427 win_T *
4428buf_jump_open_tab(buf)
4429 buf_T *buf;
4430{
4431# ifdef FEAT_WINDOWS
4432 win_T *wp;
4433 tabpage_T *tp;
4434
4435 /* First try the current tab page. */
4436 wp = buf_jump_open_win(buf);
4437 if (wp != NULL)
4438 return wp;
4439
4440 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4441 if (tp != curtab)
4442 {
4443 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4444 if (wp->w_buffer == buf)
4445 break;
4446 if (wp != NULL)
4447 {
4448 goto_tabpage_win(tp, wp);
4449 if (curwin != wp)
4450 wp = NULL; /* something went wrong */
4451 break;
4452 }
4453 }
4454
4455 return wp;
4456# else
4457 if (curwin->w_buffer == buf)
4458 return curwin;
4459 return NULL;
4460# endif
4461}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#endif
4463
4464/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004465 * Allocate a window structure and link it in the window list when "hidden" is
4466 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004469win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004470 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004471 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004473 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475 /*
4476 * allocate window structure and linesizes arrays
4477 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004478 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004479 if (new_wp == NULL)
4480 return NULL;
4481
4482 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004484 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004485 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487
Bram Moolenaar429fa852013-04-15 12:27:36 +02004488#ifdef FEAT_EVAL
4489 /* init w: variables */
4490 new_wp->w_vars = dict_alloc();
4491 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004493 win_free_lsize(new_wp);
4494 vim_free(new_wp);
4495 return NULL;
4496 }
4497 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004498#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004499
4500#ifdef FEAT_AUTOCMD
4501 /* Don't execute autocommands while the window is not properly
4502 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4503 * event. */
4504 block_autocmds();
4505#endif
4506 /*
4507 * link the window in the window list
4508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004510 if (!hidden)
4511 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512#endif
4513#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004514 new_wp->w_wincol = 0;
4515 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516#endif
4517
Bram Moolenaar429fa852013-04-15 12:27:36 +02004518 /* position the display and the cursor at the top of the file. */
4519 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004521 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004523 new_wp->w_botline = 2;
4524 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004526 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#endif
4528
Bram Moolenaar429fa852013-04-15 12:27:36 +02004529 /* We won't calculate w_fraction until resizing the window */
4530 new_wp->w_fraction = 0;
4531 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004534 if (gui.in_use)
4535 {
4536 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4537 SBAR_LEFT, new_wp);
4538 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4539 SBAR_RIGHT, new_wp);
4540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541#endif
4542#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004543 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004545#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004546 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004547#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004548#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004549 new_wp->w_match_head = NULL;
4550 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004551#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004552 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553}
4554
4555#if defined(FEAT_WINDOWS) || defined(PROTO)
4556
4557/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004558 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 */
4560 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004561win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004563 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564{
4565 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004566 buf_T *buf;
4567 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004569#ifdef FEAT_FOLDING
4570 clearFolding(wp);
4571#endif
4572
4573 /* reduce the reference count to the argument list. */
4574 alist_unlink(wp->w_alist);
4575
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004576#ifdef FEAT_AUTOCMD
4577 /* Don't execute autocommands while the window is halfway being deleted.
4578 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004579 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004580#endif
4581
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004582#ifdef FEAT_LUA
4583 lua_window_free(wp);
4584#endif
4585
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004586#ifdef FEAT_MZSCHEME
4587 mzscheme_window_free(wp);
4588#endif
4589
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590#ifdef FEAT_PERL
4591 perl_win_free(wp);
4592#endif
4593
4594#ifdef FEAT_PYTHON
4595 python_window_free(wp);
4596#endif
4597
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004598#ifdef FEAT_PYTHON3
4599 python3_window_free(wp);
4600#endif
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#ifdef FEAT_TCL
4603 tcl_window_free(wp);
4604#endif
4605
4606#ifdef FEAT_RUBY
4607 ruby_window_free(wp);
4608#endif
4609
4610 clear_winopt(&wp->w_onebuf_opt);
4611 clear_winopt(&wp->w_allbuf_opt);
4612
4613#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004614 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4615 hash_init(&wp->w_vars->dv_hashtab);
4616 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617#endif
4618
4619 if (prevwin == wp)
4620 prevwin = NULL;
4621 win_free_lsize(wp);
4622
4623 for (i = 0; i < wp->w_tagstacklen; ++i)
4624 vim_free(wp->w_tagstack[i].tagname);
4625
4626 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004627
Bram Moolenaarff18df02013-07-24 17:51:57 +02004628 /* Remove the window from the b_wininfo lists, it may happen that the
4629 * freed memory is re-used for another window. */
4630 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4631 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4632 if (wip->wi_win == wp)
4633 wip->wi_win = NULL;
4634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004636 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639#ifdef FEAT_JUMPLIST
4640 free_jumplist(wp);
4641#endif
4642
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004643#ifdef FEAT_QUICKFIX
4644 qf_free_all(wp);
4645#endif
4646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#ifdef FEAT_GUI
4648 if (gui.in_use)
4649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4651 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4652 }
4653#endif /* FEAT_GUI */
4654
Bram Moolenaar860cae12010-06-05 23:22:07 +02004655#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004656 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004657#endif
4658
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004659#ifdef FEAT_AUTOCMD
4660 if (wp != aucmd_win)
4661#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004662 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004663#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004664 if (autocmd_busy)
4665 {
4666 wp->w_next = au_pending_free_win;
4667 au_pending_free_win = wp;
4668 }
4669 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004670#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004671 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004672
4673#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004674 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676}
4677
4678/*
4679 * Append window "wp" in the window list after window "after".
4680 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004681 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682win_append(after, wp)
4683 win_T *after, *wp;
4684{
4685 win_T *before;
4686
4687 if (after == NULL) /* after NULL is in front of the first */
4688 before = firstwin;
4689 else
4690 before = after->w_next;
4691
4692 wp->w_next = before;
4693 wp->w_prev = after;
4694 if (after == NULL)
4695 firstwin = wp;
4696 else
4697 after->w_next = wp;
4698 if (before == NULL)
4699 lastwin = wp;
4700 else
4701 before->w_prev = wp;
4702}
4703
4704/*
4705 * Remove a window from the window list.
4706 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004707 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004708win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004710 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 if (wp->w_prev != NULL)
4713 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004714 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004716 else
4717 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (wp->w_next != NULL)
4719 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004720 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004722 else
4723 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724}
4725
4726/*
4727 * Append frame "frp" in a frame list after frame "after".
4728 */
4729 static void
4730frame_append(after, frp)
4731 frame_T *after, *frp;
4732{
4733 frp->fr_next = after->fr_next;
4734 after->fr_next = frp;
4735 if (frp->fr_next != NULL)
4736 frp->fr_next->fr_prev = frp;
4737 frp->fr_prev = after;
4738}
4739
4740/*
4741 * Insert frame "frp" in a frame list before frame "before".
4742 */
4743 static void
4744frame_insert(before, frp)
4745 frame_T *before, *frp;
4746{
4747 frp->fr_next = before;
4748 frp->fr_prev = before->fr_prev;
4749 before->fr_prev = frp;
4750 if (frp->fr_prev != NULL)
4751 frp->fr_prev->fr_next = frp;
4752 else
4753 frp->fr_parent->fr_child = frp;
4754}
4755
4756/*
4757 * Remove a frame from a frame list.
4758 */
4759 static void
4760frame_remove(frp)
4761 frame_T *frp;
4762{
4763 if (frp->fr_prev != NULL)
4764 frp->fr_prev->fr_next = frp->fr_next;
4765 else
4766 frp->fr_parent->fr_child = frp->fr_next;
4767 if (frp->fr_next != NULL)
4768 frp->fr_next->fr_prev = frp->fr_prev;
4769}
4770
4771#endif /* FEAT_WINDOWS */
4772
4773/*
4774 * Allocate w_lines[] for window "wp".
4775 * Return FAIL for failure, OK for success.
4776 */
4777 int
4778win_alloc_lines(wp)
4779 win_T *wp;
4780{
4781 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004782 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (wp->w_lines == NULL)
4784 return FAIL;
4785 return OK;
4786}
4787
4788/*
4789 * free lsize arrays for a window
4790 */
4791 void
4792win_free_lsize(wp)
4793 win_T *wp;
4794{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004795 /* TODO: why would wp be NULL here? */
4796 if (wp != NULL)
4797 {
4798 vim_free(wp->w_lines);
4799 wp->w_lines = NULL;
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801}
4802
4803/*
4804 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004805 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
4807 void
4808shell_new_rows()
4809{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004810 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 if (firstwin == NULL) /* not initialized yet */
4813 return;
4814#ifdef FEAT_WINDOWS
4815 if (h < frame_minheight(topframe, NULL))
4816 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004817
4818 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 * that doesn't result in the right height, forget about that option. */
4820 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004821 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 frame_new_height(topframe, h, FALSE, FALSE);
4823
4824 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4825#else
4826 if (h < 1)
4827 h = 1;
4828 win_new_height(firstwin, h);
4829#endif
4830 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004831#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004832 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004833#endif
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835#if 0
4836 /* Disabled: don't want making the screen smaller make a window larger. */
4837 if (p_ea)
4838 win_equal(curwin, FALSE, 'v');
4839#endif
4840}
4841
4842#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4843/*
4844 * Called from win_new_shellsize() after Columns changed.
4845 */
4846 void
4847shell_new_columns()
4848{
4849 if (firstwin == NULL) /* not initialized yet */
4850 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004851
4852 /* First try setting the widths of windows with 'winfixwidth'. If that
4853 * doesn't result in the right width, forget about that option. */
4854 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004855 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004856 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4857
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4859#if 0
4860 /* Disabled: don't want making the screen smaller make a window larger. */
4861 if (p_ea)
4862 win_equal(curwin, FALSE, 'h');
4863#endif
4864}
4865#endif
4866
4867#if defined(FEAT_CMDWIN) || defined(PROTO)
4868/*
4869 * Save the size of all windows in "gap".
4870 */
4871 void
4872win_size_save(gap)
4873 garray_T *gap;
4874
4875{
4876 win_T *wp;
4877
4878 ga_init2(gap, (int)sizeof(int), 1);
4879 if (ga_grow(gap, win_count() * 2) == OK)
4880 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4881 {
4882 ((int *)gap->ga_data)[gap->ga_len++] =
4883 wp->w_width + wp->w_vsep_width;
4884 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4885 }
4886}
4887
4888/*
4889 * Restore window sizes, but only if the number of windows is still the same.
4890 * Does not free the growarray.
4891 */
4892 void
4893win_size_restore(gap)
4894 garray_T *gap;
4895{
4896 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004897 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
4899 if (win_count() * 2 == gap->ga_len)
4900 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004901 /* The order matters, because frames contain other frames, but it's
4902 * difficult to get right. The easy way out is to do it twice. */
4903 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004905 i = 0;
4906 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4907 {
4908 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4909 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 }
4912 /* recompute the window positions */
4913 (void)win_comp_pos();
4914 }
4915}
4916#endif /* FEAT_CMDWIN */
4917
4918#if defined(FEAT_WINDOWS) || defined(PROTO)
4919/*
4920 * Update the position for all windows, using the width and height of the
4921 * frames.
4922 * Returns the row just after the last window.
4923 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004924 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925win_comp_pos()
4926{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004927 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 int col = 0;
4929
4930 frame_comp_pos(topframe, &row, &col);
4931 return row;
4932}
4933
4934/*
4935 * Update the position of the windows in frame "topfrp", using the width and
4936 * height of the frames.
4937 * "*row" and "*col" are the top-left position of the frame. They are updated
4938 * to the bottom-right position plus one.
4939 */
4940 static void
4941frame_comp_pos(topfrp, row, col)
4942 frame_T *topfrp;
4943 int *row;
4944 int *col;
4945{
4946 win_T *wp;
4947 frame_T *frp;
4948#ifdef FEAT_VERTSPLIT
4949 int startcol;
4950 int startrow;
4951#endif
4952
4953 wp = topfrp->fr_win;
4954 if (wp != NULL)
4955 {
4956 if (wp->w_winrow != *row
4957#ifdef FEAT_VERTSPLIT
4958 || wp->w_wincol != *col
4959#endif
4960 )
4961 {
4962 /* position changed, redraw */
4963 wp->w_winrow = *row;
4964#ifdef FEAT_VERTSPLIT
4965 wp->w_wincol = *col;
4966#endif
4967 redraw_win_later(wp, NOT_VALID);
4968 wp->w_redr_status = TRUE;
4969 }
4970 *row += wp->w_height + wp->w_status_height;
4971#ifdef FEAT_VERTSPLIT
4972 *col += wp->w_width + wp->w_vsep_width;
4973#endif
4974 }
4975 else
4976 {
4977#ifdef FEAT_VERTSPLIT
4978 startrow = *row;
4979 startcol = *col;
4980#endif
4981 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4982 {
4983#ifdef FEAT_VERTSPLIT
4984 if (topfrp->fr_layout == FR_ROW)
4985 *row = startrow; /* all frames are at the same row */
4986 else
4987 *col = startcol; /* all frames are at the same col */
4988#endif
4989 frame_comp_pos(frp, row, col);
4990 }
4991 }
4992}
4993
4994#endif /* FEAT_WINDOWS */
4995
4996/*
4997 * Set current window height and take care of repositioning other windows to
4998 * fit around it.
4999 */
5000 void
5001win_setheight(height)
5002 int height;
5003{
5004 win_setheight_win(height, curwin);
5005}
5006
5007/*
5008 * Set the window height of window "win" and take care of repositioning other
5009 * windows to fit around it.
5010 */
5011 void
5012win_setheight_win(height, win)
5013 int height;
5014 win_T *win;
5015{
5016 int row;
5017
5018 if (win == curwin)
5019 {
5020 /* Always keep current window at least one line high, even when
5021 * 'winminheight' is zero. */
5022#ifdef FEAT_WINDOWS
5023 if (height < p_wmh)
5024 height = p_wmh;
5025#endif
5026 if (height == 0)
5027 height = 1;
5028 }
5029
5030#ifdef FEAT_WINDOWS
5031 frame_setheight(win->w_frame, height + win->w_status_height);
5032
5033 /* recompute the window positions */
5034 row = win_comp_pos();
5035#else
5036 if (height > topframe->fr_height)
5037 height = topframe->fr_height;
5038 win->w_height = height;
5039 row = height;
5040#endif
5041
5042 /*
5043 * If there is extra space created between the last window and the command
5044 * line, clear it.
5045 */
5046 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5047 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5048 cmdline_row = row;
5049 msg_row = row;
5050 msg_col = 0;
5051
5052 redraw_all_later(NOT_VALID);
5053}
5054
5055#if defined(FEAT_WINDOWS) || defined(PROTO)
5056
5057/*
5058 * Set the height of a frame to "height" and take care that all frames and
5059 * windows inside it are resized. Also resize frames on the left and right if
5060 * the are in the same FR_ROW frame.
5061 *
5062 * Strategy:
5063 * If the frame is part of a FR_COL frame, try fitting the frame in that
5064 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5065 * go to containing frames to resize them and make room.
5066 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5067 * Check for the minimal height of the FR_ROW frame.
5068 * At the top level we can also use change the command line height.
5069 */
5070 static void
5071frame_setheight(curfrp, height)
5072 frame_T *curfrp;
5073 int height;
5074{
5075 int room; /* total number of lines available */
5076 int take; /* number of lines taken from other windows */
5077 int room_cmdline; /* lines available from cmdline */
5078 int run;
5079 frame_T *frp;
5080 int h;
5081 int room_reserved;
5082
5083 /* If the height already is the desired value, nothing to do. */
5084 if (curfrp->fr_height == height)
5085 return;
5086
5087 if (curfrp->fr_parent == NULL)
5088 {
5089 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005090 if (height > ROWS_AVAIL)
5091 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 if (height > 0)
5093 frame_new_height(curfrp, height, FALSE, FALSE);
5094 }
5095 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5096 {
5097 /* Row of frames: Also need to resize frames left and right of this
5098 * one. First check for the minimal height of these. */
5099 h = frame_minheight(curfrp->fr_parent, NULL);
5100 if (height < h)
5101 height = h;
5102 frame_setheight(curfrp->fr_parent, height);
5103 }
5104 else
5105 {
5106 /*
5107 * Column of frames: try to change only frames in this column.
5108 */
5109#ifdef FEAT_VERTSPLIT
5110 /*
5111 * Do this twice:
5112 * 1: compute room available, if it's not enough try resizing the
5113 * containing frame.
5114 * 2: compute the room available and adjust the height to it.
5115 * Try not to reduce the height of a window with 'winfixheight' set.
5116 */
5117 for (run = 1; run <= 2; ++run)
5118#else
5119 for (;;)
5120#endif
5121 {
5122 room = 0;
5123 room_reserved = 0;
5124 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5125 frp = frp->fr_next)
5126 {
5127 if (frp != curfrp
5128 && frp->fr_win != NULL
5129 && frp->fr_win->w_p_wfh)
5130 room_reserved += frp->fr_height;
5131 room += frp->fr_height;
5132 if (frp != curfrp)
5133 room -= frame_minheight(frp, NULL);
5134 }
5135#ifdef FEAT_VERTSPLIT
5136 if (curfrp->fr_width != Columns)
5137 room_cmdline = 0;
5138 else
5139#endif
5140 {
5141 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5142 + lastwin->w_height + lastwin->w_status_height);
5143 if (room_cmdline < 0)
5144 room_cmdline = 0;
5145 }
5146
5147 if (height <= room + room_cmdline)
5148 break;
5149#ifdef FEAT_VERTSPLIT
5150 if (run == 2 || curfrp->fr_width == Columns)
5151#endif
5152 {
5153 if (height > room + room_cmdline)
5154 height = room + room_cmdline;
5155 break;
5156 }
5157#ifdef FEAT_VERTSPLIT
5158 frame_setheight(curfrp->fr_parent, height
5159 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5160#endif
5161 /*NOTREACHED*/
5162 }
5163
5164 /*
5165 * Compute the number of lines we will take from others frames (can be
5166 * negative!).
5167 */
5168 take = height - curfrp->fr_height;
5169
5170 /* If there is not enough room, also reduce the height of a window
5171 * with 'winfixheight' set. */
5172 if (height > room + room_cmdline - room_reserved)
5173 room_reserved = room + room_cmdline - height;
5174 /* If there is only a 'winfixheight' window and making the
5175 * window smaller, need to make the other window taller. */
5176 if (take < 0 && room - curfrp->fr_height < room_reserved)
5177 room_reserved = 0;
5178
5179 if (take > 0 && room_cmdline > 0)
5180 {
5181 /* use lines from cmdline first */
5182 if (take < room_cmdline)
5183 room_cmdline = take;
5184 take -= room_cmdline;
5185 topframe->fr_height += room_cmdline;
5186 }
5187
5188 /*
5189 * set the current frame to the new height
5190 */
5191 frame_new_height(curfrp, height, FALSE, FALSE);
5192
5193 /*
5194 * First take lines from the frames after the current frame. If
5195 * that is not enough, takes lines from frames above the current
5196 * frame.
5197 */
5198 for (run = 0; run < 2; ++run)
5199 {
5200 if (run == 0)
5201 frp = curfrp->fr_next; /* 1st run: start with next window */
5202 else
5203 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5204 while (frp != NULL && take != 0)
5205 {
5206 h = frame_minheight(frp, NULL);
5207 if (room_reserved > 0
5208 && frp->fr_win != NULL
5209 && frp->fr_win->w_p_wfh)
5210 {
5211 if (room_reserved >= frp->fr_height)
5212 room_reserved -= frp->fr_height;
5213 else
5214 {
5215 if (frp->fr_height - room_reserved > take)
5216 room_reserved = frp->fr_height - take;
5217 take -= frp->fr_height - room_reserved;
5218 frame_new_height(frp, room_reserved, FALSE, FALSE);
5219 room_reserved = 0;
5220 }
5221 }
5222 else
5223 {
5224 if (frp->fr_height - take < h)
5225 {
5226 take -= frp->fr_height - h;
5227 frame_new_height(frp, h, FALSE, FALSE);
5228 }
5229 else
5230 {
5231 frame_new_height(frp, frp->fr_height - take,
5232 FALSE, FALSE);
5233 take = 0;
5234 }
5235 }
5236 if (run == 0)
5237 frp = frp->fr_next;
5238 else
5239 frp = frp->fr_prev;
5240 }
5241 }
5242 }
5243}
5244
5245#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5246/*
5247 * Set current window width and take care of repositioning other windows to
5248 * fit around it.
5249 */
5250 void
5251win_setwidth(width)
5252 int width;
5253{
5254 win_setwidth_win(width, curwin);
5255}
5256
5257 void
5258win_setwidth_win(width, wp)
5259 int width;
5260 win_T *wp;
5261{
5262 /* Always keep current window at least one column wide, even when
5263 * 'winminwidth' is zero. */
5264 if (wp == curwin)
5265 {
5266 if (width < p_wmw)
5267 width = p_wmw;
5268 if (width == 0)
5269 width = 1;
5270 }
5271
5272 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5273
5274 /* recompute the window positions */
5275 (void)win_comp_pos();
5276
5277 redraw_all_later(NOT_VALID);
5278}
5279
5280/*
5281 * Set the width of a frame to "width" and take care that all frames and
5282 * windows inside it are resized. Also resize frames above and below if the
5283 * are in the same FR_ROW frame.
5284 *
5285 * Strategy is similar to frame_setheight().
5286 */
5287 static void
5288frame_setwidth(curfrp, width)
5289 frame_T *curfrp;
5290 int width;
5291{
5292 int room; /* total number of lines available */
5293 int take; /* number of lines taken from other windows */
5294 int run;
5295 frame_T *frp;
5296 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005297 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299 /* If the width already is the desired value, nothing to do. */
5300 if (curfrp->fr_width == width)
5301 return;
5302
5303 if (curfrp->fr_parent == NULL)
5304 /* topframe: can't change width */
5305 return;
5306
5307 if (curfrp->fr_parent->fr_layout == FR_COL)
5308 {
5309 /* Column of frames: Also need to resize frames above and below of
5310 * this one. First check for the minimal width of these. */
5311 w = frame_minwidth(curfrp->fr_parent, NULL);
5312 if (width < w)
5313 width = w;
5314 frame_setwidth(curfrp->fr_parent, width);
5315 }
5316 else
5317 {
5318 /*
5319 * Row of frames: try to change only frames in this row.
5320 *
5321 * Do this twice:
5322 * 1: compute room available, if it's not enough try resizing the
5323 * containing frame.
5324 * 2: compute the room available and adjust the width to it.
5325 */
5326 for (run = 1; run <= 2; ++run)
5327 {
5328 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005329 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5331 frp = frp->fr_next)
5332 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005333 if (frp != curfrp
5334 && frp->fr_win != NULL
5335 && frp->fr_win->w_p_wfw)
5336 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 room += frp->fr_width;
5338 if (frp != curfrp)
5339 room -= frame_minwidth(frp, NULL);
5340 }
5341
5342 if (width <= room)
5343 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005344 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
5346 if (width > room)
5347 width = room;
5348 break;
5349 }
5350 frame_setwidth(curfrp->fr_parent, width
5351 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5352 }
5353
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 /*
5355 * Compute the number of lines we will take from others frames (can be
5356 * negative!).
5357 */
5358 take = width - curfrp->fr_width;
5359
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005360 /* If there is not enough room, also reduce the width of a window
5361 * with 'winfixwidth' set. */
5362 if (width > room - room_reserved)
5363 room_reserved = room - width;
5364 /* If there is only a 'winfixwidth' window and making the
5365 * window smaller, need to make the other window narrower. */
5366 if (take < 0 && room - curfrp->fr_width < room_reserved)
5367 room_reserved = 0;
5368
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 /*
5370 * set the current frame to the new width
5371 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005372 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
5374 /*
5375 * First take lines from the frames right of the current frame. If
5376 * that is not enough, takes lines from frames left of the current
5377 * frame.
5378 */
5379 for (run = 0; run < 2; ++run)
5380 {
5381 if (run == 0)
5382 frp = curfrp->fr_next; /* 1st run: start with next window */
5383 else
5384 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5385 while (frp != NULL && take != 0)
5386 {
5387 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005388 if (room_reserved > 0
5389 && frp->fr_win != NULL
5390 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005392 if (room_reserved >= frp->fr_width)
5393 room_reserved -= frp->fr_width;
5394 else
5395 {
5396 if (frp->fr_width - room_reserved > take)
5397 room_reserved = frp->fr_width - take;
5398 take -= frp->fr_width - room_reserved;
5399 frame_new_width(frp, room_reserved, FALSE, FALSE);
5400 room_reserved = 0;
5401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 }
5403 else
5404 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005405 if (frp->fr_width - take < w)
5406 {
5407 take -= frp->fr_width - w;
5408 frame_new_width(frp, w, FALSE, FALSE);
5409 }
5410 else
5411 {
5412 frame_new_width(frp, frp->fr_width - take,
5413 FALSE, FALSE);
5414 take = 0;
5415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 }
5417 if (run == 0)
5418 frp = frp->fr_next;
5419 else
5420 frp = frp->fr_prev;
5421 }
5422 }
5423 }
5424}
5425#endif /* FEAT_VERTSPLIT */
5426
5427/*
5428 * Check 'winminheight' for a valid value.
5429 */
5430 void
5431win_setminheight()
5432{
5433 int room;
5434 int first = TRUE;
5435 win_T *wp;
5436
5437 /* loop until there is a 'winminheight' that is possible */
5438 while (p_wmh > 0)
5439 {
5440 /* TODO: handle vertical splits */
5441 room = -p_wh;
5442 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5443 room += wp->w_height - p_wmh;
5444 if (room >= 0)
5445 break;
5446 --p_wmh;
5447 if (first)
5448 {
5449 EMSG(_(e_noroom));
5450 first = FALSE;
5451 }
5452 }
5453}
5454
5455#ifdef FEAT_MOUSE
5456
5457/*
5458 * Status line of dragwin is dragged "offset" lines down (negative is up).
5459 */
5460 void
5461win_drag_status_line(dragwin, offset)
5462 win_T *dragwin;
5463 int offset;
5464{
5465 frame_T *curfr;
5466 frame_T *fr;
5467 int room;
5468 int row;
5469 int up; /* if TRUE, drag status line up, otherwise down */
5470 int n;
5471
5472 fr = dragwin->w_frame;
5473 curfr = fr;
5474 if (fr != topframe) /* more than one window */
5475 {
5476 fr = fr->fr_parent;
5477 /* When the parent frame is not a column of frames, its parent should
5478 * be. */
5479 if (fr->fr_layout != FR_COL)
5480 {
5481 curfr = fr;
5482 if (fr != topframe) /* only a row of windows, may drag statusline */
5483 fr = fr->fr_parent;
5484 }
5485 }
5486
5487 /* If this is the last frame in a column, may want to resize the parent
5488 * frame instead (go two up to skip a row of frames). */
5489 while (curfr != topframe && curfr->fr_next == NULL)
5490 {
5491 if (fr != topframe)
5492 fr = fr->fr_parent;
5493 curfr = fr;
5494 if (fr != topframe)
5495 fr = fr->fr_parent;
5496 }
5497
5498 if (offset < 0) /* drag up */
5499 {
5500 up = TRUE;
5501 offset = -offset;
5502 /* sum up the room of the current frame and above it */
5503 if (fr == curfr)
5504 {
5505 /* only one window */
5506 room = fr->fr_height - frame_minheight(fr, NULL);
5507 }
5508 else
5509 {
5510 room = 0;
5511 for (fr = fr->fr_child; ; fr = fr->fr_next)
5512 {
5513 room += fr->fr_height - frame_minheight(fr, NULL);
5514 if (fr == curfr)
5515 break;
5516 }
5517 }
5518 fr = curfr->fr_next; /* put fr at frame that grows */
5519 }
5520 else /* drag down */
5521 {
5522 up = FALSE;
5523 /*
5524 * Only dragging the last status line can reduce p_ch.
5525 */
5526 room = Rows - cmdline_row;
5527 if (curfr->fr_next == NULL)
5528 room -= 1;
5529 else
5530 room -= p_ch;
5531 if (room < 0)
5532 room = 0;
5533 /* sum up the room of frames below of the current one */
5534 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5535 room += fr->fr_height - frame_minheight(fr, NULL);
5536 fr = curfr; /* put fr at window that grows */
5537 }
5538
5539 if (room < offset) /* Not enough room */
5540 offset = room; /* Move as far as we can */
5541 if (offset <= 0)
5542 return;
5543
5544 /*
5545 * Grow frame fr by "offset" lines.
5546 * Doesn't happen when dragging the last status line up.
5547 */
5548 if (fr != NULL)
5549 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5550
5551 if (up)
5552 fr = curfr; /* current frame gets smaller */
5553 else
5554 fr = curfr->fr_next; /* next frame gets smaller */
5555
5556 /*
5557 * Now make the other frames smaller.
5558 */
5559 while (fr != NULL && offset > 0)
5560 {
5561 n = frame_minheight(fr, NULL);
5562 if (fr->fr_height - offset <= n)
5563 {
5564 offset -= fr->fr_height - n;
5565 frame_new_height(fr, n, !up, FALSE);
5566 }
5567 else
5568 {
5569 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5570 break;
5571 }
5572 if (up)
5573 fr = fr->fr_prev;
5574 else
5575 fr = fr->fr_next;
5576 }
5577 row = win_comp_pos();
5578 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5579 cmdline_row = row;
5580 p_ch = Rows - cmdline_row;
5581 if (p_ch < 1)
5582 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005583 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005584 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 showmode();
5586}
5587
5588#ifdef FEAT_VERTSPLIT
5589/*
5590 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5591 */
5592 void
5593win_drag_vsep_line(dragwin, offset)
5594 win_T *dragwin;
5595 int offset;
5596{
5597 frame_T *curfr;
5598 frame_T *fr;
5599 int room;
5600 int left; /* if TRUE, drag separator line left, otherwise right */
5601 int n;
5602
5603 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005604 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 return;
5606 curfr = fr;
5607 fr = fr->fr_parent;
5608 /* When the parent frame is not a row of frames, its parent should be. */
5609 if (fr->fr_layout != FR_ROW)
5610 {
5611 if (fr == topframe) /* only a column of windows (cannot happen?) */
5612 return;
5613 curfr = fr;
5614 fr = fr->fr_parent;
5615 }
5616
5617 /* If this is the last frame in a row, may want to resize a parent
5618 * frame instead. */
5619 while (curfr->fr_next == NULL)
5620 {
5621 if (fr == topframe)
5622 break;
5623 curfr = fr;
5624 fr = fr->fr_parent;
5625 if (fr != topframe)
5626 {
5627 curfr = fr;
5628 fr = fr->fr_parent;
5629 }
5630 }
5631
5632 if (offset < 0) /* drag left */
5633 {
5634 left = TRUE;
5635 offset = -offset;
5636 /* sum up the room of the current frame and left of it */
5637 room = 0;
5638 for (fr = fr->fr_child; ; fr = fr->fr_next)
5639 {
5640 room += fr->fr_width - frame_minwidth(fr, NULL);
5641 if (fr == curfr)
5642 break;
5643 }
5644 fr = curfr->fr_next; /* put fr at frame that grows */
5645 }
5646 else /* drag right */
5647 {
5648 left = FALSE;
5649 /* sum up the room of frames right of the current one */
5650 room = 0;
5651 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5652 room += fr->fr_width - frame_minwidth(fr, NULL);
5653 fr = curfr; /* put fr at window that grows */
5654 }
5655
5656 if (room < offset) /* Not enough room */
5657 offset = room; /* Move as far as we can */
5658 if (offset <= 0) /* No room at all, quit. */
5659 return;
5660
5661 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005662 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664 /* shrink other frames: current and at the left or at the right */
5665 if (left)
5666 fr = curfr; /* current frame gets smaller */
5667 else
5668 fr = curfr->fr_next; /* next frame gets smaller */
5669
5670 while (fr != NULL && offset > 0)
5671 {
5672 n = frame_minwidth(fr, NULL);
5673 if (fr->fr_width - offset <= n)
5674 {
5675 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005676 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678 else
5679 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005680 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 break;
5682 }
5683 if (left)
5684 fr = fr->fr_prev;
5685 else
5686 fr = fr->fr_next;
5687 }
5688 (void)win_comp_pos();
5689 redraw_all_later(NOT_VALID);
5690}
5691#endif /* FEAT_VERTSPLIT */
5692#endif /* FEAT_MOUSE */
5693
5694#endif /* FEAT_WINDOWS */
5695
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005696#define FRACTION_MULT 16384L
5697
5698/*
5699 * Set wp->w_fraction for the current w_wrow and w_height.
5700 */
5701 static void
5702set_fraction(wp)
5703 win_T *wp;
5704{
5705 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005706 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005707}
5708
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709/*
5710 * Set the height of a window.
5711 * This takes care of the things inside the window, not what happens to the
5712 * window position, the frame or to other windows.
5713 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005714 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715win_new_height(wp, height)
5716 win_T *wp;
5717 int height;
5718{
5719 linenr_T lnum;
5720 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005721 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
5723 /* Don't want a negative height. Happens when splitting a tiny window.
5724 * Will equalize heights soon to fix it. */
5725 if (height < 0)
5726 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005727 if (wp->w_height == height)
5728 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005730 if (wp->w_height > 0)
5731 {
5732 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005733 /* w_wrow needs to be valid. When setting 'laststatus' this may
5734 * call win_new_height() recursively. */
5735 validate_cursor();
5736 if (wp->w_height != prev_height)
5737 return; /* Recursive call already changed the size, bail out here
5738 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005739 if (wp->w_wrow != wp->w_prev_fraction_row)
5740 set_fraction(wp);
5741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
5743 wp->w_height = height;
5744 wp->w_skipcol = 0;
5745
5746 /* Don't change w_topline when height is zero. Don't set w_topline when
5747 * 'scrollbind' is set and this isn't the current window. */
5748 if (height > 0
5749#ifdef FEAT_SCROLLBIND
5750 && (!wp->w_p_scb || wp == curwin)
5751#endif
5752 )
5753 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005754 /*
5755 * Find a value for w_topline that shows the cursor at the same
5756 * relative position in the window as before (more or less).
5757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 lnum = wp->w_cursor.lnum;
5759 if (lnum < 1) /* can happen when starting up */
5760 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005761 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5762 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5764 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005765
5766 if (sline >= 0)
5767 {
5768 /* Make sure the whole cursor line is visible, if possible. */
5769 int rows = plines_win(wp, lnum, FALSE);
5770
5771 if (sline > wp->w_height - rows)
5772 {
5773 sline = wp->w_height - rows;
5774 wp->w_wrow -= rows - line_size;
5775 }
5776 }
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 if (sline < 0)
5779 {
5780 /*
5781 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005782 * Make cursor line the first line in the window. If not enough
5783 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 */
5785 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005786 if (wp->w_wrow >= wp->w_height
5787 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5788 {
5789 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5790 --wp->w_wrow;
5791 while (wp->w_wrow >= wp->w_height)
5792 {
5793 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5794 + win_col_off2(wp);
5795 --wp->w_wrow;
5796 }
5797 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005798 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005800 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005802 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 {
5804#ifdef FEAT_FOLDING
5805 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5806 if (lnum == 1)
5807 {
5808 /* first line in buffer is folded */
5809 line_size = 1;
5810 --sline;
5811 break;
5812 }
5813#endif
5814 --lnum;
5815#ifdef FEAT_DIFF
5816 if (lnum == wp->w_topline)
5817 line_size = plines_win_nofill(wp, lnum, TRUE)
5818 + wp->w_topfill;
5819 else
5820#endif
5821 line_size = plines_win(wp, lnum, TRUE);
5822 sline -= line_size;
5823 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (sline < 0)
5826 {
5827 /*
5828 * Line we want at top would go off top of screen. Use next
5829 * line instead.
5830 */
5831#ifdef FEAT_FOLDING
5832 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5833#endif
5834 lnum++;
5835 wp->w_wrow -= line_size + sline;
5836 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005837 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
5839 /* First line of file reached, use that as topline. */
5840 lnum = 1;
5841 wp->w_wrow -= sline;
5842 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005843
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005844 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 }
5847
5848 if (wp == curwin)
5849 {
5850 if (p_so)
5851 update_topline();
5852 curs_columns(FALSE); /* validate w_wrow */
5853 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005854 if (prev_height > 0)
5855 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
5857 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005858 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859#ifdef FEAT_WINDOWS
5860 wp->w_redr_status = TRUE;
5861#endif
5862 invalidate_botline_win(wp);
5863}
5864
5865#ifdef FEAT_VERTSPLIT
5866/*
5867 * Set the width of a window.
5868 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005869 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870win_new_width(wp, width)
5871 win_T *wp;
5872 int width;
5873{
5874 wp->w_width = width;
5875 wp->w_lines_valid = 0;
5876 changed_line_abv_curs_win(wp);
5877 invalidate_botline_win(wp);
5878 if (wp == curwin)
5879 {
5880 update_topline();
5881 curs_columns(TRUE); /* validate w_wrow */
5882 }
5883 redraw_win_later(wp, NOT_VALID);
5884 wp->w_redr_status = TRUE;
5885}
5886#endif
5887
5888 void
5889win_comp_scroll(wp)
5890 win_T *wp;
5891{
5892 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5893 if (wp->w_p_scr == 0)
5894 wp->w_p_scr = 1;
5895}
5896
5897/*
5898 * command_height: called whenever p_ch has been changed
5899 */
5900 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005901command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903#ifdef FEAT_WINDOWS
5904 int h;
5905 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005906 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005908 /* Use the value of p_ch that we remembered. This is needed for when the
5909 * GUI starts up, we can't be sure in what order things happen. And when
5910 * p_ch was changed in another tab page. */
5911 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005912
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 /* Find bottom frame with width of screen. */
5914 frp = lastwin->w_frame;
5915# ifdef FEAT_VERTSPLIT
5916 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5917 frp = frp->fr_parent;
5918# endif
5919
5920 /* Avoid changing the height of a window with 'winfixheight' set. */
5921 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5922 && frp->fr_win->w_p_wfh)
5923 frp = frp->fr_prev;
5924
5925 if (starting != NO_SCREEN)
5926 {
5927 cmdline_row = Rows - p_ch;
5928
5929 if (p_ch > old_p_ch) /* p_ch got bigger */
5930 {
5931 while (p_ch > old_p_ch)
5932 {
5933 if (frp == NULL)
5934 {
5935 EMSG(_(e_noroom));
5936 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005937 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 cmdline_row = Rows - p_ch;
5939 break;
5940 }
5941 h = frp->fr_height - frame_minheight(frp, NULL);
5942 if (h > p_ch - old_p_ch)
5943 h = p_ch - old_p_ch;
5944 old_p_ch += h;
5945 frame_add_height(frp, -h);
5946 frp = frp->fr_prev;
5947 }
5948
5949 /* Recompute window positions. */
5950 (void)win_comp_pos();
5951
5952 /* clear the lines added to cmdline */
5953 if (full_screen)
5954 screen_fill((int)(cmdline_row), (int)Rows, 0,
5955 (int)Columns, ' ', ' ', 0);
5956 msg_row = cmdline_row;
5957 redraw_cmdline = TRUE;
5958 return;
5959 }
5960
5961 if (msg_row < cmdline_row)
5962 msg_row = cmdline_row;
5963 redraw_cmdline = TRUE;
5964 }
5965 frame_add_height(frp, (int)(old_p_ch - p_ch));
5966
5967 /* Recompute window positions. */
5968 if (frp != lastwin->w_frame)
5969 (void)win_comp_pos();
5970#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005972 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973#endif
5974}
5975
5976#if defined(FEAT_WINDOWS) || defined(PROTO)
5977/*
5978 * Resize frame "frp" to be "n" lines higher (negative for less high).
5979 * Also resize the frames it is contained in.
5980 */
5981 static void
5982frame_add_height(frp, n)
5983 frame_T *frp;
5984 int n;
5985{
5986 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5987 for (;;)
5988 {
5989 frp = frp->fr_parent;
5990 if (frp == NULL)
5991 break;
5992 frp->fr_height += n;
5993 }
5994}
5995
5996/*
5997 * Add or remove a status line for the bottom window(s), according to the
5998 * value of 'laststatus'.
5999 */
6000 void
6001last_status(morewin)
6002 int morewin; /* pretend there are two or more windows */
6003{
6004 /* Don't make a difference between horizontal or vertical split. */
6005 last_status_rec(topframe, (p_ls == 2
6006 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6007}
6008
6009 static void
6010last_status_rec(fr, statusline)
6011 frame_T *fr;
6012 int statusline;
6013{
6014 frame_T *fp;
6015 win_T *wp;
6016
6017 if (fr->fr_layout == FR_LEAF)
6018 {
6019 wp = fr->fr_win;
6020 if (wp->w_status_height != 0 && !statusline)
6021 {
6022 /* remove status line */
6023 win_new_height(wp, wp->w_height + 1);
6024 wp->w_status_height = 0;
6025 comp_col();
6026 }
6027 else if (wp->w_status_height == 0 && statusline)
6028 {
6029 /* Find a frame to take a line from. */
6030 fp = fr;
6031 while (fp->fr_height <= frame_minheight(fp, NULL))
6032 {
6033 if (fp == topframe)
6034 {
6035 EMSG(_(e_noroom));
6036 return;
6037 }
6038 /* In a column of frames: go to frame above. If already at
6039 * the top or in a row of frames: go to parent. */
6040 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6041 fp = fp->fr_prev;
6042 else
6043 fp = fp->fr_parent;
6044 }
6045 wp->w_status_height = 1;
6046 if (fp != fr)
6047 {
6048 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6049 frame_fix_height(wp);
6050 (void)win_comp_pos();
6051 }
6052 else
6053 win_new_height(wp, wp->w_height - 1);
6054 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006055 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 }
6057 }
6058#ifdef FEAT_VERTSPLIT
6059 else if (fr->fr_layout == FR_ROW)
6060 {
6061 /* vertically split windows, set status line for each one */
6062 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6063 last_status_rec(fp, statusline);
6064 }
6065#endif
6066 else
6067 {
6068 /* horizontally split window, set status line for last one */
6069 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6070 ;
6071 last_status_rec(fp, statusline);
6072 }
6073}
6074
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006075/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006076 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006077 */
6078 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006079tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006080{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006081#ifdef FEAT_GUI_TABLINE
6082 /* When the GUI has the tabline then this always returns zero. */
6083 if (gui_use_tabline())
6084 return 0;
6085#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006086 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006087 {
6088 case 0: return 0;
6089 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6090 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006091 return 1;
6092}
6093
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#endif /* FEAT_WINDOWS */
6095
6096#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6097/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006098 * Get the file name at the cursor.
6099 * If Visual mode is active, use the selected text if it's in one line.
6100 * Returns the name in allocated memory, NULL for failure.
6101 */
6102 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006103grab_file_name(count, file_lnum)
6104 long count;
6105 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006106{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006107 if (VIsual_active)
6108 {
6109 int len;
6110 char_u *ptr;
6111
6112 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6113 return NULL;
6114 return find_file_name_in_path(ptr, len,
6115 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6116 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006117 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6118 file_lnum);
6119
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006120}
6121
6122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 * Return the file name under or after the cursor.
6124 *
6125 * The 'path' option is searched if the file name is not absolute.
6126 * The string returned has been alloc'ed and should be freed by the caller.
6127 * NULL is returned if the file name or file is not found.
6128 *
6129 * options:
6130 * FNAME_MESS give error messages
6131 * FNAME_EXP expand to path
6132 * FNAME_HYP check for hypertext link
6133 * FNAME_INCL apply "includeexpr"
6134 */
6135 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006136file_name_at_cursor(options, count, file_lnum)
6137 int options;
6138 long count;
6139 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
6141 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006142 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6143 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144}
6145
6146/*
6147 * Return the name of the file under or after ptr[col].
6148 * Otherwise like file_name_at_cursor().
6149 */
6150 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006151file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 char_u *line;
6153 int col;
6154 int options;
6155 long count;
6156 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006157 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 char_u *ptr;
6160 int len;
6161
6162 /*
6163 * search forward for what could be the start of a file name
6164 */
6165 ptr = line + col;
6166 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006167 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (*ptr == NUL) /* nothing found */
6169 {
6170 if (options & FNAME_MESS)
6171 EMSG(_("E446: No file name under cursor"));
6172 return NULL;
6173 }
6174
6175 /*
6176 * Search backward for first char of the file name.
6177 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6178 */
6179 while (ptr > line)
6180 {
6181#ifdef FEAT_MBYTE
6182 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6183 ptr -= len + 1;
6184 else
6185#endif
6186 if (vim_isfilec(ptr[-1])
6187 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6188 --ptr;
6189 else
6190 break;
6191 }
6192
6193 /*
6194 * Search forward for the last char of the file name.
6195 * Also allow "://" when ':' is not in 'isfname'.
6196 */
6197 len = 0;
6198 while (vim_isfilec(ptr[len])
6199 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6200#ifdef FEAT_MBYTE
6201 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006202 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 else
6204#endif
6205 ++len;
6206
6207 /*
6208 * If there is trailing punctuation, remove it.
6209 * But don't remove "..", could be a directory name.
6210 */
6211 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6212 && ptr[len - 2] != '.')
6213 --len;
6214
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006215 if (file_lnum != NULL)
6216 {
6217 char_u *p;
6218
6219 /* Get the number after the file name and a separator character */
6220 p = ptr + len;
6221 p = skipwhite(p);
6222 if (*p != NUL)
6223 {
6224 if (!isdigit(*p))
6225 ++p; /* skip the separator */
6226 p = skipwhite(p);
6227 if (isdigit(*p))
6228 *file_lnum = (int)getdigits(&p);
6229 }
6230 }
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6233}
6234
6235# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6236static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6237
6238 static char_u *
6239eval_includeexpr(ptr, len)
6240 char_u *ptr;
6241 int len;
6242{
6243 char_u *res;
6244
6245 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006246 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006247 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 set_vim_var_string(VV_FNAME, NULL, 0);
6249 return res;
6250}
6251#endif
6252
6253/*
6254 * Return the name of the file ptr[len] in 'path'.
6255 * Otherwise like file_name_at_cursor().
6256 */
6257 char_u *
6258find_file_name_in_path(ptr, len, options, count, rel_fname)
6259 char_u *ptr;
6260 int len;
6261 int options;
6262 long count;
6263 char_u *rel_fname; /* file we are searching relative to */
6264{
6265 char_u *file_name;
6266 int c;
6267# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6268 char_u *tofree = NULL;
6269
6270 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6271 {
6272 tofree = eval_includeexpr(ptr, len);
6273 if (tofree != NULL)
6274 {
6275 ptr = tofree;
6276 len = (int)STRLEN(ptr);
6277 }
6278 }
6279# endif
6280
6281 if (options & FNAME_EXP)
6282 {
6283 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6284 TRUE, rel_fname);
6285
6286# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6287 /*
6288 * If the file could not be found in a normal way, try applying
6289 * 'includeexpr' (unless done already).
6290 */
6291 if (file_name == NULL
6292 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6293 {
6294 tofree = eval_includeexpr(ptr, len);
6295 if (tofree != NULL)
6296 {
6297 ptr = tofree;
6298 len = (int)STRLEN(ptr);
6299 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6300 TRUE, rel_fname);
6301 }
6302 }
6303# endif
6304 if (file_name == NULL && (options & FNAME_MESS))
6305 {
6306 c = ptr[len];
6307 ptr[len] = NUL;
6308 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6309 ptr[len] = c;
6310 }
6311
6312 /* Repeat finding the file "count" times. This matters when it
6313 * appears several times in the path. */
6314 while (file_name != NULL && --count > 0)
6315 {
6316 vim_free(file_name);
6317 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6318 }
6319 }
6320 else
6321 file_name = vim_strnsave(ptr, len);
6322
6323# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6324 vim_free(tofree);
6325# endif
6326
6327 return file_name;
6328}
6329#endif /* FEAT_SEARCHPATH */
6330
6331/*
6332 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6333 * Also check for ":\\", which MS Internet Explorer accepts, return
6334 * URL_BACKSLASH.
6335 */
6336 static int
6337path_is_url(p)
6338 char_u *p;
6339{
6340 if (STRNCMP(p, "://", (size_t)3) == 0)
6341 return URL_SLASH;
6342 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6343 return URL_BACKSLASH;
6344 return 0;
6345}
6346
6347/*
6348 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6349 * Return URL_BACKSLASH for "name:\\".
6350 * Return zero otherwise.
6351 */
6352 int
6353path_with_url(fname)
6354 char_u *fname;
6355{
6356 char_u *p;
6357
6358 for (p = fname; isalpha(*p); ++p)
6359 ;
6360 return path_is_url(p);
6361}
6362
6363/*
6364 * Return TRUE if "name" is a full (absolute) path name or URL.
6365 */
6366 int
6367vim_isAbsName(name)
6368 char_u *name;
6369{
6370 return (path_with_url(name) != 0 || mch_isFullName(name));
6371}
6372
6373/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006374 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 *
6376 * return FAIL for failure, OK otherwise
6377 */
6378 int
6379vim_FullName(fname, buf, len, force)
6380 char_u *fname, *buf;
6381 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006382 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 int retval = OK;
6385 int url;
6386
6387 *buf = NUL;
6388 if (fname == NULL)
6389 return FAIL;
6390
6391 url = path_with_url(fname);
6392 if (!url)
6393 retval = mch_FullName(fname, buf, len, force);
6394 if (url || retval == FAIL)
6395 {
6396 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006397 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6400 slash_adjust(buf);
6401#endif
6402 return retval;
6403}
6404
6405/*
6406 * Return the minimal number of rows that is needed on the screen to display
6407 * the current number of windows.
6408 */
6409 int
6410min_rows()
6411{
6412 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006413#ifdef FEAT_WINDOWS
6414 tabpage_T *tp;
6415 int n;
6416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
6418 if (firstwin == NULL) /* not initialized yet */
6419 return MIN_LINES;
6420
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006422 total = 0;
6423 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6424 {
6425 n = frame_minheight(tp->tp_topframe, NULL);
6426 if (total < n)
6427 total = n;
6428 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006429 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006431 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006433 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 return total;
6435}
6436
6437/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006438 * Return TRUE if there is only one window (in the current tab page), not
6439 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006440 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 */
6442 int
6443only_one_window()
6444{
6445#ifdef FEAT_WINDOWS
6446 int count = 0;
6447 win_T *wp;
6448
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449 /* If there is another tab page there always is another window. */
6450 if (first_tabpage->tp_next != NULL)
6451 return FALSE;
6452
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006454 if (wp->w_buffer != NULL
6455 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456# ifdef FEAT_QUICKFIX
6457 || wp->w_p_pvw
6458# endif
6459 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006460# ifdef FEAT_AUTOCMD
6461 && wp != aucmd_win
6462# endif
6463 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 ++count;
6465 return (count <= 1);
6466#else
6467 return TRUE;
6468#endif
6469}
6470
6471#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6472/*
6473 * Correct the cursor line number in other windows. Used after changing the
6474 * current buffer, and before applying autocommands.
6475 * When "do_curwin" is TRUE, also check current window.
6476 */
6477 void
6478check_lnums(do_curwin)
6479 int do_curwin;
6480{
6481 win_T *wp;
6482
6483#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006484 tabpage_T *tp;
6485
6486 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6488#else
6489 wp = curwin;
6490 if (do_curwin)
6491#endif
6492 {
6493 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6494 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6495 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6496 wp->w_topline = curbuf->b_ml.ml_line_count;
6497 }
6498}
6499#endif
6500
6501#if defined(FEAT_WINDOWS) || defined(PROTO)
6502
6503/*
6504 * A snapshot of the window sizes, to restore them after closing the help
6505 * window.
6506 * Only these fields are used:
6507 * fr_layout
6508 * fr_width
6509 * fr_height
6510 * fr_next
6511 * fr_child
6512 * fr_win (only valid for the old curwin, NULL otherwise)
6513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
6515/*
6516 * Create a snapshot of the current frame sizes.
6517 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006518 void
6519make_snapshot(idx)
6520 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006522 clear_snapshot(curtab, idx);
6523 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524}
6525
6526 static void
6527make_snapshot_rec(fr, frp)
6528 frame_T *fr;
6529 frame_T **frp;
6530{
6531 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6532 if (*frp == NULL)
6533 return;
6534 (*frp)->fr_layout = fr->fr_layout;
6535# ifdef FEAT_VERTSPLIT
6536 (*frp)->fr_width = fr->fr_width;
6537# endif
6538 (*frp)->fr_height = fr->fr_height;
6539 if (fr->fr_next != NULL)
6540 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6541 if (fr->fr_child != NULL)
6542 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6543 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6544 (*frp)->fr_win = curwin;
6545}
6546
6547/*
6548 * Remove any existing snapshot.
6549 */
6550 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006551clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006552 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006553 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006555 clear_snapshot_rec(tp->tp_snapshot[idx]);
6556 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557}
6558
6559 static void
6560clear_snapshot_rec(fr)
6561 frame_T *fr;
6562{
6563 if (fr != NULL)
6564 {
6565 clear_snapshot_rec(fr->fr_next);
6566 clear_snapshot_rec(fr->fr_child);
6567 vim_free(fr);
6568 }
6569}
6570
6571/*
6572 * Restore a previously created snapshot, if there is any.
6573 * This is only done if the screen size didn't change and the window layout is
6574 * still the same.
6575 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006576 void
6577restore_snapshot(idx, close_curwin)
6578 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 int close_curwin; /* closing current window */
6580{
6581 win_T *wp;
6582
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006583 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006585 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006587 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6588 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006590 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 win_comp_pos();
6592 if (wp != NULL && close_curwin)
6593 win_goto(wp);
6594 redraw_all_later(CLEAR);
6595 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006596 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597}
6598
6599/*
6600 * Check if frames "sn" and "fr" have the same layout, same following frames
6601 * and same children.
6602 */
6603 static int
6604check_snapshot_rec(sn, fr)
6605 frame_T *sn;
6606 frame_T *fr;
6607{
6608 if (sn->fr_layout != fr->fr_layout
6609 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6610 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6611 || (sn->fr_next != NULL
6612 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6613 || (sn->fr_child != NULL
6614 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6615 return FAIL;
6616 return OK;
6617}
6618
6619/*
6620 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6621 * following frames and children.
6622 * Returns a pointer to the old current window, or NULL.
6623 */
6624 static win_T *
6625restore_snapshot_rec(sn, fr)
6626 frame_T *sn;
6627 frame_T *fr;
6628{
6629 win_T *wp = NULL;
6630 win_T *wp2;
6631
6632 fr->fr_height = sn->fr_height;
6633# ifdef FEAT_VERTSPLIT
6634 fr->fr_width = sn->fr_width;
6635# endif
6636 if (fr->fr_layout == FR_LEAF)
6637 {
6638 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6639# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006640 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641# endif
6642 wp = sn->fr_win;
6643 }
6644 if (sn->fr_next != NULL)
6645 {
6646 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6647 if (wp2 != NULL)
6648 wp = wp2;
6649 }
6650 if (sn->fr_child != NULL)
6651 {
6652 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6653 if (wp2 != NULL)
6654 wp = wp2;
6655 }
6656 return wp;
6657}
6658
6659#endif
6660
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006661#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6662 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006663/*
6664 * Set "win" to be the curwin and "tp" to be the current tab page.
6665 * restore_win() MUST be called to undo.
6666 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006667 * When "no_display" is TRUE the display won't be affected, no redraw is
6668 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006669 * Returns FAIL if switching to "win" failed.
6670 */
6671 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006672switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006673 win_T **save_curwin UNUSED;
6674 tabpage_T **save_curtab UNUSED;
6675 win_T *win UNUSED;
6676 tabpage_T *tp UNUSED;
6677 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006678{
6679# ifdef FEAT_AUTOCMD
6680 block_autocmds();
6681# endif
6682# ifdef FEAT_WINDOWS
6683 *save_curwin = curwin;
6684 if (tp != NULL)
6685 {
6686 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006687 if (no_display)
6688 {
6689 curtab->tp_firstwin = firstwin;
6690 curtab->tp_lastwin = lastwin;
6691 curtab = tp;
6692 firstwin = curtab->tp_firstwin;
6693 lastwin = curtab->tp_lastwin;
6694 }
6695 else
6696 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006697 }
6698 if (!win_valid(win))
6699 {
6700# ifdef FEAT_AUTOCMD
6701 unblock_autocmds();
6702# endif
6703 return FAIL;
6704 }
6705 curwin = win;
6706 curbuf = curwin->w_buffer;
6707# endif
6708 return OK;
6709}
6710
6711/*
6712 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006713 * When "no_display" is TRUE the display won't be affected, no redraw is
6714 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006715 */
6716 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006717restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006718 win_T *save_curwin UNUSED;
6719 tabpage_T *save_curtab UNUSED;
6720 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006721{
6722# ifdef FEAT_WINDOWS
6723 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006724 {
6725 if (no_display)
6726 {
6727 curtab->tp_firstwin = firstwin;
6728 curtab->tp_lastwin = lastwin;
6729 curtab = save_curtab;
6730 firstwin = curtab->tp_firstwin;
6731 lastwin = curtab->tp_lastwin;
6732 }
6733 else
6734 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6735 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006736 if (win_valid(save_curwin))
6737 {
6738 curwin = save_curwin;
6739 curbuf = curwin->w_buffer;
6740 }
6741# endif
6742# ifdef FEAT_AUTOCMD
6743 unblock_autocmds();
6744# endif
6745}
6746
6747/*
6748 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6749 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6750 */
6751 void
6752switch_buffer(save_curbuf, buf)
6753 buf_T *buf;
6754 buf_T **save_curbuf;
6755{
6756# ifdef FEAT_AUTOCMD
6757 block_autocmds();
6758# endif
6759 *save_curbuf = curbuf;
6760 --curbuf->b_nwindows;
6761 curbuf = buf;
6762 curwin->w_buffer = buf;
6763 ++curbuf->b_nwindows;
6764}
6765
6766/*
6767 * Restore the current buffer after using switch_buffer().
6768 */
6769 void
6770restore_buffer(save_curbuf)
6771 buf_T *save_curbuf;
6772{
6773# ifdef FEAT_AUTOCMD
6774 unblock_autocmds();
6775# endif
6776 /* Check for valid buffer, just in case. */
6777 if (buf_valid(save_curbuf))
6778 {
6779 --curbuf->b_nwindows;
6780 curwin->w_buffer = save_curbuf;
6781 curbuf = save_curbuf;
6782 ++curbuf->b_nwindows;
6783 }
6784}
6785#endif
6786
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6788/*
6789 * Return TRUE if there is any vertically split window.
6790 */
6791 int
6792win_hasvertsplit()
6793{
6794 frame_T *fr;
6795
6796 if (topframe->fr_layout == FR_ROW)
6797 return TRUE;
6798
6799 if (topframe->fr_layout == FR_COL)
6800 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6801 if (fr->fr_layout == FR_ROW)
6802 return TRUE;
6803
6804 return FALSE;
6805}
6806#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006807
6808#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6809/*
6810 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006811 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006812 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6813 * If no particular ID is desired, -1 must be specified for 'id'.
6814 * Return ID of added match, -1 on failure.
6815 */
6816 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006817match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006818 win_T *wp;
6819 char_u *grp;
6820 char_u *pat;
6821 int prio;
6822 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006823 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006824{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006825 matchitem_T *cur;
6826 matchitem_T *prev;
6827 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006828 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006829 regprog_T *regprog = NULL;
6830 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006831
Bram Moolenaarb3414592014-06-17 17:48:32 +02006832 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006833 return -1;
6834 if (id < -1 || id == 0)
6835 {
6836 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6837 return -1;
6838 }
6839 if (id != -1)
6840 {
6841 cur = wp->w_match_head;
6842 while (cur != NULL)
6843 {
6844 if (cur->id == id)
6845 {
6846 EMSGN("E801: ID already taken: %ld", id);
6847 return -1;
6848 }
6849 cur = cur->next;
6850 }
6851 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006852 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006853 {
6854 EMSG2(_(e_nogroup), grp);
6855 return -1;
6856 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006857 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006858 {
6859 EMSG2(_(e_invarg2), pat);
6860 return -1;
6861 }
6862
6863 /* Find available match ID. */
6864 while (id == -1)
6865 {
6866 cur = wp->w_match_head;
6867 while (cur != NULL && cur->id != wp->w_next_match_id)
6868 cur = cur->next;
6869 if (cur == NULL)
6870 id = wp->w_next_match_id;
6871 wp->w_next_match_id++;
6872 }
6873
6874 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006875 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006876 m->id = id;
6877 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006878 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006879 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006880 m->match.regprog = regprog;
6881 m->match.rmm_ic = FALSE;
6882 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006883
Bram Moolenaarb3414592014-06-17 17:48:32 +02006884 /* Set up position matches */
6885 if (pos_list != NULL)
6886 {
6887 linenr_T toplnum = 0;
6888 linenr_T botlnum = 0;
6889 listitem_T *li;
6890 int i;
6891
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006892 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006893 i++, li = li->li_next)
6894 {
6895 linenr_T lnum = 0;
6896 colnr_T col = 0;
6897 int len = 1;
6898 list_T *subl;
6899 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006900 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006901
Bram Moolenaarb3414592014-06-17 17:48:32 +02006902 if (li->li_tv.v_type == VAR_LIST)
6903 {
6904 subl = li->li_tv.vval.v_list;
6905 if (subl == NULL)
6906 goto fail;
6907 subli = subl->lv_first;
6908 if (subli == NULL)
6909 goto fail;
6910 lnum = get_tv_number_chk(&subli->li_tv, &error);
6911 if (error == TRUE)
6912 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006913 if (lnum == 0)
6914 {
6915 --i;
6916 continue;
6917 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006918 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006919 subli = subli->li_next;
6920 if (subli != NULL)
6921 {
6922 col = get_tv_number_chk(&subli->li_tv, &error);
6923 if (error == TRUE)
6924 goto fail;
6925 subli = subli->li_next;
6926 if (subli != NULL)
6927 {
6928 len = get_tv_number_chk(&subli->li_tv, &error);
6929 if (error == TRUE)
6930 goto fail;
6931 }
6932 }
6933 m->pos.pos[i].col = col;
6934 m->pos.pos[i].len = len;
6935 }
6936 else if (li->li_tv.v_type == VAR_NUMBER)
6937 {
6938 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006939 {
6940 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006941 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006942 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006943 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6944 m->pos.pos[i].col = 0;
6945 m->pos.pos[i].len = 0;
6946 }
6947 else
6948 {
6949 EMSG(_("List or number required"));
6950 goto fail;
6951 }
6952 if (toplnum == 0 || lnum < toplnum)
6953 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006954 if (botlnum == 0 || lnum >= botlnum)
6955 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006956 }
6957
6958 /* Calculate top and bottom lines for redrawing area */
6959 if (toplnum != 0)
6960 {
6961 if (wp->w_buffer->b_mod_set)
6962 {
6963 if (wp->w_buffer->b_mod_top > toplnum)
6964 wp->w_buffer->b_mod_top = toplnum;
6965 if (wp->w_buffer->b_mod_bot < botlnum)
6966 wp->w_buffer->b_mod_bot = botlnum;
6967 }
6968 else
6969 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006970 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006971 wp->w_buffer->b_mod_top = toplnum;
6972 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006973 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006974 }
6975 m->pos.toplnum = toplnum;
6976 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006977 rtype = VALID;
6978 }
6979 }
6980
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006981 /* Insert new match. The match list is in ascending order with regard to
6982 * the match priorities. */
6983 cur = wp->w_match_head;
6984 prev = cur;
6985 while (cur != NULL && prio >= cur->priority)
6986 {
6987 prev = cur;
6988 cur = cur->next;
6989 }
6990 if (cur == prev)
6991 wp->w_match_head = m;
6992 else
6993 prev->next = m;
6994 m->next = cur;
6995
Bram Moolenaarb3414592014-06-17 17:48:32 +02006996 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006997 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006998
6999fail:
7000 vim_free(m);
7001 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007002}
7003
7004/*
7005 * Delete match with ID 'id' in the match list of window 'wp'.
7006 * Print error messages if 'perr' is TRUE.
7007 */
7008 int
7009match_delete(wp, id, perr)
7010 win_T *wp;
7011 int id;
7012 int perr;
7013{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007014 matchitem_T *cur = wp->w_match_head;
7015 matchitem_T *prev = cur;
7016 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007017
7018 if (id < 1)
7019 {
7020 if (perr == TRUE)
7021 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7022 id);
7023 return -1;
7024 }
7025 while (cur != NULL && cur->id != id)
7026 {
7027 prev = cur;
7028 cur = cur->next;
7029 }
7030 if (cur == NULL)
7031 {
7032 if (perr == TRUE)
7033 EMSGN("E803: ID not found: %ld", id);
7034 return -1;
7035 }
7036 if (cur == prev)
7037 wp->w_match_head = cur->next;
7038 else
7039 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007040 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007041 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007042 if (cur->pos.toplnum != 0)
7043 {
7044 if (wp->w_buffer->b_mod_set)
7045 {
7046 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7047 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7048 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7049 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7050 }
7051 else
7052 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007053 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007054 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7055 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007056 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007057 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007058 rtype = VALID;
7059 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007060 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007061 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007062 return 0;
7063}
7064
7065/*
7066 * Delete all matches in the match list of window 'wp'.
7067 */
7068 void
7069clear_matches(wp)
7070 win_T *wp;
7071{
7072 matchitem_T *m;
7073
7074 while (wp->w_match_head != NULL)
7075 {
7076 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007077 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007078 vim_free(wp->w_match_head->pattern);
7079 vim_free(wp->w_match_head);
7080 wp->w_match_head = m;
7081 }
7082 redraw_later(SOME_VALID);
7083}
7084
7085/*
7086 * Get match from ID 'id' in window 'wp'.
7087 * Return NULL if match not found.
7088 */
7089 matchitem_T *
7090get_match(wp, id)
7091 win_T *wp;
7092 int id;
7093{
7094 matchitem_T *cur = wp->w_match_head;
7095
7096 while (cur != NULL && cur->id != id)
7097 cur = cur->next;
7098 return cur;
7099}
7100#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007101
7102#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7103 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007104get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007105{
7106 int i = 1;
7107 win_T *w;
7108
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007109 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007110 ++i;
7111
7112 if (w == NULL)
7113 return 0;
7114 else
7115 return i;
7116}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007117
7118 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007119get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007120{
7121 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007122# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007123 tabpage_T *t;
7124
7125 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7126 ++i;
7127
7128 if (t == NULL)
7129 return 0;
7130 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007131# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007132 return i;
7133}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007134#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007135
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007136#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007137/*
7138 * Return TRUE if "topfrp" and its children are at the right height.
7139 */
7140 static int
7141frame_check_height(topfrp, height)
7142 frame_T *topfrp;
7143 int height;
7144{
7145 frame_T *frp;
7146
7147 if (topfrp->fr_height != height)
7148 return FALSE;
7149
7150 if (topfrp->fr_layout == FR_ROW)
7151 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7152 if (frp->fr_height != height)
7153 return FALSE;
7154
7155 return TRUE;
7156}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007157#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007158
7159#ifdef FEAT_VERTSPLIT
7160/*
7161 * Return TRUE if "topfrp" and its children are at the right width.
7162 */
7163 static int
7164frame_check_width(topfrp, width)
7165 frame_T *topfrp;
7166 int width;
7167{
7168 frame_T *frp;
7169
7170 if (topfrp->fr_width != width)
7171 return FALSE;
7172
7173 if (topfrp->fr_layout == FR_COL)
7174 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7175 if (frp->fr_width != width)
7176 return FALSE;
7177
7178 return TRUE;
7179}
7180#endif
7181