blob: 485f7ef04c847554f944bdd62dc7e799024faa2d [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;
689
690 if (flags & WSP_TOP)
691 oldwin = firstwin;
692 else if (flags & WSP_BOT)
693 oldwin = lastwin;
694 else
695 oldwin = curwin;
696
697 /* add a status line when p_ls == 1 and splitting the first window */
698 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
699 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100700 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {
702 EMSG(_(e_noroom));
703 return FAIL;
704 }
705 need_status = STATUS_HEIGHT;
706 }
707
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000708#ifdef FEAT_GUI
709 /* May be needed for the scrollbars that are going to change. */
710 if (gui.in_use)
711 out_flush();
712#endif
713
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#ifdef FEAT_VERTSPLIT
715 if (flags & WSP_VERT)
716 {
717 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718
719 /*
720 * Check if we are able to split the current window and compute its
721 * width.
722 */
723 needed = p_wmw + 1;
724 if (flags & WSP_ROOM)
725 needed += p_wiw - p_wmw;
726 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
727 {
728 available = topframe->fr_width;
729 needed += frame_minwidth(topframe, NULL);
730 }
731 else
732 available = oldwin->w_width;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100733 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
735 EMSG(_(e_noroom));
736 return FAIL;
737 }
738 if (new_size == 0)
739 new_size = oldwin->w_width / 2;
740 if (new_size > oldwin->w_width - p_wmw - 1)
741 new_size = oldwin->w_width - p_wmw - 1;
742 if (new_size < p_wmw)
743 new_size = p_wmw;
744
745 /* if it doesn't fit in the current window, need win_equal() */
746 if (oldwin->w_width - new_size - 1 < p_wmw)
747 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000748
749 /* We don't like to take lines for the new window from a
750 * 'winfixwidth' window. Take them from a window to the left or right
751 * instead, if possible. */
752 if (oldwin->w_p_wfw)
753 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000754
755 /* Only make all windows the same width if one of them (except oldwin)
756 * is wider than one of the split windows. */
757 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
758 && oldwin->w_frame->fr_parent != NULL)
759 {
760 frp = oldwin->w_frame->fr_parent->fr_child;
761 while (frp != NULL)
762 {
763 if (frp->fr_win != oldwin && frp->fr_win != NULL
764 && (frp->fr_win->w_width > new_size
765 || frp->fr_win->w_width > oldwin->w_width
766 - new_size - STATUS_HEIGHT))
767 {
768 do_equal = TRUE;
769 break;
770 }
771 frp = frp->fr_next;
772 }
773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
775 else
776#endif
777 {
778 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 /*
781 * Check if we are able to split the current window and compute its
782 * height.
783 */
784 needed = p_wmh + STATUS_HEIGHT + need_status;
785 if (flags & WSP_ROOM)
786 needed += p_wh - p_wmh;
787 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
788 {
789 available = topframe->fr_height;
790 needed += frame_minheight(topframe, NULL);
791 }
792 else
793 {
794 available = oldwin->w_height;
795 needed += p_wmh;
796 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100797 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
799 EMSG(_(e_noroom));
800 return FAIL;
801 }
802 oldwin_height = oldwin->w_height;
803 if (need_status)
804 {
805 oldwin->w_status_height = STATUS_HEIGHT;
806 oldwin_height -= STATUS_HEIGHT;
807 }
808 if (new_size == 0)
809 new_size = oldwin_height / 2;
810
811 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
812 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
813 if (new_size < p_wmh)
814 new_size = p_wmh;
815
816 /* if it doesn't fit in the current window, need win_equal() */
817 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
818 do_equal = TRUE;
819
820 /* We don't like to take lines for the new window from a
821 * 'winfixheight' window. Take them from a window above or below
822 * instead, if possible. */
823 if (oldwin->w_p_wfh)
824 {
825 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
826 oldwin);
827 oldwin_height = oldwin->w_height;
828 if (need_status)
829 oldwin_height -= STATUS_HEIGHT;
830 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000831
832 /* Only make all windows the same height if one of them (except oldwin)
833 * is higher than one of the split windows. */
834 if (!do_equal && p_ea && size == 0
835#ifdef FEAT_VERTSPLIT
836 && *p_ead != 'h'
837#endif
838 && oldwin->w_frame->fr_parent != NULL)
839 {
840 frp = oldwin->w_frame->fr_parent->fr_child;
841 while (frp != NULL)
842 {
843 if (frp->fr_win != oldwin && frp->fr_win != NULL
844 && (frp->fr_win->w_height > new_size
845 || frp->fr_win->w_height > oldwin_height - new_size
846 - STATUS_HEIGHT))
847 {
848 do_equal = TRUE;
849 break;
850 }
851 frp = frp->fr_next;
852 }
853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855
856 /*
857 * allocate new window structure and link it in the window list
858 */
859 if ((flags & WSP_TOP) == 0
860 && ((flags & WSP_BOT)
861 || (flags & WSP_BELOW)
862 || (!(flags & WSP_ABOVE)
863 && (
864#ifdef FEAT_VERTSPLIT
865 (flags & WSP_VERT) ? p_spr :
866#endif
867 p_sb))))
868 {
869 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100870 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000871 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 else
873 win_append(oldwin, wp);
874 }
875 else
876 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100877 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000878 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 else
880 win_append(oldwin->w_prev, wp);
881 }
882
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100883 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 {
885 if (wp == NULL)
886 return FAIL;
887
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000888 new_frame(wp);
889 if (wp->w_frame == NULL)
890 {
891 win_free(wp, NULL);
892 return FAIL;
893 }
894
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000895 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000896 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898
899 /*
900 * Reorganise the tree of frames to insert the new window.
901 */
902 if (flags & (WSP_TOP | WSP_BOT))
903 {
904#ifdef FEAT_VERTSPLIT
905 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
906 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
907#else
908 if (topframe->fr_layout == FR_COL)
909#endif
910 {
911 curfrp = topframe->fr_child;
912 if (flags & WSP_BOT)
913 while (curfrp->fr_next != NULL)
914 curfrp = curfrp->fr_next;
915 }
916 else
917 curfrp = topframe;
918 before = (flags & WSP_TOP);
919 }
920 else
921 {
922 curfrp = oldwin->w_frame;
923 if (flags & WSP_BELOW)
924 before = FALSE;
925 else if (flags & WSP_ABOVE)
926 before = TRUE;
927 else
928#ifdef FEAT_VERTSPLIT
929 if (flags & WSP_VERT)
930 before = !p_spr;
931 else
932#endif
933 before = !p_sb;
934 }
935 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
936 {
937 /* Need to create a new frame in the tree to make a branch. */
938 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
939 *frp = *curfrp;
940 curfrp->fr_layout = layout;
941 frp->fr_parent = curfrp;
942 frp->fr_next = NULL;
943 frp->fr_prev = NULL;
944 curfrp->fr_child = frp;
945 curfrp->fr_win = NULL;
946 curfrp = frp;
947 if (frp->fr_win != NULL)
948 oldwin->w_frame = frp;
949 else
950 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
951 frp->fr_parent = curfrp;
952 }
953
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100954 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000955 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100957 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 frp->fr_parent = curfrp->fr_parent;
959
960 /* Insert the new frame at the right place in the frame list. */
961 if (before)
962 frame_insert(curfrp, frp);
963 else
964 frame_append(curfrp, frp);
965
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100966 /* Set w_fraction now so that the cursor keeps the same relative
967 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100968 if (oldwin->w_height > 0)
969 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100970 wp->w_fraction = oldwin->w_fraction;
971
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972#ifdef FEAT_VERTSPLIT
973 if (flags & WSP_VERT)
974 {
975 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (need_status)
978 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100979 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 oldwin->w_status_height = need_status;
981 }
982 if (flags & (WSP_TOP | WSP_BOT))
983 {
984 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000985 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100986 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 wp->w_status_height = (p_ls > 0);
988 }
989 else
990 {
991 /* height and row of new window is same as current window */
992 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100993 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 wp->w_status_height = oldwin->w_status_height;
995 }
996 frp->fr_height = curfrp->fr_height;
997
998 /* "new_size" of the current window goes to the new window, use
999 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001000 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 if (before)
1002 wp->w_vsep_width = 1;
1003 else
1004 {
1005 wp->w_vsep_width = oldwin->w_vsep_width;
1006 oldwin->w_vsep_width = 1;
1007 }
1008 if (flags & (WSP_TOP | WSP_BOT))
1009 {
1010 if (flags & WSP_BOT)
1011 frame_add_vsep(curfrp);
1012 /* Set width of neighbor frame */
1013 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001014 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1015 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001018 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (before) /* new window left of current one */
1020 {
1021 wp->w_wincol = oldwin->w_wincol;
1022 oldwin->w_wincol += new_size + 1;
1023 }
1024 else /* new window right of current one */
1025 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1026 frame_fix_width(oldwin);
1027 frame_fix_width(wp);
1028 }
1029 else
1030#endif
1031 {
1032 /* width and column of new window is same as current window */
1033#ifdef FEAT_VERTSPLIT
1034 if (flags & (WSP_TOP | WSP_BOT))
1035 {
1036 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001037 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 wp->w_vsep_width = 0;
1039 }
1040 else
1041 {
1042 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001043 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 wp->w_vsep_width = oldwin->w_vsep_width;
1045 }
1046 frp->fr_width = curfrp->fr_width;
1047#endif
1048
1049 /* "new_size" of the current window goes to the new window, use
1050 * one row for the status line */
1051 win_new_height(wp, new_size);
1052 if (flags & (WSP_TOP | WSP_BOT))
1053 frame_new_height(curfrp, curfrp->fr_height
1054 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1055 else
1056 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1057 if (before) /* new window above current one */
1058 {
1059 wp->w_winrow = oldwin->w_winrow;
1060 wp->w_status_height = STATUS_HEIGHT;
1061 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1062 }
1063 else /* new window below current one */
1064 {
1065 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1066 wp->w_status_height = oldwin->w_status_height;
1067 oldwin->w_status_height = STATUS_HEIGHT;
1068 }
1069#ifdef FEAT_VERTSPLIT
1070 if (flags & WSP_BOT)
1071 frame_add_statusline(curfrp);
1072#endif
1073 frame_fix_height(wp);
1074 frame_fix_height(oldwin);
1075 }
1076
1077 if (flags & (WSP_TOP | WSP_BOT))
1078 (void)win_comp_pos();
1079
1080 /*
1081 * Both windows need redrawing
1082 */
1083 redraw_win_later(wp, NOT_VALID);
1084 wp->w_redr_status = TRUE;
1085 redraw_win_later(oldwin, NOT_VALID);
1086 oldwin->w_redr_status = TRUE;
1087
1088 if (need_status)
1089 {
1090 msg_row = Rows - 1;
1091 msg_col = sc_col;
1092 msg_clr_eos_force(); /* Old command/ruler may still be there */
1093 comp_col();
1094 msg_row = Rows - 1;
1095 msg_col = 0; /* put position back at start of line */
1096 }
1097
1098 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001099 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 */
1101 if (do_equal || dir != 0)
1102 win_equal(wp, TRUE,
1103#ifdef FEAT_VERTSPLIT
1104 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1105 : dir == 'h' ? 'b' :
1106#endif
1107 'v');
1108
1109 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1110 * size was given. */
1111#ifdef FEAT_VERTSPLIT
1112 if (flags & WSP_VERT)
1113 {
1114 i = p_wiw;
1115 if (size != 0)
1116 p_wiw = size;
1117
1118# ifdef FEAT_GUI
1119 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1120 if (gui.in_use)
1121 gui_init_which_components(NULL);
1122# endif
1123 }
1124 else
1125#endif
1126 {
1127 i = p_wh;
1128 if (size != 0)
1129 p_wh = size;
1130 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001131
1132 /*
1133 * make the new window the current window
1134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 win_enter(wp, FALSE);
1136#ifdef FEAT_VERTSPLIT
1137 if (flags & WSP_VERT)
1138 p_wiw = i;
1139 else
1140#endif
1141 p_wh = i;
1142
1143 return OK;
1144}
1145
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001146
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001147/*
1148 * Initialize window "newp" from window "oldp".
1149 * Used when splitting a window and when creating a new tab page.
1150 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001151 * WSP_NEWLOC may be specified in flags to prevent the location list from
1152 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001153 */
1154 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001155win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001156 win_T *newp;
1157 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001158 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001159{
1160 int i;
1161
1162 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001163#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001164 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001165#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001166 oldp->w_buffer->b_nwindows++;
1167 newp->w_cursor = oldp->w_cursor;
1168 newp->w_valid = 0;
1169 newp->w_curswant = oldp->w_curswant;
1170 newp->w_set_curswant = oldp->w_set_curswant;
1171 newp->w_topline = oldp->w_topline;
1172#ifdef FEAT_DIFF
1173 newp->w_topfill = oldp->w_topfill;
1174#endif
1175 newp->w_leftcol = oldp->w_leftcol;
1176 newp->w_pcmark = oldp->w_pcmark;
1177 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1178 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001179 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001180 newp->w_fraction = oldp->w_fraction;
1181 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1182#ifdef FEAT_JUMPLIST
1183 copy_jumplist(oldp, newp);
1184#endif
1185#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001186 if (flags & WSP_NEWLOC)
1187 {
1188 /* Don't copy the location list. */
1189 newp->w_llist = NULL;
1190 newp->w_llist_ref = NULL;
1191 }
1192 else
1193 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001194#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001195 newp->w_localdir = (oldp->w_localdir == NULL)
1196 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001197
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001198 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001199 for (i = 0; i < oldp->w_tagstacklen; i++)
1200 {
1201 newp->w_tagstack[i] = oldp->w_tagstack[i];
1202 if (newp->w_tagstack[i].tagname != NULL)
1203 newp->w_tagstack[i].tagname =
1204 vim_strsave(newp->w_tagstack[i].tagname);
1205 }
1206 newp->w_tagstackidx = oldp->w_tagstackidx;
1207 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001208#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001209 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001210#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001211
1212 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001213
Bram Moolenaara971b822011-09-14 14:43:25 +02001214#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001215 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001216#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001217}
1218
1219/*
1220 * Initialize window "newp" from window"old".
1221 * Only the essential things are copied.
1222 */
1223 static void
1224win_init_some(newp, oldp)
1225 win_T *newp;
1226 win_T *oldp;
1227{
1228 /* Use the same argument list. */
1229 newp->w_alist = oldp->w_alist;
1230 ++newp->w_alist->al_refcount;
1231 newp->w_arg_idx = oldp->w_arg_idx;
1232
1233 /* copy options from existing window */
1234 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001235}
1236
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237#endif /* FEAT_WINDOWS */
1238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#if defined(FEAT_WINDOWS) || defined(PROTO)
1240/*
1241 * Check if "win" is a pointer to an existing window.
1242 */
1243 int
1244win_valid(win)
1245 win_T *win;
1246{
1247 win_T *wp;
1248
1249 if (win == NULL)
1250 return FALSE;
1251 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1252 if (wp == win)
1253 return TRUE;
1254 return FALSE;
1255}
1256
1257/*
1258 * Return the number of windows.
1259 */
1260 int
1261win_count()
1262{
1263 win_T *wp;
1264 int count = 0;
1265
1266 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1267 ++count;
1268 return count;
1269}
1270
1271/*
1272 * Make "count" windows on the screen.
1273 * Return actual number of windows on the screen.
1274 * Must be called when there is just one window, filling the whole screen
1275 * (excluding the command line).
1276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 int
1278make_windows(count, vertical)
1279 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001280 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281{
1282 int maxcount;
1283 int todo;
1284
1285#ifdef FEAT_VERTSPLIT
1286 if (vertical)
1287 {
1288 /* Each windows needs at least 'winminwidth' lines and a separator
1289 * column. */
1290 maxcount = (curwin->w_width + curwin->w_vsep_width
1291 - (p_wiw - p_wmw)) / (p_wmw + 1);
1292 }
1293 else
1294#endif
1295 {
1296 /* Each window needs at least 'winminheight' lines and a status line. */
1297 maxcount = (curwin->w_height + curwin->w_status_height
1298 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1299 }
1300
1301 if (maxcount < 2)
1302 maxcount = 2;
1303 if (count > maxcount)
1304 count = maxcount;
1305
1306 /*
1307 * add status line now, otherwise first window will be too big
1308 */
1309 if (count > 1)
1310 last_status(TRUE);
1311
1312#ifdef FEAT_AUTOCMD
1313 /*
1314 * Don't execute autocommands while creating the windows. Must do that
1315 * when putting the buffers in the windows.
1316 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001317 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#endif
1319
1320 /* todo is number of windows left to create */
1321 for (todo = count - 1; todo > 0; --todo)
1322#ifdef FEAT_VERTSPLIT
1323 if (vertical)
1324 {
1325 if (win_split(curwin->w_width - (curwin->w_width - todo)
1326 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1327 break;
1328 }
1329 else
1330#endif
1331 {
1332 if (win_split(curwin->w_height - (curwin->w_height - todo
1333 * STATUS_HEIGHT) / (todo + 1)
1334 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1335 break;
1336 }
1337
1338#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001339 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#endif
1341
1342 /* return actual number of windows */
1343 return (count - todo);
1344}
1345
1346/*
1347 * Exchange current and next window
1348 */
1349 static void
1350win_exchange(Prenum)
1351 long Prenum;
1352{
1353 frame_T *frp;
1354 frame_T *frp2;
1355 win_T *wp;
1356 win_T *wp2;
1357 int temp;
1358
1359 if (lastwin == firstwin) /* just one window */
1360 {
1361 beep_flush();
1362 return;
1363 }
1364
1365#ifdef FEAT_GUI
1366 need_mouse_correct = TRUE;
1367#endif
1368
1369 /*
1370 * find window to exchange with
1371 */
1372 if (Prenum)
1373 {
1374 frp = curwin->w_frame->fr_parent->fr_child;
1375 while (frp != NULL && --Prenum > 0)
1376 frp = frp->fr_next;
1377 }
1378 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1379 frp = curwin->w_frame->fr_next;
1380 else /* Swap last window in row/col with previous */
1381 frp = curwin->w_frame->fr_prev;
1382
1383 /* We can only exchange a window with another window, not with a frame
1384 * containing windows. */
1385 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1386 return;
1387 wp = frp->fr_win;
1388
1389/*
1390 * 1. remove curwin from the list. Remember after which window it was in wp2
1391 * 2. insert curwin before wp in the list
1392 * if wp != wp2
1393 * 3. remove wp from the list
1394 * 4. insert wp after wp2
1395 * 5. exchange the status line height and vsep width.
1396 */
1397 wp2 = curwin->w_prev;
1398 frp2 = curwin->w_frame->fr_prev;
1399 if (wp->w_prev != curwin)
1400 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001401 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 frame_remove(curwin->w_frame);
1403 win_append(wp->w_prev, curwin);
1404 frame_insert(frp, curwin->w_frame);
1405 }
1406 if (wp != wp2)
1407 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001408 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 frame_remove(wp->w_frame);
1410 win_append(wp2, wp);
1411 if (frp2 == NULL)
1412 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1413 else
1414 frame_append(frp2, wp->w_frame);
1415 }
1416 temp = curwin->w_status_height;
1417 curwin->w_status_height = wp->w_status_height;
1418 wp->w_status_height = temp;
1419#ifdef FEAT_VERTSPLIT
1420 temp = curwin->w_vsep_width;
1421 curwin->w_vsep_width = wp->w_vsep_width;
1422 wp->w_vsep_width = temp;
1423
1424 /* If the windows are not in the same frame, exchange the sizes to avoid
1425 * messing up the window layout. Otherwise fix the frame sizes. */
1426 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1427 {
1428 temp = curwin->w_height;
1429 curwin->w_height = wp->w_height;
1430 wp->w_height = temp;
1431 temp = curwin->w_width;
1432 curwin->w_width = wp->w_width;
1433 wp->w_width = temp;
1434 }
1435 else
1436 {
1437 frame_fix_height(curwin);
1438 frame_fix_height(wp);
1439 frame_fix_width(curwin);
1440 frame_fix_width(wp);
1441 }
1442#endif
1443
1444 (void)win_comp_pos(); /* recompute window positions */
1445
1446 win_enter(wp, TRUE);
1447 redraw_later(CLEAR);
1448}
1449
1450/*
1451 * rotate windows: if upwards TRUE the second window becomes the first one
1452 * if upwards FALSE the first window becomes the second one
1453 */
1454 static void
1455win_rotate(upwards, count)
1456 int upwards;
1457 int count;
1458{
1459 win_T *wp1;
1460 win_T *wp2;
1461 frame_T *frp;
1462 int n;
1463
1464 if (firstwin == lastwin) /* nothing to do */
1465 {
1466 beep_flush();
1467 return;
1468 }
1469
1470#ifdef FEAT_GUI
1471 need_mouse_correct = TRUE;
1472#endif
1473
1474#ifdef FEAT_VERTSPLIT
1475 /* Check if all frames in this row/col have one window. */
1476 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1477 frp = frp->fr_next)
1478 if (frp->fr_win == NULL)
1479 {
1480 EMSG(_("E443: Cannot rotate when another window is split"));
1481 return;
1482 }
1483#endif
1484
1485 while (count--)
1486 {
1487 if (upwards) /* first window becomes last window */
1488 {
1489 /* remove first window/frame from the list */
1490 frp = curwin->w_frame->fr_parent->fr_child;
1491 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001492 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 frame_remove(frp);
1494
1495 /* find last frame and append removed window/frame after it */
1496 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1497 ;
1498 win_append(frp->fr_win, wp1);
1499 frame_append(frp, wp1->w_frame);
1500
1501 wp2 = frp->fr_win; /* previously last window */
1502 }
1503 else /* last window becomes first window */
1504 {
1505 /* find last window/frame in the list and remove it */
1506 for (frp = curwin->w_frame; frp->fr_next != NULL;
1507 frp = frp->fr_next)
1508 ;
1509 wp1 = frp->fr_win;
1510 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001511 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 frame_remove(frp);
1513
1514 /* append the removed window/frame before the first in the list */
1515 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1516 frame_insert(frp->fr_parent->fr_child, frp);
1517 }
1518
1519 /* exchange status height and vsep width of old and new last window */
1520 n = wp2->w_status_height;
1521 wp2->w_status_height = wp1->w_status_height;
1522 wp1->w_status_height = n;
1523 frame_fix_height(wp1);
1524 frame_fix_height(wp2);
1525#ifdef FEAT_VERTSPLIT
1526 n = wp2->w_vsep_width;
1527 wp2->w_vsep_width = wp1->w_vsep_width;
1528 wp1->w_vsep_width = n;
1529 frame_fix_width(wp1);
1530 frame_fix_width(wp2);
1531#endif
1532
1533 /* recompute w_winrow and w_wincol for all windows */
1534 (void)win_comp_pos();
1535 }
1536
1537 redraw_later(CLEAR);
1538}
1539
1540/*
1541 * Move the current window to the very top/bottom/left/right of the screen.
1542 */
1543 static void
1544win_totop(size, flags)
1545 int size;
1546 int flags;
1547{
1548 int dir;
1549 int height = curwin->w_height;
1550
1551 if (lastwin == firstwin)
1552 {
1553 beep_flush();
1554 return;
1555 }
1556
1557 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001558 (void)winframe_remove(curwin, &dir, NULL);
1559 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 last_status(FALSE); /* may need to remove last status line */
1561 (void)win_comp_pos(); /* recompute window positions */
1562
1563 /* Split a window on the desired side and put the window there. */
1564 (void)win_split_ins(size, flags, curwin, dir);
1565 if (!(flags & WSP_VERT))
1566 {
1567 win_setheight(height);
1568 if (p_ea)
1569 win_equal(curwin, TRUE, 'v');
1570 }
1571
1572#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1573 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1574 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001575 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577}
1578
1579/*
1580 * Move window "win1" to below/right of "win2" and make "win1" the current
1581 * window. Only works within the same frame!
1582 */
1583 void
1584win_move_after(win1, win2)
1585 win_T *win1, *win2;
1586{
1587 int height;
1588
1589 /* check if the arguments are reasonable */
1590 if (win1 == win2)
1591 return;
1592
1593 /* check if there is something to do */
1594 if (win2->w_next != win1)
1595 {
1596 /* may need move the status line/vertical separator of the last window
1597 * */
1598 if (win1 == lastwin)
1599 {
1600 height = win1->w_prev->w_status_height;
1601 win1->w_prev->w_status_height = win1->w_status_height;
1602 win1->w_status_height = height;
1603#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001604 if (win1->w_prev->w_vsep_width == 1)
1605 {
1606 /* Remove the vertical separator from the last-but-one window,
1607 * add it to the last window. Adjust the frame widths. */
1608 win1->w_prev->w_vsep_width = 0;
1609 win1->w_prev->w_frame->fr_width -= 1;
1610 win1->w_vsep_width = 1;
1611 win1->w_frame->fr_width += 1;
1612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613#endif
1614 }
1615 else if (win2 == lastwin)
1616 {
1617 height = win1->w_status_height;
1618 win1->w_status_height = win2->w_status_height;
1619 win2->w_status_height = height;
1620#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001621 if (win1->w_vsep_width == 1)
1622 {
1623 /* Remove the vertical separator from win1, add it to the last
1624 * window, win2. Adjust the frame widths. */
1625 win2->w_vsep_width = 1;
1626 win2->w_frame->fr_width += 1;
1627 win1->w_vsep_width = 0;
1628 win1->w_frame->fr_width -= 1;
1629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001632 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 frame_remove(win1->w_frame);
1634 win_append(win2, win1);
1635 frame_append(win2->w_frame, win1->w_frame);
1636
1637 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1638 redraw_later(NOT_VALID);
1639 }
1640 win_enter(win1, FALSE);
1641}
1642
1643/*
1644 * Make all windows the same height.
1645 * 'next_curwin' will soon be the current window, make sure it has enough
1646 * rows.
1647 */
1648 void
1649win_equal(next_curwin, current, dir)
1650 win_T *next_curwin; /* pointer to current window to be or NULL */
1651 int current; /* do only frame with current window */
1652 int dir; /* 'v' for vertically, 'h' for horizontally,
1653 'b' for both, 0 for using p_ead */
1654{
1655 if (dir == 0)
1656#ifdef FEAT_VERTSPLIT
1657 dir = *p_ead;
1658#else
1659 dir = 'b';
1660#endif
1661 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001662 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001663 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664}
1665
1666/*
1667 * Set a frame to a new position and height, spreading the available room
1668 * equally over contained frames.
1669 * The window "next_curwin" (if not NULL) should at least get the size from
1670 * 'winheight' and 'winwidth' if possible.
1671 */
1672 static void
1673win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1674 win_T *next_curwin; /* pointer to current window to be or NULL */
1675 int current; /* do only frame with current window */
1676 frame_T *topfr; /* frame to set size off */
1677 int dir; /* 'v', 'h' or 'b', see win_equal() */
1678 int col; /* horizontal position for frame */
1679 int row; /* vertical position for frame */
1680 int width; /* new width of frame */
1681 int height; /* new height of frame */
1682{
1683 int n, m;
1684 int extra_sep = 0;
1685 int wincount, totwincount = 0;
1686 frame_T *fr;
1687 int next_curwin_size = 0;
1688 int room = 0;
1689 int new_size;
1690 int has_next_curwin = 0;
1691 int hnc;
1692
1693 if (topfr->fr_layout == FR_LEAF)
1694 {
1695 /* Set the width/height of this frame.
1696 * Redraw when size or position changes */
1697 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1698#ifdef FEAT_VERTSPLIT
1699 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1700#endif
1701 )
1702 {
1703 topfr->fr_win->w_winrow = row;
1704 frame_new_height(topfr, height, FALSE, FALSE);
1705#ifdef FEAT_VERTSPLIT
1706 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001707 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709 redraw_all_later(CLEAR);
1710 }
1711 }
1712#ifdef FEAT_VERTSPLIT
1713 else if (topfr->fr_layout == FR_ROW)
1714 {
1715 topfr->fr_width = width;
1716 topfr->fr_height = height;
1717
1718 if (dir != 'v') /* equalize frame widths */
1719 {
1720 /* Compute the maximum number of windows horizontally in this
1721 * frame. */
1722 n = frame_minwidth(topfr, NOWIN);
1723 /* add one for the rightmost window, it doesn't have a separator */
1724 if (col + width == Columns)
1725 extra_sep = 1;
1726 else
1727 extra_sep = 0;
1728 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001729 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001731 /*
1732 * Compute width for "next_curwin" window and room available for
1733 * other windows.
1734 * "m" is the minimal width when counting p_wiw for "next_curwin".
1735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 m = frame_minwidth(topfr, next_curwin);
1737 room = width - m;
1738 if (room < 0)
1739 {
1740 next_curwin_size = p_wiw + room;
1741 room = 0;
1742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 else
1744 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001745 next_curwin_size = -1;
1746 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1747 {
1748 /* If 'winfixwidth' set keep the window width if
1749 * possible.
1750 * Watch out for this window being the next_curwin. */
1751 if (frame_fixed_width(fr))
1752 {
1753 n = frame_minwidth(fr, NOWIN);
1754 new_size = fr->fr_width;
1755 if (frame_has_win(fr, next_curwin))
1756 {
1757 room += p_wiw - p_wmw;
1758 next_curwin_size = 0;
1759 if (new_size < p_wiw)
1760 new_size = p_wiw;
1761 }
1762 else
1763 /* These windows don't use up room. */
1764 totwincount -= (n + (fr->fr_next == NULL
1765 ? extra_sep : 0)) / (p_wmw + 1);
1766 room -= new_size - n;
1767 if (room < 0)
1768 {
1769 new_size += room;
1770 room = 0;
1771 }
1772 fr->fr_newwidth = new_size;
1773 }
1774 }
1775 if (next_curwin_size == -1)
1776 {
1777 if (!has_next_curwin)
1778 next_curwin_size = 0;
1779 else if (totwincount > 1
1780 && (room + (totwincount - 2))
1781 / (totwincount - 1) > p_wiw)
1782 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001783 /* Can make all windows wider than 'winwidth', spread
1784 * the room equally. */
1785 next_curwin_size = (room + p_wiw
1786 + (totwincount - 1) * p_wmw
1787 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001788 room -= next_curwin_size - p_wiw;
1789 }
1790 else
1791 next_curwin_size = p_wiw;
1792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001794
1795 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 --totwincount; /* don't count curwin */
1797 }
1798
1799 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1800 {
1801 n = m = 0;
1802 wincount = 1;
1803 if (fr->fr_next == NULL)
1804 /* last frame gets all that remains (avoid roundoff error) */
1805 new_size = width;
1806 else if (dir == 'v')
1807 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001808 else if (frame_fixed_width(fr))
1809 {
1810 new_size = fr->fr_newwidth;
1811 wincount = 0; /* doesn't count as a sizeable window */
1812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else
1814 {
1815 /* Compute the maximum number of windows horiz. in "fr". */
1816 n = frame_minwidth(fr, NOWIN);
1817 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1818 / (p_wmw + 1);
1819 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001820 if (has_next_curwin)
1821 hnc = frame_has_win(fr, next_curwin);
1822 else
1823 hnc = FALSE;
1824 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001826 if (totwincount == 0)
1827 new_size = room;
1828 else
1829 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001831 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833 next_curwin_size -= p_wiw - (m - n);
1834 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001837 else
1838 room -= new_size;
1839 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 }
1841
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001842 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 * window, unless equalizing all frames. */
1844 if (!current || dir != 'v' || topfr->fr_parent != NULL
1845 || (new_size != fr->fr_width)
1846 || frame_has_win(fr, next_curwin))
1847 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001848 new_size, height);
1849 col += new_size;
1850 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 totwincount -= wincount;
1852 }
1853 }
1854#endif
1855 else /* topfr->fr_layout == FR_COL */
1856 {
1857#ifdef FEAT_VERTSPLIT
1858 topfr->fr_width = width;
1859#endif
1860 topfr->fr_height = height;
1861
1862 if (dir != 'h') /* equalize frame heights */
1863 {
1864 /* Compute maximum number of windows vertically in this frame. */
1865 n = frame_minheight(topfr, NOWIN);
1866 /* add one for the bottom window if it doesn't have a statusline */
1867 if (row + height == cmdline_row && p_ls == 0)
1868 extra_sep = 1;
1869 else
1870 extra_sep = 0;
1871 totwincount = (n + extra_sep) / (p_wmh + 1);
1872 has_next_curwin = frame_has_win(topfr, next_curwin);
1873
1874 /*
1875 * Compute height for "next_curwin" window and room available for
1876 * other windows.
1877 * "m" is the minimal height when counting p_wh for "next_curwin".
1878 */
1879 m = frame_minheight(topfr, next_curwin);
1880 room = height - m;
1881 if (room < 0)
1882 {
1883 /* The room is less then 'winheight', use all space for the
1884 * current window. */
1885 next_curwin_size = p_wh + room;
1886 room = 0;
1887 }
1888 else
1889 {
1890 next_curwin_size = -1;
1891 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1892 {
1893 /* If 'winfixheight' set keep the window height if
1894 * possible.
1895 * Watch out for this window being the next_curwin. */
1896 if (frame_fixed_height(fr))
1897 {
1898 n = frame_minheight(fr, NOWIN);
1899 new_size = fr->fr_height;
1900 if (frame_has_win(fr, next_curwin))
1901 {
1902 room += p_wh - p_wmh;
1903 next_curwin_size = 0;
1904 if (new_size < p_wh)
1905 new_size = p_wh;
1906 }
1907 else
1908 /* These windows don't use up room. */
1909 totwincount -= (n + (fr->fr_next == NULL
1910 ? extra_sep : 0)) / (p_wmh + 1);
1911 room -= new_size - n;
1912 if (room < 0)
1913 {
1914 new_size += room;
1915 room = 0;
1916 }
1917 fr->fr_newheight = new_size;
1918 }
1919 }
1920 if (next_curwin_size == -1)
1921 {
1922 if (!has_next_curwin)
1923 next_curwin_size = 0;
1924 else if (totwincount > 1
1925 && (room + (totwincount - 2))
1926 / (totwincount - 1) > p_wh)
1927 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001928 /* can make all windows higher than 'winheight',
1929 * spread the room equally. */
1930 next_curwin_size = (room + p_wh
1931 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 + (totwincount - 1)) / totwincount;
1933 room -= next_curwin_size - p_wh;
1934 }
1935 else
1936 next_curwin_size = p_wh;
1937 }
1938 }
1939
1940 if (has_next_curwin)
1941 --totwincount; /* don't count curwin */
1942 }
1943
1944 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1945 {
1946 n = m = 0;
1947 wincount = 1;
1948 if (fr->fr_next == NULL)
1949 /* last frame gets all that remains (avoid roundoff error) */
1950 new_size = height;
1951 else if (dir == 'h')
1952 new_size = fr->fr_height;
1953 else if (frame_fixed_height(fr))
1954 {
1955 new_size = fr->fr_newheight;
1956 wincount = 0; /* doesn't count as a sizeable window */
1957 }
1958 else
1959 {
1960 /* Compute the maximum number of windows vert. in "fr". */
1961 n = frame_minheight(fr, NOWIN);
1962 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1963 / (p_wmh + 1);
1964 m = frame_minheight(fr, next_curwin);
1965 if (has_next_curwin)
1966 hnc = frame_has_win(fr, next_curwin);
1967 else
1968 hnc = FALSE;
1969 if (hnc) /* don't count next_curwin */
1970 --wincount;
1971 if (totwincount == 0)
1972 new_size = room;
1973 else
1974 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1975 / totwincount;
1976 if (hnc) /* add next_curwin size */
1977 {
1978 next_curwin_size -= p_wh - (m - n);
1979 new_size += next_curwin_size;
1980 room -= new_size - next_curwin_size;
1981 }
1982 else
1983 room -= new_size;
1984 new_size += n;
1985 }
1986 /* Skip frame that is full width when splitting or closing a
1987 * window, unless equalizing all frames. */
1988 if (!current || dir != 'h' || topfr->fr_parent != NULL
1989 || (new_size != fr->fr_height)
1990 || frame_has_win(fr, next_curwin))
1991 win_equal_rec(next_curwin, current, fr, dir, col, row,
1992 width, new_size);
1993 row += new_size;
1994 height -= new_size;
1995 totwincount -= wincount;
1996 }
1997 }
1998}
1999
2000/*
2001 * close all windows for buffer 'buf'
2002 */
2003 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002004close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002006 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002008 win_T *wp;
2009 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002010 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002013
2014 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002016 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2017#ifdef FEAT_AUTOCMD
2018 && !(wp->w_closing || wp->w_buffer->b_closing)
2019#endif
2020 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002022 win_close(wp, FALSE);
2023
2024 /* Start all over, autocommands may change the window layout. */
2025 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002028 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002030
2031 /* Also check windows in other tab pages. */
2032 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2033 {
2034 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002035 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002036 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002037 if (wp->w_buffer == buf
2038#ifdef FEAT_AUTOCMD
2039 && !(wp->w_closing || wp->w_buffer->b_closing)
2040#endif
2041 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002042 {
2043 win_close_othertab(wp, FALSE, tp);
2044
2045 /* Start all over, the tab page may be closed and
2046 * autocommands may change the window layout. */
2047 nexttp = first_tabpage;
2048 break;
2049 }
2050 }
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002053
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002054 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002055 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002056 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057}
2058
2059/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002060 * Return TRUE if the current window is the only window that exists (ignoring
2061 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002062 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002063 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002064 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002065last_window()
2066{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002067 return (one_window() && first_tabpage->tp_next == NULL);
2068}
2069
2070/*
2071 * Return TRUE if there is only one window other than "aucmd_win" in the
2072 * current tab page.
2073 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002074 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002075one_window()
2076{
2077#ifdef FEAT_AUTOCMD
2078 win_T *wp;
2079 int seen_one = FALSE;
2080
2081 FOR_ALL_WINDOWS(wp)
2082 {
2083 if (wp != aucmd_win)
2084 {
2085 if (seen_one)
2086 return FALSE;
2087 seen_one = TRUE;
2088 }
2089 }
2090 return TRUE;
2091#else
2092 return firstwin == lastwin;
2093#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002094}
2095
2096/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002097 * Close the possibly last window in a tab page.
2098 * Returns TRUE when the window was closed already.
2099 */
2100 static int
2101close_last_window_tabpage(win, free_buf, prev_curtab)
2102 win_T *win;
2103 int free_buf;
2104 tabpage_T *prev_curtab;
2105{
2106 if (firstwin == lastwin)
2107 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002108#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002109 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002110#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002111
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002112 /*
2113 * Closing the last window in a tab page. First go to another tab
2114 * page and then close the window and the tab page. This avoids that
2115 * curwin and curtab are invalid while we are freeing memory, they may
2116 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002117 * Don't trigger autocommands yet, they may use wrong values, so do
2118 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002119 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002120 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002121 redraw_tabline = TRUE;
2122
2123 /* Safety check: Autocommands may have closed the window when jumping
2124 * to the other tab page. */
2125 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2126 {
2127 int h = tabline_height();
2128
2129 win_close_othertab(win, free_buf, prev_curtab);
2130 if (h != tabline_height())
2131 shell_new_rows();
2132 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002133 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2134 * that now. */
2135#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002136 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002137 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2138 if (old_curbuf != curbuf)
2139 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002140#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002141 return TRUE;
2142 }
2143 return FALSE;
2144}
2145
2146/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 * If "free_buf" is TRUE related buffer may be unloaded.
2149 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002150 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002151 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002153 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154win_close(win, free_buf)
2155 win_T *win;
2156 int free_buf;
2157{
2158 win_T *wp;
2159#ifdef FEAT_AUTOCMD
2160 int other_buffer = FALSE;
2161#endif
2162 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int dir;
2164 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002165 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002167 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
2169 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002170 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 }
2172
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002173#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002174 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002175 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002176 if (win == aucmd_win)
2177 {
2178 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002179 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002180 }
2181 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2182 {
2183 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002184 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002185 }
2186#endif
2187
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002188 /* When closing the last window in a tab page first go to another tab page
2189 * and then close the window and the tab page to avoid that curwin and
2190 * curtab are invalid while we are freeing memory. */
2191 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002192 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 /* When closing the help window, try restoring a snapshot after closing
2195 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002196 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 help_window = TRUE;
2198 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002199 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200
2201#ifdef FEAT_AUTOCMD
2202 if (win == curwin)
2203 {
2204 /*
2205 * Guess which window is going to be the new current window.
2206 * This may change because of the autocommands (sigh).
2207 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002208 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209
2210 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002211 * Be careful: If autocommands delete the window or cause this window
2212 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 */
2214 if (wp->w_buffer != curbuf)
2215 {
2216 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002217 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002219 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002220 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002221 win->w_closing = FALSE;
2222 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002223 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002225 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002227 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002228 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002229 win->w_closing = FALSE;
2230 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002231 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232# ifdef FEAT_EVAL
2233 /* autocmds may abort script processing */
2234 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236# endif
2237 }
2238#endif
2239
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002240#ifdef FEAT_GUI
2241 /* Avoid trouble with scrollbars that are going to be deleted in
2242 * win_free(). */
2243 if (gui.in_use)
2244 out_flush();
2245#endif
2246
Bram Moolenaara971b822011-09-14 14:43:25 +02002247#ifdef FEAT_SYN_HL
2248 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002249 if (win->w_buffer != NULL)
2250 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002251#endif
2252
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 /*
2254 * Close the link to the buffer.
2255 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002256 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002257 {
2258#ifdef FEAT_AUTOCMD
2259 win->w_closing = TRUE;
2260#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002261 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002262#ifdef FEAT_AUTOCMD
2263 if (win_valid(win))
2264 win->w_closing = FALSE;
2265#endif
2266 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002267
Bram Moolenaar802418d2013-01-17 14:00:11 +01002268 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2269 && (last_window() || curtab != prev_curtab
2270 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002271 {
2272 /* Autocommands have close all windows, quit now. Restore
2273 * curwin->w_buffer, otherwise writing viminfo may fail. */
2274 if (curwin->w_buffer == NULL)
2275 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002276 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002277 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002280 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002281 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002282 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002283 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
Bram Moolenaara971b822011-09-14 14:43:25 +02002285 /* Free the memory used for the window and get the window that received
2286 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002287 wp = win_free_mem(win, &dir, NULL);
2288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 /* Make sure curwin isn't invalid. It can cause severe trouble when
2290 * printing an error message. For win_equal() curbuf needs to be valid
2291 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002292 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 curwin = wp;
2295#ifdef FEAT_QUICKFIX
2296 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2297 {
2298 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002299 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * finding another window to go to.
2301 */
2302 for (;;)
2303 {
2304 if (wp->w_next == NULL)
2305 wp = firstwin;
2306 else
2307 wp = wp->w_next;
2308 if (wp == curwin)
2309 break;
2310 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2311 {
2312 curwin = wp;
2313 break;
2314 }
2315 }
2316 }
2317#endif
2318 curbuf = curwin->w_buffer;
2319 close_curwin = TRUE;
2320 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002321 if (p_ea
2322#ifdef FEAT_VERTSPLIT
2323 && (*p_ead == 'b' || *p_ead == dir)
2324#endif
2325 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 win_equal(curwin, TRUE,
2327#ifdef FEAT_VERTSPLIT
2328 dir
2329#else
2330 0
2331#endif
2332 );
2333 else
2334 win_comp_pos();
2335 if (close_curwin)
2336 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002337 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338#ifdef FEAT_AUTOCMD
2339 if (other_buffer)
2340 /* careful: after this wp and win may be invalid! */
2341 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2342#endif
2343 }
2344
2345 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002346 * If last window has a status line now and we don't want one,
2347 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 */
2349 last_status(FALSE);
2350
2351 /* After closing the help window, try restoring the window layout from
2352 * before it was opened. */
2353 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002354 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2357 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2358 if (gui.in_use && !win_hasvertsplit())
2359 gui_init_which_components(NULL);
2360#endif
2361
2362 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002363 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364}
2365
2366/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002367 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002368 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002369 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002370 * Caller must check if buffer is hidden and whether the tabline needs to be
2371 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002372 */
2373 void
2374win_close_othertab(win, free_buf, tp)
2375 win_T *win;
2376 int free_buf;
2377 tabpage_T *tp;
2378{
2379 win_T *wp;
2380 int dir;
2381 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002382 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002383
Bram Moolenaar362ce482012-06-06 19:02:45 +02002384#ifdef FEAT_AUTOCMD
2385 if (win->w_closing || win->w_buffer->b_closing)
2386 return; /* window is already being closed */
2387#endif
2388
Bram Moolenaarf740b292006-02-16 22:11:02 +00002389 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002390 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002391
2392 /* Careful: Autocommands may have closed the tab page or made it the
2393 * current tab page. */
2394 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2395 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002396 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002397 return;
2398
2399 /* Autocommands may have closed the window already. */
2400 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2401 ;
2402 if (wp == NULL)
2403 return;
2404
Bram Moolenaarf740b292006-02-16 22:11:02 +00002405 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002406 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002407 {
2408 if (tp == first_tabpage)
2409 first_tabpage = tp->tp_next;
2410 else
2411 {
2412 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2413 ptp = ptp->tp_next)
2414 ;
2415 if (ptp == NULL)
2416 {
2417 EMSG2(_(e_intern2), "win_close_othertab()");
2418 return;
2419 }
2420 ptp->tp_next = tp->tp_next;
2421 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002422 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002423 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002424
2425 /* Free the memory used for the window. */
2426 win_free_mem(win, &dir, tp);
2427
2428 if (free_tp)
2429 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002430}
2431
2432/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002433 * Free the memory used for a window.
2434 * Returns a pointer to the window that got the freed up space.
2435 */
2436 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002437win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002438 win_T *win;
2439 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002440 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002441{
2442 frame_T *frp;
2443 win_T *wp;
2444
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002445 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002446 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002447 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002448 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002449 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002450
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002451 /* When deleting the current window of another tab page select a new
2452 * current window. */
2453 if (tp != NULL && win == tp->tp_curwin)
2454 tp->tp_curwin = wp;
2455
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002456 return wp;
2457}
2458
2459#if defined(EXITFREE) || defined(PROTO)
2460 void
2461win_free_all()
2462{
2463 int dummy;
2464
Bram Moolenaarf740b292006-02-16 22:11:02 +00002465# ifdef FEAT_WINDOWS
2466 while (first_tabpage->tp_next != NULL)
2467 tabpage_close(TRUE);
2468# endif
2469
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002470# ifdef FEAT_AUTOCMD
2471 if (aucmd_win != NULL)
2472 {
2473 (void)win_free_mem(aucmd_win, &dummy, NULL);
2474 aucmd_win = NULL;
2475 }
2476# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002477
2478 while (firstwin != NULL)
2479 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002480}
2481#endif
2482
2483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 * Remove a window and its frame from the tree of frames.
2485 * Returns a pointer to the window that got the freed up space.
2486 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002487 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002488winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002490 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002491 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 frame_T *frp, *frp2, *frp3;
2494 frame_T *frp_close = win->w_frame;
2495 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002498 * If there is only one window there is nothing to remove.
2499 */
2500 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2501 return NULL;
2502
2503 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * Remove the window from its frame.
2505 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002506 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 wp = frame2win(frp2);
2508
2509 /* Remove this frame from the list of frames. */
2510 frame_remove(frp_close);
2511
2512#ifdef FEAT_VERTSPLIT
2513 if (frp_close->fr_parent->fr_layout == FR_COL)
2514 {
2515#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002516 /* When 'winfixheight' is set, try to find another frame in the column
2517 * (as close to the closed frame as possible) to distribute the height
2518 * to. */
2519 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2520 {
2521 frp = frp_close->fr_prev;
2522 frp3 = frp_close->fr_next;
2523 while (frp != NULL || frp3 != NULL)
2524 {
2525 if (frp != NULL)
2526 {
2527 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2528 {
2529 frp2 = frp;
2530 wp = frp->fr_win;
2531 break;
2532 }
2533 frp = frp->fr_prev;
2534 }
2535 if (frp3 != NULL)
2536 {
2537 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2538 {
2539 frp2 = frp3;
2540 wp = frp3->fr_win;
2541 break;
2542 }
2543 frp3 = frp3->fr_next;
2544 }
2545 }
2546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2548 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#ifdef FEAT_VERTSPLIT
2550 *dirp = 'v';
2551 }
2552 else
2553 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002554 /* When 'winfixwidth' is set, try to find another frame in the column
2555 * (as close to the closed frame as possible) to distribute the width
2556 * to. */
2557 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2558 {
2559 frp = frp_close->fr_prev;
2560 frp3 = frp_close->fr_next;
2561 while (frp != NULL || frp3 != NULL)
2562 {
2563 if (frp != NULL)
2564 {
2565 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2566 {
2567 frp2 = frp;
2568 wp = frp->fr_win;
2569 break;
2570 }
2571 frp = frp->fr_prev;
2572 }
2573 if (frp3 != NULL)
2574 {
2575 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2576 {
2577 frp2 = frp3;
2578 wp = frp3->fr_win;
2579 break;
2580 }
2581 frp3 = frp3->fr_next;
2582 }
2583 }
2584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002586 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 *dirp = 'h';
2588 }
2589#endif
2590
2591 /* If rows/columns go to a window below/right its positions need to be
2592 * updated. Can only be done after the sizes have been updated. */
2593 if (frp2 == frp_close->fr_next)
2594 {
2595 int row = win->w_winrow;
2596 int col = W_WINCOL(win);
2597
2598 frame_comp_pos(frp2, &row, &col);
2599 }
2600
2601 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2602 {
2603 /* There is no other frame in this list, move its info to the parent
2604 * and remove it. */
2605 frp2->fr_parent->fr_layout = frp2->fr_layout;
2606 frp2->fr_parent->fr_child = frp2->fr_child;
2607 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2608 frp->fr_parent = frp2->fr_parent;
2609 frp2->fr_parent->fr_win = frp2->fr_win;
2610 if (frp2->fr_win != NULL)
2611 frp2->fr_win->w_frame = frp2->fr_parent;
2612 frp = frp2->fr_parent;
2613 vim_free(frp2);
2614
2615 frp2 = frp->fr_parent;
2616 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2617 {
2618 /* The frame above the parent has the same layout, have to merge
2619 * the frames into this list. */
2620 if (frp2->fr_child == frp)
2621 frp2->fr_child = frp->fr_child;
2622 frp->fr_child->fr_prev = frp->fr_prev;
2623 if (frp->fr_prev != NULL)
2624 frp->fr_prev->fr_next = frp->fr_child;
2625 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2626 {
2627 frp3->fr_parent = frp2;
2628 if (frp3->fr_next == NULL)
2629 {
2630 frp3->fr_next = frp->fr_next;
2631 if (frp->fr_next != NULL)
2632 frp->fr_next->fr_prev = frp3;
2633 break;
2634 }
2635 }
2636 vim_free(frp);
2637 }
2638 }
2639
2640 return wp;
2641}
2642
2643/*
2644 * Find out which frame is going to get the freed up space when "win" is
2645 * closed.
2646 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2647 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2648 * This makes opening a window and closing it immediately keep the same window
2649 * layout.
2650 */
2651 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002652win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002654 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
2656 frame_T *frp;
2657 int b;
2658
Bram Moolenaarf740b292006-02-16 22:11:02 +00002659 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002660 /* Last window in this tab page, will go to next tab page. */
2661 return alt_tabpage()->tp_curwin->w_frame;
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 frp = win->w_frame;
2664#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002665 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 b = p_spr;
2667 else
2668#endif
2669 b = p_sb;
2670 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2671 return frp->fr_next;
2672 return frp->fr_prev;
2673}
2674
2675/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002676 * Return the tabpage that will be used if the current one is closed.
2677 */
2678 static tabpage_T *
2679alt_tabpage()
2680{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002681 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002682
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002683 /* Use the next tab page if possible. */
2684 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002685 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002686
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002687 /* Find the last but one tab page. */
2688 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2689 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002690 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002691}
2692
2693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 * Find the left-upper window in frame "frp".
2695 */
2696 static win_T *
2697frame2win(frp)
2698 frame_T *frp;
2699{
2700 while (frp->fr_win == NULL)
2701 frp = frp->fr_child;
2702 return frp->fr_win;
2703}
2704
2705/*
2706 * Return TRUE if frame "frp" contains window "wp".
2707 */
2708 static int
2709frame_has_win(frp, wp)
2710 frame_T *frp;
2711 win_T *wp;
2712{
2713 frame_T *p;
2714
2715 if (frp->fr_layout == FR_LEAF)
2716 return frp->fr_win == wp;
2717
2718 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2719 if (frame_has_win(p, wp))
2720 return TRUE;
2721 return FALSE;
2722}
2723
2724/*
2725 * Set a new height for a frame. Recursively sets the height for contained
2726 * frames and windows. Caller must take care of positions.
2727 */
2728 static void
2729frame_new_height(topfrp, height, topfirst, wfh)
2730 frame_T *topfrp;
2731 int height;
2732 int topfirst; /* resize topmost contained frame first */
2733 int wfh; /* obey 'winfixheight' when there is a choice;
2734 may cause the height not to be set */
2735{
2736 frame_T *frp;
2737 int extra_lines;
2738 int h;
2739
2740 if (topfrp->fr_win != NULL)
2741 {
2742 /* Simple case: just one window. */
2743 win_new_height(topfrp->fr_win,
2744 height - topfrp->fr_win->w_status_height);
2745 }
2746#ifdef FEAT_VERTSPLIT
2747 else if (topfrp->fr_layout == FR_ROW)
2748 {
2749 do
2750 {
2751 /* All frames in this row get the same new height. */
2752 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2753 {
2754 frame_new_height(frp, height, topfirst, wfh);
2755 if (frp->fr_height > height)
2756 {
2757 /* Could not fit the windows, make the whole row higher. */
2758 height = frp->fr_height;
2759 break;
2760 }
2761 }
2762 }
2763 while (frp != NULL);
2764 }
2765#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002766 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
2768 /* Complicated case: Resize a column of frames. Resize the bottom
2769 * frame first, frames above that when needed. */
2770
2771 frp = topfrp->fr_child;
2772 if (wfh)
2773 /* Advance past frames with one window with 'wfh' set. */
2774 while (frame_fixed_height(frp))
2775 {
2776 frp = frp->fr_next;
2777 if (frp == NULL)
2778 return; /* no frame without 'wfh', give up */
2779 }
2780 if (!topfirst)
2781 {
2782 /* Find the bottom frame of this column */
2783 while (frp->fr_next != NULL)
2784 frp = frp->fr_next;
2785 if (wfh)
2786 /* Advance back for frames with one window with 'wfh' set. */
2787 while (frame_fixed_height(frp))
2788 frp = frp->fr_prev;
2789 }
2790
2791 extra_lines = height - topfrp->fr_height;
2792 if (extra_lines < 0)
2793 {
2794 /* reduce height of contained frames, bottom or top frame first */
2795 while (frp != NULL)
2796 {
2797 h = frame_minheight(frp, NULL);
2798 if (frp->fr_height + extra_lines < h)
2799 {
2800 extra_lines += frp->fr_height - h;
2801 frame_new_height(frp, h, topfirst, wfh);
2802 }
2803 else
2804 {
2805 frame_new_height(frp, frp->fr_height + extra_lines,
2806 topfirst, wfh);
2807 break;
2808 }
2809 if (topfirst)
2810 {
2811 do
2812 frp = frp->fr_next;
2813 while (wfh && frp != NULL && frame_fixed_height(frp));
2814 }
2815 else
2816 {
2817 do
2818 frp = frp->fr_prev;
2819 while (wfh && frp != NULL && frame_fixed_height(frp));
2820 }
2821 /* Increase "height" if we could not reduce enough frames. */
2822 if (frp == NULL)
2823 height -= extra_lines;
2824 }
2825 }
2826 else if (extra_lines > 0)
2827 {
2828 /* increase height of bottom or top frame */
2829 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2830 }
2831 }
2832 topfrp->fr_height = height;
2833}
2834
2835/*
2836 * Return TRUE if height of frame "frp" should not be changed because of
2837 * the 'winfixheight' option.
2838 */
2839 static int
2840frame_fixed_height(frp)
2841 frame_T *frp;
2842{
2843 /* frame with one window: fixed height if 'winfixheight' set. */
2844 if (frp->fr_win != NULL)
2845 return frp->fr_win->w_p_wfh;
2846
2847 if (frp->fr_layout == FR_ROW)
2848 {
2849 /* The frame is fixed height if one of the frames in the row is fixed
2850 * height. */
2851 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2852 if (frame_fixed_height(frp))
2853 return TRUE;
2854 return FALSE;
2855 }
2856
2857 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2858 * frames in the row are fixed height. */
2859 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2860 if (!frame_fixed_height(frp))
2861 return FALSE;
2862 return TRUE;
2863}
2864
2865#ifdef FEAT_VERTSPLIT
2866/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002867 * Return TRUE if width of frame "frp" should not be changed because of
2868 * the 'winfixwidth' option.
2869 */
2870 static int
2871frame_fixed_width(frp)
2872 frame_T *frp;
2873{
2874 /* frame with one window: fixed width if 'winfixwidth' set. */
2875 if (frp->fr_win != NULL)
2876 return frp->fr_win->w_p_wfw;
2877
2878 if (frp->fr_layout == FR_COL)
2879 {
2880 /* The frame is fixed width if one of the frames in the row is fixed
2881 * width. */
2882 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2883 if (frame_fixed_width(frp))
2884 return TRUE;
2885 return FALSE;
2886 }
2887
2888 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2889 * frames in the row are fixed width. */
2890 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2891 if (!frame_fixed_width(frp))
2892 return FALSE;
2893 return TRUE;
2894}
2895
2896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 * Add a status line to windows at the bottom of "frp".
2898 * Note: Does not check if there is room!
2899 */
2900 static void
2901frame_add_statusline(frp)
2902 frame_T *frp;
2903{
2904 win_T *wp;
2905
2906 if (frp->fr_layout == FR_LEAF)
2907 {
2908 wp = frp->fr_win;
2909 if (wp->w_status_height == 0)
2910 {
2911 if (wp->w_height > 0) /* don't make it negative */
2912 --wp->w_height;
2913 wp->w_status_height = STATUS_HEIGHT;
2914 }
2915 }
2916 else if (frp->fr_layout == FR_ROW)
2917 {
2918 /* Handle all the frames in the row. */
2919 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2920 frame_add_statusline(frp);
2921 }
2922 else /* frp->fr_layout == FR_COL */
2923 {
2924 /* Only need to handle the last frame in the column. */
2925 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2926 ;
2927 frame_add_statusline(frp);
2928 }
2929}
2930
2931/*
2932 * Set width of a frame. Handles recursively going through contained frames.
2933 * May remove separator line for windows at the right side (for win_close()).
2934 */
2935 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002936frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 frame_T *topfrp;
2938 int width;
2939 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002940 int wfw; /* obey 'winfixwidth' when there is a choice;
2941 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 frame_T *frp;
2944 int extra_cols;
2945 int w;
2946 win_T *wp;
2947
2948 if (topfrp->fr_layout == FR_LEAF)
2949 {
2950 /* Simple case: just one window. */
2951 wp = topfrp->fr_win;
2952 /* Find out if there are any windows right of this one. */
2953 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2954 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2955 break;
2956 if (frp->fr_parent == NULL)
2957 wp->w_vsep_width = 0;
2958 win_new_width(wp, width - wp->w_vsep_width);
2959 }
2960 else if (topfrp->fr_layout == FR_COL)
2961 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002962 do
2963 {
2964 /* All frames in this column get the same new width. */
2965 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2966 {
2967 frame_new_width(frp, width, leftfirst, wfw);
2968 if (frp->fr_width > width)
2969 {
2970 /* Could not fit the windows, make whole column wider. */
2971 width = frp->fr_width;
2972 break;
2973 }
2974 }
2975 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
2977 else /* fr_layout == FR_ROW */
2978 {
2979 /* Complicated case: Resize a row of frames. Resize the rightmost
2980 * frame first, frames left of it when needed. */
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002983 if (wfw)
2984 /* Advance past frames with one window with 'wfw' set. */
2985 while (frame_fixed_width(frp))
2986 {
2987 frp = frp->fr_next;
2988 if (frp == NULL)
2989 return; /* no frame without 'wfw', give up */
2990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002992 {
2993 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 while (frp->fr_next != NULL)
2995 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002996 if (wfw)
2997 /* Advance back for frames with one window with 'wfw' set. */
2998 while (frame_fixed_width(frp))
2999 frp = frp->fr_prev;
3000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 extra_cols = width - topfrp->fr_width;
3003 if (extra_cols < 0)
3004 {
3005 /* reduce frame width, rightmost frame first */
3006 while (frp != NULL)
3007 {
3008 w = frame_minwidth(frp, NULL);
3009 if (frp->fr_width + extra_cols < w)
3010 {
3011 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003012 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 }
3014 else
3015 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003016 frame_new_width(frp, frp->fr_width + extra_cols,
3017 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 break;
3019 }
3020 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003021 {
3022 do
3023 frp = frp->fr_next;
3024 while (wfw && frp != NULL && frame_fixed_width(frp));
3025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003027 {
3028 do
3029 frp = frp->fr_prev;
3030 while (wfw && frp != NULL && frame_fixed_width(frp));
3031 }
3032 /* Increase "width" if we could not reduce enough frames. */
3033 if (frp == NULL)
3034 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036 }
3037 else if (extra_cols > 0)
3038 {
3039 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003040 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 }
3042 }
3043 topfrp->fr_width = width;
3044}
3045
3046/*
3047 * Add the vertical separator to windows at the right side of "frp".
3048 * Note: Does not check if there is room!
3049 */
3050 static void
3051frame_add_vsep(frp)
3052 frame_T *frp;
3053{
3054 win_T *wp;
3055
3056 if (frp->fr_layout == FR_LEAF)
3057 {
3058 wp = frp->fr_win;
3059 if (wp->w_vsep_width == 0)
3060 {
3061 if (wp->w_width > 0) /* don't make it negative */
3062 --wp->w_width;
3063 wp->w_vsep_width = 1;
3064 }
3065 }
3066 else if (frp->fr_layout == FR_COL)
3067 {
3068 /* Handle all the frames in the column. */
3069 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3070 frame_add_vsep(frp);
3071 }
3072 else /* frp->fr_layout == FR_ROW */
3073 {
3074 /* Only need to handle the last frame in the row. */
3075 frp = frp->fr_child;
3076 while (frp->fr_next != NULL)
3077 frp = frp->fr_next;
3078 frame_add_vsep(frp);
3079 }
3080}
3081
3082/*
3083 * Set frame width from the window it contains.
3084 */
3085 static void
3086frame_fix_width(wp)
3087 win_T *wp;
3088{
3089 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3090}
3091#endif
3092
3093/*
3094 * Set frame height from the window it contains.
3095 */
3096 static void
3097frame_fix_height(wp)
3098 win_T *wp;
3099{
3100 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3101}
3102
3103/*
3104 * Compute the minimal height for frame "topfrp".
3105 * Uses the 'winminheight' option.
3106 * When "next_curwin" isn't NULL, use p_wh for this window.
3107 * When "next_curwin" is NOWIN, don't use at least one line for the current
3108 * window.
3109 */
3110 static int
3111frame_minheight(topfrp, next_curwin)
3112 frame_T *topfrp;
3113 win_T *next_curwin;
3114{
3115 frame_T *frp;
3116 int m;
3117#ifdef FEAT_VERTSPLIT
3118 int n;
3119#endif
3120
3121 if (topfrp->fr_win != NULL)
3122 {
3123 if (topfrp->fr_win == next_curwin)
3124 m = p_wh + topfrp->fr_win->w_status_height;
3125 else
3126 {
3127 /* window: minimal height of the window plus status line */
3128 m = p_wmh + topfrp->fr_win->w_status_height;
3129 /* Current window is minimal one line high */
3130 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3131 ++m;
3132 }
3133 }
3134#ifdef FEAT_VERTSPLIT
3135 else if (topfrp->fr_layout == FR_ROW)
3136 {
3137 /* get the minimal height from each frame in this row */
3138 m = 0;
3139 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3140 {
3141 n = frame_minheight(frp, next_curwin);
3142 if (n > m)
3143 m = n;
3144 }
3145 }
3146#endif
3147 else
3148 {
3149 /* Add up the minimal heights for all frames in this column. */
3150 m = 0;
3151 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3152 m += frame_minheight(frp, next_curwin);
3153 }
3154
3155 return m;
3156}
3157
3158#ifdef FEAT_VERTSPLIT
3159/*
3160 * Compute the minimal width for frame "topfrp".
3161 * When "next_curwin" isn't NULL, use p_wiw for this window.
3162 * When "next_curwin" is NOWIN, don't use at least one column for the current
3163 * window.
3164 */
3165 static int
3166frame_minwidth(topfrp, next_curwin)
3167 frame_T *topfrp;
3168 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3169{
3170 frame_T *frp;
3171 int m, n;
3172
3173 if (topfrp->fr_win != NULL)
3174 {
3175 if (topfrp->fr_win == next_curwin)
3176 m = p_wiw + topfrp->fr_win->w_vsep_width;
3177 else
3178 {
3179 /* window: minimal width of the window plus separator column */
3180 m = p_wmw + topfrp->fr_win->w_vsep_width;
3181 /* Current window is minimal one column wide */
3182 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3183 ++m;
3184 }
3185 }
3186 else if (topfrp->fr_layout == FR_COL)
3187 {
3188 /* get the minimal width from each frame in this column */
3189 m = 0;
3190 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3191 {
3192 n = frame_minwidth(frp, next_curwin);
3193 if (n > m)
3194 m = n;
3195 }
3196 }
3197 else
3198 {
3199 /* Add up the minimal widths for all frames in this row. */
3200 m = 0;
3201 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3202 m += frame_minwidth(frp, next_curwin);
3203 }
3204
3205 return m;
3206}
3207#endif
3208
3209
3210/*
3211 * Try to close all windows except current one.
3212 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3213 * used and the buffer was modified.
3214 *
3215 * Used by ":bdel" and ":only".
3216 */
3217 void
3218close_others(message, forceit)
3219 int message;
3220 int forceit; /* always hide all other windows */
3221{
3222 win_T *wp;
3223 win_T *nextwp;
3224 int r;
3225
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003226 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
3228 if (message
3229#ifdef FEAT_AUTOCMD
3230 && !autocmd_busy
3231#endif
3232 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003233 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 return;
3235 }
3236
3237 /* Be very careful here: autocommands may change the window layout. */
3238 for (wp = firstwin; win_valid(wp); wp = nextwp)
3239 {
3240 nextwp = wp->w_next;
3241 if (wp != curwin) /* don't close current window */
3242 {
3243
3244 /* Check if it's allowed to abandon this window */
3245 r = can_abandon(wp->w_buffer, forceit);
3246#ifdef FEAT_AUTOCMD
3247 if (!win_valid(wp)) /* autocommands messed wp up */
3248 {
3249 nextwp = firstwin;
3250 continue;
3251 }
3252#endif
3253 if (!r)
3254 {
3255#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3256 if (message && (p_confirm || cmdmod.confirm) && p_write)
3257 {
3258 dialog_changed(wp->w_buffer, FALSE);
3259# ifdef FEAT_AUTOCMD
3260 if (!win_valid(wp)) /* autocommands messed wp up */
3261 {
3262 nextwp = firstwin;
3263 continue;
3264 }
3265# endif
3266 }
3267 if (bufIsChanged(wp->w_buffer))
3268#endif
3269 continue;
3270 }
3271 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3272 }
3273 }
3274
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003275 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 EMSG(_("E445: Other window contains changes"));
3277}
3278
3279#endif /* FEAT_WINDOWS */
3280
3281/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003282 * Init the current window "curwin".
3283 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 */
3285 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003286curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003288 win_init_empty(curwin);
3289}
3290
3291 void
3292win_init_empty(wp)
3293 win_T *wp;
3294{
3295 redraw_win_later(wp, NOT_VALID);
3296 wp->w_lines_valid = 0;
3297 wp->w_cursor.lnum = 1;
3298 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003300 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003302 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3303 wp->w_pcmark.col = 0;
3304 wp->w_prev_pcmark.lnum = 0;
3305 wp->w_prev_pcmark.col = 0;
3306 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003308 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003310 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003312 if (wp->w_p_rl)
3313 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003315 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003317#ifdef FEAT_SYN_HL
3318 wp->w_s = &wp->w_buffer->b_s;
3319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320}
3321
3322/*
3323 * Allocate the first window and put an empty buffer in it.
3324 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003325 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003327 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328win_alloc_first()
3329{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003330 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003331 return FAIL;
3332
3333#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003334 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003335 if (first_tabpage == NULL)
3336 return FAIL;
3337 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003338 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003339#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003340
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003341 return OK;
3342}
3343
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003344#if defined(FEAT_AUTOCMD) || defined(PROTO)
3345/*
3346 * Init "aucmd_win". This can only be done after the first
3347 * window is fully initialized, thus it can't be in win_alloc_first().
3348 */
3349 void
3350win_alloc_aucmd_win()
3351{
3352 aucmd_win = win_alloc(NULL, TRUE);
3353 if (aucmd_win != NULL)
3354 {
3355 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003356 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003357 new_frame(aucmd_win);
3358 }
3359}
3360#endif
3361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003362/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003363 * Allocate the first window or the first window in a new tab page.
3364 * When "oldwin" is NULL create an empty buffer for it.
3365 * When "oldwin" is not NULL copy info from it to the new window (only with
3366 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003367 * Return FAIL when something goes wrong (out of memory).
3368 */
3369 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003370win_alloc_firstwin(oldwin)
3371 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003372{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003373 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003374 if (oldwin == NULL)
3375 {
3376 /* Very first window, need to create an empty buffer for it and
3377 * initialize from scratch. */
3378 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3379 if (curwin == NULL || curbuf == NULL)
3380 return FAIL;
3381 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003382#ifdef FEAT_SYN_HL
3383 curwin->w_s = &(curbuf->b_s);
3384#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003385 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003387 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003389 curwin_init(); /* init current window */
3390 }
3391#ifdef FEAT_WINDOWS
3392 else
3393 {
3394 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003395 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003396
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003397 /* We don't want cursor- and scroll-binding in the first window. */
3398 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003399 }
3400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003402 new_frame(curwin);
3403 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003404 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003405 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#ifdef FEAT_VERTSPLIT
3407 topframe->fr_width = Columns;
3408#endif
3409 topframe->fr_height = Rows - p_ch;
3410 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003411
3412 return OK;
3413}
3414
3415/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003416 * Create a frame for window "wp".
3417 */
3418 static void
3419new_frame(win_T *wp)
3420{
3421 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3422
3423 wp->w_frame = frp;
3424 if (frp != NULL)
3425 {
3426 frp->fr_layout = FR_LEAF;
3427 frp->fr_win = wp;
3428 }
3429}
3430
3431/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003432 * Initialize the window and frame size to the maximum.
3433 */
3434 void
3435win_init_size()
3436{
3437 firstwin->w_height = ROWS_AVAIL;
3438 topframe->fr_height = ROWS_AVAIL;
3439#ifdef FEAT_VERTSPLIT
3440 firstwin->w_width = Columns;
3441 topframe->fr_width = Columns;
3442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443}
3444
3445#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003446
3447/*
3448 * Allocate a new tabpage_T and init the values.
3449 * Returns NULL when out of memory.
3450 */
3451 static tabpage_T *
3452alloc_tabpage()
3453{
3454 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003455# ifdef FEAT_GUI
3456 int i;
3457# endif
3458
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003459
3460 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003461 if (tp == NULL)
3462 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003463
Bram Moolenaar429fa852013-04-15 12:27:36 +02003464# ifdef FEAT_EVAL
3465 /* init t: variables */
3466 tp->tp_vars = dict_alloc();
3467 if (tp->tp_vars == NULL)
3468 {
3469 vim_free(tp);
3470 return NULL;
3471 }
3472 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3473# endif
3474
3475# ifdef FEAT_GUI
3476 for (i = 0; i < 3; i++)
3477 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003478# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003479# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003480 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003481# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003482 tp->tp_ch_used = p_ch;
3483
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003484 return tp;
3485}
3486
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003487 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003488free_tabpage(tp)
3489 tabpage_T *tp;
3490{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003491 int idx;
3492
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003493# ifdef FEAT_DIFF
3494 diff_clear(tp);
3495# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003496 for (idx = 0; idx < SNAP_COUNT; ++idx)
3497 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003498#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003499 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3500 hash_init(&tp->tp_vars->dv_hashtab);
3501 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003502#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003503
3504#ifdef FEAT_PYTHON
3505 python_tabpage_free(tp);
3506#endif
3507
3508#ifdef FEAT_PYTHON3
3509 python3_tabpage_free(tp);
3510#endif
3511
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003512 vim_free(tp);
3513}
3514
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003515/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003516 * Create a new Tab page with one window.
3517 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003518 * When "after" is 0 put it just after the current Tab page.
3519 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520 * Return FAIL or OK.
3521 */
3522 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003523win_new_tabpage(after)
3524 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003525{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003526 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003527 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003528 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003529
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003530 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003531 if (newtp == NULL)
3532 return FAIL;
3533
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003534 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003535 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003536 {
3537 vim_free(newtp);
3538 return FAIL;
3539 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003540 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003541
3542 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003543 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003544 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003545 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003546 if (after == 1)
3547 {
3548 /* New tab page becomes the first one. */
3549 newtp->tp_next = first_tabpage;
3550 first_tabpage = newtp;
3551 }
3552 else
3553 {
3554 if (after > 0)
3555 {
3556 /* Put new tab page before tab page "after". */
3557 n = 2;
3558 for (tp = first_tabpage; tp->tp_next != NULL
3559 && n < after; tp = tp->tp_next)
3560 ++n;
3561 }
3562 newtp->tp_next = tp->tp_next;
3563 tp->tp_next = newtp;
3564 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003565 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003566 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003567 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003568
3569 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003570 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003571
3572#if defined(FEAT_GUI)
3573 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3574 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003575 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003576#endif
3577
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003578 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003579#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003580 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003581 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003582#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003583 return OK;
3584 }
3585
3586 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003587 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003588 return FAIL;
3589}
3590
3591/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003592 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3593 * like with ":split".
3594 * Returns OK if a new tab page was created, FAIL otherwise.
3595 */
3596 int
3597may_open_tabpage()
3598{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003599 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003600
Bram Moolenaard326ce82007-03-11 14:48:29 +00003601 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003602 {
3603 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003604 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003605 return win_new_tabpage(n);
3606 }
3607 return FAIL;
3608}
3609
3610/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003611 * Create up to "maxcount" tabpages with empty windows.
3612 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003613 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003614 int
3615make_tabpages(maxcount)
3616 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003617{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003618 int count = maxcount;
3619 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003620
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003621 /* Limit to 'tabpagemax' tabs. */
3622 if (count > p_tpm)
3623 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003624
3625#ifdef FEAT_AUTOCMD
3626 /*
3627 * Don't execute autocommands while creating the tab pages. Must do that
3628 * when putting the buffers in the windows.
3629 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003630 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003631#endif
3632
3633 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003634 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003635 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003636
3637#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003638 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003639#endif
3640
3641 /* return actual number of tab pages */
3642 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643}
3644
3645/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003646 * Return TRUE when "tpc" points to a valid tab page.
3647 */
3648 int
3649valid_tabpage(tpc)
3650 tabpage_T *tpc;
3651{
3652 tabpage_T *tp;
3653
3654 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3655 if (tp == tpc)
3656 return TRUE;
3657 return FALSE;
3658}
3659
3660/*
3661 * Find tab page "n" (first one is 1). Returns NULL when not found.
3662 */
3663 tabpage_T *
3664find_tabpage(n)
3665 int n;
3666{
3667 tabpage_T *tp;
3668 int i = 1;
3669
3670 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3671 ++i;
3672 return tp;
3673}
3674
3675/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003676 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003677 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003678 */
3679 int
3680tabpage_index(ftp)
3681 tabpage_T *ftp;
3682{
3683 int i = 1;
3684 tabpage_T *tp;
3685
3686 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3687 ++i;
3688 return i;
3689}
3690
3691/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003692 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003693 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003694 * FAIL.
3695 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003696 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003697 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003698leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003699 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003700 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003701 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003702{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003703 tabpage_T *tp = curtab;
3704
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003705 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003706#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003707 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003708 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003709 if (new_curbuf != curbuf)
3710 {
3711 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3712 if (curtab != tp)
3713 return FAIL;
3714 }
3715 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3716 if (curtab != tp)
3717 return FAIL;
3718 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003719 if (curtab != tp)
3720 return FAIL;
3721 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003722#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003723#if defined(FEAT_GUI)
3724 /* Remove the scrollbars. They may be added back later. */
3725 if (gui.in_use)
3726 gui_remove_scrollbars();
3727#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003728 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003729 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003730 tp->tp_firstwin = firstwin;
3731 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003732 tp->tp_old_Rows = Rows;
3733 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003734 firstwin = NULL;
3735 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003736 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003737}
3738
3739/*
3740 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003741 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003742 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3743 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003744 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003745 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003746enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003747 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003748 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003749 int trigger_enter_autocmds UNUSED;
3750 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003751{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003752 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003753 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003754
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003755 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003756 firstwin = tp->tp_firstwin;
3757 lastwin = tp->tp_lastwin;
3758 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003759
3760 /* We would like doing the TabEnter event first, but we don't have a
3761 * valid current window yet, which may break some commands.
3762 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003763 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3764 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003765 prevwin = next_prevwin;
3766
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003767 last_status(FALSE); /* status line may appear or disappear */
3768 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003769 must_redraw = CLEAR; /* need to redraw everything */
3770#ifdef FEAT_DIFF
3771 diff_need_scrollbind = TRUE;
3772#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003773
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003774 /* The tabpage line may have appeared or disappeared, may need to resize
3775 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003776 * frame sizes too. Use the stored value of p_ch, so that it can be
3777 * different for each tab page. */
3778 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003779 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3780#ifdef FEAT_GUI_TABLINE
3781 && !gui_use_tabline()
3782#endif
3783 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003784 shell_new_rows();
3785#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003786 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003787 shell_new_columns(); /* update window widths */
3788#endif
3789
3790#if defined(FEAT_GUI)
3791 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3792 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003793 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003794#endif
3795
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003796#ifdef FEAT_AUTOCMD
3797 /* Apply autocommands after updating the display, when 'rows' and
3798 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003799 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003800 {
3801 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3802 if (old_curbuf != curbuf)
3803 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3804 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003805#endif
3806
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003807 redraw_all_later(CLEAR);
3808}
3809
3810/*
3811 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003812 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003813 */
3814 void
3815goto_tabpage(n)
3816 int n;
3817{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003818 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003819 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003820 int i;
3821
Bram Moolenaard68071d2006-05-02 22:08:30 +00003822 if (text_locked())
3823 {
3824 /* Not allowed when editing the command line. */
3825#ifdef FEAT_CMDWIN
3826 if (cmdwin_type != 0)
3827 EMSG(_(e_cmdwin));
3828 else
3829#endif
3830 EMSG(_(e_secure));
3831 return;
3832 }
3833
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003834 /* If there is only one it can't work. */
3835 if (first_tabpage->tp_next == NULL)
3836 {
3837 if (n > 1)
3838 beep_flush();
3839 return;
3840 }
3841
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003842 if (n == 0)
3843 {
3844 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003845 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003846 tp = first_tabpage;
3847 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003848 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003849 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003850 else if (n < 0)
3851 {
3852 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3853 * this N times. */
3854 ttp = curtab;
3855 for (i = n; i < 0; ++i)
3856 {
3857 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3858 tp = tp->tp_next)
3859 ;
3860 ttp = tp;
3861 }
3862 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003863 else if (n == 9999)
3864 {
3865 /* Go to last tab page. */
3866 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3867 ;
3868 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003869 else
3870 {
3871 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003872 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003873 if (tp == NULL)
3874 {
3875 beep_flush();
3876 return;
3877 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003878 }
3879
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003880 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003881
3882#ifdef FEAT_GUI_TABLINE
3883 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003884 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003885#endif
3886}
3887
3888/*
3889 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003890 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3891 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003892 * Note: doesn't update the GUI tab.
3893 */
3894 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003895goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003896 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003897 int trigger_enter_autocmds;
3898 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003899{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003900 /* Don't repeat a message in another tab page. */
3901 set_keep_msg(NULL, 0);
3902
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003903 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3904 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003905 {
3906 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003907 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3908 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003909 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003910 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3911 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003912 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003913}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003916 * Enter window "wp" in tab page "tp".
3917 * Also updates the GUI tab.
3918 */
3919 void
3920goto_tabpage_win(tp, wp)
3921 tabpage_T *tp;
3922 win_T *wp;
3923{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003924 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003925 if (curtab == tp && win_valid(wp))
3926 {
3927 win_enter(wp, TRUE);
3928# ifdef FEAT_GUI_TABLINE
3929 if (gui_use_tabline())
3930 gui_mch_set_curtab(tabpage_index(curtab));
3931# endif
3932 }
3933}
3934
3935/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003936 * Move the current tab page to before tab page "nr".
3937 */
3938 void
3939tabpage_move(nr)
3940 int nr;
3941{
3942 int n = nr;
3943 tabpage_T *tp;
3944
3945 if (first_tabpage->tp_next == NULL)
3946 return;
3947
3948 /* Remove the current tab page from the list of tab pages. */
3949 if (curtab == first_tabpage)
3950 first_tabpage = curtab->tp_next;
3951 else
3952 {
3953 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3954 if (tp->tp_next == curtab)
3955 break;
3956 if (tp == NULL) /* "cannot happen" */
3957 return;
3958 tp->tp_next = curtab->tp_next;
3959 }
3960
3961 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003962 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003963 {
3964 curtab->tp_next = first_tabpage;
3965 first_tabpage = curtab;
3966 }
3967 else
3968 {
3969 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3970 --n;
3971 curtab->tp_next = tp->tp_next;
3972 tp->tp_next = curtab;
3973 }
3974
3975 /* Need to redraw the tabline. Tab page contents doesn't change. */
3976 redraw_tabline = TRUE;
3977}
3978
3979
3980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 * Go to another window.
3982 * When jumping to another buffer, stop Visual mode. Do this before
3983 * changing windows so we can yank the selection into the '*' register.
3984 * When jumping to another window on the same buffer, adjust its cursor
3985 * position to keep the same Visual area.
3986 */
3987 void
3988win_goto(wp)
3989 win_T *wp;
3990{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02003991#ifdef FEAT_CONCEAL
3992 win_T *owp = curwin;
3993#endif
3994
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003995 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
3997 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003998 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 return;
4000 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004001#ifdef FEAT_AUTOCMD
4002 if (curbuf_locked())
4003 return;
4004#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (wp->w_buffer != curbuf)
4007 reset_VIsual_and_resel();
4008 else if (VIsual_active)
4009 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
4011#ifdef FEAT_GUI
4012 need_mouse_correct = TRUE;
4013#endif
4014 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004015
4016#ifdef FEAT_CONCEAL
4017 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004018 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004019 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004020 if (curwin->w_p_cole > 0 && !msg_scrolled)
4021 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023}
4024
4025#if defined(FEAT_PERL) || defined(PROTO)
4026/*
4027 * Find window number "winnr" (counting top to bottom).
4028 */
4029 win_T *
4030win_find_nr(winnr)
4031 int winnr;
4032{
4033 win_T *wp;
4034
4035# ifdef FEAT_WINDOWS
4036 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4037 if (--winnr == 0)
4038 break;
4039 return wp;
4040# else
4041 return curwin;
4042# endif
4043}
4044#endif
4045
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004046#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4047 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004048/*
4049 * Find the tabpage for window "win".
4050 */
4051 tabpage_T *
4052win_find_tabpage(win)
4053 win_T *win;
4054{
4055 win_T *wp;
4056 tabpage_T *tp;
4057
4058 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004059 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4060 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004061 if (wp == win)
4062 return tp;
4063 return NULL;
4064}
4065#endif
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#ifdef FEAT_VERTSPLIT
4068/*
4069 * Move to window above or below "count" times.
4070 */
4071 static void
4072win_goto_ver(up, count)
4073 int up; /* TRUE to go to win above */
4074 long count;
4075{
4076 frame_T *fr;
4077 frame_T *nfr;
4078 frame_T *foundfr;
4079
4080 foundfr = curwin->w_frame;
4081 while (count--)
4082 {
4083 /*
4084 * First go upwards in the tree of frames until we find a upwards or
4085 * downwards neighbor.
4086 */
4087 fr = foundfr;
4088 for (;;)
4089 {
4090 if (fr == topframe)
4091 goto end;
4092 if (up)
4093 nfr = fr->fr_prev;
4094 else
4095 nfr = fr->fr_next;
4096 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4097 break;
4098 fr = fr->fr_parent;
4099 }
4100
4101 /*
4102 * Now go downwards to find the bottom or top frame in it.
4103 */
4104 for (;;)
4105 {
4106 if (nfr->fr_layout == FR_LEAF)
4107 {
4108 foundfr = nfr;
4109 break;
4110 }
4111 fr = nfr->fr_child;
4112 if (nfr->fr_layout == FR_ROW)
4113 {
4114 /* Find the frame at the cursor row. */
4115 while (fr->fr_next != NULL
4116 && frame2win(fr)->w_wincol + fr->fr_width
4117 <= curwin->w_wincol + curwin->w_wcol)
4118 fr = fr->fr_next;
4119 }
4120 if (nfr->fr_layout == FR_COL && up)
4121 while (fr->fr_next != NULL)
4122 fr = fr->fr_next;
4123 nfr = fr;
4124 }
4125 }
4126end:
4127 if (foundfr != NULL)
4128 win_goto(foundfr->fr_win);
4129}
4130
4131/*
4132 * Move to left or right window.
4133 */
4134 static void
4135win_goto_hor(left, count)
4136 int left; /* TRUE to go to left win */
4137 long count;
4138{
4139 frame_T *fr;
4140 frame_T *nfr;
4141 frame_T *foundfr;
4142
4143 foundfr = curwin->w_frame;
4144 while (count--)
4145 {
4146 /*
4147 * First go upwards in the tree of frames until we find a left or
4148 * right neighbor.
4149 */
4150 fr = foundfr;
4151 for (;;)
4152 {
4153 if (fr == topframe)
4154 goto end;
4155 if (left)
4156 nfr = fr->fr_prev;
4157 else
4158 nfr = fr->fr_next;
4159 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4160 break;
4161 fr = fr->fr_parent;
4162 }
4163
4164 /*
4165 * Now go downwards to find the leftmost or rightmost frame in it.
4166 */
4167 for (;;)
4168 {
4169 if (nfr->fr_layout == FR_LEAF)
4170 {
4171 foundfr = nfr;
4172 break;
4173 }
4174 fr = nfr->fr_child;
4175 if (nfr->fr_layout == FR_COL)
4176 {
4177 /* Find the frame at the cursor row. */
4178 while (fr->fr_next != NULL
4179 && frame2win(fr)->w_winrow + fr->fr_height
4180 <= curwin->w_winrow + curwin->w_wrow)
4181 fr = fr->fr_next;
4182 }
4183 if (nfr->fr_layout == FR_ROW && left)
4184 while (fr->fr_next != NULL)
4185 fr = fr->fr_next;
4186 nfr = fr;
4187 }
4188 }
4189end:
4190 if (foundfr != NULL)
4191 win_goto(foundfr->fr_win);
4192}
4193#endif
4194
4195/*
4196 * Make window "wp" the current window.
4197 */
4198 void
4199win_enter(wp, undo_sync)
4200 win_T *wp;
4201 int undo_sync;
4202{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004203 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204}
4205
4206/*
4207 * Make window wp the current window.
4208 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4209 * been closed and isn't valid.
4210 */
4211 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004212win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 win_T *wp;
4214 int undo_sync;
4215 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004216 int trigger_enter_autocmds UNUSED;
4217 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
4219#ifdef FEAT_AUTOCMD
4220 int other_buffer = FALSE;
4221#endif
4222
4223 if (wp == curwin && !curwin_invalid) /* nothing to do */
4224 return;
4225
4226#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004227 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 /*
4230 * Be careful: If autocommands delete the window, return now.
4231 */
4232 if (wp->w_buffer != curbuf)
4233 {
4234 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4235 other_buffer = TRUE;
4236 if (!win_valid(wp))
4237 return;
4238 }
4239 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4240 if (!win_valid(wp))
4241 return;
4242# ifdef FEAT_EVAL
4243 /* autocmds may abort script processing */
4244 if (aborting())
4245 return;
4246# endif
4247 }
4248#endif
4249
4250 /* sync undo before leaving the current buffer */
4251 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004252 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 /* may have to copy the buffer options when 'cpo' contains 'S' */
4254 if (wp->w_buffer != curbuf)
4255 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4256 if (!curwin_invalid)
4257 {
4258 prevwin = curwin; /* remember for CTRL-W p */
4259 curwin->w_redr_status = TRUE;
4260 }
4261 curwin = wp;
4262 curbuf = wp->w_buffer;
4263 check_cursor();
4264#ifdef FEAT_VIRTUALEDIT
4265 if (!virtual_active())
4266 curwin->w_cursor.coladd = 0;
4267#endif
4268 changed_line_abv_curs(); /* assume cursor position needs updating */
4269
4270 if (curwin->w_localdir != NULL)
4271 {
4272 /* Window has a local directory: Save current directory as global
4273 * directory (unless that was done already) and change to the local
4274 * directory. */
4275 if (globaldir == NULL)
4276 {
4277 char_u cwd[MAXPATHL];
4278
4279 if (mch_dirname(cwd, MAXPATHL) == OK)
4280 globaldir = vim_strsave(cwd);
4281 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004282 if (mch_chdir((char *)curwin->w_localdir) == 0)
4283 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 else if (globaldir != NULL)
4286 {
4287 /* Window doesn't have a local directory and we are not in the global
4288 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004289 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 vim_free(globaldir);
4291 globaldir = NULL;
4292 shorten_fnames(TRUE);
4293 }
4294
4295#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004296 if (trigger_enter_autocmds)
4297 {
4298 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4299 if (other_buffer)
4300 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302#endif
4303
4304#ifdef FEAT_TITLE
4305 maketitle();
4306#endif
4307 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004308 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 if (restart_edit)
4310 redraw_later(VALID); /* causes status line redraw */
4311
4312 /* set window height to desired minimal value */
4313 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4314 win_setheight((int)p_wh);
4315 else if (curwin->w_height == 0)
4316 win_setheight(1);
4317
4318#ifdef FEAT_VERTSPLIT
4319 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004320 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 win_setwidth((int)p_wiw);
4322#endif
4323
4324#ifdef FEAT_MOUSE
4325 setmouse(); /* in case jumped to/from help buffer */
4326#endif
4327
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004328 /* Change directories when the 'acd' option is set. */
4329 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330}
4331
4332#endif /* FEAT_WINDOWS */
4333
4334#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4335/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004336 * Jump to the first open window that contains buffer "buf", if one exists.
4337 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 */
4339 win_T *
4340buf_jump_open_win(buf)
4341 buf_T *buf;
4342{
4343# ifdef FEAT_WINDOWS
4344 win_T *wp;
4345
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004346 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if (wp->w_buffer == buf)
4348 break;
4349 if (wp != NULL)
4350 win_enter(wp, FALSE);
4351 return wp;
4352# else
4353 if (curwin->w_buffer == buf)
4354 return curwin;
4355 return NULL;
4356# endif
4357}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004358
4359/*
4360 * Jump to the first open window in any tab page that contains buffer "buf",
4361 * if one exists.
4362 * Returns a pointer to the window found, otherwise NULL.
4363 */
4364 win_T *
4365buf_jump_open_tab(buf)
4366 buf_T *buf;
4367{
4368# ifdef FEAT_WINDOWS
4369 win_T *wp;
4370 tabpage_T *tp;
4371
4372 /* First try the current tab page. */
4373 wp = buf_jump_open_win(buf);
4374 if (wp != NULL)
4375 return wp;
4376
4377 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4378 if (tp != curtab)
4379 {
4380 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4381 if (wp->w_buffer == buf)
4382 break;
4383 if (wp != NULL)
4384 {
4385 goto_tabpage_win(tp, wp);
4386 if (curwin != wp)
4387 wp = NULL; /* something went wrong */
4388 break;
4389 }
4390 }
4391
4392 return wp;
4393# else
4394 if (curwin->w_buffer == buf)
4395 return curwin;
4396 return NULL;
4397# endif
4398}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399#endif
4400
4401/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004402 * Allocate a window structure and link it in the window list when "hidden" is
4403 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004406win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004407 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004408 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004410 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 /*
4413 * allocate window structure and linesizes arrays
4414 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004415 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004416 if (new_wp == NULL)
4417 return NULL;
4418
4419 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004421 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004422 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424
Bram Moolenaar429fa852013-04-15 12:27:36 +02004425#ifdef FEAT_EVAL
4426 /* init w: variables */
4427 new_wp->w_vars = dict_alloc();
4428 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004430 win_free_lsize(new_wp);
4431 vim_free(new_wp);
4432 return NULL;
4433 }
4434 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004435#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004436
4437#ifdef FEAT_AUTOCMD
4438 /* Don't execute autocommands while the window is not properly
4439 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4440 * event. */
4441 block_autocmds();
4442#endif
4443 /*
4444 * link the window in the window list
4445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004447 if (!hidden)
4448 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449#endif
4450#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004451 new_wp->w_wincol = 0;
4452 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453#endif
4454
Bram Moolenaar429fa852013-04-15 12:27:36 +02004455 /* position the display and the cursor at the top of the file. */
4456 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004458 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004460 new_wp->w_botline = 2;
4461 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004463 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464#endif
4465
Bram Moolenaar429fa852013-04-15 12:27:36 +02004466 /* We won't calculate w_fraction until resizing the window */
4467 new_wp->w_fraction = 0;
4468 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004471 if (gui.in_use)
4472 {
4473 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4474 SBAR_LEFT, new_wp);
4475 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4476 SBAR_RIGHT, new_wp);
4477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478#endif
4479#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004480 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004482#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004483 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004484#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004485#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004486 new_wp->w_match_head = NULL;
4487 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004488#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004489 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490}
4491
4492#if defined(FEAT_WINDOWS) || defined(PROTO)
4493
4494/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004495 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 */
4497 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004498win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004500 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
4502 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004503 buf_T *buf;
4504 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004506#ifdef FEAT_FOLDING
4507 clearFolding(wp);
4508#endif
4509
4510 /* reduce the reference count to the argument list. */
4511 alist_unlink(wp->w_alist);
4512
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004513#ifdef FEAT_AUTOCMD
4514 /* Don't execute autocommands while the window is halfway being deleted.
4515 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004516 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004517#endif
4518
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004519#ifdef FEAT_LUA
4520 lua_window_free(wp);
4521#endif
4522
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004523#ifdef FEAT_MZSCHEME
4524 mzscheme_window_free(wp);
4525#endif
4526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#ifdef FEAT_PERL
4528 perl_win_free(wp);
4529#endif
4530
4531#ifdef FEAT_PYTHON
4532 python_window_free(wp);
4533#endif
4534
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004535#ifdef FEAT_PYTHON3
4536 python3_window_free(wp);
4537#endif
4538
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539#ifdef FEAT_TCL
4540 tcl_window_free(wp);
4541#endif
4542
4543#ifdef FEAT_RUBY
4544 ruby_window_free(wp);
4545#endif
4546
4547 clear_winopt(&wp->w_onebuf_opt);
4548 clear_winopt(&wp->w_allbuf_opt);
4549
4550#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004551 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4552 hash_init(&wp->w_vars->dv_hashtab);
4553 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#endif
4555
4556 if (prevwin == wp)
4557 prevwin = NULL;
4558 win_free_lsize(wp);
4559
4560 for (i = 0; i < wp->w_tagstacklen; ++i)
4561 vim_free(wp->w_tagstack[i].tagname);
4562
4563 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004564
Bram Moolenaarff18df02013-07-24 17:51:57 +02004565 /* Remove the window from the b_wininfo lists, it may happen that the
4566 * freed memory is re-used for another window. */
4567 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4568 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4569 if (wip->wi_win == wp)
4570 wip->wi_win = NULL;
4571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004573 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#ifdef FEAT_JUMPLIST
4577 free_jumplist(wp);
4578#endif
4579
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004580#ifdef FEAT_QUICKFIX
4581 qf_free_all(wp);
4582#endif
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#ifdef FEAT_GUI
4585 if (gui.in_use)
4586 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4588 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4589 }
4590#endif /* FEAT_GUI */
4591
Bram Moolenaar860cae12010-06-05 23:22:07 +02004592#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004593 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004594#endif
4595
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004596#ifdef FEAT_AUTOCMD
4597 if (wp != aucmd_win)
4598#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004599 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004601
4602#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004603 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605}
4606
4607/*
4608 * Append window "wp" in the window list after window "after".
4609 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004610 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611win_append(after, wp)
4612 win_T *after, *wp;
4613{
4614 win_T *before;
4615
4616 if (after == NULL) /* after NULL is in front of the first */
4617 before = firstwin;
4618 else
4619 before = after->w_next;
4620
4621 wp->w_next = before;
4622 wp->w_prev = after;
4623 if (after == NULL)
4624 firstwin = wp;
4625 else
4626 after->w_next = wp;
4627 if (before == NULL)
4628 lastwin = wp;
4629 else
4630 before->w_prev = wp;
4631}
4632
4633/*
4634 * Remove a window from the window list.
4635 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004636 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004637win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004639 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 if (wp->w_prev != NULL)
4642 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004643 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004645 else
4646 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (wp->w_next != NULL)
4648 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004649 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004651 else
4652 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653}
4654
4655/*
4656 * Append frame "frp" in a frame list after frame "after".
4657 */
4658 static void
4659frame_append(after, frp)
4660 frame_T *after, *frp;
4661{
4662 frp->fr_next = after->fr_next;
4663 after->fr_next = frp;
4664 if (frp->fr_next != NULL)
4665 frp->fr_next->fr_prev = frp;
4666 frp->fr_prev = after;
4667}
4668
4669/*
4670 * Insert frame "frp" in a frame list before frame "before".
4671 */
4672 static void
4673frame_insert(before, frp)
4674 frame_T *before, *frp;
4675{
4676 frp->fr_next = before;
4677 frp->fr_prev = before->fr_prev;
4678 before->fr_prev = frp;
4679 if (frp->fr_prev != NULL)
4680 frp->fr_prev->fr_next = frp;
4681 else
4682 frp->fr_parent->fr_child = frp;
4683}
4684
4685/*
4686 * Remove a frame from a frame list.
4687 */
4688 static void
4689frame_remove(frp)
4690 frame_T *frp;
4691{
4692 if (frp->fr_prev != NULL)
4693 frp->fr_prev->fr_next = frp->fr_next;
4694 else
4695 frp->fr_parent->fr_child = frp->fr_next;
4696 if (frp->fr_next != NULL)
4697 frp->fr_next->fr_prev = frp->fr_prev;
4698}
4699
4700#endif /* FEAT_WINDOWS */
4701
4702/*
4703 * Allocate w_lines[] for window "wp".
4704 * Return FAIL for failure, OK for success.
4705 */
4706 int
4707win_alloc_lines(wp)
4708 win_T *wp;
4709{
4710 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004711 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (wp->w_lines == NULL)
4713 return FAIL;
4714 return OK;
4715}
4716
4717/*
4718 * free lsize arrays for a window
4719 */
4720 void
4721win_free_lsize(wp)
4722 win_T *wp;
4723{
4724 vim_free(wp->w_lines);
4725 wp->w_lines = NULL;
4726}
4727
4728/*
4729 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004730 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 */
4732 void
4733shell_new_rows()
4734{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004735 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737 if (firstwin == NULL) /* not initialized yet */
4738 return;
4739#ifdef FEAT_WINDOWS
4740 if (h < frame_minheight(topframe, NULL))
4741 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004742
4743 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * that doesn't result in the right height, forget about that option. */
4745 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004746 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 frame_new_height(topframe, h, FALSE, FALSE);
4748
4749 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4750#else
4751 if (h < 1)
4752 h = 1;
4753 win_new_height(firstwin, h);
4754#endif
4755 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004756#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004757 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004758#endif
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760#if 0
4761 /* Disabled: don't want making the screen smaller make a window larger. */
4762 if (p_ea)
4763 win_equal(curwin, FALSE, 'v');
4764#endif
4765}
4766
4767#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4768/*
4769 * Called from win_new_shellsize() after Columns changed.
4770 */
4771 void
4772shell_new_columns()
4773{
4774 if (firstwin == NULL) /* not initialized yet */
4775 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004776
4777 /* First try setting the widths of windows with 'winfixwidth'. If that
4778 * doesn't result in the right width, forget about that option. */
4779 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004780 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004781 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4782
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4784#if 0
4785 /* Disabled: don't want making the screen smaller make a window larger. */
4786 if (p_ea)
4787 win_equal(curwin, FALSE, 'h');
4788#endif
4789}
4790#endif
4791
4792#if defined(FEAT_CMDWIN) || defined(PROTO)
4793/*
4794 * Save the size of all windows in "gap".
4795 */
4796 void
4797win_size_save(gap)
4798 garray_T *gap;
4799
4800{
4801 win_T *wp;
4802
4803 ga_init2(gap, (int)sizeof(int), 1);
4804 if (ga_grow(gap, win_count() * 2) == OK)
4805 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4806 {
4807 ((int *)gap->ga_data)[gap->ga_len++] =
4808 wp->w_width + wp->w_vsep_width;
4809 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4810 }
4811}
4812
4813/*
4814 * Restore window sizes, but only if the number of windows is still the same.
4815 * Does not free the growarray.
4816 */
4817 void
4818win_size_restore(gap)
4819 garray_T *gap;
4820{
4821 win_T *wp;
4822 int i;
4823
4824 if (win_count() * 2 == gap->ga_len)
4825 {
4826 i = 0;
4827 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4828 {
4829 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4830 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4831 }
4832 /* recompute the window positions */
4833 (void)win_comp_pos();
4834 }
4835}
4836#endif /* FEAT_CMDWIN */
4837
4838#if defined(FEAT_WINDOWS) || defined(PROTO)
4839/*
4840 * Update the position for all windows, using the width and height of the
4841 * frames.
4842 * Returns the row just after the last window.
4843 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004844 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845win_comp_pos()
4846{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004847 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 int col = 0;
4849
4850 frame_comp_pos(topframe, &row, &col);
4851 return row;
4852}
4853
4854/*
4855 * Update the position of the windows in frame "topfrp", using the width and
4856 * height of the frames.
4857 * "*row" and "*col" are the top-left position of the frame. They are updated
4858 * to the bottom-right position plus one.
4859 */
4860 static void
4861frame_comp_pos(topfrp, row, col)
4862 frame_T *topfrp;
4863 int *row;
4864 int *col;
4865{
4866 win_T *wp;
4867 frame_T *frp;
4868#ifdef FEAT_VERTSPLIT
4869 int startcol;
4870 int startrow;
4871#endif
4872
4873 wp = topfrp->fr_win;
4874 if (wp != NULL)
4875 {
4876 if (wp->w_winrow != *row
4877#ifdef FEAT_VERTSPLIT
4878 || wp->w_wincol != *col
4879#endif
4880 )
4881 {
4882 /* position changed, redraw */
4883 wp->w_winrow = *row;
4884#ifdef FEAT_VERTSPLIT
4885 wp->w_wincol = *col;
4886#endif
4887 redraw_win_later(wp, NOT_VALID);
4888 wp->w_redr_status = TRUE;
4889 }
4890 *row += wp->w_height + wp->w_status_height;
4891#ifdef FEAT_VERTSPLIT
4892 *col += wp->w_width + wp->w_vsep_width;
4893#endif
4894 }
4895 else
4896 {
4897#ifdef FEAT_VERTSPLIT
4898 startrow = *row;
4899 startcol = *col;
4900#endif
4901 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4902 {
4903#ifdef FEAT_VERTSPLIT
4904 if (topfrp->fr_layout == FR_ROW)
4905 *row = startrow; /* all frames are at the same row */
4906 else
4907 *col = startcol; /* all frames are at the same col */
4908#endif
4909 frame_comp_pos(frp, row, col);
4910 }
4911 }
4912}
4913
4914#endif /* FEAT_WINDOWS */
4915
4916/*
4917 * Set current window height and take care of repositioning other windows to
4918 * fit around it.
4919 */
4920 void
4921win_setheight(height)
4922 int height;
4923{
4924 win_setheight_win(height, curwin);
4925}
4926
4927/*
4928 * Set the window height of window "win" and take care of repositioning other
4929 * windows to fit around it.
4930 */
4931 void
4932win_setheight_win(height, win)
4933 int height;
4934 win_T *win;
4935{
4936 int row;
4937
4938 if (win == curwin)
4939 {
4940 /* Always keep current window at least one line high, even when
4941 * 'winminheight' is zero. */
4942#ifdef FEAT_WINDOWS
4943 if (height < p_wmh)
4944 height = p_wmh;
4945#endif
4946 if (height == 0)
4947 height = 1;
4948 }
4949
4950#ifdef FEAT_WINDOWS
4951 frame_setheight(win->w_frame, height + win->w_status_height);
4952
4953 /* recompute the window positions */
4954 row = win_comp_pos();
4955#else
4956 if (height > topframe->fr_height)
4957 height = topframe->fr_height;
4958 win->w_height = height;
4959 row = height;
4960#endif
4961
4962 /*
4963 * If there is extra space created between the last window and the command
4964 * line, clear it.
4965 */
4966 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4967 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4968 cmdline_row = row;
4969 msg_row = row;
4970 msg_col = 0;
4971
4972 redraw_all_later(NOT_VALID);
4973}
4974
4975#if defined(FEAT_WINDOWS) || defined(PROTO)
4976
4977/*
4978 * Set the height of a frame to "height" and take care that all frames and
4979 * windows inside it are resized. Also resize frames on the left and right if
4980 * the are in the same FR_ROW frame.
4981 *
4982 * Strategy:
4983 * If the frame is part of a FR_COL frame, try fitting the frame in that
4984 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4985 * go to containing frames to resize them and make room.
4986 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4987 * Check for the minimal height of the FR_ROW frame.
4988 * At the top level we can also use change the command line height.
4989 */
4990 static void
4991frame_setheight(curfrp, height)
4992 frame_T *curfrp;
4993 int height;
4994{
4995 int room; /* total number of lines available */
4996 int take; /* number of lines taken from other windows */
4997 int room_cmdline; /* lines available from cmdline */
4998 int run;
4999 frame_T *frp;
5000 int h;
5001 int room_reserved;
5002
5003 /* If the height already is the desired value, nothing to do. */
5004 if (curfrp->fr_height == height)
5005 return;
5006
5007 if (curfrp->fr_parent == NULL)
5008 {
5009 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005010 if (height > ROWS_AVAIL)
5011 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (height > 0)
5013 frame_new_height(curfrp, height, FALSE, FALSE);
5014 }
5015 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5016 {
5017 /* Row of frames: Also need to resize frames left and right of this
5018 * one. First check for the minimal height of these. */
5019 h = frame_minheight(curfrp->fr_parent, NULL);
5020 if (height < h)
5021 height = h;
5022 frame_setheight(curfrp->fr_parent, height);
5023 }
5024 else
5025 {
5026 /*
5027 * Column of frames: try to change only frames in this column.
5028 */
5029#ifdef FEAT_VERTSPLIT
5030 /*
5031 * Do this twice:
5032 * 1: compute room available, if it's not enough try resizing the
5033 * containing frame.
5034 * 2: compute the room available and adjust the height to it.
5035 * Try not to reduce the height of a window with 'winfixheight' set.
5036 */
5037 for (run = 1; run <= 2; ++run)
5038#else
5039 for (;;)
5040#endif
5041 {
5042 room = 0;
5043 room_reserved = 0;
5044 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5045 frp = frp->fr_next)
5046 {
5047 if (frp != curfrp
5048 && frp->fr_win != NULL
5049 && frp->fr_win->w_p_wfh)
5050 room_reserved += frp->fr_height;
5051 room += frp->fr_height;
5052 if (frp != curfrp)
5053 room -= frame_minheight(frp, NULL);
5054 }
5055#ifdef FEAT_VERTSPLIT
5056 if (curfrp->fr_width != Columns)
5057 room_cmdline = 0;
5058 else
5059#endif
5060 {
5061 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5062 + lastwin->w_height + lastwin->w_status_height);
5063 if (room_cmdline < 0)
5064 room_cmdline = 0;
5065 }
5066
5067 if (height <= room + room_cmdline)
5068 break;
5069#ifdef FEAT_VERTSPLIT
5070 if (run == 2 || curfrp->fr_width == Columns)
5071#endif
5072 {
5073 if (height > room + room_cmdline)
5074 height = room + room_cmdline;
5075 break;
5076 }
5077#ifdef FEAT_VERTSPLIT
5078 frame_setheight(curfrp->fr_parent, height
5079 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5080#endif
5081 /*NOTREACHED*/
5082 }
5083
5084 /*
5085 * Compute the number of lines we will take from others frames (can be
5086 * negative!).
5087 */
5088 take = height - curfrp->fr_height;
5089
5090 /* If there is not enough room, also reduce the height of a window
5091 * with 'winfixheight' set. */
5092 if (height > room + room_cmdline - room_reserved)
5093 room_reserved = room + room_cmdline - height;
5094 /* If there is only a 'winfixheight' window and making the
5095 * window smaller, need to make the other window taller. */
5096 if (take < 0 && room - curfrp->fr_height < room_reserved)
5097 room_reserved = 0;
5098
5099 if (take > 0 && room_cmdline > 0)
5100 {
5101 /* use lines from cmdline first */
5102 if (take < room_cmdline)
5103 room_cmdline = take;
5104 take -= room_cmdline;
5105 topframe->fr_height += room_cmdline;
5106 }
5107
5108 /*
5109 * set the current frame to the new height
5110 */
5111 frame_new_height(curfrp, height, FALSE, FALSE);
5112
5113 /*
5114 * First take lines from the frames after the current frame. If
5115 * that is not enough, takes lines from frames above the current
5116 * frame.
5117 */
5118 for (run = 0; run < 2; ++run)
5119 {
5120 if (run == 0)
5121 frp = curfrp->fr_next; /* 1st run: start with next window */
5122 else
5123 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5124 while (frp != NULL && take != 0)
5125 {
5126 h = frame_minheight(frp, NULL);
5127 if (room_reserved > 0
5128 && frp->fr_win != NULL
5129 && frp->fr_win->w_p_wfh)
5130 {
5131 if (room_reserved >= frp->fr_height)
5132 room_reserved -= frp->fr_height;
5133 else
5134 {
5135 if (frp->fr_height - room_reserved > take)
5136 room_reserved = frp->fr_height - take;
5137 take -= frp->fr_height - room_reserved;
5138 frame_new_height(frp, room_reserved, FALSE, FALSE);
5139 room_reserved = 0;
5140 }
5141 }
5142 else
5143 {
5144 if (frp->fr_height - take < h)
5145 {
5146 take -= frp->fr_height - h;
5147 frame_new_height(frp, h, FALSE, FALSE);
5148 }
5149 else
5150 {
5151 frame_new_height(frp, frp->fr_height - take,
5152 FALSE, FALSE);
5153 take = 0;
5154 }
5155 }
5156 if (run == 0)
5157 frp = frp->fr_next;
5158 else
5159 frp = frp->fr_prev;
5160 }
5161 }
5162 }
5163}
5164
5165#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5166/*
5167 * Set current window width and take care of repositioning other windows to
5168 * fit around it.
5169 */
5170 void
5171win_setwidth(width)
5172 int width;
5173{
5174 win_setwidth_win(width, curwin);
5175}
5176
5177 void
5178win_setwidth_win(width, wp)
5179 int width;
5180 win_T *wp;
5181{
5182 /* Always keep current window at least one column wide, even when
5183 * 'winminwidth' is zero. */
5184 if (wp == curwin)
5185 {
5186 if (width < p_wmw)
5187 width = p_wmw;
5188 if (width == 0)
5189 width = 1;
5190 }
5191
5192 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5193
5194 /* recompute the window positions */
5195 (void)win_comp_pos();
5196
5197 redraw_all_later(NOT_VALID);
5198}
5199
5200/*
5201 * Set the width of a frame to "width" and take care that all frames and
5202 * windows inside it are resized. Also resize frames above and below if the
5203 * are in the same FR_ROW frame.
5204 *
5205 * Strategy is similar to frame_setheight().
5206 */
5207 static void
5208frame_setwidth(curfrp, width)
5209 frame_T *curfrp;
5210 int width;
5211{
5212 int room; /* total number of lines available */
5213 int take; /* number of lines taken from other windows */
5214 int run;
5215 frame_T *frp;
5216 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005217 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218
5219 /* If the width already is the desired value, nothing to do. */
5220 if (curfrp->fr_width == width)
5221 return;
5222
5223 if (curfrp->fr_parent == NULL)
5224 /* topframe: can't change width */
5225 return;
5226
5227 if (curfrp->fr_parent->fr_layout == FR_COL)
5228 {
5229 /* Column of frames: Also need to resize frames above and below of
5230 * this one. First check for the minimal width of these. */
5231 w = frame_minwidth(curfrp->fr_parent, NULL);
5232 if (width < w)
5233 width = w;
5234 frame_setwidth(curfrp->fr_parent, width);
5235 }
5236 else
5237 {
5238 /*
5239 * Row of frames: try to change only frames in this row.
5240 *
5241 * Do this twice:
5242 * 1: compute room available, if it's not enough try resizing the
5243 * containing frame.
5244 * 2: compute the room available and adjust the width to it.
5245 */
5246 for (run = 1; run <= 2; ++run)
5247 {
5248 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005249 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5251 frp = frp->fr_next)
5252 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005253 if (frp != curfrp
5254 && frp->fr_win != NULL
5255 && frp->fr_win->w_p_wfw)
5256 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 room += frp->fr_width;
5258 if (frp != curfrp)
5259 room -= frame_minwidth(frp, NULL);
5260 }
5261
5262 if (width <= room)
5263 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005264 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
5266 if (width > room)
5267 width = room;
5268 break;
5269 }
5270 frame_setwidth(curfrp->fr_parent, width
5271 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5272 }
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 /*
5275 * Compute the number of lines we will take from others frames (can be
5276 * negative!).
5277 */
5278 take = width - curfrp->fr_width;
5279
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005280 /* If there is not enough room, also reduce the width of a window
5281 * with 'winfixwidth' set. */
5282 if (width > room - room_reserved)
5283 room_reserved = room - width;
5284 /* If there is only a 'winfixwidth' window and making the
5285 * window smaller, need to make the other window narrower. */
5286 if (take < 0 && room - curfrp->fr_width < room_reserved)
5287 room_reserved = 0;
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 /*
5290 * set the current frame to the new width
5291 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005292 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294 /*
5295 * First take lines from the frames right of the current frame. If
5296 * that is not enough, takes lines from frames left of the current
5297 * frame.
5298 */
5299 for (run = 0; run < 2; ++run)
5300 {
5301 if (run == 0)
5302 frp = curfrp->fr_next; /* 1st run: start with next window */
5303 else
5304 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5305 while (frp != NULL && take != 0)
5306 {
5307 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005308 if (room_reserved > 0
5309 && frp->fr_win != NULL
5310 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005312 if (room_reserved >= frp->fr_width)
5313 room_reserved -= frp->fr_width;
5314 else
5315 {
5316 if (frp->fr_width - room_reserved > take)
5317 room_reserved = frp->fr_width - take;
5318 take -= frp->fr_width - room_reserved;
5319 frame_new_width(frp, room_reserved, FALSE, FALSE);
5320 room_reserved = 0;
5321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
5323 else
5324 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005325 if (frp->fr_width - take < w)
5326 {
5327 take -= frp->fr_width - w;
5328 frame_new_width(frp, w, FALSE, FALSE);
5329 }
5330 else
5331 {
5332 frame_new_width(frp, frp->fr_width - take,
5333 FALSE, FALSE);
5334 take = 0;
5335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337 if (run == 0)
5338 frp = frp->fr_next;
5339 else
5340 frp = frp->fr_prev;
5341 }
5342 }
5343 }
5344}
5345#endif /* FEAT_VERTSPLIT */
5346
5347/*
5348 * Check 'winminheight' for a valid value.
5349 */
5350 void
5351win_setminheight()
5352{
5353 int room;
5354 int first = TRUE;
5355 win_T *wp;
5356
5357 /* loop until there is a 'winminheight' that is possible */
5358 while (p_wmh > 0)
5359 {
5360 /* TODO: handle vertical splits */
5361 room = -p_wh;
5362 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5363 room += wp->w_height - p_wmh;
5364 if (room >= 0)
5365 break;
5366 --p_wmh;
5367 if (first)
5368 {
5369 EMSG(_(e_noroom));
5370 first = FALSE;
5371 }
5372 }
5373}
5374
5375#ifdef FEAT_MOUSE
5376
5377/*
5378 * Status line of dragwin is dragged "offset" lines down (negative is up).
5379 */
5380 void
5381win_drag_status_line(dragwin, offset)
5382 win_T *dragwin;
5383 int offset;
5384{
5385 frame_T *curfr;
5386 frame_T *fr;
5387 int room;
5388 int row;
5389 int up; /* if TRUE, drag status line up, otherwise down */
5390 int n;
5391
5392 fr = dragwin->w_frame;
5393 curfr = fr;
5394 if (fr != topframe) /* more than one window */
5395 {
5396 fr = fr->fr_parent;
5397 /* When the parent frame is not a column of frames, its parent should
5398 * be. */
5399 if (fr->fr_layout != FR_COL)
5400 {
5401 curfr = fr;
5402 if (fr != topframe) /* only a row of windows, may drag statusline */
5403 fr = fr->fr_parent;
5404 }
5405 }
5406
5407 /* If this is the last frame in a column, may want to resize the parent
5408 * frame instead (go two up to skip a row of frames). */
5409 while (curfr != topframe && curfr->fr_next == NULL)
5410 {
5411 if (fr != topframe)
5412 fr = fr->fr_parent;
5413 curfr = fr;
5414 if (fr != topframe)
5415 fr = fr->fr_parent;
5416 }
5417
5418 if (offset < 0) /* drag up */
5419 {
5420 up = TRUE;
5421 offset = -offset;
5422 /* sum up the room of the current frame and above it */
5423 if (fr == curfr)
5424 {
5425 /* only one window */
5426 room = fr->fr_height - frame_minheight(fr, NULL);
5427 }
5428 else
5429 {
5430 room = 0;
5431 for (fr = fr->fr_child; ; fr = fr->fr_next)
5432 {
5433 room += fr->fr_height - frame_minheight(fr, NULL);
5434 if (fr == curfr)
5435 break;
5436 }
5437 }
5438 fr = curfr->fr_next; /* put fr at frame that grows */
5439 }
5440 else /* drag down */
5441 {
5442 up = FALSE;
5443 /*
5444 * Only dragging the last status line can reduce p_ch.
5445 */
5446 room = Rows - cmdline_row;
5447 if (curfr->fr_next == NULL)
5448 room -= 1;
5449 else
5450 room -= p_ch;
5451 if (room < 0)
5452 room = 0;
5453 /* sum up the room of frames below of the current one */
5454 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5455 room += fr->fr_height - frame_minheight(fr, NULL);
5456 fr = curfr; /* put fr at window that grows */
5457 }
5458
5459 if (room < offset) /* Not enough room */
5460 offset = room; /* Move as far as we can */
5461 if (offset <= 0)
5462 return;
5463
5464 /*
5465 * Grow frame fr by "offset" lines.
5466 * Doesn't happen when dragging the last status line up.
5467 */
5468 if (fr != NULL)
5469 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5470
5471 if (up)
5472 fr = curfr; /* current frame gets smaller */
5473 else
5474 fr = curfr->fr_next; /* next frame gets smaller */
5475
5476 /*
5477 * Now make the other frames smaller.
5478 */
5479 while (fr != NULL && offset > 0)
5480 {
5481 n = frame_minheight(fr, NULL);
5482 if (fr->fr_height - offset <= n)
5483 {
5484 offset -= fr->fr_height - n;
5485 frame_new_height(fr, n, !up, FALSE);
5486 }
5487 else
5488 {
5489 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5490 break;
5491 }
5492 if (up)
5493 fr = fr->fr_prev;
5494 else
5495 fr = fr->fr_next;
5496 }
5497 row = win_comp_pos();
5498 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5499 cmdline_row = row;
5500 p_ch = Rows - cmdline_row;
5501 if (p_ch < 1)
5502 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005503 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005504 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 showmode();
5506}
5507
5508#ifdef FEAT_VERTSPLIT
5509/*
5510 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5511 */
5512 void
5513win_drag_vsep_line(dragwin, offset)
5514 win_T *dragwin;
5515 int offset;
5516{
5517 frame_T *curfr;
5518 frame_T *fr;
5519 int room;
5520 int left; /* if TRUE, drag separator line left, otherwise right */
5521 int n;
5522
5523 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005524 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return;
5526 curfr = fr;
5527 fr = fr->fr_parent;
5528 /* When the parent frame is not a row of frames, its parent should be. */
5529 if (fr->fr_layout != FR_ROW)
5530 {
5531 if (fr == topframe) /* only a column of windows (cannot happen?) */
5532 return;
5533 curfr = fr;
5534 fr = fr->fr_parent;
5535 }
5536
5537 /* If this is the last frame in a row, may want to resize a parent
5538 * frame instead. */
5539 while (curfr->fr_next == NULL)
5540 {
5541 if (fr == topframe)
5542 break;
5543 curfr = fr;
5544 fr = fr->fr_parent;
5545 if (fr != topframe)
5546 {
5547 curfr = fr;
5548 fr = fr->fr_parent;
5549 }
5550 }
5551
5552 if (offset < 0) /* drag left */
5553 {
5554 left = TRUE;
5555 offset = -offset;
5556 /* sum up the room of the current frame and left of it */
5557 room = 0;
5558 for (fr = fr->fr_child; ; fr = fr->fr_next)
5559 {
5560 room += fr->fr_width - frame_minwidth(fr, NULL);
5561 if (fr == curfr)
5562 break;
5563 }
5564 fr = curfr->fr_next; /* put fr at frame that grows */
5565 }
5566 else /* drag right */
5567 {
5568 left = FALSE;
5569 /* sum up the room of frames right of the current one */
5570 room = 0;
5571 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5572 room += fr->fr_width - frame_minwidth(fr, NULL);
5573 fr = curfr; /* put fr at window that grows */
5574 }
5575
5576 if (room < offset) /* Not enough room */
5577 offset = room; /* Move as far as we can */
5578 if (offset <= 0) /* No room at all, quit. */
5579 return;
5580
5581 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005582 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
5584 /* shrink other frames: current and at the left or at the right */
5585 if (left)
5586 fr = curfr; /* current frame gets smaller */
5587 else
5588 fr = curfr->fr_next; /* next frame gets smaller */
5589
5590 while (fr != NULL && offset > 0)
5591 {
5592 n = frame_minwidth(fr, NULL);
5593 if (fr->fr_width - offset <= n)
5594 {
5595 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005596 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
5598 else
5599 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005600 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 break;
5602 }
5603 if (left)
5604 fr = fr->fr_prev;
5605 else
5606 fr = fr->fr_next;
5607 }
5608 (void)win_comp_pos();
5609 redraw_all_later(NOT_VALID);
5610}
5611#endif /* FEAT_VERTSPLIT */
5612#endif /* FEAT_MOUSE */
5613
5614#endif /* FEAT_WINDOWS */
5615
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005616#define FRACTION_MULT 16384L
5617
5618/*
5619 * Set wp->w_fraction for the current w_wrow and w_height.
5620 */
5621 static void
5622set_fraction(wp)
5623 win_T *wp;
5624{
5625 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005626 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005627}
5628
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629/*
5630 * Set the height of a window.
5631 * This takes care of the things inside the window, not what happens to the
5632 * window position, the frame or to other windows.
5633 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005634 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635win_new_height(wp, height)
5636 win_T *wp;
5637 int height;
5638{
5639 linenr_T lnum;
5640 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005641 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
5643 /* Don't want a negative height. Happens when splitting a tiny window.
5644 * Will equalize heights soon to fix it. */
5645 if (height < 0)
5646 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005647 if (wp->w_height == height)
5648 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005650 if (wp->w_height > 0)
5651 {
5652 if (wp == curwin)
5653 validate_cursor(); /* w_wrow needs to be valid */
5654 if (wp->w_wrow != wp->w_prev_fraction_row)
5655 set_fraction(wp);
5656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
5658 wp->w_height = height;
5659 wp->w_skipcol = 0;
5660
5661 /* Don't change w_topline when height is zero. Don't set w_topline when
5662 * 'scrollbind' is set and this isn't the current window. */
5663 if (height > 0
5664#ifdef FEAT_SCROLLBIND
5665 && (!wp->w_p_scb || wp == curwin)
5666#endif
5667 )
5668 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005669 /*
5670 * Find a value for w_topline that shows the cursor at the same
5671 * relative position in the window as before (more or less).
5672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 lnum = wp->w_cursor.lnum;
5674 if (lnum < 1) /* can happen when starting up */
5675 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005676 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5677 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5679 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005680
5681 if (sline >= 0)
5682 {
5683 /* Make sure the whole cursor line is visible, if possible. */
5684 int rows = plines_win(wp, lnum, FALSE);
5685
5686 if (sline > wp->w_height - rows)
5687 {
5688 sline = wp->w_height - rows;
5689 wp->w_wrow -= rows - line_size;
5690 }
5691 }
5692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 if (sline < 0)
5694 {
5695 /*
5696 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005697 * Make cursor line the first line in the window. If not enough
5698 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 */
5700 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005701 if (wp->w_wrow >= wp->w_height
5702 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5703 {
5704 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5705 --wp->w_wrow;
5706 while (wp->w_wrow >= wp->w_height)
5707 {
5708 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5709 + win_col_off2(wp);
5710 --wp->w_wrow;
5711 }
5712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 else
5715 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005716 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 {
5718#ifdef FEAT_FOLDING
5719 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5720 if (lnum == 1)
5721 {
5722 /* first line in buffer is folded */
5723 line_size = 1;
5724 --sline;
5725 break;
5726 }
5727#endif
5728 --lnum;
5729#ifdef FEAT_DIFF
5730 if (lnum == wp->w_topline)
5731 line_size = plines_win_nofill(wp, lnum, TRUE)
5732 + wp->w_topfill;
5733 else
5734#endif
5735 line_size = plines_win(wp, lnum, TRUE);
5736 sline -= line_size;
5737 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 if (sline < 0)
5740 {
5741 /*
5742 * Line we want at top would go off top of screen. Use next
5743 * line instead.
5744 */
5745#ifdef FEAT_FOLDING
5746 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5747#endif
5748 lnum++;
5749 wp->w_wrow -= line_size + sline;
5750 }
5751 else if (sline > 0)
5752 {
5753 /* First line of file reached, use that as topline. */
5754 lnum = 1;
5755 wp->w_wrow -= sline;
5756 }
5757 }
5758 set_topline(wp, lnum);
5759 }
5760
5761 if (wp == curwin)
5762 {
5763 if (p_so)
5764 update_topline();
5765 curs_columns(FALSE); /* validate w_wrow */
5766 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005767 if (prev_height > 0)
5768 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005771 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#ifdef FEAT_WINDOWS
5773 wp->w_redr_status = TRUE;
5774#endif
5775 invalidate_botline_win(wp);
5776}
5777
5778#ifdef FEAT_VERTSPLIT
5779/*
5780 * Set the width of a window.
5781 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005782 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783win_new_width(wp, width)
5784 win_T *wp;
5785 int width;
5786{
5787 wp->w_width = width;
5788 wp->w_lines_valid = 0;
5789 changed_line_abv_curs_win(wp);
5790 invalidate_botline_win(wp);
5791 if (wp == curwin)
5792 {
5793 update_topline();
5794 curs_columns(TRUE); /* validate w_wrow */
5795 }
5796 redraw_win_later(wp, NOT_VALID);
5797 wp->w_redr_status = TRUE;
5798}
5799#endif
5800
5801 void
5802win_comp_scroll(wp)
5803 win_T *wp;
5804{
5805 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5806 if (wp->w_p_scr == 0)
5807 wp->w_p_scr = 1;
5808}
5809
5810/*
5811 * command_height: called whenever p_ch has been changed
5812 */
5813 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005814command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816#ifdef FEAT_WINDOWS
5817 int h;
5818 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005819 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005821 /* Use the value of p_ch that we remembered. This is needed for when the
5822 * GUI starts up, we can't be sure in what order things happen. And when
5823 * p_ch was changed in another tab page. */
5824 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 /* Find bottom frame with width of screen. */
5827 frp = lastwin->w_frame;
5828# ifdef FEAT_VERTSPLIT
5829 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5830 frp = frp->fr_parent;
5831# endif
5832
5833 /* Avoid changing the height of a window with 'winfixheight' set. */
5834 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5835 && frp->fr_win->w_p_wfh)
5836 frp = frp->fr_prev;
5837
5838 if (starting != NO_SCREEN)
5839 {
5840 cmdline_row = Rows - p_ch;
5841
5842 if (p_ch > old_p_ch) /* p_ch got bigger */
5843 {
5844 while (p_ch > old_p_ch)
5845 {
5846 if (frp == NULL)
5847 {
5848 EMSG(_(e_noroom));
5849 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005850 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 cmdline_row = Rows - p_ch;
5852 break;
5853 }
5854 h = frp->fr_height - frame_minheight(frp, NULL);
5855 if (h > p_ch - old_p_ch)
5856 h = p_ch - old_p_ch;
5857 old_p_ch += h;
5858 frame_add_height(frp, -h);
5859 frp = frp->fr_prev;
5860 }
5861
5862 /* Recompute window positions. */
5863 (void)win_comp_pos();
5864
5865 /* clear the lines added to cmdline */
5866 if (full_screen)
5867 screen_fill((int)(cmdline_row), (int)Rows, 0,
5868 (int)Columns, ' ', ' ', 0);
5869 msg_row = cmdline_row;
5870 redraw_cmdline = TRUE;
5871 return;
5872 }
5873
5874 if (msg_row < cmdline_row)
5875 msg_row = cmdline_row;
5876 redraw_cmdline = TRUE;
5877 }
5878 frame_add_height(frp, (int)(old_p_ch - p_ch));
5879
5880 /* Recompute window positions. */
5881 if (frp != lastwin->w_frame)
5882 (void)win_comp_pos();
5883#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005885 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886#endif
5887}
5888
5889#if defined(FEAT_WINDOWS) || defined(PROTO)
5890/*
5891 * Resize frame "frp" to be "n" lines higher (negative for less high).
5892 * Also resize the frames it is contained in.
5893 */
5894 static void
5895frame_add_height(frp, n)
5896 frame_T *frp;
5897 int n;
5898{
5899 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5900 for (;;)
5901 {
5902 frp = frp->fr_parent;
5903 if (frp == NULL)
5904 break;
5905 frp->fr_height += n;
5906 }
5907}
5908
5909/*
5910 * Add or remove a status line for the bottom window(s), according to the
5911 * value of 'laststatus'.
5912 */
5913 void
5914last_status(morewin)
5915 int morewin; /* pretend there are two or more windows */
5916{
5917 /* Don't make a difference between horizontal or vertical split. */
5918 last_status_rec(topframe, (p_ls == 2
5919 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5920}
5921
5922 static void
5923last_status_rec(fr, statusline)
5924 frame_T *fr;
5925 int statusline;
5926{
5927 frame_T *fp;
5928 win_T *wp;
5929
5930 if (fr->fr_layout == FR_LEAF)
5931 {
5932 wp = fr->fr_win;
5933 if (wp->w_status_height != 0 && !statusline)
5934 {
5935 /* remove status line */
5936 win_new_height(wp, wp->w_height + 1);
5937 wp->w_status_height = 0;
5938 comp_col();
5939 }
5940 else if (wp->w_status_height == 0 && statusline)
5941 {
5942 /* Find a frame to take a line from. */
5943 fp = fr;
5944 while (fp->fr_height <= frame_minheight(fp, NULL))
5945 {
5946 if (fp == topframe)
5947 {
5948 EMSG(_(e_noroom));
5949 return;
5950 }
5951 /* In a column of frames: go to frame above. If already at
5952 * the top or in a row of frames: go to parent. */
5953 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5954 fp = fp->fr_prev;
5955 else
5956 fp = fp->fr_parent;
5957 }
5958 wp->w_status_height = 1;
5959 if (fp != fr)
5960 {
5961 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5962 frame_fix_height(wp);
5963 (void)win_comp_pos();
5964 }
5965 else
5966 win_new_height(wp, wp->w_height - 1);
5967 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005968 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 }
5970 }
5971#ifdef FEAT_VERTSPLIT
5972 else if (fr->fr_layout == FR_ROW)
5973 {
5974 /* vertically split windows, set status line for each one */
5975 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5976 last_status_rec(fp, statusline);
5977 }
5978#endif
5979 else
5980 {
5981 /* horizontally split window, set status line for last one */
5982 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5983 ;
5984 last_status_rec(fp, statusline);
5985 }
5986}
5987
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005988/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005989 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005990 */
5991 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005992tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005994#ifdef FEAT_GUI_TABLINE
5995 /* When the GUI has the tabline then this always returns zero. */
5996 if (gui_use_tabline())
5997 return 0;
5998#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005999 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006000 {
6001 case 0: return 0;
6002 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6003 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006004 return 1;
6005}
6006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007#endif /* FEAT_WINDOWS */
6008
6009#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6010/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006011 * Get the file name at the cursor.
6012 * If Visual mode is active, use the selected text if it's in one line.
6013 * Returns the name in allocated memory, NULL for failure.
6014 */
6015 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006016grab_file_name(count, file_lnum)
6017 long count;
6018 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006019{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006020 if (VIsual_active)
6021 {
6022 int len;
6023 char_u *ptr;
6024
6025 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6026 return NULL;
6027 return find_file_name_in_path(ptr, len,
6028 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6029 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006030 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6031 file_lnum);
6032
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006033}
6034
6035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 * Return the file name under or after the cursor.
6037 *
6038 * The 'path' option is searched if the file name is not absolute.
6039 * The string returned has been alloc'ed and should be freed by the caller.
6040 * NULL is returned if the file name or file is not found.
6041 *
6042 * options:
6043 * FNAME_MESS give error messages
6044 * FNAME_EXP expand to path
6045 * FNAME_HYP check for hypertext link
6046 * FNAME_INCL apply "includeexpr"
6047 */
6048 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006049file_name_at_cursor(options, count, file_lnum)
6050 int options;
6051 long count;
6052 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006055 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6056 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057}
6058
6059/*
6060 * Return the name of the file under or after ptr[col].
6061 * Otherwise like file_name_at_cursor().
6062 */
6063 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006064file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 char_u *line;
6066 int col;
6067 int options;
6068 long count;
6069 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006070 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 char_u *ptr;
6073 int len;
6074
6075 /*
6076 * search forward for what could be the start of a file name
6077 */
6078 ptr = line + col;
6079 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006080 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (*ptr == NUL) /* nothing found */
6082 {
6083 if (options & FNAME_MESS)
6084 EMSG(_("E446: No file name under cursor"));
6085 return NULL;
6086 }
6087
6088 /*
6089 * Search backward for first char of the file name.
6090 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6091 */
6092 while (ptr > line)
6093 {
6094#ifdef FEAT_MBYTE
6095 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6096 ptr -= len + 1;
6097 else
6098#endif
6099 if (vim_isfilec(ptr[-1])
6100 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6101 --ptr;
6102 else
6103 break;
6104 }
6105
6106 /*
6107 * Search forward for the last char of the file name.
6108 * Also allow "://" when ':' is not in 'isfname'.
6109 */
6110 len = 0;
6111 while (vim_isfilec(ptr[len])
6112 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6113#ifdef FEAT_MBYTE
6114 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006115 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 else
6117#endif
6118 ++len;
6119
6120 /*
6121 * If there is trailing punctuation, remove it.
6122 * But don't remove "..", could be a directory name.
6123 */
6124 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6125 && ptr[len - 2] != '.')
6126 --len;
6127
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006128 if (file_lnum != NULL)
6129 {
6130 char_u *p;
6131
6132 /* Get the number after the file name and a separator character */
6133 p = ptr + len;
6134 p = skipwhite(p);
6135 if (*p != NUL)
6136 {
6137 if (!isdigit(*p))
6138 ++p; /* skip the separator */
6139 p = skipwhite(p);
6140 if (isdigit(*p))
6141 *file_lnum = (int)getdigits(&p);
6142 }
6143 }
6144
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6146}
6147
6148# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6149static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6150
6151 static char_u *
6152eval_includeexpr(ptr, len)
6153 char_u *ptr;
6154 int len;
6155{
6156 char_u *res;
6157
6158 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006159 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006160 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 set_vim_var_string(VV_FNAME, NULL, 0);
6162 return res;
6163}
6164#endif
6165
6166/*
6167 * Return the name of the file ptr[len] in 'path'.
6168 * Otherwise like file_name_at_cursor().
6169 */
6170 char_u *
6171find_file_name_in_path(ptr, len, options, count, rel_fname)
6172 char_u *ptr;
6173 int len;
6174 int options;
6175 long count;
6176 char_u *rel_fname; /* file we are searching relative to */
6177{
6178 char_u *file_name;
6179 int c;
6180# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6181 char_u *tofree = NULL;
6182
6183 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6184 {
6185 tofree = eval_includeexpr(ptr, len);
6186 if (tofree != NULL)
6187 {
6188 ptr = tofree;
6189 len = (int)STRLEN(ptr);
6190 }
6191 }
6192# endif
6193
6194 if (options & FNAME_EXP)
6195 {
6196 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6197 TRUE, rel_fname);
6198
6199# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6200 /*
6201 * If the file could not be found in a normal way, try applying
6202 * 'includeexpr' (unless done already).
6203 */
6204 if (file_name == NULL
6205 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6206 {
6207 tofree = eval_includeexpr(ptr, len);
6208 if (tofree != NULL)
6209 {
6210 ptr = tofree;
6211 len = (int)STRLEN(ptr);
6212 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6213 TRUE, rel_fname);
6214 }
6215 }
6216# endif
6217 if (file_name == NULL && (options & FNAME_MESS))
6218 {
6219 c = ptr[len];
6220 ptr[len] = NUL;
6221 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6222 ptr[len] = c;
6223 }
6224
6225 /* Repeat finding the file "count" times. This matters when it
6226 * appears several times in the path. */
6227 while (file_name != NULL && --count > 0)
6228 {
6229 vim_free(file_name);
6230 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6231 }
6232 }
6233 else
6234 file_name = vim_strnsave(ptr, len);
6235
6236# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6237 vim_free(tofree);
6238# endif
6239
6240 return file_name;
6241}
6242#endif /* FEAT_SEARCHPATH */
6243
6244/*
6245 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6246 * Also check for ":\\", which MS Internet Explorer accepts, return
6247 * URL_BACKSLASH.
6248 */
6249 static int
6250path_is_url(p)
6251 char_u *p;
6252{
6253 if (STRNCMP(p, "://", (size_t)3) == 0)
6254 return URL_SLASH;
6255 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6256 return URL_BACKSLASH;
6257 return 0;
6258}
6259
6260/*
6261 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6262 * Return URL_BACKSLASH for "name:\\".
6263 * Return zero otherwise.
6264 */
6265 int
6266path_with_url(fname)
6267 char_u *fname;
6268{
6269 char_u *p;
6270
6271 for (p = fname; isalpha(*p); ++p)
6272 ;
6273 return path_is_url(p);
6274}
6275
6276/*
6277 * Return TRUE if "name" is a full (absolute) path name or URL.
6278 */
6279 int
6280vim_isAbsName(name)
6281 char_u *name;
6282{
6283 return (path_with_url(name) != 0 || mch_isFullName(name));
6284}
6285
6286/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006287 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 *
6289 * return FAIL for failure, OK otherwise
6290 */
6291 int
6292vim_FullName(fname, buf, len, force)
6293 char_u *fname, *buf;
6294 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006295 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
6297 int retval = OK;
6298 int url;
6299
6300 *buf = NUL;
6301 if (fname == NULL)
6302 return FAIL;
6303
6304 url = path_with_url(fname);
6305 if (!url)
6306 retval = mch_FullName(fname, buf, len, force);
6307 if (url || retval == FAIL)
6308 {
6309 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006310 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
6312#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6313 slash_adjust(buf);
6314#endif
6315 return retval;
6316}
6317
6318/*
6319 * Return the minimal number of rows that is needed on the screen to display
6320 * the current number of windows.
6321 */
6322 int
6323min_rows()
6324{
6325 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006326#ifdef FEAT_WINDOWS
6327 tabpage_T *tp;
6328 int n;
6329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331 if (firstwin == NULL) /* not initialized yet */
6332 return MIN_LINES;
6333
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006335 total = 0;
6336 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6337 {
6338 n = frame_minheight(tp->tp_topframe, NULL);
6339 if (total < n)
6340 total = n;
6341 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006342 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006344 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006346 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 return total;
6348}
6349
6350/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006351 * Return TRUE if there is only one window (in the current tab page), not
6352 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006353 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 */
6355 int
6356only_one_window()
6357{
6358#ifdef FEAT_WINDOWS
6359 int count = 0;
6360 win_T *wp;
6361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006362 /* If there is another tab page there always is another window. */
6363 if (first_tabpage->tp_next != NULL)
6364 return FALSE;
6365
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006367 if (wp->w_buffer != NULL
6368 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369# ifdef FEAT_QUICKFIX
6370 || wp->w_p_pvw
6371# endif
6372 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006373# ifdef FEAT_AUTOCMD
6374 && wp != aucmd_win
6375# endif
6376 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 ++count;
6378 return (count <= 1);
6379#else
6380 return TRUE;
6381#endif
6382}
6383
6384#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6385/*
6386 * Correct the cursor line number in other windows. Used after changing the
6387 * current buffer, and before applying autocommands.
6388 * When "do_curwin" is TRUE, also check current window.
6389 */
6390 void
6391check_lnums(do_curwin)
6392 int do_curwin;
6393{
6394 win_T *wp;
6395
6396#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006397 tabpage_T *tp;
6398
6399 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6401#else
6402 wp = curwin;
6403 if (do_curwin)
6404#endif
6405 {
6406 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6407 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6408 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6409 wp->w_topline = curbuf->b_ml.ml_line_count;
6410 }
6411}
6412#endif
6413
6414#if defined(FEAT_WINDOWS) || defined(PROTO)
6415
6416/*
6417 * A snapshot of the window sizes, to restore them after closing the help
6418 * window.
6419 * Only these fields are used:
6420 * fr_layout
6421 * fr_width
6422 * fr_height
6423 * fr_next
6424 * fr_child
6425 * fr_win (only valid for the old curwin, NULL otherwise)
6426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427
6428/*
6429 * Create a snapshot of the current frame sizes.
6430 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006431 void
6432make_snapshot(idx)
6433 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006435 clear_snapshot(curtab, idx);
6436 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437}
6438
6439 static void
6440make_snapshot_rec(fr, frp)
6441 frame_T *fr;
6442 frame_T **frp;
6443{
6444 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6445 if (*frp == NULL)
6446 return;
6447 (*frp)->fr_layout = fr->fr_layout;
6448# ifdef FEAT_VERTSPLIT
6449 (*frp)->fr_width = fr->fr_width;
6450# endif
6451 (*frp)->fr_height = fr->fr_height;
6452 if (fr->fr_next != NULL)
6453 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6454 if (fr->fr_child != NULL)
6455 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6456 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6457 (*frp)->fr_win = curwin;
6458}
6459
6460/*
6461 * Remove any existing snapshot.
6462 */
6463 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006464clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006465 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006466 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006468 clear_snapshot_rec(tp->tp_snapshot[idx]);
6469 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470}
6471
6472 static void
6473clear_snapshot_rec(fr)
6474 frame_T *fr;
6475{
6476 if (fr != NULL)
6477 {
6478 clear_snapshot_rec(fr->fr_next);
6479 clear_snapshot_rec(fr->fr_child);
6480 vim_free(fr);
6481 }
6482}
6483
6484/*
6485 * Restore a previously created snapshot, if there is any.
6486 * This is only done if the screen size didn't change and the window layout is
6487 * still the same.
6488 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006489 void
6490restore_snapshot(idx, close_curwin)
6491 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 int close_curwin; /* closing current window */
6493{
6494 win_T *wp;
6495
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006496 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006498 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006500 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6501 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006503 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 win_comp_pos();
6505 if (wp != NULL && close_curwin)
6506 win_goto(wp);
6507 redraw_all_later(CLEAR);
6508 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006509 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510}
6511
6512/*
6513 * Check if frames "sn" and "fr" have the same layout, same following frames
6514 * and same children.
6515 */
6516 static int
6517check_snapshot_rec(sn, fr)
6518 frame_T *sn;
6519 frame_T *fr;
6520{
6521 if (sn->fr_layout != fr->fr_layout
6522 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6523 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6524 || (sn->fr_next != NULL
6525 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6526 || (sn->fr_child != NULL
6527 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6528 return FAIL;
6529 return OK;
6530}
6531
6532/*
6533 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6534 * following frames and children.
6535 * Returns a pointer to the old current window, or NULL.
6536 */
6537 static win_T *
6538restore_snapshot_rec(sn, fr)
6539 frame_T *sn;
6540 frame_T *fr;
6541{
6542 win_T *wp = NULL;
6543 win_T *wp2;
6544
6545 fr->fr_height = sn->fr_height;
6546# ifdef FEAT_VERTSPLIT
6547 fr->fr_width = sn->fr_width;
6548# endif
6549 if (fr->fr_layout == FR_LEAF)
6550 {
6551 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6552# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006553 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554# endif
6555 wp = sn->fr_win;
6556 }
6557 if (sn->fr_next != NULL)
6558 {
6559 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6560 if (wp2 != NULL)
6561 wp = wp2;
6562 }
6563 if (sn->fr_child != NULL)
6564 {
6565 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6566 if (wp2 != NULL)
6567 wp = wp2;
6568 }
6569 return wp;
6570}
6571
6572#endif
6573
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006574#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6575 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006576/*
6577 * Set "win" to be the curwin and "tp" to be the current tab page.
6578 * restore_win() MUST be called to undo.
6579 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006580 * When "no_display" is TRUE the display won't be affected, no redraw is
6581 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006582 * Returns FAIL if switching to "win" failed.
6583 */
6584 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006585switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006586 win_T **save_curwin UNUSED;
6587 tabpage_T **save_curtab UNUSED;
6588 win_T *win UNUSED;
6589 tabpage_T *tp UNUSED;
6590 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006591{
6592# ifdef FEAT_AUTOCMD
6593 block_autocmds();
6594# endif
6595# ifdef FEAT_WINDOWS
6596 *save_curwin = curwin;
6597 if (tp != NULL)
6598 {
6599 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006600 if (no_display)
6601 {
6602 curtab->tp_firstwin = firstwin;
6603 curtab->tp_lastwin = lastwin;
6604 curtab = tp;
6605 firstwin = curtab->tp_firstwin;
6606 lastwin = curtab->tp_lastwin;
6607 }
6608 else
6609 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006610 }
6611 if (!win_valid(win))
6612 {
6613# ifdef FEAT_AUTOCMD
6614 unblock_autocmds();
6615# endif
6616 return FAIL;
6617 }
6618 curwin = win;
6619 curbuf = curwin->w_buffer;
6620# endif
6621 return OK;
6622}
6623
6624/*
6625 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006626 * When "no_display" is TRUE the display won't be affected, no redraw is
6627 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006628 */
6629 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006630restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006631 win_T *save_curwin UNUSED;
6632 tabpage_T *save_curtab UNUSED;
6633 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006634{
6635# ifdef FEAT_WINDOWS
6636 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006637 {
6638 if (no_display)
6639 {
6640 curtab->tp_firstwin = firstwin;
6641 curtab->tp_lastwin = lastwin;
6642 curtab = save_curtab;
6643 firstwin = curtab->tp_firstwin;
6644 lastwin = curtab->tp_lastwin;
6645 }
6646 else
6647 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6648 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006649 if (win_valid(save_curwin))
6650 {
6651 curwin = save_curwin;
6652 curbuf = curwin->w_buffer;
6653 }
6654# endif
6655# ifdef FEAT_AUTOCMD
6656 unblock_autocmds();
6657# endif
6658}
6659
6660/*
6661 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6662 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6663 */
6664 void
6665switch_buffer(save_curbuf, buf)
6666 buf_T *buf;
6667 buf_T **save_curbuf;
6668{
6669# ifdef FEAT_AUTOCMD
6670 block_autocmds();
6671# endif
6672 *save_curbuf = curbuf;
6673 --curbuf->b_nwindows;
6674 curbuf = buf;
6675 curwin->w_buffer = buf;
6676 ++curbuf->b_nwindows;
6677}
6678
6679/*
6680 * Restore the current buffer after using switch_buffer().
6681 */
6682 void
6683restore_buffer(save_curbuf)
6684 buf_T *save_curbuf;
6685{
6686# ifdef FEAT_AUTOCMD
6687 unblock_autocmds();
6688# endif
6689 /* Check for valid buffer, just in case. */
6690 if (buf_valid(save_curbuf))
6691 {
6692 --curbuf->b_nwindows;
6693 curwin->w_buffer = save_curbuf;
6694 curbuf = save_curbuf;
6695 ++curbuf->b_nwindows;
6696 }
6697}
6698#endif
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6701/*
6702 * Return TRUE if there is any vertically split window.
6703 */
6704 int
6705win_hasvertsplit()
6706{
6707 frame_T *fr;
6708
6709 if (topframe->fr_layout == FR_ROW)
6710 return TRUE;
6711
6712 if (topframe->fr_layout == FR_COL)
6713 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6714 if (fr->fr_layout == FR_ROW)
6715 return TRUE;
6716
6717 return FALSE;
6718}
6719#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006720
6721#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6722/*
6723 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006724 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006725 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6726 * If no particular ID is desired, -1 must be specified for 'id'.
6727 * Return ID of added match, -1 on failure.
6728 */
6729 int
6730match_add(wp, grp, pat, prio, id)
6731 win_T *wp;
6732 char_u *grp;
6733 char_u *pat;
6734 int prio;
6735 int id;
6736{
6737 matchitem_T *cur;
6738 matchitem_T *prev;
6739 matchitem_T *m;
6740 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006741 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006742
6743 if (*grp == NUL || *pat == NUL)
6744 return -1;
6745 if (id < -1 || id == 0)
6746 {
6747 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6748 return -1;
6749 }
6750 if (id != -1)
6751 {
6752 cur = wp->w_match_head;
6753 while (cur != NULL)
6754 {
6755 if (cur->id == id)
6756 {
6757 EMSGN("E801: ID already taken: %ld", id);
6758 return -1;
6759 }
6760 cur = cur->next;
6761 }
6762 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006763 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006764 {
6765 EMSG2(_(e_nogroup), grp);
6766 return -1;
6767 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006768 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006769 {
6770 EMSG2(_(e_invarg2), pat);
6771 return -1;
6772 }
6773
6774 /* Find available match ID. */
6775 while (id == -1)
6776 {
6777 cur = wp->w_match_head;
6778 while (cur != NULL && cur->id != wp->w_next_match_id)
6779 cur = cur->next;
6780 if (cur == NULL)
6781 id = wp->w_next_match_id;
6782 wp->w_next_match_id++;
6783 }
6784
6785 /* Build new match. */
6786 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6787 m->id = id;
6788 m->priority = prio;
6789 m->pattern = vim_strsave(pat);
6790 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006791 m->match.regprog = regprog;
6792 m->match.rmm_ic = FALSE;
6793 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006794
6795 /* Insert new match. The match list is in ascending order with regard to
6796 * the match priorities. */
6797 cur = wp->w_match_head;
6798 prev = cur;
6799 while (cur != NULL && prio >= cur->priority)
6800 {
6801 prev = cur;
6802 cur = cur->next;
6803 }
6804 if (cur == prev)
6805 wp->w_match_head = m;
6806 else
6807 prev->next = m;
6808 m->next = cur;
6809
6810 redraw_later(SOME_VALID);
6811 return id;
6812}
6813
6814/*
6815 * Delete match with ID 'id' in the match list of window 'wp'.
6816 * Print error messages if 'perr' is TRUE.
6817 */
6818 int
6819match_delete(wp, id, perr)
6820 win_T *wp;
6821 int id;
6822 int perr;
6823{
6824 matchitem_T *cur = wp->w_match_head;
6825 matchitem_T *prev = cur;
6826
6827 if (id < 1)
6828 {
6829 if (perr == TRUE)
6830 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6831 id);
6832 return -1;
6833 }
6834 while (cur != NULL && cur->id != id)
6835 {
6836 prev = cur;
6837 cur = cur->next;
6838 }
6839 if (cur == NULL)
6840 {
6841 if (perr == TRUE)
6842 EMSGN("E803: ID not found: %ld", id);
6843 return -1;
6844 }
6845 if (cur == prev)
6846 wp->w_match_head = cur->next;
6847 else
6848 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006849 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006850 vim_free(cur->pattern);
6851 vim_free(cur);
6852 redraw_later(SOME_VALID);
6853 return 0;
6854}
6855
6856/*
6857 * Delete all matches in the match list of window 'wp'.
6858 */
6859 void
6860clear_matches(wp)
6861 win_T *wp;
6862{
6863 matchitem_T *m;
6864
6865 while (wp->w_match_head != NULL)
6866 {
6867 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006868 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006869 vim_free(wp->w_match_head->pattern);
6870 vim_free(wp->w_match_head);
6871 wp->w_match_head = m;
6872 }
6873 redraw_later(SOME_VALID);
6874}
6875
6876/*
6877 * Get match from ID 'id' in window 'wp'.
6878 * Return NULL if match not found.
6879 */
6880 matchitem_T *
6881get_match(wp, id)
6882 win_T *wp;
6883 int id;
6884{
6885 matchitem_T *cur = wp->w_match_head;
6886
6887 while (cur != NULL && cur->id != id)
6888 cur = cur->next;
6889 return cur;
6890}
6891#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006892
6893#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6894 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006895get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006896{
6897 int i = 1;
6898 win_T *w;
6899
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006900 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006901 ++i;
6902
6903 if (w == NULL)
6904 return 0;
6905 else
6906 return i;
6907}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006908
6909 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006910get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006911{
6912 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006913# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006914 tabpage_T *t;
6915
6916 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6917 ++i;
6918
6919 if (t == NULL)
6920 return 0;
6921 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006922# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006923 return i;
6924}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006925#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006926
Bram Moolenaarf0327f62013-06-28 20:16:55 +02006927#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006928/*
6929 * Return TRUE if "topfrp" and its children are at the right height.
6930 */
6931 static int
6932frame_check_height(topfrp, height)
6933 frame_T *topfrp;
6934 int height;
6935{
6936 frame_T *frp;
6937
6938 if (topfrp->fr_height != height)
6939 return FALSE;
6940
6941 if (topfrp->fr_layout == FR_ROW)
6942 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6943 if (frp->fr_height != height)
6944 return FALSE;
6945
6946 return TRUE;
6947}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02006948#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006949
6950#ifdef FEAT_VERTSPLIT
6951/*
6952 * Return TRUE if "topfrp" and its children are at the right width.
6953 */
6954 static int
6955frame_check_width(topfrp, width)
6956 frame_T *topfrp;
6957 int width;
6958{
6959 frame_T *frp;
6960
6961 if (topfrp->fr_width != width)
6962 return FALSE;
6963
6964 if (topfrp->fr_layout == FR_COL)
6965 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6966 if (frp->fr_width != width)
6967 return FALSE;
6968
6969 return TRUE;
6970}
6971#endif
6972