blob: 482692ffc912a3d9f51ef6ad7867020f9d0169b9 [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 Moolenaar071d4272004-06-13 20:20:40 +0000691
692 if (flags & WSP_TOP)
693 oldwin = firstwin;
694 else if (flags & WSP_BOT)
695 oldwin = lastwin;
696 else
697 oldwin = curwin;
698
699 /* add a status line when p_ls == 1 and splitting the first window */
700 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
701 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100702 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 EMSG(_(e_noroom));
705 return FAIL;
706 }
707 need_status = STATUS_HEIGHT;
708 }
709
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000710#ifdef FEAT_GUI
711 /* May be needed for the scrollbars that are going to change. */
712 if (gui.in_use)
713 out_flush();
714#endif
715
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#ifdef FEAT_VERTSPLIT
717 if (flags & WSP_VERT)
718 {
719 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
721 /*
722 * Check if we are able to split the current window and compute its
723 * width.
724 */
725 needed = p_wmw + 1;
726 if (flags & WSP_ROOM)
727 needed += p_wiw - p_wmw;
728 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
729 {
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200730 minwidth = frame_minwidth(topframe, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200732 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200735 {
736 minwidth = frame_minwidth(oldwin->w_frame, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 available = oldwin->w_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200738 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100739 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
741 EMSG(_(e_noroom));
742 return FAIL;
743 }
744 if (new_size == 0)
745 new_size = oldwin->w_width / 2;
746 if (new_size > oldwin->w_width - p_wmw - 1)
747 new_size = oldwin->w_width - p_wmw - 1;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200748 if (new_size > available - minwidth - 1)
749 new_size = available - minwidth - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (new_size < p_wmw)
751 new_size = p_wmw;
752
753 /* if it doesn't fit in the current window, need win_equal() */
754 if (oldwin->w_width - new_size - 1 < p_wmw)
755 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000756
757 /* We don't like to take lines for the new window from a
758 * 'winfixwidth' window. Take them from a window to the left or right
759 * instead, if possible. */
760 if (oldwin->w_p_wfw)
761 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000762
763 /* Only make all windows the same width if one of them (except oldwin)
764 * is wider than one of the split windows. */
765 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
766 && oldwin->w_frame->fr_parent != NULL)
767 {
768 frp = oldwin->w_frame->fr_parent->fr_child;
769 while (frp != NULL)
770 {
771 if (frp->fr_win != oldwin && frp->fr_win != NULL
772 && (frp->fr_win->w_width > new_size
773 || frp->fr_win->w_width > oldwin->w_width
774 - new_size - STATUS_HEIGHT))
775 {
776 do_equal = TRUE;
777 break;
778 }
779 frp = frp->fr_next;
780 }
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
783 else
784#endif
785 {
786 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788 /*
789 * Check if we are able to split the current window and compute its
790 * height.
791 */
792 needed = p_wmh + STATUS_HEIGHT + need_status;
793 if (flags & WSP_ROOM)
794 needed += p_wh - p_wmh;
795 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
796 {
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200797 minheight = frame_minheight(topframe, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200799 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801 else
802 {
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200803 minheight = frame_minheight(oldwin->w_frame, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 available = oldwin->w_height;
805 needed += p_wmh;
806 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100807 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 {
809 EMSG(_(e_noroom));
810 return FAIL;
811 }
812 oldwin_height = oldwin->w_height;
813 if (need_status)
814 {
815 oldwin->w_status_height = STATUS_HEIGHT;
816 oldwin_height -= STATUS_HEIGHT;
817 }
818 if (new_size == 0)
819 new_size = oldwin_height / 2;
820
821 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
822 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200823 if (new_size > available - minheight - STATUS_HEIGHT)
824 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (new_size < p_wmh)
826 new_size = p_wmh;
827
828 /* if it doesn't fit in the current window, need win_equal() */
829 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
830 do_equal = TRUE;
831
832 /* We don't like to take lines for the new window from a
833 * 'winfixheight' window. Take them from a window above or below
834 * instead, if possible. */
835 if (oldwin->w_p_wfh)
836 {
837 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
838 oldwin);
839 oldwin_height = oldwin->w_height;
840 if (need_status)
841 oldwin_height -= STATUS_HEIGHT;
842 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000843
844 /* Only make all windows the same height if one of them (except oldwin)
845 * is higher than one of the split windows. */
846 if (!do_equal && p_ea && size == 0
847#ifdef FEAT_VERTSPLIT
848 && *p_ead != 'h'
849#endif
850 && oldwin->w_frame->fr_parent != NULL)
851 {
852 frp = oldwin->w_frame->fr_parent->fr_child;
853 while (frp != NULL)
854 {
855 if (frp->fr_win != oldwin && frp->fr_win != NULL
856 && (frp->fr_win->w_height > new_size
857 || frp->fr_win->w_height > oldwin_height - new_size
858 - STATUS_HEIGHT))
859 {
860 do_equal = TRUE;
861 break;
862 }
863 frp = frp->fr_next;
864 }
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867
868 /*
869 * allocate new window structure and link it in the window list
870 */
871 if ((flags & WSP_TOP) == 0
872 && ((flags & WSP_BOT)
873 || (flags & WSP_BELOW)
874 || (!(flags & WSP_ABOVE)
875 && (
876#ifdef FEAT_VERTSPLIT
877 (flags & WSP_VERT) ? p_spr :
878#endif
879 p_sb))))
880 {
881 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100882 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000883 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 else
885 win_append(oldwin, wp);
886 }
887 else
888 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100889 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000890 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 else
892 win_append(oldwin->w_prev, wp);
893 }
894
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100895 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
897 if (wp == NULL)
898 return FAIL;
899
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000900 new_frame(wp);
901 if (wp->w_frame == NULL)
902 {
903 win_free(wp, NULL);
904 return FAIL;
905 }
906
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000907 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000908 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910
911 /*
912 * Reorganise the tree of frames to insert the new window.
913 */
914 if (flags & (WSP_TOP | WSP_BOT))
915 {
916#ifdef FEAT_VERTSPLIT
917 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
918 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
919#else
920 if (topframe->fr_layout == FR_COL)
921#endif
922 {
923 curfrp = topframe->fr_child;
924 if (flags & WSP_BOT)
925 while (curfrp->fr_next != NULL)
926 curfrp = curfrp->fr_next;
927 }
928 else
929 curfrp = topframe;
930 before = (flags & WSP_TOP);
931 }
932 else
933 {
934 curfrp = oldwin->w_frame;
935 if (flags & WSP_BELOW)
936 before = FALSE;
937 else if (flags & WSP_ABOVE)
938 before = TRUE;
939 else
940#ifdef FEAT_VERTSPLIT
941 if (flags & WSP_VERT)
942 before = !p_spr;
943 else
944#endif
945 before = !p_sb;
946 }
947 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
948 {
949 /* Need to create a new frame in the tree to make a branch. */
950 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
951 *frp = *curfrp;
952 curfrp->fr_layout = layout;
953 frp->fr_parent = curfrp;
954 frp->fr_next = NULL;
955 frp->fr_prev = NULL;
956 curfrp->fr_child = frp;
957 curfrp->fr_win = NULL;
958 curfrp = frp;
959 if (frp->fr_win != NULL)
960 oldwin->w_frame = frp;
961 else
962 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
963 frp->fr_parent = curfrp;
964 }
965
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100966 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000967 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100969 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 frp->fr_parent = curfrp->fr_parent;
971
972 /* Insert the new frame at the right place in the frame list. */
973 if (before)
974 frame_insert(curfrp, frp);
975 else
976 frame_append(curfrp, frp);
977
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100978 /* Set w_fraction now so that the cursor keeps the same relative
979 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100980 if (oldwin->w_height > 0)
981 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100982 wp->w_fraction = oldwin->w_fraction;
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984#ifdef FEAT_VERTSPLIT
985 if (flags & WSP_VERT)
986 {
987 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (need_status)
990 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100991 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 oldwin->w_status_height = need_status;
993 }
994 if (flags & (WSP_TOP | WSP_BOT))
995 {
996 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000997 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100998 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 wp->w_status_height = (p_ls > 0);
1000 }
1001 else
1002 {
1003 /* height and row of new window is same as current window */
1004 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001005 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 wp->w_status_height = oldwin->w_status_height;
1007 }
1008 frp->fr_height = curfrp->fr_height;
1009
1010 /* "new_size" of the current window goes to the new window, use
1011 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001012 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 if (before)
1014 wp->w_vsep_width = 1;
1015 else
1016 {
1017 wp->w_vsep_width = oldwin->w_vsep_width;
1018 oldwin->w_vsep_width = 1;
1019 }
1020 if (flags & (WSP_TOP | WSP_BOT))
1021 {
1022 if (flags & WSP_BOT)
1023 frame_add_vsep(curfrp);
1024 /* Set width of neighbor frame */
1025 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001026 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1027 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001030 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (before) /* new window left of current one */
1032 {
1033 wp->w_wincol = oldwin->w_wincol;
1034 oldwin->w_wincol += new_size + 1;
1035 }
1036 else /* new window right of current one */
1037 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1038 frame_fix_width(oldwin);
1039 frame_fix_width(wp);
1040 }
1041 else
1042#endif
1043 {
1044 /* width and column of new window is same as current window */
1045#ifdef FEAT_VERTSPLIT
1046 if (flags & (WSP_TOP | WSP_BOT))
1047 {
1048 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001049 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 wp->w_vsep_width = 0;
1051 }
1052 else
1053 {
1054 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001055 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 wp->w_vsep_width = oldwin->w_vsep_width;
1057 }
1058 frp->fr_width = curfrp->fr_width;
1059#endif
1060
1061 /* "new_size" of the current window goes to the new window, use
1062 * one row for the status line */
1063 win_new_height(wp, new_size);
1064 if (flags & (WSP_TOP | WSP_BOT))
1065 frame_new_height(curfrp, curfrp->fr_height
1066 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1067 else
1068 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1069 if (before) /* new window above current one */
1070 {
1071 wp->w_winrow = oldwin->w_winrow;
1072 wp->w_status_height = STATUS_HEIGHT;
1073 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1074 }
1075 else /* new window below current one */
1076 {
1077 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1078 wp->w_status_height = oldwin->w_status_height;
1079 oldwin->w_status_height = STATUS_HEIGHT;
1080 }
1081#ifdef FEAT_VERTSPLIT
1082 if (flags & WSP_BOT)
1083 frame_add_statusline(curfrp);
1084#endif
1085 frame_fix_height(wp);
1086 frame_fix_height(oldwin);
1087 }
1088
1089 if (flags & (WSP_TOP | WSP_BOT))
1090 (void)win_comp_pos();
1091
1092 /*
1093 * Both windows need redrawing
1094 */
1095 redraw_win_later(wp, NOT_VALID);
1096 wp->w_redr_status = TRUE;
1097 redraw_win_later(oldwin, NOT_VALID);
1098 oldwin->w_redr_status = TRUE;
1099
1100 if (need_status)
1101 {
1102 msg_row = Rows - 1;
1103 msg_col = sc_col;
1104 msg_clr_eos_force(); /* Old command/ruler may still be there */
1105 comp_col();
1106 msg_row = Rows - 1;
1107 msg_col = 0; /* put position back at start of line */
1108 }
1109
1110 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001111 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
1113 if (do_equal || dir != 0)
1114 win_equal(wp, TRUE,
1115#ifdef FEAT_VERTSPLIT
1116 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1117 : dir == 'h' ? 'b' :
1118#endif
1119 'v');
1120
1121 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1122 * size was given. */
1123#ifdef FEAT_VERTSPLIT
1124 if (flags & WSP_VERT)
1125 {
1126 i = p_wiw;
1127 if (size != 0)
1128 p_wiw = size;
1129
1130# ifdef FEAT_GUI
1131 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1132 if (gui.in_use)
1133 gui_init_which_components(NULL);
1134# endif
1135 }
1136 else
1137#endif
1138 {
1139 i = p_wh;
1140 if (size != 0)
1141 p_wh = size;
1142 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001143
1144 /*
1145 * make the new window the current window
1146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 win_enter(wp, FALSE);
1148#ifdef FEAT_VERTSPLIT
1149 if (flags & WSP_VERT)
1150 p_wiw = i;
1151 else
1152#endif
1153 p_wh = i;
1154
1155 return OK;
1156}
1157
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001158
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001159/*
1160 * Initialize window "newp" from window "oldp".
1161 * Used when splitting a window and when creating a new tab page.
1162 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001163 * WSP_NEWLOC may be specified in flags to prevent the location list from
1164 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001165 */
1166 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001167win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001168 win_T *newp;
1169 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001170 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001171{
1172 int i;
1173
1174 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001175#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001176 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001177#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001178 oldp->w_buffer->b_nwindows++;
1179 newp->w_cursor = oldp->w_cursor;
1180 newp->w_valid = 0;
1181 newp->w_curswant = oldp->w_curswant;
1182 newp->w_set_curswant = oldp->w_set_curswant;
1183 newp->w_topline = oldp->w_topline;
1184#ifdef FEAT_DIFF
1185 newp->w_topfill = oldp->w_topfill;
1186#endif
1187 newp->w_leftcol = oldp->w_leftcol;
1188 newp->w_pcmark = oldp->w_pcmark;
1189 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1190 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001191 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001192 newp->w_fraction = oldp->w_fraction;
1193 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1194#ifdef FEAT_JUMPLIST
1195 copy_jumplist(oldp, newp);
1196#endif
1197#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001198 if (flags & WSP_NEWLOC)
1199 {
1200 /* Don't copy the location list. */
1201 newp->w_llist = NULL;
1202 newp->w_llist_ref = NULL;
1203 }
1204 else
1205 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001206#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001207 newp->w_localdir = (oldp->w_localdir == NULL)
1208 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001209
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001210 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001211 for (i = 0; i < oldp->w_tagstacklen; i++)
1212 {
1213 newp->w_tagstack[i] = oldp->w_tagstack[i];
1214 if (newp->w_tagstack[i].tagname != NULL)
1215 newp->w_tagstack[i].tagname =
1216 vim_strsave(newp->w_tagstack[i].tagname);
1217 }
1218 newp->w_tagstackidx = oldp->w_tagstackidx;
1219 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001220#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001221 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001222#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001223
1224 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001225
Bram Moolenaara971b822011-09-14 14:43:25 +02001226#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001227 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001228#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001229}
1230
1231/*
1232 * Initialize window "newp" from window"old".
1233 * Only the essential things are copied.
1234 */
1235 static void
1236win_init_some(newp, oldp)
1237 win_T *newp;
1238 win_T *oldp;
1239{
1240 /* Use the same argument list. */
1241 newp->w_alist = oldp->w_alist;
1242 ++newp->w_alist->al_refcount;
1243 newp->w_arg_idx = oldp->w_arg_idx;
1244
1245 /* copy options from existing window */
1246 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001247}
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249#endif /* FEAT_WINDOWS */
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#if defined(FEAT_WINDOWS) || defined(PROTO)
1252/*
1253 * Check if "win" is a pointer to an existing window.
1254 */
1255 int
1256win_valid(win)
1257 win_T *win;
1258{
1259 win_T *wp;
1260
1261 if (win == NULL)
1262 return FALSE;
1263 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1264 if (wp == win)
1265 return TRUE;
1266 return FALSE;
1267}
1268
1269/*
1270 * Return the number of windows.
1271 */
1272 int
1273win_count()
1274{
1275 win_T *wp;
1276 int count = 0;
1277
1278 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1279 ++count;
1280 return count;
1281}
1282
1283/*
1284 * Make "count" windows on the screen.
1285 * Return actual number of windows on the screen.
1286 * Must be called when there is just one window, filling the whole screen
1287 * (excluding the command line).
1288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 int
1290make_windows(count, vertical)
1291 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001292 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293{
1294 int maxcount;
1295 int todo;
1296
1297#ifdef FEAT_VERTSPLIT
1298 if (vertical)
1299 {
1300 /* Each windows needs at least 'winminwidth' lines and a separator
1301 * column. */
1302 maxcount = (curwin->w_width + curwin->w_vsep_width
1303 - (p_wiw - p_wmw)) / (p_wmw + 1);
1304 }
1305 else
1306#endif
1307 {
1308 /* Each window needs at least 'winminheight' lines and a status line. */
1309 maxcount = (curwin->w_height + curwin->w_status_height
1310 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1311 }
1312
1313 if (maxcount < 2)
1314 maxcount = 2;
1315 if (count > maxcount)
1316 count = maxcount;
1317
1318 /*
1319 * add status line now, otherwise first window will be too big
1320 */
1321 if (count > 1)
1322 last_status(TRUE);
1323
1324#ifdef FEAT_AUTOCMD
1325 /*
1326 * Don't execute autocommands while creating the windows. Must do that
1327 * when putting the buffers in the windows.
1328 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001329 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#endif
1331
1332 /* todo is number of windows left to create */
1333 for (todo = count - 1; todo > 0; --todo)
1334#ifdef FEAT_VERTSPLIT
1335 if (vertical)
1336 {
1337 if (win_split(curwin->w_width - (curwin->w_width - todo)
1338 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1339 break;
1340 }
1341 else
1342#endif
1343 {
1344 if (win_split(curwin->w_height - (curwin->w_height - todo
1345 * STATUS_HEIGHT) / (todo + 1)
1346 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1347 break;
1348 }
1349
1350#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001351 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352#endif
1353
1354 /* return actual number of windows */
1355 return (count - todo);
1356}
1357
1358/*
1359 * Exchange current and next window
1360 */
1361 static void
1362win_exchange(Prenum)
1363 long Prenum;
1364{
1365 frame_T *frp;
1366 frame_T *frp2;
1367 win_T *wp;
1368 win_T *wp2;
1369 int temp;
1370
1371 if (lastwin == firstwin) /* just one window */
1372 {
1373 beep_flush();
1374 return;
1375 }
1376
1377#ifdef FEAT_GUI
1378 need_mouse_correct = TRUE;
1379#endif
1380
1381 /*
1382 * find window to exchange with
1383 */
1384 if (Prenum)
1385 {
1386 frp = curwin->w_frame->fr_parent->fr_child;
1387 while (frp != NULL && --Prenum > 0)
1388 frp = frp->fr_next;
1389 }
1390 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1391 frp = curwin->w_frame->fr_next;
1392 else /* Swap last window in row/col with previous */
1393 frp = curwin->w_frame->fr_prev;
1394
1395 /* We can only exchange a window with another window, not with a frame
1396 * containing windows. */
1397 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1398 return;
1399 wp = frp->fr_win;
1400
1401/*
1402 * 1. remove curwin from the list. Remember after which window it was in wp2
1403 * 2. insert curwin before wp in the list
1404 * if wp != wp2
1405 * 3. remove wp from the list
1406 * 4. insert wp after wp2
1407 * 5. exchange the status line height and vsep width.
1408 */
1409 wp2 = curwin->w_prev;
1410 frp2 = curwin->w_frame->fr_prev;
1411 if (wp->w_prev != curwin)
1412 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001413 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 frame_remove(curwin->w_frame);
1415 win_append(wp->w_prev, curwin);
1416 frame_insert(frp, curwin->w_frame);
1417 }
1418 if (wp != wp2)
1419 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 frame_remove(wp->w_frame);
1422 win_append(wp2, wp);
1423 if (frp2 == NULL)
1424 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1425 else
1426 frame_append(frp2, wp->w_frame);
1427 }
1428 temp = curwin->w_status_height;
1429 curwin->w_status_height = wp->w_status_height;
1430 wp->w_status_height = temp;
1431#ifdef FEAT_VERTSPLIT
1432 temp = curwin->w_vsep_width;
1433 curwin->w_vsep_width = wp->w_vsep_width;
1434 wp->w_vsep_width = temp;
1435
1436 /* If the windows are not in the same frame, exchange the sizes to avoid
1437 * messing up the window layout. Otherwise fix the frame sizes. */
1438 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1439 {
1440 temp = curwin->w_height;
1441 curwin->w_height = wp->w_height;
1442 wp->w_height = temp;
1443 temp = curwin->w_width;
1444 curwin->w_width = wp->w_width;
1445 wp->w_width = temp;
1446 }
1447 else
1448 {
1449 frame_fix_height(curwin);
1450 frame_fix_height(wp);
1451 frame_fix_width(curwin);
1452 frame_fix_width(wp);
1453 }
1454#endif
1455
1456 (void)win_comp_pos(); /* recompute window positions */
1457
1458 win_enter(wp, TRUE);
1459 redraw_later(CLEAR);
1460}
1461
1462/*
1463 * rotate windows: if upwards TRUE the second window becomes the first one
1464 * if upwards FALSE the first window becomes the second one
1465 */
1466 static void
1467win_rotate(upwards, count)
1468 int upwards;
1469 int count;
1470{
1471 win_T *wp1;
1472 win_T *wp2;
1473 frame_T *frp;
1474 int n;
1475
1476 if (firstwin == lastwin) /* nothing to do */
1477 {
1478 beep_flush();
1479 return;
1480 }
1481
1482#ifdef FEAT_GUI
1483 need_mouse_correct = TRUE;
1484#endif
1485
1486#ifdef FEAT_VERTSPLIT
1487 /* Check if all frames in this row/col have one window. */
1488 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1489 frp = frp->fr_next)
1490 if (frp->fr_win == NULL)
1491 {
1492 EMSG(_("E443: Cannot rotate when another window is split"));
1493 return;
1494 }
1495#endif
1496
1497 while (count--)
1498 {
1499 if (upwards) /* first window becomes last window */
1500 {
1501 /* remove first window/frame from the list */
1502 frp = curwin->w_frame->fr_parent->fr_child;
1503 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001504 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 frame_remove(frp);
1506
1507 /* find last frame and append removed window/frame after it */
1508 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1509 ;
1510 win_append(frp->fr_win, wp1);
1511 frame_append(frp, wp1->w_frame);
1512
1513 wp2 = frp->fr_win; /* previously last window */
1514 }
1515 else /* last window becomes first window */
1516 {
1517 /* find last window/frame in the list and remove it */
1518 for (frp = curwin->w_frame; frp->fr_next != NULL;
1519 frp = frp->fr_next)
1520 ;
1521 wp1 = frp->fr_win;
1522 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001523 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 frame_remove(frp);
1525
1526 /* append the removed window/frame before the first in the list */
1527 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1528 frame_insert(frp->fr_parent->fr_child, frp);
1529 }
1530
1531 /* exchange status height and vsep width of old and new last window */
1532 n = wp2->w_status_height;
1533 wp2->w_status_height = wp1->w_status_height;
1534 wp1->w_status_height = n;
1535 frame_fix_height(wp1);
1536 frame_fix_height(wp2);
1537#ifdef FEAT_VERTSPLIT
1538 n = wp2->w_vsep_width;
1539 wp2->w_vsep_width = wp1->w_vsep_width;
1540 wp1->w_vsep_width = n;
1541 frame_fix_width(wp1);
1542 frame_fix_width(wp2);
1543#endif
1544
1545 /* recompute w_winrow and w_wincol for all windows */
1546 (void)win_comp_pos();
1547 }
1548
1549 redraw_later(CLEAR);
1550}
1551
1552/*
1553 * Move the current window to the very top/bottom/left/right of the screen.
1554 */
1555 static void
1556win_totop(size, flags)
1557 int size;
1558 int flags;
1559{
1560 int dir;
1561 int height = curwin->w_height;
1562
1563 if (lastwin == firstwin)
1564 {
1565 beep_flush();
1566 return;
1567 }
1568
1569 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001570 (void)winframe_remove(curwin, &dir, NULL);
1571 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 last_status(FALSE); /* may need to remove last status line */
1573 (void)win_comp_pos(); /* recompute window positions */
1574
1575 /* Split a window on the desired side and put the window there. */
1576 (void)win_split_ins(size, flags, curwin, dir);
1577 if (!(flags & WSP_VERT))
1578 {
1579 win_setheight(height);
1580 if (p_ea)
1581 win_equal(curwin, TRUE, 'v');
1582 }
1583
1584#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1585 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1586 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001587 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589}
1590
1591/*
1592 * Move window "win1" to below/right of "win2" and make "win1" the current
1593 * window. Only works within the same frame!
1594 */
1595 void
1596win_move_after(win1, win2)
1597 win_T *win1, *win2;
1598{
1599 int height;
1600
1601 /* check if the arguments are reasonable */
1602 if (win1 == win2)
1603 return;
1604
1605 /* check if there is something to do */
1606 if (win2->w_next != win1)
1607 {
1608 /* may need move the status line/vertical separator of the last window
1609 * */
1610 if (win1 == lastwin)
1611 {
1612 height = win1->w_prev->w_status_height;
1613 win1->w_prev->w_status_height = win1->w_status_height;
1614 win1->w_status_height = height;
1615#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001616 if (win1->w_prev->w_vsep_width == 1)
1617 {
1618 /* Remove the vertical separator from the last-but-one window,
1619 * add it to the last window. Adjust the frame widths. */
1620 win1->w_prev->w_vsep_width = 0;
1621 win1->w_prev->w_frame->fr_width -= 1;
1622 win1->w_vsep_width = 1;
1623 win1->w_frame->fr_width += 1;
1624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625#endif
1626 }
1627 else if (win2 == lastwin)
1628 {
1629 height = win1->w_status_height;
1630 win1->w_status_height = win2->w_status_height;
1631 win2->w_status_height = height;
1632#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001633 if (win1->w_vsep_width == 1)
1634 {
1635 /* Remove the vertical separator from win1, add it to the last
1636 * window, win2. Adjust the frame widths. */
1637 win2->w_vsep_width = 1;
1638 win2->w_frame->fr_width += 1;
1639 win1->w_vsep_width = 0;
1640 win1->w_frame->fr_width -= 1;
1641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001644 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 frame_remove(win1->w_frame);
1646 win_append(win2, win1);
1647 frame_append(win2->w_frame, win1->w_frame);
1648
1649 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1650 redraw_later(NOT_VALID);
1651 }
1652 win_enter(win1, FALSE);
1653}
1654
1655/*
1656 * Make all windows the same height.
1657 * 'next_curwin' will soon be the current window, make sure it has enough
1658 * rows.
1659 */
1660 void
1661win_equal(next_curwin, current, dir)
1662 win_T *next_curwin; /* pointer to current window to be or NULL */
1663 int current; /* do only frame with current window */
1664 int dir; /* 'v' for vertically, 'h' for horizontally,
1665 'b' for both, 0 for using p_ead */
1666{
1667 if (dir == 0)
1668#ifdef FEAT_VERTSPLIT
1669 dir = *p_ead;
1670#else
1671 dir = 'b';
1672#endif
1673 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001674 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001675 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676}
1677
1678/*
1679 * Set a frame to a new position and height, spreading the available room
1680 * equally over contained frames.
1681 * The window "next_curwin" (if not NULL) should at least get the size from
1682 * 'winheight' and 'winwidth' if possible.
1683 */
1684 static void
1685win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1686 win_T *next_curwin; /* pointer to current window to be or NULL */
1687 int current; /* do only frame with current window */
1688 frame_T *topfr; /* frame to set size off */
1689 int dir; /* 'v', 'h' or 'b', see win_equal() */
1690 int col; /* horizontal position for frame */
1691 int row; /* vertical position for frame */
1692 int width; /* new width of frame */
1693 int height; /* new height of frame */
1694{
1695 int n, m;
1696 int extra_sep = 0;
1697 int wincount, totwincount = 0;
1698 frame_T *fr;
1699 int next_curwin_size = 0;
1700 int room = 0;
1701 int new_size;
1702 int has_next_curwin = 0;
1703 int hnc;
1704
1705 if (topfr->fr_layout == FR_LEAF)
1706 {
1707 /* Set the width/height of this frame.
1708 * Redraw when size or position changes */
1709 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1710#ifdef FEAT_VERTSPLIT
1711 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1712#endif
1713 )
1714 {
1715 topfr->fr_win->w_winrow = row;
1716 frame_new_height(topfr, height, FALSE, FALSE);
1717#ifdef FEAT_VERTSPLIT
1718 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001719 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721 redraw_all_later(CLEAR);
1722 }
1723 }
1724#ifdef FEAT_VERTSPLIT
1725 else if (topfr->fr_layout == FR_ROW)
1726 {
1727 topfr->fr_width = width;
1728 topfr->fr_height = height;
1729
1730 if (dir != 'v') /* equalize frame widths */
1731 {
1732 /* Compute the maximum number of windows horizontally in this
1733 * frame. */
1734 n = frame_minwidth(topfr, NOWIN);
1735 /* add one for the rightmost window, it doesn't have a separator */
1736 if (col + width == Columns)
1737 extra_sep = 1;
1738 else
1739 extra_sep = 0;
1740 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001741 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001743 /*
1744 * Compute width for "next_curwin" window and room available for
1745 * other windows.
1746 * "m" is the minimal width when counting p_wiw for "next_curwin".
1747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 m = frame_minwidth(topfr, next_curwin);
1749 room = width - m;
1750 if (room < 0)
1751 {
1752 next_curwin_size = p_wiw + room;
1753 room = 0;
1754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 else
1756 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001757 next_curwin_size = -1;
1758 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1759 {
1760 /* If 'winfixwidth' set keep the window width if
1761 * possible.
1762 * Watch out for this window being the next_curwin. */
1763 if (frame_fixed_width(fr))
1764 {
1765 n = frame_minwidth(fr, NOWIN);
1766 new_size = fr->fr_width;
1767 if (frame_has_win(fr, next_curwin))
1768 {
1769 room += p_wiw - p_wmw;
1770 next_curwin_size = 0;
1771 if (new_size < p_wiw)
1772 new_size = p_wiw;
1773 }
1774 else
1775 /* These windows don't use up room. */
1776 totwincount -= (n + (fr->fr_next == NULL
1777 ? extra_sep : 0)) / (p_wmw + 1);
1778 room -= new_size - n;
1779 if (room < 0)
1780 {
1781 new_size += room;
1782 room = 0;
1783 }
1784 fr->fr_newwidth = new_size;
1785 }
1786 }
1787 if (next_curwin_size == -1)
1788 {
1789 if (!has_next_curwin)
1790 next_curwin_size = 0;
1791 else if (totwincount > 1
1792 && (room + (totwincount - 2))
1793 / (totwincount - 1) > p_wiw)
1794 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001795 /* Can make all windows wider than 'winwidth', spread
1796 * the room equally. */
1797 next_curwin_size = (room + p_wiw
1798 + (totwincount - 1) * p_wmw
1799 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001800 room -= next_curwin_size - p_wiw;
1801 }
1802 else
1803 next_curwin_size = p_wiw;
1804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001806
1807 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 --totwincount; /* don't count curwin */
1809 }
1810
1811 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1812 {
1813 n = m = 0;
1814 wincount = 1;
1815 if (fr->fr_next == NULL)
1816 /* last frame gets all that remains (avoid roundoff error) */
1817 new_size = width;
1818 else if (dir == 'v')
1819 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001820 else if (frame_fixed_width(fr))
1821 {
1822 new_size = fr->fr_newwidth;
1823 wincount = 0; /* doesn't count as a sizeable window */
1824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 else
1826 {
1827 /* Compute the maximum number of windows horiz. in "fr". */
1828 n = frame_minwidth(fr, NOWIN);
1829 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1830 / (p_wmw + 1);
1831 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001832 if (has_next_curwin)
1833 hnc = frame_has_win(fr, next_curwin);
1834 else
1835 hnc = FALSE;
1836 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001838 if (totwincount == 0)
1839 new_size = room;
1840 else
1841 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001843 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 next_curwin_size -= p_wiw - (m - n);
1846 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001847 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001849 else
1850 room -= new_size;
1851 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 }
1853
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001854 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 * window, unless equalizing all frames. */
1856 if (!current || dir != 'v' || topfr->fr_parent != NULL
1857 || (new_size != fr->fr_width)
1858 || frame_has_win(fr, next_curwin))
1859 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001860 new_size, height);
1861 col += new_size;
1862 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 totwincount -= wincount;
1864 }
1865 }
1866#endif
1867 else /* topfr->fr_layout == FR_COL */
1868 {
1869#ifdef FEAT_VERTSPLIT
1870 topfr->fr_width = width;
1871#endif
1872 topfr->fr_height = height;
1873
1874 if (dir != 'h') /* equalize frame heights */
1875 {
1876 /* Compute maximum number of windows vertically in this frame. */
1877 n = frame_minheight(topfr, NOWIN);
1878 /* add one for the bottom window if it doesn't have a statusline */
1879 if (row + height == cmdline_row && p_ls == 0)
1880 extra_sep = 1;
1881 else
1882 extra_sep = 0;
1883 totwincount = (n + extra_sep) / (p_wmh + 1);
1884 has_next_curwin = frame_has_win(topfr, next_curwin);
1885
1886 /*
1887 * Compute height for "next_curwin" window and room available for
1888 * other windows.
1889 * "m" is the minimal height when counting p_wh for "next_curwin".
1890 */
1891 m = frame_minheight(topfr, next_curwin);
1892 room = height - m;
1893 if (room < 0)
1894 {
1895 /* The room is less then 'winheight', use all space for the
1896 * current window. */
1897 next_curwin_size = p_wh + room;
1898 room = 0;
1899 }
1900 else
1901 {
1902 next_curwin_size = -1;
1903 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1904 {
1905 /* If 'winfixheight' set keep the window height if
1906 * possible.
1907 * Watch out for this window being the next_curwin. */
1908 if (frame_fixed_height(fr))
1909 {
1910 n = frame_minheight(fr, NOWIN);
1911 new_size = fr->fr_height;
1912 if (frame_has_win(fr, next_curwin))
1913 {
1914 room += p_wh - p_wmh;
1915 next_curwin_size = 0;
1916 if (new_size < p_wh)
1917 new_size = p_wh;
1918 }
1919 else
1920 /* These windows don't use up room. */
1921 totwincount -= (n + (fr->fr_next == NULL
1922 ? extra_sep : 0)) / (p_wmh + 1);
1923 room -= new_size - n;
1924 if (room < 0)
1925 {
1926 new_size += room;
1927 room = 0;
1928 }
1929 fr->fr_newheight = new_size;
1930 }
1931 }
1932 if (next_curwin_size == -1)
1933 {
1934 if (!has_next_curwin)
1935 next_curwin_size = 0;
1936 else if (totwincount > 1
1937 && (room + (totwincount - 2))
1938 / (totwincount - 1) > p_wh)
1939 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001940 /* can make all windows higher than 'winheight',
1941 * spread the room equally. */
1942 next_curwin_size = (room + p_wh
1943 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 + (totwincount - 1)) / totwincount;
1945 room -= next_curwin_size - p_wh;
1946 }
1947 else
1948 next_curwin_size = p_wh;
1949 }
1950 }
1951
1952 if (has_next_curwin)
1953 --totwincount; /* don't count curwin */
1954 }
1955
1956 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1957 {
1958 n = m = 0;
1959 wincount = 1;
1960 if (fr->fr_next == NULL)
1961 /* last frame gets all that remains (avoid roundoff error) */
1962 new_size = height;
1963 else if (dir == 'h')
1964 new_size = fr->fr_height;
1965 else if (frame_fixed_height(fr))
1966 {
1967 new_size = fr->fr_newheight;
1968 wincount = 0; /* doesn't count as a sizeable window */
1969 }
1970 else
1971 {
1972 /* Compute the maximum number of windows vert. in "fr". */
1973 n = frame_minheight(fr, NOWIN);
1974 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1975 / (p_wmh + 1);
1976 m = frame_minheight(fr, next_curwin);
1977 if (has_next_curwin)
1978 hnc = frame_has_win(fr, next_curwin);
1979 else
1980 hnc = FALSE;
1981 if (hnc) /* don't count next_curwin */
1982 --wincount;
1983 if (totwincount == 0)
1984 new_size = room;
1985 else
1986 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1987 / totwincount;
1988 if (hnc) /* add next_curwin size */
1989 {
1990 next_curwin_size -= p_wh - (m - n);
1991 new_size += next_curwin_size;
1992 room -= new_size - next_curwin_size;
1993 }
1994 else
1995 room -= new_size;
1996 new_size += n;
1997 }
1998 /* Skip frame that is full width when splitting or closing a
1999 * window, unless equalizing all frames. */
2000 if (!current || dir != 'h' || topfr->fr_parent != NULL
2001 || (new_size != fr->fr_height)
2002 || frame_has_win(fr, next_curwin))
2003 win_equal_rec(next_curwin, current, fr, dir, col, row,
2004 width, new_size);
2005 row += new_size;
2006 height -= new_size;
2007 totwincount -= wincount;
2008 }
2009 }
2010}
2011
2012/*
2013 * close all windows for buffer 'buf'
2014 */
2015 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002016close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002018 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002020 win_T *wp;
2021 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002022 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
2024 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025
2026 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002028 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2029#ifdef FEAT_AUTOCMD
2030 && !(wp->w_closing || wp->w_buffer->b_closing)
2031#endif
2032 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002034 win_close(wp, FALSE);
2035
2036 /* Start all over, autocommands may change the window layout. */
2037 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002040 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002042
2043 /* Also check windows in other tab pages. */
2044 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2045 {
2046 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002047 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002048 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002049 if (wp->w_buffer == buf
2050#ifdef FEAT_AUTOCMD
2051 && !(wp->w_closing || wp->w_buffer->b_closing)
2052#endif
2053 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002054 {
2055 win_close_othertab(wp, FALSE, tp);
2056
2057 /* Start all over, the tab page may be closed and
2058 * autocommands may change the window layout. */
2059 nexttp = first_tabpage;
2060 break;
2061 }
2062 }
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002065
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002066 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002067 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002068 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069}
2070
2071/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002072 * Return TRUE if the current window is the only window that exists (ignoring
2073 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002074 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002075 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002076 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002077last_window()
2078{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002079 return (one_window() && first_tabpage->tp_next == NULL);
2080}
2081
2082/*
2083 * Return TRUE if there is only one window other than "aucmd_win" in the
2084 * current tab page.
2085 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002086 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002087one_window()
2088{
2089#ifdef FEAT_AUTOCMD
2090 win_T *wp;
2091 int seen_one = FALSE;
2092
2093 FOR_ALL_WINDOWS(wp)
2094 {
2095 if (wp != aucmd_win)
2096 {
2097 if (seen_one)
2098 return FALSE;
2099 seen_one = TRUE;
2100 }
2101 }
2102 return TRUE;
2103#else
2104 return firstwin == lastwin;
2105#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002106}
2107
2108/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002109 * Close the possibly last window in a tab page.
2110 * Returns TRUE when the window was closed already.
2111 */
2112 static int
2113close_last_window_tabpage(win, free_buf, prev_curtab)
2114 win_T *win;
2115 int free_buf;
2116 tabpage_T *prev_curtab;
2117{
2118 if (firstwin == lastwin)
2119 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002120#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002121 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002122#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002123
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002124 /*
2125 * Closing the last window in a tab page. First go to another tab
2126 * page and then close the window and the tab page. This avoids that
2127 * curwin and curtab are invalid while we are freeing memory, they may
2128 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002129 * Don't trigger autocommands yet, they may use wrong values, so do
2130 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002131 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002132 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002133 redraw_tabline = TRUE;
2134
2135 /* Safety check: Autocommands may have closed the window when jumping
2136 * to the other tab page. */
2137 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2138 {
2139 int h = tabline_height();
2140
2141 win_close_othertab(win, free_buf, prev_curtab);
2142 if (h != tabline_height())
2143 shell_new_rows();
2144 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002145 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2146 * that now. */
2147#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002148 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002149 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2150 if (old_curbuf != curbuf)
2151 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002152#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002153 return TRUE;
2154 }
2155 return FALSE;
2156}
2157
2158/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002159 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 * If "free_buf" is TRUE related buffer may be unloaded.
2161 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002162 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002163 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002165 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166win_close(win, free_buf)
2167 win_T *win;
2168 int free_buf;
2169{
2170 win_T *wp;
2171#ifdef FEAT_AUTOCMD
2172 int other_buffer = FALSE;
2173#endif
2174 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 int dir;
2176 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002177 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002179 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002182 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 }
2184
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002185#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002186 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002187 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002188 if (win == aucmd_win)
2189 {
2190 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002191 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002192 }
2193 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2194 {
2195 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002196 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002197 }
2198#endif
2199
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002200 /* When closing the last window in a tab page first go to another tab page
2201 * and then close the window and the tab page to avoid that curwin and
2202 * curtab are invalid while we are freeing memory. */
2203 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002204 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 /* When closing the help window, try restoring a snapshot after closing
2207 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002208 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 help_window = TRUE;
2210 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002211 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
2213#ifdef FEAT_AUTOCMD
2214 if (win == curwin)
2215 {
2216 /*
2217 * Guess which window is going to be the new current window.
2218 * This may change because of the autocommands (sigh).
2219 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002220 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002223 * Be careful: If autocommands delete the window or cause this window
2224 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 */
2226 if (wp->w_buffer != curbuf)
2227 {
2228 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002229 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002231 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002232 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002233 win->w_closing = FALSE;
2234 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002237 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002239 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002240 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002241 win->w_closing = FALSE;
2242 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244# ifdef FEAT_EVAL
2245 /* autocmds may abort script processing */
2246 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248# endif
2249 }
2250#endif
2251
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002252#ifdef FEAT_GUI
2253 /* Avoid trouble with scrollbars that are going to be deleted in
2254 * win_free(). */
2255 if (gui.in_use)
2256 out_flush();
2257#endif
2258
Bram Moolenaara971b822011-09-14 14:43:25 +02002259#ifdef FEAT_SYN_HL
2260 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002261 if (win->w_buffer != NULL)
2262 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002263#endif
2264
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 /*
2266 * Close the link to the buffer.
2267 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002268 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002269 {
2270#ifdef FEAT_AUTOCMD
2271 win->w_closing = TRUE;
2272#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002273 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002274#ifdef FEAT_AUTOCMD
2275 if (win_valid(win))
2276 win->w_closing = FALSE;
2277#endif
2278 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002279
Bram Moolenaar802418d2013-01-17 14:00:11 +01002280 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2281 && (last_window() || curtab != prev_curtab
2282 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002283 {
2284 /* Autocommands have close all windows, quit now. Restore
2285 * curwin->w_buffer, otherwise writing viminfo may fail. */
2286 if (curwin->w_buffer == NULL)
2287 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002288 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002289 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002290
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002292 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002293 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002294 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002295 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296
Bram Moolenaara971b822011-09-14 14:43:25 +02002297 /* Free the memory used for the window and get the window that received
2298 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002299 wp = win_free_mem(win, &dir, NULL);
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 /* Make sure curwin isn't invalid. It can cause severe trouble when
2302 * printing an error message. For win_equal() curbuf needs to be valid
2303 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002304 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 curwin = wp;
2307#ifdef FEAT_QUICKFIX
2308 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2309 {
2310 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002311 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 * finding another window to go to.
2313 */
2314 for (;;)
2315 {
2316 if (wp->w_next == NULL)
2317 wp = firstwin;
2318 else
2319 wp = wp->w_next;
2320 if (wp == curwin)
2321 break;
2322 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2323 {
2324 curwin = wp;
2325 break;
2326 }
2327 }
2328 }
2329#endif
2330 curbuf = curwin->w_buffer;
2331 close_curwin = TRUE;
2332 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002333 if (p_ea
2334#ifdef FEAT_VERTSPLIT
2335 && (*p_ead == 'b' || *p_ead == dir)
2336#endif
2337 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 win_equal(curwin, TRUE,
2339#ifdef FEAT_VERTSPLIT
2340 dir
2341#else
2342 0
2343#endif
2344 );
2345 else
2346 win_comp_pos();
2347 if (close_curwin)
2348 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002349 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350#ifdef FEAT_AUTOCMD
2351 if (other_buffer)
2352 /* careful: after this wp and win may be invalid! */
2353 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2354#endif
2355 }
2356
2357 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002358 * If last window has a status line now and we don't want one,
2359 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
2361 last_status(FALSE);
2362
2363 /* After closing the help window, try restoring the window layout from
2364 * before it was opened. */
2365 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002366 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2369 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2370 if (gui.in_use && !win_hasvertsplit())
2371 gui_init_which_components(NULL);
2372#endif
2373
2374 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002375 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376}
2377
2378/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002379 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002380 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002381 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002382 * Caller must check if buffer is hidden and whether the tabline needs to be
2383 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002384 */
2385 void
2386win_close_othertab(win, free_buf, tp)
2387 win_T *win;
2388 int free_buf;
2389 tabpage_T *tp;
2390{
2391 win_T *wp;
2392 int dir;
2393 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002394 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002395
Bram Moolenaar362ce482012-06-06 19:02:45 +02002396#ifdef FEAT_AUTOCMD
2397 if (win->w_closing || win->w_buffer->b_closing)
2398 return; /* window is already being closed */
2399#endif
2400
Bram Moolenaarf740b292006-02-16 22:11:02 +00002401 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002402 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002403
2404 /* Careful: Autocommands may have closed the tab page or made it the
2405 * current tab page. */
2406 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2407 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002408 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002409 return;
2410
2411 /* Autocommands may have closed the window already. */
2412 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2413 ;
2414 if (wp == NULL)
2415 return;
2416
Bram Moolenaarf740b292006-02-16 22:11:02 +00002417 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002418 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002419 {
2420 if (tp == first_tabpage)
2421 first_tabpage = tp->tp_next;
2422 else
2423 {
2424 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2425 ptp = ptp->tp_next)
2426 ;
2427 if (ptp == NULL)
2428 {
2429 EMSG2(_(e_intern2), "win_close_othertab()");
2430 return;
2431 }
2432 ptp->tp_next = tp->tp_next;
2433 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002434 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002435 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002436
2437 /* Free the memory used for the window. */
2438 win_free_mem(win, &dir, tp);
2439
2440 if (free_tp)
2441 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002442}
2443
2444/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002445 * Free the memory used for a window.
2446 * Returns a pointer to the window that got the freed up space.
2447 */
2448 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002449win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002450 win_T *win;
2451 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002452 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002453{
2454 frame_T *frp;
2455 win_T *wp;
2456
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002457 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002458 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002459 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002460 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002461 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002462
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002463 /* When deleting the current window of another tab page select a new
2464 * current window. */
2465 if (tp != NULL && win == tp->tp_curwin)
2466 tp->tp_curwin = wp;
2467
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002468 return wp;
2469}
2470
2471#if defined(EXITFREE) || defined(PROTO)
2472 void
2473win_free_all()
2474{
2475 int dummy;
2476
Bram Moolenaarf740b292006-02-16 22:11:02 +00002477# ifdef FEAT_WINDOWS
2478 while (first_tabpage->tp_next != NULL)
2479 tabpage_close(TRUE);
2480# endif
2481
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002482# ifdef FEAT_AUTOCMD
2483 if (aucmd_win != NULL)
2484 {
2485 (void)win_free_mem(aucmd_win, &dummy, NULL);
2486 aucmd_win = NULL;
2487 }
2488# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002489
2490 while (firstwin != NULL)
2491 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002492}
2493#endif
2494
2495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 * Remove a window and its frame from the tree of frames.
2497 * Returns a pointer to the window that got the freed up space.
2498 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002499 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002500winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002502 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002503 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 frame_T *frp, *frp2, *frp3;
2506 frame_T *frp_close = win->w_frame;
2507 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002510 * If there is only one window there is nothing to remove.
2511 */
2512 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2513 return NULL;
2514
2515 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 * Remove the window from its frame.
2517 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002518 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 wp = frame2win(frp2);
2520
2521 /* Remove this frame from the list of frames. */
2522 frame_remove(frp_close);
2523
2524#ifdef FEAT_VERTSPLIT
2525 if (frp_close->fr_parent->fr_layout == FR_COL)
2526 {
2527#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002528 /* When 'winfixheight' is set, try to find another frame in the column
2529 * (as close to the closed frame as possible) to distribute the height
2530 * to. */
2531 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2532 {
2533 frp = frp_close->fr_prev;
2534 frp3 = frp_close->fr_next;
2535 while (frp != NULL || frp3 != NULL)
2536 {
2537 if (frp != NULL)
2538 {
2539 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2540 {
2541 frp2 = frp;
2542 wp = frp->fr_win;
2543 break;
2544 }
2545 frp = frp->fr_prev;
2546 }
2547 if (frp3 != NULL)
2548 {
2549 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2550 {
2551 frp2 = frp3;
2552 wp = frp3->fr_win;
2553 break;
2554 }
2555 frp3 = frp3->fr_next;
2556 }
2557 }
2558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2560 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561#ifdef FEAT_VERTSPLIT
2562 *dirp = 'v';
2563 }
2564 else
2565 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002566 /* When 'winfixwidth' is set, try to find another frame in the column
2567 * (as close to the closed frame as possible) to distribute the width
2568 * to. */
2569 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2570 {
2571 frp = frp_close->fr_prev;
2572 frp3 = frp_close->fr_next;
2573 while (frp != NULL || frp3 != NULL)
2574 {
2575 if (frp != NULL)
2576 {
2577 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2578 {
2579 frp2 = frp;
2580 wp = frp->fr_win;
2581 break;
2582 }
2583 frp = frp->fr_prev;
2584 }
2585 if (frp3 != NULL)
2586 {
2587 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2588 {
2589 frp2 = frp3;
2590 wp = frp3->fr_win;
2591 break;
2592 }
2593 frp3 = frp3->fr_next;
2594 }
2595 }
2596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002598 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 *dirp = 'h';
2600 }
2601#endif
2602
2603 /* If rows/columns go to a window below/right its positions need to be
2604 * updated. Can only be done after the sizes have been updated. */
2605 if (frp2 == frp_close->fr_next)
2606 {
2607 int row = win->w_winrow;
2608 int col = W_WINCOL(win);
2609
2610 frame_comp_pos(frp2, &row, &col);
2611 }
2612
2613 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2614 {
2615 /* There is no other frame in this list, move its info to the parent
2616 * and remove it. */
2617 frp2->fr_parent->fr_layout = frp2->fr_layout;
2618 frp2->fr_parent->fr_child = frp2->fr_child;
2619 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2620 frp->fr_parent = frp2->fr_parent;
2621 frp2->fr_parent->fr_win = frp2->fr_win;
2622 if (frp2->fr_win != NULL)
2623 frp2->fr_win->w_frame = frp2->fr_parent;
2624 frp = frp2->fr_parent;
2625 vim_free(frp2);
2626
2627 frp2 = frp->fr_parent;
2628 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2629 {
2630 /* The frame above the parent has the same layout, have to merge
2631 * the frames into this list. */
2632 if (frp2->fr_child == frp)
2633 frp2->fr_child = frp->fr_child;
2634 frp->fr_child->fr_prev = frp->fr_prev;
2635 if (frp->fr_prev != NULL)
2636 frp->fr_prev->fr_next = frp->fr_child;
2637 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2638 {
2639 frp3->fr_parent = frp2;
2640 if (frp3->fr_next == NULL)
2641 {
2642 frp3->fr_next = frp->fr_next;
2643 if (frp->fr_next != NULL)
2644 frp->fr_next->fr_prev = frp3;
2645 break;
2646 }
2647 }
2648 vim_free(frp);
2649 }
2650 }
2651
2652 return wp;
2653}
2654
2655/*
2656 * Find out which frame is going to get the freed up space when "win" is
2657 * closed.
2658 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2659 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2660 * This makes opening a window and closing it immediately keep the same window
2661 * layout.
2662 */
2663 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002664win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002666 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668 frame_T *frp;
2669 int b;
2670
Bram Moolenaarf740b292006-02-16 22:11:02 +00002671 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002672 /* Last window in this tab page, will go to next tab page. */
2673 return alt_tabpage()->tp_curwin->w_frame;
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 frp = win->w_frame;
2676#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002677 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 b = p_spr;
2679 else
2680#endif
2681 b = p_sb;
2682 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2683 return frp->fr_next;
2684 return frp->fr_prev;
2685}
2686
2687/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002688 * Return the tabpage that will be used if the current one is closed.
2689 */
2690 static tabpage_T *
2691alt_tabpage()
2692{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002693 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002694
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002695 /* Use the next tab page if possible. */
2696 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002697 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002698
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002699 /* Find the last but one tab page. */
2700 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2701 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002702 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002703}
2704
2705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 * Find the left-upper window in frame "frp".
2707 */
2708 static win_T *
2709frame2win(frp)
2710 frame_T *frp;
2711{
2712 while (frp->fr_win == NULL)
2713 frp = frp->fr_child;
2714 return frp->fr_win;
2715}
2716
2717/*
2718 * Return TRUE if frame "frp" contains window "wp".
2719 */
2720 static int
2721frame_has_win(frp, wp)
2722 frame_T *frp;
2723 win_T *wp;
2724{
2725 frame_T *p;
2726
2727 if (frp->fr_layout == FR_LEAF)
2728 return frp->fr_win == wp;
2729
2730 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2731 if (frame_has_win(p, wp))
2732 return TRUE;
2733 return FALSE;
2734}
2735
2736/*
2737 * Set a new height for a frame. Recursively sets the height for contained
2738 * frames and windows. Caller must take care of positions.
2739 */
2740 static void
2741frame_new_height(topfrp, height, topfirst, wfh)
2742 frame_T *topfrp;
2743 int height;
2744 int topfirst; /* resize topmost contained frame first */
2745 int wfh; /* obey 'winfixheight' when there is a choice;
2746 may cause the height not to be set */
2747{
2748 frame_T *frp;
2749 int extra_lines;
2750 int h;
2751
2752 if (topfrp->fr_win != NULL)
2753 {
2754 /* Simple case: just one window. */
2755 win_new_height(topfrp->fr_win,
2756 height - topfrp->fr_win->w_status_height);
2757 }
2758#ifdef FEAT_VERTSPLIT
2759 else if (topfrp->fr_layout == FR_ROW)
2760 {
2761 do
2762 {
2763 /* All frames in this row get the same new height. */
2764 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2765 {
2766 frame_new_height(frp, height, topfirst, wfh);
2767 if (frp->fr_height > height)
2768 {
2769 /* Could not fit the windows, make the whole row higher. */
2770 height = frp->fr_height;
2771 break;
2772 }
2773 }
2774 }
2775 while (frp != NULL);
2776 }
2777#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002778 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
2780 /* Complicated case: Resize a column of frames. Resize the bottom
2781 * frame first, frames above that when needed. */
2782
2783 frp = topfrp->fr_child;
2784 if (wfh)
2785 /* Advance past frames with one window with 'wfh' set. */
2786 while (frame_fixed_height(frp))
2787 {
2788 frp = frp->fr_next;
2789 if (frp == NULL)
2790 return; /* no frame without 'wfh', give up */
2791 }
2792 if (!topfirst)
2793 {
2794 /* Find the bottom frame of this column */
2795 while (frp->fr_next != NULL)
2796 frp = frp->fr_next;
2797 if (wfh)
2798 /* Advance back for frames with one window with 'wfh' set. */
2799 while (frame_fixed_height(frp))
2800 frp = frp->fr_prev;
2801 }
2802
2803 extra_lines = height - topfrp->fr_height;
2804 if (extra_lines < 0)
2805 {
2806 /* reduce height of contained frames, bottom or top frame first */
2807 while (frp != NULL)
2808 {
2809 h = frame_minheight(frp, NULL);
2810 if (frp->fr_height + extra_lines < h)
2811 {
2812 extra_lines += frp->fr_height - h;
2813 frame_new_height(frp, h, topfirst, wfh);
2814 }
2815 else
2816 {
2817 frame_new_height(frp, frp->fr_height + extra_lines,
2818 topfirst, wfh);
2819 break;
2820 }
2821 if (topfirst)
2822 {
2823 do
2824 frp = frp->fr_next;
2825 while (wfh && frp != NULL && frame_fixed_height(frp));
2826 }
2827 else
2828 {
2829 do
2830 frp = frp->fr_prev;
2831 while (wfh && frp != NULL && frame_fixed_height(frp));
2832 }
2833 /* Increase "height" if we could not reduce enough frames. */
2834 if (frp == NULL)
2835 height -= extra_lines;
2836 }
2837 }
2838 else if (extra_lines > 0)
2839 {
2840 /* increase height of bottom or top frame */
2841 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2842 }
2843 }
2844 topfrp->fr_height = height;
2845}
2846
2847/*
2848 * Return TRUE if height of frame "frp" should not be changed because of
2849 * the 'winfixheight' option.
2850 */
2851 static int
2852frame_fixed_height(frp)
2853 frame_T *frp;
2854{
2855 /* frame with one window: fixed height if 'winfixheight' set. */
2856 if (frp->fr_win != NULL)
2857 return frp->fr_win->w_p_wfh;
2858
2859 if (frp->fr_layout == FR_ROW)
2860 {
2861 /* The frame is fixed height if one of the frames in the row is fixed
2862 * height. */
2863 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2864 if (frame_fixed_height(frp))
2865 return TRUE;
2866 return FALSE;
2867 }
2868
2869 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2870 * frames in the row are fixed height. */
2871 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2872 if (!frame_fixed_height(frp))
2873 return FALSE;
2874 return TRUE;
2875}
2876
2877#ifdef FEAT_VERTSPLIT
2878/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002879 * Return TRUE if width of frame "frp" should not be changed because of
2880 * the 'winfixwidth' option.
2881 */
2882 static int
2883frame_fixed_width(frp)
2884 frame_T *frp;
2885{
2886 /* frame with one window: fixed width if 'winfixwidth' set. */
2887 if (frp->fr_win != NULL)
2888 return frp->fr_win->w_p_wfw;
2889
2890 if (frp->fr_layout == FR_COL)
2891 {
2892 /* The frame is fixed width if one of the frames in the row is fixed
2893 * width. */
2894 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2895 if (frame_fixed_width(frp))
2896 return TRUE;
2897 return FALSE;
2898 }
2899
2900 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2901 * frames in the row are fixed width. */
2902 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2903 if (!frame_fixed_width(frp))
2904 return FALSE;
2905 return TRUE;
2906}
2907
2908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 * Add a status line to windows at the bottom of "frp".
2910 * Note: Does not check if there is room!
2911 */
2912 static void
2913frame_add_statusline(frp)
2914 frame_T *frp;
2915{
2916 win_T *wp;
2917
2918 if (frp->fr_layout == FR_LEAF)
2919 {
2920 wp = frp->fr_win;
2921 if (wp->w_status_height == 0)
2922 {
2923 if (wp->w_height > 0) /* don't make it negative */
2924 --wp->w_height;
2925 wp->w_status_height = STATUS_HEIGHT;
2926 }
2927 }
2928 else if (frp->fr_layout == FR_ROW)
2929 {
2930 /* Handle all the frames in the row. */
2931 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2932 frame_add_statusline(frp);
2933 }
2934 else /* frp->fr_layout == FR_COL */
2935 {
2936 /* Only need to handle the last frame in the column. */
2937 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2938 ;
2939 frame_add_statusline(frp);
2940 }
2941}
2942
2943/*
2944 * Set width of a frame. Handles recursively going through contained frames.
2945 * May remove separator line for windows at the right side (for win_close()).
2946 */
2947 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002948frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 frame_T *topfrp;
2950 int width;
2951 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002952 int wfw; /* obey 'winfixwidth' when there is a choice;
2953 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 frame_T *frp;
2956 int extra_cols;
2957 int w;
2958 win_T *wp;
2959
2960 if (topfrp->fr_layout == FR_LEAF)
2961 {
2962 /* Simple case: just one window. */
2963 wp = topfrp->fr_win;
2964 /* Find out if there are any windows right of this one. */
2965 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2966 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2967 break;
2968 if (frp->fr_parent == NULL)
2969 wp->w_vsep_width = 0;
2970 win_new_width(wp, width - wp->w_vsep_width);
2971 }
2972 else if (topfrp->fr_layout == FR_COL)
2973 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002974 do
2975 {
2976 /* All frames in this column get the same new width. */
2977 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2978 {
2979 frame_new_width(frp, width, leftfirst, wfw);
2980 if (frp->fr_width > width)
2981 {
2982 /* Could not fit the windows, make whole column wider. */
2983 width = frp->fr_width;
2984 break;
2985 }
2986 }
2987 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 }
2989 else /* fr_layout == FR_ROW */
2990 {
2991 /* Complicated case: Resize a row of frames. Resize the rightmost
2992 * frame first, frames left of it when needed. */
2993
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002995 if (wfw)
2996 /* Advance past frames with one window with 'wfw' set. */
2997 while (frame_fixed_width(frp))
2998 {
2999 frp = frp->fr_next;
3000 if (frp == NULL)
3001 return; /* no frame without 'wfw', give up */
3002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003004 {
3005 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 while (frp->fr_next != NULL)
3007 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003008 if (wfw)
3009 /* Advance back for frames with one window with 'wfw' set. */
3010 while (frame_fixed_width(frp))
3011 frp = frp->fr_prev;
3012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
3014 extra_cols = width - topfrp->fr_width;
3015 if (extra_cols < 0)
3016 {
3017 /* reduce frame width, rightmost frame first */
3018 while (frp != NULL)
3019 {
3020 w = frame_minwidth(frp, NULL);
3021 if (frp->fr_width + extra_cols < w)
3022 {
3023 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003024 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 }
3026 else
3027 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003028 frame_new_width(frp, frp->fr_width + extra_cols,
3029 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 break;
3031 }
3032 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003033 {
3034 do
3035 frp = frp->fr_next;
3036 while (wfw && frp != NULL && frame_fixed_width(frp));
3037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003039 {
3040 do
3041 frp = frp->fr_prev;
3042 while (wfw && frp != NULL && frame_fixed_width(frp));
3043 }
3044 /* Increase "width" if we could not reduce enough frames. */
3045 if (frp == NULL)
3046 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 }
3048 }
3049 else if (extra_cols > 0)
3050 {
3051 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003052 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054 }
3055 topfrp->fr_width = width;
3056}
3057
3058/*
3059 * Add the vertical separator to windows at the right side of "frp".
3060 * Note: Does not check if there is room!
3061 */
3062 static void
3063frame_add_vsep(frp)
3064 frame_T *frp;
3065{
3066 win_T *wp;
3067
3068 if (frp->fr_layout == FR_LEAF)
3069 {
3070 wp = frp->fr_win;
3071 if (wp->w_vsep_width == 0)
3072 {
3073 if (wp->w_width > 0) /* don't make it negative */
3074 --wp->w_width;
3075 wp->w_vsep_width = 1;
3076 }
3077 }
3078 else if (frp->fr_layout == FR_COL)
3079 {
3080 /* Handle all the frames in the column. */
3081 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3082 frame_add_vsep(frp);
3083 }
3084 else /* frp->fr_layout == FR_ROW */
3085 {
3086 /* Only need to handle the last frame in the row. */
3087 frp = frp->fr_child;
3088 while (frp->fr_next != NULL)
3089 frp = frp->fr_next;
3090 frame_add_vsep(frp);
3091 }
3092}
3093
3094/*
3095 * Set frame width from the window it contains.
3096 */
3097 static void
3098frame_fix_width(wp)
3099 win_T *wp;
3100{
3101 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3102}
3103#endif
3104
3105/*
3106 * Set frame height from the window it contains.
3107 */
3108 static void
3109frame_fix_height(wp)
3110 win_T *wp;
3111{
3112 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3113}
3114
3115/*
3116 * Compute the minimal height for frame "topfrp".
3117 * Uses the 'winminheight' option.
3118 * When "next_curwin" isn't NULL, use p_wh for this window.
3119 * When "next_curwin" is NOWIN, don't use at least one line for the current
3120 * window.
3121 */
3122 static int
3123frame_minheight(topfrp, next_curwin)
3124 frame_T *topfrp;
3125 win_T *next_curwin;
3126{
3127 frame_T *frp;
3128 int m;
3129#ifdef FEAT_VERTSPLIT
3130 int n;
3131#endif
3132
3133 if (topfrp->fr_win != NULL)
3134 {
3135 if (topfrp->fr_win == next_curwin)
3136 m = p_wh + topfrp->fr_win->w_status_height;
3137 else
3138 {
3139 /* window: minimal height of the window plus status line */
3140 m = p_wmh + topfrp->fr_win->w_status_height;
3141 /* Current window is minimal one line high */
3142 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3143 ++m;
3144 }
3145 }
3146#ifdef FEAT_VERTSPLIT
3147 else if (topfrp->fr_layout == FR_ROW)
3148 {
3149 /* get the minimal height from each frame in this row */
3150 m = 0;
3151 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3152 {
3153 n = frame_minheight(frp, next_curwin);
3154 if (n > m)
3155 m = n;
3156 }
3157 }
3158#endif
3159 else
3160 {
3161 /* Add up the minimal heights for all frames in this column. */
3162 m = 0;
3163 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3164 m += frame_minheight(frp, next_curwin);
3165 }
3166
3167 return m;
3168}
3169
3170#ifdef FEAT_VERTSPLIT
3171/*
3172 * Compute the minimal width for frame "topfrp".
3173 * When "next_curwin" isn't NULL, use p_wiw for this window.
3174 * When "next_curwin" is NOWIN, don't use at least one column for the current
3175 * window.
3176 */
3177 static int
3178frame_minwidth(topfrp, next_curwin)
3179 frame_T *topfrp;
3180 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3181{
3182 frame_T *frp;
3183 int m, n;
3184
3185 if (topfrp->fr_win != NULL)
3186 {
3187 if (topfrp->fr_win == next_curwin)
3188 m = p_wiw + topfrp->fr_win->w_vsep_width;
3189 else
3190 {
3191 /* window: minimal width of the window plus separator column */
3192 m = p_wmw + topfrp->fr_win->w_vsep_width;
3193 /* Current window is minimal one column wide */
3194 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3195 ++m;
3196 }
3197 }
3198 else if (topfrp->fr_layout == FR_COL)
3199 {
3200 /* get the minimal width from each frame in this column */
3201 m = 0;
3202 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3203 {
3204 n = frame_minwidth(frp, next_curwin);
3205 if (n > m)
3206 m = n;
3207 }
3208 }
3209 else
3210 {
3211 /* Add up the minimal widths for all frames in this row. */
3212 m = 0;
3213 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3214 m += frame_minwidth(frp, next_curwin);
3215 }
3216
3217 return m;
3218}
3219#endif
3220
3221
3222/*
3223 * Try to close all windows except current one.
3224 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3225 * used and the buffer was modified.
3226 *
3227 * Used by ":bdel" and ":only".
3228 */
3229 void
3230close_others(message, forceit)
3231 int message;
3232 int forceit; /* always hide all other windows */
3233{
3234 win_T *wp;
3235 win_T *nextwp;
3236 int r;
3237
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003238 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 {
3240 if (message
3241#ifdef FEAT_AUTOCMD
3242 && !autocmd_busy
3243#endif
3244 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003245 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 return;
3247 }
3248
3249 /* Be very careful here: autocommands may change the window layout. */
3250 for (wp = firstwin; win_valid(wp); wp = nextwp)
3251 {
3252 nextwp = wp->w_next;
3253 if (wp != curwin) /* don't close current window */
3254 {
3255
3256 /* Check if it's allowed to abandon this window */
3257 r = can_abandon(wp->w_buffer, forceit);
3258#ifdef FEAT_AUTOCMD
3259 if (!win_valid(wp)) /* autocommands messed wp up */
3260 {
3261 nextwp = firstwin;
3262 continue;
3263 }
3264#endif
3265 if (!r)
3266 {
3267#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3268 if (message && (p_confirm || cmdmod.confirm) && p_write)
3269 {
3270 dialog_changed(wp->w_buffer, FALSE);
3271# ifdef FEAT_AUTOCMD
3272 if (!win_valid(wp)) /* autocommands messed wp up */
3273 {
3274 nextwp = firstwin;
3275 continue;
3276 }
3277# endif
3278 }
3279 if (bufIsChanged(wp->w_buffer))
3280#endif
3281 continue;
3282 }
3283 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3284 }
3285 }
3286
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003287 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 EMSG(_("E445: Other window contains changes"));
3289}
3290
3291#endif /* FEAT_WINDOWS */
3292
3293/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003294 * Init the current window "curwin".
3295 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 */
3297 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003298curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003300 win_init_empty(curwin);
3301}
3302
3303 void
3304win_init_empty(wp)
3305 win_T *wp;
3306{
3307 redraw_win_later(wp, NOT_VALID);
3308 wp->w_lines_valid = 0;
3309 wp->w_cursor.lnum = 1;
3310 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003312 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003314 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3315 wp->w_pcmark.col = 0;
3316 wp->w_prev_pcmark.lnum = 0;
3317 wp->w_prev_pcmark.col = 0;
3318 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003320 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003322 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003324 if (wp->w_p_rl)
3325 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003327 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003329#ifdef FEAT_SYN_HL
3330 wp->w_s = &wp->w_buffer->b_s;
3331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332}
3333
3334/*
3335 * Allocate the first window and put an empty buffer in it.
3336 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003337 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003339 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340win_alloc_first()
3341{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003342 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003343 return FAIL;
3344
3345#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003346 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003347 if (first_tabpage == NULL)
3348 return FAIL;
3349 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003350 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003351#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003352
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003353 return OK;
3354}
3355
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003356#if defined(FEAT_AUTOCMD) || defined(PROTO)
3357/*
3358 * Init "aucmd_win". This can only be done after the first
3359 * window is fully initialized, thus it can't be in win_alloc_first().
3360 */
3361 void
3362win_alloc_aucmd_win()
3363{
3364 aucmd_win = win_alloc(NULL, TRUE);
3365 if (aucmd_win != NULL)
3366 {
3367 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003368 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003369 new_frame(aucmd_win);
3370 }
3371}
3372#endif
3373
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003374/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003375 * Allocate the first window or the first window in a new tab page.
3376 * When "oldwin" is NULL create an empty buffer for it.
3377 * When "oldwin" is not NULL copy info from it to the new window (only with
3378 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003379 * Return FAIL when something goes wrong (out of memory).
3380 */
3381 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003382win_alloc_firstwin(oldwin)
3383 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003384{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003385 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003386 if (oldwin == NULL)
3387 {
3388 /* Very first window, need to create an empty buffer for it and
3389 * initialize from scratch. */
3390 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3391 if (curwin == NULL || curbuf == NULL)
3392 return FAIL;
3393 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003394#ifdef FEAT_SYN_HL
3395 curwin->w_s = &(curbuf->b_s);
3396#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003397 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003399 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003401 curwin_init(); /* init current window */
3402 }
3403#ifdef FEAT_WINDOWS
3404 else
3405 {
3406 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003407 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003408
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003409 /* We don't want cursor- and scroll-binding in the first window. */
3410 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003411 }
3412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003414 new_frame(curwin);
3415 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003416 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003417 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418#ifdef FEAT_VERTSPLIT
3419 topframe->fr_width = Columns;
3420#endif
3421 topframe->fr_height = Rows - p_ch;
3422 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003423
3424 return OK;
3425}
3426
3427/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003428 * Create a frame for window "wp".
3429 */
3430 static void
3431new_frame(win_T *wp)
3432{
3433 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3434
3435 wp->w_frame = frp;
3436 if (frp != NULL)
3437 {
3438 frp->fr_layout = FR_LEAF;
3439 frp->fr_win = wp;
3440 }
3441}
3442
3443/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003444 * Initialize the window and frame size to the maximum.
3445 */
3446 void
3447win_init_size()
3448{
3449 firstwin->w_height = ROWS_AVAIL;
3450 topframe->fr_height = ROWS_AVAIL;
3451#ifdef FEAT_VERTSPLIT
3452 firstwin->w_width = Columns;
3453 topframe->fr_width = Columns;
3454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455}
3456
3457#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003458
3459/*
3460 * Allocate a new tabpage_T and init the values.
3461 * Returns NULL when out of memory.
3462 */
3463 static tabpage_T *
3464alloc_tabpage()
3465{
3466 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003467# ifdef FEAT_GUI
3468 int i;
3469# endif
3470
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003471
3472 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003473 if (tp == NULL)
3474 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003475
Bram Moolenaar429fa852013-04-15 12:27:36 +02003476# ifdef FEAT_EVAL
3477 /* init t: variables */
3478 tp->tp_vars = dict_alloc();
3479 if (tp->tp_vars == NULL)
3480 {
3481 vim_free(tp);
3482 return NULL;
3483 }
3484 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3485# endif
3486
3487# ifdef FEAT_GUI
3488 for (i = 0; i < 3; i++)
3489 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003490# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003491# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003492 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003493# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003494 tp->tp_ch_used = p_ch;
3495
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003496 return tp;
3497}
3498
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003499 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003500free_tabpage(tp)
3501 tabpage_T *tp;
3502{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003503 int idx;
3504
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003505# ifdef FEAT_DIFF
3506 diff_clear(tp);
3507# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003508 for (idx = 0; idx < SNAP_COUNT; ++idx)
3509 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003510#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003511 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3512 hash_init(&tp->tp_vars->dv_hashtab);
3513 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003514#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003515
3516#ifdef FEAT_PYTHON
3517 python_tabpage_free(tp);
3518#endif
3519
3520#ifdef FEAT_PYTHON3
3521 python3_tabpage_free(tp);
3522#endif
3523
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003524 vim_free(tp);
3525}
3526
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003527/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003528 * Create a new Tab page with one window.
3529 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003530 * When "after" is 0 put it just after the current Tab page.
3531 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003532 * Return FAIL or OK.
3533 */
3534 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003535win_new_tabpage(after)
3536 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003537{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003538 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003539 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003540 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003541
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003542 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003543 if (newtp == NULL)
3544 return FAIL;
3545
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003546 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003547 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003548 {
3549 vim_free(newtp);
3550 return FAIL;
3551 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003552 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003553
3554 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003555 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003556 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003557 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003558 if (after == 1)
3559 {
3560 /* New tab page becomes the first one. */
3561 newtp->tp_next = first_tabpage;
3562 first_tabpage = newtp;
3563 }
3564 else
3565 {
3566 if (after > 0)
3567 {
3568 /* Put new tab page before tab page "after". */
3569 n = 2;
3570 for (tp = first_tabpage; tp->tp_next != NULL
3571 && n < after; tp = tp->tp_next)
3572 ++n;
3573 }
3574 newtp->tp_next = tp->tp_next;
3575 tp->tp_next = newtp;
3576 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003577 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003578 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003579 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003580
3581 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003582 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003583
3584#if defined(FEAT_GUI)
3585 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3586 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003587 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003588#endif
3589
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003590 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003591#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003592 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003593 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003594#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003595 return OK;
3596 }
3597
3598 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003599 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600 return FAIL;
3601}
3602
3603/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003604 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3605 * like with ":split".
3606 * Returns OK if a new tab page was created, FAIL otherwise.
3607 */
3608 int
3609may_open_tabpage()
3610{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003611 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003612
Bram Moolenaard326ce82007-03-11 14:48:29 +00003613 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003614 {
3615 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003616 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003617 return win_new_tabpage(n);
3618 }
3619 return FAIL;
3620}
3621
3622/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003623 * Create up to "maxcount" tabpages with empty windows.
3624 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003625 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003626 int
3627make_tabpages(maxcount)
3628 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003630 int count = maxcount;
3631 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003632
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003633 /* Limit to 'tabpagemax' tabs. */
3634 if (count > p_tpm)
3635 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003636
3637#ifdef FEAT_AUTOCMD
3638 /*
3639 * Don't execute autocommands while creating the tab pages. Must do that
3640 * when putting the buffers in the windows.
3641 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003642 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003643#endif
3644
3645 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003646 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003647 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003648
3649#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003650 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003651#endif
3652
3653 /* return actual number of tab pages */
3654 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003655}
3656
3657/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003658 * Return TRUE when "tpc" points to a valid tab page.
3659 */
3660 int
3661valid_tabpage(tpc)
3662 tabpage_T *tpc;
3663{
3664 tabpage_T *tp;
3665
3666 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3667 if (tp == tpc)
3668 return TRUE;
3669 return FALSE;
3670}
3671
3672/*
3673 * Find tab page "n" (first one is 1). Returns NULL when not found.
3674 */
3675 tabpage_T *
3676find_tabpage(n)
3677 int n;
3678{
3679 tabpage_T *tp;
3680 int i = 1;
3681
3682 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3683 ++i;
3684 return tp;
3685}
3686
3687/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003688 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003689 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003690 */
3691 int
3692tabpage_index(ftp)
3693 tabpage_T *ftp;
3694{
3695 int i = 1;
3696 tabpage_T *tp;
3697
3698 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3699 ++i;
3700 return i;
3701}
3702
3703/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003704 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003705 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003706 * FAIL.
3707 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003708 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003709 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003710leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003711 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003712 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003713 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003714{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003715 tabpage_T *tp = curtab;
3716
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003717 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003718#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003719 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003720 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003721 if (new_curbuf != curbuf)
3722 {
3723 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3724 if (curtab != tp)
3725 return FAIL;
3726 }
3727 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3728 if (curtab != tp)
3729 return FAIL;
3730 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003731 if (curtab != tp)
3732 return FAIL;
3733 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003734#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003735#if defined(FEAT_GUI)
3736 /* Remove the scrollbars. They may be added back later. */
3737 if (gui.in_use)
3738 gui_remove_scrollbars();
3739#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003740 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003741 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003742 tp->tp_firstwin = firstwin;
3743 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003744 tp->tp_old_Rows = Rows;
3745 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003746 firstwin = NULL;
3747 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003748 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749}
3750
3751/*
3752 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003753 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003754 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3755 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003756 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003757 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003758enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003759 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003760 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003761 int trigger_enter_autocmds UNUSED;
3762 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003764 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003765 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003766
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003767 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003768 firstwin = tp->tp_firstwin;
3769 lastwin = tp->tp_lastwin;
3770 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003771
3772 /* We would like doing the TabEnter event first, but we don't have a
3773 * valid current window yet, which may break some commands.
3774 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003775 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3776 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003777 prevwin = next_prevwin;
3778
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003779 last_status(FALSE); /* status line may appear or disappear */
3780 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003781 must_redraw = CLEAR; /* need to redraw everything */
3782#ifdef FEAT_DIFF
3783 diff_need_scrollbind = TRUE;
3784#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003785
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003786 /* The tabpage line may have appeared or disappeared, may need to resize
3787 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003788 * frame sizes too. Use the stored value of p_ch, so that it can be
3789 * different for each tab page. */
3790 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003791 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3792#ifdef FEAT_GUI_TABLINE
3793 && !gui_use_tabline()
3794#endif
3795 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003796 shell_new_rows();
3797#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003798 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003799 shell_new_columns(); /* update window widths */
3800#endif
3801
3802#if defined(FEAT_GUI)
3803 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3804 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003805 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003806#endif
3807
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003808#ifdef FEAT_AUTOCMD
3809 /* Apply autocommands after updating the display, when 'rows' and
3810 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003811 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003812 {
3813 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3814 if (old_curbuf != curbuf)
3815 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3816 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003817#endif
3818
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003819 redraw_all_later(CLEAR);
3820}
3821
3822/*
3823 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003824 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003825 */
3826 void
3827goto_tabpage(n)
3828 int n;
3829{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003830 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003831 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003832 int i;
3833
Bram Moolenaard68071d2006-05-02 22:08:30 +00003834 if (text_locked())
3835 {
3836 /* Not allowed when editing the command line. */
3837#ifdef FEAT_CMDWIN
3838 if (cmdwin_type != 0)
3839 EMSG(_(e_cmdwin));
3840 else
3841#endif
3842 EMSG(_(e_secure));
3843 return;
3844 }
3845
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003846 /* If there is only one it can't work. */
3847 if (first_tabpage->tp_next == NULL)
3848 {
3849 if (n > 1)
3850 beep_flush();
3851 return;
3852 }
3853
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003854 if (n == 0)
3855 {
3856 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003857 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003858 tp = first_tabpage;
3859 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003860 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003861 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003862 else if (n < 0)
3863 {
3864 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3865 * this N times. */
3866 ttp = curtab;
3867 for (i = n; i < 0; ++i)
3868 {
3869 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3870 tp = tp->tp_next)
3871 ;
3872 ttp = tp;
3873 }
3874 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003875 else if (n == 9999)
3876 {
3877 /* Go to last tab page. */
3878 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3879 ;
3880 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003881 else
3882 {
3883 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003884 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003885 if (tp == NULL)
3886 {
3887 beep_flush();
3888 return;
3889 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003890 }
3891
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003892 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003893
3894#ifdef FEAT_GUI_TABLINE
3895 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003896 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003897#endif
3898}
3899
3900/*
3901 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003902 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3903 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003904 * Note: doesn't update the GUI tab.
3905 */
3906 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003907goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003908 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003909 int trigger_enter_autocmds;
3910 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003911{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003912 /* Don't repeat a message in another tab page. */
3913 set_keep_msg(NULL, 0);
3914
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003915 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3916 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003917 {
3918 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003919 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3920 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003921 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003922 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3923 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003924 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003928 * Enter window "wp" in tab page "tp".
3929 * Also updates the GUI tab.
3930 */
3931 void
3932goto_tabpage_win(tp, wp)
3933 tabpage_T *tp;
3934 win_T *wp;
3935{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003936 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003937 if (curtab == tp && win_valid(wp))
3938 {
3939 win_enter(wp, TRUE);
3940# ifdef FEAT_GUI_TABLINE
3941 if (gui_use_tabline())
3942 gui_mch_set_curtab(tabpage_index(curtab));
3943# endif
3944 }
3945}
3946
3947/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003948 * Move the current tab page to before tab page "nr".
3949 */
3950 void
3951tabpage_move(nr)
3952 int nr;
3953{
3954 int n = nr;
3955 tabpage_T *tp;
3956
3957 if (first_tabpage->tp_next == NULL)
3958 return;
3959
3960 /* Remove the current tab page from the list of tab pages. */
3961 if (curtab == first_tabpage)
3962 first_tabpage = curtab->tp_next;
3963 else
3964 {
3965 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3966 if (tp->tp_next == curtab)
3967 break;
3968 if (tp == NULL) /* "cannot happen" */
3969 return;
3970 tp->tp_next = curtab->tp_next;
3971 }
3972
3973 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003974 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003975 {
3976 curtab->tp_next = first_tabpage;
3977 first_tabpage = curtab;
3978 }
3979 else
3980 {
3981 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3982 --n;
3983 curtab->tp_next = tp->tp_next;
3984 tp->tp_next = curtab;
3985 }
3986
3987 /* Need to redraw the tabline. Tab page contents doesn't change. */
3988 redraw_tabline = TRUE;
3989}
3990
3991
3992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 * Go to another window.
3994 * When jumping to another buffer, stop Visual mode. Do this before
3995 * changing windows so we can yank the selection into the '*' register.
3996 * When jumping to another window on the same buffer, adjust its cursor
3997 * position to keep the same Visual area.
3998 */
3999 void
4000win_goto(wp)
4001 win_T *wp;
4002{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004003#ifdef FEAT_CONCEAL
4004 win_T *owp = curwin;
4005#endif
4006
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004007 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
4009 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004010 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 return;
4012 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004013#ifdef FEAT_AUTOCMD
4014 if (curbuf_locked())
4015 return;
4016#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 if (wp->w_buffer != curbuf)
4019 reset_VIsual_and_resel();
4020 else if (VIsual_active)
4021 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023#ifdef FEAT_GUI
4024 need_mouse_correct = TRUE;
4025#endif
4026 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004027
4028#ifdef FEAT_CONCEAL
4029 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004030 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004031 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004032 if (curwin->w_p_cole > 0 && !msg_scrolled)
4033 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035}
4036
4037#if defined(FEAT_PERL) || defined(PROTO)
4038/*
4039 * Find window number "winnr" (counting top to bottom).
4040 */
4041 win_T *
4042win_find_nr(winnr)
4043 int winnr;
4044{
4045 win_T *wp;
4046
4047# ifdef FEAT_WINDOWS
4048 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4049 if (--winnr == 0)
4050 break;
4051 return wp;
4052# else
4053 return curwin;
4054# endif
4055}
4056#endif
4057
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004058#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4059 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004060/*
4061 * Find the tabpage for window "win".
4062 */
4063 tabpage_T *
4064win_find_tabpage(win)
4065 win_T *win;
4066{
4067 win_T *wp;
4068 tabpage_T *tp;
4069
4070 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004071 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4072 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004073 if (wp == win)
4074 return tp;
4075 return NULL;
4076}
4077#endif
4078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079#ifdef FEAT_VERTSPLIT
4080/*
4081 * Move to window above or below "count" times.
4082 */
4083 static void
4084win_goto_ver(up, count)
4085 int up; /* TRUE to go to win above */
4086 long count;
4087{
4088 frame_T *fr;
4089 frame_T *nfr;
4090 frame_T *foundfr;
4091
4092 foundfr = curwin->w_frame;
4093 while (count--)
4094 {
4095 /*
4096 * First go upwards in the tree of frames until we find a upwards or
4097 * downwards neighbor.
4098 */
4099 fr = foundfr;
4100 for (;;)
4101 {
4102 if (fr == topframe)
4103 goto end;
4104 if (up)
4105 nfr = fr->fr_prev;
4106 else
4107 nfr = fr->fr_next;
4108 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4109 break;
4110 fr = fr->fr_parent;
4111 }
4112
4113 /*
4114 * Now go downwards to find the bottom or top frame in it.
4115 */
4116 for (;;)
4117 {
4118 if (nfr->fr_layout == FR_LEAF)
4119 {
4120 foundfr = nfr;
4121 break;
4122 }
4123 fr = nfr->fr_child;
4124 if (nfr->fr_layout == FR_ROW)
4125 {
4126 /* Find the frame at the cursor row. */
4127 while (fr->fr_next != NULL
4128 && frame2win(fr)->w_wincol + fr->fr_width
4129 <= curwin->w_wincol + curwin->w_wcol)
4130 fr = fr->fr_next;
4131 }
4132 if (nfr->fr_layout == FR_COL && up)
4133 while (fr->fr_next != NULL)
4134 fr = fr->fr_next;
4135 nfr = fr;
4136 }
4137 }
4138end:
4139 if (foundfr != NULL)
4140 win_goto(foundfr->fr_win);
4141}
4142
4143/*
4144 * Move to left or right window.
4145 */
4146 static void
4147win_goto_hor(left, count)
4148 int left; /* TRUE to go to left win */
4149 long count;
4150{
4151 frame_T *fr;
4152 frame_T *nfr;
4153 frame_T *foundfr;
4154
4155 foundfr = curwin->w_frame;
4156 while (count--)
4157 {
4158 /*
4159 * First go upwards in the tree of frames until we find a left or
4160 * right neighbor.
4161 */
4162 fr = foundfr;
4163 for (;;)
4164 {
4165 if (fr == topframe)
4166 goto end;
4167 if (left)
4168 nfr = fr->fr_prev;
4169 else
4170 nfr = fr->fr_next;
4171 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4172 break;
4173 fr = fr->fr_parent;
4174 }
4175
4176 /*
4177 * Now go downwards to find the leftmost or rightmost frame in it.
4178 */
4179 for (;;)
4180 {
4181 if (nfr->fr_layout == FR_LEAF)
4182 {
4183 foundfr = nfr;
4184 break;
4185 }
4186 fr = nfr->fr_child;
4187 if (nfr->fr_layout == FR_COL)
4188 {
4189 /* Find the frame at the cursor row. */
4190 while (fr->fr_next != NULL
4191 && frame2win(fr)->w_winrow + fr->fr_height
4192 <= curwin->w_winrow + curwin->w_wrow)
4193 fr = fr->fr_next;
4194 }
4195 if (nfr->fr_layout == FR_ROW && left)
4196 while (fr->fr_next != NULL)
4197 fr = fr->fr_next;
4198 nfr = fr;
4199 }
4200 }
4201end:
4202 if (foundfr != NULL)
4203 win_goto(foundfr->fr_win);
4204}
4205#endif
4206
4207/*
4208 * Make window "wp" the current window.
4209 */
4210 void
4211win_enter(wp, undo_sync)
4212 win_T *wp;
4213 int undo_sync;
4214{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004215 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216}
4217
4218/*
4219 * Make window wp the current window.
4220 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4221 * been closed and isn't valid.
4222 */
4223 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004224win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 win_T *wp;
4226 int undo_sync;
4227 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004228 int trigger_enter_autocmds UNUSED;
4229 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231#ifdef FEAT_AUTOCMD
4232 int other_buffer = FALSE;
4233#endif
4234
4235 if (wp == curwin && !curwin_invalid) /* nothing to do */
4236 return;
4237
4238#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004239 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
4241 /*
4242 * Be careful: If autocommands delete the window, return now.
4243 */
4244 if (wp->w_buffer != curbuf)
4245 {
4246 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4247 other_buffer = TRUE;
4248 if (!win_valid(wp))
4249 return;
4250 }
4251 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4252 if (!win_valid(wp))
4253 return;
4254# ifdef FEAT_EVAL
4255 /* autocmds may abort script processing */
4256 if (aborting())
4257 return;
4258# endif
4259 }
4260#endif
4261
4262 /* sync undo before leaving the current buffer */
4263 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004264 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004265
4266 /* Might need to scroll the old window before switching, e.g., when the
4267 * cursor was moved. */
4268 update_topline();
4269
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 /* may have to copy the buffer options when 'cpo' contains 'S' */
4271 if (wp->w_buffer != curbuf)
4272 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4273 if (!curwin_invalid)
4274 {
4275 prevwin = curwin; /* remember for CTRL-W p */
4276 curwin->w_redr_status = TRUE;
4277 }
4278 curwin = wp;
4279 curbuf = wp->w_buffer;
4280 check_cursor();
4281#ifdef FEAT_VIRTUALEDIT
4282 if (!virtual_active())
4283 curwin->w_cursor.coladd = 0;
4284#endif
4285 changed_line_abv_curs(); /* assume cursor position needs updating */
4286
4287 if (curwin->w_localdir != NULL)
4288 {
4289 /* Window has a local directory: Save current directory as global
4290 * directory (unless that was done already) and change to the local
4291 * directory. */
4292 if (globaldir == NULL)
4293 {
4294 char_u cwd[MAXPATHL];
4295
4296 if (mch_dirname(cwd, MAXPATHL) == OK)
4297 globaldir = vim_strsave(cwd);
4298 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004299 if (mch_chdir((char *)curwin->w_localdir) == 0)
4300 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 else if (globaldir != NULL)
4303 {
4304 /* Window doesn't have a local directory and we are not in the global
4305 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004306 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 vim_free(globaldir);
4308 globaldir = NULL;
4309 shorten_fnames(TRUE);
4310 }
4311
4312#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004313 if (trigger_enter_autocmds)
4314 {
4315 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4316 if (other_buffer)
4317 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319#endif
4320
4321#ifdef FEAT_TITLE
4322 maketitle();
4323#endif
4324 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004325 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 if (restart_edit)
4327 redraw_later(VALID); /* causes status line redraw */
4328
4329 /* set window height to desired minimal value */
4330 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4331 win_setheight((int)p_wh);
4332 else if (curwin->w_height == 0)
4333 win_setheight(1);
4334
4335#ifdef FEAT_VERTSPLIT
4336 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004337 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 win_setwidth((int)p_wiw);
4339#endif
4340
4341#ifdef FEAT_MOUSE
4342 setmouse(); /* in case jumped to/from help buffer */
4343#endif
4344
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004345 /* Change directories when the 'acd' option is set. */
4346 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347}
4348
4349#endif /* FEAT_WINDOWS */
4350
4351#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4352/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004353 * Jump to the first open window that contains buffer "buf", if one exists.
4354 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 */
4356 win_T *
4357buf_jump_open_win(buf)
4358 buf_T *buf;
4359{
4360# ifdef FEAT_WINDOWS
4361 win_T *wp;
4362
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004363 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 if (wp->w_buffer == buf)
4365 break;
4366 if (wp != NULL)
4367 win_enter(wp, FALSE);
4368 return wp;
4369# else
4370 if (curwin->w_buffer == buf)
4371 return curwin;
4372 return NULL;
4373# endif
4374}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004375
4376/*
4377 * Jump to the first open window in any tab page that contains buffer "buf",
4378 * if one exists.
4379 * Returns a pointer to the window found, otherwise NULL.
4380 */
4381 win_T *
4382buf_jump_open_tab(buf)
4383 buf_T *buf;
4384{
4385# ifdef FEAT_WINDOWS
4386 win_T *wp;
4387 tabpage_T *tp;
4388
4389 /* First try the current tab page. */
4390 wp = buf_jump_open_win(buf);
4391 if (wp != NULL)
4392 return wp;
4393
4394 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4395 if (tp != curtab)
4396 {
4397 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4398 if (wp->w_buffer == buf)
4399 break;
4400 if (wp != NULL)
4401 {
4402 goto_tabpage_win(tp, wp);
4403 if (curwin != wp)
4404 wp = NULL; /* something went wrong */
4405 break;
4406 }
4407 }
4408
4409 return wp;
4410# else
4411 if (curwin->w_buffer == buf)
4412 return curwin;
4413 return NULL;
4414# endif
4415}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416#endif
4417
4418/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004419 * Allocate a window structure and link it in the window list when "hidden" is
4420 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004423win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004424 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004425 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004427 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 /*
4430 * allocate window structure and linesizes arrays
4431 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004432 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004433 if (new_wp == NULL)
4434 return NULL;
4435
4436 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004438 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004439 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441
Bram Moolenaar429fa852013-04-15 12:27:36 +02004442#ifdef FEAT_EVAL
4443 /* init w: variables */
4444 new_wp->w_vars = dict_alloc();
4445 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004447 win_free_lsize(new_wp);
4448 vim_free(new_wp);
4449 return NULL;
4450 }
4451 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004452#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004453
4454#ifdef FEAT_AUTOCMD
4455 /* Don't execute autocommands while the window is not properly
4456 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4457 * event. */
4458 block_autocmds();
4459#endif
4460 /*
4461 * link the window in the window list
4462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004464 if (!hidden)
4465 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466#endif
4467#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004468 new_wp->w_wincol = 0;
4469 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#endif
4471
Bram Moolenaar429fa852013-04-15 12:27:36 +02004472 /* position the display and the cursor at the top of the file. */
4473 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004475 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004477 new_wp->w_botline = 2;
4478 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004480 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481#endif
4482
Bram Moolenaar429fa852013-04-15 12:27:36 +02004483 /* We won't calculate w_fraction until resizing the window */
4484 new_wp->w_fraction = 0;
4485 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004488 if (gui.in_use)
4489 {
4490 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4491 SBAR_LEFT, new_wp);
4492 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4493 SBAR_RIGHT, new_wp);
4494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495#endif
4496#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004497 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004499#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004500 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004501#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004502#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004503 new_wp->w_match_head = NULL;
4504 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004505#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004506 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507}
4508
4509#if defined(FEAT_WINDOWS) || defined(PROTO)
4510
4511/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004512 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 */
4514 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004515win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004517 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518{
4519 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004520 buf_T *buf;
4521 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004523#ifdef FEAT_FOLDING
4524 clearFolding(wp);
4525#endif
4526
4527 /* reduce the reference count to the argument list. */
4528 alist_unlink(wp->w_alist);
4529
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004530#ifdef FEAT_AUTOCMD
4531 /* Don't execute autocommands while the window is halfway being deleted.
4532 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004533 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004534#endif
4535
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004536#ifdef FEAT_LUA
4537 lua_window_free(wp);
4538#endif
4539
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004540#ifdef FEAT_MZSCHEME
4541 mzscheme_window_free(wp);
4542#endif
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544#ifdef FEAT_PERL
4545 perl_win_free(wp);
4546#endif
4547
4548#ifdef FEAT_PYTHON
4549 python_window_free(wp);
4550#endif
4551
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004552#ifdef FEAT_PYTHON3
4553 python3_window_free(wp);
4554#endif
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556#ifdef FEAT_TCL
4557 tcl_window_free(wp);
4558#endif
4559
4560#ifdef FEAT_RUBY
4561 ruby_window_free(wp);
4562#endif
4563
4564 clear_winopt(&wp->w_onebuf_opt);
4565 clear_winopt(&wp->w_allbuf_opt);
4566
4567#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004568 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4569 hash_init(&wp->w_vars->dv_hashtab);
4570 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571#endif
4572
4573 if (prevwin == wp)
4574 prevwin = NULL;
4575 win_free_lsize(wp);
4576
4577 for (i = 0; i < wp->w_tagstacklen; ++i)
4578 vim_free(wp->w_tagstack[i].tagname);
4579
4580 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004581
Bram Moolenaarff18df02013-07-24 17:51:57 +02004582 /* Remove the window from the b_wininfo lists, it may happen that the
4583 * freed memory is re-used for another window. */
4584 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4585 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4586 if (wip->wi_win == wp)
4587 wip->wi_win = NULL;
4588
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004590 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004592
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593#ifdef FEAT_JUMPLIST
4594 free_jumplist(wp);
4595#endif
4596
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004597#ifdef FEAT_QUICKFIX
4598 qf_free_all(wp);
4599#endif
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601#ifdef FEAT_GUI
4602 if (gui.in_use)
4603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4605 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4606 }
4607#endif /* FEAT_GUI */
4608
Bram Moolenaar860cae12010-06-05 23:22:07 +02004609#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004610 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004611#endif
4612
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004613#ifdef FEAT_AUTOCMD
4614 if (wp != aucmd_win)
4615#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004616 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004617#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004618 if (autocmd_busy)
4619 {
4620 wp->w_next = au_pending_free_win;
4621 au_pending_free_win = wp;
4622 }
4623 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004624#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004625 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004626
4627#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004628 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630}
4631
4632/*
4633 * Append window "wp" in the window list after window "after".
4634 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004635 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636win_append(after, wp)
4637 win_T *after, *wp;
4638{
4639 win_T *before;
4640
4641 if (after == NULL) /* after NULL is in front of the first */
4642 before = firstwin;
4643 else
4644 before = after->w_next;
4645
4646 wp->w_next = before;
4647 wp->w_prev = after;
4648 if (after == NULL)
4649 firstwin = wp;
4650 else
4651 after->w_next = wp;
4652 if (before == NULL)
4653 lastwin = wp;
4654 else
4655 before->w_prev = wp;
4656}
4657
4658/*
4659 * Remove a window from the window list.
4660 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004661 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004662win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004664 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665{
4666 if (wp->w_prev != NULL)
4667 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004668 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004670 else
4671 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 if (wp->w_next != NULL)
4673 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004674 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004676 else
4677 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678}
4679
4680/*
4681 * Append frame "frp" in a frame list after frame "after".
4682 */
4683 static void
4684frame_append(after, frp)
4685 frame_T *after, *frp;
4686{
4687 frp->fr_next = after->fr_next;
4688 after->fr_next = frp;
4689 if (frp->fr_next != NULL)
4690 frp->fr_next->fr_prev = frp;
4691 frp->fr_prev = after;
4692}
4693
4694/*
4695 * Insert frame "frp" in a frame list before frame "before".
4696 */
4697 static void
4698frame_insert(before, frp)
4699 frame_T *before, *frp;
4700{
4701 frp->fr_next = before;
4702 frp->fr_prev = before->fr_prev;
4703 before->fr_prev = frp;
4704 if (frp->fr_prev != NULL)
4705 frp->fr_prev->fr_next = frp;
4706 else
4707 frp->fr_parent->fr_child = frp;
4708}
4709
4710/*
4711 * Remove a frame from a frame list.
4712 */
4713 static void
4714frame_remove(frp)
4715 frame_T *frp;
4716{
4717 if (frp->fr_prev != NULL)
4718 frp->fr_prev->fr_next = frp->fr_next;
4719 else
4720 frp->fr_parent->fr_child = frp->fr_next;
4721 if (frp->fr_next != NULL)
4722 frp->fr_next->fr_prev = frp->fr_prev;
4723}
4724
4725#endif /* FEAT_WINDOWS */
4726
4727/*
4728 * Allocate w_lines[] for window "wp".
4729 * Return FAIL for failure, OK for success.
4730 */
4731 int
4732win_alloc_lines(wp)
4733 win_T *wp;
4734{
4735 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004736 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 if (wp->w_lines == NULL)
4738 return FAIL;
4739 return OK;
4740}
4741
4742/*
4743 * free lsize arrays for a window
4744 */
4745 void
4746win_free_lsize(wp)
4747 win_T *wp;
4748{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004749 /* TODO: why would wp be NULL here? */
4750 if (wp != NULL)
4751 {
4752 vim_free(wp->w_lines);
4753 wp->w_lines = NULL;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755}
4756
4757/*
4758 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004759 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 */
4761 void
4762shell_new_rows()
4763{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004764 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 if (firstwin == NULL) /* not initialized yet */
4767 return;
4768#ifdef FEAT_WINDOWS
4769 if (h < frame_minheight(topframe, NULL))
4770 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004771
4772 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 * that doesn't result in the right height, forget about that option. */
4774 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004775 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 frame_new_height(topframe, h, FALSE, FALSE);
4777
4778 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4779#else
4780 if (h < 1)
4781 h = 1;
4782 win_new_height(firstwin, h);
4783#endif
4784 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004785#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004786 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004787#endif
4788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789#if 0
4790 /* Disabled: don't want making the screen smaller make a window larger. */
4791 if (p_ea)
4792 win_equal(curwin, FALSE, 'v');
4793#endif
4794}
4795
4796#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4797/*
4798 * Called from win_new_shellsize() after Columns changed.
4799 */
4800 void
4801shell_new_columns()
4802{
4803 if (firstwin == NULL) /* not initialized yet */
4804 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004805
4806 /* First try setting the widths of windows with 'winfixwidth'. If that
4807 * doesn't result in the right width, forget about that option. */
4808 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004809 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004810 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4811
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4813#if 0
4814 /* Disabled: don't want making the screen smaller make a window larger. */
4815 if (p_ea)
4816 win_equal(curwin, FALSE, 'h');
4817#endif
4818}
4819#endif
4820
4821#if defined(FEAT_CMDWIN) || defined(PROTO)
4822/*
4823 * Save the size of all windows in "gap".
4824 */
4825 void
4826win_size_save(gap)
4827 garray_T *gap;
4828
4829{
4830 win_T *wp;
4831
4832 ga_init2(gap, (int)sizeof(int), 1);
4833 if (ga_grow(gap, win_count() * 2) == OK)
4834 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4835 {
4836 ((int *)gap->ga_data)[gap->ga_len++] =
4837 wp->w_width + wp->w_vsep_width;
4838 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4839 }
4840}
4841
4842/*
4843 * Restore window sizes, but only if the number of windows is still the same.
4844 * Does not free the growarray.
4845 */
4846 void
4847win_size_restore(gap)
4848 garray_T *gap;
4849{
4850 win_T *wp;
4851 int i;
4852
4853 if (win_count() * 2 == gap->ga_len)
4854 {
4855 i = 0;
4856 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4857 {
4858 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4859 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4860 }
4861 /* recompute the window positions */
4862 (void)win_comp_pos();
4863 }
4864}
4865#endif /* FEAT_CMDWIN */
4866
4867#if defined(FEAT_WINDOWS) || defined(PROTO)
4868/*
4869 * Update the position for all windows, using the width and height of the
4870 * frames.
4871 * Returns the row just after the last window.
4872 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004873 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874win_comp_pos()
4875{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004876 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 int col = 0;
4878
4879 frame_comp_pos(topframe, &row, &col);
4880 return row;
4881}
4882
4883/*
4884 * Update the position of the windows in frame "topfrp", using the width and
4885 * height of the frames.
4886 * "*row" and "*col" are the top-left position of the frame. They are updated
4887 * to the bottom-right position plus one.
4888 */
4889 static void
4890frame_comp_pos(topfrp, row, col)
4891 frame_T *topfrp;
4892 int *row;
4893 int *col;
4894{
4895 win_T *wp;
4896 frame_T *frp;
4897#ifdef FEAT_VERTSPLIT
4898 int startcol;
4899 int startrow;
4900#endif
4901
4902 wp = topfrp->fr_win;
4903 if (wp != NULL)
4904 {
4905 if (wp->w_winrow != *row
4906#ifdef FEAT_VERTSPLIT
4907 || wp->w_wincol != *col
4908#endif
4909 )
4910 {
4911 /* position changed, redraw */
4912 wp->w_winrow = *row;
4913#ifdef FEAT_VERTSPLIT
4914 wp->w_wincol = *col;
4915#endif
4916 redraw_win_later(wp, NOT_VALID);
4917 wp->w_redr_status = TRUE;
4918 }
4919 *row += wp->w_height + wp->w_status_height;
4920#ifdef FEAT_VERTSPLIT
4921 *col += wp->w_width + wp->w_vsep_width;
4922#endif
4923 }
4924 else
4925 {
4926#ifdef FEAT_VERTSPLIT
4927 startrow = *row;
4928 startcol = *col;
4929#endif
4930 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4931 {
4932#ifdef FEAT_VERTSPLIT
4933 if (topfrp->fr_layout == FR_ROW)
4934 *row = startrow; /* all frames are at the same row */
4935 else
4936 *col = startcol; /* all frames are at the same col */
4937#endif
4938 frame_comp_pos(frp, row, col);
4939 }
4940 }
4941}
4942
4943#endif /* FEAT_WINDOWS */
4944
4945/*
4946 * Set current window height and take care of repositioning other windows to
4947 * fit around it.
4948 */
4949 void
4950win_setheight(height)
4951 int height;
4952{
4953 win_setheight_win(height, curwin);
4954}
4955
4956/*
4957 * Set the window height of window "win" and take care of repositioning other
4958 * windows to fit around it.
4959 */
4960 void
4961win_setheight_win(height, win)
4962 int height;
4963 win_T *win;
4964{
4965 int row;
4966
4967 if (win == curwin)
4968 {
4969 /* Always keep current window at least one line high, even when
4970 * 'winminheight' is zero. */
4971#ifdef FEAT_WINDOWS
4972 if (height < p_wmh)
4973 height = p_wmh;
4974#endif
4975 if (height == 0)
4976 height = 1;
4977 }
4978
4979#ifdef FEAT_WINDOWS
4980 frame_setheight(win->w_frame, height + win->w_status_height);
4981
4982 /* recompute the window positions */
4983 row = win_comp_pos();
4984#else
4985 if (height > topframe->fr_height)
4986 height = topframe->fr_height;
4987 win->w_height = height;
4988 row = height;
4989#endif
4990
4991 /*
4992 * If there is extra space created between the last window and the command
4993 * line, clear it.
4994 */
4995 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4996 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4997 cmdline_row = row;
4998 msg_row = row;
4999 msg_col = 0;
5000
5001 redraw_all_later(NOT_VALID);
5002}
5003
5004#if defined(FEAT_WINDOWS) || defined(PROTO)
5005
5006/*
5007 * Set the height of a frame to "height" and take care that all frames and
5008 * windows inside it are resized. Also resize frames on the left and right if
5009 * the are in the same FR_ROW frame.
5010 *
5011 * Strategy:
5012 * If the frame is part of a FR_COL frame, try fitting the frame in that
5013 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5014 * go to containing frames to resize them and make room.
5015 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5016 * Check for the minimal height of the FR_ROW frame.
5017 * At the top level we can also use change the command line height.
5018 */
5019 static void
5020frame_setheight(curfrp, height)
5021 frame_T *curfrp;
5022 int height;
5023{
5024 int room; /* total number of lines available */
5025 int take; /* number of lines taken from other windows */
5026 int room_cmdline; /* lines available from cmdline */
5027 int run;
5028 frame_T *frp;
5029 int h;
5030 int room_reserved;
5031
5032 /* If the height already is the desired value, nothing to do. */
5033 if (curfrp->fr_height == height)
5034 return;
5035
5036 if (curfrp->fr_parent == NULL)
5037 {
5038 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005039 if (height > ROWS_AVAIL)
5040 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (height > 0)
5042 frame_new_height(curfrp, height, FALSE, FALSE);
5043 }
5044 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5045 {
5046 /* Row of frames: Also need to resize frames left and right of this
5047 * one. First check for the minimal height of these. */
5048 h = frame_minheight(curfrp->fr_parent, NULL);
5049 if (height < h)
5050 height = h;
5051 frame_setheight(curfrp->fr_parent, height);
5052 }
5053 else
5054 {
5055 /*
5056 * Column of frames: try to change only frames in this column.
5057 */
5058#ifdef FEAT_VERTSPLIT
5059 /*
5060 * Do this twice:
5061 * 1: compute room available, if it's not enough try resizing the
5062 * containing frame.
5063 * 2: compute the room available and adjust the height to it.
5064 * Try not to reduce the height of a window with 'winfixheight' set.
5065 */
5066 for (run = 1; run <= 2; ++run)
5067#else
5068 for (;;)
5069#endif
5070 {
5071 room = 0;
5072 room_reserved = 0;
5073 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5074 frp = frp->fr_next)
5075 {
5076 if (frp != curfrp
5077 && frp->fr_win != NULL
5078 && frp->fr_win->w_p_wfh)
5079 room_reserved += frp->fr_height;
5080 room += frp->fr_height;
5081 if (frp != curfrp)
5082 room -= frame_minheight(frp, NULL);
5083 }
5084#ifdef FEAT_VERTSPLIT
5085 if (curfrp->fr_width != Columns)
5086 room_cmdline = 0;
5087 else
5088#endif
5089 {
5090 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5091 + lastwin->w_height + lastwin->w_status_height);
5092 if (room_cmdline < 0)
5093 room_cmdline = 0;
5094 }
5095
5096 if (height <= room + room_cmdline)
5097 break;
5098#ifdef FEAT_VERTSPLIT
5099 if (run == 2 || curfrp->fr_width == Columns)
5100#endif
5101 {
5102 if (height > room + room_cmdline)
5103 height = room + room_cmdline;
5104 break;
5105 }
5106#ifdef FEAT_VERTSPLIT
5107 frame_setheight(curfrp->fr_parent, height
5108 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5109#endif
5110 /*NOTREACHED*/
5111 }
5112
5113 /*
5114 * Compute the number of lines we will take from others frames (can be
5115 * negative!).
5116 */
5117 take = height - curfrp->fr_height;
5118
5119 /* If there is not enough room, also reduce the height of a window
5120 * with 'winfixheight' set. */
5121 if (height > room + room_cmdline - room_reserved)
5122 room_reserved = room + room_cmdline - height;
5123 /* If there is only a 'winfixheight' window and making the
5124 * window smaller, need to make the other window taller. */
5125 if (take < 0 && room - curfrp->fr_height < room_reserved)
5126 room_reserved = 0;
5127
5128 if (take > 0 && room_cmdline > 0)
5129 {
5130 /* use lines from cmdline first */
5131 if (take < room_cmdline)
5132 room_cmdline = take;
5133 take -= room_cmdline;
5134 topframe->fr_height += room_cmdline;
5135 }
5136
5137 /*
5138 * set the current frame to the new height
5139 */
5140 frame_new_height(curfrp, height, FALSE, FALSE);
5141
5142 /*
5143 * First take lines from the frames after the current frame. If
5144 * that is not enough, takes lines from frames above the current
5145 * frame.
5146 */
5147 for (run = 0; run < 2; ++run)
5148 {
5149 if (run == 0)
5150 frp = curfrp->fr_next; /* 1st run: start with next window */
5151 else
5152 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5153 while (frp != NULL && take != 0)
5154 {
5155 h = frame_minheight(frp, NULL);
5156 if (room_reserved > 0
5157 && frp->fr_win != NULL
5158 && frp->fr_win->w_p_wfh)
5159 {
5160 if (room_reserved >= frp->fr_height)
5161 room_reserved -= frp->fr_height;
5162 else
5163 {
5164 if (frp->fr_height - room_reserved > take)
5165 room_reserved = frp->fr_height - take;
5166 take -= frp->fr_height - room_reserved;
5167 frame_new_height(frp, room_reserved, FALSE, FALSE);
5168 room_reserved = 0;
5169 }
5170 }
5171 else
5172 {
5173 if (frp->fr_height - take < h)
5174 {
5175 take -= frp->fr_height - h;
5176 frame_new_height(frp, h, FALSE, FALSE);
5177 }
5178 else
5179 {
5180 frame_new_height(frp, frp->fr_height - take,
5181 FALSE, FALSE);
5182 take = 0;
5183 }
5184 }
5185 if (run == 0)
5186 frp = frp->fr_next;
5187 else
5188 frp = frp->fr_prev;
5189 }
5190 }
5191 }
5192}
5193
5194#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5195/*
5196 * Set current window width and take care of repositioning other windows to
5197 * fit around it.
5198 */
5199 void
5200win_setwidth(width)
5201 int width;
5202{
5203 win_setwidth_win(width, curwin);
5204}
5205
5206 void
5207win_setwidth_win(width, wp)
5208 int width;
5209 win_T *wp;
5210{
5211 /* Always keep current window at least one column wide, even when
5212 * 'winminwidth' is zero. */
5213 if (wp == curwin)
5214 {
5215 if (width < p_wmw)
5216 width = p_wmw;
5217 if (width == 0)
5218 width = 1;
5219 }
5220
5221 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5222
5223 /* recompute the window positions */
5224 (void)win_comp_pos();
5225
5226 redraw_all_later(NOT_VALID);
5227}
5228
5229/*
5230 * Set the width of a frame to "width" and take care that all frames and
5231 * windows inside it are resized. Also resize frames above and below if the
5232 * are in the same FR_ROW frame.
5233 *
5234 * Strategy is similar to frame_setheight().
5235 */
5236 static void
5237frame_setwidth(curfrp, width)
5238 frame_T *curfrp;
5239 int width;
5240{
5241 int room; /* total number of lines available */
5242 int take; /* number of lines taken from other windows */
5243 int run;
5244 frame_T *frp;
5245 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005246 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248 /* If the width already is the desired value, nothing to do. */
5249 if (curfrp->fr_width == width)
5250 return;
5251
5252 if (curfrp->fr_parent == NULL)
5253 /* topframe: can't change width */
5254 return;
5255
5256 if (curfrp->fr_parent->fr_layout == FR_COL)
5257 {
5258 /* Column of frames: Also need to resize frames above and below of
5259 * this one. First check for the minimal width of these. */
5260 w = frame_minwidth(curfrp->fr_parent, NULL);
5261 if (width < w)
5262 width = w;
5263 frame_setwidth(curfrp->fr_parent, width);
5264 }
5265 else
5266 {
5267 /*
5268 * Row of frames: try to change only frames in this row.
5269 *
5270 * Do this twice:
5271 * 1: compute room available, if it's not enough try resizing the
5272 * containing frame.
5273 * 2: compute the room available and adjust the width to it.
5274 */
5275 for (run = 1; run <= 2; ++run)
5276 {
5277 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005278 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5280 frp = frp->fr_next)
5281 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005282 if (frp != curfrp
5283 && frp->fr_win != NULL
5284 && frp->fr_win->w_p_wfw)
5285 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 room += frp->fr_width;
5287 if (frp != curfrp)
5288 room -= frame_minwidth(frp, NULL);
5289 }
5290
5291 if (width <= room)
5292 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005293 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
5295 if (width > room)
5296 width = room;
5297 break;
5298 }
5299 frame_setwidth(curfrp->fr_parent, width
5300 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5301 }
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 /*
5304 * Compute the number of lines we will take from others frames (can be
5305 * negative!).
5306 */
5307 take = width - curfrp->fr_width;
5308
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005309 /* If there is not enough room, also reduce the width of a window
5310 * with 'winfixwidth' set. */
5311 if (width > room - room_reserved)
5312 room_reserved = room - width;
5313 /* If there is only a 'winfixwidth' window and making the
5314 * window smaller, need to make the other window narrower. */
5315 if (take < 0 && room - curfrp->fr_width < room_reserved)
5316 room_reserved = 0;
5317
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 /*
5319 * set the current frame to the new width
5320 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005321 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323 /*
5324 * First take lines from the frames right of the current frame. If
5325 * that is not enough, takes lines from frames left of the current
5326 * frame.
5327 */
5328 for (run = 0; run < 2; ++run)
5329 {
5330 if (run == 0)
5331 frp = curfrp->fr_next; /* 1st run: start with next window */
5332 else
5333 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5334 while (frp != NULL && take != 0)
5335 {
5336 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005337 if (room_reserved > 0
5338 && frp->fr_win != NULL
5339 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005341 if (room_reserved >= frp->fr_width)
5342 room_reserved -= frp->fr_width;
5343 else
5344 {
5345 if (frp->fr_width - room_reserved > take)
5346 room_reserved = frp->fr_width - take;
5347 take -= frp->fr_width - room_reserved;
5348 frame_new_width(frp, room_reserved, FALSE, FALSE);
5349 room_reserved = 0;
5350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352 else
5353 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005354 if (frp->fr_width - take < w)
5355 {
5356 take -= frp->fr_width - w;
5357 frame_new_width(frp, w, FALSE, FALSE);
5358 }
5359 else
5360 {
5361 frame_new_width(frp, frp->fr_width - take,
5362 FALSE, FALSE);
5363 take = 0;
5364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
5366 if (run == 0)
5367 frp = frp->fr_next;
5368 else
5369 frp = frp->fr_prev;
5370 }
5371 }
5372 }
5373}
5374#endif /* FEAT_VERTSPLIT */
5375
5376/*
5377 * Check 'winminheight' for a valid value.
5378 */
5379 void
5380win_setminheight()
5381{
5382 int room;
5383 int first = TRUE;
5384 win_T *wp;
5385
5386 /* loop until there is a 'winminheight' that is possible */
5387 while (p_wmh > 0)
5388 {
5389 /* TODO: handle vertical splits */
5390 room = -p_wh;
5391 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5392 room += wp->w_height - p_wmh;
5393 if (room >= 0)
5394 break;
5395 --p_wmh;
5396 if (first)
5397 {
5398 EMSG(_(e_noroom));
5399 first = FALSE;
5400 }
5401 }
5402}
5403
5404#ifdef FEAT_MOUSE
5405
5406/*
5407 * Status line of dragwin is dragged "offset" lines down (negative is up).
5408 */
5409 void
5410win_drag_status_line(dragwin, offset)
5411 win_T *dragwin;
5412 int offset;
5413{
5414 frame_T *curfr;
5415 frame_T *fr;
5416 int room;
5417 int row;
5418 int up; /* if TRUE, drag status line up, otherwise down */
5419 int n;
5420
5421 fr = dragwin->w_frame;
5422 curfr = fr;
5423 if (fr != topframe) /* more than one window */
5424 {
5425 fr = fr->fr_parent;
5426 /* When the parent frame is not a column of frames, its parent should
5427 * be. */
5428 if (fr->fr_layout != FR_COL)
5429 {
5430 curfr = fr;
5431 if (fr != topframe) /* only a row of windows, may drag statusline */
5432 fr = fr->fr_parent;
5433 }
5434 }
5435
5436 /* If this is the last frame in a column, may want to resize the parent
5437 * frame instead (go two up to skip a row of frames). */
5438 while (curfr != topframe && curfr->fr_next == NULL)
5439 {
5440 if (fr != topframe)
5441 fr = fr->fr_parent;
5442 curfr = fr;
5443 if (fr != topframe)
5444 fr = fr->fr_parent;
5445 }
5446
5447 if (offset < 0) /* drag up */
5448 {
5449 up = TRUE;
5450 offset = -offset;
5451 /* sum up the room of the current frame and above it */
5452 if (fr == curfr)
5453 {
5454 /* only one window */
5455 room = fr->fr_height - frame_minheight(fr, NULL);
5456 }
5457 else
5458 {
5459 room = 0;
5460 for (fr = fr->fr_child; ; fr = fr->fr_next)
5461 {
5462 room += fr->fr_height - frame_minheight(fr, NULL);
5463 if (fr == curfr)
5464 break;
5465 }
5466 }
5467 fr = curfr->fr_next; /* put fr at frame that grows */
5468 }
5469 else /* drag down */
5470 {
5471 up = FALSE;
5472 /*
5473 * Only dragging the last status line can reduce p_ch.
5474 */
5475 room = Rows - cmdline_row;
5476 if (curfr->fr_next == NULL)
5477 room -= 1;
5478 else
5479 room -= p_ch;
5480 if (room < 0)
5481 room = 0;
5482 /* sum up the room of frames below of the current one */
5483 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5484 room += fr->fr_height - frame_minheight(fr, NULL);
5485 fr = curfr; /* put fr at window that grows */
5486 }
5487
5488 if (room < offset) /* Not enough room */
5489 offset = room; /* Move as far as we can */
5490 if (offset <= 0)
5491 return;
5492
5493 /*
5494 * Grow frame fr by "offset" lines.
5495 * Doesn't happen when dragging the last status line up.
5496 */
5497 if (fr != NULL)
5498 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5499
5500 if (up)
5501 fr = curfr; /* current frame gets smaller */
5502 else
5503 fr = curfr->fr_next; /* next frame gets smaller */
5504
5505 /*
5506 * Now make the other frames smaller.
5507 */
5508 while (fr != NULL && offset > 0)
5509 {
5510 n = frame_minheight(fr, NULL);
5511 if (fr->fr_height - offset <= n)
5512 {
5513 offset -= fr->fr_height - n;
5514 frame_new_height(fr, n, !up, FALSE);
5515 }
5516 else
5517 {
5518 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5519 break;
5520 }
5521 if (up)
5522 fr = fr->fr_prev;
5523 else
5524 fr = fr->fr_next;
5525 }
5526 row = win_comp_pos();
5527 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5528 cmdline_row = row;
5529 p_ch = Rows - cmdline_row;
5530 if (p_ch < 1)
5531 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005532 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005533 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 showmode();
5535}
5536
5537#ifdef FEAT_VERTSPLIT
5538/*
5539 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5540 */
5541 void
5542win_drag_vsep_line(dragwin, offset)
5543 win_T *dragwin;
5544 int offset;
5545{
5546 frame_T *curfr;
5547 frame_T *fr;
5548 int room;
5549 int left; /* if TRUE, drag separator line left, otherwise right */
5550 int n;
5551
5552 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005553 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 return;
5555 curfr = fr;
5556 fr = fr->fr_parent;
5557 /* When the parent frame is not a row of frames, its parent should be. */
5558 if (fr->fr_layout != FR_ROW)
5559 {
5560 if (fr == topframe) /* only a column of windows (cannot happen?) */
5561 return;
5562 curfr = fr;
5563 fr = fr->fr_parent;
5564 }
5565
5566 /* If this is the last frame in a row, may want to resize a parent
5567 * frame instead. */
5568 while (curfr->fr_next == NULL)
5569 {
5570 if (fr == topframe)
5571 break;
5572 curfr = fr;
5573 fr = fr->fr_parent;
5574 if (fr != topframe)
5575 {
5576 curfr = fr;
5577 fr = fr->fr_parent;
5578 }
5579 }
5580
5581 if (offset < 0) /* drag left */
5582 {
5583 left = TRUE;
5584 offset = -offset;
5585 /* sum up the room of the current frame and left of it */
5586 room = 0;
5587 for (fr = fr->fr_child; ; fr = fr->fr_next)
5588 {
5589 room += fr->fr_width - frame_minwidth(fr, NULL);
5590 if (fr == curfr)
5591 break;
5592 }
5593 fr = curfr->fr_next; /* put fr at frame that grows */
5594 }
5595 else /* drag right */
5596 {
5597 left = FALSE;
5598 /* sum up the room of frames right of the current one */
5599 room = 0;
5600 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5601 room += fr->fr_width - frame_minwidth(fr, NULL);
5602 fr = curfr; /* put fr at window that grows */
5603 }
5604
5605 if (room < offset) /* Not enough room */
5606 offset = room; /* Move as far as we can */
5607 if (offset <= 0) /* No room at all, quit. */
5608 return;
5609
5610 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005611 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
5613 /* shrink other frames: current and at the left or at the right */
5614 if (left)
5615 fr = curfr; /* current frame gets smaller */
5616 else
5617 fr = curfr->fr_next; /* next frame gets smaller */
5618
5619 while (fr != NULL && offset > 0)
5620 {
5621 n = frame_minwidth(fr, NULL);
5622 if (fr->fr_width - offset <= n)
5623 {
5624 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005625 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else
5628 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005629 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 break;
5631 }
5632 if (left)
5633 fr = fr->fr_prev;
5634 else
5635 fr = fr->fr_next;
5636 }
5637 (void)win_comp_pos();
5638 redraw_all_later(NOT_VALID);
5639}
5640#endif /* FEAT_VERTSPLIT */
5641#endif /* FEAT_MOUSE */
5642
5643#endif /* FEAT_WINDOWS */
5644
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005645#define FRACTION_MULT 16384L
5646
5647/*
5648 * Set wp->w_fraction for the current w_wrow and w_height.
5649 */
5650 static void
5651set_fraction(wp)
5652 win_T *wp;
5653{
5654 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005655 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005656}
5657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658/*
5659 * Set the height of a window.
5660 * This takes care of the things inside the window, not what happens to the
5661 * window position, the frame or to other windows.
5662 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005663 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664win_new_height(wp, height)
5665 win_T *wp;
5666 int height;
5667{
5668 linenr_T lnum;
5669 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005670 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 /* Don't want a negative height. Happens when splitting a tiny window.
5673 * Will equalize heights soon to fix it. */
5674 if (height < 0)
5675 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005676 if (wp->w_height == height)
5677 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005679 if (wp->w_height > 0)
5680 {
5681 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005682 /* w_wrow needs to be valid. When setting 'laststatus' this may
5683 * call win_new_height() recursively. */
5684 validate_cursor();
5685 if (wp->w_height != prev_height)
5686 return; /* Recursive call already changed the size, bail out here
5687 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005688 if (wp->w_wrow != wp->w_prev_fraction_row)
5689 set_fraction(wp);
5690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691
5692 wp->w_height = height;
5693 wp->w_skipcol = 0;
5694
5695 /* Don't change w_topline when height is zero. Don't set w_topline when
5696 * 'scrollbind' is set and this isn't the current window. */
5697 if (height > 0
5698#ifdef FEAT_SCROLLBIND
5699 && (!wp->w_p_scb || wp == curwin)
5700#endif
5701 )
5702 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005703 /*
5704 * Find a value for w_topline that shows the cursor at the same
5705 * relative position in the window as before (more or less).
5706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 lnum = wp->w_cursor.lnum;
5708 if (lnum < 1) /* can happen when starting up */
5709 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005710 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5711 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5713 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005714
5715 if (sline >= 0)
5716 {
5717 /* Make sure the whole cursor line is visible, if possible. */
5718 int rows = plines_win(wp, lnum, FALSE);
5719
5720 if (sline > wp->w_height - rows)
5721 {
5722 sline = wp->w_height - rows;
5723 wp->w_wrow -= rows - line_size;
5724 }
5725 }
5726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 if (sline < 0)
5728 {
5729 /*
5730 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005731 * Make cursor line the first line in the window. If not enough
5732 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 */
5734 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005735 if (wp->w_wrow >= wp->w_height
5736 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5737 {
5738 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5739 --wp->w_wrow;
5740 while (wp->w_wrow >= wp->w_height)
5741 {
5742 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5743 + win_col_off2(wp);
5744 --wp->w_wrow;
5745 }
5746 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005747 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005749 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005751 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753#ifdef FEAT_FOLDING
5754 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5755 if (lnum == 1)
5756 {
5757 /* first line in buffer is folded */
5758 line_size = 1;
5759 --sline;
5760 break;
5761 }
5762#endif
5763 --lnum;
5764#ifdef FEAT_DIFF
5765 if (lnum == wp->w_topline)
5766 line_size = plines_win_nofill(wp, lnum, TRUE)
5767 + wp->w_topfill;
5768 else
5769#endif
5770 line_size = plines_win(wp, lnum, TRUE);
5771 sline -= line_size;
5772 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005773
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 if (sline < 0)
5775 {
5776 /*
5777 * Line we want at top would go off top of screen. Use next
5778 * line instead.
5779 */
5780#ifdef FEAT_FOLDING
5781 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5782#endif
5783 lnum++;
5784 wp->w_wrow -= line_size + sline;
5785 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005786 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 /* First line of file reached, use that as topline. */
5789 lnum = 1;
5790 wp->w_wrow -= sline;
5791 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005792
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005793 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
5796
5797 if (wp == curwin)
5798 {
5799 if (p_so)
5800 update_topline();
5801 curs_columns(FALSE); /* validate w_wrow */
5802 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005803 if (prev_height > 0)
5804 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005807 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808#ifdef FEAT_WINDOWS
5809 wp->w_redr_status = TRUE;
5810#endif
5811 invalidate_botline_win(wp);
5812}
5813
5814#ifdef FEAT_VERTSPLIT
5815/*
5816 * Set the width of a window.
5817 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005818 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819win_new_width(wp, width)
5820 win_T *wp;
5821 int width;
5822{
5823 wp->w_width = width;
5824 wp->w_lines_valid = 0;
5825 changed_line_abv_curs_win(wp);
5826 invalidate_botline_win(wp);
5827 if (wp == curwin)
5828 {
5829 update_topline();
5830 curs_columns(TRUE); /* validate w_wrow */
5831 }
5832 redraw_win_later(wp, NOT_VALID);
5833 wp->w_redr_status = TRUE;
5834}
5835#endif
5836
5837 void
5838win_comp_scroll(wp)
5839 win_T *wp;
5840{
5841 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5842 if (wp->w_p_scr == 0)
5843 wp->w_p_scr = 1;
5844}
5845
5846/*
5847 * command_height: called whenever p_ch has been changed
5848 */
5849 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005850command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852#ifdef FEAT_WINDOWS
5853 int h;
5854 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005855 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005857 /* Use the value of p_ch that we remembered. This is needed for when the
5858 * GUI starts up, we can't be sure in what order things happen. And when
5859 * p_ch was changed in another tab page. */
5860 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 /* Find bottom frame with width of screen. */
5863 frp = lastwin->w_frame;
5864# ifdef FEAT_VERTSPLIT
5865 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5866 frp = frp->fr_parent;
5867# endif
5868
5869 /* Avoid changing the height of a window with 'winfixheight' set. */
5870 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5871 && frp->fr_win->w_p_wfh)
5872 frp = frp->fr_prev;
5873
5874 if (starting != NO_SCREEN)
5875 {
5876 cmdline_row = Rows - p_ch;
5877
5878 if (p_ch > old_p_ch) /* p_ch got bigger */
5879 {
5880 while (p_ch > old_p_ch)
5881 {
5882 if (frp == NULL)
5883 {
5884 EMSG(_(e_noroom));
5885 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005886 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 cmdline_row = Rows - p_ch;
5888 break;
5889 }
5890 h = frp->fr_height - frame_minheight(frp, NULL);
5891 if (h > p_ch - old_p_ch)
5892 h = p_ch - old_p_ch;
5893 old_p_ch += h;
5894 frame_add_height(frp, -h);
5895 frp = frp->fr_prev;
5896 }
5897
5898 /* Recompute window positions. */
5899 (void)win_comp_pos();
5900
5901 /* clear the lines added to cmdline */
5902 if (full_screen)
5903 screen_fill((int)(cmdline_row), (int)Rows, 0,
5904 (int)Columns, ' ', ' ', 0);
5905 msg_row = cmdline_row;
5906 redraw_cmdline = TRUE;
5907 return;
5908 }
5909
5910 if (msg_row < cmdline_row)
5911 msg_row = cmdline_row;
5912 redraw_cmdline = TRUE;
5913 }
5914 frame_add_height(frp, (int)(old_p_ch - p_ch));
5915
5916 /* Recompute window positions. */
5917 if (frp != lastwin->w_frame)
5918 (void)win_comp_pos();
5919#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005921 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922#endif
5923}
5924
5925#if defined(FEAT_WINDOWS) || defined(PROTO)
5926/*
5927 * Resize frame "frp" to be "n" lines higher (negative for less high).
5928 * Also resize the frames it is contained in.
5929 */
5930 static void
5931frame_add_height(frp, n)
5932 frame_T *frp;
5933 int n;
5934{
5935 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5936 for (;;)
5937 {
5938 frp = frp->fr_parent;
5939 if (frp == NULL)
5940 break;
5941 frp->fr_height += n;
5942 }
5943}
5944
5945/*
5946 * Add or remove a status line for the bottom window(s), according to the
5947 * value of 'laststatus'.
5948 */
5949 void
5950last_status(morewin)
5951 int morewin; /* pretend there are two or more windows */
5952{
5953 /* Don't make a difference between horizontal or vertical split. */
5954 last_status_rec(topframe, (p_ls == 2
5955 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5956}
5957
5958 static void
5959last_status_rec(fr, statusline)
5960 frame_T *fr;
5961 int statusline;
5962{
5963 frame_T *fp;
5964 win_T *wp;
5965
5966 if (fr->fr_layout == FR_LEAF)
5967 {
5968 wp = fr->fr_win;
5969 if (wp->w_status_height != 0 && !statusline)
5970 {
5971 /* remove status line */
5972 win_new_height(wp, wp->w_height + 1);
5973 wp->w_status_height = 0;
5974 comp_col();
5975 }
5976 else if (wp->w_status_height == 0 && statusline)
5977 {
5978 /* Find a frame to take a line from. */
5979 fp = fr;
5980 while (fp->fr_height <= frame_minheight(fp, NULL))
5981 {
5982 if (fp == topframe)
5983 {
5984 EMSG(_(e_noroom));
5985 return;
5986 }
5987 /* In a column of frames: go to frame above. If already at
5988 * the top or in a row of frames: go to parent. */
5989 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5990 fp = fp->fr_prev;
5991 else
5992 fp = fp->fr_parent;
5993 }
5994 wp->w_status_height = 1;
5995 if (fp != fr)
5996 {
5997 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5998 frame_fix_height(wp);
5999 (void)win_comp_pos();
6000 }
6001 else
6002 win_new_height(wp, wp->w_height - 1);
6003 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006004 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 }
6006 }
6007#ifdef FEAT_VERTSPLIT
6008 else if (fr->fr_layout == FR_ROW)
6009 {
6010 /* vertically split windows, set status line for each one */
6011 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6012 last_status_rec(fp, statusline);
6013 }
6014#endif
6015 else
6016 {
6017 /* horizontally split window, set status line for last one */
6018 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6019 ;
6020 last_status_rec(fp, statusline);
6021 }
6022}
6023
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006024/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006025 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006026 */
6027 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006028tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006029{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006030#ifdef FEAT_GUI_TABLINE
6031 /* When the GUI has the tabline then this always returns zero. */
6032 if (gui_use_tabline())
6033 return 0;
6034#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006035 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006036 {
6037 case 0: return 0;
6038 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6039 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006040 return 1;
6041}
6042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043#endif /* FEAT_WINDOWS */
6044
6045#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6046/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006047 * Get the file name at the cursor.
6048 * If Visual mode is active, use the selected text if it's in one line.
6049 * Returns the name in allocated memory, NULL for failure.
6050 */
6051 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006052grab_file_name(count, file_lnum)
6053 long count;
6054 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006055{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006056 if (VIsual_active)
6057 {
6058 int len;
6059 char_u *ptr;
6060
6061 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6062 return NULL;
6063 return find_file_name_in_path(ptr, len,
6064 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6065 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006066 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6067 file_lnum);
6068
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006069}
6070
6071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 * Return the file name under or after the cursor.
6073 *
6074 * The 'path' option is searched if the file name is not absolute.
6075 * The string returned has been alloc'ed and should be freed by the caller.
6076 * NULL is returned if the file name or file is not found.
6077 *
6078 * options:
6079 * FNAME_MESS give error messages
6080 * FNAME_EXP expand to path
6081 * FNAME_HYP check for hypertext link
6082 * FNAME_INCL apply "includeexpr"
6083 */
6084 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006085file_name_at_cursor(options, count, file_lnum)
6086 int options;
6087 long count;
6088 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
6090 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006091 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6092 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093}
6094
6095/*
6096 * Return the name of the file under or after ptr[col].
6097 * Otherwise like file_name_at_cursor().
6098 */
6099 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006100file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 char_u *line;
6102 int col;
6103 int options;
6104 long count;
6105 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006106 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
6108 char_u *ptr;
6109 int len;
6110
6111 /*
6112 * search forward for what could be the start of a file name
6113 */
6114 ptr = line + col;
6115 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006116 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 if (*ptr == NUL) /* nothing found */
6118 {
6119 if (options & FNAME_MESS)
6120 EMSG(_("E446: No file name under cursor"));
6121 return NULL;
6122 }
6123
6124 /*
6125 * Search backward for first char of the file name.
6126 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6127 */
6128 while (ptr > line)
6129 {
6130#ifdef FEAT_MBYTE
6131 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6132 ptr -= len + 1;
6133 else
6134#endif
6135 if (vim_isfilec(ptr[-1])
6136 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6137 --ptr;
6138 else
6139 break;
6140 }
6141
6142 /*
6143 * Search forward for the last char of the file name.
6144 * Also allow "://" when ':' is not in 'isfname'.
6145 */
6146 len = 0;
6147 while (vim_isfilec(ptr[len])
6148 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6149#ifdef FEAT_MBYTE
6150 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006151 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 else
6153#endif
6154 ++len;
6155
6156 /*
6157 * If there is trailing punctuation, remove it.
6158 * But don't remove "..", could be a directory name.
6159 */
6160 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6161 && ptr[len - 2] != '.')
6162 --len;
6163
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006164 if (file_lnum != NULL)
6165 {
6166 char_u *p;
6167
6168 /* Get the number after the file name and a separator character */
6169 p = ptr + len;
6170 p = skipwhite(p);
6171 if (*p != NUL)
6172 {
6173 if (!isdigit(*p))
6174 ++p; /* skip the separator */
6175 p = skipwhite(p);
6176 if (isdigit(*p))
6177 *file_lnum = (int)getdigits(&p);
6178 }
6179 }
6180
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6182}
6183
6184# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6185static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6186
6187 static char_u *
6188eval_includeexpr(ptr, len)
6189 char_u *ptr;
6190 int len;
6191{
6192 char_u *res;
6193
6194 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006195 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006196 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 set_vim_var_string(VV_FNAME, NULL, 0);
6198 return res;
6199}
6200#endif
6201
6202/*
6203 * Return the name of the file ptr[len] in 'path'.
6204 * Otherwise like file_name_at_cursor().
6205 */
6206 char_u *
6207find_file_name_in_path(ptr, len, options, count, rel_fname)
6208 char_u *ptr;
6209 int len;
6210 int options;
6211 long count;
6212 char_u *rel_fname; /* file we are searching relative to */
6213{
6214 char_u *file_name;
6215 int c;
6216# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6217 char_u *tofree = NULL;
6218
6219 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6220 {
6221 tofree = eval_includeexpr(ptr, len);
6222 if (tofree != NULL)
6223 {
6224 ptr = tofree;
6225 len = (int)STRLEN(ptr);
6226 }
6227 }
6228# endif
6229
6230 if (options & FNAME_EXP)
6231 {
6232 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6233 TRUE, rel_fname);
6234
6235# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6236 /*
6237 * If the file could not be found in a normal way, try applying
6238 * 'includeexpr' (unless done already).
6239 */
6240 if (file_name == NULL
6241 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6242 {
6243 tofree = eval_includeexpr(ptr, len);
6244 if (tofree != NULL)
6245 {
6246 ptr = tofree;
6247 len = (int)STRLEN(ptr);
6248 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6249 TRUE, rel_fname);
6250 }
6251 }
6252# endif
6253 if (file_name == NULL && (options & FNAME_MESS))
6254 {
6255 c = ptr[len];
6256 ptr[len] = NUL;
6257 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6258 ptr[len] = c;
6259 }
6260
6261 /* Repeat finding the file "count" times. This matters when it
6262 * appears several times in the path. */
6263 while (file_name != NULL && --count > 0)
6264 {
6265 vim_free(file_name);
6266 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6267 }
6268 }
6269 else
6270 file_name = vim_strnsave(ptr, len);
6271
6272# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6273 vim_free(tofree);
6274# endif
6275
6276 return file_name;
6277}
6278#endif /* FEAT_SEARCHPATH */
6279
6280/*
6281 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6282 * Also check for ":\\", which MS Internet Explorer accepts, return
6283 * URL_BACKSLASH.
6284 */
6285 static int
6286path_is_url(p)
6287 char_u *p;
6288{
6289 if (STRNCMP(p, "://", (size_t)3) == 0)
6290 return URL_SLASH;
6291 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6292 return URL_BACKSLASH;
6293 return 0;
6294}
6295
6296/*
6297 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6298 * Return URL_BACKSLASH for "name:\\".
6299 * Return zero otherwise.
6300 */
6301 int
6302path_with_url(fname)
6303 char_u *fname;
6304{
6305 char_u *p;
6306
6307 for (p = fname; isalpha(*p); ++p)
6308 ;
6309 return path_is_url(p);
6310}
6311
6312/*
6313 * Return TRUE if "name" is a full (absolute) path name or URL.
6314 */
6315 int
6316vim_isAbsName(name)
6317 char_u *name;
6318{
6319 return (path_with_url(name) != 0 || mch_isFullName(name));
6320}
6321
6322/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006323 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 *
6325 * return FAIL for failure, OK otherwise
6326 */
6327 int
6328vim_FullName(fname, buf, len, force)
6329 char_u *fname, *buf;
6330 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006331 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 int retval = OK;
6334 int url;
6335
6336 *buf = NUL;
6337 if (fname == NULL)
6338 return FAIL;
6339
6340 url = path_with_url(fname);
6341 if (!url)
6342 retval = mch_FullName(fname, buf, len, force);
6343 if (url || retval == FAIL)
6344 {
6345 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006346 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 }
6348#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6349 slash_adjust(buf);
6350#endif
6351 return retval;
6352}
6353
6354/*
6355 * Return the minimal number of rows that is needed on the screen to display
6356 * the current number of windows.
6357 */
6358 int
6359min_rows()
6360{
6361 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006362#ifdef FEAT_WINDOWS
6363 tabpage_T *tp;
6364 int n;
6365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366
6367 if (firstwin == NULL) /* not initialized yet */
6368 return MIN_LINES;
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006371 total = 0;
6372 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6373 {
6374 n = frame_minheight(tp->tp_topframe, NULL);
6375 if (total < n)
6376 total = n;
6377 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006378 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006380 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006382 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 return total;
6384}
6385
6386/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006387 * Return TRUE if there is only one window (in the current tab page), not
6388 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006389 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 */
6391 int
6392only_one_window()
6393{
6394#ifdef FEAT_WINDOWS
6395 int count = 0;
6396 win_T *wp;
6397
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006398 /* If there is another tab page there always is another window. */
6399 if (first_tabpage->tp_next != NULL)
6400 return FALSE;
6401
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006403 if (wp->w_buffer != NULL
6404 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405# ifdef FEAT_QUICKFIX
6406 || wp->w_p_pvw
6407# endif
6408 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006409# ifdef FEAT_AUTOCMD
6410 && wp != aucmd_win
6411# endif
6412 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 ++count;
6414 return (count <= 1);
6415#else
6416 return TRUE;
6417#endif
6418}
6419
6420#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6421/*
6422 * Correct the cursor line number in other windows. Used after changing the
6423 * current buffer, and before applying autocommands.
6424 * When "do_curwin" is TRUE, also check current window.
6425 */
6426 void
6427check_lnums(do_curwin)
6428 int do_curwin;
6429{
6430 win_T *wp;
6431
6432#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006433 tabpage_T *tp;
6434
6435 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6437#else
6438 wp = curwin;
6439 if (do_curwin)
6440#endif
6441 {
6442 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6443 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6444 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6445 wp->w_topline = curbuf->b_ml.ml_line_count;
6446 }
6447}
6448#endif
6449
6450#if defined(FEAT_WINDOWS) || defined(PROTO)
6451
6452/*
6453 * A snapshot of the window sizes, to restore them after closing the help
6454 * window.
6455 * Only these fields are used:
6456 * fr_layout
6457 * fr_width
6458 * fr_height
6459 * fr_next
6460 * fr_child
6461 * fr_win (only valid for the old curwin, NULL otherwise)
6462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464/*
6465 * Create a snapshot of the current frame sizes.
6466 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006467 void
6468make_snapshot(idx)
6469 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006471 clear_snapshot(curtab, idx);
6472 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473}
6474
6475 static void
6476make_snapshot_rec(fr, frp)
6477 frame_T *fr;
6478 frame_T **frp;
6479{
6480 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6481 if (*frp == NULL)
6482 return;
6483 (*frp)->fr_layout = fr->fr_layout;
6484# ifdef FEAT_VERTSPLIT
6485 (*frp)->fr_width = fr->fr_width;
6486# endif
6487 (*frp)->fr_height = fr->fr_height;
6488 if (fr->fr_next != NULL)
6489 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6490 if (fr->fr_child != NULL)
6491 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6492 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6493 (*frp)->fr_win = curwin;
6494}
6495
6496/*
6497 * Remove any existing snapshot.
6498 */
6499 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006500clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006501 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006502 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006504 clear_snapshot_rec(tp->tp_snapshot[idx]);
6505 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506}
6507
6508 static void
6509clear_snapshot_rec(fr)
6510 frame_T *fr;
6511{
6512 if (fr != NULL)
6513 {
6514 clear_snapshot_rec(fr->fr_next);
6515 clear_snapshot_rec(fr->fr_child);
6516 vim_free(fr);
6517 }
6518}
6519
6520/*
6521 * Restore a previously created snapshot, if there is any.
6522 * This is only done if the screen size didn't change and the window layout is
6523 * still the same.
6524 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006525 void
6526restore_snapshot(idx, close_curwin)
6527 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 int close_curwin; /* closing current window */
6529{
6530 win_T *wp;
6531
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006532 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006534 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006536 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6537 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006539 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 win_comp_pos();
6541 if (wp != NULL && close_curwin)
6542 win_goto(wp);
6543 redraw_all_later(CLEAR);
6544 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006545 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546}
6547
6548/*
6549 * Check if frames "sn" and "fr" have the same layout, same following frames
6550 * and same children.
6551 */
6552 static int
6553check_snapshot_rec(sn, fr)
6554 frame_T *sn;
6555 frame_T *fr;
6556{
6557 if (sn->fr_layout != fr->fr_layout
6558 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6559 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6560 || (sn->fr_next != NULL
6561 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6562 || (sn->fr_child != NULL
6563 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6564 return FAIL;
6565 return OK;
6566}
6567
6568/*
6569 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6570 * following frames and children.
6571 * Returns a pointer to the old current window, or NULL.
6572 */
6573 static win_T *
6574restore_snapshot_rec(sn, fr)
6575 frame_T *sn;
6576 frame_T *fr;
6577{
6578 win_T *wp = NULL;
6579 win_T *wp2;
6580
6581 fr->fr_height = sn->fr_height;
6582# ifdef FEAT_VERTSPLIT
6583 fr->fr_width = sn->fr_width;
6584# endif
6585 if (fr->fr_layout == FR_LEAF)
6586 {
6587 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6588# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006589 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590# endif
6591 wp = sn->fr_win;
6592 }
6593 if (sn->fr_next != NULL)
6594 {
6595 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6596 if (wp2 != NULL)
6597 wp = wp2;
6598 }
6599 if (sn->fr_child != NULL)
6600 {
6601 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6602 if (wp2 != NULL)
6603 wp = wp2;
6604 }
6605 return wp;
6606}
6607
6608#endif
6609
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006610#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6611 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006612/*
6613 * Set "win" to be the curwin and "tp" to be the current tab page.
6614 * restore_win() MUST be called to undo.
6615 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006616 * When "no_display" is TRUE the display won't be affected, no redraw is
6617 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006618 * Returns FAIL if switching to "win" failed.
6619 */
6620 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006621switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006622 win_T **save_curwin UNUSED;
6623 tabpage_T **save_curtab UNUSED;
6624 win_T *win UNUSED;
6625 tabpage_T *tp UNUSED;
6626 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006627{
6628# ifdef FEAT_AUTOCMD
6629 block_autocmds();
6630# endif
6631# ifdef FEAT_WINDOWS
6632 *save_curwin = curwin;
6633 if (tp != NULL)
6634 {
6635 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006636 if (no_display)
6637 {
6638 curtab->tp_firstwin = firstwin;
6639 curtab->tp_lastwin = lastwin;
6640 curtab = tp;
6641 firstwin = curtab->tp_firstwin;
6642 lastwin = curtab->tp_lastwin;
6643 }
6644 else
6645 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006646 }
6647 if (!win_valid(win))
6648 {
6649# ifdef FEAT_AUTOCMD
6650 unblock_autocmds();
6651# endif
6652 return FAIL;
6653 }
6654 curwin = win;
6655 curbuf = curwin->w_buffer;
6656# endif
6657 return OK;
6658}
6659
6660/*
6661 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006662 * When "no_display" is TRUE the display won't be affected, no redraw is
6663 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006664 */
6665 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006666restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006667 win_T *save_curwin UNUSED;
6668 tabpage_T *save_curtab UNUSED;
6669 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006670{
6671# ifdef FEAT_WINDOWS
6672 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006673 {
6674 if (no_display)
6675 {
6676 curtab->tp_firstwin = firstwin;
6677 curtab->tp_lastwin = lastwin;
6678 curtab = save_curtab;
6679 firstwin = curtab->tp_firstwin;
6680 lastwin = curtab->tp_lastwin;
6681 }
6682 else
6683 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6684 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006685 if (win_valid(save_curwin))
6686 {
6687 curwin = save_curwin;
6688 curbuf = curwin->w_buffer;
6689 }
6690# endif
6691# ifdef FEAT_AUTOCMD
6692 unblock_autocmds();
6693# endif
6694}
6695
6696/*
6697 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6698 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6699 */
6700 void
6701switch_buffer(save_curbuf, buf)
6702 buf_T *buf;
6703 buf_T **save_curbuf;
6704{
6705# ifdef FEAT_AUTOCMD
6706 block_autocmds();
6707# endif
6708 *save_curbuf = curbuf;
6709 --curbuf->b_nwindows;
6710 curbuf = buf;
6711 curwin->w_buffer = buf;
6712 ++curbuf->b_nwindows;
6713}
6714
6715/*
6716 * Restore the current buffer after using switch_buffer().
6717 */
6718 void
6719restore_buffer(save_curbuf)
6720 buf_T *save_curbuf;
6721{
6722# ifdef FEAT_AUTOCMD
6723 unblock_autocmds();
6724# endif
6725 /* Check for valid buffer, just in case. */
6726 if (buf_valid(save_curbuf))
6727 {
6728 --curbuf->b_nwindows;
6729 curwin->w_buffer = save_curbuf;
6730 curbuf = save_curbuf;
6731 ++curbuf->b_nwindows;
6732 }
6733}
6734#endif
6735
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6737/*
6738 * Return TRUE if there is any vertically split window.
6739 */
6740 int
6741win_hasvertsplit()
6742{
6743 frame_T *fr;
6744
6745 if (topframe->fr_layout == FR_ROW)
6746 return TRUE;
6747
6748 if (topframe->fr_layout == FR_COL)
6749 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6750 if (fr->fr_layout == FR_ROW)
6751 return TRUE;
6752
6753 return FALSE;
6754}
6755#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006756
6757#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6758/*
6759 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006760 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006761 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6762 * If no particular ID is desired, -1 must be specified for 'id'.
6763 * Return ID of added match, -1 on failure.
6764 */
6765 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006766match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006767 win_T *wp;
6768 char_u *grp;
6769 char_u *pat;
6770 int prio;
6771 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006772 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006773{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006774 matchitem_T *cur;
6775 matchitem_T *prev;
6776 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006777 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006778 regprog_T *regprog = NULL;
6779 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006780
Bram Moolenaarb3414592014-06-17 17:48:32 +02006781 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006782 return -1;
6783 if (id < -1 || id == 0)
6784 {
6785 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6786 return -1;
6787 }
6788 if (id != -1)
6789 {
6790 cur = wp->w_match_head;
6791 while (cur != NULL)
6792 {
6793 if (cur->id == id)
6794 {
6795 EMSGN("E801: ID already taken: %ld", id);
6796 return -1;
6797 }
6798 cur = cur->next;
6799 }
6800 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006801 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006802 {
6803 EMSG2(_(e_nogroup), grp);
6804 return -1;
6805 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006806 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006807 {
6808 EMSG2(_(e_invarg2), pat);
6809 return -1;
6810 }
6811
6812 /* Find available match ID. */
6813 while (id == -1)
6814 {
6815 cur = wp->w_match_head;
6816 while (cur != NULL && cur->id != wp->w_next_match_id)
6817 cur = cur->next;
6818 if (cur == NULL)
6819 id = wp->w_next_match_id;
6820 wp->w_next_match_id++;
6821 }
6822
6823 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006824 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006825 m->id = id;
6826 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006827 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006828 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006829 m->match.regprog = regprog;
6830 m->match.rmm_ic = FALSE;
6831 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006832
Bram Moolenaarb3414592014-06-17 17:48:32 +02006833 /* Set up position matches */
6834 if (pos_list != NULL)
6835 {
6836 linenr_T toplnum = 0;
6837 linenr_T botlnum = 0;
6838 listitem_T *li;
6839 int i;
6840
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006841 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006842 i++, li = li->li_next)
6843 {
6844 linenr_T lnum = 0;
6845 colnr_T col = 0;
6846 int len = 1;
6847 list_T *subl;
6848 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006849 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006850
Bram Moolenaarb3414592014-06-17 17:48:32 +02006851 if (li->li_tv.v_type == VAR_LIST)
6852 {
6853 subl = li->li_tv.vval.v_list;
6854 if (subl == NULL)
6855 goto fail;
6856 subli = subl->lv_first;
6857 if (subli == NULL)
6858 goto fail;
6859 lnum = get_tv_number_chk(&subli->li_tv, &error);
6860 if (error == TRUE)
6861 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006862 if (lnum == 0)
6863 {
6864 --i;
6865 continue;
6866 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006867 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006868 subli = subli->li_next;
6869 if (subli != NULL)
6870 {
6871 col = get_tv_number_chk(&subli->li_tv, &error);
6872 if (error == TRUE)
6873 goto fail;
6874 subli = subli->li_next;
6875 if (subli != NULL)
6876 {
6877 len = get_tv_number_chk(&subli->li_tv, &error);
6878 if (error == TRUE)
6879 goto fail;
6880 }
6881 }
6882 m->pos.pos[i].col = col;
6883 m->pos.pos[i].len = len;
6884 }
6885 else if (li->li_tv.v_type == VAR_NUMBER)
6886 {
6887 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006888 {
6889 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006890 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006891 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006892 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6893 m->pos.pos[i].col = 0;
6894 m->pos.pos[i].len = 0;
6895 }
6896 else
6897 {
6898 EMSG(_("List or number required"));
6899 goto fail;
6900 }
6901 if (toplnum == 0 || lnum < toplnum)
6902 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006903 if (botlnum == 0 || lnum >= botlnum)
6904 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006905 }
6906
6907 /* Calculate top and bottom lines for redrawing area */
6908 if (toplnum != 0)
6909 {
6910 if (wp->w_buffer->b_mod_set)
6911 {
6912 if (wp->w_buffer->b_mod_top > toplnum)
6913 wp->w_buffer->b_mod_top = toplnum;
6914 if (wp->w_buffer->b_mod_bot < botlnum)
6915 wp->w_buffer->b_mod_bot = botlnum;
6916 }
6917 else
6918 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006919 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006920 wp->w_buffer->b_mod_top = toplnum;
6921 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006922 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006923 }
6924 m->pos.toplnum = toplnum;
6925 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006926 rtype = VALID;
6927 }
6928 }
6929
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006930 /* Insert new match. The match list is in ascending order with regard to
6931 * the match priorities. */
6932 cur = wp->w_match_head;
6933 prev = cur;
6934 while (cur != NULL && prio >= cur->priority)
6935 {
6936 prev = cur;
6937 cur = cur->next;
6938 }
6939 if (cur == prev)
6940 wp->w_match_head = m;
6941 else
6942 prev->next = m;
6943 m->next = cur;
6944
Bram Moolenaarb3414592014-06-17 17:48:32 +02006945 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006946 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006947
6948fail:
6949 vim_free(m);
6950 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006951}
6952
6953/*
6954 * Delete match with ID 'id' in the match list of window 'wp'.
6955 * Print error messages if 'perr' is TRUE.
6956 */
6957 int
6958match_delete(wp, id, perr)
6959 win_T *wp;
6960 int id;
6961 int perr;
6962{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006963 matchitem_T *cur = wp->w_match_head;
6964 matchitem_T *prev = cur;
6965 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006966
6967 if (id < 1)
6968 {
6969 if (perr == TRUE)
6970 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6971 id);
6972 return -1;
6973 }
6974 while (cur != NULL && cur->id != id)
6975 {
6976 prev = cur;
6977 cur = cur->next;
6978 }
6979 if (cur == NULL)
6980 {
6981 if (perr == TRUE)
6982 EMSGN("E803: ID not found: %ld", id);
6983 return -1;
6984 }
6985 if (cur == prev)
6986 wp->w_match_head = cur->next;
6987 else
6988 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006989 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006990 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006991 if (cur->pos.toplnum != 0)
6992 {
6993 if (wp->w_buffer->b_mod_set)
6994 {
6995 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6996 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6997 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6998 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6999 }
7000 else
7001 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007002 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007003 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7004 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007005 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007006 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007007 rtype = VALID;
7008 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007009 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007010 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007011 return 0;
7012}
7013
7014/*
7015 * Delete all matches in the match list of window 'wp'.
7016 */
7017 void
7018clear_matches(wp)
7019 win_T *wp;
7020{
7021 matchitem_T *m;
7022
7023 while (wp->w_match_head != NULL)
7024 {
7025 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007026 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007027 vim_free(wp->w_match_head->pattern);
7028 vim_free(wp->w_match_head);
7029 wp->w_match_head = m;
7030 }
7031 redraw_later(SOME_VALID);
7032}
7033
7034/*
7035 * Get match from ID 'id' in window 'wp'.
7036 * Return NULL if match not found.
7037 */
7038 matchitem_T *
7039get_match(wp, id)
7040 win_T *wp;
7041 int id;
7042{
7043 matchitem_T *cur = wp->w_match_head;
7044
7045 while (cur != NULL && cur->id != id)
7046 cur = cur->next;
7047 return cur;
7048}
7049#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007050
7051#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7052 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007053get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007054{
7055 int i = 1;
7056 win_T *w;
7057
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007058 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007059 ++i;
7060
7061 if (w == NULL)
7062 return 0;
7063 else
7064 return i;
7065}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007066
7067 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007068get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007069{
7070 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007071# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007072 tabpage_T *t;
7073
7074 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7075 ++i;
7076
7077 if (t == NULL)
7078 return 0;
7079 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007080# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007081 return i;
7082}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007083#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007084
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007085#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007086/*
7087 * Return TRUE if "topfrp" and its children are at the right height.
7088 */
7089 static int
7090frame_check_height(topfrp, height)
7091 frame_T *topfrp;
7092 int height;
7093{
7094 frame_T *frp;
7095
7096 if (topfrp->fr_height != height)
7097 return FALSE;
7098
7099 if (topfrp->fr_layout == FR_ROW)
7100 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7101 if (frp->fr_height != height)
7102 return FALSE;
7103
7104 return TRUE;
7105}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007106#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007107
7108#ifdef FEAT_VERTSPLIT
7109/*
7110 * Return TRUE if "topfrp" and its children are at the right width.
7111 */
7112 static int
7113frame_check_width(topfrp, width)
7114 frame_T *topfrp;
7115 int width;
7116{
7117 frame_T *frp;
7118
7119 if (topfrp->fr_width != width)
7120 return FALSE;
7121
7122 if (topfrp->fr_layout == FR_COL)
7123 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7124 if (frp->fr_width != width)
7125 return FALSE;
7126
7127 return TRUE;
7128}
7129#endif
7130