blob: 9fe56b1f88d5148c06a221293eb0d78afda7d974 [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 minwidth;
690 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200691 int wmw1;
692 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
694 if (flags & WSP_TOP)
695 oldwin = firstwin;
696 else if (flags & WSP_BOT)
697 oldwin = lastwin;
698 else
699 oldwin = curwin;
700
701 /* add a status line when p_ls == 1 and splitting the first window */
702 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
703 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100704 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 EMSG(_(e_noroom));
707 return FAIL;
708 }
709 need_status = STATUS_HEIGHT;
710 }
711
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000712#ifdef FEAT_GUI
713 /* May be needed for the scrollbars that are going to change. */
714 if (gui.in_use)
715 out_flush();
716#endif
717
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef FEAT_VERTSPLIT
719 if (flags & WSP_VERT)
720 {
721 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
723 /*
724 * Check if we are able to split the current window and compute its
725 * width.
726 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200727 /* Current window requires at least 1 space. */
728 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
729 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200731 needed += p_wiw - wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
733 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200734 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200736 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 }
738 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200739 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200740 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
741 available = oldwin->w_frame->fr_width;
742 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200743 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100744 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 EMSG(_(e_noroom));
747 return FAIL;
748 }
749 if (new_size == 0)
750 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200751 if (new_size > available - minwidth - 1)
752 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200753 if (new_size < wmw1)
754 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756 /* if it doesn't fit in the current window, need win_equal() */
757 if (oldwin->w_width - new_size - 1 < p_wmw)
758 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000759
760 /* We don't like to take lines for the new window from a
761 * 'winfixwidth' window. Take them from a window to the left or right
762 * instead, if possible. */
763 if (oldwin->w_p_wfw)
764 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000765
766 /* Only make all windows the same width if one of them (except oldwin)
767 * is wider than one of the split windows. */
768 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
769 && oldwin->w_frame->fr_parent != NULL)
770 {
771 frp = oldwin->w_frame->fr_parent->fr_child;
772 while (frp != NULL)
773 {
774 if (frp->fr_win != oldwin && frp->fr_win != NULL
775 && (frp->fr_win->w_width > new_size
776 || frp->fr_win->w_width > oldwin->w_width
777 - new_size - STATUS_HEIGHT))
778 {
779 do_equal = TRUE;
780 break;
781 }
782 frp = frp->fr_next;
783 }
784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 else
787#endif
788 {
789 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 /*
792 * Check if we are able to split the current window and compute its
793 * height.
794 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200795 /* Current window requires at least 1 space. */
796 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
797 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200799 needed += p_wh - wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
801 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200802 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200804 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806 else
807 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200808 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
809 available = oldwin->w_frame->fr_height;
810 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100812 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
814 EMSG(_(e_noroom));
815 return FAIL;
816 }
817 oldwin_height = oldwin->w_height;
818 if (need_status)
819 {
820 oldwin->w_status_height = STATUS_HEIGHT;
821 oldwin_height -= STATUS_HEIGHT;
822 }
823 if (new_size == 0)
824 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200825 if (new_size > available - minheight - STATUS_HEIGHT)
826 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200827 if (new_size < wmh1)
828 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
830 /* if it doesn't fit in the current window, need win_equal() */
831 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
832 do_equal = TRUE;
833
834 /* We don't like to take lines for the new window from a
835 * 'winfixheight' window. Take them from a window above or below
836 * instead, if possible. */
837 if (oldwin->w_p_wfh)
838 {
839 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
840 oldwin);
841 oldwin_height = oldwin->w_height;
842 if (need_status)
843 oldwin_height -= STATUS_HEIGHT;
844 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000845
846 /* Only make all windows the same height if one of them (except oldwin)
847 * is higher than one of the split windows. */
848 if (!do_equal && p_ea && size == 0
849#ifdef FEAT_VERTSPLIT
850 && *p_ead != 'h'
851#endif
852 && oldwin->w_frame->fr_parent != NULL)
853 {
854 frp = oldwin->w_frame->fr_parent->fr_child;
855 while (frp != NULL)
856 {
857 if (frp->fr_win != oldwin && frp->fr_win != NULL
858 && (frp->fr_win->w_height > new_size
859 || frp->fr_win->w_height > oldwin_height - new_size
860 - STATUS_HEIGHT))
861 {
862 do_equal = TRUE;
863 break;
864 }
865 frp = frp->fr_next;
866 }
867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869
870 /*
871 * allocate new window structure and link it in the window list
872 */
873 if ((flags & WSP_TOP) == 0
874 && ((flags & WSP_BOT)
875 || (flags & WSP_BELOW)
876 || (!(flags & WSP_ABOVE)
877 && (
878#ifdef FEAT_VERTSPLIT
879 (flags & WSP_VERT) ? p_spr :
880#endif
881 p_sb))))
882 {
883 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100884 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000885 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 else
887 win_append(oldwin, wp);
888 }
889 else
890 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100891 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000892 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 else
894 win_append(oldwin->w_prev, wp);
895 }
896
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100897 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
899 if (wp == NULL)
900 return FAIL;
901
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000902 new_frame(wp);
903 if (wp->w_frame == NULL)
904 {
905 win_free(wp, NULL);
906 return FAIL;
907 }
908
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000909 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000910 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912
913 /*
914 * Reorganise the tree of frames to insert the new window.
915 */
916 if (flags & (WSP_TOP | WSP_BOT))
917 {
918#ifdef FEAT_VERTSPLIT
919 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
920 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
921#else
922 if (topframe->fr_layout == FR_COL)
923#endif
924 {
925 curfrp = topframe->fr_child;
926 if (flags & WSP_BOT)
927 while (curfrp->fr_next != NULL)
928 curfrp = curfrp->fr_next;
929 }
930 else
931 curfrp = topframe;
932 before = (flags & WSP_TOP);
933 }
934 else
935 {
936 curfrp = oldwin->w_frame;
937 if (flags & WSP_BELOW)
938 before = FALSE;
939 else if (flags & WSP_ABOVE)
940 before = TRUE;
941 else
942#ifdef FEAT_VERTSPLIT
943 if (flags & WSP_VERT)
944 before = !p_spr;
945 else
946#endif
947 before = !p_sb;
948 }
949 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
950 {
951 /* Need to create a new frame in the tree to make a branch. */
952 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
953 *frp = *curfrp;
954 curfrp->fr_layout = layout;
955 frp->fr_parent = curfrp;
956 frp->fr_next = NULL;
957 frp->fr_prev = NULL;
958 curfrp->fr_child = frp;
959 curfrp->fr_win = NULL;
960 curfrp = frp;
961 if (frp->fr_win != NULL)
962 oldwin->w_frame = frp;
963 else
964 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
965 frp->fr_parent = curfrp;
966 }
967
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100968 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000969 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100971 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 frp->fr_parent = curfrp->fr_parent;
973
974 /* Insert the new frame at the right place in the frame list. */
975 if (before)
976 frame_insert(curfrp, frp);
977 else
978 frame_append(curfrp, frp);
979
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100980 /* Set w_fraction now so that the cursor keeps the same relative
981 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100982 if (oldwin->w_height > 0)
983 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100984 wp->w_fraction = oldwin->w_fraction;
985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef FEAT_VERTSPLIT
987 if (flags & WSP_VERT)
988 {
989 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (need_status)
992 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100993 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 oldwin->w_status_height = need_status;
995 }
996 if (flags & (WSP_TOP | WSP_BOT))
997 {
998 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000999 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001000 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 wp->w_status_height = (p_ls > 0);
1002 }
1003 else
1004 {
1005 /* height and row of new window is same as current window */
1006 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001007 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 wp->w_status_height = oldwin->w_status_height;
1009 }
1010 frp->fr_height = curfrp->fr_height;
1011
1012 /* "new_size" of the current window goes to the new window, use
1013 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001014 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 if (before)
1016 wp->w_vsep_width = 1;
1017 else
1018 {
1019 wp->w_vsep_width = oldwin->w_vsep_width;
1020 oldwin->w_vsep_width = 1;
1021 }
1022 if (flags & (WSP_TOP | WSP_BOT))
1023 {
1024 if (flags & WSP_BOT)
1025 frame_add_vsep(curfrp);
1026 /* Set width of neighbor frame */
1027 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001028 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1029 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001032 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (before) /* new window left of current one */
1034 {
1035 wp->w_wincol = oldwin->w_wincol;
1036 oldwin->w_wincol += new_size + 1;
1037 }
1038 else /* new window right of current one */
1039 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1040 frame_fix_width(oldwin);
1041 frame_fix_width(wp);
1042 }
1043 else
1044#endif
1045 {
1046 /* width and column of new window is same as current window */
1047#ifdef FEAT_VERTSPLIT
1048 if (flags & (WSP_TOP | WSP_BOT))
1049 {
1050 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001051 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 wp->w_vsep_width = 0;
1053 }
1054 else
1055 {
1056 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001057 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 wp->w_vsep_width = oldwin->w_vsep_width;
1059 }
1060 frp->fr_width = curfrp->fr_width;
1061#endif
1062
1063 /* "new_size" of the current window goes to the new window, use
1064 * one row for the status line */
1065 win_new_height(wp, new_size);
1066 if (flags & (WSP_TOP | WSP_BOT))
1067 frame_new_height(curfrp, curfrp->fr_height
1068 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1069 else
1070 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1071 if (before) /* new window above current one */
1072 {
1073 wp->w_winrow = oldwin->w_winrow;
1074 wp->w_status_height = STATUS_HEIGHT;
1075 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1076 }
1077 else /* new window below current one */
1078 {
1079 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1080 wp->w_status_height = oldwin->w_status_height;
1081 oldwin->w_status_height = STATUS_HEIGHT;
1082 }
1083#ifdef FEAT_VERTSPLIT
1084 if (flags & WSP_BOT)
1085 frame_add_statusline(curfrp);
1086#endif
1087 frame_fix_height(wp);
1088 frame_fix_height(oldwin);
1089 }
1090
1091 if (flags & (WSP_TOP | WSP_BOT))
1092 (void)win_comp_pos();
1093
1094 /*
1095 * Both windows need redrawing
1096 */
1097 redraw_win_later(wp, NOT_VALID);
1098 wp->w_redr_status = TRUE;
1099 redraw_win_later(oldwin, NOT_VALID);
1100 oldwin->w_redr_status = TRUE;
1101
1102 if (need_status)
1103 {
1104 msg_row = Rows - 1;
1105 msg_col = sc_col;
1106 msg_clr_eos_force(); /* Old command/ruler may still be there */
1107 comp_col();
1108 msg_row = Rows - 1;
1109 msg_col = 0; /* put position back at start of line */
1110 }
1111
1112 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001113 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
1115 if (do_equal || dir != 0)
1116 win_equal(wp, TRUE,
1117#ifdef FEAT_VERTSPLIT
1118 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1119 : dir == 'h' ? 'b' :
1120#endif
1121 'v');
1122
1123 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1124 * size was given. */
1125#ifdef FEAT_VERTSPLIT
1126 if (flags & WSP_VERT)
1127 {
1128 i = p_wiw;
1129 if (size != 0)
1130 p_wiw = size;
1131
1132# ifdef FEAT_GUI
1133 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1134 if (gui.in_use)
1135 gui_init_which_components(NULL);
1136# endif
1137 }
1138 else
1139#endif
1140 {
1141 i = p_wh;
1142 if (size != 0)
1143 p_wh = size;
1144 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001145
1146 /*
1147 * make the new window the current window
1148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 win_enter(wp, FALSE);
1150#ifdef FEAT_VERTSPLIT
1151 if (flags & WSP_VERT)
1152 p_wiw = i;
1153 else
1154#endif
1155 p_wh = i;
1156
1157 return OK;
1158}
1159
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001160
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001161/*
1162 * Initialize window "newp" from window "oldp".
1163 * Used when splitting a window and when creating a new tab page.
1164 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001165 * WSP_NEWLOC may be specified in flags to prevent the location list from
1166 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001167 */
1168 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001169win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001170 win_T *newp;
1171 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001172 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001173{
1174 int i;
1175
1176 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001177#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001178 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001179#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001180 oldp->w_buffer->b_nwindows++;
1181 newp->w_cursor = oldp->w_cursor;
1182 newp->w_valid = 0;
1183 newp->w_curswant = oldp->w_curswant;
1184 newp->w_set_curswant = oldp->w_set_curswant;
1185 newp->w_topline = oldp->w_topline;
1186#ifdef FEAT_DIFF
1187 newp->w_topfill = oldp->w_topfill;
1188#endif
1189 newp->w_leftcol = oldp->w_leftcol;
1190 newp->w_pcmark = oldp->w_pcmark;
1191 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1192 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001193 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001194 newp->w_fraction = oldp->w_fraction;
1195 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1196#ifdef FEAT_JUMPLIST
1197 copy_jumplist(oldp, newp);
1198#endif
1199#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001200 if (flags & WSP_NEWLOC)
1201 {
1202 /* Don't copy the location list. */
1203 newp->w_llist = NULL;
1204 newp->w_llist_ref = NULL;
1205 }
1206 else
1207 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001208#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001209 newp->w_localdir = (oldp->w_localdir == NULL)
1210 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001211
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001212 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001213 for (i = 0; i < oldp->w_tagstacklen; i++)
1214 {
1215 newp->w_tagstack[i] = oldp->w_tagstack[i];
1216 if (newp->w_tagstack[i].tagname != NULL)
1217 newp->w_tagstack[i].tagname =
1218 vim_strsave(newp->w_tagstack[i].tagname);
1219 }
1220 newp->w_tagstackidx = oldp->w_tagstackidx;
1221 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001222#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001223 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001224#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001225
1226 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001227
Bram Moolenaara971b822011-09-14 14:43:25 +02001228#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001229 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001230#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001231}
1232
1233/*
1234 * Initialize window "newp" from window"old".
1235 * Only the essential things are copied.
1236 */
1237 static void
1238win_init_some(newp, oldp)
1239 win_T *newp;
1240 win_T *oldp;
1241{
1242 /* Use the same argument list. */
1243 newp->w_alist = oldp->w_alist;
1244 ++newp->w_alist->al_refcount;
1245 newp->w_arg_idx = oldp->w_arg_idx;
1246
1247 /* copy options from existing window */
1248 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001249}
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#endif /* FEAT_WINDOWS */
1252
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253#if defined(FEAT_WINDOWS) || defined(PROTO)
1254/*
1255 * Check if "win" is a pointer to an existing window.
1256 */
1257 int
1258win_valid(win)
1259 win_T *win;
1260{
1261 win_T *wp;
1262
1263 if (win == NULL)
1264 return FALSE;
1265 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1266 if (wp == win)
1267 return TRUE;
1268 return FALSE;
1269}
1270
1271/*
1272 * Return the number of windows.
1273 */
1274 int
1275win_count()
1276{
1277 win_T *wp;
1278 int count = 0;
1279
1280 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1281 ++count;
1282 return count;
1283}
1284
1285/*
1286 * Make "count" windows on the screen.
1287 * Return actual number of windows on the screen.
1288 * Must be called when there is just one window, filling the whole screen
1289 * (excluding the command line).
1290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 int
1292make_windows(count, vertical)
1293 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001294 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295{
1296 int maxcount;
1297 int todo;
1298
1299#ifdef FEAT_VERTSPLIT
1300 if (vertical)
1301 {
1302 /* Each windows needs at least 'winminwidth' lines and a separator
1303 * column. */
1304 maxcount = (curwin->w_width + curwin->w_vsep_width
1305 - (p_wiw - p_wmw)) / (p_wmw + 1);
1306 }
1307 else
1308#endif
1309 {
1310 /* Each window needs at least 'winminheight' lines and a status line. */
1311 maxcount = (curwin->w_height + curwin->w_status_height
1312 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1313 }
1314
1315 if (maxcount < 2)
1316 maxcount = 2;
1317 if (count > maxcount)
1318 count = maxcount;
1319
1320 /*
1321 * add status line now, otherwise first window will be too big
1322 */
1323 if (count > 1)
1324 last_status(TRUE);
1325
1326#ifdef FEAT_AUTOCMD
1327 /*
1328 * Don't execute autocommands while creating the windows. Must do that
1329 * when putting the buffers in the windows.
1330 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001331 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332#endif
1333
1334 /* todo is number of windows left to create */
1335 for (todo = count - 1; todo > 0; --todo)
1336#ifdef FEAT_VERTSPLIT
1337 if (vertical)
1338 {
1339 if (win_split(curwin->w_width - (curwin->w_width - todo)
1340 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1341 break;
1342 }
1343 else
1344#endif
1345 {
1346 if (win_split(curwin->w_height - (curwin->w_height - todo
1347 * STATUS_HEIGHT) / (todo + 1)
1348 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1349 break;
1350 }
1351
1352#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001353 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#endif
1355
1356 /* return actual number of windows */
1357 return (count - todo);
1358}
1359
1360/*
1361 * Exchange current and next window
1362 */
1363 static void
1364win_exchange(Prenum)
1365 long Prenum;
1366{
1367 frame_T *frp;
1368 frame_T *frp2;
1369 win_T *wp;
1370 win_T *wp2;
1371 int temp;
1372
1373 if (lastwin == firstwin) /* just one window */
1374 {
1375 beep_flush();
1376 return;
1377 }
1378
1379#ifdef FEAT_GUI
1380 need_mouse_correct = TRUE;
1381#endif
1382
1383 /*
1384 * find window to exchange with
1385 */
1386 if (Prenum)
1387 {
1388 frp = curwin->w_frame->fr_parent->fr_child;
1389 while (frp != NULL && --Prenum > 0)
1390 frp = frp->fr_next;
1391 }
1392 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1393 frp = curwin->w_frame->fr_next;
1394 else /* Swap last window in row/col with previous */
1395 frp = curwin->w_frame->fr_prev;
1396
1397 /* We can only exchange a window with another window, not with a frame
1398 * containing windows. */
1399 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1400 return;
1401 wp = frp->fr_win;
1402
1403/*
1404 * 1. remove curwin from the list. Remember after which window it was in wp2
1405 * 2. insert curwin before wp in the list
1406 * if wp != wp2
1407 * 3. remove wp from the list
1408 * 4. insert wp after wp2
1409 * 5. exchange the status line height and vsep width.
1410 */
1411 wp2 = curwin->w_prev;
1412 frp2 = curwin->w_frame->fr_prev;
1413 if (wp->w_prev != curwin)
1414 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001415 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 frame_remove(curwin->w_frame);
1417 win_append(wp->w_prev, curwin);
1418 frame_insert(frp, curwin->w_frame);
1419 }
1420 if (wp != wp2)
1421 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001422 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 frame_remove(wp->w_frame);
1424 win_append(wp2, wp);
1425 if (frp2 == NULL)
1426 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1427 else
1428 frame_append(frp2, wp->w_frame);
1429 }
1430 temp = curwin->w_status_height;
1431 curwin->w_status_height = wp->w_status_height;
1432 wp->w_status_height = temp;
1433#ifdef FEAT_VERTSPLIT
1434 temp = curwin->w_vsep_width;
1435 curwin->w_vsep_width = wp->w_vsep_width;
1436 wp->w_vsep_width = temp;
1437
1438 /* If the windows are not in the same frame, exchange the sizes to avoid
1439 * messing up the window layout. Otherwise fix the frame sizes. */
1440 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1441 {
1442 temp = curwin->w_height;
1443 curwin->w_height = wp->w_height;
1444 wp->w_height = temp;
1445 temp = curwin->w_width;
1446 curwin->w_width = wp->w_width;
1447 wp->w_width = temp;
1448 }
1449 else
1450 {
1451 frame_fix_height(curwin);
1452 frame_fix_height(wp);
1453 frame_fix_width(curwin);
1454 frame_fix_width(wp);
1455 }
1456#endif
1457
1458 (void)win_comp_pos(); /* recompute window positions */
1459
1460 win_enter(wp, TRUE);
1461 redraw_later(CLEAR);
1462}
1463
1464/*
1465 * rotate windows: if upwards TRUE the second window becomes the first one
1466 * if upwards FALSE the first window becomes the second one
1467 */
1468 static void
1469win_rotate(upwards, count)
1470 int upwards;
1471 int count;
1472{
1473 win_T *wp1;
1474 win_T *wp2;
1475 frame_T *frp;
1476 int n;
1477
1478 if (firstwin == lastwin) /* nothing to do */
1479 {
1480 beep_flush();
1481 return;
1482 }
1483
1484#ifdef FEAT_GUI
1485 need_mouse_correct = TRUE;
1486#endif
1487
1488#ifdef FEAT_VERTSPLIT
1489 /* Check if all frames in this row/col have one window. */
1490 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1491 frp = frp->fr_next)
1492 if (frp->fr_win == NULL)
1493 {
1494 EMSG(_("E443: Cannot rotate when another window is split"));
1495 return;
1496 }
1497#endif
1498
1499 while (count--)
1500 {
1501 if (upwards) /* first window becomes last window */
1502 {
1503 /* remove first window/frame from the list */
1504 frp = curwin->w_frame->fr_parent->fr_child;
1505 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001506 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 frame_remove(frp);
1508
1509 /* find last frame and append removed window/frame after it */
1510 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1511 ;
1512 win_append(frp->fr_win, wp1);
1513 frame_append(frp, wp1->w_frame);
1514
1515 wp2 = frp->fr_win; /* previously last window */
1516 }
1517 else /* last window becomes first window */
1518 {
1519 /* find last window/frame in the list and remove it */
1520 for (frp = curwin->w_frame; frp->fr_next != NULL;
1521 frp = frp->fr_next)
1522 ;
1523 wp1 = frp->fr_win;
1524 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001525 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 frame_remove(frp);
1527
1528 /* append the removed window/frame before the first in the list */
1529 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1530 frame_insert(frp->fr_parent->fr_child, frp);
1531 }
1532
1533 /* exchange status height and vsep width of old and new last window */
1534 n = wp2->w_status_height;
1535 wp2->w_status_height = wp1->w_status_height;
1536 wp1->w_status_height = n;
1537 frame_fix_height(wp1);
1538 frame_fix_height(wp2);
1539#ifdef FEAT_VERTSPLIT
1540 n = wp2->w_vsep_width;
1541 wp2->w_vsep_width = wp1->w_vsep_width;
1542 wp1->w_vsep_width = n;
1543 frame_fix_width(wp1);
1544 frame_fix_width(wp2);
1545#endif
1546
1547 /* recompute w_winrow and w_wincol for all windows */
1548 (void)win_comp_pos();
1549 }
1550
1551 redraw_later(CLEAR);
1552}
1553
1554/*
1555 * Move the current window to the very top/bottom/left/right of the screen.
1556 */
1557 static void
1558win_totop(size, flags)
1559 int size;
1560 int flags;
1561{
1562 int dir;
1563 int height = curwin->w_height;
1564
1565 if (lastwin == firstwin)
1566 {
1567 beep_flush();
1568 return;
1569 }
1570
1571 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001572 (void)winframe_remove(curwin, &dir, NULL);
1573 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 last_status(FALSE); /* may need to remove last status line */
1575 (void)win_comp_pos(); /* recompute window positions */
1576
1577 /* Split a window on the desired side and put the window there. */
1578 (void)win_split_ins(size, flags, curwin, dir);
1579 if (!(flags & WSP_VERT))
1580 {
1581 win_setheight(height);
1582 if (p_ea)
1583 win_equal(curwin, TRUE, 'v');
1584 }
1585
1586#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1587 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1588 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001589 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591}
1592
1593/*
1594 * Move window "win1" to below/right of "win2" and make "win1" the current
1595 * window. Only works within the same frame!
1596 */
1597 void
1598win_move_after(win1, win2)
1599 win_T *win1, *win2;
1600{
1601 int height;
1602
1603 /* check if the arguments are reasonable */
1604 if (win1 == win2)
1605 return;
1606
1607 /* check if there is something to do */
1608 if (win2->w_next != win1)
1609 {
1610 /* may need move the status line/vertical separator of the last window
1611 * */
1612 if (win1 == lastwin)
1613 {
1614 height = win1->w_prev->w_status_height;
1615 win1->w_prev->w_status_height = win1->w_status_height;
1616 win1->w_status_height = height;
1617#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001618 if (win1->w_prev->w_vsep_width == 1)
1619 {
1620 /* Remove the vertical separator from the last-but-one window,
1621 * add it to the last window. Adjust the frame widths. */
1622 win1->w_prev->w_vsep_width = 0;
1623 win1->w_prev->w_frame->fr_width -= 1;
1624 win1->w_vsep_width = 1;
1625 win1->w_frame->fr_width += 1;
1626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627#endif
1628 }
1629 else if (win2 == lastwin)
1630 {
1631 height = win1->w_status_height;
1632 win1->w_status_height = win2->w_status_height;
1633 win2->w_status_height = height;
1634#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001635 if (win1->w_vsep_width == 1)
1636 {
1637 /* Remove the vertical separator from win1, add it to the last
1638 * window, win2. Adjust the frame widths. */
1639 win2->w_vsep_width = 1;
1640 win2->w_frame->fr_width += 1;
1641 win1->w_vsep_width = 0;
1642 win1->w_frame->fr_width -= 1;
1643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
1645 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001646 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 frame_remove(win1->w_frame);
1648 win_append(win2, win1);
1649 frame_append(win2->w_frame, win1->w_frame);
1650
1651 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1652 redraw_later(NOT_VALID);
1653 }
1654 win_enter(win1, FALSE);
1655}
1656
1657/*
1658 * Make all windows the same height.
1659 * 'next_curwin' will soon be the current window, make sure it has enough
1660 * rows.
1661 */
1662 void
1663win_equal(next_curwin, current, dir)
1664 win_T *next_curwin; /* pointer to current window to be or NULL */
1665 int current; /* do only frame with current window */
1666 int dir; /* 'v' for vertically, 'h' for horizontally,
1667 'b' for both, 0 for using p_ead */
1668{
1669 if (dir == 0)
1670#ifdef FEAT_VERTSPLIT
1671 dir = *p_ead;
1672#else
1673 dir = 'b';
1674#endif
1675 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001676 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001677 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678}
1679
1680/*
1681 * Set a frame to a new position and height, spreading the available room
1682 * equally over contained frames.
1683 * The window "next_curwin" (if not NULL) should at least get the size from
1684 * 'winheight' and 'winwidth' if possible.
1685 */
1686 static void
1687win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1688 win_T *next_curwin; /* pointer to current window to be or NULL */
1689 int current; /* do only frame with current window */
1690 frame_T *topfr; /* frame to set size off */
1691 int dir; /* 'v', 'h' or 'b', see win_equal() */
1692 int col; /* horizontal position for frame */
1693 int row; /* vertical position for frame */
1694 int width; /* new width of frame */
1695 int height; /* new height of frame */
1696{
1697 int n, m;
1698 int extra_sep = 0;
1699 int wincount, totwincount = 0;
1700 frame_T *fr;
1701 int next_curwin_size = 0;
1702 int room = 0;
1703 int new_size;
1704 int has_next_curwin = 0;
1705 int hnc;
1706
1707 if (topfr->fr_layout == FR_LEAF)
1708 {
1709 /* Set the width/height of this frame.
1710 * Redraw when size or position changes */
1711 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1712#ifdef FEAT_VERTSPLIT
1713 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1714#endif
1715 )
1716 {
1717 topfr->fr_win->w_winrow = row;
1718 frame_new_height(topfr, height, FALSE, FALSE);
1719#ifdef FEAT_VERTSPLIT
1720 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001721 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722#endif
1723 redraw_all_later(CLEAR);
1724 }
1725 }
1726#ifdef FEAT_VERTSPLIT
1727 else if (topfr->fr_layout == FR_ROW)
1728 {
1729 topfr->fr_width = width;
1730 topfr->fr_height = height;
1731
1732 if (dir != 'v') /* equalize frame widths */
1733 {
1734 /* Compute the maximum number of windows horizontally in this
1735 * frame. */
1736 n = frame_minwidth(topfr, NOWIN);
1737 /* add one for the rightmost window, it doesn't have a separator */
1738 if (col + width == Columns)
1739 extra_sep = 1;
1740 else
1741 extra_sep = 0;
1742 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001743 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001745 /*
1746 * Compute width for "next_curwin" window and room available for
1747 * other windows.
1748 * "m" is the minimal width when counting p_wiw for "next_curwin".
1749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 m = frame_minwidth(topfr, next_curwin);
1751 room = width - m;
1752 if (room < 0)
1753 {
1754 next_curwin_size = p_wiw + room;
1755 room = 0;
1756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 else
1758 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001759 next_curwin_size = -1;
1760 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1761 {
1762 /* If 'winfixwidth' set keep the window width if
1763 * possible.
1764 * Watch out for this window being the next_curwin. */
1765 if (frame_fixed_width(fr))
1766 {
1767 n = frame_minwidth(fr, NOWIN);
1768 new_size = fr->fr_width;
1769 if (frame_has_win(fr, next_curwin))
1770 {
1771 room += p_wiw - p_wmw;
1772 next_curwin_size = 0;
1773 if (new_size < p_wiw)
1774 new_size = p_wiw;
1775 }
1776 else
1777 /* These windows don't use up room. */
1778 totwincount -= (n + (fr->fr_next == NULL
1779 ? extra_sep : 0)) / (p_wmw + 1);
1780 room -= new_size - n;
1781 if (room < 0)
1782 {
1783 new_size += room;
1784 room = 0;
1785 }
1786 fr->fr_newwidth = new_size;
1787 }
1788 }
1789 if (next_curwin_size == -1)
1790 {
1791 if (!has_next_curwin)
1792 next_curwin_size = 0;
1793 else if (totwincount > 1
1794 && (room + (totwincount - 2))
1795 / (totwincount - 1) > p_wiw)
1796 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001797 /* Can make all windows wider than 'winwidth', spread
1798 * the room equally. */
1799 next_curwin_size = (room + p_wiw
1800 + (totwincount - 1) * p_wmw
1801 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001802 room -= next_curwin_size - p_wiw;
1803 }
1804 else
1805 next_curwin_size = p_wiw;
1806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001808
1809 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 --totwincount; /* don't count curwin */
1811 }
1812
1813 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1814 {
1815 n = m = 0;
1816 wincount = 1;
1817 if (fr->fr_next == NULL)
1818 /* last frame gets all that remains (avoid roundoff error) */
1819 new_size = width;
1820 else if (dir == 'v')
1821 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001822 else if (frame_fixed_width(fr))
1823 {
1824 new_size = fr->fr_newwidth;
1825 wincount = 0; /* doesn't count as a sizeable window */
1826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 else
1828 {
1829 /* Compute the maximum number of windows horiz. in "fr". */
1830 n = frame_minwidth(fr, NOWIN);
1831 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1832 / (p_wmw + 1);
1833 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001834 if (has_next_curwin)
1835 hnc = frame_has_win(fr, next_curwin);
1836 else
1837 hnc = FALSE;
1838 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001840 if (totwincount == 0)
1841 new_size = room;
1842 else
1843 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001845 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
1847 next_curwin_size -= p_wiw - (m - n);
1848 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001849 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001851 else
1852 room -= new_size;
1853 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
1855
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001856 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 * window, unless equalizing all frames. */
1858 if (!current || dir != 'v' || topfr->fr_parent != NULL
1859 || (new_size != fr->fr_width)
1860 || frame_has_win(fr, next_curwin))
1861 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001862 new_size, height);
1863 col += new_size;
1864 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 totwincount -= wincount;
1866 }
1867 }
1868#endif
1869 else /* topfr->fr_layout == FR_COL */
1870 {
1871#ifdef FEAT_VERTSPLIT
1872 topfr->fr_width = width;
1873#endif
1874 topfr->fr_height = height;
1875
1876 if (dir != 'h') /* equalize frame heights */
1877 {
1878 /* Compute maximum number of windows vertically in this frame. */
1879 n = frame_minheight(topfr, NOWIN);
1880 /* add one for the bottom window if it doesn't have a statusline */
1881 if (row + height == cmdline_row && p_ls == 0)
1882 extra_sep = 1;
1883 else
1884 extra_sep = 0;
1885 totwincount = (n + extra_sep) / (p_wmh + 1);
1886 has_next_curwin = frame_has_win(topfr, next_curwin);
1887
1888 /*
1889 * Compute height for "next_curwin" window and room available for
1890 * other windows.
1891 * "m" is the minimal height when counting p_wh for "next_curwin".
1892 */
1893 m = frame_minheight(topfr, next_curwin);
1894 room = height - m;
1895 if (room < 0)
1896 {
1897 /* The room is less then 'winheight', use all space for the
1898 * current window. */
1899 next_curwin_size = p_wh + room;
1900 room = 0;
1901 }
1902 else
1903 {
1904 next_curwin_size = -1;
1905 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1906 {
1907 /* If 'winfixheight' set keep the window height if
1908 * possible.
1909 * Watch out for this window being the next_curwin. */
1910 if (frame_fixed_height(fr))
1911 {
1912 n = frame_minheight(fr, NOWIN);
1913 new_size = fr->fr_height;
1914 if (frame_has_win(fr, next_curwin))
1915 {
1916 room += p_wh - p_wmh;
1917 next_curwin_size = 0;
1918 if (new_size < p_wh)
1919 new_size = p_wh;
1920 }
1921 else
1922 /* These windows don't use up room. */
1923 totwincount -= (n + (fr->fr_next == NULL
1924 ? extra_sep : 0)) / (p_wmh + 1);
1925 room -= new_size - n;
1926 if (room < 0)
1927 {
1928 new_size += room;
1929 room = 0;
1930 }
1931 fr->fr_newheight = new_size;
1932 }
1933 }
1934 if (next_curwin_size == -1)
1935 {
1936 if (!has_next_curwin)
1937 next_curwin_size = 0;
1938 else if (totwincount > 1
1939 && (room + (totwincount - 2))
1940 / (totwincount - 1) > p_wh)
1941 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001942 /* can make all windows higher than 'winheight',
1943 * spread the room equally. */
1944 next_curwin_size = (room + p_wh
1945 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 + (totwincount - 1)) / totwincount;
1947 room -= next_curwin_size - p_wh;
1948 }
1949 else
1950 next_curwin_size = p_wh;
1951 }
1952 }
1953
1954 if (has_next_curwin)
1955 --totwincount; /* don't count curwin */
1956 }
1957
1958 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1959 {
1960 n = m = 0;
1961 wincount = 1;
1962 if (fr->fr_next == NULL)
1963 /* last frame gets all that remains (avoid roundoff error) */
1964 new_size = height;
1965 else if (dir == 'h')
1966 new_size = fr->fr_height;
1967 else if (frame_fixed_height(fr))
1968 {
1969 new_size = fr->fr_newheight;
1970 wincount = 0; /* doesn't count as a sizeable window */
1971 }
1972 else
1973 {
1974 /* Compute the maximum number of windows vert. in "fr". */
1975 n = frame_minheight(fr, NOWIN);
1976 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1977 / (p_wmh + 1);
1978 m = frame_minheight(fr, next_curwin);
1979 if (has_next_curwin)
1980 hnc = frame_has_win(fr, next_curwin);
1981 else
1982 hnc = FALSE;
1983 if (hnc) /* don't count next_curwin */
1984 --wincount;
1985 if (totwincount == 0)
1986 new_size = room;
1987 else
1988 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1989 / totwincount;
1990 if (hnc) /* add next_curwin size */
1991 {
1992 next_curwin_size -= p_wh - (m - n);
1993 new_size += next_curwin_size;
1994 room -= new_size - next_curwin_size;
1995 }
1996 else
1997 room -= new_size;
1998 new_size += n;
1999 }
2000 /* Skip frame that is full width when splitting or closing a
2001 * window, unless equalizing all frames. */
2002 if (!current || dir != 'h' || topfr->fr_parent != NULL
2003 || (new_size != fr->fr_height)
2004 || frame_has_win(fr, next_curwin))
2005 win_equal_rec(next_curwin, current, fr, dir, col, row,
2006 width, new_size);
2007 row += new_size;
2008 height -= new_size;
2009 totwincount -= wincount;
2010 }
2011 }
2012}
2013
2014/*
2015 * close all windows for buffer 'buf'
2016 */
2017 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002018close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002020 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002022 win_T *wp;
2023 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002024 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027
2028 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002030 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2031#ifdef FEAT_AUTOCMD
2032 && !(wp->w_closing || wp->w_buffer->b_closing)
2033#endif
2034 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002036 win_close(wp, FALSE);
2037
2038 /* Start all over, autocommands may change the window layout. */
2039 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 }
2041 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002042 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002044
2045 /* Also check windows in other tab pages. */
2046 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2047 {
2048 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002049 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002050 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002051 if (wp->w_buffer == buf
2052#ifdef FEAT_AUTOCMD
2053 && !(wp->w_closing || wp->w_buffer->b_closing)
2054#endif
2055 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002056 {
2057 win_close_othertab(wp, FALSE, tp);
2058
2059 /* Start all over, the tab page may be closed and
2060 * autocommands may change the window layout. */
2061 nexttp = first_tabpage;
2062 break;
2063 }
2064 }
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002067
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002068 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002069 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002070 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071}
2072
2073/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002074 * Return TRUE if the current window is the only window that exists (ignoring
2075 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002076 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002077 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002078 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002079last_window()
2080{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002081 return (one_window() && first_tabpage->tp_next == NULL);
2082}
2083
2084/*
2085 * Return TRUE if there is only one window other than "aucmd_win" in the
2086 * current tab page.
2087 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002088 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002089one_window()
2090{
2091#ifdef FEAT_AUTOCMD
2092 win_T *wp;
2093 int seen_one = FALSE;
2094
2095 FOR_ALL_WINDOWS(wp)
2096 {
2097 if (wp != aucmd_win)
2098 {
2099 if (seen_one)
2100 return FALSE;
2101 seen_one = TRUE;
2102 }
2103 }
2104 return TRUE;
2105#else
2106 return firstwin == lastwin;
2107#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002108}
2109
2110/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002111 * Close the possibly last window in a tab page.
2112 * Returns TRUE when the window was closed already.
2113 */
2114 static int
2115close_last_window_tabpage(win, free_buf, prev_curtab)
2116 win_T *win;
2117 int free_buf;
2118 tabpage_T *prev_curtab;
2119{
2120 if (firstwin == lastwin)
2121 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002122#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002123 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002124#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002125
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002126 /*
2127 * Closing the last window in a tab page. First go to another tab
2128 * page and then close the window and the tab page. This avoids that
2129 * curwin and curtab are invalid while we are freeing memory, they may
2130 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002131 * Don't trigger autocommands yet, they may use wrong values, so do
2132 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002133 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002134 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002135 redraw_tabline = TRUE;
2136
2137 /* Safety check: Autocommands may have closed the window when jumping
2138 * to the other tab page. */
2139 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2140 {
2141 int h = tabline_height();
2142
2143 win_close_othertab(win, free_buf, prev_curtab);
2144 if (h != tabline_height())
2145 shell_new_rows();
2146 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002147 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2148 * that now. */
2149#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002150 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002151 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2152 if (old_curbuf != curbuf)
2153 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002154#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002155 return TRUE;
2156 }
2157 return FALSE;
2158}
2159
2160/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002161 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 * If "free_buf" is TRUE related buffer may be unloaded.
2163 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002164 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002165 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002167 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168win_close(win, free_buf)
2169 win_T *win;
2170 int free_buf;
2171{
2172 win_T *wp;
2173#ifdef FEAT_AUTOCMD
2174 int other_buffer = FALSE;
2175#endif
2176 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 int dir;
2178 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002179 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002181 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
2183 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002184 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
2186
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002187#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002188 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002189 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002190 if (win == aucmd_win)
2191 {
2192 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002193 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002194 }
2195 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2196 {
2197 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002198 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002199 }
2200#endif
2201
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002202 /* When closing the last window in a tab page first go to another tab page
2203 * and then close the window and the tab page to avoid that curwin and
2204 * curtab are invalid while we are freeing memory. */
2205 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002206 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002207
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 /* When closing the help window, try restoring a snapshot after closing
2209 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002210 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 help_window = TRUE;
2212 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002213 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214
2215#ifdef FEAT_AUTOCMD
2216 if (win == curwin)
2217 {
2218 /*
2219 * Guess which window is going to be the new current window.
2220 * This may change because of the autocommands (sigh).
2221 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002222 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002225 * Be careful: If autocommands delete the window or cause this window
2226 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 */
2228 if (wp->w_buffer != curbuf)
2229 {
2230 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002231 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002233 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002234 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002235 win->w_closing = FALSE;
2236 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002237 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002239 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002241 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002242 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002243 win->w_closing = FALSE;
2244 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246# ifdef FEAT_EVAL
2247 /* autocmds may abort script processing */
2248 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002249 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250# endif
2251 }
2252#endif
2253
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002254#ifdef FEAT_GUI
2255 /* Avoid trouble with scrollbars that are going to be deleted in
2256 * win_free(). */
2257 if (gui.in_use)
2258 out_flush();
2259#endif
2260
Bram Moolenaara971b822011-09-14 14:43:25 +02002261#ifdef FEAT_SYN_HL
2262 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002263 if (win->w_buffer != NULL)
2264 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002265#endif
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 /*
2268 * Close the link to the buffer.
2269 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002270 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002271 {
2272#ifdef FEAT_AUTOCMD
2273 win->w_closing = TRUE;
2274#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002275 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002276#ifdef FEAT_AUTOCMD
2277 if (win_valid(win))
2278 win->w_closing = FALSE;
2279#endif
2280 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002281
Bram Moolenaar802418d2013-01-17 14:00:11 +01002282 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2283 && (last_window() || curtab != prev_curtab
2284 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002285 {
2286 /* Autocommands have close all windows, quit now. Restore
2287 * curwin->w_buffer, otherwise writing viminfo may fail. */
2288 if (curwin->w_buffer == NULL)
2289 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002290 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002291 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002292
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002294 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002295 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002296 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002297 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaara971b822011-09-14 14:43:25 +02002299 /* Free the memory used for the window and get the window that received
2300 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002301 wp = win_free_mem(win, &dir, NULL);
2302
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 /* Make sure curwin isn't invalid. It can cause severe trouble when
2304 * printing an error message. For win_equal() curbuf needs to be valid
2305 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002306 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 curwin = wp;
2309#ifdef FEAT_QUICKFIX
2310 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2311 {
2312 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002313 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 * finding another window to go to.
2315 */
2316 for (;;)
2317 {
2318 if (wp->w_next == NULL)
2319 wp = firstwin;
2320 else
2321 wp = wp->w_next;
2322 if (wp == curwin)
2323 break;
2324 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2325 {
2326 curwin = wp;
2327 break;
2328 }
2329 }
2330 }
2331#endif
2332 curbuf = curwin->w_buffer;
2333 close_curwin = TRUE;
2334 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002335 if (p_ea
2336#ifdef FEAT_VERTSPLIT
2337 && (*p_ead == 'b' || *p_ead == dir)
2338#endif
2339 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 win_equal(curwin, TRUE,
2341#ifdef FEAT_VERTSPLIT
2342 dir
2343#else
2344 0
2345#endif
2346 );
2347 else
2348 win_comp_pos();
2349 if (close_curwin)
2350 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002351 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#ifdef FEAT_AUTOCMD
2353 if (other_buffer)
2354 /* careful: after this wp and win may be invalid! */
2355 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2356#endif
2357 }
2358
2359 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002360 * If last window has a status line now and we don't want one,
2361 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 */
2363 last_status(FALSE);
2364
2365 /* After closing the help window, try restoring the window layout from
2366 * before it was opened. */
2367 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002368 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2371 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2372 if (gui.in_use && !win_hasvertsplit())
2373 gui_init_which_components(NULL);
2374#endif
2375
2376 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002377 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378}
2379
2380/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002381 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002382 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002383 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002384 * Caller must check if buffer is hidden and whether the tabline needs to be
2385 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002386 */
2387 void
2388win_close_othertab(win, free_buf, tp)
2389 win_T *win;
2390 int free_buf;
2391 tabpage_T *tp;
2392{
2393 win_T *wp;
2394 int dir;
2395 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002396 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002397
Bram Moolenaar362ce482012-06-06 19:02:45 +02002398#ifdef FEAT_AUTOCMD
2399 if (win->w_closing || win->w_buffer->b_closing)
2400 return; /* window is already being closed */
2401#endif
2402
Bram Moolenaarf740b292006-02-16 22:11:02 +00002403 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002404 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002405
2406 /* Careful: Autocommands may have closed the tab page or made it the
2407 * current tab page. */
2408 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2409 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002410 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002411 return;
2412
2413 /* Autocommands may have closed the window already. */
2414 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2415 ;
2416 if (wp == NULL)
2417 return;
2418
Bram Moolenaarf740b292006-02-16 22:11:02 +00002419 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002420 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002421 {
2422 if (tp == first_tabpage)
2423 first_tabpage = tp->tp_next;
2424 else
2425 {
2426 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2427 ptp = ptp->tp_next)
2428 ;
2429 if (ptp == NULL)
2430 {
2431 EMSG2(_(e_intern2), "win_close_othertab()");
2432 return;
2433 }
2434 ptp->tp_next = tp->tp_next;
2435 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002436 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002437 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002438
2439 /* Free the memory used for the window. */
2440 win_free_mem(win, &dir, tp);
2441
2442 if (free_tp)
2443 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002444}
2445
2446/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002447 * Free the memory used for a window.
2448 * Returns a pointer to the window that got the freed up space.
2449 */
2450 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002451win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002452 win_T *win;
2453 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002454 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002455{
2456 frame_T *frp;
2457 win_T *wp;
2458
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002459 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002460 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002461 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002462 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002463 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002464
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002465 /* When deleting the current window of another tab page select a new
2466 * current window. */
2467 if (tp != NULL && win == tp->tp_curwin)
2468 tp->tp_curwin = wp;
2469
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002470 return wp;
2471}
2472
2473#if defined(EXITFREE) || defined(PROTO)
2474 void
2475win_free_all()
2476{
2477 int dummy;
2478
Bram Moolenaarf740b292006-02-16 22:11:02 +00002479# ifdef FEAT_WINDOWS
2480 while (first_tabpage->tp_next != NULL)
2481 tabpage_close(TRUE);
2482# endif
2483
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002484# ifdef FEAT_AUTOCMD
2485 if (aucmd_win != NULL)
2486 {
2487 (void)win_free_mem(aucmd_win, &dummy, NULL);
2488 aucmd_win = NULL;
2489 }
2490# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002491
2492 while (firstwin != NULL)
2493 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002494
2495 /* No window should be used after this. Set curwin to NULL to crash
2496 * instead of using freed memory. */
2497 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002498}
2499#endif
2500
2501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 * Remove a window and its frame from the tree of frames.
2503 * Returns a pointer to the window that got the freed up space.
2504 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002505 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002506winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002508 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002509 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 frame_T *frp, *frp2, *frp3;
2512 frame_T *frp_close = win->w_frame;
2513 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
2515 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002516 * If there is only one window there is nothing to remove.
2517 */
2518 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2519 return NULL;
2520
2521 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 * Remove the window from its frame.
2523 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002524 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 wp = frame2win(frp2);
2526
2527 /* Remove this frame from the list of frames. */
2528 frame_remove(frp_close);
2529
2530#ifdef FEAT_VERTSPLIT
2531 if (frp_close->fr_parent->fr_layout == FR_COL)
2532 {
2533#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002534 /* When 'winfixheight' is set, try to find another frame in the column
2535 * (as close to the closed frame as possible) to distribute the height
2536 * to. */
2537 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2538 {
2539 frp = frp_close->fr_prev;
2540 frp3 = frp_close->fr_next;
2541 while (frp != NULL || frp3 != NULL)
2542 {
2543 if (frp != NULL)
2544 {
2545 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2546 {
2547 frp2 = frp;
2548 wp = frp->fr_win;
2549 break;
2550 }
2551 frp = frp->fr_prev;
2552 }
2553 if (frp3 != NULL)
2554 {
2555 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2556 {
2557 frp2 = frp3;
2558 wp = frp3->fr_win;
2559 break;
2560 }
2561 frp3 = frp3->fr_next;
2562 }
2563 }
2564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2566 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567#ifdef FEAT_VERTSPLIT
2568 *dirp = 'v';
2569 }
2570 else
2571 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002572 /* When 'winfixwidth' is set, try to find another frame in the column
2573 * (as close to the closed frame as possible) to distribute the width
2574 * to. */
2575 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2576 {
2577 frp = frp_close->fr_prev;
2578 frp3 = frp_close->fr_next;
2579 while (frp != NULL || frp3 != NULL)
2580 {
2581 if (frp != NULL)
2582 {
2583 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2584 {
2585 frp2 = frp;
2586 wp = frp->fr_win;
2587 break;
2588 }
2589 frp = frp->fr_prev;
2590 }
2591 if (frp3 != NULL)
2592 {
2593 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2594 {
2595 frp2 = frp3;
2596 wp = frp3->fr_win;
2597 break;
2598 }
2599 frp3 = frp3->fr_next;
2600 }
2601 }
2602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002604 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 *dirp = 'h';
2606 }
2607#endif
2608
2609 /* If rows/columns go to a window below/right its positions need to be
2610 * updated. Can only be done after the sizes have been updated. */
2611 if (frp2 == frp_close->fr_next)
2612 {
2613 int row = win->w_winrow;
2614 int col = W_WINCOL(win);
2615
2616 frame_comp_pos(frp2, &row, &col);
2617 }
2618
2619 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2620 {
2621 /* There is no other frame in this list, move its info to the parent
2622 * and remove it. */
2623 frp2->fr_parent->fr_layout = frp2->fr_layout;
2624 frp2->fr_parent->fr_child = frp2->fr_child;
2625 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2626 frp->fr_parent = frp2->fr_parent;
2627 frp2->fr_parent->fr_win = frp2->fr_win;
2628 if (frp2->fr_win != NULL)
2629 frp2->fr_win->w_frame = frp2->fr_parent;
2630 frp = frp2->fr_parent;
2631 vim_free(frp2);
2632
2633 frp2 = frp->fr_parent;
2634 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2635 {
2636 /* The frame above the parent has the same layout, have to merge
2637 * the frames into this list. */
2638 if (frp2->fr_child == frp)
2639 frp2->fr_child = frp->fr_child;
2640 frp->fr_child->fr_prev = frp->fr_prev;
2641 if (frp->fr_prev != NULL)
2642 frp->fr_prev->fr_next = frp->fr_child;
2643 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2644 {
2645 frp3->fr_parent = frp2;
2646 if (frp3->fr_next == NULL)
2647 {
2648 frp3->fr_next = frp->fr_next;
2649 if (frp->fr_next != NULL)
2650 frp->fr_next->fr_prev = frp3;
2651 break;
2652 }
2653 }
2654 vim_free(frp);
2655 }
2656 }
2657
2658 return wp;
2659}
2660
2661/*
2662 * Find out which frame is going to get the freed up space when "win" is
2663 * closed.
2664 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2665 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2666 * This makes opening a window and closing it immediately keep the same window
2667 * layout.
2668 */
2669 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002670win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002672 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 frame_T *frp;
2675 int b;
2676
Bram Moolenaarf740b292006-02-16 22:11:02 +00002677 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002678 /* Last window in this tab page, will go to next tab page. */
2679 return alt_tabpage()->tp_curwin->w_frame;
2680
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 frp = win->w_frame;
2682#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002683 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 b = p_spr;
2685 else
2686#endif
2687 b = p_sb;
2688 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2689 return frp->fr_next;
2690 return frp->fr_prev;
2691}
2692
2693/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002694 * Return the tabpage that will be used if the current one is closed.
2695 */
2696 static tabpage_T *
2697alt_tabpage()
2698{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002699 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002700
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002701 /* Use the next tab page if possible. */
2702 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002703 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002704
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002705 /* Find the last but one tab page. */
2706 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2707 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002708 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002709}
2710
2711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 * Find the left-upper window in frame "frp".
2713 */
2714 static win_T *
2715frame2win(frp)
2716 frame_T *frp;
2717{
2718 while (frp->fr_win == NULL)
2719 frp = frp->fr_child;
2720 return frp->fr_win;
2721}
2722
2723/*
2724 * Return TRUE if frame "frp" contains window "wp".
2725 */
2726 static int
2727frame_has_win(frp, wp)
2728 frame_T *frp;
2729 win_T *wp;
2730{
2731 frame_T *p;
2732
2733 if (frp->fr_layout == FR_LEAF)
2734 return frp->fr_win == wp;
2735
2736 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2737 if (frame_has_win(p, wp))
2738 return TRUE;
2739 return FALSE;
2740}
2741
2742/*
2743 * Set a new height for a frame. Recursively sets the height for contained
2744 * frames and windows. Caller must take care of positions.
2745 */
2746 static void
2747frame_new_height(topfrp, height, topfirst, wfh)
2748 frame_T *topfrp;
2749 int height;
2750 int topfirst; /* resize topmost contained frame first */
2751 int wfh; /* obey 'winfixheight' when there is a choice;
2752 may cause the height not to be set */
2753{
2754 frame_T *frp;
2755 int extra_lines;
2756 int h;
2757
2758 if (topfrp->fr_win != NULL)
2759 {
2760 /* Simple case: just one window. */
2761 win_new_height(topfrp->fr_win,
2762 height - topfrp->fr_win->w_status_height);
2763 }
2764#ifdef FEAT_VERTSPLIT
2765 else if (topfrp->fr_layout == FR_ROW)
2766 {
2767 do
2768 {
2769 /* All frames in this row get the same new height. */
2770 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2771 {
2772 frame_new_height(frp, height, topfirst, wfh);
2773 if (frp->fr_height > height)
2774 {
2775 /* Could not fit the windows, make the whole row higher. */
2776 height = frp->fr_height;
2777 break;
2778 }
2779 }
2780 }
2781 while (frp != NULL);
2782 }
2783#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002784 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 {
2786 /* Complicated case: Resize a column of frames. Resize the bottom
2787 * frame first, frames above that when needed. */
2788
2789 frp = topfrp->fr_child;
2790 if (wfh)
2791 /* Advance past frames with one window with 'wfh' set. */
2792 while (frame_fixed_height(frp))
2793 {
2794 frp = frp->fr_next;
2795 if (frp == NULL)
2796 return; /* no frame without 'wfh', give up */
2797 }
2798 if (!topfirst)
2799 {
2800 /* Find the bottom frame of this column */
2801 while (frp->fr_next != NULL)
2802 frp = frp->fr_next;
2803 if (wfh)
2804 /* Advance back for frames with one window with 'wfh' set. */
2805 while (frame_fixed_height(frp))
2806 frp = frp->fr_prev;
2807 }
2808
2809 extra_lines = height - topfrp->fr_height;
2810 if (extra_lines < 0)
2811 {
2812 /* reduce height of contained frames, bottom or top frame first */
2813 while (frp != NULL)
2814 {
2815 h = frame_minheight(frp, NULL);
2816 if (frp->fr_height + extra_lines < h)
2817 {
2818 extra_lines += frp->fr_height - h;
2819 frame_new_height(frp, h, topfirst, wfh);
2820 }
2821 else
2822 {
2823 frame_new_height(frp, frp->fr_height + extra_lines,
2824 topfirst, wfh);
2825 break;
2826 }
2827 if (topfirst)
2828 {
2829 do
2830 frp = frp->fr_next;
2831 while (wfh && frp != NULL && frame_fixed_height(frp));
2832 }
2833 else
2834 {
2835 do
2836 frp = frp->fr_prev;
2837 while (wfh && frp != NULL && frame_fixed_height(frp));
2838 }
2839 /* Increase "height" if we could not reduce enough frames. */
2840 if (frp == NULL)
2841 height -= extra_lines;
2842 }
2843 }
2844 else if (extra_lines > 0)
2845 {
2846 /* increase height of bottom or top frame */
2847 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2848 }
2849 }
2850 topfrp->fr_height = height;
2851}
2852
2853/*
2854 * Return TRUE if height of frame "frp" should not be changed because of
2855 * the 'winfixheight' option.
2856 */
2857 static int
2858frame_fixed_height(frp)
2859 frame_T *frp;
2860{
2861 /* frame with one window: fixed height if 'winfixheight' set. */
2862 if (frp->fr_win != NULL)
2863 return frp->fr_win->w_p_wfh;
2864
2865 if (frp->fr_layout == FR_ROW)
2866 {
2867 /* The frame is fixed height if one of the frames in the row is fixed
2868 * height. */
2869 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2870 if (frame_fixed_height(frp))
2871 return TRUE;
2872 return FALSE;
2873 }
2874
2875 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2876 * frames in the row are fixed height. */
2877 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2878 if (!frame_fixed_height(frp))
2879 return FALSE;
2880 return TRUE;
2881}
2882
2883#ifdef FEAT_VERTSPLIT
2884/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002885 * Return TRUE if width of frame "frp" should not be changed because of
2886 * the 'winfixwidth' option.
2887 */
2888 static int
2889frame_fixed_width(frp)
2890 frame_T *frp;
2891{
2892 /* frame with one window: fixed width if 'winfixwidth' set. */
2893 if (frp->fr_win != NULL)
2894 return frp->fr_win->w_p_wfw;
2895
2896 if (frp->fr_layout == FR_COL)
2897 {
2898 /* The frame is fixed width if one of the frames in the row is fixed
2899 * width. */
2900 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2901 if (frame_fixed_width(frp))
2902 return TRUE;
2903 return FALSE;
2904 }
2905
2906 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2907 * frames in the row are fixed width. */
2908 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2909 if (!frame_fixed_width(frp))
2910 return FALSE;
2911 return TRUE;
2912}
2913
2914/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 * Add a status line to windows at the bottom of "frp".
2916 * Note: Does not check if there is room!
2917 */
2918 static void
2919frame_add_statusline(frp)
2920 frame_T *frp;
2921{
2922 win_T *wp;
2923
2924 if (frp->fr_layout == FR_LEAF)
2925 {
2926 wp = frp->fr_win;
2927 if (wp->w_status_height == 0)
2928 {
2929 if (wp->w_height > 0) /* don't make it negative */
2930 --wp->w_height;
2931 wp->w_status_height = STATUS_HEIGHT;
2932 }
2933 }
2934 else if (frp->fr_layout == FR_ROW)
2935 {
2936 /* Handle all the frames in the row. */
2937 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2938 frame_add_statusline(frp);
2939 }
2940 else /* frp->fr_layout == FR_COL */
2941 {
2942 /* Only need to handle the last frame in the column. */
2943 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2944 ;
2945 frame_add_statusline(frp);
2946 }
2947}
2948
2949/*
2950 * Set width of a frame. Handles recursively going through contained frames.
2951 * May remove separator line for windows at the right side (for win_close()).
2952 */
2953 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002954frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 frame_T *topfrp;
2956 int width;
2957 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002958 int wfw; /* obey 'winfixwidth' when there is a choice;
2959 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
2961 frame_T *frp;
2962 int extra_cols;
2963 int w;
2964 win_T *wp;
2965
2966 if (topfrp->fr_layout == FR_LEAF)
2967 {
2968 /* Simple case: just one window. */
2969 wp = topfrp->fr_win;
2970 /* Find out if there are any windows right of this one. */
2971 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2972 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2973 break;
2974 if (frp->fr_parent == NULL)
2975 wp->w_vsep_width = 0;
2976 win_new_width(wp, width - wp->w_vsep_width);
2977 }
2978 else if (topfrp->fr_layout == FR_COL)
2979 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002980 do
2981 {
2982 /* All frames in this column get the same new width. */
2983 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2984 {
2985 frame_new_width(frp, width, leftfirst, wfw);
2986 if (frp->fr_width > width)
2987 {
2988 /* Could not fit the windows, make whole column wider. */
2989 width = frp->fr_width;
2990 break;
2991 }
2992 }
2993 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995 else /* fr_layout == FR_ROW */
2996 {
2997 /* Complicated case: Resize a row of frames. Resize the rightmost
2998 * frame first, frames left of it when needed. */
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003001 if (wfw)
3002 /* Advance past frames with one window with 'wfw' set. */
3003 while (frame_fixed_width(frp))
3004 {
3005 frp = frp->fr_next;
3006 if (frp == NULL)
3007 return; /* no frame without 'wfw', give up */
3008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003010 {
3011 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 while (frp->fr_next != NULL)
3013 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003014 if (wfw)
3015 /* Advance back for frames with one window with 'wfw' set. */
3016 while (frame_fixed_width(frp))
3017 frp = frp->fr_prev;
3018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019
3020 extra_cols = width - topfrp->fr_width;
3021 if (extra_cols < 0)
3022 {
3023 /* reduce frame width, rightmost frame first */
3024 while (frp != NULL)
3025 {
3026 w = frame_minwidth(frp, NULL);
3027 if (frp->fr_width + extra_cols < w)
3028 {
3029 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003030 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 }
3032 else
3033 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003034 frame_new_width(frp, frp->fr_width + extra_cols,
3035 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 break;
3037 }
3038 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003039 {
3040 do
3041 frp = frp->fr_next;
3042 while (wfw && frp != NULL && frame_fixed_width(frp));
3043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003045 {
3046 do
3047 frp = frp->fr_prev;
3048 while (wfw && frp != NULL && frame_fixed_width(frp));
3049 }
3050 /* Increase "width" if we could not reduce enough frames. */
3051 if (frp == NULL)
3052 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054 }
3055 else if (extra_cols > 0)
3056 {
3057 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003058 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 }
3060 }
3061 topfrp->fr_width = width;
3062}
3063
3064/*
3065 * Add the vertical separator to windows at the right side of "frp".
3066 * Note: Does not check if there is room!
3067 */
3068 static void
3069frame_add_vsep(frp)
3070 frame_T *frp;
3071{
3072 win_T *wp;
3073
3074 if (frp->fr_layout == FR_LEAF)
3075 {
3076 wp = frp->fr_win;
3077 if (wp->w_vsep_width == 0)
3078 {
3079 if (wp->w_width > 0) /* don't make it negative */
3080 --wp->w_width;
3081 wp->w_vsep_width = 1;
3082 }
3083 }
3084 else if (frp->fr_layout == FR_COL)
3085 {
3086 /* Handle all the frames in the column. */
3087 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3088 frame_add_vsep(frp);
3089 }
3090 else /* frp->fr_layout == FR_ROW */
3091 {
3092 /* Only need to handle the last frame in the row. */
3093 frp = frp->fr_child;
3094 while (frp->fr_next != NULL)
3095 frp = frp->fr_next;
3096 frame_add_vsep(frp);
3097 }
3098}
3099
3100/*
3101 * Set frame width from the window it contains.
3102 */
3103 static void
3104frame_fix_width(wp)
3105 win_T *wp;
3106{
3107 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3108}
3109#endif
3110
3111/*
3112 * Set frame height from the window it contains.
3113 */
3114 static void
3115frame_fix_height(wp)
3116 win_T *wp;
3117{
3118 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3119}
3120
3121/*
3122 * Compute the minimal height for frame "topfrp".
3123 * Uses the 'winminheight' option.
3124 * When "next_curwin" isn't NULL, use p_wh for this window.
3125 * When "next_curwin" is NOWIN, don't use at least one line for the current
3126 * window.
3127 */
3128 static int
3129frame_minheight(topfrp, next_curwin)
3130 frame_T *topfrp;
3131 win_T *next_curwin;
3132{
3133 frame_T *frp;
3134 int m;
3135#ifdef FEAT_VERTSPLIT
3136 int n;
3137#endif
3138
3139 if (topfrp->fr_win != NULL)
3140 {
3141 if (topfrp->fr_win == next_curwin)
3142 m = p_wh + topfrp->fr_win->w_status_height;
3143 else
3144 {
3145 /* window: minimal height of the window plus status line */
3146 m = p_wmh + topfrp->fr_win->w_status_height;
3147 /* Current window is minimal one line high */
3148 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3149 ++m;
3150 }
3151 }
3152#ifdef FEAT_VERTSPLIT
3153 else if (topfrp->fr_layout == FR_ROW)
3154 {
3155 /* get the minimal height from each frame in this row */
3156 m = 0;
3157 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3158 {
3159 n = frame_minheight(frp, next_curwin);
3160 if (n > m)
3161 m = n;
3162 }
3163 }
3164#endif
3165 else
3166 {
3167 /* Add up the minimal heights for all frames in this column. */
3168 m = 0;
3169 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3170 m += frame_minheight(frp, next_curwin);
3171 }
3172
3173 return m;
3174}
3175
3176#ifdef FEAT_VERTSPLIT
3177/*
3178 * Compute the minimal width for frame "topfrp".
3179 * When "next_curwin" isn't NULL, use p_wiw for this window.
3180 * When "next_curwin" is NOWIN, don't use at least one column for the current
3181 * window.
3182 */
3183 static int
3184frame_minwidth(topfrp, next_curwin)
3185 frame_T *topfrp;
3186 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3187{
3188 frame_T *frp;
3189 int m, n;
3190
3191 if (topfrp->fr_win != NULL)
3192 {
3193 if (topfrp->fr_win == next_curwin)
3194 m = p_wiw + topfrp->fr_win->w_vsep_width;
3195 else
3196 {
3197 /* window: minimal width of the window plus separator column */
3198 m = p_wmw + topfrp->fr_win->w_vsep_width;
3199 /* Current window is minimal one column wide */
3200 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3201 ++m;
3202 }
3203 }
3204 else if (topfrp->fr_layout == FR_COL)
3205 {
3206 /* get the minimal width from each frame in this column */
3207 m = 0;
3208 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3209 {
3210 n = frame_minwidth(frp, next_curwin);
3211 if (n > m)
3212 m = n;
3213 }
3214 }
3215 else
3216 {
3217 /* Add up the minimal widths for all frames in this row. */
3218 m = 0;
3219 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3220 m += frame_minwidth(frp, next_curwin);
3221 }
3222
3223 return m;
3224}
3225#endif
3226
3227
3228/*
3229 * Try to close all windows except current one.
3230 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3231 * used and the buffer was modified.
3232 *
3233 * Used by ":bdel" and ":only".
3234 */
3235 void
3236close_others(message, forceit)
3237 int message;
3238 int forceit; /* always hide all other windows */
3239{
3240 win_T *wp;
3241 win_T *nextwp;
3242 int r;
3243
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003244 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 {
3246 if (message
3247#ifdef FEAT_AUTOCMD
3248 && !autocmd_busy
3249#endif
3250 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003251 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 return;
3253 }
3254
3255 /* Be very careful here: autocommands may change the window layout. */
3256 for (wp = firstwin; win_valid(wp); wp = nextwp)
3257 {
3258 nextwp = wp->w_next;
3259 if (wp != curwin) /* don't close current window */
3260 {
3261
3262 /* Check if it's allowed to abandon this window */
3263 r = can_abandon(wp->w_buffer, forceit);
3264#ifdef FEAT_AUTOCMD
3265 if (!win_valid(wp)) /* autocommands messed wp up */
3266 {
3267 nextwp = firstwin;
3268 continue;
3269 }
3270#endif
3271 if (!r)
3272 {
3273#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3274 if (message && (p_confirm || cmdmod.confirm) && p_write)
3275 {
3276 dialog_changed(wp->w_buffer, FALSE);
3277# ifdef FEAT_AUTOCMD
3278 if (!win_valid(wp)) /* autocommands messed wp up */
3279 {
3280 nextwp = firstwin;
3281 continue;
3282 }
3283# endif
3284 }
3285 if (bufIsChanged(wp->w_buffer))
3286#endif
3287 continue;
3288 }
3289 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3290 }
3291 }
3292
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003293 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 EMSG(_("E445: Other window contains changes"));
3295}
3296
3297#endif /* FEAT_WINDOWS */
3298
3299/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003300 * Init the current window "curwin".
3301 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
3303 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003304curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003306 win_init_empty(curwin);
3307}
3308
3309 void
3310win_init_empty(wp)
3311 win_T *wp;
3312{
3313 redraw_win_later(wp, NOT_VALID);
3314 wp->w_lines_valid = 0;
3315 wp->w_cursor.lnum = 1;
3316 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003318 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003320 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3321 wp->w_pcmark.col = 0;
3322 wp->w_prev_pcmark.lnum = 0;
3323 wp->w_prev_pcmark.col = 0;
3324 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003326 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003328 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003330 if (wp->w_p_rl)
3331 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003333 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003335#ifdef FEAT_SYN_HL
3336 wp->w_s = &wp->w_buffer->b_s;
3337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338}
3339
3340/*
3341 * Allocate the first window and put an empty buffer in it.
3342 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003345 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346win_alloc_first()
3347{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003348 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003349 return FAIL;
3350
3351#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003352 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003353 if (first_tabpage == NULL)
3354 return FAIL;
3355 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003356 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003357#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003358
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003359 return OK;
3360}
3361
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003362#if defined(FEAT_AUTOCMD) || defined(PROTO)
3363/*
3364 * Init "aucmd_win". This can only be done after the first
3365 * window is fully initialized, thus it can't be in win_alloc_first().
3366 */
3367 void
3368win_alloc_aucmd_win()
3369{
3370 aucmd_win = win_alloc(NULL, TRUE);
3371 if (aucmd_win != NULL)
3372 {
3373 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003374 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003375 new_frame(aucmd_win);
3376 }
3377}
3378#endif
3379
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003380/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003381 * Allocate the first window or the first window in a new tab page.
3382 * When "oldwin" is NULL create an empty buffer for it.
3383 * When "oldwin" is not NULL copy info from it to the new window (only with
3384 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003385 * Return FAIL when something goes wrong (out of memory).
3386 */
3387 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003388win_alloc_firstwin(oldwin)
3389 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003390{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003391 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003392 if (oldwin == NULL)
3393 {
3394 /* Very first window, need to create an empty buffer for it and
3395 * initialize from scratch. */
3396 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3397 if (curwin == NULL || curbuf == NULL)
3398 return FAIL;
3399 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003400#ifdef FEAT_SYN_HL
3401 curwin->w_s = &(curbuf->b_s);
3402#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003403 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003405 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003407 curwin_init(); /* init current window */
3408 }
3409#ifdef FEAT_WINDOWS
3410 else
3411 {
3412 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003413 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003414
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003415 /* We don't want cursor- and scroll-binding in the first window. */
3416 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003417 }
3418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003420 new_frame(curwin);
3421 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003422 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003423 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424#ifdef FEAT_VERTSPLIT
3425 topframe->fr_width = Columns;
3426#endif
3427 topframe->fr_height = Rows - p_ch;
3428 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003429
3430 return OK;
3431}
3432
3433/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003434 * Create a frame for window "wp".
3435 */
3436 static void
3437new_frame(win_T *wp)
3438{
3439 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3440
3441 wp->w_frame = frp;
3442 if (frp != NULL)
3443 {
3444 frp->fr_layout = FR_LEAF;
3445 frp->fr_win = wp;
3446 }
3447}
3448
3449/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003450 * Initialize the window and frame size to the maximum.
3451 */
3452 void
3453win_init_size()
3454{
3455 firstwin->w_height = ROWS_AVAIL;
3456 topframe->fr_height = ROWS_AVAIL;
3457#ifdef FEAT_VERTSPLIT
3458 firstwin->w_width = Columns;
3459 topframe->fr_width = Columns;
3460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461}
3462
3463#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003464
3465/*
3466 * Allocate a new tabpage_T and init the values.
3467 * Returns NULL when out of memory.
3468 */
3469 static tabpage_T *
3470alloc_tabpage()
3471{
3472 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003473# ifdef FEAT_GUI
3474 int i;
3475# endif
3476
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003477
3478 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003479 if (tp == NULL)
3480 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003481
Bram Moolenaar429fa852013-04-15 12:27:36 +02003482# ifdef FEAT_EVAL
3483 /* init t: variables */
3484 tp->tp_vars = dict_alloc();
3485 if (tp->tp_vars == NULL)
3486 {
3487 vim_free(tp);
3488 return NULL;
3489 }
3490 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3491# endif
3492
3493# ifdef FEAT_GUI
3494 for (i = 0; i < 3; i++)
3495 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003496# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003497# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003498 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003499# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003500 tp->tp_ch_used = p_ch;
3501
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003502 return tp;
3503}
3504
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003505 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003506free_tabpage(tp)
3507 tabpage_T *tp;
3508{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003509 int idx;
3510
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003511# ifdef FEAT_DIFF
3512 diff_clear(tp);
3513# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003514 for (idx = 0; idx < SNAP_COUNT; ++idx)
3515 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003516#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003517 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3518 hash_init(&tp->tp_vars->dv_hashtab);
3519 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003520#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003521
3522#ifdef FEAT_PYTHON
3523 python_tabpage_free(tp);
3524#endif
3525
3526#ifdef FEAT_PYTHON3
3527 python3_tabpage_free(tp);
3528#endif
3529
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003530 vim_free(tp);
3531}
3532
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003533/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003534 * Create a new Tab page with one window.
3535 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003536 * When "after" is 0 put it just after the current Tab page.
3537 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538 * Return FAIL or OK.
3539 */
3540 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003541win_new_tabpage(after)
3542 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003543{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003544 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003545 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003546 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003547
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003548 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003549 if (newtp == NULL)
3550 return FAIL;
3551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003552 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003553 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003554 {
3555 vim_free(newtp);
3556 return FAIL;
3557 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003558 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003559
3560 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003561 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003562 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003563 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003564 if (after == 1)
3565 {
3566 /* New tab page becomes the first one. */
3567 newtp->tp_next = first_tabpage;
3568 first_tabpage = newtp;
3569 }
3570 else
3571 {
3572 if (after > 0)
3573 {
3574 /* Put new tab page before tab page "after". */
3575 n = 2;
3576 for (tp = first_tabpage; tp->tp_next != NULL
3577 && n < after; tp = tp->tp_next)
3578 ++n;
3579 }
3580 newtp->tp_next = tp->tp_next;
3581 tp->tp_next = newtp;
3582 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003583 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003584 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003585 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003586
3587 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003588 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003589
3590#if defined(FEAT_GUI)
3591 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3592 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003593 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003594#endif
3595
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003596 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003597#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003598 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003599 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003600#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003601 return OK;
3602 }
3603
3604 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003605 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003606 return FAIL;
3607}
3608
3609/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003610 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3611 * like with ":split".
3612 * Returns OK if a new tab page was created, FAIL otherwise.
3613 */
3614 int
3615may_open_tabpage()
3616{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003617 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003618
Bram Moolenaard326ce82007-03-11 14:48:29 +00003619 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003620 {
3621 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003622 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003623 return win_new_tabpage(n);
3624 }
3625 return FAIL;
3626}
3627
3628/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003629 * Create up to "maxcount" tabpages with empty windows.
3630 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003631 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003632 int
3633make_tabpages(maxcount)
3634 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003636 int count = maxcount;
3637 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003638
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003639 /* Limit to 'tabpagemax' tabs. */
3640 if (count > p_tpm)
3641 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003642
3643#ifdef FEAT_AUTOCMD
3644 /*
3645 * Don't execute autocommands while creating the tab pages. Must do that
3646 * when putting the buffers in the windows.
3647 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003648 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003649#endif
3650
3651 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003652 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003653 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003654
3655#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003656 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003657#endif
3658
3659 /* return actual number of tab pages */
3660 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003661}
3662
3663/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003664 * Return TRUE when "tpc" points to a valid tab page.
3665 */
3666 int
3667valid_tabpage(tpc)
3668 tabpage_T *tpc;
3669{
3670 tabpage_T *tp;
3671
3672 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3673 if (tp == tpc)
3674 return TRUE;
3675 return FALSE;
3676}
3677
3678/*
3679 * Find tab page "n" (first one is 1). Returns NULL when not found.
3680 */
3681 tabpage_T *
3682find_tabpage(n)
3683 int n;
3684{
3685 tabpage_T *tp;
3686 int i = 1;
3687
3688 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3689 ++i;
3690 return tp;
3691}
3692
3693/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003694 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003695 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003696 */
3697 int
3698tabpage_index(ftp)
3699 tabpage_T *ftp;
3700{
3701 int i = 1;
3702 tabpage_T *tp;
3703
3704 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3705 ++i;
3706 return i;
3707}
3708
3709/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003710 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003711 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003712 * FAIL.
3713 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003714 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003715 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003716leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003717 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003718 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003719 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003720{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003721 tabpage_T *tp = curtab;
3722
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003723 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003724#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003725 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003726 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003727 if (new_curbuf != curbuf)
3728 {
3729 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3730 if (curtab != tp)
3731 return FAIL;
3732 }
3733 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3734 if (curtab != tp)
3735 return FAIL;
3736 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003737 if (curtab != tp)
3738 return FAIL;
3739 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003740#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003741#if defined(FEAT_GUI)
3742 /* Remove the scrollbars. They may be added back later. */
3743 if (gui.in_use)
3744 gui_remove_scrollbars();
3745#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003746 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003747 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003748 tp->tp_firstwin = firstwin;
3749 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003750 tp->tp_old_Rows = Rows;
3751 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003752 firstwin = NULL;
3753 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003754 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003755}
3756
3757/*
3758 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003759 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003760 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3761 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003762 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003764enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003765 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003766 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003767 int trigger_enter_autocmds UNUSED;
3768 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003769{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003770 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003771 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003772
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003773 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003774 firstwin = tp->tp_firstwin;
3775 lastwin = tp->tp_lastwin;
3776 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003777
3778 /* We would like doing the TabEnter event first, but we don't have a
3779 * valid current window yet, which may break some commands.
3780 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003781 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3782 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003783 prevwin = next_prevwin;
3784
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003785 last_status(FALSE); /* status line may appear or disappear */
3786 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003787 must_redraw = CLEAR; /* need to redraw everything */
3788#ifdef FEAT_DIFF
3789 diff_need_scrollbind = TRUE;
3790#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003791
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003792 /* The tabpage line may have appeared or disappeared, may need to resize
3793 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003794 * frame sizes too. Use the stored value of p_ch, so that it can be
3795 * different for each tab page. */
3796 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003797 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3798#ifdef FEAT_GUI_TABLINE
3799 && !gui_use_tabline()
3800#endif
3801 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003802 shell_new_rows();
3803#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003804 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003805 shell_new_columns(); /* update window widths */
3806#endif
3807
3808#if defined(FEAT_GUI)
3809 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3810 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003811 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003812#endif
3813
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003814#ifdef FEAT_AUTOCMD
3815 /* Apply autocommands after updating the display, when 'rows' and
3816 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003817 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003818 {
3819 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3820 if (old_curbuf != curbuf)
3821 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3822 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003823#endif
3824
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003825 redraw_all_later(CLEAR);
3826}
3827
3828/*
3829 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003830 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831 */
3832 void
3833goto_tabpage(n)
3834 int n;
3835{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003836 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003837 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003838 int i;
3839
Bram Moolenaard68071d2006-05-02 22:08:30 +00003840 if (text_locked())
3841 {
3842 /* Not allowed when editing the command line. */
3843#ifdef FEAT_CMDWIN
3844 if (cmdwin_type != 0)
3845 EMSG(_(e_cmdwin));
3846 else
3847#endif
3848 EMSG(_(e_secure));
3849 return;
3850 }
3851
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003852 /* If there is only one it can't work. */
3853 if (first_tabpage->tp_next == NULL)
3854 {
3855 if (n > 1)
3856 beep_flush();
3857 return;
3858 }
3859
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003860 if (n == 0)
3861 {
3862 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003863 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003864 tp = first_tabpage;
3865 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003866 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003867 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003868 else if (n < 0)
3869 {
3870 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3871 * this N times. */
3872 ttp = curtab;
3873 for (i = n; i < 0; ++i)
3874 {
3875 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3876 tp = tp->tp_next)
3877 ;
3878 ttp = tp;
3879 }
3880 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003881 else if (n == 9999)
3882 {
3883 /* Go to last tab page. */
3884 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3885 ;
3886 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003887 else
3888 {
3889 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003890 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003891 if (tp == NULL)
3892 {
3893 beep_flush();
3894 return;
3895 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003896 }
3897
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003898 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003899
3900#ifdef FEAT_GUI_TABLINE
3901 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003902 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003903#endif
3904}
3905
3906/*
3907 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003908 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3909 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003910 * Note: doesn't update the GUI tab.
3911 */
3912 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003913goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003914 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003915 int trigger_enter_autocmds;
3916 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003917{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003918 /* Don't repeat a message in another tab page. */
3919 set_keep_msg(NULL, 0);
3920
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003921 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3922 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003923 {
3924 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003925 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3926 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003927 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003928 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3929 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003930 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003931}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932
3933/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003934 * Enter window "wp" in tab page "tp".
3935 * Also updates the GUI tab.
3936 */
3937 void
3938goto_tabpage_win(tp, wp)
3939 tabpage_T *tp;
3940 win_T *wp;
3941{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003942 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003943 if (curtab == tp && win_valid(wp))
3944 {
3945 win_enter(wp, TRUE);
3946# ifdef FEAT_GUI_TABLINE
3947 if (gui_use_tabline())
3948 gui_mch_set_curtab(tabpage_index(curtab));
3949# endif
3950 }
3951}
3952
3953/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003954 * Move the current tab page to before tab page "nr".
3955 */
3956 void
3957tabpage_move(nr)
3958 int nr;
3959{
3960 int n = nr;
3961 tabpage_T *tp;
3962
3963 if (first_tabpage->tp_next == NULL)
3964 return;
3965
3966 /* Remove the current tab page from the list of tab pages. */
3967 if (curtab == first_tabpage)
3968 first_tabpage = curtab->tp_next;
3969 else
3970 {
3971 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3972 if (tp->tp_next == curtab)
3973 break;
3974 if (tp == NULL) /* "cannot happen" */
3975 return;
3976 tp->tp_next = curtab->tp_next;
3977 }
3978
3979 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003980 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003981 {
3982 curtab->tp_next = first_tabpage;
3983 first_tabpage = curtab;
3984 }
3985 else
3986 {
3987 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3988 --n;
3989 curtab->tp_next = tp->tp_next;
3990 tp->tp_next = curtab;
3991 }
3992
3993 /* Need to redraw the tabline. Tab page contents doesn't change. */
3994 redraw_tabline = TRUE;
3995}
3996
3997
3998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * Go to another window.
4000 * When jumping to another buffer, stop Visual mode. Do this before
4001 * changing windows so we can yank the selection into the '*' register.
4002 * When jumping to another window on the same buffer, adjust its cursor
4003 * position to keep the same Visual area.
4004 */
4005 void
4006win_goto(wp)
4007 win_T *wp;
4008{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004009#ifdef FEAT_CONCEAL
4010 win_T *owp = curwin;
4011#endif
4012
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004013 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
4015 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004016 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 return;
4018 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019#ifdef FEAT_AUTOCMD
4020 if (curbuf_locked())
4021 return;
4022#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (wp->w_buffer != curbuf)
4025 reset_VIsual_and_resel();
4026 else if (VIsual_active)
4027 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028
4029#ifdef FEAT_GUI
4030 need_mouse_correct = TRUE;
4031#endif
4032 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004033
4034#ifdef FEAT_CONCEAL
4035 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004036 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004037 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004038 if (curwin->w_p_cole > 0 && !msg_scrolled)
4039 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041}
4042
4043#if defined(FEAT_PERL) || defined(PROTO)
4044/*
4045 * Find window number "winnr" (counting top to bottom).
4046 */
4047 win_T *
4048win_find_nr(winnr)
4049 int winnr;
4050{
4051 win_T *wp;
4052
4053# ifdef FEAT_WINDOWS
4054 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4055 if (--winnr == 0)
4056 break;
4057 return wp;
4058# else
4059 return curwin;
4060# endif
4061}
4062#endif
4063
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004064#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4065 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004066/*
4067 * Find the tabpage for window "win".
4068 */
4069 tabpage_T *
4070win_find_tabpage(win)
4071 win_T *win;
4072{
4073 win_T *wp;
4074 tabpage_T *tp;
4075
4076 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004077 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4078 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004079 if (wp == win)
4080 return tp;
4081 return NULL;
4082}
4083#endif
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#ifdef FEAT_VERTSPLIT
4086/*
4087 * Move to window above or below "count" times.
4088 */
4089 static void
4090win_goto_ver(up, count)
4091 int up; /* TRUE to go to win above */
4092 long count;
4093{
4094 frame_T *fr;
4095 frame_T *nfr;
4096 frame_T *foundfr;
4097
4098 foundfr = curwin->w_frame;
4099 while (count--)
4100 {
4101 /*
4102 * First go upwards in the tree of frames until we find a upwards or
4103 * downwards neighbor.
4104 */
4105 fr = foundfr;
4106 for (;;)
4107 {
4108 if (fr == topframe)
4109 goto end;
4110 if (up)
4111 nfr = fr->fr_prev;
4112 else
4113 nfr = fr->fr_next;
4114 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4115 break;
4116 fr = fr->fr_parent;
4117 }
4118
4119 /*
4120 * Now go downwards to find the bottom or top frame in it.
4121 */
4122 for (;;)
4123 {
4124 if (nfr->fr_layout == FR_LEAF)
4125 {
4126 foundfr = nfr;
4127 break;
4128 }
4129 fr = nfr->fr_child;
4130 if (nfr->fr_layout == FR_ROW)
4131 {
4132 /* Find the frame at the cursor row. */
4133 while (fr->fr_next != NULL
4134 && frame2win(fr)->w_wincol + fr->fr_width
4135 <= curwin->w_wincol + curwin->w_wcol)
4136 fr = fr->fr_next;
4137 }
4138 if (nfr->fr_layout == FR_COL && up)
4139 while (fr->fr_next != NULL)
4140 fr = fr->fr_next;
4141 nfr = fr;
4142 }
4143 }
4144end:
4145 if (foundfr != NULL)
4146 win_goto(foundfr->fr_win);
4147}
4148
4149/*
4150 * Move to left or right window.
4151 */
4152 static void
4153win_goto_hor(left, count)
4154 int left; /* TRUE to go to left win */
4155 long count;
4156{
4157 frame_T *fr;
4158 frame_T *nfr;
4159 frame_T *foundfr;
4160
4161 foundfr = curwin->w_frame;
4162 while (count--)
4163 {
4164 /*
4165 * First go upwards in the tree of frames until we find a left or
4166 * right neighbor.
4167 */
4168 fr = foundfr;
4169 for (;;)
4170 {
4171 if (fr == topframe)
4172 goto end;
4173 if (left)
4174 nfr = fr->fr_prev;
4175 else
4176 nfr = fr->fr_next;
4177 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4178 break;
4179 fr = fr->fr_parent;
4180 }
4181
4182 /*
4183 * Now go downwards to find the leftmost or rightmost frame in it.
4184 */
4185 for (;;)
4186 {
4187 if (nfr->fr_layout == FR_LEAF)
4188 {
4189 foundfr = nfr;
4190 break;
4191 }
4192 fr = nfr->fr_child;
4193 if (nfr->fr_layout == FR_COL)
4194 {
4195 /* Find the frame at the cursor row. */
4196 while (fr->fr_next != NULL
4197 && frame2win(fr)->w_winrow + fr->fr_height
4198 <= curwin->w_winrow + curwin->w_wrow)
4199 fr = fr->fr_next;
4200 }
4201 if (nfr->fr_layout == FR_ROW && left)
4202 while (fr->fr_next != NULL)
4203 fr = fr->fr_next;
4204 nfr = fr;
4205 }
4206 }
4207end:
4208 if (foundfr != NULL)
4209 win_goto(foundfr->fr_win);
4210}
4211#endif
4212
4213/*
4214 * Make window "wp" the current window.
4215 */
4216 void
4217win_enter(wp, undo_sync)
4218 win_T *wp;
4219 int undo_sync;
4220{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004221 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222}
4223
4224/*
4225 * Make window wp the current window.
4226 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4227 * been closed and isn't valid.
4228 */
4229 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004230win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 win_T *wp;
4232 int undo_sync;
4233 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004234 int trigger_enter_autocmds UNUSED;
4235 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237#ifdef FEAT_AUTOCMD
4238 int other_buffer = FALSE;
4239#endif
4240
4241 if (wp == curwin && !curwin_invalid) /* nothing to do */
4242 return;
4243
4244#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004245 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
4247 /*
4248 * Be careful: If autocommands delete the window, return now.
4249 */
4250 if (wp->w_buffer != curbuf)
4251 {
4252 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4253 other_buffer = TRUE;
4254 if (!win_valid(wp))
4255 return;
4256 }
4257 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4258 if (!win_valid(wp))
4259 return;
4260# ifdef FEAT_EVAL
4261 /* autocmds may abort script processing */
4262 if (aborting())
4263 return;
4264# endif
4265 }
4266#endif
4267
4268 /* sync undo before leaving the current buffer */
4269 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004270 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004271
4272 /* Might need to scroll the old window before switching, e.g., when the
4273 * cursor was moved. */
4274 update_topline();
4275
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 /* may have to copy the buffer options when 'cpo' contains 'S' */
4277 if (wp->w_buffer != curbuf)
4278 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4279 if (!curwin_invalid)
4280 {
4281 prevwin = curwin; /* remember for CTRL-W p */
4282 curwin->w_redr_status = TRUE;
4283 }
4284 curwin = wp;
4285 curbuf = wp->w_buffer;
4286 check_cursor();
4287#ifdef FEAT_VIRTUALEDIT
4288 if (!virtual_active())
4289 curwin->w_cursor.coladd = 0;
4290#endif
4291 changed_line_abv_curs(); /* assume cursor position needs updating */
4292
4293 if (curwin->w_localdir != NULL)
4294 {
4295 /* Window has a local directory: Save current directory as global
4296 * directory (unless that was done already) and change to the local
4297 * directory. */
4298 if (globaldir == NULL)
4299 {
4300 char_u cwd[MAXPATHL];
4301
4302 if (mch_dirname(cwd, MAXPATHL) == OK)
4303 globaldir = vim_strsave(cwd);
4304 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004305 if (mch_chdir((char *)curwin->w_localdir) == 0)
4306 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 else if (globaldir != NULL)
4309 {
4310 /* Window doesn't have a local directory and we are not in the global
4311 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004312 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 vim_free(globaldir);
4314 globaldir = NULL;
4315 shorten_fnames(TRUE);
4316 }
4317
4318#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004319 if (trigger_enter_autocmds)
4320 {
4321 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4322 if (other_buffer)
4323 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325#endif
4326
4327#ifdef FEAT_TITLE
4328 maketitle();
4329#endif
4330 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004331 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 if (restart_edit)
4333 redraw_later(VALID); /* causes status line redraw */
4334
4335 /* set window height to desired minimal value */
4336 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4337 win_setheight((int)p_wh);
4338 else if (curwin->w_height == 0)
4339 win_setheight(1);
4340
4341#ifdef FEAT_VERTSPLIT
4342 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004343 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 win_setwidth((int)p_wiw);
4345#endif
4346
4347#ifdef FEAT_MOUSE
4348 setmouse(); /* in case jumped to/from help buffer */
4349#endif
4350
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004351 /* Change directories when the 'acd' option is set. */
4352 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353}
4354
4355#endif /* FEAT_WINDOWS */
4356
4357#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4358/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004359 * Jump to the first open window that contains buffer "buf", if one exists.
4360 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
4362 win_T *
4363buf_jump_open_win(buf)
4364 buf_T *buf;
4365{
4366# ifdef FEAT_WINDOWS
4367 win_T *wp;
4368
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004369 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (wp->w_buffer == buf)
4371 break;
4372 if (wp != NULL)
4373 win_enter(wp, FALSE);
4374 return wp;
4375# else
4376 if (curwin->w_buffer == buf)
4377 return curwin;
4378 return NULL;
4379# endif
4380}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004381
4382/*
4383 * Jump to the first open window in any tab page that contains buffer "buf",
4384 * if one exists.
4385 * Returns a pointer to the window found, otherwise NULL.
4386 */
4387 win_T *
4388buf_jump_open_tab(buf)
4389 buf_T *buf;
4390{
4391# ifdef FEAT_WINDOWS
4392 win_T *wp;
4393 tabpage_T *tp;
4394
4395 /* First try the current tab page. */
4396 wp = buf_jump_open_win(buf);
4397 if (wp != NULL)
4398 return wp;
4399
4400 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4401 if (tp != curtab)
4402 {
4403 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4404 if (wp->w_buffer == buf)
4405 break;
4406 if (wp != NULL)
4407 {
4408 goto_tabpage_win(tp, wp);
4409 if (curwin != wp)
4410 wp = NULL; /* something went wrong */
4411 break;
4412 }
4413 }
4414
4415 return wp;
4416# else
4417 if (curwin->w_buffer == buf)
4418 return curwin;
4419 return NULL;
4420# endif
4421}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422#endif
4423
4424/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004425 * Allocate a window structure and link it in the window list when "hidden" is
4426 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004429win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004430 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004431 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004433 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
4435 /*
4436 * allocate window structure and linesizes arrays
4437 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004438 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004439 if (new_wp == NULL)
4440 return NULL;
4441
4442 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004444 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004445 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447
Bram Moolenaar429fa852013-04-15 12:27:36 +02004448#ifdef FEAT_EVAL
4449 /* init w: variables */
4450 new_wp->w_vars = dict_alloc();
4451 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004453 win_free_lsize(new_wp);
4454 vim_free(new_wp);
4455 return NULL;
4456 }
4457 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004458#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004459
4460#ifdef FEAT_AUTOCMD
4461 /* Don't execute autocommands while the window is not properly
4462 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4463 * event. */
4464 block_autocmds();
4465#endif
4466 /*
4467 * link the window in the window list
4468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004470 if (!hidden)
4471 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#endif
4473#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004474 new_wp->w_wincol = 0;
4475 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476#endif
4477
Bram Moolenaar429fa852013-04-15 12:27:36 +02004478 /* position the display and the cursor at the top of the file. */
4479 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004481 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004483 new_wp->w_botline = 2;
4484 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004486 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487#endif
4488
Bram Moolenaar429fa852013-04-15 12:27:36 +02004489 /* We won't calculate w_fraction until resizing the window */
4490 new_wp->w_fraction = 0;
4491 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004494 if (gui.in_use)
4495 {
4496 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4497 SBAR_LEFT, new_wp);
4498 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4499 SBAR_RIGHT, new_wp);
4500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501#endif
4502#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004503 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004505#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004506 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004507#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004508#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004509 new_wp->w_match_head = NULL;
4510 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004511#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004512 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513}
4514
4515#if defined(FEAT_WINDOWS) || defined(PROTO)
4516
4517/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004518 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 */
4520 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004521win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004523 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004526 buf_T *buf;
4527 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004529#ifdef FEAT_FOLDING
4530 clearFolding(wp);
4531#endif
4532
4533 /* reduce the reference count to the argument list. */
4534 alist_unlink(wp->w_alist);
4535
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004536#ifdef FEAT_AUTOCMD
4537 /* Don't execute autocommands while the window is halfway being deleted.
4538 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004539 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004540#endif
4541
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004542#ifdef FEAT_LUA
4543 lua_window_free(wp);
4544#endif
4545
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004546#ifdef FEAT_MZSCHEME
4547 mzscheme_window_free(wp);
4548#endif
4549
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550#ifdef FEAT_PERL
4551 perl_win_free(wp);
4552#endif
4553
4554#ifdef FEAT_PYTHON
4555 python_window_free(wp);
4556#endif
4557
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004558#ifdef FEAT_PYTHON3
4559 python3_window_free(wp);
4560#endif
4561
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562#ifdef FEAT_TCL
4563 tcl_window_free(wp);
4564#endif
4565
4566#ifdef FEAT_RUBY
4567 ruby_window_free(wp);
4568#endif
4569
4570 clear_winopt(&wp->w_onebuf_opt);
4571 clear_winopt(&wp->w_allbuf_opt);
4572
4573#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004574 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4575 hash_init(&wp->w_vars->dv_hashtab);
4576 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#endif
4578
4579 if (prevwin == wp)
4580 prevwin = NULL;
4581 win_free_lsize(wp);
4582
4583 for (i = 0; i < wp->w_tagstacklen; ++i)
4584 vim_free(wp->w_tagstack[i].tagname);
4585
4586 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004587
Bram Moolenaarff18df02013-07-24 17:51:57 +02004588 /* Remove the window from the b_wininfo lists, it may happen that the
4589 * freed memory is re-used for another window. */
4590 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4591 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4592 if (wip->wi_win == wp)
4593 wip->wi_win = NULL;
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004596 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004598
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599#ifdef FEAT_JUMPLIST
4600 free_jumplist(wp);
4601#endif
4602
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004603#ifdef FEAT_QUICKFIX
4604 qf_free_all(wp);
4605#endif
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#ifdef FEAT_GUI
4608 if (gui.in_use)
4609 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4611 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4612 }
4613#endif /* FEAT_GUI */
4614
Bram Moolenaar860cae12010-06-05 23:22:07 +02004615#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004616 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004617#endif
4618
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004619#ifdef FEAT_AUTOCMD
4620 if (wp != aucmd_win)
4621#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004622 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004623#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004624 if (autocmd_busy)
4625 {
4626 wp->w_next = au_pending_free_win;
4627 au_pending_free_win = wp;
4628 }
4629 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004630#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004631 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004632
4633#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004634 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636}
4637
4638/*
4639 * Append window "wp" in the window list after window "after".
4640 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004641 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642win_append(after, wp)
4643 win_T *after, *wp;
4644{
4645 win_T *before;
4646
4647 if (after == NULL) /* after NULL is in front of the first */
4648 before = firstwin;
4649 else
4650 before = after->w_next;
4651
4652 wp->w_next = before;
4653 wp->w_prev = after;
4654 if (after == NULL)
4655 firstwin = wp;
4656 else
4657 after->w_next = wp;
4658 if (before == NULL)
4659 lastwin = wp;
4660 else
4661 before->w_prev = wp;
4662}
4663
4664/*
4665 * Remove a window from the window list.
4666 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004667 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004668win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004670 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671{
4672 if (wp->w_prev != NULL)
4673 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004674 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004676 else
4677 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 if (wp->w_next != NULL)
4679 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004680 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004682 else
4683 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684}
4685
4686/*
4687 * Append frame "frp" in a frame list after frame "after".
4688 */
4689 static void
4690frame_append(after, frp)
4691 frame_T *after, *frp;
4692{
4693 frp->fr_next = after->fr_next;
4694 after->fr_next = frp;
4695 if (frp->fr_next != NULL)
4696 frp->fr_next->fr_prev = frp;
4697 frp->fr_prev = after;
4698}
4699
4700/*
4701 * Insert frame "frp" in a frame list before frame "before".
4702 */
4703 static void
4704frame_insert(before, frp)
4705 frame_T *before, *frp;
4706{
4707 frp->fr_next = before;
4708 frp->fr_prev = before->fr_prev;
4709 before->fr_prev = frp;
4710 if (frp->fr_prev != NULL)
4711 frp->fr_prev->fr_next = frp;
4712 else
4713 frp->fr_parent->fr_child = frp;
4714}
4715
4716/*
4717 * Remove a frame from a frame list.
4718 */
4719 static void
4720frame_remove(frp)
4721 frame_T *frp;
4722{
4723 if (frp->fr_prev != NULL)
4724 frp->fr_prev->fr_next = frp->fr_next;
4725 else
4726 frp->fr_parent->fr_child = frp->fr_next;
4727 if (frp->fr_next != NULL)
4728 frp->fr_next->fr_prev = frp->fr_prev;
4729}
4730
4731#endif /* FEAT_WINDOWS */
4732
4733/*
4734 * Allocate w_lines[] for window "wp".
4735 * Return FAIL for failure, OK for success.
4736 */
4737 int
4738win_alloc_lines(wp)
4739 win_T *wp;
4740{
4741 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004742 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if (wp->w_lines == NULL)
4744 return FAIL;
4745 return OK;
4746}
4747
4748/*
4749 * free lsize arrays for a window
4750 */
4751 void
4752win_free_lsize(wp)
4753 win_T *wp;
4754{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004755 /* TODO: why would wp be NULL here? */
4756 if (wp != NULL)
4757 {
4758 vim_free(wp->w_lines);
4759 wp->w_lines = NULL;
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761}
4762
4763/*
4764 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004765 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 */
4767 void
4768shell_new_rows()
4769{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004770 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 if (firstwin == NULL) /* not initialized yet */
4773 return;
4774#ifdef FEAT_WINDOWS
4775 if (h < frame_minheight(topframe, NULL))
4776 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004777
4778 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 * that doesn't result in the right height, forget about that option. */
4780 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004781 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 frame_new_height(topframe, h, FALSE, FALSE);
4783
4784 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4785#else
4786 if (h < 1)
4787 h = 1;
4788 win_new_height(firstwin, h);
4789#endif
4790 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004791#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004792 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004793#endif
4794
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795#if 0
4796 /* Disabled: don't want making the screen smaller make a window larger. */
4797 if (p_ea)
4798 win_equal(curwin, FALSE, 'v');
4799#endif
4800}
4801
4802#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4803/*
4804 * Called from win_new_shellsize() after Columns changed.
4805 */
4806 void
4807shell_new_columns()
4808{
4809 if (firstwin == NULL) /* not initialized yet */
4810 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004811
4812 /* First try setting the widths of windows with 'winfixwidth'. If that
4813 * doesn't result in the right width, forget about that option. */
4814 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004815 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004816 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4819#if 0
4820 /* Disabled: don't want making the screen smaller make a window larger. */
4821 if (p_ea)
4822 win_equal(curwin, FALSE, 'h');
4823#endif
4824}
4825#endif
4826
4827#if defined(FEAT_CMDWIN) || defined(PROTO)
4828/*
4829 * Save the size of all windows in "gap".
4830 */
4831 void
4832win_size_save(gap)
4833 garray_T *gap;
4834
4835{
4836 win_T *wp;
4837
4838 ga_init2(gap, (int)sizeof(int), 1);
4839 if (ga_grow(gap, win_count() * 2) == OK)
4840 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4841 {
4842 ((int *)gap->ga_data)[gap->ga_len++] =
4843 wp->w_width + wp->w_vsep_width;
4844 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4845 }
4846}
4847
4848/*
4849 * Restore window sizes, but only if the number of windows is still the same.
4850 * Does not free the growarray.
4851 */
4852 void
4853win_size_restore(gap)
4854 garray_T *gap;
4855{
4856 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004857 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 if (win_count() * 2 == gap->ga_len)
4860 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004861 /* The order matters, because frames contain other frames, but it's
4862 * difficult to get right. The easy way out is to do it twice. */
4863 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004865 i = 0;
4866 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4867 {
4868 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4869 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 /* recompute the window positions */
4873 (void)win_comp_pos();
4874 }
4875}
4876#endif /* FEAT_CMDWIN */
4877
4878#if defined(FEAT_WINDOWS) || defined(PROTO)
4879/*
4880 * Update the position for all windows, using the width and height of the
4881 * frames.
4882 * Returns the row just after the last window.
4883 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004884 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885win_comp_pos()
4886{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004887 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 int col = 0;
4889
4890 frame_comp_pos(topframe, &row, &col);
4891 return row;
4892}
4893
4894/*
4895 * Update the position of the windows in frame "topfrp", using the width and
4896 * height of the frames.
4897 * "*row" and "*col" are the top-left position of the frame. They are updated
4898 * to the bottom-right position plus one.
4899 */
4900 static void
4901frame_comp_pos(topfrp, row, col)
4902 frame_T *topfrp;
4903 int *row;
4904 int *col;
4905{
4906 win_T *wp;
4907 frame_T *frp;
4908#ifdef FEAT_VERTSPLIT
4909 int startcol;
4910 int startrow;
4911#endif
4912
4913 wp = topfrp->fr_win;
4914 if (wp != NULL)
4915 {
4916 if (wp->w_winrow != *row
4917#ifdef FEAT_VERTSPLIT
4918 || wp->w_wincol != *col
4919#endif
4920 )
4921 {
4922 /* position changed, redraw */
4923 wp->w_winrow = *row;
4924#ifdef FEAT_VERTSPLIT
4925 wp->w_wincol = *col;
4926#endif
4927 redraw_win_later(wp, NOT_VALID);
4928 wp->w_redr_status = TRUE;
4929 }
4930 *row += wp->w_height + wp->w_status_height;
4931#ifdef FEAT_VERTSPLIT
4932 *col += wp->w_width + wp->w_vsep_width;
4933#endif
4934 }
4935 else
4936 {
4937#ifdef FEAT_VERTSPLIT
4938 startrow = *row;
4939 startcol = *col;
4940#endif
4941 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4942 {
4943#ifdef FEAT_VERTSPLIT
4944 if (topfrp->fr_layout == FR_ROW)
4945 *row = startrow; /* all frames are at the same row */
4946 else
4947 *col = startcol; /* all frames are at the same col */
4948#endif
4949 frame_comp_pos(frp, row, col);
4950 }
4951 }
4952}
4953
4954#endif /* FEAT_WINDOWS */
4955
4956/*
4957 * Set current window height and take care of repositioning other windows to
4958 * fit around it.
4959 */
4960 void
4961win_setheight(height)
4962 int height;
4963{
4964 win_setheight_win(height, curwin);
4965}
4966
4967/*
4968 * Set the window height of window "win" and take care of repositioning other
4969 * windows to fit around it.
4970 */
4971 void
4972win_setheight_win(height, win)
4973 int height;
4974 win_T *win;
4975{
4976 int row;
4977
4978 if (win == curwin)
4979 {
4980 /* Always keep current window at least one line high, even when
4981 * 'winminheight' is zero. */
4982#ifdef FEAT_WINDOWS
4983 if (height < p_wmh)
4984 height = p_wmh;
4985#endif
4986 if (height == 0)
4987 height = 1;
4988 }
4989
4990#ifdef FEAT_WINDOWS
4991 frame_setheight(win->w_frame, height + win->w_status_height);
4992
4993 /* recompute the window positions */
4994 row = win_comp_pos();
4995#else
4996 if (height > topframe->fr_height)
4997 height = topframe->fr_height;
4998 win->w_height = height;
4999 row = height;
5000#endif
5001
5002 /*
5003 * If there is extra space created between the last window and the command
5004 * line, clear it.
5005 */
5006 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5007 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5008 cmdline_row = row;
5009 msg_row = row;
5010 msg_col = 0;
5011
5012 redraw_all_later(NOT_VALID);
5013}
5014
5015#if defined(FEAT_WINDOWS) || defined(PROTO)
5016
5017/*
5018 * Set the height of a frame to "height" and take care that all frames and
5019 * windows inside it are resized. Also resize frames on the left and right if
5020 * the are in the same FR_ROW frame.
5021 *
5022 * Strategy:
5023 * If the frame is part of a FR_COL frame, try fitting the frame in that
5024 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5025 * go to containing frames to resize them and make room.
5026 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5027 * Check for the minimal height of the FR_ROW frame.
5028 * At the top level we can also use change the command line height.
5029 */
5030 static void
5031frame_setheight(curfrp, height)
5032 frame_T *curfrp;
5033 int height;
5034{
5035 int room; /* total number of lines available */
5036 int take; /* number of lines taken from other windows */
5037 int room_cmdline; /* lines available from cmdline */
5038 int run;
5039 frame_T *frp;
5040 int h;
5041 int room_reserved;
5042
5043 /* If the height already is the desired value, nothing to do. */
5044 if (curfrp->fr_height == height)
5045 return;
5046
5047 if (curfrp->fr_parent == NULL)
5048 {
5049 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005050 if (height > ROWS_AVAIL)
5051 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (height > 0)
5053 frame_new_height(curfrp, height, FALSE, FALSE);
5054 }
5055 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5056 {
5057 /* Row of frames: Also need to resize frames left and right of this
5058 * one. First check for the minimal height of these. */
5059 h = frame_minheight(curfrp->fr_parent, NULL);
5060 if (height < h)
5061 height = h;
5062 frame_setheight(curfrp->fr_parent, height);
5063 }
5064 else
5065 {
5066 /*
5067 * Column of frames: try to change only frames in this column.
5068 */
5069#ifdef FEAT_VERTSPLIT
5070 /*
5071 * Do this twice:
5072 * 1: compute room available, if it's not enough try resizing the
5073 * containing frame.
5074 * 2: compute the room available and adjust the height to it.
5075 * Try not to reduce the height of a window with 'winfixheight' set.
5076 */
5077 for (run = 1; run <= 2; ++run)
5078#else
5079 for (;;)
5080#endif
5081 {
5082 room = 0;
5083 room_reserved = 0;
5084 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5085 frp = frp->fr_next)
5086 {
5087 if (frp != curfrp
5088 && frp->fr_win != NULL
5089 && frp->fr_win->w_p_wfh)
5090 room_reserved += frp->fr_height;
5091 room += frp->fr_height;
5092 if (frp != curfrp)
5093 room -= frame_minheight(frp, NULL);
5094 }
5095#ifdef FEAT_VERTSPLIT
5096 if (curfrp->fr_width != Columns)
5097 room_cmdline = 0;
5098 else
5099#endif
5100 {
5101 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5102 + lastwin->w_height + lastwin->w_status_height);
5103 if (room_cmdline < 0)
5104 room_cmdline = 0;
5105 }
5106
5107 if (height <= room + room_cmdline)
5108 break;
5109#ifdef FEAT_VERTSPLIT
5110 if (run == 2 || curfrp->fr_width == Columns)
5111#endif
5112 {
5113 if (height > room + room_cmdline)
5114 height = room + room_cmdline;
5115 break;
5116 }
5117#ifdef FEAT_VERTSPLIT
5118 frame_setheight(curfrp->fr_parent, height
5119 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5120#endif
5121 /*NOTREACHED*/
5122 }
5123
5124 /*
5125 * Compute the number of lines we will take from others frames (can be
5126 * negative!).
5127 */
5128 take = height - curfrp->fr_height;
5129
5130 /* If there is not enough room, also reduce the height of a window
5131 * with 'winfixheight' set. */
5132 if (height > room + room_cmdline - room_reserved)
5133 room_reserved = room + room_cmdline - height;
5134 /* If there is only a 'winfixheight' window and making the
5135 * window smaller, need to make the other window taller. */
5136 if (take < 0 && room - curfrp->fr_height < room_reserved)
5137 room_reserved = 0;
5138
5139 if (take > 0 && room_cmdline > 0)
5140 {
5141 /* use lines from cmdline first */
5142 if (take < room_cmdline)
5143 room_cmdline = take;
5144 take -= room_cmdline;
5145 topframe->fr_height += room_cmdline;
5146 }
5147
5148 /*
5149 * set the current frame to the new height
5150 */
5151 frame_new_height(curfrp, height, FALSE, FALSE);
5152
5153 /*
5154 * First take lines from the frames after the current frame. If
5155 * that is not enough, takes lines from frames above the current
5156 * frame.
5157 */
5158 for (run = 0; run < 2; ++run)
5159 {
5160 if (run == 0)
5161 frp = curfrp->fr_next; /* 1st run: start with next window */
5162 else
5163 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5164 while (frp != NULL && take != 0)
5165 {
5166 h = frame_minheight(frp, NULL);
5167 if (room_reserved > 0
5168 && frp->fr_win != NULL
5169 && frp->fr_win->w_p_wfh)
5170 {
5171 if (room_reserved >= frp->fr_height)
5172 room_reserved -= frp->fr_height;
5173 else
5174 {
5175 if (frp->fr_height - room_reserved > take)
5176 room_reserved = frp->fr_height - take;
5177 take -= frp->fr_height - room_reserved;
5178 frame_new_height(frp, room_reserved, FALSE, FALSE);
5179 room_reserved = 0;
5180 }
5181 }
5182 else
5183 {
5184 if (frp->fr_height - take < h)
5185 {
5186 take -= frp->fr_height - h;
5187 frame_new_height(frp, h, FALSE, FALSE);
5188 }
5189 else
5190 {
5191 frame_new_height(frp, frp->fr_height - take,
5192 FALSE, FALSE);
5193 take = 0;
5194 }
5195 }
5196 if (run == 0)
5197 frp = frp->fr_next;
5198 else
5199 frp = frp->fr_prev;
5200 }
5201 }
5202 }
5203}
5204
5205#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5206/*
5207 * Set current window width and take care of repositioning other windows to
5208 * fit around it.
5209 */
5210 void
5211win_setwidth(width)
5212 int width;
5213{
5214 win_setwidth_win(width, curwin);
5215}
5216
5217 void
5218win_setwidth_win(width, wp)
5219 int width;
5220 win_T *wp;
5221{
5222 /* Always keep current window at least one column wide, even when
5223 * 'winminwidth' is zero. */
5224 if (wp == curwin)
5225 {
5226 if (width < p_wmw)
5227 width = p_wmw;
5228 if (width == 0)
5229 width = 1;
5230 }
5231
5232 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5233
5234 /* recompute the window positions */
5235 (void)win_comp_pos();
5236
5237 redraw_all_later(NOT_VALID);
5238}
5239
5240/*
5241 * Set the width of a frame to "width" and take care that all frames and
5242 * windows inside it are resized. Also resize frames above and below if the
5243 * are in the same FR_ROW frame.
5244 *
5245 * Strategy is similar to frame_setheight().
5246 */
5247 static void
5248frame_setwidth(curfrp, width)
5249 frame_T *curfrp;
5250 int width;
5251{
5252 int room; /* total number of lines available */
5253 int take; /* number of lines taken from other windows */
5254 int run;
5255 frame_T *frp;
5256 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005257 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259 /* If the width already is the desired value, nothing to do. */
5260 if (curfrp->fr_width == width)
5261 return;
5262
5263 if (curfrp->fr_parent == NULL)
5264 /* topframe: can't change width */
5265 return;
5266
5267 if (curfrp->fr_parent->fr_layout == FR_COL)
5268 {
5269 /* Column of frames: Also need to resize frames above and below of
5270 * this one. First check for the minimal width of these. */
5271 w = frame_minwidth(curfrp->fr_parent, NULL);
5272 if (width < w)
5273 width = w;
5274 frame_setwidth(curfrp->fr_parent, width);
5275 }
5276 else
5277 {
5278 /*
5279 * Row of frames: try to change only frames in this row.
5280 *
5281 * Do this twice:
5282 * 1: compute room available, if it's not enough try resizing the
5283 * containing frame.
5284 * 2: compute the room available and adjust the width to it.
5285 */
5286 for (run = 1; run <= 2; ++run)
5287 {
5288 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005289 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5291 frp = frp->fr_next)
5292 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005293 if (frp != curfrp
5294 && frp->fr_win != NULL
5295 && frp->fr_win->w_p_wfw)
5296 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 room += frp->fr_width;
5298 if (frp != curfrp)
5299 room -= frame_minwidth(frp, NULL);
5300 }
5301
5302 if (width <= room)
5303 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005304 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
5306 if (width > room)
5307 width = room;
5308 break;
5309 }
5310 frame_setwidth(curfrp->fr_parent, width
5311 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5312 }
5313
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 /*
5315 * Compute the number of lines we will take from others frames (can be
5316 * negative!).
5317 */
5318 take = width - curfrp->fr_width;
5319
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005320 /* If there is not enough room, also reduce the width of a window
5321 * with 'winfixwidth' set. */
5322 if (width > room - room_reserved)
5323 room_reserved = room - width;
5324 /* If there is only a 'winfixwidth' window and making the
5325 * window smaller, need to make the other window narrower. */
5326 if (take < 0 && room - curfrp->fr_width < room_reserved)
5327 room_reserved = 0;
5328
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 /*
5330 * set the current frame to the new width
5331 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005332 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
5334 /*
5335 * First take lines from the frames right of the current frame. If
5336 * that is not enough, takes lines from frames left of the current
5337 * frame.
5338 */
5339 for (run = 0; run < 2; ++run)
5340 {
5341 if (run == 0)
5342 frp = curfrp->fr_next; /* 1st run: start with next window */
5343 else
5344 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5345 while (frp != NULL && take != 0)
5346 {
5347 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005348 if (room_reserved > 0
5349 && frp->fr_win != NULL
5350 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005352 if (room_reserved >= frp->fr_width)
5353 room_reserved -= frp->fr_width;
5354 else
5355 {
5356 if (frp->fr_width - room_reserved > take)
5357 room_reserved = frp->fr_width - take;
5358 take -= frp->fr_width - room_reserved;
5359 frame_new_width(frp, room_reserved, FALSE, FALSE);
5360 room_reserved = 0;
5361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 else
5364 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005365 if (frp->fr_width - take < w)
5366 {
5367 take -= frp->fr_width - w;
5368 frame_new_width(frp, w, FALSE, FALSE);
5369 }
5370 else
5371 {
5372 frame_new_width(frp, frp->fr_width - take,
5373 FALSE, FALSE);
5374 take = 0;
5375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377 if (run == 0)
5378 frp = frp->fr_next;
5379 else
5380 frp = frp->fr_prev;
5381 }
5382 }
5383 }
5384}
5385#endif /* FEAT_VERTSPLIT */
5386
5387/*
5388 * Check 'winminheight' for a valid value.
5389 */
5390 void
5391win_setminheight()
5392{
5393 int room;
5394 int first = TRUE;
5395 win_T *wp;
5396
5397 /* loop until there is a 'winminheight' that is possible */
5398 while (p_wmh > 0)
5399 {
5400 /* TODO: handle vertical splits */
5401 room = -p_wh;
5402 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5403 room += wp->w_height - p_wmh;
5404 if (room >= 0)
5405 break;
5406 --p_wmh;
5407 if (first)
5408 {
5409 EMSG(_(e_noroom));
5410 first = FALSE;
5411 }
5412 }
5413}
5414
5415#ifdef FEAT_MOUSE
5416
5417/*
5418 * Status line of dragwin is dragged "offset" lines down (negative is up).
5419 */
5420 void
5421win_drag_status_line(dragwin, offset)
5422 win_T *dragwin;
5423 int offset;
5424{
5425 frame_T *curfr;
5426 frame_T *fr;
5427 int room;
5428 int row;
5429 int up; /* if TRUE, drag status line up, otherwise down */
5430 int n;
5431
5432 fr = dragwin->w_frame;
5433 curfr = fr;
5434 if (fr != topframe) /* more than one window */
5435 {
5436 fr = fr->fr_parent;
5437 /* When the parent frame is not a column of frames, its parent should
5438 * be. */
5439 if (fr->fr_layout != FR_COL)
5440 {
5441 curfr = fr;
5442 if (fr != topframe) /* only a row of windows, may drag statusline */
5443 fr = fr->fr_parent;
5444 }
5445 }
5446
5447 /* If this is the last frame in a column, may want to resize the parent
5448 * frame instead (go two up to skip a row of frames). */
5449 while (curfr != topframe && curfr->fr_next == NULL)
5450 {
5451 if (fr != topframe)
5452 fr = fr->fr_parent;
5453 curfr = fr;
5454 if (fr != topframe)
5455 fr = fr->fr_parent;
5456 }
5457
5458 if (offset < 0) /* drag up */
5459 {
5460 up = TRUE;
5461 offset = -offset;
5462 /* sum up the room of the current frame and above it */
5463 if (fr == curfr)
5464 {
5465 /* only one window */
5466 room = fr->fr_height - frame_minheight(fr, NULL);
5467 }
5468 else
5469 {
5470 room = 0;
5471 for (fr = fr->fr_child; ; fr = fr->fr_next)
5472 {
5473 room += fr->fr_height - frame_minheight(fr, NULL);
5474 if (fr == curfr)
5475 break;
5476 }
5477 }
5478 fr = curfr->fr_next; /* put fr at frame that grows */
5479 }
5480 else /* drag down */
5481 {
5482 up = FALSE;
5483 /*
5484 * Only dragging the last status line can reduce p_ch.
5485 */
5486 room = Rows - cmdline_row;
5487 if (curfr->fr_next == NULL)
5488 room -= 1;
5489 else
5490 room -= p_ch;
5491 if (room < 0)
5492 room = 0;
5493 /* sum up the room of frames below of the current one */
5494 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5495 room += fr->fr_height - frame_minheight(fr, NULL);
5496 fr = curfr; /* put fr at window that grows */
5497 }
5498
5499 if (room < offset) /* Not enough room */
5500 offset = room; /* Move as far as we can */
5501 if (offset <= 0)
5502 return;
5503
5504 /*
5505 * Grow frame fr by "offset" lines.
5506 * Doesn't happen when dragging the last status line up.
5507 */
5508 if (fr != NULL)
5509 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5510
5511 if (up)
5512 fr = curfr; /* current frame gets smaller */
5513 else
5514 fr = curfr->fr_next; /* next frame gets smaller */
5515
5516 /*
5517 * Now make the other frames smaller.
5518 */
5519 while (fr != NULL && offset > 0)
5520 {
5521 n = frame_minheight(fr, NULL);
5522 if (fr->fr_height - offset <= n)
5523 {
5524 offset -= fr->fr_height - n;
5525 frame_new_height(fr, n, !up, FALSE);
5526 }
5527 else
5528 {
5529 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5530 break;
5531 }
5532 if (up)
5533 fr = fr->fr_prev;
5534 else
5535 fr = fr->fr_next;
5536 }
5537 row = win_comp_pos();
5538 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5539 cmdline_row = row;
5540 p_ch = Rows - cmdline_row;
5541 if (p_ch < 1)
5542 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005543 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005544 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 showmode();
5546}
5547
5548#ifdef FEAT_VERTSPLIT
5549/*
5550 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5551 */
5552 void
5553win_drag_vsep_line(dragwin, offset)
5554 win_T *dragwin;
5555 int offset;
5556{
5557 frame_T *curfr;
5558 frame_T *fr;
5559 int room;
5560 int left; /* if TRUE, drag separator line left, otherwise right */
5561 int n;
5562
5563 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005564 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 return;
5566 curfr = fr;
5567 fr = fr->fr_parent;
5568 /* When the parent frame is not a row of frames, its parent should be. */
5569 if (fr->fr_layout != FR_ROW)
5570 {
5571 if (fr == topframe) /* only a column of windows (cannot happen?) */
5572 return;
5573 curfr = fr;
5574 fr = fr->fr_parent;
5575 }
5576
5577 /* If this is the last frame in a row, may want to resize a parent
5578 * frame instead. */
5579 while (curfr->fr_next == NULL)
5580 {
5581 if (fr == topframe)
5582 break;
5583 curfr = fr;
5584 fr = fr->fr_parent;
5585 if (fr != topframe)
5586 {
5587 curfr = fr;
5588 fr = fr->fr_parent;
5589 }
5590 }
5591
5592 if (offset < 0) /* drag left */
5593 {
5594 left = TRUE;
5595 offset = -offset;
5596 /* sum up the room of the current frame and left of it */
5597 room = 0;
5598 for (fr = fr->fr_child; ; fr = fr->fr_next)
5599 {
5600 room += fr->fr_width - frame_minwidth(fr, NULL);
5601 if (fr == curfr)
5602 break;
5603 }
5604 fr = curfr->fr_next; /* put fr at frame that grows */
5605 }
5606 else /* drag right */
5607 {
5608 left = FALSE;
5609 /* sum up the room of frames right of the current one */
5610 room = 0;
5611 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5612 room += fr->fr_width - frame_minwidth(fr, NULL);
5613 fr = curfr; /* put fr at window that grows */
5614 }
5615
5616 if (room < offset) /* Not enough room */
5617 offset = room; /* Move as far as we can */
5618 if (offset <= 0) /* No room at all, quit. */
5619 return;
5620
5621 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005622 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 /* shrink other frames: current and at the left or at the right */
5625 if (left)
5626 fr = curfr; /* current frame gets smaller */
5627 else
5628 fr = curfr->fr_next; /* next frame gets smaller */
5629
5630 while (fr != NULL && offset > 0)
5631 {
5632 n = frame_minwidth(fr, NULL);
5633 if (fr->fr_width - offset <= n)
5634 {
5635 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005636 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638 else
5639 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005640 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 break;
5642 }
5643 if (left)
5644 fr = fr->fr_prev;
5645 else
5646 fr = fr->fr_next;
5647 }
5648 (void)win_comp_pos();
5649 redraw_all_later(NOT_VALID);
5650}
5651#endif /* FEAT_VERTSPLIT */
5652#endif /* FEAT_MOUSE */
5653
5654#endif /* FEAT_WINDOWS */
5655
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005656#define FRACTION_MULT 16384L
5657
5658/*
5659 * Set wp->w_fraction for the current w_wrow and w_height.
5660 */
5661 static void
5662set_fraction(wp)
5663 win_T *wp;
5664{
5665 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005666 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005667}
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669/*
5670 * Set the height of a window.
5671 * This takes care of the things inside the window, not what happens to the
5672 * window position, the frame or to other windows.
5673 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005674 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675win_new_height(wp, height)
5676 win_T *wp;
5677 int height;
5678{
5679 linenr_T lnum;
5680 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005681 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
5683 /* Don't want a negative height. Happens when splitting a tiny window.
5684 * Will equalize heights soon to fix it. */
5685 if (height < 0)
5686 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005687 if (wp->w_height == height)
5688 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005690 if (wp->w_height > 0)
5691 {
5692 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005693 /* w_wrow needs to be valid. When setting 'laststatus' this may
5694 * call win_new_height() recursively. */
5695 validate_cursor();
5696 if (wp->w_height != prev_height)
5697 return; /* Recursive call already changed the size, bail out here
5698 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005699 if (wp->w_wrow != wp->w_prev_fraction_row)
5700 set_fraction(wp);
5701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703 wp->w_height = height;
5704 wp->w_skipcol = 0;
5705
5706 /* Don't change w_topline when height is zero. Don't set w_topline when
5707 * 'scrollbind' is set and this isn't the current window. */
5708 if (height > 0
5709#ifdef FEAT_SCROLLBIND
5710 && (!wp->w_p_scb || wp == curwin)
5711#endif
5712 )
5713 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005714 /*
5715 * Find a value for w_topline that shows the cursor at the same
5716 * relative position in the window as before (more or less).
5717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 lnum = wp->w_cursor.lnum;
5719 if (lnum < 1) /* can happen when starting up */
5720 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005721 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5722 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5724 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005725
5726 if (sline >= 0)
5727 {
5728 /* Make sure the whole cursor line is visible, if possible. */
5729 int rows = plines_win(wp, lnum, FALSE);
5730
5731 if (sline > wp->w_height - rows)
5732 {
5733 sline = wp->w_height - rows;
5734 wp->w_wrow -= rows - line_size;
5735 }
5736 }
5737
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (sline < 0)
5739 {
5740 /*
5741 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005742 * Make cursor line the first line in the window. If not enough
5743 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 */
5745 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005746 if (wp->w_wrow >= wp->w_height
5747 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5748 {
5749 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5750 --wp->w_wrow;
5751 while (wp->w_wrow >= wp->w_height)
5752 {
5753 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5754 + win_col_off2(wp);
5755 --wp->w_wrow;
5756 }
5757 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005758 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005760 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005762 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 {
5764#ifdef FEAT_FOLDING
5765 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5766 if (lnum == 1)
5767 {
5768 /* first line in buffer is folded */
5769 line_size = 1;
5770 --sline;
5771 break;
5772 }
5773#endif
5774 --lnum;
5775#ifdef FEAT_DIFF
5776 if (lnum == wp->w_topline)
5777 line_size = plines_win_nofill(wp, lnum, TRUE)
5778 + wp->w_topfill;
5779 else
5780#endif
5781 line_size = plines_win(wp, lnum, TRUE);
5782 sline -= line_size;
5783 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 if (sline < 0)
5786 {
5787 /*
5788 * Line we want at top would go off top of screen. Use next
5789 * line instead.
5790 */
5791#ifdef FEAT_FOLDING
5792 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5793#endif
5794 lnum++;
5795 wp->w_wrow -= line_size + sline;
5796 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005797 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
5799 /* First line of file reached, use that as topline. */
5800 lnum = 1;
5801 wp->w_wrow -= sline;
5802 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005803
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005804 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807
5808 if (wp == curwin)
5809 {
5810 if (p_so)
5811 update_topline();
5812 curs_columns(FALSE); /* validate w_wrow */
5813 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005814 if (prev_height > 0)
5815 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
5817 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005818 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819#ifdef FEAT_WINDOWS
5820 wp->w_redr_status = TRUE;
5821#endif
5822 invalidate_botline_win(wp);
5823}
5824
5825#ifdef FEAT_VERTSPLIT
5826/*
5827 * Set the width of a window.
5828 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005829 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830win_new_width(wp, width)
5831 win_T *wp;
5832 int width;
5833{
5834 wp->w_width = width;
5835 wp->w_lines_valid = 0;
5836 changed_line_abv_curs_win(wp);
5837 invalidate_botline_win(wp);
5838 if (wp == curwin)
5839 {
5840 update_topline();
5841 curs_columns(TRUE); /* validate w_wrow */
5842 }
5843 redraw_win_later(wp, NOT_VALID);
5844 wp->w_redr_status = TRUE;
5845}
5846#endif
5847
5848 void
5849win_comp_scroll(wp)
5850 win_T *wp;
5851{
5852 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5853 if (wp->w_p_scr == 0)
5854 wp->w_p_scr = 1;
5855}
5856
5857/*
5858 * command_height: called whenever p_ch has been changed
5859 */
5860 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005861command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863#ifdef FEAT_WINDOWS
5864 int h;
5865 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005866 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005868 /* Use the value of p_ch that we remembered. This is needed for when the
5869 * GUI starts up, we can't be sure in what order things happen. And when
5870 * p_ch was changed in another tab page. */
5871 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 /* Find bottom frame with width of screen. */
5874 frp = lastwin->w_frame;
5875# ifdef FEAT_VERTSPLIT
5876 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5877 frp = frp->fr_parent;
5878# endif
5879
5880 /* Avoid changing the height of a window with 'winfixheight' set. */
5881 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5882 && frp->fr_win->w_p_wfh)
5883 frp = frp->fr_prev;
5884
5885 if (starting != NO_SCREEN)
5886 {
5887 cmdline_row = Rows - p_ch;
5888
5889 if (p_ch > old_p_ch) /* p_ch got bigger */
5890 {
5891 while (p_ch > old_p_ch)
5892 {
5893 if (frp == NULL)
5894 {
5895 EMSG(_(e_noroom));
5896 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005897 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 cmdline_row = Rows - p_ch;
5899 break;
5900 }
5901 h = frp->fr_height - frame_minheight(frp, NULL);
5902 if (h > p_ch - old_p_ch)
5903 h = p_ch - old_p_ch;
5904 old_p_ch += h;
5905 frame_add_height(frp, -h);
5906 frp = frp->fr_prev;
5907 }
5908
5909 /* Recompute window positions. */
5910 (void)win_comp_pos();
5911
5912 /* clear the lines added to cmdline */
5913 if (full_screen)
5914 screen_fill((int)(cmdline_row), (int)Rows, 0,
5915 (int)Columns, ' ', ' ', 0);
5916 msg_row = cmdline_row;
5917 redraw_cmdline = TRUE;
5918 return;
5919 }
5920
5921 if (msg_row < cmdline_row)
5922 msg_row = cmdline_row;
5923 redraw_cmdline = TRUE;
5924 }
5925 frame_add_height(frp, (int)(old_p_ch - p_ch));
5926
5927 /* Recompute window positions. */
5928 if (frp != lastwin->w_frame)
5929 (void)win_comp_pos();
5930#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005932 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933#endif
5934}
5935
5936#if defined(FEAT_WINDOWS) || defined(PROTO)
5937/*
5938 * Resize frame "frp" to be "n" lines higher (negative for less high).
5939 * Also resize the frames it is contained in.
5940 */
5941 static void
5942frame_add_height(frp, n)
5943 frame_T *frp;
5944 int n;
5945{
5946 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5947 for (;;)
5948 {
5949 frp = frp->fr_parent;
5950 if (frp == NULL)
5951 break;
5952 frp->fr_height += n;
5953 }
5954}
5955
5956/*
5957 * Add or remove a status line for the bottom window(s), according to the
5958 * value of 'laststatus'.
5959 */
5960 void
5961last_status(morewin)
5962 int morewin; /* pretend there are two or more windows */
5963{
5964 /* Don't make a difference between horizontal or vertical split. */
5965 last_status_rec(topframe, (p_ls == 2
5966 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5967}
5968
5969 static void
5970last_status_rec(fr, statusline)
5971 frame_T *fr;
5972 int statusline;
5973{
5974 frame_T *fp;
5975 win_T *wp;
5976
5977 if (fr->fr_layout == FR_LEAF)
5978 {
5979 wp = fr->fr_win;
5980 if (wp->w_status_height != 0 && !statusline)
5981 {
5982 /* remove status line */
5983 win_new_height(wp, wp->w_height + 1);
5984 wp->w_status_height = 0;
5985 comp_col();
5986 }
5987 else if (wp->w_status_height == 0 && statusline)
5988 {
5989 /* Find a frame to take a line from. */
5990 fp = fr;
5991 while (fp->fr_height <= frame_minheight(fp, NULL))
5992 {
5993 if (fp == topframe)
5994 {
5995 EMSG(_(e_noroom));
5996 return;
5997 }
5998 /* In a column of frames: go to frame above. If already at
5999 * the top or in a row of frames: go to parent. */
6000 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6001 fp = fp->fr_prev;
6002 else
6003 fp = fp->fr_parent;
6004 }
6005 wp->w_status_height = 1;
6006 if (fp != fr)
6007 {
6008 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6009 frame_fix_height(wp);
6010 (void)win_comp_pos();
6011 }
6012 else
6013 win_new_height(wp, wp->w_height - 1);
6014 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006015 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 }
6017 }
6018#ifdef FEAT_VERTSPLIT
6019 else if (fr->fr_layout == FR_ROW)
6020 {
6021 /* vertically split windows, set status line for each one */
6022 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6023 last_status_rec(fp, statusline);
6024 }
6025#endif
6026 else
6027 {
6028 /* horizontally split window, set status line for last one */
6029 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6030 ;
6031 last_status_rec(fp, statusline);
6032 }
6033}
6034
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006035/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006036 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006037 */
6038 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006039tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006040{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006041#ifdef FEAT_GUI_TABLINE
6042 /* When the GUI has the tabline then this always returns zero. */
6043 if (gui_use_tabline())
6044 return 0;
6045#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006046 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006047 {
6048 case 0: return 0;
6049 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6050 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006051 return 1;
6052}
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054#endif /* FEAT_WINDOWS */
6055
6056#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6057/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006058 * Get the file name at the cursor.
6059 * If Visual mode is active, use the selected text if it's in one line.
6060 * Returns the name in allocated memory, NULL for failure.
6061 */
6062 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006063grab_file_name(count, file_lnum)
6064 long count;
6065 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006066{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006067 if (VIsual_active)
6068 {
6069 int len;
6070 char_u *ptr;
6071
6072 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6073 return NULL;
6074 return find_file_name_in_path(ptr, len,
6075 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6076 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006077 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6078 file_lnum);
6079
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006080}
6081
6082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 * Return the file name under or after the cursor.
6084 *
6085 * The 'path' option is searched if the file name is not absolute.
6086 * The string returned has been alloc'ed and should be freed by the caller.
6087 * NULL is returned if the file name or file is not found.
6088 *
6089 * options:
6090 * FNAME_MESS give error messages
6091 * FNAME_EXP expand to path
6092 * FNAME_HYP check for hypertext link
6093 * FNAME_INCL apply "includeexpr"
6094 */
6095 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006096file_name_at_cursor(options, count, file_lnum)
6097 int options;
6098 long count;
6099 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
6101 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006102 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6103 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104}
6105
6106/*
6107 * Return the name of the file under or after ptr[col].
6108 * Otherwise like file_name_at_cursor().
6109 */
6110 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006111file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 char_u *line;
6113 int col;
6114 int options;
6115 long count;
6116 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006117 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 char_u *ptr;
6120 int len;
6121
6122 /*
6123 * search forward for what could be the start of a file name
6124 */
6125 ptr = line + col;
6126 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006127 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (*ptr == NUL) /* nothing found */
6129 {
6130 if (options & FNAME_MESS)
6131 EMSG(_("E446: No file name under cursor"));
6132 return NULL;
6133 }
6134
6135 /*
6136 * Search backward for first char of the file name.
6137 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6138 */
6139 while (ptr > line)
6140 {
6141#ifdef FEAT_MBYTE
6142 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6143 ptr -= len + 1;
6144 else
6145#endif
6146 if (vim_isfilec(ptr[-1])
6147 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6148 --ptr;
6149 else
6150 break;
6151 }
6152
6153 /*
6154 * Search forward for the last char of the file name.
6155 * Also allow "://" when ':' is not in 'isfname'.
6156 */
6157 len = 0;
6158 while (vim_isfilec(ptr[len])
6159 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6160#ifdef FEAT_MBYTE
6161 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006162 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 else
6164#endif
6165 ++len;
6166
6167 /*
6168 * If there is trailing punctuation, remove it.
6169 * But don't remove "..", could be a directory name.
6170 */
6171 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6172 && ptr[len - 2] != '.')
6173 --len;
6174
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006175 if (file_lnum != NULL)
6176 {
6177 char_u *p;
6178
6179 /* Get the number after the file name and a separator character */
6180 p = ptr + len;
6181 p = skipwhite(p);
6182 if (*p != NUL)
6183 {
6184 if (!isdigit(*p))
6185 ++p; /* skip the separator */
6186 p = skipwhite(p);
6187 if (isdigit(*p))
6188 *file_lnum = (int)getdigits(&p);
6189 }
6190 }
6191
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6193}
6194
6195# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6196static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6197
6198 static char_u *
6199eval_includeexpr(ptr, len)
6200 char_u *ptr;
6201 int len;
6202{
6203 char_u *res;
6204
6205 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006206 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006207 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 set_vim_var_string(VV_FNAME, NULL, 0);
6209 return res;
6210}
6211#endif
6212
6213/*
6214 * Return the name of the file ptr[len] in 'path'.
6215 * Otherwise like file_name_at_cursor().
6216 */
6217 char_u *
6218find_file_name_in_path(ptr, len, options, count, rel_fname)
6219 char_u *ptr;
6220 int len;
6221 int options;
6222 long count;
6223 char_u *rel_fname; /* file we are searching relative to */
6224{
6225 char_u *file_name;
6226 int c;
6227# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6228 char_u *tofree = NULL;
6229
6230 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6231 {
6232 tofree = eval_includeexpr(ptr, len);
6233 if (tofree != NULL)
6234 {
6235 ptr = tofree;
6236 len = (int)STRLEN(ptr);
6237 }
6238 }
6239# endif
6240
6241 if (options & FNAME_EXP)
6242 {
6243 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6244 TRUE, rel_fname);
6245
6246# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6247 /*
6248 * If the file could not be found in a normal way, try applying
6249 * 'includeexpr' (unless done already).
6250 */
6251 if (file_name == NULL
6252 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6253 {
6254 tofree = eval_includeexpr(ptr, len);
6255 if (tofree != NULL)
6256 {
6257 ptr = tofree;
6258 len = (int)STRLEN(ptr);
6259 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6260 TRUE, rel_fname);
6261 }
6262 }
6263# endif
6264 if (file_name == NULL && (options & FNAME_MESS))
6265 {
6266 c = ptr[len];
6267 ptr[len] = NUL;
6268 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6269 ptr[len] = c;
6270 }
6271
6272 /* Repeat finding the file "count" times. This matters when it
6273 * appears several times in the path. */
6274 while (file_name != NULL && --count > 0)
6275 {
6276 vim_free(file_name);
6277 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6278 }
6279 }
6280 else
6281 file_name = vim_strnsave(ptr, len);
6282
6283# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6284 vim_free(tofree);
6285# endif
6286
6287 return file_name;
6288}
6289#endif /* FEAT_SEARCHPATH */
6290
6291/*
6292 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6293 * Also check for ":\\", which MS Internet Explorer accepts, return
6294 * URL_BACKSLASH.
6295 */
6296 static int
6297path_is_url(p)
6298 char_u *p;
6299{
6300 if (STRNCMP(p, "://", (size_t)3) == 0)
6301 return URL_SLASH;
6302 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6303 return URL_BACKSLASH;
6304 return 0;
6305}
6306
6307/*
6308 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6309 * Return URL_BACKSLASH for "name:\\".
6310 * Return zero otherwise.
6311 */
6312 int
6313path_with_url(fname)
6314 char_u *fname;
6315{
6316 char_u *p;
6317
6318 for (p = fname; isalpha(*p); ++p)
6319 ;
6320 return path_is_url(p);
6321}
6322
6323/*
6324 * Return TRUE if "name" is a full (absolute) path name or URL.
6325 */
6326 int
6327vim_isAbsName(name)
6328 char_u *name;
6329{
6330 return (path_with_url(name) != 0 || mch_isFullName(name));
6331}
6332
6333/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006334 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 *
6336 * return FAIL for failure, OK otherwise
6337 */
6338 int
6339vim_FullName(fname, buf, len, force)
6340 char_u *fname, *buf;
6341 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006342 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 int retval = OK;
6345 int url;
6346
6347 *buf = NUL;
6348 if (fname == NULL)
6349 return FAIL;
6350
6351 url = path_with_url(fname);
6352 if (!url)
6353 retval = mch_FullName(fname, buf, len, force);
6354 if (url || retval == FAIL)
6355 {
6356 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006357 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 }
6359#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6360 slash_adjust(buf);
6361#endif
6362 return retval;
6363}
6364
6365/*
6366 * Return the minimal number of rows that is needed on the screen to display
6367 * the current number of windows.
6368 */
6369 int
6370min_rows()
6371{
6372 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006373#ifdef FEAT_WINDOWS
6374 tabpage_T *tp;
6375 int n;
6376#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
6378 if (firstwin == NULL) /* not initialized yet */
6379 return MIN_LINES;
6380
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006382 total = 0;
6383 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6384 {
6385 n = frame_minheight(tp->tp_topframe, NULL);
6386 if (total < n)
6387 total = n;
6388 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006389 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006391 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006393 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 return total;
6395}
6396
6397/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006398 * Return TRUE if there is only one window (in the current tab page), not
6399 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006400 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 */
6402 int
6403only_one_window()
6404{
6405#ifdef FEAT_WINDOWS
6406 int count = 0;
6407 win_T *wp;
6408
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006409 /* If there is another tab page there always is another window. */
6410 if (first_tabpage->tp_next != NULL)
6411 return FALSE;
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006414 if (wp->w_buffer != NULL
6415 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416# ifdef FEAT_QUICKFIX
6417 || wp->w_p_pvw
6418# endif
6419 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006420# ifdef FEAT_AUTOCMD
6421 && wp != aucmd_win
6422# endif
6423 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 ++count;
6425 return (count <= 1);
6426#else
6427 return TRUE;
6428#endif
6429}
6430
6431#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6432/*
6433 * Correct the cursor line number in other windows. Used after changing the
6434 * current buffer, and before applying autocommands.
6435 * When "do_curwin" is TRUE, also check current window.
6436 */
6437 void
6438check_lnums(do_curwin)
6439 int do_curwin;
6440{
6441 win_T *wp;
6442
6443#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006444 tabpage_T *tp;
6445
6446 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6448#else
6449 wp = curwin;
6450 if (do_curwin)
6451#endif
6452 {
6453 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6454 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6455 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6456 wp->w_topline = curbuf->b_ml.ml_line_count;
6457 }
6458}
6459#endif
6460
6461#if defined(FEAT_WINDOWS) || defined(PROTO)
6462
6463/*
6464 * A snapshot of the window sizes, to restore them after closing the help
6465 * window.
6466 * Only these fields are used:
6467 * fr_layout
6468 * fr_width
6469 * fr_height
6470 * fr_next
6471 * fr_child
6472 * fr_win (only valid for the old curwin, NULL otherwise)
6473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474
6475/*
6476 * Create a snapshot of the current frame sizes.
6477 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006478 void
6479make_snapshot(idx)
6480 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006482 clear_snapshot(curtab, idx);
6483 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486 static void
6487make_snapshot_rec(fr, frp)
6488 frame_T *fr;
6489 frame_T **frp;
6490{
6491 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6492 if (*frp == NULL)
6493 return;
6494 (*frp)->fr_layout = fr->fr_layout;
6495# ifdef FEAT_VERTSPLIT
6496 (*frp)->fr_width = fr->fr_width;
6497# endif
6498 (*frp)->fr_height = fr->fr_height;
6499 if (fr->fr_next != NULL)
6500 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6501 if (fr->fr_child != NULL)
6502 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6503 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6504 (*frp)->fr_win = curwin;
6505}
6506
6507/*
6508 * Remove any existing snapshot.
6509 */
6510 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006511clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006512 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006513 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006515 clear_snapshot_rec(tp->tp_snapshot[idx]);
6516 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517}
6518
6519 static void
6520clear_snapshot_rec(fr)
6521 frame_T *fr;
6522{
6523 if (fr != NULL)
6524 {
6525 clear_snapshot_rec(fr->fr_next);
6526 clear_snapshot_rec(fr->fr_child);
6527 vim_free(fr);
6528 }
6529}
6530
6531/*
6532 * Restore a previously created snapshot, if there is any.
6533 * This is only done if the screen size didn't change and the window layout is
6534 * still the same.
6535 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006536 void
6537restore_snapshot(idx, close_curwin)
6538 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 int close_curwin; /* closing current window */
6540{
6541 win_T *wp;
6542
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006543 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006545 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006547 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6548 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006550 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 win_comp_pos();
6552 if (wp != NULL && close_curwin)
6553 win_goto(wp);
6554 redraw_all_later(CLEAR);
6555 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006556 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557}
6558
6559/*
6560 * Check if frames "sn" and "fr" have the same layout, same following frames
6561 * and same children.
6562 */
6563 static int
6564check_snapshot_rec(sn, fr)
6565 frame_T *sn;
6566 frame_T *fr;
6567{
6568 if (sn->fr_layout != fr->fr_layout
6569 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6570 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6571 || (sn->fr_next != NULL
6572 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6573 || (sn->fr_child != NULL
6574 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6575 return FAIL;
6576 return OK;
6577}
6578
6579/*
6580 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6581 * following frames and children.
6582 * Returns a pointer to the old current window, or NULL.
6583 */
6584 static win_T *
6585restore_snapshot_rec(sn, fr)
6586 frame_T *sn;
6587 frame_T *fr;
6588{
6589 win_T *wp = NULL;
6590 win_T *wp2;
6591
6592 fr->fr_height = sn->fr_height;
6593# ifdef FEAT_VERTSPLIT
6594 fr->fr_width = sn->fr_width;
6595# endif
6596 if (fr->fr_layout == FR_LEAF)
6597 {
6598 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6599# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006600 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601# endif
6602 wp = sn->fr_win;
6603 }
6604 if (sn->fr_next != NULL)
6605 {
6606 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6607 if (wp2 != NULL)
6608 wp = wp2;
6609 }
6610 if (sn->fr_child != NULL)
6611 {
6612 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6613 if (wp2 != NULL)
6614 wp = wp2;
6615 }
6616 return wp;
6617}
6618
6619#endif
6620
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006621#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6622 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006623/*
6624 * Set "win" to be the curwin and "tp" to be the current tab page.
6625 * restore_win() MUST be called to undo.
6626 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006627 * When "no_display" is TRUE the display won't be affected, no redraw is
6628 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006629 * Returns FAIL if switching to "win" failed.
6630 */
6631 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006632switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006633 win_T **save_curwin UNUSED;
6634 tabpage_T **save_curtab UNUSED;
6635 win_T *win UNUSED;
6636 tabpage_T *tp UNUSED;
6637 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006638{
6639# ifdef FEAT_AUTOCMD
6640 block_autocmds();
6641# endif
6642# ifdef FEAT_WINDOWS
6643 *save_curwin = curwin;
6644 if (tp != NULL)
6645 {
6646 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006647 if (no_display)
6648 {
6649 curtab->tp_firstwin = firstwin;
6650 curtab->tp_lastwin = lastwin;
6651 curtab = tp;
6652 firstwin = curtab->tp_firstwin;
6653 lastwin = curtab->tp_lastwin;
6654 }
6655 else
6656 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006657 }
6658 if (!win_valid(win))
6659 {
6660# ifdef FEAT_AUTOCMD
6661 unblock_autocmds();
6662# endif
6663 return FAIL;
6664 }
6665 curwin = win;
6666 curbuf = curwin->w_buffer;
6667# endif
6668 return OK;
6669}
6670
6671/*
6672 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006673 * When "no_display" is TRUE the display won't be affected, no redraw is
6674 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006675 */
6676 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006677restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006678 win_T *save_curwin UNUSED;
6679 tabpage_T *save_curtab UNUSED;
6680 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006681{
6682# ifdef FEAT_WINDOWS
6683 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006684 {
6685 if (no_display)
6686 {
6687 curtab->tp_firstwin = firstwin;
6688 curtab->tp_lastwin = lastwin;
6689 curtab = save_curtab;
6690 firstwin = curtab->tp_firstwin;
6691 lastwin = curtab->tp_lastwin;
6692 }
6693 else
6694 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6695 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006696 if (win_valid(save_curwin))
6697 {
6698 curwin = save_curwin;
6699 curbuf = curwin->w_buffer;
6700 }
6701# endif
6702# ifdef FEAT_AUTOCMD
6703 unblock_autocmds();
6704# endif
6705}
6706
6707/*
6708 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6709 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6710 */
6711 void
6712switch_buffer(save_curbuf, buf)
6713 buf_T *buf;
6714 buf_T **save_curbuf;
6715{
6716# ifdef FEAT_AUTOCMD
6717 block_autocmds();
6718# endif
6719 *save_curbuf = curbuf;
6720 --curbuf->b_nwindows;
6721 curbuf = buf;
6722 curwin->w_buffer = buf;
6723 ++curbuf->b_nwindows;
6724}
6725
6726/*
6727 * Restore the current buffer after using switch_buffer().
6728 */
6729 void
6730restore_buffer(save_curbuf)
6731 buf_T *save_curbuf;
6732{
6733# ifdef FEAT_AUTOCMD
6734 unblock_autocmds();
6735# endif
6736 /* Check for valid buffer, just in case. */
6737 if (buf_valid(save_curbuf))
6738 {
6739 --curbuf->b_nwindows;
6740 curwin->w_buffer = save_curbuf;
6741 curbuf = save_curbuf;
6742 ++curbuf->b_nwindows;
6743 }
6744}
6745#endif
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6748/*
6749 * Return TRUE if there is any vertically split window.
6750 */
6751 int
6752win_hasvertsplit()
6753{
6754 frame_T *fr;
6755
6756 if (topframe->fr_layout == FR_ROW)
6757 return TRUE;
6758
6759 if (topframe->fr_layout == FR_COL)
6760 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6761 if (fr->fr_layout == FR_ROW)
6762 return TRUE;
6763
6764 return FALSE;
6765}
6766#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006767
6768#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6769/*
6770 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006771 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006772 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6773 * If no particular ID is desired, -1 must be specified for 'id'.
6774 * Return ID of added match, -1 on failure.
6775 */
6776 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006777match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006778 win_T *wp;
6779 char_u *grp;
6780 char_u *pat;
6781 int prio;
6782 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006783 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006784{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006785 matchitem_T *cur;
6786 matchitem_T *prev;
6787 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006788 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006789 regprog_T *regprog = NULL;
6790 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006791
Bram Moolenaarb3414592014-06-17 17:48:32 +02006792 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006793 return -1;
6794 if (id < -1 || id == 0)
6795 {
6796 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6797 return -1;
6798 }
6799 if (id != -1)
6800 {
6801 cur = wp->w_match_head;
6802 while (cur != NULL)
6803 {
6804 if (cur->id == id)
6805 {
6806 EMSGN("E801: ID already taken: %ld", id);
6807 return -1;
6808 }
6809 cur = cur->next;
6810 }
6811 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006812 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006813 {
6814 EMSG2(_(e_nogroup), grp);
6815 return -1;
6816 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006817 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006818 {
6819 EMSG2(_(e_invarg2), pat);
6820 return -1;
6821 }
6822
6823 /* Find available match ID. */
6824 while (id == -1)
6825 {
6826 cur = wp->w_match_head;
6827 while (cur != NULL && cur->id != wp->w_next_match_id)
6828 cur = cur->next;
6829 if (cur == NULL)
6830 id = wp->w_next_match_id;
6831 wp->w_next_match_id++;
6832 }
6833
6834 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006835 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006836 m->id = id;
6837 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006838 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006839 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006840 m->match.regprog = regprog;
6841 m->match.rmm_ic = FALSE;
6842 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006843
Bram Moolenaarb3414592014-06-17 17:48:32 +02006844 /* Set up position matches */
6845 if (pos_list != NULL)
6846 {
6847 linenr_T toplnum = 0;
6848 linenr_T botlnum = 0;
6849 listitem_T *li;
6850 int i;
6851
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006852 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006853 i++, li = li->li_next)
6854 {
6855 linenr_T lnum = 0;
6856 colnr_T col = 0;
6857 int len = 1;
6858 list_T *subl;
6859 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006860 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006861
Bram Moolenaarb3414592014-06-17 17:48:32 +02006862 if (li->li_tv.v_type == VAR_LIST)
6863 {
6864 subl = li->li_tv.vval.v_list;
6865 if (subl == NULL)
6866 goto fail;
6867 subli = subl->lv_first;
6868 if (subli == NULL)
6869 goto fail;
6870 lnum = get_tv_number_chk(&subli->li_tv, &error);
6871 if (error == TRUE)
6872 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006873 if (lnum == 0)
6874 {
6875 --i;
6876 continue;
6877 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006878 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006879 subli = subli->li_next;
6880 if (subli != NULL)
6881 {
6882 col = get_tv_number_chk(&subli->li_tv, &error);
6883 if (error == TRUE)
6884 goto fail;
6885 subli = subli->li_next;
6886 if (subli != NULL)
6887 {
6888 len = get_tv_number_chk(&subli->li_tv, &error);
6889 if (error == TRUE)
6890 goto fail;
6891 }
6892 }
6893 m->pos.pos[i].col = col;
6894 m->pos.pos[i].len = len;
6895 }
6896 else if (li->li_tv.v_type == VAR_NUMBER)
6897 {
6898 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006899 {
6900 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006901 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006902 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006903 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6904 m->pos.pos[i].col = 0;
6905 m->pos.pos[i].len = 0;
6906 }
6907 else
6908 {
6909 EMSG(_("List or number required"));
6910 goto fail;
6911 }
6912 if (toplnum == 0 || lnum < toplnum)
6913 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006914 if (botlnum == 0 || lnum >= botlnum)
6915 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006916 }
6917
6918 /* Calculate top and bottom lines for redrawing area */
6919 if (toplnum != 0)
6920 {
6921 if (wp->w_buffer->b_mod_set)
6922 {
6923 if (wp->w_buffer->b_mod_top > toplnum)
6924 wp->w_buffer->b_mod_top = toplnum;
6925 if (wp->w_buffer->b_mod_bot < botlnum)
6926 wp->w_buffer->b_mod_bot = botlnum;
6927 }
6928 else
6929 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006930 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006931 wp->w_buffer->b_mod_top = toplnum;
6932 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006933 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006934 }
6935 m->pos.toplnum = toplnum;
6936 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006937 rtype = VALID;
6938 }
6939 }
6940
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006941 /* Insert new match. The match list is in ascending order with regard to
6942 * the match priorities. */
6943 cur = wp->w_match_head;
6944 prev = cur;
6945 while (cur != NULL && prio >= cur->priority)
6946 {
6947 prev = cur;
6948 cur = cur->next;
6949 }
6950 if (cur == prev)
6951 wp->w_match_head = m;
6952 else
6953 prev->next = m;
6954 m->next = cur;
6955
Bram Moolenaarb3414592014-06-17 17:48:32 +02006956 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006957 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006958
6959fail:
6960 vim_free(m);
6961 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006962}
6963
6964/*
6965 * Delete match with ID 'id' in the match list of window 'wp'.
6966 * Print error messages if 'perr' is TRUE.
6967 */
6968 int
6969match_delete(wp, id, perr)
6970 win_T *wp;
6971 int id;
6972 int perr;
6973{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006974 matchitem_T *cur = wp->w_match_head;
6975 matchitem_T *prev = cur;
6976 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006977
6978 if (id < 1)
6979 {
6980 if (perr == TRUE)
6981 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6982 id);
6983 return -1;
6984 }
6985 while (cur != NULL && cur->id != id)
6986 {
6987 prev = cur;
6988 cur = cur->next;
6989 }
6990 if (cur == NULL)
6991 {
6992 if (perr == TRUE)
6993 EMSGN("E803: ID not found: %ld", id);
6994 return -1;
6995 }
6996 if (cur == prev)
6997 wp->w_match_head = cur->next;
6998 else
6999 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007000 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007001 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007002 if (cur->pos.toplnum != 0)
7003 {
7004 if (wp->w_buffer->b_mod_set)
7005 {
7006 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7007 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7008 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7009 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7010 }
7011 else
7012 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007013 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007014 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7015 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007016 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007017 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007018 rtype = VALID;
7019 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007020 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007021 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007022 return 0;
7023}
7024
7025/*
7026 * Delete all matches in the match list of window 'wp'.
7027 */
7028 void
7029clear_matches(wp)
7030 win_T *wp;
7031{
7032 matchitem_T *m;
7033
7034 while (wp->w_match_head != NULL)
7035 {
7036 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007037 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007038 vim_free(wp->w_match_head->pattern);
7039 vim_free(wp->w_match_head);
7040 wp->w_match_head = m;
7041 }
7042 redraw_later(SOME_VALID);
7043}
7044
7045/*
7046 * Get match from ID 'id' in window 'wp'.
7047 * Return NULL if match not found.
7048 */
7049 matchitem_T *
7050get_match(wp, id)
7051 win_T *wp;
7052 int id;
7053{
7054 matchitem_T *cur = wp->w_match_head;
7055
7056 while (cur != NULL && cur->id != id)
7057 cur = cur->next;
7058 return cur;
7059}
7060#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007061
7062#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7063 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007064get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007065{
7066 int i = 1;
7067 win_T *w;
7068
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007069 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007070 ++i;
7071
7072 if (w == NULL)
7073 return 0;
7074 else
7075 return i;
7076}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007077
7078 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007079get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007080{
7081 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007082# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007083 tabpage_T *t;
7084
7085 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7086 ++i;
7087
7088 if (t == NULL)
7089 return 0;
7090 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007091# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007092 return i;
7093}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007094#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007095
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007096#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007097/*
7098 * Return TRUE if "topfrp" and its children are at the right height.
7099 */
7100 static int
7101frame_check_height(topfrp, height)
7102 frame_T *topfrp;
7103 int height;
7104{
7105 frame_T *frp;
7106
7107 if (topfrp->fr_height != height)
7108 return FALSE;
7109
7110 if (topfrp->fr_layout == FR_ROW)
7111 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7112 if (frp->fr_height != height)
7113 return FALSE;
7114
7115 return TRUE;
7116}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007117#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007118
7119#ifdef FEAT_VERTSPLIT
7120/*
7121 * Return TRUE if "topfrp" and its children are at the right width.
7122 */
7123 static int
7124frame_check_width(topfrp, width)
7125 frame_T *topfrp;
7126 int width;
7127{
7128 frame_T *frp;
7129
7130 if (topfrp->fr_width != width)
7131 return FALSE;
7132
7133 if (topfrp->fr_layout == FR_COL)
7134 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7135 if (frp->fr_width != width)
7136 return FALSE;
7137
7138 return TRUE;
7139}
7140#endif
7141