blob: f81f5527b52a4d145ee8586e26fec571d59a52af [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;
687 frame_T *frp, *curfrp;
688 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 Moolenaar071d4272004-06-13 20:20:40 +0000733 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
734 {
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 }
739 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200740 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200741 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
742 available = oldwin->w_frame->fr_width;
743 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200744 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100745 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 EMSG(_(e_noroom));
748 return FAIL;
749 }
750 if (new_size == 0)
751 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200752 if (new_size > available - minwidth - 1)
753 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200754 if (new_size < wmw1)
755 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756
757 /* if it doesn't fit in the current window, need win_equal() */
758 if (oldwin->w_width - new_size - 1 < p_wmw)
759 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000760
761 /* We don't like to take lines for the new window from a
762 * 'winfixwidth' window. Take them from a window to the left or right
763 * instead, if possible. */
764 if (oldwin->w_p_wfw)
765 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000766
767 /* Only make all windows the same width if one of them (except oldwin)
768 * is wider than one of the split windows. */
769 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
770 && oldwin->w_frame->fr_parent != NULL)
771 {
772 frp = oldwin->w_frame->fr_parent->fr_child;
773 while (frp != NULL)
774 {
775 if (frp->fr_win != oldwin && frp->fr_win != NULL
776 && (frp->fr_win->w_width > new_size
777 || frp->fr_win->w_width > oldwin->w_width
778 - new_size - STATUS_HEIGHT))
779 {
780 do_equal = TRUE;
781 break;
782 }
783 frp = frp->fr_next;
784 }
785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 else
788#endif
789 {
790 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 /*
793 * Check if we are able to split the current window and compute its
794 * height.
795 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200796 /* Current window requires at least 1 space. */
797 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
798 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200800 needed += p_wh - wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
802 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200803 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200805 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
807 else
808 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200809 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
810 available = oldwin->w_frame->fr_height;
811 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100813 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
815 EMSG(_(e_noroom));
816 return FAIL;
817 }
818 oldwin_height = oldwin->w_height;
819 if (need_status)
820 {
821 oldwin->w_status_height = STATUS_HEIGHT;
822 oldwin_height -= STATUS_HEIGHT;
823 }
824 if (new_size == 0)
825 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200826 if (new_size > available - minheight - STATUS_HEIGHT)
827 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200828 if (new_size < wmh1)
829 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830
831 /* if it doesn't fit in the current window, need win_equal() */
832 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
833 do_equal = TRUE;
834
835 /* We don't like to take lines for the new window from a
836 * 'winfixheight' window. Take them from a window above or below
837 * instead, if possible. */
838 if (oldwin->w_p_wfh)
839 {
840 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
841 oldwin);
842 oldwin_height = oldwin->w_height;
843 if (need_status)
844 oldwin_height -= STATUS_HEIGHT;
845 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000846
847 /* Only make all windows the same height if one of them (except oldwin)
848 * is higher than one of the split windows. */
849 if (!do_equal && p_ea && size == 0
850#ifdef FEAT_VERTSPLIT
851 && *p_ead != 'h'
852#endif
853 && oldwin->w_frame->fr_parent != NULL)
854 {
855 frp = oldwin->w_frame->fr_parent->fr_child;
856 while (frp != NULL)
857 {
858 if (frp->fr_win != oldwin && frp->fr_win != NULL
859 && (frp->fr_win->w_height > new_size
860 || frp->fr_win->w_height > oldwin_height - new_size
861 - STATUS_HEIGHT))
862 {
863 do_equal = TRUE;
864 break;
865 }
866 frp = frp->fr_next;
867 }
868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870
871 /*
872 * allocate new window structure and link it in the window list
873 */
874 if ((flags & WSP_TOP) == 0
875 && ((flags & WSP_BOT)
876 || (flags & WSP_BELOW)
877 || (!(flags & WSP_ABOVE)
878 && (
879#ifdef FEAT_VERTSPLIT
880 (flags & WSP_VERT) ? p_spr :
881#endif
882 p_sb))))
883 {
884 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100885 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000886 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 else
888 win_append(oldwin, wp);
889 }
890 else
891 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100892 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000893 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 else
895 win_append(oldwin->w_prev, wp);
896 }
897
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100898 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 {
900 if (wp == NULL)
901 return FAIL;
902
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000903 new_frame(wp);
904 if (wp->w_frame == NULL)
905 {
906 win_free(wp, NULL);
907 return FAIL;
908 }
909
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000910 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000911 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913
914 /*
915 * Reorganise the tree of frames to insert the new window.
916 */
917 if (flags & (WSP_TOP | WSP_BOT))
918 {
919#ifdef FEAT_VERTSPLIT
920 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
921 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
922#else
923 if (topframe->fr_layout == FR_COL)
924#endif
925 {
926 curfrp = topframe->fr_child;
927 if (flags & WSP_BOT)
928 while (curfrp->fr_next != NULL)
929 curfrp = curfrp->fr_next;
930 }
931 else
932 curfrp = topframe;
933 before = (flags & WSP_TOP);
934 }
935 else
936 {
937 curfrp = oldwin->w_frame;
938 if (flags & WSP_BELOW)
939 before = FALSE;
940 else if (flags & WSP_ABOVE)
941 before = TRUE;
942 else
943#ifdef FEAT_VERTSPLIT
944 if (flags & WSP_VERT)
945 before = !p_spr;
946 else
947#endif
948 before = !p_sb;
949 }
950 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
951 {
952 /* Need to create a new frame in the tree to make a branch. */
953 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
954 *frp = *curfrp;
955 curfrp->fr_layout = layout;
956 frp->fr_parent = curfrp;
957 frp->fr_next = NULL;
958 frp->fr_prev = NULL;
959 curfrp->fr_child = frp;
960 curfrp->fr_win = NULL;
961 curfrp = frp;
962 if (frp->fr_win != NULL)
963 oldwin->w_frame = frp;
964 else
965 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
966 frp->fr_parent = curfrp;
967 }
968
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100969 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000970 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100972 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 frp->fr_parent = curfrp->fr_parent;
974
975 /* Insert the new frame at the right place in the frame list. */
976 if (before)
977 frame_insert(curfrp, frp);
978 else
979 frame_append(curfrp, frp);
980
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100981 /* Set w_fraction now so that the cursor keeps the same relative
982 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100983 if (oldwin->w_height > 0)
984 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100985 wp->w_fraction = oldwin->w_fraction;
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#ifdef FEAT_VERTSPLIT
988 if (flags & WSP_VERT)
989 {
990 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (need_status)
993 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100994 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 oldwin->w_status_height = need_status;
996 }
997 if (flags & (WSP_TOP | WSP_BOT))
998 {
999 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001000 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001001 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 wp->w_status_height = (p_ls > 0);
1003 }
1004 else
1005 {
1006 /* height and row of new window is same as current window */
1007 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001008 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 wp->w_status_height = oldwin->w_status_height;
1010 }
1011 frp->fr_height = curfrp->fr_height;
1012
1013 /* "new_size" of the current window goes to the new window, use
1014 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001015 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (before)
1017 wp->w_vsep_width = 1;
1018 else
1019 {
1020 wp->w_vsep_width = oldwin->w_vsep_width;
1021 oldwin->w_vsep_width = 1;
1022 }
1023 if (flags & (WSP_TOP | WSP_BOT))
1024 {
1025 if (flags & WSP_BOT)
1026 frame_add_vsep(curfrp);
1027 /* Set width of neighbor frame */
1028 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001029 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1030 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001033 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 if (before) /* new window left of current one */
1035 {
1036 wp->w_wincol = oldwin->w_wincol;
1037 oldwin->w_wincol += new_size + 1;
1038 }
1039 else /* new window right of current one */
1040 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1041 frame_fix_width(oldwin);
1042 frame_fix_width(wp);
1043 }
1044 else
1045#endif
1046 {
1047 /* width and column of new window is same as current window */
1048#ifdef FEAT_VERTSPLIT
1049 if (flags & (WSP_TOP | WSP_BOT))
1050 {
1051 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001052 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 wp->w_vsep_width = 0;
1054 }
1055 else
1056 {
1057 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001058 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 wp->w_vsep_width = oldwin->w_vsep_width;
1060 }
1061 frp->fr_width = curfrp->fr_width;
1062#endif
1063
1064 /* "new_size" of the current window goes to the new window, use
1065 * one row for the status line */
1066 win_new_height(wp, new_size);
1067 if (flags & (WSP_TOP | WSP_BOT))
1068 frame_new_height(curfrp, curfrp->fr_height
1069 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1070 else
1071 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1072 if (before) /* new window above current one */
1073 {
1074 wp->w_winrow = oldwin->w_winrow;
1075 wp->w_status_height = STATUS_HEIGHT;
1076 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1077 }
1078 else /* new window below current one */
1079 {
1080 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1081 wp->w_status_height = oldwin->w_status_height;
1082 oldwin->w_status_height = STATUS_HEIGHT;
1083 }
1084#ifdef FEAT_VERTSPLIT
1085 if (flags & WSP_BOT)
1086 frame_add_statusline(curfrp);
1087#endif
1088 frame_fix_height(wp);
1089 frame_fix_height(oldwin);
1090 }
1091
1092 if (flags & (WSP_TOP | WSP_BOT))
1093 (void)win_comp_pos();
1094
1095 /*
1096 * Both windows need redrawing
1097 */
1098 redraw_win_later(wp, NOT_VALID);
1099 wp->w_redr_status = TRUE;
1100 redraw_win_later(oldwin, NOT_VALID);
1101 oldwin->w_redr_status = TRUE;
1102
1103 if (need_status)
1104 {
1105 msg_row = Rows - 1;
1106 msg_col = sc_col;
1107 msg_clr_eos_force(); /* Old command/ruler may still be there */
1108 comp_col();
1109 msg_row = Rows - 1;
1110 msg_col = 0; /* put position back at start of line */
1111 }
1112
1113 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001114 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 */
1116 if (do_equal || dir != 0)
1117 win_equal(wp, TRUE,
1118#ifdef FEAT_VERTSPLIT
1119 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1120 : dir == 'h' ? 'b' :
1121#endif
1122 'v');
1123
1124 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1125 * size was given. */
1126#ifdef FEAT_VERTSPLIT
1127 if (flags & WSP_VERT)
1128 {
1129 i = p_wiw;
1130 if (size != 0)
1131 p_wiw = size;
1132
1133# ifdef FEAT_GUI
1134 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1135 if (gui.in_use)
1136 gui_init_which_components(NULL);
1137# endif
1138 }
1139 else
1140#endif
1141 {
1142 i = p_wh;
1143 if (size != 0)
1144 p_wh = size;
1145 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001146
1147 /*
1148 * make the new window the current window
1149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 win_enter(wp, FALSE);
1151#ifdef FEAT_VERTSPLIT
1152 if (flags & WSP_VERT)
1153 p_wiw = i;
1154 else
1155#endif
1156 p_wh = i;
1157
1158 return OK;
1159}
1160
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001161
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001162/*
1163 * Initialize window "newp" from window "oldp".
1164 * Used when splitting a window and when creating a new tab page.
1165 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001166 * WSP_NEWLOC may be specified in flags to prevent the location list from
1167 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168 */
1169 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001170win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001171 win_T *newp;
1172 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001173 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001174{
1175 int i;
1176
1177 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001178#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001179 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001180#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001181 oldp->w_buffer->b_nwindows++;
1182 newp->w_cursor = oldp->w_cursor;
1183 newp->w_valid = 0;
1184 newp->w_curswant = oldp->w_curswant;
1185 newp->w_set_curswant = oldp->w_set_curswant;
1186 newp->w_topline = oldp->w_topline;
1187#ifdef FEAT_DIFF
1188 newp->w_topfill = oldp->w_topfill;
1189#endif
1190 newp->w_leftcol = oldp->w_leftcol;
1191 newp->w_pcmark = oldp->w_pcmark;
1192 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1193 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001194 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001195 newp->w_fraction = oldp->w_fraction;
1196 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1197#ifdef FEAT_JUMPLIST
1198 copy_jumplist(oldp, newp);
1199#endif
1200#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001201 if (flags & WSP_NEWLOC)
1202 {
1203 /* Don't copy the location list. */
1204 newp->w_llist = NULL;
1205 newp->w_llist_ref = NULL;
1206 }
1207 else
1208 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001209#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001210 newp->w_localdir = (oldp->w_localdir == NULL)
1211 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001212
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001213 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001214 for (i = 0; i < oldp->w_tagstacklen; i++)
1215 {
1216 newp->w_tagstack[i] = oldp->w_tagstack[i];
1217 if (newp->w_tagstack[i].tagname != NULL)
1218 newp->w_tagstack[i].tagname =
1219 vim_strsave(newp->w_tagstack[i].tagname);
1220 }
1221 newp->w_tagstackidx = oldp->w_tagstackidx;
1222 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001223#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001224 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001225#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001226
1227 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001228
Bram Moolenaara971b822011-09-14 14:43:25 +02001229#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001230 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001231#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001232}
1233
1234/*
1235 * Initialize window "newp" from window"old".
1236 * Only the essential things are copied.
1237 */
1238 static void
1239win_init_some(newp, oldp)
1240 win_T *newp;
1241 win_T *oldp;
1242{
1243 /* Use the same argument list. */
1244 newp->w_alist = oldp->w_alist;
1245 ++newp->w_alist->al_refcount;
1246 newp->w_arg_idx = oldp->w_arg_idx;
1247
1248 /* copy options from existing window */
1249 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001250}
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252#endif /* FEAT_WINDOWS */
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254#if defined(FEAT_WINDOWS) || defined(PROTO)
1255/*
1256 * Check if "win" is a pointer to an existing window.
1257 */
1258 int
1259win_valid(win)
1260 win_T *win;
1261{
1262 win_T *wp;
1263
1264 if (win == NULL)
1265 return FALSE;
1266 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1267 if (wp == win)
1268 return TRUE;
1269 return FALSE;
1270}
1271
1272/*
1273 * Return the number of windows.
1274 */
1275 int
1276win_count()
1277{
1278 win_T *wp;
1279 int count = 0;
1280
1281 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1282 ++count;
1283 return count;
1284}
1285
1286/*
1287 * Make "count" windows on the screen.
1288 * Return actual number of windows on the screen.
1289 * Must be called when there is just one window, filling the whole screen
1290 * (excluding the command line).
1291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 int
1293make_windows(count, vertical)
1294 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001295 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int maxcount;
1298 int todo;
1299
1300#ifdef FEAT_VERTSPLIT
1301 if (vertical)
1302 {
1303 /* Each windows needs at least 'winminwidth' lines and a separator
1304 * column. */
1305 maxcount = (curwin->w_width + curwin->w_vsep_width
1306 - (p_wiw - p_wmw)) / (p_wmw + 1);
1307 }
1308 else
1309#endif
1310 {
1311 /* Each window needs at least 'winminheight' lines and a status line. */
1312 maxcount = (curwin->w_height + curwin->w_status_height
1313 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1314 }
1315
1316 if (maxcount < 2)
1317 maxcount = 2;
1318 if (count > maxcount)
1319 count = maxcount;
1320
1321 /*
1322 * add status line now, otherwise first window will be too big
1323 */
1324 if (count > 1)
1325 last_status(TRUE);
1326
1327#ifdef FEAT_AUTOCMD
1328 /*
1329 * Don't execute autocommands while creating the windows. Must do that
1330 * when putting the buffers in the windows.
1331 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001332 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333#endif
1334
1335 /* todo is number of windows left to create */
1336 for (todo = count - 1; todo > 0; --todo)
1337#ifdef FEAT_VERTSPLIT
1338 if (vertical)
1339 {
1340 if (win_split(curwin->w_width - (curwin->w_width - todo)
1341 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1342 break;
1343 }
1344 else
1345#endif
1346 {
1347 if (win_split(curwin->w_height - (curwin->w_height - todo
1348 * STATUS_HEIGHT) / (todo + 1)
1349 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1350 break;
1351 }
1352
1353#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001354 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#endif
1356
1357 /* return actual number of windows */
1358 return (count - todo);
1359}
1360
1361/*
1362 * Exchange current and next window
1363 */
1364 static void
1365win_exchange(Prenum)
1366 long Prenum;
1367{
1368 frame_T *frp;
1369 frame_T *frp2;
1370 win_T *wp;
1371 win_T *wp2;
1372 int temp;
1373
1374 if (lastwin == firstwin) /* just one window */
1375 {
1376 beep_flush();
1377 return;
1378 }
1379
1380#ifdef FEAT_GUI
1381 need_mouse_correct = TRUE;
1382#endif
1383
1384 /*
1385 * find window to exchange with
1386 */
1387 if (Prenum)
1388 {
1389 frp = curwin->w_frame->fr_parent->fr_child;
1390 while (frp != NULL && --Prenum > 0)
1391 frp = frp->fr_next;
1392 }
1393 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1394 frp = curwin->w_frame->fr_next;
1395 else /* Swap last window in row/col with previous */
1396 frp = curwin->w_frame->fr_prev;
1397
1398 /* We can only exchange a window with another window, not with a frame
1399 * containing windows. */
1400 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1401 return;
1402 wp = frp->fr_win;
1403
1404/*
1405 * 1. remove curwin from the list. Remember after which window it was in wp2
1406 * 2. insert curwin before wp in the list
1407 * if wp != wp2
1408 * 3. remove wp from the list
1409 * 4. insert wp after wp2
1410 * 5. exchange the status line height and vsep width.
1411 */
1412 wp2 = curwin->w_prev;
1413 frp2 = curwin->w_frame->fr_prev;
1414 if (wp->w_prev != curwin)
1415 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 frame_remove(curwin->w_frame);
1418 win_append(wp->w_prev, curwin);
1419 frame_insert(frp, curwin->w_frame);
1420 }
1421 if (wp != wp2)
1422 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001423 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 frame_remove(wp->w_frame);
1425 win_append(wp2, wp);
1426 if (frp2 == NULL)
1427 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1428 else
1429 frame_append(frp2, wp->w_frame);
1430 }
1431 temp = curwin->w_status_height;
1432 curwin->w_status_height = wp->w_status_height;
1433 wp->w_status_height = temp;
1434#ifdef FEAT_VERTSPLIT
1435 temp = curwin->w_vsep_width;
1436 curwin->w_vsep_width = wp->w_vsep_width;
1437 wp->w_vsep_width = temp;
1438
1439 /* If the windows are not in the same frame, exchange the sizes to avoid
1440 * messing up the window layout. Otherwise fix the frame sizes. */
1441 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1442 {
1443 temp = curwin->w_height;
1444 curwin->w_height = wp->w_height;
1445 wp->w_height = temp;
1446 temp = curwin->w_width;
1447 curwin->w_width = wp->w_width;
1448 wp->w_width = temp;
1449 }
1450 else
1451 {
1452 frame_fix_height(curwin);
1453 frame_fix_height(wp);
1454 frame_fix_width(curwin);
1455 frame_fix_width(wp);
1456 }
1457#endif
1458
1459 (void)win_comp_pos(); /* recompute window positions */
1460
1461 win_enter(wp, TRUE);
1462 redraw_later(CLEAR);
1463}
1464
1465/*
1466 * rotate windows: if upwards TRUE the second window becomes the first one
1467 * if upwards FALSE the first window becomes the second one
1468 */
1469 static void
1470win_rotate(upwards, count)
1471 int upwards;
1472 int count;
1473{
1474 win_T *wp1;
1475 win_T *wp2;
1476 frame_T *frp;
1477 int n;
1478
1479 if (firstwin == lastwin) /* nothing to do */
1480 {
1481 beep_flush();
1482 return;
1483 }
1484
1485#ifdef FEAT_GUI
1486 need_mouse_correct = TRUE;
1487#endif
1488
1489#ifdef FEAT_VERTSPLIT
1490 /* Check if all frames in this row/col have one window. */
1491 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1492 frp = frp->fr_next)
1493 if (frp->fr_win == NULL)
1494 {
1495 EMSG(_("E443: Cannot rotate when another window is split"));
1496 return;
1497 }
1498#endif
1499
1500 while (count--)
1501 {
1502 if (upwards) /* first window becomes last window */
1503 {
1504 /* remove first window/frame from the list */
1505 frp = curwin->w_frame->fr_parent->fr_child;
1506 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001507 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 frame_remove(frp);
1509
1510 /* find last frame and append removed window/frame after it */
1511 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1512 ;
1513 win_append(frp->fr_win, wp1);
1514 frame_append(frp, wp1->w_frame);
1515
1516 wp2 = frp->fr_win; /* previously last window */
1517 }
1518 else /* last window becomes first window */
1519 {
1520 /* find last window/frame in the list and remove it */
1521 for (frp = curwin->w_frame; frp->fr_next != NULL;
1522 frp = frp->fr_next)
1523 ;
1524 wp1 = frp->fr_win;
1525 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001526 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 frame_remove(frp);
1528
1529 /* append the removed window/frame before the first in the list */
1530 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1531 frame_insert(frp->fr_parent->fr_child, frp);
1532 }
1533
1534 /* exchange status height and vsep width of old and new last window */
1535 n = wp2->w_status_height;
1536 wp2->w_status_height = wp1->w_status_height;
1537 wp1->w_status_height = n;
1538 frame_fix_height(wp1);
1539 frame_fix_height(wp2);
1540#ifdef FEAT_VERTSPLIT
1541 n = wp2->w_vsep_width;
1542 wp2->w_vsep_width = wp1->w_vsep_width;
1543 wp1->w_vsep_width = n;
1544 frame_fix_width(wp1);
1545 frame_fix_width(wp2);
1546#endif
1547
1548 /* recompute w_winrow and w_wincol for all windows */
1549 (void)win_comp_pos();
1550 }
1551
1552 redraw_later(CLEAR);
1553}
1554
1555/*
1556 * Move the current window to the very top/bottom/left/right of the screen.
1557 */
1558 static void
1559win_totop(size, flags)
1560 int size;
1561 int flags;
1562{
1563 int dir;
1564 int height = curwin->w_height;
1565
1566 if (lastwin == firstwin)
1567 {
1568 beep_flush();
1569 return;
1570 }
1571
1572 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001573 (void)winframe_remove(curwin, &dir, NULL);
1574 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 last_status(FALSE); /* may need to remove last status line */
1576 (void)win_comp_pos(); /* recompute window positions */
1577
1578 /* Split a window on the desired side and put the window there. */
1579 (void)win_split_ins(size, flags, curwin, dir);
1580 if (!(flags & WSP_VERT))
1581 {
1582 win_setheight(height);
1583 if (p_ea)
1584 win_equal(curwin, TRUE, 'v');
1585 }
1586
1587#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1588 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1589 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001590 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592}
1593
1594/*
1595 * Move window "win1" to below/right of "win2" and make "win1" the current
1596 * window. Only works within the same frame!
1597 */
1598 void
1599win_move_after(win1, win2)
1600 win_T *win1, *win2;
1601{
1602 int height;
1603
1604 /* check if the arguments are reasonable */
1605 if (win1 == win2)
1606 return;
1607
1608 /* check if there is something to do */
1609 if (win2->w_next != win1)
1610 {
1611 /* may need move the status line/vertical separator of the last window
1612 * */
1613 if (win1 == lastwin)
1614 {
1615 height = win1->w_prev->w_status_height;
1616 win1->w_prev->w_status_height = win1->w_status_height;
1617 win1->w_status_height = height;
1618#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001619 if (win1->w_prev->w_vsep_width == 1)
1620 {
1621 /* Remove the vertical separator from the last-but-one window,
1622 * add it to the last window. Adjust the frame widths. */
1623 win1->w_prev->w_vsep_width = 0;
1624 win1->w_prev->w_frame->fr_width -= 1;
1625 win1->w_vsep_width = 1;
1626 win1->w_frame->fr_width += 1;
1627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629 }
1630 else if (win2 == lastwin)
1631 {
1632 height = win1->w_status_height;
1633 win1->w_status_height = win2->w_status_height;
1634 win2->w_status_height = height;
1635#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001636 if (win1->w_vsep_width == 1)
1637 {
1638 /* Remove the vertical separator from win1, add it to the last
1639 * window, win2. Adjust the frame widths. */
1640 win2->w_vsep_width = 1;
1641 win2->w_frame->fr_width += 1;
1642 win1->w_vsep_width = 0;
1643 win1->w_frame->fr_width -= 1;
1644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
1646 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001647 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 frame_remove(win1->w_frame);
1649 win_append(win2, win1);
1650 frame_append(win2->w_frame, win1->w_frame);
1651
1652 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1653 redraw_later(NOT_VALID);
1654 }
1655 win_enter(win1, FALSE);
1656}
1657
1658/*
1659 * Make all windows the same height.
1660 * 'next_curwin' will soon be the current window, make sure it has enough
1661 * rows.
1662 */
1663 void
1664win_equal(next_curwin, current, dir)
1665 win_T *next_curwin; /* pointer to current window to be or NULL */
1666 int current; /* do only frame with current window */
1667 int dir; /* 'v' for vertically, 'h' for horizontally,
1668 'b' for both, 0 for using p_ead */
1669{
1670 if (dir == 0)
1671#ifdef FEAT_VERTSPLIT
1672 dir = *p_ead;
1673#else
1674 dir = 'b';
1675#endif
1676 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001677 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001678 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679}
1680
1681/*
1682 * Set a frame to a new position and height, spreading the available room
1683 * equally over contained frames.
1684 * The window "next_curwin" (if not NULL) should at least get the size from
1685 * 'winheight' and 'winwidth' if possible.
1686 */
1687 static void
1688win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1689 win_T *next_curwin; /* pointer to current window to be or NULL */
1690 int current; /* do only frame with current window */
1691 frame_T *topfr; /* frame to set size off */
1692 int dir; /* 'v', 'h' or 'b', see win_equal() */
1693 int col; /* horizontal position for frame */
1694 int row; /* vertical position for frame */
1695 int width; /* new width of frame */
1696 int height; /* new height of frame */
1697{
1698 int n, m;
1699 int extra_sep = 0;
1700 int wincount, totwincount = 0;
1701 frame_T *fr;
1702 int next_curwin_size = 0;
1703 int room = 0;
1704 int new_size;
1705 int has_next_curwin = 0;
1706 int hnc;
1707
1708 if (topfr->fr_layout == FR_LEAF)
1709 {
1710 /* Set the width/height of this frame.
1711 * Redraw when size or position changes */
1712 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1713#ifdef FEAT_VERTSPLIT
1714 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1715#endif
1716 )
1717 {
1718 topfr->fr_win->w_winrow = row;
1719 frame_new_height(topfr, height, FALSE, FALSE);
1720#ifdef FEAT_VERTSPLIT
1721 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001722 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#endif
1724 redraw_all_later(CLEAR);
1725 }
1726 }
1727#ifdef FEAT_VERTSPLIT
1728 else if (topfr->fr_layout == FR_ROW)
1729 {
1730 topfr->fr_width = width;
1731 topfr->fr_height = height;
1732
1733 if (dir != 'v') /* equalize frame widths */
1734 {
1735 /* Compute the maximum number of windows horizontally in this
1736 * frame. */
1737 n = frame_minwidth(topfr, NOWIN);
1738 /* add one for the rightmost window, it doesn't have a separator */
1739 if (col + width == Columns)
1740 extra_sep = 1;
1741 else
1742 extra_sep = 0;
1743 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001744 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001746 /*
1747 * Compute width for "next_curwin" window and room available for
1748 * other windows.
1749 * "m" is the minimal width when counting p_wiw for "next_curwin".
1750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 m = frame_minwidth(topfr, next_curwin);
1752 room = width - m;
1753 if (room < 0)
1754 {
1755 next_curwin_size = p_wiw + room;
1756 room = 0;
1757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 else
1759 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001760 next_curwin_size = -1;
1761 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1762 {
1763 /* If 'winfixwidth' set keep the window width if
1764 * possible.
1765 * Watch out for this window being the next_curwin. */
1766 if (frame_fixed_width(fr))
1767 {
1768 n = frame_minwidth(fr, NOWIN);
1769 new_size = fr->fr_width;
1770 if (frame_has_win(fr, next_curwin))
1771 {
1772 room += p_wiw - p_wmw;
1773 next_curwin_size = 0;
1774 if (new_size < p_wiw)
1775 new_size = p_wiw;
1776 }
1777 else
1778 /* These windows don't use up room. */
1779 totwincount -= (n + (fr->fr_next == NULL
1780 ? extra_sep : 0)) / (p_wmw + 1);
1781 room -= new_size - n;
1782 if (room < 0)
1783 {
1784 new_size += room;
1785 room = 0;
1786 }
1787 fr->fr_newwidth = new_size;
1788 }
1789 }
1790 if (next_curwin_size == -1)
1791 {
1792 if (!has_next_curwin)
1793 next_curwin_size = 0;
1794 else if (totwincount > 1
1795 && (room + (totwincount - 2))
1796 / (totwincount - 1) > p_wiw)
1797 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001798 /* Can make all windows wider than 'winwidth', spread
1799 * the room equally. */
1800 next_curwin_size = (room + p_wiw
1801 + (totwincount - 1) * p_wmw
1802 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001803 room -= next_curwin_size - p_wiw;
1804 }
1805 else
1806 next_curwin_size = p_wiw;
1807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001809
1810 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 --totwincount; /* don't count curwin */
1812 }
1813
1814 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1815 {
1816 n = m = 0;
1817 wincount = 1;
1818 if (fr->fr_next == NULL)
1819 /* last frame gets all that remains (avoid roundoff error) */
1820 new_size = width;
1821 else if (dir == 'v')
1822 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001823 else if (frame_fixed_width(fr))
1824 {
1825 new_size = fr->fr_newwidth;
1826 wincount = 0; /* doesn't count as a sizeable window */
1827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 else
1829 {
1830 /* Compute the maximum number of windows horiz. in "fr". */
1831 n = frame_minwidth(fr, NOWIN);
1832 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1833 / (p_wmw + 1);
1834 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 if (has_next_curwin)
1836 hnc = frame_has_win(fr, next_curwin);
1837 else
1838 hnc = FALSE;
1839 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001841 if (totwincount == 0)
1842 new_size = room;
1843 else
1844 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001846 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 next_curwin_size -= p_wiw - (m - n);
1849 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001852 else
1853 room -= new_size;
1854 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001857 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 * window, unless equalizing all frames. */
1859 if (!current || dir != 'v' || topfr->fr_parent != NULL
1860 || (new_size != fr->fr_width)
1861 || frame_has_win(fr, next_curwin))
1862 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001863 new_size, height);
1864 col += new_size;
1865 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 totwincount -= wincount;
1867 }
1868 }
1869#endif
1870 else /* topfr->fr_layout == FR_COL */
1871 {
1872#ifdef FEAT_VERTSPLIT
1873 topfr->fr_width = width;
1874#endif
1875 topfr->fr_height = height;
1876
1877 if (dir != 'h') /* equalize frame heights */
1878 {
1879 /* Compute maximum number of windows vertically in this frame. */
1880 n = frame_minheight(topfr, NOWIN);
1881 /* add one for the bottom window if it doesn't have a statusline */
1882 if (row + height == cmdline_row && p_ls == 0)
1883 extra_sep = 1;
1884 else
1885 extra_sep = 0;
1886 totwincount = (n + extra_sep) / (p_wmh + 1);
1887 has_next_curwin = frame_has_win(topfr, next_curwin);
1888
1889 /*
1890 * Compute height for "next_curwin" window and room available for
1891 * other windows.
1892 * "m" is the minimal height when counting p_wh for "next_curwin".
1893 */
1894 m = frame_minheight(topfr, next_curwin);
1895 room = height - m;
1896 if (room < 0)
1897 {
1898 /* The room is less then 'winheight', use all space for the
1899 * current window. */
1900 next_curwin_size = p_wh + room;
1901 room = 0;
1902 }
1903 else
1904 {
1905 next_curwin_size = -1;
1906 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1907 {
1908 /* If 'winfixheight' set keep the window height if
1909 * possible.
1910 * Watch out for this window being the next_curwin. */
1911 if (frame_fixed_height(fr))
1912 {
1913 n = frame_minheight(fr, NOWIN);
1914 new_size = fr->fr_height;
1915 if (frame_has_win(fr, next_curwin))
1916 {
1917 room += p_wh - p_wmh;
1918 next_curwin_size = 0;
1919 if (new_size < p_wh)
1920 new_size = p_wh;
1921 }
1922 else
1923 /* These windows don't use up room. */
1924 totwincount -= (n + (fr->fr_next == NULL
1925 ? extra_sep : 0)) / (p_wmh + 1);
1926 room -= new_size - n;
1927 if (room < 0)
1928 {
1929 new_size += room;
1930 room = 0;
1931 }
1932 fr->fr_newheight = new_size;
1933 }
1934 }
1935 if (next_curwin_size == -1)
1936 {
1937 if (!has_next_curwin)
1938 next_curwin_size = 0;
1939 else if (totwincount > 1
1940 && (room + (totwincount - 2))
1941 / (totwincount - 1) > p_wh)
1942 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001943 /* can make all windows higher than 'winheight',
1944 * spread the room equally. */
1945 next_curwin_size = (room + p_wh
1946 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 + (totwincount - 1)) / totwincount;
1948 room -= next_curwin_size - p_wh;
1949 }
1950 else
1951 next_curwin_size = p_wh;
1952 }
1953 }
1954
1955 if (has_next_curwin)
1956 --totwincount; /* don't count curwin */
1957 }
1958
1959 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1960 {
1961 n = m = 0;
1962 wincount = 1;
1963 if (fr->fr_next == NULL)
1964 /* last frame gets all that remains (avoid roundoff error) */
1965 new_size = height;
1966 else if (dir == 'h')
1967 new_size = fr->fr_height;
1968 else if (frame_fixed_height(fr))
1969 {
1970 new_size = fr->fr_newheight;
1971 wincount = 0; /* doesn't count as a sizeable window */
1972 }
1973 else
1974 {
1975 /* Compute the maximum number of windows vert. in "fr". */
1976 n = frame_minheight(fr, NOWIN);
1977 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1978 / (p_wmh + 1);
1979 m = frame_minheight(fr, next_curwin);
1980 if (has_next_curwin)
1981 hnc = frame_has_win(fr, next_curwin);
1982 else
1983 hnc = FALSE;
1984 if (hnc) /* don't count next_curwin */
1985 --wincount;
1986 if (totwincount == 0)
1987 new_size = room;
1988 else
1989 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1990 / totwincount;
1991 if (hnc) /* add next_curwin size */
1992 {
1993 next_curwin_size -= p_wh - (m - n);
1994 new_size += next_curwin_size;
1995 room -= new_size - next_curwin_size;
1996 }
1997 else
1998 room -= new_size;
1999 new_size += n;
2000 }
2001 /* Skip frame that is full width when splitting or closing a
2002 * window, unless equalizing all frames. */
2003 if (!current || dir != 'h' || topfr->fr_parent != NULL
2004 || (new_size != fr->fr_height)
2005 || frame_has_win(fr, next_curwin))
2006 win_equal_rec(next_curwin, current, fr, dir, col, row,
2007 width, new_size);
2008 row += new_size;
2009 height -= new_size;
2010 totwincount -= wincount;
2011 }
2012 }
2013}
2014
2015/*
2016 * close all windows for buffer 'buf'
2017 */
2018 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002019close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002021 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002023 win_T *wp;
2024 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002025 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002028
2029 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002031 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2032#ifdef FEAT_AUTOCMD
2033 && !(wp->w_closing || wp->w_buffer->b_closing)
2034#endif
2035 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002037 win_close(wp, FALSE);
2038
2039 /* Start all over, autocommands may change the window layout. */
2040 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
2042 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002043 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002045
2046 /* Also check windows in other tab pages. */
2047 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2048 {
2049 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002050 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002051 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002052 if (wp->w_buffer == buf
2053#ifdef FEAT_AUTOCMD
2054 && !(wp->w_closing || wp->w_buffer->b_closing)
2055#endif
2056 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002057 {
2058 win_close_othertab(wp, FALSE, tp);
2059
2060 /* Start all over, the tab page may be closed and
2061 * autocommands may change the window layout. */
2062 nexttp = first_tabpage;
2063 break;
2064 }
2065 }
2066
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002068
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002069 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002070 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002071 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072}
2073
2074/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002075 * Return TRUE if the current window is the only window that exists (ignoring
2076 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002077 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002078 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002079 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002080last_window()
2081{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002082 return (one_window() && first_tabpage->tp_next == NULL);
2083}
2084
2085/*
2086 * Return TRUE if there is only one window other than "aucmd_win" in the
2087 * current tab page.
2088 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002089 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002090one_window()
2091{
2092#ifdef FEAT_AUTOCMD
2093 win_T *wp;
2094 int seen_one = FALSE;
2095
2096 FOR_ALL_WINDOWS(wp)
2097 {
2098 if (wp != aucmd_win)
2099 {
2100 if (seen_one)
2101 return FALSE;
2102 seen_one = TRUE;
2103 }
2104 }
2105 return TRUE;
2106#else
2107 return firstwin == lastwin;
2108#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002109}
2110
2111/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002112 * Close the possibly last window in a tab page.
2113 * Returns TRUE when the window was closed already.
2114 */
2115 static int
2116close_last_window_tabpage(win, free_buf, prev_curtab)
2117 win_T *win;
2118 int free_buf;
2119 tabpage_T *prev_curtab;
2120{
2121 if (firstwin == lastwin)
2122 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002123#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002124 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002125#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002126
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002127 /*
2128 * Closing the last window in a tab page. First go to another tab
2129 * page and then close the window and the tab page. This avoids that
2130 * curwin and curtab are invalid while we are freeing memory, they may
2131 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002132 * Don't trigger autocommands yet, they may use wrong values, so do
2133 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002134 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002135 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002136 redraw_tabline = TRUE;
2137
2138 /* Safety check: Autocommands may have closed the window when jumping
2139 * to the other tab page. */
2140 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2141 {
2142 int h = tabline_height();
2143
2144 win_close_othertab(win, free_buf, prev_curtab);
2145 if (h != tabline_height())
2146 shell_new_rows();
2147 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002148 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2149 * that now. */
2150#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002151 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002152 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2153 if (old_curbuf != curbuf)
2154 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002155#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002156 return TRUE;
2157 }
2158 return FALSE;
2159}
2160
2161/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002162 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 * If "free_buf" is TRUE related buffer may be unloaded.
2164 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002165 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002166 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002168 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169win_close(win, free_buf)
2170 win_T *win;
2171 int free_buf;
2172{
2173 win_T *wp;
2174#ifdef FEAT_AUTOCMD
2175 int other_buffer = FALSE;
2176#endif
2177 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 int dir;
2179 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002180 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002182 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
2184 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002185 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002188#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002189 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002190 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002191 if (win == aucmd_win)
2192 {
2193 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002194 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002195 }
2196 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2197 {
2198 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002199 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002200 }
2201#endif
2202
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002203 /* When closing the last window in a tab page first go to another tab page
2204 * and then close the window and the tab page to avoid that curwin and
2205 * curtab are invalid while we are freeing memory. */
2206 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002207 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002208
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 /* When closing the help window, try restoring a snapshot after closing
2210 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002211 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 help_window = TRUE;
2213 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002214 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
2216#ifdef FEAT_AUTOCMD
2217 if (win == curwin)
2218 {
2219 /*
2220 * Guess which window is going to be the new current window.
2221 * This may change because of the autocommands (sigh).
2222 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002223 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002226 * Be careful: If autocommands delete the window or cause this window
2227 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 */
2229 if (wp->w_buffer != curbuf)
2230 {
2231 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002232 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002234 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002235 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002236 win->w_closing = FALSE;
2237 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002238 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002240 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002242 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002243 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002244 win->w_closing = FALSE;
2245 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247# ifdef FEAT_EVAL
2248 /* autocmds may abort script processing */
2249 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002250 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251# endif
2252 }
2253#endif
2254
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002255#ifdef FEAT_GUI
2256 /* Avoid trouble with scrollbars that are going to be deleted in
2257 * win_free(). */
2258 if (gui.in_use)
2259 out_flush();
2260#endif
2261
Bram Moolenaara971b822011-09-14 14:43:25 +02002262#ifdef FEAT_SYN_HL
2263 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002264 if (win->w_buffer != NULL)
2265 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002266#endif
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 /*
2269 * Close the link to the buffer.
2270 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002271 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002272 {
2273#ifdef FEAT_AUTOCMD
2274 win->w_closing = TRUE;
2275#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002276 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002277#ifdef FEAT_AUTOCMD
2278 if (win_valid(win))
2279 win->w_closing = FALSE;
2280#endif
2281 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002282
Bram Moolenaar802418d2013-01-17 14:00:11 +01002283 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2284 && (last_window() || curtab != prev_curtab
2285 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002286 {
2287 /* Autocommands have close all windows, quit now. Restore
2288 * curwin->w_buffer, otherwise writing viminfo may fail. */
2289 if (curwin->w_buffer == NULL)
2290 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002291 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002292 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002293
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002295 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002296 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002297 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
Bram Moolenaara971b822011-09-14 14:43:25 +02002300 /* Free the memory used for the window and get the window that received
2301 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002302 wp = win_free_mem(win, &dir, NULL);
2303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 /* Make sure curwin isn't invalid. It can cause severe trouble when
2305 * printing an error message. For win_equal() curbuf needs to be valid
2306 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002307 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
2309 curwin = wp;
2310#ifdef FEAT_QUICKFIX
2311 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2312 {
2313 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002314 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 * finding another window to go to.
2316 */
2317 for (;;)
2318 {
2319 if (wp->w_next == NULL)
2320 wp = firstwin;
2321 else
2322 wp = wp->w_next;
2323 if (wp == curwin)
2324 break;
2325 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2326 {
2327 curwin = wp;
2328 break;
2329 }
2330 }
2331 }
2332#endif
2333 curbuf = curwin->w_buffer;
2334 close_curwin = TRUE;
2335 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002336 if (p_ea
2337#ifdef FEAT_VERTSPLIT
2338 && (*p_ead == 'b' || *p_ead == dir)
2339#endif
2340 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 win_equal(curwin, TRUE,
2342#ifdef FEAT_VERTSPLIT
2343 dir
2344#else
2345 0
2346#endif
2347 );
2348 else
2349 win_comp_pos();
2350 if (close_curwin)
2351 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002352 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353#ifdef FEAT_AUTOCMD
2354 if (other_buffer)
2355 /* careful: after this wp and win may be invalid! */
2356 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2357#endif
2358 }
2359
2360 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002361 * If last window has a status line now and we don't want one,
2362 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 */
2364 last_status(FALSE);
2365
2366 /* After closing the help window, try restoring the window layout from
2367 * before it was opened. */
2368 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002369 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2372 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2373 if (gui.in_use && !win_hasvertsplit())
2374 gui_init_which_components(NULL);
2375#endif
2376
2377 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002378 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379}
2380
2381/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002382 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002383 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002384 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002385 * Caller must check if buffer is hidden and whether the tabline needs to be
2386 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002387 */
2388 void
2389win_close_othertab(win, free_buf, tp)
2390 win_T *win;
2391 int free_buf;
2392 tabpage_T *tp;
2393{
2394 win_T *wp;
2395 int dir;
2396 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002397 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002398
Bram Moolenaar362ce482012-06-06 19:02:45 +02002399#ifdef FEAT_AUTOCMD
2400 if (win->w_closing || win->w_buffer->b_closing)
2401 return; /* window is already being closed */
2402#endif
2403
Bram Moolenaarf740b292006-02-16 22:11:02 +00002404 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002405 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002406
2407 /* Careful: Autocommands may have closed the tab page or made it the
2408 * current tab page. */
2409 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2410 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002411 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002412 return;
2413
2414 /* Autocommands may have closed the window already. */
2415 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2416 ;
2417 if (wp == NULL)
2418 return;
2419
Bram Moolenaarf740b292006-02-16 22:11:02 +00002420 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002421 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002422 {
2423 if (tp == first_tabpage)
2424 first_tabpage = tp->tp_next;
2425 else
2426 {
2427 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2428 ptp = ptp->tp_next)
2429 ;
2430 if (ptp == NULL)
2431 {
2432 EMSG2(_(e_intern2), "win_close_othertab()");
2433 return;
2434 }
2435 ptp->tp_next = tp->tp_next;
2436 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002437 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002438 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002439
2440 /* Free the memory used for the window. */
2441 win_free_mem(win, &dir, tp);
2442
2443 if (free_tp)
2444 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002445}
2446
2447/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002448 * Free the memory used for a window.
2449 * Returns a pointer to the window that got the freed up space.
2450 */
2451 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002452win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002453 win_T *win;
2454 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002455 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002456{
2457 frame_T *frp;
2458 win_T *wp;
2459
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002460 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002461 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002462 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002463 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002464 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002465
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002466 /* When deleting the current window of another tab page select a new
2467 * current window. */
2468 if (tp != NULL && win == tp->tp_curwin)
2469 tp->tp_curwin = wp;
2470
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002471 return wp;
2472}
2473
2474#if defined(EXITFREE) || defined(PROTO)
2475 void
2476win_free_all()
2477{
2478 int dummy;
2479
Bram Moolenaarf740b292006-02-16 22:11:02 +00002480# ifdef FEAT_WINDOWS
2481 while (first_tabpage->tp_next != NULL)
2482 tabpage_close(TRUE);
2483# endif
2484
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002485# ifdef FEAT_AUTOCMD
2486 if (aucmd_win != NULL)
2487 {
2488 (void)win_free_mem(aucmd_win, &dummy, NULL);
2489 aucmd_win = NULL;
2490 }
2491# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002492
2493 while (firstwin != NULL)
2494 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002495
2496 /* No window should be used after this. Set curwin to NULL to crash
2497 * instead of using freed memory. */
2498 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002499}
2500#endif
2501
2502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * Remove a window and its frame from the tree of frames.
2504 * Returns a pointer to the window that got the freed up space.
2505 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002506 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002507winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002509 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002510 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
2512 frame_T *frp, *frp2, *frp3;
2513 frame_T *frp_close = win->w_frame;
2514 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002517 * If there is only one window there is nothing to remove.
2518 */
2519 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2520 return NULL;
2521
2522 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 * Remove the window from its frame.
2524 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002525 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 wp = frame2win(frp2);
2527
2528 /* Remove this frame from the list of frames. */
2529 frame_remove(frp_close);
2530
2531#ifdef FEAT_VERTSPLIT
2532 if (frp_close->fr_parent->fr_layout == FR_COL)
2533 {
2534#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002535 /* When 'winfixheight' is set, try to find another frame in the column
2536 * (as close to the closed frame as possible) to distribute the height
2537 * to. */
2538 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2539 {
2540 frp = frp_close->fr_prev;
2541 frp3 = frp_close->fr_next;
2542 while (frp != NULL || frp3 != NULL)
2543 {
2544 if (frp != NULL)
2545 {
2546 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2547 {
2548 frp2 = frp;
2549 wp = frp->fr_win;
2550 break;
2551 }
2552 frp = frp->fr_prev;
2553 }
2554 if (frp3 != NULL)
2555 {
2556 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2557 {
2558 frp2 = frp3;
2559 wp = frp3->fr_win;
2560 break;
2561 }
2562 frp3 = frp3->fr_next;
2563 }
2564 }
2565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2567 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568#ifdef FEAT_VERTSPLIT
2569 *dirp = 'v';
2570 }
2571 else
2572 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002573 /* When 'winfixwidth' is set, try to find another frame in the column
2574 * (as close to the closed frame as possible) to distribute the width
2575 * to. */
2576 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2577 {
2578 frp = frp_close->fr_prev;
2579 frp3 = frp_close->fr_next;
2580 while (frp != NULL || frp3 != NULL)
2581 {
2582 if (frp != NULL)
2583 {
2584 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2585 {
2586 frp2 = frp;
2587 wp = frp->fr_win;
2588 break;
2589 }
2590 frp = frp->fr_prev;
2591 }
2592 if (frp3 != NULL)
2593 {
2594 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2595 {
2596 frp2 = frp3;
2597 wp = frp3->fr_win;
2598 break;
2599 }
2600 frp3 = frp3->fr_next;
2601 }
2602 }
2603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002605 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 *dirp = 'h';
2607 }
2608#endif
2609
2610 /* If rows/columns go to a window below/right its positions need to be
2611 * updated. Can only be done after the sizes have been updated. */
2612 if (frp2 == frp_close->fr_next)
2613 {
2614 int row = win->w_winrow;
2615 int col = W_WINCOL(win);
2616
2617 frame_comp_pos(frp2, &row, &col);
2618 }
2619
2620 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2621 {
2622 /* There is no other frame in this list, move its info to the parent
2623 * and remove it. */
2624 frp2->fr_parent->fr_layout = frp2->fr_layout;
2625 frp2->fr_parent->fr_child = frp2->fr_child;
2626 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2627 frp->fr_parent = frp2->fr_parent;
2628 frp2->fr_parent->fr_win = frp2->fr_win;
2629 if (frp2->fr_win != NULL)
2630 frp2->fr_win->w_frame = frp2->fr_parent;
2631 frp = frp2->fr_parent;
2632 vim_free(frp2);
2633
2634 frp2 = frp->fr_parent;
2635 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2636 {
2637 /* The frame above the parent has the same layout, have to merge
2638 * the frames into this list. */
2639 if (frp2->fr_child == frp)
2640 frp2->fr_child = frp->fr_child;
2641 frp->fr_child->fr_prev = frp->fr_prev;
2642 if (frp->fr_prev != NULL)
2643 frp->fr_prev->fr_next = frp->fr_child;
2644 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2645 {
2646 frp3->fr_parent = frp2;
2647 if (frp3->fr_next == NULL)
2648 {
2649 frp3->fr_next = frp->fr_next;
2650 if (frp->fr_next != NULL)
2651 frp->fr_next->fr_prev = frp3;
2652 break;
2653 }
2654 }
2655 vim_free(frp);
2656 }
2657 }
2658
2659 return wp;
2660}
2661
2662/*
2663 * Find out which frame is going to get the freed up space when "win" is
2664 * closed.
2665 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2666 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2667 * This makes opening a window and closing it immediately keep the same window
2668 * layout.
2669 */
2670 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002671win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002673 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 frame_T *frp;
2676 int b;
2677
Bram Moolenaarf740b292006-02-16 22:11:02 +00002678 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002679 /* Last window in this tab page, will go to next tab page. */
2680 return alt_tabpage()->tp_curwin->w_frame;
2681
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 frp = win->w_frame;
2683#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002684 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 b = p_spr;
2686 else
2687#endif
2688 b = p_sb;
2689 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2690 return frp->fr_next;
2691 return frp->fr_prev;
2692}
2693
2694/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002695 * Return the tabpage that will be used if the current one is closed.
2696 */
2697 static tabpage_T *
2698alt_tabpage()
2699{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002700 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002701
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002702 /* Use the next tab page if possible. */
2703 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002704 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002705
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002706 /* Find the last but one tab page. */
2707 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2708 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002709 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002710}
2711
2712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 * Find the left-upper window in frame "frp".
2714 */
2715 static win_T *
2716frame2win(frp)
2717 frame_T *frp;
2718{
2719 while (frp->fr_win == NULL)
2720 frp = frp->fr_child;
2721 return frp->fr_win;
2722}
2723
2724/*
2725 * Return TRUE if frame "frp" contains window "wp".
2726 */
2727 static int
2728frame_has_win(frp, wp)
2729 frame_T *frp;
2730 win_T *wp;
2731{
2732 frame_T *p;
2733
2734 if (frp->fr_layout == FR_LEAF)
2735 return frp->fr_win == wp;
2736
2737 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2738 if (frame_has_win(p, wp))
2739 return TRUE;
2740 return FALSE;
2741}
2742
2743/*
2744 * Set a new height for a frame. Recursively sets the height for contained
2745 * frames and windows. Caller must take care of positions.
2746 */
2747 static void
2748frame_new_height(topfrp, height, topfirst, wfh)
2749 frame_T *topfrp;
2750 int height;
2751 int topfirst; /* resize topmost contained frame first */
2752 int wfh; /* obey 'winfixheight' when there is a choice;
2753 may cause the height not to be set */
2754{
2755 frame_T *frp;
2756 int extra_lines;
2757 int h;
2758
2759 if (topfrp->fr_win != NULL)
2760 {
2761 /* Simple case: just one window. */
2762 win_new_height(topfrp->fr_win,
2763 height - topfrp->fr_win->w_status_height);
2764 }
2765#ifdef FEAT_VERTSPLIT
2766 else if (topfrp->fr_layout == FR_ROW)
2767 {
2768 do
2769 {
2770 /* All frames in this row get the same new height. */
2771 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2772 {
2773 frame_new_height(frp, height, topfirst, wfh);
2774 if (frp->fr_height > height)
2775 {
2776 /* Could not fit the windows, make the whole row higher. */
2777 height = frp->fr_height;
2778 break;
2779 }
2780 }
2781 }
2782 while (frp != NULL);
2783 }
2784#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002785 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 {
2787 /* Complicated case: Resize a column of frames. Resize the bottom
2788 * frame first, frames above that when needed. */
2789
2790 frp = topfrp->fr_child;
2791 if (wfh)
2792 /* Advance past frames with one window with 'wfh' set. */
2793 while (frame_fixed_height(frp))
2794 {
2795 frp = frp->fr_next;
2796 if (frp == NULL)
2797 return; /* no frame without 'wfh', give up */
2798 }
2799 if (!topfirst)
2800 {
2801 /* Find the bottom frame of this column */
2802 while (frp->fr_next != NULL)
2803 frp = frp->fr_next;
2804 if (wfh)
2805 /* Advance back for frames with one window with 'wfh' set. */
2806 while (frame_fixed_height(frp))
2807 frp = frp->fr_prev;
2808 }
2809
2810 extra_lines = height - topfrp->fr_height;
2811 if (extra_lines < 0)
2812 {
2813 /* reduce height of contained frames, bottom or top frame first */
2814 while (frp != NULL)
2815 {
2816 h = frame_minheight(frp, NULL);
2817 if (frp->fr_height + extra_lines < h)
2818 {
2819 extra_lines += frp->fr_height - h;
2820 frame_new_height(frp, h, topfirst, wfh);
2821 }
2822 else
2823 {
2824 frame_new_height(frp, frp->fr_height + extra_lines,
2825 topfirst, wfh);
2826 break;
2827 }
2828 if (topfirst)
2829 {
2830 do
2831 frp = frp->fr_next;
2832 while (wfh && frp != NULL && frame_fixed_height(frp));
2833 }
2834 else
2835 {
2836 do
2837 frp = frp->fr_prev;
2838 while (wfh && frp != NULL && frame_fixed_height(frp));
2839 }
2840 /* Increase "height" if we could not reduce enough frames. */
2841 if (frp == NULL)
2842 height -= extra_lines;
2843 }
2844 }
2845 else if (extra_lines > 0)
2846 {
2847 /* increase height of bottom or top frame */
2848 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2849 }
2850 }
2851 topfrp->fr_height = height;
2852}
2853
2854/*
2855 * Return TRUE if height of frame "frp" should not be changed because of
2856 * the 'winfixheight' option.
2857 */
2858 static int
2859frame_fixed_height(frp)
2860 frame_T *frp;
2861{
2862 /* frame with one window: fixed height if 'winfixheight' set. */
2863 if (frp->fr_win != NULL)
2864 return frp->fr_win->w_p_wfh;
2865
2866 if (frp->fr_layout == FR_ROW)
2867 {
2868 /* The frame is fixed height if one of the frames in the row is fixed
2869 * height. */
2870 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2871 if (frame_fixed_height(frp))
2872 return TRUE;
2873 return FALSE;
2874 }
2875
2876 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2877 * frames in the row are fixed height. */
2878 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2879 if (!frame_fixed_height(frp))
2880 return FALSE;
2881 return TRUE;
2882}
2883
2884#ifdef FEAT_VERTSPLIT
2885/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002886 * Return TRUE if width of frame "frp" should not be changed because of
2887 * the 'winfixwidth' option.
2888 */
2889 static int
2890frame_fixed_width(frp)
2891 frame_T *frp;
2892{
2893 /* frame with one window: fixed width if 'winfixwidth' set. */
2894 if (frp->fr_win != NULL)
2895 return frp->fr_win->w_p_wfw;
2896
2897 if (frp->fr_layout == FR_COL)
2898 {
2899 /* The frame is fixed width if one of the frames in the row is fixed
2900 * width. */
2901 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2902 if (frame_fixed_width(frp))
2903 return TRUE;
2904 return FALSE;
2905 }
2906
2907 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2908 * frames in the row are fixed width. */
2909 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2910 if (!frame_fixed_width(frp))
2911 return FALSE;
2912 return TRUE;
2913}
2914
2915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 * Add a status line to windows at the bottom of "frp".
2917 * Note: Does not check if there is room!
2918 */
2919 static void
2920frame_add_statusline(frp)
2921 frame_T *frp;
2922{
2923 win_T *wp;
2924
2925 if (frp->fr_layout == FR_LEAF)
2926 {
2927 wp = frp->fr_win;
2928 if (wp->w_status_height == 0)
2929 {
2930 if (wp->w_height > 0) /* don't make it negative */
2931 --wp->w_height;
2932 wp->w_status_height = STATUS_HEIGHT;
2933 }
2934 }
2935 else if (frp->fr_layout == FR_ROW)
2936 {
2937 /* Handle all the frames in the row. */
2938 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2939 frame_add_statusline(frp);
2940 }
2941 else /* frp->fr_layout == FR_COL */
2942 {
2943 /* Only need to handle the last frame in the column. */
2944 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2945 ;
2946 frame_add_statusline(frp);
2947 }
2948}
2949
2950/*
2951 * Set width of a frame. Handles recursively going through contained frames.
2952 * May remove separator line for windows at the right side (for win_close()).
2953 */
2954 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002955frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 frame_T *topfrp;
2957 int width;
2958 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002959 int wfw; /* obey 'winfixwidth' when there is a choice;
2960 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
2962 frame_T *frp;
2963 int extra_cols;
2964 int w;
2965 win_T *wp;
2966
2967 if (topfrp->fr_layout == FR_LEAF)
2968 {
2969 /* Simple case: just one window. */
2970 wp = topfrp->fr_win;
2971 /* Find out if there are any windows right of this one. */
2972 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2973 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2974 break;
2975 if (frp->fr_parent == NULL)
2976 wp->w_vsep_width = 0;
2977 win_new_width(wp, width - wp->w_vsep_width);
2978 }
2979 else if (topfrp->fr_layout == FR_COL)
2980 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002981 do
2982 {
2983 /* All frames in this column get the same new width. */
2984 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2985 {
2986 frame_new_width(frp, width, leftfirst, wfw);
2987 if (frp->fr_width > width)
2988 {
2989 /* Could not fit the windows, make whole column wider. */
2990 width = frp->fr_width;
2991 break;
2992 }
2993 }
2994 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 else /* fr_layout == FR_ROW */
2997 {
2998 /* Complicated case: Resize a row of frames. Resize the rightmost
2999 * frame first, frames left of it when needed. */
3000
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003002 if (wfw)
3003 /* Advance past frames with one window with 'wfw' set. */
3004 while (frame_fixed_width(frp))
3005 {
3006 frp = frp->fr_next;
3007 if (frp == NULL)
3008 return; /* no frame without 'wfw', give up */
3009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003011 {
3012 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 while (frp->fr_next != NULL)
3014 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003015 if (wfw)
3016 /* Advance back for frames with one window with 'wfw' set. */
3017 while (frame_fixed_width(frp))
3018 frp = frp->fr_prev;
3019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
3021 extra_cols = width - topfrp->fr_width;
3022 if (extra_cols < 0)
3023 {
3024 /* reduce frame width, rightmost frame first */
3025 while (frp != NULL)
3026 {
3027 w = frame_minwidth(frp, NULL);
3028 if (frp->fr_width + extra_cols < w)
3029 {
3030 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003031 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033 else
3034 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003035 frame_new_width(frp, frp->fr_width + extra_cols,
3036 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 break;
3038 }
3039 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003040 {
3041 do
3042 frp = frp->fr_next;
3043 while (wfw && frp != NULL && frame_fixed_width(frp));
3044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003046 {
3047 do
3048 frp = frp->fr_prev;
3049 while (wfw && frp != NULL && frame_fixed_width(frp));
3050 }
3051 /* Increase "width" if we could not reduce enough frames. */
3052 if (frp == NULL)
3053 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
3055 }
3056 else if (extra_cols > 0)
3057 {
3058 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003059 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
3061 }
3062 topfrp->fr_width = width;
3063}
3064
3065/*
3066 * Add the vertical separator to windows at the right side of "frp".
3067 * Note: Does not check if there is room!
3068 */
3069 static void
3070frame_add_vsep(frp)
3071 frame_T *frp;
3072{
3073 win_T *wp;
3074
3075 if (frp->fr_layout == FR_LEAF)
3076 {
3077 wp = frp->fr_win;
3078 if (wp->w_vsep_width == 0)
3079 {
3080 if (wp->w_width > 0) /* don't make it negative */
3081 --wp->w_width;
3082 wp->w_vsep_width = 1;
3083 }
3084 }
3085 else if (frp->fr_layout == FR_COL)
3086 {
3087 /* Handle all the frames in the column. */
3088 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3089 frame_add_vsep(frp);
3090 }
3091 else /* frp->fr_layout == FR_ROW */
3092 {
3093 /* Only need to handle the last frame in the row. */
3094 frp = frp->fr_child;
3095 while (frp->fr_next != NULL)
3096 frp = frp->fr_next;
3097 frame_add_vsep(frp);
3098 }
3099}
3100
3101/*
3102 * Set frame width from the window it contains.
3103 */
3104 static void
3105frame_fix_width(wp)
3106 win_T *wp;
3107{
3108 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3109}
3110#endif
3111
3112/*
3113 * Set frame height from the window it contains.
3114 */
3115 static void
3116frame_fix_height(wp)
3117 win_T *wp;
3118{
3119 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3120}
3121
3122/*
3123 * Compute the minimal height for frame "topfrp".
3124 * Uses the 'winminheight' option.
3125 * When "next_curwin" isn't NULL, use p_wh for this window.
3126 * When "next_curwin" is NOWIN, don't use at least one line for the current
3127 * window.
3128 */
3129 static int
3130frame_minheight(topfrp, next_curwin)
3131 frame_T *topfrp;
3132 win_T *next_curwin;
3133{
3134 frame_T *frp;
3135 int m;
3136#ifdef FEAT_VERTSPLIT
3137 int n;
3138#endif
3139
3140 if (topfrp->fr_win != NULL)
3141 {
3142 if (topfrp->fr_win == next_curwin)
3143 m = p_wh + topfrp->fr_win->w_status_height;
3144 else
3145 {
3146 /* window: minimal height of the window plus status line */
3147 m = p_wmh + topfrp->fr_win->w_status_height;
3148 /* Current window is minimal one line high */
3149 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3150 ++m;
3151 }
3152 }
3153#ifdef FEAT_VERTSPLIT
3154 else if (topfrp->fr_layout == FR_ROW)
3155 {
3156 /* get the minimal height from each frame in this row */
3157 m = 0;
3158 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3159 {
3160 n = frame_minheight(frp, next_curwin);
3161 if (n > m)
3162 m = n;
3163 }
3164 }
3165#endif
3166 else
3167 {
3168 /* Add up the minimal heights for all frames in this column. */
3169 m = 0;
3170 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3171 m += frame_minheight(frp, next_curwin);
3172 }
3173
3174 return m;
3175}
3176
3177#ifdef FEAT_VERTSPLIT
3178/*
3179 * Compute the minimal width for frame "topfrp".
3180 * When "next_curwin" isn't NULL, use p_wiw for this window.
3181 * When "next_curwin" is NOWIN, don't use at least one column for the current
3182 * window.
3183 */
3184 static int
3185frame_minwidth(topfrp, next_curwin)
3186 frame_T *topfrp;
3187 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3188{
3189 frame_T *frp;
3190 int m, n;
3191
3192 if (topfrp->fr_win != NULL)
3193 {
3194 if (topfrp->fr_win == next_curwin)
3195 m = p_wiw + topfrp->fr_win->w_vsep_width;
3196 else
3197 {
3198 /* window: minimal width of the window plus separator column */
3199 m = p_wmw + topfrp->fr_win->w_vsep_width;
3200 /* Current window is minimal one column wide */
3201 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3202 ++m;
3203 }
3204 }
3205 else if (topfrp->fr_layout == FR_COL)
3206 {
3207 /* get the minimal width from each frame in this column */
3208 m = 0;
3209 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3210 {
3211 n = frame_minwidth(frp, next_curwin);
3212 if (n > m)
3213 m = n;
3214 }
3215 }
3216 else
3217 {
3218 /* Add up the minimal widths for all frames in this row. */
3219 m = 0;
3220 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3221 m += frame_minwidth(frp, next_curwin);
3222 }
3223
3224 return m;
3225}
3226#endif
3227
3228
3229/*
3230 * Try to close all windows except current one.
3231 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3232 * used and the buffer was modified.
3233 *
3234 * Used by ":bdel" and ":only".
3235 */
3236 void
3237close_others(message, forceit)
3238 int message;
3239 int forceit; /* always hide all other windows */
3240{
3241 win_T *wp;
3242 win_T *nextwp;
3243 int r;
3244
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003245 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
3247 if (message
3248#ifdef FEAT_AUTOCMD
3249 && !autocmd_busy
3250#endif
3251 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003252 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 return;
3254 }
3255
3256 /* Be very careful here: autocommands may change the window layout. */
3257 for (wp = firstwin; win_valid(wp); wp = nextwp)
3258 {
3259 nextwp = wp->w_next;
3260 if (wp != curwin) /* don't close current window */
3261 {
3262
3263 /* Check if it's allowed to abandon this window */
3264 r = can_abandon(wp->w_buffer, forceit);
3265#ifdef FEAT_AUTOCMD
3266 if (!win_valid(wp)) /* autocommands messed wp up */
3267 {
3268 nextwp = firstwin;
3269 continue;
3270 }
3271#endif
3272 if (!r)
3273 {
3274#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3275 if (message && (p_confirm || cmdmod.confirm) && p_write)
3276 {
3277 dialog_changed(wp->w_buffer, FALSE);
3278# ifdef FEAT_AUTOCMD
3279 if (!win_valid(wp)) /* autocommands messed wp up */
3280 {
3281 nextwp = firstwin;
3282 continue;
3283 }
3284# endif
3285 }
3286 if (bufIsChanged(wp->w_buffer))
3287#endif
3288 continue;
3289 }
3290 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3291 }
3292 }
3293
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003294 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 EMSG(_("E445: Other window contains changes"));
3296}
3297
3298#endif /* FEAT_WINDOWS */
3299
3300/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003301 * Init the current window "curwin".
3302 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 */
3304 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003305curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003307 win_init_empty(curwin);
3308}
3309
3310 void
3311win_init_empty(wp)
3312 win_T *wp;
3313{
3314 redraw_win_later(wp, NOT_VALID);
3315 wp->w_lines_valid = 0;
3316 wp->w_cursor.lnum = 1;
3317 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003319 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003321 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3322 wp->w_pcmark.col = 0;
3323 wp->w_prev_pcmark.lnum = 0;
3324 wp->w_prev_pcmark.col = 0;
3325 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003327 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003329 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003331 if (wp->w_p_rl)
3332 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003334 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003336#ifdef FEAT_SYN_HL
3337 wp->w_s = &wp->w_buffer->b_s;
3338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339}
3340
3341/*
3342 * Allocate the first window and put an empty buffer in it.
3343 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003344 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003346 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347win_alloc_first()
3348{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003349 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003350 return FAIL;
3351
3352#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003353 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003354 if (first_tabpage == NULL)
3355 return FAIL;
3356 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003357 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003358#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003359
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003360 return OK;
3361}
3362
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003363#if defined(FEAT_AUTOCMD) || defined(PROTO)
3364/*
3365 * Init "aucmd_win". This can only be done after the first
3366 * window is fully initialized, thus it can't be in win_alloc_first().
3367 */
3368 void
3369win_alloc_aucmd_win()
3370{
3371 aucmd_win = win_alloc(NULL, TRUE);
3372 if (aucmd_win != NULL)
3373 {
3374 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003375 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003376 new_frame(aucmd_win);
3377 }
3378}
3379#endif
3380
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003381/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003382 * Allocate the first window or the first window in a new tab page.
3383 * When "oldwin" is NULL create an empty buffer for it.
3384 * When "oldwin" is not NULL copy info from it to the new window (only with
3385 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003386 * Return FAIL when something goes wrong (out of memory).
3387 */
3388 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003389win_alloc_firstwin(oldwin)
3390 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003391{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003392 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003393 if (oldwin == NULL)
3394 {
3395 /* Very first window, need to create an empty buffer for it and
3396 * initialize from scratch. */
3397 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3398 if (curwin == NULL || curbuf == NULL)
3399 return FAIL;
3400 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003401#ifdef FEAT_SYN_HL
3402 curwin->w_s = &(curbuf->b_s);
3403#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003404 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003406 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003408 curwin_init(); /* init current window */
3409 }
3410#ifdef FEAT_WINDOWS
3411 else
3412 {
3413 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003414 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003415
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003416 /* We don't want cursor- and scroll-binding in the first window. */
3417 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003418 }
3419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003421 new_frame(curwin);
3422 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003424 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#ifdef FEAT_VERTSPLIT
3426 topframe->fr_width = Columns;
3427#endif
3428 topframe->fr_height = Rows - p_ch;
3429 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003430
3431 return OK;
3432}
3433
3434/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003435 * Create a frame for window "wp".
3436 */
3437 static void
3438new_frame(win_T *wp)
3439{
3440 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3441
3442 wp->w_frame = frp;
3443 if (frp != NULL)
3444 {
3445 frp->fr_layout = FR_LEAF;
3446 frp->fr_win = wp;
3447 }
3448}
3449
3450/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003451 * Initialize the window and frame size to the maximum.
3452 */
3453 void
3454win_init_size()
3455{
3456 firstwin->w_height = ROWS_AVAIL;
3457 topframe->fr_height = ROWS_AVAIL;
3458#ifdef FEAT_VERTSPLIT
3459 firstwin->w_width = Columns;
3460 topframe->fr_width = Columns;
3461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462}
3463
3464#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003465
3466/*
3467 * Allocate a new tabpage_T and init the values.
3468 * Returns NULL when out of memory.
3469 */
3470 static tabpage_T *
3471alloc_tabpage()
3472{
3473 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003474# ifdef FEAT_GUI
3475 int i;
3476# endif
3477
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003478
3479 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003480 if (tp == NULL)
3481 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003482
Bram Moolenaar429fa852013-04-15 12:27:36 +02003483# ifdef FEAT_EVAL
3484 /* init t: variables */
3485 tp->tp_vars = dict_alloc();
3486 if (tp->tp_vars == NULL)
3487 {
3488 vim_free(tp);
3489 return NULL;
3490 }
3491 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3492# endif
3493
3494# ifdef FEAT_GUI
3495 for (i = 0; i < 3; i++)
3496 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003497# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003498# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003499 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003500# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003501 tp->tp_ch_used = p_ch;
3502
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003503 return tp;
3504}
3505
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003506 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003507free_tabpage(tp)
3508 tabpage_T *tp;
3509{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003510 int idx;
3511
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003512# ifdef FEAT_DIFF
3513 diff_clear(tp);
3514# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003515 for (idx = 0; idx < SNAP_COUNT; ++idx)
3516 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003517#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003518 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3519 hash_init(&tp->tp_vars->dv_hashtab);
3520 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003521#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003522
3523#ifdef FEAT_PYTHON
3524 python_tabpage_free(tp);
3525#endif
3526
3527#ifdef FEAT_PYTHON3
3528 python3_tabpage_free(tp);
3529#endif
3530
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003531 vim_free(tp);
3532}
3533
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003534/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003535 * Create a new Tab page with one window.
3536 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003537 * When "after" is 0 put it just after the current Tab page.
3538 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003539 * Return FAIL or OK.
3540 */
3541 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003542win_new_tabpage(after)
3543 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003544{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003545 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003546 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003547 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003548
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003549 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003550 if (newtp == NULL)
3551 return FAIL;
3552
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003553 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003554 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003555 {
3556 vim_free(newtp);
3557 return FAIL;
3558 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003559 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003560
3561 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003562 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003563 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003564 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003565 if (after == 1)
3566 {
3567 /* New tab page becomes the first one. */
3568 newtp->tp_next = first_tabpage;
3569 first_tabpage = newtp;
3570 }
3571 else
3572 {
3573 if (after > 0)
3574 {
3575 /* Put new tab page before tab page "after". */
3576 n = 2;
3577 for (tp = first_tabpage; tp->tp_next != NULL
3578 && n < after; tp = tp->tp_next)
3579 ++n;
3580 }
3581 newtp->tp_next = tp->tp_next;
3582 tp->tp_next = newtp;
3583 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003584 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003585 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003586 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003587
3588 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003589 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003590
3591#if defined(FEAT_GUI)
3592 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3593 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003594 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003595#endif
3596
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003597 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003598#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003599 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003600 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003601#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003602 return OK;
3603 }
3604
3605 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003606 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003607 return FAIL;
3608}
3609
3610/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003611 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3612 * like with ":split".
3613 * Returns OK if a new tab page was created, FAIL otherwise.
3614 */
3615 int
3616may_open_tabpage()
3617{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003618 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003619
Bram Moolenaard326ce82007-03-11 14:48:29 +00003620 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003621 {
3622 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003623 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003624 return win_new_tabpage(n);
3625 }
3626 return FAIL;
3627}
3628
3629/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003630 * Create up to "maxcount" tabpages with empty windows.
3631 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003632 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003633 int
3634make_tabpages(maxcount)
3635 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003636{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003637 int count = maxcount;
3638 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003640 /* Limit to 'tabpagemax' tabs. */
3641 if (count > p_tpm)
3642 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003643
3644#ifdef FEAT_AUTOCMD
3645 /*
3646 * Don't execute autocommands while creating the tab pages. Must do that
3647 * when putting the buffers in the windows.
3648 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003649 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003650#endif
3651
3652 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003653 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003654 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003655
3656#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003657 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003658#endif
3659
3660 /* return actual number of tab pages */
3661 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003662}
3663
3664/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003665 * Return TRUE when "tpc" points to a valid tab page.
3666 */
3667 int
3668valid_tabpage(tpc)
3669 tabpage_T *tpc;
3670{
3671 tabpage_T *tp;
3672
3673 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3674 if (tp == tpc)
3675 return TRUE;
3676 return FALSE;
3677}
3678
3679/*
3680 * Find tab page "n" (first one is 1). Returns NULL when not found.
3681 */
3682 tabpage_T *
3683find_tabpage(n)
3684 int n;
3685{
3686 tabpage_T *tp;
3687 int i = 1;
3688
3689 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3690 ++i;
3691 return tp;
3692}
3693
3694/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003695 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003696 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003697 */
3698 int
3699tabpage_index(ftp)
3700 tabpage_T *ftp;
3701{
3702 int i = 1;
3703 tabpage_T *tp;
3704
3705 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3706 ++i;
3707 return i;
3708}
3709
3710/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003711 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003712 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003713 * FAIL.
3714 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003715 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003716 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003717leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003718 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003719 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003720 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003721{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003722 tabpage_T *tp = curtab;
3723
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003724 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003725#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003726 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003727 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003728 if (new_curbuf != curbuf)
3729 {
3730 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3731 if (curtab != tp)
3732 return FAIL;
3733 }
3734 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3735 if (curtab != tp)
3736 return FAIL;
3737 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003738 if (curtab != tp)
3739 return FAIL;
3740 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003741#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003742#if defined(FEAT_GUI)
3743 /* Remove the scrollbars. They may be added back later. */
3744 if (gui.in_use)
3745 gui_remove_scrollbars();
3746#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003747 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003748 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749 tp->tp_firstwin = firstwin;
3750 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003751 tp->tp_old_Rows = Rows;
3752 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003753 firstwin = NULL;
3754 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003755 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003756}
3757
3758/*
3759 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003760 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003761 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3762 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003764 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003765enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003766 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003767 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003768 int trigger_enter_autocmds UNUSED;
3769 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003770{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003771 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003772 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003773
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003774 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003775 firstwin = tp->tp_firstwin;
3776 lastwin = tp->tp_lastwin;
3777 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003778
3779 /* We would like doing the TabEnter event first, but we don't have a
3780 * valid current window yet, which may break some commands.
3781 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003782 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3783 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003784 prevwin = next_prevwin;
3785
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003786 last_status(FALSE); /* status line may appear or disappear */
3787 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003788 must_redraw = CLEAR; /* need to redraw everything */
3789#ifdef FEAT_DIFF
3790 diff_need_scrollbind = TRUE;
3791#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003792
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003793 /* The tabpage line may have appeared or disappeared, may need to resize
3794 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003795 * frame sizes too. Use the stored value of p_ch, so that it can be
3796 * different for each tab page. */
3797 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003798 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3799#ifdef FEAT_GUI_TABLINE
3800 && !gui_use_tabline()
3801#endif
3802 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003803 shell_new_rows();
3804#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003805 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003806 shell_new_columns(); /* update window widths */
3807#endif
3808
3809#if defined(FEAT_GUI)
3810 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3811 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003812 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003813#endif
3814
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003815#ifdef FEAT_AUTOCMD
3816 /* Apply autocommands after updating the display, when 'rows' and
3817 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003818 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003819 {
3820 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3821 if (old_curbuf != curbuf)
3822 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3823 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003824#endif
3825
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003826 redraw_all_later(CLEAR);
3827}
3828
3829/*
3830 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003831 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003832 */
3833 void
3834goto_tabpage(n)
3835 int n;
3836{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003837 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003838 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003839 int i;
3840
Bram Moolenaard68071d2006-05-02 22:08:30 +00003841 if (text_locked())
3842 {
3843 /* Not allowed when editing the command line. */
3844#ifdef FEAT_CMDWIN
3845 if (cmdwin_type != 0)
3846 EMSG(_(e_cmdwin));
3847 else
3848#endif
3849 EMSG(_(e_secure));
3850 return;
3851 }
3852
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003853 /* If there is only one it can't work. */
3854 if (first_tabpage->tp_next == NULL)
3855 {
3856 if (n > 1)
3857 beep_flush();
3858 return;
3859 }
3860
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003861 if (n == 0)
3862 {
3863 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003864 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003865 tp = first_tabpage;
3866 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003867 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003868 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003869 else if (n < 0)
3870 {
3871 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3872 * this N times. */
3873 ttp = curtab;
3874 for (i = n; i < 0; ++i)
3875 {
3876 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3877 tp = tp->tp_next)
3878 ;
3879 ttp = tp;
3880 }
3881 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003882 else if (n == 9999)
3883 {
3884 /* Go to last tab page. */
3885 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3886 ;
3887 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003888 else
3889 {
3890 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003891 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003892 if (tp == NULL)
3893 {
3894 beep_flush();
3895 return;
3896 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003897 }
3898
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003899 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003900
3901#ifdef FEAT_GUI_TABLINE
3902 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003903 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003904#endif
3905}
3906
3907/*
3908 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003909 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3910 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003911 * Note: doesn't update the GUI tab.
3912 */
3913 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003914goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003915 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003916 int trigger_enter_autocmds;
3917 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003918{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003919 /* Don't repeat a message in another tab page. */
3920 set_keep_msg(NULL, 0);
3921
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003922 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3923 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003924 {
3925 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003926 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3927 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003928 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003929 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3930 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003931 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003932}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003935 * Enter window "wp" in tab page "tp".
3936 * Also updates the GUI tab.
3937 */
3938 void
3939goto_tabpage_win(tp, wp)
3940 tabpage_T *tp;
3941 win_T *wp;
3942{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003943 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003944 if (curtab == tp && win_valid(wp))
3945 {
3946 win_enter(wp, TRUE);
3947# ifdef FEAT_GUI_TABLINE
3948 if (gui_use_tabline())
3949 gui_mch_set_curtab(tabpage_index(curtab));
3950# endif
3951 }
3952}
3953
3954/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003955 * Move the current tab page to before tab page "nr".
3956 */
3957 void
3958tabpage_move(nr)
3959 int nr;
3960{
3961 int n = nr;
3962 tabpage_T *tp;
3963
3964 if (first_tabpage->tp_next == NULL)
3965 return;
3966
3967 /* Remove the current tab page from the list of tab pages. */
3968 if (curtab == first_tabpage)
3969 first_tabpage = curtab->tp_next;
3970 else
3971 {
3972 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3973 if (tp->tp_next == curtab)
3974 break;
3975 if (tp == NULL) /* "cannot happen" */
3976 return;
3977 tp->tp_next = curtab->tp_next;
3978 }
3979
3980 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003981 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003982 {
3983 curtab->tp_next = first_tabpage;
3984 first_tabpage = curtab;
3985 }
3986 else
3987 {
3988 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3989 --n;
3990 curtab->tp_next = tp->tp_next;
3991 tp->tp_next = curtab;
3992 }
3993
3994 /* Need to redraw the tabline. Tab page contents doesn't change. */
3995 redraw_tabline = TRUE;
3996}
3997
3998
3999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 * Go to another window.
4001 * When jumping to another buffer, stop Visual mode. Do this before
4002 * changing windows so we can yank the selection into the '*' register.
4003 * When jumping to another window on the same buffer, adjust its cursor
4004 * position to keep the same Visual area.
4005 */
4006 void
4007win_goto(wp)
4008 win_T *wp;
4009{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004010#ifdef FEAT_CONCEAL
4011 win_T *owp = curwin;
4012#endif
4013
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004014 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004017 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 return;
4019 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_AUTOCMD
4021 if (curbuf_locked())
4022 return;
4023#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004024
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 if (wp->w_buffer != curbuf)
4026 reset_VIsual_and_resel();
4027 else if (VIsual_active)
4028 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
4030#ifdef FEAT_GUI
4031 need_mouse_correct = TRUE;
4032#endif
4033 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004034
4035#ifdef FEAT_CONCEAL
4036 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004037 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004038 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004039 if (curwin->w_p_cole > 0 && !msg_scrolled)
4040 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042}
4043
4044#if defined(FEAT_PERL) || defined(PROTO)
4045/*
4046 * Find window number "winnr" (counting top to bottom).
4047 */
4048 win_T *
4049win_find_nr(winnr)
4050 int winnr;
4051{
4052 win_T *wp;
4053
4054# ifdef FEAT_WINDOWS
4055 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4056 if (--winnr == 0)
4057 break;
4058 return wp;
4059# else
4060 return curwin;
4061# endif
4062}
4063#endif
4064
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004065#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4066 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004067/*
4068 * Find the tabpage for window "win".
4069 */
4070 tabpage_T *
4071win_find_tabpage(win)
4072 win_T *win;
4073{
4074 win_T *wp;
4075 tabpage_T *tp;
4076
4077 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004078 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4079 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004080 if (wp == win)
4081 return tp;
4082 return NULL;
4083}
4084#endif
4085
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086#ifdef FEAT_VERTSPLIT
4087/*
4088 * Move to window above or below "count" times.
4089 */
4090 static void
4091win_goto_ver(up, count)
4092 int up; /* TRUE to go to win above */
4093 long count;
4094{
4095 frame_T *fr;
4096 frame_T *nfr;
4097 frame_T *foundfr;
4098
4099 foundfr = curwin->w_frame;
4100 while (count--)
4101 {
4102 /*
4103 * First go upwards in the tree of frames until we find a upwards or
4104 * downwards neighbor.
4105 */
4106 fr = foundfr;
4107 for (;;)
4108 {
4109 if (fr == topframe)
4110 goto end;
4111 if (up)
4112 nfr = fr->fr_prev;
4113 else
4114 nfr = fr->fr_next;
4115 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4116 break;
4117 fr = fr->fr_parent;
4118 }
4119
4120 /*
4121 * Now go downwards to find the bottom or top frame in it.
4122 */
4123 for (;;)
4124 {
4125 if (nfr->fr_layout == FR_LEAF)
4126 {
4127 foundfr = nfr;
4128 break;
4129 }
4130 fr = nfr->fr_child;
4131 if (nfr->fr_layout == FR_ROW)
4132 {
4133 /* Find the frame at the cursor row. */
4134 while (fr->fr_next != NULL
4135 && frame2win(fr)->w_wincol + fr->fr_width
4136 <= curwin->w_wincol + curwin->w_wcol)
4137 fr = fr->fr_next;
4138 }
4139 if (nfr->fr_layout == FR_COL && up)
4140 while (fr->fr_next != NULL)
4141 fr = fr->fr_next;
4142 nfr = fr;
4143 }
4144 }
4145end:
4146 if (foundfr != NULL)
4147 win_goto(foundfr->fr_win);
4148}
4149
4150/*
4151 * Move to left or right window.
4152 */
4153 static void
4154win_goto_hor(left, count)
4155 int left; /* TRUE to go to left win */
4156 long count;
4157{
4158 frame_T *fr;
4159 frame_T *nfr;
4160 frame_T *foundfr;
4161
4162 foundfr = curwin->w_frame;
4163 while (count--)
4164 {
4165 /*
4166 * First go upwards in the tree of frames until we find a left or
4167 * right neighbor.
4168 */
4169 fr = foundfr;
4170 for (;;)
4171 {
4172 if (fr == topframe)
4173 goto end;
4174 if (left)
4175 nfr = fr->fr_prev;
4176 else
4177 nfr = fr->fr_next;
4178 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4179 break;
4180 fr = fr->fr_parent;
4181 }
4182
4183 /*
4184 * Now go downwards to find the leftmost or rightmost frame in it.
4185 */
4186 for (;;)
4187 {
4188 if (nfr->fr_layout == FR_LEAF)
4189 {
4190 foundfr = nfr;
4191 break;
4192 }
4193 fr = nfr->fr_child;
4194 if (nfr->fr_layout == FR_COL)
4195 {
4196 /* Find the frame at the cursor row. */
4197 while (fr->fr_next != NULL
4198 && frame2win(fr)->w_winrow + fr->fr_height
4199 <= curwin->w_winrow + curwin->w_wrow)
4200 fr = fr->fr_next;
4201 }
4202 if (nfr->fr_layout == FR_ROW && left)
4203 while (fr->fr_next != NULL)
4204 fr = fr->fr_next;
4205 nfr = fr;
4206 }
4207 }
4208end:
4209 if (foundfr != NULL)
4210 win_goto(foundfr->fr_win);
4211}
4212#endif
4213
4214/*
4215 * Make window "wp" the current window.
4216 */
4217 void
4218win_enter(wp, undo_sync)
4219 win_T *wp;
4220 int undo_sync;
4221{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004222 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223}
4224
4225/*
4226 * Make window wp the current window.
4227 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4228 * been closed and isn't valid.
4229 */
4230 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004231win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 win_T *wp;
4233 int undo_sync;
4234 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004235 int trigger_enter_autocmds UNUSED;
4236 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238#ifdef FEAT_AUTOCMD
4239 int other_buffer = FALSE;
4240#endif
4241
4242 if (wp == curwin && !curwin_invalid) /* nothing to do */
4243 return;
4244
4245#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004246 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 /*
4249 * Be careful: If autocommands delete the window, return now.
4250 */
4251 if (wp->w_buffer != curbuf)
4252 {
4253 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4254 other_buffer = TRUE;
4255 if (!win_valid(wp))
4256 return;
4257 }
4258 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4259 if (!win_valid(wp))
4260 return;
4261# ifdef FEAT_EVAL
4262 /* autocmds may abort script processing */
4263 if (aborting())
4264 return;
4265# endif
4266 }
4267#endif
4268
4269 /* sync undo before leaving the current buffer */
4270 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004271 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004272
4273 /* Might need to scroll the old window before switching, e.g., when the
4274 * cursor was moved. */
4275 update_topline();
4276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 /* may have to copy the buffer options when 'cpo' contains 'S' */
4278 if (wp->w_buffer != curbuf)
4279 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4280 if (!curwin_invalid)
4281 {
4282 prevwin = curwin; /* remember for CTRL-W p */
4283 curwin->w_redr_status = TRUE;
4284 }
4285 curwin = wp;
4286 curbuf = wp->w_buffer;
4287 check_cursor();
4288#ifdef FEAT_VIRTUALEDIT
4289 if (!virtual_active())
4290 curwin->w_cursor.coladd = 0;
4291#endif
4292 changed_line_abv_curs(); /* assume cursor position needs updating */
4293
4294 if (curwin->w_localdir != NULL)
4295 {
4296 /* Window has a local directory: Save current directory as global
4297 * directory (unless that was done already) and change to the local
4298 * directory. */
4299 if (globaldir == NULL)
4300 {
4301 char_u cwd[MAXPATHL];
4302
4303 if (mch_dirname(cwd, MAXPATHL) == OK)
4304 globaldir = vim_strsave(cwd);
4305 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004306 if (mch_chdir((char *)curwin->w_localdir) == 0)
4307 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309 else if (globaldir != NULL)
4310 {
4311 /* Window doesn't have a local directory and we are not in the global
4312 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004313 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 vim_free(globaldir);
4315 globaldir = NULL;
4316 shorten_fnames(TRUE);
4317 }
4318
4319#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004320 if (trigger_enter_autocmds)
4321 {
4322 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4323 if (other_buffer)
4324 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326#endif
4327
4328#ifdef FEAT_TITLE
4329 maketitle();
4330#endif
4331 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004332 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 if (restart_edit)
4334 redraw_later(VALID); /* causes status line redraw */
4335
4336 /* set window height to desired minimal value */
4337 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4338 win_setheight((int)p_wh);
4339 else if (curwin->w_height == 0)
4340 win_setheight(1);
4341
4342#ifdef FEAT_VERTSPLIT
4343 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004344 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 win_setwidth((int)p_wiw);
4346#endif
4347
4348#ifdef FEAT_MOUSE
4349 setmouse(); /* in case jumped to/from help buffer */
4350#endif
4351
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004352 /* Change directories when the 'acd' option is set. */
4353 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354}
4355
4356#endif /* FEAT_WINDOWS */
4357
4358#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4359/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004360 * Jump to the first open window that contains buffer "buf", if one exists.
4361 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 */
4363 win_T *
4364buf_jump_open_win(buf)
4365 buf_T *buf;
4366{
4367# ifdef FEAT_WINDOWS
4368 win_T *wp;
4369
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004370 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 if (wp->w_buffer == buf)
4372 break;
4373 if (wp != NULL)
4374 win_enter(wp, FALSE);
4375 return wp;
4376# else
4377 if (curwin->w_buffer == buf)
4378 return curwin;
4379 return NULL;
4380# endif
4381}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004382
4383/*
4384 * Jump to the first open window in any tab page that contains buffer "buf",
4385 * if one exists.
4386 * Returns a pointer to the window found, otherwise NULL.
4387 */
4388 win_T *
4389buf_jump_open_tab(buf)
4390 buf_T *buf;
4391{
4392# ifdef FEAT_WINDOWS
4393 win_T *wp;
4394 tabpage_T *tp;
4395
4396 /* First try the current tab page. */
4397 wp = buf_jump_open_win(buf);
4398 if (wp != NULL)
4399 return wp;
4400
4401 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4402 if (tp != curtab)
4403 {
4404 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4405 if (wp->w_buffer == buf)
4406 break;
4407 if (wp != NULL)
4408 {
4409 goto_tabpage_win(tp, wp);
4410 if (curwin != wp)
4411 wp = NULL; /* something went wrong */
4412 break;
4413 }
4414 }
4415
4416 return wp;
4417# else
4418 if (curwin->w_buffer == buf)
4419 return curwin;
4420 return NULL;
4421# endif
4422}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423#endif
4424
4425/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004426 * Allocate a window structure and link it in the window list when "hidden" is
4427 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004430win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004431 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004432 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004434 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 /*
4437 * allocate window structure and linesizes arrays
4438 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004439 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004440 if (new_wp == NULL)
4441 return NULL;
4442
4443 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004445 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004446 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448
Bram Moolenaar429fa852013-04-15 12:27:36 +02004449#ifdef FEAT_EVAL
4450 /* init w: variables */
4451 new_wp->w_vars = dict_alloc();
4452 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004454 win_free_lsize(new_wp);
4455 vim_free(new_wp);
4456 return NULL;
4457 }
4458 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004459#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004460
4461#ifdef FEAT_AUTOCMD
4462 /* Don't execute autocommands while the window is not properly
4463 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4464 * event. */
4465 block_autocmds();
4466#endif
4467 /*
4468 * link the window in the window list
4469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004471 if (!hidden)
4472 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
4474#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004475 new_wp->w_wincol = 0;
4476 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477#endif
4478
Bram Moolenaar429fa852013-04-15 12:27:36 +02004479 /* position the display and the cursor at the top of the file. */
4480 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004482 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004484 new_wp->w_botline = 2;
4485 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004487 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488#endif
4489
Bram Moolenaar429fa852013-04-15 12:27:36 +02004490 /* We won't calculate w_fraction until resizing the window */
4491 new_wp->w_fraction = 0;
4492 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
4494#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004495 if (gui.in_use)
4496 {
4497 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4498 SBAR_LEFT, new_wp);
4499 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4500 SBAR_RIGHT, new_wp);
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502#endif
4503#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004504 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004506#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004507 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004508#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004509#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004510 new_wp->w_match_head = NULL;
4511 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004512#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004513 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514}
4515
4516#if defined(FEAT_WINDOWS) || defined(PROTO)
4517
4518/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004519 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 */
4521 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004522win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004524 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004527 buf_T *buf;
4528 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004530#ifdef FEAT_FOLDING
4531 clearFolding(wp);
4532#endif
4533
4534 /* reduce the reference count to the argument list. */
4535 alist_unlink(wp->w_alist);
4536
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004537#ifdef FEAT_AUTOCMD
4538 /* Don't execute autocommands while the window is halfway being deleted.
4539 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004540 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004541#endif
4542
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004543#ifdef FEAT_LUA
4544 lua_window_free(wp);
4545#endif
4546
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004547#ifdef FEAT_MZSCHEME
4548 mzscheme_window_free(wp);
4549#endif
4550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#ifdef FEAT_PERL
4552 perl_win_free(wp);
4553#endif
4554
4555#ifdef FEAT_PYTHON
4556 python_window_free(wp);
4557#endif
4558
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004559#ifdef FEAT_PYTHON3
4560 python3_window_free(wp);
4561#endif
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563#ifdef FEAT_TCL
4564 tcl_window_free(wp);
4565#endif
4566
4567#ifdef FEAT_RUBY
4568 ruby_window_free(wp);
4569#endif
4570
4571 clear_winopt(&wp->w_onebuf_opt);
4572 clear_winopt(&wp->w_allbuf_opt);
4573
4574#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004575 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4576 hash_init(&wp->w_vars->dv_hashtab);
4577 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#endif
4579
4580 if (prevwin == wp)
4581 prevwin = NULL;
4582 win_free_lsize(wp);
4583
4584 for (i = 0; i < wp->w_tagstacklen; ++i)
4585 vim_free(wp->w_tagstack[i].tagname);
4586
4587 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004588
Bram Moolenaarff18df02013-07-24 17:51:57 +02004589 /* Remove the window from the b_wininfo lists, it may happen that the
4590 * freed memory is re-used for another window. */
4591 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4592 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4593 if (wip->wi_win == wp)
4594 wip->wi_win = NULL;
4595
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004597 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600#ifdef FEAT_JUMPLIST
4601 free_jumplist(wp);
4602#endif
4603
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004604#ifdef FEAT_QUICKFIX
4605 qf_free_all(wp);
4606#endif
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608#ifdef FEAT_GUI
4609 if (gui.in_use)
4610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4612 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4613 }
4614#endif /* FEAT_GUI */
4615
Bram Moolenaar860cae12010-06-05 23:22:07 +02004616#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004617 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004618#endif
4619
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004620#ifdef FEAT_AUTOCMD
4621 if (wp != aucmd_win)
4622#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004623 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004624#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004625 if (autocmd_busy)
4626 {
4627 wp->w_next = au_pending_free_win;
4628 au_pending_free_win = wp;
4629 }
4630 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004631#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004632 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004633
4634#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004635 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637}
4638
4639/*
4640 * Append window "wp" in the window list after window "after".
4641 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004642 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643win_append(after, wp)
4644 win_T *after, *wp;
4645{
4646 win_T *before;
4647
4648 if (after == NULL) /* after NULL is in front of the first */
4649 before = firstwin;
4650 else
4651 before = after->w_next;
4652
4653 wp->w_next = before;
4654 wp->w_prev = after;
4655 if (after == NULL)
4656 firstwin = wp;
4657 else
4658 after->w_next = wp;
4659 if (before == NULL)
4660 lastwin = wp;
4661 else
4662 before->w_prev = wp;
4663}
4664
4665/*
4666 * Remove a window from the window list.
4667 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004668 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004669win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004671 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
4673 if (wp->w_prev != NULL)
4674 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004675 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004677 else
4678 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 if (wp->w_next != NULL)
4680 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004681 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004683 else
4684 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685}
4686
4687/*
4688 * Append frame "frp" in a frame list after frame "after".
4689 */
4690 static void
4691frame_append(after, frp)
4692 frame_T *after, *frp;
4693{
4694 frp->fr_next = after->fr_next;
4695 after->fr_next = frp;
4696 if (frp->fr_next != NULL)
4697 frp->fr_next->fr_prev = frp;
4698 frp->fr_prev = after;
4699}
4700
4701/*
4702 * Insert frame "frp" in a frame list before frame "before".
4703 */
4704 static void
4705frame_insert(before, frp)
4706 frame_T *before, *frp;
4707{
4708 frp->fr_next = before;
4709 frp->fr_prev = before->fr_prev;
4710 before->fr_prev = frp;
4711 if (frp->fr_prev != NULL)
4712 frp->fr_prev->fr_next = frp;
4713 else
4714 frp->fr_parent->fr_child = frp;
4715}
4716
4717/*
4718 * Remove a frame from a frame list.
4719 */
4720 static void
4721frame_remove(frp)
4722 frame_T *frp;
4723{
4724 if (frp->fr_prev != NULL)
4725 frp->fr_prev->fr_next = frp->fr_next;
4726 else
4727 frp->fr_parent->fr_child = frp->fr_next;
4728 if (frp->fr_next != NULL)
4729 frp->fr_next->fr_prev = frp->fr_prev;
4730}
4731
4732#endif /* FEAT_WINDOWS */
4733
4734/*
4735 * Allocate w_lines[] for window "wp".
4736 * Return FAIL for failure, OK for success.
4737 */
4738 int
4739win_alloc_lines(wp)
4740 win_T *wp;
4741{
4742 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004743 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 if (wp->w_lines == NULL)
4745 return FAIL;
4746 return OK;
4747}
4748
4749/*
4750 * free lsize arrays for a window
4751 */
4752 void
4753win_free_lsize(wp)
4754 win_T *wp;
4755{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004756 /* TODO: why would wp be NULL here? */
4757 if (wp != NULL)
4758 {
4759 vim_free(wp->w_lines);
4760 wp->w_lines = NULL;
4761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762}
4763
4764/*
4765 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004766 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 */
4768 void
4769shell_new_rows()
4770{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004771 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773 if (firstwin == NULL) /* not initialized yet */
4774 return;
4775#ifdef FEAT_WINDOWS
4776 if (h < frame_minheight(topframe, NULL))
4777 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004778
4779 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 * that doesn't result in the right height, forget about that option. */
4781 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004782 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 frame_new_height(topframe, h, FALSE, FALSE);
4784
4785 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4786#else
4787 if (h < 1)
4788 h = 1;
4789 win_new_height(firstwin, h);
4790#endif
4791 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004792#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004793 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004794#endif
4795
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796#if 0
4797 /* Disabled: don't want making the screen smaller make a window larger. */
4798 if (p_ea)
4799 win_equal(curwin, FALSE, 'v');
4800#endif
4801}
4802
4803#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4804/*
4805 * Called from win_new_shellsize() after Columns changed.
4806 */
4807 void
4808shell_new_columns()
4809{
4810 if (firstwin == NULL) /* not initialized yet */
4811 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004812
4813 /* First try setting the widths of windows with 'winfixwidth'. If that
4814 * doesn't result in the right width, forget about that option. */
4815 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004816 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004817 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4820#if 0
4821 /* Disabled: don't want making the screen smaller make a window larger. */
4822 if (p_ea)
4823 win_equal(curwin, FALSE, 'h');
4824#endif
4825}
4826#endif
4827
4828#if defined(FEAT_CMDWIN) || defined(PROTO)
4829/*
4830 * Save the size of all windows in "gap".
4831 */
4832 void
4833win_size_save(gap)
4834 garray_T *gap;
4835
4836{
4837 win_T *wp;
4838
4839 ga_init2(gap, (int)sizeof(int), 1);
4840 if (ga_grow(gap, win_count() * 2) == OK)
4841 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4842 {
4843 ((int *)gap->ga_data)[gap->ga_len++] =
4844 wp->w_width + wp->w_vsep_width;
4845 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4846 }
4847}
4848
4849/*
4850 * Restore window sizes, but only if the number of windows is still the same.
4851 * Does not free the growarray.
4852 */
4853 void
4854win_size_restore(gap)
4855 garray_T *gap;
4856{
4857 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004858 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860 if (win_count() * 2 == gap->ga_len)
4861 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004862 /* The order matters, because frames contain other frames, but it's
4863 * difficult to get right. The easy way out is to do it twice. */
4864 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004866 i = 0;
4867 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4868 {
4869 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4870 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
4873 /* recompute the window positions */
4874 (void)win_comp_pos();
4875 }
4876}
4877#endif /* FEAT_CMDWIN */
4878
4879#if defined(FEAT_WINDOWS) || defined(PROTO)
4880/*
4881 * Update the position for all windows, using the width and height of the
4882 * frames.
4883 * Returns the row just after the last window.
4884 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004885 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886win_comp_pos()
4887{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004888 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 int col = 0;
4890
4891 frame_comp_pos(topframe, &row, &col);
4892 return row;
4893}
4894
4895/*
4896 * Update the position of the windows in frame "topfrp", using the width and
4897 * height of the frames.
4898 * "*row" and "*col" are the top-left position of the frame. They are updated
4899 * to the bottom-right position plus one.
4900 */
4901 static void
4902frame_comp_pos(topfrp, row, col)
4903 frame_T *topfrp;
4904 int *row;
4905 int *col;
4906{
4907 win_T *wp;
4908 frame_T *frp;
4909#ifdef FEAT_VERTSPLIT
4910 int startcol;
4911 int startrow;
4912#endif
4913
4914 wp = topfrp->fr_win;
4915 if (wp != NULL)
4916 {
4917 if (wp->w_winrow != *row
4918#ifdef FEAT_VERTSPLIT
4919 || wp->w_wincol != *col
4920#endif
4921 )
4922 {
4923 /* position changed, redraw */
4924 wp->w_winrow = *row;
4925#ifdef FEAT_VERTSPLIT
4926 wp->w_wincol = *col;
4927#endif
4928 redraw_win_later(wp, NOT_VALID);
4929 wp->w_redr_status = TRUE;
4930 }
4931 *row += wp->w_height + wp->w_status_height;
4932#ifdef FEAT_VERTSPLIT
4933 *col += wp->w_width + wp->w_vsep_width;
4934#endif
4935 }
4936 else
4937 {
4938#ifdef FEAT_VERTSPLIT
4939 startrow = *row;
4940 startcol = *col;
4941#endif
4942 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4943 {
4944#ifdef FEAT_VERTSPLIT
4945 if (topfrp->fr_layout == FR_ROW)
4946 *row = startrow; /* all frames are at the same row */
4947 else
4948 *col = startcol; /* all frames are at the same col */
4949#endif
4950 frame_comp_pos(frp, row, col);
4951 }
4952 }
4953}
4954
4955#endif /* FEAT_WINDOWS */
4956
4957/*
4958 * Set current window height and take care of repositioning other windows to
4959 * fit around it.
4960 */
4961 void
4962win_setheight(height)
4963 int height;
4964{
4965 win_setheight_win(height, curwin);
4966}
4967
4968/*
4969 * Set the window height of window "win" and take care of repositioning other
4970 * windows to fit around it.
4971 */
4972 void
4973win_setheight_win(height, win)
4974 int height;
4975 win_T *win;
4976{
4977 int row;
4978
4979 if (win == curwin)
4980 {
4981 /* Always keep current window at least one line high, even when
4982 * 'winminheight' is zero. */
4983#ifdef FEAT_WINDOWS
4984 if (height < p_wmh)
4985 height = p_wmh;
4986#endif
4987 if (height == 0)
4988 height = 1;
4989 }
4990
4991#ifdef FEAT_WINDOWS
4992 frame_setheight(win->w_frame, height + win->w_status_height);
4993
4994 /* recompute the window positions */
4995 row = win_comp_pos();
4996#else
4997 if (height > topframe->fr_height)
4998 height = topframe->fr_height;
4999 win->w_height = height;
5000 row = height;
5001#endif
5002
5003 /*
5004 * If there is extra space created between the last window and the command
5005 * line, clear it.
5006 */
5007 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5008 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5009 cmdline_row = row;
5010 msg_row = row;
5011 msg_col = 0;
5012
5013 redraw_all_later(NOT_VALID);
5014}
5015
5016#if defined(FEAT_WINDOWS) || defined(PROTO)
5017
5018/*
5019 * Set the height of a frame to "height" and take care that all frames and
5020 * windows inside it are resized. Also resize frames on the left and right if
5021 * the are in the same FR_ROW frame.
5022 *
5023 * Strategy:
5024 * If the frame is part of a FR_COL frame, try fitting the frame in that
5025 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5026 * go to containing frames to resize them and make room.
5027 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5028 * Check for the minimal height of the FR_ROW frame.
5029 * At the top level we can also use change the command line height.
5030 */
5031 static void
5032frame_setheight(curfrp, height)
5033 frame_T *curfrp;
5034 int height;
5035{
5036 int room; /* total number of lines available */
5037 int take; /* number of lines taken from other windows */
5038 int room_cmdline; /* lines available from cmdline */
5039 int run;
5040 frame_T *frp;
5041 int h;
5042 int room_reserved;
5043
5044 /* If the height already is the desired value, nothing to do. */
5045 if (curfrp->fr_height == height)
5046 return;
5047
5048 if (curfrp->fr_parent == NULL)
5049 {
5050 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005051 if (height > ROWS_AVAIL)
5052 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 if (height > 0)
5054 frame_new_height(curfrp, height, FALSE, FALSE);
5055 }
5056 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5057 {
5058 /* Row of frames: Also need to resize frames left and right of this
5059 * one. First check for the minimal height of these. */
5060 h = frame_minheight(curfrp->fr_parent, NULL);
5061 if (height < h)
5062 height = h;
5063 frame_setheight(curfrp->fr_parent, height);
5064 }
5065 else
5066 {
5067 /*
5068 * Column of frames: try to change only frames in this column.
5069 */
5070#ifdef FEAT_VERTSPLIT
5071 /*
5072 * Do this twice:
5073 * 1: compute room available, if it's not enough try resizing the
5074 * containing frame.
5075 * 2: compute the room available and adjust the height to it.
5076 * Try not to reduce the height of a window with 'winfixheight' set.
5077 */
5078 for (run = 1; run <= 2; ++run)
5079#else
5080 for (;;)
5081#endif
5082 {
5083 room = 0;
5084 room_reserved = 0;
5085 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5086 frp = frp->fr_next)
5087 {
5088 if (frp != curfrp
5089 && frp->fr_win != NULL
5090 && frp->fr_win->w_p_wfh)
5091 room_reserved += frp->fr_height;
5092 room += frp->fr_height;
5093 if (frp != curfrp)
5094 room -= frame_minheight(frp, NULL);
5095 }
5096#ifdef FEAT_VERTSPLIT
5097 if (curfrp->fr_width != Columns)
5098 room_cmdline = 0;
5099 else
5100#endif
5101 {
5102 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5103 + lastwin->w_height + lastwin->w_status_height);
5104 if (room_cmdline < 0)
5105 room_cmdline = 0;
5106 }
5107
5108 if (height <= room + room_cmdline)
5109 break;
5110#ifdef FEAT_VERTSPLIT
5111 if (run == 2 || curfrp->fr_width == Columns)
5112#endif
5113 {
5114 if (height > room + room_cmdline)
5115 height = room + room_cmdline;
5116 break;
5117 }
5118#ifdef FEAT_VERTSPLIT
5119 frame_setheight(curfrp->fr_parent, height
5120 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5121#endif
5122 /*NOTREACHED*/
5123 }
5124
5125 /*
5126 * Compute the number of lines we will take from others frames (can be
5127 * negative!).
5128 */
5129 take = height - curfrp->fr_height;
5130
5131 /* If there is not enough room, also reduce the height of a window
5132 * with 'winfixheight' set. */
5133 if (height > room + room_cmdline - room_reserved)
5134 room_reserved = room + room_cmdline - height;
5135 /* If there is only a 'winfixheight' window and making the
5136 * window smaller, need to make the other window taller. */
5137 if (take < 0 && room - curfrp->fr_height < room_reserved)
5138 room_reserved = 0;
5139
5140 if (take > 0 && room_cmdline > 0)
5141 {
5142 /* use lines from cmdline first */
5143 if (take < room_cmdline)
5144 room_cmdline = take;
5145 take -= room_cmdline;
5146 topframe->fr_height += room_cmdline;
5147 }
5148
5149 /*
5150 * set the current frame to the new height
5151 */
5152 frame_new_height(curfrp, height, FALSE, FALSE);
5153
5154 /*
5155 * First take lines from the frames after the current frame. If
5156 * that is not enough, takes lines from frames above the current
5157 * frame.
5158 */
5159 for (run = 0; run < 2; ++run)
5160 {
5161 if (run == 0)
5162 frp = curfrp->fr_next; /* 1st run: start with next window */
5163 else
5164 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5165 while (frp != NULL && take != 0)
5166 {
5167 h = frame_minheight(frp, NULL);
5168 if (room_reserved > 0
5169 && frp->fr_win != NULL
5170 && frp->fr_win->w_p_wfh)
5171 {
5172 if (room_reserved >= frp->fr_height)
5173 room_reserved -= frp->fr_height;
5174 else
5175 {
5176 if (frp->fr_height - room_reserved > take)
5177 room_reserved = frp->fr_height - take;
5178 take -= frp->fr_height - room_reserved;
5179 frame_new_height(frp, room_reserved, FALSE, FALSE);
5180 room_reserved = 0;
5181 }
5182 }
5183 else
5184 {
5185 if (frp->fr_height - take < h)
5186 {
5187 take -= frp->fr_height - h;
5188 frame_new_height(frp, h, FALSE, FALSE);
5189 }
5190 else
5191 {
5192 frame_new_height(frp, frp->fr_height - take,
5193 FALSE, FALSE);
5194 take = 0;
5195 }
5196 }
5197 if (run == 0)
5198 frp = frp->fr_next;
5199 else
5200 frp = frp->fr_prev;
5201 }
5202 }
5203 }
5204}
5205
5206#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5207/*
5208 * Set current window width and take care of repositioning other windows to
5209 * fit around it.
5210 */
5211 void
5212win_setwidth(width)
5213 int width;
5214{
5215 win_setwidth_win(width, curwin);
5216}
5217
5218 void
5219win_setwidth_win(width, wp)
5220 int width;
5221 win_T *wp;
5222{
5223 /* Always keep current window at least one column wide, even when
5224 * 'winminwidth' is zero. */
5225 if (wp == curwin)
5226 {
5227 if (width < p_wmw)
5228 width = p_wmw;
5229 if (width == 0)
5230 width = 1;
5231 }
5232
5233 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5234
5235 /* recompute the window positions */
5236 (void)win_comp_pos();
5237
5238 redraw_all_later(NOT_VALID);
5239}
5240
5241/*
5242 * Set the width of a frame to "width" and take care that all frames and
5243 * windows inside it are resized. Also resize frames above and below if the
5244 * are in the same FR_ROW frame.
5245 *
5246 * Strategy is similar to frame_setheight().
5247 */
5248 static void
5249frame_setwidth(curfrp, width)
5250 frame_T *curfrp;
5251 int width;
5252{
5253 int room; /* total number of lines available */
5254 int take; /* number of lines taken from other windows */
5255 int run;
5256 frame_T *frp;
5257 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005258 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259
5260 /* If the width already is the desired value, nothing to do. */
5261 if (curfrp->fr_width == width)
5262 return;
5263
5264 if (curfrp->fr_parent == NULL)
5265 /* topframe: can't change width */
5266 return;
5267
5268 if (curfrp->fr_parent->fr_layout == FR_COL)
5269 {
5270 /* Column of frames: Also need to resize frames above and below of
5271 * this one. First check for the minimal width of these. */
5272 w = frame_minwidth(curfrp->fr_parent, NULL);
5273 if (width < w)
5274 width = w;
5275 frame_setwidth(curfrp->fr_parent, width);
5276 }
5277 else
5278 {
5279 /*
5280 * Row of frames: try to change only frames in this row.
5281 *
5282 * Do this twice:
5283 * 1: compute room available, if it's not enough try resizing the
5284 * containing frame.
5285 * 2: compute the room available and adjust the width to it.
5286 */
5287 for (run = 1; run <= 2; ++run)
5288 {
5289 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005290 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5292 frp = frp->fr_next)
5293 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005294 if (frp != curfrp
5295 && frp->fr_win != NULL
5296 && frp->fr_win->w_p_wfw)
5297 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 room += frp->fr_width;
5299 if (frp != curfrp)
5300 room -= frame_minwidth(frp, NULL);
5301 }
5302
5303 if (width <= room)
5304 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005305 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
5307 if (width > room)
5308 width = room;
5309 break;
5310 }
5311 frame_setwidth(curfrp->fr_parent, width
5312 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5313 }
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 /*
5316 * Compute the number of lines we will take from others frames (can be
5317 * negative!).
5318 */
5319 take = width - curfrp->fr_width;
5320
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005321 /* If there is not enough room, also reduce the width of a window
5322 * with 'winfixwidth' set. */
5323 if (width > room - room_reserved)
5324 room_reserved = room - width;
5325 /* If there is only a 'winfixwidth' window and making the
5326 * window smaller, need to make the other window narrower. */
5327 if (take < 0 && room - curfrp->fr_width < room_reserved)
5328 room_reserved = 0;
5329
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 /*
5331 * set the current frame to the new width
5332 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005333 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 /*
5336 * First take lines from the frames right of the current frame. If
5337 * that is not enough, takes lines from frames left of the current
5338 * frame.
5339 */
5340 for (run = 0; run < 2; ++run)
5341 {
5342 if (run == 0)
5343 frp = curfrp->fr_next; /* 1st run: start with next window */
5344 else
5345 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5346 while (frp != NULL && take != 0)
5347 {
5348 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005349 if (room_reserved > 0
5350 && frp->fr_win != NULL
5351 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005353 if (room_reserved >= frp->fr_width)
5354 room_reserved -= frp->fr_width;
5355 else
5356 {
5357 if (frp->fr_width - room_reserved > take)
5358 room_reserved = frp->fr_width - take;
5359 take -= frp->fr_width - room_reserved;
5360 frame_new_width(frp, room_reserved, FALSE, FALSE);
5361 room_reserved = 0;
5362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 }
5364 else
5365 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005366 if (frp->fr_width - take < w)
5367 {
5368 take -= frp->fr_width - w;
5369 frame_new_width(frp, w, FALSE, FALSE);
5370 }
5371 else
5372 {
5373 frame_new_width(frp, frp->fr_width - take,
5374 FALSE, FALSE);
5375 take = 0;
5376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378 if (run == 0)
5379 frp = frp->fr_next;
5380 else
5381 frp = frp->fr_prev;
5382 }
5383 }
5384 }
5385}
5386#endif /* FEAT_VERTSPLIT */
5387
5388/*
5389 * Check 'winminheight' for a valid value.
5390 */
5391 void
5392win_setminheight()
5393{
5394 int room;
5395 int first = TRUE;
5396 win_T *wp;
5397
5398 /* loop until there is a 'winminheight' that is possible */
5399 while (p_wmh > 0)
5400 {
5401 /* TODO: handle vertical splits */
5402 room = -p_wh;
5403 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5404 room += wp->w_height - p_wmh;
5405 if (room >= 0)
5406 break;
5407 --p_wmh;
5408 if (first)
5409 {
5410 EMSG(_(e_noroom));
5411 first = FALSE;
5412 }
5413 }
5414}
5415
5416#ifdef FEAT_MOUSE
5417
5418/*
5419 * Status line of dragwin is dragged "offset" lines down (negative is up).
5420 */
5421 void
5422win_drag_status_line(dragwin, offset)
5423 win_T *dragwin;
5424 int offset;
5425{
5426 frame_T *curfr;
5427 frame_T *fr;
5428 int room;
5429 int row;
5430 int up; /* if TRUE, drag status line up, otherwise down */
5431 int n;
5432
5433 fr = dragwin->w_frame;
5434 curfr = fr;
5435 if (fr != topframe) /* more than one window */
5436 {
5437 fr = fr->fr_parent;
5438 /* When the parent frame is not a column of frames, its parent should
5439 * be. */
5440 if (fr->fr_layout != FR_COL)
5441 {
5442 curfr = fr;
5443 if (fr != topframe) /* only a row of windows, may drag statusline */
5444 fr = fr->fr_parent;
5445 }
5446 }
5447
5448 /* If this is the last frame in a column, may want to resize the parent
5449 * frame instead (go two up to skip a row of frames). */
5450 while (curfr != topframe && curfr->fr_next == NULL)
5451 {
5452 if (fr != topframe)
5453 fr = fr->fr_parent;
5454 curfr = fr;
5455 if (fr != topframe)
5456 fr = fr->fr_parent;
5457 }
5458
5459 if (offset < 0) /* drag up */
5460 {
5461 up = TRUE;
5462 offset = -offset;
5463 /* sum up the room of the current frame and above it */
5464 if (fr == curfr)
5465 {
5466 /* only one window */
5467 room = fr->fr_height - frame_minheight(fr, NULL);
5468 }
5469 else
5470 {
5471 room = 0;
5472 for (fr = fr->fr_child; ; fr = fr->fr_next)
5473 {
5474 room += fr->fr_height - frame_minheight(fr, NULL);
5475 if (fr == curfr)
5476 break;
5477 }
5478 }
5479 fr = curfr->fr_next; /* put fr at frame that grows */
5480 }
5481 else /* drag down */
5482 {
5483 up = FALSE;
5484 /*
5485 * Only dragging the last status line can reduce p_ch.
5486 */
5487 room = Rows - cmdline_row;
5488 if (curfr->fr_next == NULL)
5489 room -= 1;
5490 else
5491 room -= p_ch;
5492 if (room < 0)
5493 room = 0;
5494 /* sum up the room of frames below of the current one */
5495 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5496 room += fr->fr_height - frame_minheight(fr, NULL);
5497 fr = curfr; /* put fr at window that grows */
5498 }
5499
5500 if (room < offset) /* Not enough room */
5501 offset = room; /* Move as far as we can */
5502 if (offset <= 0)
5503 return;
5504
5505 /*
5506 * Grow frame fr by "offset" lines.
5507 * Doesn't happen when dragging the last status line up.
5508 */
5509 if (fr != NULL)
5510 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5511
5512 if (up)
5513 fr = curfr; /* current frame gets smaller */
5514 else
5515 fr = curfr->fr_next; /* next frame gets smaller */
5516
5517 /*
5518 * Now make the other frames smaller.
5519 */
5520 while (fr != NULL && offset > 0)
5521 {
5522 n = frame_minheight(fr, NULL);
5523 if (fr->fr_height - offset <= n)
5524 {
5525 offset -= fr->fr_height - n;
5526 frame_new_height(fr, n, !up, FALSE);
5527 }
5528 else
5529 {
5530 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5531 break;
5532 }
5533 if (up)
5534 fr = fr->fr_prev;
5535 else
5536 fr = fr->fr_next;
5537 }
5538 row = win_comp_pos();
5539 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5540 cmdline_row = row;
5541 p_ch = Rows - cmdline_row;
5542 if (p_ch < 1)
5543 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005544 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005545 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 showmode();
5547}
5548
5549#ifdef FEAT_VERTSPLIT
5550/*
5551 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5552 */
5553 void
5554win_drag_vsep_line(dragwin, offset)
5555 win_T *dragwin;
5556 int offset;
5557{
5558 frame_T *curfr;
5559 frame_T *fr;
5560 int room;
5561 int left; /* if TRUE, drag separator line left, otherwise right */
5562 int n;
5563
5564 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005565 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 return;
5567 curfr = fr;
5568 fr = fr->fr_parent;
5569 /* When the parent frame is not a row of frames, its parent should be. */
5570 if (fr->fr_layout != FR_ROW)
5571 {
5572 if (fr == topframe) /* only a column of windows (cannot happen?) */
5573 return;
5574 curfr = fr;
5575 fr = fr->fr_parent;
5576 }
5577
5578 /* If this is the last frame in a row, may want to resize a parent
5579 * frame instead. */
5580 while (curfr->fr_next == NULL)
5581 {
5582 if (fr == topframe)
5583 break;
5584 curfr = fr;
5585 fr = fr->fr_parent;
5586 if (fr != topframe)
5587 {
5588 curfr = fr;
5589 fr = fr->fr_parent;
5590 }
5591 }
5592
5593 if (offset < 0) /* drag left */
5594 {
5595 left = TRUE;
5596 offset = -offset;
5597 /* sum up the room of the current frame and left of it */
5598 room = 0;
5599 for (fr = fr->fr_child; ; fr = fr->fr_next)
5600 {
5601 room += fr->fr_width - frame_minwidth(fr, NULL);
5602 if (fr == curfr)
5603 break;
5604 }
5605 fr = curfr->fr_next; /* put fr at frame that grows */
5606 }
5607 else /* drag right */
5608 {
5609 left = FALSE;
5610 /* sum up the room of frames right of the current one */
5611 room = 0;
5612 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5613 room += fr->fr_width - frame_minwidth(fr, NULL);
5614 fr = curfr; /* put fr at window that grows */
5615 }
5616
5617 if (room < offset) /* Not enough room */
5618 offset = room; /* Move as far as we can */
5619 if (offset <= 0) /* No room at all, quit. */
5620 return;
5621
5622 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005623 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 /* shrink other frames: current and at the left or at the right */
5626 if (left)
5627 fr = curfr; /* current frame gets smaller */
5628 else
5629 fr = curfr->fr_next; /* next frame gets smaller */
5630
5631 while (fr != NULL && offset > 0)
5632 {
5633 n = frame_minwidth(fr, NULL);
5634 if (fr->fr_width - offset <= n)
5635 {
5636 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005637 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 }
5639 else
5640 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005641 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 break;
5643 }
5644 if (left)
5645 fr = fr->fr_prev;
5646 else
5647 fr = fr->fr_next;
5648 }
5649 (void)win_comp_pos();
5650 redraw_all_later(NOT_VALID);
5651}
5652#endif /* FEAT_VERTSPLIT */
5653#endif /* FEAT_MOUSE */
5654
5655#endif /* FEAT_WINDOWS */
5656
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005657#define FRACTION_MULT 16384L
5658
5659/*
5660 * Set wp->w_fraction for the current w_wrow and w_height.
5661 */
5662 static void
5663set_fraction(wp)
5664 win_T *wp;
5665{
5666 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005667 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005668}
5669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670/*
5671 * Set the height of a window.
5672 * This takes care of the things inside the window, not what happens to the
5673 * window position, the frame or to other windows.
5674 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005675 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676win_new_height(wp, height)
5677 win_T *wp;
5678 int height;
5679{
5680 linenr_T lnum;
5681 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005682 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684 /* Don't want a negative height. Happens when splitting a tiny window.
5685 * Will equalize heights soon to fix it. */
5686 if (height < 0)
5687 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005688 if (wp->w_height == height)
5689 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005691 if (wp->w_height > 0)
5692 {
5693 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005694 /* w_wrow needs to be valid. When setting 'laststatus' this may
5695 * call win_new_height() recursively. */
5696 validate_cursor();
5697 if (wp->w_height != prev_height)
5698 return; /* Recursive call already changed the size, bail out here
5699 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005700 if (wp->w_wrow != wp->w_prev_fraction_row)
5701 set_fraction(wp);
5702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
5704 wp->w_height = height;
5705 wp->w_skipcol = 0;
5706
5707 /* Don't change w_topline when height is zero. Don't set w_topline when
5708 * 'scrollbind' is set and this isn't the current window. */
5709 if (height > 0
5710#ifdef FEAT_SCROLLBIND
5711 && (!wp->w_p_scb || wp == curwin)
5712#endif
5713 )
5714 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005715 /*
5716 * Find a value for w_topline that shows the cursor at the same
5717 * relative position in the window as before (more or less).
5718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 lnum = wp->w_cursor.lnum;
5720 if (lnum < 1) /* can happen when starting up */
5721 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005722 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5723 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5725 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005726
5727 if (sline >= 0)
5728 {
5729 /* Make sure the whole cursor line is visible, if possible. */
5730 int rows = plines_win(wp, lnum, FALSE);
5731
5732 if (sline > wp->w_height - rows)
5733 {
5734 sline = wp->w_height - rows;
5735 wp->w_wrow -= rows - line_size;
5736 }
5737 }
5738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if (sline < 0)
5740 {
5741 /*
5742 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005743 * Make cursor line the first line in the window. If not enough
5744 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 */
5746 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005747 if (wp->w_wrow >= wp->w_height
5748 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5749 {
5750 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5751 --wp->w_wrow;
5752 while (wp->w_wrow >= wp->w_height)
5753 {
5754 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5755 + win_col_off2(wp);
5756 --wp->w_wrow;
5757 }
5758 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005759 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005761 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005763 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
5765#ifdef FEAT_FOLDING
5766 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5767 if (lnum == 1)
5768 {
5769 /* first line in buffer is folded */
5770 line_size = 1;
5771 --sline;
5772 break;
5773 }
5774#endif
5775 --lnum;
5776#ifdef FEAT_DIFF
5777 if (lnum == wp->w_topline)
5778 line_size = plines_win_nofill(wp, lnum, TRUE)
5779 + wp->w_topfill;
5780 else
5781#endif
5782 line_size = plines_win(wp, lnum, TRUE);
5783 sline -= line_size;
5784 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 if (sline < 0)
5787 {
5788 /*
5789 * Line we want at top would go off top of screen. Use next
5790 * line instead.
5791 */
5792#ifdef FEAT_FOLDING
5793 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5794#endif
5795 lnum++;
5796 wp->w_wrow -= line_size + sline;
5797 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005798 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
5800 /* First line of file reached, use that as topline. */
5801 lnum = 1;
5802 wp->w_wrow -= sline;
5803 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005804
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005805 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808
5809 if (wp == curwin)
5810 {
5811 if (p_so)
5812 update_topline();
5813 curs_columns(FALSE); /* validate w_wrow */
5814 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005815 if (prev_height > 0)
5816 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
5818 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005819 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820#ifdef FEAT_WINDOWS
5821 wp->w_redr_status = TRUE;
5822#endif
5823 invalidate_botline_win(wp);
5824}
5825
5826#ifdef FEAT_VERTSPLIT
5827/*
5828 * Set the width of a window.
5829 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005830 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831win_new_width(wp, width)
5832 win_T *wp;
5833 int width;
5834{
5835 wp->w_width = width;
5836 wp->w_lines_valid = 0;
5837 changed_line_abv_curs_win(wp);
5838 invalidate_botline_win(wp);
5839 if (wp == curwin)
5840 {
5841 update_topline();
5842 curs_columns(TRUE); /* validate w_wrow */
5843 }
5844 redraw_win_later(wp, NOT_VALID);
5845 wp->w_redr_status = TRUE;
5846}
5847#endif
5848
5849 void
5850win_comp_scroll(wp)
5851 win_T *wp;
5852{
5853 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5854 if (wp->w_p_scr == 0)
5855 wp->w_p_scr = 1;
5856}
5857
5858/*
5859 * command_height: called whenever p_ch has been changed
5860 */
5861 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005862command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
5864#ifdef FEAT_WINDOWS
5865 int h;
5866 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005867 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005869 /* Use the value of p_ch that we remembered. This is needed for when the
5870 * GUI starts up, we can't be sure in what order things happen. And when
5871 * p_ch was changed in another tab page. */
5872 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005873
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 /* Find bottom frame with width of screen. */
5875 frp = lastwin->w_frame;
5876# ifdef FEAT_VERTSPLIT
5877 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5878 frp = frp->fr_parent;
5879# endif
5880
5881 /* Avoid changing the height of a window with 'winfixheight' set. */
5882 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5883 && frp->fr_win->w_p_wfh)
5884 frp = frp->fr_prev;
5885
5886 if (starting != NO_SCREEN)
5887 {
5888 cmdline_row = Rows - p_ch;
5889
5890 if (p_ch > old_p_ch) /* p_ch got bigger */
5891 {
5892 while (p_ch > old_p_ch)
5893 {
5894 if (frp == NULL)
5895 {
5896 EMSG(_(e_noroom));
5897 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005898 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 cmdline_row = Rows - p_ch;
5900 break;
5901 }
5902 h = frp->fr_height - frame_minheight(frp, NULL);
5903 if (h > p_ch - old_p_ch)
5904 h = p_ch - old_p_ch;
5905 old_p_ch += h;
5906 frame_add_height(frp, -h);
5907 frp = frp->fr_prev;
5908 }
5909
5910 /* Recompute window positions. */
5911 (void)win_comp_pos();
5912
5913 /* clear the lines added to cmdline */
5914 if (full_screen)
5915 screen_fill((int)(cmdline_row), (int)Rows, 0,
5916 (int)Columns, ' ', ' ', 0);
5917 msg_row = cmdline_row;
5918 redraw_cmdline = TRUE;
5919 return;
5920 }
5921
5922 if (msg_row < cmdline_row)
5923 msg_row = cmdline_row;
5924 redraw_cmdline = TRUE;
5925 }
5926 frame_add_height(frp, (int)(old_p_ch - p_ch));
5927
5928 /* Recompute window positions. */
5929 if (frp != lastwin->w_frame)
5930 (void)win_comp_pos();
5931#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005933 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934#endif
5935}
5936
5937#if defined(FEAT_WINDOWS) || defined(PROTO)
5938/*
5939 * Resize frame "frp" to be "n" lines higher (negative for less high).
5940 * Also resize the frames it is contained in.
5941 */
5942 static void
5943frame_add_height(frp, n)
5944 frame_T *frp;
5945 int n;
5946{
5947 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5948 for (;;)
5949 {
5950 frp = frp->fr_parent;
5951 if (frp == NULL)
5952 break;
5953 frp->fr_height += n;
5954 }
5955}
5956
5957/*
5958 * Add or remove a status line for the bottom window(s), according to the
5959 * value of 'laststatus'.
5960 */
5961 void
5962last_status(morewin)
5963 int morewin; /* pretend there are two or more windows */
5964{
5965 /* Don't make a difference between horizontal or vertical split. */
5966 last_status_rec(topframe, (p_ls == 2
5967 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5968}
5969
5970 static void
5971last_status_rec(fr, statusline)
5972 frame_T *fr;
5973 int statusline;
5974{
5975 frame_T *fp;
5976 win_T *wp;
5977
5978 if (fr->fr_layout == FR_LEAF)
5979 {
5980 wp = fr->fr_win;
5981 if (wp->w_status_height != 0 && !statusline)
5982 {
5983 /* remove status line */
5984 win_new_height(wp, wp->w_height + 1);
5985 wp->w_status_height = 0;
5986 comp_col();
5987 }
5988 else if (wp->w_status_height == 0 && statusline)
5989 {
5990 /* Find a frame to take a line from. */
5991 fp = fr;
5992 while (fp->fr_height <= frame_minheight(fp, NULL))
5993 {
5994 if (fp == topframe)
5995 {
5996 EMSG(_(e_noroom));
5997 return;
5998 }
5999 /* In a column of frames: go to frame above. If already at
6000 * the top or in a row of frames: go to parent. */
6001 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6002 fp = fp->fr_prev;
6003 else
6004 fp = fp->fr_parent;
6005 }
6006 wp->w_status_height = 1;
6007 if (fp != fr)
6008 {
6009 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6010 frame_fix_height(wp);
6011 (void)win_comp_pos();
6012 }
6013 else
6014 win_new_height(wp, wp->w_height - 1);
6015 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006016 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 }
6018 }
6019#ifdef FEAT_VERTSPLIT
6020 else if (fr->fr_layout == FR_ROW)
6021 {
6022 /* vertically split windows, set status line for each one */
6023 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6024 last_status_rec(fp, statusline);
6025 }
6026#endif
6027 else
6028 {
6029 /* horizontally split window, set status line for last one */
6030 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6031 ;
6032 last_status_rec(fp, statusline);
6033 }
6034}
6035
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006036/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006037 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006038 */
6039 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006040tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006041{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006042#ifdef FEAT_GUI_TABLINE
6043 /* When the GUI has the tabline then this always returns zero. */
6044 if (gui_use_tabline())
6045 return 0;
6046#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006047 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006048 {
6049 case 0: return 0;
6050 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6051 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006052 return 1;
6053}
6054
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055#endif /* FEAT_WINDOWS */
6056
6057#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6058/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006059 * Get the file name at the cursor.
6060 * If Visual mode is active, use the selected text if it's in one line.
6061 * Returns the name in allocated memory, NULL for failure.
6062 */
6063 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006064grab_file_name(count, file_lnum)
6065 long count;
6066 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006067{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006068 if (VIsual_active)
6069 {
6070 int len;
6071 char_u *ptr;
6072
6073 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6074 return NULL;
6075 return find_file_name_in_path(ptr, len,
6076 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6077 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006078 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6079 file_lnum);
6080
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006081}
6082
6083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 * Return the file name under or after the cursor.
6085 *
6086 * The 'path' option is searched if the file name is not absolute.
6087 * The string returned has been alloc'ed and should be freed by the caller.
6088 * NULL is returned if the file name or file is not found.
6089 *
6090 * options:
6091 * FNAME_MESS give error messages
6092 * FNAME_EXP expand to path
6093 * FNAME_HYP check for hypertext link
6094 * FNAME_INCL apply "includeexpr"
6095 */
6096 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006097file_name_at_cursor(options, count, file_lnum)
6098 int options;
6099 long count;
6100 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006103 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6104 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105}
6106
6107/*
6108 * Return the name of the file under or after ptr[col].
6109 * Otherwise like file_name_at_cursor().
6110 */
6111 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006112file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 char_u *line;
6114 int col;
6115 int options;
6116 long count;
6117 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006118 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 char_u *ptr;
6121 int len;
6122
6123 /*
6124 * search forward for what could be the start of a file name
6125 */
6126 ptr = line + col;
6127 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006128 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 if (*ptr == NUL) /* nothing found */
6130 {
6131 if (options & FNAME_MESS)
6132 EMSG(_("E446: No file name under cursor"));
6133 return NULL;
6134 }
6135
6136 /*
6137 * Search backward for first char of the file name.
6138 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6139 */
6140 while (ptr > line)
6141 {
6142#ifdef FEAT_MBYTE
6143 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6144 ptr -= len + 1;
6145 else
6146#endif
6147 if (vim_isfilec(ptr[-1])
6148 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6149 --ptr;
6150 else
6151 break;
6152 }
6153
6154 /*
6155 * Search forward for the last char of the file name.
6156 * Also allow "://" when ':' is not in 'isfname'.
6157 */
6158 len = 0;
6159 while (vim_isfilec(ptr[len])
6160 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6161#ifdef FEAT_MBYTE
6162 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006163 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 else
6165#endif
6166 ++len;
6167
6168 /*
6169 * If there is trailing punctuation, remove it.
6170 * But don't remove "..", could be a directory name.
6171 */
6172 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6173 && ptr[len - 2] != '.')
6174 --len;
6175
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006176 if (file_lnum != NULL)
6177 {
6178 char_u *p;
6179
6180 /* Get the number after the file name and a separator character */
6181 p = ptr + len;
6182 p = skipwhite(p);
6183 if (*p != NUL)
6184 {
6185 if (!isdigit(*p))
6186 ++p; /* skip the separator */
6187 p = skipwhite(p);
6188 if (isdigit(*p))
6189 *file_lnum = (int)getdigits(&p);
6190 }
6191 }
6192
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6194}
6195
6196# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6197static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6198
6199 static char_u *
6200eval_includeexpr(ptr, len)
6201 char_u *ptr;
6202 int len;
6203{
6204 char_u *res;
6205
6206 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006207 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006208 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 set_vim_var_string(VV_FNAME, NULL, 0);
6210 return res;
6211}
6212#endif
6213
6214/*
6215 * Return the name of the file ptr[len] in 'path'.
6216 * Otherwise like file_name_at_cursor().
6217 */
6218 char_u *
6219find_file_name_in_path(ptr, len, options, count, rel_fname)
6220 char_u *ptr;
6221 int len;
6222 int options;
6223 long count;
6224 char_u *rel_fname; /* file we are searching relative to */
6225{
6226 char_u *file_name;
6227 int c;
6228# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6229 char_u *tofree = NULL;
6230
6231 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6232 {
6233 tofree = eval_includeexpr(ptr, len);
6234 if (tofree != NULL)
6235 {
6236 ptr = tofree;
6237 len = (int)STRLEN(ptr);
6238 }
6239 }
6240# endif
6241
6242 if (options & FNAME_EXP)
6243 {
6244 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6245 TRUE, rel_fname);
6246
6247# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6248 /*
6249 * If the file could not be found in a normal way, try applying
6250 * 'includeexpr' (unless done already).
6251 */
6252 if (file_name == NULL
6253 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6254 {
6255 tofree = eval_includeexpr(ptr, len);
6256 if (tofree != NULL)
6257 {
6258 ptr = tofree;
6259 len = (int)STRLEN(ptr);
6260 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6261 TRUE, rel_fname);
6262 }
6263 }
6264# endif
6265 if (file_name == NULL && (options & FNAME_MESS))
6266 {
6267 c = ptr[len];
6268 ptr[len] = NUL;
6269 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6270 ptr[len] = c;
6271 }
6272
6273 /* Repeat finding the file "count" times. This matters when it
6274 * appears several times in the path. */
6275 while (file_name != NULL && --count > 0)
6276 {
6277 vim_free(file_name);
6278 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6279 }
6280 }
6281 else
6282 file_name = vim_strnsave(ptr, len);
6283
6284# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6285 vim_free(tofree);
6286# endif
6287
6288 return file_name;
6289}
6290#endif /* FEAT_SEARCHPATH */
6291
6292/*
6293 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6294 * Also check for ":\\", which MS Internet Explorer accepts, return
6295 * URL_BACKSLASH.
6296 */
6297 static int
6298path_is_url(p)
6299 char_u *p;
6300{
6301 if (STRNCMP(p, "://", (size_t)3) == 0)
6302 return URL_SLASH;
6303 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6304 return URL_BACKSLASH;
6305 return 0;
6306}
6307
6308/*
6309 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6310 * Return URL_BACKSLASH for "name:\\".
6311 * Return zero otherwise.
6312 */
6313 int
6314path_with_url(fname)
6315 char_u *fname;
6316{
6317 char_u *p;
6318
6319 for (p = fname; isalpha(*p); ++p)
6320 ;
6321 return path_is_url(p);
6322}
6323
6324/*
6325 * Return TRUE if "name" is a full (absolute) path name or URL.
6326 */
6327 int
6328vim_isAbsName(name)
6329 char_u *name;
6330{
6331 return (path_with_url(name) != 0 || mch_isFullName(name));
6332}
6333
6334/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006335 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 *
6337 * return FAIL for failure, OK otherwise
6338 */
6339 int
6340vim_FullName(fname, buf, len, force)
6341 char_u *fname, *buf;
6342 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006343 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 int retval = OK;
6346 int url;
6347
6348 *buf = NUL;
6349 if (fname == NULL)
6350 return FAIL;
6351
6352 url = path_with_url(fname);
6353 if (!url)
6354 retval = mch_FullName(fname, buf, len, force);
6355 if (url || retval == FAIL)
6356 {
6357 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006358 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6361 slash_adjust(buf);
6362#endif
6363 return retval;
6364}
6365
6366/*
6367 * Return the minimal number of rows that is needed on the screen to display
6368 * the current number of windows.
6369 */
6370 int
6371min_rows()
6372{
6373 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006374#ifdef FEAT_WINDOWS
6375 tabpage_T *tp;
6376 int n;
6377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378
6379 if (firstwin == NULL) /* not initialized yet */
6380 return MIN_LINES;
6381
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006383 total = 0;
6384 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6385 {
6386 n = frame_minheight(tp->tp_topframe, NULL);
6387 if (total < n)
6388 total = n;
6389 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006390 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006392 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006394 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 return total;
6396}
6397
6398/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006399 * Return TRUE if there is only one window (in the current tab page), not
6400 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006401 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 */
6403 int
6404only_one_window()
6405{
6406#ifdef FEAT_WINDOWS
6407 int count = 0;
6408 win_T *wp;
6409
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006410 /* If there is another tab page there always is another window. */
6411 if (first_tabpage->tp_next != NULL)
6412 return FALSE;
6413
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006415 if (wp->w_buffer != NULL
6416 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417# ifdef FEAT_QUICKFIX
6418 || wp->w_p_pvw
6419# endif
6420 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006421# ifdef FEAT_AUTOCMD
6422 && wp != aucmd_win
6423# endif
6424 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 ++count;
6426 return (count <= 1);
6427#else
6428 return TRUE;
6429#endif
6430}
6431
6432#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6433/*
6434 * Correct the cursor line number in other windows. Used after changing the
6435 * current buffer, and before applying autocommands.
6436 * When "do_curwin" is TRUE, also check current window.
6437 */
6438 void
6439check_lnums(do_curwin)
6440 int do_curwin;
6441{
6442 win_T *wp;
6443
6444#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006445 tabpage_T *tp;
6446
6447 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6449#else
6450 wp = curwin;
6451 if (do_curwin)
6452#endif
6453 {
6454 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6455 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6456 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6457 wp->w_topline = curbuf->b_ml.ml_line_count;
6458 }
6459}
6460#endif
6461
6462#if defined(FEAT_WINDOWS) || defined(PROTO)
6463
6464/*
6465 * A snapshot of the window sizes, to restore them after closing the help
6466 * window.
6467 * Only these fields are used:
6468 * fr_layout
6469 * fr_width
6470 * fr_height
6471 * fr_next
6472 * fr_child
6473 * fr_win (only valid for the old curwin, NULL otherwise)
6474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475
6476/*
6477 * Create a snapshot of the current frame sizes.
6478 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006479 void
6480make_snapshot(idx)
6481 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006483 clear_snapshot(curtab, idx);
6484 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485}
6486
6487 static void
6488make_snapshot_rec(fr, frp)
6489 frame_T *fr;
6490 frame_T **frp;
6491{
6492 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6493 if (*frp == NULL)
6494 return;
6495 (*frp)->fr_layout = fr->fr_layout;
6496# ifdef FEAT_VERTSPLIT
6497 (*frp)->fr_width = fr->fr_width;
6498# endif
6499 (*frp)->fr_height = fr->fr_height;
6500 if (fr->fr_next != NULL)
6501 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6502 if (fr->fr_child != NULL)
6503 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6504 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6505 (*frp)->fr_win = curwin;
6506}
6507
6508/*
6509 * Remove any existing snapshot.
6510 */
6511 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006512clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006513 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006514 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006516 clear_snapshot_rec(tp->tp_snapshot[idx]);
6517 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518}
6519
6520 static void
6521clear_snapshot_rec(fr)
6522 frame_T *fr;
6523{
6524 if (fr != NULL)
6525 {
6526 clear_snapshot_rec(fr->fr_next);
6527 clear_snapshot_rec(fr->fr_child);
6528 vim_free(fr);
6529 }
6530}
6531
6532/*
6533 * Restore a previously created snapshot, if there is any.
6534 * This is only done if the screen size didn't change and the window layout is
6535 * still the same.
6536 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006537 void
6538restore_snapshot(idx, close_curwin)
6539 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 int close_curwin; /* closing current window */
6541{
6542 win_T *wp;
6543
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006544 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006546 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006548 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6549 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006551 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 win_comp_pos();
6553 if (wp != NULL && close_curwin)
6554 win_goto(wp);
6555 redraw_all_later(CLEAR);
6556 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006557 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558}
6559
6560/*
6561 * Check if frames "sn" and "fr" have the same layout, same following frames
6562 * and same children.
6563 */
6564 static int
6565check_snapshot_rec(sn, fr)
6566 frame_T *sn;
6567 frame_T *fr;
6568{
6569 if (sn->fr_layout != fr->fr_layout
6570 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6571 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6572 || (sn->fr_next != NULL
6573 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6574 || (sn->fr_child != NULL
6575 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6576 return FAIL;
6577 return OK;
6578}
6579
6580/*
6581 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6582 * following frames and children.
6583 * Returns a pointer to the old current window, or NULL.
6584 */
6585 static win_T *
6586restore_snapshot_rec(sn, fr)
6587 frame_T *sn;
6588 frame_T *fr;
6589{
6590 win_T *wp = NULL;
6591 win_T *wp2;
6592
6593 fr->fr_height = sn->fr_height;
6594# ifdef FEAT_VERTSPLIT
6595 fr->fr_width = sn->fr_width;
6596# endif
6597 if (fr->fr_layout == FR_LEAF)
6598 {
6599 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6600# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006601 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602# endif
6603 wp = sn->fr_win;
6604 }
6605 if (sn->fr_next != NULL)
6606 {
6607 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6608 if (wp2 != NULL)
6609 wp = wp2;
6610 }
6611 if (sn->fr_child != NULL)
6612 {
6613 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6614 if (wp2 != NULL)
6615 wp = wp2;
6616 }
6617 return wp;
6618}
6619
6620#endif
6621
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006622#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6623 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006624/*
6625 * Set "win" to be the curwin and "tp" to be the current tab page.
6626 * restore_win() MUST be called to undo.
6627 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006628 * When "no_display" is TRUE the display won't be affected, no redraw is
6629 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006630 * Returns FAIL if switching to "win" failed.
6631 */
6632 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006633switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006634 win_T **save_curwin UNUSED;
6635 tabpage_T **save_curtab UNUSED;
6636 win_T *win UNUSED;
6637 tabpage_T *tp UNUSED;
6638 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006639{
6640# ifdef FEAT_AUTOCMD
6641 block_autocmds();
6642# endif
6643# ifdef FEAT_WINDOWS
6644 *save_curwin = curwin;
6645 if (tp != NULL)
6646 {
6647 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006648 if (no_display)
6649 {
6650 curtab->tp_firstwin = firstwin;
6651 curtab->tp_lastwin = lastwin;
6652 curtab = tp;
6653 firstwin = curtab->tp_firstwin;
6654 lastwin = curtab->tp_lastwin;
6655 }
6656 else
6657 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006658 }
6659 if (!win_valid(win))
6660 {
6661# ifdef FEAT_AUTOCMD
6662 unblock_autocmds();
6663# endif
6664 return FAIL;
6665 }
6666 curwin = win;
6667 curbuf = curwin->w_buffer;
6668# endif
6669 return OK;
6670}
6671
6672/*
6673 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006674 * When "no_display" is TRUE the display won't be affected, no redraw is
6675 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006676 */
6677 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006678restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006679 win_T *save_curwin UNUSED;
6680 tabpage_T *save_curtab UNUSED;
6681 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006682{
6683# ifdef FEAT_WINDOWS
6684 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006685 {
6686 if (no_display)
6687 {
6688 curtab->tp_firstwin = firstwin;
6689 curtab->tp_lastwin = lastwin;
6690 curtab = save_curtab;
6691 firstwin = curtab->tp_firstwin;
6692 lastwin = curtab->tp_lastwin;
6693 }
6694 else
6695 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6696 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006697 if (win_valid(save_curwin))
6698 {
6699 curwin = save_curwin;
6700 curbuf = curwin->w_buffer;
6701 }
6702# endif
6703# ifdef FEAT_AUTOCMD
6704 unblock_autocmds();
6705# endif
6706}
6707
6708/*
6709 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6710 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6711 */
6712 void
6713switch_buffer(save_curbuf, buf)
6714 buf_T *buf;
6715 buf_T **save_curbuf;
6716{
6717# ifdef FEAT_AUTOCMD
6718 block_autocmds();
6719# endif
6720 *save_curbuf = curbuf;
6721 --curbuf->b_nwindows;
6722 curbuf = buf;
6723 curwin->w_buffer = buf;
6724 ++curbuf->b_nwindows;
6725}
6726
6727/*
6728 * Restore the current buffer after using switch_buffer().
6729 */
6730 void
6731restore_buffer(save_curbuf)
6732 buf_T *save_curbuf;
6733{
6734# ifdef FEAT_AUTOCMD
6735 unblock_autocmds();
6736# endif
6737 /* Check for valid buffer, just in case. */
6738 if (buf_valid(save_curbuf))
6739 {
6740 --curbuf->b_nwindows;
6741 curwin->w_buffer = save_curbuf;
6742 curbuf = save_curbuf;
6743 ++curbuf->b_nwindows;
6744 }
6745}
6746#endif
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6749/*
6750 * Return TRUE if there is any vertically split window.
6751 */
6752 int
6753win_hasvertsplit()
6754{
6755 frame_T *fr;
6756
6757 if (topframe->fr_layout == FR_ROW)
6758 return TRUE;
6759
6760 if (topframe->fr_layout == FR_COL)
6761 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6762 if (fr->fr_layout == FR_ROW)
6763 return TRUE;
6764
6765 return FALSE;
6766}
6767#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006768
6769#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6770/*
6771 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006772 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006773 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6774 * If no particular ID is desired, -1 must be specified for 'id'.
6775 * Return ID of added match, -1 on failure.
6776 */
6777 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006778match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006779 win_T *wp;
6780 char_u *grp;
6781 char_u *pat;
6782 int prio;
6783 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006784 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006785{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006786 matchitem_T *cur;
6787 matchitem_T *prev;
6788 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006789 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006790 regprog_T *regprog = NULL;
6791 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006792
Bram Moolenaarb3414592014-06-17 17:48:32 +02006793 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006794 return -1;
6795 if (id < -1 || id == 0)
6796 {
6797 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6798 return -1;
6799 }
6800 if (id != -1)
6801 {
6802 cur = wp->w_match_head;
6803 while (cur != NULL)
6804 {
6805 if (cur->id == id)
6806 {
6807 EMSGN("E801: ID already taken: %ld", id);
6808 return -1;
6809 }
6810 cur = cur->next;
6811 }
6812 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006813 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006814 {
6815 EMSG2(_(e_nogroup), grp);
6816 return -1;
6817 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006818 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006819 {
6820 EMSG2(_(e_invarg2), pat);
6821 return -1;
6822 }
6823
6824 /* Find available match ID. */
6825 while (id == -1)
6826 {
6827 cur = wp->w_match_head;
6828 while (cur != NULL && cur->id != wp->w_next_match_id)
6829 cur = cur->next;
6830 if (cur == NULL)
6831 id = wp->w_next_match_id;
6832 wp->w_next_match_id++;
6833 }
6834
6835 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006836 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006837 m->id = id;
6838 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006839 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006840 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006841 m->match.regprog = regprog;
6842 m->match.rmm_ic = FALSE;
6843 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006844
Bram Moolenaarb3414592014-06-17 17:48:32 +02006845 /* Set up position matches */
6846 if (pos_list != NULL)
6847 {
6848 linenr_T toplnum = 0;
6849 linenr_T botlnum = 0;
6850 listitem_T *li;
6851 int i;
6852
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006853 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006854 i++, li = li->li_next)
6855 {
6856 linenr_T lnum = 0;
6857 colnr_T col = 0;
6858 int len = 1;
6859 list_T *subl;
6860 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006861 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006862
Bram Moolenaarb3414592014-06-17 17:48:32 +02006863 if (li->li_tv.v_type == VAR_LIST)
6864 {
6865 subl = li->li_tv.vval.v_list;
6866 if (subl == NULL)
6867 goto fail;
6868 subli = subl->lv_first;
6869 if (subli == NULL)
6870 goto fail;
6871 lnum = get_tv_number_chk(&subli->li_tv, &error);
6872 if (error == TRUE)
6873 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006874 if (lnum == 0)
6875 {
6876 --i;
6877 continue;
6878 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006879 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006880 subli = subli->li_next;
6881 if (subli != NULL)
6882 {
6883 col = get_tv_number_chk(&subli->li_tv, &error);
6884 if (error == TRUE)
6885 goto fail;
6886 subli = subli->li_next;
6887 if (subli != NULL)
6888 {
6889 len = get_tv_number_chk(&subli->li_tv, &error);
6890 if (error == TRUE)
6891 goto fail;
6892 }
6893 }
6894 m->pos.pos[i].col = col;
6895 m->pos.pos[i].len = len;
6896 }
6897 else if (li->li_tv.v_type == VAR_NUMBER)
6898 {
6899 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006900 {
6901 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006902 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006903 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006904 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6905 m->pos.pos[i].col = 0;
6906 m->pos.pos[i].len = 0;
6907 }
6908 else
6909 {
6910 EMSG(_("List or number required"));
6911 goto fail;
6912 }
6913 if (toplnum == 0 || lnum < toplnum)
6914 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006915 if (botlnum == 0 || lnum >= botlnum)
6916 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006917 }
6918
6919 /* Calculate top and bottom lines for redrawing area */
6920 if (toplnum != 0)
6921 {
6922 if (wp->w_buffer->b_mod_set)
6923 {
6924 if (wp->w_buffer->b_mod_top > toplnum)
6925 wp->w_buffer->b_mod_top = toplnum;
6926 if (wp->w_buffer->b_mod_bot < botlnum)
6927 wp->w_buffer->b_mod_bot = botlnum;
6928 }
6929 else
6930 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006931 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006932 wp->w_buffer->b_mod_top = toplnum;
6933 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006934 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006935 }
6936 m->pos.toplnum = toplnum;
6937 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006938 rtype = VALID;
6939 }
6940 }
6941
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006942 /* Insert new match. The match list is in ascending order with regard to
6943 * the match priorities. */
6944 cur = wp->w_match_head;
6945 prev = cur;
6946 while (cur != NULL && prio >= cur->priority)
6947 {
6948 prev = cur;
6949 cur = cur->next;
6950 }
6951 if (cur == prev)
6952 wp->w_match_head = m;
6953 else
6954 prev->next = m;
6955 m->next = cur;
6956
Bram Moolenaarb3414592014-06-17 17:48:32 +02006957 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006958 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006959
6960fail:
6961 vim_free(m);
6962 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006963}
6964
6965/*
6966 * Delete match with ID 'id' in the match list of window 'wp'.
6967 * Print error messages if 'perr' is TRUE.
6968 */
6969 int
6970match_delete(wp, id, perr)
6971 win_T *wp;
6972 int id;
6973 int perr;
6974{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006975 matchitem_T *cur = wp->w_match_head;
6976 matchitem_T *prev = cur;
6977 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006978
6979 if (id < 1)
6980 {
6981 if (perr == TRUE)
6982 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6983 id);
6984 return -1;
6985 }
6986 while (cur != NULL && cur->id != id)
6987 {
6988 prev = cur;
6989 cur = cur->next;
6990 }
6991 if (cur == NULL)
6992 {
6993 if (perr == TRUE)
6994 EMSGN("E803: ID not found: %ld", id);
6995 return -1;
6996 }
6997 if (cur == prev)
6998 wp->w_match_head = cur->next;
6999 else
7000 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007001 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007002 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007003 if (cur->pos.toplnum != 0)
7004 {
7005 if (wp->w_buffer->b_mod_set)
7006 {
7007 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7008 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7009 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7010 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7011 }
7012 else
7013 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007014 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007015 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7016 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007017 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007018 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007019 rtype = VALID;
7020 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007021 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007022 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007023 return 0;
7024}
7025
7026/*
7027 * Delete all matches in the match list of window 'wp'.
7028 */
7029 void
7030clear_matches(wp)
7031 win_T *wp;
7032{
7033 matchitem_T *m;
7034
7035 while (wp->w_match_head != NULL)
7036 {
7037 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007038 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007039 vim_free(wp->w_match_head->pattern);
7040 vim_free(wp->w_match_head);
7041 wp->w_match_head = m;
7042 }
7043 redraw_later(SOME_VALID);
7044}
7045
7046/*
7047 * Get match from ID 'id' in window 'wp'.
7048 * Return NULL if match not found.
7049 */
7050 matchitem_T *
7051get_match(wp, id)
7052 win_T *wp;
7053 int id;
7054{
7055 matchitem_T *cur = wp->w_match_head;
7056
7057 while (cur != NULL && cur->id != id)
7058 cur = cur->next;
7059 return cur;
7060}
7061#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007062
7063#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7064 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007065get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007066{
7067 int i = 1;
7068 win_T *w;
7069
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007070 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007071 ++i;
7072
7073 if (w == NULL)
7074 return 0;
7075 else
7076 return i;
7077}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007078
7079 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007080get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007081{
7082 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007083# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007084 tabpage_T *t;
7085
7086 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7087 ++i;
7088
7089 if (t == NULL)
7090 return 0;
7091 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007092# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007093 return i;
7094}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007095#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007096
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007097#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007098/*
7099 * Return TRUE if "topfrp" and its children are at the right height.
7100 */
7101 static int
7102frame_check_height(topfrp, height)
7103 frame_T *topfrp;
7104 int height;
7105{
7106 frame_T *frp;
7107
7108 if (topfrp->fr_height != height)
7109 return FALSE;
7110
7111 if (topfrp->fr_layout == FR_ROW)
7112 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7113 if (frp->fr_height != height)
7114 return FALSE;
7115
7116 return TRUE;
7117}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007118#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007119
7120#ifdef FEAT_VERTSPLIT
7121/*
7122 * Return TRUE if "topfrp" and its children are at the right width.
7123 */
7124 static int
7125frame_check_width(topfrp, width)
7126 frame_T *topfrp;
7127 int width;
7128{
7129 frame_T *frp;
7130
7131 if (topfrp->fr_width != width)
7132 return FALSE;
7133
7134 if (topfrp->fr_layout == FR_COL)
7135 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7136 if (frp->fr_width != width)
7137 return FALSE;
7138
7139 return TRUE;
7140}
7141#endif
7142