blob: bbd8d1e518248972e4de3ffbfb3d99ab822cccab [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));
56#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));
59#endif
60static 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
69#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000070
Bram Moolenaar746ebd32009-06-16 14:01:43 +000071static win_T *win_alloc __ARGS((win_T *after, int hidden));
Bram Moolenaar0215e8e2010-12-17 17:35:10 +010072static void set_fraction __ARGS((win_T *wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
74#define URL_SLASH 1 /* path_is_url() has found "://" */
75#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
76
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000077#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar05159a02005-02-26 23:04:13 +000079#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000080# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000081#else
82# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000083#endif
84
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000086
87static char *m_onlyone = N_("Already only one window");
88
Bram Moolenaar071d4272004-06-13 20:20:40 +000089/*
90 * all CTRL-W window commands are handled here, called from normal_cmd().
91 */
92 void
93do_window(nchar, Prenum, xchar)
94 int nchar;
95 long Prenum;
96 int xchar; /* extra char from ":wincmd gx" or NUL */
97{
98 long Prenum1;
99 win_T *wp;
100#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
101 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000102 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
104#ifdef FEAT_FIND_ID
105 int type = FIND_DEFINE;
106 int len;
107#endif
108 char_u cbuf[40];
109
110 if (Prenum == 0)
111 Prenum1 = 1;
112 else
113 Prenum1 = Prenum;
114
115#ifdef FEAT_CMDWIN
116# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
117#else
118# define CHECK_CMDWIN
119#endif
120
121 switch (nchar)
122 {
123/* split current window in two parts, horizontally */
124 case 'S':
125 case Ctrl_S:
126 case 's':
127 CHECK_CMDWIN
128#ifdef FEAT_VISUAL
129 reset_VIsual_and_resel(); /* stop Visual mode */
130#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000131#ifdef FEAT_QUICKFIX
132 /* When splitting the quickfix window open a new buffer in it,
133 * don't replicate the quickfix buffer. */
134 if (bt_quickfix(curbuf))
135 goto newwindow;
136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#ifdef FEAT_GUI
138 need_mouse_correct = TRUE;
139#endif
140 win_split((int)Prenum, 0);
141 break;
142
143#ifdef FEAT_VERTSPLIT
144/* split current window in two parts, vertically */
145 case Ctrl_V:
146 case 'v':
147 CHECK_CMDWIN
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000148# ifdef FEAT_VISUAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000150# endif
151# ifdef FEAT_QUICKFIX
152 /* When splitting the quickfix window open a new buffer in it,
153 * don't replicate the quickfix buffer. */
154 if (bt_quickfix(curbuf))
155 goto newwindow;
156# endif
157# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 win_split((int)Prenum, WSP_VERT);
161 break;
162#endif
163
164/* split current window and edit alternate file */
165 case Ctrl_HAT:
166 case '^':
167 CHECK_CMDWIN
168#ifdef FEAT_VISUAL
169 reset_VIsual_and_resel(); /* stop Visual mode */
170#endif
171 STRCPY(cbuf, "split #");
172 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000173 vim_snprintf((char *)cbuf + 7, sizeof(cbuf) - 7,
174 "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 do_cmdline_cmd(cbuf);
176 break;
177
178/* open new window */
179 case Ctrl_N:
180 case 'n':
181 CHECK_CMDWIN
182#ifdef FEAT_VISUAL
183 reset_VIsual_and_resel(); /* stop Visual mode */
184#endif
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000185#ifdef FEAT_QUICKFIX
186newwindow:
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000189 /* window height */
190 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 else
192 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000193#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
194 if (nchar == 'v' || nchar == Ctrl_V)
195 STRCAT(cbuf, "v");
196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 STRCAT(cbuf, "new");
198 do_cmdline_cmd(cbuf);
199 break;
200
201/* quit current window */
202 case Ctrl_Q:
203 case 'q':
204#ifdef FEAT_VISUAL
205 reset_VIsual_and_resel(); /* stop Visual mode */
206#endif
207 do_cmdline_cmd((char_u *)"quit");
208 break;
209
210/* close current window */
211 case Ctrl_C:
212 case 'c':
213#ifdef FEAT_VISUAL
214 reset_VIsual_and_resel(); /* stop Visual mode */
215#endif
216 do_cmdline_cmd((char_u *)"close");
217 break;
218
219#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
220/* close preview window */
221 case Ctrl_Z:
222 case 'z':
223 CHECK_CMDWIN
224#ifdef FEAT_VISUAL
225 reset_VIsual_and_resel(); /* stop Visual mode */
226#endif
227 do_cmdline_cmd((char_u *)"pclose");
228 break;
229
230/* cursor to preview window */
231 case 'P':
232 for (wp = firstwin; wp != NULL; wp = wp->w_next)
233 if (wp->w_p_pvw)
234 break;
235 if (wp == NULL)
236 EMSG(_("E441: There is no preview window"));
237 else
238 win_goto(wp);
239 break;
240#endif
241
242/* close all but current window */
243 case Ctrl_O:
244 case 'o':
245 CHECK_CMDWIN
246#ifdef FEAT_VISUAL
247 reset_VIsual_and_resel(); /* stop Visual mode */
248#endif
249 do_cmdline_cmd((char_u *)"only");
250 break;
251
252/* cursor to next window with wrap around */
253 case Ctrl_W:
254 case 'w':
255/* cursor to previous window with wrap around */
256 case 'W':
257 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000258 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 beep_flush();
260 else
261 {
262 if (Prenum) /* go to specified window */
263 {
264 for (wp = firstwin; --Prenum > 0; )
265 {
266 if (wp->w_next == NULL)
267 break;
268 else
269 wp = wp->w_next;
270 }
271 }
272 else
273 {
274 if (nchar == 'W') /* go to previous window */
275 {
276 wp = curwin->w_prev;
277 if (wp == NULL)
278 wp = lastwin; /* wrap around */
279 }
280 else /* go to next window */
281 {
282 wp = curwin->w_next;
283 if (wp == NULL)
284 wp = firstwin; /* wrap around */
285 }
286 }
287 win_goto(wp);
288 }
289 break;
290
291/* cursor to window below */
292 case 'j':
293 case K_DOWN:
294 case Ctrl_J:
295 CHECK_CMDWIN
296#ifdef FEAT_VERTSPLIT
297 win_goto_ver(FALSE, Prenum1);
298#else
299 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
300 wp = wp->w_next)
301 ;
302 win_goto(wp);
303#endif
304 break;
305
306/* cursor to window above */
307 case 'k':
308 case K_UP:
309 case Ctrl_K:
310 CHECK_CMDWIN
311#ifdef FEAT_VERTSPLIT
312 win_goto_ver(TRUE, Prenum1);
313#else
314 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
315 wp = wp->w_prev)
316 ;
317 win_goto(wp);
318#endif
319 break;
320
321#ifdef FEAT_VERTSPLIT
322/* cursor to left window */
323 case 'h':
324 case K_LEFT:
325 case Ctrl_H:
326 case K_BS:
327 CHECK_CMDWIN
328 win_goto_hor(TRUE, Prenum1);
329 break;
330
331/* cursor to right window */
332 case 'l':
333 case K_RIGHT:
334 case Ctrl_L:
335 CHECK_CMDWIN
336 win_goto_hor(FALSE, Prenum1);
337 break;
338#endif
339
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000340/* move window to new tab page */
341 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000342 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000343 MSG(_(m_onlyone));
344 else
345 {
346 tabpage_T *oldtab = curtab;
347 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000348
349 /* First create a new tab with the window, then go back to
350 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000351 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000352 if (win_new_tabpage((int)Prenum) == OK
353 && valid_tabpage(oldtab))
354 {
355 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200356 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000357 if (curwin == wp)
358 win_close(curwin, FALSE);
359 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200360 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000361 }
362 }
363 break;
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365/* cursor to top-left window */
366 case 't':
367 case Ctrl_T:
368 win_goto(firstwin);
369 break;
370
371/* cursor to bottom-right window */
372 case 'b':
373 case Ctrl_B:
374 win_goto(lastwin);
375 break;
376
377/* cursor to last accessed (previous) window */
378 case 'p':
379 case Ctrl_P:
380 if (prevwin == NULL)
381 beep_flush();
382 else
383 win_goto(prevwin);
384 break;
385
386/* exchange current and next window */
387 case 'x':
388 case Ctrl_X:
389 CHECK_CMDWIN
390 win_exchange(Prenum);
391 break;
392
393/* rotate windows downwards */
394 case Ctrl_R:
395 case 'r':
396 CHECK_CMDWIN
397#ifdef FEAT_VISUAL
398 reset_VIsual_and_resel(); /* stop Visual mode */
399#endif
400 win_rotate(FALSE, (int)Prenum1); /* downwards */
401 break;
402
403/* rotate windows upwards */
404 case 'R':
405 CHECK_CMDWIN
406#ifdef FEAT_VISUAL
407 reset_VIsual_and_resel(); /* stop Visual mode */
408#endif
409 win_rotate(TRUE, (int)Prenum1); /* upwards */
410 break;
411
412/* move window to the very top/bottom/left/right */
413 case 'K':
414 case 'J':
415#ifdef FEAT_VERTSPLIT
416 case 'H':
417 case 'L':
418#endif
419 CHECK_CMDWIN
420 win_totop((int)Prenum,
421 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
422 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
423 break;
424
425/* make all windows the same height */
426 case '=':
427#ifdef FEAT_GUI
428 need_mouse_correct = TRUE;
429#endif
430 win_equal(NULL, FALSE, 'b');
431 break;
432
433/* increase current window height */
434 case '+':
435#ifdef FEAT_GUI
436 need_mouse_correct = TRUE;
437#endif
438 win_setheight(curwin->w_height + (int)Prenum1);
439 break;
440
441/* decrease current window height */
442 case '-':
443#ifdef FEAT_GUI
444 need_mouse_correct = TRUE;
445#endif
446 win_setheight(curwin->w_height - (int)Prenum1);
447 break;
448
449/* set current window height */
450 case Ctrl__:
451 case '_':
452#ifdef FEAT_GUI
453 need_mouse_correct = TRUE;
454#endif
455 win_setheight(Prenum ? (int)Prenum : 9999);
456 break;
457
458#ifdef FEAT_VERTSPLIT
459/* increase current window width */
460 case '>':
461#ifdef FEAT_GUI
462 need_mouse_correct = TRUE;
463#endif
464 win_setwidth(curwin->w_width + (int)Prenum1);
465 break;
466
467/* decrease current window width */
468 case '<':
469#ifdef FEAT_GUI
470 need_mouse_correct = TRUE;
471#endif
472 win_setwidth(curwin->w_width - (int)Prenum1);
473 break;
474
475/* set current window width */
476 case '|':
477#ifdef FEAT_GUI
478 need_mouse_correct = TRUE;
479#endif
480 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
481 break;
482#endif
483
484/* jump to tag and split window if tag exists (in preview window) */
485#if defined(FEAT_QUICKFIX)
486 case '}':
487 CHECK_CMDWIN
488 if (Prenum)
489 g_do_tagpreview = Prenum;
490 else
491 g_do_tagpreview = p_pvh;
492 /*FALLTHROUGH*/
493#endif
494 case ']':
495 case Ctrl_RSB:
496 CHECK_CMDWIN
497#ifdef FEAT_VISUAL
498 reset_VIsual_and_resel(); /* stop Visual mode */
499#endif
500 if (Prenum)
501 postponed_split = Prenum;
502 else
503 postponed_split = -1;
504
505 /* Execute the command right here, required when
506 * "wincmd ]" was used in a function. */
507 do_nv_ident(Ctrl_RSB, NUL);
508 break;
509
510#ifdef FEAT_SEARCHPATH
511/* edit file name under cursor in a new window */
512 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000515wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000517
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000518 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 if (ptr != NULL)
520 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000521# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 setpcmark();
525 if (win_split(0, 0) == OK)
526 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200527 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000528 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
529 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000530 if (nchar == 'F' && lnum >= 0)
531 {
532 curwin->w_cursor.lnum = lnum;
533 check_cursor_lnum();
534 beginline(BL_SOL | BL_FIX);
535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 }
537 vim_free(ptr);
538 }
539 break;
540#endif
541
542#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000543/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 * new window -- webb
545 */
546 case 'i': /* Go to any match */
547 case Ctrl_I:
548 type = FIND_ANY;
549 /* FALLTHROUGH */
550 case 'd': /* Go to definition, using 'define' */
551 case Ctrl_D:
552 CHECK_CMDWIN
553 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
554 break;
555 find_pattern_in_path(ptr, 0, len, TRUE,
556 Prenum == 0 ? TRUE : FALSE, type,
557 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
558 curwin->w_set_curswant = TRUE;
559 break;
560#endif
561
Bram Moolenaar05159a02005-02-26 23:04:13 +0000562 case K_KENTER:
563 case CAR:
564#if defined(FEAT_QUICKFIX)
565 /*
566 * In a quickfix window a <CR> jumps to the error under the
567 * cursor in a new window.
568 */
569 if (bt_quickfix(curbuf))
570 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000571 sprintf((char *)cbuf, "split +%ld%s",
572 (long)curwin->w_cursor.lnum,
573 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000574 do_cmdline_cmd(cbuf);
575 }
576#endif
577 break;
578
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580/* CTRL-W g extended commands */
581 case 'g':
582 case Ctrl_G:
583 CHECK_CMDWIN
584#ifdef USE_ON_FLY_SCROLL
585 dont_scroll = TRUE; /* disallow scrolling here */
586#endif
587 ++no_mapping;
588 ++allow_keys; /* no mapping for xchar, but allow key codes */
589 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000590 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 --no_mapping;
593 --allow_keys;
594#ifdef FEAT_CMDL_INFO
595 (void)add_to_showcmd(xchar);
596#endif
597 switch (xchar)
598 {
599#if defined(FEAT_QUICKFIX)
600 case '}':
601 xchar = Ctrl_RSB;
602 if (Prenum)
603 g_do_tagpreview = Prenum;
604 else
605 g_do_tagpreview = p_pvh;
606 /*FALLTHROUGH*/
607#endif
608 case ']':
609 case Ctrl_RSB:
610#ifdef FEAT_VISUAL
611 reset_VIsual_and_resel(); /* stop Visual mode */
612#endif
613 if (Prenum)
614 postponed_split = Prenum;
615 else
616 postponed_split = -1;
617
618 /* Execute the command right here, required when
619 * "wincmd g}" was used in a function. */
620 do_nv_ident('g', xchar);
621 break;
622
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000623#ifdef FEAT_SEARCHPATH
624 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000625 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100626 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000627 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000628 goto wingotofile;
629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 default:
631 beep_flush();
632 break;
633 }
634 break;
635
636 default: beep_flush();
637 break;
638 }
639}
640
641/*
642 * split the current window, implements CTRL-W s and :split
643 *
644 * "size" is the height or width for the new window, 0 to use half of current
645 * height or width.
646 *
647 * "flags":
648 * WSP_ROOM: require enough room for new window
649 * WSP_VERT: vertical split.
650 * WSP_TOP: open window at the top-left of the shell (help window).
651 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
652 * WSP_HELP: creating the help window, keep layout snapshot
653 *
654 * return FAIL for failure, OK otherwise
655 */
656 int
657win_split(size, flags)
658 int size;
659 int flags;
660{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000661 /* When the ":tab" modifier was used open a new tab page instead. */
662 if (may_open_tabpage() == OK)
663 return OK;
664
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 /* Add flags from ":vertical", ":topleft" and ":botright". */
666 flags |= cmdmod.split;
667 if ((flags & WSP_TOP) && (flags & WSP_BOT))
668 {
669 EMSG(_("E442: Can't split topleft and botright at the same time"));
670 return FAIL;
671 }
672
673 /* When creating the help window make a snapshot of the window layout.
674 * Otherwise clear the snapshot, it's now invalid. */
675 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000676 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000678 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679
680 return win_split_ins(size, flags, NULL, 0);
681}
682
683/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100684 * When "new_wp" is NULL: split the current window in two.
685 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 * top/left/right/bottom.
687 * return FAIL for failure, OK otherwise
688 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000689 int
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100690win_split_ins(size, flags, new_wp, dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 int size;
692 int flags;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100693 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 int dir;
695{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100696 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 win_T *oldwin;
698 int new_size = size;
699 int i;
700 int need_status = 0;
701 int do_equal = FALSE;
702 int needed;
703 int available;
704 int oldwin_height = 0;
705 int layout;
706 frame_T *frp, *curfrp;
707 int before;
708
709 if (flags & WSP_TOP)
710 oldwin = firstwin;
711 else if (flags & WSP_BOT)
712 oldwin = lastwin;
713 else
714 oldwin = curwin;
715
716 /* add a status line when p_ls == 1 and splitting the first window */
717 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
718 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100719 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
721 EMSG(_(e_noroom));
722 return FAIL;
723 }
724 need_status = STATUS_HEIGHT;
725 }
726
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000727#ifdef FEAT_GUI
728 /* May be needed for the scrollbars that are going to change. */
729 if (gui.in_use)
730 out_flush();
731#endif
732
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733#ifdef FEAT_VERTSPLIT
734 if (flags & WSP_VERT)
735 {
736 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 /*
739 * Check if we are able to split the current window and compute its
740 * width.
741 */
742 needed = p_wmw + 1;
743 if (flags & WSP_ROOM)
744 needed += p_wiw - p_wmw;
745 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
746 {
747 available = topframe->fr_width;
748 needed += frame_minwidth(topframe, NULL);
749 }
750 else
751 available = oldwin->w_width;
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100752 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 EMSG(_(e_noroom));
755 return FAIL;
756 }
757 if (new_size == 0)
758 new_size = oldwin->w_width / 2;
759 if (new_size > oldwin->w_width - p_wmw - 1)
760 new_size = oldwin->w_width - p_wmw - 1;
761 if (new_size < p_wmw)
762 new_size = p_wmw;
763
764 /* if it doesn't fit in the current window, need win_equal() */
765 if (oldwin->w_width - new_size - 1 < p_wmw)
766 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000767
768 /* We don't like to take lines for the new window from a
769 * 'winfixwidth' window. Take them from a window to the left or right
770 * instead, if possible. */
771 if (oldwin->w_p_wfw)
772 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000773
774 /* Only make all windows the same width if one of them (except oldwin)
775 * is wider than one of the split windows. */
776 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
777 && oldwin->w_frame->fr_parent != NULL)
778 {
779 frp = oldwin->w_frame->fr_parent->fr_child;
780 while (frp != NULL)
781 {
782 if (frp->fr_win != oldwin && frp->fr_win != NULL
783 && (frp->fr_win->w_width > new_size
784 || frp->fr_win->w_width > oldwin->w_width
785 - new_size - STATUS_HEIGHT))
786 {
787 do_equal = TRUE;
788 break;
789 }
790 frp = frp->fr_next;
791 }
792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794 else
795#endif
796 {
797 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798
799 /*
800 * Check if we are able to split the current window and compute its
801 * height.
802 */
803 needed = p_wmh + STATUS_HEIGHT + need_status;
804 if (flags & WSP_ROOM)
805 needed += p_wh - p_wmh;
806 if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
807 {
808 available = topframe->fr_height;
809 needed += frame_minheight(topframe, NULL);
810 }
811 else
812 {
813 available = oldwin->w_height;
814 needed += p_wmh;
815 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100816 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
818 EMSG(_(e_noroom));
819 return FAIL;
820 }
821 oldwin_height = oldwin->w_height;
822 if (need_status)
823 {
824 oldwin->w_status_height = STATUS_HEIGHT;
825 oldwin_height -= STATUS_HEIGHT;
826 }
827 if (new_size == 0)
828 new_size = oldwin_height / 2;
829
830 if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
831 new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
832 if (new_size < p_wmh)
833 new_size = p_wmh;
834
835 /* if it doesn't fit in the current window, need win_equal() */
836 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
837 do_equal = TRUE;
838
839 /* We don't like to take lines for the new window from a
840 * 'winfixheight' window. Take them from a window above or below
841 * instead, if possible. */
842 if (oldwin->w_p_wfh)
843 {
844 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
845 oldwin);
846 oldwin_height = oldwin->w_height;
847 if (need_status)
848 oldwin_height -= STATUS_HEIGHT;
849 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000850
851 /* Only make all windows the same height if one of them (except oldwin)
852 * is higher than one of the split windows. */
853 if (!do_equal && p_ea && size == 0
854#ifdef FEAT_VERTSPLIT
855 && *p_ead != 'h'
856#endif
857 && oldwin->w_frame->fr_parent != NULL)
858 {
859 frp = oldwin->w_frame->fr_parent->fr_child;
860 while (frp != NULL)
861 {
862 if (frp->fr_win != oldwin && frp->fr_win != NULL
863 && (frp->fr_win->w_height > new_size
864 || frp->fr_win->w_height > oldwin_height - new_size
865 - STATUS_HEIGHT))
866 {
867 do_equal = TRUE;
868 break;
869 }
870 frp = frp->fr_next;
871 }
872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874
875 /*
876 * allocate new window structure and link it in the window list
877 */
878 if ((flags & WSP_TOP) == 0
879 && ((flags & WSP_BOT)
880 || (flags & WSP_BELOW)
881 || (!(flags & WSP_ABOVE)
882 && (
883#ifdef FEAT_VERTSPLIT
884 (flags & WSP_VERT) ? p_spr :
885#endif
886 p_sb))))
887 {
888 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100889 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000890 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 else
892 win_append(oldwin, wp);
893 }
894 else
895 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100896 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000897 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 else
899 win_append(oldwin->w_prev, wp);
900 }
901
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100902 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
904 if (wp == NULL)
905 return FAIL;
906
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000907 new_frame(wp);
908 if (wp->w_frame == NULL)
909 {
910 win_free(wp, NULL);
911 return FAIL;
912 }
913
Bram Moolenaar2a0449d2006-02-20 21:27:21 +0000914 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +0000915 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917
918 /*
919 * Reorganise the tree of frames to insert the new window.
920 */
921 if (flags & (WSP_TOP | WSP_BOT))
922 {
923#ifdef FEAT_VERTSPLIT
924 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
925 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
926#else
927 if (topframe->fr_layout == FR_COL)
928#endif
929 {
930 curfrp = topframe->fr_child;
931 if (flags & WSP_BOT)
932 while (curfrp->fr_next != NULL)
933 curfrp = curfrp->fr_next;
934 }
935 else
936 curfrp = topframe;
937 before = (flags & WSP_TOP);
938 }
939 else
940 {
941 curfrp = oldwin->w_frame;
942 if (flags & WSP_BELOW)
943 before = FALSE;
944 else if (flags & WSP_ABOVE)
945 before = TRUE;
946 else
947#ifdef FEAT_VERTSPLIT
948 if (flags & WSP_VERT)
949 before = !p_spr;
950 else
951#endif
952 before = !p_sb;
953 }
954 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
955 {
956 /* Need to create a new frame in the tree to make a branch. */
957 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
958 *frp = *curfrp;
959 curfrp->fr_layout = layout;
960 frp->fr_parent = curfrp;
961 frp->fr_next = NULL;
962 frp->fr_prev = NULL;
963 curfrp->fr_child = frp;
964 curfrp->fr_win = NULL;
965 curfrp = frp;
966 if (frp->fr_win != NULL)
967 oldwin->w_frame = frp;
968 else
969 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
970 frp->fr_parent = curfrp;
971 }
972
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100973 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000974 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100976 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 frp->fr_parent = curfrp->fr_parent;
978
979 /* Insert the new frame at the right place in the frame list. */
980 if (before)
981 frame_insert(curfrp, frp);
982 else
983 frame_append(curfrp, frp);
984
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100985 /* Set w_fraction now so that the cursor keeps the same relative
986 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +0100987 if (oldwin->w_height > 0)
988 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100989 wp->w_fraction = oldwin->w_fraction;
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#ifdef FEAT_VERTSPLIT
992 if (flags & WSP_VERT)
993 {
994 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +0100995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (need_status)
997 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +0100998 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 oldwin->w_status_height = need_status;
1000 }
1001 if (flags & (WSP_TOP | WSP_BOT))
1002 {
1003 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001004 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001005 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 wp->w_status_height = (p_ls > 0);
1007 }
1008 else
1009 {
1010 /* height and row of new window is same as current window */
1011 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001012 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 wp->w_status_height = oldwin->w_status_height;
1014 }
1015 frp->fr_height = curfrp->fr_height;
1016
1017 /* "new_size" of the current window goes to the new window, use
1018 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001019 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (before)
1021 wp->w_vsep_width = 1;
1022 else
1023 {
1024 wp->w_vsep_width = oldwin->w_vsep_width;
1025 oldwin->w_vsep_width = 1;
1026 }
1027 if (flags & (WSP_TOP | WSP_BOT))
1028 {
1029 if (flags & WSP_BOT)
1030 frame_add_vsep(curfrp);
1031 /* Set width of neighbor frame */
1032 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001033 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1034 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 }
1036 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001037 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (before) /* new window left of current one */
1039 {
1040 wp->w_wincol = oldwin->w_wincol;
1041 oldwin->w_wincol += new_size + 1;
1042 }
1043 else /* new window right of current one */
1044 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1045 frame_fix_width(oldwin);
1046 frame_fix_width(wp);
1047 }
1048 else
1049#endif
1050 {
1051 /* width and column of new window is same as current window */
1052#ifdef FEAT_VERTSPLIT
1053 if (flags & (WSP_TOP | WSP_BOT))
1054 {
1055 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001056 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 wp->w_vsep_width = 0;
1058 }
1059 else
1060 {
1061 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001062 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 wp->w_vsep_width = oldwin->w_vsep_width;
1064 }
1065 frp->fr_width = curfrp->fr_width;
1066#endif
1067
1068 /* "new_size" of the current window goes to the new window, use
1069 * one row for the status line */
1070 win_new_height(wp, new_size);
1071 if (flags & (WSP_TOP | WSP_BOT))
1072 frame_new_height(curfrp, curfrp->fr_height
1073 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1074 else
1075 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1076 if (before) /* new window above current one */
1077 {
1078 wp->w_winrow = oldwin->w_winrow;
1079 wp->w_status_height = STATUS_HEIGHT;
1080 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1081 }
1082 else /* new window below current one */
1083 {
1084 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1085 wp->w_status_height = oldwin->w_status_height;
1086 oldwin->w_status_height = STATUS_HEIGHT;
1087 }
1088#ifdef FEAT_VERTSPLIT
1089 if (flags & WSP_BOT)
1090 frame_add_statusline(curfrp);
1091#endif
1092 frame_fix_height(wp);
1093 frame_fix_height(oldwin);
1094 }
1095
1096 if (flags & (WSP_TOP | WSP_BOT))
1097 (void)win_comp_pos();
1098
1099 /*
1100 * Both windows need redrawing
1101 */
1102 redraw_win_later(wp, NOT_VALID);
1103 wp->w_redr_status = TRUE;
1104 redraw_win_later(oldwin, NOT_VALID);
1105 oldwin->w_redr_status = TRUE;
1106
1107 if (need_status)
1108 {
1109 msg_row = Rows - 1;
1110 msg_col = sc_col;
1111 msg_clr_eos_force(); /* Old command/ruler may still be there */
1112 comp_col();
1113 msg_row = Rows - 1;
1114 msg_col = 0; /* put position back at start of line */
1115 }
1116
1117 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001118 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 */
1120 if (do_equal || dir != 0)
1121 win_equal(wp, TRUE,
1122#ifdef FEAT_VERTSPLIT
1123 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1124 : dir == 'h' ? 'b' :
1125#endif
1126 'v');
1127
1128 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1129 * size was given. */
1130#ifdef FEAT_VERTSPLIT
1131 if (flags & WSP_VERT)
1132 {
1133 i = p_wiw;
1134 if (size != 0)
1135 p_wiw = size;
1136
1137# ifdef FEAT_GUI
1138 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1139 if (gui.in_use)
1140 gui_init_which_components(NULL);
1141# endif
1142 }
1143 else
1144#endif
1145 {
1146 i = p_wh;
1147 if (size != 0)
1148 p_wh = size;
1149 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001150
1151 /*
1152 * make the new window the current window
1153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 win_enter(wp, FALSE);
1155#ifdef FEAT_VERTSPLIT
1156 if (flags & WSP_VERT)
1157 p_wiw = i;
1158 else
1159#endif
1160 p_wh = i;
1161
1162 return OK;
1163}
1164
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001165
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001166/*
1167 * Initialize window "newp" from window "oldp".
1168 * Used when splitting a window and when creating a new tab page.
1169 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001170 * WSP_NEWLOC may be specified in flags to prevent the location list from
1171 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001172 */
1173 static void
Bram Moolenaar884ae642009-02-22 01:37:59 +00001174win_init(newp, oldp, flags)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001175 win_T *newp;
1176 win_T *oldp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001177 int flags UNUSED;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001178{
1179 int i;
1180
1181 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001182#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001183 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001184#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001185 oldp->w_buffer->b_nwindows++;
1186 newp->w_cursor = oldp->w_cursor;
1187 newp->w_valid = 0;
1188 newp->w_curswant = oldp->w_curswant;
1189 newp->w_set_curswant = oldp->w_set_curswant;
1190 newp->w_topline = oldp->w_topline;
1191#ifdef FEAT_DIFF
1192 newp->w_topfill = oldp->w_topfill;
1193#endif
1194 newp->w_leftcol = oldp->w_leftcol;
1195 newp->w_pcmark = oldp->w_pcmark;
1196 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1197 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001198 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001199 newp->w_fraction = oldp->w_fraction;
1200 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1201#ifdef FEAT_JUMPLIST
1202 copy_jumplist(oldp, newp);
1203#endif
1204#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001205 if (flags & WSP_NEWLOC)
1206 {
1207 /* Don't copy the location list. */
1208 newp->w_llist = NULL;
1209 newp->w_llist_ref = NULL;
1210 }
1211 else
1212 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001213#endif
1214 if (oldp->w_localdir != NULL)
1215 newp->w_localdir = vim_strsave(oldp->w_localdir);
1216
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001217 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001218 for (i = 0; i < oldp->w_tagstacklen; i++)
1219 {
1220 newp->w_tagstack[i] = oldp->w_tagstack[i];
1221 if (newp->w_tagstack[i].tagname != NULL)
1222 newp->w_tagstack[i].tagname =
1223 vim_strsave(newp->w_tagstack[i].tagname);
1224 }
1225 newp->w_tagstackidx = oldp->w_tagstackidx;
1226 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001227#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001228 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001229#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001230
1231 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001232
Bram Moolenaara971b822011-09-14 14:43:25 +02001233#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001234 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001235#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001236}
1237
1238/*
1239 * Initialize window "newp" from window"old".
1240 * Only the essential things are copied.
1241 */
1242 static void
1243win_init_some(newp, oldp)
1244 win_T *newp;
1245 win_T *oldp;
1246{
1247 /* Use the same argument list. */
1248 newp->w_alist = oldp->w_alist;
1249 ++newp->w_alist->al_refcount;
1250 newp->w_arg_idx = oldp->w_arg_idx;
1251
1252 /* copy options from existing window */
1253 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001254}
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif /* FEAT_WINDOWS */
1257
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#if defined(FEAT_WINDOWS) || defined(PROTO)
1259/*
1260 * Check if "win" is a pointer to an existing window.
1261 */
1262 int
1263win_valid(win)
1264 win_T *win;
1265{
1266 win_T *wp;
1267
1268 if (win == NULL)
1269 return FALSE;
1270 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1271 if (wp == win)
1272 return TRUE;
1273 return FALSE;
1274}
1275
1276/*
1277 * Return the number of windows.
1278 */
1279 int
1280win_count()
1281{
1282 win_T *wp;
1283 int count = 0;
1284
1285 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1286 ++count;
1287 return count;
1288}
1289
1290/*
1291 * Make "count" windows on the screen.
1292 * Return actual number of windows on the screen.
1293 * Must be called when there is just one window, filling the whole screen
1294 * (excluding the command line).
1295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 int
1297make_windows(count, vertical)
1298 int count;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00001299 int vertical UNUSED; /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int maxcount;
1302 int todo;
1303
1304#ifdef FEAT_VERTSPLIT
1305 if (vertical)
1306 {
1307 /* Each windows needs at least 'winminwidth' lines and a separator
1308 * column. */
1309 maxcount = (curwin->w_width + curwin->w_vsep_width
1310 - (p_wiw - p_wmw)) / (p_wmw + 1);
1311 }
1312 else
1313#endif
1314 {
1315 /* Each window needs at least 'winminheight' lines and a status line. */
1316 maxcount = (curwin->w_height + curwin->w_status_height
1317 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1318 }
1319
1320 if (maxcount < 2)
1321 maxcount = 2;
1322 if (count > maxcount)
1323 count = maxcount;
1324
1325 /*
1326 * add status line now, otherwise first window will be too big
1327 */
1328 if (count > 1)
1329 last_status(TRUE);
1330
1331#ifdef FEAT_AUTOCMD
1332 /*
1333 * Don't execute autocommands while creating the windows. Must do that
1334 * when putting the buffers in the windows.
1335 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001336 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#endif
1338
1339 /* todo is number of windows left to create */
1340 for (todo = count - 1; todo > 0; --todo)
1341#ifdef FEAT_VERTSPLIT
1342 if (vertical)
1343 {
1344 if (win_split(curwin->w_width - (curwin->w_width - todo)
1345 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1346 break;
1347 }
1348 else
1349#endif
1350 {
1351 if (win_split(curwin->w_height - (curwin->w_height - todo
1352 * STATUS_HEIGHT) / (todo + 1)
1353 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1354 break;
1355 }
1356
1357#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001358 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#endif
1360
1361 /* return actual number of windows */
1362 return (count - todo);
1363}
1364
1365/*
1366 * Exchange current and next window
1367 */
1368 static void
1369win_exchange(Prenum)
1370 long Prenum;
1371{
1372 frame_T *frp;
1373 frame_T *frp2;
1374 win_T *wp;
1375 win_T *wp2;
1376 int temp;
1377
1378 if (lastwin == firstwin) /* just one window */
1379 {
1380 beep_flush();
1381 return;
1382 }
1383
1384#ifdef FEAT_GUI
1385 need_mouse_correct = TRUE;
1386#endif
1387
1388 /*
1389 * find window to exchange with
1390 */
1391 if (Prenum)
1392 {
1393 frp = curwin->w_frame->fr_parent->fr_child;
1394 while (frp != NULL && --Prenum > 0)
1395 frp = frp->fr_next;
1396 }
1397 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1398 frp = curwin->w_frame->fr_next;
1399 else /* Swap last window in row/col with previous */
1400 frp = curwin->w_frame->fr_prev;
1401
1402 /* We can only exchange a window with another window, not with a frame
1403 * containing windows. */
1404 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1405 return;
1406 wp = frp->fr_win;
1407
1408/*
1409 * 1. remove curwin from the list. Remember after which window it was in wp2
1410 * 2. insert curwin before wp in the list
1411 * if wp != wp2
1412 * 3. remove wp from the list
1413 * 4. insert wp after wp2
1414 * 5. exchange the status line height and vsep width.
1415 */
1416 wp2 = curwin->w_prev;
1417 frp2 = curwin->w_frame->fr_prev;
1418 if (wp->w_prev != curwin)
1419 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 frame_remove(curwin->w_frame);
1422 win_append(wp->w_prev, curwin);
1423 frame_insert(frp, curwin->w_frame);
1424 }
1425 if (wp != wp2)
1426 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001427 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 frame_remove(wp->w_frame);
1429 win_append(wp2, wp);
1430 if (frp2 == NULL)
1431 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1432 else
1433 frame_append(frp2, wp->w_frame);
1434 }
1435 temp = curwin->w_status_height;
1436 curwin->w_status_height = wp->w_status_height;
1437 wp->w_status_height = temp;
1438#ifdef FEAT_VERTSPLIT
1439 temp = curwin->w_vsep_width;
1440 curwin->w_vsep_width = wp->w_vsep_width;
1441 wp->w_vsep_width = temp;
1442
1443 /* If the windows are not in the same frame, exchange the sizes to avoid
1444 * messing up the window layout. Otherwise fix the frame sizes. */
1445 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1446 {
1447 temp = curwin->w_height;
1448 curwin->w_height = wp->w_height;
1449 wp->w_height = temp;
1450 temp = curwin->w_width;
1451 curwin->w_width = wp->w_width;
1452 wp->w_width = temp;
1453 }
1454 else
1455 {
1456 frame_fix_height(curwin);
1457 frame_fix_height(wp);
1458 frame_fix_width(curwin);
1459 frame_fix_width(wp);
1460 }
1461#endif
1462
1463 (void)win_comp_pos(); /* recompute window positions */
1464
1465 win_enter(wp, TRUE);
1466 redraw_later(CLEAR);
1467}
1468
1469/*
1470 * rotate windows: if upwards TRUE the second window becomes the first one
1471 * if upwards FALSE the first window becomes the second one
1472 */
1473 static void
1474win_rotate(upwards, count)
1475 int upwards;
1476 int count;
1477{
1478 win_T *wp1;
1479 win_T *wp2;
1480 frame_T *frp;
1481 int n;
1482
1483 if (firstwin == lastwin) /* nothing to do */
1484 {
1485 beep_flush();
1486 return;
1487 }
1488
1489#ifdef FEAT_GUI
1490 need_mouse_correct = TRUE;
1491#endif
1492
1493#ifdef FEAT_VERTSPLIT
1494 /* Check if all frames in this row/col have one window. */
1495 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1496 frp = frp->fr_next)
1497 if (frp->fr_win == NULL)
1498 {
1499 EMSG(_("E443: Cannot rotate when another window is split"));
1500 return;
1501 }
1502#endif
1503
1504 while (count--)
1505 {
1506 if (upwards) /* first window becomes last window */
1507 {
1508 /* remove first window/frame from the list */
1509 frp = curwin->w_frame->fr_parent->fr_child;
1510 wp1 = frp->fr_win;
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 /* find last frame and append removed window/frame after it */
1515 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1516 ;
1517 win_append(frp->fr_win, wp1);
1518 frame_append(frp, wp1->w_frame);
1519
1520 wp2 = frp->fr_win; /* previously last window */
1521 }
1522 else /* last window becomes first window */
1523 {
1524 /* find last window/frame in the list and remove it */
1525 for (frp = curwin->w_frame; frp->fr_next != NULL;
1526 frp = frp->fr_next)
1527 ;
1528 wp1 = frp->fr_win;
1529 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001530 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 frame_remove(frp);
1532
1533 /* append the removed window/frame before the first in the list */
1534 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1535 frame_insert(frp->fr_parent->fr_child, frp);
1536 }
1537
1538 /* exchange status height and vsep width of old and new last window */
1539 n = wp2->w_status_height;
1540 wp2->w_status_height = wp1->w_status_height;
1541 wp1->w_status_height = n;
1542 frame_fix_height(wp1);
1543 frame_fix_height(wp2);
1544#ifdef FEAT_VERTSPLIT
1545 n = wp2->w_vsep_width;
1546 wp2->w_vsep_width = wp1->w_vsep_width;
1547 wp1->w_vsep_width = n;
1548 frame_fix_width(wp1);
1549 frame_fix_width(wp2);
1550#endif
1551
1552 /* recompute w_winrow and w_wincol for all windows */
1553 (void)win_comp_pos();
1554 }
1555
1556 redraw_later(CLEAR);
1557}
1558
1559/*
1560 * Move the current window to the very top/bottom/left/right of the screen.
1561 */
1562 static void
1563win_totop(size, flags)
1564 int size;
1565 int flags;
1566{
1567 int dir;
1568 int height = curwin->w_height;
1569
1570 if (lastwin == firstwin)
1571 {
1572 beep_flush();
1573 return;
1574 }
1575
1576 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001577 (void)winframe_remove(curwin, &dir, NULL);
1578 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 last_status(FALSE); /* may need to remove last status line */
1580 (void)win_comp_pos(); /* recompute window positions */
1581
1582 /* Split a window on the desired side and put the window there. */
1583 (void)win_split_ins(size, flags, curwin, dir);
1584 if (!(flags & WSP_VERT))
1585 {
1586 win_setheight(height);
1587 if (p_ea)
1588 win_equal(curwin, TRUE, 'v');
1589 }
1590
1591#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1592 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1593 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001594 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596}
1597
1598/*
1599 * Move window "win1" to below/right of "win2" and make "win1" the current
1600 * window. Only works within the same frame!
1601 */
1602 void
1603win_move_after(win1, win2)
1604 win_T *win1, *win2;
1605{
1606 int height;
1607
1608 /* check if the arguments are reasonable */
1609 if (win1 == win2)
1610 return;
1611
1612 /* check if there is something to do */
1613 if (win2->w_next != win1)
1614 {
1615 /* may need move the status line/vertical separator of the last window
1616 * */
1617 if (win1 == lastwin)
1618 {
1619 height = win1->w_prev->w_status_height;
1620 win1->w_prev->w_status_height = win1->w_status_height;
1621 win1->w_status_height = height;
1622#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001623 if (win1->w_prev->w_vsep_width == 1)
1624 {
1625 /* Remove the vertical separator from the last-but-one window,
1626 * add it to the last window. Adjust the frame widths. */
1627 win1->w_prev->w_vsep_width = 0;
1628 win1->w_prev->w_frame->fr_width -= 1;
1629 win1->w_vsep_width = 1;
1630 win1->w_frame->fr_width += 1;
1631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#endif
1633 }
1634 else if (win2 == lastwin)
1635 {
1636 height = win1->w_status_height;
1637 win1->w_status_height = win2->w_status_height;
1638 win2->w_status_height = height;
1639#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001640 if (win1->w_vsep_width == 1)
1641 {
1642 /* Remove the vertical separator from win1, add it to the last
1643 * window, win2. Adjust the frame widths. */
1644 win2->w_vsep_width = 1;
1645 win2->w_frame->fr_width += 1;
1646 win1->w_vsep_width = 0;
1647 win1->w_frame->fr_width -= 1;
1648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
1650 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001651 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 frame_remove(win1->w_frame);
1653 win_append(win2, win1);
1654 frame_append(win2->w_frame, win1->w_frame);
1655
1656 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1657 redraw_later(NOT_VALID);
1658 }
1659 win_enter(win1, FALSE);
1660}
1661
1662/*
1663 * Make all windows the same height.
1664 * 'next_curwin' will soon be the current window, make sure it has enough
1665 * rows.
1666 */
1667 void
1668win_equal(next_curwin, current, dir)
1669 win_T *next_curwin; /* pointer to current window to be or NULL */
1670 int current; /* do only frame with current window */
1671 int dir; /* 'v' for vertically, 'h' for horizontally,
1672 'b' for both, 0 for using p_ead */
1673{
1674 if (dir == 0)
1675#ifdef FEAT_VERTSPLIT
1676 dir = *p_ead;
1677#else
1678 dir = 'b';
1679#endif
1680 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001681 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001682 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683}
1684
1685/*
1686 * Set a frame to a new position and height, spreading the available room
1687 * equally over contained frames.
1688 * The window "next_curwin" (if not NULL) should at least get the size from
1689 * 'winheight' and 'winwidth' if possible.
1690 */
1691 static void
1692win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1693 win_T *next_curwin; /* pointer to current window to be or NULL */
1694 int current; /* do only frame with current window */
1695 frame_T *topfr; /* frame to set size off */
1696 int dir; /* 'v', 'h' or 'b', see win_equal() */
1697 int col; /* horizontal position for frame */
1698 int row; /* vertical position for frame */
1699 int width; /* new width of frame */
1700 int height; /* new height of frame */
1701{
1702 int n, m;
1703 int extra_sep = 0;
1704 int wincount, totwincount = 0;
1705 frame_T *fr;
1706 int next_curwin_size = 0;
1707 int room = 0;
1708 int new_size;
1709 int has_next_curwin = 0;
1710 int hnc;
1711
1712 if (topfr->fr_layout == FR_LEAF)
1713 {
1714 /* Set the width/height of this frame.
1715 * Redraw when size or position changes */
1716 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1717#ifdef FEAT_VERTSPLIT
1718 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1719#endif
1720 )
1721 {
1722 topfr->fr_win->w_winrow = row;
1723 frame_new_height(topfr, height, FALSE, FALSE);
1724#ifdef FEAT_VERTSPLIT
1725 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001726 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
1728 redraw_all_later(CLEAR);
1729 }
1730 }
1731#ifdef FEAT_VERTSPLIT
1732 else if (topfr->fr_layout == FR_ROW)
1733 {
1734 topfr->fr_width = width;
1735 topfr->fr_height = height;
1736
1737 if (dir != 'v') /* equalize frame widths */
1738 {
1739 /* Compute the maximum number of windows horizontally in this
1740 * frame. */
1741 n = frame_minwidth(topfr, NOWIN);
1742 /* add one for the rightmost window, it doesn't have a separator */
1743 if (col + width == Columns)
1744 extra_sep = 1;
1745 else
1746 extra_sep = 0;
1747 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001748 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001750 /*
1751 * Compute width for "next_curwin" window and room available for
1752 * other windows.
1753 * "m" is the minimal width when counting p_wiw for "next_curwin".
1754 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 m = frame_minwidth(topfr, next_curwin);
1756 room = width - m;
1757 if (room < 0)
1758 {
1759 next_curwin_size = p_wiw + room;
1760 room = 0;
1761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 else
1763 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001764 next_curwin_size = -1;
1765 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1766 {
1767 /* If 'winfixwidth' set keep the window width if
1768 * possible.
1769 * Watch out for this window being the next_curwin. */
1770 if (frame_fixed_width(fr))
1771 {
1772 n = frame_minwidth(fr, NOWIN);
1773 new_size = fr->fr_width;
1774 if (frame_has_win(fr, next_curwin))
1775 {
1776 room += p_wiw - p_wmw;
1777 next_curwin_size = 0;
1778 if (new_size < p_wiw)
1779 new_size = p_wiw;
1780 }
1781 else
1782 /* These windows don't use up room. */
1783 totwincount -= (n + (fr->fr_next == NULL
1784 ? extra_sep : 0)) / (p_wmw + 1);
1785 room -= new_size - n;
1786 if (room < 0)
1787 {
1788 new_size += room;
1789 room = 0;
1790 }
1791 fr->fr_newwidth = new_size;
1792 }
1793 }
1794 if (next_curwin_size == -1)
1795 {
1796 if (!has_next_curwin)
1797 next_curwin_size = 0;
1798 else if (totwincount > 1
1799 && (room + (totwincount - 2))
1800 / (totwincount - 1) > p_wiw)
1801 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001802 /* Can make all windows wider than 'winwidth', spread
1803 * the room equally. */
1804 next_curwin_size = (room + p_wiw
1805 + (totwincount - 1) * p_wmw
1806 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001807 room -= next_curwin_size - p_wiw;
1808 }
1809 else
1810 next_curwin_size = p_wiw;
1811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001813
1814 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 --totwincount; /* don't count curwin */
1816 }
1817
1818 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1819 {
1820 n = m = 0;
1821 wincount = 1;
1822 if (fr->fr_next == NULL)
1823 /* last frame gets all that remains (avoid roundoff error) */
1824 new_size = width;
1825 else if (dir == 'v')
1826 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001827 else if (frame_fixed_width(fr))
1828 {
1829 new_size = fr->fr_newwidth;
1830 wincount = 0; /* doesn't count as a sizeable window */
1831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 else
1833 {
1834 /* Compute the maximum number of windows horiz. in "fr". */
1835 n = frame_minwidth(fr, NOWIN);
1836 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1837 / (p_wmw + 1);
1838 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001839 if (has_next_curwin)
1840 hnc = frame_has_win(fr, next_curwin);
1841 else
1842 hnc = FALSE;
1843 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001845 if (totwincount == 0)
1846 new_size = room;
1847 else
1848 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001850 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 next_curwin_size -= p_wiw - (m - n);
1853 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001854 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001856 else
1857 room -= new_size;
1858 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001861 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 * window, unless equalizing all frames. */
1863 if (!current || dir != 'v' || topfr->fr_parent != NULL
1864 || (new_size != fr->fr_width)
1865 || frame_has_win(fr, next_curwin))
1866 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001867 new_size, height);
1868 col += new_size;
1869 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 totwincount -= wincount;
1871 }
1872 }
1873#endif
1874 else /* topfr->fr_layout == FR_COL */
1875 {
1876#ifdef FEAT_VERTSPLIT
1877 topfr->fr_width = width;
1878#endif
1879 topfr->fr_height = height;
1880
1881 if (dir != 'h') /* equalize frame heights */
1882 {
1883 /* Compute maximum number of windows vertically in this frame. */
1884 n = frame_minheight(topfr, NOWIN);
1885 /* add one for the bottom window if it doesn't have a statusline */
1886 if (row + height == cmdline_row && p_ls == 0)
1887 extra_sep = 1;
1888 else
1889 extra_sep = 0;
1890 totwincount = (n + extra_sep) / (p_wmh + 1);
1891 has_next_curwin = frame_has_win(topfr, next_curwin);
1892
1893 /*
1894 * Compute height for "next_curwin" window and room available for
1895 * other windows.
1896 * "m" is the minimal height when counting p_wh for "next_curwin".
1897 */
1898 m = frame_minheight(topfr, next_curwin);
1899 room = height - m;
1900 if (room < 0)
1901 {
1902 /* The room is less then 'winheight', use all space for the
1903 * current window. */
1904 next_curwin_size = p_wh + room;
1905 room = 0;
1906 }
1907 else
1908 {
1909 next_curwin_size = -1;
1910 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1911 {
1912 /* If 'winfixheight' set keep the window height if
1913 * possible.
1914 * Watch out for this window being the next_curwin. */
1915 if (frame_fixed_height(fr))
1916 {
1917 n = frame_minheight(fr, NOWIN);
1918 new_size = fr->fr_height;
1919 if (frame_has_win(fr, next_curwin))
1920 {
1921 room += p_wh - p_wmh;
1922 next_curwin_size = 0;
1923 if (new_size < p_wh)
1924 new_size = p_wh;
1925 }
1926 else
1927 /* These windows don't use up room. */
1928 totwincount -= (n + (fr->fr_next == NULL
1929 ? extra_sep : 0)) / (p_wmh + 1);
1930 room -= new_size - n;
1931 if (room < 0)
1932 {
1933 new_size += room;
1934 room = 0;
1935 }
1936 fr->fr_newheight = new_size;
1937 }
1938 }
1939 if (next_curwin_size == -1)
1940 {
1941 if (!has_next_curwin)
1942 next_curwin_size = 0;
1943 else if (totwincount > 1
1944 && (room + (totwincount - 2))
1945 / (totwincount - 1) > p_wh)
1946 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001947 /* can make all windows higher than 'winheight',
1948 * spread the room equally. */
1949 next_curwin_size = (room + p_wh
1950 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 + (totwincount - 1)) / totwincount;
1952 room -= next_curwin_size - p_wh;
1953 }
1954 else
1955 next_curwin_size = p_wh;
1956 }
1957 }
1958
1959 if (has_next_curwin)
1960 --totwincount; /* don't count curwin */
1961 }
1962
1963 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1964 {
1965 n = m = 0;
1966 wincount = 1;
1967 if (fr->fr_next == NULL)
1968 /* last frame gets all that remains (avoid roundoff error) */
1969 new_size = height;
1970 else if (dir == 'h')
1971 new_size = fr->fr_height;
1972 else if (frame_fixed_height(fr))
1973 {
1974 new_size = fr->fr_newheight;
1975 wincount = 0; /* doesn't count as a sizeable window */
1976 }
1977 else
1978 {
1979 /* Compute the maximum number of windows vert. in "fr". */
1980 n = frame_minheight(fr, NOWIN);
1981 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1982 / (p_wmh + 1);
1983 m = frame_minheight(fr, next_curwin);
1984 if (has_next_curwin)
1985 hnc = frame_has_win(fr, next_curwin);
1986 else
1987 hnc = FALSE;
1988 if (hnc) /* don't count next_curwin */
1989 --wincount;
1990 if (totwincount == 0)
1991 new_size = room;
1992 else
1993 new_size = (wincount * room + ((unsigned)totwincount >> 1))
1994 / totwincount;
1995 if (hnc) /* add next_curwin size */
1996 {
1997 next_curwin_size -= p_wh - (m - n);
1998 new_size += next_curwin_size;
1999 room -= new_size - next_curwin_size;
2000 }
2001 else
2002 room -= new_size;
2003 new_size += n;
2004 }
2005 /* Skip frame that is full width when splitting or closing a
2006 * window, unless equalizing all frames. */
2007 if (!current || dir != 'h' || topfr->fr_parent != NULL
2008 || (new_size != fr->fr_height)
2009 || frame_has_win(fr, next_curwin))
2010 win_equal_rec(next_curwin, current, fr, dir, col, row,
2011 width, new_size);
2012 row += new_size;
2013 height -= new_size;
2014 totwincount -= wincount;
2015 }
2016 }
2017}
2018
2019/*
2020 * close all windows for buffer 'buf'
2021 */
2022 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00002023close_windows(buf, keep_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 buf_T *buf;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002025 int keep_curwin; /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002027 win_T *wp;
2028 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002029 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030
2031 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002032
2033 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002035 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2036#ifdef FEAT_AUTOCMD
2037 && !(wp->w_closing || wp->w_buffer->b_closing)
2038#endif
2039 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002041 win_close(wp, FALSE);
2042
2043 /* Start all over, autocommands may change the window layout. */
2044 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002047 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002049
2050 /* Also check windows in other tab pages. */
2051 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2052 {
2053 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002054 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002055 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002056 if (wp->w_buffer == buf
2057#ifdef FEAT_AUTOCMD
2058 && !(wp->w_closing || wp->w_buffer->b_closing)
2059#endif
2060 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002061 {
2062 win_close_othertab(wp, FALSE, tp);
2063
2064 /* Start all over, the tab page may be closed and
2065 * autocommands may change the window layout. */
2066 nexttp = first_tabpage;
2067 break;
2068 }
2069 }
2070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002072
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002073 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002074 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002075 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076}
2077
2078/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002079 * Return TRUE if the current window is the only window that exists (ignoring
2080 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002081 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002082 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002083 static int
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002084last_window()
2085{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002086 return (one_window() && first_tabpage->tp_next == NULL);
2087}
2088
2089/*
2090 * Return TRUE if there is only one window other than "aucmd_win" in the
2091 * current tab page.
2092 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002093 int
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002094one_window()
2095{
2096#ifdef FEAT_AUTOCMD
2097 win_T *wp;
2098 int seen_one = FALSE;
2099
2100 FOR_ALL_WINDOWS(wp)
2101 {
2102 if (wp != aucmd_win)
2103 {
2104 if (seen_one)
2105 return FALSE;
2106 seen_one = TRUE;
2107 }
2108 }
2109 return TRUE;
2110#else
2111 return firstwin == lastwin;
2112#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002113}
2114
2115/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002116 * Close the possibly last window in a tab page.
2117 * Returns TRUE when the window was closed already.
2118 */
2119 static int
2120close_last_window_tabpage(win, free_buf, prev_curtab)
2121 win_T *win;
2122 int free_buf;
2123 tabpage_T *prev_curtab;
2124{
2125 if (firstwin == lastwin)
2126 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002127 buf_T *old_curbuf = curbuf;
2128
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002129 /*
2130 * Closing the last window in a tab page. First go to another tab
2131 * page and then close the window and the tab page. This avoids that
2132 * curwin and curtab are invalid while we are freeing memory, they may
2133 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002134 * Don't trigger autocommands yet, they may use wrong values, so do
2135 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002136 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002137 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002138 redraw_tabline = TRUE;
2139
2140 /* Safety check: Autocommands may have closed the window when jumping
2141 * to the other tab page. */
2142 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2143 {
2144 int h = tabline_height();
2145
2146 win_close_othertab(win, free_buf, prev_curtab);
2147 if (h != tabline_height())
2148 shell_new_rows();
2149 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002150 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2151 * that now. */
2152#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002153 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002154 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2155 if (old_curbuf != curbuf)
2156 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002157#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002158 return TRUE;
2159 }
2160 return FALSE;
2161}
2162
2163/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002164 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 * If "free_buf" is TRUE related buffer may be unloaded.
2166 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002167 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 */
2169 void
2170win_close(win, free_buf)
2171 win_T *win;
2172 int free_buf;
2173{
2174 win_T *wp;
2175#ifdef FEAT_AUTOCMD
2176 int other_buffer = FALSE;
2177#endif
2178 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 int dir;
2180 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002181 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002183 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
2185 EMSG(_("E444: Cannot close last window"));
2186 return;
2187 }
2188
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002189#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002190 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002191 return; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002192 if (win == aucmd_win)
2193 {
2194 EMSG(_("E813: Cannot close autocmd window"));
2195 return;
2196 }
2197 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2198 {
2199 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
2200 return;
2201 }
2202#endif
2203
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002204 /* When closing the last window in a tab page first go to another tab page
2205 * and then close the window and the tab page to avoid that curwin and
2206 * curtab are invalid while we are freeing memory. */
2207 if (close_last_window_tabpage(win, free_buf, prev_curtab))
2208 return;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 /* When closing the help window, try restoring a snapshot after closing
2211 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002212 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 help_window = TRUE;
2214 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002215 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216
2217#ifdef FEAT_AUTOCMD
2218 if (win == curwin)
2219 {
2220 /*
2221 * Guess which window is going to be the new current window.
2222 * This may change because of the autocommands (sigh).
2223 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002224 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002227 * Be careful: If autocommands delete the window or cause this window
2228 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 */
2230 if (wp->w_buffer != curbuf)
2231 {
2232 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002233 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002235 if (!win_valid(win))
2236 return;
2237 win->w_closing = FALSE;
2238 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return;
2240 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002241 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002243 if (!win_valid(win))
2244 return;
2245 win->w_closing = FALSE;
2246 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 return;
2248# ifdef FEAT_EVAL
2249 /* autocmds may abort script processing */
2250 if (aborting())
2251 return;
2252# endif
2253 }
2254#endif
2255
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002256#ifdef FEAT_GUI
2257 /* Avoid trouble with scrollbars that are going to be deleted in
2258 * win_free(). */
2259 if (gui.in_use)
2260 out_flush();
2261#endif
2262
Bram Moolenaara971b822011-09-14 14:43:25 +02002263#ifdef FEAT_SYN_HL
2264 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002265 if (win->w_buffer != NULL)
2266 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002267#endif
2268
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 /*
2270 * Close the link to the buffer.
2271 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002272 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002273 {
2274#ifdef FEAT_AUTOCMD
2275 win->w_closing = TRUE;
2276#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002277 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002278#ifdef FEAT_AUTOCMD
2279 if (win_valid(win))
2280 win->w_closing = FALSE;
2281#endif
2282 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002283
Bram Moolenaar802418d2013-01-17 14:00:11 +01002284 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2285 && (last_window() || curtab != prev_curtab
2286 || close_last_window_tabpage(win, free_buf, prev_curtab)))
2287 /* Autocommands have close all windows, quit now. */
2288 getout(0);
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002291 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002292 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002293 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 return;
2295
Bram Moolenaara971b822011-09-14 14:43:25 +02002296 /* Free the memory used for the window and get the window that received
2297 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002298 wp = win_free_mem(win, &dir, NULL);
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 /* Make sure curwin isn't invalid. It can cause severe trouble when
2301 * printing an error message. For win_equal() curbuf needs to be valid
2302 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002303 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 curwin = wp;
2306#ifdef FEAT_QUICKFIX
2307 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2308 {
2309 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002310 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 * finding another window to go to.
2312 */
2313 for (;;)
2314 {
2315 if (wp->w_next == NULL)
2316 wp = firstwin;
2317 else
2318 wp = wp->w_next;
2319 if (wp == curwin)
2320 break;
2321 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2322 {
2323 curwin = wp;
2324 break;
2325 }
2326 }
2327 }
2328#endif
2329 curbuf = curwin->w_buffer;
2330 close_curwin = TRUE;
2331 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002332 if (p_ea
2333#ifdef FEAT_VERTSPLIT
2334 && (*p_ead == 'b' || *p_ead == dir)
2335#endif
2336 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 win_equal(curwin, TRUE,
2338#ifdef FEAT_VERTSPLIT
2339 dir
2340#else
2341 0
2342#endif
2343 );
2344 else
2345 win_comp_pos();
2346 if (close_curwin)
2347 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002348 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#ifdef FEAT_AUTOCMD
2350 if (other_buffer)
2351 /* careful: after this wp and win may be invalid! */
2352 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2353#endif
2354 }
2355
2356 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002357 * If last window has a status line now and we don't want one,
2358 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 */
2360 last_status(FALSE);
2361
2362 /* After closing the help window, try restoring the window layout from
2363 * before it was opened. */
2364 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002365 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2368 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2369 if (gui.in_use && !win_hasvertsplit())
2370 gui_init_which_components(NULL);
2371#endif
2372
2373 redraw_all_later(NOT_VALID);
2374}
2375
2376/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002377 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002378 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002379 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002380 * Caller must check if buffer is hidden and whether the tabline needs to be
2381 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002382 */
2383 void
2384win_close_othertab(win, free_buf, tp)
2385 win_T *win;
2386 int free_buf;
2387 tabpage_T *tp;
2388{
2389 win_T *wp;
2390 int dir;
2391 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002392 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002393
Bram Moolenaar362ce482012-06-06 19:02:45 +02002394#ifdef FEAT_AUTOCMD
2395 if (win->w_closing || win->w_buffer->b_closing)
2396 return; /* window is already being closed */
2397#endif
2398
Bram Moolenaarf740b292006-02-16 22:11:02 +00002399 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002400 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002401
2402 /* Careful: Autocommands may have closed the tab page or made it the
2403 * current tab page. */
2404 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2405 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002406 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002407 return;
2408
2409 /* Autocommands may have closed the window already. */
2410 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2411 ;
2412 if (wp == NULL)
2413 return;
2414
Bram Moolenaarf740b292006-02-16 22:11:02 +00002415 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002416 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002417 {
2418 if (tp == first_tabpage)
2419 first_tabpage = tp->tp_next;
2420 else
2421 {
2422 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2423 ptp = ptp->tp_next)
2424 ;
2425 if (ptp == NULL)
2426 {
2427 EMSG2(_(e_intern2), "win_close_othertab()");
2428 return;
2429 }
2430 ptp->tp_next = tp->tp_next;
2431 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002432 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002433 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002434
2435 /* Free the memory used for the window. */
2436 win_free_mem(win, &dir, tp);
2437
2438 if (free_tp)
2439 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002440}
2441
2442/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002443 * Free the memory used for a window.
2444 * Returns a pointer to the window that got the freed up space.
2445 */
2446 static win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002447win_free_mem(win, dirp, tp)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002448 win_T *win;
2449 int *dirp; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002450 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002451{
2452 frame_T *frp;
2453 win_T *wp;
2454
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002455 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002456 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002457 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002458 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002459 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002460
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002461 /* When deleting the current window of another tab page select a new
2462 * current window. */
2463 if (tp != NULL && win == tp->tp_curwin)
2464 tp->tp_curwin = wp;
2465
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002466 return wp;
2467}
2468
2469#if defined(EXITFREE) || defined(PROTO)
2470 void
2471win_free_all()
2472{
2473 int dummy;
2474
Bram Moolenaarf740b292006-02-16 22:11:02 +00002475# ifdef FEAT_WINDOWS
2476 while (first_tabpage->tp_next != NULL)
2477 tabpage_close(TRUE);
2478# endif
2479
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002480# ifdef FEAT_AUTOCMD
2481 if (aucmd_win != NULL)
2482 {
2483 (void)win_free_mem(aucmd_win, &dummy, NULL);
2484 aucmd_win = NULL;
2485 }
2486# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002487
2488 while (firstwin != NULL)
2489 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002490}
2491#endif
2492
2493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 * Remove a window and its frame from the tree of frames.
2495 * Returns a pointer to the window that got the freed up space.
2496 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002497 win_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002498winframe_remove(win, dirp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 win_T *win;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00002500 int *dirp UNUSED; /* set to 'v' or 'h' for direction if 'ea' */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002501 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502{
2503 frame_T *frp, *frp2, *frp3;
2504 frame_T *frp_close = win->w_frame;
2505 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
2507 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002508 * If there is only one window there is nothing to remove.
2509 */
2510 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2511 return NULL;
2512
2513 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 * Remove the window from its frame.
2515 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002516 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 wp = frame2win(frp2);
2518
2519 /* Remove this frame from the list of frames. */
2520 frame_remove(frp_close);
2521
2522#ifdef FEAT_VERTSPLIT
2523 if (frp_close->fr_parent->fr_layout == FR_COL)
2524 {
2525#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002526 /* When 'winfixheight' is set, try to find another frame in the column
2527 * (as close to the closed frame as possible) to distribute the height
2528 * to. */
2529 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2530 {
2531 frp = frp_close->fr_prev;
2532 frp3 = frp_close->fr_next;
2533 while (frp != NULL || frp3 != NULL)
2534 {
2535 if (frp != NULL)
2536 {
2537 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2538 {
2539 frp2 = frp;
2540 wp = frp->fr_win;
2541 break;
2542 }
2543 frp = frp->fr_prev;
2544 }
2545 if (frp3 != NULL)
2546 {
2547 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2548 {
2549 frp2 = frp3;
2550 wp = frp3->fr_win;
2551 break;
2552 }
2553 frp3 = frp3->fr_next;
2554 }
2555 }
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2558 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559#ifdef FEAT_VERTSPLIT
2560 *dirp = 'v';
2561 }
2562 else
2563 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002564 /* When 'winfixwidth' is set, try to find another frame in the column
2565 * (as close to the closed frame as possible) to distribute the width
2566 * to. */
2567 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2568 {
2569 frp = frp_close->fr_prev;
2570 frp3 = frp_close->fr_next;
2571 while (frp != NULL || frp3 != NULL)
2572 {
2573 if (frp != NULL)
2574 {
2575 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2576 {
2577 frp2 = frp;
2578 wp = frp->fr_win;
2579 break;
2580 }
2581 frp = frp->fr_prev;
2582 }
2583 if (frp3 != NULL)
2584 {
2585 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2586 {
2587 frp2 = frp3;
2588 wp = frp3->fr_win;
2589 break;
2590 }
2591 frp3 = frp3->fr_next;
2592 }
2593 }
2594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002596 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 *dirp = 'h';
2598 }
2599#endif
2600
2601 /* If rows/columns go to a window below/right its positions need to be
2602 * updated. Can only be done after the sizes have been updated. */
2603 if (frp2 == frp_close->fr_next)
2604 {
2605 int row = win->w_winrow;
2606 int col = W_WINCOL(win);
2607
2608 frame_comp_pos(frp2, &row, &col);
2609 }
2610
2611 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2612 {
2613 /* There is no other frame in this list, move its info to the parent
2614 * and remove it. */
2615 frp2->fr_parent->fr_layout = frp2->fr_layout;
2616 frp2->fr_parent->fr_child = frp2->fr_child;
2617 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2618 frp->fr_parent = frp2->fr_parent;
2619 frp2->fr_parent->fr_win = frp2->fr_win;
2620 if (frp2->fr_win != NULL)
2621 frp2->fr_win->w_frame = frp2->fr_parent;
2622 frp = frp2->fr_parent;
2623 vim_free(frp2);
2624
2625 frp2 = frp->fr_parent;
2626 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2627 {
2628 /* The frame above the parent has the same layout, have to merge
2629 * the frames into this list. */
2630 if (frp2->fr_child == frp)
2631 frp2->fr_child = frp->fr_child;
2632 frp->fr_child->fr_prev = frp->fr_prev;
2633 if (frp->fr_prev != NULL)
2634 frp->fr_prev->fr_next = frp->fr_child;
2635 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2636 {
2637 frp3->fr_parent = frp2;
2638 if (frp3->fr_next == NULL)
2639 {
2640 frp3->fr_next = frp->fr_next;
2641 if (frp->fr_next != NULL)
2642 frp->fr_next->fr_prev = frp3;
2643 break;
2644 }
2645 }
2646 vim_free(frp);
2647 }
2648 }
2649
2650 return wp;
2651}
2652
2653/*
2654 * Find out which frame is going to get the freed up space when "win" is
2655 * closed.
2656 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2657 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2658 * This makes opening a window and closing it immediately keep the same window
2659 * layout.
2660 */
2661 static frame_T *
Bram Moolenaarf740b292006-02-16 22:11:02 +00002662win_altframe(win, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002664 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
2666 frame_T *frp;
2667 int b;
2668
Bram Moolenaarf740b292006-02-16 22:11:02 +00002669 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002670 /* Last window in this tab page, will go to next tab page. */
2671 return alt_tabpage()->tp_curwin->w_frame;
2672
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 frp = win->w_frame;
2674#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002675 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 b = p_spr;
2677 else
2678#endif
2679 b = p_sb;
2680 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2681 return frp->fr_next;
2682 return frp->fr_prev;
2683}
2684
2685/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002686 * Return the tabpage that will be used if the current one is closed.
2687 */
2688 static tabpage_T *
2689alt_tabpage()
2690{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002691 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002692
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002693 /* Use the next tab page if possible. */
2694 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002695 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002696
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002697 /* Find the last but one tab page. */
2698 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2699 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002700 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002701}
2702
2703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 * Find the left-upper window in frame "frp".
2705 */
2706 static win_T *
2707frame2win(frp)
2708 frame_T *frp;
2709{
2710 while (frp->fr_win == NULL)
2711 frp = frp->fr_child;
2712 return frp->fr_win;
2713}
2714
2715/*
2716 * Return TRUE if frame "frp" contains window "wp".
2717 */
2718 static int
2719frame_has_win(frp, wp)
2720 frame_T *frp;
2721 win_T *wp;
2722{
2723 frame_T *p;
2724
2725 if (frp->fr_layout == FR_LEAF)
2726 return frp->fr_win == wp;
2727
2728 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2729 if (frame_has_win(p, wp))
2730 return TRUE;
2731 return FALSE;
2732}
2733
2734/*
2735 * Set a new height for a frame. Recursively sets the height for contained
2736 * frames and windows. Caller must take care of positions.
2737 */
2738 static void
2739frame_new_height(topfrp, height, topfirst, wfh)
2740 frame_T *topfrp;
2741 int height;
2742 int topfirst; /* resize topmost contained frame first */
2743 int wfh; /* obey 'winfixheight' when there is a choice;
2744 may cause the height not to be set */
2745{
2746 frame_T *frp;
2747 int extra_lines;
2748 int h;
2749
2750 if (topfrp->fr_win != NULL)
2751 {
2752 /* Simple case: just one window. */
2753 win_new_height(topfrp->fr_win,
2754 height - topfrp->fr_win->w_status_height);
2755 }
2756#ifdef FEAT_VERTSPLIT
2757 else if (topfrp->fr_layout == FR_ROW)
2758 {
2759 do
2760 {
2761 /* All frames in this row get the same new height. */
2762 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2763 {
2764 frame_new_height(frp, height, topfirst, wfh);
2765 if (frp->fr_height > height)
2766 {
2767 /* Could not fit the windows, make the whole row higher. */
2768 height = frp->fr_height;
2769 break;
2770 }
2771 }
2772 }
2773 while (frp != NULL);
2774 }
2775#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002776 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
2778 /* Complicated case: Resize a column of frames. Resize the bottom
2779 * frame first, frames above that when needed. */
2780
2781 frp = topfrp->fr_child;
2782 if (wfh)
2783 /* Advance past frames with one window with 'wfh' set. */
2784 while (frame_fixed_height(frp))
2785 {
2786 frp = frp->fr_next;
2787 if (frp == NULL)
2788 return; /* no frame without 'wfh', give up */
2789 }
2790 if (!topfirst)
2791 {
2792 /* Find the bottom frame of this column */
2793 while (frp->fr_next != NULL)
2794 frp = frp->fr_next;
2795 if (wfh)
2796 /* Advance back for frames with one window with 'wfh' set. */
2797 while (frame_fixed_height(frp))
2798 frp = frp->fr_prev;
2799 }
2800
2801 extra_lines = height - topfrp->fr_height;
2802 if (extra_lines < 0)
2803 {
2804 /* reduce height of contained frames, bottom or top frame first */
2805 while (frp != NULL)
2806 {
2807 h = frame_minheight(frp, NULL);
2808 if (frp->fr_height + extra_lines < h)
2809 {
2810 extra_lines += frp->fr_height - h;
2811 frame_new_height(frp, h, topfirst, wfh);
2812 }
2813 else
2814 {
2815 frame_new_height(frp, frp->fr_height + extra_lines,
2816 topfirst, wfh);
2817 break;
2818 }
2819 if (topfirst)
2820 {
2821 do
2822 frp = frp->fr_next;
2823 while (wfh && frp != NULL && frame_fixed_height(frp));
2824 }
2825 else
2826 {
2827 do
2828 frp = frp->fr_prev;
2829 while (wfh && frp != NULL && frame_fixed_height(frp));
2830 }
2831 /* Increase "height" if we could not reduce enough frames. */
2832 if (frp == NULL)
2833 height -= extra_lines;
2834 }
2835 }
2836 else if (extra_lines > 0)
2837 {
2838 /* increase height of bottom or top frame */
2839 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2840 }
2841 }
2842 topfrp->fr_height = height;
2843}
2844
2845/*
2846 * Return TRUE if height of frame "frp" should not be changed because of
2847 * the 'winfixheight' option.
2848 */
2849 static int
2850frame_fixed_height(frp)
2851 frame_T *frp;
2852{
2853 /* frame with one window: fixed height if 'winfixheight' set. */
2854 if (frp->fr_win != NULL)
2855 return frp->fr_win->w_p_wfh;
2856
2857 if (frp->fr_layout == FR_ROW)
2858 {
2859 /* The frame is fixed height if one of the frames in the row is fixed
2860 * height. */
2861 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2862 if (frame_fixed_height(frp))
2863 return TRUE;
2864 return FALSE;
2865 }
2866
2867 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2868 * frames in the row are fixed height. */
2869 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2870 if (!frame_fixed_height(frp))
2871 return FALSE;
2872 return TRUE;
2873}
2874
2875#ifdef FEAT_VERTSPLIT
2876/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002877 * Return TRUE if width of frame "frp" should not be changed because of
2878 * the 'winfixwidth' option.
2879 */
2880 static int
2881frame_fixed_width(frp)
2882 frame_T *frp;
2883{
2884 /* frame with one window: fixed width if 'winfixwidth' set. */
2885 if (frp->fr_win != NULL)
2886 return frp->fr_win->w_p_wfw;
2887
2888 if (frp->fr_layout == FR_COL)
2889 {
2890 /* The frame is fixed width if one of the frames in the row is fixed
2891 * width. */
2892 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2893 if (frame_fixed_width(frp))
2894 return TRUE;
2895 return FALSE;
2896 }
2897
2898 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2899 * frames in the row are fixed width. */
2900 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2901 if (!frame_fixed_width(frp))
2902 return FALSE;
2903 return TRUE;
2904}
2905
2906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 * Add a status line to windows at the bottom of "frp".
2908 * Note: Does not check if there is room!
2909 */
2910 static void
2911frame_add_statusline(frp)
2912 frame_T *frp;
2913{
2914 win_T *wp;
2915
2916 if (frp->fr_layout == FR_LEAF)
2917 {
2918 wp = frp->fr_win;
2919 if (wp->w_status_height == 0)
2920 {
2921 if (wp->w_height > 0) /* don't make it negative */
2922 --wp->w_height;
2923 wp->w_status_height = STATUS_HEIGHT;
2924 }
2925 }
2926 else if (frp->fr_layout == FR_ROW)
2927 {
2928 /* Handle all the frames in the row. */
2929 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2930 frame_add_statusline(frp);
2931 }
2932 else /* frp->fr_layout == FR_COL */
2933 {
2934 /* Only need to handle the last frame in the column. */
2935 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2936 ;
2937 frame_add_statusline(frp);
2938 }
2939}
2940
2941/*
2942 * Set width of a frame. Handles recursively going through contained frames.
2943 * May remove separator line for windows at the right side (for win_close()).
2944 */
2945 static void
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002946frame_new_width(topfrp, width, leftfirst, wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 frame_T *topfrp;
2948 int width;
2949 int leftfirst; /* resize leftmost contained frame first */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002950 int wfw; /* obey 'winfixwidth' when there is a choice;
2951 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 frame_T *frp;
2954 int extra_cols;
2955 int w;
2956 win_T *wp;
2957
2958 if (topfrp->fr_layout == FR_LEAF)
2959 {
2960 /* Simple case: just one window. */
2961 wp = topfrp->fr_win;
2962 /* Find out if there are any windows right of this one. */
2963 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2964 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2965 break;
2966 if (frp->fr_parent == NULL)
2967 wp->w_vsep_width = 0;
2968 win_new_width(wp, width - wp->w_vsep_width);
2969 }
2970 else if (topfrp->fr_layout == FR_COL)
2971 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002972 do
2973 {
2974 /* All frames in this column get the same new width. */
2975 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2976 {
2977 frame_new_width(frp, width, leftfirst, wfw);
2978 if (frp->fr_width > width)
2979 {
2980 /* Could not fit the windows, make whole column wider. */
2981 width = frp->fr_width;
2982 break;
2983 }
2984 }
2985 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 }
2987 else /* fr_layout == FR_ROW */
2988 {
2989 /* Complicated case: Resize a row of frames. Resize the rightmost
2990 * frame first, frames left of it when needed. */
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002993 if (wfw)
2994 /* Advance past frames with one window with 'wfw' set. */
2995 while (frame_fixed_width(frp))
2996 {
2997 frp = frp->fr_next;
2998 if (frp == NULL)
2999 return; /* no frame without 'wfw', give up */
3000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003002 {
3003 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 while (frp->fr_next != NULL)
3005 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003006 if (wfw)
3007 /* Advance back for frames with one window with 'wfw' set. */
3008 while (frame_fixed_width(frp))
3009 frp = frp->fr_prev;
3010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011
3012 extra_cols = width - topfrp->fr_width;
3013 if (extra_cols < 0)
3014 {
3015 /* reduce frame width, rightmost frame first */
3016 while (frp != NULL)
3017 {
3018 w = frame_minwidth(frp, NULL);
3019 if (frp->fr_width + extra_cols < w)
3020 {
3021 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003022 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 }
3024 else
3025 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003026 frame_new_width(frp, frp->fr_width + extra_cols,
3027 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 break;
3029 }
3030 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003031 {
3032 do
3033 frp = frp->fr_next;
3034 while (wfw && frp != NULL && frame_fixed_width(frp));
3035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003037 {
3038 do
3039 frp = frp->fr_prev;
3040 while (wfw && frp != NULL && frame_fixed_width(frp));
3041 }
3042 /* Increase "width" if we could not reduce enough frames. */
3043 if (frp == NULL)
3044 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 }
3046 }
3047 else if (extra_cols > 0)
3048 {
3049 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003050 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
3052 }
3053 topfrp->fr_width = width;
3054}
3055
3056/*
3057 * Add the vertical separator to windows at the right side of "frp".
3058 * Note: Does not check if there is room!
3059 */
3060 static void
3061frame_add_vsep(frp)
3062 frame_T *frp;
3063{
3064 win_T *wp;
3065
3066 if (frp->fr_layout == FR_LEAF)
3067 {
3068 wp = frp->fr_win;
3069 if (wp->w_vsep_width == 0)
3070 {
3071 if (wp->w_width > 0) /* don't make it negative */
3072 --wp->w_width;
3073 wp->w_vsep_width = 1;
3074 }
3075 }
3076 else if (frp->fr_layout == FR_COL)
3077 {
3078 /* Handle all the frames in the column. */
3079 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3080 frame_add_vsep(frp);
3081 }
3082 else /* frp->fr_layout == FR_ROW */
3083 {
3084 /* Only need to handle the last frame in the row. */
3085 frp = frp->fr_child;
3086 while (frp->fr_next != NULL)
3087 frp = frp->fr_next;
3088 frame_add_vsep(frp);
3089 }
3090}
3091
3092/*
3093 * Set frame width from the window it contains.
3094 */
3095 static void
3096frame_fix_width(wp)
3097 win_T *wp;
3098{
3099 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3100}
3101#endif
3102
3103/*
3104 * Set frame height from the window it contains.
3105 */
3106 static void
3107frame_fix_height(wp)
3108 win_T *wp;
3109{
3110 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3111}
3112
3113/*
3114 * Compute the minimal height for frame "topfrp".
3115 * Uses the 'winminheight' option.
3116 * When "next_curwin" isn't NULL, use p_wh for this window.
3117 * When "next_curwin" is NOWIN, don't use at least one line for the current
3118 * window.
3119 */
3120 static int
3121frame_minheight(topfrp, next_curwin)
3122 frame_T *topfrp;
3123 win_T *next_curwin;
3124{
3125 frame_T *frp;
3126 int m;
3127#ifdef FEAT_VERTSPLIT
3128 int n;
3129#endif
3130
3131 if (topfrp->fr_win != NULL)
3132 {
3133 if (topfrp->fr_win == next_curwin)
3134 m = p_wh + topfrp->fr_win->w_status_height;
3135 else
3136 {
3137 /* window: minimal height of the window plus status line */
3138 m = p_wmh + topfrp->fr_win->w_status_height;
3139 /* Current window is minimal one line high */
3140 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3141 ++m;
3142 }
3143 }
3144#ifdef FEAT_VERTSPLIT
3145 else if (topfrp->fr_layout == FR_ROW)
3146 {
3147 /* get the minimal height from each frame in this row */
3148 m = 0;
3149 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3150 {
3151 n = frame_minheight(frp, next_curwin);
3152 if (n > m)
3153 m = n;
3154 }
3155 }
3156#endif
3157 else
3158 {
3159 /* Add up the minimal heights for all frames in this column. */
3160 m = 0;
3161 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3162 m += frame_minheight(frp, next_curwin);
3163 }
3164
3165 return m;
3166}
3167
3168#ifdef FEAT_VERTSPLIT
3169/*
3170 * Compute the minimal width for frame "topfrp".
3171 * When "next_curwin" isn't NULL, use p_wiw for this window.
3172 * When "next_curwin" is NOWIN, don't use at least one column for the current
3173 * window.
3174 */
3175 static int
3176frame_minwidth(topfrp, next_curwin)
3177 frame_T *topfrp;
3178 win_T *next_curwin; /* use p_wh and p_wiw for next_curwin */
3179{
3180 frame_T *frp;
3181 int m, n;
3182
3183 if (topfrp->fr_win != NULL)
3184 {
3185 if (topfrp->fr_win == next_curwin)
3186 m = p_wiw + topfrp->fr_win->w_vsep_width;
3187 else
3188 {
3189 /* window: minimal width of the window plus separator column */
3190 m = p_wmw + topfrp->fr_win->w_vsep_width;
3191 /* Current window is minimal one column wide */
3192 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3193 ++m;
3194 }
3195 }
3196 else if (topfrp->fr_layout == FR_COL)
3197 {
3198 /* get the minimal width from each frame in this column */
3199 m = 0;
3200 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3201 {
3202 n = frame_minwidth(frp, next_curwin);
3203 if (n > m)
3204 m = n;
3205 }
3206 }
3207 else
3208 {
3209 /* Add up the minimal widths for all frames in this row. */
3210 m = 0;
3211 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3212 m += frame_minwidth(frp, next_curwin);
3213 }
3214
3215 return m;
3216}
3217#endif
3218
3219
3220/*
3221 * Try to close all windows except current one.
3222 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3223 * used and the buffer was modified.
3224 *
3225 * Used by ":bdel" and ":only".
3226 */
3227 void
3228close_others(message, forceit)
3229 int message;
3230 int forceit; /* always hide all other windows */
3231{
3232 win_T *wp;
3233 win_T *nextwp;
3234 int r;
3235
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003236 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 {
3238 if (message
3239#ifdef FEAT_AUTOCMD
3240 && !autocmd_busy
3241#endif
3242 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003243 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 return;
3245 }
3246
3247 /* Be very careful here: autocommands may change the window layout. */
3248 for (wp = firstwin; win_valid(wp); wp = nextwp)
3249 {
3250 nextwp = wp->w_next;
3251 if (wp != curwin) /* don't close current window */
3252 {
3253
3254 /* Check if it's allowed to abandon this window */
3255 r = can_abandon(wp->w_buffer, forceit);
3256#ifdef FEAT_AUTOCMD
3257 if (!win_valid(wp)) /* autocommands messed wp up */
3258 {
3259 nextwp = firstwin;
3260 continue;
3261 }
3262#endif
3263 if (!r)
3264 {
3265#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3266 if (message && (p_confirm || cmdmod.confirm) && p_write)
3267 {
3268 dialog_changed(wp->w_buffer, FALSE);
3269# ifdef FEAT_AUTOCMD
3270 if (!win_valid(wp)) /* autocommands messed wp up */
3271 {
3272 nextwp = firstwin;
3273 continue;
3274 }
3275# endif
3276 }
3277 if (bufIsChanged(wp->w_buffer))
3278#endif
3279 continue;
3280 }
3281 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3282 }
3283 }
3284
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003285 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 EMSG(_("E445: Other window contains changes"));
3287}
3288
3289#endif /* FEAT_WINDOWS */
3290
3291/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003292 * Init the current window "curwin".
3293 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 */
3295 void
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003296curwin_init()
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003298 win_init_empty(curwin);
3299}
3300
3301 void
3302win_init_empty(wp)
3303 win_T *wp;
3304{
3305 redraw_win_later(wp, NOT_VALID);
3306 wp->w_lines_valid = 0;
3307 wp->w_cursor.lnum = 1;
3308 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003310 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003312 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3313 wp->w_pcmark.col = 0;
3314 wp->w_prev_pcmark.lnum = 0;
3315 wp->w_prev_pcmark.col = 0;
3316 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003318 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003320 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003322 if (wp->w_p_rl)
3323 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003325 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003327#ifdef FEAT_SYN_HL
3328 wp->w_s = &wp->w_buffer->b_s;
3329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330}
3331
3332/*
3333 * Allocate the first window and put an empty buffer in it.
3334 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003335 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003337 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338win_alloc_first()
3339{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003340 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003341 return FAIL;
3342
3343#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003344 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003345 if (first_tabpage == NULL)
3346 return FAIL;
3347 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003348 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003349#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003350
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003351 return OK;
3352}
3353
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003354#if defined(FEAT_AUTOCMD) || defined(PROTO)
3355/*
3356 * Init "aucmd_win". This can only be done after the first
3357 * window is fully initialized, thus it can't be in win_alloc_first().
3358 */
3359 void
3360win_alloc_aucmd_win()
3361{
3362 aucmd_win = win_alloc(NULL, TRUE);
3363 if (aucmd_win != NULL)
3364 {
3365 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003366 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003367 new_frame(aucmd_win);
3368 }
3369}
3370#endif
3371
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003372/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003373 * Allocate the first window or the first window in a new tab page.
3374 * When "oldwin" is NULL create an empty buffer for it.
3375 * When "oldwin" is not NULL copy info from it to the new window (only with
3376 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003377 * Return FAIL when something goes wrong (out of memory).
3378 */
3379 static int
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003380win_alloc_firstwin(oldwin)
3381 win_T *oldwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003382{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003383 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003384 if (oldwin == NULL)
3385 {
3386 /* Very first window, need to create an empty buffer for it and
3387 * initialize from scratch. */
3388 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3389 if (curwin == NULL || curbuf == NULL)
3390 return FAIL;
3391 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003392#ifdef FEAT_SYN_HL
3393 curwin->w_s = &(curbuf->b_s);
3394#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003395 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003397 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003399 curwin_init(); /* init current window */
3400 }
3401#ifdef FEAT_WINDOWS
3402 else
3403 {
3404 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003405 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003406
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003407 /* We don't want cursor- and scroll-binding in the first window. */
3408 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003409 }
3410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003412 new_frame(curwin);
3413 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003414 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003415 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#ifdef FEAT_VERTSPLIT
3417 topframe->fr_width = Columns;
3418#endif
3419 topframe->fr_height = Rows - p_ch;
3420 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421
3422 return OK;
3423}
3424
3425/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003426 * Create a frame for window "wp".
3427 */
3428 static void
3429new_frame(win_T *wp)
3430{
3431 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3432
3433 wp->w_frame = frp;
3434 if (frp != NULL)
3435 {
3436 frp->fr_layout = FR_LEAF;
3437 frp->fr_win = wp;
3438 }
3439}
3440
3441/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003442 * Initialize the window and frame size to the maximum.
3443 */
3444 void
3445win_init_size()
3446{
3447 firstwin->w_height = ROWS_AVAIL;
3448 topframe->fr_height = ROWS_AVAIL;
3449#ifdef FEAT_VERTSPLIT
3450 firstwin->w_width = Columns;
3451 topframe->fr_width = Columns;
3452#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453}
3454
3455#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003456
3457/*
3458 * Allocate a new tabpage_T and init the values.
3459 * Returns NULL when out of memory.
3460 */
3461 static tabpage_T *
3462alloc_tabpage()
3463{
3464 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003465# ifdef FEAT_GUI
3466 int i;
3467# endif
3468
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003469
3470 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003471 if (tp == NULL)
3472 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003473
Bram Moolenaar429fa852013-04-15 12:27:36 +02003474# ifdef FEAT_EVAL
3475 /* init t: variables */
3476 tp->tp_vars = dict_alloc();
3477 if (tp->tp_vars == NULL)
3478 {
3479 vim_free(tp);
3480 return NULL;
3481 }
3482 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3483# endif
3484
3485# ifdef FEAT_GUI
3486 for (i = 0; i < 3; i++)
3487 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003488# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003489# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003490 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003491# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003492 tp->tp_ch_used = p_ch;
3493
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003494 return tp;
3495}
3496
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003497 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003498free_tabpage(tp)
3499 tabpage_T *tp;
3500{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003501 int idx;
3502
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003503# ifdef FEAT_DIFF
3504 diff_clear(tp);
3505# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003506 for (idx = 0; idx < SNAP_COUNT; ++idx)
3507 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003508#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003509 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3510 hash_init(&tp->tp_vars->dv_hashtab);
3511 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003512#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003513
3514#ifdef FEAT_PYTHON
3515 python_tabpage_free(tp);
3516#endif
3517
3518#ifdef FEAT_PYTHON3
3519 python3_tabpage_free(tp);
3520#endif
3521
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003522 vim_free(tp);
3523}
3524
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003525/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003526 * Create a new Tab page with one window.
3527 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003528 * When "after" is 0 put it just after the current Tab page.
3529 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003530 * Return FAIL or OK.
3531 */
3532 int
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003533win_new_tabpage(after)
3534 int after;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003535{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003536 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003537 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003538 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003539
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003540 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003541 if (newtp == NULL)
3542 return FAIL;
3543
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003544 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003545 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003546 {
3547 vim_free(newtp);
3548 return FAIL;
3549 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003550 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003551
3552 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003553 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003554 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003555 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003556 if (after == 1)
3557 {
3558 /* New tab page becomes the first one. */
3559 newtp->tp_next = first_tabpage;
3560 first_tabpage = newtp;
3561 }
3562 else
3563 {
3564 if (after > 0)
3565 {
3566 /* Put new tab page before tab page "after". */
3567 n = 2;
3568 for (tp = first_tabpage; tp->tp_next != NULL
3569 && n < after; tp = tp->tp_next)
3570 ++n;
3571 }
3572 newtp->tp_next = tp->tp_next;
3573 tp->tp_next = newtp;
3574 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003575 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003576 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003577 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003578
3579 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003580 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003581
3582#if defined(FEAT_GUI)
3583 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3584 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003585 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003586#endif
3587
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003588 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003589#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003590 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003591 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003592#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003593 return OK;
3594 }
3595
3596 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003597 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003598 return FAIL;
3599}
3600
3601/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003602 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3603 * like with ":split".
3604 * Returns OK if a new tab page was created, FAIL otherwise.
3605 */
3606 int
3607may_open_tabpage()
3608{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003609 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003610
Bram Moolenaard326ce82007-03-11 14:48:29 +00003611 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003612 {
3613 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003614 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003615 return win_new_tabpage(n);
3616 }
3617 return FAIL;
3618}
3619
3620/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003621 * Create up to "maxcount" tabpages with empty windows.
3622 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003623 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003624 int
3625make_tabpages(maxcount)
3626 int maxcount;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003627{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003628 int count = maxcount;
3629 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003630
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003631 /* Limit to 'tabpagemax' tabs. */
3632 if (count > p_tpm)
3633 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003634
3635#ifdef FEAT_AUTOCMD
3636 /*
3637 * Don't execute autocommands while creating the tab pages. Must do that
3638 * when putting the buffers in the windows.
3639 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003640 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003641#endif
3642
3643 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003644 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003646
3647#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003648 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003649#endif
3650
3651 /* return actual number of tab pages */
3652 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003653}
3654
3655/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003656 * Return TRUE when "tpc" points to a valid tab page.
3657 */
3658 int
3659valid_tabpage(tpc)
3660 tabpage_T *tpc;
3661{
3662 tabpage_T *tp;
3663
3664 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3665 if (tp == tpc)
3666 return TRUE;
3667 return FALSE;
3668}
3669
3670/*
3671 * Find tab page "n" (first one is 1). Returns NULL when not found.
3672 */
3673 tabpage_T *
3674find_tabpage(n)
3675 int n;
3676{
3677 tabpage_T *tp;
3678 int i = 1;
3679
3680 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3681 ++i;
3682 return tp;
3683}
3684
3685/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003686 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003687 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003688 */
3689 int
3690tabpage_index(ftp)
3691 tabpage_T *ftp;
3692{
3693 int i = 1;
3694 tabpage_T *tp;
3695
3696 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3697 ++i;
3698 return i;
3699}
3700
3701/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003702 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003703 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003704 * FAIL.
3705 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003706 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003707 static int
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003708leave_tabpage(new_curbuf, trigger_leave_autocmds)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003709 buf_T *new_curbuf UNUSED; /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003710 NULL if unknown */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003711 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003713 tabpage_T *tp = curtab;
3714
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003715#ifdef FEAT_VISUAL
3716 reset_VIsual_and_resel(); /* stop Visual mode */
3717#endif
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003718#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003719 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003720 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003721 if (new_curbuf != curbuf)
3722 {
3723 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3724 if (curtab != tp)
3725 return FAIL;
3726 }
3727 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3728 if (curtab != tp)
3729 return FAIL;
3730 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003731 if (curtab != tp)
3732 return FAIL;
3733 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003734#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003735#if defined(FEAT_GUI)
3736 /* Remove the scrollbars. They may be added back later. */
3737 if (gui.in_use)
3738 gui_remove_scrollbars();
3739#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003740 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003741 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003742 tp->tp_firstwin = firstwin;
3743 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003744 tp->tp_old_Rows = Rows;
3745 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003746 firstwin = NULL;
3747 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003748 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749}
3750
3751/*
3752 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003753 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003754 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3755 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003756 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003757 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003758enter_tabpage(tp, old_curbuf, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003759 tabpage_T *tp;
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00003760 buf_T *old_curbuf UNUSED;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003761 int trigger_enter_autocmds UNUSED;
3762 int trigger_leave_autocmds UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003764 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003765 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003766
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003767 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003768 firstwin = tp->tp_firstwin;
3769 lastwin = tp->tp_lastwin;
3770 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003771
3772 /* We would like doing the TabEnter event first, but we don't have a
3773 * valid current window yet, which may break some commands.
3774 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003775 win_enter_ext(tp->tp_curwin, FALSE, TRUE, trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003776 prevwin = next_prevwin;
3777
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003778 last_status(FALSE); /* status line may appear or disappear */
3779 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003780 must_redraw = CLEAR; /* need to redraw everything */
3781#ifdef FEAT_DIFF
3782 diff_need_scrollbind = TRUE;
3783#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003784
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003785 /* The tabpage line may have appeared or disappeared, may need to resize
3786 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003787 * frame sizes too. Use the stored value of p_ch, so that it can be
3788 * different for each tab page. */
3789 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003790 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3791#ifdef FEAT_GUI_TABLINE
3792 && !gui_use_tabline()
3793#endif
3794 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003795 shell_new_rows();
3796#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003797 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003798 shell_new_columns(); /* update window widths */
3799#endif
3800
3801#if defined(FEAT_GUI)
3802 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3803 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003804 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003805#endif
3806
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003807#ifdef FEAT_AUTOCMD
3808 /* Apply autocommands after updating the display, when 'rows' and
3809 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003810 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003811 {
3812 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3813 if (old_curbuf != curbuf)
3814 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3815 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003816#endif
3817
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003818 redraw_all_later(CLEAR);
3819}
3820
3821/*
3822 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003823 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003824 */
3825 void
3826goto_tabpage(n)
3827 int n;
3828{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003829 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003830 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831 int i;
3832
Bram Moolenaard68071d2006-05-02 22:08:30 +00003833 if (text_locked())
3834 {
3835 /* Not allowed when editing the command line. */
3836#ifdef FEAT_CMDWIN
3837 if (cmdwin_type != 0)
3838 EMSG(_(e_cmdwin));
3839 else
3840#endif
3841 EMSG(_(e_secure));
3842 return;
3843 }
3844
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003845 /* If there is only one it can't work. */
3846 if (first_tabpage->tp_next == NULL)
3847 {
3848 if (n > 1)
3849 beep_flush();
3850 return;
3851 }
3852
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003853 if (n == 0)
3854 {
3855 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003856 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003857 tp = first_tabpage;
3858 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003859 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003860 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003861 else if (n < 0)
3862 {
3863 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3864 * this N times. */
3865 ttp = curtab;
3866 for (i = n; i < 0; ++i)
3867 {
3868 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3869 tp = tp->tp_next)
3870 ;
3871 ttp = tp;
3872 }
3873 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003874 else if (n == 9999)
3875 {
3876 /* Go to last tab page. */
3877 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3878 ;
3879 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003880 else
3881 {
3882 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003883 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003884 if (tp == NULL)
3885 {
3886 beep_flush();
3887 return;
3888 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003889 }
3890
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003891 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003892
3893#ifdef FEAT_GUI_TABLINE
3894 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003895 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003896#endif
3897}
3898
3899/*
3900 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003901 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3902 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003903 * Note: doesn't update the GUI tab.
3904 */
3905 void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003906goto_tabpage_tp(tp, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003907 tabpage_T *tp;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003908 int trigger_enter_autocmds;
3909 int trigger_leave_autocmds;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003910{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003911 /* Don't repeat a message in another tab page. */
3912 set_keep_msg(NULL, 0);
3913
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003914 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3915 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003916 {
3917 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003918 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3919 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003920 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003921 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3922 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003923 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003924}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003927 * Enter window "wp" in tab page "tp".
3928 * Also updates the GUI tab.
3929 */
3930 void
3931goto_tabpage_win(tp, wp)
3932 tabpage_T *tp;
3933 win_T *wp;
3934{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003935 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003936 if (curtab == tp && win_valid(wp))
3937 {
3938 win_enter(wp, TRUE);
3939# ifdef FEAT_GUI_TABLINE
3940 if (gui_use_tabline())
3941 gui_mch_set_curtab(tabpage_index(curtab));
3942# endif
3943 }
3944}
3945
3946/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003947 * Move the current tab page to before tab page "nr".
3948 */
3949 void
3950tabpage_move(nr)
3951 int nr;
3952{
3953 int n = nr;
3954 tabpage_T *tp;
3955
3956 if (first_tabpage->tp_next == NULL)
3957 return;
3958
3959 /* Remove the current tab page from the list of tab pages. */
3960 if (curtab == first_tabpage)
3961 first_tabpage = curtab->tp_next;
3962 else
3963 {
3964 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3965 if (tp->tp_next == curtab)
3966 break;
3967 if (tp == NULL) /* "cannot happen" */
3968 return;
3969 tp->tp_next = curtab->tp_next;
3970 }
3971
3972 /* Re-insert it at the specified position. */
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02003973 if (n <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003974 {
3975 curtab->tp_next = first_tabpage;
3976 first_tabpage = curtab;
3977 }
3978 else
3979 {
3980 for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3981 --n;
3982 curtab->tp_next = tp->tp_next;
3983 tp->tp_next = curtab;
3984 }
3985
3986 /* Need to redraw the tabline. Tab page contents doesn't change. */
3987 redraw_tabline = TRUE;
3988}
3989
3990
3991/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 * Go to another window.
3993 * When jumping to another buffer, stop Visual mode. Do this before
3994 * changing windows so we can yank the selection into the '*' register.
3995 * When jumping to another window on the same buffer, adjust its cursor
3996 * position to keep the same Visual area.
3997 */
3998 void
3999win_goto(wp)
4000 win_T *wp;
4001{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004002#ifdef FEAT_CONCEAL
4003 win_T *owp = curwin;
4004#endif
4005
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004006 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
4008 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004009 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return;
4011 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004012#ifdef FEAT_AUTOCMD
4013 if (curbuf_locked())
4014 return;
4015#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017#ifdef FEAT_VISUAL
4018 if (wp->w_buffer != curbuf)
4019 reset_VIsual_and_resel();
4020 else if (VIsual_active)
4021 wp->w_cursor = curwin->w_cursor;
4022#endif
4023
4024#ifdef FEAT_GUI
4025 need_mouse_correct = TRUE;
4026#endif
4027 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004028
4029#ifdef FEAT_CONCEAL
4030 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004031 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004032 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004033 if (curwin->w_p_cole > 0 && !msg_scrolled)
4034 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036}
4037
4038#if defined(FEAT_PERL) || defined(PROTO)
4039/*
4040 * Find window number "winnr" (counting top to bottom).
4041 */
4042 win_T *
4043win_find_nr(winnr)
4044 int winnr;
4045{
4046 win_T *wp;
4047
4048# ifdef FEAT_WINDOWS
4049 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4050 if (--winnr == 0)
4051 break;
4052 return wp;
4053# else
4054 return curwin;
4055# endif
4056}
4057#endif
4058
4059#ifdef FEAT_VERTSPLIT
4060/*
4061 * Move to window above or below "count" times.
4062 */
4063 static void
4064win_goto_ver(up, count)
4065 int up; /* TRUE to go to win above */
4066 long count;
4067{
4068 frame_T *fr;
4069 frame_T *nfr;
4070 frame_T *foundfr;
4071
4072 foundfr = curwin->w_frame;
4073 while (count--)
4074 {
4075 /*
4076 * First go upwards in the tree of frames until we find a upwards or
4077 * downwards neighbor.
4078 */
4079 fr = foundfr;
4080 for (;;)
4081 {
4082 if (fr == topframe)
4083 goto end;
4084 if (up)
4085 nfr = fr->fr_prev;
4086 else
4087 nfr = fr->fr_next;
4088 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4089 break;
4090 fr = fr->fr_parent;
4091 }
4092
4093 /*
4094 * Now go downwards to find the bottom or top frame in it.
4095 */
4096 for (;;)
4097 {
4098 if (nfr->fr_layout == FR_LEAF)
4099 {
4100 foundfr = nfr;
4101 break;
4102 }
4103 fr = nfr->fr_child;
4104 if (nfr->fr_layout == FR_ROW)
4105 {
4106 /* Find the frame at the cursor row. */
4107 while (fr->fr_next != NULL
4108 && frame2win(fr)->w_wincol + fr->fr_width
4109 <= curwin->w_wincol + curwin->w_wcol)
4110 fr = fr->fr_next;
4111 }
4112 if (nfr->fr_layout == FR_COL && up)
4113 while (fr->fr_next != NULL)
4114 fr = fr->fr_next;
4115 nfr = fr;
4116 }
4117 }
4118end:
4119 if (foundfr != NULL)
4120 win_goto(foundfr->fr_win);
4121}
4122
4123/*
4124 * Move to left or right window.
4125 */
4126 static void
4127win_goto_hor(left, count)
4128 int left; /* TRUE to go to left win */
4129 long count;
4130{
4131 frame_T *fr;
4132 frame_T *nfr;
4133 frame_T *foundfr;
4134
4135 foundfr = curwin->w_frame;
4136 while (count--)
4137 {
4138 /*
4139 * First go upwards in the tree of frames until we find a left or
4140 * right neighbor.
4141 */
4142 fr = foundfr;
4143 for (;;)
4144 {
4145 if (fr == topframe)
4146 goto end;
4147 if (left)
4148 nfr = fr->fr_prev;
4149 else
4150 nfr = fr->fr_next;
4151 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4152 break;
4153 fr = fr->fr_parent;
4154 }
4155
4156 /*
4157 * Now go downwards to find the leftmost or rightmost frame in it.
4158 */
4159 for (;;)
4160 {
4161 if (nfr->fr_layout == FR_LEAF)
4162 {
4163 foundfr = nfr;
4164 break;
4165 }
4166 fr = nfr->fr_child;
4167 if (nfr->fr_layout == FR_COL)
4168 {
4169 /* Find the frame at the cursor row. */
4170 while (fr->fr_next != NULL
4171 && frame2win(fr)->w_winrow + fr->fr_height
4172 <= curwin->w_winrow + curwin->w_wrow)
4173 fr = fr->fr_next;
4174 }
4175 if (nfr->fr_layout == FR_ROW && left)
4176 while (fr->fr_next != NULL)
4177 fr = fr->fr_next;
4178 nfr = fr;
4179 }
4180 }
4181end:
4182 if (foundfr != NULL)
4183 win_goto(foundfr->fr_win);
4184}
4185#endif
4186
4187/*
4188 * Make window "wp" the current window.
4189 */
4190 void
4191win_enter(wp, undo_sync)
4192 win_T *wp;
4193 int undo_sync;
4194{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004195 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196}
4197
4198/*
4199 * Make window wp the current window.
4200 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4201 * been closed and isn't valid.
4202 */
4203 static void
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004204win_enter_ext(wp, undo_sync, curwin_invalid, trigger_enter_autocmds, trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 win_T *wp;
4206 int undo_sync;
4207 int curwin_invalid;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004208 int trigger_enter_autocmds UNUSED;
4209 int trigger_leave_autocmds UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211#ifdef FEAT_AUTOCMD
4212 int other_buffer = FALSE;
4213#endif
4214
4215 if (wp == curwin && !curwin_invalid) /* nothing to do */
4216 return;
4217
4218#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004219 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
4221 /*
4222 * Be careful: If autocommands delete the window, return now.
4223 */
4224 if (wp->w_buffer != curbuf)
4225 {
4226 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4227 other_buffer = TRUE;
4228 if (!win_valid(wp))
4229 return;
4230 }
4231 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4232 if (!win_valid(wp))
4233 return;
4234# ifdef FEAT_EVAL
4235 /* autocmds may abort script processing */
4236 if (aborting())
4237 return;
4238# endif
4239 }
4240#endif
4241
4242 /* sync undo before leaving the current buffer */
4243 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004244 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 /* may have to copy the buffer options when 'cpo' contains 'S' */
4246 if (wp->w_buffer != curbuf)
4247 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4248 if (!curwin_invalid)
4249 {
4250 prevwin = curwin; /* remember for CTRL-W p */
4251 curwin->w_redr_status = TRUE;
4252 }
4253 curwin = wp;
4254 curbuf = wp->w_buffer;
4255 check_cursor();
4256#ifdef FEAT_VIRTUALEDIT
4257 if (!virtual_active())
4258 curwin->w_cursor.coladd = 0;
4259#endif
4260 changed_line_abv_curs(); /* assume cursor position needs updating */
4261
4262 if (curwin->w_localdir != NULL)
4263 {
4264 /* Window has a local directory: Save current directory as global
4265 * directory (unless that was done already) and change to the local
4266 * directory. */
4267 if (globaldir == NULL)
4268 {
4269 char_u cwd[MAXPATHL];
4270
4271 if (mch_dirname(cwd, MAXPATHL) == OK)
4272 globaldir = vim_strsave(cwd);
4273 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004274 if (mch_chdir((char *)curwin->w_localdir) == 0)
4275 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 else if (globaldir != NULL)
4278 {
4279 /* Window doesn't have a local directory and we are not in the global
4280 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004281 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 vim_free(globaldir);
4283 globaldir = NULL;
4284 shorten_fnames(TRUE);
4285 }
4286
4287#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004288 if (trigger_enter_autocmds)
4289 {
4290 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4291 if (other_buffer)
4292 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294#endif
4295
4296#ifdef FEAT_TITLE
4297 maketitle();
4298#endif
4299 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004300 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 if (restart_edit)
4302 redraw_later(VALID); /* causes status line redraw */
4303
4304 /* set window height to desired minimal value */
4305 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4306 win_setheight((int)p_wh);
4307 else if (curwin->w_height == 0)
4308 win_setheight(1);
4309
4310#ifdef FEAT_VERTSPLIT
4311 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004312 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 win_setwidth((int)p_wiw);
4314#endif
4315
4316#ifdef FEAT_MOUSE
4317 setmouse(); /* in case jumped to/from help buffer */
4318#endif
4319
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004320 /* Change directories when the 'acd' option is set. */
4321 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322}
4323
4324#endif /* FEAT_WINDOWS */
4325
4326#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4327/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004328 * Jump to the first open window that contains buffer "buf", if one exists.
4329 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 */
4331 win_T *
4332buf_jump_open_win(buf)
4333 buf_T *buf;
4334{
4335# ifdef FEAT_WINDOWS
4336 win_T *wp;
4337
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004338 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 if (wp->w_buffer == buf)
4340 break;
4341 if (wp != NULL)
4342 win_enter(wp, FALSE);
4343 return wp;
4344# else
4345 if (curwin->w_buffer == buf)
4346 return curwin;
4347 return NULL;
4348# endif
4349}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004350
4351/*
4352 * Jump to the first open window in any tab page that contains buffer "buf",
4353 * if one exists.
4354 * Returns a pointer to the window found, otherwise NULL.
4355 */
4356 win_T *
4357buf_jump_open_tab(buf)
4358 buf_T *buf;
4359{
4360# ifdef FEAT_WINDOWS
4361 win_T *wp;
4362 tabpage_T *tp;
4363
4364 /* First try the current tab page. */
4365 wp = buf_jump_open_win(buf);
4366 if (wp != NULL)
4367 return wp;
4368
4369 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4370 if (tp != curtab)
4371 {
4372 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4373 if (wp->w_buffer == buf)
4374 break;
4375 if (wp != NULL)
4376 {
4377 goto_tabpage_win(tp, wp);
4378 if (curwin != wp)
4379 wp = NULL; /* something went wrong */
4380 break;
4381 }
4382 }
4383
4384 return wp;
4385# else
4386 if (curwin->w_buffer == buf)
4387 return curwin;
4388 return NULL;
4389# endif
4390}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391#endif
4392
4393/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004394 * Allocate a window structure and link it in the window list when "hidden" is
4395 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 static win_T *
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004398win_alloc(after, hidden)
Bram Moolenaar4bdbbf72009-05-21 21:27:43 +00004399 win_T *after UNUSED;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004400 int hidden UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004402 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403
4404 /*
4405 * allocate window structure and linesizes arrays
4406 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004407 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004408 if (new_wp == NULL)
4409 return NULL;
4410
4411 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004413 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004414 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416
Bram Moolenaar429fa852013-04-15 12:27:36 +02004417#ifdef FEAT_EVAL
4418 /* init w: variables */
4419 new_wp->w_vars = dict_alloc();
4420 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004422 win_free_lsize(new_wp);
4423 vim_free(new_wp);
4424 return NULL;
4425 }
4426 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004427#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004428
4429#ifdef FEAT_AUTOCMD
4430 /* Don't execute autocommands while the window is not properly
4431 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4432 * event. */
4433 block_autocmds();
4434#endif
4435 /*
4436 * link the window in the window list
4437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004439 if (!hidden)
4440 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441#endif
4442#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004443 new_wp->w_wincol = 0;
4444 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445#endif
4446
Bram Moolenaar429fa852013-04-15 12:27:36 +02004447 /* position the display and the cursor at the top of the file. */
4448 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004450 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004452 new_wp->w_botline = 2;
4453 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004455 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456#endif
4457
Bram Moolenaar429fa852013-04-15 12:27:36 +02004458 /* We won't calculate w_fraction until resizing the window */
4459 new_wp->w_fraction = 0;
4460 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004463 if (gui.in_use)
4464 {
4465 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4466 SBAR_LEFT, new_wp);
4467 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4468 SBAR_RIGHT, new_wp);
4469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#endif
4471#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004472 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004474#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004475 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004476#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004477#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004478 new_wp->w_match_head = NULL;
4479 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004480#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004481 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482}
4483
4484#if defined(FEAT_WINDOWS) || defined(PROTO)
4485
4486/*
4487 * remove window 'wp' from the window list and free the structure
4488 */
4489 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004490win_free(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004492 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493{
4494 int i;
4495
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004496#ifdef FEAT_FOLDING
4497 clearFolding(wp);
4498#endif
4499
4500 /* reduce the reference count to the argument list. */
4501 alist_unlink(wp->w_alist);
4502
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004503#ifdef FEAT_AUTOCMD
4504 /* Don't execute autocommands while the window is halfway being deleted.
4505 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004506 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004507#endif
4508
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004509#ifdef FEAT_LUA
4510 lua_window_free(wp);
4511#endif
4512
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004513#ifdef FEAT_MZSCHEME
4514 mzscheme_window_free(wp);
4515#endif
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#ifdef FEAT_PERL
4518 perl_win_free(wp);
4519#endif
4520
4521#ifdef FEAT_PYTHON
4522 python_window_free(wp);
4523#endif
4524
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004525#ifdef FEAT_PYTHON3
4526 python3_window_free(wp);
4527#endif
4528
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529#ifdef FEAT_TCL
4530 tcl_window_free(wp);
4531#endif
4532
4533#ifdef FEAT_RUBY
4534 ruby_window_free(wp);
4535#endif
4536
4537 clear_winopt(&wp->w_onebuf_opt);
4538 clear_winopt(&wp->w_allbuf_opt);
4539
4540#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004541 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4542 hash_init(&wp->w_vars->dv_hashtab);
4543 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544#endif
4545
4546 if (prevwin == wp)
4547 prevwin = NULL;
4548 win_free_lsize(wp);
4549
4550 for (i = 0; i < wp->w_tagstacklen; ++i)
4551 vim_free(wp->w_tagstack[i].tagname);
4552
4553 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004556 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559#ifdef FEAT_JUMPLIST
4560 free_jumplist(wp);
4561#endif
4562
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004563#ifdef FEAT_QUICKFIX
4564 qf_free_all(wp);
4565#endif
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567#ifdef FEAT_GUI
4568 if (gui.in_use)
4569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4571 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4572 }
4573#endif /* FEAT_GUI */
4574
Bram Moolenaar860cae12010-06-05 23:22:07 +02004575#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004576 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004577#endif
4578
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004579#ifdef FEAT_AUTOCMD
4580 if (wp != aucmd_win)
4581#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004582 win_remove(wp, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004584
4585#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004586 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588}
4589
4590/*
4591 * Append window "wp" in the window list after window "after".
4592 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004593 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594win_append(after, wp)
4595 win_T *after, *wp;
4596{
4597 win_T *before;
4598
4599 if (after == NULL) /* after NULL is in front of the first */
4600 before = firstwin;
4601 else
4602 before = after->w_next;
4603
4604 wp->w_next = before;
4605 wp->w_prev = after;
4606 if (after == NULL)
4607 firstwin = wp;
4608 else
4609 after->w_next = wp;
4610 if (before == NULL)
4611 lastwin = wp;
4612 else
4613 before->w_prev = wp;
4614}
4615
4616/*
4617 * Remove a window from the window list.
4618 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004619 void
Bram Moolenaarf740b292006-02-16 22:11:02 +00004620win_remove(wp, tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004622 tabpage_T *tp; /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 if (wp->w_prev != NULL)
4625 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004626 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004628 else
4629 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (wp->w_next != NULL)
4631 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004632 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004634 else
4635 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636}
4637
4638/*
4639 * Append frame "frp" in a frame list after frame "after".
4640 */
4641 static void
4642frame_append(after, frp)
4643 frame_T *after, *frp;
4644{
4645 frp->fr_next = after->fr_next;
4646 after->fr_next = frp;
4647 if (frp->fr_next != NULL)
4648 frp->fr_next->fr_prev = frp;
4649 frp->fr_prev = after;
4650}
4651
4652/*
4653 * Insert frame "frp" in a frame list before frame "before".
4654 */
4655 static void
4656frame_insert(before, frp)
4657 frame_T *before, *frp;
4658{
4659 frp->fr_next = before;
4660 frp->fr_prev = before->fr_prev;
4661 before->fr_prev = frp;
4662 if (frp->fr_prev != NULL)
4663 frp->fr_prev->fr_next = frp;
4664 else
4665 frp->fr_parent->fr_child = frp;
4666}
4667
4668/*
4669 * Remove a frame from a frame list.
4670 */
4671 static void
4672frame_remove(frp)
4673 frame_T *frp;
4674{
4675 if (frp->fr_prev != NULL)
4676 frp->fr_prev->fr_next = frp->fr_next;
4677 else
4678 frp->fr_parent->fr_child = frp->fr_next;
4679 if (frp->fr_next != NULL)
4680 frp->fr_next->fr_prev = frp->fr_prev;
4681}
4682
4683#endif /* FEAT_WINDOWS */
4684
4685/*
4686 * Allocate w_lines[] for window "wp".
4687 * Return FAIL for failure, OK for success.
4688 */
4689 int
4690win_alloc_lines(wp)
4691 win_T *wp;
4692{
4693 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004694 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 if (wp->w_lines == NULL)
4696 return FAIL;
4697 return OK;
4698}
4699
4700/*
4701 * free lsize arrays for a window
4702 */
4703 void
4704win_free_lsize(wp)
4705 win_T *wp;
4706{
4707 vim_free(wp->w_lines);
4708 wp->w_lines = NULL;
4709}
4710
4711/*
4712 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004713 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 */
4715 void
4716shell_new_rows()
4717{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004718 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720 if (firstwin == NULL) /* not initialized yet */
4721 return;
4722#ifdef FEAT_WINDOWS
4723 if (h < frame_minheight(topframe, NULL))
4724 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004725
4726 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 * that doesn't result in the right height, forget about that option. */
4728 frame_new_height(topframe, h, FALSE, TRUE);
4729 if (topframe->fr_height != h)
4730 frame_new_height(topframe, h, FALSE, FALSE);
4731
4732 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4733#else
4734 if (h < 1)
4735 h = 1;
4736 win_new_height(firstwin, h);
4737#endif
4738 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004739#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004740 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004741#endif
4742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743#if 0
4744 /* Disabled: don't want making the screen smaller make a window larger. */
4745 if (p_ea)
4746 win_equal(curwin, FALSE, 'v');
4747#endif
4748}
4749
4750#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4751/*
4752 * Called from win_new_shellsize() after Columns changed.
4753 */
4754 void
4755shell_new_columns()
4756{
4757 if (firstwin == NULL) /* not initialized yet */
4758 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004759
4760 /* First try setting the widths of windows with 'winfixwidth'. If that
4761 * doesn't result in the right width, forget about that option. */
4762 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4763 if (topframe->fr_width != Columns)
4764 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4767#if 0
4768 /* Disabled: don't want making the screen smaller make a window larger. */
4769 if (p_ea)
4770 win_equal(curwin, FALSE, 'h');
4771#endif
4772}
4773#endif
4774
4775#if defined(FEAT_CMDWIN) || defined(PROTO)
4776/*
4777 * Save the size of all windows in "gap".
4778 */
4779 void
4780win_size_save(gap)
4781 garray_T *gap;
4782
4783{
4784 win_T *wp;
4785
4786 ga_init2(gap, (int)sizeof(int), 1);
4787 if (ga_grow(gap, win_count() * 2) == OK)
4788 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4789 {
4790 ((int *)gap->ga_data)[gap->ga_len++] =
4791 wp->w_width + wp->w_vsep_width;
4792 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4793 }
4794}
4795
4796/*
4797 * Restore window sizes, but only if the number of windows is still the same.
4798 * Does not free the growarray.
4799 */
4800 void
4801win_size_restore(gap)
4802 garray_T *gap;
4803{
4804 win_T *wp;
4805 int i;
4806
4807 if (win_count() * 2 == gap->ga_len)
4808 {
4809 i = 0;
4810 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4811 {
4812 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4813 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4814 }
4815 /* recompute the window positions */
4816 (void)win_comp_pos();
4817 }
4818}
4819#endif /* FEAT_CMDWIN */
4820
4821#if defined(FEAT_WINDOWS) || defined(PROTO)
4822/*
4823 * Update the position for all windows, using the width and height of the
4824 * frames.
4825 * Returns the row just after the last window.
4826 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004827 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828win_comp_pos()
4829{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004830 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 int col = 0;
4832
4833 frame_comp_pos(topframe, &row, &col);
4834 return row;
4835}
4836
4837/*
4838 * Update the position of the windows in frame "topfrp", using the width and
4839 * height of the frames.
4840 * "*row" and "*col" are the top-left position of the frame. They are updated
4841 * to the bottom-right position plus one.
4842 */
4843 static void
4844frame_comp_pos(topfrp, row, col)
4845 frame_T *topfrp;
4846 int *row;
4847 int *col;
4848{
4849 win_T *wp;
4850 frame_T *frp;
4851#ifdef FEAT_VERTSPLIT
4852 int startcol;
4853 int startrow;
4854#endif
4855
4856 wp = topfrp->fr_win;
4857 if (wp != NULL)
4858 {
4859 if (wp->w_winrow != *row
4860#ifdef FEAT_VERTSPLIT
4861 || wp->w_wincol != *col
4862#endif
4863 )
4864 {
4865 /* position changed, redraw */
4866 wp->w_winrow = *row;
4867#ifdef FEAT_VERTSPLIT
4868 wp->w_wincol = *col;
4869#endif
4870 redraw_win_later(wp, NOT_VALID);
4871 wp->w_redr_status = TRUE;
4872 }
4873 *row += wp->w_height + wp->w_status_height;
4874#ifdef FEAT_VERTSPLIT
4875 *col += wp->w_width + wp->w_vsep_width;
4876#endif
4877 }
4878 else
4879 {
4880#ifdef FEAT_VERTSPLIT
4881 startrow = *row;
4882 startcol = *col;
4883#endif
4884 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4885 {
4886#ifdef FEAT_VERTSPLIT
4887 if (topfrp->fr_layout == FR_ROW)
4888 *row = startrow; /* all frames are at the same row */
4889 else
4890 *col = startcol; /* all frames are at the same col */
4891#endif
4892 frame_comp_pos(frp, row, col);
4893 }
4894 }
4895}
4896
4897#endif /* FEAT_WINDOWS */
4898
4899/*
4900 * Set current window height and take care of repositioning other windows to
4901 * fit around it.
4902 */
4903 void
4904win_setheight(height)
4905 int height;
4906{
4907 win_setheight_win(height, curwin);
4908}
4909
4910/*
4911 * Set the window height of window "win" and take care of repositioning other
4912 * windows to fit around it.
4913 */
4914 void
4915win_setheight_win(height, win)
4916 int height;
4917 win_T *win;
4918{
4919 int row;
4920
4921 if (win == curwin)
4922 {
4923 /* Always keep current window at least one line high, even when
4924 * 'winminheight' is zero. */
4925#ifdef FEAT_WINDOWS
4926 if (height < p_wmh)
4927 height = p_wmh;
4928#endif
4929 if (height == 0)
4930 height = 1;
4931 }
4932
4933#ifdef FEAT_WINDOWS
4934 frame_setheight(win->w_frame, height + win->w_status_height);
4935
4936 /* recompute the window positions */
4937 row = win_comp_pos();
4938#else
4939 if (height > topframe->fr_height)
4940 height = topframe->fr_height;
4941 win->w_height = height;
4942 row = height;
4943#endif
4944
4945 /*
4946 * If there is extra space created between the last window and the command
4947 * line, clear it.
4948 */
4949 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4950 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4951 cmdline_row = row;
4952 msg_row = row;
4953 msg_col = 0;
4954
4955 redraw_all_later(NOT_VALID);
4956}
4957
4958#if defined(FEAT_WINDOWS) || defined(PROTO)
4959
4960/*
4961 * Set the height of a frame to "height" and take care that all frames and
4962 * windows inside it are resized. Also resize frames on the left and right if
4963 * the are in the same FR_ROW frame.
4964 *
4965 * Strategy:
4966 * If the frame is part of a FR_COL frame, try fitting the frame in that
4967 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4968 * go to containing frames to resize them and make room.
4969 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4970 * Check for the minimal height of the FR_ROW frame.
4971 * At the top level we can also use change the command line height.
4972 */
4973 static void
4974frame_setheight(curfrp, height)
4975 frame_T *curfrp;
4976 int height;
4977{
4978 int room; /* total number of lines available */
4979 int take; /* number of lines taken from other windows */
4980 int room_cmdline; /* lines available from cmdline */
4981 int run;
4982 frame_T *frp;
4983 int h;
4984 int room_reserved;
4985
4986 /* If the height already is the desired value, nothing to do. */
4987 if (curfrp->fr_height == height)
4988 return;
4989
4990 if (curfrp->fr_parent == NULL)
4991 {
4992 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004993 if (height > ROWS_AVAIL)
4994 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (height > 0)
4996 frame_new_height(curfrp, height, FALSE, FALSE);
4997 }
4998 else if (curfrp->fr_parent->fr_layout == FR_ROW)
4999 {
5000 /* Row of frames: Also need to resize frames left and right of this
5001 * one. First check for the minimal height of these. */
5002 h = frame_minheight(curfrp->fr_parent, NULL);
5003 if (height < h)
5004 height = h;
5005 frame_setheight(curfrp->fr_parent, height);
5006 }
5007 else
5008 {
5009 /*
5010 * Column of frames: try to change only frames in this column.
5011 */
5012#ifdef FEAT_VERTSPLIT
5013 /*
5014 * Do this twice:
5015 * 1: compute room available, if it's not enough try resizing the
5016 * containing frame.
5017 * 2: compute the room available and adjust the height to it.
5018 * Try not to reduce the height of a window with 'winfixheight' set.
5019 */
5020 for (run = 1; run <= 2; ++run)
5021#else
5022 for (;;)
5023#endif
5024 {
5025 room = 0;
5026 room_reserved = 0;
5027 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5028 frp = frp->fr_next)
5029 {
5030 if (frp != curfrp
5031 && frp->fr_win != NULL
5032 && frp->fr_win->w_p_wfh)
5033 room_reserved += frp->fr_height;
5034 room += frp->fr_height;
5035 if (frp != curfrp)
5036 room -= frame_minheight(frp, NULL);
5037 }
5038#ifdef FEAT_VERTSPLIT
5039 if (curfrp->fr_width != Columns)
5040 room_cmdline = 0;
5041 else
5042#endif
5043 {
5044 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5045 + lastwin->w_height + lastwin->w_status_height);
5046 if (room_cmdline < 0)
5047 room_cmdline = 0;
5048 }
5049
5050 if (height <= room + room_cmdline)
5051 break;
5052#ifdef FEAT_VERTSPLIT
5053 if (run == 2 || curfrp->fr_width == Columns)
5054#endif
5055 {
5056 if (height > room + room_cmdline)
5057 height = room + room_cmdline;
5058 break;
5059 }
5060#ifdef FEAT_VERTSPLIT
5061 frame_setheight(curfrp->fr_parent, height
5062 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5063#endif
5064 /*NOTREACHED*/
5065 }
5066
5067 /*
5068 * Compute the number of lines we will take from others frames (can be
5069 * negative!).
5070 */
5071 take = height - curfrp->fr_height;
5072
5073 /* If there is not enough room, also reduce the height of a window
5074 * with 'winfixheight' set. */
5075 if (height > room + room_cmdline - room_reserved)
5076 room_reserved = room + room_cmdline - height;
5077 /* If there is only a 'winfixheight' window and making the
5078 * window smaller, need to make the other window taller. */
5079 if (take < 0 && room - curfrp->fr_height < room_reserved)
5080 room_reserved = 0;
5081
5082 if (take > 0 && room_cmdline > 0)
5083 {
5084 /* use lines from cmdline first */
5085 if (take < room_cmdline)
5086 room_cmdline = take;
5087 take -= room_cmdline;
5088 topframe->fr_height += room_cmdline;
5089 }
5090
5091 /*
5092 * set the current frame to the new height
5093 */
5094 frame_new_height(curfrp, height, FALSE, FALSE);
5095
5096 /*
5097 * First take lines from the frames after the current frame. If
5098 * that is not enough, takes lines from frames above the current
5099 * frame.
5100 */
5101 for (run = 0; run < 2; ++run)
5102 {
5103 if (run == 0)
5104 frp = curfrp->fr_next; /* 1st run: start with next window */
5105 else
5106 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5107 while (frp != NULL && take != 0)
5108 {
5109 h = frame_minheight(frp, NULL);
5110 if (room_reserved > 0
5111 && frp->fr_win != NULL
5112 && frp->fr_win->w_p_wfh)
5113 {
5114 if (room_reserved >= frp->fr_height)
5115 room_reserved -= frp->fr_height;
5116 else
5117 {
5118 if (frp->fr_height - room_reserved > take)
5119 room_reserved = frp->fr_height - take;
5120 take -= frp->fr_height - room_reserved;
5121 frame_new_height(frp, room_reserved, FALSE, FALSE);
5122 room_reserved = 0;
5123 }
5124 }
5125 else
5126 {
5127 if (frp->fr_height - take < h)
5128 {
5129 take -= frp->fr_height - h;
5130 frame_new_height(frp, h, FALSE, FALSE);
5131 }
5132 else
5133 {
5134 frame_new_height(frp, frp->fr_height - take,
5135 FALSE, FALSE);
5136 take = 0;
5137 }
5138 }
5139 if (run == 0)
5140 frp = frp->fr_next;
5141 else
5142 frp = frp->fr_prev;
5143 }
5144 }
5145 }
5146}
5147
5148#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5149/*
5150 * Set current window width and take care of repositioning other windows to
5151 * fit around it.
5152 */
5153 void
5154win_setwidth(width)
5155 int width;
5156{
5157 win_setwidth_win(width, curwin);
5158}
5159
5160 void
5161win_setwidth_win(width, wp)
5162 int width;
5163 win_T *wp;
5164{
5165 /* Always keep current window at least one column wide, even when
5166 * 'winminwidth' is zero. */
5167 if (wp == curwin)
5168 {
5169 if (width < p_wmw)
5170 width = p_wmw;
5171 if (width == 0)
5172 width = 1;
5173 }
5174
5175 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5176
5177 /* recompute the window positions */
5178 (void)win_comp_pos();
5179
5180 redraw_all_later(NOT_VALID);
5181}
5182
5183/*
5184 * Set the width of a frame to "width" and take care that all frames and
5185 * windows inside it are resized. Also resize frames above and below if the
5186 * are in the same FR_ROW frame.
5187 *
5188 * Strategy is similar to frame_setheight().
5189 */
5190 static void
5191frame_setwidth(curfrp, width)
5192 frame_T *curfrp;
5193 int width;
5194{
5195 int room; /* total number of lines available */
5196 int take; /* number of lines taken from other windows */
5197 int run;
5198 frame_T *frp;
5199 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005200 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201
5202 /* If the width already is the desired value, nothing to do. */
5203 if (curfrp->fr_width == width)
5204 return;
5205
5206 if (curfrp->fr_parent == NULL)
5207 /* topframe: can't change width */
5208 return;
5209
5210 if (curfrp->fr_parent->fr_layout == FR_COL)
5211 {
5212 /* Column of frames: Also need to resize frames above and below of
5213 * this one. First check for the minimal width of these. */
5214 w = frame_minwidth(curfrp->fr_parent, NULL);
5215 if (width < w)
5216 width = w;
5217 frame_setwidth(curfrp->fr_parent, width);
5218 }
5219 else
5220 {
5221 /*
5222 * Row of frames: try to change only frames in this row.
5223 *
5224 * Do this twice:
5225 * 1: compute room available, if it's not enough try resizing the
5226 * containing frame.
5227 * 2: compute the room available and adjust the width to it.
5228 */
5229 for (run = 1; run <= 2; ++run)
5230 {
5231 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005232 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5234 frp = frp->fr_next)
5235 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005236 if (frp != curfrp
5237 && frp->fr_win != NULL
5238 && frp->fr_win->w_p_wfw)
5239 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 room += frp->fr_width;
5241 if (frp != curfrp)
5242 room -= frame_minwidth(frp, NULL);
5243 }
5244
5245 if (width <= room)
5246 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005247 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
5249 if (width > room)
5250 width = room;
5251 break;
5252 }
5253 frame_setwidth(curfrp->fr_parent, width
5254 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5255 }
5256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 /*
5258 * Compute the number of lines we will take from others frames (can be
5259 * negative!).
5260 */
5261 take = width - curfrp->fr_width;
5262
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005263 /* If there is not enough room, also reduce the width of a window
5264 * with 'winfixwidth' set. */
5265 if (width > room - room_reserved)
5266 room_reserved = room - width;
5267 /* If there is only a 'winfixwidth' window and making the
5268 * window smaller, need to make the other window narrower. */
5269 if (take < 0 && room - curfrp->fr_width < room_reserved)
5270 room_reserved = 0;
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 /*
5273 * set the current frame to the new width
5274 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005275 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * First take lines from the frames right of the current frame. If
5279 * that is not enough, takes lines from frames left of the current
5280 * frame.
5281 */
5282 for (run = 0; run < 2; ++run)
5283 {
5284 if (run == 0)
5285 frp = curfrp->fr_next; /* 1st run: start with next window */
5286 else
5287 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5288 while (frp != NULL && take != 0)
5289 {
5290 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005291 if (room_reserved > 0
5292 && frp->fr_win != NULL
5293 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005295 if (room_reserved >= frp->fr_width)
5296 room_reserved -= frp->fr_width;
5297 else
5298 {
5299 if (frp->fr_width - room_reserved > take)
5300 room_reserved = frp->fr_width - take;
5301 take -= frp->fr_width - room_reserved;
5302 frame_new_width(frp, room_reserved, FALSE, FALSE);
5303 room_reserved = 0;
5304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306 else
5307 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005308 if (frp->fr_width - take < w)
5309 {
5310 take -= frp->fr_width - w;
5311 frame_new_width(frp, w, FALSE, FALSE);
5312 }
5313 else
5314 {
5315 frame_new_width(frp, frp->fr_width - take,
5316 FALSE, FALSE);
5317 take = 0;
5318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 }
5320 if (run == 0)
5321 frp = frp->fr_next;
5322 else
5323 frp = frp->fr_prev;
5324 }
5325 }
5326 }
5327}
5328#endif /* FEAT_VERTSPLIT */
5329
5330/*
5331 * Check 'winminheight' for a valid value.
5332 */
5333 void
5334win_setminheight()
5335{
5336 int room;
5337 int first = TRUE;
5338 win_T *wp;
5339
5340 /* loop until there is a 'winminheight' that is possible */
5341 while (p_wmh > 0)
5342 {
5343 /* TODO: handle vertical splits */
5344 room = -p_wh;
5345 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5346 room += wp->w_height - p_wmh;
5347 if (room >= 0)
5348 break;
5349 --p_wmh;
5350 if (first)
5351 {
5352 EMSG(_(e_noroom));
5353 first = FALSE;
5354 }
5355 }
5356}
5357
5358#ifdef FEAT_MOUSE
5359
5360/*
5361 * Status line of dragwin is dragged "offset" lines down (negative is up).
5362 */
5363 void
5364win_drag_status_line(dragwin, offset)
5365 win_T *dragwin;
5366 int offset;
5367{
5368 frame_T *curfr;
5369 frame_T *fr;
5370 int room;
5371 int row;
5372 int up; /* if TRUE, drag status line up, otherwise down */
5373 int n;
5374
5375 fr = dragwin->w_frame;
5376 curfr = fr;
5377 if (fr != topframe) /* more than one window */
5378 {
5379 fr = fr->fr_parent;
5380 /* When the parent frame is not a column of frames, its parent should
5381 * be. */
5382 if (fr->fr_layout != FR_COL)
5383 {
5384 curfr = fr;
5385 if (fr != topframe) /* only a row of windows, may drag statusline */
5386 fr = fr->fr_parent;
5387 }
5388 }
5389
5390 /* If this is the last frame in a column, may want to resize the parent
5391 * frame instead (go two up to skip a row of frames). */
5392 while (curfr != topframe && curfr->fr_next == NULL)
5393 {
5394 if (fr != topframe)
5395 fr = fr->fr_parent;
5396 curfr = fr;
5397 if (fr != topframe)
5398 fr = fr->fr_parent;
5399 }
5400
5401 if (offset < 0) /* drag up */
5402 {
5403 up = TRUE;
5404 offset = -offset;
5405 /* sum up the room of the current frame and above it */
5406 if (fr == curfr)
5407 {
5408 /* only one window */
5409 room = fr->fr_height - frame_minheight(fr, NULL);
5410 }
5411 else
5412 {
5413 room = 0;
5414 for (fr = fr->fr_child; ; fr = fr->fr_next)
5415 {
5416 room += fr->fr_height - frame_minheight(fr, NULL);
5417 if (fr == curfr)
5418 break;
5419 }
5420 }
5421 fr = curfr->fr_next; /* put fr at frame that grows */
5422 }
5423 else /* drag down */
5424 {
5425 up = FALSE;
5426 /*
5427 * Only dragging the last status line can reduce p_ch.
5428 */
5429 room = Rows - cmdline_row;
5430 if (curfr->fr_next == NULL)
5431 room -= 1;
5432 else
5433 room -= p_ch;
5434 if (room < 0)
5435 room = 0;
5436 /* sum up the room of frames below of the current one */
5437 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5438 room += fr->fr_height - frame_minheight(fr, NULL);
5439 fr = curfr; /* put fr at window that grows */
5440 }
5441
5442 if (room < offset) /* Not enough room */
5443 offset = room; /* Move as far as we can */
5444 if (offset <= 0)
5445 return;
5446
5447 /*
5448 * Grow frame fr by "offset" lines.
5449 * Doesn't happen when dragging the last status line up.
5450 */
5451 if (fr != NULL)
5452 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5453
5454 if (up)
5455 fr = curfr; /* current frame gets smaller */
5456 else
5457 fr = curfr->fr_next; /* next frame gets smaller */
5458
5459 /*
5460 * Now make the other frames smaller.
5461 */
5462 while (fr != NULL && offset > 0)
5463 {
5464 n = frame_minheight(fr, NULL);
5465 if (fr->fr_height - offset <= n)
5466 {
5467 offset -= fr->fr_height - n;
5468 frame_new_height(fr, n, !up, FALSE);
5469 }
5470 else
5471 {
5472 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5473 break;
5474 }
5475 if (up)
5476 fr = fr->fr_prev;
5477 else
5478 fr = fr->fr_next;
5479 }
5480 row = win_comp_pos();
5481 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5482 cmdline_row = row;
5483 p_ch = Rows - cmdline_row;
5484 if (p_ch < 1)
5485 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005486 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005487 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 showmode();
5489}
5490
5491#ifdef FEAT_VERTSPLIT
5492/*
5493 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5494 */
5495 void
5496win_drag_vsep_line(dragwin, offset)
5497 win_T *dragwin;
5498 int offset;
5499{
5500 frame_T *curfr;
5501 frame_T *fr;
5502 int room;
5503 int left; /* if TRUE, drag separator line left, otherwise right */
5504 int n;
5505
5506 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005507 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 return;
5509 curfr = fr;
5510 fr = fr->fr_parent;
5511 /* When the parent frame is not a row of frames, its parent should be. */
5512 if (fr->fr_layout != FR_ROW)
5513 {
5514 if (fr == topframe) /* only a column of windows (cannot happen?) */
5515 return;
5516 curfr = fr;
5517 fr = fr->fr_parent;
5518 }
5519
5520 /* If this is the last frame in a row, may want to resize a parent
5521 * frame instead. */
5522 while (curfr->fr_next == NULL)
5523 {
5524 if (fr == topframe)
5525 break;
5526 curfr = fr;
5527 fr = fr->fr_parent;
5528 if (fr != topframe)
5529 {
5530 curfr = fr;
5531 fr = fr->fr_parent;
5532 }
5533 }
5534
5535 if (offset < 0) /* drag left */
5536 {
5537 left = TRUE;
5538 offset = -offset;
5539 /* sum up the room of the current frame and left of it */
5540 room = 0;
5541 for (fr = fr->fr_child; ; fr = fr->fr_next)
5542 {
5543 room += fr->fr_width - frame_minwidth(fr, NULL);
5544 if (fr == curfr)
5545 break;
5546 }
5547 fr = curfr->fr_next; /* put fr at frame that grows */
5548 }
5549 else /* drag right */
5550 {
5551 left = FALSE;
5552 /* sum up the room of frames right of the current one */
5553 room = 0;
5554 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5555 room += fr->fr_width - frame_minwidth(fr, NULL);
5556 fr = curfr; /* put fr at window that grows */
5557 }
5558
5559 if (room < offset) /* Not enough room */
5560 offset = room; /* Move as far as we can */
5561 if (offset <= 0) /* No room at all, quit. */
5562 return;
5563
5564 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005565 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567 /* shrink other frames: current and at the left or at the right */
5568 if (left)
5569 fr = curfr; /* current frame gets smaller */
5570 else
5571 fr = curfr->fr_next; /* next frame gets smaller */
5572
5573 while (fr != NULL && offset > 0)
5574 {
5575 n = frame_minwidth(fr, NULL);
5576 if (fr->fr_width - offset <= n)
5577 {
5578 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005579 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
5581 else
5582 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005583 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 break;
5585 }
5586 if (left)
5587 fr = fr->fr_prev;
5588 else
5589 fr = fr->fr_next;
5590 }
5591 (void)win_comp_pos();
5592 redraw_all_later(NOT_VALID);
5593}
5594#endif /* FEAT_VERTSPLIT */
5595#endif /* FEAT_MOUSE */
5596
5597#endif /* FEAT_WINDOWS */
5598
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005599#define FRACTION_MULT 16384L
5600
5601/*
5602 * Set wp->w_fraction for the current w_wrow and w_height.
5603 */
5604 static void
5605set_fraction(wp)
5606 win_T *wp;
5607{
5608 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5609 + FRACTION_MULT / 2) / (long)wp->w_height;
5610}
5611
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612/*
5613 * Set the height of a window.
5614 * This takes care of the things inside the window, not what happens to the
5615 * window position, the frame or to other windows.
5616 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005617 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618win_new_height(wp, height)
5619 win_T *wp;
5620 int height;
5621{
5622 linenr_T lnum;
5623 int sline, line_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 /* Don't want a negative height. Happens when splitting a tiny window.
5626 * Will equalize heights soon to fix it. */
5627 if (height < 0)
5628 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005629 if (wp->w_height == height)
5630 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632 if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005633 set_fraction(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
5635 wp->w_height = height;
5636 wp->w_skipcol = 0;
5637
5638 /* Don't change w_topline when height is zero. Don't set w_topline when
5639 * 'scrollbind' is set and this isn't the current window. */
5640 if (height > 0
5641#ifdef FEAT_SCROLLBIND
5642 && (!wp->w_p_scb || wp == curwin)
5643#endif
5644 )
5645 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005646 /*
5647 * Find a value for w_topline that shows the cursor at the same
5648 * relative position in the window as before (more or less).
5649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 lnum = wp->w_cursor.lnum;
5651 if (lnum < 1) /* can happen when starting up */
5652 lnum = 1;
5653 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5654 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5655 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005656
5657 if (sline >= 0)
5658 {
5659 /* Make sure the whole cursor line is visible, if possible. */
5660 int rows = plines_win(wp, lnum, FALSE);
5661
5662 if (sline > wp->w_height - rows)
5663 {
5664 sline = wp->w_height - rows;
5665 wp->w_wrow -= rows - line_size;
5666 }
5667 }
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 if (sline < 0)
5670 {
5671 /*
5672 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005673 * Make cursor line the first line in the window. If not enough
5674 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 */
5676 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005677 if (wp->w_wrow >= wp->w_height
5678 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5679 {
5680 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5681 --wp->w_wrow;
5682 while (wp->w_wrow >= wp->w_height)
5683 {
5684 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5685 + win_col_off2(wp);
5686 --wp->w_wrow;
5687 }
5688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 else
5691 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005692 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
5694#ifdef FEAT_FOLDING
5695 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5696 if (lnum == 1)
5697 {
5698 /* first line in buffer is folded */
5699 line_size = 1;
5700 --sline;
5701 break;
5702 }
5703#endif
5704 --lnum;
5705#ifdef FEAT_DIFF
5706 if (lnum == wp->w_topline)
5707 line_size = plines_win_nofill(wp, lnum, TRUE)
5708 + wp->w_topfill;
5709 else
5710#endif
5711 line_size = plines_win(wp, lnum, TRUE);
5712 sline -= line_size;
5713 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005714
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 if (sline < 0)
5716 {
5717 /*
5718 * Line we want at top would go off top of screen. Use next
5719 * line instead.
5720 */
5721#ifdef FEAT_FOLDING
5722 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5723#endif
5724 lnum++;
5725 wp->w_wrow -= line_size + sline;
5726 }
5727 else if (sline > 0)
5728 {
5729 /* First line of file reached, use that as topline. */
5730 lnum = 1;
5731 wp->w_wrow -= sline;
5732 }
5733 }
5734 set_topline(wp, lnum);
5735 }
5736
5737 if (wp == curwin)
5738 {
5739 if (p_so)
5740 update_topline();
5741 curs_columns(FALSE); /* validate w_wrow */
5742 }
5743 wp->w_prev_fraction_row = wp->w_wrow;
5744
5745 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005746 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747#ifdef FEAT_WINDOWS
5748 wp->w_redr_status = TRUE;
5749#endif
5750 invalidate_botline_win(wp);
5751}
5752
5753#ifdef FEAT_VERTSPLIT
5754/*
5755 * Set the width of a window.
5756 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005757 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758win_new_width(wp, width)
5759 win_T *wp;
5760 int width;
5761{
5762 wp->w_width = width;
5763 wp->w_lines_valid = 0;
5764 changed_line_abv_curs_win(wp);
5765 invalidate_botline_win(wp);
5766 if (wp == curwin)
5767 {
5768 update_topline();
5769 curs_columns(TRUE); /* validate w_wrow */
5770 }
5771 redraw_win_later(wp, NOT_VALID);
5772 wp->w_redr_status = TRUE;
5773}
5774#endif
5775
5776 void
5777win_comp_scroll(wp)
5778 win_T *wp;
5779{
5780 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5781 if (wp->w_p_scr == 0)
5782 wp->w_p_scr = 1;
5783}
5784
5785/*
5786 * command_height: called whenever p_ch has been changed
5787 */
5788 void
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005789command_height()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791#ifdef FEAT_WINDOWS
5792 int h;
5793 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005794 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005796 /* Use the value of p_ch that we remembered. This is needed for when the
5797 * GUI starts up, we can't be sure in what order things happen. And when
5798 * p_ch was changed in another tab page. */
5799 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 /* Find bottom frame with width of screen. */
5802 frp = lastwin->w_frame;
5803# ifdef FEAT_VERTSPLIT
5804 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5805 frp = frp->fr_parent;
5806# endif
5807
5808 /* Avoid changing the height of a window with 'winfixheight' set. */
5809 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5810 && frp->fr_win->w_p_wfh)
5811 frp = frp->fr_prev;
5812
5813 if (starting != NO_SCREEN)
5814 {
5815 cmdline_row = Rows - p_ch;
5816
5817 if (p_ch > old_p_ch) /* p_ch got bigger */
5818 {
5819 while (p_ch > old_p_ch)
5820 {
5821 if (frp == NULL)
5822 {
5823 EMSG(_(e_noroom));
5824 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005825 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 cmdline_row = Rows - p_ch;
5827 break;
5828 }
5829 h = frp->fr_height - frame_minheight(frp, NULL);
5830 if (h > p_ch - old_p_ch)
5831 h = p_ch - old_p_ch;
5832 old_p_ch += h;
5833 frame_add_height(frp, -h);
5834 frp = frp->fr_prev;
5835 }
5836
5837 /* Recompute window positions. */
5838 (void)win_comp_pos();
5839
5840 /* clear the lines added to cmdline */
5841 if (full_screen)
5842 screen_fill((int)(cmdline_row), (int)Rows, 0,
5843 (int)Columns, ' ', ' ', 0);
5844 msg_row = cmdline_row;
5845 redraw_cmdline = TRUE;
5846 return;
5847 }
5848
5849 if (msg_row < cmdline_row)
5850 msg_row = cmdline_row;
5851 redraw_cmdline = TRUE;
5852 }
5853 frame_add_height(frp, (int)(old_p_ch - p_ch));
5854
5855 /* Recompute window positions. */
5856 if (frp != lastwin->w_frame)
5857 (void)win_comp_pos();
5858#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005860 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861#endif
5862}
5863
5864#if defined(FEAT_WINDOWS) || defined(PROTO)
5865/*
5866 * Resize frame "frp" to be "n" lines higher (negative for less high).
5867 * Also resize the frames it is contained in.
5868 */
5869 static void
5870frame_add_height(frp, n)
5871 frame_T *frp;
5872 int n;
5873{
5874 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5875 for (;;)
5876 {
5877 frp = frp->fr_parent;
5878 if (frp == NULL)
5879 break;
5880 frp->fr_height += n;
5881 }
5882}
5883
5884/*
5885 * Add or remove a status line for the bottom window(s), according to the
5886 * value of 'laststatus'.
5887 */
5888 void
5889last_status(morewin)
5890 int morewin; /* pretend there are two or more windows */
5891{
5892 /* Don't make a difference between horizontal or vertical split. */
5893 last_status_rec(topframe, (p_ls == 2
5894 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5895}
5896
5897 static void
5898last_status_rec(fr, statusline)
5899 frame_T *fr;
5900 int statusline;
5901{
5902 frame_T *fp;
5903 win_T *wp;
5904
5905 if (fr->fr_layout == FR_LEAF)
5906 {
5907 wp = fr->fr_win;
5908 if (wp->w_status_height != 0 && !statusline)
5909 {
5910 /* remove status line */
5911 win_new_height(wp, wp->w_height + 1);
5912 wp->w_status_height = 0;
5913 comp_col();
5914 }
5915 else if (wp->w_status_height == 0 && statusline)
5916 {
5917 /* Find a frame to take a line from. */
5918 fp = fr;
5919 while (fp->fr_height <= frame_minheight(fp, NULL))
5920 {
5921 if (fp == topframe)
5922 {
5923 EMSG(_(e_noroom));
5924 return;
5925 }
5926 /* In a column of frames: go to frame above. If already at
5927 * the top or in a row of frames: go to parent. */
5928 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5929 fp = fp->fr_prev;
5930 else
5931 fp = fp->fr_parent;
5932 }
5933 wp->w_status_height = 1;
5934 if (fp != fr)
5935 {
5936 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5937 frame_fix_height(wp);
5938 (void)win_comp_pos();
5939 }
5940 else
5941 win_new_height(wp, wp->w_height - 1);
5942 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005943 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 }
5945 }
5946#ifdef FEAT_VERTSPLIT
5947 else if (fr->fr_layout == FR_ROW)
5948 {
5949 /* vertically split windows, set status line for each one */
5950 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5951 last_status_rec(fp, statusline);
5952 }
5953#endif
5954 else
5955 {
5956 /* horizontally split window, set status line for last one */
5957 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5958 ;
5959 last_status_rec(fp, statusline);
5960 }
5961}
5962
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005963/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005964 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005965 */
5966 int
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005967tabline_height()
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005968{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005969#ifdef FEAT_GUI_TABLINE
5970 /* When the GUI has the tabline then this always returns zero. */
5971 if (gui_use_tabline())
5972 return 0;
5973#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005974 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005975 {
5976 case 0: return 0;
5977 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5978 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005979 return 1;
5980}
5981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982#endif /* FEAT_WINDOWS */
5983
5984#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5985/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005986 * Get the file name at the cursor.
5987 * If Visual mode is active, use the selected text if it's in one line.
5988 * Returns the name in allocated memory, NULL for failure.
5989 */
5990 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005991grab_file_name(count, file_lnum)
5992 long count;
5993 linenr_T *file_lnum;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005994{
5995# ifdef FEAT_VISUAL
5996 if (VIsual_active)
5997 {
5998 int len;
5999 char_u *ptr;
6000
6001 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6002 return NULL;
6003 return find_file_name_in_path(ptr, len,
6004 FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
6005 }
6006# endif
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006007 return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
6008 file_lnum);
6009
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006010}
6011
6012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 * Return the file name under or after the cursor.
6014 *
6015 * The 'path' option is searched if the file name is not absolute.
6016 * The string returned has been alloc'ed and should be freed by the caller.
6017 * NULL is returned if the file name or file is not found.
6018 *
6019 * options:
6020 * FNAME_MESS give error messages
6021 * FNAME_EXP expand to path
6022 * FNAME_HYP check for hypertext link
6023 * FNAME_INCL apply "includeexpr"
6024 */
6025 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006026file_name_at_cursor(options, count, file_lnum)
6027 int options;
6028 long count;
6029 linenr_T *file_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030{
6031 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006032 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6033 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034}
6035
6036/*
6037 * Return the name of the file under or after ptr[col].
6038 * Otherwise like file_name_at_cursor().
6039 */
6040 char_u *
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006041file_name_in_line(line, col, options, count, rel_fname, file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 char_u *line;
6043 int col;
6044 int options;
6045 long count;
6046 char_u *rel_fname; /* file we are searching relative to */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006047 linenr_T *file_lnum; /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
6049 char_u *ptr;
6050 int len;
6051
6052 /*
6053 * search forward for what could be the start of a file name
6054 */
6055 ptr = line + col;
6056 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006057 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 if (*ptr == NUL) /* nothing found */
6059 {
6060 if (options & FNAME_MESS)
6061 EMSG(_("E446: No file name under cursor"));
6062 return NULL;
6063 }
6064
6065 /*
6066 * Search backward for first char of the file name.
6067 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6068 */
6069 while (ptr > line)
6070 {
6071#ifdef FEAT_MBYTE
6072 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6073 ptr -= len + 1;
6074 else
6075#endif
6076 if (vim_isfilec(ptr[-1])
6077 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6078 --ptr;
6079 else
6080 break;
6081 }
6082
6083 /*
6084 * Search forward for the last char of the file name.
6085 * Also allow "://" when ':' is not in 'isfname'.
6086 */
6087 len = 0;
6088 while (vim_isfilec(ptr[len])
6089 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
6090#ifdef FEAT_MBYTE
6091 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006092 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 else
6094#endif
6095 ++len;
6096
6097 /*
6098 * If there is trailing punctuation, remove it.
6099 * But don't remove "..", could be a directory name.
6100 */
6101 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6102 && ptr[len - 2] != '.')
6103 --len;
6104
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006105 if (file_lnum != NULL)
6106 {
6107 char_u *p;
6108
6109 /* Get the number after the file name and a separator character */
6110 p = ptr + len;
6111 p = skipwhite(p);
6112 if (*p != NUL)
6113 {
6114 if (!isdigit(*p))
6115 ++p; /* skip the separator */
6116 p = skipwhite(p);
6117 if (isdigit(*p))
6118 *file_lnum = (int)getdigits(&p);
6119 }
6120 }
6121
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6123}
6124
6125# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6126static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
6127
6128 static char_u *
6129eval_includeexpr(ptr, len)
6130 char_u *ptr;
6131 int len;
6132{
6133 char_u *res;
6134
6135 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006136 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006137 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 set_vim_var_string(VV_FNAME, NULL, 0);
6139 return res;
6140}
6141#endif
6142
6143/*
6144 * Return the name of the file ptr[len] in 'path'.
6145 * Otherwise like file_name_at_cursor().
6146 */
6147 char_u *
6148find_file_name_in_path(ptr, len, options, count, rel_fname)
6149 char_u *ptr;
6150 int len;
6151 int options;
6152 long count;
6153 char_u *rel_fname; /* file we are searching relative to */
6154{
6155 char_u *file_name;
6156 int c;
6157# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6158 char_u *tofree = NULL;
6159
6160 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6161 {
6162 tofree = eval_includeexpr(ptr, len);
6163 if (tofree != NULL)
6164 {
6165 ptr = tofree;
6166 len = (int)STRLEN(ptr);
6167 }
6168 }
6169# endif
6170
6171 if (options & FNAME_EXP)
6172 {
6173 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6174 TRUE, rel_fname);
6175
6176# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6177 /*
6178 * If the file could not be found in a normal way, try applying
6179 * 'includeexpr' (unless done already).
6180 */
6181 if (file_name == NULL
6182 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6183 {
6184 tofree = eval_includeexpr(ptr, len);
6185 if (tofree != NULL)
6186 {
6187 ptr = tofree;
6188 len = (int)STRLEN(ptr);
6189 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6190 TRUE, rel_fname);
6191 }
6192 }
6193# endif
6194 if (file_name == NULL && (options & FNAME_MESS))
6195 {
6196 c = ptr[len];
6197 ptr[len] = NUL;
6198 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6199 ptr[len] = c;
6200 }
6201
6202 /* Repeat finding the file "count" times. This matters when it
6203 * appears several times in the path. */
6204 while (file_name != NULL && --count > 0)
6205 {
6206 vim_free(file_name);
6207 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6208 }
6209 }
6210 else
6211 file_name = vim_strnsave(ptr, len);
6212
6213# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6214 vim_free(tofree);
6215# endif
6216
6217 return file_name;
6218}
6219#endif /* FEAT_SEARCHPATH */
6220
6221/*
6222 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6223 * Also check for ":\\", which MS Internet Explorer accepts, return
6224 * URL_BACKSLASH.
6225 */
6226 static int
6227path_is_url(p)
6228 char_u *p;
6229{
6230 if (STRNCMP(p, "://", (size_t)3) == 0)
6231 return URL_SLASH;
6232 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6233 return URL_BACKSLASH;
6234 return 0;
6235}
6236
6237/*
6238 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6239 * Return URL_BACKSLASH for "name:\\".
6240 * Return zero otherwise.
6241 */
6242 int
6243path_with_url(fname)
6244 char_u *fname;
6245{
6246 char_u *p;
6247
6248 for (p = fname; isalpha(*p); ++p)
6249 ;
6250 return path_is_url(p);
6251}
6252
6253/*
6254 * Return TRUE if "name" is a full (absolute) path name or URL.
6255 */
6256 int
6257vim_isAbsName(name)
6258 char_u *name;
6259{
6260 return (path_with_url(name) != 0 || mch_isFullName(name));
6261}
6262
6263/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006264 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 *
6266 * return FAIL for failure, OK otherwise
6267 */
6268 int
6269vim_FullName(fname, buf, len, force)
6270 char_u *fname, *buf;
6271 int len;
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006272 int force; /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 int retval = OK;
6275 int url;
6276
6277 *buf = NUL;
6278 if (fname == NULL)
6279 return FAIL;
6280
6281 url = path_with_url(fname);
6282 if (!url)
6283 retval = mch_FullName(fname, buf, len, force);
6284 if (url || retval == FAIL)
6285 {
6286 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006287 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289#if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
6290 slash_adjust(buf);
6291#endif
6292 return retval;
6293}
6294
6295/*
6296 * Return the minimal number of rows that is needed on the screen to display
6297 * the current number of windows.
6298 */
6299 int
6300min_rows()
6301{
6302 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006303#ifdef FEAT_WINDOWS
6304 tabpage_T *tp;
6305 int n;
6306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
6308 if (firstwin == NULL) /* not initialized yet */
6309 return MIN_LINES;
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006312 total = 0;
6313 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6314 {
6315 n = frame_minheight(tp->tp_topframe, NULL);
6316 if (total < n)
6317 total = n;
6318 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006319 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006321 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006323 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 return total;
6325}
6326
6327/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006328 * Return TRUE if there is only one window (in the current tab page), not
6329 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006330 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 */
6332 int
6333only_one_window()
6334{
6335#ifdef FEAT_WINDOWS
6336 int count = 0;
6337 win_T *wp;
6338
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006339 /* If there is another tab page there always is another window. */
6340 if (first_tabpage->tp_next != NULL)
6341 return FALSE;
6342
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006344 if (wp->w_buffer != NULL
6345 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346# ifdef FEAT_QUICKFIX
6347 || wp->w_p_pvw
6348# endif
6349 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006350# ifdef FEAT_AUTOCMD
6351 && wp != aucmd_win
6352# endif
6353 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 ++count;
6355 return (count <= 1);
6356#else
6357 return TRUE;
6358#endif
6359}
6360
6361#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6362/*
6363 * Correct the cursor line number in other windows. Used after changing the
6364 * current buffer, and before applying autocommands.
6365 * When "do_curwin" is TRUE, also check current window.
6366 */
6367 void
6368check_lnums(do_curwin)
6369 int do_curwin;
6370{
6371 win_T *wp;
6372
6373#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006374 tabpage_T *tp;
6375
6376 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6378#else
6379 wp = curwin;
6380 if (do_curwin)
6381#endif
6382 {
6383 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6384 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6385 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6386 wp->w_topline = curbuf->b_ml.ml_line_count;
6387 }
6388}
6389#endif
6390
6391#if defined(FEAT_WINDOWS) || defined(PROTO)
6392
6393/*
6394 * A snapshot of the window sizes, to restore them after closing the help
6395 * window.
6396 * Only these fields are used:
6397 * fr_layout
6398 * fr_width
6399 * fr_height
6400 * fr_next
6401 * fr_child
6402 * fr_win (only valid for the old curwin, NULL otherwise)
6403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405/*
6406 * Create a snapshot of the current frame sizes.
6407 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006408 void
6409make_snapshot(idx)
6410 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006412 clear_snapshot(curtab, idx);
6413 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415
6416 static void
6417make_snapshot_rec(fr, frp)
6418 frame_T *fr;
6419 frame_T **frp;
6420{
6421 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6422 if (*frp == NULL)
6423 return;
6424 (*frp)->fr_layout = fr->fr_layout;
6425# ifdef FEAT_VERTSPLIT
6426 (*frp)->fr_width = fr->fr_width;
6427# endif
6428 (*frp)->fr_height = fr->fr_height;
6429 if (fr->fr_next != NULL)
6430 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6431 if (fr->fr_child != NULL)
6432 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6433 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6434 (*frp)->fr_win = curwin;
6435}
6436
6437/*
6438 * Remove any existing snapshot.
6439 */
6440 static void
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006441clear_snapshot(tp, idx)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006442 tabpage_T *tp;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006443 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006445 clear_snapshot_rec(tp->tp_snapshot[idx]);
6446 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447}
6448
6449 static void
6450clear_snapshot_rec(fr)
6451 frame_T *fr;
6452{
6453 if (fr != NULL)
6454 {
6455 clear_snapshot_rec(fr->fr_next);
6456 clear_snapshot_rec(fr->fr_child);
6457 vim_free(fr);
6458 }
6459}
6460
6461/*
6462 * Restore a previously created snapshot, if there is any.
6463 * This is only done if the screen size didn't change and the window layout is
6464 * still the same.
6465 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006466 void
6467restore_snapshot(idx, close_curwin)
6468 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 int close_curwin; /* closing current window */
6470{
6471 win_T *wp;
6472
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006473 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006475 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006477 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6478 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006480 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 win_comp_pos();
6482 if (wp != NULL && close_curwin)
6483 win_goto(wp);
6484 redraw_all_later(CLEAR);
6485 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006486 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487}
6488
6489/*
6490 * Check if frames "sn" and "fr" have the same layout, same following frames
6491 * and same children.
6492 */
6493 static int
6494check_snapshot_rec(sn, fr)
6495 frame_T *sn;
6496 frame_T *fr;
6497{
6498 if (sn->fr_layout != fr->fr_layout
6499 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6500 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6501 || (sn->fr_next != NULL
6502 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6503 || (sn->fr_child != NULL
6504 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6505 return FAIL;
6506 return OK;
6507}
6508
6509/*
6510 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6511 * following frames and children.
6512 * Returns a pointer to the old current window, or NULL.
6513 */
6514 static win_T *
6515restore_snapshot_rec(sn, fr)
6516 frame_T *sn;
6517 frame_T *fr;
6518{
6519 win_T *wp = NULL;
6520 win_T *wp2;
6521
6522 fr->fr_height = sn->fr_height;
6523# ifdef FEAT_VERTSPLIT
6524 fr->fr_width = sn->fr_width;
6525# endif
6526 if (fr->fr_layout == FR_LEAF)
6527 {
6528 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6529# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006530 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531# endif
6532 wp = sn->fr_win;
6533 }
6534 if (sn->fr_next != NULL)
6535 {
6536 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6537 if (wp2 != NULL)
6538 wp = wp2;
6539 }
6540 if (sn->fr_child != NULL)
6541 {
6542 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6543 if (wp2 != NULL)
6544 wp = wp2;
6545 }
6546 return wp;
6547}
6548
6549#endif
6550
6551#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6552/*
6553 * Return TRUE if there is any vertically split window.
6554 */
6555 int
6556win_hasvertsplit()
6557{
6558 frame_T *fr;
6559
6560 if (topframe->fr_layout == FR_ROW)
6561 return TRUE;
6562
6563 if (topframe->fr_layout == FR_COL)
6564 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6565 if (fr->fr_layout == FR_ROW)
6566 return TRUE;
6567
6568 return FALSE;
6569}
6570#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006571
6572#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6573/*
6574 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006575 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006576 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6577 * If no particular ID is desired, -1 must be specified for 'id'.
6578 * Return ID of added match, -1 on failure.
6579 */
6580 int
6581match_add(wp, grp, pat, prio, id)
6582 win_T *wp;
6583 char_u *grp;
6584 char_u *pat;
6585 int prio;
6586 int id;
6587{
6588 matchitem_T *cur;
6589 matchitem_T *prev;
6590 matchitem_T *m;
6591 int hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006592 regprog_T *regprog;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006593
6594 if (*grp == NUL || *pat == NUL)
6595 return -1;
6596 if (id < -1 || id == 0)
6597 {
6598 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6599 return -1;
6600 }
6601 if (id != -1)
6602 {
6603 cur = wp->w_match_head;
6604 while (cur != NULL)
6605 {
6606 if (cur->id == id)
6607 {
6608 EMSGN("E801: ID already taken: %ld", id);
6609 return -1;
6610 }
6611 cur = cur->next;
6612 }
6613 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006614 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006615 {
6616 EMSG2(_(e_nogroup), grp);
6617 return -1;
6618 }
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006619 if ((regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006620 {
6621 EMSG2(_(e_invarg2), pat);
6622 return -1;
6623 }
6624
6625 /* Find available match ID. */
6626 while (id == -1)
6627 {
6628 cur = wp->w_match_head;
6629 while (cur != NULL && cur->id != wp->w_next_match_id)
6630 cur = cur->next;
6631 if (cur == NULL)
6632 id = wp->w_next_match_id;
6633 wp->w_next_match_id++;
6634 }
6635
6636 /* Build new match. */
6637 m = (matchitem_T *)alloc(sizeof(matchitem_T));
6638 m->id = id;
6639 m->priority = prio;
6640 m->pattern = vim_strsave(pat);
6641 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006642 m->match.regprog = regprog;
6643 m->match.rmm_ic = FALSE;
6644 m->match.rmm_maxcol = 0;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006645
6646 /* Insert new match. The match list is in ascending order with regard to
6647 * the match priorities. */
6648 cur = wp->w_match_head;
6649 prev = cur;
6650 while (cur != NULL && prio >= cur->priority)
6651 {
6652 prev = cur;
6653 cur = cur->next;
6654 }
6655 if (cur == prev)
6656 wp->w_match_head = m;
6657 else
6658 prev->next = m;
6659 m->next = cur;
6660
6661 redraw_later(SOME_VALID);
6662 return id;
6663}
6664
6665/*
6666 * Delete match with ID 'id' in the match list of window 'wp'.
6667 * Print error messages if 'perr' is TRUE.
6668 */
6669 int
6670match_delete(wp, id, perr)
6671 win_T *wp;
6672 int id;
6673 int perr;
6674{
6675 matchitem_T *cur = wp->w_match_head;
6676 matchitem_T *prev = cur;
6677
6678 if (id < 1)
6679 {
6680 if (perr == TRUE)
6681 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6682 id);
6683 return -1;
6684 }
6685 while (cur != NULL && cur->id != id)
6686 {
6687 prev = cur;
6688 cur = cur->next;
6689 }
6690 if (cur == NULL)
6691 {
6692 if (perr == TRUE)
6693 EMSGN("E803: ID not found: %ld", id);
6694 return -1;
6695 }
6696 if (cur == prev)
6697 wp->w_match_head = cur->next;
6698 else
6699 prev->next = cur->next;
6700 vim_free(cur->match.regprog);
6701 vim_free(cur->pattern);
6702 vim_free(cur);
6703 redraw_later(SOME_VALID);
6704 return 0;
6705}
6706
6707/*
6708 * Delete all matches in the match list of window 'wp'.
6709 */
6710 void
6711clear_matches(wp)
6712 win_T *wp;
6713{
6714 matchitem_T *m;
6715
6716 while (wp->w_match_head != NULL)
6717 {
6718 m = wp->w_match_head->next;
6719 vim_free(wp->w_match_head->match.regprog);
6720 vim_free(wp->w_match_head->pattern);
6721 vim_free(wp->w_match_head);
6722 wp->w_match_head = m;
6723 }
6724 redraw_later(SOME_VALID);
6725}
6726
6727/*
6728 * Get match from ID 'id' in window 'wp'.
6729 * Return NULL if match not found.
6730 */
6731 matchitem_T *
6732get_match(wp, id)
6733 win_T *wp;
6734 int id;
6735{
6736 matchitem_T *cur = wp->w_match_head;
6737
6738 while (cur != NULL && cur->id != id)
6739 cur = cur->next;
6740 return cur;
6741}
6742#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006743
6744#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6745 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006746get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006747{
6748 int i = 1;
6749 win_T *w;
6750
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006751 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006752 ++i;
6753
6754 if (w == NULL)
6755 return 0;
6756 else
6757 return i;
6758}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006759
6760 int
6761get_tab_number(tabpage_T *tp)
6762{
6763 int i = 1;
6764 tabpage_T *t;
6765
6766 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6767 ++i;
6768
6769 if (t == NULL)
6770 return 0;
6771 else
6772 return i;
6773}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006774#endif