blob: 81064518c1051fa302ef1c4c317955d9e416af27 [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 Moolenaarec1561c2014-06-17 13:52:40 +02004253
4254 /* Might need to scroll the old window before switching, e.g., when the
4255 * cursor was moved. */
4256 update_topline();
4257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 /* may have to copy the buffer options when 'cpo' contains 'S' */
4259 if (wp->w_buffer != curbuf)
4260 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4261 if (!curwin_invalid)
4262 {
4263 prevwin = curwin; /* remember for CTRL-W p */
4264 curwin->w_redr_status = TRUE;
4265 }
4266 curwin = wp;
4267 curbuf = wp->w_buffer;
4268 check_cursor();
4269#ifdef FEAT_VIRTUALEDIT
4270 if (!virtual_active())
4271 curwin->w_cursor.coladd = 0;
4272#endif
4273 changed_line_abv_curs(); /* assume cursor position needs updating */
4274
4275 if (curwin->w_localdir != NULL)
4276 {
4277 /* Window has a local directory: Save current directory as global
4278 * directory (unless that was done already) and change to the local
4279 * directory. */
4280 if (globaldir == NULL)
4281 {
4282 char_u cwd[MAXPATHL];
4283
4284 if (mch_dirname(cwd, MAXPATHL) == OK)
4285 globaldir = vim_strsave(cwd);
4286 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004287 if (mch_chdir((char *)curwin->w_localdir) == 0)
4288 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 else if (globaldir != NULL)
4291 {
4292 /* Window doesn't have a local directory and we are not in the global
4293 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004294 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 vim_free(globaldir);
4296 globaldir = NULL;
4297 shorten_fnames(TRUE);
4298 }
4299
4300#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004301 if (trigger_enter_autocmds)
4302 {
4303 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4304 if (other_buffer)
4305 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307#endif
4308
4309#ifdef FEAT_TITLE
4310 maketitle();
4311#endif
4312 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004313 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 if (restart_edit)
4315 redraw_later(VALID); /* causes status line redraw */
4316
4317 /* set window height to desired minimal value */
4318 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4319 win_setheight((int)p_wh);
4320 else if (curwin->w_height == 0)
4321 win_setheight(1);
4322
4323#ifdef FEAT_VERTSPLIT
4324 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004325 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 win_setwidth((int)p_wiw);
4327#endif
4328
4329#ifdef FEAT_MOUSE
4330 setmouse(); /* in case jumped to/from help buffer */
4331#endif
4332
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004333 /* Change directories when the 'acd' option is set. */
4334 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335}
4336
4337#endif /* FEAT_WINDOWS */
4338
4339#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4340/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004341 * Jump to the first open window that contains buffer "buf", if one exists.
4342 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 */
4344 win_T *
4345buf_jump_open_win(buf)
4346 buf_T *buf;
4347{
4348# ifdef FEAT_WINDOWS
4349 win_T *wp;
4350
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004351 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if (wp->w_buffer == buf)
4353 break;
4354 if (wp != NULL)
4355 win_enter(wp, FALSE);
4356 return wp;
4357# else
4358 if (curwin->w_buffer == buf)
4359 return curwin;
4360 return NULL;
4361# endif
4362}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004363
4364/*
4365 * Jump to the first open window in any tab page that contains buffer "buf",
4366 * if one exists.
4367 * Returns a pointer to the window found, otherwise NULL.
4368 */
4369 win_T *
4370buf_jump_open_tab(buf)
4371 buf_T *buf;
4372{
4373# ifdef FEAT_WINDOWS
4374 win_T *wp;
4375 tabpage_T *tp;
4376
4377 /* First try the current tab page. */
4378 wp = buf_jump_open_win(buf);
4379 if (wp != NULL)
4380 return wp;
4381
4382 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4383 if (tp != curtab)
4384 {
4385 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4386 if (wp->w_buffer == buf)
4387 break;
4388 if (wp != NULL)
4389 {
4390 goto_tabpage_win(tp, wp);
4391 if (curwin != wp)
4392 wp = NULL; /* something went wrong */
4393 break;
4394 }
4395 }
4396
4397 return wp;
4398# else
4399 if (curwin->w_buffer == buf)
4400 return curwin;
4401 return NULL;
4402# endif
4403}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404#endif
4405
4406/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004407 * Allocate a window structure and link it in the window list when "hidden" is
4408 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004411win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004412 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004413 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004415 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416
4417 /*
4418 * allocate window structure and linesizes arrays
4419 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004420 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004421 if (new_wp == NULL)
4422 return NULL;
4423
4424 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004426 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004427 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429
Bram Moolenaar429fa852013-04-15 12:27:36 +02004430#ifdef FEAT_EVAL
4431 /* init w: variables */
4432 new_wp->w_vars = dict_alloc();
4433 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004435 win_free_lsize(new_wp);
4436 vim_free(new_wp);
4437 return NULL;
4438 }
4439 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004440#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004441
4442#ifdef FEAT_AUTOCMD
4443 /* Don't execute autocommands while the window is not properly
4444 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4445 * event. */
4446 block_autocmds();
4447#endif
4448 /*
4449 * link the window in the window list
4450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004452 if (!hidden)
4453 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454#endif
4455#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004456 new_wp->w_wincol = 0;
4457 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458#endif
4459
Bram Moolenaar429fa852013-04-15 12:27:36 +02004460 /* position the display and the cursor at the top of the file. */
4461 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004463 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004465 new_wp->w_botline = 2;
4466 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004468 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469#endif
4470
Bram Moolenaar429fa852013-04-15 12:27:36 +02004471 /* We won't calculate w_fraction until resizing the window */
4472 new_wp->w_fraction = 0;
4473 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004476 if (gui.in_use)
4477 {
4478 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4479 SBAR_LEFT, new_wp);
4480 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4481 SBAR_RIGHT, new_wp);
4482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483#endif
4484#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004485 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004487#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004488 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004489#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004490#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004491 new_wp->w_match_head = NULL;
4492 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004493#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004494 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495}
4496
4497#if defined(FEAT_WINDOWS) || defined(PROTO)
4498
4499/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004500 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 */
4502 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004503win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004505 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506{
4507 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004508 buf_T *buf;
4509 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004511#ifdef FEAT_FOLDING
4512 clearFolding(wp);
4513#endif
4514
4515 /* reduce the reference count to the argument list. */
4516 alist_unlink(wp->w_alist);
4517
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004518#ifdef FEAT_AUTOCMD
4519 /* Don't execute autocommands while the window is halfway being deleted.
4520 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004521 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004522#endif
4523
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004524#ifdef FEAT_LUA
4525 lua_window_free(wp);
4526#endif
4527
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004528#ifdef FEAT_MZSCHEME
4529 mzscheme_window_free(wp);
4530#endif
4531
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532#ifdef FEAT_PERL
4533 perl_win_free(wp);
4534#endif
4535
4536#ifdef FEAT_PYTHON
4537 python_window_free(wp);
4538#endif
4539
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004540#ifdef FEAT_PYTHON3
4541 python3_window_free(wp);
4542#endif
4543
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544#ifdef FEAT_TCL
4545 tcl_window_free(wp);
4546#endif
4547
4548#ifdef FEAT_RUBY
4549 ruby_window_free(wp);
4550#endif
4551
4552 clear_winopt(&wp->w_onebuf_opt);
4553 clear_winopt(&wp->w_allbuf_opt);
4554
4555#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004556 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4557 hash_init(&wp->w_vars->dv_hashtab);
4558 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559#endif
4560
4561 if (prevwin == wp)
4562 prevwin = NULL;
4563 win_free_lsize(wp);
4564
4565 for (i = 0; i < wp->w_tagstacklen; ++i)
4566 vim_free(wp->w_tagstack[i].tagname);
4567
4568 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004569
Bram Moolenaarff18df02013-07-24 17:51:57 +02004570 /* Remove the window from the b_wininfo lists, it may happen that the
4571 * freed memory is re-used for another window. */
4572 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4573 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4574 if (wip->wi_win == wp)
4575 wip->wi_win = NULL;
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004578 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581#ifdef FEAT_JUMPLIST
4582 free_jumplist(wp);
4583#endif
4584
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004585#ifdef FEAT_QUICKFIX
4586 qf_free_all(wp);
4587#endif
4588
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589#ifdef FEAT_GUI
4590 if (gui.in_use)
4591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4593 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4594 }
4595#endif /* FEAT_GUI */
4596
Bram Moolenaar860cae12010-06-05 23:22:07 +02004597#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004598 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004599#endif
4600
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004601#ifdef FEAT_AUTOCMD
4602 if (wp != aucmd_win)
4603#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004604 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004605#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004606 if (autocmd_busy)
4607 {
4608 wp->w_next = au_pending_free_win;
4609 au_pending_free_win = wp;
4610 }
4611 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004612#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004613 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004614
4615#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004616 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618}
4619
4620/*
4621 * Append window "wp" in the window list after window "after".
4622 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004623 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624win_append(after, wp)
4625 win_T *after, *wp;
4626{
4627 win_T *before;
4628
4629 if (after == NULL) /* after NULL is in front of the first */
4630 before = firstwin;
4631 else
4632 before = after->w_next;
4633
4634 wp->w_next = before;
4635 wp->w_prev = after;
4636 if (after == NULL)
4637 firstwin = wp;
4638 else
4639 after->w_next = wp;
4640 if (before == NULL)
4641 lastwin = wp;
4642 else
4643 before->w_prev = wp;
4644}
4645
4646/*
4647 * Remove a window from the window list.
4648 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004649 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004650win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004652 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
4654 if (wp->w_prev != NULL)
4655 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004656 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004658 else
4659 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (wp->w_next != NULL)
4661 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004662 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004664 else
4665 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666}
4667
4668/*
4669 * Append frame "frp" in a frame list after frame "after".
4670 */
4671 static void
4672frame_append(after, frp)
4673 frame_T *after, *frp;
4674{
4675 frp->fr_next = after->fr_next;
4676 after->fr_next = frp;
4677 if (frp->fr_next != NULL)
4678 frp->fr_next->fr_prev = frp;
4679 frp->fr_prev = after;
4680}
4681
4682/*
4683 * Insert frame "frp" in a frame list before frame "before".
4684 */
4685 static void
4686frame_insert(before, frp)
4687 frame_T *before, *frp;
4688{
4689 frp->fr_next = before;
4690 frp->fr_prev = before->fr_prev;
4691 before->fr_prev = frp;
4692 if (frp->fr_prev != NULL)
4693 frp->fr_prev->fr_next = frp;
4694 else
4695 frp->fr_parent->fr_child = frp;
4696}
4697
4698/*
4699 * Remove a frame from a frame list.
4700 */
4701 static void
4702frame_remove(frp)
4703 frame_T *frp;
4704{
4705 if (frp->fr_prev != NULL)
4706 frp->fr_prev->fr_next = frp->fr_next;
4707 else
4708 frp->fr_parent->fr_child = frp->fr_next;
4709 if (frp->fr_next != NULL)
4710 frp->fr_next->fr_prev = frp->fr_prev;
4711}
4712
4713#endif /* FEAT_WINDOWS */
4714
4715/*
4716 * Allocate w_lines[] for window "wp".
4717 * Return FAIL for failure, OK for success.
4718 */
4719 int
4720win_alloc_lines(wp)
4721 win_T *wp;
4722{
4723 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004724 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 if (wp->w_lines == NULL)
4726 return FAIL;
4727 return OK;
4728}
4729
4730/*
4731 * free lsize arrays for a window
4732 */
4733 void
4734win_free_lsize(wp)
4735 win_T *wp;
4736{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004737 /* TODO: why would wp be NULL here? */
4738 if (wp != NULL)
4739 {
4740 vim_free(wp->w_lines);
4741 wp->w_lines = NULL;
4742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743}
4744
4745/*
4746 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004747 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 */
4749 void
4750shell_new_rows()
4751{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004752 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753
4754 if (firstwin == NULL) /* not initialized yet */
4755 return;
4756#ifdef FEAT_WINDOWS
4757 if (h < frame_minheight(topframe, NULL))
4758 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004759
4760 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 * that doesn't result in the right height, forget about that option. */
4762 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004763 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 frame_new_height(topframe, h, FALSE, FALSE);
4765
4766 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4767#else
4768 if (h < 1)
4769 h = 1;
4770 win_new_height(firstwin, h);
4771#endif
4772 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004773#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004774 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004775#endif
4776
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777#if 0
4778 /* Disabled: don't want making the screen smaller make a window larger. */
4779 if (p_ea)
4780 win_equal(curwin, FALSE, 'v');
4781#endif
4782}
4783
4784#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4785/*
4786 * Called from win_new_shellsize() after Columns changed.
4787 */
4788 void
4789shell_new_columns()
4790{
4791 if (firstwin == NULL) /* not initialized yet */
4792 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004793
4794 /* First try setting the widths of windows with 'winfixwidth'. If that
4795 * doesn't result in the right width, forget about that option. */
4796 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004797 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004798 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4799
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4801#if 0
4802 /* Disabled: don't want making the screen smaller make a window larger. */
4803 if (p_ea)
4804 win_equal(curwin, FALSE, 'h');
4805#endif
4806}
4807#endif
4808
4809#if defined(FEAT_CMDWIN) || defined(PROTO)
4810/*
4811 * Save the size of all windows in "gap".
4812 */
4813 void
4814win_size_save(gap)
4815 garray_T *gap;
4816
4817{
4818 win_T *wp;
4819
4820 ga_init2(gap, (int)sizeof(int), 1);
4821 if (ga_grow(gap, win_count() * 2) == OK)
4822 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4823 {
4824 ((int *)gap->ga_data)[gap->ga_len++] =
4825 wp->w_width + wp->w_vsep_width;
4826 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4827 }
4828}
4829
4830/*
4831 * Restore window sizes, but only if the number of windows is still the same.
4832 * Does not free the growarray.
4833 */
4834 void
4835win_size_restore(gap)
4836 garray_T *gap;
4837{
4838 win_T *wp;
4839 int i;
4840
4841 if (win_count() * 2 == gap->ga_len)
4842 {
4843 i = 0;
4844 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4845 {
4846 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4847 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4848 }
4849 /* recompute the window positions */
4850 (void)win_comp_pos();
4851 }
4852}
4853#endif /* FEAT_CMDWIN */
4854
4855#if defined(FEAT_WINDOWS) || defined(PROTO)
4856/*
4857 * Update the position for all windows, using the width and height of the
4858 * frames.
4859 * Returns the row just after the last window.
4860 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004861 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862win_comp_pos()
4863{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004864 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 int col = 0;
4866
4867 frame_comp_pos(topframe, &row, &col);
4868 return row;
4869}
4870
4871/*
4872 * Update the position of the windows in frame "topfrp", using the width and
4873 * height of the frames.
4874 * "*row" and "*col" are the top-left position of the frame. They are updated
4875 * to the bottom-right position plus one.
4876 */
4877 static void
4878frame_comp_pos(topfrp, row, col)
4879 frame_T *topfrp;
4880 int *row;
4881 int *col;
4882{
4883 win_T *wp;
4884 frame_T *frp;
4885#ifdef FEAT_VERTSPLIT
4886 int startcol;
4887 int startrow;
4888#endif
4889
4890 wp = topfrp->fr_win;
4891 if (wp != NULL)
4892 {
4893 if (wp->w_winrow != *row
4894#ifdef FEAT_VERTSPLIT
4895 || wp->w_wincol != *col
4896#endif
4897 )
4898 {
4899 /* position changed, redraw */
4900 wp->w_winrow = *row;
4901#ifdef FEAT_VERTSPLIT
4902 wp->w_wincol = *col;
4903#endif
4904 redraw_win_later(wp, NOT_VALID);
4905 wp->w_redr_status = TRUE;
4906 }
4907 *row += wp->w_height + wp->w_status_height;
4908#ifdef FEAT_VERTSPLIT
4909 *col += wp->w_width + wp->w_vsep_width;
4910#endif
4911 }
4912 else
4913 {
4914#ifdef FEAT_VERTSPLIT
4915 startrow = *row;
4916 startcol = *col;
4917#endif
4918 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4919 {
4920#ifdef FEAT_VERTSPLIT
4921 if (topfrp->fr_layout == FR_ROW)
4922 *row = startrow; /* all frames are at the same row */
4923 else
4924 *col = startcol; /* all frames are at the same col */
4925#endif
4926 frame_comp_pos(frp, row, col);
4927 }
4928 }
4929}
4930
4931#endif /* FEAT_WINDOWS */
4932
4933/*
4934 * Set current window height and take care of repositioning other windows to
4935 * fit around it.
4936 */
4937 void
4938win_setheight(height)
4939 int height;
4940{
4941 win_setheight_win(height, curwin);
4942}
4943
4944/*
4945 * Set the window height of window "win" and take care of repositioning other
4946 * windows to fit around it.
4947 */
4948 void
4949win_setheight_win(height, win)
4950 int height;
4951 win_T *win;
4952{
4953 int row;
4954
4955 if (win == curwin)
4956 {
4957 /* Always keep current window at least one line high, even when
4958 * 'winminheight' is zero. */
4959#ifdef FEAT_WINDOWS
4960 if (height < p_wmh)
4961 height = p_wmh;
4962#endif
4963 if (height == 0)
4964 height = 1;
4965 }
4966
4967#ifdef FEAT_WINDOWS
4968 frame_setheight(win->w_frame, height + win->w_status_height);
4969
4970 /* recompute the window positions */
4971 row = win_comp_pos();
4972#else
4973 if (height > topframe->fr_height)
4974 height = topframe->fr_height;
4975 win->w_height = height;
4976 row = height;
4977#endif
4978
4979 /*
4980 * If there is extra space created between the last window and the command
4981 * line, clear it.
4982 */
4983 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4984 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4985 cmdline_row = row;
4986 msg_row = row;
4987 msg_col = 0;
4988
4989 redraw_all_later(NOT_VALID);
4990}
4991
4992#if defined(FEAT_WINDOWS) || defined(PROTO)
4993
4994/*
4995 * Set the height of a frame to "height" and take care that all frames and
4996 * windows inside it are resized. Also resize frames on the left and right if
4997 * the are in the same FR_ROW frame.
4998 *
4999 * Strategy:
5000 * If the frame is part of a FR_COL frame, try fitting the frame in that
5001 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5002 * go to containing frames to resize them and make room.
5003 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5004 * Check for the minimal height of the FR_ROW frame.
5005 * At the top level we can also use change the command line height.
5006 */
5007 static void
5008frame_setheight(curfrp, height)
5009 frame_T *curfrp;
5010 int height;
5011{
5012 int room; /* total number of lines available */
5013 int take; /* number of lines taken from other windows */
5014 int room_cmdline; /* lines available from cmdline */
5015 int run;
5016 frame_T *frp;
5017 int h;
5018 int room_reserved;
5019
5020 /* If the height already is the desired value, nothing to do. */
5021 if (curfrp->fr_height == height)
5022 return;
5023
5024 if (curfrp->fr_parent == NULL)
5025 {
5026 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005027 if (height > ROWS_AVAIL)
5028 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 if (height > 0)
5030 frame_new_height(curfrp, height, FALSE, FALSE);
5031 }
5032 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5033 {
5034 /* Row of frames: Also need to resize frames left and right of this
5035 * one. First check for the minimal height of these. */
5036 h = frame_minheight(curfrp->fr_parent, NULL);
5037 if (height < h)
5038 height = h;
5039 frame_setheight(curfrp->fr_parent, height);
5040 }
5041 else
5042 {
5043 /*
5044 * Column of frames: try to change only frames in this column.
5045 */
5046#ifdef FEAT_VERTSPLIT
5047 /*
5048 * Do this twice:
5049 * 1: compute room available, if it's not enough try resizing the
5050 * containing frame.
5051 * 2: compute the room available and adjust the height to it.
5052 * Try not to reduce the height of a window with 'winfixheight' set.
5053 */
5054 for (run = 1; run <= 2; ++run)
5055#else
5056 for (;;)
5057#endif
5058 {
5059 room = 0;
5060 room_reserved = 0;
5061 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5062 frp = frp->fr_next)
5063 {
5064 if (frp != curfrp
5065 && frp->fr_win != NULL
5066 && frp->fr_win->w_p_wfh)
5067 room_reserved += frp->fr_height;
5068 room += frp->fr_height;
5069 if (frp != curfrp)
5070 room -= frame_minheight(frp, NULL);
5071 }
5072#ifdef FEAT_VERTSPLIT
5073 if (curfrp->fr_width != Columns)
5074 room_cmdline = 0;
5075 else
5076#endif
5077 {
5078 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5079 + lastwin->w_height + lastwin->w_status_height);
5080 if (room_cmdline < 0)
5081 room_cmdline = 0;
5082 }
5083
5084 if (height <= room + room_cmdline)
5085 break;
5086#ifdef FEAT_VERTSPLIT
5087 if (run == 2 || curfrp->fr_width == Columns)
5088#endif
5089 {
5090 if (height > room + room_cmdline)
5091 height = room + room_cmdline;
5092 break;
5093 }
5094#ifdef FEAT_VERTSPLIT
5095 frame_setheight(curfrp->fr_parent, height
5096 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5097#endif
5098 /*NOTREACHED*/
5099 }
5100
5101 /*
5102 * Compute the number of lines we will take from others frames (can be
5103 * negative!).
5104 */
5105 take = height - curfrp->fr_height;
5106
5107 /* If there is not enough room, also reduce the height of a window
5108 * with 'winfixheight' set. */
5109 if (height > room + room_cmdline - room_reserved)
5110 room_reserved = room + room_cmdline - height;
5111 /* If there is only a 'winfixheight' window and making the
5112 * window smaller, need to make the other window taller. */
5113 if (take < 0 && room - curfrp->fr_height < room_reserved)
5114 room_reserved = 0;
5115
5116 if (take > 0 && room_cmdline > 0)
5117 {
5118 /* use lines from cmdline first */
5119 if (take < room_cmdline)
5120 room_cmdline = take;
5121 take -= room_cmdline;
5122 topframe->fr_height += room_cmdline;
5123 }
5124
5125 /*
5126 * set the current frame to the new height
5127 */
5128 frame_new_height(curfrp, height, FALSE, FALSE);
5129
5130 /*
5131 * First take lines from the frames after the current frame. If
5132 * that is not enough, takes lines from frames above the current
5133 * frame.
5134 */
5135 for (run = 0; run < 2; ++run)
5136 {
5137 if (run == 0)
5138 frp = curfrp->fr_next; /* 1st run: start with next window */
5139 else
5140 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5141 while (frp != NULL && take != 0)
5142 {
5143 h = frame_minheight(frp, NULL);
5144 if (room_reserved > 0
5145 && frp->fr_win != NULL
5146 && frp->fr_win->w_p_wfh)
5147 {
5148 if (room_reserved >= frp->fr_height)
5149 room_reserved -= frp->fr_height;
5150 else
5151 {
5152 if (frp->fr_height - room_reserved > take)
5153 room_reserved = frp->fr_height - take;
5154 take -= frp->fr_height - room_reserved;
5155 frame_new_height(frp, room_reserved, FALSE, FALSE);
5156 room_reserved = 0;
5157 }
5158 }
5159 else
5160 {
5161 if (frp->fr_height - take < h)
5162 {
5163 take -= frp->fr_height - h;
5164 frame_new_height(frp, h, FALSE, FALSE);
5165 }
5166 else
5167 {
5168 frame_new_height(frp, frp->fr_height - take,
5169 FALSE, FALSE);
5170 take = 0;
5171 }
5172 }
5173 if (run == 0)
5174 frp = frp->fr_next;
5175 else
5176 frp = frp->fr_prev;
5177 }
5178 }
5179 }
5180}
5181
5182#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5183/*
5184 * Set current window width and take care of repositioning other windows to
5185 * fit around it.
5186 */
5187 void
5188win_setwidth(width)
5189 int width;
5190{
5191 win_setwidth_win(width, curwin);
5192}
5193
5194 void
5195win_setwidth_win(width, wp)
5196 int width;
5197 win_T *wp;
5198{
5199 /* Always keep current window at least one column wide, even when
5200 * 'winminwidth' is zero. */
5201 if (wp == curwin)
5202 {
5203 if (width < p_wmw)
5204 width = p_wmw;
5205 if (width == 0)
5206 width = 1;
5207 }
5208
5209 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5210
5211 /* recompute the window positions */
5212 (void)win_comp_pos();
5213
5214 redraw_all_later(NOT_VALID);
5215}
5216
5217/*
5218 * Set the width of a frame to "width" and take care that all frames and
5219 * windows inside it are resized. Also resize frames above and below if the
5220 * are in the same FR_ROW frame.
5221 *
5222 * Strategy is similar to frame_setheight().
5223 */
5224 static void
5225frame_setwidth(curfrp, width)
5226 frame_T *curfrp;
5227 int width;
5228{
5229 int room; /* total number of lines available */
5230 int take; /* number of lines taken from other windows */
5231 int run;
5232 frame_T *frp;
5233 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005234 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236 /* If the width already is the desired value, nothing to do. */
5237 if (curfrp->fr_width == width)
5238 return;
5239
5240 if (curfrp->fr_parent == NULL)
5241 /* topframe: can't change width */
5242 return;
5243
5244 if (curfrp->fr_parent->fr_layout == FR_COL)
5245 {
5246 /* Column of frames: Also need to resize frames above and below of
5247 * this one. First check for the minimal width of these. */
5248 w = frame_minwidth(curfrp->fr_parent, NULL);
5249 if (width < w)
5250 width = w;
5251 frame_setwidth(curfrp->fr_parent, width);
5252 }
5253 else
5254 {
5255 /*
5256 * Row of frames: try to change only frames in this row.
5257 *
5258 * Do this twice:
5259 * 1: compute room available, if it's not enough try resizing the
5260 * containing frame.
5261 * 2: compute the room available and adjust the width to it.
5262 */
5263 for (run = 1; run <= 2; ++run)
5264 {
5265 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005266 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5268 frp = frp->fr_next)
5269 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005270 if (frp != curfrp
5271 && frp->fr_win != NULL
5272 && frp->fr_win->w_p_wfw)
5273 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 room += frp->fr_width;
5275 if (frp != curfrp)
5276 room -= frame_minwidth(frp, NULL);
5277 }
5278
5279 if (width <= room)
5280 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005281 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
5283 if (width > room)
5284 width = room;
5285 break;
5286 }
5287 frame_setwidth(curfrp->fr_parent, width
5288 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5289 }
5290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 /*
5292 * Compute the number of lines we will take from others frames (can be
5293 * negative!).
5294 */
5295 take = width - curfrp->fr_width;
5296
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005297 /* If there is not enough room, also reduce the width of a window
5298 * with 'winfixwidth' set. */
5299 if (width > room - room_reserved)
5300 room_reserved = room - width;
5301 /* If there is only a 'winfixwidth' window and making the
5302 * window smaller, need to make the other window narrower. */
5303 if (take < 0 && room - curfrp->fr_width < room_reserved)
5304 room_reserved = 0;
5305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 /*
5307 * set the current frame to the new width
5308 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005309 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
5311 /*
5312 * First take lines from the frames right of the current frame. If
5313 * that is not enough, takes lines from frames left of the current
5314 * frame.
5315 */
5316 for (run = 0; run < 2; ++run)
5317 {
5318 if (run == 0)
5319 frp = curfrp->fr_next; /* 1st run: start with next window */
5320 else
5321 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5322 while (frp != NULL && take != 0)
5323 {
5324 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005325 if (room_reserved > 0
5326 && frp->fr_win != NULL
5327 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005329 if (room_reserved >= frp->fr_width)
5330 room_reserved -= frp->fr_width;
5331 else
5332 {
5333 if (frp->fr_width - room_reserved > take)
5334 room_reserved = frp->fr_width - take;
5335 take -= frp->fr_width - room_reserved;
5336 frame_new_width(frp, room_reserved, FALSE, FALSE);
5337 room_reserved = 0;
5338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340 else
5341 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005342 if (frp->fr_width - take < w)
5343 {
5344 take -= frp->fr_width - w;
5345 frame_new_width(frp, w, FALSE, FALSE);
5346 }
5347 else
5348 {
5349 frame_new_width(frp, frp->fr_width - take,
5350 FALSE, FALSE);
5351 take = 0;
5352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
5354 if (run == 0)
5355 frp = frp->fr_next;
5356 else
5357 frp = frp->fr_prev;
5358 }
5359 }
5360 }
5361}
5362#endif /* FEAT_VERTSPLIT */
5363
5364/*
5365 * Check 'winminheight' for a valid value.
5366 */
5367 void
5368win_setminheight()
5369{
5370 int room;
5371 int first = TRUE;
5372 win_T *wp;
5373
5374 /* loop until there is a 'winminheight' that is possible */
5375 while (p_wmh > 0)
5376 {
5377 /* TODO: handle vertical splits */
5378 room = -p_wh;
5379 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5380 room += wp->w_height - p_wmh;
5381 if (room >= 0)
5382 break;
5383 --p_wmh;
5384 if (first)
5385 {
5386 EMSG(_(e_noroom));
5387 first = FALSE;
5388 }
5389 }
5390}
5391
5392#ifdef FEAT_MOUSE
5393
5394/*
5395 * Status line of dragwin is dragged "offset" lines down (negative is up).
5396 */
5397 void
5398win_drag_status_line(dragwin, offset)
5399 win_T *dragwin;
5400 int offset;
5401{
5402 frame_T *curfr;
5403 frame_T *fr;
5404 int room;
5405 int row;
5406 int up; /* if TRUE, drag status line up, otherwise down */
5407 int n;
5408
5409 fr = dragwin->w_frame;
5410 curfr = fr;
5411 if (fr != topframe) /* more than one window */
5412 {
5413 fr = fr->fr_parent;
5414 /* When the parent frame is not a column of frames, its parent should
5415 * be. */
5416 if (fr->fr_layout != FR_COL)
5417 {
5418 curfr = fr;
5419 if (fr != topframe) /* only a row of windows, may drag statusline */
5420 fr = fr->fr_parent;
5421 }
5422 }
5423
5424 /* If this is the last frame in a column, may want to resize the parent
5425 * frame instead (go two up to skip a row of frames). */
5426 while (curfr != topframe && curfr->fr_next == NULL)
5427 {
5428 if (fr != topframe)
5429 fr = fr->fr_parent;
5430 curfr = fr;
5431 if (fr != topframe)
5432 fr = fr->fr_parent;
5433 }
5434
5435 if (offset < 0) /* drag up */
5436 {
5437 up = TRUE;
5438 offset = -offset;
5439 /* sum up the room of the current frame and above it */
5440 if (fr == curfr)
5441 {
5442 /* only one window */
5443 room = fr->fr_height - frame_minheight(fr, NULL);
5444 }
5445 else
5446 {
5447 room = 0;
5448 for (fr = fr->fr_child; ; fr = fr->fr_next)
5449 {
5450 room += fr->fr_height - frame_minheight(fr, NULL);
5451 if (fr == curfr)
5452 break;
5453 }
5454 }
5455 fr = curfr->fr_next; /* put fr at frame that grows */
5456 }
5457 else /* drag down */
5458 {
5459 up = FALSE;
5460 /*
5461 * Only dragging the last status line can reduce p_ch.
5462 */
5463 room = Rows - cmdline_row;
5464 if (curfr->fr_next == NULL)
5465 room -= 1;
5466 else
5467 room -= p_ch;
5468 if (room < 0)
5469 room = 0;
5470 /* sum up the room of frames below of the current one */
5471 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5472 room += fr->fr_height - frame_minheight(fr, NULL);
5473 fr = curfr; /* put fr at window that grows */
5474 }
5475
5476 if (room < offset) /* Not enough room */
5477 offset = room; /* Move as far as we can */
5478 if (offset <= 0)
5479 return;
5480
5481 /*
5482 * Grow frame fr by "offset" lines.
5483 * Doesn't happen when dragging the last status line up.
5484 */
5485 if (fr != NULL)
5486 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5487
5488 if (up)
5489 fr = curfr; /* current frame gets smaller */
5490 else
5491 fr = curfr->fr_next; /* next frame gets smaller */
5492
5493 /*
5494 * Now make the other frames smaller.
5495 */
5496 while (fr != NULL && offset > 0)
5497 {
5498 n = frame_minheight(fr, NULL);
5499 if (fr->fr_height - offset <= n)
5500 {
5501 offset -= fr->fr_height - n;
5502 frame_new_height(fr, n, !up, FALSE);
5503 }
5504 else
5505 {
5506 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5507 break;
5508 }
5509 if (up)
5510 fr = fr->fr_prev;
5511 else
5512 fr = fr->fr_next;
5513 }
5514 row = win_comp_pos();
5515 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5516 cmdline_row = row;
5517 p_ch = Rows - cmdline_row;
5518 if (p_ch < 1)
5519 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005520 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005521 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 showmode();
5523}
5524
5525#ifdef FEAT_VERTSPLIT
5526/*
5527 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5528 */
5529 void
5530win_drag_vsep_line(dragwin, offset)
5531 win_T *dragwin;
5532 int offset;
5533{
5534 frame_T *curfr;
5535 frame_T *fr;
5536 int room;
5537 int left; /* if TRUE, drag separator line left, otherwise right */
5538 int n;
5539
5540 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005541 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 return;
5543 curfr = fr;
5544 fr = fr->fr_parent;
5545 /* When the parent frame is not a row of frames, its parent should be. */
5546 if (fr->fr_layout != FR_ROW)
5547 {
5548 if (fr == topframe) /* only a column of windows (cannot happen?) */
5549 return;
5550 curfr = fr;
5551 fr = fr->fr_parent;
5552 }
5553
5554 /* If this is the last frame in a row, may want to resize a parent
5555 * frame instead. */
5556 while (curfr->fr_next == NULL)
5557 {
5558 if (fr == topframe)
5559 break;
5560 curfr = fr;
5561 fr = fr->fr_parent;
5562 if (fr != topframe)
5563 {
5564 curfr = fr;
5565 fr = fr->fr_parent;
5566 }
5567 }
5568
5569 if (offset < 0) /* drag left */
5570 {
5571 left = TRUE;
5572 offset = -offset;
5573 /* sum up the room of the current frame and left of it */
5574 room = 0;
5575 for (fr = fr->fr_child; ; fr = fr->fr_next)
5576 {
5577 room += fr->fr_width - frame_minwidth(fr, NULL);
5578 if (fr == curfr)
5579 break;
5580 }
5581 fr = curfr->fr_next; /* put fr at frame that grows */
5582 }
5583 else /* drag right */
5584 {
5585 left = FALSE;
5586 /* sum up the room of frames right of the current one */
5587 room = 0;
5588 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5589 room += fr->fr_width - frame_minwidth(fr, NULL);
5590 fr = curfr; /* put fr at window that grows */
5591 }
5592
5593 if (room < offset) /* Not enough room */
5594 offset = room; /* Move as far as we can */
5595 if (offset <= 0) /* No room at all, quit. */
5596 return;
5597
5598 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005599 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600
5601 /* shrink other frames: current and at the left or at the right */
5602 if (left)
5603 fr = curfr; /* current frame gets smaller */
5604 else
5605 fr = curfr->fr_next; /* next frame gets smaller */
5606
5607 while (fr != NULL && offset > 0)
5608 {
5609 n = frame_minwidth(fr, NULL);
5610 if (fr->fr_width - offset <= n)
5611 {
5612 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005613 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 }
5615 else
5616 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005617 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 break;
5619 }
5620 if (left)
5621 fr = fr->fr_prev;
5622 else
5623 fr = fr->fr_next;
5624 }
5625 (void)win_comp_pos();
5626 redraw_all_later(NOT_VALID);
5627}
5628#endif /* FEAT_VERTSPLIT */
5629#endif /* FEAT_MOUSE */
5630
5631#endif /* FEAT_WINDOWS */
5632
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005633#define FRACTION_MULT 16384L
5634
5635/*
5636 * Set wp->w_fraction for the current w_wrow and w_height.
5637 */
5638 static void
5639set_fraction(wp)
5640 win_T *wp;
5641{
5642 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005643 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005644}
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646/*
5647 * Set the height of a window.
5648 * This takes care of the things inside the window, not what happens to the
5649 * window position, the frame or to other windows.
5650 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005651 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652win_new_height(wp, height)
5653 win_T *wp;
5654 int height;
5655{
5656 linenr_T lnum;
5657 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005658 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 /* Don't want a negative height. Happens when splitting a tiny window.
5661 * Will equalize heights soon to fix it. */
5662 if (height < 0)
5663 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005664 if (wp->w_height == height)
5665 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005667 if (wp->w_height > 0)
5668 {
5669 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005670 /* w_wrow needs to be valid. When setting 'laststatus' this may
5671 * call win_new_height() recursively. */
5672 validate_cursor();
5673 if (wp->w_height != prev_height)
5674 return; /* Recursive call already changed the size, bail out here
5675 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005676 if (wp->w_wrow != wp->w_prev_fraction_row)
5677 set_fraction(wp);
5678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
5680 wp->w_height = height;
5681 wp->w_skipcol = 0;
5682
5683 /* Don't change w_topline when height is zero. Don't set w_topline when
5684 * 'scrollbind' is set and this isn't the current window. */
5685 if (height > 0
5686#ifdef FEAT_SCROLLBIND
5687 && (!wp->w_p_scb || wp == curwin)
5688#endif
5689 )
5690 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005691 /*
5692 * Find a value for w_topline that shows the cursor at the same
5693 * relative position in the window as before (more or less).
5694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 lnum = wp->w_cursor.lnum;
5696 if (lnum < 1) /* can happen when starting up */
5697 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005698 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5699 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5701 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005702
5703 if (sline >= 0)
5704 {
5705 /* Make sure the whole cursor line is visible, if possible. */
5706 int rows = plines_win(wp, lnum, FALSE);
5707
5708 if (sline > wp->w_height - rows)
5709 {
5710 sline = wp->w_height - rows;
5711 wp->w_wrow -= rows - line_size;
5712 }
5713 }
5714
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 if (sline < 0)
5716 {
5717 /*
5718 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005719 * Make cursor line the first line in the window. If not enough
5720 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 */
5722 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005723 if (wp->w_wrow >= wp->w_height
5724 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5725 {
5726 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5727 --wp->w_wrow;
5728 while (wp->w_wrow >= wp->w_height)
5729 {
5730 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5731 + win_col_off2(wp);
5732 --wp->w_wrow;
5733 }
5734 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005735 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005737 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005739 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
5741#ifdef FEAT_FOLDING
5742 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5743 if (lnum == 1)
5744 {
5745 /* first line in buffer is folded */
5746 line_size = 1;
5747 --sline;
5748 break;
5749 }
5750#endif
5751 --lnum;
5752#ifdef FEAT_DIFF
5753 if (lnum == wp->w_topline)
5754 line_size = plines_win_nofill(wp, lnum, TRUE)
5755 + wp->w_topfill;
5756 else
5757#endif
5758 line_size = plines_win(wp, lnum, TRUE);
5759 sline -= line_size;
5760 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 if (sline < 0)
5763 {
5764 /*
5765 * Line we want at top would go off top of screen. Use next
5766 * line instead.
5767 */
5768#ifdef FEAT_FOLDING
5769 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5770#endif
5771 lnum++;
5772 wp->w_wrow -= line_size + sline;
5773 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005774 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 {
5776 /* First line of file reached, use that as topline. */
5777 lnum = 1;
5778 wp->w_wrow -= sline;
5779 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005780
5781 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784
5785 if (wp == curwin)
5786 {
5787 if (p_so)
5788 update_topline();
5789 curs_columns(FALSE); /* validate w_wrow */
5790 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005791 if (prev_height > 0)
5792 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005795 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#ifdef FEAT_WINDOWS
5797 wp->w_redr_status = TRUE;
5798#endif
5799 invalidate_botline_win(wp);
5800}
5801
5802#ifdef FEAT_VERTSPLIT
5803/*
5804 * Set the width of a window.
5805 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005806 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807win_new_width(wp, width)
5808 win_T *wp;
5809 int width;
5810{
5811 wp->w_width = width;
5812 wp->w_lines_valid = 0;
5813 changed_line_abv_curs_win(wp);
5814 invalidate_botline_win(wp);
5815 if (wp == curwin)
5816 {
5817 update_topline();
5818 curs_columns(TRUE); /* validate w_wrow */
5819 }
5820 redraw_win_later(wp, NOT_VALID);
5821 wp->w_redr_status = TRUE;
5822}
5823#endif
5824
5825 void
5826win_comp_scroll(wp)
5827 win_T *wp;
5828{
5829 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5830 if (wp->w_p_scr == 0)
5831 wp->w_p_scr = 1;
5832}
5833
5834/*
5835 * command_height: called whenever p_ch has been changed
5836 */
5837 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005838command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840#ifdef FEAT_WINDOWS
5841 int h;
5842 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005843 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005845 /* Use the value of p_ch that we remembered. This is needed for when the
5846 * GUI starts up, we can't be sure in what order things happen. And when
5847 * p_ch was changed in another tab page. */
5848 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005849
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 /* Find bottom frame with width of screen. */
5851 frp = lastwin->w_frame;
5852# ifdef FEAT_VERTSPLIT
5853 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5854 frp = frp->fr_parent;
5855# endif
5856
5857 /* Avoid changing the height of a window with 'winfixheight' set. */
5858 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5859 && frp->fr_win->w_p_wfh)
5860 frp = frp->fr_prev;
5861
5862 if (starting != NO_SCREEN)
5863 {
5864 cmdline_row = Rows - p_ch;
5865
5866 if (p_ch > old_p_ch) /* p_ch got bigger */
5867 {
5868 while (p_ch > old_p_ch)
5869 {
5870 if (frp == NULL)
5871 {
5872 EMSG(_(e_noroom));
5873 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005874 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 cmdline_row = Rows - p_ch;
5876 break;
5877 }
5878 h = frp->fr_height - frame_minheight(frp, NULL);
5879 if (h > p_ch - old_p_ch)
5880 h = p_ch - old_p_ch;
5881 old_p_ch += h;
5882 frame_add_height(frp, -h);
5883 frp = frp->fr_prev;
5884 }
5885
5886 /* Recompute window positions. */
5887 (void)win_comp_pos();
5888
5889 /* clear the lines added to cmdline */
5890 if (full_screen)
5891 screen_fill((int)(cmdline_row), (int)Rows, 0,
5892 (int)Columns, ' ', ' ', 0);
5893 msg_row = cmdline_row;
5894 redraw_cmdline = TRUE;
5895 return;
5896 }
5897
5898 if (msg_row < cmdline_row)
5899 msg_row = cmdline_row;
5900 redraw_cmdline = TRUE;
5901 }
5902 frame_add_height(frp, (int)(old_p_ch - p_ch));
5903
5904 /* Recompute window positions. */
5905 if (frp != lastwin->w_frame)
5906 (void)win_comp_pos();
5907#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005909 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910#endif
5911}
5912
5913#if defined(FEAT_WINDOWS) || defined(PROTO)
5914/*
5915 * Resize frame "frp" to be "n" lines higher (negative for less high).
5916 * Also resize the frames it is contained in.
5917 */
5918 static void
5919frame_add_height(frp, n)
5920 frame_T *frp;
5921 int n;
5922{
5923 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5924 for (;;)
5925 {
5926 frp = frp->fr_parent;
5927 if (frp == NULL)
5928 break;
5929 frp->fr_height += n;
5930 }
5931}
5932
5933/*
5934 * Add or remove a status line for the bottom window(s), according to the
5935 * value of 'laststatus'.
5936 */
5937 void
5938last_status(morewin)
5939 int morewin; /* pretend there are two or more windows */
5940{
5941 /* Don't make a difference between horizontal or vertical split. */
5942 last_status_rec(topframe, (p_ls == 2
5943 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5944}
5945
5946 static void
5947last_status_rec(fr, statusline)
5948 frame_T *fr;
5949 int statusline;
5950{
5951 frame_T *fp;
5952 win_T *wp;
5953
5954 if (fr->fr_layout == FR_LEAF)
5955 {
5956 wp = fr->fr_win;
5957 if (wp->w_status_height != 0 && !statusline)
5958 {
5959 /* remove status line */
5960 win_new_height(wp, wp->w_height + 1);
5961 wp->w_status_height = 0;
5962 comp_col();
5963 }
5964 else if (wp->w_status_height == 0 && statusline)
5965 {
5966 /* Find a frame to take a line from. */
5967 fp = fr;
5968 while (fp->fr_height <= frame_minheight(fp, NULL))
5969 {
5970 if (fp == topframe)
5971 {
5972 EMSG(_(e_noroom));
5973 return;
5974 }
5975 /* In a column of frames: go to frame above. If already at
5976 * the top or in a row of frames: go to parent. */
5977 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5978 fp = fp->fr_prev;
5979 else
5980 fp = fp->fr_parent;
5981 }
5982 wp->w_status_height = 1;
5983 if (fp != fr)
5984 {
5985 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5986 frame_fix_height(wp);
5987 (void)win_comp_pos();
5988 }
5989 else
5990 win_new_height(wp, wp->w_height - 1);
5991 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005992 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 }
5994 }
5995#ifdef FEAT_VERTSPLIT
5996 else if (fr->fr_layout == FR_ROW)
5997 {
5998 /* vertically split windows, set status line for each one */
5999 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6000 last_status_rec(fp, statusline);
6001 }
6002#endif
6003 else
6004 {
6005 /* horizontally split window, set status line for last one */
6006 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6007 ;
6008 last_status_rec(fp, statusline);
6009 }
6010}
6011
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006012/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006013 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006014 */
6015 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006016tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006017{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006018#ifdef FEAT_GUI_TABLINE
6019 /* When the GUI has the tabline then this always returns zero. */
6020 if (gui_use_tabline())
6021 return 0;
6022#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006023 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006024 {
6025 case 0: return 0;
6026 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6027 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006028 return 1;
6029}
6030
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031#endif /* FEAT_WINDOWS */
6032
6033#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6034/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006035 * Get the file name at the cursor.
6036 * If Visual mode is active, use the selected text if it's in one line.
6037 * Returns the name in allocated memory, NULL for failure.
6038 */
6039 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006040grab_file_name(count, file_lnum)
6041 long count;
6042 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006043{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006044 if (VIsual_active)
6045 {
6046 int len;
6047 char_u *ptr;
6048
6049 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6050 return NULL;
6051 return find_file_name_in_path(ptr, len,
6052 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6053 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006054 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6055 file_lnum);
6056
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006057}
6058
6059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 * Return the file name under or after the cursor.
6061 *
6062 * The 'path' option is searched if the file name is not absolute.
6063 * The string returned has been alloc'ed and should be freed by the caller.
6064 * NULL is returned if the file name or file is not found.
6065 *
6066 * options:
6067 * FNAME_MESS give error messages
6068 * FNAME_EXP expand to path
6069 * FNAME_HYP check for hypertext link
6070 * FNAME_INCL apply "includeexpr"
6071 */
6072 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006073file_name_at_cursor(options, count, file_lnum)
6074 int options;
6075 long count;
6076 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006079 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6080 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081}
6082
6083/*
6084 * Return the name of the file under or after ptr[col].
6085 * Otherwise like file_name_at_cursor().
6086 */
6087 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006088file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 char_u *line;
6090 int col;
6091 int options;
6092 long count;
6093 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006094 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 char_u *ptr;
6097 int len;
6098
6099 /*
6100 * search forward for what could be the start of a file name
6101 */
6102 ptr = line + col;
6103 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006104 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (*ptr == NUL) /* nothing found */
6106 {
6107 if (options & FNAME_MESS)
6108 EMSG(_("E446: No file name under cursor"));
6109 return NULL;
6110 }
6111
6112 /*
6113 * Search backward for first char of the file name.
6114 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6115 */
6116 while (ptr > line)
6117 {
6118#ifdef FEAT_MBYTE
6119 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6120 ptr -= len + 1;
6121 else
6122#endif
6123 if (vim_isfilec(ptr[-1])
6124 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6125 --ptr;
6126 else
6127 break;
6128 }
6129
6130 /*
6131 * Search forward for the last char of the file name.
6132 * Also allow "://" when ':' is not in 'isfname'.
6133 */
6134 len = 0;
6135 while (vim_isfilec(ptr[len])
6136 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6137#ifdef FEAT_MBYTE
6138 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006139 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 else
6141#endif
6142 ++len;
6143
6144 /*
6145 * If there is trailing punctuation, remove it.
6146 * But don't remove "..", could be a directory name.
6147 */
6148 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6149 && ptr[len - 2] != '.')
6150 --len;
6151
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006152 if (file_lnum != NULL)
6153 {
6154 char_u *p;
6155
6156 /* Get the number after the file name and a separator character */
6157 p = ptr + len;
6158 p = skipwhite(p);
6159 if (*p != NUL)
6160 {
6161 if (!isdigit(*p))
6162 ++p; /* skip the separator */
6163 p = skipwhite(p);
6164 if (isdigit(*p))
6165 *file_lnum = (int)getdigits(&p);
6166 }
6167 }
6168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6170}
6171
6172# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6173static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6174
6175 static char_u *
6176eval_includeexpr(ptr, len)
6177 char_u *ptr;
6178 int len;
6179{
6180 char_u *res;
6181
6182 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006183 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006184 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 set_vim_var_string(VV_FNAME, NULL, 0);
6186 return res;
6187}
6188#endif
6189
6190/*
6191 * Return the name of the file ptr[len] in 'path'.
6192 * Otherwise like file_name_at_cursor().
6193 */
6194 char_u *
6195find_file_name_in_path(ptr, len, options, count, rel_fname)
6196 char_u *ptr;
6197 int len;
6198 int options;
6199 long count;
6200 char_u *rel_fname; /* file we are searching relative to */
6201{
6202 char_u *file_name;
6203 int c;
6204# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6205 char_u *tofree = NULL;
6206
6207 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6208 {
6209 tofree = eval_includeexpr(ptr, len);
6210 if (tofree != NULL)
6211 {
6212 ptr = tofree;
6213 len = (int)STRLEN(ptr);
6214 }
6215 }
6216# endif
6217
6218 if (options & FNAME_EXP)
6219 {
6220 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6221 TRUE, rel_fname);
6222
6223# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6224 /*
6225 * If the file could not be found in a normal way, try applying
6226 * 'includeexpr' (unless done already).
6227 */
6228 if (file_name == NULL
6229 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6230 {
6231 tofree = eval_includeexpr(ptr, len);
6232 if (tofree != NULL)
6233 {
6234 ptr = tofree;
6235 len = (int)STRLEN(ptr);
6236 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6237 TRUE, rel_fname);
6238 }
6239 }
6240# endif
6241 if (file_name == NULL && (options & FNAME_MESS))
6242 {
6243 c = ptr[len];
6244 ptr[len] = NUL;
6245 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6246 ptr[len] = c;
6247 }
6248
6249 /* Repeat finding the file "count" times. This matters when it
6250 * appears several times in the path. */
6251 while (file_name != NULL && --count > 0)
6252 {
6253 vim_free(file_name);
6254 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6255 }
6256 }
6257 else
6258 file_name = vim_strnsave(ptr, len);
6259
6260# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6261 vim_free(tofree);
6262# endif
6263
6264 return file_name;
6265}
6266#endif /* FEAT_SEARCHPATH */
6267
6268/*
6269 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6270 * Also check for ":\\", which MS Internet Explorer accepts, return
6271 * URL_BACKSLASH.
6272 */
6273 static int
6274path_is_url(p)
6275 char_u *p;
6276{
6277 if (STRNCMP(p, "://", (size_t)3) == 0)
6278 return URL_SLASH;
6279 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6280 return URL_BACKSLASH;
6281 return 0;
6282}
6283
6284/*
6285 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6286 * Return URL_BACKSLASH for "name:\\".
6287 * Return zero otherwise.
6288 */
6289 int
6290path_with_url(fname)
6291 char_u *fname;
6292{
6293 char_u *p;
6294
6295 for (p = fname; isalpha(*p); ++p)
6296 ;
6297 return path_is_url(p);
6298}
6299
6300/*
6301 * Return TRUE if "name" is a full (absolute) path name or URL.
6302 */
6303 int
6304vim_isAbsName(name)
6305 char_u *name;
6306{
6307 return (path_with_url(name) != 0 || mch_isFullName(name));
6308}
6309
6310/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006311 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 *
6313 * return FAIL for failure, OK otherwise
6314 */
6315 int
6316vim_FullName(fname, buf, len, force)
6317 char_u *fname, *buf;
6318 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006319 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 int retval = OK;
6322 int url;
6323
6324 *buf = NUL;
6325 if (fname == NULL)
6326 return FAIL;
6327
6328 url = path_with_url(fname);
6329 if (!url)
6330 retval = mch_FullName(fname, buf, len, force);
6331 if (url || retval == FAIL)
6332 {
6333 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006334 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6337 slash_adjust(buf);
6338#endif
6339 return retval;
6340}
6341
6342/*
6343 * Return the minimal number of rows that is needed on the screen to display
6344 * the current number of windows.
6345 */
6346 int
6347min_rows()
6348{
6349 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006350#ifdef FEAT_WINDOWS
6351 tabpage_T *tp;
6352 int n;
6353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
6355 if (firstwin == NULL) /* not initialized yet */
6356 return MIN_LINES;
6357
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006359 total = 0;
6360 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6361 {
6362 n = frame_minheight(tp->tp_topframe, NULL);
6363 if (total < n)
6364 total = n;
6365 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006366 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006368 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006370 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 return total;
6372}
6373
6374/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006375 * Return TRUE if there is only one window (in the current tab page), not
6376 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006377 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 */
6379 int
6380only_one_window()
6381{
6382#ifdef FEAT_WINDOWS
6383 int count = 0;
6384 win_T *wp;
6385
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006386 /* If there is another tab page there always is another window. */
6387 if (first_tabpage->tp_next != NULL)
6388 return FALSE;
6389
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006391 if (wp->w_buffer != NULL
6392 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393# ifdef FEAT_QUICKFIX
6394 || wp->w_p_pvw
6395# endif
6396 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006397# ifdef FEAT_AUTOCMD
6398 && wp != aucmd_win
6399# endif
6400 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 ++count;
6402 return (count <= 1);
6403#else
6404 return TRUE;
6405#endif
6406}
6407
6408#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6409/*
6410 * Correct the cursor line number in other windows. Used after changing the
6411 * current buffer, and before applying autocommands.
6412 * When "do_curwin" is TRUE, also check current window.
6413 */
6414 void
6415check_lnums(do_curwin)
6416 int do_curwin;
6417{
6418 win_T *wp;
6419
6420#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006421 tabpage_T *tp;
6422
6423 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6425#else
6426 wp = curwin;
6427 if (do_curwin)
6428#endif
6429 {
6430 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6431 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6432 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6433 wp->w_topline = curbuf->b_ml.ml_line_count;
6434 }
6435}
6436#endif
6437
6438#if defined(FEAT_WINDOWS) || defined(PROTO)
6439
6440/*
6441 * A snapshot of the window sizes, to restore them after closing the help
6442 * window.
6443 * Only these fields are used:
6444 * fr_layout
6445 * fr_width
6446 * fr_height
6447 * fr_next
6448 * fr_child
6449 * fr_win (only valid for the old curwin, NULL otherwise)
6450 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
6452/*
6453 * Create a snapshot of the current frame sizes.
6454 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006455 void
6456make_snapshot(idx)
6457 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006459 clear_snapshot(curtab, idx);
6460 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461}
6462
6463 static void
6464make_snapshot_rec(fr, frp)
6465 frame_T *fr;
6466 frame_T **frp;
6467{
6468 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6469 if (*frp == NULL)
6470 return;
6471 (*frp)->fr_layout = fr->fr_layout;
6472# ifdef FEAT_VERTSPLIT
6473 (*frp)->fr_width = fr->fr_width;
6474# endif
6475 (*frp)->fr_height = fr->fr_height;
6476 if (fr->fr_next != NULL)
6477 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6478 if (fr->fr_child != NULL)
6479 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6480 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6481 (*frp)->fr_win = curwin;
6482}
6483
6484/*
6485 * Remove any existing snapshot.
6486 */
6487 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006488clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006489 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006490 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006492 clear_snapshot_rec(tp->tp_snapshot[idx]);
6493 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494}
6495
6496 static void
6497clear_snapshot_rec(fr)
6498 frame_T *fr;
6499{
6500 if (fr != NULL)
6501 {
6502 clear_snapshot_rec(fr->fr_next);
6503 clear_snapshot_rec(fr->fr_child);
6504 vim_free(fr);
6505 }
6506}
6507
6508/*
6509 * Restore a previously created snapshot, if there is any.
6510 * This is only done if the screen size didn't change and the window layout is
6511 * still the same.
6512 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006513 void
6514restore_snapshot(idx, close_curwin)
6515 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 int close_curwin; /* closing current window */
6517{
6518 win_T *wp;
6519
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006520 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006522 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006524 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6525 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006527 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 win_comp_pos();
6529 if (wp != NULL && close_curwin)
6530 win_goto(wp);
6531 redraw_all_later(CLEAR);
6532 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006533 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534}
6535
6536/*
6537 * Check if frames "sn" and "fr" have the same layout, same following frames
6538 * and same children.
6539 */
6540 static int
6541check_snapshot_rec(sn, fr)
6542 frame_T *sn;
6543 frame_T *fr;
6544{
6545 if (sn->fr_layout != fr->fr_layout
6546 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6547 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6548 || (sn->fr_next != NULL
6549 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6550 || (sn->fr_child != NULL
6551 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6552 return FAIL;
6553 return OK;
6554}
6555
6556/*
6557 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6558 * following frames and children.
6559 * Returns a pointer to the old current window, or NULL.
6560 */
6561 static win_T *
6562restore_snapshot_rec(sn, fr)
6563 frame_T *sn;
6564 frame_T *fr;
6565{
6566 win_T *wp = NULL;
6567 win_T *wp2;
6568
6569 fr->fr_height = sn->fr_height;
6570# ifdef FEAT_VERTSPLIT
6571 fr->fr_width = sn->fr_width;
6572# endif
6573 if (fr->fr_layout == FR_LEAF)
6574 {
6575 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6576# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006577 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578# endif
6579 wp = sn->fr_win;
6580 }
6581 if (sn->fr_next != NULL)
6582 {
6583 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6584 if (wp2 != NULL)
6585 wp = wp2;
6586 }
6587 if (sn->fr_child != NULL)
6588 {
6589 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6590 if (wp2 != NULL)
6591 wp = wp2;
6592 }
6593 return wp;
6594}
6595
6596#endif
6597
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006598#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6599 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006600/*
6601 * Set "win" to be the curwin and "tp" to be the current tab page.
6602 * restore_win() MUST be called to undo.
6603 * No autocommands will be executed.
Bram Moolenaard6949742013-06-16 14:18:28 +02006604 * When "no_display" is TRUE the display won't be affected, no redraw is
6605 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006606 * Returns FAIL if switching to "win" failed.
6607 */
6608 int
Bram Moolenaard6949742013-06-16 14:18:28 +02006609switch_win(save_curwin, save_curtab, win, tp, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006610 win_T **save_curwin UNUSED;
6611 tabpage_T **save_curtab UNUSED;
6612 win_T *win UNUSED;
6613 tabpage_T *tp UNUSED;
6614 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006615{
6616# ifdef FEAT_AUTOCMD
6617 block_autocmds();
6618# endif
6619# ifdef FEAT_WINDOWS
6620 *save_curwin = curwin;
6621 if (tp != NULL)
6622 {
6623 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006624 if (no_display)
6625 {
6626 curtab->tp_firstwin = firstwin;
6627 curtab->tp_lastwin = lastwin;
6628 curtab = tp;
6629 firstwin = curtab->tp_firstwin;
6630 lastwin = curtab->tp_lastwin;
6631 }
6632 else
6633 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006634 }
6635 if (!win_valid(win))
6636 {
6637# ifdef FEAT_AUTOCMD
6638 unblock_autocmds();
6639# endif
6640 return FAIL;
6641 }
6642 curwin = win;
6643 curbuf = curwin->w_buffer;
6644# endif
6645 return OK;
6646}
6647
6648/*
6649 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006650 * When "no_display" is TRUE the display won't be affected, no redraw is
6651 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006652 */
6653 void
Bram Moolenaard6949742013-06-16 14:18:28 +02006654restore_win(save_curwin, save_curtab, no_display)
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006655 win_T *save_curwin UNUSED;
6656 tabpage_T *save_curtab UNUSED;
6657 int no_display UNUSED;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006658{
6659# ifdef FEAT_WINDOWS
6660 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006661 {
6662 if (no_display)
6663 {
6664 curtab->tp_firstwin = firstwin;
6665 curtab->tp_lastwin = lastwin;
6666 curtab = save_curtab;
6667 firstwin = curtab->tp_firstwin;
6668 lastwin = curtab->tp_lastwin;
6669 }
6670 else
6671 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6672 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006673 if (win_valid(save_curwin))
6674 {
6675 curwin = save_curwin;
6676 curbuf = curwin->w_buffer;
6677 }
6678# endif
6679# ifdef FEAT_AUTOCMD
6680 unblock_autocmds();
6681# endif
6682}
6683
6684/*
6685 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6686 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6687 */
6688 void
6689switch_buffer(save_curbuf, buf)
6690 buf_T *buf;
6691 buf_T **save_curbuf;
6692{
6693# ifdef FEAT_AUTOCMD
6694 block_autocmds();
6695# endif
6696 *save_curbuf = curbuf;
6697 --curbuf->b_nwindows;
6698 curbuf = buf;
6699 curwin->w_buffer = buf;
6700 ++curbuf->b_nwindows;
6701}
6702
6703/*
6704 * Restore the current buffer after using switch_buffer().
6705 */
6706 void
6707restore_buffer(save_curbuf)
6708 buf_T *save_curbuf;
6709{
6710# ifdef FEAT_AUTOCMD
6711 unblock_autocmds();
6712# endif
6713 /* Check for valid buffer, just in case. */
6714 if (buf_valid(save_curbuf))
6715 {
6716 --curbuf->b_nwindows;
6717 curwin->w_buffer = save_curbuf;
6718 curbuf = save_curbuf;
6719 ++curbuf->b_nwindows;
6720 }
6721}
6722#endif
6723
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6725/*
6726 * Return TRUE if there is any vertically split window.
6727 */
6728 int
6729win_hasvertsplit()
6730{
6731 frame_T *fr;
6732
6733 if (topframe->fr_layout == FR_ROW)
6734 return TRUE;
6735
6736 if (topframe->fr_layout == FR_COL)
6737 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6738 if (fr->fr_layout == FR_ROW)
6739 return TRUE;
6740
6741 return FALSE;
6742}
6743#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006744
6745#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6746/*
6747 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006748 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006749 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6750 * If no particular ID is desired, -1 must be specified for 'id'.
6751 * Return ID of added match, -1 on failure.
6752 */
6753 int
Bram Moolenaarb3414592014-06-17 17:48:32 +02006754match_add(wp, grp, pat, prio, id, pos_list)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006755 win_T *wp;
6756 char_u *grp;
6757 char_u *pat;
6758 int prio;
6759 int id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006760 list_T *pos_list;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006761{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006762 matchitem_T *cur;
6763 matchitem_T *prev;
6764 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006765 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006766 regprog_T *regprog = NULL;
6767 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006768
Bram Moolenaarb3414592014-06-17 17:48:32 +02006769 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006770 return -1;
6771 if (id < -1 || id == 0)
6772 {
6773 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6774 return -1;
6775 }
6776 if (id != -1)
6777 {
6778 cur = wp->w_match_head;
6779 while (cur != NULL)
6780 {
6781 if (cur->id == id)
6782 {
6783 EMSGN("E801: ID already taken: %ld", id);
6784 return -1;
6785 }
6786 cur = cur->next;
6787 }
6788 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006789 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006790 {
6791 EMSG2(_(e_nogroup), grp);
6792 return -1;
6793 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006794 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006795 {
6796 EMSG2(_(e_invarg2), pat);
6797 return -1;
6798 }
6799
6800 /* Find available match ID. */
6801 while (id == -1)
6802 {
6803 cur = wp->w_match_head;
6804 while (cur != NULL && cur->id != wp->w_next_match_id)
6805 cur = cur->next;
6806 if (cur == NULL)
6807 id = wp->w_next_match_id;
6808 wp->w_next_match_id++;
6809 }
6810
6811 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006812 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006813 m->id = id;
6814 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006815 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006816 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006817 m->match.regprog = regprog;
6818 m->match.rmm_ic = FALSE;
6819 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006820
Bram Moolenaarb3414592014-06-17 17:48:32 +02006821 /* Set up position matches */
6822 if (pos_list != NULL)
6823 {
6824 linenr_T toplnum = 0;
6825 linenr_T botlnum = 0;
6826 listitem_T *li;
6827 int i;
6828
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006829 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006830 i++, li = li->li_next)
6831 {
6832 linenr_T lnum = 0;
6833 colnr_T col = 0;
6834 int len = 1;
6835 list_T *subl;
6836 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006837 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006838
Bram Moolenaarb3414592014-06-17 17:48:32 +02006839 if (li->li_tv.v_type == VAR_LIST)
6840 {
6841 subl = li->li_tv.vval.v_list;
6842 if (subl == NULL)
6843 goto fail;
6844 subli = subl->lv_first;
6845 if (subli == NULL)
6846 goto fail;
6847 lnum = get_tv_number_chk(&subli->li_tv, &error);
6848 if (error == TRUE)
6849 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006850 if (lnum == 0)
6851 {
6852 --i;
6853 continue;
6854 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006855 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006856 subli = subli->li_next;
6857 if (subli != NULL)
6858 {
6859 col = get_tv_number_chk(&subli->li_tv, &error);
6860 if (error == TRUE)
6861 goto fail;
6862 subli = subli->li_next;
6863 if (subli != NULL)
6864 {
6865 len = get_tv_number_chk(&subli->li_tv, &error);
6866 if (error == TRUE)
6867 goto fail;
6868 }
6869 }
6870 m->pos.pos[i].col = col;
6871 m->pos.pos[i].len = len;
6872 }
6873 else if (li->li_tv.v_type == VAR_NUMBER)
6874 {
6875 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006876 {
6877 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006878 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006879 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006880 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6881 m->pos.pos[i].col = 0;
6882 m->pos.pos[i].len = 0;
6883 }
6884 else
6885 {
6886 EMSG(_("List or number required"));
6887 goto fail;
6888 }
6889 if (toplnum == 0 || lnum < toplnum)
6890 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006891 if (botlnum == 0 || lnum >= botlnum)
6892 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006893 }
6894
6895 /* Calculate top and bottom lines for redrawing area */
6896 if (toplnum != 0)
6897 {
6898 if (wp->w_buffer->b_mod_set)
6899 {
6900 if (wp->w_buffer->b_mod_top > toplnum)
6901 wp->w_buffer->b_mod_top = toplnum;
6902 if (wp->w_buffer->b_mod_bot < botlnum)
6903 wp->w_buffer->b_mod_bot = botlnum;
6904 }
6905 else
6906 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006907 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006908 wp->w_buffer->b_mod_top = toplnum;
6909 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006910 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006911 }
6912 m->pos.toplnum = toplnum;
6913 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006914 rtype = VALID;
6915 }
6916 }
6917
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006918 /* Insert new match. The match list is in ascending order with regard to
6919 * the match priorities. */
6920 cur = wp->w_match_head;
6921 prev = cur;
6922 while (cur != NULL && prio >= cur->priority)
6923 {
6924 prev = cur;
6925 cur = cur->next;
6926 }
6927 if (cur == prev)
6928 wp->w_match_head = m;
6929 else
6930 prev->next = m;
6931 m->next = cur;
6932
Bram Moolenaarb3414592014-06-17 17:48:32 +02006933 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006934 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006935
6936fail:
6937 vim_free(m);
6938 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006939}
6940
6941/*
6942 * Delete match with ID 'id' in the match list of window 'wp'.
6943 * Print error messages if 'perr' is TRUE.
6944 */
6945 int
6946match_delete(wp, id, perr)
6947 win_T *wp;
6948 int id;
6949 int perr;
6950{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006951 matchitem_T *cur = wp->w_match_head;
6952 matchitem_T *prev = cur;
6953 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006954
6955 if (id < 1)
6956 {
6957 if (perr == TRUE)
6958 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6959 id);
6960 return -1;
6961 }
6962 while (cur != NULL && cur->id != id)
6963 {
6964 prev = cur;
6965 cur = cur->next;
6966 }
6967 if (cur == NULL)
6968 {
6969 if (perr == TRUE)
6970 EMSGN("E803: ID not found: %ld", id);
6971 return -1;
6972 }
6973 if (cur == prev)
6974 wp->w_match_head = cur->next;
6975 else
6976 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006977 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006978 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006979 if (cur->pos.toplnum != 0)
6980 {
6981 if (wp->w_buffer->b_mod_set)
6982 {
6983 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6984 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6985 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6986 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6987 }
6988 else
6989 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006990 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006991 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6992 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006993 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006994 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006995 rtype = VALID;
6996 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006997 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006998 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006999 return 0;
7000}
7001
7002/*
7003 * Delete all matches in the match list of window 'wp'.
7004 */
7005 void
7006clear_matches(wp)
7007 win_T *wp;
7008{
7009 matchitem_T *m;
7010
7011 while (wp->w_match_head != NULL)
7012 {
7013 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007014 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007015 vim_free(wp->w_match_head->pattern);
7016 vim_free(wp->w_match_head);
7017 wp->w_match_head = m;
7018 }
7019 redraw_later(SOME_VALID);
7020}
7021
7022/*
7023 * Get match from ID 'id' in window 'wp'.
7024 * Return NULL if match not found.
7025 */
7026 matchitem_T *
7027get_match(wp, id)
7028 win_T *wp;
7029 int id;
7030{
7031 matchitem_T *cur = wp->w_match_head;
7032
7033 while (cur != NULL && cur->id != id)
7034 cur = cur->next;
7035 return cur;
7036}
7037#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007038
7039#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7040 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007041get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007042{
7043 int i = 1;
7044 win_T *w;
7045
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007046 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007047 ++i;
7048
7049 if (w == NULL)
7050 return 0;
7051 else
7052 return i;
7053}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007054
7055 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007056get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007057{
7058 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007059# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007060 tabpage_T *t;
7061
7062 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7063 ++i;
7064
7065 if (t == NULL)
7066 return 0;
7067 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007068# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007069 return i;
7070}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007071#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007072
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007073#ifdef FEAT_WINDOWS
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007074/*
7075 * Return TRUE if "topfrp" and its children are at the right height.
7076 */
7077 static int
7078frame_check_height(topfrp, height)
7079 frame_T *topfrp;
7080 int height;
7081{
7082 frame_T *frp;
7083
7084 if (topfrp->fr_height != height)
7085 return FALSE;
7086
7087 if (topfrp->fr_layout == FR_ROW)
7088 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7089 if (frp->fr_height != height)
7090 return FALSE;
7091
7092 return TRUE;
7093}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007094#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007095
7096#ifdef FEAT_VERTSPLIT
7097/*
7098 * Return TRUE if "topfrp" and its children are at the right width.
7099 */
7100 static int
7101frame_check_width(topfrp, width)
7102 frame_T *topfrp;
7103 int width;
7104{
7105 frame_T *frp;
7106
7107 if (topfrp->fr_width != width)
7108 return FALSE;
7109
7110 if (topfrp->fr_layout == FR_COL)
7111 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7112 if (frp->fr_width != width)
7113 return FALSE;
7114
7115 return TRUE;
7116}
7117#endif
7118