blob: fe6f335cca0e1baa9bea041ab2600ea7164cbd98 [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{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004724 /* TODO: why would wp be NULL here? */
4725 if (wp != NULL)
4726 {
4727 vim_free(wp->w_lines);
4728 wp->w_lines = NULL;
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730}
4731
4732/*
4733 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004734 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 */
4736 void
4737shell_new_rows()
4738{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004739 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741 if (firstwin == NULL) /* not initialized yet */
4742 return;
4743#ifdef FEAT_WINDOWS
4744 if (h < frame_minheight(topframe, NULL))
4745 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004746
4747 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 * that doesn't result in the right height, forget about that option. */
4749 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004750 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 frame_new_height(topframe, h, FALSE, FALSE);
4752
4753 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4754#else
4755 if (h < 1)
4756 h = 1;
4757 win_new_height(firstwin, h);
4758#endif
4759 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004760#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004761 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004762#endif
4763
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764#if 0
4765 /* Disabled: don't want making the screen smaller make a window larger. */
4766 if (p_ea)
4767 win_equal(curwin, FALSE, 'v');
4768#endif
4769}
4770
4771#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4772/*
4773 * Called from win_new_shellsize() after Columns changed.
4774 */
4775 void
4776shell_new_columns()
4777{
4778 if (firstwin == NULL) /* not initialized yet */
4779 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004780
4781 /* First try setting the widths of windows with 'winfixwidth'. If that
4782 * doesn't result in the right width, forget about that option. */
4783 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004784 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004785 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4788#if 0
4789 /* Disabled: don't want making the screen smaller make a window larger. */
4790 if (p_ea)
4791 win_equal(curwin, FALSE, 'h');
4792#endif
4793}
4794#endif
4795
4796#if defined(FEAT_CMDWIN) || defined(PROTO)
4797/*
4798 * Save the size of all windows in "gap".
4799 */
4800 void
4801win_size_save(gap)
4802 garray_T *gap;
4803
4804{
4805 win_T *wp;
4806
4807 ga_init2(gap, (int)sizeof(int), 1);
4808 if (ga_grow(gap, win_count() * 2) == OK)
4809 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4810 {
4811 ((int *)gap->ga_data)[gap->ga_len++] =
4812 wp->w_width + wp->w_vsep_width;
4813 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4814 }
4815}
4816
4817/*
4818 * Restore window sizes, but only if the number of windows is still the same.
4819 * Does not free the growarray.
4820 */
4821 void
4822win_size_restore(gap)
4823 garray_T *gap;
4824{
4825 win_T *wp;
4826 int i;
4827
4828 if (win_count() * 2 == gap->ga_len)
4829 {
4830 i = 0;
4831 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4832 {
4833 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4834 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4835 }
4836 /* recompute the window positions */
4837 (void)win_comp_pos();
4838 }
4839}
4840#endif /* FEAT_CMDWIN */
4841
4842#if defined(FEAT_WINDOWS) || defined(PROTO)
4843/*
4844 * Update the position for all windows, using the width and height of the
4845 * frames.
4846 * Returns the row just after the last window.
4847 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004848 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849win_comp_pos()
4850{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004851 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 int col = 0;
4853
4854 frame_comp_pos(topframe, &row, &col);
4855 return row;
4856}
4857
4858/*
4859 * Update the position of the windows in frame "topfrp", using the width and
4860 * height of the frames.
4861 * "*row" and "*col" are the top-left position of the frame. They are updated
4862 * to the bottom-right position plus one.
4863 */
4864 static void
4865frame_comp_pos(topfrp, row, col)
4866 frame_T *topfrp;
4867 int *row;
4868 int *col;
4869{
4870 win_T *wp;
4871 frame_T *frp;
4872#ifdef FEAT_VERTSPLIT
4873 int startcol;
4874 int startrow;
4875#endif
4876
4877 wp = topfrp->fr_win;
4878 if (wp != NULL)
4879 {
4880 if (wp->w_winrow != *row
4881#ifdef FEAT_VERTSPLIT
4882 || wp->w_wincol != *col
4883#endif
4884 )
4885 {
4886 /* position changed, redraw */
4887 wp->w_winrow = *row;
4888#ifdef FEAT_VERTSPLIT
4889 wp->w_wincol = *col;
4890#endif
4891 redraw_win_later(wp, NOT_VALID);
4892 wp->w_redr_status = TRUE;
4893 }
4894 *row += wp->w_height + wp->w_status_height;
4895#ifdef FEAT_VERTSPLIT
4896 *col += wp->w_width + wp->w_vsep_width;
4897#endif
4898 }
4899 else
4900 {
4901#ifdef FEAT_VERTSPLIT
4902 startrow = *row;
4903 startcol = *col;
4904#endif
4905 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4906 {
4907#ifdef FEAT_VERTSPLIT
4908 if (topfrp->fr_layout == FR_ROW)
4909 *row = startrow; /* all frames are at the same row */
4910 else
4911 *col = startcol; /* all frames are at the same col */
4912#endif
4913 frame_comp_pos(frp, row, col);
4914 }
4915 }
4916}
4917
4918#endif /* FEAT_WINDOWS */
4919
4920/*
4921 * Set current window height and take care of repositioning other windows to
4922 * fit around it.
4923 */
4924 void
4925win_setheight(height)
4926 int height;
4927{
4928 win_setheight_win(height, curwin);
4929}
4930
4931/*
4932 * Set the window height of window "win" and take care of repositioning other
4933 * windows to fit around it.
4934 */
4935 void
4936win_setheight_win(height, win)
4937 int height;
4938 win_T *win;
4939{
4940 int row;
4941
4942 if (win == curwin)
4943 {
4944 /* Always keep current window at least one line high, even when
4945 * 'winminheight' is zero. */
4946#ifdef FEAT_WINDOWS
4947 if (height < p_wmh)
4948 height = p_wmh;
4949#endif
4950 if (height == 0)
4951 height = 1;
4952 }
4953
4954#ifdef FEAT_WINDOWS
4955 frame_setheight(win->w_frame, height + win->w_status_height);
4956
4957 /* recompute the window positions */
4958 row = win_comp_pos();
4959#else
4960 if (height > topframe->fr_height)
4961 height = topframe->fr_height;
4962 win->w_height = height;
4963 row = height;
4964#endif
4965
4966 /*
4967 * If there is extra space created between the last window and the command
4968 * line, clear it.
4969 */
4970 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4971 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4972 cmdline_row = row;
4973 msg_row = row;
4974 msg_col = 0;
4975
4976 redraw_all_later(NOT_VALID);
4977}
4978
4979#if defined(FEAT_WINDOWS) || defined(PROTO)
4980
4981/*
4982 * Set the height of a frame to "height" and take care that all frames and
4983 * windows inside it are resized. Also resize frames on the left and right if
4984 * the are in the same FR_ROW frame.
4985 *
4986 * Strategy:
4987 * If the frame is part of a FR_COL frame, try fitting the frame in that
4988 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4989 * go to containing frames to resize them and make room.
4990 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4991 * Check for the minimal height of the FR_ROW frame.
4992 * At the top level we can also use change the command line height.
4993 */
4994 static void
4995frame_setheight(curfrp, height)
4996 frame_T *curfrp;
4997 int height;
4998{
4999 int room; /* total number of lines available */
5000 int take; /* number of lines taken from other windows */
5001 int room_cmdline; /* lines available from cmdline */
5002 int run;
5003 frame_T *frp;
5004 int h;
5005 int room_reserved;
5006
5007 /* If the height already is the desired value, nothing to do. */
5008 if (curfrp->fr_height == height)
5009 return;
5010
5011 if (curfrp->fr_parent == NULL)
5012 {
5013 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005014 if (height > ROWS_AVAIL)
5015 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (height > 0)
5017 frame_new_height(curfrp, height, FALSE, FALSE);
5018 }
5019 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5020 {
5021 /* Row of frames: Also need to resize frames left and right of this
5022 * one. First check for the minimal height of these. */
5023 h = frame_minheight(curfrp->fr_parent, NULL);
5024 if (height < h)
5025 height = h;
5026 frame_setheight(curfrp->fr_parent, height);
5027 }
5028 else
5029 {
5030 /*
5031 * Column of frames: try to change only frames in this column.
5032 */
5033#ifdef FEAT_VERTSPLIT
5034 /*
5035 * Do this twice:
5036 * 1: compute room available, if it's not enough try resizing the
5037 * containing frame.
5038 * 2: compute the room available and adjust the height to it.
5039 * Try not to reduce the height of a window with 'winfixheight' set.
5040 */
5041 for (run = 1; run <= 2; ++run)
5042#else
5043 for (;;)
5044#endif
5045 {
5046 room = 0;
5047 room_reserved = 0;
5048 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5049 frp = frp->fr_next)
5050 {
5051 if (frp != curfrp
5052 && frp->fr_win != NULL
5053 && frp->fr_win->w_p_wfh)
5054 room_reserved += frp->fr_height;
5055 room += frp->fr_height;
5056 if (frp != curfrp)
5057 room -= frame_minheight(frp, NULL);
5058 }
5059#ifdef FEAT_VERTSPLIT
5060 if (curfrp->fr_width != Columns)
5061 room_cmdline = 0;
5062 else
5063#endif
5064 {
5065 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5066 + lastwin->w_height + lastwin->w_status_height);
5067 if (room_cmdline < 0)
5068 room_cmdline = 0;
5069 }
5070
5071 if (height <= room + room_cmdline)
5072 break;
5073#ifdef FEAT_VERTSPLIT
5074 if (run == 2 || curfrp->fr_width == Columns)
5075#endif
5076 {
5077 if (height > room + room_cmdline)
5078 height = room + room_cmdline;
5079 break;
5080 }
5081#ifdef FEAT_VERTSPLIT
5082 frame_setheight(curfrp->fr_parent, height
5083 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5084#endif
5085 /*NOTREACHED*/
5086 }
5087
5088 /*
5089 * Compute the number of lines we will take from others frames (can be
5090 * negative!).
5091 */
5092 take = height - curfrp->fr_height;
5093
5094 /* If there is not enough room, also reduce the height of a window
5095 * with 'winfixheight' set. */
5096 if (height > room + room_cmdline - room_reserved)
5097 room_reserved = room + room_cmdline - height;
5098 /* If there is only a 'winfixheight' window and making the
5099 * window smaller, need to make the other window taller. */
5100 if (take < 0 && room - curfrp->fr_height < room_reserved)
5101 room_reserved = 0;
5102
5103 if (take > 0 && room_cmdline > 0)
5104 {
5105 /* use lines from cmdline first */
5106 if (take < room_cmdline)
5107 room_cmdline = take;
5108 take -= room_cmdline;
5109 topframe->fr_height += room_cmdline;
5110 }
5111
5112 /*
5113 * set the current frame to the new height
5114 */
5115 frame_new_height(curfrp, height, FALSE, FALSE);
5116
5117 /*
5118 * First take lines from the frames after the current frame. If
5119 * that is not enough, takes lines from frames above the current
5120 * frame.
5121 */
5122 for (run = 0; run < 2; ++run)
5123 {
5124 if (run == 0)
5125 frp = curfrp->fr_next; /* 1st run: start with next window */
5126 else
5127 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5128 while (frp != NULL && take != 0)
5129 {
5130 h = frame_minheight(frp, NULL);
5131 if (room_reserved > 0
5132 && frp->fr_win != NULL
5133 && frp->fr_win->w_p_wfh)
5134 {
5135 if (room_reserved >= frp->fr_height)
5136 room_reserved -= frp->fr_height;
5137 else
5138 {
5139 if (frp->fr_height - room_reserved > take)
5140 room_reserved = frp->fr_height - take;
5141 take -= frp->fr_height - room_reserved;
5142 frame_new_height(frp, room_reserved, FALSE, FALSE);
5143 room_reserved = 0;
5144 }
5145 }
5146 else
5147 {
5148 if (frp->fr_height - take < h)
5149 {
5150 take -= frp->fr_height - h;
5151 frame_new_height(frp, h, FALSE, FALSE);
5152 }
5153 else
5154 {
5155 frame_new_height(frp, frp->fr_height - take,
5156 FALSE, FALSE);
5157 take = 0;
5158 }
5159 }
5160 if (run == 0)
5161 frp = frp->fr_next;
5162 else
5163 frp = frp->fr_prev;
5164 }
5165 }
5166 }
5167}
5168
5169#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5170/*
5171 * Set current window width and take care of repositioning other windows to
5172 * fit around it.
5173 */
5174 void
5175win_setwidth(width)
5176 int width;
5177{
5178 win_setwidth_win(width, curwin);
5179}
5180
5181 void
5182win_setwidth_win(width, wp)
5183 int width;
5184 win_T *wp;
5185{
5186 /* Always keep current window at least one column wide, even when
5187 * 'winminwidth' is zero. */
5188 if (wp == curwin)
5189 {
5190 if (width < p_wmw)
5191 width = p_wmw;
5192 if (width == 0)
5193 width = 1;
5194 }
5195
5196 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5197
5198 /* recompute the window positions */
5199 (void)win_comp_pos();
5200
5201 redraw_all_later(NOT_VALID);
5202}
5203
5204/*
5205 * Set the width of a frame to "width" and take care that all frames and
5206 * windows inside it are resized. Also resize frames above and below if the
5207 * are in the same FR_ROW frame.
5208 *
5209 * Strategy is similar to frame_setheight().
5210 */
5211 static void
5212frame_setwidth(curfrp, width)
5213 frame_T *curfrp;
5214 int width;
5215{
5216 int room; /* total number of lines available */
5217 int take; /* number of lines taken from other windows */
5218 int run;
5219 frame_T *frp;
5220 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005221 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
5223 /* If the width already is the desired value, nothing to do. */
5224 if (curfrp->fr_width == width)
5225 return;
5226
5227 if (curfrp->fr_parent == NULL)
5228 /* topframe: can't change width */
5229 return;
5230
5231 if (curfrp->fr_parent->fr_layout == FR_COL)
5232 {
5233 /* Column of frames: Also need to resize frames above and below of
5234 * this one. First check for the minimal width of these. */
5235 w = frame_minwidth(curfrp->fr_parent, NULL);
5236 if (width < w)
5237 width = w;
5238 frame_setwidth(curfrp->fr_parent, width);
5239 }
5240 else
5241 {
5242 /*
5243 * Row of frames: try to change only frames in this row.
5244 *
5245 * Do this twice:
5246 * 1: compute room available, if it's not enough try resizing the
5247 * containing frame.
5248 * 2: compute the room available and adjust the width to it.
5249 */
5250 for (run = 1; run <= 2; ++run)
5251 {
5252 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005253 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5255 frp = frp->fr_next)
5256 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005257 if (frp != curfrp
5258 && frp->fr_win != NULL
5259 && frp->fr_win->w_p_wfw)
5260 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 room += frp->fr_width;
5262 if (frp != curfrp)
5263 room -= frame_minwidth(frp, NULL);
5264 }
5265
5266 if (width <= room)
5267 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005268 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 if (width > room)
5271 width = room;
5272 break;
5273 }
5274 frame_setwidth(curfrp->fr_parent, width
5275 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5276 }
5277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 /*
5279 * Compute the number of lines we will take from others frames (can be
5280 * negative!).
5281 */
5282 take = width - curfrp->fr_width;
5283
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005284 /* If there is not enough room, also reduce the width of a window
5285 * with 'winfixwidth' set. */
5286 if (width > room - room_reserved)
5287 room_reserved = room - width;
5288 /* If there is only a 'winfixwidth' window and making the
5289 * window smaller, need to make the other window narrower. */
5290 if (take < 0 && room - curfrp->fr_width < room_reserved)
5291 room_reserved = 0;
5292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 /*
5294 * set the current frame to the new width
5295 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005296 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
5298 /*
5299 * First take lines from the frames right of the current frame. If
5300 * that is not enough, takes lines from frames left of the current
5301 * frame.
5302 */
5303 for (run = 0; run < 2; ++run)
5304 {
5305 if (run == 0)
5306 frp = curfrp->fr_next; /* 1st run: start with next window */
5307 else
5308 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5309 while (frp != NULL && take != 0)
5310 {
5311 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005312 if (room_reserved > 0
5313 && frp->fr_win != NULL
5314 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005316 if (room_reserved >= frp->fr_width)
5317 room_reserved -= frp->fr_width;
5318 else
5319 {
5320 if (frp->fr_width - room_reserved > take)
5321 room_reserved = frp->fr_width - take;
5322 take -= frp->fr_width - room_reserved;
5323 frame_new_width(frp, room_reserved, FALSE, FALSE);
5324 room_reserved = 0;
5325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327 else
5328 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005329 if (frp->fr_width - take < w)
5330 {
5331 take -= frp->fr_width - w;
5332 frame_new_width(frp, w, FALSE, FALSE);
5333 }
5334 else
5335 {
5336 frame_new_width(frp, frp->fr_width - take,
5337 FALSE, FALSE);
5338 take = 0;
5339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
5341 if (run == 0)
5342 frp = frp->fr_next;
5343 else
5344 frp = frp->fr_prev;
5345 }
5346 }
5347 }
5348}
5349#endif /* FEAT_VERTSPLIT */
5350
5351/*
5352 * Check 'winminheight' for a valid value.
5353 */
5354 void
5355win_setminheight()
5356{
5357 int room;
5358 int first = TRUE;
5359 win_T *wp;
5360
5361 /* loop until there is a 'winminheight' that is possible */
5362 while (p_wmh > 0)
5363 {
5364 /* TODO: handle vertical splits */
5365 room = -p_wh;
5366 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5367 room += wp->w_height - p_wmh;
5368 if (room >= 0)
5369 break;
5370 --p_wmh;
5371 if (first)
5372 {
5373 EMSG(_(e_noroom));
5374 first = FALSE;
5375 }
5376 }
5377}
5378
5379#ifdef FEAT_MOUSE
5380
5381/*
5382 * Status line of dragwin is dragged "offset" lines down (negative is up).
5383 */
5384 void
5385win_drag_status_line(dragwin, offset)
5386 win_T *dragwin;
5387 int offset;
5388{
5389 frame_T *curfr;
5390 frame_T *fr;
5391 int room;
5392 int row;
5393 int up; /* if TRUE, drag status line up, otherwise down */
5394 int n;
5395
5396 fr = dragwin->w_frame;
5397 curfr = fr;
5398 if (fr != topframe) /* more than one window */
5399 {
5400 fr = fr->fr_parent;
5401 /* When the parent frame is not a column of frames, its parent should
5402 * be. */
5403 if (fr->fr_layout != FR_COL)
5404 {
5405 curfr = fr;
5406 if (fr != topframe) /* only a row of windows, may drag statusline */
5407 fr = fr->fr_parent;
5408 }
5409 }
5410
5411 /* If this is the last frame in a column, may want to resize the parent
5412 * frame instead (go two up to skip a row of frames). */
5413 while (curfr != topframe && curfr->fr_next == NULL)
5414 {
5415 if (fr != topframe)
5416 fr = fr->fr_parent;
5417 curfr = fr;
5418 if (fr != topframe)
5419 fr = fr->fr_parent;
5420 }
5421
5422 if (offset < 0) /* drag up */
5423 {
5424 up = TRUE;
5425 offset = -offset;
5426 /* sum up the room of the current frame and above it */
5427 if (fr == curfr)
5428 {
5429 /* only one window */
5430 room = fr->fr_height - frame_minheight(fr, NULL);
5431 }
5432 else
5433 {
5434 room = 0;
5435 for (fr = fr->fr_child; ; fr = fr->fr_next)
5436 {
5437 room += fr->fr_height - frame_minheight(fr, NULL);
5438 if (fr == curfr)
5439 break;
5440 }
5441 }
5442 fr = curfr->fr_next; /* put fr at frame that grows */
5443 }
5444 else /* drag down */
5445 {
5446 up = FALSE;
5447 /*
5448 * Only dragging the last status line can reduce p_ch.
5449 */
5450 room = Rows - cmdline_row;
5451 if (curfr->fr_next == NULL)
5452 room -= 1;
5453 else
5454 room -= p_ch;
5455 if (room < 0)
5456 room = 0;
5457 /* sum up the room of frames below of the current one */
5458 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5459 room += fr->fr_height - frame_minheight(fr, NULL);
5460 fr = curfr; /* put fr at window that grows */
5461 }
5462
5463 if (room < offset) /* Not enough room */
5464 offset = room; /* Move as far as we can */
5465 if (offset <= 0)
5466 return;
5467
5468 /*
5469 * Grow frame fr by "offset" lines.
5470 * Doesn't happen when dragging the last status line up.
5471 */
5472 if (fr != NULL)
5473 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5474
5475 if (up)
5476 fr = curfr; /* current frame gets smaller */
5477 else
5478 fr = curfr->fr_next; /* next frame gets smaller */
5479
5480 /*
5481 * Now make the other frames smaller.
5482 */
5483 while (fr != NULL && offset > 0)
5484 {
5485 n = frame_minheight(fr, NULL);
5486 if (fr->fr_height - offset <= n)
5487 {
5488 offset -= fr->fr_height - n;
5489 frame_new_height(fr, n, !up, FALSE);
5490 }
5491 else
5492 {
5493 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5494 break;
5495 }
5496 if (up)
5497 fr = fr->fr_prev;
5498 else
5499 fr = fr->fr_next;
5500 }
5501 row = win_comp_pos();
5502 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5503 cmdline_row = row;
5504 p_ch = Rows - cmdline_row;
5505 if (p_ch < 1)
5506 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005507 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005508 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 showmode();
5510}
5511
5512#ifdef FEAT_VERTSPLIT
5513/*
5514 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5515 */
5516 void
5517win_drag_vsep_line(dragwin, offset)
5518 win_T *dragwin;
5519 int offset;
5520{
5521 frame_T *curfr;
5522 frame_T *fr;
5523 int room;
5524 int left; /* if TRUE, drag separator line left, otherwise right */
5525 int n;
5526
5527 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005528 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 return;
5530 curfr = fr;
5531 fr = fr->fr_parent;
5532 /* When the parent frame is not a row of frames, its parent should be. */
5533 if (fr->fr_layout != FR_ROW)
5534 {
5535 if (fr == topframe) /* only a column of windows (cannot happen?) */
5536 return;
5537 curfr = fr;
5538 fr = fr->fr_parent;
5539 }
5540
5541 /* If this is the last frame in a row, may want to resize a parent
5542 * frame instead. */
5543 while (curfr->fr_next == NULL)
5544 {
5545 if (fr == topframe)
5546 break;
5547 curfr = fr;
5548 fr = fr->fr_parent;
5549 if (fr != topframe)
5550 {
5551 curfr = fr;
5552 fr = fr->fr_parent;
5553 }
5554 }
5555
5556 if (offset < 0) /* drag left */
5557 {
5558 left = TRUE;
5559 offset = -offset;
5560 /* sum up the room of the current frame and left of it */
5561 room = 0;
5562 for (fr = fr->fr_child; ; fr = fr->fr_next)
5563 {
5564 room += fr->fr_width - frame_minwidth(fr, NULL);
5565 if (fr == curfr)
5566 break;
5567 }
5568 fr = curfr->fr_next; /* put fr at frame that grows */
5569 }
5570 else /* drag right */
5571 {
5572 left = FALSE;
5573 /* sum up the room of frames right of the current one */
5574 room = 0;
5575 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5576 room += fr->fr_width - frame_minwidth(fr, NULL);
5577 fr = curfr; /* put fr at window that grows */
5578 }
5579
5580 if (room < offset) /* Not enough room */
5581 offset = room; /* Move as far as we can */
5582 if (offset <= 0) /* No room at all, quit. */
5583 return;
5584
5585 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005586 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588 /* shrink other frames: current and at the left or at the right */
5589 if (left)
5590 fr = curfr; /* current frame gets smaller */
5591 else
5592 fr = curfr->fr_next; /* next frame gets smaller */
5593
5594 while (fr != NULL && offset > 0)
5595 {
5596 n = frame_minwidth(fr, NULL);
5597 if (fr->fr_width - offset <= n)
5598 {
5599 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005600 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 else
5603 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005604 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 break;
5606 }
5607 if (left)
5608 fr = fr->fr_prev;
5609 else
5610 fr = fr->fr_next;
5611 }
5612 (void)win_comp_pos();
5613 redraw_all_later(NOT_VALID);
5614}
5615#endif /* FEAT_VERTSPLIT */
5616#endif /* FEAT_MOUSE */
5617
5618#endif /* FEAT_WINDOWS */
5619
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005620#define FRACTION_MULT 16384L
5621
5622/*
5623 * Set wp->w_fraction for the current w_wrow and w_height.
5624 */
5625 static void
5626set_fraction(wp)
5627 win_T *wp;
5628{
5629 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005630 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005631}
5632
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633/*
5634 * Set the height of a window.
5635 * This takes care of the things inside the window, not what happens to the
5636 * window position, the frame or to other windows.
5637 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005638 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639win_new_height(wp, height)
5640 win_T *wp;
5641 int height;
5642{
5643 linenr_T lnum;
5644 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005645 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
5647 /* Don't want a negative height. Happens when splitting a tiny window.
5648 * Will equalize heights soon to fix it. */
5649 if (height < 0)
5650 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005651 if (wp->w_height == height)
5652 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005654 if (wp->w_height > 0)
5655 {
5656 if (wp == curwin)
5657 validate_cursor(); /* w_wrow needs to be valid */
5658 if (wp->w_wrow != wp->w_prev_fraction_row)
5659 set_fraction(wp);
5660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 wp->w_height = height;
5663 wp->w_skipcol = 0;
5664
5665 /* Don't change w_topline when height is zero. Don't set w_topline when
5666 * 'scrollbind' is set and this isn't the current window. */
5667 if (height > 0
5668#ifdef FEAT_SCROLLBIND
5669 && (!wp->w_p_scb || wp == curwin)
5670#endif
5671 )
5672 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005673 /*
5674 * Find a value for w_topline that shows the cursor at the same
5675 * relative position in the window as before (more or less).
5676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 lnum = wp->w_cursor.lnum;
5678 if (lnum < 1) /* can happen when starting up */
5679 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005680 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5681 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5683 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005684
5685 if (sline >= 0)
5686 {
5687 /* Make sure the whole cursor line is visible, if possible. */
5688 int rows = plines_win(wp, lnum, FALSE);
5689
5690 if (sline > wp->w_height - rows)
5691 {
5692 sline = wp->w_height - rows;
5693 wp->w_wrow -= rows - line_size;
5694 }
5695 }
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if (sline < 0)
5698 {
5699 /*
5700 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005701 * Make cursor line the first line in the window. If not enough
5702 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 */
5704 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005705 if (wp->w_wrow >= wp->w_height
5706 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5707 {
5708 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5709 --wp->w_wrow;
5710 while (wp->w_wrow >= wp->w_height)
5711 {
5712 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5713 + win_col_off2(wp);
5714 --wp->w_wrow;
5715 }
5716 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005717 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005719 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005721 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723#ifdef FEAT_FOLDING
5724 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5725 if (lnum == 1)
5726 {
5727 /* first line in buffer is folded */
5728 line_size = 1;
5729 --sline;
5730 break;
5731 }
5732#endif
5733 --lnum;
5734#ifdef FEAT_DIFF
5735 if (lnum == wp->w_topline)
5736 line_size = plines_win_nofill(wp, lnum, TRUE)
5737 + wp->w_topfill;
5738 else
5739#endif
5740 line_size = plines_win(wp, lnum, TRUE);
5741 sline -= line_size;
5742 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005743
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 if (sline < 0)
5745 {
5746 /*
5747 * Line we want at top would go off top of screen. Use next
5748 * line instead.
5749 */
5750#ifdef FEAT_FOLDING
5751 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5752#endif
5753 lnum++;
5754 wp->w_wrow -= line_size + sline;
5755 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005756 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 /* First line of file reached, use that as topline. */
5759 lnum = 1;
5760 wp->w_wrow -= sline;
5761 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005762
5763 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766
5767 if (wp == curwin)
5768 {
5769 if (p_so)
5770 update_topline();
5771 curs_columns(FALSE); /* validate w_wrow */
5772 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005773 if (prev_height > 0)
5774 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
5776 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005777 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778#ifdef FEAT_WINDOWS
5779 wp->w_redr_status = TRUE;
5780#endif
5781 invalidate_botline_win(wp);
5782}
5783
5784#ifdef FEAT_VERTSPLIT
5785/*
5786 * Set the width of a window.
5787 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005788 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789win_new_width(wp, width)
5790 win_T *wp;
5791 int width;
5792{
5793 wp->w_width = width;
5794 wp->w_lines_valid = 0;
5795 changed_line_abv_curs_win(wp);
5796 invalidate_botline_win(wp);
5797 if (wp == curwin)
5798 {
5799 update_topline();
5800 curs_columns(TRUE); /* validate w_wrow */
5801 }
5802 redraw_win_later(wp, NOT_VALID);
5803 wp->w_redr_status = TRUE;
5804}
5805#endif
5806
5807 void
5808win_comp_scroll(wp)
5809 win_T *wp;
5810{
5811 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5812 if (wp->w_p_scr == 0)
5813 wp->w_p_scr = 1;
5814}
5815
5816/*
5817 * command_height: called whenever p_ch has been changed
5818 */
5819 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005820command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
5822#ifdef FEAT_WINDOWS
5823 int h;
5824 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005825 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005827 /* Use the value of p_ch that we remembered. This is needed for when the
5828 * GUI starts up, we can't be sure in what order things happen. And when
5829 * p_ch was changed in another tab page. */
5830 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 /* Find bottom frame with width of screen. */
5833 frp = lastwin->w_frame;
5834# ifdef FEAT_VERTSPLIT
5835 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5836 frp = frp->fr_parent;
5837# endif
5838
5839 /* Avoid changing the height of a window with 'winfixheight' set. */
5840 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5841 && frp->fr_win->w_p_wfh)
5842 frp = frp->fr_prev;
5843
5844 if (starting != NO_SCREEN)
5845 {
5846 cmdline_row = Rows - p_ch;
5847
5848 if (p_ch > old_p_ch) /* p_ch got bigger */
5849 {
5850 while (p_ch > old_p_ch)
5851 {
5852 if (frp == NULL)
5853 {
5854 EMSG(_(e_noroom));
5855 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005856 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 cmdline_row = Rows - p_ch;
5858 break;
5859 }
5860 h = frp->fr_height - frame_minheight(frp, NULL);
5861 if (h > p_ch - old_p_ch)
5862 h = p_ch - old_p_ch;
5863 old_p_ch += h;
5864 frame_add_height(frp, -h);
5865 frp = frp->fr_prev;
5866 }
5867
5868 /* Recompute window positions. */
5869 (void)win_comp_pos();
5870
5871 /* clear the lines added to cmdline */
5872 if (full_screen)
5873 screen_fill((int)(cmdline_row), (int)Rows, 0,
5874 (int)Columns, ' ', ' ', 0);
5875 msg_row = cmdline_row;
5876 redraw_cmdline = TRUE;
5877 return;
5878 }
5879
5880 if (msg_row < cmdline_row)
5881 msg_row = cmdline_row;
5882 redraw_cmdline = TRUE;
5883 }
5884 frame_add_height(frp, (int)(old_p_ch - p_ch));
5885
5886 /* Recompute window positions. */
5887 if (frp != lastwin->w_frame)
5888 (void)win_comp_pos();
5889#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005891 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#endif
5893}
5894
5895#if defined(FEAT_WINDOWS) || defined(PROTO)
5896/*
5897 * Resize frame "frp" to be "n" lines higher (negative for less high).
5898 * Also resize the frames it is contained in.
5899 */
5900 static void
5901frame_add_height(frp, n)
5902 frame_T *frp;
5903 int n;
5904{
5905 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5906 for (;;)
5907 {
5908 frp = frp->fr_parent;
5909 if (frp == NULL)
5910 break;
5911 frp->fr_height += n;
5912 }
5913}
5914
5915/*
5916 * Add or remove a status line for the bottom window(s), according to the
5917 * value of 'laststatus'.
5918 */
5919 void
5920last_status(morewin)
5921 int morewin; /* pretend there are two or more windows */
5922{
5923 /* Don't make a difference between horizontal or vertical split. */
5924 last_status_rec(topframe, (p_ls == 2
5925 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5926}
5927
5928 static void
5929last_status_rec(fr, statusline)
5930 frame_T *fr;
5931 int statusline;
5932{
5933 frame_T *fp;
5934 win_T *wp;
5935
5936 if (fr->fr_layout == FR_LEAF)
5937 {
5938 wp = fr->fr_win;
5939 if (wp->w_status_height != 0 && !statusline)
5940 {
5941 /* remove status line */
5942 win_new_height(wp, wp->w_height + 1);
5943 wp->w_status_height = 0;
5944 comp_col();
5945 }
5946 else if (wp->w_status_height == 0 && statusline)
5947 {
5948 /* Find a frame to take a line from. */
5949 fp = fr;
5950 while (fp->fr_height <= frame_minheight(fp, NULL))
5951 {
5952 if (fp == topframe)
5953 {
5954 EMSG(_(e_noroom));
5955 return;
5956 }
5957 /* In a column of frames: go to frame above. If already at
5958 * the top or in a row of frames: go to parent. */
5959 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5960 fp = fp->fr_prev;
5961 else
5962 fp = fp->fr_parent;
5963 }
5964 wp->w_status_height = 1;
5965 if (fp != fr)
5966 {
5967 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5968 frame_fix_height(wp);
5969 (void)win_comp_pos();
5970 }
5971 else
5972 win_new_height(wp, wp->w_height - 1);
5973 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005974 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
5976 }
5977#ifdef FEAT_VERTSPLIT
5978 else if (fr->fr_layout == FR_ROW)
5979 {
5980 /* vertically split windows, set status line for each one */
5981 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5982 last_status_rec(fp, statusline);
5983 }
5984#endif
5985 else
5986 {
5987 /* horizontally split window, set status line for last one */
5988 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5989 ;
5990 last_status_rec(fp, statusline);
5991 }
5992}
5993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005994/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005995 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005996 */
5997 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005998tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006000#ifdef FEAT_GUI_TABLINE
6001 /* When the GUI has the tabline then this always returns zero. */
6002 if (gui_use_tabline())
6003 return 0;
6004#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006005 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006006 {
6007 case 0: return 0;
6008 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6009 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006010 return 1;
6011}
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#endif /* FEAT_WINDOWS */
6014
6015#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6016/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006017 * Get the file name at the cursor.
6018 * If Visual mode is active, use the selected text if it's in one line.
6019 * Returns the name in allocated memory, NULL for failure.
6020 */
6021 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006022grab_file_name(count, file_lnum)
6023 long count;
6024 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006025{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006026 if (VIsual_active)
6027 {
6028 int len;
6029 char_u *ptr;
6030
6031 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6032 return NULL;
6033 return find_file_name_in_path(ptr, len,
6034 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6035 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006036 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6037 file_lnum);
6038
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006039}
6040
6041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 * Return the file name under or after the cursor.
6043 *
6044 * The 'path' option is searched if the file name is not absolute.
6045 * The string returned has been alloc'ed and should be freed by the caller.
6046 * NULL is returned if the file name or file is not found.
6047 *
6048 * options:
6049 * FNAME_MESS give error messages
6050 * FNAME_EXP expand to path
6051 * FNAME_HYP check for hypertext link
6052 * FNAME_INCL apply "includeexpr"
6053 */
6054 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006055file_name_at_cursor(options, count, file_lnum)
6056 int options;
6057 long count;
6058 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006061 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6062 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063}
6064
6065/*
6066 * Return the name of the file under or after ptr[col].
6067 * Otherwise like file_name_at_cursor().
6068 */
6069 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006070file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 char_u *line;
6072 int col;
6073 int options;
6074 long count;
6075 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006076 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 char_u *ptr;
6079 int len;
6080
6081 /*
6082 * search forward for what could be the start of a file name
6083 */
6084 ptr = line + col;
6085 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006086 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 if (*ptr == NUL) /* nothing found */
6088 {
6089 if (options & FNAME_MESS)
6090 EMSG(_("E446: No file name under cursor"));
6091 return NULL;
6092 }
6093
6094 /*
6095 * Search backward for first char of the file name.
6096 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6097 */
6098 while (ptr > line)
6099 {
6100#ifdef FEAT_MBYTE
6101 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6102 ptr -= len + 1;
6103 else
6104#endif
6105 if (vim_isfilec(ptr[-1])
6106 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6107 --ptr;
6108 else
6109 break;
6110 }
6111
6112 /*
6113 * Search forward for the last char of the file name.
6114 * Also allow "://" when ':' is not in 'isfname'.
6115 */
6116 len = 0;
6117 while (vim_isfilec(ptr[len])
6118 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6119#ifdef FEAT_MBYTE
6120 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006121 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 else
6123#endif
6124 ++len;
6125
6126 /*
6127 * If there is trailing punctuation, remove it.
6128 * But don't remove "..", could be a directory name.
6129 */
6130 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6131 && ptr[len - 2] != '.')
6132 --len;
6133
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006134 if (file_lnum != NULL)
6135 {
6136 char_u *p;
6137
6138 /* Get the number after the file name and a separator character */
6139 p = ptr + len;
6140 p = skipwhite(p);
6141 if (*p != NUL)
6142 {
6143 if (!isdigit(*p))
6144 ++p; /* skip the separator */
6145 p = skipwhite(p);
6146 if (isdigit(*p))
6147 *file_lnum = (int)getdigits(&p);
6148 }
6149 }
6150
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6152}
6153
6154# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6155static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6156
6157 static char_u *
6158eval_includeexpr(ptr, len)
6159 char_u *ptr;
6160 int len;
6161{
6162 char_u *res;
6163
6164 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006165 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006166 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 set_vim_var_string(VV_FNAME, NULL, 0);
6168 return res;
6169}
6170#endif
6171
6172/*
6173 * Return the name of the file ptr[len] in 'path'.
6174 * Otherwise like file_name_at_cursor().
6175 */
6176 char_u *
6177find_file_name_in_path(ptr, len, options, count, rel_fname)
6178 char_u *ptr;
6179 int len;
6180 int options;
6181 long count;
6182 char_u *rel_fname; /* file we are searching relative to */
6183{
6184 char_u *file_name;
6185 int c;
6186# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6187 char_u *tofree = NULL;
6188
6189 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6190 {
6191 tofree = eval_includeexpr(ptr, len);
6192 if (tofree != NULL)
6193 {
6194 ptr = tofree;
6195 len = (int)STRLEN(ptr);
6196 }
6197 }
6198# endif
6199
6200 if (options & FNAME_EXP)
6201 {
6202 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6203 TRUE, rel_fname);
6204
6205# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6206 /*
6207 * If the file could not be found in a normal way, try applying
6208 * 'includeexpr' (unless done already).
6209 */
6210 if (file_name == NULL
6211 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6212 {
6213 tofree = eval_includeexpr(ptr, len);
6214 if (tofree != NULL)
6215 {
6216 ptr = tofree;
6217 len = (int)STRLEN(ptr);
6218 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6219 TRUE, rel_fname);
6220 }
6221 }
6222# endif
6223 if (file_name == NULL && (options & FNAME_MESS))
6224 {
6225 c = ptr[len];
6226 ptr[len] = NUL;
6227 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6228 ptr[len] = c;
6229 }
6230
6231 /* Repeat finding the file "count" times. This matters when it
6232 * appears several times in the path. */
6233 while (file_name != NULL && --count > 0)
6234 {
6235 vim_free(file_name);
6236 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6237 }
6238 }
6239 else
6240 file_name = vim_strnsave(ptr, len);
6241
6242# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6243 vim_free(tofree);
6244# endif
6245
6246 return file_name;
6247}
6248#endif /* FEAT_SEARCHPATH */
6249
6250/*
6251 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6252 * Also check for ":\\", which MS Internet Explorer accepts, return
6253 * URL_BACKSLASH.
6254 */
6255 static int
6256path_is_url(p)
6257 char_u *p;
6258{
6259 if (STRNCMP(p, "://", (size_t)3) == 0)
6260 return URL_SLASH;
6261 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6262 return URL_BACKSLASH;
6263 return 0;
6264}
6265
6266/*
6267 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6268 * Return URL_BACKSLASH for "name:\\".
6269 * Return zero otherwise.
6270 */
6271 int
6272path_with_url(fname)
6273 char_u *fname;
6274{
6275 char_u *p;
6276
6277 for (p = fname; isalpha(*p); ++p)
6278 ;
6279 return path_is_url(p);
6280}
6281
6282/*
6283 * Return TRUE if "name" is a full (absolute) path name or URL.
6284 */
6285 int
6286vim_isAbsName(name)
6287 char_u *name;
6288{
6289 return (path_with_url(name) != 0 || mch_isFullName(name));
6290}
6291
6292/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006293 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 *
6295 * return FAIL for failure, OK otherwise
6296 */
6297 int
6298vim_FullName(fname, buf, len, force)
6299 char_u *fname, *buf;
6300 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006301 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 int retval = OK;
6304 int url;
6305
6306 *buf = NUL;
6307 if (fname == NULL)
6308 return FAIL;
6309
6310 url = path_with_url(fname);
6311 if (!url)
6312 retval = mch_FullName(fname, buf, len, force);
6313 if (url || retval == FAIL)
6314 {
6315 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006316 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 }
6318#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6319 slash_adjust(buf);
6320#endif
6321 return retval;
6322}
6323
6324/*
6325 * Return the minimal number of rows that is needed on the screen to display
6326 * the current number of windows.
6327 */
6328 int
6329min_rows()
6330{
6331 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006332#ifdef FEAT_WINDOWS
6333 tabpage_T *tp;
6334 int n;
6335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337 if (firstwin == NULL) /* not initialized yet */
6338 return MIN_LINES;
6339
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006341 total = 0;
6342 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6343 {
6344 n = frame_minheight(tp->tp_topframe, NULL);
6345 if (total < n)
6346 total = n;
6347 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006348 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006350 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006352 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 return total;
6354}
6355
6356/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006357 * Return TRUE if there is only one window (in the current tab page), not
6358 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006359 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 */
6361 int
6362only_one_window()
6363{
6364#ifdef FEAT_WINDOWS
6365 int count = 0;
6366 win_T *wp;
6367
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006368 /* If there is another tab page there always is another window. */
6369 if (first_tabpage->tp_next != NULL)
6370 return FALSE;
6371
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006373 if (wp->w_buffer != NULL
6374 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375# ifdef FEAT_QUICKFIX
6376 || wp->w_p_pvw
6377# endif
6378 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006379# ifdef FEAT_AUTOCMD
6380 && wp != aucmd_win
6381# endif
6382 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 ++count;
6384 return (count <= 1);
6385#else
6386 return TRUE;
6387#endif
6388}
6389
6390#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6391/*
6392 * Correct the cursor line number in other windows. Used after changing the
6393 * current buffer, and before applying autocommands.
6394 * When "do_curwin" is TRUE, also check current window.
6395 */
6396 void
6397check_lnums(do_curwin)
6398 int do_curwin;
6399{
6400 win_T *wp;
6401
6402#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006403 tabpage_T *tp;
6404
6405 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6407#else
6408 wp = curwin;
6409 if (do_curwin)
6410#endif
6411 {
6412 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6413 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6414 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6415 wp->w_topline = curbuf->b_ml.ml_line_count;
6416 }
6417}
6418#endif
6419
6420#if defined(FEAT_WINDOWS) || defined(PROTO)
6421
6422/*
6423 * A snapshot of the window sizes, to restore them after closing the help
6424 * window.
6425 * Only these fields are used:
6426 * fr_layout
6427 * fr_width
6428 * fr_height
6429 * fr_next
6430 * fr_child
6431 * fr_win (only valid for the old curwin, NULL otherwise)
6432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433
6434/*
6435 * Create a snapshot of the current frame sizes.
6436 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006437 void
6438make_snapshot(idx)
6439 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006441 clear_snapshot(curtab, idx);
6442 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443}
6444
6445 static void
6446make_snapshot_rec(fr, frp)
6447 frame_T *fr;
6448 frame_T **frp;
6449{
6450 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6451 if (*frp == NULL)
6452 return;
6453 (*frp)->fr_layout = fr->fr_layout;
6454# ifdef FEAT_VERTSPLIT
6455 (*frp)->fr_width = fr->fr_width;
6456# endif
6457 (*frp)->fr_height = fr->fr_height;
6458 if (fr->fr_next != NULL)
6459 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6460 if (fr->fr_child != NULL)
6461 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6462 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6463 (*frp)->fr_win = curwin;
6464}
6465
6466/*
6467 * Remove any existing snapshot.
6468 */
6469 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006470clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006471 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006472 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006474 clear_snapshot_rec(tp->tp_snapshot[idx]);
6475 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476}
6477
6478 static void
6479clear_snapshot_rec(fr)
6480 frame_T *fr;
6481{
6482 if (fr != NULL)
6483 {
6484 clear_snapshot_rec(fr->fr_next);
6485 clear_snapshot_rec(fr->fr_child);
6486 vim_free(fr);
6487 }
6488}
6489
6490/*
6491 * Restore a previously created snapshot, if there is any.
6492 * This is only done if the screen size didn't change and the window layout is
6493 * still the same.
6494 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006495 void
6496restore_snapshot(idx, close_curwin)
6497 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 int close_curwin; /* closing current window */
6499{
6500 win_T *wp;
6501
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006502 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006504 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006506 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6507 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006509 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 win_comp_pos();
6511 if (wp != NULL && close_curwin)
6512 win_goto(wp);
6513 redraw_all_later(CLEAR);
6514 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006515 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516}
6517
6518/*
6519 * Check if frames "sn" and "fr" have the same layout, same following frames
6520 * and same children.
6521 */
6522 static int
6523check_snapshot_rec(sn, fr)
6524 frame_T *sn;
6525 frame_T *fr;
6526{
6527 if (sn->fr_layout != fr->fr_layout
6528 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6529 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6530 || (sn->fr_next != NULL
6531 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6532 || (sn->fr_child != NULL
6533 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6534 return FAIL;
6535 return OK;
6536}
6537
6538/*
6539 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6540 * following frames and children.
6541 * Returns a pointer to the old current window, or NULL.
6542 */
6543 static win_T *
6544restore_snapshot_rec(sn, fr)
6545 frame_T *sn;
6546 frame_T *fr;
6547{
6548 win_T *wp = NULL;
6549 win_T *wp2;
6550
6551 fr->fr_height = sn->fr_height;
6552# ifdef FEAT_VERTSPLIT
6553 fr->fr_width = sn->fr_width;
6554# endif
6555 if (fr->fr_layout == FR_LEAF)
6556 {
6557 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6558# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006559 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560# endif
6561 wp = sn->fr_win;
6562 }
6563 if (sn->fr_next != NULL)
6564 {
6565 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6566 if (wp2 != NULL)
6567 wp = wp2;
6568 }
6569 if (sn->fr_child != NULL)
6570 {
6571 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6572 if (wp2 != NULL)
6573 wp = wp2;
6574 }
6575 return wp;
6576}
6577
6578#endif
6579
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006580#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6581 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006582/*
6583 * Set "win" to be the curwin and "tp" to be the current tab page.
6584 * restore_win() MUST be called to undo.
6585 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006586 * When "no_display" is TRUE the display won't be affected, no redraw is
6587 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006588 * Returns FAIL if switching to "win" failed.
6589 */
6590 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006591switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006592 win_T **save_curwin UNUSED;
6593 tabpage_T **save_curtab UNUSED;
6594 win_T *win UNUSED;
6595 tabpage_T *tp UNUSED;
6596 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006597{
6598# ifdef FEAT_AUTOCMD
6599 block_autocmds();
6600# endif
6601# ifdef FEAT_WINDOWS
6602 *save_curwin = curwin;
6603 if (tp != NULL)
6604 {
6605 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006606 if (no_display)
6607 {
6608 curtab->tp_firstwin = firstwin;
6609 curtab->tp_lastwin = lastwin;
6610 curtab = tp;
6611 firstwin = curtab->tp_firstwin;
6612 lastwin = curtab->tp_lastwin;
6613 }
6614 else
6615 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006616 }
6617 if (!win_valid(win))
6618 {
6619# ifdef FEAT_AUTOCMD
6620 unblock_autocmds();
6621# endif
6622 return FAIL;
6623 }
6624 curwin = win;
6625 curbuf = curwin->w_buffer;
6626# endif
6627 return OK;
6628}
6629
6630/*
6631 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006632 * When "no_display" is TRUE the display won't be affected, no redraw is
6633 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006634 */
6635 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006636restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006637 win_T *save_curwin UNUSED;
6638 tabpage_T *save_curtab UNUSED;
6639 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006640{
6641# ifdef FEAT_WINDOWS
6642 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006643 {
6644 if (no_display)
6645 {
6646 curtab->tp_firstwin = firstwin;
6647 curtab->tp_lastwin = lastwin;
6648 curtab = save_curtab;
6649 firstwin = curtab->tp_firstwin;
6650 lastwin = curtab->tp_lastwin;
6651 }
6652 else
6653 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6654 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006655 if (win_valid(save_curwin))
6656 {
6657 curwin = save_curwin;
6658 curbuf = curwin->w_buffer;
6659 }
6660# endif
6661# ifdef FEAT_AUTOCMD
6662 unblock_autocmds();
6663# endif
6664}
6665
6666/*
6667 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6668 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6669 */
6670 void
6671switch_buffer(save_curbuf, buf)
6672 buf_T *buf;
6673 buf_T **save_curbuf;
6674{
6675# ifdef FEAT_AUTOCMD
6676 block_autocmds();
6677# endif
6678 *save_curbuf = curbuf;
6679 --curbuf->b_nwindows;
6680 curbuf = buf;
6681 curwin->w_buffer = buf;
6682 ++curbuf->b_nwindows;
6683}
6684
6685/*
6686 * Restore the current buffer after using switch_buffer().
6687 */
6688 void
6689restore_buffer(save_curbuf)
6690 buf_T *save_curbuf;
6691{
6692# ifdef FEAT_AUTOCMD
6693 unblock_autocmds();
6694# endif
6695 /* Check for valid buffer, just in case. */
6696 if (buf_valid(save_curbuf))
6697 {
6698 --curbuf->b_nwindows;
6699 curwin->w_buffer = save_curbuf;
6700 curbuf = save_curbuf;
6701 ++curbuf->b_nwindows;
6702 }
6703}
6704#endif
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6707/*
6708 * Return TRUE if there is any vertically split window.
6709 */
6710 int
6711win_hasvertsplit()
6712{
6713 frame_T *fr;
6714
6715 if (topframe->fr_layout == FR_ROW)
6716 return TRUE;
6717
6718 if (topframe->fr_layout == FR_COL)
6719 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6720 if (fr->fr_layout == FR_ROW)
6721 return TRUE;
6722
6723 return FALSE;
6724}
6725#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006726
6727#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6728/*
6729 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006730 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006731 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6732 * If no particular ID is desired, -1 must be specified for 'id'.
6733 * Return ID of added match, -1 on failure.
6734 */
6735 int
6736match_add(wp, grp, pat, prio, id)
6737 win_T *wp;
6738 char_u *grp;
6739 char_u *pat;
6740 int prio;
6741 int id;
6742{
6743 matchitem_T *cur;
6744 matchitem_T *prev;
6745 matchitem_T *m;
6746 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006747 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006748
6749 if (*grp == NUL || *pat == NUL)
6750 return -1;
6751 if (id < -1 || id == 0)
6752 {
6753 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6754 return -1;
6755 }
6756 if (id != -1)
6757 {
6758 cur = wp->w_match_head;
6759 while (cur != NULL)
6760 {
6761 if (cur->id == id)
6762 {
6763 EMSGN("E801: ID already taken: %ld", id);
6764 return -1;
6765 }
6766 cur = cur->next;
6767 }
6768 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006769 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006770 {
6771 EMSG2(_(e_nogroup), grp);
6772 return -1;
6773 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006774 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006775 {
6776 EMSG2(_(e_invarg2), pat);
6777 return -1;
6778 }
6779
6780 /* Find available match ID. */
6781 while (id == -1)
6782 {
6783 cur = wp->w_match_head;
6784 while (cur != NULL && cur->id != wp->w_next_match_id)
6785 cur = cur->next;
6786 if (cur == NULL)
6787 id = wp->w_next_match_id;
6788 wp->w_next_match_id++;
6789 }
6790
6791 /* Build new match. */
6792 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6793 m->id = id;
6794 m->priority = prio;
6795 m->pattern = vim_strsave(pat);
6796 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006797 m->match.regprog = regprog;
6798 m->match.rmm_ic = FALSE;
6799 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006800
6801 /* Insert new match. The match list is in ascending order with regard to
6802 * the match priorities. */
6803 cur = wp->w_match_head;
6804 prev = cur;
6805 while (cur != NULL && prio >= cur->priority)
6806 {
6807 prev = cur;
6808 cur = cur->next;
6809 }
6810 if (cur == prev)
6811 wp->w_match_head = m;
6812 else
6813 prev->next = m;
6814 m->next = cur;
6815
6816 redraw_later(SOME_VALID);
6817 return id;
6818}
6819
6820/*
6821 * Delete match with ID 'id' in the match list of window 'wp'.
6822 * Print error messages if 'perr' is TRUE.
6823 */
6824 int
6825match_delete(wp, id, perr)
6826 win_T *wp;
6827 int id;
6828 int perr;
6829{
6830 matchitem_T *cur = wp->w_match_head;
6831 matchitem_T *prev = cur;
6832
6833 if (id < 1)
6834 {
6835 if (perr == TRUE)
6836 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6837 id);
6838 return -1;
6839 }
6840 while (cur != NULL && cur->id != id)
6841 {
6842 prev = cur;
6843 cur = cur->next;
6844 }
6845 if (cur == NULL)
6846 {
6847 if (perr == TRUE)
6848 EMSGN("E803: ID not found: %ld", id);
6849 return -1;
6850 }
6851 if (cur == prev)
6852 wp->w_match_head = cur->next;
6853 else
6854 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006855 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006856 vim_free(cur->pattern);
6857 vim_free(cur);
6858 redraw_later(SOME_VALID);
6859 return 0;
6860}
6861
6862/*
6863 * Delete all matches in the match list of window 'wp'.
6864 */
6865 void
6866clear_matches(wp)
6867 win_T *wp;
6868{
6869 matchitem_T *m;
6870
6871 while (wp->w_match_head != NULL)
6872 {
6873 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006874 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006875 vim_free(wp->w_match_head->pattern);
6876 vim_free(wp->w_match_head);
6877 wp->w_match_head = m;
6878 }
6879 redraw_later(SOME_VALID);
6880}
6881
6882/*
6883 * Get match from ID 'id' in window 'wp'.
6884 * Return NULL if match not found.
6885 */
6886 matchitem_T *
6887get_match(wp, id)
6888 win_T *wp;
6889 int id;
6890{
6891 matchitem_T *cur = wp->w_match_head;
6892
6893 while (cur != NULL && cur->id != id)
6894 cur = cur->next;
6895 return cur;
6896}
6897#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006898
6899#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6900 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006901get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006902{
6903 int i = 1;
6904 win_T *w;
6905
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006906 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006907 ++i;
6908
6909 if (w == NULL)
6910 return 0;
6911 else
6912 return i;
6913}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006914
6915 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006916get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006917{
6918 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006919# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006920 tabpage_T *t;
6921
6922 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6923 ++i;
6924
6925 if (t == NULL)
6926 return 0;
6927 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006928# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006929 return i;
6930}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006931#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006932
Bram Moolenaarf0327f62013-06-28 20:16:55 +02006933#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006934/*
6935 * Return TRUE if "topfrp" and its children are at the right height.
6936 */
6937 static int
6938frame_check_height(topfrp, height)
6939 frame_T *topfrp;
6940 int height;
6941{
6942 frame_T *frp;
6943
6944 if (topfrp->fr_height != height)
6945 return FALSE;
6946
6947 if (topfrp->fr_layout == FR_ROW)
6948 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6949 if (frp->fr_height != height)
6950 return FALSE;
6951
6952 return TRUE;
6953}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02006954#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006955
6956#ifdef FEAT_VERTSPLIT
6957/*
6958 * Return TRUE if "topfrp" and its children are at the right width.
6959 */
6960 static int
6961frame_check_width(topfrp, width)
6962 frame_T *topfrp;
6963 int width;
6964{
6965 frame_T *frp;
6966
6967 if (topfrp->fr_width != width)
6968 return FALSE;
6969
6970 if (topfrp->fr_layout == FR_COL)
6971 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6972 if (frp->fr_width != width)
6973 return FALSE;
6974
6975 return TRUE;
6976}
6977#endif
6978